Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Google, Inc. |
| 3 | * |
| 4 | * This is part of HarfBuzz, a text shaping library. |
| 5 | * |
| 6 | * Permission is hereby granted, without written agreement and without |
| 7 | * license or royalty fees, to use, copy, modify, and distribute this |
| 8 | * software and its documentation for any purpose, provided that the |
| 9 | * above copyright notice and the following two paragraphs appear in |
| 10 | * all copies of this software. |
| 11 | * |
| 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 16 | * DAMAGE. |
| 17 | * |
| 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 23 | * |
| 24 | * Google Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
Behdad Esfahbod | 40ec3bb | 2017-11-03 16:57:30 -0400 | [diff] [blame] | 27 | #include "hb-private.hh" |
| 28 | #include "hb-debug.hh" |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 29 | #include "hb-shape-plan-private.hh" |
Behdad Esfahbod | 5b95c14 | 2012-07-26 23:46:53 -0400 | [diff] [blame] | 30 | #include "hb-shaper-private.hh" |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 31 | #include "hb-font-private.hh" |
Behdad Esfahbod | 7abddbb | 2013-02-15 07:46:57 -0500 | [diff] [blame] | 32 | #include "hb-buffer-private.hh" |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 33 | |
Behdad Esfahbod | bc3d0dc | 2014-08-12 16:49:18 -0400 | [diff] [blame] | 34 | |
Behdad Esfahbod | 4c8ac4f | 2012-08-08 17:44:19 -0400 | [diff] [blame] | 35 | static void |
Behdad Esfahbod | 5b95c14 | 2012-07-26 23:46:53 -0400 | [diff] [blame] | 36 | hb_shape_plan_plan (hb_shape_plan_t *shape_plan, |
| 37 | const hb_feature_t *user_features, |
| 38 | unsigned int num_user_features, |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 39 | const int *coords, |
| 40 | unsigned int num_coords, |
Behdad Esfahbod | 5b95c14 | 2012-07-26 23:46:53 -0400 | [diff] [blame] | 41 | const char * const *shaper_list) |
| 42 | { |
Behdad Esfahbod | bc3d0dc | 2014-08-12 16:49:18 -0400 | [diff] [blame] | 43 | DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 44 | "num_features=%d num_coords=%d shaper_list=%p", |
Behdad Esfahbod | bc3d0dc | 2014-08-12 16:49:18 -0400 | [diff] [blame] | 45 | num_user_features, |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 46 | num_coords, |
Behdad Esfahbod | bc3d0dc | 2014-08-12 16:49:18 -0400 | [diff] [blame] | 47 | shaper_list); |
| 48 | |
Behdad Esfahbod | 5b95c14 | 2012-07-26 23:46:53 -0400 | [diff] [blame] | 49 | const hb_shaper_pair_t *shapers = _hb_shapers_get (); |
Behdad Esfahbod | 5b95c14 | 2012-07-26 23:46:53 -0400 | [diff] [blame] | 50 | |
| 51 | #define HB_SHAPER_PLAN(shaper) \ |
| 52 | HB_STMT_START { \ |
Behdad Esfahbod | f47b921 | 2013-12-02 05:57:27 -0500 | [diff] [blame] | 53 | if (hb_##shaper##_shaper_face_data_ensure (shape_plan->face_unsafe)) { \ |
Behdad Esfahbod | 3b7c4e2 | 2012-07-27 03:12:23 -0400 | [diff] [blame] | 54 | HB_SHAPER_DATA (shaper, shape_plan) = \ |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 55 | HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, \ |
| 56 | user_features, num_user_features, \ |
| 57 | coords, num_coords); \ |
Behdad Esfahbod | 3b7c4e2 | 2012-07-27 03:12:23 -0400 | [diff] [blame] | 58 | shape_plan->shaper_func = _hb_##shaper##_shape; \ |
Behdad Esfahbod | f9edd5d | 2012-11-16 13:23:37 -0800 | [diff] [blame] | 59 | shape_plan->shaper_name = #shaper; \ |
Behdad Esfahbod | 3b7c4e2 | 2012-07-27 03:12:23 -0400 | [diff] [blame] | 60 | return; \ |
Behdad Esfahbod | 5b95c14 | 2012-07-26 23:46:53 -0400 | [diff] [blame] | 61 | } \ |
| 62 | } HB_STMT_END |
| 63 | |
| 64 | if (likely (!shaper_list)) { |
| 65 | for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++) |
| 66 | if (0) |
| 67 | ; |
| 68 | #define HB_SHAPER_IMPLEMENT(shaper) \ |
| 69 | else if (shapers[i].func == _hb_##shaper##_shape) \ |
| 70 | HB_SHAPER_PLAN (shaper); |
| 71 | #include "hb-shaper-list.hh" |
| 72 | #undef HB_SHAPER_IMPLEMENT |
| 73 | } else { |
| 74 | for (; *shaper_list; shaper_list++) |
| 75 | if (0) |
| 76 | ; |
| 77 | #define HB_SHAPER_IMPLEMENT(shaper) \ |
| 78 | else if (0 == strcmp (*shaper_list, #shaper)) \ |
| 79 | HB_SHAPER_PLAN (shaper); |
| 80 | #include "hb-shaper-list.hh" |
| 81 | #undef HB_SHAPER_IMPLEMENT |
| 82 | } |
| 83 | |
| 84 | #undef HB_SHAPER_PLAN |
| 85 | } |
| 86 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * hb_shape_plan_t |
| 90 | */ |
| 91 | |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 92 | /** |
Behdad Esfahbod | 085d429 | 2013-09-12 17:14:33 -0400 | [diff] [blame] | 93 | * hb_shape_plan_create: (Xconstructor) |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 94 | * @face: |
| 95 | * @props: |
| 96 | * @user_features: (array length=num_user_features): |
| 97 | * @num_user_features: |
| 98 | * @shaper_list: (array zero-terminated=1): |
| 99 | * |
| 100 | * |
| 101 | * |
| 102 | * Return value: (transfer full): |
| 103 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 104 | * Since: 0.9.7 |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 105 | **/ |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 106 | hb_shape_plan_t * |
| 107 | hb_shape_plan_create (hb_face_t *face, |
| 108 | const hb_segment_properties_t *props, |
| 109 | const hb_feature_t *user_features, |
| 110 | unsigned int num_user_features, |
| 111 | const char * const *shaper_list) |
| 112 | { |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 113 | return hb_shape_plan_create2 (face, props, |
| 114 | user_features, num_user_features, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 115 | nullptr, 0, |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 116 | shaper_list); |
| 117 | } |
| 118 | |
| 119 | hb_shape_plan_t * |
| 120 | hb_shape_plan_create2 (hb_face_t *face, |
| 121 | const hb_segment_properties_t *props, |
| 122 | const hb_feature_t *user_features, |
| 123 | unsigned int num_user_features, |
| 124 | const int *orig_coords, |
| 125 | unsigned int num_coords, |
| 126 | const char * const *shaper_list) |
| 127 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 128 | DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr, |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 129 | "face=%p num_features=%d num_coords=%d shaper_list=%p", |
Behdad Esfahbod | dc9aba6 | 2014-08-12 17:14:36 -0400 | [diff] [blame] | 130 | face, |
Behdad Esfahbod | bc3d0dc | 2014-08-12 16:49:18 -0400 | [diff] [blame] | 131 | num_user_features, |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 132 | num_coords, |
Behdad Esfahbod | bc3d0dc | 2014-08-12 16:49:18 -0400 | [diff] [blame] | 133 | shaper_list); |
| 134 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 135 | hb_shape_plan_t *shape_plan; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 136 | hb_feature_t *features = nullptr; |
| 137 | int *coords = nullptr; |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 138 | |
| 139 | if (unlikely (!face)) |
| 140 | face = hb_face_get_empty (); |
Behdad Esfahbod | eb0bf3a | 2014-08-06 15:36:41 -0400 | [diff] [blame] | 141 | if (unlikely (!props)) |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 142 | return hb_shape_plan_get_empty (); |
Behdad Esfahbod | a5efaac | 2015-10-02 08:02:29 +0100 | [diff] [blame] | 143 | if (num_user_features && !(features = (hb_feature_t *) calloc (num_user_features, sizeof (hb_feature_t)))) |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 144 | return hb_shape_plan_get_empty (); |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 145 | if (num_coords && !(coords = (int *) calloc (num_coords, sizeof (int)))) |
| 146 | { |
| 147 | free (features); |
| 148 | return hb_shape_plan_get_empty (); |
| 149 | } |
| 150 | if (!(shape_plan = hb_object_create<hb_shape_plan_t> ())) |
| 151 | { |
| 152 | free (coords); |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 153 | free (features); |
| 154 | return hb_shape_plan_get_empty (); |
| 155 | } |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 156 | |
Behdad Esfahbod | dac8602 | 2014-06-03 17:57:00 -0400 | [diff] [blame] | 157 | assert (props->direction != HB_DIRECTION_INVALID); |
| 158 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 159 | hb_face_make_immutable (face); |
Behdad Esfahbod | 68af14d | 2017-08-09 17:09:21 -0700 | [diff] [blame] | 160 | shape_plan->default_shaper_list = !shaper_list; |
Behdad Esfahbod | f47b921 | 2013-12-02 05:57:27 -0500 | [diff] [blame] | 161 | shape_plan->face_unsafe = face; |
Behdad Esfahbod | 5b95c14 | 2012-07-26 23:46:53 -0400 | [diff] [blame] | 162 | shape_plan->props = *props; |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 163 | shape_plan->num_user_features = num_user_features; |
| 164 | shape_plan->user_features = features; |
| 165 | if (num_user_features) |
| 166 | memcpy (features, user_features, num_user_features * sizeof (hb_feature_t)); |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 167 | shape_plan->num_coords = num_coords; |
| 168 | shape_plan->coords = coords; |
| 169 | if (num_coords) |
| 170 | memcpy (coords, orig_coords, num_coords * sizeof (int)); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 171 | |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 172 | hb_shape_plan_plan (shape_plan, |
| 173 | user_features, num_user_features, |
| 174 | coords, num_coords, |
| 175 | shaper_list); |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 176 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 177 | return shape_plan; |
| 178 | } |
| 179 | |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 180 | /** |
| 181 | * hb_shape_plan_get_empty: |
| 182 | * |
| 183 | * |
| 184 | * |
| 185 | * Return value: (transfer full): |
| 186 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 187 | * Since: 0.9.7 |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 188 | **/ |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 189 | hb_shape_plan_t * |
| 190 | hb_shape_plan_get_empty (void) |
| 191 | { |
| 192 | static const hb_shape_plan_t _hb_shape_plan_nil = { |
| 193 | HB_OBJECT_HEADER_STATIC, |
Behdad Esfahbod | ea278d3 | 2012-07-27 02:12:28 -0400 | [diff] [blame] | 194 | |
Behdad Esfahbod | ef6e9ce | 2012-07-29 21:35:22 -0400 | [diff] [blame] | 195 | true, /* default_shaper_list */ |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 196 | nullptr, /* face */ |
Behdad Esfahbod | f306410 | 2012-11-15 18:39:46 -0800 | [diff] [blame] | 197 | HB_SEGMENT_PROPERTIES_DEFAULT, /* props */ |
Behdad Esfahbod | ea278d3 | 2012-07-27 02:12:28 -0400 | [diff] [blame] | 198 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 199 | nullptr, /* shaper_func */ |
| 200 | nullptr, /* shaper_name */ |
Behdad Esfahbod | ea278d3 | 2012-07-27 02:12:28 -0400 | [diff] [blame] | 201 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 202 | nullptr, /* user_features */ |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 203 | 0, /* num_user_featurs */ |
| 204 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 205 | nullptr, /* coords */ |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 206 | 0, /* num_coords */ |
| 207 | |
Behdad Esfahbod | ea278d3 | 2012-07-27 02:12:28 -0400 | [diff] [blame] | 208 | { |
| 209 | #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID, |
| 210 | #include "hb-shaper-list.hh" |
| 211 | #undef HB_SHAPER_IMPLEMENT |
| 212 | } |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | return const_cast<hb_shape_plan_t *> (&_hb_shape_plan_nil); |
| 216 | } |
| 217 | |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 218 | /** |
| 219 | * hb_shape_plan_reference: (skip) |
| 220 | * @shape_plan: a shape plan. |
| 221 | * |
| 222 | * |
| 223 | * |
| 224 | * Return value: (transfer full): |
| 225 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 226 | * Since: 0.9.7 |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 227 | **/ |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 228 | hb_shape_plan_t * |
| 229 | hb_shape_plan_reference (hb_shape_plan_t *shape_plan) |
| 230 | { |
| 231 | return hb_object_reference (shape_plan); |
| 232 | } |
| 233 | |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 234 | /** |
| 235 | * hb_shape_plan_destroy: (skip) |
| 236 | * @shape_plan: a shape plan. |
| 237 | * |
| 238 | * |
| 239 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 240 | * Since: 0.9.7 |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 241 | **/ |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 242 | void |
| 243 | hb_shape_plan_destroy (hb_shape_plan_t *shape_plan) |
| 244 | { |
| 245 | if (!hb_object_destroy (shape_plan)) return; |
| 246 | |
| 247 | #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, shape_plan); |
| 248 | #include "hb-shaper-list.hh" |
| 249 | #undef HB_SHAPER_IMPLEMENT |
| 250 | |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 251 | free (shape_plan->user_features); |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 252 | free (shape_plan->coords); |
Behdad Esfahbod | 46ee108 | 2012-08-03 18:21:13 -0700 | [diff] [blame] | 253 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 254 | free (shape_plan); |
| 255 | } |
Behdad Esfahbod | c32c096 | 2012-07-27 01:13:53 -0400 | [diff] [blame] | 256 | |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 257 | /** |
| 258 | * hb_shape_plan_set_user_data: (skip) |
| 259 | * @shape_plan: a shape plan. |
| 260 | * @key: |
| 261 | * @data: |
| 262 | * @destroy: |
| 263 | * @replace: |
| 264 | * |
| 265 | * |
| 266 | * |
| 267 | * Return value: |
| 268 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 269 | * Since: 0.9.7 |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 270 | **/ |
Behdad Esfahbod | f306410 | 2012-11-15 18:39:46 -0800 | [diff] [blame] | 271 | hb_bool_t |
| 272 | hb_shape_plan_set_user_data (hb_shape_plan_t *shape_plan, |
| 273 | hb_user_data_key_t *key, |
| 274 | void * data, |
| 275 | hb_destroy_func_t destroy, |
| 276 | hb_bool_t replace) |
| 277 | { |
| 278 | return hb_object_set_user_data (shape_plan, key, data, destroy, replace); |
| 279 | } |
| 280 | |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 281 | /** |
| 282 | * hb_shape_plan_get_user_data: (skip) |
| 283 | * @shape_plan: a shape plan. |
| 284 | * @key: |
| 285 | * |
| 286 | * |
| 287 | * |
| 288 | * Return value: (transfer none): |
| 289 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 290 | * Since: 0.9.7 |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 291 | **/ |
Behdad Esfahbod | f306410 | 2012-11-15 18:39:46 -0800 | [diff] [blame] | 292 | void * |
| 293 | hb_shape_plan_get_user_data (hb_shape_plan_t *shape_plan, |
| 294 | hb_user_data_key_t *key) |
| 295 | { |
| 296 | return hb_object_get_user_data (shape_plan, key); |
| 297 | } |
| 298 | |
Behdad Esfahbod | c32c096 | 2012-07-27 01:13:53 -0400 | [diff] [blame] | 299 | |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 300 | /** |
| 301 | * hb_shape_plan_execute: |
| 302 | * @shape_plan: a shape plan. |
| 303 | * @font: a font. |
| 304 | * @buffer: a buffer. |
| 305 | * @features: (array length=num_features): |
| 306 | * @num_features: |
| 307 | * |
| 308 | * |
| 309 | * |
| 310 | * Return value: |
| 311 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 312 | * Since: 0.9.7 |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 313 | **/ |
Behdad Esfahbod | c32c096 | 2012-07-27 01:13:53 -0400 | [diff] [blame] | 314 | hb_bool_t |
Behdad Esfahbod | f306410 | 2012-11-15 18:39:46 -0800 | [diff] [blame] | 315 | hb_shape_plan_execute (hb_shape_plan_t *shape_plan, |
Behdad Esfahbod | c32c096 | 2012-07-27 01:13:53 -0400 | [diff] [blame] | 316 | hb_font_t *font, |
| 317 | hb_buffer_t *buffer, |
| 318 | const hb_feature_t *features, |
| 319 | unsigned int num_features) |
| 320 | { |
Behdad Esfahbod | bc3d0dc | 2014-08-12 16:49:18 -0400 | [diff] [blame] | 321 | DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, |
Khaled Hosny | 3d976d2 | 2016-08-27 16:38:42 +0300 | [diff] [blame] | 322 | "num_features=%d shaper_func=%p, shaper_name=%s", |
Behdad Esfahbod | bc3d0dc | 2014-08-12 16:49:18 -0400 | [diff] [blame] | 323 | num_features, |
Khaled Hosny | 3d976d2 | 2016-08-27 16:38:42 +0300 | [diff] [blame] | 324 | shape_plan->shaper_func, |
| 325 | shape_plan->shaper_name); |
Behdad Esfahbod | bc3d0dc | 2014-08-12 16:49:18 -0400 | [diff] [blame] | 326 | |
Behdad Esfahbod | 26ba4d1 | 2015-10-02 07:25:52 +0100 | [diff] [blame] | 327 | if (unlikely (!buffer->len)) |
| 328 | return true; |
| 329 | |
| 330 | assert (!hb_object_is_inert (buffer)); |
| 331 | assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE); |
| 332 | |
| 333 | if (unlikely (hb_object_is_inert (shape_plan))) |
Behdad Esfahbod | c32c096 | 2012-07-27 01:13:53 -0400 | [diff] [blame] | 334 | return false; |
| 335 | |
Behdad Esfahbod | f47b921 | 2013-12-02 05:57:27 -0500 | [diff] [blame] | 336 | assert (shape_plan->face_unsafe == font->face); |
Behdad Esfahbod | 7abddbb | 2013-02-15 07:46:57 -0500 | [diff] [blame] | 337 | assert (hb_segment_properties_equal (&shape_plan->props, &buffer->props)); |
| 338 | |
Behdad Esfahbod | c32c096 | 2012-07-27 01:13:53 -0400 | [diff] [blame] | 339 | #define HB_SHAPER_EXECUTE(shaper) \ |
| 340 | HB_STMT_START { \ |
Behdad Esfahbod | 3b7c4e2 | 2012-07-27 03:12:23 -0400 | [diff] [blame] | 341 | return HB_SHAPER_DATA (shaper, shape_plan) && \ |
| 342 | hb_##shaper##_shaper_font_data_ensure (font) && \ |
| 343 | _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \ |
Behdad Esfahbod | c32c096 | 2012-07-27 01:13:53 -0400 | [diff] [blame] | 344 | } HB_STMT_END |
| 345 | |
Behdad Esfahbod | c5b668f | 2012-07-27 02:49:39 -0400 | [diff] [blame] | 346 | if (0) |
| 347 | ; |
Behdad Esfahbod | c32c096 | 2012-07-27 01:13:53 -0400 | [diff] [blame] | 348 | #define HB_SHAPER_IMPLEMENT(shaper) \ |
Behdad Esfahbod | c5b668f | 2012-07-27 02:49:39 -0400 | [diff] [blame] | 349 | else if (shape_plan->shaper_func == _hb_##shaper##_shape) \ |
| 350 | HB_SHAPER_EXECUTE (shaper); |
Behdad Esfahbod | c32c096 | 2012-07-27 01:13:53 -0400 | [diff] [blame] | 351 | #include "hb-shaper-list.hh" |
| 352 | #undef HB_SHAPER_IMPLEMENT |
| 353 | |
| 354 | #undef HB_SHAPER_EXECUTE |
| 355 | |
| 356 | return false; |
| 357 | } |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 358 | |
| 359 | |
| 360 | /* |
| 361 | * caching |
| 362 | */ |
| 363 | |
| 364 | #if 0 |
Behdad Esfahbod | 801298b | 2012-08-08 14:26:36 -0400 | [diff] [blame] | 365 | static unsigned int |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 366 | hb_shape_plan_hash (const hb_shape_plan_t *shape_plan) |
| 367 | { |
| 368 | return hb_segment_properties_hash (&shape_plan->props) + |
Behdad Esfahbod | 801298b | 2012-08-08 14:26:36 -0400 | [diff] [blame] | 369 | shape_plan->default_shaper_list ? 0 : (intptr_t) shape_plan->shaper_func; |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 370 | } |
| 371 | #endif |
| 372 | |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 373 | /* User-feature caching is currently somewhat dumb: |
| 374 | * it only finds matches where the feature array is identical, |
| 375 | * not cases where the feature lists would be compatible for plan purposes |
| 376 | * but have different ranges, for example. |
| 377 | */ |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 378 | struct hb_shape_plan_proposal_t |
| 379 | { |
| 380 | const hb_segment_properties_t props; |
| 381 | const char * const *shaper_list; |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 382 | const hb_feature_t *user_features; |
| 383 | unsigned int num_user_features; |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 384 | const int *coords; |
| 385 | unsigned int num_coords; |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 386 | hb_shape_func_t *shaper_func; |
| 387 | }; |
| 388 | |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 389 | static inline hb_bool_t |
| 390 | hb_shape_plan_user_features_match (const hb_shape_plan_t *shape_plan, |
| 391 | const hb_shape_plan_proposal_t *proposal) |
| 392 | { |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 393 | if (proposal->num_user_features != shape_plan->num_user_features) |
| 394 | return false; |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 395 | for (unsigned int i = 0, n = proposal->num_user_features; i < n; i++) |
| 396 | if (proposal->user_features[i].tag != shape_plan->user_features[i].tag || |
| 397 | proposal->user_features[i].value != shape_plan->user_features[i].value || |
| 398 | proposal->user_features[i].start != shape_plan->user_features[i].start || |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 399 | proposal->user_features[i].end != shape_plan->user_features[i].end) |
| 400 | return false; |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | static inline hb_bool_t |
| 405 | hb_shape_plan_coords_match (const hb_shape_plan_t *shape_plan, |
| 406 | const hb_shape_plan_proposal_t *proposal) |
| 407 | { |
| 408 | if (proposal->num_coords != shape_plan->num_coords) |
| 409 | return false; |
| 410 | for (unsigned int i = 0, n = proposal->num_coords; i < n; i++) |
| 411 | if (proposal->coords[i] != shape_plan->coords[i]) |
| 412 | return false; |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 413 | return true; |
| 414 | } |
| 415 | |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 416 | static hb_bool_t |
| 417 | hb_shape_plan_matches (const hb_shape_plan_t *shape_plan, |
| 418 | const hb_shape_plan_proposal_t *proposal) |
| 419 | { |
| 420 | return hb_segment_properties_equal (&shape_plan->props, &proposal->props) && |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 421 | hb_shape_plan_user_features_match (shape_plan, proposal) && |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 422 | hb_shape_plan_coords_match (shape_plan, proposal) && |
Behdad Esfahbod | 68af14d | 2017-08-09 17:09:21 -0700 | [diff] [blame] | 423 | ((shape_plan->default_shaper_list && !proposal->shaper_list) || |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 424 | (shape_plan->shaper_func == proposal->shaper_func)); |
| 425 | } |
| 426 | |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 427 | static inline hb_bool_t |
| 428 | hb_non_global_user_features_present (const hb_feature_t *user_features, |
| 429 | unsigned int num_user_features) |
| 430 | { |
Chris Peterson | aacca37 | 2017-04-17 23:25:24 -0700 | [diff] [blame] | 431 | while (num_user_features) { |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 432 | if (user_features->start != 0 || user_features->end != (unsigned int) -1) |
| 433 | return true; |
Chris Peterson | aacca37 | 2017-04-17 23:25:24 -0700 | [diff] [blame] | 434 | num_user_features--; |
| 435 | user_features++; |
| 436 | } |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 437 | return false; |
| 438 | } |
| 439 | |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 440 | static inline hb_bool_t |
| 441 | hb_coords_present (const int *coords, |
| 442 | unsigned int num_coords) |
| 443 | { |
| 444 | return num_coords != 0; |
| 445 | } |
| 446 | |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 447 | /** |
| 448 | * hb_shape_plan_create_cached: |
| 449 | * @face: |
| 450 | * @props: |
| 451 | * @user_features: (array length=num_user_features): |
| 452 | * @num_user_features: |
| 453 | * @shaper_list: (array zero-terminated=1): |
| 454 | * |
| 455 | * |
| 456 | * |
| 457 | * Return value: (transfer full): |
| 458 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 459 | * Since: 0.9.7 |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 460 | **/ |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 461 | hb_shape_plan_t * |
| 462 | hb_shape_plan_create_cached (hb_face_t *face, |
| 463 | const hb_segment_properties_t *props, |
| 464 | const hb_feature_t *user_features, |
| 465 | unsigned int num_user_features, |
| 466 | const char * const *shaper_list) |
| 467 | { |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 468 | return hb_shape_plan_create_cached2 (face, props, |
| 469 | user_features, num_user_features, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 470 | nullptr, 0, |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 471 | shaper_list); |
| 472 | } |
| 473 | |
| 474 | hb_shape_plan_t * |
| 475 | hb_shape_plan_create_cached2 (hb_face_t *face, |
| 476 | const hb_segment_properties_t *props, |
| 477 | const hb_feature_t *user_features, |
| 478 | unsigned int num_user_features, |
| 479 | const int *coords, |
| 480 | unsigned int num_coords, |
| 481 | const char * const *shaper_list) |
| 482 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 483 | DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr, |
Behdad Esfahbod | dc9aba6 | 2014-08-12 17:14:36 -0400 | [diff] [blame] | 484 | "face=%p num_features=%d shaper_list=%p", |
| 485 | face, |
Behdad Esfahbod | bc3d0dc | 2014-08-12 16:49:18 -0400 | [diff] [blame] | 486 | num_user_features, |
| 487 | shaper_list); |
| 488 | |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 489 | hb_shape_plan_proposal_t proposal = { |
| 490 | *props, |
| 491 | shaper_list, |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 492 | user_features, |
| 493 | num_user_features, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 494 | nullptr |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 495 | }; |
| 496 | |
| 497 | if (shaper_list) { |
Behdad Esfahbod | e956c65 | 2014-08-12 17:03:27 -0400 | [diff] [blame] | 498 | /* Choose shaper. Adapted from hb_shape_plan_plan(). |
| 499 | * Must choose shaper exactly the same way as that function. */ |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 500 | for (const char * const *shaper_item = shaper_list; *shaper_item; shaper_item++) |
| 501 | if (0) |
| 502 | ; |
| 503 | #define HB_SHAPER_IMPLEMENT(shaper) \ |
Behdad Esfahbod | e956c65 | 2014-08-12 17:03:27 -0400 | [diff] [blame] | 504 | else if (0 == strcmp (*shaper_item, #shaper) && \ |
| 505 | hb_##shaper##_shaper_face_data_ensure (face)) \ |
| 506 | { \ |
| 507 | proposal.shaper_func = _hb_##shaper##_shape; \ |
| 508 | break; \ |
| 509 | } |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 510 | #include "hb-shaper-list.hh" |
| 511 | #undef HB_SHAPER_IMPLEMENT |
| 512 | |
Behdad Esfahbod | 30eed75 | 2014-08-12 17:15:09 -0400 | [diff] [blame] | 513 | if (unlikely (!proposal.shaper_func)) |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 514 | return hb_shape_plan_get_empty (); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | retry: |
| 519 | hb_face_t::plan_node_t *cached_plan_nodes = (hb_face_t::plan_node_t *) hb_atomic_ptr_get (&face->shape_plans); |
jfkthame | 19e77e0 | 2017-10-02 13:08:49 +0100 | [diff] [blame] | 520 | |
| 521 | /* Don't look for plan in the cache if there were variation coordinates XXX Fix me. */ |
| 522 | if (!hb_coords_present (coords, num_coords)) |
| 523 | for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next) |
| 524 | if (hb_shape_plan_matches (node->shape_plan, &proposal)) |
| 525 | { |
| 526 | DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache"); |
| 527 | return hb_shape_plan_reference (node->shape_plan); |
| 528 | } |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 529 | |
| 530 | /* Not found. */ |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 531 | hb_shape_plan_t *shape_plan = hb_shape_plan_create2 (face, props, |
| 532 | user_features, num_user_features, |
| 533 | coords, num_coords, |
| 534 | shaper_list); |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 535 | |
Behdad Esfahbod | eb0bf3a | 2014-08-06 15:36:41 -0400 | [diff] [blame] | 536 | /* Don't add to the cache if face is inert. */ |
| 537 | if (unlikely (hb_object_is_inert (face))) |
| 538 | return shape_plan; |
| 539 | |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 540 | /* Don't add the plan to the cache if there were user features with non-global ranges */ |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 541 | if (hb_non_global_user_features_present (user_features, num_user_features)) |
| 542 | return shape_plan; |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 543 | /* Don't add the plan to the cache if there were variation coordinates XXX Fix me. */ |
| 544 | if (hb_coords_present (coords, num_coords)) |
| 545 | return shape_plan; |
Jonathan Kew | ca8d96c | 2013-12-02 05:22:00 -0500 | [diff] [blame] | 546 | |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 547 | hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) calloc (1, sizeof (hb_face_t::plan_node_t)); |
| 548 | if (unlikely (!node)) |
| 549 | return shape_plan; |
| 550 | |
| 551 | node->shape_plan = shape_plan; |
| 552 | node->next = cached_plan_nodes; |
| 553 | |
| 554 | if (!hb_atomic_ptr_cmpexch (&face->shape_plans, cached_plan_nodes, node)) { |
| 555 | hb_shape_plan_destroy (shape_plan); |
| 556 | free (node); |
| 557 | goto retry; |
| 558 | } |
Behdad Esfahbod | dc9aba6 | 2014-08-12 17:14:36 -0400 | [diff] [blame] | 559 | DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache"); |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 560 | |
Behdad Esfahbod | 2f87ceb | 2012-07-27 04:02:38 -0400 | [diff] [blame] | 561 | return hb_shape_plan_reference (shape_plan); |
| 562 | } |
Behdad Esfahbod | f9edd5d | 2012-11-16 13:23:37 -0800 | [diff] [blame] | 563 | |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 564 | /** |
| 565 | * hb_shape_plan_get_shaper: |
| 566 | * @shape_plan: a shape plan. |
| 567 | * |
| 568 | * |
| 569 | * |
| 570 | * Return value: (transfer none): |
| 571 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 572 | * Since: 0.9.7 |
Behdad Esfahbod | 288f289 | 2013-09-06 15:40:22 -0400 | [diff] [blame] | 573 | **/ |
Behdad Esfahbod | f9edd5d | 2012-11-16 13:23:37 -0800 | [diff] [blame] | 574 | const char * |
| 575 | hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan) |
| 576 | { |
Behdad Esfahbod | 7c5b7fe | 2012-11-22 14:15:08 -0500 | [diff] [blame] | 577 | return shape_plan->shaper_name; |
Behdad Esfahbod | f9edd5d | 2012-11-16 13:23:37 -0800 | [diff] [blame] | 578 | } |