summaryrefslogtreecommitdiff
path: root/fribidi_char_sets.c
blob: dd5f9d10b2972e36cad958885dac048a0931e697 (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
/* FriBidi - Library of BiDi algorithm
 * Copyright (C) 1999,2000 Dov Grobgeld, and
 * Copyright (C) 2001 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 "fribidi_char_sets.h"
#include <string.h>

typedef struct
{
  /* Convert the character string "s" to unicode string "us" and
     return it's length. */
  int (*charset_to_unicode) (char *s, int length,
			     /* output */
			     FriBidiChar *us);
  /* Convert the unicode string "us" with length "length" to character
     string "s" and return it's length. */
  int (*unicode_to_charset) (FriBidiChar *us, int length,
			     /* output */
			     char *s);
  /* Charset's name. */
  char *name;
  /* Charset's title. */
  char *title;
  /* Comments, if any. */
  char *(*desc) (void);
  /* Some charsets like CapRTL may need to change some fribidis tables, by
     calling this function, they can do this changes. */
  boolean (*enter) (void);
  /* Some charsets like CapRTL may need to change some fribidis tables, by
     calling this function, they can undo their changes, perhaps to enter
     another mode. */
  boolean (*leave) (void);
}
FriBidiCharSetHandler;

/* Each charset must define the functions and strings named below
   (in _FRIBIDI_ADD_CHAR_SET) or define them as NULL, if not any. */

#define _FRIBIDI_ADD_CHAR_SET(char_set) \
  { \
    fribidi_##char_set##_to_unicode, \
    fribidi_unicode_to_##char_set, \
    fribidi_char_set_name_##char_set, \
    fribidi_char_set_title_##char_set, \
    fribidi_char_set_desc_##char_set, \
    fribidi_char_set_enter_##char_set, \
    fribidi_char_set_leave_##char_set, \
  }

FriBidiCharSetHandler fribidi_char_sets[FRIBIDI_CHAR_SETS_NUM + 1] = {
  {NULL, NULL, "Not Implemented", NULL, NULL, NULL},
  _FRIBIDI_ADD_CHAR_SET (utf8),
  _FRIBIDI_ADD_CHAR_SET (cap_rtl),
  _FRIBIDI_ADD_CHAR_SET (iso8859_6),
  _FRIBIDI_ADD_CHAR_SET (iso8859_8),
  _FRIBIDI_ADD_CHAR_SET (cp1255),
  _FRIBIDI_ADD_CHAR_SET (cp1256),
  _FRIBIDI_ADD_CHAR_SET (isiri_3342),
};

#undef _FRIBIDI_ADD_CHAR_SET

/* Return the charset which name is "s". */
FriBidiCharSet
fribidi_parse_charset (char *s)
{
  int i;

  for (i = FRIBIDI_CHAR_SETS_NUM; i; i--)
    if (fribidi_strcasecmp (s, fribidi_char_sets[i].name) == 0)
      return i;

  return FRIBIDI_CHARSET_NOT_FOUND;
}


/* Convert the character string "s" in charset "char_set" to unicode
   string "us" and return it's length. */
int
fribidi_charset_to_unicode (FriBidiCharSet char_set, char *s, int length,
			    /* output */
			    FriBidiChar *us)
{
  fribidi_char_set_enter (char_set);
  return fribidi_char_sets[char_set].charset_to_unicode == NULL ? 0 :
    (*fribidi_char_sets[char_set].charset_to_unicode) (s, length, us);
}

/* Convert the unicode string "us" with length "length" to character
   string "s" in charset "char_set" and return it's length. */
int
fribidi_unicode_to_charset (FriBidiCharSet char_set, FriBidiChar *us,
			    int length,
			    /* output */
			    char *s)
{
  fribidi_char_set_enter (char_set);
  return fribidi_char_sets[char_set].unicode_to_charset == NULL ? 0 :
    (*fribidi_char_sets[char_set].unicode_to_charset) (us, length, s);
}

/* Return the string containing the name of the charset. */
char *
fribidi_char_set_name (FriBidiCharSet char_set)
{
  return fribidi_char_sets[char_set].name == NULL ? (char *) "" :
    fribidi_char_sets[char_set].name;
}

/* Return the string containing the title of the charset. */
char *
fribidi_char_set_title (FriBidiCharSet char_set)
{
  return fribidi_char_sets[char_set].title == NULL ?
    fribidi_char_set_name (char_set) : fribidi_char_sets[char_set].title;
}

/* Return the string containing the comments about the charset, if any. */
char *
fribidi_char_set_desc (FriBidiCharSet char_set)
{
  return fribidi_char_sets[char_set].desc == NULL ?
    NULL : fribidi_char_sets[char_set].desc ();
}

static FriBidiCharSet current_char_set = FRIBIDI_CHARSET_DEFAULT;

/* Some charsets like CapRTL may need to change some fribidis tables, by
   calling this function, they can do this changes. */
boolean
fribidi_char_set_enter (FriBidiCharSet char_set)
{
  if (char_set != current_char_set && fribidi_char_sets[char_set].enter)
    {
      fribidi_char_set_leave (current_char_set);
      current_char_set = char_set;
      return (*fribidi_char_sets[char_set].enter) ();
    }
  else
    return TRUE;
}

/* Some charsets like CapRTL may need to change some fribidis tables, by
   calling this function, they can undo their changes, maybe to enter
   another mode. */
boolean
fribidi_char_set_leave (FriBidiCharSet char_set)
{
  if (char_set == current_char_set && fribidi_char_sets[char_set].leave)
    return (*fribidi_char_sets[char_set].leave) ();
  else
    return TRUE;
}


/* 0.9.x interface, deprecated, just for compatibility. */
int
fribidi_charset_to_unicode_0_9 (FriBidiCharSet char_set, char *s,
				/* output */
				FriBidiChar *us)
{
  return fribidi_charset_to_unicode (char_set, s, strlen (s), us);
}

/* Also old character sets. */

#define FRIBIDI_0_9_TO_UNICODE_DEFINE(cs)	\
	int fribidi_##cs##_to_unicode_0_9 (char *s, FriBidiChar *us)	\
	{	\
	  return fribidi_##cs##_to_unicode (s, strlen(s), us);	\
	}
FRIBIDI_0_9_TO_UNICODE_DEFINE (utf8)
FRIBIDI_0_9_TO_UNICODE_DEFINE (cap_rtl)
FRIBIDI_0_9_TO_UNICODE_DEFINE (iso8859_6)
FRIBIDI_0_9_TO_UNICODE_DEFINE (iso8859_8)
FRIBIDI_0_9_TO_UNICODE_DEFINE (cp1255)
FRIBIDI_0_9_TO_UNICODE_DEFINE (cp1256)
FRIBIDI_0_9_TO_UNICODE_DEFINE (isiri_3342)
#undef FRIBIDI_0_9_TO_UNICODE_DEFINE
#endif