hb-ot-color-sbix-table.hh 4.9 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 28 29
/*
 * 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

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

30
#define HB_OT_TAG_sbix HB_TAG('s','b','i','x')
31 32 33 34 35 36

namespace OT {


struct SBIXGlyph
{
B
Behdad Esfahbod 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  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'. */
  HBUINT8	data[VAR];	/* The actual embedded graphic data. The total
				 * length is inferred from sequential entries in
				 * the glyphDataOffsets array and the fixed size
				 * (8 bytes) of the preceding fields. */
52
  public:
B
Behdad Esfahbod 已提交
53
  DEFINE_SIZE_ARRAY (8, data);
54 55
};

B
Behdad Esfahbod 已提交
56
struct SBIXStrike
57
{
58 59
  friend struct sbix;

60 61 62 63
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
B
Behdad Esfahbod 已提交
64 65 66
		  c->check_array (imageOffsetsZ,
				  sizeof (HBUINT32),
				  1 + c->num_glyphs));
67 68
  }

B
Behdad Esfahbod 已提交
69 70 71
  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.) */
72 73
  protected:
  LOffsetTo<SBIXGlyph>	imageOffsetsZ[VAR]; // VAR=maxp.numGlyphs + 1
B
Behdad Esfahbod 已提交
74 75
					/* Offset from the beginning of the strike data header
					 * to bitmap data for an individual glyph ID. */
76 77 78 79 80 81
  public:
  DEFINE_SIZE_STATIC (8);
};

/*
 * sbix -- Standard Bitmap Graphics Table
82
 * https://docs.microsoft.com/en-us/typography/opentype/spec/sbix
83
 */
84

85 86
struct sbix
{
87
  static const hb_tag_t tableTag = HB_OT_TAG_sbix;
88 89 90 91

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
92
    return_trace (c->check_struct (this) && strikes.sanitize (c, this));
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
  struct accelerator_t
  {
    inline void init (hb_face_t *face)
    {
      num_glyphs = hb_face_get_glyph_count (face);

      OT::Sanitizer<OT::sbix> sanitizer;
      sanitizer.set_num_glyphs (num_glyphs);
      sbix_blob = sanitizer.sanitize (face->reference_table (HB_OT_TAG_sbix));
      sbix_len = hb_blob_get_length (sbix_blob);
      sbix_table = OT::Sanitizer<OT::sbix>::lock_instance (sbix_blob);

    }

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

    inline void dump (void (*callback) (const uint8_t* data, unsigned int length,
        unsigned int group, unsigned int gid)) const
    {
      for (unsigned group = 0; group < sbix_table->strikes.len; ++group)
      {
        const SBIXStrike &strike = sbix_table->strikes[group](sbix_table);
        for (unsigned int glyph = 0; glyph < num_glyphs; ++glyph)
          if (strike.imageOffsetsZ[glyph + 1] - strike.imageOffsetsZ[glyph] > 0)
          {
            const SBIXGlyph &sbixGlyph = strike.imageOffsetsZ[glyph]((const void *) &strike);
            callback ((const uint8_t*) sbixGlyph.data,
              strike.imageOffsetsZ[glyph + 1] - strike.imageOffsetsZ[glyph] - 8,
              group, glyph);
          }
      }
    }

    private:
    hb_blob_t *sbix_blob;
    const sbix *sbix_table;

    unsigned int sbix_len;
    unsigned int num_glyphs;

  };
139 140

  protected:
B
Behdad Esfahbod 已提交
141 142 143 144 145 146
  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). */
  ArrayOf<LOffsetTo<SBIXStrike>, HBUINT32>
		strikes;	/* Offsets from the beginning of the 'sbix'
				 * table to data for each individual bitmap strike. */
147
  public:
B
Behdad Esfahbod 已提交
148
  DEFINE_SIZE_ARRAY (8, strikes);
149 150 151 152 153
};

} /* namespace OT */

#endif /* HB_OT_COLOR_SBIX_TABLE_HH */