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