• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016  Google, Inc.
3  * Copyright © 2018  Ebrahim Byagowi
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Google Author(s): Sascha Brawer
26  */
27 
28 #ifndef HB_OT_COLOR_CPAL_TABLE_HH
29 #define HB_OT_COLOR_CPAL_TABLE_HH
30 
31 #include "hb-open-type.hh"
32 #include "hb-ot-color.h"
33 #include "hb-ot-name.h"
34 
35 
36 /*
37  * CPAL -- Color Palette
38  * https://docs.microsoft.com/en-us/typography/opentype/spec/cpal
39  */
40 #define HB_OT_TAG_CPAL HB_TAG('C','P','A','L')
41 
42 namespace OT {
43 
44 
45 struct CPALV1Tail
46 {
47   friend struct CPAL;
48 
49   private:
get_palette_flagsOT::CPALV1Tail50   hb_ot_color_palette_flags_t get_palette_flags (const void *base,
51 						 unsigned int palette_index,
52 						 unsigned int palette_count) const
53   {
54     if (!paletteFlagsZ) return HB_OT_COLOR_PALETTE_FLAG_DEFAULT;
55     return (hb_ot_color_palette_flags_t) (uint32_t)
56 	   (base+paletteFlagsZ).as_array (palette_count)[palette_index];
57   }
58 
get_palette_name_idOT::CPALV1Tail59   hb_ot_name_id_t get_palette_name_id (const void *base,
60 				       unsigned int palette_index,
61 				       unsigned int palette_count) const
62   {
63     if (!paletteLabelsZ) return HB_OT_NAME_ID_INVALID;
64     return (base+paletteLabelsZ).as_array (palette_count)[palette_index];
65   }
66 
get_color_name_idOT::CPALV1Tail67   hb_ot_name_id_t get_color_name_id (const void *base,
68 				     unsigned int color_index,
69 				     unsigned int color_count) const
70   {
71     if (!colorLabelsZ) return HB_OT_NAME_ID_INVALID;
72     return (base+colorLabelsZ).as_array (color_count)[color_index];
73   }
74 
75   public:
serializeOT::CPALV1Tail76   bool serialize (hb_serialize_context_t *c,
77                   unsigned palette_count,
78                   unsigned color_count,
79                   const void *base,
80                   const hb_map_t *color_index_map) const
81   {
82     TRACE_SERIALIZE (this);
83     auto *out = c->allocate_size<CPALV1Tail> (static_size);
84     if (unlikely (!out)) return_trace (false);
85 
86     out->paletteFlagsZ = 0;
87     if (paletteFlagsZ)
88       out->paletteFlagsZ.serialize_copy (c, paletteFlagsZ, base, 0, hb_serialize_context_t::Head, palette_count);
89 
90     out->paletteLabelsZ = 0;
91     if (paletteLabelsZ)
92       out->paletteLabelsZ.serialize_copy (c, paletteLabelsZ, base, 0, hb_serialize_context_t::Head, palette_count);
93 
94     const hb_array_t<const NameID> colorLabels = (base+colorLabelsZ).as_array (color_count);
95     if (colorLabelsZ)
96     {
97       c->push ();
98       for (const auto _ : colorLabels)
99       {
100 	const hb_codepoint_t *v;
101         if (!color_index_map->has (_, &v)) continue;
102         NameID new_color_idx;
103 	new_color_idx = *v;
104         if (!c->copy<NameID> (new_color_idx))
105         {
106           c->pop_discard ();
107           return_trace (false);
108         }
109       }
110       c->add_link (out->colorLabelsZ, c->pop_pack ());
111     }
112     return_trace (true);
113   }
114 
sanitizeOT::CPALV1Tail115   bool sanitize (hb_sanitize_context_t *c,
116 		 const void *base,
117 		 unsigned int palette_count,
118 		 unsigned int color_count) const
119   {
120     TRACE_SANITIZE (this);
121     return_trace (c->check_struct (this) &&
122 		  (!paletteFlagsZ  || (base+paletteFlagsZ).sanitize (c, palette_count)) &&
123 		  (!paletteLabelsZ || (base+paletteLabelsZ).sanitize (c, palette_count)) &&
124 		  (!colorLabelsZ   || (base+colorLabelsZ).sanitize (c, color_count)));
125   }
126 
127   protected:
128   // TODO(garretrieger): these offsets can hold nulls so we should not be using non-null offsets
129   //                     here. Currently they are needed since UnsizedArrayOf doesn't define null_size
130   NNOffset32To<UnsizedArrayOf<HBUINT32>>
131 		paletteFlagsZ;		/* Offset from the beginning of CPAL table to
132 					 * the Palette Type Array. Set to 0 if no array
133 					 * is provided. */
134   NNOffset32To<UnsizedArrayOf<NameID>>
135 		paletteLabelsZ;		/* Offset from the beginning of CPAL table to
136 					 * the palette labels array. Set to 0 if no
137 					 * array is provided. */
138   NNOffset32To<UnsizedArrayOf<NameID>>
139 		colorLabelsZ;		/* Offset from the beginning of CPAL table to
140 					 * the color labels array. Set to 0
141 					 * if no array is provided. */
142   public:
143   DEFINE_SIZE_STATIC (12);
144 };
145 
146 typedef HBUINT32 BGRAColor;
147 
148 struct CPAL
149 {
150   static constexpr hb_tag_t tableTag = HB_OT_TAG_CPAL;
151 
has_dataOT::CPAL152   bool has_data () const { return numPalettes; }
153 
get_sizeOT::CPAL154   unsigned int get_size () const
155   { return min_size + numPalettes * sizeof (colorRecordIndicesZ[0]); }
156 
get_palette_countOT::CPAL157   unsigned int get_palette_count () const { return numPalettes; }
get_color_countOT::CPAL158   unsigned int   get_color_count () const { return numColors; }
159 
get_palette_flagsOT::CPAL160   hb_ot_color_palette_flags_t get_palette_flags (unsigned int palette_index) const
161   { return v1 ().get_palette_flags (this, palette_index, numPalettes); }
162 
get_palette_name_idOT::CPAL163   hb_ot_name_id_t get_palette_name_id (unsigned int palette_index) const
164   { return v1 ().get_palette_name_id (this, palette_index, numPalettes); }
165 
get_color_name_idOT::CPAL166   hb_ot_name_id_t get_color_name_id (unsigned int color_index) const
167   { return v1 ().get_color_name_id (this, color_index, numColors); }
168 
get_palette_colorsOT::CPAL169   unsigned int get_palette_colors (unsigned int  palette_index,
170 				   unsigned int  start_offset,
171 				   unsigned int *color_count, /* IN/OUT.  May be NULL. */
172 				   hb_color_t   *colors       /* OUT.     May be NULL. */) const
173   {
174     if (unlikely (palette_index >= numPalettes))
175     {
176       if (color_count) *color_count = 0;
177       return 0;
178     }
179     unsigned int start_index = colorRecordIndicesZ[palette_index];
180     hb_array_t<const BGRAColor> all_colors ((this+colorRecordsZ).arrayZ, numColorRecords);
181     hb_array_t<const BGRAColor> palette_colors = all_colors.sub_array (start_index,
182 								       numColors);
183     if (color_count)
184     {
185       + palette_colors.sub_array (start_offset, color_count)
186       | hb_sink (hb_array (colors, *color_count))
187       ;
188     }
189     return numColors;
190   }
191 
192   private:
v1OT::CPAL193   const CPALV1Tail& v1 () const
194   {
195     if (version == 0) return Null (CPALV1Tail);
196     return StructAfter<CPALV1Tail> (*this);
197   }
198 
199   public:
serializeOT::CPAL200   bool serialize (hb_serialize_context_t *c,
201                   const hb_array_t<const HBUINT16> &color_record_indices,
202                   const hb_array_t<const BGRAColor> &color_records,
203                   const hb_vector_t<unsigned>& first_color_index_for_layer,
204                   const hb_map_t& first_color_to_layer_index,
205                   const hb_set_t &retained_color_indices) const
206   {
207     TRACE_SERIALIZE (this);
208 
209     // TODO(grieger): limit total final size.
210 
211     for (const auto idx : color_record_indices)
212     {
213       hb_codepoint_t layer_index = first_color_to_layer_index[idx];
214 
215       HBUINT16 new_idx;
216       new_idx = layer_index * retained_color_indices.get_population ();
217       if (!c->copy<HBUINT16> (new_idx)) return_trace (false);
218     }
219 
220     c->push ();
221     for (unsigned first_color_index : first_color_index_for_layer)
222     {
223       for (hb_codepoint_t color_index : retained_color_indices)
224       {
225         if (!c->copy<BGRAColor> (color_records[first_color_index + color_index]))
226         {
227           c->pop_discard ();
228           return_trace (false);
229         }
230       }
231     }
232 
233     c->add_link (colorRecordsZ, c->pop_pack ());
234     return_trace (true);
235   }
236 
subsetOT::CPAL237   bool subset (hb_subset_context_t *c) const
238   {
239     TRACE_SUBSET (this);
240     if (!numPalettes) return_trace (false);
241 
242     const hb_map_t *color_index_map = c->plan->colr_palettes;
243     if (color_index_map->is_empty ()) return_trace (false);
244 
245     hb_set_t retained_color_indices;
246     for (const auto _ : color_index_map->keys ())
247     {
248       if (_ == 0xFFFF) continue;
249       retained_color_indices.add (_);
250     }
251     if (retained_color_indices.is_empty ()) return_trace (false);
252 
253     auto *out = c->serializer->start_embed (*this);
254     if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
255 
256 
257     out->version = version;
258     out->numColors = retained_color_indices.get_population ();
259     out->numPalettes = numPalettes;
260 
261     hb_vector_t<unsigned> first_color_index_for_layer;
262     hb_map_t first_color_to_layer_index;
263 
264     const hb_array_t<const HBUINT16> colorRecordIndices = colorRecordIndicesZ.as_array (numPalettes);
265     for (const auto first_color_record_idx : colorRecordIndices)
266     {
267       if (first_color_to_layer_index.has (first_color_record_idx)) continue;
268 
269       first_color_index_for_layer.push (first_color_record_idx);
270       first_color_to_layer_index.set (first_color_record_idx,
271                                       first_color_index_for_layer.length - 1);
272     }
273 
274     out->numColorRecords = first_color_index_for_layer.length
275                            * retained_color_indices.get_population ();
276 
277     const hb_array_t<const BGRAColor> color_records = (this+colorRecordsZ).as_array (numColorRecords);
278     if (!out->serialize (c->serializer,
279                          colorRecordIndices,
280                          color_records,
281                          first_color_index_for_layer,
282                          first_color_to_layer_index,
283                          retained_color_indices))
284       return_trace (false);
285 
286     if (version == 1)
287       return_trace (v1 ().serialize (c->serializer, numPalettes, numColors, this, color_index_map));
288 
289     return_trace (true);
290   }
291 
sanitizeOT::CPAL292   bool sanitize (hb_sanitize_context_t *c) const
293   {
294     TRACE_SANITIZE (this);
295     return_trace (c->check_struct (this) &&
296 		  (this+colorRecordsZ).sanitize (c, numColorRecords) &&
297 		  colorRecordIndicesZ.sanitize (c, numPalettes) &&
298 		  (version == 0 || v1 ().sanitize (c, this, numPalettes, numColors)));
299   }
300 
301   protected:
302   HBUINT16	version;		/* Table version number */
303   /* Version 0 */
304   HBUINT16	numColors;		/* Number of colors in each palette. */
305   HBUINT16	numPalettes;		/* Number of palettes in the table. */
306   HBUINT16	numColorRecords;	/* Total number of color records, combined for
307 					 * all palettes. */
308   NNOffset32To<UnsizedArrayOf<BGRAColor>>
309 		colorRecordsZ;		/* Offset from the beginning of CPAL table to
310 					 * the first ColorRecord. */
311   UnsizedArrayOf<HBUINT16>
312 		colorRecordIndicesZ;	/* Index of each palette’s first color record in
313 					 * the combined color record array. */
314 /*CPALV1Tail	v1;*/
315   public:
316   DEFINE_SIZE_ARRAY (12, colorRecordIndicesZ);
317 };
318 
319 } /* namespace OT */
320 
321 
322 #endif /* HB_OT_COLOR_CPAL_TABLE_HH */
323