49093de8a1e0abb7ae78032cca1d9f92d517e205
[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_PROPS_UNCLASSIFIED = 0x0001,
54   HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH   = 0x0002,
55   HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE     = 0x0004,
56   HB_OT_LAYOUT_GLYPH_PROPS_MARK         = 0x0008,
57   HB_OT_LAYOUT_GLYPH_PROPS_COMPONENT    = 0x0010
58 } hb_ot_layout_glyph_class_mask_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_PROPS_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_lookup_would_substitute_fast (hb_face_t            *face,
142                                            unsigned int          lookup_index,
143                                            const hb_codepoint_t *glyphs,
144                                            unsigned int          glyphs_length,
145                                            hb_bool_t             zero_context);
146
147
148 /* Should be called before all the substitute_lookup's are done. */
149 HB_INTERNAL void
150 hb_ot_layout_substitute_start (hb_font_t    *font,
151                                hb_buffer_t  *buffer);
152
153 HB_INTERNAL hb_bool_t
154 hb_ot_layout_substitute_lookup (hb_font_t    *font,
155                                 hb_buffer_t  *buffer,
156                                 unsigned int  lookup_index,
157                                 hb_mask_t     mask);
158
159 /* Should be called after all the substitute_lookup's are done */
160 HB_INTERNAL void
161 hb_ot_layout_substitute_finish (hb_font_t    *font,
162                                 hb_buffer_t  *buffer);
163
164
165 /* Should be called before all the position_lookup's are done.  Resets positions to zero. */
166 HB_INTERNAL void
167 hb_ot_layout_position_start (hb_font_t    *font,
168                              hb_buffer_t  *buffer);
169
170 HB_INTERNAL hb_bool_t
171 hb_ot_layout_position_lookup (hb_font_t    *font,
172                               hb_buffer_t  *buffer,
173                               unsigned int  lookup_index,
174                               hb_mask_t     mask);
175
176 /* Should be called after all the position_lookup's are done */
177 HB_INTERNAL void
178 hb_ot_layout_position_finish (hb_font_t    *font,
179                               hb_buffer_t  *buffer,
180                               hb_bool_t     zero_width_attached_marks);
181
182
183
184 /*
185  * hb_ot_layout_t
186  */
187
188 namespace OT {
189   struct GDEF;
190   struct GSUB;
191   struct GPOS;
192 }
193
194 struct hb_ot_layout_t
195 {
196   hb_blob_t *gdef_blob;
197   hb_blob_t *gsub_blob;
198   hb_blob_t *gpos_blob;
199
200   const struct OT::GDEF *gdef;
201   const struct OT::GSUB *gsub;
202   const struct OT::GPOS *gpos;
203
204   unsigned int gsub_lookup_count;
205   unsigned int gpos_lookup_count;
206
207   hb_set_digest_t *gsub_digests;
208   hb_set_digest_t *gpos_digests;
209 };
210
211
212 HB_INTERNAL hb_ot_layout_t *
213 _hb_ot_layout_create (hb_face_t *face);
214
215 HB_INTERNAL void
216 _hb_ot_layout_destroy (hb_ot_layout_t *layout);
217
218
219
220 #endif /* HB_OT_LAYOUT_PRIVATE_HH */