Imported Upstream version 0.9.3
[platform/upstream/harfbuzz.git] / src / hb-graphite2.cc
1 /*
2  * Copyright © 2011  Martin Hosken
3  * Copyright © 2011  SIL International
4  * Copyright © 2011,2012  Google, Inc.
5  *
6  *  This is part of HarfBuzz, a text shaping library.
7  *
8  * Permission is hereby granted, without written agreement and without
9  * license or royalty fees, to use, copy, modify, and distribute this
10  * software and its documentation for any purpose, provided that the
11  * above copyright notice and the following two paragraphs appear in
12  * all copies of this software.
13  *
14  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18  * DAMAGE.
19  *
20  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25  *
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #define HB_SHAPER graphite2
30 #define hb_graphite2_shaper_font_data_t gr_font
31 #include "hb-shaper-impl-private.hh"
32
33 #include <graphite2/Font.h>
34 #include <graphite2/Segment.h>
35
36 #include "hb-graphite2.h"
37
38 #include "hb-ot-tag.h"
39
40
41 HB_SHAPER_DATA_ENSURE_DECLARE(graphite2, face)
42 HB_SHAPER_DATA_ENSURE_DECLARE(graphite2, font)
43
44
45 /*
46  * shaper face data
47  */
48
49 typedef struct hb_graphite2_tablelist_t {
50   struct hb_graphite2_tablelist_t *next;
51   hb_blob_t *blob;
52   unsigned int tag;
53 } hb_graphite2_tablelist_t;
54
55 struct hb_graphite2_shaper_face_data_t {
56   hb_face_t *face;
57   gr_face   *grface;
58   hb_graphite2_tablelist_t *tlist;
59 };
60
61 static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
62 {
63   hb_graphite2_shaper_face_data_t *face_data = (hb_graphite2_shaper_face_data_t *) data;
64   hb_graphite2_tablelist_t *tlist = face_data->tlist;
65
66   hb_blob_t *blob = NULL;
67
68   for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next)
69     if (p->tag == tag) {
70       blob = p->blob;
71       break;
72     }
73
74   if (unlikely (!blob))
75   {
76     blob = face_data->face->reference_table (tag);
77
78     hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) calloc (1, sizeof (hb_graphite2_tablelist_t));
79     if (unlikely (!p)) {
80       hb_blob_destroy (blob);
81       return NULL;
82     }
83     p->blob = blob;
84     p->tag = tag;
85
86     /* TODO Not thread-safe, but fairly harmless.
87      * We can do the double-chcked pointer cmpexch thing here. */
88     p->next = face_data->tlist;
89     face_data->tlist = p;
90   }
91
92   unsigned int tlen;
93   const char *d = hb_blob_get_data (blob, &tlen);
94   *len = tlen;
95   return d;
96 }
97
98 hb_graphite2_shaper_face_data_t *
99 _hb_graphite2_shaper_face_data_create (hb_face_t *face)
100 {
101   hb_blob_t *silf_blob = face->reference_table (HB_GRAPHITE2_TAG_SILF);
102   /* Umm, we just reference the table to check whether it exists.
103    * Maybe add better API for this? */
104   if (!hb_blob_get_length (silf_blob))
105   {
106     hb_blob_destroy (silf_blob);
107     return NULL;
108   }
109   hb_blob_destroy (silf_blob);
110
111   hb_graphite2_shaper_face_data_t *data = (hb_graphite2_shaper_face_data_t *) calloc (1, sizeof (hb_graphite2_shaper_face_data_t));
112   if (unlikely (!data))
113     hb_blob_destroy (silf_blob);
114
115   data->face = face;
116   data->grface = gr_make_face (data, &hb_graphite2_get_table, gr_face_default);
117
118   if (unlikely (!data->grface)) {
119     free (data);
120     return NULL;
121   }
122
123   return data;
124 }
125
126 void
127 _hb_graphite2_shaper_face_data_destroy (hb_graphite2_shaper_face_data_t *data)
128 {
129   hb_graphite2_tablelist_t *tlist = data->tlist;
130
131   while (tlist)
132   {
133     hb_graphite2_tablelist_t *old = tlist;
134     hb_blob_destroy (tlist->blob);
135     tlist = tlist->next;
136     free (old);
137   }
138
139   gr_face_destroy (data->grface);
140
141   free (data);
142 }
143
144
145 /*
146  * shaper font data
147  */
148
149 static float hb_graphite2_get_advance (const void *hb_font, unsigned short gid)
150 {
151   return ((hb_font_t *) hb_font)->get_glyph_h_advance (gid);
152 }
153
154 hb_graphite2_shaper_font_data_t *
155 _hb_graphite2_shaper_font_data_create (hb_font_t *font)
156 {
157   if (unlikely (!hb_graphite2_shaper_face_data_ensure (font->face))) return NULL;
158
159   hb_face_t *face = font->face;
160   hb_graphite2_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
161
162   return gr_make_font_with_advance_fn (font->x_scale, font, &hb_graphite2_get_advance, face_data->grface);
163 }
164
165 void
166 _hb_graphite2_shaper_font_data_destroy (hb_graphite2_shaper_font_data_t *data)
167 {
168   gr_font_destroy (data);
169 }
170
171
172 /*
173  * shaper shape_plan data
174  */
175
176 struct hb_graphite2_shaper_shape_plan_data_t {};
177
178 hb_graphite2_shaper_shape_plan_data_t *
179 _hb_graphite2_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan HB_UNUSED,
180                                              const hb_feature_t *user_features HB_UNUSED,
181                                              unsigned int        num_user_features HB_UNUSED)
182 {
183   return (hb_graphite2_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
184 }
185
186 void
187 _hb_graphite2_shaper_shape_plan_data_destroy (hb_graphite2_shaper_shape_plan_data_t *data HB_UNUSED)
188 {
189 }
190
191
192 /*
193  * shaper
194  */
195
196 struct hb_graphite2_cluster_t {
197   unsigned int base_char;
198   unsigned int num_chars;
199   unsigned int base_glyph;
200   unsigned int num_glyphs;
201 };
202
203 hb_bool_t
204 _hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
205                      hb_font_t          *font,
206                      hb_buffer_t        *buffer,
207                      const hb_feature_t *features,
208                      unsigned int        num_features)
209 {
210   hb_face_t *face = font->face;
211   gr_face *grface = HB_SHAPER_DATA_GET (face)->grface;
212   gr_font *grfont = HB_SHAPER_DATA_GET (font);
213
214   unsigned int charlen;
215   hb_glyph_info_t *bufferi = hb_buffer_get_glyph_infos (buffer, &charlen);
216
217   int success = 0;
218
219   const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
220   const char *lang_end = strchr (lang, '-');
221   int lang_len = lang_end ? lang_end - lang : -1;
222   gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
223
224   while (num_features--)
225   {
226     const gr_feature_ref *fref = gr_face_find_fref (grface, features->tag);
227     if (fref)
228       gr_fref_set_feature_value (fref, features->value, feats);
229     features++;
230   }
231
232   /* TODO Use scratch buffer for these. */
233   hb_codepoint_t *gids = NULL, *pg;
234   hb_graphite2_cluster_t *clusters = NULL;
235   gr_segment *seg = NULL;
236   uint32_t *text = NULL;
237   const gr_slot *is;
238   unsigned int ci = 0, ic = 0;
239   float curradvx = 0., curradvy = 0.;
240   unsigned int glyphlen = 0;
241   unsigned int *p;
242
243   text = (uint32_t *) malloc ((charlen + 1) * sizeof (uint32_t));
244   if (!text) goto dieout;
245
246   p = text;
247   for (unsigned int i = 0; i < charlen; ++i)
248     *p++ = bufferi++->codepoint;
249   *p = 0;
250
251   hb_tag_t script_tag[2];
252   hb_ot_tags_from_script (hb_buffer_get_script (buffer), &script_tag[0], &script_tag[1]);
253
254   seg = gr_make_seg (grfont, grface,
255                      script_tag[1] == HB_TAG_NONE ? script_tag[0] : script_tag[1],
256                      feats,
257                      gr_utf32, text, charlen,
258                      2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
259   if (!seg) goto dieout;
260
261   glyphlen = gr_seg_n_slots (seg);
262   clusters = (hb_graphite2_cluster_t *) calloc (charlen, sizeof (hb_graphite2_cluster_t));
263   if (!glyphlen || !clusters) goto dieout;
264
265   gids = (hb_codepoint_t *) malloc (glyphlen * sizeof (hb_codepoint_t));
266   if (!gids) goto dieout;
267
268   pg = gids;
269   for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
270   {
271     unsigned int before = gr_slot_before (is);
272     unsigned int after = gr_slot_after (is);
273     *pg = gr_slot_gid (is);
274     pg++;
275     while (clusters[ci].base_char > before && ci)
276     {
277       clusters[ci-1].num_chars += clusters[ci].num_chars;
278       clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
279       ci--;
280     }
281
282     if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
283     {
284       hb_graphite2_cluster_t *c = clusters + ci + 1;
285       c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
286       c->num_chars = before - c->base_char;
287       c->base_glyph = ic;
288       c->num_glyphs = 0;
289       ci++;
290     }
291     clusters[ci].num_glyphs++;
292
293     if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
294         clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
295   }
296   ci++;
297
298   buffer->clear_output ();
299   for (unsigned int i = 0; i < ci; ++i)
300     buffer->replace_glyphs (clusters[i].num_chars, clusters[i].num_glyphs, gids + clusters[i].base_glyph);
301   buffer->swap_buffers ();
302
303   if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
304     curradvx = gr_seg_advance_X(seg);
305
306   hb_glyph_position_t *pPos;
307   for (pPos = hb_buffer_get_glyph_positions (buffer, NULL), is = gr_seg_first_slot (seg);
308        is; pPos++, is = gr_slot_next_in_segment (is))
309   {
310     pPos->x_offset = gr_slot_origin_X (is) - curradvx;
311     pPos->y_offset = gr_slot_origin_Y (is) - curradvy;
312     pPos->x_advance = gr_slot_advance_X (is, grface, grfont);
313     pPos->y_advance = gr_slot_advance_Y (is, grface, grfont);
314     if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
315       curradvx -= pPos->x_advance;
316     pPos->x_offset = gr_slot_origin_X (is) - curradvx;
317     if (!HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
318       curradvx += pPos->x_advance;
319     pPos->y_offset = gr_slot_origin_Y (is) - curradvy;
320     curradvy += pPos->y_advance;
321   }
322   if (!HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
323     pPos[-1].x_advance += gr_seg_advance_X(seg) - curradvx;
324
325   if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
326     hb_buffer_reverse_clusters (buffer);
327
328   success = 1;
329
330 dieout:
331   if (feats) gr_featureval_destroy (feats);
332   if (gids) free (gids);
333   if (clusters) free (clusters);
334   if (seg) gr_seg_destroy (seg);
335   if (text) free (text);
336   return success;
337 }