hb-ot-color.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright © 2016 Google, Inc.
  3. * Copyright © 2018 Ebrahim Byagowi
  4. *
  5. * This is part of HarfBuzz, a text shaping library.
  6. *
  7. * Permission is hereby granted, without written agreement and without
  8. * license or royalty fees, to use, copy, modify, and distribute this
  9. * software and its documentation for any purpose, provided that the
  10. * above copyright notice and the following two paragraphs appear in
  11. * all copies of this software.
  12. *
  13. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  14. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  15. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  16. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  17. * DAMAGE.
  18. *
  19. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  20. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  21. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  22. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  23. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  24. *
  25. * Google Author(s): Sascha Brawer, Behdad Esfahbod
  26. */
  27. #include "hb.hh"
  28. #ifndef HB_NO_COLOR
  29. #include "hb-ot.h"
  30. #include "hb-ot-color-cbdt-table.hh"
  31. #include "hb-ot-color-colr-table.hh"
  32. #include "hb-ot-color-cpal-table.hh"
  33. #include "hb-ot-color-sbix-table.hh"
  34. #include "hb-ot-color-svg-table.hh"
  35. /**
  36. * SECTION:hb-ot-color
  37. * @title: hb-ot-color
  38. * @short_description: OpenType Color Fonts
  39. * @include: hb-ot.h
  40. *
  41. * Functions for fetching color-font information from OpenType font faces.
  42. *
  43. * HarfBuzz supports `COLR`/`CPAL`, `sbix`, `CBDT`, and `SVG` color fonts.
  44. **/
  45. /*
  46. * CPAL
  47. */
  48. /**
  49. * hb_ot_color_has_palettes:
  50. * @face: #hb_face_t to work upon
  51. *
  52. * Tests whether a face includes a `CPAL` color-palette table.
  53. *
  54. * Return value: `true` if data found, `false` otherwise
  55. *
  56. * Since: 2.1.0
  57. */
  58. hb_bool_t
  59. hb_ot_color_has_palettes (hb_face_t *face)
  60. {
  61. return face->table.CPAL->has_data ();
  62. }
  63. /**
  64. * hb_ot_color_palette_get_count:
  65. * @face: #hb_face_t to work upon
  66. *
  67. * Fetches the number of color palettes in a face.
  68. *
  69. * Return value: the number of palettes found
  70. *
  71. * Since: 2.1.0
  72. */
  73. unsigned int
  74. hb_ot_color_palette_get_count (hb_face_t *face)
  75. {
  76. return face->table.CPAL->get_palette_count ();
  77. }
  78. /**
  79. * hb_ot_color_palette_get_name_id:
  80. * @face: #hb_face_t to work upon
  81. * @palette_index: The index of the color palette
  82. *
  83. * Fetches the `name` table Name ID that provides display names for
  84. * a `CPAL` color palette.
  85. *
  86. * Palette display names can be generic (e.g., "Default") or provide
  87. * specific, themed names (e.g., "Spring", "Summer", "Fall", and "Winter").
  88. *
  89. * Return value: the Named ID found for the palette.
  90. * If the requested palette has no name the result is #HB_OT_NAME_ID_INVALID.
  91. *
  92. * Since: 2.1.0
  93. */
  94. hb_ot_name_id_t
  95. hb_ot_color_palette_get_name_id (hb_face_t *face,
  96. unsigned int palette_index)
  97. {
  98. return face->table.CPAL->get_palette_name_id (palette_index);
  99. }
  100. /**
  101. * hb_ot_color_palette_color_get_name_id:
  102. * @face: #hb_face_t to work upon
  103. * @color_index: The index of the color
  104. *
  105. * Fetches the `name` table Name ID that provides display names for
  106. * the specified color in a face's `CPAL` color palette.
  107. *
  108. * Display names can be generic (e.g., "Background") or specific
  109. * (e.g., "Eye color").
  110. *
  111. * Return value: the Name ID found for the color.
  112. *
  113. * Since: 2.1.0
  114. */
  115. hb_ot_name_id_t
  116. hb_ot_color_palette_color_get_name_id (hb_face_t *face,
  117. unsigned int color_index)
  118. {
  119. return face->table.CPAL->get_color_name_id (color_index);
  120. }
  121. /**
  122. * hb_ot_color_palette_get_flags:
  123. * @face: #hb_face_t to work upon
  124. * @palette_index: The index of the color palette
  125. *
  126. * Fetches the flags defined for a color palette.
  127. *
  128. * Return value: the #hb_ot_color_palette_flags_t of the requested color palette
  129. *
  130. * Since: 2.1.0
  131. */
  132. hb_ot_color_palette_flags_t
  133. hb_ot_color_palette_get_flags (hb_face_t *face,
  134. unsigned int palette_index)
  135. {
  136. return face->table.CPAL->get_palette_flags (palette_index);
  137. }
  138. /**
  139. * hb_ot_color_palette_get_colors:
  140. * @face: #hb_face_t to work upon
  141. * @palette_index: the index of the color palette to query
  142. * @start_offset: offset of the first color to retrieve
  143. * @color_count: (inout) (optional): Input = the maximum number of colors to return;
  144. * Output = the actual number of colors returned (may be zero)
  145. * @colors: (out) (array length=color_count) (nullable): The array of #hb_color_t records found
  146. *
  147. * Fetches a list of the colors in a color palette.
  148. *
  149. * After calling this function, @colors will be filled with the palette
  150. * colors. If @colors is NULL, the function will just return the number
  151. * of total colors without storing any actual colors; this can be used
  152. * for allocating a buffer of suitable size before calling
  153. * hb_ot_color_palette_get_colors() a second time.
  154. *
  155. * Return value: the total number of colors in the palette
  156. *
  157. * Since: 2.1.0
  158. */
  159. unsigned int
  160. hb_ot_color_palette_get_colors (hb_face_t *face,
  161. unsigned int palette_index,
  162. unsigned int start_offset,
  163. unsigned int *colors_count /* IN/OUT. May be NULL. */,
  164. hb_color_t *colors /* OUT. May be NULL. */)
  165. {
  166. return face->table.CPAL->get_palette_colors (palette_index, start_offset, colors_count, colors);
  167. }
  168. /*
  169. * COLR
  170. */
  171. /**
  172. * hb_ot_color_has_layers:
  173. * @face: #hb_face_t to work upon
  174. *
  175. * Tests whether a face includes any `COLR` color layers.
  176. *
  177. * Return value: `true` if data found, `false` otherwise
  178. *
  179. * Since: 2.1.0
  180. */
  181. hb_bool_t
  182. hb_ot_color_has_layers (hb_face_t *face)
  183. {
  184. return face->table.COLR->has_data ();
  185. }
  186. /**
  187. * hb_ot_color_glyph_get_layers:
  188. * @face: #hb_face_t to work upon
  189. * @glyph: The glyph index to query
  190. * @start_offset: offset of the first layer to retrieve
  191. * @layer_count: (inout) (optional): Input = the maximum number of layers to return;
  192. * Output = the actual number of layers returned (may be zero)
  193. * @layers: (out) (array length=layer_count) (nullable): The array of layers found
  194. *
  195. * Fetches a list of all color layers for the specified glyph index in the specified
  196. * face. The list returned will begin at the offset provided.
  197. *
  198. * Return value: Total number of layers available for the glyph index queried
  199. *
  200. * Since: 2.1.0
  201. */
  202. unsigned int
  203. hb_ot_color_glyph_get_layers (hb_face_t *face,
  204. hb_codepoint_t glyph,
  205. unsigned int start_offset,
  206. unsigned int *layer_count, /* IN/OUT. May be NULL. */
  207. hb_ot_color_layer_t *layers /* OUT. May be NULL. */)
  208. {
  209. return face->table.COLR->get_glyph_layers (glyph, start_offset, layer_count, layers);
  210. }
  211. /*
  212. * SVG
  213. */
  214. /**
  215. * hb_ot_color_has_svg:
  216. * @face: #hb_face_t to work upon.
  217. *
  218. * Tests whether a face includes any `SVG` glyph images.
  219. *
  220. * Return value: `true` if data found, `false` otherwise.
  221. *
  222. * Since: 2.1.0
  223. */
  224. hb_bool_t
  225. hb_ot_color_has_svg (hb_face_t *face)
  226. {
  227. return face->table.SVG->has_data ();
  228. }
  229. /**
  230. * hb_ot_color_glyph_reference_svg:
  231. * @face: #hb_face_t to work upon
  232. * @glyph: a svg glyph index
  233. *
  234. * Fetches the SVG document for a glyph. The blob may be either plain text or gzip-encoded.
  235. *
  236. * If the glyph has no SVG document, the singleton empty blob is returned.
  237. *
  238. * Return value: (transfer full): An #hb_blob_t containing the SVG document of the glyph, if available
  239. *
  240. * Since: 2.1.0
  241. */
  242. hb_blob_t *
  243. hb_ot_color_glyph_reference_svg (hb_face_t *face, hb_codepoint_t glyph)
  244. {
  245. return face->table.SVG->reference_blob_for_glyph (glyph);
  246. }
  247. /*
  248. * PNG: CBDT or sbix
  249. */
  250. /**
  251. * hb_ot_color_has_png:
  252. * @face: #hb_face_t to work upon
  253. *
  254. * Tests whether a face has PNG glyph images (either in `CBDT` or `sbix` tables).
  255. *
  256. * Return value: `true` if data found, `false` otherwise
  257. *
  258. * Since: 2.1.0
  259. */
  260. hb_bool_t
  261. hb_ot_color_has_png (hb_face_t *face)
  262. {
  263. return face->table.CBDT->has_data () || face->table.sbix->has_data ();
  264. }
  265. /**
  266. * hb_ot_color_glyph_reference_png:
  267. * @font: #hb_font_t to work upon
  268. * @glyph: a glyph index
  269. *
  270. * Fetches the PNG image for a glyph. This function takes a font object, not a face object,
  271. * as input. To get an optimally sized PNG blob, the PPEM values must be set on the @font
  272. * object. If PPEM is unset, the blob returned will be the largest PNG available.
  273. *
  274. * If the glyph has no PNG image, the singleton empty blob is returned.
  275. *
  276. * Return value: (transfer full): An #hb_blob_t containing the PNG image for the glyph, if available
  277. *
  278. * Since: 2.1.0
  279. */
  280. hb_blob_t *
  281. hb_ot_color_glyph_reference_png (hb_font_t *font, hb_codepoint_t glyph)
  282. {
  283. hb_blob_t *blob = hb_blob_get_empty ();
  284. if (font->face->table.sbix->has_data ())
  285. blob = font->face->table.sbix->reference_png (font, glyph, nullptr, nullptr, nullptr);
  286. if (!blob->length && font->face->table.CBDT->has_data ())
  287. blob = font->face->table.CBDT->reference_png (font, glyph);
  288. return blob;
  289. }
  290. #endif