summaryrefslogtreecommitdiff
path: root/fribidi_char_sets_cap_rtl.c
blob: 73231cfcb2ff733ae3ac46cf057bb0c7a97f17fe (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* FriBidi - Library of BiDi algorithm
 * Copyright (C) 1999,2000 Dov Grobgeld, and
 * 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 <dov@imagic.weizmann.ac.il> and
 * <fwpg@sharif.edu>.
 */

#include "fribidi_config.h"
#ifndef FRIBIDI_NO_CHARSETS

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "fribidi.h"

FRIBIDI_API FriBidiCharType fribidi_get_type_internal (FriBidiChar uch);

#define WS FRIBIDI_PROP_TYPE_WS
#define BS FRIBIDI_PROP_TYPE_BS
#define EO FRIBIDI_PROP_TYPE_EO
#define CTL FRIBIDI_PROP_TYPE_CTL
#define LRE FRIBIDI_PROP_TYPE_LRE
#define RLE FRIBIDI_PROP_TYPE_RLE
#define ES FRIBIDI_PROP_TYPE_ES
#define LRO FRIBIDI_PROP_TYPE_LRO
#define RLO FRIBIDI_PROP_TYPE_RLO
#define AL FRIBIDI_PROP_TYPE_AL
#define SS FRIBIDI_PROP_TYPE_SS
#define ET FRIBIDI_PROP_TYPE_ET
#define NSM FRIBIDI_PROP_TYPE_NSM
#define LTR FRIBIDI_PROP_TYPE_LTR
#define ON FRIBIDI_PROP_TYPE_ON
#define AN FRIBIDI_PROP_TYPE_AN
#define BN FRIBIDI_PROP_TYPE_BN
#define RTL FRIBIDI_PROP_TYPE_RTL
#define CS FRIBIDI_PROP_TYPE_CS
#define PDF FRIBIDI_PROP_TYPE_PDF
#define EN FRIBIDI_PROP_TYPE_EN

static FriBidiPropCharType CapRTLCharTypes[] = {
/* *INDENT-OFF* */
  ON, ON, ON, ON, LTR,RTL,ON, ON, ON, ON, ON, ON, ON, BS, RLO,RLE, /* 00-0f */
  LRO,LRE,PDF,WS, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,  /* 10-1f */
  WS, ON, ON, ON, ET, ON, ON, ON, ON, ON, ON, ET, CS, ON, ES, ES,  /* 20-2f */
  EN, EN, EN, EN, EN, EN, AN, AN, AN, AN, CS, ON, ON, ON, ON, ON,  /* 30-3f */
  RTL,AL, AL, AL, AL, AL, AL, RTL,RTL,RTL,RTL,RTL,RTL,RTL,RTL,RTL, /* 40-4f */
  RTL,RTL,RTL,RTL,RTL,RTL,RTL,RTL,RTL,RTL,RTL,ON, BS, ON, ON, ON,  /* 50-5f */
  NSM,LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR, /* 60-6f */
  LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR,LTR,ON, SS, ON, WS, ON,  /* 70-7f */
/* *INDENT-ON* */
};

#undef WS
#undef BS
#undef EO
#undef CTL
#undef LRE
#undef RLE
#undef ES
#undef LRO
#undef RLO
#undef AL
#undef SS
#undef ET
#undef NSM
#undef LTR
#undef ON
#undef AN
#undef BN
#undef RTL
#undef CS
#undef PDF
#undef EN

#define CAPRTL_CHARS (sizeof CapRTLCharTypes / sizeof CapRTLCharTypes[0])

static FriBidiChar *caprtl_to_unicode = NULL;

char
fribidi_unicode_to_cap_rtl_c (FriBidiChar uch)
{
  int i;
  for (i = 0; i < CAPRTL_CHARS; i++)
    if (uch == caprtl_to_unicode[i])
      return (char) i;
  return '?';
}

int
fribidi_cap_rtl_to_unicode (char *s,
			    int len,
			    FriBidiChar *us)
{
  int i, j;

  j = 0;
  for (i = 0; i < len; i++)
    {
      char ch;

      ch = s[i];
      if (ch == '_')
	{
	  switch (ch = s[++i])
	    {
	    case '>':
	      us[j++] = UNI_LRM;
	      break;
	    case '<':
	      us[j++] = UNI_RLM;
	      break;
	    case 'l':
	      us[j++] = UNI_LRE;
	      break;
	    case 'r':
	      us[j++] = UNI_RLE;
	      break;
	    case 'o':
	      us[j++] = UNI_PDF;
	      break;
	    case 'L':
	      us[j++] = UNI_LRO;
	      break;
	    case 'R':
	      us[j++] = UNI_RLO;
	      break;
	    case '_':
	      us[j++] = '_';
	      break;
	    default:
	      us[j++] = '_';
	      i--;
	      break;
	    }
	}
      else
	us[j++] = caprtl_to_unicode[(int) s[i]];
    }

  return j;
}

int
fribidi_unicode_to_cap_rtl (FriBidiChar *us,
			    int length,
			    char *s)
{
  int i, j;

  j = 0;
  for (i = 0; i < length; i++)
    {
      FriBidiChar ch = us[i];
      if (!FRIBIDI_IS_EXPLICIT (fribidi_get_type_internal (ch)) && ch != '_'
	  && ch != UNI_LRM && ch != UNI_RLM)
	s[j++] = fribidi_unicode_to_cap_rtl_c (ch);
      else
	{
	  s[j++] = '_';
	  switch (ch)
	    {
	    case UNI_LRM:
	      s[j++] = '>';
	      break;
	    case UNI_RLM:
	      s[j++] = '<';
	      break;
	    case UNI_LRE:
	      s[j++] = 'l';
	      break;
	    case UNI_RLE:
	      s[j++] = 'r';
	      break;
	    case UNI_PDF:
	      s[j++] = 'o';
	      break;
	    case UNI_LRO:
	      s[j++] = 'L';
	      break;
	    case UNI_RLO:
	      s[j++] = 'R';
	      break;
	    case '_':
	      s[j++] = '_';
	      break;
	    default:
	      j--;
	      if (ch < 256)
		s[j++] = fribidi_unicode_to_cap_rtl_c (ch);
	      else
		s[j++] = '?';
	      break;
	    }
	}
    }
  s[j] = 0;

  return j;
}

char *
fribidi_char_set_desc_cap_rtl (void)
{
  static char *s = 0;
  int l, i, j;

  if (s)
    return s;

  l = 4000;
  s = (char *) fribidi_malloc (NULL, l);
  i = 0;
  i += snprintf (s + i, l - i,
		 "CapRTL is a character set for testing with the reference\n"
		 "implementation, with explicit marks escape strings, and\n"
		 "the property that contains all unicode character types in\n"
		 "ASCII range 1-127.\n"
		 "\n"
		 "Warning: CapRTL character types are subject to change.\n"
		 "\n" "CapRTL's character types:\n");
  for (j = 0; j < CAPRTL_CHARS; j++)
    {
      if (j % 4 == 0)
	s[i++] = '\n';
      i += snprintf (s + i, l - i, "  * 0x%02x %c%c %-3s ", j,
		     j < 0x20 ? '^' : ' ',
		     j < 0x20 ? j + '@' : j < 0x7f ? j : ' ',
		     fribidi_type_name (fribidi_prop_to_type
					[(unsigned char)
					 CapRTLCharTypes[j]]));
    }
  i += snprintf (s + i, l - i,
		 "\n\n"
		 "Escape sequences:\n"
		 "  Character `_' is used to escape explicit marks. The list is:\n"
		 "    * _>  LRM\n"
		 "    * _<  RLM\n"
		 "    * _l  LRE\n"
		 "    * _r  RLE\n"
		 "    * _L  LRO\n"
		 "    * _R  RLO\n" "    * _o  PDF\n" "    * __  `_' itself\n"
		 "\n");
  return s;
}

fribidi_boolean
fribidi_char_set_enter_cap_rtl (void)
{
  if (!caprtl_to_unicode)
    {
      int request[FRIBIDI_TYPES_COUNT + 1];
      int i, count;

      caprtl_to_unicode =
	(FriBidiChar *) fribidi_malloc (NULL,
					CAPRTL_CHARS *
					sizeof caprtl_to_unicode[0]);
      for (i = 0; i < FRIBIDI_TYPES_COUNT; i++)
	request[i] = 0;
      for (i = 0; i < CAPRTL_CHARS; i++)
	if (fribidi_get_mirror_char (NULL, i, NULL))
	  caprtl_to_unicode[i] = i;
      for (count = 0, i = 0; i < CAPRTL_CHARS; i++)
	if (caprtl_to_unicode[i] == 0)
	  {
	    request[(unsigned char) CapRTLCharTypes[i]]++;
	    count++;
	  }
      for (i = 1; i < 0x10000 && count; i++)	/* Assign BMP chars to CapRTL entries */
	if (!fribidi_get_mirror_char (NULL, i, NULL))
	  {
	    int j, k;
	    for (j = 0; j < FRIBIDI_TYPES_COUNT; j++)
	      if (fribidi_prop_to_type[j] == fribidi_get_type_internal (i))
		break;
	    if (!request[j])	/* Do not need this type */
	      continue;
	    for (k = 0; k < CAPRTL_CHARS; k++)
	      if (!caprtl_to_unicode[k] && j == CapRTLCharTypes[k])
		break;
	    if (k < CAPRTL_CHARS)
	      {
		request[j]--;
		count--;
		caprtl_to_unicode[k] = i;
	      }
	  }
    }

  return FRIBIDI_TRUE;
}

fribidi_boolean
fribidi_char_set_leave_cap_rtl (void)
{
  return FRIBIDI_TRUE;
}

#endif