Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / src / hb-ot-color-colr-table.hh
1 /*
2  * Copyright © 2018  Ebrahim Byagowi
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 #ifndef HB_OT_COLOR_COLR_TABLE_HH
26 #define HB_OT_COLOR_COLR_TABLE_HH
27
28 #include "hb-open-type-private.hh"
29
30 /*
31  * Color Palette
32  * http://www.microsoft.com/typography/otspec/colr.htm
33  */
34
35 #define HB_OT_TAG_COLR HB_TAG('C','O','L','R')
36
37 namespace OT {
38
39
40 struct LayerRecord
41 {
42   friend struct COLR;
43
44   inline bool sanitize (hb_sanitize_context_t *c) const
45   {
46     TRACE_SANITIZE (this);
47     return_trace (c->check_struct (this));
48   }
49
50   protected:
51   GlyphID gID;                  /* Glyph ID of layer glyph */
52   HBUINT16 paletteIndex;        /* Index value to use with a selected color palette */
53   public:
54   DEFINE_SIZE_STATIC (4);
55 };
56
57 struct BaseGlyphRecord
58 {
59   friend struct COLR;
60
61   inline bool sanitize (hb_sanitize_context_t *c) const
62   {
63     TRACE_SANITIZE (this);
64     return_trace (c->check_struct (this));
65   }
66
67   protected:
68   GlyphID gID;                  /* Glyph ID of reference glyph */
69   HBUINT16 firstLayerIndex;     /* Index to the layer record */
70   HBUINT16 numLayers;           /* Number of color layers associated with this glyph */
71   public:
72   DEFINE_SIZE_STATIC (6);
73 };
74
75 struct COLR
76 {
77   static const hb_tag_t tableTag = HB_OT_TAG_COLR;
78
79   inline bool sanitize (hb_sanitize_context_t *c) const
80   {
81     TRACE_SANITIZE (this);
82     if (!(c->check_struct (this) &&
83         c->check_array ((const void*) &layerRecordsOffsetZ, sizeof (LayerRecord), numLayerRecords) &&
84         c->check_array ((const void*) &baseGlyphRecordsZ, sizeof (BaseGlyphRecord), numBaseGlyphRecords)))
85       return_trace (false);
86
87     const BaseGlyphRecord* base_glyph_records = &baseGlyphRecordsZ (this);
88     for (unsigned int i = 0; i < numBaseGlyphRecords; ++i)
89       if (base_glyph_records[i].firstLayerIndex +
90           base_glyph_records[i].numLayers > numLayerRecords)
91         return_trace (false);
92
93     return_trace (true);
94   }
95
96   inline bool get_base_glyph_record (
97     hb_codepoint_t glyph_id, unsigned int &first_layer, unsigned int &num_layers) const
98   {
99     const BaseGlyphRecord* base_glyph_records = &baseGlyphRecordsZ (this);
100     unsigned int min = 0, max = numBaseGlyphRecords - 1;
101     while (min <= max)
102     {
103       unsigned int mid = (min + max) / 2;
104       hb_codepoint_t gID = base_glyph_records[mid].gID;
105       if (gID > glyph_id)
106         max = mid - 1;
107       else if (gID < glyph_id)
108         min = mid + 1;
109       else
110       {
111         first_layer = base_glyph_records[mid].firstLayerIndex;
112         num_layers = base_glyph_records[mid].numLayers;
113         return true;
114       }
115     }
116     return false;
117   }
118
119   inline void get_layer_record (int layer,
120     hb_codepoint_t &glyph_id, unsigned int &palette_index) const
121   {
122     const LayerRecord* records = &layerRecordsOffsetZ (this);
123     glyph_id = records[layer].gID;
124     palette_index = records[layer].paletteIndex;
125   }
126
127   protected:
128   HBUINT16      version;                /* Table version number */
129   HBUINT16      numBaseGlyphRecords;    /* Number of Base Glyph Records */
130   LOffsetTo<BaseGlyphRecord>
131                 baseGlyphRecordsZ;      /* Offset to Base Glyph records. */
132   LOffsetTo<LayerRecord>
133                 layerRecordsOffsetZ;    /* Offset to Layer Records */
134   HBUINT16      numLayerRecords;        /* Number of Layer Records */
135   public:
136   DEFINE_SIZE_STATIC (14);
137 };
138
139 } /* namespace OT */
140
141
142 #endif /* HB_OT_COLOR_COLR_TABLE_HH */