font.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. #ifndef MUPDF_FITZ_FONT_H
  2. #define MUPDF_FITZ_FONT_H
  3. #include "mupdf/fitz/system.h"
  4. #include "mupdf/fitz/context.h"
  5. #include "mupdf/fitz/geometry.h"
  6. #include "mupdf/fitz/buffer.h"
  7. /* forward declaration for circular dependency */
  8. struct fz_device_s;
  9. /*
  10. An abstract font handle.
  11. */
  12. typedef struct fz_font_s fz_font;
  13. /*
  14. * Fonts come in two variants:
  15. * Regular fonts are handled by FreeType.
  16. * Type 3 fonts have callbacks to the interpreter.
  17. */
  18. /*
  19. fz_font_ft_face: Retrieve the FT_Face handle
  20. for the font.
  21. font: The font to query
  22. Returns the FT_Face handle for the font, or NULL
  23. if not a freetype handled font. (Cast to void *
  24. to avoid nasty header exposure).
  25. */
  26. void *fz_font_ft_face(fz_context *ctx, fz_font *font);
  27. /*
  28. fz_font_t3_procs: Retrieve the Type3 procs
  29. for a font.
  30. font: The font to query
  31. Returns the t3_procs pointer. Will be NULL for a
  32. non type-3 font.
  33. */
  34. fz_buffer **fz_font_t3_procs(fz_context *ctx, fz_font *font);
  35. /*
  36. ft_error_string: map an FT error number to a
  37. static string.
  38. err: The error number to lookup.
  39. Returns a pointer to a static textual representation
  40. of a freetype error.
  41. */
  42. const char *ft_error_string(int err);
  43. /* common CJK font collections */
  44. enum { FZ_ADOBE_CNS_1, FZ_ADOBE_GB_1, FZ_ADOBE_JAPAN_1, FZ_ADOBE_KOREA_1 };
  45. /*
  46. fz_font_flags_t: Every fz_font carries a set of flags
  47. within it, in a fz_font_flags_t structure.
  48. */
  49. typedef struct
  50. {
  51. unsigned int is_mono : 1;
  52. unsigned int is_serif : 1;
  53. unsigned int is_bold : 1;
  54. unsigned int is_italic : 1;
  55. unsigned int ft_substitute : 1; /* use substitute metrics */
  56. unsigned int ft_stretch : 1; /* stretch to match PDF metrics */
  57. unsigned int fake_bold : 1; /* synthesize bold */
  58. unsigned int fake_italic : 1; /* synthesize italic */
  59. unsigned int force_hinting : 1; /* force hinting for DynaLab fonts */
  60. unsigned int has_opentype : 1; /* has opentype shaping tables */
  61. unsigned int invalid_bbox : 1;
  62. unsigned int use_glyph_bbox : 1;
  63. } fz_font_flags_t;
  64. /*
  65. fz_font_flags: Retrieve a pointer to the font flags
  66. for a given font. These can then be updated as required.
  67. font: The font to query
  68. Returns a pointer to the flags structure (or NULL, if
  69. the font is NULL).
  70. */
  71. fz_font_flags_t *fz_font_flags(fz_font *font);
  72. /*
  73. fz_shaper_data_t: In order to shape a given font, we need to
  74. declare it to a shaper library (harfbuzz, by default, but others
  75. are possible). To avoid redeclaring it every time we need to
  76. shape, we hold a shaper handle and the destructor for it within
  77. the font itself. The handle is initialised by the caller when
  78. first required and the destructor is called when the fz_font is
  79. destroyed.
  80. */
  81. typedef struct
  82. {
  83. void *shaper_handle;
  84. void (*destroy)(fz_context *ctx, void *); /* Destructor for shape_handle */
  85. } fz_shaper_data_t;
  86. /*
  87. fz_shaper_data_t: Retrieve a pointer to the shaper data
  88. structure for the given font.
  89. font: The font to query.
  90. Returns a pointer to the shaper data structure (or NULL if
  91. font is NULL).
  92. */
  93. fz_shaper_data_t *fz_font_shaper_data(fz_context *ctx, fz_font *font);
  94. /*
  95. fz_font_name: Retrieve a pointer to the name of the font.
  96. font: The font to query.
  97. Returns a pointer to an internal copy of the font name.
  98. Will never be NULL, but may be the empty string.
  99. */
  100. const char *fz_font_name(fz_context *ctx, fz_font *font);
  101. /*
  102. fz_font_is_bold: Returns true if the font is bold.
  103. */
  104. int fz_font_is_bold(fz_context *ctx, fz_font *font);
  105. /*
  106. fz_font_is_italic: Returns true if the font is italic.
  107. */
  108. int fz_font_is_italic(fz_context *ctx, fz_font *font);
  109. /*
  110. fz_font_is_serif: Returns true if the font is serif.
  111. */
  112. int fz_font_is_serif(fz_context *ctx, fz_font *font);
  113. /*
  114. fz_font_is_monospaced: Returns true if the font is monospaced.
  115. */
  116. int fz_font_is_monospaced(fz_context *ctx, fz_font *font);
  117. /*
  118. fz_font_bbox: Retrieve a pointer to the font bbox.
  119. font: The font to query.
  120. Returns a pointer to the font bbox (or NULL if the
  121. font is NULL).
  122. */
  123. fz_rect *fz_font_bbox(fz_context *ctx, fz_font *font);
  124. /*
  125. fz_load_system_font_fn: Type for user supplied system font loading hook.
  126. name: The name of the font to load.
  127. bold: 1 if a bold font desired, 0 otherwise.
  128. italic: 1 if an italic font desired, 0 otherwise.
  129. needs_exact_metrics: 1 if an exact metric match is required for the font requested.
  130. Returns a new font handle, or NULL if no font found (or on error).
  131. */
  132. typedef fz_font *(*fz_load_system_font_fn)(fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics);
  133. /*
  134. fz_load_system_cjk_font_fn: Type for user supplied cjk font loading hook.
  135. name: The name of the font to load.
  136. ros: The registry from which to load the font (e.g. FZ_ADOBE_KOREA_1)
  137. serif: 1 if a serif font is desired, 0 otherwise.
  138. Returns a new font handle, or NULL if no font found (or on error).
  139. */
  140. typedef fz_font *(*fz_load_system_cjk_font_fn)(fz_context *ctx, const char *name, int ros, int serif);
  141. /*
  142. fz_load_system_fallback_font_fn: Type for user supplied fallback font loading hook.
  143. name: The name of the font to load.
  144. script: UCDN script enum.
  145. language: FZ_LANG enum.
  146. serif, bold, italic: boolean style flags.
  147. Returns a new font handle, or NULL if no font found (or on error).
  148. */
  149. typedef fz_font *(*fz_load_system_fallback_font_fn)(fz_context *ctx, int script, int language, int serif, int bold, int italic);
  150. /*
  151. fz_install_load_system_font_fn: Install functions to allow
  152. MuPDF to request fonts from the system.
  153. Only one set of hooks can be in use at a time.
  154. */
  155. void fz_install_load_system_font_funcs(fz_context *ctx,
  156. fz_load_system_font_fn f,
  157. fz_load_system_cjk_font_fn f_cjk,
  158. fz_load_system_fallback_font_fn f_fallback);
  159. /* fz_load_*_font returns NULL if no font could be loaded (also on error) */
  160. /*
  161. fz_load_system_font: Attempt to load a given font from the
  162. system.
  163. name: The name of the desired font.
  164. bold: 1 if bold desired, 0 otherwise.
  165. italic: 1 if italic desired, 0 otherwise.
  166. needs_exact_metrics: 1 if an exact metrical match is required,
  167. 0 otherwise.
  168. Returns a new font handle, or NULL if no matching font was found
  169. (or on error).
  170. */
  171. fz_font *fz_load_system_font(fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics);
  172. /*
  173. fz_load_system_cjk_font: Attempt to load a given font from
  174. the system.
  175. name: The name of the desired font.
  176. ros: The registry to load the font from (e.g. FZ_ADOBE_KOREA_1)
  177. serif: 1 if serif desired, 0 otherwise.
  178. Returns a new font handle, or NULL if no matching font was found
  179. (or on error).
  180. */
  181. fz_font *fz_load_system_cjk_font(fz_context *ctx, const char *name, int ros, int serif);
  182. /*
  183. fz_lookup_builtin_font: Search the builtin fonts for a match.
  184. Whether a given font is present or not will depend on the
  185. configuration in which MuPDF is built.
  186. name: The name of the font desired.
  187. bold: 1 if bold desired, 0 otherwise.
  188. italic: 1 if italic desired, 0 otherwise.
  189. len: Pointer to a place to receive the length of the discovered
  190. font buffer.
  191. Returns a pointer to the font file data, or NULL if not present.
  192. */
  193. const char *fz_lookup_builtin_font(fz_context *ctx, const char *name, int bold, int italic, int *len);
  194. /*
  195. fz_lookup_base14_font: Search the builtin base14 fonts for a match.
  196. Whether a given font is present or not will depend on the
  197. configuration in which MuPDF is built.
  198. name: The name of the font desired.
  199. len: Pointer to a place to receive the length of the discovered
  200. font buffer.
  201. Returns a pointer to the font file data, or NULL if not present.
  202. */
  203. const char *fz_lookup_base14_font(fz_context *ctx, const char *name, int *len);
  204. /*
  205. fz_lookup_cjk_font: Search the builtin cjk fonts for a match.
  206. Whether a font is present or not will depend on the
  207. configuration in which MuPDF is built.
  208. registry: The desired registry to lookup in (e.g.
  209. FZ_ADOBE_KOREA_1)
  210. serif: 1 if serif desired, 0 otherwise.
  211. wmode: 1 for vertical mode, 0 for horizontal.
  212. len: Pointer to a place to receive the length of the discovered
  213. font buffer.
  214. index: Pointer to a place to store the index of the discovered
  215. font.
  216. Returns a pointer to the font file data, or NULL if not present.
  217. */
  218. const char *fz_lookup_cjk_font(fz_context *ctx, int registry, int serif, int wmode, int *len, int *index);
  219. /*
  220. fz_lookup_noto_font: Search the builtin noto fonts for a match.
  221. Whether a font is present or not will depend on the
  222. configuration in which MuPDF is built.
  223. script: The script desired (e.g. UCDN_SCRIPT_KATAKANA)
  224. lang: The language desired (e.g. FZ_LANG_ja)
  225. serif: 1 if serif desired, 0 otherwise.
  226. len: Pointer to a place to receive the length of the discovered
  227. font buffer.
  228. Returns a pointer to the font file data, or NULL if not present.
  229. */
  230. const char *fz_lookup_noto_font(fz_context *ctx, int script, int lang, int serif, int *len);
  231. /*
  232. fz_lookup_noto_symbol_font: Search the builtin noto fonts
  233. for a symbol font. Whether a font is present or not will
  234. depend on the configuration in which MuPDF is built.
  235. len: Pointer to a place to receive the length of the discovered
  236. font buffer.
  237. Returns a pointer to the font file data, or NULL if not present.
  238. */
  239. const char *fz_lookup_noto_symbol_font(fz_context *ctx, int *len);
  240. /*
  241. fz_lookup_noto_emoji_font: Search the builtin noto fonts
  242. for an emoji font. Whether a font is present or not will
  243. depend on the configuration in which MuPDF is built.
  244. len: Pointer to a place to receive the length of the discovered
  245. font buffer.
  246. Returns a pointer to the font file data, or NULL if not present.
  247. */
  248. const char *fz_lookup_noto_emoji_font(fz_context *ctx, int *len);
  249. /*
  250. fz_load_fallback_font: Try to load a fallback font for the
  251. given combination of font attributes. Whether a font is
  252. present or not will depend on the configuration in which
  253. MuPDF is built.
  254. script: The script desired (e.g. UCDN_SCRIPT_KATAKANA)
  255. language: The language desired (e.g. FZ_LANG_ja)
  256. serif: 1 if serif desired, 0 otherwise.
  257. bold: 1 if bold desired, 0 otherwise.
  258. italic: 1 if italic desired, 0 otherwise.
  259. Returns a new font handle, or NULL if not available.
  260. */
  261. fz_font *fz_load_fallback_font(fz_context *ctx, int script, int language, int serif, int bold, int italic);
  262. /*
  263. fz_load_fallback_symbol_font: Try to load a fallback
  264. symbol font. Whether a font is present or not will
  265. depend on the configuration in which MuPDF is built.
  266. Returns a new font handle, or NULL if not available.
  267. */
  268. fz_font *fz_load_fallback_symbol_font(fz_context *ctx);
  269. /*
  270. fz_load_fallback_emoji_font: Try to load a fallback
  271. emoji font. Whether a font is present or not will
  272. depend on the configuration in which MuPDF is built.
  273. Returns a new font handle, or NULL if not available.
  274. */
  275. fz_font *fz_load_fallback_emoji_font(fz_context *ctx);
  276. /*
  277. fz_new_type3_font: Create a new (empty) type3 font.
  278. name: Name of font (or NULL).
  279. matrix: Font matrix.
  280. Returns a new font handle, or throws exception on
  281. allocation failure.
  282. */
  283. fz_font *fz_new_type3_font(fz_context *ctx, const char *name, const fz_matrix *matrix);
  284. /*
  285. fz_new_font_from_memory: Create a new font from a font
  286. file in memory.
  287. name: Name of font (leave NULL to use name from font).
  288. data: Pointer to the font file data.
  289. len: Length of the font file data.
  290. index: Which font from the file to load (0 for default).
  291. use_glyph_box: 1 if we should use the glyph bbox, 0 otherwise.
  292. Returns new font handle, or throws exception on error.
  293. */
  294. fz_font *fz_new_font_from_memory(fz_context *ctx, const char *name, const char *data, int len, int index, int use_glyph_bbox);
  295. /*
  296. fz_new_font_from_buffer: Create a new font from a font
  297. file in a fz_buffer.
  298. name: Name of font (leave NULL to use name from font).
  299. buffer: Buffer to load from.
  300. index: Which font from the file to load (0 for default).
  301. use_glyph_box: 1 if we should use the glyph bbox, 0 otherwise.
  302. Returns new font handle, or throws exception on error.
  303. */
  304. fz_font *fz_new_font_from_buffer(fz_context *ctx, const char *name, fz_buffer *buffer, int index, int use_glyph_bbox);
  305. /*
  306. fz_new_font_from_file: Create a new font from a font
  307. file.
  308. name: Name of font (leave NULL to use name from font).
  309. path: File path to load from.
  310. index: Which font from the file to load (0 for default).
  311. use_glyph_box: 1 if we should use the glyph bbox, 0 otherwise.
  312. Returns new font handle, or throws exception on error.
  313. */
  314. fz_font *fz_new_font_from_file(fz_context *ctx, const char *name, const char *path, int index, int use_glyph_bbox);
  315. /*
  316. Add a reference to an existing fz_font.
  317. font: The font to add a reference to.
  318. Returns the same font.
  319. */
  320. fz_font *fz_keep_font(fz_context *ctx, fz_font *font);
  321. /*
  322. Drop a reference to a fz_font, destroying the
  323. font when the last reference is dropped.
  324. font: The font to drop a reference to.
  325. */
  326. void fz_drop_font(fz_context *ctx, fz_font *font);
  327. /*
  328. fz_set_font_bbox: Set the font bbox.
  329. font: The font to set the bbox for.
  330. xmin, ymin, xmax, ymax: The bounding box.
  331. */
  332. void fz_set_font_bbox(fz_context *ctx, fz_font *font, float xmin, float ymin, float xmax, float ymax);
  333. /*
  334. fz_bound_glyph: Return a bbox for a given glyph in a font.
  335. font: The font to look for the glyph in.
  336. gid: The glyph to bound.
  337. trm: The matrix to apply to the glyph before bounding.
  338. r: Pointer to a fz_rect to use for storage.
  339. Returns r, after filling it in with the bounds of the given glyph.
  340. */
  341. fz_rect *fz_bound_glyph(fz_context *ctx, fz_font *font, int gid, const fz_matrix *trm, fz_rect *r);
  342. /*
  343. fz_glyph_cacheable: Determine if a given glyph in a font
  344. is cacheable. Certain glyphs in a type 3 font cannot safely
  345. be cached, as their appearance depends on the enclosing
  346. graphic state.
  347. font: The font to look for the glyph in.
  348. gif: The glyph to query.
  349. Returns non-zero if cacheable, 0 if not.
  350. */
  351. int fz_glyph_cacheable(fz_context *ctx, fz_font *font, int gid);
  352. /*
  353. fz_run_t3_glyph: Run a glyph from a Type3 font to
  354. a given device.
  355. font: The font to find the glyph in.
  356. gid: The glyph to run.
  357. trm: The transform to apply.
  358. dev: The device to render onto.
  359. */
  360. void fz_run_t3_glyph(fz_context *ctx, fz_font *font, int gid, const fz_matrix *trm, struct fz_device_s *dev);
  361. /*
  362. fz_decouple_type3_font: Internal function to remove the
  363. references to a document held by a Type3 font. This is
  364. called during document destruction to ensure that Type3
  365. fonts clean up properly.
  366. Without this call being made, Type3 fonts can be left
  367. holding pdf_obj references for the sake of interpretation
  368. operations that will never come. These references
  369. cannot be freed after the document, hence this function
  370. forces them to be freed earlier in the process.
  371. font: The font to decouple.
  372. t3doc: The document to which the font may refer.
  373. */
  374. void fz_decouple_type3_font(fz_context *ctx, fz_font *font, void *t3doc);
  375. /*
  376. fz_advance_glyph: Return the advance for a given glyph.
  377. font: The font to look for the glyph in.
  378. glyph: The glyph to find the advance for.
  379. wmode: 1 for vertical mode, 0 for horizontal.
  380. Returns the advance for the glyph.
  381. */
  382. float fz_advance_glyph(fz_context *ctx, fz_font *font, int glyph, int wmode);
  383. /*
  384. fz_encode_character: Find the glyph id for a given unicode
  385. character within a font.
  386. font: The font to look for the unicode character in.
  387. unicode: The unicode character to encode.
  388. Returns the glyph id for the given unicode value, or 0 if
  389. unknown.
  390. */
  391. int fz_encode_character(fz_context *ctx, fz_font *font, int unicode);
  392. /*
  393. fz_encode_character_with_fallback: Find the glyph id for
  394. a given unicode character within a font, falling back to
  395. an alternative if not found.
  396. font: The font to look for the unicode character in.
  397. unicode: The unicode character to encode.
  398. script: The script in use.
  399. language: The language in use.
  400. out_font: The font handle in which the given glyph represents
  401. the requested unicode character. The caller does not own the
  402. reference it is passed, so should call fz_keep_font if it is
  403. not simply to be used immediately.
  404. Returns the glyph id for the given unicode value in the supplied
  405. font (and sets *out_font to font) if it is present. Otherwise
  406. an alternative fallback font (based on script/language) is
  407. searched for. If the glyph is found therein, *out_font is set
  408. to this reference, and the glyph reference is returned. If it
  409. cannot be found anywhere, the function returns 0.
  410. */
  411. int fz_encode_character_with_fallback(fz_context *ctx, fz_font *font, int unicode, int script, int language, fz_font **out_font);
  412. /*
  413. fz_get_glyph_name: Find the name of a glyph
  414. font: The font to look for the glyph in.
  415. glyph: The glyph id to look for.
  416. buf: Pointer to a buffer for the name to be inserted into.
  417. size: The size of the buffer.
  418. If a font contains a name table, then the name of the glyph
  419. will be returned in the supplied buffer. Otherwise a name
  420. is synthesised. The name will be truncated to fit in
  421. the buffer.
  422. */
  423. void fz_get_glyph_name(fz_context *ctx, fz_font *font, int glyph, char *buf, int size);
  424. /*
  425. fz_print_font: Output textual information about a font
  426. to a given output stream.
  427. out: The output stream to output to.
  428. font: The font to output details for.
  429. */
  430. void fz_print_font(fz_context *ctx, fz_output *out, fz_font *font);
  431. /*
  432. Internal functions for our Harfbuzz integration
  433. to work around the lack of thread safety.
  434. */
  435. /*
  436. hb_lock: Lock against Harfbuzz being called
  437. simultaneously in several threads. This reuses
  438. FZ_LOCK_FREETYPE.
  439. */
  440. void hb_lock(fz_context *ctx);
  441. /*
  442. hb_unlock: Unlock after a Harfbuzz call. This reuses
  443. FZ_LOCK_FREETYPE.
  444. */
  445. void hb_unlock(fz_context *ctx);
  446. #endif