| 1 | /* |
| 2 | * Copyright © 2018 Ebrahim Byagowi |
| 3 | * |
| 4 | * This is part of HarfBuzz, a text shaping library. |
| 5 | * |
| 6 | * Permission is hereby granted, without written agreement and without |
| 7 | * license or royalty fees, to use, copy, modify, and distribute this |
| 8 | * software and its documentation for any purpose, provided that the |
| 9 | * above copyright notice and the following two paragraphs appear in |
| 10 | * all copies of this software. |
| 11 | * |
| 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 16 | * DAMAGE. |
| 17 | * |
| 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 23 | */ |
| 24 | |
| 25 | #ifndef OT_COLOR_SVG_SVG_HH |
| 26 | #define OT_COLOR_SVG_SVG_HH |
| 27 | |
| 28 | #include "../../../hb-open-type.hh" |
| 29 | #include "../../../hb-blob.hh" |
| 30 | #include "../../../hb-paint.hh" |
| 31 | |
| 32 | /* |
| 33 | * SVG -- SVG (Scalable Vector Graphics) |
| 34 | * https://docs.microsoft.com/en-us/typography/opentype/spec/svg |
| 35 | */ |
| 36 | |
| 37 | #define HB_OT_TAG_SVG HB_TAG('S','V','G',' ') |
| 38 | |
| 39 | |
| 40 | namespace OT { |
| 41 | |
| 42 | |
| 43 | struct SVGDocumentIndexEntry |
| 44 | { |
| 45 | int cmp (hb_codepoint_t g) const |
| 46 | { return g < startGlyphID ? -1 : g > endGlyphID ? 1 : 0; } |
| 47 | |
| 48 | hb_blob_t *reference_blob (hb_blob_t *svg_blob, unsigned int index_offset) const |
| 49 | { |
| 50 | return hb_blob_create_sub_blob (svg_blob, |
| 51 | index_offset + (unsigned int) svgDoc, |
| 52 | svgDocLength); |
| 53 | } |
| 54 | |
| 55 | bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 56 | { |
| 57 | TRACE_SANITIZE (this); |
| 58 | return_trace (c->check_struct (this) && |
| 59 | svgDoc.sanitize (c, base, svgDocLength)); |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | HBUINT16 startGlyphID; /* The first glyph ID in the range described by |
| 64 | * this index entry. */ |
| 65 | HBUINT16 endGlyphID; /* The last glyph ID in the range described by |
| 66 | * this index entry. Must be >= startGlyphID. */ |
| 67 | NNOffset32To<UnsizedArrayOf<HBUINT8>> |
| 68 | svgDoc; /* Offset from the beginning of the SVG Document Index |
| 69 | * to an SVG document. Must be non-zero. */ |
| 70 | HBUINT32 svgDocLength; /* Length of the SVG document. |
| 71 | * Must be non-zero. */ |
| 72 | public: |
| 73 | DEFINE_SIZE_STATIC (12); |
| 74 | }; |
| 75 | |
| 76 | struct SVG |
| 77 | { |
| 78 | static constexpr hb_tag_t tableTag = HB_OT_TAG_SVG; |
| 79 | |
| 80 | bool has_data () const { return svgDocEntries; } |
| 81 | |
| 82 | struct accelerator_t |
| 83 | { |
| 84 | accelerator_t (hb_face_t *face) |
| 85 | { table = hb_sanitize_context_t ().reference_table<SVG> (face); } |
| 86 | ~accelerator_t () { table.destroy (); } |
| 87 | |
| 88 | hb_blob_t *reference_blob_for_glyph (hb_codepoint_t glyph_id) const |
| 89 | { |
| 90 | return table->get_glyph_entry (glyph_id).reference_blob (table.get_blob (), |
| 91 | table->svgDocEntries); |
| 92 | } |
| 93 | |
| 94 | bool has_data () const { return table->has_data (); } |
| 95 | |
| 96 | bool paint_glyph (hb_font_t *font HB_UNUSED, hb_codepoint_t glyph, hb_paint_funcs_t *funcs, void *data) const |
| 97 | { |
| 98 | if (!has_data ()) |
| 99 | return false; |
| 100 | |
| 101 | hb_blob_t *blob = reference_blob_for_glyph (glyph); |
| 102 | |
| 103 | if (blob == hb_blob_get_empty ()) |
| 104 | return false; |
| 105 | |
| 106 | funcs->image (data, |
| 107 | blob, |
| 108 | 0, 0, |
| 109 | HB_PAINT_IMAGE_FORMAT_SVG, |
| 110 | font->slant_xy, |
| 111 | nullptr); |
| 112 | |
| 113 | hb_blob_destroy (blob); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | hb_blob_ptr_t<SVG> table; |
| 119 | public: |
| 120 | DEFINE_SIZE_STATIC (sizeof (hb_blob_ptr_t<SVG>)); |
| 121 | }; |
| 122 | |
| 123 | const SVGDocumentIndexEntry &get_glyph_entry (hb_codepoint_t glyph_id) const |
| 124 | { return (this+svgDocEntries).bsearch (glyph_id); } |
| 125 | |
| 126 | bool sanitize (hb_sanitize_context_t *c) const |
| 127 | { |
| 128 | TRACE_SANITIZE (this); |
| 129 | return_trace (likely (c->check_struct (this) && |
| 130 | (this+svgDocEntries).sanitize_shallow (c))); |
| 131 | } |
| 132 | |
| 133 | protected: |
| 134 | HBUINT16 version; /* Table version (starting at 0). */ |
| 135 | Offset32To<SortedArray16Of<SVGDocumentIndexEntry>> |
| 136 | svgDocEntries; /* Offset (relative to the start of the SVG table) to the |
| 137 | * SVG Documents Index. Must be non-zero. */ |
| 138 | /* Array of SVG Document Index Entries. */ |
| 139 | HBUINT32 reserved; /* Set to 0. */ |
| 140 | public: |
| 141 | DEFINE_SIZE_STATIC (10); |
| 142 | }; |
| 143 | |
| 144 | struct SVG_accelerator_t : SVG::accelerator_t { |
| 145 | SVG_accelerator_t (hb_face_t *face) : SVG::accelerator_t (face) {} |
| 146 | }; |
| 147 | |
| 148 | } /* namespace OT */ |
| 149 | |
| 150 | |
| 151 | #endif /* OT_COLOR_SVG_SVG_HH */ |
| 152 | |