diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/Makefile.am | 2 | ||||
-rw-r--r-- | tools/gen-script-for-lang-new.c | 122 |
2 files changed, 106 insertions, 18 deletions
diff --git a/tools/Makefile.am b/tools/Makefile.am index 8702a62e..fdc746e9 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -7,7 +7,7 @@ INCLUDES = \ -DPANGO_DISABLE_DEPRECATED \ $(PANGO_DEBUG_FLAGS) -noinst_PROGRAMS = \ +EXTRA_PROGRAMS = \ gen-script-for-lang \ gen-script-for-lang-new diff --git a/tools/gen-script-for-lang-new.c b/tools/gen-script-for-lang-new.c index fb5eebc8..2ef49859 100644 --- a/tools/gen-script-for-lang-new.c +++ b/tools/gen-script-for-lang-new.c @@ -24,6 +24,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <time.h> #include <pango/pango-enum-types.h> #include <pango/pango-script.h> @@ -34,8 +35,13 @@ #define MAX_SCRIPTS 3 typedef struct { + PangoScript script; + int freq; +} ScriptInfo; + +typedef struct { PangoLanguage *lang; - PangoScript scripts[MAX_SCRIPTS]; + ScriptInfo scripts[MAX_SCRIPTS]; } LangInfo; static const char *get_script_name (PangoScript script) @@ -81,17 +87,19 @@ script_for_char (gunichar ch, for (j = 0; j < MAX_SCRIPTS; j++) { - if (info->scripts[j] == script) + if (info->scripts[j].script == script) break; - if (info->scripts[j] == PANGO_SCRIPT_INVALID_CODE) + if (info->scripts[j].script == PANGO_SCRIPT_COMMON) { - info->scripts[j] = script; + info->scripts[j].script = script; break; } } if (j == MAX_SCRIPTS) - fail ("More than %d scripts found for %s\n", MAX_SCRIPTS, pango_language_to_string (info->lang)); + fail ("More than %d scripts found for %s. Increase MAX_SCRIPTS.\n", MAX_SCRIPTS, pango_language_to_string (info->lang)); + + info->scripts[j].freq++; } } @@ -140,7 +148,10 @@ do_lang (GArray *script_array, info.lang = pango_language_from_string ((const char *)lang); for (j = 0; j < MAX_SCRIPTS; j++) - info.scripts[j] = PANGO_SCRIPT_INVALID_CODE; + { + info.scripts[j].script = PANGO_SCRIPT_COMMON; + info.scripts[j].freq = 0; + } scripts_for_lang (&info); @@ -148,6 +159,36 @@ do_lang (GArray *script_array, } static int +compare_script (gconstpointer a, + gconstpointer b, + gpointer data) +{ + const ScriptInfo *info_a = a; + const ScriptInfo *info_b = b; + G_GNUC_UNUSED LangInfo *lang_info = data; + + /* first compare frequencies, higher first */ + if (info_a->freq > info_b->freq) + return -1; + if (info_a->freq < info_b->freq) + return +1; + + /* next compare script indices, higher first (it's more specific) */ + if (info_a->script > info_b->script) + return -1; + if (info_a->script < info_b->script) + return +1; + + /* for stability, next compare pointers themselves, smaller first */ + if (info_a < info_b) + return -1; + if (info_a > info_b) + return +1; + + return 0; +} + +static int compare_lang (gconstpointer a, gconstpointer b) { @@ -161,12 +202,21 @@ compare_lang (gconstpointer a, int main (void) { GArray *script_array; + unsigned int i; int j; int max_lang_len = 0; + int max_script_len = 0; + FcStrList *langs; FcChar8* lang; + char date_buf[200]; + const char *date_str = "unknown"; + time_t t; + struct tm *tmp; + int fc_version; + g_type_init (); script_array = g_array_new (FALSE, FALSE, sizeof (LangInfo)); @@ -185,36 +235,74 @@ int main (void) for (i = 0; i < script_array->len; i++) { LangInfo *info = &g_array_index (script_array, LangInfo, i); - + max_lang_len = MAX (max_lang_len, - 1 + (int)strlen (pango_language_to_string (info->lang))); - } + (int)strlen (pango_language_to_string (info->lang))); + + g_qsort_with_data (info->scripts, + G_N_ELEMENTS (info->scripts), + sizeof (info->scripts[0]), + compare_script, + info); + + for (j = 0; j < MAX_SCRIPTS; j++) + if (!info->scripts[j].freq) + break; - g_print ("typedef struct {\n" + max_script_len = MAX (max_script_len, j); + } + + if ((t = time(NULL), tmp = localtime (&t)) && strftime(date_buf, sizeof(date_buf), "%F", tmp)) + date_str = date_buf; + + fc_version = FcGetVersion (); + + g_print ("/* pango-script-lang-table.h:\n" + " * \n" + " * Generated by %s\n" + " * Date: %s\n" + " * Source: fontconfig-%d.%d.%d\n" + " * \n" + " * Do not edit.\n" + " */\n", + __FILE__, + date_str, + fc_version / 10000, (fc_version / 100) % 100, fc_version % 100); + + g_print ("typedef struct _PangoScriptForLang {\n" " const char lang[%d];\n" " PangoScript scripts[%d];\n" "} PangoScriptForLang;\n" "\n" "static const PangoScriptForLang pango_script_for_lang[] = {\n", - max_lang_len, - MAX_SCRIPTS); + max_lang_len + 1, + max_script_len); for (i = 0; i < script_array->len; i++) { LangInfo *info = &g_array_index (script_array, LangInfo, i); - g_print (" { \"%s\", { ", pango_language_to_string (info->lang)); + g_print (" { \"%s\", %*s{ ", + pango_language_to_string (info->lang), + max_lang_len - strlen (pango_language_to_string (info->lang)), ""); for (j = 0; j < MAX_SCRIPTS; j++) { + if (!info->scripts[j].freq) + break; + if (j != 0) g_print (", "); - g_print ("%s", get_script_name (info->scripts[j])); + g_print ("%s/*%d*/", + get_script_name (info->scripts[j].script), + info->scripts[j].freq); } - g_print (" } },\n"); + g_print (" } }"); + if (i + 1 != script_array->len) + g_print (","); + g_print ("\n"); } g_print ("};\n"); - - + return 0; } |