1 /****************************************************************************
5 * Interface for OT-SVG support related things (specification).
7 * Copyright (C) 2022-2023 by
8 * David Turner, Robert Wilhelm, Werner Lemberg, and Moazin Khatti.
10 * This file is part of the FreeType project, and may only be used,
11 * modified, and distributed under the terms of the FreeType project
12 * license, LICENSE.TXT. By continuing to use, modify, or distribute
13 * this file you indicate that you have read the license and
14 * understand and accept it fully.
22 #include <freetype/freetype.h>
25 #error "freetype.h of FreeType 1 has been loaded!"
26 #error "Please fix the directory search order for header files"
27 #error "so that freetype.h of FreeType 2 is found first."
34 /**************************************************************************
43 * OT-SVG API between FreeType and an external SVG rendering library.
46 * This section describes the four hooks necessary to render SVG
47 * 'documents' that are contained in an OpenType font's 'SVG~' table.
49 * For more information on the implementation, see our standard hooks
50 * based on 'librsvg' in the [FreeType Demo
51 * Programs](https://gitlab.freedesktop.org/freetype/freetype-demos)
57 /**************************************************************************
63 * A callback that is called when the first OT-SVG glyph is rendered in
64 * the lifetime of an @FT_Library object. In a typical implementation,
65 * one would want to allocate a structure and point the `data_pointer`
66 * to it and perform any library initializations that might be needed.
70 * The SVG rendering module stores a pointer variable that can be used
71 * by clients to store any data that needs to be shared across
72 * different hooks. `data_pointer` is essentially a pointer to that
73 * pointer such that it can be written to as well as read from.
76 * FreeType error code. 0 means success.
82 (*SVG_Lib_Init_Func)( FT_Pointer *data_pointer );
85 /**************************************************************************
91 * A callback that is called when the `ot-svg` module is being freed.
92 * It is only called if the init hook was called earlier. This means
93 * that neither the init nor the free hook is called if no OT-SVG glyph
96 * In a typical implementation, one would want to free any state
97 * structure that was allocated in the init hook and perform any
98 * library-related closure that might be needed.
102 * The SVG rendering module stores a pointer variable that can be used
103 * by clients to store any data that needs to be shared across
104 * different hooks. `data_pointer` is essentially a pointer to that
105 * pointer such that it can be written to as well as read from.
111 (*SVG_Lib_Free_Func)( FT_Pointer *data_pointer );
114 /**************************************************************************
117 * SVG_Lib_Render_Func
120 * A callback that is called to render an OT-SVG glyph. This callback
121 * hook is called right after the preset hook @SVG_Lib_Preset_Slot_Func
122 * has been called with `cache` set to `TRUE`. The data necessary to
123 * render is available through the handle @FT_SVG_Document, which is set
124 * in the `other` field of @FT_GlyphSlotRec.
126 * The render hook is expected to render the SVG glyph to the bitmap
127 * buffer that is allocated already at `slot->bitmap.buffer`. It also
128 * sets the `num_grays` value as well as `slot->format`.
132 * The slot to render.
136 * The SVG rendering module stores a pointer variable that can be used
137 * by clients to store any data that needs to be shared across
138 * different hooks. `data_pointer` is essentially a pointer to that
139 * pointer such that it can be written to as well as read from.
142 * FreeType error code. 0 means success.
148 (*SVG_Lib_Render_Func)( FT_GlyphSlot slot,
149 FT_Pointer *data_pointer );
152 /**************************************************************************
155 * SVG_Lib_Preset_Slot_Func
158 * A callback that is called to preset the glyph slot. It is called from
161 * 1. When `FT_Load_Glyph` needs to preset the glyph slot.
163 * 2. Right before the `svg` module calls the render callback hook.
165 * When it is the former, the argument `cache` is set to `FALSE`. When
166 * it is the latter, the argument `cache` is set to `TRUE`. This
167 * distinction has been made because many calculations that are necessary
168 * for presetting a glyph slot are the same needed later for the render
169 * callback hook. Thus, if `cache` is `TRUE`, the hook can _cache_ those
170 * calculations in a memory block referenced by the state pointer.
172 * This hook is expected to preset the slot by setting parameters such as
173 * `bitmap_left`, `bitmap_top`, `width`, `rows`, `pitch`, and
174 * `pixel_mode`. It is also expected to set all the metrics for the slot
175 * including the vertical advance if it is not already set. Typically,
176 * fonts have horizontal advances but not vertical ones. If those are
177 * available, they had already been set, otherwise they have to be
178 * estimated and set manually. The hook must take into account the
179 * transformations that have been set, and translate the transformation
180 * matrices into the SVG coordinate system, as the original matrix is
181 * intended for the TTF/CFF coordinate system.
185 * The glyph slot that has the SVG document loaded.
192 * The SVG rendering module stores a pointer variable that can be used
193 * by clients to store any data that needs to be shared across
194 * different hooks. `data_pointer` is essentially a pointer to that
195 * pointer such that it can be written to as well as read from.
198 * FreeType error code. 0 means success.
204 (*SVG_Lib_Preset_Slot_Func)( FT_GlyphSlot slot,
209 /**************************************************************************
215 * A structure that stores the four hooks needed to render OT-SVG glyphs
216 * properly. The structure is publicly used to set the hooks via the
217 * @svg-hooks driver property.
219 * The behavior of each hook is described in its documentation. One
220 * thing to note is that the preset hook and the render hook often need
221 * to do the same operations; therefore, it's better to cache the
222 * intermediate data in a state structure to avoid calculating it twice.
223 * For example, in the preset hook one can draw the glyph on a recorder
224 * surface and later create a bitmap surface from it in the render hook.
226 * All four hooks must be non-NULL.
230 * The initialization hook.
244 typedef struct SVG_RendererHooks_
246 SVG_Lib_Init_Func init_svg;
247 SVG_Lib_Free_Func free_svg;
248 SVG_Lib_Render_Func render_svg;
250 SVG_Lib_Preset_Slot_Func preset_slot;
255 /**************************************************************************
261 * A structure that models one SVG document.
265 * A pointer to the SVG document.
267 * svg_document_length ::
268 * The length of `svg_document`.
271 * A metrics object storing the size information.
274 * The size of the EM square.
277 * The first glyph ID in the glyph range covered by this document.
280 * The last glyph ID in the glyph range covered by this document.
283 * A 2x2 transformation matrix to apply to the glyph while rendering
287 * The translation to apply to the glyph while rendering.
290 * When an @FT_GlyphSlot object `slot` is passed down to a renderer, the
291 * renderer can only access the `metrics` and `units_per_EM` fields via
292 * `slot->face`. However, when @FT_Glyph_To_Bitmap sets up a dummy
293 * object, it has no way to set a `face` object. Thus, metrics
294 * information and `units_per_EM` (which is necessary for OT-SVG) has to
295 * be stored separately.
300 typedef struct FT_SVG_DocumentRec_
302 FT_Byte* svg_document;
303 FT_ULong svg_document_length;
305 FT_Size_Metrics metrics;
306 FT_UShort units_per_EM;
308 FT_UShort start_glyph_id;
309 FT_UShort end_glyph_id;
314 } FT_SVG_DocumentRec;
317 /**************************************************************************
323 * A handle to an @FT_SVG_DocumentRec object.
328 typedef struct FT_SVG_DocumentRec_* FT_SVG_Document;
333 #endif /* OTSVG_H_ */