c20f6bef2d81d11f96ac53459b2ac75b8a0f3aa2
[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 #include "hb-shaper-impl-private.hh"
31
32 #include "hb-graphite2.h"
33
34 #include <graphite2/Segment.h>
35
36
37 HB_SHAPER_DATA_ENSURE_DEFINE(graphite2, face)
38 HB_SHAPER_DATA_ENSURE_DEFINE(graphite2, font)
39
40
41 /*
42  * shaper face data
43  */
44
45 typedef struct hb_graphite2_tablelist_t {
46   struct hb_graphite2_tablelist_t *next;
47   hb_blob_t *blob;
48   unsigned int tag;
49 } hb_graphite2_tablelist_t;
50
51 struct hb_graphite2_shaper_face_data_t {
52   hb_face_t *face;
53   gr_face   *grface;
54   hb_graphite2_tablelist_t *tlist;
55 };
56
57 static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
58 {
59   hb_graphite2_shaper_face_data_t *face_data = (hb_graphite2_shaper_face_data_t *) data;
60   hb_graphite2_tablelist_t *tlist = face_data->tlist;
61
62   hb_blob_t *blob = nullptr;
63
64   for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next)
65     if (p->tag == tag) {
66       blob = p->blob;
67       break;
68     }
69
70   if (unlikely (!blob))
71   {
72     blob = face_data->face->reference_table (tag);
73
74     hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) calloc (1, sizeof (hb_graphite2_tablelist_t));
75     if (unlikely (!p)) {
76       hb_blob_destroy (blob);
77       return nullptr;
78     }
79     p->blob = blob;
80     p->tag = tag;
81
82 retry:
83     hb_graphite2_tablelist_t *tlist = (hb_graphite2_tablelist_t *) hb_atomic_ptr_get (&face_data->tlist);
84     p->next = tlist;
85
86     if (!hb_atomic_ptr_cmpexch (&face_data->tlist, tlist, p))
87       goto retry;
88   }
89
90   unsigned int tlen;
91   const char *d = hb_blob_get_data (blob, &tlen);
92   *len = tlen;
93   return d;
94 }
95
96 hb_graphite2_shaper_face_data_t *
97 _hb_graphite2_shaper_face_data_create (hb_face_t *face)
98 {
99   hb_blob_t *silf_blob = face->reference_table (HB_GRAPHITE2_TAG_SILF);
100   /* Umm, we just reference the table to check whether it exists.
101    * Maybe add better API for this? */
102   if (!hb_blob_get_length (silf_blob))
103   {
104     hb_blob_destroy (silf_blob);
105     return nullptr;
106   }
107   hb_blob_destroy (silf_blob);
108
109   hb_graphite2_shaper_face_data_t *data = (hb_graphite2_shaper_face_data_t *) calloc (1, sizeof (hb_graphite2_shaper_face_data_t));
110   if (unlikely (!data))
111     return nullptr;
112
113   data->face = face;
114   data->grface = gr_make_face (data, &hb_graphite2_get_table, gr_face_preloadAll);
115
116   if (unlikely (!data->grface)) {
117     free (data);
118     return nullptr;
119   }
120
121   return data;
122 }
123
124 void
125 _hb_graphite2_shaper_face_data_destroy (hb_graphite2_shaper_face_data_t *data)
126 {
127   hb_graphite2_tablelist_t *tlist = data->tlist;
128
129   while (tlist)
130   {
131     hb_graphite2_tablelist_t *old = tlist;
132     hb_blob_destroy (tlist->blob);
133     tlist = tlist->next;
134     free (old);
135   }
136
137   gr_face_destroy (data->grface);
138
139   free (data);
140 }
141
142 /*
143  * Since: 0.9.10
144  */
145 gr_face *
146 hb_graphite2_face_get_gr_face (hb_face_t *face)
147 {
148   if (unlikely (!hb_graphite2_shaper_face_data_ensure (face))) return nullptr;
149   return HB_SHAPER_DATA_GET (face)->grface;
150 }
151
152
153 /*
154  * shaper font data
155  */
156
157 struct hb_graphite2_shaper_font_data_t {};
158
159 hb_graphite2_shaper_font_data_t *
160 _hb_graphite2_shaper_font_data_create (hb_font_t *font HB_UNUSED)
161 {
162   return (hb_graphite2_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
163 }
164
165 void
166 _hb_graphite2_shaper_font_data_destroy (hb_graphite2_shaper_font_data_t *data HB_UNUSED)
167 {
168 }
169
170 /*
171  * Since: 0.9.10
172  */
173 gr_font *
174 hb_graphite2_font_get_gr_font (hb_font_t *font)
175 {
176   return nullptr;
177 }
178
179
180 /*
181  * shaper shape_plan data
182  */
183
184 struct hb_graphite2_shaper_shape_plan_data_t {};
185
186 hb_graphite2_shaper_shape_plan_data_t *
187 _hb_graphite2_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan HB_UNUSED,
188                                              const hb_feature_t *user_features HB_UNUSED,
189                                              unsigned int        num_user_features HB_UNUSED,
190                                              const int          *coords HB_UNUSED,
191                                              unsigned int        num_coords HB_UNUSED)
192 {
193   return (hb_graphite2_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
194 }
195
196 void
197 _hb_graphite2_shaper_shape_plan_data_destroy (hb_graphite2_shaper_shape_plan_data_t *data HB_UNUSED)
198 {
199 }
200
201
202 /*
203  * shaper
204  */
205
206 struct hb_graphite2_cluster_t {
207   unsigned int base_char;
208   unsigned int num_chars;
209   unsigned int base_glyph;
210   unsigned int num_glyphs;
211   unsigned int cluster;
212   float advance;
213 };
214
215 hb_bool_t
216 _hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
217                      hb_font_t          *font,
218                      hb_buffer_t        *buffer,
219                      const hb_feature_t *features,
220                      unsigned int        num_features)
221 {
222   hb_face_t *face = font->face;
223   gr_face *grface = HB_SHAPER_DATA_GET (face)->grface;
224
225   const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
226   const char *lang_end = lang ? strchr (lang, '-') : nullptr;
227   int lang_len = lang_end ? lang_end - lang : -1;
228   gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
229
230   for (unsigned int i = 0; i < num_features; i++)
231   {
232     const gr_feature_ref *fref = gr_face_find_fref (grface, features[i].tag);
233     if (fref)
234       gr_fref_set_feature_value (fref, features[i].value, feats);
235   }
236
237   gr_segment *seg = nullptr;
238   const gr_slot *is;
239   unsigned int ci = 0, ic = 0;
240   float curradvx = 0., curradvy = 0.;
241
242   unsigned int scratch_size;
243   hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
244
245   uint32_t *chars = (uint32_t *) scratch;
246
247   for (unsigned int i = 0; i < buffer->len; ++i)
248     chars[i] = buffer->info[i].codepoint;
249
250   /* TODO ensure_native_direction. */
251
252   hb_tag_t script_tag[2];
253   hb_ot_tags_from_script (hb_buffer_get_script (buffer), &script_tag[0], &script_tag[1]);
254
255   seg = gr_make_seg (nullptr, grface,
256                      script_tag[1] == HB_TAG_NONE ? script_tag[0] : script_tag[1],
257                      feats,
258                      gr_utf32, chars, buffer->len,
259                      2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
260
261   if (unlikely (!seg)) {
262     if (feats) gr_featureval_destroy (feats);
263     return false;
264   }
265
266   unsigned int glyph_count = gr_seg_n_slots (seg);
267   if (unlikely (!glyph_count)) {
268     if (feats) gr_featureval_destroy (feats);
269     gr_seg_destroy (seg);
270     buffer->len = 0;
271     return true;
272   }
273
274   buffer->ensure (glyph_count);
275   scratch = buffer->get_scratch_buffer (&scratch_size);
276   while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) +
277           DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size)
278   {
279     if (unlikely (!buffer->ensure (buffer->allocated * 2)))
280     {
281       if (feats) gr_featureval_destroy (feats);
282       gr_seg_destroy (seg);
283       return false;
284     }
285     scratch = buffer->get_scratch_buffer (&scratch_size);
286   }
287
288 #define ALLOCATE_ARRAY(Type, name, len) \
289   Type *name = (Type *) scratch; \
290   { \
291     unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
292     assert (_consumed <= scratch_size); \
293     scratch += _consumed; \
294     scratch_size -= _consumed; \
295   }
296
297   ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
298   ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count);
299
300 #undef ALLOCATE_ARRAY
301
302   memset (clusters, 0, sizeof (clusters[0]) * buffer->len);
303
304   hb_codepoint_t *pg = gids;
305   clusters[0].cluster = buffer->info[0].cluster;
306   float curradv = 0.;
307   if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
308   {
309     curradv = gr_slot_origin_X(gr_seg_first_slot(seg));
310     clusters[0].advance = gr_seg_advance_X(seg) - curradv;
311   }
312   else
313     clusters[0].advance = 0;
314   for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
315   {
316     unsigned int before = gr_slot_before (is);
317     unsigned int after = gr_slot_after (is);
318     *pg = gr_slot_gid (is);
319     pg++;
320     while (clusters[ci].base_char > before && ci)
321     {
322       clusters[ci-1].num_chars += clusters[ci].num_chars;
323       clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
324       clusters[ci-1].advance += clusters[ci].advance;
325       ci--;
326     }
327
328     if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
329     {
330       hb_graphite2_cluster_t *c = clusters + ci + 1;
331       c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
332       c->cluster = buffer->info[c->base_char].cluster;
333       c->num_chars = before - c->base_char;
334       c->base_glyph = ic;
335       c->num_glyphs = 0;
336       if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
337         c->advance = curradv - gr_slot_origin_X(is);
338       else
339       {
340         c->advance = 0;
341         clusters[ci].advance += gr_slot_origin_X(is) - curradv;
342       }
343       ci++;
344       curradv = gr_slot_origin_X(is);
345     }
346     clusters[ci].num_glyphs++;
347
348     if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
349         clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
350   }
351
352   if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
353     clusters[ci].advance += curradv;
354   else
355     clusters[ci].advance += gr_seg_advance_X(seg) - curradv;
356   ci++;
357
358   for (unsigned int i = 0; i < ci; ++i)
359   {
360     for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j)
361     {
362       hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j];
363       info->codepoint = gids[clusters[i].base_glyph + j];
364       info->cluster = clusters[i].cluster;
365       info->var1.i32 = clusters[i].advance;     // all glyphs in the cluster get the same advance
366     }
367   }
368   buffer->len = glyph_count;
369
370   unsigned int upem = hb_face_get_upem (face);
371   float xscale = (float) font->x_scale / upem;
372   float yscale = (float) font->y_scale / upem;
373   yscale *= yscale / xscale;
374   /* Positioning. */
375   unsigned int currclus = (unsigned int) -1;
376   const hb_glyph_info_t *info = buffer->info;
377   hb_glyph_position_t *pPos = hb_buffer_get_glyph_positions (buffer, nullptr);
378   if (!HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
379   {
380     curradvx = 0;
381     for (is = gr_seg_first_slot (seg); is; pPos++, ++info, is = gr_slot_next_in_segment (is))
382     {
383       pPos->x_offset = gr_slot_origin_X (is) * xscale - curradvx;
384       pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
385       if (info->cluster != currclus) {
386         pPos->x_advance = info->var1.i32 * xscale;
387         curradvx += pPos->x_advance;
388         currclus = info->cluster;
389       } else
390         pPos->x_advance = 0.;
391
392       pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
393       curradvy += pPos->y_advance;
394     }
395   }
396   else
397   {
398     curradvx = gr_seg_advance_X(seg) * xscale;
399     for (is = gr_seg_first_slot (seg); is; pPos++, info++, is = gr_slot_next_in_segment (is))
400     {
401       if (info->cluster != currclus)
402       {
403         pPos->x_advance = info->var1.i32 * xscale;
404         curradvx -= pPos->x_advance;
405         currclus = info->cluster;
406       } else
407         pPos->x_advance = 0.;
408
409       pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
410       curradvy -= pPos->y_advance;
411       pPos->x_offset = (gr_slot_origin_X (is) - info->var1.i32) * xscale - curradvx + pPos->x_advance;
412       pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
413     }
414     hb_buffer_reverse_clusters (buffer);
415   }
416
417   if (feats) gr_featureval_destroy (feats);
418   gr_seg_destroy (seg);
419
420   buffer->unsafe_to_break_all ();
421
422   return true;
423 }