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>
46 # if !STDC_HEADERS && HAVE_MEMORY_H
57 #define appname "gen-arabic-shaping-tab"
58 #define outputname "arabic-shaping.tab.i"
65 fprintf (stderr, appname ": %s\n", msg);
75 fprintf (stderr, appname ": ");
76 fprintf (stderr, fmt, p);
77 fprintf (stderr, "\n");
88 fprintf (stderr, appname ": ");
89 fprintf (stderr, fmt, p, q);
90 fprintf (stderr, "\n");
101 fprintf (stderr, appname ": ");
102 fprintf (stderr, fmt, l, p);
103 fprintf (stderr, "\n");
107 static const char *arabic_shaping_tags[] = {
118 const char *type_list[]
121 const char **p = type_list;
123 if (!strcmp (s, p[0]))
124 return p - type_list;
128 #define table_name "ArShap"
129 #define macro_name "FRIBIDI_GET_ARABIC_SHAPE_PRES"
131 #define START_CHAR 0x600
132 #define END_CHAR 0x700
134 static FriBidiChar table[FRIBIDI_UNICODE_CHARS][4];
135 static char buf[4000];
136 static char tag[sizeof (buf)], buf2[sizeof (buf)];
137 static FriBidiChar minshaped, maxshaped;
144 register FriBidiChar c;
147 for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
148 for (shape = 0; shape < 4; shape++)
157 minshaped = FRIBIDI_UNICODE_CHARS;
164 read_unicode_data_txt (
168 unsigned long c, unshaped, l;
171 while (fgets (buf, sizeof buf, f))
181 if (*s == '#' || *s == '\0' || *s == '\n')
184 i = sscanf (s, "%lx;%*[^;];%*[^;];%*[^;];%*[^;];<%[^;> ]> %lx %[^; ]", &c, tag, &unshaped, buf2);
189 if (i != 3 || c >= FRIBIDI_UNICODE_CHARS || unshaped >= FRIBIDI_UNICODE_CHARS)
190 die3l ("UnicodeData.txt: invalid input at line %ld: %s", l, s);
192 shape = shape_is (tag, arabic_shaping_tags);
195 table[unshaped][shape] = c;
196 if (unshaped < minshaped)
197 minshaped = unshaped;
198 if (unshaped > maxshaped)
199 maxshaped = unshaped;
206 const char *data_file_type[],
207 const char *data_file_name[]
212 for (; data_file_name[0] && data_file_type[0];
213 data_file_name++, data_file_type++)
215 if (!(f = fopen (data_file_name[0], "rt")))
216 die2 ("error: cannot open `%s' for reading", data_file_name[0]);
218 if (!strcmp (data_file_type[0], "UnicodeData.txt"))
219 read_unicode_data_txt (f);
221 die2 ("error: unknown data-file type %s", data_file_type[0]);
229 gen_arabic_shaping_tab (
230 int max_depth /* currently unused */,
231 const char *data_file_type[]
234 register FriBidiChar c;
237 if (maxshaped < minshaped)
238 die ("error: no shaping pair found, something wrong with reading input");
240 printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
241 FRIBIDI_VERSION ")\n" " * from the files %s, %s of Unicode version "
242 FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type[0],
246 " use %s(key,shape) to access your table\n\n"
247 " required memory: %ld\n"
249 macro_name, (long)(maxshaped - minshaped + 1) * 4 * sizeof (FriBidiChar));
251 printf ("\n" "/* *IND" "ENT-OFF* */\n\n");
253 printf ("static const FriBidiChar %s[%d][%d] = {\n", table_name, maxshaped - minshaped + 1, 4);
254 for (c = minshaped; c <= maxshaped; c++)
257 for (shape = 0; shape < 4; shape++)
258 printf ("0x%04lx,", (unsigned long)table[c][shape]);
265 printf ("/* *IND" "ENT-ON* */\n\n");
267 printf ("#ifndef FRIBIDI_ACCESS_SHAPE_TABLE\n"
268 "# define FRIBIDI_ACCESS_SHAPE_TABLE(table,min,max,x,shape) \\\n"
269 " (((x)<(min)||(x)>(max))?(x):(table)[(x)-(min)][(shape)])\n"
271 printf ("#define %s(x,shape) "
272 "FRIBIDI_ACCESS_SHAPE_TABLE(%s, 0x%04lx, 0x%04lx, (x), (shape))\n\n",
273 macro_name, table_name, (unsigned long)minshaped, (unsigned long)maxshaped);
275 printf ("/* End of generated " outputname " */\n");
284 const char *data_file_type[] =
285 { "UnicodeData.txt", NULL };
288 die3 ("usage:\n " appname " max-depth /path/to/%s /path/to/%s [junk...]",
289 data_file_type[0], data_file_type[1]);
292 int max_depth = atoi (argv[1]);
293 const char *data_file_name[] = { NULL, NULL };
294 data_file_name[0] = argv[2];
297 die ("invalid depth");
300 read_data (data_file_type, data_file_name);
301 gen_arabic_shaping_tab (max_depth, data_file_type);