/src/harfbuzz/src/hb-ot-post-table-v2subset.hh
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2021 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 | | */ |
25 | | |
26 | | #ifndef HB_OT_POST_TABLE_V2SUBSET_HH |
27 | | #define HB_OT_POST_TABLE_V2SUBSET_HH |
28 | | |
29 | | #include "hb-open-type.hh" |
30 | | #include "hb-ot-post-table.hh" |
31 | | |
32 | | /* |
33 | | * post -- PostScript |
34 | | * https://docs.microsoft.com/en-us/typography/opentype/spec/post |
35 | | */ |
36 | | |
37 | | namespace OT { |
38 | | template<typename Iterator> |
39 | | HB_INTERNAL bool postV2Tail::serialize (hb_serialize_context_t *c, |
40 | | Iterator it, |
41 | | const void* _post) const |
42 | 185 | { |
43 | 185 | TRACE_SERIALIZE (this); |
44 | 185 | auto *out = c->start_embed (this); |
45 | 185 | if (unlikely (!c->check_success (out))) return_trace (false); |
46 | 185 | if (!out->glyphNameIndex.serialize (c, + it |
47 | 185 | | hb_map (hb_second))) |
48 | 68 | return_trace (false); |
49 | | |
50 | 117 | hb_set_t copied_indices; |
51 | 117 | for (const auto& _ : + it ) |
52 | 560k | { |
53 | 560k | unsigned glyph_id = _.first; |
54 | 560k | unsigned new_index = _.second; |
55 | | |
56 | 560k | if (new_index < 258) continue; |
57 | 2.38k | if (copied_indices.has (new_index)) continue; |
58 | 819 | copied_indices.add (new_index); |
59 | | |
60 | 819 | hb_bytes_t s = reinterpret_cast<const post::accelerator_t*> (_post)->find_glyph_name (glyph_id); |
61 | 819 | HBUINT8 *o = c->allocate_size<HBUINT8> (HBUINT8::static_size * (s.length + 1)); |
62 | 819 | if (unlikely (!o)) return_trace (false); |
63 | 819 | if (!c->check_assign (o[0], s.length, HB_SERIALIZE_ERROR_INT_OVERFLOW)) return_trace (false); |
64 | 819 | hb_memcpy (o+1, s.arrayZ, HBUINT8::static_size * s.length); |
65 | 819 | } |
66 | | |
67 | 117 | return_trace (true); |
68 | 117 | } |
69 | | |
70 | | HB_INTERNAL bool postV2Tail::subset (hb_subset_context_t *c) const |
71 | 185 | { |
72 | 185 | TRACE_SUBSET (this); |
73 | | |
74 | 185 | const hb_map_t &reverse_glyph_map = *c->plan->reverse_glyph_map; |
75 | 185 | unsigned num_glyphs = c->plan->num_output_glyphs (); |
76 | 185 | hb_map_t old_new_index_map, old_gid_new_index_map; |
77 | 185 | unsigned i = 0; |
78 | | |
79 | 185 | post::accelerator_t _post (c->plan->source); |
80 | | |
81 | 185 | hb_hashmap_t<hb_bytes_t, uint32_t, true> glyph_name_to_new_index; |
82 | | |
83 | 185 | old_new_index_map.alloc (num_glyphs); |
84 | 185 | old_gid_new_index_map.alloc (num_glyphs); |
85 | 185 | glyph_name_to_new_index.alloc (num_glyphs); |
86 | | |
87 | 185 | for (auto _ : c->plan->new_to_old_gid_list) |
88 | 4.05k | { |
89 | 4.05k | hb_codepoint_t old_gid = _.second; |
90 | 4.05k | unsigned old_index = glyphNameIndex[old_gid]; |
91 | | |
92 | 4.05k | unsigned new_index; |
93 | 4.05k | const uint32_t *new_index2; |
94 | 4.05k | if (old_index <= 257) |
95 | 1.49k | new_index = old_index; |
96 | 2.56k | else if (old_new_index_map.has (old_index, &new_index2)) |
97 | 254 | new_index = *new_index2; |
98 | 2.31k | else |
99 | 2.31k | { |
100 | 2.31k | hb_bytes_t s = _post.find_glyph_name (old_gid); |
101 | 2.31k | new_index = glyph_name_to_new_index.get (s); |
102 | 2.31k | if (new_index == (unsigned)-1) |
103 | 939 | { |
104 | 939 | int standard_glyph_index = -1; |
105 | 237k | for (unsigned i = 0; i < format1_names_length; i++) |
106 | 236k | { |
107 | 236k | if (s == format1_names (i)) |
108 | 32 | { |
109 | 32 | standard_glyph_index = i; |
110 | 32 | break; |
111 | 32 | } |
112 | 236k | } |
113 | | |
114 | 939 | if (standard_glyph_index == -1) |
115 | 907 | { |
116 | 907 | new_index = 258 + i; |
117 | 907 | i++; |
118 | 907 | } |
119 | 32 | else |
120 | 32 | { new_index = standard_glyph_index; } |
121 | 939 | glyph_name_to_new_index.set (s, new_index); |
122 | 939 | } |
123 | 2.31k | old_new_index_map.set (old_index, new_index); |
124 | 2.31k | } |
125 | 4.05k | old_gid_new_index_map.set (old_gid, new_index); |
126 | 4.05k | } |
127 | | |
128 | 185 | if (old_gid_new_index_map.in_error()) |
129 | 0 | return_trace (false); |
130 | | |
131 | 185 | auto index_iter = |
132 | 185 | + hb_range (num_glyphs) |
133 | 185 | | hb_map_retains_sorting ([&](hb_codepoint_t new_gid) |
134 | 1.12M | { |
135 | 1.12M | hb_codepoint_t *old_gid; |
136 | | /* use 0 for retain-gid holes, which refers to the name .notdef, |
137 | | * as the glyphNameIndex entry for that glyph ID."*/ |
138 | 1.12M | unsigned new_index = 0; |
139 | 1.12M | if (reverse_glyph_map.has (new_gid, &old_gid)) { |
140 | 7.61k | new_index = old_gid_new_index_map.get (*old_gid); |
141 | 7.61k | return hb_pair_t<unsigned, unsigned> (*old_gid, new_index); |
142 | 7.61k | } |
143 | 1.11M | return hb_pair_t<unsigned, unsigned> (new_gid, new_index); |
144 | 1.12M | }) |
145 | 185 | ; |
146 | | |
147 | 185 | return_trace (serialize (c->serializer, index_iter, &_post)); |
148 | 185 | } |
149 | | |
150 | | } /* namespace OT */ |
151 | | #endif /* HB_OT_POST_TABLE_V2SUBSET_HH */ |