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