[GDEF] Support MarkFilteringSets
[framework/uifw/harfbuzz.git] / src / hb-ot-layout.cc
1 /*
2  * Copyright (C) 1998-2004  David Turner and Werner Lemberg
3  * Copyright (C) 2006  Behdad Esfahbod
4  * Copyright (C) 2007,2008,2009  Red Hat, Inc.
5  *
6  *  This is part of HarfBuzz, an OpenType Layout engine 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  * Red Hat Author(s): Behdad Esfahbod
27  */
28
29 #define HB_OT_LAYOUT_CC
30
31 /* XXX */
32 #include "hb-buffer-private.h"
33
34 #include "hb-ot-layout.h"
35 #include "hb-ot-layout-private.h"
36
37 #include "hb-ot-layout-open-private.h"
38 #include "hb-ot-layout-gdef-private.h"
39 #include "hb-ot-layout-gsub-private.h"
40 #include "hb-ot-layout-gpos-private.h"
41
42
43 #include <stdlib.h>
44 #include <string.h>
45
46
47 hb_ot_layout_t *
48 hb_ot_layout_create (void)
49 {
50   hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
51
52   layout->gdef = &Null(GDEF);
53   layout->gsub = &Null(GSUB);
54   layout->gpos = &Null(GPOS);
55
56   return layout;
57 }
58
59 hb_ot_layout_t *
60 hb_ot_layout_create_for_data (const char *font_data,
61                               int         face_index)
62 {
63   hb_ot_layout_t *layout;
64
65   if (HB_UNLIKELY (font_data == NULL))
66     return hb_ot_layout_create ();
67
68   layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
69
70   const OpenTypeFontFile &font = OpenTypeFontFile::get_for_data (font_data);
71   const OpenTypeFontFace &face = font.get_face (face_index);
72
73   layout->gdef = &GDEF::get_for_data (font.get_table_data (face.get_table_by_tag (GDEF::Tag)));
74   layout->gsub = &GSUB::get_for_data (font.get_table_data (face.get_table_by_tag (GSUB::Tag)));
75   layout->gpos = &GPOS::get_for_data (font.get_table_data (face.get_table_by_tag (GPOS::Tag)));
76
77   return layout;
78 }
79
80 void
81 hb_ot_layout_destroy (hb_ot_layout_t *layout)
82 {
83   free (layout);
84 }
85
86 void
87 hb_ot_layout_set_direction (hb_ot_layout_t *layout,
88                             hb_bool_t r2l)
89 {
90   layout->gpos_info.r2l = !!r2l;
91 }
92
93 void
94 hb_ot_layout_set_hinting (hb_ot_layout_t *layout,
95                           hb_bool_t hinted)
96 {
97   layout->gpos_info.dvi = !hinted;
98 }
99
100 void
101 hb_ot_layout_set_scale (hb_ot_layout_t *layout,
102                         hb_16dot16_t x_scale, hb_16dot16_t y_scale)
103 {
104   layout->gpos_info.x_scale = x_scale;
105   layout->gpos_info.y_scale = y_scale;
106 }
107
108 void
109 hb_ot_layout_set_ppem (hb_ot_layout_t *layout,
110                        unsigned int x_ppem, unsigned int y_ppem)
111 {
112   layout->gpos_info.x_ppem = x_ppem;
113   layout->gpos_info.y_ppem = y_ppem;
114 }
115
116
117 /*
118  * GDEF
119  */
120
121 /* TODO the public class_t is a mess */
122
123 hb_bool_t
124 hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout)
125 {
126   return layout->gdef->has_glyph_classes ();
127 }
128
129 HB_INTERNAL hb_bool_t
130 _hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout)
131 {
132   return layout->new_gdef.len > 0;
133 }
134
135 HB_INTERNAL unsigned int
136 _hb_ot_layout_get_glyph_property (hb_ot_layout_t *layout,
137                                   hb_codepoint_t  glyph)
138 {
139   hb_ot_layout_class_t klass;
140
141   klass = layout->gdef->get_glyph_class (glyph);
142
143   if (!klass && glyph < layout->new_gdef.len)
144     klass = layout->new_gdef.klasses[glyph];
145
146   switch (klass) {
147   default:
148   case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
149   case GDEF::BaseGlyph:         return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
150   case GDEF::LigatureGlyph:     return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
151   case GDEF::ComponentGlyph:    return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
152   case GDEF::MarkGlyph:
153         /* TODO old harfbuzz doesn't always parse mark attachments as it says it was
154          * introduced without a version bump, so it may not be safe */
155         klass = layout->gdef->get_mark_attachment_type (glyph);
156         return HB_OT_LAYOUT_GLYPH_CLASS_MARK + klass << 8;
157   }
158 }
159
160 HB_INTERNAL hb_bool_t
161 _hb_ot_layout_check_glyph_property (hb_ot_layout_t  *layout,
162                                     hb_glyph_info_t *ginfo,
163                                     unsigned int     lookup_flags,
164                                     unsigned int    *property)
165 {
166   hb_ot_layout_glyph_class_t glyph_class;
167   unsigned int desired_attachment_class;
168
169   if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
170   {
171     ginfo->gproperty = *property = _hb_ot_layout_get_glyph_property (layout, ginfo->gindex);
172     if (ginfo->gproperty == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED)
173       return false;
174   }
175
176   *property = ginfo->gproperty;
177
178   /* If the glyph was found in the MarkAttachmentClass table,
179    * then that class value is stored in the high byte of the result.
180    * The low byte contains the basic type of the glyph as defined by
181    * the GlyphClassDef table.
182    */
183   glyph_class = (hb_ot_layout_glyph_class_t) *property;
184
185   /* Not covered, if, for example, glyph_class is HB_GDEF_LIGATURE and
186    * lookup_flags includes LookupFlags::IgnoreLigatures
187    */
188   if (lookup_flags & glyph_class)
189     return false;
190
191   if (glyph_class & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
192   {
193     /* If using mark filtering sets, the high short of
194      * lookup_flags hsa the set index.
195      */
196     if (lookup_flags & LookupFlag::UseMarkFilteringSet)
197       return layout->gdef->mark_set_covers (lookup_flags >> 16, ginfo->gindex);
198
199     /* The second byte of lookup_flags has the meaning
200      * "ignore marks of attachment type different than
201      * the attachment type specified."
202      */
203     if (lookup_flags & LookupFlag::MarkAttachmentType && *property & LookupFlag::MarkAttachmentType)
204       return (lookup_flags & LookupFlag::MarkAttachmentType) == (*property & LookupFlag::MarkAttachmentType);
205   }
206
207
208   return true;
209 }
210
211 HB_INTERNAL void
212 _hb_ot_layout_set_glyph_property (hb_ot_layout_t *layout,
213                                   hb_codepoint_t  glyph,
214                                   unsigned int    property)
215 { hb_ot_layout_set_glyph_class (layout, glyph, (hb_ot_layout_glyph_class_t) (property & 0xff)); }
216
217
218 hb_ot_layout_glyph_class_t
219 hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout,
220                               hb_codepoint_t  glyph)
221 { return (hb_ot_layout_glyph_class_t) (_hb_ot_layout_get_glyph_property (layout, glyph) & 0xff); }
222
223 void
224 hb_ot_layout_set_glyph_class (hb_ot_layout_t             *layout,
225                               hb_codepoint_t              glyph,
226                               hb_ot_layout_glyph_class_t  klass)
227 {
228   /* TODO optimize this, similar to old harfbuzz code for example */
229
230   hb_ot_layout_class_t gdef_klass;
231   int len = layout->new_gdef.len;
232
233   if (HB_UNLIKELY (glyph > 65535))
234     return;
235
236   if (glyph >= len) {
237     int new_len;
238     unsigned char *new_klasses;
239
240     new_len = len == 0 ? 120 : 2 * len;
241     if (new_len > 65535)
242       new_len = 65535;
243     new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
244
245     if (HB_UNLIKELY (!new_klasses))
246       return;
247
248     memset (new_klasses + len, 0, new_len - len);
249
250     layout->new_gdef.klasses = new_klasses;
251     layout->new_gdef.len = new_len;
252   }
253
254   switch (klass) {
255   default:
256   case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED:   gdef_klass = GDEF::UnclassifiedGlyph;   break;
257   case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH:     gdef_klass = GDEF::BaseGlyph;           break;
258   case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE:       gdef_klass = GDEF::LigatureGlyph;       break;
259   case HB_OT_LAYOUT_GLYPH_CLASS_MARK:           gdef_klass = GDEF::MarkGlyph;           break;
260   case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT:      gdef_klass = GDEF::ComponentGlyph;      break;
261   }
262
263   layout->new_gdef.klasses[glyph] = gdef_klass;
264   return;
265 }
266
267 void
268 hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout,
269                                   uint16_t        num_total_glyphs,
270                                   hb_codepoint_t *glyphs,
271                                   unsigned char  *klasses,
272                                   uint16_t        count)
273 {
274   if (HB_UNLIKELY (!count || !glyphs || !klasses))
275     return;
276
277   if (layout->new_gdef.len == 0) {
278     layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
279     layout->new_gdef.len = count;
280   }
281
282   for (unsigned int i = 0; i < count; i++)
283     hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
284 }
285
286 hb_bool_t
287 hb_ot_layout_get_attach_points (hb_ot_layout_t *layout,
288                                 hb_codepoint_t  glyph,
289                                 unsigned int   *point_count /* IN/OUT */,
290                                 unsigned int   *point_array /* OUT */)
291 {
292   return layout->gdef->get_attach_points (glyph, point_count, point_array);
293 }
294
295 hb_bool_t
296 hb_ot_layout_get_lig_carets (hb_ot_layout_t *layout,
297                              hb_codepoint_t  glyph,
298                              unsigned int   *caret_count /* IN/OUT */,
299                              int            *caret_array /* OUT */)
300 {
301   return layout->gdef->get_lig_carets (layout, glyph, caret_count, caret_array);
302 }
303
304 /*
305  * GSUB/GPOS
306  */
307
308 static const GSUBGPOS&
309 get_gsubgpos_table (hb_ot_layout_t            *layout,
310                     hb_ot_layout_table_type_t  table_type)
311 {
312   switch (table_type) {
313     case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub);
314     case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos);
315     default:                           return Null(GSUBGPOS);
316   }
317 }
318
319
320 unsigned int
321 hb_ot_layout_table_get_script_count (hb_ot_layout_t            *layout,
322                                      hb_ot_layout_table_type_t  table_type)
323 {
324   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
325
326   return g.get_script_count ();
327 }
328
329 hb_tag_t
330 hb_ot_layout_table_get_script_tag (hb_ot_layout_t            *layout,
331                                    hb_ot_layout_table_type_t  table_type,
332                                    unsigned int               script_index)
333 {
334   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
335
336   return g.get_script_tag (script_index);
337 }
338
339 hb_bool_t
340 hb_ot_layout_table_find_script (hb_ot_layout_t            *layout,
341                                 hb_ot_layout_table_type_t  table_type,
342                                 hb_tag_t                   script_tag,
343                                 unsigned int              *script_index)
344 {
345   ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
346   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
347
348   if (g.find_script_index (script_tag, script_index))
349     return TRUE;
350
351   /* try finding 'DFLT' */
352   if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
353     return FALSE;
354
355   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
356   if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
357     return FALSE;
358
359   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
360   return FALSE;
361 }
362
363 unsigned int
364 hb_ot_layout_table_get_feature_count (hb_ot_layout_t            *layout,
365                                       hb_ot_layout_table_type_t  table_type)
366 {
367   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
368
369   return g.get_feature_count ();
370 }
371
372 hb_tag_t
373 hb_ot_layout_table_get_feature_tag (hb_ot_layout_t            *layout,
374                                     hb_ot_layout_table_type_t  table_type,
375                                     unsigned int               feature_index)
376 {
377   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
378
379   return g.get_feature_tag (feature_index);
380 }
381
382 hb_bool_t
383 hb_ot_layout_table_find_feature (hb_ot_layout_t            *layout,
384                                  hb_ot_layout_table_type_t  table_type,
385                                  hb_tag_t                   feature_tag,
386                                  unsigned int              *feature_index)
387 {
388   ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
389   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
390
391   if (g.find_feature_index (feature_tag, feature_index))
392     return TRUE;
393
394   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
395   return FALSE;
396 }
397
398 unsigned int
399 hb_ot_layout_table_get_lookup_count (hb_ot_layout_t            *layout,
400                                      hb_ot_layout_table_type_t  table_type)
401 {
402   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
403
404   return g.get_lookup_count ();
405 }
406
407
408 unsigned int
409 hb_ot_layout_script_get_language_count (hb_ot_layout_t            *layout,
410                                         hb_ot_layout_table_type_t  table_type,
411                                         unsigned int               script_index)
412 {
413   const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
414
415   return s.get_lang_sys_count ();
416 }
417
418 hb_tag_t
419 hb_ot_layout_script_get_language_tag (hb_ot_layout_t            *layout,
420                                       hb_ot_layout_table_type_t  table_type,
421                                       unsigned int               script_index,
422                                       unsigned int               language_index)
423 {
424   const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
425
426   return s.get_lang_sys_tag (language_index);
427 }
428
429 hb_bool_t
430 hb_ot_layout_script_find_language (hb_ot_layout_t            *layout,
431                                    hb_ot_layout_table_type_t  table_type,
432                                    unsigned int               script_index,
433                                    hb_tag_t                   language_tag,
434                                    unsigned int              *language_index)
435 {
436   ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
437   const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
438
439   if (s.find_lang_sys_index (language_tag, language_index))
440     return TRUE;
441
442   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
443   if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
444     return FALSE;
445
446   if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
447   return FALSE;
448 }
449
450 hb_bool_t
451 hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t            *layout,
452                                                   hb_ot_layout_table_type_t  table_type,
453                                                   unsigned int               script_index,
454                                                   unsigned int               language_index,
455                                                   unsigned int              *feature_index)
456 {
457   const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
458
459   if (feature_index) *feature_index = l.get_required_feature_index ();
460
461   return l.has_required_feature ();
462 }
463
464 unsigned int
465 hb_ot_layout_language_get_feature_count (hb_ot_layout_t            *layout,
466                                          hb_ot_layout_table_type_t  table_type,
467                                          unsigned int               script_index,
468                                          unsigned int               language_index)
469 {
470   const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
471
472   return l.get_feature_count ();
473 }
474
475 unsigned int
476 hb_ot_layout_language_get_feature_index (hb_ot_layout_t            *layout,
477                                          hb_ot_layout_table_type_t  table_type,
478                                          unsigned int               script_index,
479                                          unsigned int               language_index,
480                                          unsigned int               num_feature)
481 {
482   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
483   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
484
485   return l.get_feature_index (num_feature);
486 }
487
488 hb_tag_t
489 hb_ot_layout_language_get_feature_tag (hb_ot_layout_t            *layout,
490                                        hb_ot_layout_table_type_t  table_type,
491                                        unsigned int               script_index,
492                                        unsigned int               language_index,
493                                        unsigned int               num_feature)
494 {
495   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
496   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
497   unsigned int feature_index = l.get_feature_index (num_feature);
498
499   return g.get_feature_tag (feature_index);
500 }
501
502
503 hb_bool_t
504 hb_ot_layout_language_find_feature (hb_ot_layout_t            *layout,
505                                     hb_ot_layout_table_type_t  table_type,
506                                     unsigned int               script_index,
507                                     unsigned int               language_index,
508                                     hb_tag_t                   feature_tag,
509                                     unsigned int              *feature_index)
510 {
511   ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
512   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
513   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
514
515   unsigned int num_features = l.get_feature_count ();
516   for (unsigned int i = 0; i < num_features; i++) {
517     unsigned int f_index = l.get_feature_index (i);
518
519     if (feature_tag == g.get_feature_tag (f_index)) {
520       if (feature_index) *feature_index = f_index;
521       return TRUE;
522     }
523   }
524
525   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
526   return FALSE;
527 }
528
529 unsigned int
530 hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t            *layout,
531                                        hb_ot_layout_table_type_t  table_type,
532                                        unsigned int               feature_index)
533 {
534   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
535   const Feature &f = g.get_feature (feature_index);
536
537   return f.get_lookup_count ();
538 }
539
540 unsigned int
541 hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t            *layout,
542                                        hb_ot_layout_table_type_t  table_type,
543                                        unsigned int               feature_index,
544                                        unsigned int               num_lookup)
545 {
546   const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
547   const Feature &f = g.get_feature (feature_index);
548
549   return f.get_lookup_index (num_lookup);
550 }
551
552 /*
553  * GSUB
554  */
555
556 hb_bool_t
557 hb_ot_layout_substitute_lookup (hb_ot_layout_t              *layout,
558                                 hb_buffer_t                 *buffer,
559                                 unsigned int                 lookup_index,
560                                 hb_ot_layout_feature_mask_t  mask)
561 {
562   return layout->gsub->substitute_lookup (layout, buffer, lookup_index, mask);
563 }
564
565 /*
566  * GPOS
567  */
568
569 hb_bool_t
570 hb_ot_layout_position_lookup   (hb_ot_layout_t              *layout,
571                                 hb_buffer_t                 *buffer,
572                                 unsigned int                 lookup_index,
573                                 hb_ot_layout_feature_mask_t  mask)
574 {
575   return layout->gpos->position_lookup (layout, buffer, lookup_index, mask);
576 }