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 "../OT/Layout/Common/Coverage.hh" 29 30 #ifndef GRAPH_COVERAGE_GRAPH_HH 31 #define GRAPH_COVERAGE_GRAPH_HH 32 33 namespace graph { 34 35 struct CoverageFormat1 : public OT::Layout::Common::CoverageFormat1_3<SmallTypes> 36 { sanitizegraph::CoverageFormat137 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::Layout::Common::CoverageFormat1_3<SmallTypes>::min_size; 41 if (vertex_len < min_size) return false; 42 hb_barrier (); 43 return vertex_len >= min_size + glyphArray.get_size () - glyphArray.len.get_size (); 44 } 45 }; 46 47 struct CoverageFormat2 : public OT::Layout::Common::CoverageFormat2_4<SmallTypes> 48 { sanitizegraph::CoverageFormat249 bool sanitize (graph_t::vertex_t& vertex) const 50 { 51 int64_t vertex_len = vertex.obj.tail - vertex.obj.head; 52 constexpr unsigned min_size = OT::Layout::Common::CoverageFormat2_4<SmallTypes>::min_size; 53 if (vertex_len < min_size) return false; 54 hb_barrier (); 55 return vertex_len >= min_size + rangeRecord.get_size () - rangeRecord.len.get_size (); 56 } 57 }; 58 59 struct Coverage : public OT::Layout::Common::Coverage 60 { clone_coveragegraph::Coverage61 static Coverage* clone_coverage (gsubgpos_graph_context_t& c, 62 unsigned coverage_id, 63 unsigned new_parent_id, 64 unsigned link_position, 65 unsigned start, unsigned end) 66 67 { 68 unsigned coverage_size = c.graph.vertices_[coverage_id].table_size (); 69 auto& coverage_v = c.graph.vertices_[coverage_id]; 70 Coverage* coverage_table = (Coverage*) coverage_v.obj.head; 71 if (!coverage_table || !coverage_table->sanitize (coverage_v)) 72 return nullptr; 73 74 auto new_coverage = 75 + hb_zip (coverage_table->iter (), hb_range ()) 76 | hb_filter ([&] (hb_pair_t<unsigned, unsigned> p) { 77 return p.second >= start && p.second < end; 78 }) 79 | hb_map_retains_sorting (hb_first) 80 ; 81 82 return add_coverage (c, new_parent_id, link_position, new_coverage, coverage_size); 83 } 84 85 template<typename It> add_coveragegraph::Coverage86 static Coverage* add_coverage (gsubgpos_graph_context_t& c, 87 unsigned parent_id, 88 unsigned link_position, 89 It glyphs, 90 unsigned max_size) 91 { 92 unsigned coverage_prime_id = c.graph.new_node (nullptr, nullptr); 93 auto& coverage_prime_vertex = c.graph.vertices_[coverage_prime_id]; 94 if (!make_coverage (c, glyphs, coverage_prime_id, max_size)) 95 return nullptr; 96 97 auto* coverage_link = c.graph.vertices_[parent_id].obj.real_links.push (); 98 coverage_link->width = SmallTypes::size; 99 coverage_link->objidx = coverage_prime_id; 100 coverage_link->position = link_position; 101 coverage_prime_vertex.add_parent (parent_id); 102 103 return (Coverage*) coverage_prime_vertex.obj.head; 104 } 105 106 template<typename It> make_coveragegraph::Coverage107 static bool make_coverage (gsubgpos_graph_context_t& c, 108 It glyphs, 109 unsigned dest_obj, 110 unsigned max_size) 111 { 112 char* buffer = (char*) hb_calloc (1, max_size); 113 hb_serialize_context_t serializer (buffer, max_size); 114 OT::Layout::Common::Coverage_serialize (&serializer, glyphs); 115 serializer.end_serialize (); 116 if (serializer.in_error ()) 117 { 118 hb_free (buffer); 119 return false; 120 } 121 122 hb_bytes_t coverage_copy = serializer.copy_bytes (); 123 if (!coverage_copy.arrayZ) return false; 124 // Give ownership to the context, it will cleanup the buffer. 125 if (!c.add_buffer ((char *) coverage_copy.arrayZ)) 126 { 127 hb_free ((char *) coverage_copy.arrayZ); 128 return false; 129 } 130 131 auto& obj = c.graph.vertices_[dest_obj].obj; 132 obj.head = (char *) coverage_copy.arrayZ; 133 obj.tail = obj.head + coverage_copy.length; 134 135 hb_free (buffer); 136 return true; 137 } 138 sanitizegraph::Coverage139 bool sanitize (graph_t::vertex_t& vertex) const 140 { 141 int64_t vertex_len = vertex.obj.tail - vertex.obj.head; 142 if (vertex_len < OT::Layout::Common::Coverage::min_size) return false; 143 hb_barrier (); 144 switch (u.format) 145 { 146 case 1: return ((CoverageFormat1*)this)->sanitize (vertex); 147 case 2: return ((CoverageFormat2*)this)->sanitize (vertex); 148 #ifndef HB_NO_BEYOND_64K 149 // Not currently supported 150 case 3: 151 case 4: 152 #endif 153 default: return false; 154 } 155 } 156 }; 157 158 159 } 160 161 #endif // GRAPH_COVERAGE_GRAPH_HH 162