Imported Upstream version 0.9.3
[platform/upstream/harfbuzz.git] / src / hb-ot-layout-private.hh
1 /*
2  * Copyright © 2007,2008,2009  Red Hat, Inc.
3  * Copyright © 2012  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #ifndef HB_OT_LAYOUT_PRIVATE_HH
30 #define HB_OT_LAYOUT_PRIVATE_HH
31
32 #include "hb-private.hh"
33
34 #include "hb-ot-layout.h"
35
36 #include "hb-font-private.hh"
37 #include "hb-buffer-private.hh"
38 #include "hb-set-private.hh"
39
40
41 /* buffer var allocations, used during the GSUB/GPOS processing */
42 #define glyph_props()           var1.u16[0] /* GDEF glyph properties */
43 #define syllable()              var1.u8[2] /* GSUB/GPOS shaping boundaries */
44 #define lig_props()             var1.u8[3] /* GSUB/GPOS ligature tracking */
45
46 #define hb_ot_layout_from_face(face) ((hb_ot_layout_t *) face->shaper_data.ot)
47
48 /*
49  * GDEF
50  */
51
52 typedef enum {
53   HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED = 0x0001,
54   HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH   = 0x0002,
55   HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE     = 0x0004,
56   HB_OT_LAYOUT_GLYPH_CLASS_MARK         = 0x0008,
57   HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT    = 0x0010
58 } hb_ot_layout_glyph_class_t;
59
60
61
62 /*
63  * GSUB/GPOS
64  */
65
66 /* lig_id / lig_comp
67  *
68  * When a ligature is formed:
69  *
70  *   - The ligature glyph and any marks in between all the same newly allocated
71  *     lig_id,
72  *   - The ligature glyph will get lig_num_comps set to the number of components
73  *   - The marks get lig_comp > 0, reflecting which component of the ligature
74  *     they were applied to.
75  *   - This is used in GPOS to attach marks to the right component of a ligature
76  *     in MarkLigPos.
77  *
78  * When a multiple-substitution is done:
79  *
80  *   - All resulting glyphs will have lig_id = 0,
81  *   - The resulting glyphs will have lig_comp = 0, 1, 2, ... respectively.
82  *   - This is used in GPOS to attach marks to the first component of a
83  *     multiple substitution in MarkBasePos.
84  *
85  * The numbers are also used in GPOS to do mark-to-mark positioning only
86  * to marks that belong to the same component of a ligature in MarkMarPos.
87  */
88 #define IS_LIG_BASE 0x10
89 static inline void
90 set_lig_props_for_ligature (hb_glyph_info_t &info, unsigned int lig_id, unsigned int lig_num_comps)
91 {
92   info.lig_props() = (lig_id << 5) | IS_LIG_BASE | (lig_num_comps & 0x0F);
93 }
94 static inline void
95 set_lig_props_for_mark (hb_glyph_info_t &info, unsigned int lig_id, unsigned int lig_comp)
96 {
97   info.lig_props() = (lig_id << 5) | (lig_comp & 0x0F);
98 }
99 static inline void
100 set_lig_props_for_component (hb_glyph_info_t &info, unsigned int comp)
101 {
102   set_lig_props_for_mark (info, 0, comp);
103 }
104
105 static inline unsigned int
106 get_lig_id (const hb_glyph_info_t &info)
107 {
108   return info.lig_props() >> 5;
109 }
110 static inline bool
111 is_a_ligature (const hb_glyph_info_t &info)
112 {
113   return !!(info.lig_props() & IS_LIG_BASE);
114 }
115 static inline unsigned int
116 get_lig_comp (const hb_glyph_info_t &info)
117 {
118   if (is_a_ligature (info))
119     return 0;
120   else
121     return info.lig_props() & 0x0F;
122 }
123 static inline unsigned int
124 get_lig_num_comps (const hb_glyph_info_t &info)
125 {
126   if ((info.glyph_props() & HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE) && is_a_ligature (info))
127     return info.lig_props() & 0x0F;
128   else
129     return 1;
130 }
131
132 static inline uint8_t allocate_lig_id (hb_buffer_t *buffer) {
133   uint8_t lig_id = buffer->next_serial () & 0x07;
134   if (unlikely (!lig_id))
135     lig_id = allocate_lig_id (buffer); /* in case of overflow */
136   return lig_id;
137 }
138
139
140 HB_INTERNAL hb_bool_t
141 hb_ot_layout_would_substitute_lookup_fast (hb_face_t            *face,
142                                            const hb_codepoint_t *glyphs,
143                                            unsigned int          glyphs_length,
144                                            unsigned int          lookup_index);
145
146
147 /* Should be called before all the substitute_lookup's are done. */
148 HB_INTERNAL void
149 hb_ot_layout_substitute_start (hb_font_t    *font,
150                                hb_buffer_t  *buffer);
151
152 HB_INTERNAL hb_bool_t
153 hb_ot_layout_substitute_lookup (hb_font_t    *font,
154                                 hb_buffer_t  *buffer,
155                                 unsigned int  lookup_index,
156                                 hb_mask_t     mask);
157
158 /* Should be called after all the substitute_lookup's are done */
159 HB_INTERNAL void
160 hb_ot_layout_substitute_finish (hb_font_t    *font,
161                                 hb_buffer_t  *buffer);
162
163
164 /* Should be called before all the position_lookup's are done.  Resets positions to zero. */
165 HB_INTERNAL void
166 hb_ot_layout_position_start (hb_font_t    *font,
167                              hb_buffer_t  *buffer);
168
169 HB_INTERNAL hb_bool_t
170 hb_ot_layout_position_lookup (hb_font_t    *font,
171                               hb_buffer_t  *buffer,
172                               unsigned int  lookup_index,
173                               hb_mask_t     mask);
174
175 /* Should be called after all the position_lookup's are done */
176 HB_INTERNAL void
177 hb_ot_layout_position_finish (hb_font_t    *font,
178                               hb_buffer_t  *buffer,
179                               hb_bool_t     zero_width_attached_marks);
180
181
182
183 /*
184  * hb_ot_layout_t
185  */
186
187 struct hb_ot_layout_t
188 {
189   hb_blob_t *gdef_blob;
190   hb_blob_t *gsub_blob;
191   hb_blob_t *gpos_blob;
192
193   const struct GDEF *gdef;
194   const struct GSUB *gsub;
195   const struct GPOS *gpos;
196
197   unsigned int gsub_lookup_count;
198   unsigned int gpos_lookup_count;
199
200   hb_set_digest_t *gsub_digests;
201   hb_set_digest_t *gpos_digests;
202 };
203
204
205 HB_INTERNAL hb_ot_layout_t *
206 _hb_ot_layout_create (hb_face_t *face);
207
208 HB_INTERNAL void
209 _hb_ot_layout_destroy (hb_ot_layout_t *layout);
210
211
212
213 #endif /* HB_OT_LAYOUT_PRIVATE_HH */