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 | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 27 | #ifndef HB_SHAPER_HH |
| 28 | #define HB_SHAPER_HH |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 29 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 30 | #include "hb.hh" |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 31 | |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 32 | typedef hb_bool_t hb_shape_func_t (hb_shape_plan_t *shape_plan, |
| 33 | hb_font_t *font, |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 34 | hb_buffer_t *buffer, |
| 35 | const hb_feature_t *features, |
| 36 | unsigned int num_features); |
| 37 | |
| 38 | #define HB_SHAPER_IMPLEMENT(name) \ |
| 39 | extern "C" HB_INTERNAL hb_shape_func_t _hb_##name##_shape; |
| 40 | #include "hb-shaper-list.hh" |
| 41 | #undef HB_SHAPER_IMPLEMENT |
| 42 | |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 43 | struct hb_shaper_pair_t { |
| 44 | char name[16]; |
| 45 | hb_shape_func_t *func; |
| 46 | }; |
| 47 | |
| 48 | HB_INTERNAL const hb_shaper_pair_t * |
| 49 | _hb_shapers_get (void); |
| 50 | |
| 51 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 52 | /* Means: succeeded, but don't need to keep any data. */ |
| 53 | #define HB_SHAPER_DATA_SUCCEEDED ((void *) +1) |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 54 | /* Means: tried but failed to create. */ |
| 55 | #define HB_SHAPER_DATA_INVALID ((void *) -1) |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 56 | |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 57 | #define HB_SHAPER_DATA_TYPE_NAME(shaper, object) hb_##shaper##_##object##_data_t |
Behdad Esfahbod | fc8189b | 2017-02-03 16:17:08 -0800 | [diff] [blame] | 58 | #define HB_SHAPER_DATA_TYPE(shaper, object) struct HB_SHAPER_DATA_TYPE_NAME(shaper, object) |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 59 | #define HB_SHAPER_DATA_INSTANCE(shaper, object, instance) (* reinterpret_cast<hb_atomic_ptr_t<HB_SHAPER_DATA_TYPE(shaper, object) *> *> (&(instance)->shaper_data.shaper)) |
Behdad Esfahbod | fc8189b | 2017-02-03 16:17:08 -0800 | [diff] [blame] | 60 | #define HB_SHAPER_DATA(shaper, object) HB_SHAPER_DATA_INSTANCE(shaper, object, object) |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 61 | #define HB_SHAPER_DATA_CREATE_FUNC(shaper, object) _hb_##shaper##_shaper_##object##_data_create |
| 62 | #define HB_SHAPER_DATA_DESTROY_FUNC(shaper, object) _hb_##shaper##_shaper_##object##_data_destroy |
Behdad Esfahbod | 466b3e5 | 2017-02-03 16:43:25 -0800 | [diff] [blame] | 63 | #define HB_SHAPER_DATA_ENSURE_FUNC(shaper, object) hb_##shaper##_shaper_##object##_data_ensure |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 64 | |
| 65 | #define HB_SHAPER_DATA_PROTOTYPE(shaper, object) \ |
| 66 | HB_SHAPER_DATA_TYPE (shaper, object); /* Type forward declaration. */ \ |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 67 | extern "C" HB_INTERNAL HB_SHAPER_DATA_TYPE (shaper, object) * \ |
Behdad Esfahbod | 5b95c14 | 2012-07-26 23:46:53 -0400 | [diff] [blame] | 68 | HB_SHAPER_DATA_CREATE_FUNC (shaper, object) (hb_##object##_t *object HB_SHAPER_DATA_CREATE_FUNC_EXTRA_ARGS); \ |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 69 | extern "C" HB_INTERNAL void \ |
Behdad Esfahbod | 466b3e5 | 2017-02-03 16:43:25 -0800 | [diff] [blame] | 70 | HB_SHAPER_DATA_DESTROY_FUNC (shaper, object) (HB_SHAPER_DATA_TYPE (shaper, object) *data); \ |
| 71 | extern "C" HB_INTERNAL bool \ |
| 72 | HB_SHAPER_DATA_ENSURE_FUNC (shaper, object) (hb_##object##_t *object) |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 73 | |
| 74 | #define HB_SHAPER_DATA_DESTROY(shaper, object) \ |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 75 | if (HB_SHAPER_DATA_TYPE (shaper, object) *data = HB_SHAPER_DATA (shaper, object).get ()) \ |
Konstantin Ritt | b306f96 | 2015-01-27 20:08:41 +0400 | [diff] [blame] | 76 | if (data != HB_SHAPER_DATA_INVALID && data != HB_SHAPER_DATA_SUCCEEDED) \ |
| 77 | HB_SHAPER_DATA_DESTROY_FUNC (shaper, object) (data); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 78 | |
Behdad Esfahbod | 466b3e5 | 2017-02-03 16:43:25 -0800 | [diff] [blame] | 79 | #define HB_SHAPER_DATA_ENSURE_DEFINE(shaper, object) \ |
Behdad Esfahbod | 8d450dd | 2017-10-11 12:43:46 +0200 | [diff] [blame] | 80 | HB_SHAPER_DATA_ENSURE_DEFINE_WITH_CONDITION(shaper, object, true) |
| 81 | |
| 82 | #define HB_SHAPER_DATA_ENSURE_DEFINE_WITH_CONDITION(shaper, object, condition) \ |
Behdad Esfahbod | 466b3e5 | 2017-02-03 16:43:25 -0800 | [diff] [blame] | 83 | bool \ |
| 84 | HB_SHAPER_DATA_ENSURE_FUNC(shaper, object) (hb_##object##_t *object) \ |
Behdad Esfahbod | cfe9882 | 2012-07-27 03:06:30 -0400 | [diff] [blame] | 85 | {\ |
| 86 | retry: \ |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 87 | HB_SHAPER_DATA_TYPE (shaper, object) *data = HB_SHAPER_DATA (shaper, object).get (); \ |
Behdad Esfahbod | 8d450dd | 2017-10-11 12:43:46 +0200 | [diff] [blame] | 88 | if (likely (data) && !(condition)) { \ |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 89 | /* XXX-MT-bug \ |
| 90 | * Note that evaluating condition above can be dangerous if another thread \ |
Behdad Esfahbod | 74467b7 | 2018-07-25 14:30:07 -0700 | [diff] [blame] | 91 | * got here first and destructed data. That's, as always, bad use pattern. \ |
| 92 | * If you modify the font (change font size), other threads must not be \ |
Behdad Esfahbod | 1a624c6 | 2018-08-01 17:53:30 -0700 | [diff] [blame] | 93 | * using it at the same time. However, since this check is delayed to \ |
| 94 | * when one actually tries to shape something, this is a XXX race condition \ |
| 95 | * (and the only know we have that I know of) right now. Ie. you modify the \ |
| 96 | * font size in one thread, then (supposedly safely) try to use it from two \ |
| 97 | * or more threads and BOOM! I'm not sure how to fix this. We want RCU. \ |
| 98 | * Maybe when it doesn't matter when we finally implement AAT shaping, as |
| 99 | * this (condition) is currently only used by hb-coretext. */ \ |
Behdad Esfahbod | 8d450dd | 2017-10-11 12:43:46 +0200 | [diff] [blame] | 100 | /* Drop and recreate. */ \ |
| 101 | /* If someone dropped it in the mean time, throw it away and don't touch it. \ |
| 102 | * Otherwise, destruct it. */ \ |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 103 | if (likely (HB_SHAPER_DATA (shaper, object).cmpexch (data, nullptr))) \ |
| 104 | { \ |
Behdad Esfahbod | 8d450dd | 2017-10-11 12:43:46 +0200 | [diff] [blame] | 105 | HB_SHAPER_DATA_DESTROY_FUNC (shaper, object) (data); \ |
| 106 | } \ |
| 107 | goto retry; \ |
| 108 | } \ |
Behdad Esfahbod | cfe9882 | 2012-07-27 03:06:30 -0400 | [diff] [blame] | 109 | if (unlikely (!data)) { \ |
| 110 | data = HB_SHAPER_DATA_CREATE_FUNC (shaper, object) (object); \ |
| 111 | if (unlikely (!data)) \ |
| 112 | data = (HB_SHAPER_DATA_TYPE (shaper, object) *) HB_SHAPER_DATA_INVALID; \ |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 113 | if (unlikely (!HB_SHAPER_DATA (shaper, object).cmpexch (nullptr, data))) { \ |
Behdad Esfahbod | 69fd6e1 | 2013-01-10 00:03:36 -0600 | [diff] [blame] | 114 | if (data && \ |
| 115 | data != HB_SHAPER_DATA_INVALID && \ |
| 116 | data != HB_SHAPER_DATA_SUCCEEDED) \ |
| 117 | HB_SHAPER_DATA_DESTROY_FUNC (shaper, object) (data); \ |
Behdad Esfahbod | cfe9882 | 2012-07-27 03:06:30 -0400 | [diff] [blame] | 118 | goto retry; \ |
| 119 | } \ |
| 120 | } \ |
Behdad Esfahbod | 36d0fbb | 2018-07-31 01:15:04 -0700 | [diff] [blame] | 121 | return data != nullptr && (void *) data != HB_SHAPER_DATA_INVALID; \ |
Behdad Esfahbod | cfe9882 | 2012-07-27 03:06:30 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 124 | |
Behdad Esfahbod | f5152ce | 2018-07-31 01:27:04 -0700 | [diff] [blame] | 125 | /* For embedding in face / font / ... */ |
| 126 | struct hb_shaper_data_t { |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 127 | #define HB_SHAPER_IMPLEMENT(shaper) hb_atomic_ptr_t<void *> shaper; |
Behdad Esfahbod | f5152ce | 2018-07-31 01:27:04 -0700 | [diff] [blame] | 128 | #include "hb-shaper-list.hh" |
| 129 | #undef HB_SHAPER_IMPLEMENT |
| 130 | }; |
| 131 | #define HB_SHAPERS_COUNT (sizeof (hb_shaper_data_t) / sizeof (void *)) |
| 132 | |
| 133 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 134 | #endif /* HB_SHAPER_HH */ |