Imported Upstream version 1.8.1
[platform/upstream/harfbuzz.git] / src / hb-ot-color-sbix-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_SBIX_TABLE_HH
26 #define HB_OT_COLOR_SBIX_TABLE_HH
27
28 #include "hb-open-type-private.hh"
29
30 /*
31  * sbix -- Standard Bitmap Graphics
32  * https://docs.microsoft.com/en-us/typography/opentype/spec/sbix
33  * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6sbix.html
34  */
35 #define HB_OT_TAG_sbix HB_TAG('s','b','i','x')
36
37
38 namespace OT {
39
40
41 struct SBIXGlyph
42 {
43   HBINT16       xOffset;        /* The horizontal (x-axis) offset from the left
44                                  * edge of the graphic to the glyph’s origin.
45                                  * That is, the x-coordinate of the point on the
46                                  * baseline at the left edge of the glyph. */
47   HBINT16       yOffset;        /* The vertical (y-axis) offset from the bottom
48                                  * edge of the graphic to the glyph’s origin.
49                                  * That is, the y-coordinate of the point on the
50                                  * baseline at the left edge of the glyph. */
51   Tag           graphicType;    /* Indicates the format of the embedded graphic
52                                  * data: one of 'jpg ', 'png ' or 'tiff', or the
53                                  * special format 'dupe'. */
54   UnsizedArrayOf<HBUINT8>
55                 data;           /* The actual embedded graphic data. The total
56                                  * length is inferred from sequential entries in
57                                  * the glyphDataOffsets array and the fixed size
58                                  * (8 bytes) of the preceding fields. */
59   public:
60   DEFINE_SIZE_ARRAY (8, data);
61 };
62
63 struct SBIXStrike
64 {
65   friend struct sbix;
66
67   inline bool sanitize (hb_sanitize_context_t *c) const
68   {
69     TRACE_SANITIZE (this);
70     return_trace (c->check_struct (this) &&
71                   imageOffsetsZ.sanitize_shallow (c, c->num_glyphs + 1));
72   }
73
74   protected:
75   HBUINT16      ppem;           /* The PPEM size for which this strike was designed. */
76   HBUINT16      resolution;     /* The device pixel density (in PPI) for which this
77                                  * strike was designed. (E.g., 96 PPI, 192 PPI.) */
78   UnsizedArrayOf<LOffsetTo<SBIXGlyph> >
79                 imageOffsetsZ;  /* Offset from the beginning of the strike data header
80                                  * to bitmap data for an individual glyph ID. */
81   public:
82   DEFINE_SIZE_STATIC (8);
83 };
84
85 struct sbix
86 {
87   static const hb_tag_t tableTag = HB_OT_TAG_sbix;
88
89   inline bool sanitize (hb_sanitize_context_t *c) const
90   {
91     TRACE_SANITIZE (this);
92     return_trace (likely (c->check_struct (this) && strikes.sanitize (c, this)));
93   }
94
95   struct accelerator_t
96   {
97     inline void init (hb_face_t *face)
98     {
99       num_glyphs = hb_face_get_glyph_count (face);
100
101       OT::Sanitizer<OT::sbix> sanitizer;
102       sanitizer.set_num_glyphs (num_glyphs);
103       sbix_blob = sanitizer.sanitize (face->reference_table (HB_OT_TAG_sbix));
104       sbix_len = hb_blob_get_length (sbix_blob);
105       sbix_table = sbix_blob->as<OT::sbix> ();
106
107     }
108
109     inline void fini (void)
110     {
111       hb_blob_destroy (sbix_blob);
112     }
113
114     inline void dump (void (*callback) (const uint8_t* data, unsigned int length,
115                                         unsigned int group, unsigned int gid)) const
116     {
117       for (unsigned group = 0; group < sbix_table->strikes.len; ++group)
118       {
119         const SBIXStrike &strike = sbix_table->strikes[group](sbix_table);
120         for (unsigned int glyph = 0; glyph < num_glyphs; ++glyph)
121           if (strike.imageOffsetsZ[glyph + 1] - strike.imageOffsetsZ[glyph] > 0)
122           {
123             const SBIXGlyph &sbixGlyph = strike.imageOffsetsZ[glyph]((const void *) &strike);
124             callback ((const uint8_t*) &sbixGlyph.data,
125                       strike.imageOffsetsZ[glyph + 1] - strike.imageOffsetsZ[glyph] - 8,
126                       group, glyph);
127           }
128       }
129     }
130
131     private:
132     hb_blob_t *sbix_blob;
133     const sbix *sbix_table;
134
135     unsigned int sbix_len;
136     unsigned int num_glyphs;
137
138   };
139
140   protected:
141   HBUINT16      version;        /* Table version number — set to 1 */
142   HBUINT16      flags;          /* Bit 0: Set to 1. Bit 1: Draw outlines.
143                                  * Bits 2 to 15: reserved (set to 0). */
144   LArrayOf<LOffsetTo<SBIXStrike> >
145                 strikes;        /* Offsets from the beginning of the 'sbix'
146                                  * table to data for each individual bitmap strike. */
147   public:
148   DEFINE_SIZE_ARRAY (8, strikes);
149 };
150
151 } /* namespace OT */
152
153 #endif /* HB_OT_COLOR_SBIX_TABLE_HH */