xref: /aosp_15_r20/external/harfbuzz_ng/src/hb-subset-accelerator.hh (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
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 #ifndef HB_SUBSET_ACCELERATOR_HH
28 #define HB_SUBSET_ACCELERATOR_HH
29 
30 
31 #include "hb.hh"
32 
33 #include "hb-map.hh"
34 #include "hb-multimap.hh"
35 #include "hb-set.hh"
36 
37 extern HB_INTERNAL hb_user_data_key_t _hb_subset_accelerator_user_data_key;
38 
39 namespace CFF {
40 struct cff_subset_accelerator_t;
41 }
42 
43 namespace OT {
44 struct SubtableUnicodesCache;
45 struct cff1_subset_accelerator_t;
46 struct cff2_subset_accelerator_t;
47 }
48 
49 struct hb_subset_accelerator_t
50 {
user_data_keyhb_subset_accelerator_t51   static hb_user_data_key_t* user_data_key()
52   {
53     return &_hb_subset_accelerator_user_data_key;
54   }
55 
createhb_subset_accelerator_t56   static hb_subset_accelerator_t* create(hb_face_t *source,
57 					 const hb_map_t& unicode_to_gid_,
58 					 const hb_set_t& unicodes_,
59 					 bool has_seac_) {
60     hb_subset_accelerator_t* accel =
61         (hb_subset_accelerator_t*) hb_calloc (1, sizeof(hb_subset_accelerator_t));
62 
63     if (unlikely (!accel)) return accel;
64 
65     new (accel) hb_subset_accelerator_t (source,
66 					 unicode_to_gid_,
67 					 unicodes_,
68 					 has_seac_);
69 
70     return accel;
71   }
72 
destroyhb_subset_accelerator_t73   static void destroy (void* p)
74   {
75     if (!p) return;
76 
77     hb_subset_accelerator_t *accel = (hb_subset_accelerator_t *) p;
78 
79     accel->~hb_subset_accelerator_t ();
80 
81     hb_free (accel);
82   }
83 
hb_subset_accelerator_thb_subset_accelerator_t84   hb_subset_accelerator_t (hb_face_t *source,
85 			   const hb_map_t& unicode_to_gid_,
86 			   const hb_set_t& unicodes_,
87 			   bool has_seac_) :
88     unicode_to_gid(unicode_to_gid_),
89     unicodes(unicodes_),
90     cmap_cache(nullptr),
91     destroy_cmap_cache(nullptr),
92     has_seac(has_seac_),
93     source(hb_face_reference (source))
94   {
95     gid_to_unicodes.alloc (unicode_to_gid.get_population ());
96     for (const auto &_ : unicode_to_gid)
97     {
98       auto unicode = _.first;
99       auto gid = _.second;
100       gid_to_unicodes.add (gid, unicode);
101     }
102   }
103 
104   HB_INTERNAL ~hb_subset_accelerator_t ();
105 
106   // Generic
107 
108   mutable hb_mutex_t sanitized_table_cache_lock;
109   mutable hb_hashmap_t<hb_tag_t, hb::unique_ptr<hb_blob_t>> sanitized_table_cache;
110 
111   hb_map_t unicode_to_gid;
112   hb_multimap_t gid_to_unicodes;
113   hb_set_t unicodes;
114 
115   // cmap
116   const OT::SubtableUnicodesCache* cmap_cache;
117   hb_destroy_func_t destroy_cmap_cache;
118 
119   // CFF
120   bool has_seac;
121 
122   // TODO(garretrieger): cumulative glyf checksum map
123 
in_errorhb_subset_accelerator_t124   bool in_error () const
125   {
126     return unicode_to_gid.in_error () ||
127 	   gid_to_unicodes.in_error () ||
128 	   unicodes.in_error () ||
129 	   sanitized_table_cache.in_error ();
130   }
131 
132   hb_face_t *source;
133 #ifndef HB_NO_SUBSET_CFF
134   // These have to be immediately after source:
135   mutable hb_face_lazy_loader_t<OT::cff1_subset_accelerator_t, 1> cff1_accel;
136   mutable hb_face_lazy_loader_t<OT::cff2_subset_accelerator_t, 2> cff2_accel;
137 #endif
138 };
139 
140 
141 #endif /* HB_SUBSET_ACCELERATOR_HH */
142