xref: /aosp_15_r20/external/harfbuzz_ng/src/OT/Color/svg/svg.hh (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
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 {
cmpOT::SVGDocumentIndexEntry45   int cmp (hb_codepoint_t g) const
46   { return g < startGlyphID ? -1 : g > endGlyphID ? 1 : 0; }
47 
reference_blobOT::SVGDocumentIndexEntry48   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 
sanitizeOT::SVGDocumentIndexEntry55   bool sanitize (hb_sanitize_context_t *c, const void *base) const
56   {
57     TRACE_SANITIZE (this);
58     return_trace (c->check_struct (this) &&
59 		  hb_barrier () &&
60 		  svgDoc.sanitize (c, base, svgDocLength));
61   }
62 
63   protected:
64   HBUINT16	startGlyphID;	/* The first glyph ID in the range described by
65 				 * this index entry. */
66   HBUINT16	endGlyphID;	/* The last glyph ID in the range described by
67 				 * this index entry. Must be >= startGlyphID. */
68   NNOffset32To<UnsizedArrayOf<HBUINT8>>
69 		svgDoc;		/* Offset from the beginning of the SVG Document Index
70 				 * to an SVG document. Must be non-zero. */
71   HBUINT32	svgDocLength;	/* Length of the SVG document.
72 				 * Must be non-zero. */
73   public:
74   DEFINE_SIZE_STATIC (12);
75 };
76 
77 struct SVG
78 {
79   static constexpr hb_tag_t tableTag = HB_OT_TAG_SVG;
80 
has_dataOT::SVG81   bool has_data () const { return svgDocEntries; }
82 
83   struct accelerator_t
84   {
accelerator_tOT::SVG::accelerator_t85     accelerator_t (hb_face_t *face)
86     { table = hb_sanitize_context_t ().reference_table<SVG> (face); }
~accelerator_tOT::SVG::accelerator_t87     ~accelerator_t () { table.destroy (); }
88 
reference_blob_for_glyphOT::SVG::accelerator_t89     hb_blob_t *reference_blob_for_glyph (hb_codepoint_t glyph_id) const
90     {
91       return table->get_glyph_entry (glyph_id).reference_blob (table.get_blob (),
92 							       table->svgDocEntries);
93     }
94 
has_dataOT::SVG::accelerator_t95     bool has_data () const { return table->has_data (); }
96 
paint_glyphOT::SVG::accelerator_t97     bool paint_glyph (hb_font_t *font HB_UNUSED, hb_codepoint_t glyph, hb_paint_funcs_t *funcs, void *data) const
98     {
99       if (!has_data ())
100         return false;
101 
102       hb_blob_t *blob = reference_blob_for_glyph (glyph);
103 
104       if (blob == hb_blob_get_empty ())
105         return false;
106 
107       funcs->image (data,
108 		    blob,
109 		    0, 0,
110 		    HB_PAINT_IMAGE_FORMAT_SVG,
111 		    font->slant_xy,
112 		    nullptr);
113 
114       hb_blob_destroy (blob);
115       return true;
116     }
117 
118     private:
119     hb_blob_ptr_t<SVG> table;
120     public:
121     DEFINE_SIZE_STATIC (sizeof (hb_blob_ptr_t<SVG>));
122   };
123 
get_glyph_entryOT::SVG124   const SVGDocumentIndexEntry &get_glyph_entry (hb_codepoint_t glyph_id) const
125   { return (this+svgDocEntries).bsearch (glyph_id); }
126 
sanitizeOT::SVG127   bool sanitize (hb_sanitize_context_t *c) const
128   {
129     TRACE_SANITIZE (this);
130     return_trace (likely (c->check_struct (this) &&
131 			  (this+svgDocEntries).sanitize_shallow (c)));
132   }
133 
134   protected:
135   HBUINT16	version;	/* Table version (starting at 0). */
136   Offset32To<SortedArray16Of<SVGDocumentIndexEntry>>
137 		svgDocEntries;	/* Offset (relative to the start of the SVG table) to the
138 				 * SVG Documents Index. Must be non-zero. */
139 				/* Array of SVG Document Index Entries. */
140   HBUINT32	reserved;	/* Set to 0. */
141   public:
142   DEFINE_SIZE_STATIC (10);
143 };
144 
145 struct SVG_accelerator_t : SVG::accelerator_t {
SVG_accelerator_tOT::SVG_accelerator_t146   SVG_accelerator_t (hb_face_t *face) : SVG::accelerator_t (face) {}
147 };
148 
149 } /* namespace OT */
150 
151 
152 #endif /* OT_COLOR_SVG_SVG_HH */
153