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 | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 30 | #ifndef HB_BUFFER_HH |
| 31 | #define HB_BUFFER_HH |
Behdad Esfahbod | 6b34713 | 2007-10-11 08:30:50 +0000 | [diff] [blame] | 32 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 33 | #include "hb.hh" |
| 34 | #include "hb-unicode.hh" |
Behdad Esfahbod | 6b34713 | 2007-10-11 08:30:50 +0000 | [diff] [blame] | 35 | |
Behdad Esfahbod | 6b34713 | 2007-10-11 08:30:50 +0000 | [diff] [blame] | 36 | |
Behdad Esfahbod | baf7779 | 2017-11-14 21:53:48 -0800 | [diff] [blame] | 37 | #ifndef HB_BUFFER_MAX_LEN_FACTOR |
| 38 | #define HB_BUFFER_MAX_LEN_FACTOR 32 |
Behdad Esfahbod | 4301703 | 2015-11-05 23:44:59 -0800 | [diff] [blame] | 39 | #endif |
| 40 | #ifndef HB_BUFFER_MAX_LEN_MIN |
| 41 | #define HB_BUFFER_MAX_LEN_MIN 8192 |
| 42 | #endif |
Behdad Esfahbod | f1c4adb | 2015-11-06 09:46:05 -0800 | [diff] [blame] | 43 | #ifndef HB_BUFFER_MAX_LEN_DEFAULT |
Behdad Esfahbod | 4301703 | 2015-11-05 23:44:59 -0800 | [diff] [blame] | 44 | #define HB_BUFFER_MAX_LEN_DEFAULT 0x3FFFFFFF /* Shaping more than a billion chars? Let us know! */ |
| 45 | #endif |
| 46 | |
Behdad Esfahbod | baf7779 | 2017-11-14 21:53:48 -0800 | [diff] [blame] | 47 | #ifndef HB_BUFFER_MAX_OPS_FACTOR |
| 48 | #define HB_BUFFER_MAX_OPS_FACTOR 64 |
| 49 | #endif |
| 50 | #ifndef HB_BUFFER_MAX_OPS_MIN |
| 51 | #define HB_BUFFER_MAX_OPS_MIN 1024 |
| 52 | #endif |
| 53 | #ifndef HB_BUFFER_MAX_OPS_DEFAULT |
| 54 | #define HB_BUFFER_MAX_OPS_DEFAULT 0x1FFFFFFF /* Shaping more than a billion operations? Let us know! */ |
| 55 | #endif |
| 56 | |
Behdad Esfahbod | 606bf57 | 2018-09-16 19:33:48 +0200 | [diff] [blame] | 57 | static_assert ((sizeof (hb_glyph_info_t) == 20), ""); |
| 58 | static_assert ((sizeof (hb_glyph_info_t) == sizeof (hb_glyph_position_t)), ""); |
Behdad Esfahbod | f1322e5 | 2009-08-01 22:53:04 -0400 | [diff] [blame] | 59 | |
Chun-wei Fan | 167c327 | 2015-11-09 17:17:56 +0800 | [diff] [blame] | 60 | HB_MARK_AS_FLAG_T (hb_buffer_flags_t); |
| 61 | HB_MARK_AS_FLAG_T (hb_buffer_serialize_flags_t); |
Jonathan Kew | 331d66c | 2014-07-19 23:09:09 +0100 | [diff] [blame] | 62 | HB_MARK_AS_FLAG_T (hb_buffer_diff_flags_t); |
Behdad Esfahbod | aa7044d | 2015-11-04 16:25:57 -0800 | [diff] [blame] | 63 | |
Behdad Esfahbod | 49ef630 | 2015-11-04 17:27:07 -0800 | [diff] [blame] | 64 | enum hb_buffer_scratch_flags_t { |
| 65 | HB_BUFFER_SCRATCH_FLAG_DEFAULT = 0x00000000u, |
Behdad Esfahbod | e3e4bb0 | 2015-11-04 18:58:02 -0800 | [diff] [blame] | 66 | HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII = 0x00000001u, |
| 67 | HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES = 0x00000002u, |
| 68 | HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK = 0x00000004u, |
Behdad Esfahbod | cbc3a76 | 2016-02-11 16:48:13 +0700 | [diff] [blame] | 69 | HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT = 0x00000008u, |
Behdad Esfahbod | 1c17c2b | 2017-08-11 19:06:07 -0700 | [diff] [blame] | 70 | HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK = 0x00000010u, |
Behdad Esfahbod | c36f3f5 | 2018-10-02 14:34:29 +0200 | [diff] [blame] | 71 | HB_BUFFER_SCRATCH_FLAG_HAS_CGJ = 0x00000020u, |
Behdad Esfahbod | 1c17c2b | 2017-08-11 19:06:07 -0700 | [diff] [blame] | 72 | |
Behdad Esfahbod | 73fec39 | 2015-11-05 15:22:18 -0800 | [diff] [blame] | 73 | /* Reserved for complex shapers' internal use. */ |
| 74 | HB_BUFFER_SCRATCH_FLAG_COMPLEX0 = 0x01000000u, |
| 75 | HB_BUFFER_SCRATCH_FLAG_COMPLEX1 = 0x02000000u, |
| 76 | HB_BUFFER_SCRATCH_FLAG_COMPLEX2 = 0x04000000u, |
| 77 | HB_BUFFER_SCRATCH_FLAG_COMPLEX3 = 0x08000000u, |
Behdad Esfahbod | 49ef630 | 2015-11-04 17:27:07 -0800 | [diff] [blame] | 78 | }; |
Chun-wei Fan | 167c327 | 2015-11-09 17:17:56 +0800 | [diff] [blame] | 79 | HB_MARK_AS_FLAG_T (hb_buffer_scratch_flags_t); |
Behdad Esfahbod | 49ef630 | 2015-11-04 17:27:07 -0800 | [diff] [blame] | 80 | |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 81 | |
| 82 | /* |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 83 | * hb_buffer_t |
| 84 | */ |
Behdad Esfahbod | f1322e5 | 2009-08-01 22:53:04 -0400 | [diff] [blame] | 85 | |
Behdad Esfahbod | 3506672 | 2018-08-06 06:17:48 -0700 | [diff] [blame] | 86 | struct hb_buffer_t |
| 87 | { |
Behdad Esfahbod | fca368c | 2011-04-21 18:24:02 -0400 | [diff] [blame] | 88 | hb_object_header_t header; |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 89 | |
| 90 | /* Information about how the text in the buffer should be treated */ |
Behdad Esfahbod | a7c5046 | 2010-10-08 18:47:47 -0400 | [diff] [blame] | 91 | hb_unicode_funcs_t *unicode; /* Unicode functions */ |
Behdad Esfahbod | 0c7df22 | 2012-11-13 14:42:35 -0800 | [diff] [blame] | 92 | hb_buffer_flags_t flags; /* BOT / EOT / etc. */ |
Behdad Esfahbod | 376d587 | 2015-07-22 16:51:12 +0100 | [diff] [blame] | 93 | hb_buffer_cluster_level_t cluster_level; |
Behdad Esfahbod | 976c8f4 | 2014-07-16 15:34:20 -0400 | [diff] [blame] | 94 | hb_codepoint_t replacement; /* U+FFFD or something else. */ |
Behdad Esfahbod | 71b65eb | 2018-10-07 18:41:52 +0200 | [diff] [blame] | 95 | hb_codepoint_t invisible; /* 0 or something else. */ |
Bruce Mitchener | 90218fa | 2018-01-31 20:44:45 +0700 | [diff] [blame] | 96 | hb_buffer_scratch_flags_t scratch_flags; /* Have space-fallback, etc. */ |
Behdad Esfahbod | 4301703 | 2015-11-05 23:44:59 -0800 | [diff] [blame] | 97 | unsigned int max_len; /* Maximum allowed len. */ |
Behdad Esfahbod | baf7779 | 2017-11-14 21:53:48 -0800 | [diff] [blame] | 98 | int max_ops; /* Maximum allowed operations. */ |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 99 | |
| 100 | /* Buffer contents */ |
Behdad Esfahbod | 96fdc04 | 2012-09-06 22:26:16 -0400 | [diff] [blame] | 101 | hb_buffer_content_type_t content_type; |
Behdad Esfahbod | b5fbc3b | 2014-08-11 18:40:01 -0400 | [diff] [blame] | 102 | hb_segment_properties_t props; /* Script, language, direction */ |
Behdad Esfahbod | 96fdc04 | 2012-09-06 22:26:16 -0400 | [diff] [blame] | 103 | |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 104 | bool successful; /* Allocations successful */ |
Behdad Esfahbod | fca368c | 2011-04-21 18:24:02 -0400 | [diff] [blame] | 105 | bool have_output; /* Whether we have an output buffer going on */ |
| 106 | bool have_positions; /* Whether we have positions */ |
Behdad Esfahbod | 910a33f | 2010-05-14 22:13:38 -0400 | [diff] [blame] | 107 | |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 108 | unsigned int idx; /* Cursor into ->info and ->pos arrays */ |
Behdad Esfahbod | 910a33f | 2010-05-14 22:13:38 -0400 | [diff] [blame] | 109 | unsigned int len; /* Length of ->info and ->pos arrays */ |
Behdad Esfahbod | cc1a8a9 | 2011-01-06 14:58:52 -0500 | [diff] [blame] | 110 | unsigned int out_len; /* Length of ->out array if have_output */ |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 111 | |
Behdad Esfahbod | c0af193 | 2011-04-15 19:26:24 -0400 | [diff] [blame] | 112 | unsigned int allocated; /* Length of allocated arrays */ |
Behdad Esfahbod | 88474c6 | 2010-10-27 14:42:15 -0400 | [diff] [blame] | 113 | hb_glyph_info_t *info; |
| 114 | hb_glyph_info_t *out_info; |
| 115 | hb_glyph_position_t *pos; |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 116 | |
Behdad Esfahbod | f4a579b | 2011-07-25 16:20:16 -0400 | [diff] [blame] | 117 | unsigned int serial; |
Behdad Esfahbod | 05207a7 | 2012-09-25 17:44:53 -0400 | [diff] [blame] | 118 | |
Behdad Esfahbod | 05207a7 | 2012-09-25 17:44:53 -0400 | [diff] [blame] | 119 | /* Text before / after the main buffer contents. |
| 120 | * Always in Unicode, and ordered outward. |
| 121 | * Index 0 is for "pre-context", 1 for "post-context". */ |
Behdad Esfahbod | 5d4b037 | 2019-01-22 12:11:24 +0100 | [diff] [blame] | 122 | static constexpr unsigned CONTEXT_LENGTH = 5u; |
Behdad Esfahbod | 05207a7 | 2012-09-25 17:44:53 -0400 | [diff] [blame] | 123 | hb_codepoint_t context[2][CONTEXT_LENGTH]; |
| 124 | unsigned int context_len[2]; |
| 125 | |
Behdad Esfahbod | 9881650 | 2016-02-25 12:23:02 +0900 | [diff] [blame] | 126 | /* Debugging API */ |
Behdad Esfahbod | eb9798e | 2019-06-18 13:29:55 -0700 | [diff] [blame] | 127 | #ifndef HB_NO_BUFFER_MESSAGE |
Behdad Esfahbod | 0475ef2 | 2015-12-18 18:17:07 +0000 | [diff] [blame] | 128 | hb_buffer_message_func_t message_func; |
Behdad Esfahbod | eb9798e | 2019-06-18 13:29:55 -0700 | [diff] [blame] | 129 | #endif |
Behdad Esfahbod | 0475ef2 | 2015-12-18 18:17:07 +0000 | [diff] [blame] | 130 | void *message_data; |
| 131 | hb_destroy_func_t message_destroy; |
| 132 | |
Behdad Esfahbod | 9881650 | 2016-02-25 12:23:02 +0900 | [diff] [blame] | 133 | /* Internal debugging. */ |
Behdad Esfahbod | 0c7fb74 | 2016-02-25 14:40:09 +0900 | [diff] [blame] | 134 | /* The bits here reflect current allocations of the bytes in glyph_info_t's var1 and var2. */ |
| 135 | #ifndef HB_NDEBUG |
| 136 | uint8_t allocated_var_bits; |
Behdad Esfahbod | 9881650 | 2016-02-25 12:23:02 +0900 | [diff] [blame] | 137 | #endif |
Behdad Esfahbod | 173dab6 | 2017-11-14 21:27:24 -0800 | [diff] [blame] | 138 | |
| 139 | |
| 140 | /* Methods */ |
| 141 | |
Behdad Esfahbod | 9aebfb4 | 2018-12-18 13:22:17 -0500 | [diff] [blame] | 142 | bool in_error () const { return !successful; } |
| 143 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 144 | void allocate_var (unsigned int start, unsigned int count) |
Behdad Esfahbod | 0c7fb74 | 2016-02-25 14:40:09 +0900 | [diff] [blame] | 145 | { |
| 146 | #ifndef HB_NDEBUG |
| 147 | unsigned int end = start + count; |
| 148 | assert (end <= 8); |
Behdad Esfahbod | 3331731 | 2016-08-08 17:24:04 -0700 | [diff] [blame] | 149 | unsigned int bits = (1u<<end) - (1u<<start); |
Behdad Esfahbod | 0c7fb74 | 2016-02-25 14:40:09 +0900 | [diff] [blame] | 150 | assert (0 == (allocated_var_bits & bits)); |
| 151 | allocated_var_bits |= bits; |
| 152 | #endif |
| 153 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 154 | void deallocate_var (unsigned int start, unsigned int count) |
Behdad Esfahbod | 0c7fb74 | 2016-02-25 14:40:09 +0900 | [diff] [blame] | 155 | { |
| 156 | #ifndef HB_NDEBUG |
| 157 | unsigned int end = start + count; |
| 158 | assert (end <= 8); |
Behdad Esfahbod | 3331731 | 2016-08-08 17:24:04 -0700 | [diff] [blame] | 159 | unsigned int bits = (1u<<end) - (1u<<start); |
Behdad Esfahbod | 0c7fb74 | 2016-02-25 14:40:09 +0900 | [diff] [blame] | 160 | assert (bits == (allocated_var_bits & bits)); |
| 161 | allocated_var_bits &= ~bits; |
| 162 | #endif |
| 163 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 164 | void assert_var (unsigned int start, unsigned int count) |
Behdad Esfahbod | 0c7fb74 | 2016-02-25 14:40:09 +0900 | [diff] [blame] | 165 | { |
| 166 | #ifndef HB_NDEBUG |
| 167 | unsigned int end = start + count; |
| 168 | assert (end <= 8); |
Behdad Esfahbod | 3331731 | 2016-08-08 17:24:04 -0700 | [diff] [blame] | 169 | unsigned int bits = (1u<<end) - (1u<<start); |
Behdad Esfahbod | 0c7fb74 | 2016-02-25 14:40:09 +0900 | [diff] [blame] | 170 | assert (bits == (allocated_var_bits & bits)); |
| 171 | #endif |
| 172 | } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 173 | void deallocate_var_all () |
Behdad Esfahbod | 0c7fb74 | 2016-02-25 14:40:09 +0900 | [diff] [blame] | 174 | { |
| 175 | #ifndef HB_NDEBUG |
| 176 | allocated_var_bits = 0; |
| 177 | #endif |
| 178 | } |
Behdad Esfahbod | 9881650 | 2016-02-25 12:23:02 +0900 | [diff] [blame] | 179 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 180 | hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; } |
| 181 | hb_glyph_info_t cur (unsigned int i = 0) const { return info[idx + i]; } |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 182 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 183 | hb_glyph_position_t &cur_pos (unsigned int i = 0) { return pos[idx + i]; } |
| 184 | hb_glyph_position_t cur_pos (unsigned int i = 0) const { return pos[idx + i]; } |
Behdad Esfahbod | 173dab6 | 2017-11-14 21:27:24 -0800 | [diff] [blame] | 185 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 186 | hb_glyph_info_t &prev () { return out_info[out_len ? out_len - 1 : 0]; } |
| 187 | hb_glyph_info_t prev () const { return out_info[out_len ? out_len - 1 : 0]; } |
Behdad Esfahbod | 173dab6 | 2017-11-14 21:27:24 -0800 | [diff] [blame] | 188 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 189 | bool has_separate_output () const { return info != out_info; } |
Behdad Esfahbod | 173dab6 | 2017-11-14 21:27:24 -0800 | [diff] [blame] | 190 | |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 191 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 192 | HB_INTERNAL void reset (); |
| 193 | HB_INTERNAL void clear (); |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 194 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 195 | unsigned int backtrack_len () const { return have_output? out_len : idx; } |
| 196 | unsigned int lookahead_len () const { return len - idx; } |
| 197 | unsigned int next_serial () { return serial++; } |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 198 | |
| 199 | HB_INTERNAL void add (hb_codepoint_t codepoint, |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 200 | unsigned int cluster); |
Behdad Esfahbod | 847794e | 2013-02-27 17:59:28 -0500 | [diff] [blame] | 201 | HB_INTERNAL void add_info (const hb_glyph_info_t &glyph_info); |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 202 | |
| 203 | HB_INTERNAL void reverse_range (unsigned int start, unsigned int end); |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 204 | HB_INTERNAL void reverse (); |
| 205 | HB_INTERNAL void reverse_clusters (); |
| 206 | HB_INTERNAL void guess_segment_properties (); |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 207 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 208 | HB_INTERNAL void swap_buffers (); |
| 209 | HB_INTERNAL void remove_output (); |
| 210 | HB_INTERNAL void clear_output (); |
| 211 | HB_INTERNAL void clear_positions (); |
Behdad Esfahbod | 69cc492 | 2012-07-31 14:51:36 -0400 | [diff] [blame] | 212 | |
Behdad Esfahbod | 9ebe8c0 | 2011-08-26 09:29:42 +0200 | [diff] [blame] | 213 | HB_INTERNAL void replace_glyphs (unsigned int num_in, |
| 214 | unsigned int num_out, |
Behdad Esfahbod | 8e3715f | 2012-04-23 22:18:54 -0400 | [diff] [blame] | 215 | const hb_codepoint_t *glyph_data); |
Behdad Esfahbod | 69cc492 | 2012-07-31 14:51:36 -0400 | [diff] [blame] | 216 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 217 | void replace_glyph (hb_codepoint_t glyph_index) |
Behdad Esfahbod | 2a6f152 | 2018-10-03 20:09:14 +0200 | [diff] [blame] | 218 | { |
| 219 | if (unlikely (out_info != info || out_len != idx)) { |
| 220 | if (unlikely (!make_room_for (1, 1))) return; |
| 221 | out_info[out_len] = info[idx]; |
| 222 | } |
| 223 | out_info[out_len].codepoint = glyph_index; |
| 224 | |
| 225 | idx++; |
| 226 | out_len++; |
| 227 | } |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 228 | /* Makes a copy of the glyph at idx to output and replace glyph_index */ |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 229 | hb_glyph_info_t & output_glyph (hb_codepoint_t glyph_index) |
Behdad Esfahbod | 2a6f152 | 2018-10-03 20:09:14 +0200 | [diff] [blame] | 230 | { |
| 231 | if (unlikely (!make_room_for (0, 1))) return Crap(hb_glyph_info_t); |
| 232 | |
Behdad Esfahbod | 60c1397 | 2018-10-14 19:36:46 -0700 | [diff] [blame] | 233 | if (unlikely (idx == len && !out_len)) |
| 234 | return Crap(hb_glyph_info_t); |
| 235 | |
| 236 | out_info[out_len] = idx < len ? info[idx] : out_info[out_len - 1]; |
Behdad Esfahbod | 2a6f152 | 2018-10-03 20:09:14 +0200 | [diff] [blame] | 237 | out_info[out_len].codepoint = glyph_index; |
| 238 | |
| 239 | out_len++; |
| 240 | |
| 241 | return out_info[out_len - 1]; |
| 242 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 243 | void output_info (const hb_glyph_info_t &glyph_info) |
Behdad Esfahbod | 2a6f152 | 2018-10-03 20:09:14 +0200 | [diff] [blame] | 244 | { |
| 245 | if (unlikely (!make_room_for (0, 1))) return; |
| 246 | |
| 247 | out_info[out_len] = glyph_info; |
| 248 | |
| 249 | out_len++; |
| 250 | } |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 251 | /* Copies glyph at idx to output but doesn't advance idx */ |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 252 | void copy_glyph () |
Behdad Esfahbod | 2a6f152 | 2018-10-03 20:09:14 +0200 | [diff] [blame] | 253 | { |
| 254 | if (unlikely (!make_room_for (0, 1))) return; |
| 255 | |
| 256 | out_info[out_len] = info[idx]; |
| 257 | |
| 258 | out_len++; |
| 259 | } |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 260 | /* Copies glyph at idx to output and advance idx. |
| 261 | * If there's no output, just advance idx. */ |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 262 | void |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 263 | next_glyph () |
Behdad Esfahbod | 1be368e | 2012-08-31 16:29:17 -0400 | [diff] [blame] | 264 | { |
| 265 | if (have_output) |
| 266 | { |
Behdad Esfahbod | e883f52 | 2018-10-09 14:50:57 -0400 | [diff] [blame] | 267 | if (out_info != info || out_len != idx) |
| 268 | { |
Behdad Esfahbod | 13188cb | 2015-11-19 11:59:03 -0800 | [diff] [blame] | 269 | if (unlikely (!make_room_for (1, 1))) return; |
Behdad Esfahbod | 1be368e | 2012-08-31 16:29:17 -0400 | [diff] [blame] | 270 | out_info[out_len] = info[idx]; |
| 271 | } |
| 272 | out_len++; |
| 273 | } |
Behdad Esfahbod | 18e1c6b | 2015-11-19 11:50:58 -0800 | [diff] [blame] | 274 | |
Behdad Esfahbod | 1be368e | 2012-08-31 16:29:17 -0400 | [diff] [blame] | 275 | idx++; |
| 276 | } |
Behdad Esfahbod | e883f52 | 2018-10-09 14:50:57 -0400 | [diff] [blame] | 277 | /* Copies n glyphs at idx to output and advance idx. |
| 278 | * If there's no output, just advance idx. */ |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 279 | void |
Behdad Esfahbod | e883f52 | 2018-10-09 14:50:57 -0400 | [diff] [blame] | 280 | next_glyphs (unsigned int n) |
| 281 | { |
| 282 | if (have_output) |
| 283 | { |
| 284 | if (out_info != info || out_len != idx) |
| 285 | { |
| 286 | if (unlikely (!make_room_for (n, n))) return; |
| 287 | memmove (out_info + out_len, info + idx, n * sizeof (out_info[0])); |
| 288 | } |
| 289 | out_len += n; |
| 290 | } |
| 291 | |
| 292 | idx += n; |
| 293 | } |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 294 | /* Advance idx without copying to output. */ |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 295 | void skip_glyph () { idx++; } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 296 | void reset_masks (hb_mask_t mask) |
Behdad Esfahbod | bd7378b | 2010-10-13 18:33:16 -0400 | [diff] [blame] | 297 | { |
Behdad Esfahbod | 31f18ab | 2011-06-15 09:49:58 -0400 | [diff] [blame] | 298 | for (unsigned int j = 0; j < len; j++) |
| 299 | info[j].mask = mask; |
Behdad Esfahbod | bd7378b | 2010-10-13 18:33:16 -0400 | [diff] [blame] | 300 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 301 | void add_masks (hb_mask_t mask) |
Behdad Esfahbod | bd7378b | 2010-10-13 18:33:16 -0400 | [diff] [blame] | 302 | { |
Behdad Esfahbod | 31f18ab | 2011-06-15 09:49:58 -0400 | [diff] [blame] | 303 | for (unsigned int j = 0; j < len; j++) |
| 304 | info[j].mask |= mask; |
Behdad Esfahbod | bd7378b | 2010-10-13 18:33:16 -0400 | [diff] [blame] | 305 | } |
Behdad Esfahbod | 40bd7e9 | 2016-05-02 14:47:45 +0200 | [diff] [blame] | 306 | HB_INTERNAL void set_masks (hb_mask_t value, hb_mask_t mask, |
| 307 | unsigned int cluster_start, unsigned int cluster_end); |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 308 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 309 | void merge_clusters (unsigned int start, unsigned int end) |
Behdad Esfahbod | 701112d | 2015-07-22 15:42:20 +0100 | [diff] [blame] | 310 | { |
| 311 | if (end - start < 2) |
| 312 | return; |
| 313 | merge_clusters_impl (start, end); |
| 314 | } |
Behdad Esfahbod | 40bd7e9 | 2016-05-02 14:47:45 +0200 | [diff] [blame] | 315 | HB_INTERNAL void merge_clusters_impl (unsigned int start, unsigned int end); |
| 316 | HB_INTERNAL void merge_out_clusters (unsigned int start, unsigned int end); |
Behdad Esfahbod | 5f13bbd | 2015-06-19 13:31:49 -0700 | [diff] [blame] | 317 | /* Merge clusters for deleting current glyph, and skip it. */ |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 318 | HB_INTERNAL void delete_glyph (); |
Behdad Esfahbod | d4cc447 | 2012-04-07 21:52:28 -0400 | [diff] [blame] | 319 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 320 | void unsafe_to_break (unsigned int start, |
Behdad Esfahbod | 40bd7e9 | 2016-05-02 14:47:45 +0200 | [diff] [blame] | 321 | unsigned int end) |
| 322 | { |
| 323 | if (end - start < 2) |
| 324 | return; |
| 325 | unsafe_to_break_impl (start, end); |
| 326 | } |
| 327 | HB_INTERNAL void unsafe_to_break_impl (unsigned int start, unsigned int end); |
| 328 | HB_INTERNAL void unsafe_to_break_from_outbuffer (unsigned int start, unsigned int end); |
| 329 | |
| 330 | |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 331 | /* Internal methods */ |
Behdad Esfahbod | 2a6f152 | 2018-10-03 20:09:14 +0200 | [diff] [blame] | 332 | HB_INTERNAL bool move_to (unsigned int i); /* i is output-buffer index. */ |
| 333 | |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 334 | HB_INTERNAL bool enlarge (unsigned int size); |
| 335 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 336 | bool ensure (unsigned int size) |
Behdad Esfahbod | 02c6c8c | 2013-11-15 13:05:38 -0500 | [diff] [blame] | 337 | { return likely (!size || size < allocated) ? true : enlarge (size); } |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 338 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 339 | bool ensure_inplace (unsigned int size) |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 340 | { return likely (!size || size < allocated); } |
| 341 | |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 342 | HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out); |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 343 | HB_INTERNAL bool shift_forward (unsigned int count); |
Behdad Esfahbod | e62df43 | 2011-08-03 17:38:54 -0400 | [diff] [blame] | 344 | |
Behdad Esfahbod | 061cb46 | 2013-11-13 14:50:25 -0500 | [diff] [blame] | 345 | typedef long scratch_buffer_t; |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 346 | HB_INTERNAL scratch_buffer_t *get_scratch_buffer (unsigned int *size); |
Behdad Esfahbod | 05207a7 | 2012-09-25 17:44:53 -0400 | [diff] [blame] | 347 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 348 | void clear_context (unsigned int side) { context_len[side] = 0; } |
Behdad Esfahbod | e995d33 | 2015-09-01 16:13:32 +0100 | [diff] [blame] | 349 | |
| 350 | HB_INTERNAL void sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *)); |
Behdad Esfahbod | 0475ef2 | 2015-12-18 18:17:07 +0000 | [diff] [blame] | 351 | |
Behdad Esfahbod | c7439d4 | 2019-06-05 12:13:49 -0700 | [diff] [blame] | 352 | bool messaging () |
| 353 | { |
| 354 | #ifdef HB_NO_BUFFER_MESSAGE |
| 355 | return false; |
Behdad Esfahbod | eb9798e | 2019-06-18 13:29:55 -0700 | [diff] [blame] | 356 | #else |
Behdad Esfahbod | c7439d4 | 2019-06-05 12:13:49 -0700 | [diff] [blame] | 357 | return unlikely (message_func); |
Behdad Esfahbod | eb9798e | 2019-06-18 13:29:55 -0700 | [diff] [blame] | 358 | #endif |
Behdad Esfahbod | c7439d4 | 2019-06-05 12:13:49 -0700 | [diff] [blame] | 359 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 360 | bool message (hb_font_t *font, const char *fmt, ...) HB_PRINTF_FUNC(3, 4) |
Behdad Esfahbod | 0475ef2 | 2015-12-18 18:17:07 +0000 | [diff] [blame] | 361 | { |
Behdad Esfahbod | 7f3b409 | 2019-06-20 14:24:43 -0700 | [diff] [blame] | 362 | #ifdef HB_NO_BUFFER_MESSAGE |
| 363 | return true; |
| 364 | #else |
Behdad Esfahbod | 0475ef2 | 2015-12-18 18:17:07 +0000 | [diff] [blame] | 365 | if (!messaging ()) |
| 366 | return true; |
| 367 | va_list ap; |
| 368 | va_start (ap, fmt); |
| 369 | bool ret = message_impl (font, fmt, ap); |
| 370 | va_end (ap); |
| 371 | return ret; |
Behdad Esfahbod | 7f3b409 | 2019-06-20 14:24:43 -0700 | [diff] [blame] | 372 | #endif |
Behdad Esfahbod | 0475ef2 | 2015-12-18 18:17:07 +0000 | [diff] [blame] | 373 | } |
| 374 | HB_INTERNAL bool message_impl (hb_font_t *font, const char *fmt, va_list ap) HB_PRINTF_FUNC(3, 0); |
Behdad Esfahbod | e2a2b5b | 2017-08-10 20:10:12 -0700 | [diff] [blame] | 375 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 376 | static void |
Behdad Esfahbod | 3f291ff | 2018-01-11 17:57:46 +0100 | [diff] [blame] | 377 | set_cluster (hb_glyph_info_t &inf, unsigned int cluster, unsigned int mask = 0) |
Behdad Esfahbod | e2a2b5b | 2017-08-10 20:10:12 -0700 | [diff] [blame] | 378 | { |
Behdad Esfahbod | 3f291ff | 2018-01-11 17:57:46 +0100 | [diff] [blame] | 379 | if (inf.cluster != cluster) |
Behdad Esfahbod | e2a2b5b | 2017-08-10 20:10:12 -0700 | [diff] [blame] | 380 | { |
| 381 | if (mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) |
Behdad Esfahbod | 3f291ff | 2018-01-11 17:57:46 +0100 | [diff] [blame] | 382 | inf.mask |= HB_GLYPH_FLAG_UNSAFE_TO_BREAK; |
Behdad Esfahbod | e2a2b5b | 2017-08-10 20:10:12 -0700 | [diff] [blame] | 383 | else |
Behdad Esfahbod | 3f291ff | 2018-01-11 17:57:46 +0100 | [diff] [blame] | 384 | inf.mask &= ~HB_GLYPH_FLAG_UNSAFE_TO_BREAK; |
Behdad Esfahbod | e2a2b5b | 2017-08-10 20:10:12 -0700 | [diff] [blame] | 385 | } |
Behdad Esfahbod | 3f291ff | 2018-01-11 17:57:46 +0100 | [diff] [blame] | 386 | inf.cluster = cluster; |
Behdad Esfahbod | e2a2b5b | 2017-08-10 20:10:12 -0700 | [diff] [blame] | 387 | } |
Behdad Esfahbod | 1c17c2b | 2017-08-11 19:06:07 -0700 | [diff] [blame] | 388 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 389 | int |
Behdad Esfahbod | 3f291ff | 2018-01-11 17:57:46 +0100 | [diff] [blame] | 390 | _unsafe_to_break_find_min_cluster (const hb_glyph_info_t *infos, |
Behdad Esfahbod | 1c17c2b | 2017-08-11 19:06:07 -0700 | [diff] [blame] | 391 | unsigned int start, unsigned int end, |
| 392 | unsigned int cluster) const |
| 393 | { |
| 394 | for (unsigned int i = start; i < end; i++) |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 395 | cluster = hb_min (cluster, infos[i].cluster); |
Behdad Esfahbod | 1c17c2b | 2017-08-11 19:06:07 -0700 | [diff] [blame] | 396 | return cluster; |
| 397 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 398 | void |
Behdad Esfahbod | 3f291ff | 2018-01-11 17:57:46 +0100 | [diff] [blame] | 399 | _unsafe_to_break_set_mask (hb_glyph_info_t *infos, |
Behdad Esfahbod | 1c17c2b | 2017-08-11 19:06:07 -0700 | [diff] [blame] | 400 | unsigned int start, unsigned int end, |
| 401 | unsigned int cluster) |
| 402 | { |
| 403 | for (unsigned int i = start; i < end; i++) |
Behdad Esfahbod | 3f291ff | 2018-01-11 17:57:46 +0100 | [diff] [blame] | 404 | if (cluster != infos[i].cluster) |
Behdad Esfahbod | 1c17c2b | 2017-08-11 19:06:07 -0700 | [diff] [blame] | 405 | { |
| 406 | scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK; |
Behdad Esfahbod | 3f291ff | 2018-01-11 17:57:46 +0100 | [diff] [blame] | 407 | infos[i].mask |= HB_GLYPH_FLAG_UNSAFE_TO_BREAK; |
Behdad Esfahbod | 1c17c2b | 2017-08-11 19:06:07 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
Behdad Esfahbod | e4da380 | 2017-11-10 17:14:27 -0800 | [diff] [blame] | 410 | |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 411 | void unsafe_to_break_all () { unsafe_to_break_impl (0, len); } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 412 | void safe_to_break_all () |
Behdad Esfahbod | e4da380 | 2017-11-10 17:14:27 -0800 | [diff] [blame] | 413 | { |
| 414 | for (unsigned int i = 0; i < len; i++) |
| 415 | info[i].mask &= ~HB_GLYPH_FLAG_UNSAFE_TO_BREAK; |
| 416 | } |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 417 | }; |
Behdad Esfahbod | 3506672 | 2018-08-06 06:17:48 -0700 | [diff] [blame] | 418 | DECLARE_NULL_INSTANCE (hb_buffer_t); |
Behdad Esfahbod | d0316a8 | 2010-05-12 23:34:52 -0400 | [diff] [blame] | 419 | |
| 420 | |
Behdad Esfahbod | 1c17c2b | 2017-08-11 19:06:07 -0700 | [diff] [blame] | 421 | /* Loop over clusters. Duplicated in foreach_syllable(). */ |
| 422 | #define foreach_cluster(buffer, start, end) \ |
| 423 | for (unsigned int \ |
| 424 | _count = buffer->len, \ |
| 425 | start = 0, end = _count ? _next_cluster (buffer, 0) : 0; \ |
| 426 | start < _count; \ |
| 427 | start = end, end = _next_cluster (buffer, start)) |
| 428 | |
| 429 | static inline unsigned int |
| 430 | _next_cluster (hb_buffer_t *buffer, unsigned int start) |
| 431 | { |
| 432 | hb_glyph_info_t *info = buffer->info; |
| 433 | unsigned int count = buffer->len; |
| 434 | |
| 435 | unsigned int cluster = info[start].cluster; |
| 436 | while (++start < count && cluster == info[start].cluster) |
| 437 | ; |
| 438 | |
| 439 | return start; |
| 440 | } |
| 441 | |
| 442 | |
Behdad Esfahbod | 0c7fb74 | 2016-02-25 14:40:09 +0900 | [diff] [blame] | 443 | #define HB_BUFFER_XALLOCATE_VAR(b, func, var) \ |
Behdad Esfahbod | b65c060 | 2011-07-28 16:48:43 -0400 | [diff] [blame] | 444 | b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \ |
Behdad Esfahbod | 0c7fb74 | 2016-02-25 14:40:09 +0900 | [diff] [blame] | 445 | sizeof (b->info[0].var)) |
| 446 | #define HB_BUFFER_ALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var ()) |
| 447 | #define HB_BUFFER_DEALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var ()) |
| 448 | #define HB_BUFFER_ASSERT_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, assert_var, var ()) |
Behdad Esfahbod | b65c060 | 2011-07-28 16:48:43 -0400 | [diff] [blame] | 449 | |
| 450 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 451 | #endif /* HB_BUFFER_HH */ |