Imported Upstream version 0.9.3
[platform/upstream/harfbuzz.git] / src / hb-ot-layout.cc
1 /*
2  * Copyright © 1998-2004  David Turner and Werner Lemberg
3  * Copyright © 2006  Behdad Esfahbod
4  * Copyright © 2007,2008,2009  Red Hat, Inc.
5  * Copyright © 2012  Google, Inc.
6  *
7  *  This is part of HarfBuzz, a text shaping library.
8  *
9  * Permission is hereby granted, without written agreement and without
10  * license or royalty fees, to use, copy, modify, and distribute this
11  * software and its documentation for any purpose, provided that the
12  * above copyright notice and the following two paragraphs appear in
13  * all copies of this software.
14  *
15  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
16  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
17  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
18  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19  * DAMAGE.
20  *
21  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
22  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
23  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
24  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
25  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26  *
27  * Red Hat Author(s): Behdad Esfahbod
28  * Google Author(s): Behdad Esfahbod
29  */
30
31 #include "hb-ot-layout-private.hh"
32
33 #include "hb-ot-layout-gdef-table.hh"
34 #include "hb-ot-layout-gsub-table.hh"
35 #include "hb-ot-layout-gpos-table.hh"
36 #include "hb-ot-maxp-table.hh"
37
38
39 #include <stdlib.h>
40 #include <string.h>
41
42
43 HB_SHAPER_DATA_ENSURE_DECLARE(ot, face)
44
45 hb_ot_layout_t *
46 _hb_ot_layout_create (hb_face_t *face)
47 {
48   hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
49   if (unlikely (!layout))
50     return NULL;
51
52   layout->gdef_blob = Sanitizer<GDEF>::sanitize (face->reference_table (HB_OT_TAG_GDEF));
53   layout->gdef = Sanitizer<GDEF>::lock_instance (layout->gdef_blob);
54
55   layout->gsub_blob = Sanitizer<GSUB>::sanitize (face->reference_table (HB_OT_TAG_GSUB));
56   layout->gsub = Sanitizer<GSUB>::lock_instance (layout->gsub_blob);
57
58   layout->gpos_blob = Sanitizer<GPOS>::sanitize (face->reference_table (HB_OT_TAG_GPOS));
59   layout->gpos = Sanitizer<GPOS>::lock_instance (layout->gpos_blob);
60
61   layout->gsub_lookup_count = layout->gsub->get_lookup_count ();
62   layout->gpos_lookup_count = layout->gpos->get_lookup_count ();
63
64   layout->gsub_digests = (hb_set_digest_t *) calloc (layout->gsub->get_lookup_count (), sizeof (hb_set_digest_t));
65   layout->gpos_digests = (hb_set_digest_t *) calloc (layout->gpos->get_lookup_count (), sizeof (hb_set_digest_t));
66
67   if (unlikely ((layout->gsub_lookup_count && !layout->gsub_digests) ||
68                 (layout->gpos_lookup_count && !layout->gpos_digests)))
69   {
70     _hb_ot_layout_destroy (layout);
71     return NULL;
72   }
73
74   for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
75     layout->gsub->add_coverage (&layout->gsub_digests[i], i);
76   for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
77     layout->gpos->add_coverage (&layout->gpos_digests[i], i);
78
79   return layout;
80 }
81
82 void
83 _hb_ot_layout_destroy (hb_ot_layout_t *layout)
84 {
85   hb_blob_destroy (layout->gdef_blob);
86   hb_blob_destroy (layout->gsub_blob);
87   hb_blob_destroy (layout->gpos_blob);
88
89   free (layout->gsub_digests);
90   free (layout->gpos_digests);
91
92   free (layout);
93 }
94
95 static inline const GDEF&
96 _get_gdef (hb_face_t *face)
97 {
98   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(GDEF);
99   return *hb_ot_layout_from_face (face)->gdef;
100 }
101 static inline const GSUB&
102 _get_gsub (hb_face_t *face)
103 {
104   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(GSUB);
105   return *hb_ot_layout_from_face (face)->gsub;
106 }
107 static inline const GPOS&
108 _get_gpos (hb_face_t *face)
109 {
110   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(GPOS);
111   return *hb_ot_layout_from_face (face)->gpos;
112 }
113
114
115 /*
116  * GDEF
117  */
118
119 hb_bool_t
120 hb_ot_layout_has_glyph_classes (hb_face_t *face)
121 {
122   return _get_gdef (face).has_glyph_classes ();
123 }
124
125
126 unsigned int
127 hb_ot_layout_get_attach_points (hb_face_t      *face,
128                                 hb_codepoint_t  glyph,
129                                 unsigned int    start_offset,
130                                 unsigned int   *point_count /* IN/OUT */,
131                                 unsigned int   *point_array /* OUT */)
132 {
133   return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
134 }
135
136 unsigned int
137 hb_ot_layout_get_ligature_carets (hb_font_t      *font,
138                                   hb_direction_t  direction,
139                                   hb_codepoint_t  glyph,
140                                   unsigned int    start_offset,
141                                   unsigned int   *caret_count /* IN/OUT */,
142                                   int            *caret_array /* OUT */)
143 {
144   return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
145 }
146
147
148 /*
149  * GSUB/GPOS
150  */
151
152 static const GSUBGPOS&
153 get_gsubgpos_table (hb_face_t *face,
154                     hb_tag_t   table_tag)
155 {
156   switch (table_tag) {
157     case HB_OT_TAG_GSUB: return _get_gsub (face);
158     case HB_OT_TAG_GPOS: return _get_gpos (face);
159     default:             return Null(GSUBGPOS);
160   }
161 }
162
163
164 unsigned int
165 hb_ot_layout_table_get_script_tags (hb_face_t    *face,
166                                     hb_tag_t      table_tag,
167                                     unsigned int  start_offset,
168                                     unsigned int *script_count /* IN/OUT */,
169                                     hb_tag_t     *script_tags /* OUT */)
170 {
171   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
172
173   return g.get_script_tags (start_offset, script_count, script_tags);
174 }
175
176 hb_bool_t
177 hb_ot_layout_table_find_script (hb_face_t    *face,
178                                 hb_tag_t      table_tag,
179                                 hb_tag_t      script_tag,
180                                 unsigned int *script_index)
181 {
182   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
183   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
184
185   if (g.find_script_index (script_tag, script_index))
186     return true;
187
188   /* try finding 'DFLT' */
189   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
190     return false;
191
192   /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
193    * including many versions of DejaVu Sans Mono! */
194   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
195     return false;
196
197   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
198   return false;
199 }
200
201 hb_bool_t
202 hb_ot_layout_table_choose_script (hb_face_t      *face,
203                                   hb_tag_t        table_tag,
204                                   const hb_tag_t *script_tags,
205                                   unsigned int   *script_index,
206                                   hb_tag_t       *chosen_script)
207 {
208   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
209   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
210
211   while (*script_tags)
212   {
213     if (g.find_script_index (*script_tags, script_index)) {
214       if (chosen_script)
215         *chosen_script = *script_tags;
216       return true;
217     }
218     script_tags++;
219   }
220
221   /* try finding 'DFLT' */
222   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
223     if (chosen_script)
224       *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
225     return false;
226   }
227
228   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
229   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) {
230     if (chosen_script)
231       *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE;
232     return false;
233   }
234
235   /* try with 'latn'; some old fonts put their features there even though
236      they're really trying to support Thai, for example :( */
237 #define HB_OT_TAG_LATIN_SCRIPT          HB_TAG ('l', 'a', 't', 'n')
238   if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) {
239     if (chosen_script)
240       *chosen_script = HB_OT_TAG_LATIN_SCRIPT;
241     return false;
242   }
243
244   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
245   if (chosen_script)
246     *chosen_script = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
247   return false;
248 }
249
250 unsigned int
251 hb_ot_layout_table_get_feature_tags (hb_face_t    *face,
252                                      hb_tag_t      table_tag,
253                                      unsigned int  start_offset,
254                                      unsigned int *feature_count /* IN/OUT */,
255                                      hb_tag_t     *feature_tags /* OUT */)
256 {
257   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
258
259   return g.get_feature_tags (start_offset, feature_count, feature_tags);
260 }
261
262
263 unsigned int
264 hb_ot_layout_script_get_language_tags (hb_face_t    *face,
265                                        hb_tag_t      table_tag,
266                                        unsigned int  script_index,
267                                        unsigned int  start_offset,
268                                        unsigned int *language_count /* IN/OUT */,
269                                        hb_tag_t     *language_tags /* OUT */)
270 {
271   const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
272
273   return s.get_lang_sys_tags (start_offset, language_count, language_tags);
274 }
275
276 hb_bool_t
277 hb_ot_layout_script_find_language (hb_face_t    *face,
278                                    hb_tag_t      table_tag,
279                                    unsigned int  script_index,
280                                    hb_tag_t      language_tag,
281                                    unsigned int *language_index)
282 {
283   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
284   const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
285
286   if (s.find_lang_sys_index (language_tag, language_index))
287     return true;
288
289   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
290   if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
291     return false;
292
293   if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
294   return false;
295 }
296
297 hb_bool_t
298 hb_ot_layout_language_get_required_feature_index (hb_face_t    *face,
299                                                   hb_tag_t      table_tag,
300                                                   unsigned int  script_index,
301                                                   unsigned int  language_index,
302                                                   unsigned int *feature_index)
303 {
304   const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
305
306   if (feature_index) *feature_index = l.get_required_feature_index ();
307
308   return l.has_required_feature ();
309 }
310
311 unsigned int
312 hb_ot_layout_language_get_feature_indexes (hb_face_t    *face,
313                                            hb_tag_t      table_tag,
314                                            unsigned int  script_index,
315                                            unsigned int  language_index,
316                                            unsigned int  start_offset,
317                                            unsigned int *feature_count /* IN/OUT */,
318                                            unsigned int *feature_indexes /* OUT */)
319 {
320   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
321   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
322
323   return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
324 }
325
326 unsigned int
327 hb_ot_layout_language_get_feature_tags (hb_face_t    *face,
328                                         hb_tag_t      table_tag,
329                                         unsigned int  script_index,
330                                         unsigned int  language_index,
331                                         unsigned int  start_offset,
332                                         unsigned int *feature_count /* IN/OUT */,
333                                         hb_tag_t     *feature_tags /* OUT */)
334 {
335   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
336   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
337
338   ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
339   unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
340
341   if (feature_tags) {
342     unsigned int count = *feature_count;
343     for (unsigned int i = 0; i < count; i++)
344       feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
345   }
346
347   return ret;
348 }
349
350
351 hb_bool_t
352 hb_ot_layout_language_find_feature (hb_face_t    *face,
353                                     hb_tag_t      table_tag,
354                                     unsigned int  script_index,
355                                     unsigned int  language_index,
356                                     hb_tag_t      feature_tag,
357                                     unsigned int *feature_index)
358 {
359   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
360   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
361   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
362
363   unsigned int num_features = l.get_feature_count ();
364   for (unsigned int i = 0; i < num_features; i++) {
365     unsigned int f_index = l.get_feature_index (i);
366
367     if (feature_tag == g.get_feature_tag (f_index)) {
368       if (feature_index) *feature_index = f_index;
369       return true;
370     }
371   }
372
373   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
374   return false;
375 }
376
377 unsigned int
378 hb_ot_layout_feature_get_lookup_indexes (hb_face_t    *face,
379                                          hb_tag_t      table_tag,
380                                          unsigned int  feature_index,
381                                          unsigned int  start_offset,
382                                          unsigned int *lookup_count /* IN/OUT */,
383                                          unsigned int *lookup_indexes /* OUT */)
384 {
385   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
386   const Feature &f = g.get_feature (feature_index);
387
388   return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
389 }
390
391
392 /*
393  * GSUB
394  */
395
396 hb_bool_t
397 hb_ot_layout_has_substitution (hb_face_t *face)
398 {
399   return &_get_gsub (face) != &Null(GSUB);
400 }
401
402 hb_bool_t
403 hb_ot_layout_would_substitute_lookup (hb_face_t            *face,
404                                       const hb_codepoint_t *glyphs,
405                                       unsigned int          glyphs_length,
406                                       unsigned int          lookup_index)
407 {
408   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return false;
409   return hb_ot_layout_would_substitute_lookup_fast (face, glyphs, glyphs_length, lookup_index);
410 }
411
412 hb_bool_t
413 hb_ot_layout_would_substitute_lookup_fast (hb_face_t            *face,
414                                            const hb_codepoint_t *glyphs,
415                                            unsigned int          glyphs_length,
416                                            unsigned int          lookup_index)
417 {
418   if (unlikely (lookup_index >= hb_ot_layout_from_face (face)->gsub_lookup_count)) return false;
419   hb_would_apply_context_t c (face, glyphs, glyphs_length, &hb_ot_layout_from_face (face)->gsub_digests[lookup_index]);
420   return hb_ot_layout_from_face (face)->gsub->would_substitute_lookup (&c, lookup_index);
421 }
422
423 void
424 hb_ot_layout_substitute_start (hb_font_t *font, hb_buffer_t *buffer)
425 {
426   GSUB::substitute_start (font, buffer);
427 }
428
429 hb_bool_t
430 hb_ot_layout_substitute_lookup (hb_font_t    *font,
431                                 hb_buffer_t  *buffer,
432                                 unsigned int  lookup_index,
433                                 hb_mask_t     mask)
434 {
435   if (unlikely (lookup_index >= hb_ot_layout_from_face (font->face)->gsub_lookup_count)) return false;
436   hb_apply_context_t c (font, buffer, mask, &hb_ot_layout_from_face (font->face)->gsub_digests[lookup_index]);
437   return hb_ot_layout_from_face (font->face)->gsub->substitute_lookup (&c, lookup_index);
438 }
439
440 void
441 hb_ot_layout_substitute_finish (hb_font_t *font, hb_buffer_t *buffer)
442 {
443   GSUB::substitute_finish (font, buffer);
444 }
445
446 void
447 hb_ot_layout_substitute_closure_lookup (hb_face_t    *face,
448                                         hb_set_t     *glyphs,
449                                         unsigned int  lookup_index)
450 {
451   hb_closure_context_t c (face, glyphs);
452   _get_gsub (face).closure_lookup (&c, lookup_index);
453 }
454
455 /*
456  * GPOS
457  */
458
459 hb_bool_t
460 hb_ot_layout_has_positioning (hb_face_t *face)
461 {
462   return &_get_gpos (face) != &Null(GPOS);
463 }
464
465 void
466 hb_ot_layout_position_start (hb_font_t *font, hb_buffer_t *buffer)
467 {
468   GPOS::position_start (font, buffer);
469 }
470
471 hb_bool_t
472 hb_ot_layout_position_lookup (hb_font_t    *font,
473                               hb_buffer_t  *buffer,
474                               unsigned int  lookup_index,
475                               hb_mask_t     mask)
476 {
477   if (unlikely (lookup_index >= hb_ot_layout_from_face (font->face)->gpos_lookup_count)) return false;
478   hb_apply_context_t c (font, buffer, mask, &hb_ot_layout_from_face (font->face)->gpos_digests[lookup_index]);
479   return hb_ot_layout_from_face (font->face)->gpos->position_lookup (&c, lookup_index);
480 }
481
482 void
483 hb_ot_layout_position_finish (hb_font_t *font, hb_buffer_t *buffer, hb_bool_t zero_width_attached_marks)
484 {
485   GPOS::position_finish (font, buffer, zero_width_attached_marks);
486 }