hb-ot-color-sbix-table.hh 7.6 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 78 79 80 81 82 83 84
  inline unsigned int get_ppem () const
  { return ppem; }

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

  inline unsigned int blob_size (unsigned int glyph_id) const
  {
    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,
85
				    unsigned int  sbix_len,
86 87 88 89 90 91 92 93 94
				    unsigned int  strike_offset,
				    unsigned int *x_offset,
				    unsigned int *y_offset,
				    hb_tag_t      requested_file_type,
				    unsigned int  num_glyphs) const
  {
    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 100 101 102 103 104 105 106
    const SBIXGlyph *glyph = &(this+imageOffsetsZ[glyph_id]);
    if (unlikely (glyph->graphicType == HB_TAG ('d','u','p','e') &&
		  blob_size (glyph_id) >= 2))
    {
      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 108
        if (strike_offset + (unsigned int) imageOffsetsZ[glyph_id] + SBIXGlyph::min_size > sbix_len)
          return hb_blob_get_empty ();
109 110 111 112 113 114 115 116 117 118 119
      }
    }
    if (unlikely (requested_file_type != glyph->graphicType))
      return hb_blob_get_empty ();
    if (likely (x_offset)) *x_offset = glyph->xOffset;
    if (likely (y_offset)) *y_offset = glyph->yOffset;
    unsigned int offset = strike_offset + SBIXGlyph::min_size;
    offset += imageOffsetsZ[glyph_id];
    return hb_blob_create_sub_blob (sbix_blob, offset, blob_size (glyph_id));
  }

120
  protected:
121 122 123 124 125 126
  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. */
127 128 129 130 131 132
  public:
  DEFINE_SIZE_STATIC (8);
};

struct sbix
{
133
  static const hb_tag_t tableTag = HB_OT_TAG_sbix;
134 135 136 137

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
138
    return_trace (likely (c->check_struct (this) && strikes.sanitize (c, this)));
139 140
  }

141 142 143 144
  struct accelerator_t
  {
    inline void init (hb_face_t *face)
    {
145
      sbix_blob = hb_sanitize_context_t().reference_table<sbix> (face);
146
      sbix_len = hb_blob_get_length (sbix_blob);
147
      sbix_table = sbix_blob->as<sbix> ();
148
      num_glyphs = face->get_num_glyphs ();
149 150 151 152 153 154 155
    }

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

156
    inline void dump (void (*callback) (hb_blob_t *data,
157
					unsigned int group, unsigned int gid)) const
158
    {
159
      for (unsigned group = 0; group < sbix_table->strikes.len; group++)
160
      {
161 162 163 164 165
	const SBIXStrike &strike = sbix_table+sbix_table->strikes[group];
	for (unsigned int glyph_id = 0; glyph_id < num_glyphs; glyph_id++)
	{
	  hb_tag_t tag;
	  hb_blob_t *blob;
166 167 168
	  blob = strike.get_glyph_blob (glyph_id, sbix_blob, sbix_len,
					sbix_table->strikes[group],
					nullptr, nullptr,
169 170 171
					HB_TAG('p','n','g',' '), num_glyphs);
	  if (hb_blob_get_length (blob)) callback (blob, group, glyph_id);
	}
172 173 174
      }
    }

175
    inline hb_blob_t *reference_blob_for_glyph (hb_codepoint_t  glyph_id,
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
						unsigned int    ptem HB_UNUSED,
						unsigned int    requested_ppem,
						unsigned int    requested_file_type,
						unsigned int   *available_x_ppem,
						unsigned int   *available_y_ppem) const
    {
      if (unlikely (sbix_len == 0 || sbix_table->strikes.len == 0))
        return hb_blob_get_empty ();

      /* TODO: Does spec guarantee strikes are ascended sorted? */
      unsigned int group = sbix_table->strikes.len - 1;
      if (requested_ppem != 0)
	/* TODO: Use bsearch maybe or doesn't worth it? */
        for (group = 0; group < sbix_table->strikes.len; group++)
	  if ((sbix_table+sbix_table->strikes[group]).get_ppem () >= requested_ppem)
	    break;

      const SBIXStrike &strike = sbix_table+sbix_table->strikes[group];
      if (available_x_ppem) *available_x_ppem = strike.get_ppem ();
      if (available_y_ppem) *available_y_ppem = strike.get_ppem ();
196 197
      return strike.get_glyph_blob (glyph_id, sbix_blob, sbix_len,
				    sbix_table->strikes[group],
198 199 200 201 202 203
				    nullptr, nullptr, requested_file_type, num_glyphs);
    }

    inline bool has_data () const
    { return sbix_len; }

204 205 206 207 208 209 210
    private:
    hb_blob_t *sbix_blob;
    const sbix *sbix_table;

    unsigned int sbix_len;
    unsigned int num_glyphs;
  };
211 212

  protected:
B
Behdad Esfahbod 已提交
213 214 215
  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). */
216
  LArrayOf<LOffsetTo<SBIXStrike> >
B
Behdad Esfahbod 已提交
217 218
		strikes;	/* Offsets from the beginning of the 'sbix'
				 * table to data for each individual bitmap strike. */
219
  public:
B
Behdad Esfahbod 已提交
220
  DEFINE_SIZE_ARRAY (8, strikes);
221 222
};

E
Ebrahim Byagowi 已提交
223 224
struct sbix_accelerator_t : sbix::accelerator_t {};

225 226 227
} /* namespace OT */

#endif /* HB_OT_COLOR_SBIX_TABLE_HH */