summaryrefslogtreecommitdiff
path: root/Build/source/libs/harfbuzz/harfbuzz-src/src/hb-ot-color-cpal-table.hh
blob: e364c8a44d3eda1aa99ad91b6c1eb1f23221744d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright © 2016  Google, Inc.
 * Copyright © 2018  Ebrahim Byagowi
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Google Author(s): Sascha Brawer
 */

#ifndef HB_OT_COLOR_CPAL_TABLE_HH
#define HB_OT_COLOR_CPAL_TABLE_HH

#include "hb-open-type-private.hh"


/*
 * Following parts to be moved to a public header.
 */

/**
 * hb_ot_color_t:
 * ARGB data type for holding color values.
 *
 * Since: REPLACEME
 */
typedef uint32_t hb_ot_color_t;


/**
 * hb_ot_color_palette_flags_t:
 * @HB_OT_COLOR_PALETTE_FLAG_DEFAULT: default indicating that there is nothing special to note about a color palette.
 * @HB_OT_COLOR_PALETTE_FLAG_FOR_LIGHT_BACKGROUND: flag indicating that the color palette is suitable for rendering text on light background.
 * @HB_OT_COLOR_PALETTE_FLAG_FOR_DARK_BACKGROUND: flag indicating that the color palette is suitable for rendering text on dark background.
 *
 * Since: REPLACEME
 */
typedef enum { /*< flags >*/
  HB_OT_COLOR_PALETTE_FLAG_DEFAULT = 0x00000000u,
  HB_OT_COLOR_PALETTE_FLAG_FOR_LIGHT_BACKGROUND = 0x00000001u,
  HB_OT_COLOR_PALETTE_FLAG_FOR_DARK_BACKGROUND = 0x00000002u,
} hb_ot_color_palette_flags_t;

// HB_EXTERN unsigned int
// hb_ot_color_get_palette_count (hb_face_t *face);

// HB_EXTERN unsigned int
// hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette);

// HB_EXTERN hb_ot_color_palette_flags_t
// hb_ot_color_get_palette_flags (hb_face_t *face, unsigned int palette);

// HB_EXTERN unsigned int
// hb_ot_color_get_palette_colors (hb_face_t       *face,
// 				unsigned int     palette, /* default=0 */
// 				unsigned int     start_offset,
// 				unsigned int    *color_count /* IN/OUT */,
// 				hb_ot_color_t   *colors /* OUT */);





/*
 * Color Palette
 * http://www.microsoft.com/typography/otspec/cpal.htm
 */

#define HB_OT_TAG_CPAL HB_TAG('C','P','A','L')

namespace OT {


struct CPALV1Tail
{
  friend struct CPAL;

  inline bool sanitize (hb_sanitize_context_t *c, unsigned int palettes) const
  {
    TRACE_SANITIZE (this);
    return_trace (
      c->check_struct (this) &&
      c->check_array ((const void*) &paletteFlags, sizeof (HBUINT32), palettes) &&
      c->check_array ((const void*) &paletteLabel, sizeof (HBUINT16), palettes) &&
      c->check_array ((const void*) &paletteEntryLabel, sizeof (HBUINT16), palettes));
  }

  private:
  inline hb_ot_color_palette_flags_t
  get_palette_flags (const void *base, unsigned int palette) const
  {
    const HBUINT32* flags = &paletteFlags (base);
    return (hb_ot_color_palette_flags_t) (uint32_t) flags[palette];
  }

  inline unsigned int
  get_palette_name_id (const void *base, unsigned int palette) const
  {
    const HBUINT16* name_ids = &paletteLabel (base);
    return name_ids[palette];
  }

  protected:
  LOffsetTo<HBUINT32> paletteFlags;
  LOffsetTo<HBUINT16> paletteLabel;
  LOffsetTo<HBUINT16> paletteEntryLabel;
  public:
  DEFINE_SIZE_STATIC (12);
};

typedef HBUINT32 BGRAColor;

struct CPAL
{
  static const hb_tag_t tableTag = HB_OT_TAG_CPAL;

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    if (!(c->check_struct (this) && // This checks colorRecordIndicesX sanity also, see #get_size
        c->check_array ((const void*) &colorRecordsZ, sizeof (BGRAColor), numColorRecords)))
      return_trace (false);

    // Check for indices sanity so no need for doing it runtime
    for (unsigned int i = 0; i < numPalettes; ++i)
      if (colorRecordIndicesX[i] + numPaletteEntries > numColorRecords)
        return_trace (false);

    // If version is zero, we are done here; otherwise we need to check tail also
    if (version == 0)
      return_trace (true);

    const CPALV1Tail &v1 = StructAfter<CPALV1Tail> (*this);
    return_trace (v1.sanitize (c, numPalettes));
  }

  inline unsigned int get_size (void) const
  {
    return min_size + numPalettes * sizeof (HBUINT16);
  }

  inline hb_ot_color_palette_flags_t get_palette_flags (unsigned int palette) const
  {
    if (version == 0 || palette >= numPalettes)
      return HB_OT_COLOR_PALETTE_FLAG_DEFAULT;

    const CPALV1Tail& cpal1 = StructAfter<CPALV1Tail> (*this);
    return cpal1.get_palette_flags (this, palette);
  }

  inline unsigned int get_palette_name_id (unsigned int palette) const
  {
    if (version == 0 || palette >= numPalettes)
      return 0xFFFF;

    const CPALV1Tail& cpal1 = StructAfter<CPALV1Tail> (*this);
    return cpal1.get_palette_name_id (this, palette);
  }

  inline unsigned int get_palette_count () const
  {
    return numPalettes;
  }

  inline hb_ot_color_t get_color_record_argb (unsigned int color_index, unsigned int palette) const
  {
    if (color_index >= numPaletteEntries || palette >= numPalettes)
      return 0;

    const BGRAColor* records = &colorRecordsZ(this);
    // No need for more range check as it is already done on #sanitize
    return records[colorRecordIndicesX[palette] + color_index];
  }

  protected:
  HBUINT16	version;
  /* Version 0 */
  HBUINT16	numPaletteEntries;
  HBUINT16	numPalettes;
  HBUINT16	numColorRecords;
  LOffsetTo<HBUINT32>	colorRecordsZ;
  HBUINT16	colorRecordIndicesX[VAR];  // VAR=numPalettes
/*CPALV1Tail	v1[VAR];*/
  public:
  DEFINE_SIZE_ARRAY (12, colorRecordIndicesX);
};

} /* namespace OT */


#endif /* HB_OT_COLOR_CPAL_TABLE_HH */