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