Debian Codegraph @

gtk/emoji/convert-emoji.c

raw
1 /* convert-emoji.c: A tool to create an Emoji data GVariant
2 * Copyright 2017, Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /* Build with gcc -o convert-emoji convert-emoji.c `pkg-config --cflags --libs json-glib-1.0`
19 */
20
21 /* Reads data from the json files in emojibase, expecting
22 * language-specific data.raw.json as input
23 */
24
25 /* The format of the generated data is: a(aussasasu).
26 * Each member of the array has the following fields:
27 * au - sequence of unicode codepoints, including the emoji presentation
28 * selector (FE0F) where necessary. skin tone variations are represented
29 * with either the first tone code point (1F3FB) or 0. the choice indicates
30 * the handling of the generic sequence (i.e., no tone), which may have a
31 * default text presentation and thus require the emoji presentation
32 * selector (unlike sequences with a tone, which are always presented as
33 * emojis). 0 indicates the text case, that is, replace this code point
34 * with FE0F, while 1F3FB indicates this code point should be omitted.
35 * s - name in english, e.g. "man worker"
36 * s - name in locale
37 * as - keywords in english, e.g. "man", "worker"
38 * as - keywords in locale
39 * u - the group that this item belongs to:
40 * 0: smileys-emotion
41 * 1: people-body
42 * 2: component
43 * 3: animals-nature
44 * 4: food-drink
45 * 5: travel-places
46 * 6: activities
47 * 7: objects
48 * 8: symbols
49 * 9: flags
50 */
51 #include <json-glib/json-glib.h>
52 #include <string.h>
53
54 gboolean
55 parse_code (GVariantBuilder *b,
56 const char *code,
57 gboolean needs_presentation_selector)
58 {
59 g_auto(GStrv) strv = NULL;
60 int j;
61
62 strv = g_strsplit (code, "-", -1);
63 for (j = 0; strv[j]; j++)
64 {
65 guint32 u;
66 char *end;
67
68 u = (guint32) g_ascii_strtoull (strv[j], &end, 16);
69 if (*end != '\0')
70 {
71 g_error ("failed to parse code: %s\n", strv[j]);
72 return FALSE;
73 }
74 if (0x1f3fb <= u && u <= 0x1f3ff)
75 {
76 if (needs_presentation_selector)
77 {
78 if (strv[j+1])
79 {
80 g_error ("unexpected inner skin tone in default-text generic sequence: %s\n", code);
81 return FALSE;
82 }
83 g_variant_builder_add (b, "u", 0);
84 needs_presentation_selector = FALSE;
85 }
86 else
87 {
88 g_variant_builder_add (b, "u", 0x1f3fb);
89 }
90 }
91 else
92 {
93 g_variant_builder_add (b, "u", u);
94 }
95 }
96
97 if (needs_presentation_selector)
98 g_variant_builder_add (b, "u", 0xfe0f);
99
100 return TRUE;
101 }
102
103 int
104 main (int argc, char *argv[])
105 {
106 JsonParser *parser;
107 JsonNode *root;
108 JsonParser *parser_en;
109 JsonNode *root_en;
110 JsonObject *ro;
111 JsonArray *array;
112 JsonArray *array_en;
113 JsonNode *node;
114 const char *unicode;
115 JsonObjectIter iter;
116 GError *error = NULL;
117 guint length, length_en, i;
118 GVariantBuilder builder;
119 GVariant *v;
120 GString *s;
121 GHashTable *names;
122
123 if (argc != 4) //0 -> compiled file, 1 -> en/data.raw.json, 2 -> de/data.raw.json, 3 -> de.data
124 {
125 g_print ("Usage: emoji-convert INPUT1 INPUT2 OUTPUT\nINPUT1 should be raw json data for English\nINPUT2 should be raw json data for the locale\n");
126 return 1;
127 }
128
129 parser = json_parser_new ();
130 parser_en = json_parser_new ();
131
132 if (!json_parser_load_from_file (parser_en, argv[1], &error))
133 {
134 g_error ("%s", error->message);
135 return 1;
136 }
137 if (!json_parser_load_from_file (parser, argv[2], &error))
138 {
139 g_error ("%s", error->message);
140 return 1;
141 }
142 root = json_parser_get_root (parser);
143 array = json_node_get_array (root);
144 length = json_array_get_length (array);
145 root_en = json_parser_get_root (parser_en);
146 array_en = json_node_get_array (root_en);
147 length_en = json_array_get_length (array_en);
148
149 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(aussasasu)"));
150 for (i = 0; i < length; i++)
151 {
152 JsonObject *obj = json_array_get_object_element (array, i);
153 JsonObject *obj_en = json_array_get_object_element (array_en, i);
154 GVariantBuilder b1;
155 GVariantBuilder b2;
156 GVariantBuilder b3;
157 guint group;
158 const char *name;
159 const char *name_en;
160 char *code;
161 const char *text;
162 gboolean needs_presentation_selector;
163
164 if (!json_object_has_member (obj, "group"))
165 continue;
166 if (!json_object_has_member (obj_en, "group"))
167 continue;
168
169 group = json_object_get_int_member (obj, "group");
170 name = json_object_get_string_member (obj, "label");
171 name_en = json_object_get_string_member (obj_en, "label");
172
173 if (g_str_has_suffix (name_en, "skin tone"))
174 continue;
175
176 if (json_object_has_member (obj, "skins") && json_object_has_member (obj_en, "skins"))
177 {
178 JsonArray *a2 = json_object_get_array_member (obj, "skins");
179 JsonNode *n2 = json_array_get_element (a2, 0);
180 JsonObject *o2 = json_node_get_object (n2);
181 code = g_strdup (json_object_get_string_member (o2, "hexcode"));
182 }
183 else
184 {
185 code = g_strdup (json_object_get_string_member (obj, "hexcode"));
186 }
187
188 text = json_object_get_string_member (obj, "text");
189 needs_presentation_selector = *text != '\0' && json_object_get_int_member (obj, "type") == 0;
190
191 g_variant_builder_init (&b1, G_VARIANT_TYPE ("au"));
192 if (!parse_code (&b1, code, needs_presentation_selector))
193 return 1;
194
195 g_variant_builder_init (&b2, G_VARIANT_TYPE ("as"));
196 if (json_object_has_member (obj_en, "tags"))
197 {
198 JsonArray *tags_en = json_object_get_array_member (obj_en, "tags");
199 for (int j = 0; j < json_array_get_length (tags_en); j++)
200 {
201 g_variant_builder_add (&b2, "s", json_array_get_string_element (tags_en, j));
202 }
203 }
204
205 g_variant_builder_init (&b3, G_VARIANT_TYPE ("as"));
206 if (json_object_has_member (obj, "tags"))
207 {
208 JsonArray *tags = json_object_get_array_member (obj, "tags");
209 for (int j = 0; j < json_array_get_length (tags); j++)
210 {
211 g_variant_builder_add (&b3, "s", json_array_get_string_element (tags, j));
212 }
213 }
214 g_variant_builder_add (&builder, "(aussasasu)", &b1, name_en, name, &b2, &b3, group);
215 }
216 v = g_variant_builder_end (&builder);
217 if (g_str_has_suffix (argv[3], ".json"))
218 {
219 JsonNode *node;
220 char *out;
221
222 node = json_gvariant_serialize (v);
223 out = json_to_string (node, TRUE);
224 if (!g_file_set_contents (argv[3], out, -1, &error))
225 {
226 g_error ("%s", error->message);
227 return 1;
228 }
229 }
230 else
231 {
232 GBytes *bytes;
233
234 bytes = g_variant_get_data_as_bytes (v);
235 if (!g_file_set_contents (argv[3], g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), &error))
236 {
237 g_error ("%s", error->message);
238 return 1;
239 }
240 }
241
242 return 0;
243 }