otsvg.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /****************************************************************************
  2. *
  3. * otsvg.h
  4. *
  5. * Interface for OT-SVG support related things (specification).
  6. *
  7. * Copyright (C) 2022-2023 by
  8. * David Turner, Robert Wilhelm, Werner Lemberg, and Moazin Khatti.
  9. *
  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.
  15. *
  16. */
  17. #ifndef OTSVG_H_
  18. #define OTSVG_H_
  19. #include <freetype/freetype.h>
  20. #ifdef FREETYPE_H
  21. #error "freetype.h of FreeType 1 has been loaded!"
  22. #error "Please fix the directory search order for header files"
  23. #error "so that freetype.h of FreeType 2 is found first."
  24. #endif
  25. FT_BEGIN_HEADER
  26. /**************************************************************************
  27. *
  28. * @section:
  29. * svg_fonts
  30. *
  31. * @title:
  32. * OpenType SVG Fonts
  33. *
  34. * @abstract:
  35. * OT-SVG API between FreeType and an external SVG rendering library.
  36. *
  37. * @description:
  38. * This section describes the four hooks necessary to render SVG
  39. * 'documents' that are contained in an OpenType font's 'SVG~' table.
  40. *
  41. * For more information on the implementation, see our standard hooks
  42. * based on 'librsvg' in the [FreeType Demo
  43. * Programs](https://gitlab.freedesktop.org/freetype/freetype-demos)
  44. * repository.
  45. *
  46. */
  47. /**************************************************************************
  48. *
  49. * @functype:
  50. * SVG_Lib_Init_Func
  51. *
  52. * @description:
  53. * A callback that is called when the first OT-SVG glyph is rendered in
  54. * the lifetime of an @FT_Library object. In a typical implementation,
  55. * one would want to allocate a structure and point the `data_pointer`
  56. * to it and perform any library initializations that might be needed.
  57. *
  58. * @inout:
  59. * data_pointer ::
  60. * The SVG rendering module stores a pointer variable that can be used
  61. * by clients to store any data that needs to be shared across
  62. * different hooks. `data_pointer` is essentially a pointer to that
  63. * pointer such that it can be written to as well as read from.
  64. *
  65. * @return:
  66. * FreeType error code. 0 means success.
  67. *
  68. * @since:
  69. * 2.12
  70. */
  71. typedef FT_Error
  72. (*SVG_Lib_Init_Func)( FT_Pointer *data_pointer );
  73. /**************************************************************************
  74. *
  75. * @functype:
  76. * SVG_Lib_Free_Func
  77. *
  78. * @description:
  79. * A callback that is called when the `ot-svg` module is being freed.
  80. * It is only called if the init hook was called earlier. This means
  81. * that neither the init nor the free hook is called if no OT-SVG glyph
  82. * is rendered.
  83. *
  84. * In a typical implementation, one would want to free any state
  85. * structure that was allocated in the init hook and perform any
  86. * library-related closure that might be needed.
  87. *
  88. * @inout:
  89. * data_pointer ::
  90. * The SVG rendering module stores a pointer variable that can be used
  91. * by clients to store any data that needs to be shared across
  92. * different hooks. `data_pointer` is essentially a pointer to that
  93. * pointer such that it can be written to as well as read from.
  94. *
  95. * @since:
  96. * 2.12
  97. */
  98. typedef void
  99. (*SVG_Lib_Free_Func)( FT_Pointer *data_pointer );
  100. /**************************************************************************
  101. *
  102. * @functype:
  103. * SVG_Lib_Render_Func
  104. *
  105. * @description:
  106. * A callback that is called to render an OT-SVG glyph. This callback
  107. * hook is called right after the preset hook @SVG_Lib_Preset_Slot_Func
  108. * has been called with `cache` set to `TRUE`. The data necessary to
  109. * render is available through the handle @FT_SVG_Document, which is set
  110. * in the `other` field of @FT_GlyphSlotRec.
  111. *
  112. * The render hook is expected to render the SVG glyph to the bitmap
  113. * buffer that is allocated already at `slot->bitmap.buffer`. It also
  114. * sets the `num_grays` value as well as `slot->format`.
  115. *
  116. * @input:
  117. * slot ::
  118. * The slot to render.
  119. *
  120. * @inout:
  121. * data_pointer ::
  122. * The SVG rendering module stores a pointer variable that can be used
  123. * by clients to store any data that needs to be shared across
  124. * different hooks. `data_pointer` is essentially a pointer to that
  125. * pointer such that it can be written to as well as read from.
  126. *
  127. * @return:
  128. * FreeType error code. 0 means success.
  129. *
  130. * @since:
  131. * 2.12
  132. */
  133. typedef FT_Error
  134. (*SVG_Lib_Render_Func)( FT_GlyphSlot slot,
  135. FT_Pointer *data_pointer );
  136. /**************************************************************************
  137. *
  138. * @functype:
  139. * SVG_Lib_Preset_Slot_Func
  140. *
  141. * @description:
  142. * A callback that is called to preset the glyph slot. It is called from
  143. * two places.
  144. *
  145. * 1. When `FT_Load_Glyph` needs to preset the glyph slot.
  146. *
  147. * 2. Right before the `svg` module calls the render callback hook.
  148. *
  149. * When it is the former, the argument `cache` is set to `FALSE`. When
  150. * it is the latter, the argument `cache` is set to `TRUE`. This
  151. * distinction has been made because many calculations that are necessary
  152. * for presetting a glyph slot are the same needed later for the render
  153. * callback hook. Thus, if `cache` is `TRUE`, the hook can _cache_ those
  154. * calculations in a memory block referenced by the state pointer.
  155. *
  156. * This hook is expected to preset the slot by setting parameters such as
  157. * `bitmap_left`, `bitmap_top`, `width`, `rows`, `pitch`, and
  158. * `pixel_mode`. It is also expected to set all the metrics for the slot
  159. * including the vertical advance if it is not already set. Typically,
  160. * fonts have horizontal advances but not vertical ones. If those are
  161. * available, they had already been set, otherwise they have to be
  162. * estimated and set manually. The hook must take into account the
  163. * transformations that have been set, and translate the transformation
  164. * matrices into the SVG coordinate system, as the original matrix is
  165. * intended for the TTF/CFF coordinate system.
  166. *
  167. * @input:
  168. * slot ::
  169. * The glyph slot that has the SVG document loaded.
  170. *
  171. * cache ::
  172. * See description.
  173. *
  174. * @inout:
  175. * data_pointer ::
  176. * The SVG rendering module stores a pointer variable that can be used
  177. * by clients to store any data that needs to be shared across
  178. * different hooks. `data_pointer` is essentially a pointer to that
  179. * pointer such that it can be written to as well as read from.
  180. *
  181. * @return:
  182. * FreeType error code. 0 means success.
  183. *
  184. * @since:
  185. * 2.12
  186. */
  187. typedef FT_Error
  188. (*SVG_Lib_Preset_Slot_Func)( FT_GlyphSlot slot,
  189. FT_Bool cache,
  190. FT_Pointer *state );
  191. /**************************************************************************
  192. *
  193. * @struct:
  194. * SVG_RendererHooks
  195. *
  196. * @description:
  197. * A structure that stores the four hooks needed to render OT-SVG glyphs
  198. * properly. The structure is publicly used to set the hooks via the
  199. * @svg-hooks driver property.
  200. *
  201. * The behavior of each hook is described in its documentation. One
  202. * thing to note is that the preset hook and the render hook often need
  203. * to do the same operations; therefore, it's better to cache the
  204. * intermediate data in a state structure to avoid calculating it twice.
  205. * For example, in the preset hook one can draw the glyph on a recorder
  206. * surface and later create a bitmap surface from it in the render hook.
  207. *
  208. * All four hooks must be non-NULL.
  209. *
  210. * @fields:
  211. * init_svg ::
  212. * The initialization hook.
  213. *
  214. * free_svg ::
  215. * The cleanup hook.
  216. *
  217. * render_hook ::
  218. * The render hook.
  219. *
  220. * preset_slot ::
  221. * The preset hook.
  222. *
  223. * @since:
  224. * 2.12
  225. */
  226. typedef struct SVG_RendererHooks_
  227. {
  228. SVG_Lib_Init_Func init_svg;
  229. SVG_Lib_Free_Func free_svg;
  230. SVG_Lib_Render_Func render_svg;
  231. SVG_Lib_Preset_Slot_Func preset_slot;
  232. } SVG_RendererHooks;
  233. /**************************************************************************
  234. *
  235. * @struct:
  236. * FT_SVG_DocumentRec
  237. *
  238. * @description:
  239. * A structure that models one SVG document.
  240. *
  241. * @fields:
  242. * svg_document ::
  243. * A pointer to the SVG document.
  244. *
  245. * svg_document_length ::
  246. * The length of `svg_document`.
  247. *
  248. * metrics ::
  249. * A metrics object storing the size information.
  250. *
  251. * units_per_EM ::
  252. * The size of the EM square.
  253. *
  254. * start_glyph_id ::
  255. * The first glyph ID in the glyph range covered by this document.
  256. *
  257. * end_glyph_id ::
  258. * The last glyph ID in the glyph range covered by this document.
  259. *
  260. * transform ::
  261. * A 2x2 transformation matrix to apply to the glyph while rendering
  262. * it.
  263. *
  264. * delta ::
  265. * The translation to apply to the glyph while rendering.
  266. *
  267. * @note:
  268. * When an @FT_GlyphSlot object `slot` is passed down to a renderer, the
  269. * renderer can only access the `metrics` and `units_per_EM` fields via
  270. * `slot->face`. However, when @FT_Glyph_To_Bitmap sets up a dummy
  271. * object, it has no way to set a `face` object. Thus, metrics
  272. * information and `units_per_EM` (which is necessary for OT-SVG) has to
  273. * be stored separately.
  274. *
  275. * @since:
  276. * 2.12
  277. */
  278. typedef struct FT_SVG_DocumentRec_
  279. {
  280. FT_Byte* svg_document;
  281. FT_ULong svg_document_length;
  282. FT_Size_Metrics metrics;
  283. FT_UShort units_per_EM;
  284. FT_UShort start_glyph_id;
  285. FT_UShort end_glyph_id;
  286. FT_Matrix transform;
  287. FT_Vector delta;
  288. } FT_SVG_DocumentRec;
  289. /**************************************************************************
  290. *
  291. * @type:
  292. * FT_SVG_Document
  293. *
  294. * @description:
  295. * A handle to an @FT_SVG_DocumentRec object.
  296. *
  297. * @since:
  298. * 2.12
  299. */
  300. typedef struct FT_SVG_DocumentRec_* FT_SVG_Document;
  301. FT_END_HEADER
  302. #endif /* OTSVG_H_ */
  303. /* END */