Behdad Esfahbod | a2a9a02 | 2008-01-15 22:46:32 +0000 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 1998-2004 David Turner and Werner Lemberg |
| 3 | * Copyright © 2004,2007,2009,2010 Red Hat, Inc. |
Behdad Esfahbod | 05207a7 | 2012-09-25 17:44:53 -0400 | [diff] [blame] | 4 | * Copyright © 2011,2012 Google, Inc. |
Behdad Esfahbod | 6b34713 | 2007-10-11 08:30:50 +0000 | [diff] [blame] | 5 | * |
Behdad Esfahbod | 8f0d7e0 | 2011-04-15 18:59:56 -0400 | [diff] [blame] | 6 | * This is part of HarfBuzz, a text shaping library. |
Behdad Esfahbod | 6b34713 | 2007-10-11 08:30:50 +0000 | [diff] [blame] | 7 | * |
Behdad Esfahbod | a2a9a02 | 2008-01-15 22:46:32 +0000 | [diff] [blame] | 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. |
Behdad Esfahbod | 6b34713 | 2007-10-11 08:30:50 +0000 | [diff] [blame] | 13 | * |
Behdad Esfahbod | a2a9a02 | 2008-01-15 22:46:32 +0000 | [diff] [blame] | 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 |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 27 | * Google Author(s): Behdad Esfahbod |
Behdad Esfahbod | 6b34713 | 2007-10-11 08:30:50 +0000 | [diff] [blame] | 28 | */ |
Behdad Esfahbod | a2a9a02 | 2008-01-15 22:46:32 +0000 | [diff] [blame] | 29 | |
Behdad Esfahbod | da8edbb | 2010-06-09 07:15:39 -0400 | [diff] [blame] | 30 | #ifndef HB_BUFFER_PRIVATE_HH |
| 31 | #define HB_BUFFER_PRIVATE_HH |
Behdad Esfahbod | 6b34713 | 2007-10-11 08:30:50 +0000 | [diff] [blame] | 32 | |
Behdad Esfahbod | c57d454 | 2011-04-20 18:50:27 -0400 | [diff] [blame] | 33 | #include "hb-private.hh" |
Behdad Esfahbod | 5c0adce | 2009-05-20 05:42:12 -0400 | [diff] [blame] | 34 | #include "hb-buffer.h" |
Behdad Esfahbod | fca368c | 2011-04-21 18:24:02 -0400 | [diff] [blame] | 35 | #include "hb-object-private.hh" |
Behdad Esfahbod | fb194b8 | 2011-04-20 02:00:47 -0400 | [diff] [blame] | 36 | #include "hb-unicode-private.hh" |
Behdad Esfahbod | 6b34713 | 2007-10-11 08:30:50 +0000 | [diff] [blame] | 37 | |
Behdad Esfahbod | 6b34713 | 2007-10-11 08:30:50 +0000 | [diff] [blame] | 38 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 39 | |
Behdad Esfahbod | 88474c6 | 2010-10-27 14:42:15 -0400 | [diff] [blame] | 40 | ASSERT_STATIC (sizeof (hb_glyph_info_t) == 20); |
Behdad Esfahbod | f1322e5 | 2009-08-01 22:53:04 -0400 | [diff] [blame] | 41 | ASSERT_STATIC (sizeof (hb_glyph_info_t) == sizeof (hb_glyph_position_t)); |
| 42 | |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * hb_segment_properties_t |
| 46 | */ |
| 47 | |
Behdad Esfahbod | 1bc1cb3 | 2012-06-16 15:21:55 -0400 | [diff] [blame] | 48 | typedef struct hb_segment_properties_t { |
Behdad Esfahbod | a7c5046 | 2010-10-08 18:47:47 -0400 | [diff] [blame] | 49 | hb_direction_t direction; |
| 50 | hb_script_t script; |
| 51 | hb_language_t language; |
Behdad Esfahbod | 6220e5f | 2012-06-06 03:30:09 -0400 | [diff] [blame] | 52 | ASSERT_POD (); |
Behdad Esfahbod | a7c5046 | 2010-10-08 18:47:47 -0400 | [diff] [blame] | 53 | } hb_segment_properties_t; |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 54 | |
Behdad Esfahbod | ea278d3 | 2012-07-27 02:12:28 -0400 | [diff] [blame] | 55 | #define _HB_BUFFER_PROPS_DEFAULT { HB_DIRECTION_INVALID, HB_SCRIPT_INVALID, HB_LANGUAGE_INVALID } |
Behdad Esfahbod | a7c5046 | 2010-10-08 18:47:47 -0400 | [diff] [blame] | 56 | |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 57 | static inline hb_bool_t |
| 58 | hb_segment_properties_equal (const hb_segment_properties_t *a, |
| 59 | const hb_segment_properties_t *b) |
| 60 | { |
| 61 | return a->direction == b->direction && |
| 62 | a->script == b->script && |
| 63 | a->language == b->language; |
| 64 | } |
| 65 | |
| 66 | |
Behdad Esfahbod | 801298b | 2012-08-08 14:26:36 -0400 | [diff] [blame] | 67 | #if 0 |
| 68 | static inline unsigned int |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 69 | hb_segment_properties_hash (const hb_segment_properties_t *p) |
| 70 | { |
| 71 | /* TODO improve */ |
Behdad Esfahbod | 801298b | 2012-08-08 14:26:36 -0400 | [diff] [blame] | 72 | return (unsigned int) p->direction + |
| 73 | (unsigned int) p->script + |
| 74 | (intptr_t) (p->language); |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 75 | } |
Behdad Esfahbod | 801298b | 2012-08-08 14:26:36 -0400 | [diff] [blame] | 76 | #endif |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 77 | |
| 78 | |
| 79 | |
| 80 | /* |
| 81 | * hb_buffer_t |
| 82 | */ |
Behdad Esfahbod | f1322e5 | 2009-08-01 22:53:04 -0400 | [diff] [blame] | 83 | |
Behdad Esfahbod | 1bc1cb3 | 2012-06-16 15:21:55 -0400 | [diff] [blame] | 84 | struct hb_buffer_t { |
Behdad Esfahbod | fca368c | 2011-04-21 18:24:02 -0400 | [diff] [blame] | 85 | hb_object_header_t header; |
Behdad Esfahbod | 6220e5f | 2012-06-06 03:30:09 -0400 | [diff] [blame] | 86 | ASSERT_POD (); |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 87 | |
| 88 | /* Information about how the text in the buffer should be treated */ |
Behdad Esfahbod | 4e4ef24 | 2010-07-23 17:22:11 -0400 | [diff] [blame] | 89 | |
Behdad Esfahbod | a7c5046 | 2010-10-08 18:47:47 -0400 | [diff] [blame] | 90 | hb_unicode_funcs_t *unicode; /* Unicode functions */ |
| 91 | hb_segment_properties_t props; /* Script, language, direction */ |
Behdad Esfahbod | 0c7df22 | 2012-11-13 14:42:35 -0800 | [diff] [blame^] | 92 | hb_buffer_flags_t flags; /* BOT / EOT / etc. */ |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 93 | |
| 94 | /* Buffer contents */ |
| 95 | |
Behdad Esfahbod | 96fdc04 | 2012-09-06 22:26:16 -0400 | [diff] [blame] | 96 | hb_buffer_content_type_t content_type; |
| 97 | |
Behdad Esfahbod | e0db4b8 | 2011-04-28 12:56:49 -0400 | [diff] [blame] | 98 | bool in_error; /* Allocation failed */ |
Behdad Esfahbod | fca368c | 2011-04-21 18:24:02 -0400 | [diff] [blame] | 99 | bool have_output; /* Whether we have an output buffer going on */ |
| 100 | bool have_positions; /* Whether we have positions */ |
Behdad Esfahbod | 910a33f | 2010-05-14 22:13:38 -0400 | [diff] [blame] | 101 | |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 102 | unsigned int idx; /* Cursor into ->info and ->pos arrays */ |
Behdad Esfahbod | 910a33f | 2010-05-14 22:13:38 -0400 | [diff] [blame] | 103 | unsigned int len; /* Length of ->info and ->pos arrays */ |
Behdad Esfahbod | cc1a8a9 | 2011-01-06 14:58:52 -0500 | [diff] [blame] | 104 | unsigned int out_len; /* Length of ->out array if have_output */ |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 105 | |
Behdad Esfahbod | c0af193 | 2011-04-15 19:26:24 -0400 | [diff] [blame] | 106 | unsigned int allocated; /* Length of allocated arrays */ |
Behdad Esfahbod | 88474c6 | 2010-10-27 14:42:15 -0400 | [diff] [blame] | 107 | hb_glyph_info_t *info; |
| 108 | hb_glyph_info_t *out_info; |
| 109 | hb_glyph_position_t *pos; |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 110 | |
Behdad Esfahbod | 99c2695 | 2012-05-13 15:45:18 +0200 | [diff] [blame] | 111 | inline hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; } |
| 112 | inline hb_glyph_info_t cur (unsigned int i = 0) const { return info[idx + i]; } |
| 113 | |
| 114 | inline hb_glyph_position_t &cur_pos (unsigned int i = 0) { return pos[idx + i]; } |
| 115 | inline hb_glyph_position_t cur_pos (unsigned int i = 0) const { return pos[idx + i]; } |
| 116 | |
| 117 | inline hb_glyph_info_t &prev (void) { return out_info[out_len - 1]; } |
| 118 | inline hb_glyph_info_t prev (void) const { return info[out_len - 1]; } |
| 119 | |
Behdad Esfahbod | f4a579b | 2011-07-25 16:20:16 -0400 | [diff] [blame] | 120 | unsigned int serial; |
Behdad Esfahbod | 05207a7 | 2012-09-25 17:44:53 -0400 | [diff] [blame] | 121 | |
| 122 | /* These reflect current allocations of the bytes in glyph_info_t's var1 and var2. */ |
Behdad Esfahbod | f4a579b | 2011-07-25 16:20:16 -0400 | [diff] [blame] | 123 | uint8_t allocated_var_bytes[8]; |
| 124 | const char *allocated_var_owner[8]; |
| 125 | |
Behdad Esfahbod | 05207a7 | 2012-09-25 17:44:53 -0400 | [diff] [blame] | 126 | /* Text before / after the main buffer contents. |
| 127 | * Always in Unicode, and ordered outward. |
| 128 | * Index 0 is for "pre-context", 1 for "post-context". */ |
| 129 | static const unsigned int CONTEXT_LENGTH = 5; |
| 130 | hb_codepoint_t context[2][CONTEXT_LENGTH]; |
| 131 | unsigned int context_len[2]; |
| 132 | |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 133 | |
| 134 | /* Methods */ |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 135 | |
| 136 | HB_INTERNAL void reset (void); |
Behdad Esfahbod | 82ecaff | 2012-11-13 13:57:52 -0800 | [diff] [blame] | 137 | HB_INTERNAL void clear (void); |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 138 | |
Behdad Esfahbod | cc1a8a9 | 2011-01-06 14:58:52 -0500 | [diff] [blame] | 139 | inline unsigned int backtrack_len (void) const |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 140 | { return have_output? out_len : idx; } |
Behdad Esfahbod | dbf56b1 | 2010-10-27 17:06:12 -0400 | [diff] [blame] | 141 | inline unsigned int next_serial (void) { return serial++; } |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 142 | |
Behdad Esfahbod | f4a579b | 2011-07-25 16:20:16 -0400 | [diff] [blame] | 143 | HB_INTERNAL void allocate_var (unsigned int byte_i, unsigned int count, const char *owner); |
| 144 | HB_INTERNAL void deallocate_var (unsigned int byte_i, unsigned int count, const char *owner); |
Behdad Esfahbod | 965c280 | 2012-08-29 13:59:16 -0400 | [diff] [blame] | 145 | HB_INTERNAL void assert_var (unsigned int byte_i, unsigned int count, const char *owner); |
Behdad Esfahbod | a9ad3d3 | 2011-07-28 15:42:18 -0400 | [diff] [blame] | 146 | HB_INTERNAL void deallocate_var_all (void); |
Behdad Esfahbod | f4a579b | 2011-07-25 16:20:16 -0400 | [diff] [blame] | 147 | |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 148 | HB_INTERNAL void add (hb_codepoint_t codepoint, |
| 149 | hb_mask_t mask, |
| 150 | unsigned int cluster); |
| 151 | |
| 152 | HB_INTERNAL void reverse_range (unsigned int start, unsigned int end); |
| 153 | HB_INTERNAL void reverse (void); |
| 154 | HB_INTERNAL void reverse_clusters (void); |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 155 | HB_INTERNAL void guess_properties (void); |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 156 | |
| 157 | HB_INTERNAL void swap_buffers (void); |
Behdad Esfahbod | 0bc7a38 | 2012-10-29 22:02:45 -0700 | [diff] [blame] | 158 | HB_INTERNAL void remove_output (void); |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 159 | HB_INTERNAL void clear_output (void); |
| 160 | HB_INTERNAL void clear_positions (void); |
Behdad Esfahbod | 69cc492 | 2012-07-31 14:51:36 -0400 | [diff] [blame] | 161 | |
Behdad Esfahbod | 9ebe8c0 | 2011-08-26 09:29:42 +0200 | [diff] [blame] | 162 | HB_INTERNAL void replace_glyphs (unsigned int num_in, |
| 163 | unsigned int num_out, |
Behdad Esfahbod | 8e3715f | 2012-04-23 22:18:54 -0400 | [diff] [blame] | 164 | const hb_codepoint_t *glyph_data); |
Behdad Esfahbod | 69cc492 | 2012-07-31 14:51:36 -0400 | [diff] [blame] | 165 | |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 166 | HB_INTERNAL void replace_glyph (hb_codepoint_t glyph_index); |
| 167 | /* Makes a copy of the glyph at idx to output and replace glyph_index */ |
| 168 | HB_INTERNAL void output_glyph (hb_codepoint_t glyph_index); |
Behdad Esfahbod | b85800f | 2012-08-31 18:12:01 -0400 | [diff] [blame] | 169 | HB_INTERNAL void output_info (hb_glyph_info_t &glyph_info); |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 170 | /* Copies glyph at idx to output but doesn't advance idx */ |
| 171 | HB_INTERNAL void copy_glyph (void); |
| 172 | /* Copies glyph at idx to output and advance idx. |
| 173 | * If there's no output, just advance idx. */ |
Behdad Esfahbod | 1be368e | 2012-08-31 16:29:17 -0400 | [diff] [blame] | 174 | inline void |
| 175 | next_glyph (void) |
| 176 | { |
| 177 | if (have_output) |
| 178 | { |
| 179 | if (unlikely (out_info != info || out_len != idx)) { |
| 180 | if (unlikely (!make_room_for (1, 1))) return; |
| 181 | out_info[out_len] = info[idx]; |
| 182 | } |
| 183 | out_len++; |
| 184 | } |
| 185 | |
| 186 | idx++; |
| 187 | } |
| 188 | |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 189 | /* Advance idx without copying to output. */ |
| 190 | inline void skip_glyph (void) { idx++; } |
Behdad Esfahbod | 1ce7b87 | 2010-05-21 17:31:45 +0100 | [diff] [blame] | 191 | |
Behdad Esfahbod | bd7378b | 2010-10-13 18:33:16 -0400 | [diff] [blame] | 192 | inline void reset_masks (hb_mask_t mask) |
| 193 | { |
Behdad Esfahbod | 31f18ab | 2011-06-15 09:49:58 -0400 | [diff] [blame] | 194 | for (unsigned int j = 0; j < len; j++) |
| 195 | info[j].mask = mask; |
Behdad Esfahbod | bd7378b | 2010-10-13 18:33:16 -0400 | [diff] [blame] | 196 | } |
| 197 | inline void add_masks (hb_mask_t mask) |
| 198 | { |
Behdad Esfahbod | 31f18ab | 2011-06-15 09:49:58 -0400 | [diff] [blame] | 199 | for (unsigned int j = 0; j < len; j++) |
| 200 | info[j].mask |= mask; |
Behdad Esfahbod | bd7378b | 2010-10-13 18:33:16 -0400 | [diff] [blame] | 201 | } |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 202 | HB_INTERNAL void set_masks (hb_mask_t value, |
| 203 | hb_mask_t mask, |
| 204 | unsigned int cluster_start, |
| 205 | unsigned int cluster_end); |
| 206 | |
Behdad Esfahbod | d4cc447 | 2012-04-07 21:52:28 -0400 | [diff] [blame] | 207 | HB_INTERNAL void merge_clusters (unsigned int start, |
| 208 | unsigned int end); |
| 209 | HB_INTERNAL void merge_out_clusters (unsigned int start, |
| 210 | unsigned int end); |
| 211 | |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 212 | /* Internal methods */ |
| 213 | HB_INTERNAL bool enlarge (unsigned int size); |
| 214 | |
| 215 | inline bool ensure (unsigned int size) |
Behdad Esfahbod | d16ccc4 | 2012-07-18 15:43:55 -0400 | [diff] [blame] | 216 | { return likely (size < allocated) ? true : enlarge (size); } |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 217 | |
| 218 | HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out); |
Behdad Esfahbod | e62df43 | 2011-08-03 17:38:54 -0400 | [diff] [blame] | 219 | |
| 220 | HB_INTERNAL void *get_scratch_buffer (unsigned int *size); |
Behdad Esfahbod | 05207a7 | 2012-09-25 17:44:53 -0400 | [diff] [blame] | 221 | |
| 222 | inline void clear_context (unsigned int side) { context_len[side] = 0; } |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | |
Behdad Esfahbod | b65c060 | 2011-07-28 16:48:43 -0400 | [diff] [blame] | 226 | #define HB_BUFFER_XALLOCATE_VAR(b, func, var, owner) \ |
| 227 | b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \ |
| 228 | sizeof (b->info[0].var), owner) |
| 229 | #define HB_BUFFER_ALLOCATE_VAR(b, var) \ |
| 230 | HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var (), #var) |
| 231 | #define HB_BUFFER_DEALLOCATE_VAR(b, var) \ |
| 232 | HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var (), #var) |
Behdad Esfahbod | 965c280 | 2012-08-29 13:59:16 -0400 | [diff] [blame] | 233 | #define HB_BUFFER_ASSERT_VAR(b, var) \ |
| 234 | HB_BUFFER_XALLOCATE_VAR (b, assert_var, var (), #var) |
Behdad Esfahbod | b65c060 | 2011-07-28 16:48:43 -0400 | [diff] [blame] | 235 | |
| 236 | |
Behdad Esfahbod | da8edbb | 2010-06-09 07:15:39 -0400 | [diff] [blame] | 237 | #endif /* HB_BUFFER_PRIVATE_HH */ |