Debian Codegraph @

lib/common/font-manager-orthographies.c

raw
1 /* font-manager-orthography.c
2 *
3 * Copyright (C) 2009-2022 Jerry Casiano
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.
17 *
18 * If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
19 */
20
21 #include "font-manager-orthographies.h"
22
23 /**
24 * SECTION: font-manager-orthographies
25 * @short_description: Font language support
26 * @title: Orthographies
27 * @include: font-manager-orthographies.h
28 */
29
30 #define N_ARABIC G_N_ELEMENTS(ArabicOrthographies)
31 #define N_CHINESE G_N_ELEMENTS(ChineseOrthographies)
32 #define N_GREEK G_N_ELEMENTS(GreekOrthographies)
33 #define N_JAPANESE G_N_ELEMENTS(JapaneseOrthographies)
34 #define N_KOREAN G_N_ELEMENTS(KoreanOrthographies)
35 #define N_LATIN G_N_ELEMENTS(LatinOrthographies)
36 #define N_MISC G_N_ELEMENTS(UncategorizedOrthographies)
37
38 #define GET_OBJECT(n) json_node_get_object((JsonNode *) n)
39 #define HAS_COVERAGE(n) JSON_NODE_HOLDS_OBJECT((JsonNode *) n) && json_object_has_member(GET_OBJECT(n), "coverage")
40 #define GET_COVERAGE(n) HAS_COVERAGE(n) ? json_object_get_double_member(GET_OBJECT(n), "coverage") : 0.0
41 #define LEN_CHARSET(n) json_array_get_length(json_object_get_array_member(GET_OBJECT(n), "filter"))
42
43 static GList *
44 _hb_set_to_list (const hb_set_t *charset)
45 {
46 GList *result = NULL;
47 hb_codepoint_t codepoint = HB_SET_VALUE_INVALID;
48 while (hb_set_next(charset, &codepoint))
49 if (font_manager_unicode_unichar_isgraph(codepoint))
50 result = g_list_prepend(result, GINT_TO_POINTER(codepoint));
51 return g_list_reverse(result);
52 }
53
54 static JsonArray *
55 _hb_set_to_json_array (const hb_set_t *charset)
56 {
57 JsonArray *result = json_array_new();
58 hb_codepoint_t codepoint = HB_SET_VALUE_INVALID;
59 while (hb_set_next(charset, &codepoint))
60 if (font_manager_unicode_unichar_isgraph(codepoint))
61 json_array_add_int_element(result, codepoint);
62 return result;
63 }
64
65 static hb_set_t *
66 get_charset_from_font_object (JsonObject *font)
67 {
68 hb_blob_t *blob = hb_blob_create_from_file(json_object_get_string_member(font, "filepath"));
69 hb_face_t *face = hb_face_create(blob, json_object_get_int_member(font, "findex"));
70 hb_set_t *charset = hb_set_create();
71 hb_face_collect_unicodes(face, charset);
72 hb_blob_destroy(blob);
73 hb_face_destroy(face);
74 return charset;
75 }
76
77 static gint
78 sort_by_charset_size (gconstpointer a, gconstpointer b)
79 {
80 /* Using variables to avoid unused value warning */
81 gint len_a = LEN_CHARSET(a);
82 gint len_b = LEN_CHARSET(b);
83 return len_a - len_b;
84 }
85
86 static gint
87 sort_by_coverage (gconstpointer a, gconstpointer b)
88 {
89 gint order = (int) GET_COVERAGE(a) - GET_COVERAGE(b);
90 return order != 0 ? order : sort_by_charset_size(a, b);
91 }
92
93 static gchar *
94 get_sample_from_charlist (GList *charlist)
95 {
96 GString *res = g_string_new(NULL);
97 guint length = g_list_length(charlist);
98 if (length > 0)
99 for (int i = 0; i < 24; i++) {
100 int rand = g_random_int_range(0, length);
101 gunichar ch = GPOINTER_TO_INT(g_list_nth_data(charlist, rand));
102 g_string_append_unichar(res, ch);
103 }
104 return g_string_free(res, FALSE);
105 }
106
107 static gchar *
108 get_sample_from_charset (hb_set_t *charset)
109 {
110 GList *charlist = _hb_set_to_list(charset);
111 gchar *res = get_sample_from_charlist(charlist);
112 g_list_free(charlist);
113 return res;
114 }
115
116 static JsonObject *
117 get_default_orthography (JsonObject *orthography)
118 {
119 GList *orthographies = json_object_get_values(orthography);
120 JsonObject *res = NULL;
121 if (g_list_length(orthographies) > 0) {
122 orthographies = g_list_sort(orthographies, sort_by_coverage);
123 JsonNode *node = g_list_nth_data(orthographies, 0);
124 if (node && json_node_get_node_type(node) == JSON_NODE_OBJECT)
125 res = json_node_get_object(node);
126 }
127 g_list_free(orthographies);
128 return res;
129 }
130
131 static double
132 get_coverage_from_charset (JsonObject *results,
133 hb_set_t *charset,
134 const FontManagerOrthographyData *data)
135 {
136 int hits = 0, tries = 0;
137 JsonArray *filter = NULL;
138
139 /* If it doesn't contain key there's no point in going further */
140 if (!hb_set_has(charset, data->key))
141 return 0;
142
143 if (results)
144 filter = json_array_new();
145
146 for (int i = 0; data->values[i] != FONT_MANAGER_END_OF_DATA; i++) {
147
148 if (data->values[i] == FONT_MANAGER_START_RANGE_PAIR) {
149
150 gunichar start = data->values[++i];
151 gunichar end = data->values[++i];
152
153 for (gunichar codepoint = start; codepoint <= end; codepoint++) {
154 tries++;
155 if (hb_set_has(charset, codepoint))
156 hits++;
157 if (results)
158 json_array_add_int_element(filter, (int) codepoint);
159 }
160
161 } else {
162
163 tries++;
164 if (hb_set_has(charset, data->values[i]))
165 hits++;
166 if (results)
167 json_array_add_int_element(filter, (int) data->values[i]);
168
169 }
170
171 }
172
173 if (results)
174 json_object_set_array_member(results, "filter", filter);
175
176 return ((double) 100 * hits/tries );
177 }
178
179 static gboolean
180 check_orthography (JsonObject *results,
181 hb_set_t *charset,
182 const FontManagerOrthographyData *data)
183 {
184 g_autoptr(JsonObject) res = NULL;
185 if (results)
186 res = json_object_new();
187 double coverage = get_coverage_from_charset(res, charset, data);
188 if (coverage == 0)
189 return FALSE;
190 if (!results)
191 return TRUE;
192 json_object_set_string_member(res, "name", data->name);
193 json_object_set_string_member(res, "native", data->native);
194 json_object_set_string_member(res, "sample", data->sample);
195 json_object_set_double_member(res, "coverage", coverage);
196 json_object_set_object_member(results, data->name, json_object_ref(res));
197 return TRUE;
198 }
199
200 static gboolean
201 charset_contains_sample_string (const hb_set_t *charset, const char *sample)
202 {
203 const char *p = sample;
204 while (*p) {
205 gunichar codepoint = g_utf8_get_char(p);
206 if (!hb_set_has(charset, codepoint))
207 return FALSE;
208 p = g_utf8_next_char(p);
209 }
210 return TRUE;
211 }
212
213 static void
214 check_orthographies (JsonObject *results,
215 hb_set_t *charset,
216 const FontManagerOrthographyData orth[],
217 int len)
218 {
219 for (int i = 0; i < len; i++)
220 check_orthography(results, charset, &orth[i]);
221 return;
222 }
223
224 static gchar *
225 get_default_sample_string_for_orthography (JsonObject *orthography)
226 {
227 if (json_object_get_size(orthography) > 0) {
228 JsonObject *def = get_default_orthography(orthography);
229 if (def && json_object_get_double_member(def, "coverage") > 90) {
230 const gchar *sample = NULL;
231 if (json_object_has_member(orthography, "sample"))
232 sample = json_object_get_string_member(orthography, "sample");
233 if (sample != NULL && g_strcmp0(sample, "") != 0)
234 return g_strdup(sample);
235 }
236 }
237
238 if (json_object_has_member(orthography, "Basic Latin")) {
239 JsonObject *latin = json_object_get_object_member(orthography, "Basic Latin");
240 if (json_object_get_double_member(latin, "coverage") > 90) {
241 PangoLanguage *xx = pango_language_from_string("xx");
242 return g_strdup(pango_language_get_sample_string(xx));
243 }
244 }
245
246 return NULL;
247 }
248
249 static gchar *
250 get_sample_string (JsonObject *orthography, hb_set_t *charset)
251 {
252 const char *local_sample = pango_language_get_sample_string(NULL);
253 if (charset_contains_sample_string(charset, local_sample))
254 return NULL;
255 gchar *sample = get_default_sample_string_for_orthography(orthography);
256 return sample ? sample : get_sample_from_charset(charset);
257 }
258
259 /**
260 * font_manager_get_orthography_results:
261 * @font: (nullable) (transfer none): #JsonObject
262 *
263 * The #JsonObject returned will have the following structure:
264 *
265 *|[
266 * {
267 * "Basic Latin": {
268 * "filter": [65, 66, ... 122],
269 * "name": "Basic Latin",
270 * "native": "Basic Latin",
271 * "sample": "AaBbCcGgQqRrSsZz",
272 * "coverage": 100.0
273 * },
274 * ...,
275 * "sample" : null
276 * }
277 *]|
278 *
279 * The returned object contains a member for each orthography detected in @font.
280 *
281 * sample will be set to %NULL if the font supports rendering the sample string returned
282 * by #font_manager_get_localized_pangram, otherwise sample will be set to the
283 * sample string from the member with the highest coverage, if that should fail then
284 * sample will be set to a string randomly generated from the characters available in @font.
285 *
286 * Returns: (nullable) (transfer full): #JsonObject containing orthography results
287 */
288 JsonObject *
289 font_manager_get_orthography_results (JsonObject *font)
290 {
291 hb_set_t *charset = NULL;
292 JsonObject *results = json_object_new();
293
294 if (font)
295 charset = get_charset_from_font_object(font);
296
297 if (charset) {
298 if (check_orthography(NULL, charset, LatinOrthographies))
299 check_orthographies(results, charset, LatinOrthographies, N_LATIN);
300
301 if (check_orthography(NULL, charset, GreekOrthographies))
302 check_orthographies(results, charset, GreekOrthographies, N_GREEK);
303
304 if (check_orthography(NULL, charset, ArabicOrthographies))
305 check_orthographies(results, charset, ArabicOrthographies, N_ARABIC);
306
307 check_orthographies(results, charset, ChineseOrthographies, N_CHINESE);
308 check_orthographies(results, charset, JapaneseOrthographies, N_JAPANESE);
309 check_orthographies(results, charset, KoreanOrthographies, N_KOREAN);
310 check_orthographies(results, charset, UncategorizedOrthographies, N_MISC);
311 }
312
313 if (charset && !hb_set_is_empty(charset)) {
314
315 if (json_object_get_size(results) == 0) {
316 JsonObject *uncategorized = json_object_new();
317 JsonArray *char_array = _hb_set_to_json_array(charset);
318 json_object_set_string_member(uncategorized, "name", "Uncategorized");
319 json_object_set_double_member(uncategorized, "coverage", 100);
320 json_object_set_array_member(uncategorized, "filter", char_array);
321 json_object_set_object_member(results, "Uncategorized", uncategorized);
322 }
323
324 g_autofree gchar *sample = get_sample_string(results, charset);
325 json_object_set_string_member(results, "sample", sample);
326
327 } else {
328
329 json_object_set_string_member(results, "sample", NULL);
330
331 }
332
333 if (charset)
334 hb_set_destroy(charset);
335
336 return results;
337 }
338
339 /**
340 * font_manager_get_sample_string:
341 * @font: #JsonObject
342 *
343 * Returns: (nullable) (transfer full): A newly allocated string that must be freed with #g_free
344 * or %NULL if the systems default language is supported
345 */
346 gchar *
347 font_manager_get_sample_string (JsonObject *font)
348 {
349 const char *local_sample = pango_language_get_sample_string(NULL);
350 hb_set_t *charset = get_charset_from_font_object(font);
351 if (charset_contains_sample_string(charset, local_sample)) {
352 hb_set_destroy(charset);
353 return NULL;
354 }
355 g_autoptr(JsonObject) orthography = font_manager_get_orthography_results(font);
356 gchar *sample = get_default_sample_string_for_orthography(orthography);
357 if (!sample)
358 sample = get_sample_from_charset(charset);
359 hb_set_destroy(charset);
360 return sample;
361 }
362