1 /* 2 * Copyright © 2022 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): Garret Rieger 25 */ 26 27 #include "graph.hh" 28 #include "../hb-ot-layout-common.hh" 29 30 #ifndef GRAPH_CLASSDEF_GRAPH_HH 31 #define GRAPH_CLASSDEF_GRAPH_HH 32 33 namespace graph { 34 35 struct ClassDefFormat1 : public OT::ClassDefFormat1_3<SmallTypes> 36 { sanitizegraph::ClassDefFormat137 bool sanitize (graph_t::vertex_t& vertex) const 38 { 39 int64_t vertex_len = vertex.obj.tail - vertex.obj.head; 40 constexpr unsigned min_size = OT::ClassDefFormat1_3<SmallTypes>::min_size; 41 if (vertex_len < min_size) return false; 42 return vertex_len >= min_size + classValue.get_size () - classValue.len.get_size (); 43 } 44 }; 45 46 struct ClassDefFormat2 : public OT::ClassDefFormat2_4<SmallTypes> 47 { sanitizegraph::ClassDefFormat248 bool sanitize (graph_t::vertex_t& vertex) const 49 { 50 int64_t vertex_len = vertex.obj.tail - vertex.obj.head; 51 constexpr unsigned min_size = OT::ClassDefFormat2_4<SmallTypes>::min_size; 52 if (vertex_len < min_size) return false; 53 return vertex_len >= min_size + rangeRecord.get_size () - rangeRecord.len.get_size (); 54 } 55 }; 56 57 struct ClassDef : public OT::ClassDef 58 { 59 template<typename It> add_class_defgraph::ClassDef60 static bool add_class_def (gsubgpos_graph_context_t& c, 61 unsigned parent_id, 62 unsigned link_position, 63 It glyph_and_class, 64 unsigned max_size) 65 { 66 unsigned class_def_prime_id = c.graph.new_node (nullptr, nullptr); 67 auto& class_def_prime_vertex = c.graph.vertices_[class_def_prime_id]; 68 if (!make_class_def (c, glyph_and_class, class_def_prime_id, max_size)) 69 return false; 70 71 auto* class_def_link = c.graph.vertices_[parent_id].obj.real_links.push (); 72 class_def_link->width = SmallTypes::size; 73 class_def_link->objidx = class_def_prime_id; 74 class_def_link->position = link_position; 75 class_def_prime_vertex.parents.push (parent_id); 76 77 return true; 78 } 79 80 template<typename It> make_class_defgraph::ClassDef81 static bool make_class_def (gsubgpos_graph_context_t& c, 82 It glyph_and_class, 83 unsigned dest_obj, 84 unsigned max_size) 85 { 86 char* buffer = (char*) hb_calloc (1, max_size); 87 hb_serialize_context_t serializer (buffer, max_size); 88 OT::ClassDef_serialize (&serializer, glyph_and_class); 89 serializer.end_serialize (); 90 if (serializer.in_error ()) 91 { 92 hb_free (buffer); 93 return false; 94 } 95 96 hb_bytes_t class_def_copy = serializer.copy_bytes (); 97 c.add_buffer ((char *) class_def_copy.arrayZ); // Give ownership to the context, it will cleanup the buffer. 98 99 auto& obj = c.graph.vertices_[dest_obj].obj; 100 obj.head = (char *) class_def_copy.arrayZ; 101 obj.tail = obj.head + class_def_copy.length; 102 103 hb_free (buffer); 104 return true; 105 } 106 sanitizegraph::ClassDef107 bool sanitize (graph_t::vertex_t& vertex) const 108 { 109 int64_t vertex_len = vertex.obj.tail - vertex.obj.head; 110 if (vertex_len < OT::ClassDef::min_size) return false; 111 switch (u.format) 112 { 113 case 1: return ((ClassDefFormat1*)this)->sanitize (vertex); 114 case 2: return ((ClassDefFormat2*)this)->sanitize (vertex); 115 #ifndef HB_NO_BEYOND_64K 116 // Not currently supported 117 case 3: 118 case 4: 119 #endif 120 default: return false; 121 } 122 } 123 }; 124 125 126 struct class_def_size_estimator_t 127 { 128 template<typename It> class_def_size_estimator_tgraph::class_def_size_estimator_t129 class_def_size_estimator_t (It glyph_and_class) 130 : gids_consecutive (true), num_ranges_per_class (), glyphs_per_class () 131 { 132 unsigned last_gid = (unsigned) -1; 133 for (auto p : + glyph_and_class) 134 { 135 unsigned gid = p.first; 136 unsigned klass = p.second; 137 138 if (last_gid != (unsigned) -1 && gid != last_gid + 1) 139 gids_consecutive = false; 140 last_gid = gid; 141 142 hb_set_t* glyphs; 143 if (glyphs_per_class.has (klass, &glyphs) && glyphs) { 144 glyphs->add (gid); 145 continue; 146 } 147 148 hb_set_t new_glyphs; 149 new_glyphs.add (gid); 150 glyphs_per_class.set (klass, std::move (new_glyphs)); 151 } 152 153 if (in_error ()) return; 154 155 for (unsigned klass : glyphs_per_class.keys ()) 156 { 157 if (!klass) continue; // class 0 doesn't get encoded. 158 159 const hb_set_t& glyphs = glyphs_per_class.get (klass); 160 hb_codepoint_t start = HB_SET_VALUE_INVALID; 161 hb_codepoint_t end = HB_SET_VALUE_INVALID; 162 163 unsigned count = 0; 164 while (glyphs.next_range (&start, &end)) 165 count++; 166 167 num_ranges_per_class.set (klass, count); 168 } 169 } 170 171 // Incremental increase in the Coverage and ClassDef table size 172 // (worst case) if all glyphs associated with 'klass' were added. incremental_coverage_sizegraph::class_def_size_estimator_t173 unsigned incremental_coverage_size (unsigned klass) const 174 { 175 // Coverage takes 2 bytes per glyph worst case, 176 return 2 * glyphs_per_class.get (klass).get_population (); 177 } 178 179 // Incremental increase in the Coverage and ClassDef table size 180 // (worst case) if all glyphs associated with 'klass' were added. incremental_class_def_sizegraph::class_def_size_estimator_t181 unsigned incremental_class_def_size (unsigned klass) const 182 { 183 // ClassDef takes 6 bytes per range 184 unsigned class_def_2_size = 6 * num_ranges_per_class.get (klass); 185 if (gids_consecutive) 186 { 187 // ClassDef1 takes 2 bytes per glyph, but only can be used 188 // when gids are consecutive. 189 return hb_min (2 * glyphs_per_class.get (klass).get_population (), class_def_2_size); 190 } 191 192 return class_def_2_size; 193 } 194 in_errorgraph::class_def_size_estimator_t195 bool in_error () 196 { 197 if (num_ranges_per_class.in_error ()) return true; 198 if (glyphs_per_class.in_error ()) return true; 199 200 for (const hb_set_t& s : glyphs_per_class.values ()) 201 { 202 if (s.in_error ()) return true; 203 } 204 return false; 205 } 206 207 private: 208 bool gids_consecutive; 209 hb_hashmap_t<unsigned, unsigned> num_ranges_per_class; 210 hb_hashmap_t<unsigned, hb_set_t> glyphs_per_class; 211 }; 212 213 214 } 215 216 #endif // GRAPH_CLASSDEF_GRAPH_HH 217