blob: 111078c240c04ac7b6050855b32274f73c69da4c [file] [log] [blame]
Behdad Esfahboda2a9a022008-01-15 22:46:32 +00001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 1998-2004 David Turner and Werner Lemberg
3 * Copyright © 2004,2007,2009,2010 Red Hat, Inc.
Behdad Esfahbod05207a72012-09-25 17:44:53 -04004 * Copyright © 2011,2012 Google, Inc.
Behdad Esfahbod6b347132007-10-11 08:30:50 +00005 *
Behdad Esfahbod8f0d7e02011-04-15 18:59:56 -04006 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod6b347132007-10-11 08:30:50 +00007 *
Behdad Esfahboda2a9a022008-01-15 22:46:32 +00008 * 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.
Behdad Esfahbod6b347132007-10-11 08:30:50 +000013 *
Behdad Esfahboda2a9a022008-01-15 22:46:32 +000014 * 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
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -040027 * Google Author(s): Behdad Esfahbod
Behdad Esfahbod6b347132007-10-11 08:30:50 +000028 */
Behdad Esfahboda2a9a022008-01-15 22:46:32 +000029
Behdad Esfahbodda8edbb2010-06-09 07:15:39 -040030#ifndef HB_BUFFER_PRIVATE_HH
31#define HB_BUFFER_PRIVATE_HH
Behdad Esfahbod6b347132007-10-11 08:30:50 +000032
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040033#include "hb-private.hh"
Behdad Esfahbodfca368c2011-04-21 18:24:02 -040034#include "hb-object-private.hh"
Behdad Esfahbodfb194b82011-04-20 02:00:47 -040035#include "hb-unicode-private.hh"
Behdad Esfahbod6b347132007-10-11 08:30:50 +000036
Behdad Esfahbod6b347132007-10-11 08:30:50 +000037
Behdad Esfahbod43017032015-11-05 23:44:59 -080038#ifndef HB_BUFFER_MAX_EXPANSION_FACTOR
39#define HB_BUFFER_MAX_EXPANSION_FACTOR 32
40#endif
41#ifndef HB_BUFFER_MAX_LEN_MIN
42#define HB_BUFFER_MAX_LEN_MIN 8192
43#endif
Behdad Esfahbodf1c4adb2015-11-06 09:46:05 -080044#ifndef HB_BUFFER_MAX_LEN_DEFAULT
Behdad Esfahbod43017032015-11-05 23:44:59 -080045#define HB_BUFFER_MAX_LEN_DEFAULT 0x3FFFFFFF /* Shaping more than a billion chars? Let us know! */
46#endif
47
Behdad Esfahbod88474c62010-10-27 14:42:15 -040048ASSERT_STATIC (sizeof (hb_glyph_info_t) == 20);
Behdad Esfahbodf1322e52009-08-01 22:53:04 -040049ASSERT_STATIC (sizeof (hb_glyph_info_t) == sizeof (hb_glyph_position_t));
50
Chun-wei Fan167c3272015-11-09 17:17:56 +080051HB_MARK_AS_FLAG_T (hb_buffer_flags_t);
52HB_MARK_AS_FLAG_T (hb_buffer_serialize_flags_t);
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -080053
Behdad Esfahbod49ef6302015-11-04 17:27:07 -080054enum hb_buffer_scratch_flags_t {
55 HB_BUFFER_SCRATCH_FLAG_DEFAULT = 0x00000000u,
Behdad Esfahbode3e4bb02015-11-04 18:58:02 -080056 HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII = 0x00000001u,
57 HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES = 0x00000002u,
58 HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK = 0x00000004u,
Behdad Esfahbod550bd142015-11-04 22:58:58 -080059 HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_CURSIVE = 0x00000008u,
60 HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT = 0x00000010u,
Behdad Esfahbod73fec392015-11-05 15:22:18 -080061 /* Reserved for complex shapers' internal use. */
62 HB_BUFFER_SCRATCH_FLAG_COMPLEX0 = 0x01000000u,
63 HB_BUFFER_SCRATCH_FLAG_COMPLEX1 = 0x02000000u,
64 HB_BUFFER_SCRATCH_FLAG_COMPLEX2 = 0x04000000u,
65 HB_BUFFER_SCRATCH_FLAG_COMPLEX3 = 0x08000000u,
Behdad Esfahbod49ef6302015-11-04 17:27:07 -080066};
Chun-wei Fan167c3272015-11-09 17:17:56 +080067HB_MARK_AS_FLAG_T (hb_buffer_scratch_flags_t);
Behdad Esfahbod49ef6302015-11-04 17:27:07 -080068
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -040069
70/*
Behdad Esfahbod2f87ceb2012-07-27 04:02:38 -040071 * hb_buffer_t
72 */
Behdad Esfahbodf1322e52009-08-01 22:53:04 -040073
Behdad Esfahbod1bc1cb32012-06-16 15:21:55 -040074struct hb_buffer_t {
Behdad Esfahbodfca368c2011-04-21 18:24:02 -040075 hb_object_header_t header;
Behdad Esfahbod6220e5f2012-06-06 03:30:09 -040076 ASSERT_POD ();
Behdad Esfahbodd0316a82010-05-12 23:34:52 -040077
78 /* Information about how the text in the buffer should be treated */
Behdad Esfahboda7c50462010-10-08 18:47:47 -040079 hb_unicode_funcs_t *unicode; /* Unicode functions */
Behdad Esfahbod0c7df222012-11-13 14:42:35 -080080 hb_buffer_flags_t flags; /* BOT / EOT / etc. */
Behdad Esfahbod376d5872015-07-22 16:51:12 +010081 hb_buffer_cluster_level_t cluster_level;
Behdad Esfahbod976c8f42014-07-16 15:34:20 -040082 hb_codepoint_t replacement; /* U+FFFD or something else. */
Behdad Esfahbod49ef6302015-11-04 17:27:07 -080083 hb_buffer_scratch_flags_t scratch_flags; /* Have space-flallback, etc. */
Behdad Esfahbod43017032015-11-05 23:44:59 -080084 unsigned int max_len; /* Maximum allowed len. */
Behdad Esfahbodd0316a82010-05-12 23:34:52 -040085
86 /* Buffer contents */
Behdad Esfahbod96fdc042012-09-06 22:26:16 -040087 hb_buffer_content_type_t content_type;
Behdad Esfahbodb5fbc3b2014-08-11 18:40:01 -040088 hb_segment_properties_t props; /* Script, language, direction */
Behdad Esfahbod96fdc042012-09-06 22:26:16 -040089
Behdad Esfahbode0db4b82011-04-28 12:56:49 -040090 bool in_error; /* Allocation failed */
Behdad Esfahbodfca368c2011-04-21 18:24:02 -040091 bool have_output; /* Whether we have an output buffer going on */
92 bool have_positions; /* Whether we have positions */
Behdad Esfahbod910a33f2010-05-14 22:13:38 -040093
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -040094 unsigned int idx; /* Cursor into ->info and ->pos arrays */
Behdad Esfahbod910a33f2010-05-14 22:13:38 -040095 unsigned int len; /* Length of ->info and ->pos arrays */
Behdad Esfahbodcc1a8a92011-01-06 14:58:52 -050096 unsigned int out_len; /* Length of ->out array if have_output */
Behdad Esfahbodd0316a82010-05-12 23:34:52 -040097
Behdad Esfahbodc0af1932011-04-15 19:26:24 -040098 unsigned int allocated; /* Length of allocated arrays */
Behdad Esfahbod88474c62010-10-27 14:42:15 -040099 hb_glyph_info_t *info;
100 hb_glyph_info_t *out_info;
101 hb_glyph_position_t *pos;
Behdad Esfahbodd0316a82010-05-12 23:34:52 -0400102
Behdad Esfahbod99c26952012-05-13 15:45:18 +0200103 inline hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; }
104 inline hb_glyph_info_t cur (unsigned int i = 0) const { return info[idx + i]; }
105
106 inline hb_glyph_position_t &cur_pos (unsigned int i = 0) { return pos[idx + i]; }
107 inline hb_glyph_position_t cur_pos (unsigned int i = 0) const { return pos[idx + i]; }
108
Behdad Esfahbod85658392015-11-04 14:46:52 -0800109 inline hb_glyph_info_t &prev (void) { return out_info[out_len ? out_len - 1 : 0]; }
110 inline hb_glyph_info_t prev (void) const { return out_info[out_len ? out_len - 1 : 0]; }
Behdad Esfahbod99c26952012-05-13 15:45:18 +0200111
Behdad Esfahbod8b63efb2013-05-02 14:29:32 -0400112 inline bool has_separate_output (void) const { return info != out_info; }
113
Behdad Esfahbodf4a579b2011-07-25 16:20:16 -0400114 unsigned int serial;
Behdad Esfahbod05207a72012-09-25 17:44:53 -0400115
116 /* These reflect current allocations of the bytes in glyph_info_t's var1 and var2. */
Behdad Esfahbodf4a579b2011-07-25 16:20:16 -0400117 uint8_t allocated_var_bytes[8];
118 const char *allocated_var_owner[8];
119
Behdad Esfahbod05207a72012-09-25 17:44:53 -0400120 /* Text before / after the main buffer contents.
121 * Always in Unicode, and ordered outward.
122 * Index 0 is for "pre-context", 1 for "post-context". */
123 static const unsigned int CONTEXT_LENGTH = 5;
124 hb_codepoint_t context[2][CONTEXT_LENGTH];
125 unsigned int context_len[2];
126
Behdad Esfahbodd0316a82010-05-12 23:34:52 -0400127
128 /* Methods */
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400129
130 HB_INTERNAL void reset (void);
Behdad Esfahbod82ecaff2012-11-13 13:57:52 -0800131 HB_INTERNAL void clear (void);
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400132
Behdad Esfahbodcc1a8a92011-01-06 14:58:52 -0500133 inline unsigned int backtrack_len (void) const
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400134 { return have_output? out_len : idx; }
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200135 inline unsigned int lookahead_len (void) const
136 { return len - idx; }
Behdad Esfahboddbf56b12010-10-27 17:06:12 -0400137 inline unsigned int next_serial (void) { return serial++; }
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400138
Behdad Esfahbodf4a579b2011-07-25 16:20:16 -0400139 HB_INTERNAL void allocate_var (unsigned int byte_i, unsigned int count, const char *owner);
140 HB_INTERNAL void deallocate_var (unsigned int byte_i, unsigned int count, const char *owner);
Behdad Esfahbod965c2802012-08-29 13:59:16 -0400141 HB_INTERNAL void assert_var (unsigned int byte_i, unsigned int count, const char *owner);
Behdad Esfahboda9ad3d32011-07-28 15:42:18 -0400142 HB_INTERNAL void deallocate_var_all (void);
Behdad Esfahbodf4a579b2011-07-25 16:20:16 -0400143
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400144 HB_INTERNAL void add (hb_codepoint_t codepoint,
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400145 unsigned int cluster);
Behdad Esfahbod847794e2013-02-27 17:59:28 -0500146 HB_INTERNAL void add_info (const hb_glyph_info_t &glyph_info);
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400147
148 HB_INTERNAL void reverse_range (unsigned int start, unsigned int end);
149 HB_INTERNAL void reverse (void);
150 HB_INTERNAL void reverse_clusters (void);
Behdad Esfahbod3f82f8f2012-11-15 18:45:31 -0800151 HB_INTERNAL void guess_segment_properties (void);
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400152
153 HB_INTERNAL void swap_buffers (void);
Behdad Esfahbod0bc7a382012-10-29 22:02:45 -0700154 HB_INTERNAL void remove_output (void);
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400155 HB_INTERNAL void clear_output (void);
156 HB_INTERNAL void clear_positions (void);
Behdad Esfahbod69cc4922012-07-31 14:51:36 -0400157
Behdad Esfahbod9ebe8c02011-08-26 09:29:42 +0200158 HB_INTERNAL void replace_glyphs (unsigned int num_in,
159 unsigned int num_out,
Behdad Esfahbod8e3715f2012-04-23 22:18:54 -0400160 const hb_codepoint_t *glyph_data);
Behdad Esfahbod69cc4922012-07-31 14:51:36 -0400161
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400162 HB_INTERNAL void replace_glyph (hb_codepoint_t glyph_index);
163 /* Makes a copy of the glyph at idx to output and replace glyph_index */
164 HB_INTERNAL void output_glyph (hb_codepoint_t glyph_index);
Behdad Esfahbod847794e2013-02-27 17:59:28 -0500165 HB_INTERNAL void output_info (const hb_glyph_info_t &glyph_info);
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400166 /* Copies glyph at idx to output but doesn't advance idx */
167 HB_INTERNAL void copy_glyph (void);
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200168 HB_INTERNAL bool move_to (unsigned int i); /* i is output-buffer index. */
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400169 /* Copies glyph at idx to output and advance idx.
170 * If there's no output, just advance idx. */
Behdad Esfahbod1be368e2012-08-31 16:29:17 -0400171 inline void
172 next_glyph (void)
173 {
174 if (have_output)
175 {
176 if (unlikely (out_info != info || out_len != idx)) {
Behdad Esfahbod13188cb2015-11-19 11:59:03 -0800177 if (unlikely (!make_room_for (1, 1))) return;
Behdad Esfahbod1be368e2012-08-31 16:29:17 -0400178 out_info[out_len] = info[idx];
179 }
180 out_len++;
181 }
Behdad Esfahbod18e1c6b2015-11-19 11:50:58 -0800182
Behdad Esfahbod1be368e2012-08-31 16:29:17 -0400183 idx++;
184 }
185
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400186 /* Advance idx without copying to output. */
187 inline void skip_glyph (void) { idx++; }
Behdad Esfahbod1ce7b872010-05-21 17:31:45 +0100188
Behdad Esfahbodbd7378b2010-10-13 18:33:16 -0400189 inline void reset_masks (hb_mask_t mask)
190 {
Behdad Esfahbod31f18ab2011-06-15 09:49:58 -0400191 for (unsigned int j = 0; j < len; j++)
192 info[j].mask = mask;
Behdad Esfahbodbd7378b2010-10-13 18:33:16 -0400193 }
194 inline void add_masks (hb_mask_t mask)
195 {
Behdad Esfahbod31f18ab2011-06-15 09:49:58 -0400196 for (unsigned int j = 0; j < len; j++)
197 info[j].mask |= mask;
Behdad Esfahbodbd7378b2010-10-13 18:33:16 -0400198 }
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400199 HB_INTERNAL void set_masks (hb_mask_t value,
200 hb_mask_t mask,
201 unsigned int cluster_start,
202 unsigned int cluster_end);
203
Behdad Esfahbodd4cc4472012-04-07 21:52:28 -0400204 HB_INTERNAL void merge_clusters (unsigned int start,
Behdad Esfahbod701112d2015-07-22 15:42:20 +0100205 unsigned int end)
206 {
207 if (end - start < 2)
208 return;
209 merge_clusters_impl (start, end);
210 }
211 HB_INTERNAL void merge_clusters_impl (unsigned int start,
212 unsigned int end);
Behdad Esfahbodd4cc4472012-04-07 21:52:28 -0400213 HB_INTERNAL void merge_out_clusters (unsigned int start,
214 unsigned int end);
Behdad Esfahbod5f13bbd2015-06-19 13:31:49 -0700215 /* Merge clusters for deleting current glyph, and skip it. */
216 HB_INTERNAL void delete_glyph (void);
Behdad Esfahbodd4cc4472012-04-07 21:52:28 -0400217
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400218 /* Internal methods */
219 HB_INTERNAL bool enlarge (unsigned int size);
220
221 inline bool ensure (unsigned int size)
Behdad Esfahbod02c6c8c2013-11-15 13:05:38 -0500222 { return likely (!size || size < allocated) ? true : enlarge (size); }
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400223
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400224 inline bool ensure_inplace (unsigned int size)
225 { return likely (!size || size < allocated); }
226
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400227 HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out);
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200228 HB_INTERNAL bool shift_forward (unsigned int count);
Behdad Esfahbode62df432011-08-03 17:38:54 -0400229
Behdad Esfahbod061cb462013-11-13 14:50:25 -0500230 typedef long scratch_buffer_t;
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500231 HB_INTERNAL scratch_buffer_t *get_scratch_buffer (unsigned int *size);
Behdad Esfahbod05207a72012-09-25 17:44:53 -0400232
233 inline void clear_context (unsigned int side) { context_len[side] = 0; }
Behdad Esfahbode995d332015-09-01 16:13:32 +0100234
235 HB_INTERNAL void sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *));
Behdad Esfahbodd0316a82010-05-12 23:34:52 -0400236};
237
238
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400239#define HB_BUFFER_XALLOCATE_VAR(b, func, var, owner) \
240 b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
241 sizeof (b->info[0].var), owner)
242#define HB_BUFFER_ALLOCATE_VAR(b, var) \
243 HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var (), #var)
244#define HB_BUFFER_DEALLOCATE_VAR(b, var) \
245 HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var (), #var)
Behdad Esfahbod965c2802012-08-29 13:59:16 -0400246#define HB_BUFFER_ASSERT_VAR(b, var) \
247 HB_BUFFER_XALLOCATE_VAR (b, assert_var, var (), #var)
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400248
249
Behdad Esfahbodda8edbb2010-06-09 07:15:39 -0400250#endif /* HB_BUFFER_PRIVATE_HH */