summaryrefslogtreecommitdiff
path: root/fribidi_create_mirroring.c
blob: a1aef1f8d15b0b557b1ddca99d3ff487e8207acb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* FriBidi - Library of BiDi algorithm
 * Copyright (C) 2001,2002 Behdad Esfahbod. 
 * 
 * This library is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version. 
 * 
 * This library is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 * Lesser General Public License for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this library, in a file named COPYING; if not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA  
 * 
 * For licensing issues, contact <fwpg@sharif.edu>. 
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "fribidi_unicode.h"

static void
err2 (char *fmt,
      char *p)
{
  fprintf (stderr, "fribidi_create_mirroring: error: ");
  fprintf (stderr, fmt, p);
  fprintf (stderr, "\n");
  exit (1);
}

static int table[0x110000];
static char *bidi_mirroring_file;

static int mirroring_count;

static void
read_bidi_mirroring ()
{
  char s[500];
  unsigned int i, j;
  FILE *f;

  for (i = 0; i < 0x110000; i++)
    table[i] = 0;
  mirroring_count = 0;
  printf ("Reading `BidiMirroring.txt'\n");
  if (!(f = fopen (bidi_mirroring_file, "rt")))
    err2 ("cannot open `%s' for reading", bidi_mirroring_file);
/*  fgets (s, sizeof s, f);
  sscanf (s, "# BidiMirroring-%s.txt", bidi_mirroring_version = malloc (20));*/
  while (fgets (s, sizeof s, f))
    {
      if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
	continue;
      sscanf (s, "%x; %x", &i, &j);
      table[i] = j;
      table[j] = i;
      mirroring_count++;
    }
  fclose (f);
}

static char *
headermacro (char *file)
{
  char *t = strdup (file);
  char *p = t;
  while (*p)
    {
      if (*p >= 'a' && *p <= 'z')
	*p += 'A' - 'a';
      else if ((*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
	*p = '_';
      p++;
    }
  return t;
}

static void
write_mirror (char *file)
{
  int i;
  FILE *f;
  char *FILENAME = headermacro (file);

  printf ("Writing `%s'\n", file);
  if (!(f = fopen (file, "wt")))
    err2 ("cannot open `%s' for writing", file);
  fprintf (f, "/*\n"
	   "  This file was automatically created from BidiMirroring.txt, version %s\n"
	   "  by fribidi_create_mirroring\n*/\n\n", FRIBIDI_UNICODE_VERSION);
  fprintf (f, "#ifndef %s\n#define %s\n\n#include \"fribidi.h\"\n\n",
	   FILENAME, FILENAME);
  fprintf (f, "/*\n"
	   "  Mirrored characters include all the characters in the Unicode list\n"
	   "  that have been declared as being mirrored and that have a mirrored\n"
	   "  equivalent.\n"
	   "\n"
	   "  There are lots of characters that are designed as being mirrored\n"
	   "  but do not have any mirrored glyph, e.g. the sign for there exist.\n");
  fprintf (f,
	   "  Are these used in Arabic? That is are all the mathematical signs\n"
	   "  that are assigned to be mirrorable actually mirrored in Arabic?\n"
	   "  If that is the case, we'll change the below code to include also\n"
	   "  characters that mirror to themself. It will then be the responsibility\n"
	   "  of the display engine to actually mirror these.\n" "*/\n\n");
  fprintf (f, "/* *INDENT-OFF" "* */\n\n");
  fprintf (f, "static const struct\n"
	   "{\n"
	   "  FriBidiChar ch, mirrored_ch;\n"
	   "}\n" "FriBidiMirroredChars[] =\n" "{\n");
  for (i = 0; i < 0x110000; i++)
    if (table[i])
      fprintf (f, "  {0x%04X, 0x%04X},\n", i, table[i]);
  fprintf (f, "} ;\n\n");
  fprintf (f, "/* *INDE" "NT-ON* */\n\n");
  fprintf (f, "static const int nFriBidiMirroredChars = %d;\n\n", mirroring_count);
  fprintf (f, "\n#endif /* %s */\n", FILENAME);
  fclose (f);
}

int
main (int argc,
      char **argv)
{
  char *p;

  p = (argc >= 2) ? argv[1] : "unidata";
  bidi_mirroring_file = malloc (50 + strlen (p));
  sprintf (bidi_mirroring_file, "%s/BidiMirroring.txt", p);
  read_bidi_mirroring ();
  write_mirror ("fribidi_tab_mirroring.i");
  return 0;
}