Simplify mark skipping logic
[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, 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_props,
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_props includes LookupFlags::IgnoreLigatures
152    */
153   if (property & lookup_props & 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_props has the set index.
160      */
161     if (lookup_props & LookupFlag::UseMarkFilteringSet)
162       return _get_gdef (face).mark_set_covers (lookup_props >> 16, ginfo->codepoint);
163
164     /* The second byte of lookup_props has the meaning
165      * "ignore marks of attachment type different than
166      * the attachment type specified."
167      */
168     if (lookup_props & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
169       return (lookup_props & 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_props,
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 it's a mark, skip it we don't accept it. */
188   if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
189     return !_hb_ot_layout_check_glyph_property (face, ginfo, lookup_props, NULL);
190
191   /* If not a mark, don't skip. */
192   return false;
193 }
194
195 unsigned int
196 hb_ot_layout_get_attach_points (hb_face_t      *face,
197                                 hb_codepoint_t  glyph,
198                                 unsigned int    start_offset,
199                                 unsigned int   *point_count /* IN/OUT */,
200                                 unsigned int   *point_array /* OUT */)
201 {
202   return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
203 }
204
205 unsigned int
206 hb_ot_layout_get_ligature_carets (hb_font_t      *font,
207                                   hb_face_t      *face,
208                                   hb_direction_t  direction,
209                                   hb_codepoint_t  glyph,
210                                   unsigned int    start_offset,
211                                   unsigned int   *caret_count /* IN/OUT */,
212                                   int            *caret_array /* OUT */)
213 {
214   hb_ot_layout_context_t c;
215   c.font = font;
216   c.face = face;
217   return _get_gdef (face).get_lig_carets (&c, direction, glyph, start_offset, caret_count, caret_array);
218 }
219
220 /*
221  * GSUB/GPOS
222  */
223
224 static const GSUBGPOS&
225 get_gsubgpos_table (hb_face_t *face,
226                     hb_tag_t   table_tag)
227 {
228   switch (table_tag) {
229     case HB_OT_TAG_GSUB: return _get_gsub (face);
230     case HB_OT_TAG_GPOS: return _get_gpos (face);
231     default:             return Null(GSUBGPOS);
232   }
233 }
234
235
236 unsigned int
237 hb_ot_layout_table_get_script_tags (hb_face_t    *face,
238                                     hb_tag_t      table_tag,
239                                     unsigned int  start_offset,
240                                     unsigned int *script_count /* IN/OUT */,
241                                     hb_tag_t     *script_tags /* OUT */)
242 {
243   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
244
245   return g.get_script_tags (start_offset, script_count, script_tags);
246 }
247
248 hb_bool_t
249 hb_ot_layout_table_find_script (hb_face_t    *face,
250                                 hb_tag_t      table_tag,
251                                 hb_tag_t      script_tag,
252                                 unsigned int *script_index)
253 {
254   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
255   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
256
257   if (g.find_script_index (script_tag, script_index))
258     return TRUE;
259
260   /* try finding 'DFLT' */
261   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
262     return FALSE;
263
264   /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
265    * including many versions of DejaVu Sans Mono! */
266   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
267     return FALSE;
268
269   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
270   return FALSE;
271 }
272
273 hb_bool_t
274 hb_ot_layout_table_choose_script (hb_face_t      *face,
275                                   hb_tag_t        table_tag,
276                                   const hb_tag_t *script_tags,
277                                   unsigned int   *script_index)
278 {
279   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
280   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
281
282   while (*script_tags)
283   {
284     if (g.find_script_index (*script_tags, script_index))
285       return TRUE;
286     script_tags++;
287   }
288
289   /* try finding 'DFLT' */
290   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
291     return FALSE;
292
293   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
294   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
295     return FALSE;
296
297   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
298   return FALSE;
299 }
300
301 unsigned int
302 hb_ot_layout_table_get_feature_tags (hb_face_t    *face,
303                                      hb_tag_t      table_tag,
304                                      unsigned int  start_offset,
305                                      unsigned int *feature_count /* IN/OUT */,
306                                      hb_tag_t     *feature_tags /* OUT */)
307 {
308   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
309
310   return g.get_feature_tags (start_offset, feature_count, feature_tags);
311 }
312
313
314 unsigned int
315 hb_ot_layout_script_get_language_tags (hb_face_t    *face,
316                                        hb_tag_t      table_tag,
317                                        unsigned int  script_index,
318                                        unsigned int  start_offset,
319                                        unsigned int *language_count /* IN/OUT */,
320                                        hb_tag_t     *language_tags /* OUT */)
321 {
322   const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
323
324   return s.get_lang_sys_tags (start_offset, language_count, language_tags);
325 }
326
327 hb_bool_t
328 hb_ot_layout_script_find_language (hb_face_t    *face,
329                                    hb_tag_t      table_tag,
330                                    unsigned int  script_index,
331                                    hb_tag_t      language_tag,
332                                    unsigned int *language_index)
333 {
334   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
335   const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
336
337   if (s.find_lang_sys_index (language_tag, language_index))
338     return TRUE;
339
340   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
341   if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
342     return FALSE;
343
344   if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
345   return FALSE;
346 }
347
348 hb_bool_t
349 hb_ot_layout_language_get_required_feature_index (hb_face_t    *face,
350                                                   hb_tag_t      table_tag,
351                                                   unsigned int  script_index,
352                                                   unsigned int  language_index,
353                                                   unsigned int *feature_index)
354 {
355   const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
356
357   if (feature_index) *feature_index = l.get_required_feature_index ();
358
359   return l.has_required_feature ();
360 }
361
362 unsigned int
363 hb_ot_layout_language_get_feature_indexes (hb_face_t    *face,
364                                            hb_tag_t      table_tag,
365                                            unsigned int  script_index,
366                                            unsigned int  language_index,
367                                            unsigned int  start_offset,
368                                            unsigned int *feature_count /* IN/OUT */,
369                                            unsigned int *feature_indexes /* OUT */)
370 {
371   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
372   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
373
374   return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
375 }
376
377 unsigned int
378 hb_ot_layout_language_get_feature_tags (hb_face_t    *face,
379                                         hb_tag_t      table_tag,
380                                         unsigned int  script_index,
381                                         unsigned int  language_index,
382                                         unsigned int  start_offset,
383                                         unsigned int *feature_count /* IN/OUT */,
384                                         hb_tag_t     *feature_tags /* OUT */)
385 {
386   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
387   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
388
389   ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
390   unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
391
392   if (feature_tags) {
393     unsigned int count = *feature_count;
394     for (unsigned int i = 0; i < count; i++)
395       feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
396   }
397
398   return ret;
399 }
400
401
402 hb_bool_t
403 hb_ot_layout_language_find_feature (hb_face_t    *face,
404                                     hb_tag_t      table_tag,
405                                     unsigned int  script_index,
406                                     unsigned int  language_index,
407                                     hb_tag_t      feature_tag,
408                                     unsigned int *feature_index)
409 {
410   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
411   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
412   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
413
414   unsigned int num_features = l.get_feature_count ();
415   for (unsigned int i = 0; i < num_features; i++) {
416     unsigned int f_index = l.get_feature_index (i);
417
418     if (feature_tag == g.get_feature_tag (f_index)) {
419       if (feature_index) *feature_index = f_index;
420       return TRUE;
421     }
422   }
423
424   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
425   return FALSE;
426 }
427
428 unsigned int
429 hb_ot_layout_feature_get_lookup_indexes (hb_face_t    *face,
430                                          hb_tag_t      table_tag,
431                                          unsigned int  feature_index,
432                                          unsigned int  start_offset,
433                                          unsigned int *lookup_count /* IN/OUT */,
434                                          unsigned int *lookup_indexes /* OUT */)
435 {
436   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
437   const Feature &f = g.get_feature (feature_index);
438
439   return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
440 }
441
442
443 /*
444  * GSUB
445  */
446
447 hb_bool_t
448 hb_ot_layout_has_substitution (hb_face_t *face)
449 {
450   return &_get_gsub (face) != &Null(GSUB);
451 }
452
453 hb_bool_t
454 hb_ot_layout_substitute_lookup (hb_face_t    *face,
455                                 hb_buffer_t  *buffer,
456                                 unsigned int  lookup_index,
457                                 hb_mask_t     mask)
458 {
459   hb_ot_layout_context_t c;
460   c.font = NULL;
461   c.face = face;
462   return _get_gsub (face).substitute_lookup (&c, buffer, lookup_index, mask);
463 }
464
465
466 /*
467  * GPOS
468  */
469
470 hb_bool_t
471 hb_ot_layout_has_positioning (hb_face_t *face)
472 {
473   return &_get_gpos (face) != &Null(GPOS);
474 }
475
476 hb_bool_t
477 hb_ot_layout_position_lookup   (hb_font_t    *font,
478                                 hb_face_t    *face,
479                                 hb_buffer_t  *buffer,
480                                 unsigned int  lookup_index,
481                                 hb_mask_t     mask)
482 {
483   hb_ot_layout_context_t c;
484   c.font = font;
485   c.face = face;
486   return _get_gpos (face).position_lookup (&c, buffer, lookup_index, mask);
487 }
488
489 void
490 hb_ot_layout_position_finish (hb_font_t    *font HB_UNUSED,
491                               hb_face_t    *face HB_UNUSED,
492                               hb_buffer_t  *buffer)
493 {
494   unsigned int i, j;
495   unsigned int len = hb_buffer_get_length (buffer);
496   hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer);
497   hb_direction_t direction = buffer->props.direction;
498
499   /* TODO: Vertical */
500
501   /* Handle cursive connections:
502    * First handle all chain-back connections, then handle all chain-forward connections. */
503   if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
504   {
505     for (j = 0; j < len; j++) {
506       if (pos[j].cursive_chain() < 0)
507         pos[j].y_offset += pos[j + pos[j].cursive_chain()].y_offset;
508     }
509     for (i = len; i > 0; i--) {
510       j = i - 1;
511       if (pos[j].cursive_chain() > 0)
512         pos[j].y_offset += pos[j + pos[j].cursive_chain()].y_offset;
513     }
514   }
515   else
516   {
517     for (j = 0; j < len; j++) {
518       if (pos[j].cursive_chain() < 0)
519         pos[j].x_offset += pos[j + pos[j].cursive_chain()].x_offset;
520     }
521     for (i = len; i > 0; i--) {
522       j = i - 1;
523       if (pos[j].cursive_chain() > 0)
524         pos[j].x_offset += pos[j + pos[j].cursive_chain()].x_offset;
525     }
526   }
527
528
529   /* Handle attachments */
530   for (i = 0; i < len; i++)
531     if (pos[i].back())
532     {
533       unsigned int back = i - pos[i].back();
534       pos[i].x_offset += pos[back].x_offset;
535       pos[i].y_offset += pos[back].y_offset;
536
537       if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
538         for (j = back + 1; j < i + 1; j++) {
539           pos[i].x_offset += pos[j].x_advance;
540           pos[i].y_offset += pos[j].y_advance;
541         }
542       else
543         for (j = back; j < i; j++) {
544           pos[i].x_offset -= pos[j].x_advance;
545           pos[i].y_offset -= pos[j].y_advance;
546         }
547     }
548 }
549
550
551 HB_END_DECLS