Git init
[external/pango1.0.git] / modules / arabic / arabic-fc.c
1 /* Pango
2  * arabic-fc.c: Arabic shaper for FreeType-based backends
3  *
4  * Copyright (C) 2000, 2003, 2007 Red Hat Software
5  * Authors:
6  *   Owen Taylor <otaylor@redhat.com>
7  *   Behdad Esfahbod <behdad@behdad.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "config.h"
26 #include <string.h>
27
28 #include "arabic-ot.h"
29
30 #include "pango-engine.h"
31 #include "pango-utils.h"
32 #include "pangofc-font.h"
33
34 /* No extra fields needed */
35 typedef PangoEngineShape      ArabicEngineFc;
36 typedef PangoEngineShapeClass ArabicEngineFcClass ;
37
38 #define SCRIPT_ENGINE_NAME "ArabicScriptEngineFc"
39 #define RENDER_TYPE PANGO_RENDER_TYPE_FC
40
41 static PangoEngineScriptInfo arabic_scripts[] = {
42   { PANGO_SCRIPT_ARABIC, "*" },
43   { PANGO_SCRIPT_NKO, "*" }
44 };
45
46 static PangoEngineInfo script_engines[] = {
47   {
48     SCRIPT_ENGINE_NAME,
49     PANGO_ENGINE_TYPE_SHAPE,
50     RENDER_TYPE,
51     arabic_scripts, G_N_ELEMENTS(arabic_scripts)
52   }
53 };
54
55 static const PangoOTFeatureMap gsub_features[] =
56 {
57   {"ccmp", PANGO_OT_ALL_GLYPHS},
58   {"locl", PANGO_OT_ALL_GLYPHS},
59   {"isol", isolated},
60   {"fina", final},
61   {"medi", medial},
62   {"init", initial},
63   {"rlig", PANGO_OT_ALL_GLYPHS},
64   {"calt", PANGO_OT_ALL_GLYPHS},
65   {"liga", PANGO_OT_ALL_GLYPHS},
66   /* 'dlig' should be turned-on/off-able.  lets turn off for now. */
67   /* {"dlig", PANGO_OT_ALL_GLYPHS}, */
68   {"cswh", PANGO_OT_ALL_GLYPHS},
69   {"mset", PANGO_OT_ALL_GLYPHS}
70 };
71
72 static const PangoOTFeatureMap gpos_features[] =
73 {
74   {"curs", PANGO_OT_ALL_GLYPHS},
75   {"kern", PANGO_OT_ALL_GLYPHS},
76   {"mark", PANGO_OT_ALL_GLYPHS},
77   {"mkmk", PANGO_OT_ALL_GLYPHS}
78 };
79
80 static void
81 arabic_engine_shape (PangoEngineShape *engine G_GNUC_UNUSED,
82                      PangoFont        *font,
83                      const char       *text,
84                      gint              length,
85                      const PangoAnalysis *analysis,
86                      PangoGlyphString *glyphs)
87 {
88   PangoFcFont *fc_font;
89   FT_Face face;
90   PangoOTRulesetDescription desc;
91   const PangoOTRuleset *ruleset;
92   PangoOTBuffer *buffer;
93   gulong *properties = NULL;
94   glong n_chars;
95   gunichar *wcs;
96   const char *p;
97   int cluster = 0;
98   gboolean rtl = analysis->level % 2 != 0;
99   gboolean reverse;
100   int i;
101
102   g_return_if_fail (font != NULL);
103   g_return_if_fail (text != NULL);
104   g_return_if_fail (length >= 0);
105   g_return_if_fail (analysis != NULL);
106
107   fc_font = PANGO_FC_FONT (font);
108   face = pango_fc_font_lock_face (fc_font);
109   if (!face)
110     return;
111
112   buffer = pango_ot_buffer_new (fc_font);
113   pango_ot_buffer_set_rtl (buffer, rtl);
114   pango_ot_buffer_set_zero_width_marks (buffer, TRUE);
115
116   wcs = g_utf8_to_ucs4_fast (text, length, &n_chars);
117   properties = g_new0 (gulong, n_chars);
118
119   reverse = !rtl ^ (analysis->gravity == PANGO_GRAVITY_NORTH || analysis->gravity == PANGO_GRAVITY_WEST);
120   Arabic_Assign_Properties (wcs, properties, n_chars, reverse);
121
122   g_free (wcs);
123
124   p = text;
125   for (i=0; i < n_chars; i++)
126     {
127       gunichar wc;
128       PangoGlyph glyph;
129
130       wc = g_utf8_get_char (p);
131
132       if (g_unichar_type (wc) != G_UNICODE_NON_SPACING_MARK)
133         cluster = p - text;
134
135       if (pango_is_zero_width (wc))
136         glyph = PANGO_GLYPH_EMPTY;
137       else
138         {
139           gunichar c = wc;
140
141           if (analysis->level % 2)
142             g_unichar_get_mirror_char (c, &c);
143
144           /* Hack - Microsoft fonts are strange and don't contain the
145            * correct rules to shape ARABIC LETTER FARSI YEH in
146            * medial/initial position. It looks identical to ARABIC LETTER
147            * YEH in these positions, so we substitute if the font contains
148            * ARABIC LETTER YEH
149            */
150           if (c == 0x6cc &&
151               ((properties[i] & (initial | medial)) != (initial | medial)) &&
152               pango_fc_font_has_char (fc_font, 0x64a))
153             c = 0x64a;
154
155           glyph = pango_fc_font_get_glyph (fc_font, c);
156         }
157
158       if (!glyph)
159         glyph = PANGO_GET_UNKNOWN_GLYPH (wc);
160
161       pango_ot_buffer_add_glyph (buffer, glyph, properties[i], cluster);
162
163       p = g_utf8_next_char (p);
164     }
165
166   g_free (properties);
167
168   desc.script = analysis->script;
169   desc.language = analysis->language;
170
171   desc.n_static_gsub_features = G_N_ELEMENTS (gsub_features);
172   desc.static_gsub_features = gsub_features;
173   desc.n_static_gpos_features = G_N_ELEMENTS (gpos_features);
174   desc.static_gpos_features = gpos_features;
175
176   /* TODO populate other_features from analysis->extra_attrs */
177   desc.n_other_features = 0;
178   desc.other_features = NULL;
179
180   ruleset = pango_ot_ruleset_get_for_description (pango_ot_info_get (face), &desc);
181
182   pango_ot_ruleset_substitute (ruleset, buffer);
183   pango_ot_ruleset_position (ruleset, buffer);
184   pango_ot_buffer_output (buffer, glyphs);
185
186   pango_ot_buffer_destroy (buffer);
187
188   pango_fc_font_unlock_face (fc_font);
189 }
190
191 static void
192 arabic_engine_fc_class_init (PangoEngineShapeClass *class)
193 {
194   class->script_shape = arabic_engine_shape;
195 }
196
197 PANGO_ENGINE_SHAPE_DEFINE_TYPE (ArabicEngineFc, arabic_engine_fc,
198                                 arabic_engine_fc_class_init, NULL)
199
200 void
201 PANGO_MODULE_ENTRY(init) (GTypeModule *module)
202 {
203   arabic_engine_fc_register_type (module);
204 }
205
206 void
207 PANGO_MODULE_ENTRY(exit) (void)
208 {
209 }
210
211 void
212 PANGO_MODULE_ENTRY(list) (PangoEngineInfo **engines,
213                           int              *n_engines)
214 {
215   *engines = script_engines;
216   *n_engines = G_N_ELEMENTS (script_engines);
217 }
218
219 PangoEngine *
220 PANGO_MODULE_ENTRY(create) (const char *id)
221 {
222   if (!strcmp (id, SCRIPT_ENGINE_NAME))
223     return g_object_new (arabic_engine_fc_type, NULL);
224   else
225     return NULL;
226 }