WIP removing external synthesized GDEF support and implementing it internally
[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, a text shaping 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-private.hh"
32
33 #include "hb-ot-layout-gdef-private.hh"
34 #include "hb-ot-layout-gsub-private.hh"
35 #include "hb-ot-layout-gpos-private.hh"
36
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 HB_BEGIN_DECLS
42
43
44 hb_ot_layout_t *
45 _hb_ot_layout_new (hb_face_t *face)
46 {
47   /* Remove this object altogether */
48   hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
49
50   layout->gdef_blob = Sanitizer<GDEF>::sanitize (hb_face_get_table (face, HB_OT_TAG_GDEF));
51   layout->gdef = Sanitizer<GDEF>::lock_instance (layout->gdef_blob);
52
53   layout->gsub_blob = Sanitizer<GSUB>::sanitize (hb_face_get_table (face, HB_OT_TAG_GSUB));
54   layout->gsub = Sanitizer<GSUB>::lock_instance (layout->gsub_blob);
55
56   layout->gpos_blob = Sanitizer<GPOS>::sanitize (hb_face_get_table (face, HB_OT_TAG_GPOS));
57   layout->gpos = Sanitizer<GPOS>::lock_instance (layout->gpos_blob);
58
59   return layout;
60 }
61
62 void
63 _hb_ot_layout_free (hb_ot_layout_t *layout)
64 {
65   hb_blob_unlock (layout->gdef_blob);
66   hb_blob_unlock (layout->gsub_blob);
67   hb_blob_unlock (layout->gpos_blob);
68
69   hb_blob_destroy (layout->gdef_blob);
70   hb_blob_destroy (layout->gsub_blob);
71   hb_blob_destroy (layout->gpos_blob);
72
73   free (layout);
74 }
75
76 static const GDEF&
77 _get_gdef (hb_face_t *face)
78 {
79   return likely (face->ot_layout && face->ot_layout->gdef) ? *face->ot_layout->gdef : Null(GDEF);
80 }
81
82 static const GSUB&
83 _get_gsub (hb_face_t *face)
84 {
85   return likely (face->ot_layout && face->ot_layout->gsub) ? *face->ot_layout->gsub : Null(GSUB);
86 }
87
88 static const GPOS&
89 _get_gpos (hb_face_t *face)
90 {
91   return likely (face->ot_layout && face->ot_layout->gpos) ? *face->ot_layout->gpos : Null(GPOS);
92 }
93
94
95 /*
96  * GDEF
97  */
98
99 hb_bool_t
100 hb_ot_layout_has_glyph_classes (hb_face_t *face)
101 {
102   return _get_gdef (face).has_glyph_classes ();
103 }
104
105 static unsigned int
106 _hb_ot_layout_get_glyph_property_from_gdef (hb_face_t       *face,
107                                             hb_glyph_info_t *info)
108 {
109   hb_codepoint_t glyph = info->codepoint;
110
111   unsigned int klass;
112   const GDEF &gdef = _get_gdef (face);
113
114   klass = gdef.get_glyph_class (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         klass = gdef.get_mark_attachment_type (glyph);
124         return HB_OT_LAYOUT_GLYPH_CLASS_MARK | (klass << 8);
125   }
126 }
127
128 static inline unsigned int
129 _hb_ot_layout_get_glyph_property (hb_face_t       *face,
130                                   hb_glyph_info_t *info)
131 {
132   if (!info->gproperty())
133     info->gproperty() = _hb_ot_layout_get_glyph_property_from_gdef (face, info);
134
135   return info->gproperty();
136 }
137
138 hb_bool_t
139 _hb_ot_layout_check_glyph_property (hb_face_t    *face,
140                                     hb_glyph_info_t *ginfo,
141                                     unsigned int  lookup_flags,
142                                     unsigned int *property_out)
143 {
144   unsigned int property;
145
146   property = _hb_ot_layout_get_glyph_property (face, ginfo);
147   if (property_out)
148     *property_out = property;
149
150   /* Not covered, if, for example, glyph class is ligature and
151    * lookup_flags includes LookupFlags::IgnoreLigatures
152    */
153   if (property & lookup_flags & LookupFlag::IgnoreFlags)
154     return false;
155
156   if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
157   {
158     /* If using mark filtering sets, the high short of
159      * lookup_flags has the set index.
160      */
161     if (lookup_flags & LookupFlag::UseMarkFilteringSet)
162       return _get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
163
164     /* The second byte of lookup_flags has the meaning
165      * "ignore marks of attachment type different than
166      * the attachment type specified."
167      */
168     if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
169       return (lookup_flags & LookupFlag::MarkAttachmentType) == (property & LookupFlag::MarkAttachmentType);
170   }
171
172   return true;
173 }
174
175 hb_bool_t
176 _hb_ot_layout_skip_mark (hb_face_t    *face,
177                          hb_glyph_info_t *ginfo,
178                          unsigned int  lookup_flags,
179                          unsigned int *property_out)
180 {
181   unsigned int property;
182
183   property = _hb_ot_layout_get_glyph_property (face, ginfo);
184   if (property_out)
185     *property_out = property;
186
187   if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
188   {
189     /* Skip mark if lookup_flags includes LookupFlags::IgnoreMarks */
190     if (lookup_flags & LookupFlag::IgnoreMarks)
191       return true;
192
193     /* If using mark filtering sets, the high short of lookup_flags has the set index. */
194     if (lookup_flags & LookupFlag::UseMarkFilteringSet)
195       return !_get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
196
197     /* The second byte of lookup_flags has the meaning "ignore marks of attachment type
198      * different than the attachment type specified." */
199     if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
200       return (lookup_flags & LookupFlag::MarkAttachmentType) != (property & LookupFlag::MarkAttachmentType);
201   }
202
203   return false;
204 }
205
206 unsigned int
207 hb_ot_layout_get_attach_points (hb_face_t      *face,
208                                 hb_codepoint_t  glyph,
209                                 unsigned int    start_offset,
210                                 unsigned int   *point_count /* IN/OUT */,
211                                 unsigned int   *point_array /* OUT */)
212 {
213   return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
214 }
215
216 unsigned int
217 hb_ot_layout_get_ligature_carets (hb_font_t      *font,
218                                   hb_face_t      *face,
219                                   hb_direction_t  direction,
220                                   hb_codepoint_t  glyph,
221                                   unsigned int    start_offset,
222                                   unsigned int   *caret_count /* IN/OUT */,
223                                   int            *caret_array /* OUT */)
224 {
225   hb_ot_layout_context_t c;
226   c.font = font;
227   c.face = face;
228   return _get_gdef (face).get_lig_carets (&c, direction, glyph, start_offset, caret_count, caret_array);
229 }
230
231 /*
232  * GSUB/GPOS
233  */
234
235 static const GSUBGPOS&
236 get_gsubgpos_table (hb_face_t *face,
237                     hb_tag_t   table_tag)
238 {
239   switch (table_tag) {
240     case HB_OT_TAG_GSUB: return _get_gsub (face);
241     case HB_OT_TAG_GPOS: return _get_gpos (face);
242     default:             return Null(GSUBGPOS);
243   }
244 }
245
246
247 unsigned int
248 hb_ot_layout_table_get_script_tags (hb_face_t    *face,
249                                     hb_tag_t      table_tag,
250                                     unsigned int  start_offset,
251                                     unsigned int *script_count /* IN/OUT */,
252                                     hb_tag_t     *script_tags /* OUT */)
253 {
254   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
255
256   return g.get_script_tags (start_offset, script_count, script_tags);
257 }
258
259 hb_bool_t
260 hb_ot_layout_table_find_script (hb_face_t    *face,
261                                 hb_tag_t      table_tag,
262                                 hb_tag_t      script_tag,
263                                 unsigned int *script_index)
264 {
265   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
266   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
267
268   if (g.find_script_index (script_tag, script_index))
269     return TRUE;
270
271   /* try finding 'DFLT' */
272   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
273     return FALSE;
274
275   /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
276    * including many versions of DejaVu Sans Mono! */
277   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
278     return FALSE;
279
280   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
281   return FALSE;
282 }
283
284 hb_bool_t
285 hb_ot_layout_table_choose_script (hb_face_t      *face,
286                                   hb_tag_t        table_tag,
287                                   const hb_tag_t *script_tags,
288                                   unsigned int   *script_index)
289 {
290   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
291   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
292
293   while (*script_tags)
294   {
295     if (g.find_script_index (*script_tags, script_index))
296       return TRUE;
297     script_tags++;
298   }
299
300   /* try finding 'DFLT' */
301   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
302     return FALSE;
303
304   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
305   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
306     return FALSE;
307
308   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
309   return FALSE;
310 }
311
312 unsigned int
313 hb_ot_layout_table_get_feature_tags (hb_face_t    *face,
314                                      hb_tag_t      table_tag,
315                                      unsigned int  start_offset,
316                                      unsigned int *feature_count /* IN/OUT */,
317                                      hb_tag_t     *feature_tags /* OUT */)
318 {
319   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
320
321   return g.get_feature_tags (start_offset, feature_count, feature_tags);
322 }
323
324
325 unsigned int
326 hb_ot_layout_script_get_language_tags (hb_face_t    *face,
327                                        hb_tag_t      table_tag,
328                                        unsigned int  script_index,
329                                        unsigned int  start_offset,
330                                        unsigned int *language_count /* IN/OUT */,
331                                        hb_tag_t     *language_tags /* OUT */)
332 {
333   const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
334
335   return s.get_lang_sys_tags (start_offset, language_count, language_tags);
336 }
337
338 hb_bool_t
339 hb_ot_layout_script_find_language (hb_face_t    *face,
340                                    hb_tag_t      table_tag,
341                                    unsigned int  script_index,
342                                    hb_tag_t      language_tag,
343                                    unsigned int *language_index)
344 {
345   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
346   const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
347
348   if (s.find_lang_sys_index (language_tag, language_index))
349     return TRUE;
350
351   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
352   if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
353     return FALSE;
354
355   if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
356   return FALSE;
357 }
358
359 hb_bool_t
360 hb_ot_layout_language_get_required_feature_index (hb_face_t    *face,
361                                                   hb_tag_t      table_tag,
362                                                   unsigned int  script_index,
363                                                   unsigned int  language_index,
364                                                   unsigned int *feature_index)
365 {
366   const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
367
368   if (feature_index) *feature_index = l.get_required_feature_index ();
369
370   return l.has_required_feature ();
371 }
372
373 unsigned int
374 hb_ot_layout_language_get_feature_indexes (hb_face_t    *face,
375                                            hb_tag_t      table_tag,
376                                            unsigned int  script_index,
377                                            unsigned int  language_index,
378                                            unsigned int  start_offset,
379                                            unsigned int *feature_count /* IN/OUT */,
380                                            unsigned int *feature_indexes /* OUT */)
381 {
382   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
383   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
384
385   return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
386 }
387
388 unsigned int
389 hb_ot_layout_language_get_feature_tags (hb_face_t    *face,
390                                         hb_tag_t      table_tag,
391                                         unsigned int  script_index,
392                                         unsigned int  language_index,
393                                         unsigned int  start_offset,
394                                         unsigned int *feature_count /* IN/OUT */,
395                                         hb_tag_t     *feature_tags /* OUT */)
396 {
397   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
398   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
399
400   ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
401   unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
402
403   if (feature_tags) {
404     unsigned int count = *feature_count;
405     for (unsigned int i = 0; i < count; i++)
406       feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
407   }
408
409   return ret;
410 }
411
412
413 hb_bool_t
414 hb_ot_layout_language_find_feature (hb_face_t    *face,
415                                     hb_tag_t      table_tag,
416                                     unsigned int  script_index,
417                                     unsigned int  language_index,
418                                     hb_tag_t      feature_tag,
419                                     unsigned int *feature_index)
420 {
421   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
422   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
423   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
424
425   unsigned int num_features = l.get_feature_count ();
426   for (unsigned int i = 0; i < num_features; i++) {
427     unsigned int f_index = l.get_feature_index (i);
428
429     if (feature_tag == g.get_feature_tag (f_index)) {
430       if (feature_index) *feature_index = f_index;
431       return TRUE;
432     }
433   }
434
435   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
436   return FALSE;
437 }
438
439 unsigned int
440 hb_ot_layout_feature_get_lookup_indexes (hb_face_t    *face,
441                                          hb_tag_t      table_tag,
442                                          unsigned int  feature_index,
443                                          unsigned int  start_offset,
444                                          unsigned int *lookup_count /* IN/OUT */,
445                                          unsigned int *lookup_indexes /* OUT */)
446 {
447   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
448   const Feature &f = g.get_feature (feature_index);
449
450   return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
451 }
452
453
454 /*
455  * GSUB
456  */
457
458 hb_bool_t
459 hb_ot_layout_has_substitution (hb_face_t *face)
460 {
461   return &_get_gsub (face) != &Null(GSUB);
462 }
463
464 hb_bool_t
465 hb_ot_layout_substitute_lookup (hb_face_t    *face,
466                                 hb_buffer_t  *buffer,
467                                 unsigned int  lookup_index,
468                                 hb_mask_t     mask)
469 {
470   hb_ot_layout_context_t c;
471   c.font = NULL;
472   c.face = face;
473   return _get_gsub (face).substitute_lookup (&c, buffer, lookup_index, mask);
474 }
475
476
477 /*
478  * GPOS
479  */
480
481 hb_bool_t
482 hb_ot_layout_has_positioning (hb_face_t *face)
483 {
484   return &_get_gpos (face) != &Null(GPOS);
485 }
486
487 hb_bool_t
488 hb_ot_layout_position_lookup   (hb_font_t    *font,
489                                 hb_face_t    *face,
490                                 hb_buffer_t  *buffer,
491                                 unsigned int  lookup_index,
492                                 hb_mask_t     mask)
493 {
494   hb_ot_layout_context_t c;
495   c.font = font;
496   c.face = face;
497   return _get_gpos (face).position_lookup (&c, buffer, lookup_index, mask);
498 }
499
500 void
501 hb_ot_layout_position_finish (hb_font_t    *font HB_UNUSED,
502                               hb_face_t    *face HB_UNUSED,
503                               hb_buffer_t  *buffer)
504 {
505   unsigned int i, j;
506   unsigned int len = hb_buffer_get_length (buffer);
507   hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer);
508   hb_direction_t direction = buffer->props.direction;
509
510   /* TODO: Vertical */
511
512   /* Handle cursive connections:
513    * First handle all chain-back connections, then handle all chain-forward connections. */
514   if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
515   {
516     for (j = 0; j < len; j++) {
517       if (pos[j].cursive_chain() < 0)
518         pos[j].y_offset += pos[j + pos[j].cursive_chain()].y_offset;
519     }
520     for (i = len; i > 0; i--) {
521       j = i - 1;
522       if (pos[j].cursive_chain() > 0)
523         pos[j].y_offset += pos[j + pos[j].cursive_chain()].y_offset;
524     }
525   }
526   else
527   {
528     for (j = 0; j < len; j++) {
529       if (pos[j].cursive_chain() < 0)
530         pos[j].x_offset += pos[j + pos[j].cursive_chain()].x_offset;
531     }
532     for (i = len; i > 0; i--) {
533       j = i - 1;
534       if (pos[j].cursive_chain() > 0)
535         pos[j].x_offset += pos[j + pos[j].cursive_chain()].x_offset;
536     }
537   }
538
539
540   /* Handle attachments */
541   for (i = 0; i < len; i++)
542     if (pos[i].back())
543     {
544       unsigned int back = i - pos[i].back();
545       pos[i].x_offset += pos[back].x_offset;
546       pos[i].y_offset += pos[back].y_offset;
547
548       if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
549         for (j = back + 1; j < i + 1; j++) {
550           pos[i].x_offset += pos[j].x_advance;
551           pos[i].y_offset += pos[j].y_advance;
552         }
553       else
554         for (j = back; j < i; j++) {
555           pos[i].x_offset -= pos[j].x_advance;
556           pos[i].y_offset -= pos[j].y_advance;
557         }
558     }
559 }
560
561
562 HB_END_DECLS