2 * gen-script-for-lang.c: Utility program to generate pango-script-lang-table.h
4 * Copyright (C) 2003 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
29 #include <pango/pango-enum-types.h>
30 #include <pango/pango-script.h>
31 #include <pango/pango-types.h>
33 #include <fontconfig/fontconfig.h>
44 ScriptInfo scripts[MAX_SCRIPTS];
47 static const char *get_script_name (PangoScript script)
49 static GEnumClass *class = NULL;
52 class = g_type_class_ref (PANGO_TYPE_SCRIPT);
54 value = g_enum_get_value (class, script);
57 return value->value_name;
60 static void fail (const char *format, ...) G_GNUC_PRINTF (1, 2) G_GNUC_NORETURN;
61 static void fail (const char *format, ...)
65 va_start (vap, format);
66 vfprintf (stderr, format, vap);
73 script_for_char (gunichar ch,
76 PangoScript script = pango_script_for_unichar (ch);
77 if (script != PANGO_SCRIPT_COMMON &&
78 script != PANGO_SCRIPT_INHERITED)
82 if (script == PANGO_SCRIPT_UNKNOWN)
84 g_message ("Script unknown for U+%04X", ch);
88 for (j = 0; j < MAX_SCRIPTS; j++)
90 if (info->scripts[j].script == script)
92 if (info->scripts[j].script == PANGO_SCRIPT_COMMON)
94 info->scripts[j].script = script;
100 fail ("More than %d scripts found for %s. Increase MAX_SCRIPTS.\n", MAX_SCRIPTS, pango_language_to_string (info->lang));
102 info->scripts[j].freq++;
107 scripts_for_lang (LangInfo *info)
109 const FcCharSet *charset;
111 FcChar32 map[FC_CHARSET_MAP_SIZE];
114 charset = FcLangGetCharSet ((const FcChar8 *) info->lang);
118 for (ucs4 = FcCharSetFirstPage (charset, map, &pos);
119 ucs4 != FC_CHARSET_DONE;
120 ucs4 = FcCharSetNextPage (charset, map, &pos))
123 for (i = 0; i < FC_CHARSET_MAP_SIZE; i++)
125 FcChar32 bits = map[i];
126 FcChar32 base = ucs4 + i * 32;
132 script_for_char (base + b, info);
142 do_lang (GArray *script_array,
148 info.lang = pango_language_from_string ((const char *)lang);
150 for (j = 0; j < MAX_SCRIPTS; j++)
152 info.scripts[j].script = PANGO_SCRIPT_COMMON;
153 info.scripts[j].freq = 0;
156 scripts_for_lang (&info);
158 g_array_append_val (script_array, info);
162 compare_script (gconstpointer a,
166 const ScriptInfo *info_a = a;
167 const ScriptInfo *info_b = b;
168 G_GNUC_UNUSED LangInfo *lang_info = data;
170 /* first compare frequencies, higher first */
171 if (info_a->freq > info_b->freq)
173 if (info_a->freq < info_b->freq)
176 /* next compare script indices, higher first (it's more specific) */
177 if (info_a->script > info_b->script)
179 if (info_a->script < info_b->script)
182 /* for stability, next compare pointers themselves, smaller first */
192 compare_lang (gconstpointer a,
195 const LangInfo *info_a = a;
196 const LangInfo *info_b = b;
198 return strcmp (pango_language_to_string (info_a->lang),
199 pango_language_to_string (info_b->lang));
204 GArray *script_array;
208 int max_lang_len = 0;
209 int max_script_len = 0;
216 const char *date_str = "unknown";
223 script_array = g_array_new (FALSE, FALSE, sizeof (LangInfo));
226 langs_set = FcGetLangs ();
227 langs = FcStrListCreate (langs_set);
228 FcStrSetDestroy (langs_set);
230 while ((lang = FcStrListNext (langs)))
231 do_lang (script_array, lang);
233 FcStrListDone (langs);
236 g_array_sort (script_array, compare_lang);
238 for (i = 0; i < script_array->len; i++)
240 LangInfo *info = &g_array_index (script_array, LangInfo, i);
242 max_lang_len = MAX (max_lang_len,
243 (int)strlen (pango_language_to_string (info->lang)));
245 g_qsort_with_data (info->scripts,
246 G_N_ELEMENTS (info->scripts),
247 sizeof (info->scripts[0]),
251 for (j = 0; j < MAX_SCRIPTS; j++)
252 if (!info->scripts[j].freq)
255 max_script_len = MAX (max_script_len, j);
258 if ((t = time(NULL), tmp = localtime (&t)) && strftime(date_buf, sizeof(date_buf), "%F", tmp))
261 fc_version = FcGetVersion ();
263 g_print ("/* pango-script-lang-table.h:\n"
265 " * Generated by %s\n"
267 " * Source: fontconfig-%d.%d.%d\n"
273 fc_version / 10000, (fc_version / 100) % 100, fc_version % 100);
275 g_print ("typedef struct _PangoScriptForLang {\n"
276 " const char lang[%d];\n"
277 " PangoScript scripts[%d];\n"
278 "} PangoScriptForLang;\n"
280 "static const PangoScriptForLang pango_script_for_lang[] = {\n",
284 for (i = 0; i < script_array->len; i++)
286 LangInfo *info = &g_array_index (script_array, LangInfo, i);
288 g_print (" { \"%s\", %*s{ ",
289 pango_language_to_string (info->lang),
290 max_lang_len - strlen (pango_language_to_string (info->lang)), "");
291 for (j = 0; j < MAX_SCRIPTS; j++)
293 if (!info->scripts[j].freq)
299 get_script_name (info->scripts[j].script),
300 info->scripts[j].freq);
303 if (i + 1 != script_array->len)