[API] Rename hb_buffer_ensure() to hb_buffer_pre_allocate()
[profile/ivi/org.tizen.video-player.git] / src / hb-common.c
1 /*
2  * Copyright (C) 2009,2010  Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Red Hat Author(s): Behdad Esfahbod
25  */
26
27 #include "hb-private.h"
28
29 HB_BEGIN_DECLS
30
31
32 /* hb_tag_t */
33
34 hb_tag_t
35 hb_tag_from_string (const char *s)
36 {
37   char tag[4];
38   unsigned int i;
39
40   if (!s)
41     return HB_TAG_NONE;
42
43   for (i = 0; i < 4 && s[i]; i++)
44     tag[i] = s[i];
45   for (; i < 4; i++)
46     tag[i] = ' ';
47
48   return HB_TAG_CHAR4 (tag);
49 }
50
51
52 /* hb_language_t */
53
54 struct _hb_language_t {
55   const char s[1];
56 };
57
58 static const char canon_map[256] = {
59    0,   0,   0,   0,   0,   0,   0,   0,    0,   0,   0,   0,   0,   0,   0,   0,
60    0,   0,   0,   0,   0,   0,   0,   0,    0,   0,   0,   0,   0,   0,   0,   0,
61    0,   0,   0,   0,   0,   0,   0,   0,    0,   0,   0,   0,   0,  '-',  0,   0,
62   '0', '1', '2', '3', '4', '5', '6', '7',  '8', '9',  0,   0,   0,   0,   0,   0,
63   '-', 'a', 'b', 'c', 'd', 'e', 'f', 'g',  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
64   'p', 'q', 'r', 's', 't', 'u', 'v', 'w',  'x', 'y', 'z',  0,   0,   0,   0,  '-',
65    0,  'a', 'b', 'c', 'd', 'e', 'f', 'g',  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
66   'p', 'q', 'r', 's', 't', 'u', 'v', 'w',  'x', 'y', 'z',  0,   0,   0,   0,   0
67 };
68
69 static hb_bool_t
70 lang_equal (const void *v1,
71             const void *v2)
72 {
73   const unsigned char *p1 = v1;
74   const unsigned char *p2 = v2;
75
76   while (canon_map[*p1] && canon_map[*p1] == canon_map[*p2])
77     {
78       p1++, p2++;
79     }
80
81   return (canon_map[*p1] == canon_map[*p2]);
82 }
83
84 #if 0
85 static unsigned int
86 lang_hash (const void *key)
87 {
88   const unsigned char *p = key;
89   unsigned int h = 0;
90   while (canon_map[*p])
91     {
92       h = (h << 5) - h + canon_map[*p];
93       p++;
94     }
95
96   return h;
97 }
98 #endif
99
100
101 hb_language_t
102 hb_language_from_string (const char *str)
103 {
104   static unsigned int num_langs;
105   static unsigned int num_alloced;
106   static hb_language_t *langs;
107   unsigned int i;
108   unsigned char *p;
109
110   /* TODO Use a hash table or something */
111
112   if (!str || !*str)
113     return NULL;
114
115   for (i = 0; i < num_langs; i++)
116     if (lang_equal (str, langs[i]->s))
117       return langs[i];
118
119   if (unlikely (num_langs == num_alloced)) {
120     unsigned int new_alloced = 2 * (8 + num_alloced);
121     hb_language_t *new_langs = realloc (langs, new_alloced * sizeof (langs[0]));
122     if (!new_langs)
123       return NULL;
124     num_alloced = new_alloced;
125     langs = new_langs;
126   }
127
128   langs[i] = (hb_language_t) strdup (str);
129   for (p = (unsigned char *) langs[i]->s; *p; p++)
130     *p = canon_map[*p];
131
132   num_langs++;
133
134   return langs[i];
135 }
136
137 const char *
138 hb_language_to_string (hb_language_t language)
139 {
140   return language->s;
141 }
142
143
144 /* hb_script_t */
145
146 hb_script_t
147 hb_script_from_iso15924_tag (hb_tag_t tag)
148 {
149   /* Be lenient, adjust case (one capital letter followed by three small letters) */
150   tag = (tag & 0xDFDFDFDF) | 0x00202020;
151
152   switch (tag) {
153     case HB_TAG('C','y','r','s'): return HB_SCRIPT_CYRILLIC;
154     case HB_TAG('G','e','o','a'): return HB_SCRIPT_GEORGIAN;
155     case HB_TAG('G','e','o','n'): return HB_SCRIPT_GEORGIAN;
156     case HB_TAG('L','a','t','f'): return HB_SCRIPT_LATIN;
157     case HB_TAG('L','a','t','g'): return HB_SCRIPT_LATIN;
158     case HB_TAG('S','y','r','e'): return HB_SCRIPT_SYRIAC;
159     case HB_TAG('S','y','r','j'): return HB_SCRIPT_SYRIAC;
160     case HB_TAG('S','y','r','n'): return HB_SCRIPT_SYRIAC;
161   }
162
163   /* If it looks right, just use the tag as a script */
164   if (((uint32_t) tag & 0xE0E0E0E0) == 0x40606060)
165     return (hb_script_t) tag;
166
167   /* Otherwise, return unknown */
168   return HB_SCRIPT_UNKNOWN;
169 }
170
171 hb_script_t
172 hb_script_from_string (const char *s)
173 {
174   return hb_script_from_iso15924_tag (hb_tag_from_string (s));
175 }
176
177 hb_tag_t
178 hb_script_to_iso15924_tag (hb_script_t script)
179 {
180   return (hb_tag_t) script;
181 }
182
183 hb_direction_t
184 hb_script_get_horizontal_direction (hb_script_t script)
185 {
186   switch ((hb_tag_t) script)
187   {
188     case HB_SCRIPT_ARABIC:
189     case HB_SCRIPT_HEBREW:
190     case HB_SCRIPT_SYRIAC:
191     case HB_SCRIPT_THAANA:
192
193     /* Unicode-4.0 additions */
194     case HB_SCRIPT_CYPRIOT:
195
196     /* Unicode-5.0 additions */
197     case HB_SCRIPT_PHOENICIAN:
198     case HB_SCRIPT_NKO:
199
200     /* Unicode-5.2 additions */
201     case HB_SCRIPT_AVESTAN:
202     case HB_SCRIPT_IMPERIAL_ARAMAIC:
203     case HB_SCRIPT_INSCRIPTIONAL_PAHLAVI:
204     case HB_SCRIPT_INSCRIPTIONAL_PARTHIAN:
205     case HB_SCRIPT_OLD_SOUTH_ARABIAN:
206     case HB_SCRIPT_OLD_TURKIC:
207     case HB_SCRIPT_SAMARITAN:
208
209     /* Unicode-6.0 additions */
210     case HB_SCRIPT_MANDAIC:
211
212       return HB_DIRECTION_RTL;
213   }
214
215   return HB_DIRECTION_LTR;
216 }
217
218
219 HB_END_DECLS