1 /*
2  * Copyright © 1998-2004  David Turner and Werner Lemberg
3  * Copyright © 2004,2007,2009  Red Hat, Inc.
4  * Copyright © 2011,2012  Google, 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): Owen Taylor, Behdad Esfahbod
27  * Google Author(s): Behdad Esfahbod
28  */
29 
30 #ifndef HB_H_IN
31 #error "Include <hb.h> instead."
32 #endif
33 
34 #ifndef HB_BUFFER_H
35 #define HB_BUFFER_H
36 
37 #include "hb-common.h"
38 #include "hb-unicode.h"
39 #include "hb-font.h"
40 
41 HB_BEGIN_DECLS
42 
43 
44 typedef struct hb_glyph_info_t {
45   hb_codepoint_t codepoint;
46   hb_mask_t      mask;
47   uint32_t       cluster;
48 
49   /*< private >*/
50   hb_var_int_t   var1;
51   hb_var_int_t   var2;
52 } hb_glyph_info_t;
53 
54 typedef struct hb_glyph_position_t {
55   hb_position_t  x_advance;
56   hb_position_t  y_advance;
57   hb_position_t  x_offset;
58   hb_position_t  y_offset;
59 
60   /*< private >*/
61   hb_var_int_t   var;
62 } hb_glyph_position_t;
63 
64 
65 typedef struct hb_segment_properties_t {
66   hb_direction_t  direction;
67   hb_script_t     script;
68   hb_language_t   language;
69   /*< private >*/
70   void           *reserved1;
71   void           *reserved2;
72 } hb_segment_properties_t;
73 
74 #define HB_SEGMENT_PROPERTIES_DEFAULT {HB_DIRECTION_INVALID, \
75 				       HB_SCRIPT_INVALID, \
76 				       HB_LANGUAGE_INVALID, \
77 				       NULL, \
78 				       NULL}
79 
80 hb_bool_t
81 hb_segment_properties_equal (const hb_segment_properties_t *a,
82 			     const hb_segment_properties_t *b);
83 
84 unsigned int
85 hb_segment_properties_hash (const hb_segment_properties_t *p);
86 
87 
88 
89 /*
90  * hb_buffer_t
91  */
92 
93 typedef struct hb_buffer_t hb_buffer_t;
94 
95 hb_buffer_t *
96 hb_buffer_create (void);
97 
98 hb_buffer_t *
99 hb_buffer_get_empty (void);
100 
101 hb_buffer_t *
102 hb_buffer_reference (hb_buffer_t *buffer);
103 
104 void
105 hb_buffer_destroy (hb_buffer_t *buffer);
106 
107 hb_bool_t
108 hb_buffer_set_user_data (hb_buffer_t        *buffer,
109 			 hb_user_data_key_t *key,
110 			 void *              data,
111 			 hb_destroy_func_t   destroy,
112 			 hb_bool_t           replace);
113 
114 void *
115 hb_buffer_get_user_data (hb_buffer_t        *buffer,
116 			 hb_user_data_key_t *key);
117 
118 
119 typedef enum {
120   HB_BUFFER_CONTENT_TYPE_INVALID = 0,
121   HB_BUFFER_CONTENT_TYPE_UNICODE,
122   HB_BUFFER_CONTENT_TYPE_GLYPHS
123 } hb_buffer_content_type_t;
124 
125 void
126 hb_buffer_set_content_type (hb_buffer_t              *buffer,
127 			    hb_buffer_content_type_t  content_type);
128 
129 hb_buffer_content_type_t
130 hb_buffer_get_content_type (hb_buffer_t *buffer);
131 
132 
133 void
134 hb_buffer_set_unicode_funcs (hb_buffer_t        *buffer,
135 			     hb_unicode_funcs_t *unicode_funcs);
136 
137 hb_unicode_funcs_t *
138 hb_buffer_get_unicode_funcs (hb_buffer_t        *buffer);
139 
140 void
141 hb_buffer_set_direction (hb_buffer_t    *buffer,
142 			 hb_direction_t  direction);
143 
144 hb_direction_t
145 hb_buffer_get_direction (hb_buffer_t *buffer);
146 
147 void
148 hb_buffer_set_script (hb_buffer_t *buffer,
149 		      hb_script_t  script);
150 
151 hb_script_t
152 hb_buffer_get_script (hb_buffer_t *buffer);
153 
154 void
155 hb_buffer_set_language (hb_buffer_t   *buffer,
156 			hb_language_t  language);
157 
158 
159 hb_language_t
160 hb_buffer_get_language (hb_buffer_t *buffer);
161 
162 void
163 hb_buffer_set_segment_properties (hb_buffer_t *buffer,
164 				  const hb_segment_properties_t *props);
165 
166 void
167 hb_buffer_get_segment_properties (hb_buffer_t *buffer,
168 				  hb_segment_properties_t *props);
169 
170 void
171 hb_buffer_guess_segment_properties (hb_buffer_t *buffer);
172 
173 
174 typedef enum { /*< flags >*/
175   HB_BUFFER_FLAG_DEFAULT			= 0x00000000u,
176   HB_BUFFER_FLAG_BOT				= 0x00000001u, /* Beginning-of-text */
177   HB_BUFFER_FLAG_EOT				= 0x00000002u, /* End-of-text */
178   HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES	= 0x00000004u
179 } hb_buffer_flags_t;
180 
181 void
182 hb_buffer_set_flags (hb_buffer_t       *buffer,
183 		     hb_buffer_flags_t  flags);
184 
185 hb_buffer_flags_t
186 hb_buffer_get_flags (hb_buffer_t *buffer);
187 
188 
189 
190 #define HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT 0xFFFDu
191 
192 /* Sets codepoint used to replace invalid UTF-8/16/32 entries.
193  * Default is 0xFFFDu. */
194 void
195 hb_buffer_set_replacement_codepoint (hb_buffer_t    *buffer,
196 				     hb_codepoint_t  replacement);
197 
198 hb_codepoint_t
199 hb_buffer_get_replacement_codepoint (hb_buffer_t    *buffer);
200 
201 
202 /* Resets the buffer.  Afterwards it's as if it was just created,
203  * except that it has a larger buffer allocated perhaps... */
204 void
205 hb_buffer_reset (hb_buffer_t *buffer);
206 
207 /* Like reset, but does NOT clear unicode_funcs and replacement_codepoint. */
208 void
209 hb_buffer_clear_contents (hb_buffer_t *buffer);
210 
211 /* Returns false if allocation failed */
212 hb_bool_t
213 hb_buffer_pre_allocate (hb_buffer_t  *buffer,
214 		        unsigned int  size);
215 
216 
217 /* Returns false if allocation has failed before */
218 hb_bool_t
219 hb_buffer_allocation_successful (hb_buffer_t  *buffer);
220 
221 void
222 hb_buffer_reverse (hb_buffer_t *buffer);
223 
224 void
225 hb_buffer_reverse_clusters (hb_buffer_t *buffer);
226 
227 
228 /* Filling the buffer in */
229 
230 void
231 hb_buffer_add (hb_buffer_t    *buffer,
232 	       hb_codepoint_t  codepoint,
233 	       unsigned int    cluster);
234 
235 void
236 hb_buffer_add_utf8 (hb_buffer_t  *buffer,
237 		    const char   *text,
238 		    int           text_length,
239 		    unsigned int  item_offset,
240 		    int           item_length);
241 
242 void
243 hb_buffer_add_utf16 (hb_buffer_t    *buffer,
244 		     const uint16_t *text,
245 		     int             text_length,
246 		     unsigned int    item_offset,
247 		     int             item_length);
248 
249 void
250 hb_buffer_add_utf32 (hb_buffer_t    *buffer,
251 		     const uint32_t *text,
252 		     int             text_length,
253 		     unsigned int    item_offset,
254 		     int             item_length);
255 
256 /* Like add_utf32 but does NOT check for invalid Unicode codepoints. */
257 void
258 hb_buffer_add_codepoints (hb_buffer_t          *buffer,
259 			  const hb_codepoint_t *text,
260 			  int                   text_length,
261 			  unsigned int          item_offset,
262 			  int                   item_length);
263 
264 
265 /* Clears any new items added at the end */
266 hb_bool_t
267 hb_buffer_set_length (hb_buffer_t  *buffer,
268 		      unsigned int  length);
269 
270 /* Return value valid as long as buffer not modified */
271 unsigned int
272 hb_buffer_get_length (hb_buffer_t *buffer);
273 
274 /* Getting glyphs out of the buffer */
275 
276 /* Return value valid as long as buffer not modified */
277 hb_glyph_info_t *
278 hb_buffer_get_glyph_infos (hb_buffer_t  *buffer,
279                            unsigned int *length);
280 
281 /* Return value valid as long as buffer not modified */
282 hb_glyph_position_t *
283 hb_buffer_get_glyph_positions (hb_buffer_t  *buffer,
284                                unsigned int *length);
285 
286 
287 /* Reorders a glyph buffer to have canonical in-cluster glyph order / position.
288  * The resulting clusters should behave identical to pre-reordering clusters.
289  * NOTE: This has nothing to do with Unicode normalization. */
290 void
291 hb_buffer_normalize_glyphs (hb_buffer_t *buffer);
292 
293 
294 /*
295  * Serialize
296  */
297 
298 typedef enum { /*< flags >*/
299   HB_BUFFER_SERIALIZE_FLAG_DEFAULT		= 0x00000000u,
300   HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS		= 0x00000001u,
301   HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS		= 0x00000002u,
302   HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES	= 0x00000004u
303 } hb_buffer_serialize_flags_t;
304 
305 typedef enum {
306   HB_BUFFER_SERIALIZE_FORMAT_TEXT	= HB_TAG('T','E','X','T'),
307   HB_BUFFER_SERIALIZE_FORMAT_JSON	= HB_TAG('J','S','O','N'),
308   HB_BUFFER_SERIALIZE_FORMAT_INVALID	= HB_TAG_NONE
309 } hb_buffer_serialize_format_t;
310 
311 /* len=-1 means str is NUL-terminated. */
312 hb_buffer_serialize_format_t
313 hb_buffer_serialize_format_from_string (const char *str, int len);
314 
315 const char *
316 hb_buffer_serialize_format_to_string (hb_buffer_serialize_format_t format);
317 
318 const char **
319 hb_buffer_serialize_list_formats (void);
320 
321 /* Returns number of items, starting at start, that were serialized. */
322 unsigned int
323 hb_buffer_serialize_glyphs (hb_buffer_t *buffer,
324 			    unsigned int start,
325 			    unsigned int end,
326 			    char *buf,
327 			    unsigned int buf_size,
328 			    unsigned int *buf_consumed, /* May be NULL */
329 			    hb_font_t *font, /* May be NULL */
330 			    hb_buffer_serialize_format_t format,
331 			    hb_buffer_serialize_flags_t flags);
332 
333 hb_bool_t
334 hb_buffer_deserialize_glyphs (hb_buffer_t *buffer,
335 			      const char *buf,
336 			      int buf_len, /* -1 means nul-terminated */
337 			      const char **end_ptr, /* May be NULL */
338 			      hb_font_t *font, /* May be NULL */
339 			      hb_buffer_serialize_format_t format);
340 
341 
342 HB_END_DECLS
343 
344 #endif /* HB_BUFFER_H */
345