hb-ot-color-sbix-table.hh 8.4 KB
Newer Older
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
/*
 * 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.
 */

#ifndef HB_OT_COLOR_SBIX_TABLE_HH
#define HB_OT_COLOR_SBIX_TABLE_HH

28
#include "hb-open-type.hh"
29

30 31 32 33 34
/*
 * sbix -- Standard Bitmap Graphics
 * https://docs.microsoft.com/en-us/typography/opentype/spec/sbix
 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6sbix.html
 */
35
#define HB_OT_TAG_sbix HB_TAG('s','b','i','x')
36

37

38 39 40 41 42
namespace OT {


struct SBIXGlyph
{
B
Behdad Esfahbod 已提交
43 44 45 46 47 48 49 50 51 52 53
  HBINT16	xOffset;	/* The horizontal (x-axis) offset from the left
				 * edge of the graphic to the glyph’s origin.
				 * That is, the x-coordinate of the point on the
				 * baseline at the left edge of the glyph. */
  HBINT16	yOffset;	/* The vertical (y-axis) offset from the bottom
				 * edge of the graphic to the glyph’s origin.
				 * That is, the y-coordinate of the point on the
				 * baseline at the left edge of the glyph. */
  Tag		graphicType;	/* Indicates the format of the embedded graphic
				 * data: one of 'jpg ', 'png ' or 'tiff', or the
				 * special format 'dupe'. */
54 55
  UnsizedArrayOf<HBUINT8>
		data;		/* The actual embedded graphic data. The total
B
Behdad Esfahbod 已提交
56 57 58
				 * length is inferred from sequential entries in
				 * the glyphDataOffsets array and the fixed size
				 * (8 bytes) of the preceding fields. */
59
  public:
B
Behdad Esfahbod 已提交
60
  DEFINE_SIZE_ARRAY (8, data);
61 62
};

B
Behdad Esfahbod 已提交
63
struct SBIXStrike
64 65 66 67 68
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
69
		  imageOffsetsZ.sanitize_shallow (c, c->get_num_glyphs () + 1));
70 71
  }

72 73 74 75 76 77
  inline unsigned int get_ppem () const
  { return ppem; }

  inline unsigned int get_resolution () const
  { return resolution; }

78
  inline unsigned int calculate_blob_size (unsigned int glyph_id) const
79 80 81 82 83 84
  {
    return imageOffsetsZ[glyph_id + 1] - imageOffsetsZ[glyph_id] - SBIXGlyph::min_size;
  }

  inline hb_blob_t *get_glyph_blob (unsigned int  glyph_id,
				    hb_blob_t    *sbix_blob,
B
Behdad Esfahbod 已提交
85
				    hb_tag_t      file_type,
86 87
				    int          *x_offset,
				    int          *y_offset,
88 89
				    unsigned int  num_glyphs) const
  {
90
    unsigned int sbix_len = sbix_blob->length;
B
Behdad Esfahbod 已提交
91
    unsigned int strike_offset = (const char *) this - (const char *) sbix_blob->data;
92 93 94
    if (imageOffsetsZ[glyph_id + 1] - imageOffsetsZ[glyph_id] == 0)
      return hb_blob_get_empty ();

95 96 97
    if (strike_offset + (unsigned int) imageOffsetsZ[glyph_id] + SBIXGlyph::min_size > sbix_len)
      return hb_blob_get_empty ();

98 99
    const SBIXGlyph *glyph = &(this+imageOffsetsZ[glyph_id]);
    if (unlikely (glyph->graphicType == HB_TAG ('d','u','p','e') &&
100
		  calculate_blob_size (glyph_id) >= 2))
101 102 103 104 105 106
    {
      unsigned int new_glyph_id = *((HBUINT16 *) &glyph->data);
      if (new_glyph_id < num_glyphs)
      {
	glyph = &(this+imageOffsetsZ[new_glyph_id]);
	glyph_id = new_glyph_id;
107
        if (strike_offset + (unsigned int) imageOffsetsZ[glyph_id] + SBIXGlyph::min_size > sbix_len)
B
Behdad Esfahbod 已提交
108
	  return hb_blob_get_empty ();
109
      }
B
Behdad Esfahbod 已提交
110 111
      else
	return hb_blob_get_empty ();
112
    }
E
Ebrahim Byagowi 已提交
113
    if (unlikely (file_type != glyph->graphicType))
114
      return hb_blob_get_empty ();
115 116 117 118 119 120
    unsigned int blob_size = calculate_blob_size (glyph_id);
    if (unlikely (blob_size == 0))
      return hb_blob_get_empty ();

    if (x_offset) *x_offset = glyph->xOffset;
    if (y_offset) *y_offset = glyph->yOffset;
121 122
    unsigned int offset = strike_offset + SBIXGlyph::min_size;
    offset += imageOffsetsZ[glyph_id];
123
    return hb_blob_create_sub_blob (sbix_blob, offset, blob_size);
124 125
  }

126
  protected:
127 128 129 130 131 132
  HBUINT16	ppem;		/* The PPEM size for which this strike was designed. */
  HBUINT16	resolution;	/* The device pixel density (in PPI) for which this
				 * strike was designed. (E.g., 96 PPI, 192 PPI.) */
  UnsizedArrayOf<LOffsetTo<SBIXGlyph> >
		imageOffsetsZ;	/* Offset from the beginning of the strike data header
				 * to bitmap data for an individual glyph ID. */
133 134 135 136 137 138
  public:
  DEFINE_SIZE_STATIC (8);
};

struct sbix
{
139
  static const hb_tag_t tableTag = HB_OT_TAG_sbix;
140 141 142 143

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
144
    return_trace (likely (c->check_struct (this) && strikes.sanitize (c, this)));
145 146
  }

147 148 149 150
  struct accelerator_t
  {
    inline void init (hb_face_t *face)
    {
151
      sbix_blob = hb_sanitize_context_t().reference_table<sbix> (face);
152
      sbix_len = hb_blob_get_length (sbix_blob);
153
      table = sbix_blob->as<sbix> ();
154
      num_glyphs = face->get_num_glyphs ();
155 156 157 158 159 160 161
    }

    inline void fini (void)
    {
      hb_blob_destroy (sbix_blob);
    }

162 163
    inline bool has_data () const { return sbix_len; }

164 165
    inline bool get_extents (hb_font_t          *font,
			     hb_codepoint_t      glyph,
166 167 168
			     hb_glyph_extents_t *extents) const
    {
      /* We only support PNG right now, and following function checks type. */
169
      return get_png_extents (font, glyph, extents);
170 171
    }

172 173 174 175
    inline hb_blob_t *reference_png (hb_font_t      *font,
				     hb_codepoint_t  glyph_id,
				     int            *x_offset,
				     int            *y_offset) const
B
Behdad Esfahbod 已提交
176
    {
177
      return get_strike (font).get_glyph_blob (glyph_id, sbix_blob,
B
Behdad Esfahbod 已提交
178
					       HB_TAG ('p','n','g',' '),
B
Behdad Esfahbod 已提交
179
					       x_offset, y_offset,
B
Behdad Esfahbod 已提交
180 181 182 183 184 185
					       num_glyphs);
    }

    private:

    inline const SBIXStrike &get_strike (hb_font_t *font) const
186
    {
187
      if (unlikely (!table->strikes.len))
B
Behdad Esfahbod 已提交
188
        return Null(SBIXStrike);
189

190 191 192 193 194
      unsigned int requested_ppem = MAX (font->x_ppem, font->y_ppem);
      if (!requested_ppem)
        requested_ppem = 1<<30; /* Choose largest strike. */
      /* TODO Add DPI sensitivity as well? */
      unsigned int best_i = 0;
195
      unsigned int best_ppem = (table+table->strikes[0]).get_ppem ();
196

197
      for (unsigned int i = 1; i < table->strikes.len; i++)
198
      {
199
	unsigned int ppem = (table+table->strikes[i]).get_ppem ();
200 201 202 203 204 205 206
	if ((requested_ppem <= ppem && ppem < best_ppem) ||
	    (requested_ppem > best_ppem && ppem > best_ppem))
	{
	  best_i = i;
	  best_ppem = ppem;
	}
      }
207

B
Behdad Esfahbod 已提交
208
      return table+table->strikes[best_i];
209 210
    }

211 212
    struct PNGHeader
    {
213
      HBUINT8	signature[8];
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
      struct
      {
        struct
	{
	  HBUINT32	length;
	  Tag		type;
	}		header;
	HBUINT32	width;
	HBUINT32	height;
	HBUINT8		bitDepth;
	HBUINT8		colorType;
	HBUINT8		compressionMethod;
	HBUINT8		filterMethod;
	HBUINT8		interlaceMethod;
      } IHDR;

      public:
      DEFINE_SIZE_STATIC (29);
232 233
    };

234 235
    inline bool get_png_extents (hb_font_t          *font,
				 hb_codepoint_t      glyph,
236 237
				 hb_glyph_extents_t *extents) const
    {
238
      if (likely (sbix_len == 0))
239
        return false;
240

241
      int x_offset = 0, y_offset = 0;
242
      hb_blob_t *blob = reference_png (font, glyph, &x_offset, &y_offset);
E
Ebrahim Byagowi 已提交
243

244
      if (unlikely (blob->length < sizeof (PNGHeader)))
245 246
        return false;

247 248
      const PNGHeader &png = *blob->as<PNGHeader>();

249 250
      extents->x_bearing = x_offset;
      extents->y_bearing = y_offset;
251 252
      extents->width     = png.IHDR.width;
      extents->height    = png.IHDR.height;
253 254 255
      hb_blob_destroy (blob);

      return true;
256 257
    }

258 259
    private:
    hb_blob_t *sbix_blob;
260
    const sbix *table;
261 262 263 264

    unsigned int sbix_len;
    unsigned int num_glyphs;
  };
265 266

  protected:
B
Behdad Esfahbod 已提交
267 268 269
  HBUINT16	version;	/* Table version number — set to 1 */
  HBUINT16	flags;		/* Bit 0: Set to 1. Bit 1: Draw outlines.
				 * Bits 2 to 15: reserved (set to 0). */
270
  LArrayOf<LOffsetTo<SBIXStrike> >
B
Behdad Esfahbod 已提交
271 272
		strikes;	/* Offsets from the beginning of the 'sbix'
				 * table to data for each individual bitmap strike. */
273
  public:
B
Behdad Esfahbod 已提交
274
  DEFINE_SIZE_ARRAY (8, strikes);
275 276
};

E
Ebrahim Byagowi 已提交
277 278
struct sbix_accelerator_t : sbix::accelerator_t {};

279 280 281
} /* namespace OT */

#endif /* HB_OT_COLOR_SBIX_TABLE_HH */