Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -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 | #include "hb.hh" |
| 28 | #include "hb-shaper.hh" |
| 29 | #include "hb-machinery.hh" |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 30 | |
| 31 | |
Behdad Esfahbod | 19802df | 2022-06-16 13:50:35 -0600 | [diff] [blame] | 32 | static const hb_shaper_entry_t _hb_all_shapers[] = { |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 33 | #define HB_SHAPER_IMPLEMENT(name) {#name, _hb_##name##_shape}, |
| 34 | #include "hb-shaper-list.hh" |
| 35 | #undef HB_SHAPER_IMPLEMENT |
| 36 | }; |
Behdad Esfahbod | 9d5b534 | 2019-06-26 13:58:40 -0700 | [diff] [blame] | 37 | #ifndef HB_NO_SHAPER |
Behdad Esfahbod | 19802df | 2022-06-16 13:50:35 -0600 | [diff] [blame] | 38 | static_assert (0 != ARRAY_LENGTH_CONST (_hb_all_shapers), "No shaper enabled."); |
Behdad Esfahbod | 9d5b534 | 2019-06-26 13:58:40 -0700 | [diff] [blame] | 39 | #endif |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 40 | |
Behdad Esfahbod | ed11632 | 2021-09-14 07:09:54 -0400 | [diff] [blame] | 41 | static inline void free_static_shapers (); |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 42 | |
Behdad Esfahbod | a5cf1a8 | 2022-06-20 18:01:25 -0600 | [diff] [blame] | 43 | static struct hb_shapers_lazy_loader_t : hb_lazy_loader_t<hb_shaper_entry_t, |
Behdad Esfahbod | 747d256 | 2018-08-12 17:32:10 -0700 | [diff] [blame] | 44 | hb_shapers_lazy_loader_t> |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 45 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 46 | static hb_shaper_entry_t *create () |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 47 | { |
| 48 | char *env = getenv ("HB_SHAPER_LIST"); |
Behdad Esfahbod | 747d256 | 2018-08-12 17:32:10 -0700 | [diff] [blame] | 49 | if (!env || !*env) |
| 50 | return nullptr; |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 51 | |
Behdad Esfahbod | 19802df | 2022-06-16 13:50:35 -0600 | [diff] [blame] | 52 | hb_shaper_entry_t *shapers = (hb_shaper_entry_t *) hb_calloc (1, sizeof (_hb_all_shapers)); |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 53 | if (unlikely (!shapers)) |
Behdad Esfahbod | 747d256 | 2018-08-12 17:32:10 -0700 | [diff] [blame] | 54 | return nullptr; |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 55 | |
Behdad Esfahbod | 59c45f6 | 2022-11-22 12:54:50 -0700 | [diff] [blame] | 56 | hb_memcpy (shapers, _hb_all_shapers, sizeof (_hb_all_shapers)); |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 57 | |
| 58 | /* Reorder shaper list to prefer requested shapers. */ |
| 59 | unsigned int i = 0; |
| 60 | char *end, *p = env; |
Behdad Esfahbod | 747d256 | 2018-08-12 17:32:10 -0700 | [diff] [blame] | 61 | for (;;) |
| 62 | { |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 63 | end = strchr (p, ','); |
| 64 | if (!end) |
| 65 | end = p + strlen (p); |
| 66 | |
Behdad Esfahbod | 42da7da | 2022-07-13 12:46:36 -0600 | [diff] [blame] | 67 | for (unsigned int j = i; j < ARRAY_LENGTH_CONST (_hb_all_shapers); j++) |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 68 | if (end - p == (int) strlen (shapers[j].name) && |
| 69 | 0 == strncmp (shapers[j].name, p, end - p)) |
| 70 | { |
| 71 | /* Reorder this shaper to position i */ |
Behdad Esfahbod | 3bf1ce7 | 2018-11-14 21:08:54 -0500 | [diff] [blame] | 72 | struct hb_shaper_entry_t t = shapers[j]; |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 73 | memmove (&shapers[i + 1], &shapers[i], sizeof (shapers[i]) * (j - i)); |
| 74 | shapers[i] = t; |
| 75 | i++; |
| 76 | } |
| 77 | |
| 78 | if (!*end) |
| 79 | break; |
| 80 | else |
| 81 | p = end + 1; |
| 82 | } |
| 83 | |
Behdad Esfahbod | ed11632 | 2021-09-14 07:09:54 -0400 | [diff] [blame] | 84 | hb_atexit (free_static_shapers); |
Behdad Esfahbod | 747d256 | 2018-08-12 17:32:10 -0700 | [diff] [blame] | 85 | |
| 86 | return shapers; |
| 87 | } |
Behdad Esfahbod | a5cf1a8 | 2022-06-20 18:01:25 -0600 | [diff] [blame] | 88 | static void destroy (hb_shaper_entry_t *p) { hb_free (p); } |
Behdad Esfahbod | 19802df | 2022-06-16 13:50:35 -0600 | [diff] [blame] | 89 | static const hb_shaper_entry_t *get_null () { return _hb_all_shapers; } |
Behdad Esfahbod | 747d256 | 2018-08-12 17:32:10 -0700 | [diff] [blame] | 90 | } static_shapers; |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 91 | |
Behdad Esfahbod | ed11632 | 2021-09-14 07:09:54 -0400 | [diff] [blame] | 92 | static inline |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 93 | void free_static_shapers () |
Behdad Esfahbod | 747d256 | 2018-08-12 17:32:10 -0700 | [diff] [blame] | 94 | { |
| 95 | static_shapers.free_instance (); |
| 96 | } |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 97 | |
Behdad Esfahbod | 3bf1ce7 | 2018-11-14 21:08:54 -0500 | [diff] [blame] | 98 | const hb_shaper_entry_t * |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 99 | _hb_shapers_get () |
Behdad Esfahbod | 747d256 | 2018-08-12 17:32:10 -0700 | [diff] [blame] | 100 | { |
| 101 | return static_shapers.get_unconst (); |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 102 | } |