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
|
/* FriBidi - Library of BiDi algorithm
* Copyright (C) 1999 Dov Grobgeld
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*======================================================================
// A main program for fribidi.
//
// Dov Grobgeld
//----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <glib.h>
#include "fribidi.h"
#define CASE(s) if(!strcmp(S_, s))
void die(gchar *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
vfprintf(stderr, fmt, ap);
exit(-1);
}
#define UNI_ALEF 0x05D0
#define UNI_TAV 0x05EA
/* defining explicit bidi marks */
#define UNI_LRM 0x200E
#define UNI_RLM 0x200F
#define UNI_LRE 0x202a
#define UNI_RLE 0x202b
#define UNI_PDF 0x202c
#define UNI_LRO 0x202d
#define UNI_RLO 0x202e
void charset_to_unicode(gint char_set,
guchar *s,
/* output */
FriBidiChar *us,
int *length
)
{
int i, j;
int len = strlen(s);
if (char_set == 0)
{
/* Convert A-Z into hebrew characters */
j = 0;
for (i=0; i<len+1; i++)
{
guchar ch = s[i];
/* we use '_' for explicit marks. The list is:
*
* _> LRM
* _< RLM
* _l LRE
* _r RLE
* _p PDF
* _L LRO
* _R RLO
* __ underscore itself
*
*/
if (ch == '_') {
(*length)--;
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 'p': us[j++] = UNI_PDF; break;
case 'L': us[j++] = UNI_LRO; break;
case 'R': us[j++] = UNI_RLO; break;
case '_': us[j++] = '_'; break;
default: i--; break;
}
} else if (ch >= 'A' && ch <= 'Z')
us[j++] = ch - 'A' + UNI_ALEF;
else
us[j++] = s[i];
}
}
else if (char_set == FRIBIDI_CHARSET_8859_6)
fribidi_iso8859_6_to_unicode(s, us);
else if (char_set == FRIBIDI_CHARSET_8859_8)
fribidi_iso8859_8_to_unicode(s, us);
else if (char_set == FRIBIDI_CHARSET_8859_6)
fribidi_iso8859_6_to_unicode(s, us);
else if (char_set == FRIBIDI_CHARSET_ISIRI_3342)
fribidi_isiri_3342_to_unicode(s, us);
else if (char_set == FRIBIDI_CHARSET_CP1255)
fribidi_cp1255_to_unicode(s, us);
else if (char_set == FRIBIDI_CHARSET_CP1256)
fribidi_cp1256_to_unicode(s, us);
else if (char_set == FRIBIDI_CHARSET_UTF8)
*length = fribidi_utf8_to_unicode(s, us);
else
die("Sorry! Not implemented!\n");
}
void unicode_to_charset(gint char_set,
FriBidiChar *us,
int length,
/* output */
gchar *s)
{
if (char_set == 0)
{
/* Convert hebrew characters into A-Z */
int i, j = 0;
for (i=0, j=0; i<length; i++)
{
FriBidiChar ch = us[i];
/*{-BE start*/
switch (ch)
{
case UNI_LRM: s[j++] = '_'; s[j++] = '>'; break;
case UNI_RLM: s[j++] = '_'; s[j++] = '<'; break;
case UNI_LRE: s[j++] = '_'; s[j++] = 'l'; break;
case UNI_RLE: s[j++] = '_'; s[j++] = 'r'; break;
case UNI_PDF: s[j++] = '_'; s[j++] = 'o'; break;
case UNI_LRO: s[j++] = '_'; s[j++] = 'L'; break;
case UNI_RLO: s[j++] = '_'; s[j++] = 'R'; break;
case '_' : s[j++] = '_'; s[j++] = '_'; break;
default:
if (ch >= UNI_ALEF && ch <= UNI_TAV)
s[j++] = ch-UNI_ALEF+'A';
else if (ch < 256)
s[j++] = ch;
else
s[j++] = '¿';
}
}
s[j] = 0;
}
else if (char_set == FRIBIDI_CHARSET_8859_6)
fribidi_unicode_to_iso8859_6(us, length, s);
else if (char_set == FRIBIDI_CHARSET_8859_8)
fribidi_unicode_to_iso8859_8(us, length, s);
else if (char_set == FRIBIDI_CHARSET_ISIRI_3342)
fribidi_unicode_to_isiri_3342(us, length, s);
else if (char_set == FRIBIDI_CHARSET_CP1255)
fribidi_unicode_to_cp1255(us, length, s);
else if (char_set == FRIBIDI_CHARSET_CP1256)
fribidi_unicode_to_cp1256(us, length, s);
else if (char_set == FRIBIDI_CHARSET_UTF8)
(void) fribidi_unicode_to_utf8(us, length, s);
else
die("Sorry! Not implemented!\n");
}
int main(int argc, char *argv[])
{
int argp=1;
FILE *IN;
gboolean text_width = 80;
gboolean do_pad = TRUE;
gboolean do_fill = FALSE;
gint char_set = 0;
guchar *bol_text = NULL, *eol_text = NULL;
FriBidiCharType input_base_direction = FRIBIDI_TYPE_N;
/* Parse the command line */
while(argp < argc && argv[argp][0] == '-')
{
gchar *S_ = argv[argp++];
CASE("-help")
{
printf("fribidi - A command line interface to the fribidi library\n"
"\n"
"Syntax:\n"
" fribidi {options} [filename]\n"
"\n"
"Options:\n"
" -nopad Don't right adjustify RTL lines\n"
" -fill Fill lines up to margin. (Not implemented).\n"
" -width w Specify width of text\n"
" -eol eol End lines with the string given by eol.\n"
" -bol bol Start lines with the string given by bol.\n"
" -rtl Force base direction to RTL.\n"
" -ltr Force base direction to LTR.\n"
" -debug Print debug information.\n"
" -charset cs Specify charset. Default is CapRTL. Available options are:\n"
" * 8859-8 (Hebrew)\n"
" * 8859-6 (Arabic)\n"
" * CP1255 (Hebrew/Yiddish)\n"
" * CP1256 (MS-Arabic)\n"
" * ISIRI-3342 (Farsi)\n"
" * UTF-8\n"
);
exit(0);
}
CASE("-nopad") { do_pad = FALSE; continue; }
CASE("-width") { text_width = atoi(argv[argp++]); continue; }
CASE("-eol") { eol_text = argv[argp++]; continue; }
CASE("-bol") { bol_text = argv[argp++]; continue; }
CASE("-rtl") { input_base_direction = FRIBIDI_TYPE_R; continue; }
CASE("-ltr") { input_base_direction = FRIBIDI_TYPE_L; continue; }
CASE("-fill") { do_fill = TRUE; continue; }
CASE("-debug") { fribidi_set_debug(1); continue; }
CASE("-charset")
{
gchar *S_ = argv[argp++];
while(1) {
CASE("CP1255") { char_set = FRIBIDI_CHARSET_CP1255; break; }
CASE("CP1256") { char_set = FRIBIDI_CHARSET_CP1256; break; }
CASE("8859-6") { char_set = FRIBIDI_CHARSET_8859_8; break; }
CASE("8859-8") { char_set = FRIBIDI_CHARSET_8859_8; break; }
CASE("ISIRI-3342") { char_set = FRIBIDI_CHARSET_ISIRI_3342; break;}
CASE("UTF-8") { char_set = FRIBIDI_CHARSET_UTF8; break; }
die("Unknown char set %s!\n", S_);
}
continue;
}
die("Unknown option %s!\n", S_);
}
/* Open the infile for reading */
if (argp >= argc)
{
IN = stdin;
}
else
{
gchar *fn = argv[argp++];
IN = fopen(fn, "r");
}
/* Read and process input one line at a time */
{
guchar S_[2048];
while(fgets(S_, sizeof(S_), IN))
{
int len = strlen(S_);
FriBidiChar us[2048], out_us[2048];
guchar outstring[2048];
FriBidiCharType base;
int i;
/* chop */
if (S_[len-1] == '\n')
S_[len-1] = '\0';
len--;
charset_to_unicode(char_set, S_, us, &len);
/* Create a bidi string */
base = input_base_direction;
fribidi_log2vis(us,
len,
&base,
/* output */
out_us,
NULL, /* No need for log_to_vis mapping */
NULL, /* No need for vis_to_log mapping */
NULL /* No need for embedding level */
);
/* Convert it to something to print */
unicode_to_charset(char_set, out_us, len, outstring);
if (bol_text)
printf("%s", bol_text);
if (base == FRIBIDI_TYPE_R && do_pad)
for (i=0; i<text_width-strlen(outstring); i++) /* debugged ;) */
printf(" ");
printf("%s", outstring);
if (eol_text)
printf("%s", eol_text);
printf("\n");
} /* one line worth */
}
return 0;
}
|