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