Font.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later */
  2. /* Copyright 2010, SIL International, All rights reserved. */
  3. #pragma once
  4. #include "graphite2/Types.h"
  5. #define GR2_VERSION_MAJOR 1
  6. #define GR2_VERSION_MINOR 3
  7. #define GR2_VERSION_BUGFIX 14
  8. #ifdef __cplusplus
  9. extern "C"
  10. {
  11. #endif
  12. typedef struct gr_face gr_face;
  13. typedef struct gr_font gr_font;
  14. typedef struct gr_feature_ref gr_feature_ref;
  15. typedef struct gr_feature_val gr_feature_val;
  16. /**
  17. * Returns version information on this engine
  18. */
  19. GR2_API void gr_engine_version(int *nMajor, int *nMinor, int *nBugFix);
  20. /**
  21. * The Face Options allow the application to require that certain tables are
  22. * read during face construction. This may be of concern if the appFaceHandle
  23. * used in the gr_get_table_fn may change.
  24. * The values can be combined
  25. */
  26. enum gr_face_options {
  27. /** No preload, no cmap caching, fail if the graphite tables are invalid */
  28. gr_face_default = 0,
  29. /** Dumb rendering will be enabled if the graphite tables are invalid. @deprecated Since 1.311 */
  30. gr_face_dumbRendering = 1,
  31. /** preload glyphs at construction time */
  32. gr_face_preloadGlyphs = 2,
  33. /** Cache the lookup from code point to glyph ID at construction time */
  34. gr_face_cacheCmap = 4,
  35. /** Preload everything */
  36. gr_face_preloadAll = gr_face_preloadGlyphs | gr_face_cacheCmap
  37. };
  38. /** Holds information about a particular Graphite silf table that has been loaded */
  39. struct gr_faceinfo {
  40. gr_uint16 extra_ascent; /**< The extra_ascent in the GDL, in design units */
  41. gr_uint16 extra_descent; /**< The extra_descent in the GDL, in design units */
  42. gr_uint16 upem; /**< The design units for the font */
  43. enum gr_space_contextuals {
  44. gr_space_unknown = 0, /**< no information is known. */
  45. gr_space_none = 1, /**< the space character never occurs in any rules. */
  46. gr_space_left_only = 2, /**< the space character only occurs as the first element in a rule. */
  47. gr_space_right_only = 3, /**< the space character only occurs as the last element in a rule. */
  48. gr_space_either_only = 4, /**< the space character only occurs as the only element in a rule. */
  49. gr_space_both = 5, /**< the space character may occur as the first or last element of a rule. */
  50. gr_space_cross = 6 /**< the space character occurs in a rule not as a first or last element. */
  51. } space_contextuals;
  52. unsigned int has_bidi_pass : 1; /**< the table specifies that a bidirectional pass should run */
  53. unsigned int line_ends : 1; /**< there are line end contextuals somewhere */
  54. unsigned int justifies : 1; /**< there are .justify properties set somewhere on some glyphs */
  55. };
  56. typedef struct gr_faceinfo gr_faceinfo;
  57. /** type describing function to retrieve font table information
  58. *
  59. * @return a pointer to the table in memory. The pointed to memory must exist as
  60. * long as the gr_face which makes the call.
  61. * @param appFaceHandle is the unique information passed to gr_make_face()
  62. * @param name is a 32bit tag to the table name.
  63. * @param len returned by this function to say how long the table is in memory.
  64. */
  65. typedef const void *(*gr_get_table_fn)(const void* appFaceHandle, unsigned int name, size_t *len);
  66. /** type describing function to release any resources allocated by the above get_table table function
  67. *
  68. * @param appFaceHandle is the unique information passed to gr_make_face()
  69. * @param pointer to table memory returned by get_table.
  70. */
  71. typedef void (*gr_release_table_fn)(const void* appFaceHandle, const void *table_buffer);
  72. /** struct housing function pointers to manage font table buffers for the graphite engine. */
  73. struct gr_face_ops
  74. {
  75. /** size in bytes of this structure */
  76. size_t size;
  77. /** a pointer to a function to request a table from the client. */
  78. gr_get_table_fn get_table;
  79. /** is a pointer to a function to notify the client the a table can be released.
  80. * This can be NULL to signify that the client does not wish to do any release handling. */
  81. gr_release_table_fn release_table;
  82. };
  83. typedef struct gr_face_ops gr_face_ops;
  84. /** Create a gr_face object given application information and a table functions.
  85. *
  86. * @return gr_face or NULL if the font fails to load for some reason.
  87. * @param appFaceHandle This is application specific information that is passed
  88. * to the getTable function. The appFaceHandle must stay
  89. * alive as long as the gr_face is alive.
  90. * @param face_ops Pointer to face specific callback structure for table
  91. * management. Must stay alive for the duration of the
  92. * call only.
  93. * @param faceOptions Bitfield describing various options. See enum gr_face_options for details.
  94. */
  95. GR2_API gr_face* gr_make_face_with_ops(const void* appFaceHandle/*non-NULL*/, const gr_face_ops *face_ops, unsigned int faceOptions);
  96. /** @deprecated Since v1.2.0 in favour of gr_make_face_with_ops.
  97. * Create a gr_face object given application information and a getTable function.
  98. *
  99. * @return gr_face or NULL if the font fails to load for some reason.
  100. * @param appFaceHandle This is application specific information that is passed
  101. * to the getTable function. The appFaceHandle must stay
  102. * alive as long as the gr_face is alive.
  103. * @param getTable Callback function to get table data.
  104. * @param faceOptions Bitfield describing various options. See enum gr_face_options for details.
  105. */
  106. GR2_DEPRECATED_API gr_face* gr_make_face(const void* appFaceHandle/*non-NULL*/, gr_get_table_fn getTable, unsigned int faceOptions);
  107. /** @deprecated Since 1.3.7 this function is now an alias for gr_make_face_with_ops().
  108. *
  109. * Create a gr_face object given application information, with subsegmental caching support
  110. *
  111. * @return gr_face or NULL if the font fails to load.
  112. * @param appFaceHandle is a pointer to application specific information that is passed to getTable.
  113. * This may not be NULL and must stay alive as long as the gr_face is alive.
  114. * @param face_ops Pointer to face specific callback structure for table management. Must stay
  115. * alive for the duration of the call only.
  116. * @param segCacheMaxSize Unused.
  117. * @param faceOptions Bitfield of values from enum gr_face_options
  118. */
  119. GR2_DEPRECATED_API gr_face* gr_make_face_with_seg_cache_and_ops(const void* appFaceHandle, const gr_face_ops *face_ops, unsigned int segCacheMaxSize, unsigned int faceOptions);
  120. /** @deprecated Since 1.3.7 this function is now an alias for gr_make_face().
  121. *
  122. * Create a gr_face object given application information, with subsegmental caching support.
  123. * This function is deprecated as of v1.2.0 in favour of gr_make_face_with_seg_cache_and_ops.
  124. *
  125. * @return gr_face or NULL if the font fails to load.
  126. * @param appFaceHandle is a pointer to application specific information that is passed to getTable.
  127. * This may not be NULL and must stay alive as long as the gr_face is alive.
  128. * @param getTable The function graphite calls to access font table data
  129. * @param segCacheMaxSize How large the segment cache is.
  130. * @param faceOptions Bitfield of values from enum gr_face_options
  131. */
  132. GR2_DEPRECATED_API gr_face* gr_make_face_with_seg_cache(const void* appFaceHandle, gr_get_table_fn getTable, unsigned int segCacheMaxSize, unsigned int faceOptions);
  133. /** Convert a tag in a string into a gr_uint32
  134. *
  135. * @return gr_uint32 tag, zero padded
  136. * @param str a nul terminated string of which at most the first 4 characters are read
  137. */
  138. GR2_API gr_uint32 gr_str_to_tag(const char *str);
  139. /** Convert a gr_uint32 tag into a string
  140. *
  141. * @param tag contains the tag to convert
  142. * @param str is a pointer to a char array of at least size 4 bytes. The first 4 bytes of this array
  143. * will be overwritten by this function. No nul is appended.
  144. */
  145. GR2_API void gr_tag_to_str(gr_uint32 tag, char *str);
  146. /** Get feature values for a given language or default
  147. *
  148. * @return a copy of the default feature values for a given language. The application must call
  149. * gr_featureval_destroy() to free this object when done.
  150. * @param pFace The font face to get feature values from
  151. * @param langname The language tag to get feature values for. If there is no such language or
  152. * langname is 0, the default feature values for the font are returned.
  153. * langname is right 0 padded and assumes lowercase. Thus the en langauge
  154. * would be 0x656E0000. Langname may also be space padded, thus 0x656E2020.
  155. */
  156. GR2_API gr_feature_val* gr_face_featureval_for_lang(const gr_face* pFace, gr_uint32 langname);
  157. /** Get feature reference for a given feature id from a face
  158. *
  159. * @return a feature reference corresponding to the given id. This data is part of the gr_face and
  160. * will be freed when the face is destroyed.
  161. * @param pFace Font face to get information on.
  162. * @param featId Feature id tag to get reference to.
  163. */
  164. GR2_API const gr_feature_ref* gr_face_find_fref(const gr_face* pFace, gr_uint32 featId);
  165. /** Returns number of feature references in a face **/
  166. GR2_API gr_uint16 gr_face_n_fref(const gr_face* pFace);
  167. /** Returns feature reference at given index in face **/
  168. GR2_API const gr_feature_ref* gr_face_fref(const gr_face* pFace, gr_uint16 i);
  169. /** Return number of languages the face knows about **/
  170. GR2_API unsigned short gr_face_n_languages(const gr_face* pFace);
  171. /** Returns a language id corresponding to a language of given index in the face **/
  172. GR2_API gr_uint32 gr_face_lang_by_index(const gr_face* pFace, gr_uint16 i);
  173. /** Destroy the given face and free its memory **/
  174. GR2_API void gr_face_destroy(gr_face *face);
  175. /** Returns the number of glyphs in the face **/
  176. GR2_API unsigned short gr_face_n_glyphs(const gr_face* pFace);
  177. /** Returns a faceinfo for the face and script **/
  178. GR2_API const gr_faceinfo *gr_face_info(const gr_face *pFace, gr_uint32 script);
  179. /** Returns whether the font supports a given Unicode character
  180. *
  181. * @return true if the character is supported.
  182. * @param pFace face to test within
  183. * @param usv Unicode Scalar Value of character to test
  184. * @param script Tag of script for selecting which set of pseudo glyphs to test. May be NULL.
  185. */
  186. GR2_API int gr_face_is_char_supported(const gr_face *pFace, gr_uint32 usv, gr_uint32 script);
  187. #ifndef GRAPHITE2_NFILEFACE
  188. /** Create gr_face from a font file
  189. *
  190. * @return gr_face that accesses a font file directly. Returns NULL on failure.
  191. * @param filename Full path and filename to font file
  192. * @param faceOptions Bitfile from enum gr_face_options to control face options.
  193. */
  194. GR2_API gr_face* gr_make_file_face(const char *filename, unsigned int faceOptions);
  195. /** @deprecated Since 1.3.7. This function is now an alias for gr_make_file_face().
  196. *
  197. * Create gr_face from a font file, with subsegment caching support.
  198. *
  199. * @return gr_face that accesses a font file directly. Returns NULL on failure.
  200. * @param filename Full path and filename to font file
  201. * @param segCacheMaxSize Specifies how big to make the cache in segments.
  202. * @param faceOptions Bitfield from enum gr_face_options to control face options.
  203. */
  204. GR2_DEPRECATED_API gr_face* gr_make_file_face_with_seg_cache(const char *filename, unsigned int segCacheMaxSize, unsigned int faceOptions);
  205. #endif // !GRAPHITE2_NFILEFACE
  206. /** Create a font from a face
  207. *
  208. * @return gr_font Call font_destroy to free this font
  209. * @param ppm Resolution of the font in pixels per em
  210. * @param face Face this font corresponds to. This must stay alive as long as the font is alive.
  211. */
  212. GR2_API gr_font* gr_make_font(float ppm, const gr_face *face);
  213. /** query function to find the hinted advance of a glyph
  214. *
  215. * @param appFontHandle is the unique information passed to gr_make_font_with_advance()
  216. * @param glyphid is the glyph to retireve the hinted advance for.
  217. */
  218. typedef float (*gr_advance_fn)(const void* appFontHandle, gr_uint16 glyphid);
  219. /** struct housing function pointers to manage font hinted metrics for the
  220. * graphite engine. */
  221. struct gr_font_ops
  222. {
  223. /** size of the structure in bytes to allow for future extensibility */
  224. size_t size;
  225. /** a pointer to a function to retrieve the hinted
  226. * advance width of a glyph which the font cannot
  227. * provide without client assistance. This can be
  228. * NULL to signify no horizontal hinted metrics are necessary. */
  229. gr_advance_fn glyph_advance_x;
  230. /** a pointer to a function to retrieve the hinted
  231. * advance height of a glyph which the font cannot
  232. * provide without client assistance. This can be
  233. * NULL to signify no horizontal hinted metrics are necessary. */
  234. gr_advance_fn glyph_advance_y;
  235. };
  236. typedef struct gr_font_ops gr_font_ops;
  237. /** Creates a font with hinted advance width query functions
  238. *
  239. * @return gr_font to be destroyed via font_destroy
  240. * @param ppm size of font in pixels per em
  241. * @param appFontHandle font specific information that must stay alive as long
  242. * as the font does
  243. * @param font_ops pointer font specific callback structure for hinted metrics.
  244. * Need only stay alive for the duration of the call.
  245. * @param face the face this font corresponds to. Must stay alive as long as
  246. * the font does.
  247. */
  248. GR2_API gr_font* gr_make_font_with_ops(float ppm, const void* appFontHandle, const gr_font_ops * font_ops, const gr_face *face);
  249. /** Creates a font with hinted advance width query function.
  250. * This function is deprecated. Use gr_make_font_with_ops instead.
  251. *
  252. * @return gr_font to be destroyed via font_destroy
  253. * @param ppm size of font in pixels per em
  254. * @param appFontHandle font specific information that must stay alive as long
  255. * as the font does
  256. * @param getAdvance callback function reference that returns horizontal advance in pixels for a glyph.
  257. * @param face the face this font corresponds to. Must stay alive as long as
  258. * the font does.
  259. */
  260. GR2_API gr_font* gr_make_font_with_advance_fn(float ppm, const void* appFontHandle, gr_advance_fn getAdvance, const gr_face *face);
  261. /** Free a font **/
  262. GR2_API void gr_font_destroy(gr_font *font);
  263. /** get a feature value
  264. *
  265. * @return value of specific feature or 0 if any problems.
  266. * @param pfeatureref gr_feature_ref to the feature
  267. * @param feats gr_feature_val containing all the values
  268. */
  269. GR2_API gr_uint16 gr_fref_feature_value(const gr_feature_ref* pfeatureref, const gr_feature_val* feats);
  270. /** set a feature value
  271. *
  272. * @return false if there were any problems (value out of range, etc.)
  273. * @param pfeatureref gr_feature_ref to the feature
  274. * @param val value to set the feature to
  275. * @param pDest the gr_feature_val containing all the values for all the features
  276. */
  277. GR2_API int gr_fref_set_feature_value(const gr_feature_ref* pfeatureref, gr_uint16 val, gr_feature_val* pDest);
  278. /** Returns the id tag for a gr_feature_ref **/
  279. GR2_API gr_uint32 gr_fref_id(const gr_feature_ref* pfeatureref);
  280. /** Returns number of values a feature may take, given a gr_feature_ref **/
  281. GR2_API gr_uint16 gr_fref_n_values(const gr_feature_ref* pfeatureref);
  282. /** Returns the value associated with a particular value in a feature
  283. *
  284. * @return value
  285. * @param pfeatureref gr_feature_ref of the feature of interest
  286. * @param settingno Index up to the return value of gr_fref_n_values() of the value
  287. */
  288. GR2_API gr_int16 gr_fref_value(const gr_feature_ref* pfeatureref, gr_uint16 settingno);
  289. /** Returns a string of the UI name of a feature
  290. *
  291. * @return string of the UI name, in the encoding form requested. Call gr_label_destroy() after use.
  292. * @param pfeatureref gr_feature_ref of the feature
  293. * @param langId This is a pointer since the face may not support a string in the requested
  294. * language. The actual language of the string is returned in langId
  295. * @param utf Encoding form for the string
  296. * @param length Used to return the length of the string returned in bytes.
  297. */
  298. GR2_API void* gr_fref_label(const gr_feature_ref* pfeatureref, gr_uint16 *langId, enum gr_encform utf, gr_uint32 *length);
  299. /** Return a UI string for a possible value of a feature
  300. *
  301. * @return string of the UI name, in the encoding form requested. nul terminated. Call gr_label_destroy()
  302. * after use.
  303. * @param pfeatureref gr_feature_ref of the feature
  304. * @param settingno Value setting index
  305. * @param langId This is a pointer to the requested language. The requested language id is
  306. * replaced by the actual language id of the string returned.
  307. * @param utf Encoding form for the string
  308. * @param length Returns the length of the string returned in bytes.
  309. */
  310. GR2_API void* gr_fref_value_label(const gr_feature_ref* pfeatureref, gr_uint16 settingno/*rather than a value*/, gr_uint16 *langId, enum gr_encform utf, gr_uint32 *length);
  311. /** Destroy a previously returned label string **/
  312. GR2_API void gr_label_destroy(void * label);
  313. /** Copies a gr_feature_val **/
  314. GR2_API gr_feature_val* gr_featureval_clone(const gr_feature_val* pfeatures);
  315. /** Destroys a gr_feature_val **/
  316. GR2_API void gr_featureval_destroy(gr_feature_val *pfeatures);
  317. #ifdef __cplusplus
  318. }
  319. #endif