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