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