2 * gen-arabic-shaping-tab.c - generate arabic-shaping.tab.i
4 * $Id: gen-arabic-shaping-tab.c,v 1.2 2006-01-31 03:23:12 behdad Exp $
6 * $Date: 2006-01-31 03:23:12 $
8 * $Source: /home/behdad/src/fdo/fribidi/togit/git/../fribidi/fribidi2/gen.tab/gen-arabic-shaping-tab.c,v $
11 * Behdad Esfahbod, 2004, 2005
13 * Copyright (C) 2004 Sharif FarsiWeb, Inc
14 * Copyright (C) 2004, 2005 Behdad Esfahbod
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this library, in a file named COPYING; if not, write to the
28 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 * Boston, MA 02110-1301, USA
31 * For licensing issues, contact <license@farsiweb.info>.
36 #include <fribidi-unicode.h>
48 # if !STDC_HEADERS && HAVE_MEMORY_H
59 #define appname "gen-arabic-shaping-tab"
60 #define outputname "arabic-shaping.tab.i"
67 fprintf (stderr, appname ": %s\n", msg);
77 fprintf (stderr, appname ": ");
78 fprintf (stderr, fmt, p);
79 fprintf (stderr, "\n");
90 fprintf (stderr, appname ": ");
91 fprintf (stderr, fmt, p, q);
92 fprintf (stderr, "\n");
103 fprintf (stderr, appname ": ");
104 fprintf (stderr, fmt, l, p);
105 fprintf (stderr, "\n");
109 static const char *arabic_shaping_tags[] = {
120 const char *type_list[]
123 const char **p = type_list;
125 if (!strcmp (s, p[0]))
126 return p - type_list;
130 #define table_name "ArShap"
131 #define macro_name "FRIBIDI_GET_ARABIC_SHAPE_PRES"
133 #define START_CHAR 0x600
134 #define END_CHAR 0x700
136 static FriBidiChar table[FRIBIDI_UNICODE_CHARS][4];
137 static char buf[4000];
138 static char tag[sizeof (buf)], buf2[sizeof (buf)];
139 static FriBidiChar minshaped, maxshaped;
146 register FriBidiChar c;
149 for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
150 for (shape = 0; shape < 4; shape++)
159 minshaped = FRIBIDI_UNICODE_CHARS;
166 read_unicode_data_txt (
170 unsigned long c, unshaped, l;
173 while (fgets (buf, sizeof buf, f))
183 if (*s == '#' || *s == '\0' || *s == '\n')
186 i = sscanf (s, "%lx;%*[^;];%*[^;];%*[^;];%*[^;];<%[^;> ]> %lx %[^; ]", &c, tag, &unshaped, buf2);
191 if (i != 3 || c >= FRIBIDI_UNICODE_CHARS || unshaped >= FRIBIDI_UNICODE_CHARS)
192 die3l ("UnicodeData.txt: invalid input at line %ld: %s", l, s);
194 shape = shape_is (tag, arabic_shaping_tags);
197 table[unshaped][shape] = c;
198 if (unshaped < minshaped)
199 minshaped = unshaped;
200 if (unshaped > maxshaped)
201 maxshaped = unshaped;
208 const char *data_file_type[],
209 const char *data_file_name[]
214 for (; data_file_name[0] && data_file_type[0];
215 data_file_name++, data_file_type++)
217 fprintf (stderr, "Reading `%s'\n", data_file_name[0]);
218 if (!(f = fopen (data_file_name[0], "rt")))
219 die2 ("error: cannot open `%s' for reading", data_file_name[0]);
221 if (!strcmp (data_file_type[0], "UnicodeData.txt"))
222 read_unicode_data_txt (f);
224 die2 ("error: unknown data-file type %s", data_file_type[0]);
232 gen_arabic_shaping_tab (
233 int max_depth /* currently unused */,
234 const char *data_file_type[]
237 register FriBidiChar c;
240 if (maxshaped < minshaped)
241 die ("error: no shaping pair found, something wrong with reading input");
244 "Generating `" outputname "', it may take up to a few minutes\n");
245 printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
246 FRIBIDI_VERSION ")\n" " * from the files %s, %s of Unicode version "
247 FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type[0],
251 " use %s(key,shape) to access your table\n\n"
252 " required memory: %ld\n"
254 macro_name, (long)(maxshaped - minshaped + 1) * 4 * sizeof (FriBidiChar));
256 printf ("\n" "/* *IND" "ENT-OFF* */\n\n");
258 printf ("static const FriBidiChar %s[%d][%d] = {\n", table_name, maxshaped - minshaped + 1, 4);
259 for (c = minshaped; c <= maxshaped; c++)
262 for (shape = 0; shape < 4; shape++)
263 printf ("0x%04lx,", (unsigned long)table[c][shape]);
270 printf ("/* *IND" "ENT-ON* */\n\n");
272 printf ("#ifndef FRIBIDI_ACCESS_SHAPE_TABLE\n"
273 "# define FRIBIDI_ACCESS_SHAPE_TABLE(table,min,max,x,shape) \\\n"
274 " (((x)<(min)||(x)>(max))?(x):(table)[(x)-(min)][(shape)])\n"
276 printf ("#define %s(x,shape) "
277 "FRIBIDI_ACCESS_SHAPE_TABLE(%s, 0x%04lx, 0x%04lx, (x), (shape))\n\n",
278 macro_name, table_name, (unsigned long)minshaped, (unsigned long)maxshaped);
280 printf ("/* End of generated " outputname " */\n");
289 const char *data_file_type[] =
290 { "UnicodeData.txt", NULL };
293 die3 ("usage:\n " appname " max-depth /path/to/%s /path/to/%s [junk...]",
294 data_file_type[0], data_file_type[1]);
297 int max_depth = atoi (argv[1]);
298 const char *data_file_name[] = { NULL, NULL };
299 data_file_name[0] = argv[2];
302 die ("invalid depth");
305 read_data (data_file_type, data_file_name);
306 gen_arabic_shaping_tab (max_depth, data_file_type);