Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | bfa8716 | 2018-02-10 13:37:28 -0600 | [diff] [blame] | 2 | * Copyright © 2018 Google, Inc. |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 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 | * |
Rod Sheeter | d463e9f | 2018-02-14 15:04:15 -0800 | [diff] [blame] | 24 | * Google Author(s): Garret Rieger, Rod Sheeter, Behdad Esfahbod |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 25 | */ |
| 26 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 27 | #include "hb.hh" |
| 28 | #include "hb-open-type.hh" |
Rod Sheeter | 0859a00 | 2018-02-07 15:59:36 -0800 | [diff] [blame] | 29 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 30 | #include "hb-subset.hh" |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 31 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 32 | #include "hb-open-file.hh" |
Rod Sheeter | 0859a00 | 2018-02-07 15:59:36 -0800 | [diff] [blame] | 33 | #include "hb-ot-cmap-table.hh" |
Garret Rieger | 6a45e5d | 2018-02-06 16:04:09 -0800 | [diff] [blame] | 34 | #include "hb-ot-glyf-table.hh" |
Garret Rieger | fe42862 | 2018-02-21 14:18:49 -0800 | [diff] [blame] | 35 | #include "hb-ot-hdmx-table.hh" |
Garret Rieger | 83e1ef9 | 2018-02-12 11:22:32 -0800 | [diff] [blame] | 36 | #include "hb-ot-head-table.hh" |
Rod Sheeter | fa87770 | 2018-02-14 14:16:25 -0800 | [diff] [blame] | 37 | #include "hb-ot-hhea-table.hh" |
| 38 | #include "hb-ot-hmtx-table.hh" |
Garret Rieger | 83e1ef9 | 2018-02-12 11:22:32 -0800 | [diff] [blame] | 39 | #include "hb-ot-maxp-table.hh" |
ckitagawa | 43b6c86 | 2020-01-09 10:44:20 -0500 | [diff] [blame] | 40 | #include "hb-ot-color-sbix-table.hh" |
ckitagawa | ed857c4 | 2020-01-24 08:52:23 -0500 | [diff] [blame] | 41 | #include "hb-ot-color-colr-table.hh" |
Garret Rieger | 2490438 | 2018-02-12 17:31:01 -0800 | [diff] [blame] | 42 | #include "hb-ot-os2-table.hh" |
Garret Rieger | dd10769 | 2018-03-06 18:33:39 -0800 | [diff] [blame] | 43 | #include "hb-ot-post-table.hh" |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 44 | #include "hb-ot-cff1-table.hh" |
Michiharu Ariza | 3a61c3e | 2018-07-18 14:17:52 -0700 | [diff] [blame] | 45 | #include "hb-ot-cff2-table.hh" |
Michiharu Ariza | f212c05 | 2018-09-20 09:45:43 -0700 | [diff] [blame] | 46 | #include "hb-ot-vorg-table.hh" |
Qunxin Liu | 408c1da | 2019-03-29 10:34:32 -0700 | [diff] [blame] | 47 | #include "hb-ot-name-table.hh" |
ckitagawa | e128f80 | 2020-01-21 13:35:43 -0500 | [diff] [blame] | 48 | #include "hb-ot-color-cbdt-table.hh" |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 49 | #include "hb-ot-layout-gsub-table.hh" |
| 50 | #include "hb-ot-layout-gpos-table.hh" |
Michiharu Ariza | 2c5ed7f | 2019-02-26 11:11:50 -0800 | [diff] [blame] | 51 | #include "hb-ot-var-gvar-table.hh" |
Michiharu Ariza | 09df17e | 2019-02-20 15:48:29 -0800 | [diff] [blame] | 52 | #include "hb-ot-var-hvar-table.hh" |
Garret Rieger | 6a45e5d | 2018-02-06 16:04:09 -0800 | [diff] [blame] | 53 | |
Behdad Esfahbod | 2da0d87 | 2018-02-07 12:44:35 -0500 | [diff] [blame] | 54 | |
Ebrahim Byagowi | 4301671 | 2020-02-05 15:02:20 +0330 | [diff] [blame] | 55 | static unsigned |
| 56 | _plan_estimate_subset_table_size (hb_subset_plan_t *plan, unsigned table_len) |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 57 | { |
Ebrahim Byagowi | 4301671 | 2020-02-05 15:02:20 +0330 | [diff] [blame] | 58 | unsigned src_glyphs = plan->source->get_num_glyphs (); |
| 59 | unsigned dst_glyphs = plan->glyphset ()->get_population (); |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 60 | |
Behdad Esfahbod | 5f17dbc | 2018-09-07 10:24:22 -0400 | [diff] [blame] | 61 | if (unlikely (!src_glyphs)) |
| 62 | return 512 + table_len; |
| 63 | |
Ebrahim Byagowi | 4301671 | 2020-02-05 15:02:20 +0330 | [diff] [blame] | 64 | return 512 + (unsigned) (table_len * sqrt ((double) dst_glyphs / src_glyphs)); |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 67 | template<typename TableType> |
| 68 | static bool |
Ebrahim Byagowi | 5b43603 | 2020-02-04 21:29:19 +0330 | [diff] [blame] | 69 | _subset (hb_subset_plan_t *plan) |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 70 | { |
Qunxin Liu | 209491f | 2019-05-29 14:03:17 -0700 | [diff] [blame] | 71 | bool result = false; |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 72 | hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source); |
| 73 | const TableType *table = source_blob->as<TableType> (); |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 74 | |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 75 | hb_tag_t tag = TableType::tableTag; |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 76 | if (source_blob->data) |
| 77 | { |
Behdad Esfahbod | 3a4e5dd | 2018-10-29 18:05:25 -0700 | [diff] [blame] | 78 | hb_vector_t<char> buf; |
Behdad Esfahbod | 521262b | 2019-05-07 11:08:08 -0700 | [diff] [blame] | 79 | /* TODO Not all tables are glyph-related. 'name' table size for example should not be |
| 80 | * affected by number of glyphs. Accommodate that. */ |
Ebrahim Byagowi | 4301671 | 2020-02-05 15:02:20 +0330 | [diff] [blame] | 81 | unsigned buf_size = _plan_estimate_subset_table_size (plan, source_blob->length); |
| 82 | DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c initial estimated table size: %u bytes.", HB_UNTAG (tag), buf_size); |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 83 | if (unlikely (!buf.alloc (buf_size))) |
| 84 | { |
Ebrahim Byagowi | 4301671 | 2020-02-05 15:02:20 +0330 | [diff] [blame] | 85 | DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size); |
Ebrahim Byagowi | 28aba78 | 2019-08-23 16:47:15 +0430 | [diff] [blame] | 86 | hb_blob_destroy (source_blob); |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | retry: |
Behdad Esfahbod | 861bc75 | 2018-11-29 14:34:44 -0500 | [diff] [blame] | 90 | hb_serialize_context_t serializer ((void *) buf, buf_size); |
Behdad Esfahbod | 2e675cc | 2019-04-01 16:45:50 -0700 | [diff] [blame] | 91 | serializer.start_serialize<TableType> (); |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 92 | hb_subset_context_t c (plan, &serializer); |
Behdad Esfahbod | 6977a95 | 2019-04-27 10:05:25 -0700 | [diff] [blame] | 93 | bool needed = table->subset (&c); |
Behdad Esfahbod | fe05e48 | 2019-03-30 15:06:25 -0700 | [diff] [blame] | 94 | if (serializer.ran_out_of_room) |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 95 | { |
| 96 | buf_size += (buf_size >> 1) + 32; |
Ebrahim Byagowi | 4301671 | 2020-02-05 15:02:20 +0330 | [diff] [blame] | 97 | DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.", HB_UNTAG (tag), buf_size); |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 98 | if (unlikely (!buf.alloc (buf_size))) |
| 99 | { |
Ebrahim Byagowi | 4301671 | 2020-02-05 15:02:20 +0330 | [diff] [blame] | 100 | DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.", HB_UNTAG (tag), buf_size); |
Ebrahim Byagowi | 28aba78 | 2019-08-23 16:47:15 +0430 | [diff] [blame] | 101 | hb_blob_destroy (source_blob); |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | goto retry; |
| 105 | } |
Qunxin Liu | 19afd25 | 2019-04-24 14:02:29 -0700 | [diff] [blame] | 106 | serializer.end_serialize (); |
Behdad Esfahbod | 2b051e7 | 2019-04-27 10:01:11 -0700 | [diff] [blame] | 107 | |
Behdad Esfahbod | 6977a95 | 2019-04-27 10:05:25 -0700 | [diff] [blame] | 108 | result = !serializer.in_error (); |
Behdad Esfahbod | fe05e48 | 2019-03-30 15:06:25 -0700 | [diff] [blame] | 109 | |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 110 | if (result) |
| 111 | { |
Behdad Esfahbod | 6977a95 | 2019-04-27 10:05:25 -0700 | [diff] [blame] | 112 | if (needed) |
| 113 | { |
| 114 | hb_blob_t *dest_blob = serializer.copy_blob (); |
Ebrahim Byagowi | 4301671 | 2020-02-05 15:02:20 +0330 | [diff] [blame] | 115 | DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c final subset table size: %u bytes.", HB_UNTAG (tag), dest_blob->length); |
Behdad Esfahbod | 6977a95 | 2019-04-27 10:05:25 -0700 | [diff] [blame] | 116 | result = c.plan->add_table (tag, dest_blob); |
| 117 | hb_blob_destroy (dest_blob); |
| 118 | } |
| 119 | else |
| 120 | { |
Ebrahim Byagowi | 4301671 | 2020-02-05 15:02:20 +0330 | [diff] [blame] | 121 | DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag)); |
Behdad Esfahbod | 6977a95 | 2019-04-27 10:05:25 -0700 | [diff] [blame] | 122 | } |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | else |
Ebrahim Byagowi | 4301671 | 2020-02-05 15:02:20 +0330 | [diff] [blame] | 126 | DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag)); |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 127 | |
| 128 | hb_blob_destroy (source_blob); |
Ebrahim Byagowi | 4301671 | 2020-02-05 15:02:20 +0330 | [diff] [blame] | 129 | DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset %s", HB_UNTAG (tag), result ? "success" : "FAILED!"); |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 130 | return result; |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Behdad Esfahbod | a981d79 | 2018-02-10 15:17:28 -0600 | [diff] [blame] | 133 | static bool |
Ebrahim Byagowi | b1f6310 | 2020-02-05 00:28:21 +0330 | [diff] [blame] | 134 | _is_table_present (hb_face_t *source, hb_tag_t tag) |
ckitagawa | 0d61926 | 2020-02-04 13:36:51 -0500 | [diff] [blame] | 135 | { |
Ebrahim Byagowi | dcb5dfc | 2020-02-05 15:01:25 +0330 | [diff] [blame] | 136 | hb_tag_t table_tags[32]; |
| 137 | unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags); |
| 138 | while ((hb_face_get_table_tags (source, offset, &num_tables, table_tags), num_tables)) |
Ebrahim Byagowi | b1f6310 | 2020-02-05 00:28:21 +0330 | [diff] [blame] | 139 | { |
| 140 | for (unsigned i = 0; i < num_tables; ++i) |
Ebrahim Byagowi | dcb5dfc | 2020-02-05 15:01:25 +0330 | [diff] [blame] | 141 | if (table_tags[i] == tag) |
Ebrahim Byagowi | b1f6310 | 2020-02-05 00:28:21 +0330 | [diff] [blame] | 142 | return true; |
| 143 | offset += num_tables; |
ckitagawa | 0d61926 | 2020-02-04 13:36:51 -0500 | [diff] [blame] | 144 | } |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | static bool |
| 149 | _should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag) |
| 150 | { |
| 151 | if (plan->drop_tables->has (tag)) |
| 152 | return true; |
| 153 | |
Ebrahim Byagowi | a859333 | 2020-02-05 15:00:25 +0330 | [diff] [blame] | 154 | switch (tag) |
| 155 | { |
| 156 | case HB_TAG ('c','v','a','r'): /* hint table, fallthrough */ |
| 157 | case HB_TAG ('c','v','t',' '): /* hint table, fallthrough */ |
| 158 | case HB_TAG ('f','p','g','m'): /* hint table, fallthrough */ |
| 159 | case HB_TAG ('p','r','e','p'): /* hint table, fallthrough */ |
| 160 | case HB_TAG ('h','d','m','x'): /* hint table, fallthrough */ |
| 161 | case HB_TAG ('V','D','M','X'): /* hint table, fallthrough */ |
| 162 | return plan->drop_hints; |
ckitagawa | 0d61926 | 2020-02-04 13:36:51 -0500 | [diff] [blame] | 163 | |
| 164 | #ifdef HB_NO_SUBSET_LAYOUT |
| 165 | // Drop Layout Tables if requested. |
Ebrahim Byagowi | a859333 | 2020-02-05 15:00:25 +0330 | [diff] [blame] | 166 | case HB_OT_TAG_GDEF: |
| 167 | case HB_OT_TAG_GPOS: |
| 168 | case HB_OT_TAG_GSUB: |
| 169 | case HB_TAG ('m','o','r','x'): |
| 170 | case HB_TAG ('m','o','r','t'): |
| 171 | case HB_TAG ('k','e','r','x'): |
| 172 | case HB_TAG ('k','e','r','n'): |
| 173 | return true; |
ckitagawa | 0d61926 | 2020-02-04 13:36:51 -0500 | [diff] [blame] | 174 | #endif |
| 175 | |
Ebrahim Byagowi | a859333 | 2020-02-05 15:00:25 +0330 | [diff] [blame] | 176 | default: |
| 177 | return false; |
ckitagawa | 0d61926 | 2020-02-04 13:36:51 -0500 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | static bool |
Ebrahim Byagowi | a859333 | 2020-02-05 15:00:25 +0330 | [diff] [blame] | 182 | _subset_table (hb_subset_plan_t *plan, hb_tag_t tag) |
Garret Rieger | aac7d96 | 2018-02-08 18:18:16 -0800 | [diff] [blame] | 183 | { |
Ebrahim Byagowi | a859333 | 2020-02-05 15:00:25 +0330 | [diff] [blame] | 184 | DEBUG_MSG (SUBSET, nullptr, "subset %c%c%c%c", HB_UNTAG (tag)); |
| 185 | switch (tag) |
| 186 | { |
| 187 | case HB_OT_TAG_glyf: return _subset<const OT::glyf> (plan); |
| 188 | case HB_OT_TAG_hdmx: return _subset<const OT::hdmx> (plan); |
| 189 | case HB_OT_TAG_name: return _subset<const OT::name> (plan); |
| 190 | case HB_OT_TAG_head: |
| 191 | if (_is_table_present (plan->source, HB_OT_TAG_glyf) && !_should_drop_table (plan, HB_OT_TAG_glyf)) |
| 192 | return true; /* skip head, handled by glyf */ |
| 193 | return _subset<const OT::head> (plan); |
| 194 | case HB_OT_TAG_hhea: return true; /* skip hhea, handled by hmtx */ |
| 195 | case HB_OT_TAG_hmtx: return _subset<const OT::hmtx> (plan); |
| 196 | case HB_OT_TAG_vhea: return true; /* skip vhea, handled by vmtx */ |
| 197 | case HB_OT_TAG_vmtx: return _subset<const OT::vmtx> (plan); |
| 198 | case HB_OT_TAG_maxp: return _subset<const OT::maxp> (plan); |
| 199 | case HB_OT_TAG_sbix: return _subset<const OT::sbix> (plan); |
| 200 | case HB_OT_TAG_loca: return true; /* skip loca, handled by glyf */ |
| 201 | case HB_OT_TAG_cmap: return _subset<const OT::cmap> (plan); |
| 202 | case HB_OT_TAG_OS2 : return _subset<const OT::OS2 > (plan); |
| 203 | case HB_OT_TAG_post: return _subset<const OT::post> (plan); |
| 204 | case HB_OT_TAG_COLR: return _subset<const OT::COLR> (plan); |
Ebrahim Byagowi | a7f694d | 2020-02-05 16:31:21 +0330 | [diff] [blame] | 205 | case HB_OT_TAG_CBLC: return _subset<const OT::CBLC> (plan); |
| 206 | case HB_OT_TAG_CBDT: return true; /* skip CBDT, handled by CBLC */ |
Behdad Esfahbod | 5ea8ad5 | 2019-05-10 23:36:42 -0700 | [diff] [blame] | 207 | |
| 208 | #ifndef HB_NO_SUBSET_CFF |
Ebrahim Byagowi | a859333 | 2020-02-05 15:00:25 +0330 | [diff] [blame] | 209 | case HB_OT_TAG_cff1: return _subset<const OT::cff1> (plan); |
| 210 | case HB_OT_TAG_cff2: return _subset<const OT::cff2> (plan); |
| 211 | case HB_OT_TAG_VORG: return _subset<const OT::VORG> (plan); |
Behdad Esfahbod | 5ea8ad5 | 2019-05-10 23:36:42 -0700 | [diff] [blame] | 212 | #endif |
Behdad Esfahbod | 47e538a | 2019-04-12 22:50:22 -0400 | [diff] [blame] | 213 | |
Behdad Esfahbod | 227d85e | 2019-05-10 23:15:58 -0700 | [diff] [blame] | 214 | #ifndef HB_NO_SUBSET_LAYOUT |
Ebrahim Byagowi | a859333 | 2020-02-05 15:00:25 +0330 | [diff] [blame] | 215 | case HB_OT_TAG_GDEF: return _subset<const OT::GDEF> (plan); |
| 216 | case HB_OT_TAG_GSUB: return _subset<const OT::GSUB> (plan); |
| 217 | case HB_OT_TAG_GPOS: return _subset<const OT::GPOS> (plan); |
| 218 | case HB_OT_TAG_gvar: return _subset<const OT::gvar> (plan); |
| 219 | case HB_OT_TAG_HVAR: return _subset<const OT::HVAR> (plan); |
| 220 | case HB_OT_TAG_VVAR: return _subset<const OT::VVAR> (plan); |
Behdad Esfahbod | 47e538a | 2019-04-12 22:50:22 -0400 | [diff] [blame] | 221 | #endif |
Behdad Esfahbod | a1e5e07 | 2018-09-05 16:24:28 -0700 | [diff] [blame] | 222 | |
Ebrahim Byagowi | a859333 | 2020-02-05 15:00:25 +0330 | [diff] [blame] | 223 | default: |
| 224 | hb_blob_t *source_table = hb_face_reference_table (plan->source, tag); |
| 225 | bool result = plan->add_table (tag, source_table); |
| 226 | hb_blob_destroy (source_table); |
| 227 | return result; |
Garret Rieger | 83e1ef9 | 2018-02-12 11:22:32 -0800 | [diff] [blame] | 228 | } |
Garret Rieger | aac7d96 | 2018-02-08 18:18:16 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 231 | /** |
| 232 | * hb_subset: |
Behdad Esfahbod | 7fd0b61 | 2018-02-07 16:44:52 -0600 | [diff] [blame] | 233 | * @source: font face data to be subset. |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 234 | * @input: input to use for the subsetting. |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 235 | * |
Behdad Esfahbod | f39166f | 2018-08-29 18:09:55 -0700 | [diff] [blame] | 236 | * Subsets a font according to provided input. |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 237 | **/ |
Behdad Esfahbod | af02812 | 2018-02-07 13:07:46 -0500 | [diff] [blame] | 238 | hb_face_t * |
Ebrahim Byagowi | dcb5dfc | 2020-02-05 15:01:25 +0330 | [diff] [blame] | 239 | hb_subset (hb_face_t *source, hb_subset_input_t *input) |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 240 | { |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 241 | if (unlikely (!input || !source)) return hb_face_get_empty (); |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 242 | |
Behdad Esfahbod | f39166f | 2018-08-29 18:09:55 -0700 | [diff] [blame] | 243 | hb_subset_plan_t *plan = hb_subset_plan_create (source, input); |
Garret Rieger | 31ec3c2 | 2018-02-06 15:37:34 -0800 | [diff] [blame] | 244 | |
Ebrahim Byagowi | 41d6e95 | 2019-09-02 01:41:48 +0430 | [diff] [blame] | 245 | hb_set_t tags_set; |
Ebrahim Byagowi | dcb5dfc | 2020-02-05 15:01:25 +0330 | [diff] [blame] | 246 | bool success = true; |
| 247 | hb_tag_t table_tags[32]; |
| 248 | unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags); |
| 249 | while ((hb_face_get_table_tags (source, offset, &num_tables, table_tags), num_tables)) |
| 250 | { |
| 251 | for (unsigned i = 0; i < num_tables; ++i) |
Garret Rieger | aac7d96 | 2018-02-08 18:18:16 -0800 | [diff] [blame] | 252 | { |
| 253 | hb_tag_t tag = table_tags[i]; |
Ebrahim Byagowi | dcb5dfc | 2020-02-05 15:01:25 +0330 | [diff] [blame] | 254 | if (_should_drop_table (plan, tag) && !tags_set.has (tag)) continue; |
Ebrahim Byagowi | 41d6e95 | 2019-09-02 01:41:48 +0430 | [diff] [blame] | 255 | tags_set.add (tag); |
Ebrahim Byagowi | dcb5dfc | 2020-02-05 15:01:25 +0330 | [diff] [blame] | 256 | success = _subset_table (plan, tag); |
| 257 | if (unlikely (!success)) goto end; |
Garret Rieger | aac7d96 | 2018-02-08 18:18:16 -0800 | [diff] [blame] | 258 | } |
Ebrahim Byagowi | dcb5dfc | 2020-02-05 15:01:25 +0330 | [diff] [blame] | 259 | offset += num_tables; |
| 260 | } |
| 261 | end: |
Rod Sheeter | 13193a9 | 2018-02-07 16:09:52 -0800 | [diff] [blame] | 262 | |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 263 | hb_face_t *result = success ? hb_face_reference (plan->dest) : hb_face_get_empty (); |
Garret Rieger | 5df080b | 2018-02-12 10:15:59 -0800 | [diff] [blame] | 264 | hb_subset_plan_destroy (plan); |
Rod Sheeter | fa87770 | 2018-02-14 14:16:25 -0800 | [diff] [blame] | 265 | return result; |
Garret Rieger | a2965f2 | 2018-01-31 14:53:09 -0800 | [diff] [blame] | 266 | } |