2 * gen-arabic-shaping-tab.c - generate arabic-shaping.tab.i
5 * Behdad Esfahbod, 2004, 2005
7 * Copyright (C) 2004 Sharif FarsiWeb, Inc
8 * Copyright (C) 2004, 2005 Behdad Esfahbod
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library, in a file named COPYING; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA
25 * For licensing issues, contact <fribidi.license@gmail.com>.
30 #include <fribidi-unicode.h>
42 # if !STDC_HEADERS && HAVE_MEMORY_H
53 #define appname "gen-arabic-shaping-tab"
54 #define outputname "arabic-shaping.tab.i"
61 fprintf (stderr, appname ": %s\n", msg);
71 fprintf (stderr, appname ": ");
72 fprintf (stderr, fmt, p);
73 fprintf (stderr, "\n");
84 fprintf (stderr, appname ": ");
85 fprintf (stderr, fmt, p, q);
86 fprintf (stderr, "\n");
97 fprintf (stderr, appname ": ");
98 fprintf (stderr, fmt, l, p);
99 fprintf (stderr, "\n");
103 static const char *arabic_shaping_tags[] = {
114 const char *type_list[]
117 const char **p = type_list;
119 if (!strcmp (s, p[0]))
120 return p - type_list;
124 #define table_name "ArShap"
125 #define macro_name "FRIBIDI_GET_ARABIC_SHAPE_PRES"
127 #define START_CHAR 0x600
128 #define END_CHAR 0x700
130 static FriBidiChar table[FRIBIDI_UNICODE_CHARS][4];
131 static char buf[4000];
132 static char tag[sizeof (buf)], buf2[sizeof (buf)];
133 static FriBidiChar minshaped, maxshaped;
140 register FriBidiChar c;
143 for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
144 for (shape = 0; shape < 4; shape++)
153 minshaped = FRIBIDI_UNICODE_CHARS;
160 read_unicode_data_txt (
164 unsigned long c, unshaped, l;
167 while (fgets (buf, sizeof buf, f))
177 if (*s == '#' || *s == '\0' || *s == '\n')
180 i = sscanf (s, "%lx;%*[^;];%*[^;];%*[^;];%*[^;];<%[^;> ]> %lx %[^; ]", &c, tag, &unshaped, buf2);
185 if (i != 3 || c >= FRIBIDI_UNICODE_CHARS || unshaped >= FRIBIDI_UNICODE_CHARS)
186 die3l ("UnicodeData.txt: invalid input at line %ld: %s", l, s);
188 shape = shape_is (tag, arabic_shaping_tags);
191 table[unshaped][shape] = c;
192 if (unshaped < minshaped)
193 minshaped = unshaped;
194 if (unshaped > maxshaped)
195 maxshaped = unshaped;
202 const char *data_file_type[],
203 const char *data_file_name[]
208 for (; data_file_name[0] && data_file_type[0];
209 data_file_name++, data_file_type++)
211 if (!(f = fopen (data_file_name[0], "rt")))
212 die2 ("error: cannot open `%s' for reading", data_file_name[0]);
214 if (!strcmp (data_file_type[0], "UnicodeData.txt"))
215 read_unicode_data_txt (f);
217 die2 ("error: unknown data-file type %s", data_file_type[0]);
225 gen_arabic_shaping_tab (
226 int max_depth /* currently unused */,
227 const char *data_file_type[]
230 register FriBidiChar c;
233 if (maxshaped < minshaped)
234 die ("error: no shaping pair found, something wrong with reading input");
236 printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
237 FRIBIDI_VERSION ")\n" " * from the files %s, %s of Unicode version "
238 FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type[0],
242 " use %s(key,shape) to access your table\n\n"
243 " required memory: %ld\n"
245 macro_name, (long)(maxshaped - minshaped + 1) * 4 * sizeof (FriBidiChar));
247 printf ("\n" "/* *IND" "ENT-OFF* */\n\n");
249 printf ("static const FriBidiChar %s[%d][%d] = {\n", table_name, maxshaped - minshaped + 1, 4);
250 for (c = minshaped; c <= maxshaped; c++)
253 for (shape = 0; shape < 4; shape++)
254 printf ("0x%04lx,", (unsigned long)table[c][shape]);
261 printf ("/* *IND" "ENT-ON* */\n\n");
263 printf ("#ifndef FRIBIDI_ACCESS_SHAPE_TABLE\n"
264 "# define FRIBIDI_ACCESS_SHAPE_TABLE(table,min,max,x,shape) \\\n"
265 " (((x)<(min)||(x)>(max))?(x):(table)[(x)-(min)][(shape)])\n"
267 printf ("#define %s(x,shape) "
268 "FRIBIDI_ACCESS_SHAPE_TABLE(%s, 0x%04lx, 0x%04lx, (x), (shape))\n\n",
269 macro_name, table_name, (unsigned long)minshaped, (unsigned long)maxshaped);
271 printf ("/* End of generated " outputname " */\n");
280 const char *data_file_type[] =
281 { "UnicodeData.txt", NULL };
284 die3 ("usage:\n " appname " max-depth /path/to/%s /path/to/%s [junk...]",
285 data_file_type[0], data_file_type[1]);
288 int max_depth = atoi (argv[1]);
289 const char *data_file_name[] = { NULL, NULL };
290 data_file_name[0] = argv[2];
293 die ("invalid depth");
296 read_data (data_file_type, data_file_name);
297 gen_arabic_shaping_tab (max_depth, data_file_type);