mupdf
Loading...
Searching...
No Matches
font.h
Go to the documentation of this file.
1// Copyright (C) 2004-2025 Artifex Software, Inc.
2//
3// This file is part of MuPDF.
4//
5// MuPDF is free software: you can redistribute it and/or modify it under the
6// terms of the GNU Affero General Public License as published by the Free
7// Software Foundation, either version 3 of the License, or (at your option)
8// any later version.
9//
10// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13// details.
14//
15// You should have received a copy of the GNU Affero General Public License
16// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17//
18// Alternative licensing terms are available from the licensor.
19// For commercial licensing, see <https://www.artifex.com/> or contact
20// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21// CA 94129, USA, for further information.
22
23#ifndef MUPDF_FITZ_FONT_H
24#define MUPDF_FITZ_FONT_H
25
26#include "mupdf/fitz/system.h"
27#include "mupdf/fitz/context.h"
28#include "mupdf/fitz/geometry.h"
29#include "mupdf/fitz/buffer.h"
30#include "mupdf/fitz/color.h"
31
32/* forward declaration for circular dependency */
33struct fz_device;
34
35/* Various font encoding tables and lookup functions */
36
37FZ_DATA extern const char *fz_glyph_name_from_adobe_standard[256];
38FZ_DATA extern const char *fz_glyph_name_from_iso8859_7[256];
39FZ_DATA extern const char *fz_glyph_name_from_koi8u[256];
40FZ_DATA extern const char *fz_glyph_name_from_mac_expert[256];
41FZ_DATA extern const char *fz_glyph_name_from_mac_roman[256];
42FZ_DATA extern const char *fz_glyph_name_from_win_ansi[256];
43FZ_DATA extern const char *fz_glyph_name_from_windows_1252[256];
44
45FZ_DATA extern const unsigned short fz_unicode_from_iso8859_1[256];
46FZ_DATA extern const unsigned short fz_unicode_from_iso8859_7[256];
47FZ_DATA extern const unsigned short fz_unicode_from_koi8u[256];
48FZ_DATA extern const unsigned short fz_unicode_from_pdf_doc_encoding[256];
49FZ_DATA extern const unsigned short fz_unicode_from_windows_1250[256];
50FZ_DATA extern const unsigned short fz_unicode_from_windows_1251[256];
51FZ_DATA extern const unsigned short fz_unicode_from_windows_1252[256];
52
59
60int fz_unicode_from_glyph_name(const char *name);
62const char **fz_duplicate_glyph_names_from_unicode(int unicode);
63const char *fz_glyph_name_from_unicode_sc(int unicode);
64
69
71 // get maximum size estimate of converted text (fast)
72 int (*decode_bound)(fz_text_decoder *dec, unsigned char *input, int n);
73
74 // get exact size of converted text (slow)
75 int (*decode_size)(fz_text_decoder *dec, unsigned char *input, int n);
76
77 // convert text into output buffer
78 void (*decode)(fz_text_decoder *dec, char *output, unsigned char *input, int n);
79
80 // for internal use only; do not touch!
81 void *table1;
82 void *table2;
83};
84
85/* Initialize a text decoder using an IANA encoding name.
86 * See source/fitz/text-decoder.c for the exact list of supported encodings.
87 * Will throw an exception if the requested encoding is not available.
88 *
89 * The following is a subset of the supported encodings (see source/fitz/text-decoder.c for the full list):
90 * iso-8859-1
91 * iso-8859-7
92 * koi8-r
93 * euc-jp
94 * shift_jis
95 * euc-kr
96 * euc-cn
97 * gb18030
98 * euc-tw
99 * big5
100 */
101void fz_init_text_decoder(fz_context *ctx, fz_text_decoder *dec, const char *encoding);
102
106typedef struct fz_font fz_font;
107
113
125
136
137/* common CJK font collections */
139
144typedef struct
145{
146 unsigned int is_mono : 1;
147 unsigned int is_serif : 1;
148 unsigned int is_bold : 1;
149 unsigned int is_italic : 1;
150 unsigned int ft_substitute : 1; /* use substitute metrics */
151 unsigned int ft_stretch : 1; /* stretch to match PDF metrics */
152
153 unsigned int fake_bold : 1; /* synthesize bold */
154 unsigned int fake_italic : 1; /* synthesize italic */
155 unsigned int has_opentype : 1; /* has opentype shaping tables */
156 unsigned int invalid_bbox : 1;
157
158 unsigned int cjk : 1;
159 unsigned int cjk_lang : 2; /* CNS, GB, JAPAN, or KOREA */
160
161 unsigned int embed : 1;
162 unsigned int never_embed : 1;
164
175
185typedef struct
186{
188 void (*destroy)(fz_context *ctx, void *); /* Destructor for shape_handle */
190
201
210const char *fz_font_name(fz_context *ctx, fz_font *font);
211
216
221
226
231
241
255typedef fz_font *(fz_load_system_font_fn)(fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics);
256
269typedef fz_font *(fz_load_system_cjk_font_fn)(fz_context *ctx, const char *name, int ordering, int serif);
270
284typedef fz_font *(fz_load_system_fallback_font_fn)(fz_context *ctx, int script, int language, int serif, int bold, int italic);
285
296
312fz_font *fz_load_system_font(fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics);
313
327fz_font *fz_load_system_cjk_font(fz_context *ctx, const char *name, int ordering, int serif);
328
345const unsigned char *fz_lookup_builtin_font(fz_context *ctx, const char *name, int bold, int italic, int *len);
346
359const unsigned char *fz_lookup_base14_font(fz_context *ctx, const char *name, int *len);
360
373const unsigned char *fz_lookup_cjk_font(fz_context *ctx, int ordering, int *len, int *index);
374
391const unsigned char *fz_lookup_cjk_font_by_language(fz_context *ctx, const char *lang, int *len, int *subfont);
392
398
413const unsigned char *fz_lookup_noto_font(fz_context *ctx, int script, int lang, int *len, int *subfont);
414
420const unsigned char *fz_lookup_noto_math_font(fz_context *ctx, int *len);
421const unsigned char *fz_lookup_noto_music_font(fz_context *ctx, int *len);
422const unsigned char *fz_lookup_noto_symbol1_font(fz_context *ctx, int *len);
423const unsigned char *fz_lookup_noto_symbol2_font(fz_context *ctx, int *len);
424const unsigned char *fz_lookup_noto_emoji_font(fz_context *ctx, int *len);
425const unsigned char *fz_lookup_noto_boxes_font(fz_context *ctx, int *len);
426
432const char *fz_lookup_noto_stem_from_script(fz_context *ctx, int script, int language);
433const char *fz_lookup_script_name(fz_context *ctx, int script, int language);
434
453fz_font *fz_load_fallback_font(fz_context *ctx, int script, int language, int serif, int bold, int italic);
454
466
484fz_font *fz_new_font_from_memory(fz_context *ctx, const char *name, const unsigned char *data, int len, int index, int use_glyph_bbox);
485
502
518fz_font *fz_new_font_from_file(fz_context *ctx, const char *name, const char *path, int index, int use_glyph_bbox);
519
524fz_font *fz_new_cjk_font(fz_context *ctx, int ordering);
525fz_font *fz_new_builtin_font(fz_context *ctx, const char *name, int is_bold, int is_italic);
526
530void fz_set_font_embedding(fz_context *ctx, fz_font *font, int embed);
531
540
548
556void fz_set_font_bbox(fz_context *ctx, fz_font *font, float xmin, float ymin, float xmax, float ymax);
557
571
584int fz_glyph_cacheable(fz_context *ctx, fz_font *font, int gid);
585
598void fz_run_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, struct fz_device *dev);
599
611float fz_advance_glyph(fz_context *ctx, fz_font *font, int glyph, int wmode);
612
624int fz_encode_character(fz_context *ctx, fz_font *font, int unicode);
625
636int fz_encode_character_sc(fz_context *ctx, fz_font *font, int unicode);
637
647int fz_encode_character_by_glyph_name(fz_context *ctx, fz_font *font, const char *glyphname);
648
674int fz_encode_character_with_fallback(fz_context *ctx, fz_font *font, int unicode, int script, int language, fz_font **out_font);
675
692void fz_get_glyph_name(fz_context *ctx, fz_font *font, int glyph, char *buf, int size);
693
698
703
707void fz_font_digest(fz_context *ctx, fz_font *font, unsigned char digest[16]);
708
709/* Implementation details: subject to change. */
710
712
722const char *ft_error_string(int err);
723int ft_char_index(void *face, int cid);
724int ft_name_index(void *face, const char *name);
725
730
737
743
747typedef void (fz_cmap_callback)(fz_context *ctx, void *opaque, unsigned long ucs, unsigned int gid);
748
752void fz_enumerate_font_cmap(fz_context *ctx, fz_font *font, fz_cmap_callback *cb, void *opaque);
753
764
771
772#define FZ_MAX_TRUSTWORTHY_ASCENT 8
773#define FZ_MAX_TRUSTWORTHY_DESCENT -3
774
776{
777 int refs;
778 char name[32];
779 char family[32];
781
783
784 void *ft_face; /* has an FT_Face if used */
786
789 fz_buffer **t3procs; /* has 256 entries if used */
790 struct fz_display_list **t3lists; /* has 256 entries if used */
791 float *t3widths; /* has 256 entries if used */
792 unsigned short *t3flags; /* has 256 entries if used */
793 void *t3doc; /* a pdf_document for the callback */
794 void (*t3run)(fz_context *ctx, void *doc, void *resources, fz_buffer *contents, struct fz_device *dev, fz_matrix ctm, void *gstate, fz_default_colorspaces *default_cs, void *fill_gstate, void *stroke_gstate);
795 void (*t3freeres)(fz_context *ctx, void *doc, void *resources);
796 int t3loading; /* to detect recursive type3 fonts */
797
798 fz_rect bbox; /* font bbox is used only for t3 fonts */
799
800 float ascender;
803
805
806 /* per glyph bounding box cache. */
809
810 /* substitute metrics */
812 short width_default; /* in 1000 units */
813 short *width_table; /* in 1000 units */
814
815 /* cached glyph metrics */
817
818 /* cached encoding lookup */
819 uint16_t *encoding_cache[256];
820
821 /* cached md5sum for caching */
823 unsigned char digest[16];
824
825 /* Which font to use in a collection. */
827};
828
830
832
833/* Internal function. Must be called with FT_ALLOC_LOCK
834 * held. Returns 1 if this thread (context!) already holds
835 * the freeetype lock. */
837
838/* Internal function: Extract a ttf from the ttc that underlies
839 * a given fz_font. Caller takes ownership of the returned
840 * buffer.
841 */
843
844/* Internal function: Given a ttf in a buffer, create a subset
845 * ttf in a new buffer that only provides the required gids.
846 * Caller takes ownership of the returned buffer.
847 *
848 * EXPERIMENTAL AND VERY SUBJECT TO CHANGE.
849 */
850fz_buffer *fz_subset_ttf_for_gids(fz_context *ctx, fz_buffer *orig, int *gids, int num_gids, int symbolic, int cidfont);
851
852/* Internal function: Given a cff in a buffer, create a subset
853 * cff in a new buffer that only provides the required gids.
854 * Caller takes ownership of the returned buffer.
855 *
856 * EXPERIMENTAL AND VERY SUBJECT TO CHANGE.
857 */
858fz_buffer *fz_subset_cff_for_gids(fz_context *ctx, fz_buffer *orig, int *gids, int num_gids, int symbolic, int cidfont);
859
860#endif
struct fz_display_list fz_display_list
Definition display-list.h:47
#define FZ_DATA
Definition export.h:49
FZ_DATA const char * fz_glyph_name_from_mac_expert[256]
const char * fz_lookup_script_name(fz_context *ctx, int script, int language)
fz_buffer * fz_subset_ttf_for_gids(fz_context *ctx, fz_buffer *orig, int *gids, int num_gids, int symbolic, int cidfont)
int fz_unicode_from_glyph_name(const char *name)
int ft_char_index(void *face, int cid)
const char * ft_error_string(int err)
void fz_hb_unlock(fz_context *ctx)
void fz_run_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, struct fz_device *dev)
const unsigned char * fz_lookup_noto_symbol2_font(fz_context *ctx, int *len)
fz_ascdesc_source
Definition font.h:766
@ FZ_ASCDESC_FROM_FONT
Definition font.h:767
@ FZ_ASCDESC_DEFAULT
Definition font.h:768
@ FZ_ASCDESC_FROM_BOUNDS
Definition font.h:769
void fz_enumerate_font_cmap(fz_context *ctx, fz_font *font, fz_cmap_callback *cb, void *opaque)
int fz_windows_1252_from_unicode(int u)
void fz_ft_lock(fz_context *ctx)
int fz_encode_character(fz_context *ctx, fz_font *font, int unicode)
int fz_windows_1251_from_unicode(int u)
void fz_cmap_callback(fz_context *ctx, void *opaque, unsigned long ucs, unsigned int gid)
Definition font.h:747
int fz_ft_lock_held(fz_context *ctx)
fz_buffer ** fz_font_t3_procs(fz_context *ctx, fz_font *font)
FZ_DATA const unsigned short fz_unicode_from_windows_1250[256]
fz_font * fz_new_base14_font(fz_context *ctx, const char *name)
fz_font * fz_load_fallback_font(fz_context *ctx, int script, int language, int serif, int bold, int italic)
int fz_font_is_italic(fz_context *ctx, fz_font *font)
FZ_DATA const unsigned short fz_unicode_from_pdf_doc_encoding[256]
int fz_koi8u_from_unicode(int u)
int fz_font_is_monospaced(fz_context *ctx, fz_font *font)
float fz_font_ascender(fz_context *ctx, fz_font *font)
FZ_DATA const char * fz_glyph_name_from_iso8859_7[256]
void fz_drop_font(fz_context *ctx, fz_font *font)
const unsigned char * fz_lookup_base14_font(fz_context *ctx, const char *name, int *len)
const char * fz_glyph_name_from_unicode_sc(int unicode)
const unsigned char * fz_lookup_noto_music_font(fz_context *ctx, int *len)
int fz_encode_character_by_glyph_name(fz_context *ctx, fz_font *font, const char *glyphname)
int fz_windows_1250_from_unicode(int u)
FZ_DATA const unsigned short fz_unicode_from_koi8u[256]
void * fz_font_ft_face(fz_context *ctx, fz_font *font)
void fz_hb_lock(fz_context *ctx)
FZ_DATA const unsigned short fz_unicode_from_iso8859_1[256]
FZ_DATA const char * fz_glyph_name_from_koi8u[256]
int fz_glyph_cacheable(fz_context *ctx, fz_font *font, int gid)
const unsigned char * fz_lookup_noto_boxes_font(fz_context *ctx, int *len)
fz_font * fz_new_font_from_memory(fz_context *ctx, const char *name, const unsigned char *data, int len, int index, int use_glyph_bbox)
FZ_DATA const char * fz_glyph_name_from_windows_1252[256]
int fz_unicode_from_glyph_name_strict(const char *name)
int fz_lookup_cjk_ordering_by_language(const char *name)
const unsigned char * fz_lookup_noto_math_font(fz_context *ctx, int *len)
int fz_iso8859_7_from_unicode(int u)
fz_buffer * fz_extract_ttf_from_ttc(fz_context *ctx, fz_font *font)
int fz_encode_character_sc(fz_context *ctx, fz_font *font, int unicode)
const unsigned char * fz_lookup_cjk_font(fz_context *ctx, int ordering, int *len, int *index)
FZ_DATA const char * fz_glyph_name_from_mac_roman[256]
void fz_set_font_embedding(fz_context *ctx, fz_font *font, int embed)
fz_font * fz_new_builtin_font(fz_context *ctx, const char *name, int is_bold, int is_italic)
FZ_DATA const unsigned short fz_unicode_from_iso8859_7[256]
int ft_name_index(void *face, const char *name)
void fz_install_load_system_font_funcs(fz_context *ctx, fz_load_system_font_fn *f, fz_load_system_cjk_font_fn *f_cjk, fz_load_system_fallback_font_fn *f_fallback)
fz_font * fz_new_font_from_buffer(fz_context *ctx, const char *name, fz_buffer *buffer, int index, int use_glyph_bbox)
fz_font * fz_load_system_font(fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics)
FZ_DATA const char * fz_glyph_name_from_win_ansi[256]
FZ_DATA const unsigned short fz_unicode_from_windows_1252[256]
const unsigned char * fz_lookup_noto_emoji_font(fz_context *ctx, int *len)
fz_font * fz_keep_font(fz_context *ctx, fz_font *font)
FZ_DATA const char * fz_glyph_name_from_adobe_standard[256]
const unsigned char * fz_lookup_cjk_font_by_language(fz_context *ctx, const char *lang, int *len, int *subfont)
int fz_font_is_serif(fz_context *ctx, fz_font *font)
FZ_DATA const unsigned short fz_unicode_from_windows_1251[256]
const char ** fz_duplicate_glyph_names_from_unicode(int unicode)
fz_rect fz_font_bbox(fz_context *ctx, fz_font *font)
int fz_font_is_bold(fz_context *ctx, fz_font *font)
void fz_font_digest(fz_context *ctx, fz_font *font, unsigned char digest[16])
fz_font * fz_new_font_from_file(fz_context *ctx, const char *name, const char *path, int index, int use_glyph_bbox)
const unsigned char * fz_lookup_noto_symbol1_font(fz_context *ctx, int *len)
float fz_advance_glyph(fz_context *ctx, fz_font *font, int glyph, int wmode)
void fz_set_font_bbox(fz_context *ctx, fz_font *font, float xmin, float ymin, float xmax, float ymax)
int fz_iso8859_1_from_unicode(int u)
float fz_font_descender(fz_context *ctx, fz_font *font)
void fz_init_text_decoder(fz_context *ctx, fz_text_decoder *dec, const char *encoding)
fz_font * fz_load_system_cjk_font(fz_context *ctx, const char *name, int ordering, int serif)
void fz_get_glyph_name(fz_context *ctx, fz_font *font, int glyph, char *buf, int size)
fz_buffer * fz_subset_cff_for_gids(fz_context *ctx, fz_buffer *orig, int *gids, int num_gids, int symbolic, int cidfont)
fz_font * fz_new_type3_font(fz_context *ctx, const char *name, fz_matrix matrix)
fz_font * fz_load_system_fallback_font_fn(fz_context *ctx, int script, int language, int serif, int bold, int italic)
Definition font.h:284
fz_font_flags_t * fz_font_flags(fz_font *font)
void fz_decouple_type3_font(fz_context *ctx, fz_font *font, void *t3doc)
fz_font * fz_load_system_font_fn(fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics)
Definition font.h:255
fz_font * fz_new_cjk_font(fz_context *ctx, int ordering)
fz_shaper_data_t * fz_font_shaper_data(fz_context *ctx, fz_font *font)
const unsigned char * fz_lookup_builtin_font(fz_context *ctx, const char *name, int bold, int italic, int *len)
const char * fz_font_name(fz_context *ctx, fz_font *font)
const char * fz_lookup_noto_stem_from_script(fz_context *ctx, int script, int language)
fz_font * fz_load_system_cjk_font_fn(fz_context *ctx, const char *name, int ordering, int serif)
Definition font.h:269
void fz_calculate_font_ascender_descender(fz_context *ctx, fz_font *font)
@ FZ_ADOBE_JAPAN
Definition font.h:138
@ FZ_ADOBE_CNS
Definition font.h:138
@ FZ_ADOBE_KOREA
Definition font.h:138
@ FZ_ADOBE_GB
Definition font.h:138
void fz_ft_unlock(fz_context *ctx)
fz_rect fz_bound_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm)
int fz_encode_character_with_fallback(fz_context *ctx, fz_font *font, int unicode, int script, int language, fz_font **out_font)
const unsigned char * fz_lookup_noto_font(fz_context *ctx, int script, int lang, int *len, int *subfont)
Definition buffer.h:41
Definition context.h:886
Definition color.h:339
Definition device.h:290
Definition font.h:145
unsigned int never_embed
Definition font.h:162
unsigned int fake_bold
Definition font.h:153
unsigned int invalid_bbox
Definition font.h:156
unsigned int fake_italic
Definition font.h:154
unsigned int is_mono
Definition font.h:146
unsigned int ft_substitute
Definition font.h:150
unsigned int ft_stretch
Definition font.h:151
unsigned int cjk_lang
Definition font.h:159
unsigned int is_bold
Definition font.h:148
unsigned int has_opentype
Definition font.h:155
unsigned int is_serif
Definition font.h:147
unsigned int cjk
Definition font.h:158
unsigned int is_italic
Definition font.h:149
unsigned int embed
Definition font.h:161
Definition font.h:776
void * ft_face
Definition font.h:784
struct fz_display_list ** t3lists
Definition font.h:790
fz_ascdesc_source ascdesc_src
Definition font.h:802
fz_shaper_data_t shaper_data
Definition font.h:785
int refs
Definition font.h:777
void(* t3run)(fz_context *ctx, void *doc, void *resources, fz_buffer *contents, struct fz_device *dev, fz_matrix ctm, void *gstate, fz_default_colorspaces *default_cs, void *fill_gstate, void *stroke_gstate)
Definition font.h:794
int width_count
Definition font.h:811
fz_rect ** bbox_table
Definition font.h:807
int glyph_count
Definition font.h:804
void(* t3freeres)(fz_context *ctx, void *doc, void *resources)
Definition font.h:795
void * t3doc
Definition font.h:793
fz_matrix t3matrix
Definition font.h:787
fz_rect bbox
Definition font.h:798
float ascender
Definition font.h:800
unsigned char digest[16]
Definition font.h:823
short width_default
Definition font.h:812
uint16_t * encoding_cache[256]
Definition font.h:819
fz_buffer * buffer
Definition font.h:780
short * width_table
Definition font.h:813
char name[32]
Definition font.h:778
int use_glyph_bbox
Definition font.h:808
unsigned short * t3flags
Definition font.h:792
float * t3widths
Definition font.h:791
int t3loading
Definition font.h:796
int subfont
Definition font.h:826
char family[32]
Definition font.h:779
void * t3resources
Definition font.h:788
int has_digest
Definition font.h:822
fz_buffer ** t3procs
Definition font.h:789
float descender
Definition font.h:801
float ** advance_cache
Definition font.h:816
fz_font_flags_t flags
Definition font.h:782
Definition geometry.h:388
Definition geometry.h:231
Definition font.h:186
void * shaper_handle
Definition font.h:187
void(* destroy)(fz_context *ctx, void *)
Definition font.h:188
Definition font.h:70
int(* decode_size)(fz_text_decoder *dec, unsigned char *input, int n)
Definition font.h:75
void(* decode)(fz_text_decoder *dec, char *output, unsigned char *input, int n)
Definition font.h:78
void * table1
Definition font.h:81
int(* decode_bound)(fz_text_decoder *dec, unsigned char *input, int n)
Definition font.h:72
void * table2
Definition font.h:82