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
41HB_BEGIN_DECLS
42
43
44typedef 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
54typedef 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
65typedef 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
80hb_bool_t
81hb_segment_properties_equal (const hb_segment_properties_t *a,
82			     const hb_segment_properties_t *b);
83
84unsigned int
85hb_segment_properties_hash (const hb_segment_properties_t *p);
86
87
88
89/*
90 * hb_buffer_t
91 */
92
93typedef struct hb_buffer_t hb_buffer_t;
94
95hb_buffer_t *
96hb_buffer_create (void);
97
98hb_buffer_t *
99hb_buffer_get_empty (void);
100
101hb_buffer_t *
102hb_buffer_reference (hb_buffer_t *buffer);
103
104void
105hb_buffer_destroy (hb_buffer_t *buffer);
106
107hb_bool_t
108hb_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
114void *
115hb_buffer_get_user_data (hb_buffer_t        *buffer,
116			 hb_user_data_key_t *key);
117
118
119typedef 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
125void
126hb_buffer_set_content_type (hb_buffer_t              *buffer,
127			    hb_buffer_content_type_t  content_type);
128
129hb_buffer_content_type_t
130hb_buffer_get_content_type (hb_buffer_t *buffer);
131
132
133void
134hb_buffer_set_unicode_funcs (hb_buffer_t        *buffer,
135			     hb_unicode_funcs_t *unicode_funcs);
136
137hb_unicode_funcs_t *
138hb_buffer_get_unicode_funcs (hb_buffer_t        *buffer);
139
140void
141hb_buffer_set_direction (hb_buffer_t    *buffer,
142			 hb_direction_t  direction);
143
144hb_direction_t
145hb_buffer_get_direction (hb_buffer_t *buffer);
146
147void
148hb_buffer_set_script (hb_buffer_t *buffer,
149		      hb_script_t  script);
150
151hb_script_t
152hb_buffer_get_script (hb_buffer_t *buffer);
153
154void
155hb_buffer_set_language (hb_buffer_t   *buffer,
156			hb_language_t  language);
157
158
159hb_language_t
160hb_buffer_get_language (hb_buffer_t *buffer);
161
162void
163hb_buffer_set_segment_properties (hb_buffer_t *buffer,
164				  const hb_segment_properties_t *props);
165
166void
167hb_buffer_get_segment_properties (hb_buffer_t *buffer,
168				  hb_segment_properties_t *props);
169
170void
171hb_buffer_guess_segment_properties (hb_buffer_t *buffer);
172
173
174typedef enum {
175  HB_BUFFER_FLAGS_DEFAULT			= 0x00000000,
176  HB_BUFFER_FLAG_BOT				= 0x00000001, /* Beginning-of-text */
177  HB_BUFFER_FLAG_EOT				= 0x00000002, /* End-of-text */
178  HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES	= 0x00000004
179} hb_buffer_flags_t;
180
181void
182hb_buffer_set_flags (hb_buffer_t       *buffer,
183		     hb_buffer_flags_t  flags);
184
185hb_buffer_flags_t
186hb_buffer_get_flags (hb_buffer_t *buffer);
187
188
189/* Resets the buffer.  Afterwards it's as if it was just created,
190 * except that it has a larger buffer allocated perhaps... */
191void
192hb_buffer_reset (hb_buffer_t *buffer);
193
194/* Like reset, but does NOT clear unicode_funcs. */
195void
196hb_buffer_clear_contents (hb_buffer_t *buffer);
197
198/* Returns false if allocation failed */
199hb_bool_t
200hb_buffer_pre_allocate (hb_buffer_t  *buffer,
201		        unsigned int  size);
202
203
204/* Returns false if allocation has failed before */
205hb_bool_t
206hb_buffer_allocation_successful (hb_buffer_t  *buffer);
207
208void
209hb_buffer_reverse (hb_buffer_t *buffer);
210
211void
212hb_buffer_reverse_clusters (hb_buffer_t *buffer);
213
214
215/* Filling the buffer in */
216
217void
218hb_buffer_add (hb_buffer_t    *buffer,
219	       hb_codepoint_t  codepoint,
220	       unsigned int    cluster);
221
222void
223hb_buffer_add_utf8 (hb_buffer_t  *buffer,
224		    const char   *text,
225		    int           text_length,
226		    unsigned int  item_offset,
227		    int           item_length);
228
229void
230hb_buffer_add_utf16 (hb_buffer_t    *buffer,
231		     const uint16_t *text,
232		     int             text_length,
233		     unsigned int    item_offset,
234		     int             item_length);
235
236void
237hb_buffer_add_utf32 (hb_buffer_t    *buffer,
238		     const uint32_t *text,
239		     int             text_length,
240		     unsigned int    item_offset,
241		     int             item_length);
242
243
244/* Clears any new items added at the end */
245hb_bool_t
246hb_buffer_set_length (hb_buffer_t  *buffer,
247		      unsigned int  length);
248
249/* Return value valid as long as buffer not modified */
250unsigned int
251hb_buffer_get_length (hb_buffer_t *buffer);
252
253/* Getting glyphs out of the buffer */
254
255/* Return value valid as long as buffer not modified */
256hb_glyph_info_t *
257hb_buffer_get_glyph_infos (hb_buffer_t  *buffer,
258                           unsigned int *length);
259
260/* Return value valid as long as buffer not modified */
261hb_glyph_position_t *
262hb_buffer_get_glyph_positions (hb_buffer_t  *buffer,
263                               unsigned int *length);
264
265
266/* Reorders a glyph buffer to have canonical in-cluster glyph order / position.
267 * The resulting clusters should behave identical to pre-reordering clusters.
268 * NOTE: This has nothing to do with Unicode normalization. */
269void
270hb_buffer_normalize_glyphs (hb_buffer_t *buffer);
271
272
273/*
274 * Serialize
275 */
276
277typedef enum {
278  HB_BUFFER_SERIALIZE_FLAGS_DEFAULT		= 0x00000000,
279  HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS		= 0x00000001,
280  HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS		= 0x00000002,
281  HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES	= 0x00000004
282} hb_buffer_serialize_flags_t;
283
284typedef enum {
285  HB_BUFFER_SERIALIZE_FORMAT_TEXT	= HB_TAG('T','E','X','T'),
286  HB_BUFFER_SERIALIZE_FORMAT_JSON	= HB_TAG('J','S','O','N'),
287  HB_BUFFER_SERIALIZE_FORMAT_INVALID	= HB_TAG_NONE
288} hb_buffer_serialize_format_t;
289
290/* len=-1 means str is NUL-terminated. */
291hb_buffer_serialize_format_t
292hb_buffer_serialize_format_from_string (const char *str, int len);
293
294const char *
295hb_buffer_serialize_format_to_string (hb_buffer_serialize_format_t format);
296
297const char **
298hb_buffer_serialize_list_formats (void);
299
300/* Returns number of items, starting at start, that were serialized. */
301unsigned int
302hb_buffer_serialize_glyphs (hb_buffer_t *buffer,
303			    unsigned int start,
304			    unsigned int end,
305			    char *buf,
306			    unsigned int buf_size,
307			    unsigned int *buf_consumed, /* May be NULL */
308			    hb_font_t *font, /* May be NULL */
309			    hb_buffer_serialize_format_t format,
310			    hb_buffer_serialize_flags_t flags);
311
312hb_bool_t
313hb_buffer_deserialize_glyphs (hb_buffer_t *buffer,
314			      const char *buf,
315			      int buf_len, /* -1 means nul-terminated */
316			      const char **end_ptr, /* May be NULL */
317			      hb_font_t *font, /* May be NULL */
318			      hb_buffer_serialize_format_t format);
319
320
321HB_END_DECLS
322
323#endif /* HB_BUFFER_H */
324