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