2018-03-10 08:37:56 +01:00
|
|
|
|
/*
|
|
|
|
|
* 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
|
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
|
#include "hb-open-type.hh"
|
2018-03-10 08:37:56 +01:00
|
|
|
|
|
2018-04-12 11:08:19 +02:00
|
|
|
|
/*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2018-03-27 14:27:09 +02:00
|
|
|
|
#define HB_OT_TAG_sbix HB_TAG('s','b','i','x')
|
2018-03-10 08:37:56 +01:00
|
|
|
|
|
2018-04-12 11:08:19 +02:00
|
|
|
|
|
2018-03-10 08:37:56 +01:00
|
|
|
|
namespace OT {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct SBIXGlyph
|
|
|
|
|
{
|
2018-03-11 20:38:40 +01:00
|
|
|
|
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'. */
|
2018-04-11 14:43:20 +02:00
|
|
|
|
UnsizedArrayOf<HBUINT8>
|
|
|
|
|
data; /* The actual embedded graphic data. The total
|
2018-03-11 20:38:40 +01:00
|
|
|
|
* length is inferred from sequential entries in
|
|
|
|
|
* the glyphDataOffsets array and the fixed size
|
|
|
|
|
* (8 bytes) of the preceding fields. */
|
2018-03-10 08:37:56 +01:00
|
|
|
|
public:
|
2018-03-11 20:38:40 +01:00
|
|
|
|
DEFINE_SIZE_ARRAY (8, data);
|
2018-03-10 08:37:56 +01:00
|
|
|
|
};
|
|
|
|
|
|
2018-03-11 20:38:40 +01:00
|
|
|
|
struct SBIXStrike
|
2018-03-10 08:37:56 +01:00
|
|
|
|
{
|
|
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
|
{
|
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
|
return_trace (c->check_struct (this) &&
|
2018-07-17 18:45:25 +02:00
|
|
|
|
imageOffsetsZ.sanitize_shallow (c, c->get_num_glyphs () + 1));
|
2018-03-10 08:37:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 22:25:11 +02:00
|
|
|
|
inline unsigned int get_ppem () const
|
|
|
|
|
{ return ppem; }
|
|
|
|
|
|
|
|
|
|
inline unsigned int get_resolution () const
|
|
|
|
|
{ return resolution; }
|
|
|
|
|
|
2018-10-28 20:49:04 +01:00
|
|
|
|
inline unsigned int calculate_blob_size (unsigned int glyph_id) const
|
2018-10-26 22:25:11 +02:00
|
|
|
|
{
|
|
|
|
|
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,
|
2018-10-27 09:20:53 +02:00
|
|
|
|
unsigned int sbix_len,
|
2018-10-26 22:25:11 +02:00
|
|
|
|
unsigned int strike_offset,
|
2018-10-28 20:49:04 +01:00
|
|
|
|
int *x_offset,
|
|
|
|
|
int *y_offset,
|
2018-10-28 19:08:56 +01:00
|
|
|
|
hb_tag_t file_type,
|
2018-10-26 22:25:11 +02:00
|
|
|
|
unsigned int num_glyphs) const
|
|
|
|
|
{
|
|
|
|
|
if (imageOffsetsZ[glyph_id + 1] - imageOffsetsZ[glyph_id] == 0)
|
|
|
|
|
return hb_blob_get_empty ();
|
|
|
|
|
|
2018-10-27 09:20:53 +02:00
|
|
|
|
if (strike_offset + (unsigned int) imageOffsetsZ[glyph_id] + SBIXGlyph::min_size > sbix_len)
|
|
|
|
|
return hb_blob_get_empty ();
|
|
|
|
|
|
2018-10-26 22:25:11 +02:00
|
|
|
|
const SBIXGlyph *glyph = &(this+imageOffsetsZ[glyph_id]);
|
|
|
|
|
if (unlikely (glyph->graphicType == HB_TAG ('d','u','p','e') &&
|
2018-10-28 20:49:04 +01:00
|
|
|
|
calculate_blob_size (glyph_id) >= 2))
|
2018-10-26 22:25:11 +02:00
|
|
|
|
{
|
|
|
|
|
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;
|
2018-10-27 09:20:53 +02:00
|
|
|
|
if (strike_offset + (unsigned int) imageOffsetsZ[glyph_id] + SBIXGlyph::min_size > sbix_len)
|
|
|
|
|
return hb_blob_get_empty ();
|
2018-10-26 22:25:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-28 19:08:56 +01:00
|
|
|
|
if (unlikely (file_type != glyph->graphicType))
|
2018-10-26 22:25:11 +02:00
|
|
|
|
return hb_blob_get_empty ();
|
2018-10-28 20:49:04 +01:00
|
|
|
|
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;
|
2018-10-26 22:25:11 +02:00
|
|
|
|
unsigned int offset = strike_offset + SBIXGlyph::min_size;
|
|
|
|
|
offset += imageOffsetsZ[glyph_id];
|
2018-10-28 20:49:04 +01:00
|
|
|
|
return hb_blob_create_sub_blob (sbix_blob, offset, blob_size);
|
2018-10-26 22:25:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-10 08:37:56 +01:00
|
|
|
|
protected:
|
2018-04-11 14:43:20 +02:00
|
|
|
|
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. */
|
2018-03-10 08:37:56 +01:00
|
|
|
|
public:
|
|
|
|
|
DEFINE_SIZE_STATIC (8);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct sbix
|
|
|
|
|
{
|
2018-03-27 14:27:09 +02:00
|
|
|
|
static const hb_tag_t tableTag = HB_OT_TAG_sbix;
|
2018-03-10 08:37:56 +01:00
|
|
|
|
|
|
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
|
{
|
|
|
|
|
TRACE_SANITIZE (this);
|
2018-04-18 09:39:37 +02:00
|
|
|
|
return_trace (likely (c->check_struct (this) && strikes.sanitize (c, this)));
|
2018-03-10 08:37:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 14:27:09 +02:00
|
|
|
|
struct accelerator_t
|
|
|
|
|
{
|
|
|
|
|
inline void init (hb_face_t *face)
|
|
|
|
|
{
|
2018-07-23 20:57:45 +02:00
|
|
|
|
sbix_blob = hb_sanitize_context_t().reference_table<sbix> (face);
|
2018-03-27 14:27:09 +02:00
|
|
|
|
sbix_len = hb_blob_get_length (sbix_blob);
|
2018-07-23 20:57:45 +02:00
|
|
|
|
sbix_table = sbix_blob->as<sbix> ();
|
2018-10-26 22:25:11 +02:00
|
|
|
|
num_glyphs = face->get_num_glyphs ();
|
2018-03-27 14:27:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void fini (void)
|
|
|
|
|
{
|
|
|
|
|
hb_blob_destroy (sbix_blob);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-28 22:20:50 +01:00
|
|
|
|
inline bool has_data () const { return sbix_len; }
|
|
|
|
|
|
2018-10-27 11:14:33 +02:00
|
|
|
|
/* only to support dump-emoji, don't use it anywhere else */
|
|
|
|
|
inline unsigned int *get_available_ppems (unsigned int *length)
|
2018-03-27 14:27:09 +02:00
|
|
|
|
{
|
2018-10-27 11:14:33 +02:00
|
|
|
|
if (unlikely (sbix_len == 0 || sbix_table->strikes.len == 0))
|
|
|
|
|
return nullptr;
|
|
|
|
|
*length = sbix_table->strikes.len;
|
|
|
|
|
unsigned int *result;
|
|
|
|
|
result = (unsigned int *) malloc (sizeof (unsigned int) * sbix_table->strikes.len);
|
|
|
|
|
for (unsigned int i = 0; i < sbix_table->strikes.len; i++)
|
|
|
|
|
result[i] = (sbix_table+sbix_table->strikes[i]).get_ppem ();
|
|
|
|
|
return result;
|
2018-03-27 14:27:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-28 22:51:18 +01:00
|
|
|
|
inline bool get_extents (hb_font_t *font,
|
|
|
|
|
hb_codepoint_t glyph,
|
2018-10-28 22:20:50 +01:00
|
|
|
|
hb_glyph_extents_t *extents) const
|
|
|
|
|
{
|
|
|
|
|
/* We only support PNG right now, and following function checks type. */
|
2018-10-28 22:51:18 +01:00
|
|
|
|
return get_png_extents (font, glyph, extents);
|
2018-10-28 22:20:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-28 22:56:55 +01:00
|
|
|
|
inline hb_blob_t *reference_png (hb_font_t *font,
|
|
|
|
|
hb_codepoint_t glyph_id,
|
|
|
|
|
int *x_offset,
|
|
|
|
|
int *y_offset) const
|
2018-10-26 22:25:11 +02:00
|
|
|
|
{
|
2018-10-28 22:51:18 +01:00
|
|
|
|
if (unlikely (!sbix_len || !sbix_table->strikes.len))
|
2018-10-26 22:25:11 +02:00
|
|
|
|
return hb_blob_get_empty ();
|
|
|
|
|
|
2018-10-28 22:51:18 +01:00
|
|
|
|
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;
|
|
|
|
|
unsigned int best_ppem = (sbix_table+sbix_table->strikes[0]).get_ppem ();
|
|
|
|
|
|
|
|
|
|
for (unsigned int i = 1; i < sbix_table->strikes.len; i++)
|
|
|
|
|
{
|
|
|
|
|
unsigned int ppem = (sbix_table+sbix_table->strikes[i]).get_ppem ();
|
|
|
|
|
if ((requested_ppem <= ppem && ppem < best_ppem) ||
|
|
|
|
|
(requested_ppem > best_ppem && ppem > best_ppem))
|
|
|
|
|
{
|
|
|
|
|
best_i = i;
|
|
|
|
|
best_ppem = ppem;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-26 22:25:11 +02:00
|
|
|
|
|
2018-10-28 22:51:18 +01:00
|
|
|
|
const SBIXStrike &strike = sbix_table+sbix_table->strikes[best_i];
|
2018-10-27 09:20:53 +02:00
|
|
|
|
return strike.get_glyph_blob (glyph_id, sbix_blob, sbix_len,
|
2018-10-28 22:51:18 +01:00
|
|
|
|
sbix_table->strikes[best_i],
|
2018-10-28 20:49:04 +01:00
|
|
|
|
x_offset, y_offset,
|
2018-10-28 22:56:55 +01:00
|
|
|
|
HB_TAG ('p','n','g',' '),
|
|
|
|
|
num_glyphs);
|
2018-10-28 20:49:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-28 22:20:50 +01:00
|
|
|
|
private:
|
|
|
|
|
|
2018-10-28 21:13:50 +01:00
|
|
|
|
struct PNGHeader
|
|
|
|
|
{
|
2018-10-28 21:58:44 +01:00
|
|
|
|
HBUINT8 signature[8];
|
2018-10-28 22:16:52 +01:00
|
|
|
|
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);
|
2018-10-28 21:13:50 +01:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-28 22:51:18 +01:00
|
|
|
|
inline bool get_png_extents (hb_font_t *font,
|
|
|
|
|
hb_codepoint_t glyph,
|
2018-10-28 20:49:04 +01:00
|
|
|
|
hb_glyph_extents_t *extents) const
|
|
|
|
|
{
|
2018-10-28 21:13:50 +01:00
|
|
|
|
if (likely (sbix_len == 0))
|
2018-10-28 21:33:01 +01:00
|
|
|
|
return false;
|
2018-10-28 21:13:50 +01:00
|
|
|
|
|
2018-10-28 22:52:25 +01:00
|
|
|
|
int x_offset = 0, y_offset = 0;
|
2018-10-28 22:56:55 +01:00
|
|
|
|
hb_blob_t *blob = reference_png (font, glyph, &x_offset, &y_offset);
|
2018-10-28 21:37:26 +01:00
|
|
|
|
|
2018-10-28 22:16:52 +01:00
|
|
|
|
if (unlikely (blob->length < sizeof (PNGHeader)))
|
2018-10-28 20:49:04 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
2018-10-28 22:52:25 +01:00
|
|
|
|
const PNGHeader &png = *blob->as<PNGHeader>();
|
|
|
|
|
|
2018-10-28 20:49:04 +01:00
|
|
|
|
extents->x_bearing = x_offset;
|
|
|
|
|
extents->y_bearing = y_offset;
|
2018-10-28 22:16:52 +01:00
|
|
|
|
extents->width = png.IHDR.width;
|
|
|
|
|
extents->height = png.IHDR.height;
|
2018-10-28 20:49:04 +01:00
|
|
|
|
hb_blob_destroy (blob);
|
|
|
|
|
|
|
|
|
|
return true;
|
2018-10-26 22:25:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 14:27:09 +02:00
|
|
|
|
private:
|
|
|
|
|
hb_blob_t *sbix_blob;
|
|
|
|
|
const sbix *sbix_table;
|
|
|
|
|
|
|
|
|
|
unsigned int sbix_len;
|
|
|
|
|
unsigned int num_glyphs;
|
|
|
|
|
};
|
2018-03-10 08:37:56 +01:00
|
|
|
|
|
|
|
|
|
protected:
|
2018-03-11 20:38:40 +01:00
|
|
|
|
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). */
|
2018-04-11 14:43:20 +02:00
|
|
|
|
LArrayOf<LOffsetTo<SBIXStrike> >
|
2018-03-11 20:38:40 +01:00
|
|
|
|
strikes; /* Offsets from the beginning of the 'sbix'
|
|
|
|
|
* table to data for each individual bitmap strike. */
|
2018-03-10 08:37:56 +01:00
|
|
|
|
public:
|
2018-03-11 20:38:40 +01:00
|
|
|
|
DEFINE_SIZE_ARRAY (8, strikes);
|
2018-03-10 08:37:56 +01:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-23 16:30:48 +02:00
|
|
|
|
struct sbix_accelerator_t : sbix::accelerator_t {};
|
|
|
|
|
|
2018-03-10 08:37:56 +01:00
|
|
|
|
} /* namespace OT */
|
|
|
|
|
|
|
|
|
|
#endif /* HB_OT_COLOR_SBIX_TABLE_HH */
|