2017-08-11 15:47:02 +00:00
|
|
|
/* convert-emoji.c: A tool to create an Emoji data GVariant
|
|
|
|
* Copyright 2017, Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Build with gcc -o convert-emoji convert-emoji.c `pkg-config --cflags --libs json-glib-1.0`
|
|
|
|
*/
|
|
|
|
|
2020-10-04 02:26:56 +00:00
|
|
|
/* Reads data from the json files in emojibase, expecting
|
|
|
|
* language-specific data.raw.json as input
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* The format of the generated data is: a(ausasu).
|
2020-04-14 23:13:17 +00:00
|
|
|
* Each member of the array has the following fields:
|
|
|
|
* au - sequence of unicode codepoints. If the
|
|
|
|
* sequence contains a 0, it marks the point
|
|
|
|
* where skin tone modifiers should be inserted
|
2020-04-15 02:29:25 +00:00
|
|
|
* s - name, e.g. "man worker"
|
|
|
|
* as - keywords, e.g. "man", "worker"
|
2020-10-04 02:26:56 +00:00
|
|
|
* u - the group that this item belongs to:
|
|
|
|
* 0: smileys-emotion
|
|
|
|
* 1: people-body
|
|
|
|
* 2: component
|
|
|
|
* 3: animals-nature
|
|
|
|
* 4: food-drink
|
|
|
|
* 5: travel-places
|
|
|
|
* 6: activities
|
|
|
|
* 7: objects
|
|
|
|
* 8: symbols
|
|
|
|
* 9: flags
|
2020-04-14 23:13:17 +00:00
|
|
|
*/
|
2017-08-11 15:47:02 +00:00
|
|
|
#include <json-glib/json-glib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
parse_code (GVariantBuilder *b,
|
2020-10-04 02:26:56 +00:00
|
|
|
const char *code)
|
2017-08-11 15:47:02 +00:00
|
|
|
{
|
|
|
|
g_auto(GStrv) strv = NULL;
|
|
|
|
int j;
|
|
|
|
|
2020-10-04 02:26:56 +00:00
|
|
|
strv = g_strsplit (code, "-", -1);
|
2017-08-11 15:47:02 +00:00
|
|
|
for (j = 0; strv[j]; j++)
|
|
|
|
{
|
|
|
|
guint32 u;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
u = (guint32) g_ascii_strtoull (strv[j], &end, 16);
|
|
|
|
if (*end != '\0')
|
|
|
|
{
|
|
|
|
g_error ("failed to parse code: %s\n", strv[j]);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2017-08-13 19:16:11 +00:00
|
|
|
if (0x1f3fb <= u && u <= 0x1f3ff)
|
|
|
|
g_variant_builder_add (b, "u", 0);
|
|
|
|
else
|
2020-10-04 02:26:56 +00:00
|
|
|
g_variant_builder_add (b, "u", u);
|
2017-08-11 15:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
JsonParser *parser;
|
|
|
|
JsonNode *root;
|
2017-08-19 18:05:16 +00:00
|
|
|
JsonObject *ro;
|
2020-10-04 02:26:56 +00:00
|
|
|
JsonArray *array;
|
2017-08-19 18:05:16 +00:00
|
|
|
JsonNode *node;
|
2020-10-04 02:26:56 +00:00
|
|
|
const char *unicode;
|
2017-08-19 18:05:16 +00:00
|
|
|
JsonObjectIter iter;
|
2017-08-11 15:47:02 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
guint length, i;
|
|
|
|
GVariantBuilder builder;
|
|
|
|
GVariant *v;
|
|
|
|
GString *s;
|
2017-08-19 18:05:16 +00:00
|
|
|
GHashTable *names;
|
|
|
|
GString *name_key;
|
|
|
|
|
2020-10-04 02:26:56 +00:00
|
|
|
if (argc != 3)
|
2017-08-19 18:05:16 +00:00
|
|
|
{
|
2020-10-04 02:26:56 +00:00
|
|
|
g_print ("Usage: emoji-convert INPUT OUTPUT\n");
|
2017-08-19 18:05:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
parser = json_parser_new ();
|
2017-08-11 15:47:02 +00:00
|
|
|
|
|
|
|
if (!json_parser_load_from_file (parser, argv[1], &error))
|
|
|
|
{
|
|
|
|
g_error ("%s", error->message);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
root = json_parser_get_root (parser);
|
|
|
|
array = json_node_get_array (root);
|
|
|
|
length = json_array_get_length (array);
|
|
|
|
|
2020-10-04 02:26:56 +00:00
|
|
|
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ausasu)"));
|
|
|
|
for (i = 0; i < length; i++)
|
2017-08-11 15:47:02 +00:00
|
|
|
{
|
2020-10-04 02:26:56 +00:00
|
|
|
JsonObject *obj = json_array_get_object_element (array, i);
|
2017-08-13 19:16:11 +00:00
|
|
|
GVariantBuilder b1;
|
2020-04-15 02:29:25 +00:00
|
|
|
GVariantBuilder b2;
|
2020-10-04 02:26:56 +00:00
|
|
|
guint group;
|
2017-08-11 15:47:02 +00:00
|
|
|
const char *name;
|
2017-08-13 19:16:11 +00:00
|
|
|
char *code;
|
2017-08-11 15:47:02 +00:00
|
|
|
|
2020-10-04 02:26:56 +00:00
|
|
|
if (!json_object_has_member (obj, "group"))
|
|
|
|
continue;
|
2017-08-11 15:47:02 +00:00
|
|
|
|
2020-10-04 02:26:56 +00:00
|
|
|
group = json_object_get_int_member (obj, "group");
|
2022-02-22 09:44:43 +00:00
|
|
|
name = json_object_get_string_member (obj, "label");
|
2017-08-11 15:47:02 +00:00
|
|
|
|
2020-10-04 02:26:56 +00:00
|
|
|
if (json_object_has_member (obj, "skins"))
|
2017-08-11 15:47:02 +00:00
|
|
|
{
|
2020-10-04 02:26:56 +00:00
|
|
|
JsonArray *a2 = json_object_get_array_member (obj, "skins");
|
|
|
|
JsonNode *n2 = json_array_get_element (a2, 0);
|
|
|
|
JsonObject *o2 = json_node_get_object (n2);
|
|
|
|
code = g_strdup (json_object_get_string_member (o2, "hexcode"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code = g_strdup (json_object_get_string_member (obj, "hexcode"));
|
2017-08-11 15:47:02 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 02:26:56 +00:00
|
|
|
g_variant_builder_init (&b1, G_VARIANT_TYPE ("au"));
|
|
|
|
|
|
|
|
if (!parse_code (&b1, code))
|
2017-08-13 19:16:11 +00:00
|
|
|
return 1;
|
|
|
|
|
2020-04-15 02:29:25 +00:00
|
|
|
g_variant_builder_init (&b2, G_VARIANT_TYPE ("as"));
|
2020-10-04 02:26:56 +00:00
|
|
|
if (json_object_has_member (obj, "tags"))
|
2020-04-15 02:29:25 +00:00
|
|
|
{
|
2020-10-04 02:26:56 +00:00
|
|
|
JsonArray *tags = json_object_get_array_member (obj, "tags");
|
|
|
|
for (int j = 0; j < json_array_get_length (tags); j++)
|
|
|
|
g_variant_builder_add (&b2, "s", json_array_get_string_element (tags, j));
|
2020-04-15 02:29:25 +00:00
|
|
|
}
|
2017-08-19 18:05:16 +00:00
|
|
|
|
2020-10-04 02:26:56 +00:00
|
|
|
g_variant_builder_add (&builder, "(ausasu)", &b1, name, &b2, group);
|
2017-08-11 15:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v = g_variant_builder_end (&builder);
|
2020-10-04 02:26:56 +00:00
|
|
|
if (g_str_has_suffix (argv[2], ".json"))
|
2017-08-11 15:47:02 +00:00
|
|
|
{
|
2020-04-14 23:13:17 +00:00
|
|
|
JsonNode *node;
|
|
|
|
char *out;
|
|
|
|
|
|
|
|
node = json_gvariant_serialize (v);
|
|
|
|
out = json_to_string (node, TRUE);
|
2020-10-04 02:26:56 +00:00
|
|
|
if (!g_file_set_contents (argv[2], out, -1, &error))
|
2020-04-14 23:13:17 +00:00
|
|
|
{
|
|
|
|
g_error ("%s", error->message);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GBytes *bytes;
|
|
|
|
|
|
|
|
bytes = g_variant_get_data_as_bytes (v);
|
2020-10-04 02:26:56 +00:00
|
|
|
if (!g_file_set_contents (argv[2], g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), &error))
|
2020-04-14 23:13:17 +00:00
|
|
|
{
|
|
|
|
g_error ("%s", error->message);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-08-11 15:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|