harfbuzz/src/hb-ot-color-svg-table.hh

186 lines
5.4 KiB
C++
Raw Normal View History

/*
* 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_SVG_TABLE_HH
#define HB_OT_COLOR_SVG_TABLE_HH
#include "hb-open-type.hh"
/*
* SVG -- SVG (Scalable Vector Graphics)
* https://docs.microsoft.com/en-us/typography/opentype/spec/svg
*/
#define HB_OT_TAG_SVG HB_TAG('S','V','G',' ')
namespace OT {
struct SVGDocumentIndexEntry
{
friend struct SVG;
friend struct SVGDocumentIndex;
inline int cmp (hb_codepoint_t g) const
{ return g < startGlyphID ? -1 : g > endGlyphID ? 1 : 0; }
static int cmp (const void *pa, const void *pb)
{
const hb_codepoint_t *a = (const hb_codepoint_t *) pa;
const SVGDocumentIndexEntry *b = (const SVGDocumentIndexEntry *) pb;
return b->cmp (*a);
}
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this));
}
protected:
2018-04-11 14:43:20 +02:00
HBUINT16 startGlyphID; /* The first glyph ID in the range described by
* this index entry. */
HBUINT16 endGlyphID; /* The last glyph ID in the range described by
* this index entry. Must be >= startGlyphID. */
LOffsetTo<UnsizedArrayOf<HBUINT8>, false>
2018-04-11 14:43:20 +02:00
svgDoc; /* Offset from the beginning of the SVG Document Index
* to an SVG document. Must be non-zero. */
HBUINT32 svgDocLength; /* Length of the SVG document.
2018-04-11 14:43:20 +02:00
* Must be non-zero. */
public:
DEFINE_SIZE_STATIC (12);
};
struct SVGDocumentIndex
{
friend struct SVG;
inline const SVGDocumentIndexEntry &get_glyph_entry (hb_codepoint_t glyph_id) const
{
const SVGDocumentIndexEntry *rec;
rec = (SVGDocumentIndexEntry *) bsearch (&glyph_id,
&entries.arrayZ,
entries.len,
sizeof (SVGDocumentIndexEntry),
SVGDocumentIndexEntry::cmp);
return likely (rec && glyph_id <= rec->endGlyphID) ? *rec : Null(SVGDocumentIndexEntry);
}
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) &&
entries.sanitize_shallow (c));
}
protected:
2018-03-12 11:30:11 +01:00
ArrayOf<SVGDocumentIndexEntry>
2018-04-11 14:43:20 +02:00
entries; /* Array of SVG Document Index Entries. */
public:
DEFINE_SIZE_ARRAY (2, entries);
};
struct SVG
{
static const hb_tag_t tableTag = HB_OT_TAG_SVG;
struct accelerator_t
{
inline void init (hb_face_t *face)
{
svg_blob = hb_sanitize_context_t().reference_table<SVG> (face);
svg_len = hb_blob_get_length (svg_blob);
svg = svg_blob->as<SVG> ();
}
inline void fini (void)
{
hb_blob_destroy (svg_blob);
}
inline hb_blob_t*
failed_create_blob (hb_codepoint_t glyph_id,
hb_codepoint_t *start_glyph_id,
hb_codepoint_t *end_glyph_id) const
{
if (start_glyph_id) *start_glyph_id = 0;
if (end_glyph_id) *end_glyph_id = 0;
return hb_blob_get_empty ();
}
inline hb_blob_t*
create_blob (hb_codepoint_t glyph_id,
hb_codepoint_t *start_glyph_id,
hb_codepoint_t *end_glyph_id) const
{
if (unlikely (svg_len == 0))
return failed_create_blob (glyph_id, start_glyph_id, end_glyph_id);
2018-04-11 14:43:20 +02:00
const SVGDocumentIndex &index = svg+svg->svgDocIndex;
const SVGDocumentIndexEntry &entry = index.get_glyph_entry (glyph_id);
if (unlikely (entry.svgDocLength == 0))
return failed_create_blob (glyph_id, start_glyph_id, end_glyph_id);
unsigned int blob_offset = entry.svgDoc;
blob_offset += svg->svgDocIndex;
if (unlikely (blob_offset > svg_len || blob_offset + entry.svgDocLength > svg_len))
return failed_create_blob (glyph_id, start_glyph_id, end_glyph_id);
if (start_glyph_id) *start_glyph_id = entry.startGlyphID;
if (end_glyph_id) *end_glyph_id = entry.endGlyphID;
return hb_blob_create_sub_blob (svg_blob, blob_offset, entry.svgDocLength);
}
inline bool has_data () const
{ return svg_len; }
private:
hb_blob_t *svg_blob;
const SVG *svg;
unsigned int svg_len;
};
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (likely (c->check_struct (this) &&
(this+svgDocIndex).sanitize (c)));
}
protected:
2018-03-12 11:30:11 +01:00
HBUINT16 version; /* Table version (starting at 0). */
LOffsetTo<SVGDocumentIndex>
2018-04-11 14:43:20 +02:00
svgDocIndex; /* Offset (relative to the start of the SVG table) to the
2018-03-12 11:30:11 +01:00
* SVG Documents Index. Must be non-zero. */
HBUINT32 reserved; /* Set to 0. */
public:
DEFINE_SIZE_STATIC (10);
};
struct SVG_accelerator_t : SVG::accelerator_t {};
} /* namespace OT */
#endif /* HB_OT_COLOR_SVG_TABLE_HH */