[API] Make hb_font_create() take a face and reference it
[framework/uifw/harfbuzz.git] / src / hb-ot-layout.cc
1 /*
2  * Copyright © 1998-2004  David Turner and Werner Lemberg
3  * Copyright © 2006  Behdad Esfahbod
4  * Copyright © 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_reference_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_reference_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_reference_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 inline 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 inline 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 inline 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 unsigned int
106 _hb_ot_layout_get_glyph_property (hb_face_t       *face,
107                                   hb_glyph_info_t *info)
108 {
109   if (!info->props_cache())
110   {
111     const GDEF &gdef = _get_gdef (face);
112     info->props_cache() = gdef.get_glyph_props (info->codepoint);
113   }
114
115   return info->props_cache();
116 }
117
118 static hb_bool_t
119 _hb_ot_layout_match_properties (hb_face_t      *face,
120                                 hb_codepoint_t  codepoint,
121                                 unsigned int    glyph_props,
122                                 unsigned int    lookup_props)
123 {
124   /* Not covered, if, for example, glyph class is ligature and
125    * lookup_props includes LookupFlags::IgnoreLigatures
126    */
127   if (glyph_props & lookup_props & LookupFlag::IgnoreFlags)
128     return false;
129
130   if (glyph_props & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
131   {
132     /* If using mark filtering sets, the high short of
133      * lookup_props has the set index.
134      */
135     if (lookup_props & LookupFlag::UseMarkFilteringSet)
136       return _get_gdef (face).mark_set_covers (lookup_props >> 16, codepoint);
137
138     /* The second byte of lookup_props has the meaning
139      * "ignore marks of attachment type different than
140      * the attachment type specified."
141      */
142     if (lookup_props & LookupFlag::MarkAttachmentType && glyph_props & LookupFlag::MarkAttachmentType)
143       return (lookup_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
144   }
145
146   return true;
147 }
148
149 hb_bool_t
150 _hb_ot_layout_check_glyph_property (hb_face_t    *face,
151                                     hb_glyph_info_t *ginfo,
152                                     unsigned int  lookup_props,
153                                     unsigned int *property_out)
154 {
155   unsigned int property;
156
157   property = _hb_ot_layout_get_glyph_property (face, ginfo);
158   (void) (property_out && (*property_out = property));
159
160   return _hb_ot_layout_match_properties (face, ginfo->codepoint, property, lookup_props);
161 }
162
163 hb_bool_t
164 _hb_ot_layout_skip_mark (hb_face_t    *face,
165                          hb_glyph_info_t *ginfo,
166                          unsigned int  lookup_props,
167                          unsigned int *property_out)
168 {
169   unsigned int property;
170
171   property = _hb_ot_layout_get_glyph_property (face, ginfo);
172   (void) (property_out && (*property_out = property));
173
174   /* If it's a mark, skip it we don't accept it. */
175   if (unlikely (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
176     return !_hb_ot_layout_match_properties (face, ginfo->codepoint, property, lookup_props);
177
178   /* If not a mark, don't skip. */
179   return false;
180 }
181
182
183
184 unsigned int
185 hb_ot_layout_get_attach_points (hb_face_t      *face,
186                                 hb_codepoint_t  glyph,
187                                 unsigned int    start_offset,
188                                 unsigned int   *point_count /* IN/OUT */,
189                                 unsigned int   *point_array /* OUT */)
190 {
191   return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
192 }
193
194 unsigned int
195 hb_ot_layout_get_ligature_carets (hb_font_t      *font,
196                                   hb_direction_t  direction,
197                                   hb_codepoint_t  glyph,
198                                   unsigned int    start_offset,
199                                   unsigned int   *caret_count /* IN/OUT */,
200                                   int            *caret_array /* OUT */)
201 {
202   hb_ot_layout_context_t c;
203   c.font = font;
204   c.face = font->face;
205   return _get_gdef (c.face).get_lig_carets (&c, direction, glyph, start_offset, caret_count, caret_array);
206 }
207
208 /*
209  * GSUB/GPOS
210  */
211
212 static const GSUBGPOS&
213 get_gsubgpos_table (hb_face_t *face,
214                     hb_tag_t   table_tag)
215 {
216   switch (table_tag) {
217     case HB_OT_TAG_GSUB: return _get_gsub (face);
218     case HB_OT_TAG_GPOS: return _get_gpos (face);
219     default:             return Null(GSUBGPOS);
220   }
221 }
222
223
224 unsigned int
225 hb_ot_layout_table_get_script_tags (hb_face_t    *face,
226                                     hb_tag_t      table_tag,
227                                     unsigned int  start_offset,
228                                     unsigned int *script_count /* IN/OUT */,
229                                     hb_tag_t     *script_tags /* OUT */)
230 {
231   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
232
233   return g.get_script_tags (start_offset, script_count, script_tags);
234 }
235
236 hb_bool_t
237 hb_ot_layout_table_find_script (hb_face_t    *face,
238                                 hb_tag_t      table_tag,
239                                 hb_tag_t      script_tag,
240                                 unsigned int *script_index)
241 {
242   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
243   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
244
245   if (g.find_script_index (script_tag, script_index))
246     return TRUE;
247
248   /* try finding 'DFLT' */
249   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
250     return FALSE;
251
252   /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
253    * including many versions of DejaVu Sans Mono! */
254   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
255     return FALSE;
256
257   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
258   return FALSE;
259 }
260
261 hb_bool_t
262 hb_ot_layout_table_choose_script (hb_face_t      *face,
263                                   hb_tag_t        table_tag,
264                                   const hb_tag_t *script_tags,
265                                   unsigned int   *script_index)
266 {
267   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
268   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
269
270   while (*script_tags)
271   {
272     if (g.find_script_index (*script_tags, script_index))
273       return TRUE;
274     script_tags++;
275   }
276
277   /* try finding 'DFLT' */
278   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
279     return FALSE;
280
281   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
282   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
283     return FALSE;
284
285   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
286   return FALSE;
287 }
288
289 unsigned int
290 hb_ot_layout_table_get_feature_tags (hb_face_t    *face,
291                                      hb_tag_t      table_tag,
292                                      unsigned int  start_offset,
293                                      unsigned int *feature_count /* IN/OUT */,
294                                      hb_tag_t     *feature_tags /* OUT */)
295 {
296   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
297
298   return g.get_feature_tags (start_offset, feature_count, feature_tags);
299 }
300
301
302 unsigned int
303 hb_ot_layout_script_get_language_tags (hb_face_t    *face,
304                                        hb_tag_t      table_tag,
305                                        unsigned int  script_index,
306                                        unsigned int  start_offset,
307                                        unsigned int *language_count /* IN/OUT */,
308                                        hb_tag_t     *language_tags /* OUT */)
309 {
310   const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
311
312   return s.get_lang_sys_tags (start_offset, language_count, language_tags);
313 }
314
315 hb_bool_t
316 hb_ot_layout_script_find_language (hb_face_t    *face,
317                                    hb_tag_t      table_tag,
318                                    unsigned int  script_index,
319                                    hb_tag_t      language_tag,
320                                    unsigned int *language_index)
321 {
322   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
323   const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
324
325   if (s.find_lang_sys_index (language_tag, language_index))
326     return TRUE;
327
328   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
329   if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
330     return FALSE;
331
332   if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
333   return FALSE;
334 }
335
336 hb_bool_t
337 hb_ot_layout_language_get_required_feature_index (hb_face_t    *face,
338                                                   hb_tag_t      table_tag,
339                                                   unsigned int  script_index,
340                                                   unsigned int  language_index,
341                                                   unsigned int *feature_index)
342 {
343   const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
344
345   if (feature_index) *feature_index = l.get_required_feature_index ();
346
347   return l.has_required_feature ();
348 }
349
350 unsigned int
351 hb_ot_layout_language_get_feature_indexes (hb_face_t    *face,
352                                            hb_tag_t      table_tag,
353                                            unsigned int  script_index,
354                                            unsigned int  language_index,
355                                            unsigned int  start_offset,
356                                            unsigned int *feature_count /* IN/OUT */,
357                                            unsigned int *feature_indexes /* OUT */)
358 {
359   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
360   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
361
362   return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
363 }
364
365 unsigned int
366 hb_ot_layout_language_get_feature_tags (hb_face_t    *face,
367                                         hb_tag_t      table_tag,
368                                         unsigned int  script_index,
369                                         unsigned int  language_index,
370                                         unsigned int  start_offset,
371                                         unsigned int *feature_count /* IN/OUT */,
372                                         hb_tag_t     *feature_tags /* OUT */)
373 {
374   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
375   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
376
377   ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
378   unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
379
380   if (feature_tags) {
381     unsigned int count = *feature_count;
382     for (unsigned int i = 0; i < count; i++)
383       feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
384   }
385
386   return ret;
387 }
388
389
390 hb_bool_t
391 hb_ot_layout_language_find_feature (hb_face_t    *face,
392                                     hb_tag_t      table_tag,
393                                     unsigned int  script_index,
394                                     unsigned int  language_index,
395                                     hb_tag_t      feature_tag,
396                                     unsigned int *feature_index)
397 {
398   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
399   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
400   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
401
402   unsigned int num_features = l.get_feature_count ();
403   for (unsigned int i = 0; i < num_features; i++) {
404     unsigned int f_index = l.get_feature_index (i);
405
406     if (feature_tag == g.get_feature_tag (f_index)) {
407       if (feature_index) *feature_index = f_index;
408       return TRUE;
409     }
410   }
411
412   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
413   return FALSE;
414 }
415
416 unsigned int
417 hb_ot_layout_feature_get_lookup_indexes (hb_face_t    *face,
418                                          hb_tag_t      table_tag,
419                                          unsigned int  feature_index,
420                                          unsigned int  start_offset,
421                                          unsigned int *lookup_count /* IN/OUT */,
422                                          unsigned int *lookup_indexes /* OUT */)
423 {
424   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
425   const Feature &f = g.get_feature (feature_index);
426
427   return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
428 }
429
430
431 /*
432  * GSUB
433  */
434
435 hb_bool_t
436 hb_ot_layout_has_substitution (hb_face_t *face)
437 {
438   return &_get_gsub (face) != &Null(GSUB);
439 }
440
441 hb_bool_t
442 hb_ot_layout_substitute_lookup (hb_face_t    *face,
443                                 hb_buffer_t  *buffer,
444                                 unsigned int  lookup_index,
445                                 hb_mask_t     mask)
446 {
447   hb_ot_layout_context_t c;
448   c.font = NULL;
449   c.face = face;
450   return _get_gsub (face).substitute_lookup (&c, buffer, lookup_index, mask);
451 }
452
453
454 /*
455  * GPOS
456  */
457
458 hb_bool_t
459 hb_ot_layout_has_positioning (hb_face_t *face)
460 {
461   return &_get_gpos (face) != &Null(GPOS);
462 }
463
464 void
465 hb_ot_layout_position_start (hb_buffer_t  *buffer)
466 {
467   buffer->clear_positions ();
468 }
469
470 hb_bool_t
471 hb_ot_layout_position_lookup   (hb_font_t    *font,
472                                 hb_buffer_t  *buffer,
473                                 unsigned int  lookup_index,
474                                 hb_mask_t     mask)
475 {
476   hb_ot_layout_context_t c;
477   c.font = font;
478   c.face = font->face;
479   return _get_gpos (c.face).position_lookup (&c, buffer, lookup_index, mask);
480 }
481
482 void
483 hb_ot_layout_position_finish (hb_buffer_t  *buffer)
484 {
485   GPOS::position_finish (buffer);
486 }
487
488
489 HB_END_DECLS