~ubuntu-branches/ubuntu/vivid/fribidi/vivid

« back to all changes in this revision

Viewing changes to fribidi_create_char_types.c

  • Committer: Bazaar Package Importer
  • Author(s): Lior Kaplan
  • Date: 2006-09-16 10:37:10 UTC
  • mfrom: (3.1.3 edgy)
  • Revision ID: james.westby@ubuntu.com-20060916103710-1ktngzyx9azkujhl
Tags: 0.10.7-4
* Fix manual page name section (Closes: #368632)
* debian/control: Upgrade standards version to 3.7.2 (no changes needed).
* debian/copyright: Update Dov Grobgeld's contact address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* FriBidi - Library of BiDi algorithm
 
2
 * Copyright (C) 2001,2002 Behdad Esfahbod. 
 
3
 * 
 
4
 * This library is free software; you can redistribute it and/or 
 
5
 * modify it under the terms of the GNU Lesser General Public 
 
6
 * License as published by the Free Software Foundation; either 
 
7
 * version 2.1 of the License, or (at your option) any later version. 
 
8
 * 
 
9
 * This library is distributed in the hope that it will be useful, 
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 
12
 * Lesser General Public License for more details. 
 
13
 * 
 
14
 * You should have received a copy of the GNU Lesser General Public License 
 
15
 * along with this library, in a file named COPYING; if not, write to the 
 
16
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 
17
 * Boston, MA 02111-1307, USA  
 
18
 * 
 
19
 * For licensing issues, contact <fwpg@sharif.edu>. 
 
20
 */
 
21
 
 
22
#include <stdlib.h>
 
23
#include <stdio.h>
 
24
#include <string.h>
 
25
#include "packtab.h"
 
26
#ifdef HAVE_CONFIG_H
 
27
#include "config.h"
 
28
#endif
 
29
#include "fribidi_unicode.h"
 
30
 
 
31
static void
 
32
err (const char *msg)
 
33
{
 
34
  fprintf (stderr, "fribidi_create_char_types: %s\n", msg);
 
35
  exit (1);
 
36
}
 
37
 
 
38
static void
 
39
err2 (const char *fmt, const char *p)
 
40
{
 
41
  fprintf (stderr, "fribidi_create_char_types: ");
 
42
  fprintf (stderr, fmt, p);
 
43
  fprintf (stderr, "\n");
 
44
  exit (1);
 
45
}
 
46
 
 
47
/* *INDENT-OFF* */
 
48
struct
 
49
{
 
50
  const char *name;
 
51
  int key;
 
52
}
 
53
type_names[] =
 
54
{
 
55
  {"LTR", 0}, {"L", 0},
 
56
  {"RTL", 1}, {"R", 1},
 
57
  {"AL", 2},
 
58
  {"ON", 3},
 
59
  {"BN", 4},
 
60
  {"AN", 5},
 
61
  {"BS", 6}, {"B", 6},
 
62
  {"CS", 7},
 
63
  {"EN", 8},
 
64
  {"ES", 9},
 
65
  {"ET", 10},
 
66
  {"LRE", 11},
 
67
  {"LRO", 12},
 
68
  {"NSM", 13},
 
69
  {"PDF", 14},
 
70
  {"RLE", 15},
 
71
  {"RLO", 16},
 
72
  {"SS", 17}, {"S", 17},
 
73
  {"WS", 18},
 
74
};
 
75
/* *INDENT-ON* */
 
76
 
 
77
#define type_names_count (sizeof (type_names) / sizeof (type_names[0]))
 
78
 
 
79
static char *names[type_names_count];
 
80
 
 
81
static char *unidata_file;
 
82
 
 
83
static char
 
84
get_type (const char *s)
 
85
{
 
86
  int i;
 
87
 
 
88
  for (i = 0; i < type_names_count; i++)
 
89
    if (!strcmp (s, type_names[i].name))
 
90
      return type_names[i].key;
 
91
  err2 ("type name `%s' not found", s);
 
92
  return 0;
 
93
}
 
94
 
 
95
#define table_name "FriBidiPropertyBlock"
 
96
#define key_type_name "FriBidiPropCharType"
 
97
#define macro_name "FRIBIDI_GET_TYPE"
 
98
#define function_name "fribidi_get_type_internal"
 
99
#define char_type_name "FriBidiCharType"
 
100
#define char_name "FriBidiChar"
 
101
#define prop_to_type_name "fribidi_prop_to_type"
 
102
#define default_type "LTR"
 
103
#define export_api "FRIBIDI_API"
 
104
 
 
105
static int table[FRIBIDI_UNICODE_CHARS];
 
106
 
 
107
static void
 
108
init_table (void)
 
109
{
 
110
  int i;
 
111
  register FriBidiChar c;
 
112
  int deftype = get_type (default_type),
 
113
    RTL = get_type ("RTL"), AL = get_type ("AL"), BN = get_type ("BN");
 
114
 
 
115
  for (i = 0; i < type_names_count; i++)
 
116
    names[i] = 0;
 
117
  for (i = type_names_count - 1; i >= 0; i--)
 
118
    names[type_names[i].key] = type_names[i].name;
 
119
 
 
120
  /* initialize table */
 
121
  for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
 
122
    table[c] = deftype;
 
123
 
 
124
  for (c = 0x0590; c < 0x0600; c++)
 
125
    table[c] = RTL;
 
126
  for (c = 0x07C0; c < 0x0900; c++)
 
127
    table[c] = RTL;
 
128
  for (c = 0xFB1D; c < 0xFB50; c++)
 
129
    table[c] = RTL;
 
130
 
 
131
  for (c = 0x0600; c < 0x07C0; c++)
 
132
    table[c] = AL;
 
133
  for (c = 0xFB50; c < 0xFDD0; c++)
 
134
    table[c] = AL;
 
135
  for (c = 0xFDF0; c < 0xFE00; c++)
 
136
    table[c] = AL;
 
137
  for (c = 0xFE70; c < 0xFF00; c++)
 
138
    table[c] = AL;
 
139
 
 
140
  for (c = 0x2060; c < 0x2070; c++)
 
141
    table[c] = BN;
 
142
  for (c = 0xFDD0; c < 0xFDF0; c++)
 
143
    table[c] = BN;
 
144
  for (c = 0xFFF0; c < 0xFFF9; c++)
 
145
    table[c] = BN;
 
146
  for (c = 0xFFFF; c < FRIBIDI_UNICODE_CHARS; c += 0x10000)
 
147
    table[c - 1] = table[c] = BN;
 
148
 
 
149
  if (FRIBIDI_UNICODE_CHARS > 0x10000)
 
150
    {
 
151
      for (c = 0x10800; c < 0x11000; c++)
 
152
        table[c] = RTL;
 
153
 
 
154
      for (c = 0xE0000; c < 0xE1000; c++)
 
155
        table[c] = BN;
 
156
    }
 
157
}
 
158
 
 
159
static void
 
160
read_unicode_data (void)
 
161
{
 
162
  char s[500], tp[10];
 
163
  unsigned int i;
 
164
  FILE *f;
 
165
 
 
166
  printf ("Reading `UnicodeData.txt'\n");
 
167
  if (!(f = fopen (unidata_file, "rt")))
 
168
    err2 ("error: cannot open `%s' for reading", unidata_file);
 
169
  while (fgets (s, sizeof s, f))
 
170
    {
 
171
      sscanf (s, "%x;%*[^;];%*[^;];%*[^;];%[^;]", &i, tp);
 
172
      table[i] = get_type (tp);
 
173
    }
 
174
  fclose (f);
 
175
}
 
176
 
 
177
static char *
 
178
headermacro (const char *file)
 
179
{
 
180
  char *t = strdup (file);
 
181
  char *p = t;
 
182
  while (*p)
 
183
    {
 
184
      if (*p >= 'a' && *p <= 'z')
 
185
        *p += 'A' - 'a';
 
186
      else if ((*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
 
187
        *p = '_';
 
188
      p++;
 
189
    }
 
190
  return t;
 
191
}
 
192
 
 
193
static void
 
194
write_char_type (const char *file, int max_depth)
 
195
{
 
196
  int i;
 
197
  FILE *f;
 
198
  char *FILENAME = headermacro (file);
 
199
 
 
200
  printf ("Writing `%s', it may take a few minutes\n", file);
 
201
  if (!(f = fopen (file, "wt")))
 
202
    err2 ("error: cannot open `%s' for writing", file);
 
203
  fprintf (f, "/*\n"
 
204
           "  This file was automatically created from UnicodeData.txt version %s\n"
 
205
           "  by fribidi_create_char_types\n*/\n\n", FRIBIDI_UNICODE_VERSION);
 
206
 
 
207
  fprintf (f, "#ifndef %s\n#define %s\n\n#include \"fribidi.h\"\n\n",
 
208
           FILENAME, FILENAME);
 
209
 
 
210
  for (i = 0; i < type_names_count; i++)
 
211
    if (names[i])
 
212
      fprintf (f, "#define %s FRIBIDI_PROP_TYPE_%s\n", names[i], names[i]);
 
213
  fprintf (f, "\n");
 
214
 
 
215
  fprintf (f, "#define PACKTAB_UINT8 fribidi_uint8\n");
 
216
  fprintf (f, "#define PACKTAB_UINT16 fribidi_uint16\n");
 
217
  fprintf (f, "#define PACKTAB_UINT32 fribidi_uint32\n");
 
218
 
 
219
  if (!pack_table
 
220
      (table, FRIBIDI_UNICODE_CHARS, sizeof (char), max_depth, 3, names,
 
221
       key_type_name, table_name, macro_name, f))
 
222
    err ("error: insufficient memory, decrease max_depth");
 
223
 
 
224
  for (i = type_names_count - 1; i >= 0; i--)
 
225
    if (names[i])
 
226
      fprintf (f, "#undef %s\n", names[i]);
 
227
 
 
228
  fprintf (f,
 
229
           "/*======================================================================\n"
 
230
           " *  %s() returns the bidi type of a character.\n"
 
231
           " *----------------------------------------------------------------------*/\n"
 
232
           "%s %s\n"
 
233
           "%s (%s uch)\n"
 
234
           "{\n"
 
235
           "  if (uch < 0x%x)\n"
 
236
           "    return %s[(unsigned char)%s (uch)];\n"
 
237
           "  else\n"
 
238
           "    return FRIBIDI_TYPE_%s;\n"
 
239
           "  /* Non-Unicode chars */\n"
 
240
           "}\n"
 
241
           "\n",
 
242
           function_name, export_api, char_type_name, function_name,
 
243
           char_name, FRIBIDI_UNICODE_CHARS, prop_to_type_name, macro_name,
 
244
           default_type);
 
245
  fprintf (f, "\n#endif /* %s */\n", FILENAME);
 
246
 
 
247
  fclose (f);
 
248
}
 
249
 
 
250
int
 
251
main (int argc, char **argv)
 
252
{
 
253
  int max_depth;
 
254
  char file[50], *p;
 
255
  if (argc < 2)
 
256
    err ("usage: fribidi_create_char_types max_depth [UnicodeData.txt path]");
 
257
  p = (argc >= 3) ? argv[2] : "unidata";
 
258
  unidata_file = malloc (50 + strlen (p));
 
259
  sprintf (unidata_file, "%s/UnicodeData.txt", p);
 
260
  max_depth = atoi (argv[1]);
 
261
  if (!max_depth)
 
262
    err ("invalid depth");
 
263
  if (max_depth < 2 || max_depth > 9)
 
264
    err2 ("invalid max_depth `%s', max_depth should be between 2 and 9",
 
265
          argv[1]);
 
266
  sprintf (file, "fribidi_tab_char_type_%d.i", max_depth);
 
267
  init_table ();
 
268
  read_unicode_data ();
 
269
  write_char_type (file, max_depth);
 
270
  return 0;
 
271
}