summaryrefslogtreecommitdiff
path: root/mupdf/pdf_fontfile.c
blob: 01f963e4883fe8c80eb41373fad66a96a9e6f704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "fitz.h"
#include "mupdf.h"

extern const unsigned char pdf_font_Dingbats_ttf_buf[];
extern const unsigned int  pdf_font_Dingbats_ttf_len;
extern const unsigned char pdf_font_NimbusMonL_Bold_ttf_buf[];
extern const unsigned int  pdf_font_NimbusMonL_Bold_ttf_len;
extern const unsigned char pdf_font_NimbusMonL_BoldObli_ttf_buf[];
extern const unsigned int  pdf_font_NimbusMonL_BoldObli_ttf_len;
extern const unsigned char pdf_font_NimbusMonL_Regu_ttf_buf[];
extern const unsigned int  pdf_font_NimbusMonL_Regu_ttf_len;
extern const unsigned char pdf_font_NimbusMonL_ReguObli_ttf_buf[];
extern const unsigned int  pdf_font_NimbusMonL_ReguObli_ttf_len;
extern const unsigned char pdf_font_NimbusRomNo9L_Medi_ttf_buf[];
extern const unsigned int  pdf_font_NimbusRomNo9L_Medi_ttf_len;
extern const unsigned char pdf_font_NimbusRomNo9L_MediItal_ttf_buf[];
extern const unsigned int  pdf_font_NimbusRomNo9L_MediItal_ttf_len;
extern const unsigned char pdf_font_NimbusRomNo9L_Regu_ttf_buf[];
extern const unsigned int  pdf_font_NimbusRomNo9L_Regu_ttf_len;
extern const unsigned char pdf_font_NimbusRomNo9L_ReguItal_ttf_buf[];
extern const unsigned int  pdf_font_NimbusRomNo9L_ReguItal_ttf_len;
extern const unsigned char pdf_font_NimbusSanL_Bold_ttf_buf[];
extern const unsigned int  pdf_font_NimbusSanL_Bold_ttf_len;
extern const unsigned char pdf_font_NimbusSanL_BoldItal_ttf_buf[];
extern const unsigned int  pdf_font_NimbusSanL_BoldItal_ttf_len;
extern const unsigned char pdf_font_NimbusSanL_Regu_ttf_buf[];
extern const unsigned int  pdf_font_NimbusSanL_Regu_ttf_len;
extern const unsigned char pdf_font_NimbusSanL_ReguItal_ttf_buf[];
extern const unsigned int  pdf_font_NimbusSanL_ReguItal_ttf_len;
extern const unsigned char pdf_font_StandardSymL_ttf_buf[];
extern const unsigned int  pdf_font_StandardSymL_ttf_len;
extern const unsigned char pdf_font_URWChanceryL_MediItal_ttf_buf[];
extern const unsigned int  pdf_font_URWChanceryL_MediItal_ttf_len;

#ifndef NOCJK
extern const unsigned char pdf_font_DroidSansFallback_ttf_buf[];
extern const unsigned int  pdf_font_DroidSansFallback_ttf_len;
#endif

enum
{
	FD_FIXED = 1 << 0,
	FD_SERIF = 1 << 1,
	FD_SYMBOLIC = 1 << 2,
	FD_SCRIPT = 1 << 3,
	FD_NONSYMBOLIC = 1 << 5,
	FD_ITALIC = 1 << 6,
	FD_ALLCAP = 1 << 16,
	FD_SMALLCAP = 1 << 17,
	FD_FORCEBOLD = 1 << 18
};

enum { CNS, GB, Japan, Korea };
enum { MINCHO, GOTHIC };

static const struct
{
	const char *name;
	const unsigned char *ttf;
	const unsigned int *len;
	} basefonts[] = {
	{ "Courier",
		pdf_font_NimbusMonL_Regu_ttf_buf,
		&pdf_font_NimbusMonL_Regu_ttf_len },
	{ "Courier-Bold",
		pdf_font_NimbusMonL_Bold_ttf_buf,
		&pdf_font_NimbusMonL_Bold_ttf_len },
	{ "Courier-Oblique",
		pdf_font_NimbusMonL_ReguObli_ttf_buf,
		&pdf_font_NimbusMonL_ReguObli_ttf_len },
	{ "Courier-BoldOblique",
		pdf_font_NimbusMonL_BoldObli_ttf_buf,
		&pdf_font_NimbusMonL_BoldObli_ttf_len },
	{ "Helvetica",
		pdf_font_NimbusSanL_Regu_ttf_buf,
		&pdf_font_NimbusSanL_Regu_ttf_len },
	{ "Helvetica-Bold",
		pdf_font_NimbusSanL_Bold_ttf_buf,
		&pdf_font_NimbusSanL_Bold_ttf_len },
	{ "Helvetica-Oblique",
		pdf_font_NimbusSanL_ReguItal_ttf_buf,
		&pdf_font_NimbusSanL_ReguItal_ttf_len },
	{ "Helvetica-BoldOblique",
		pdf_font_NimbusSanL_BoldItal_ttf_buf,
		&pdf_font_NimbusSanL_BoldItal_ttf_len },
	{ "Times-Roman",
		pdf_font_NimbusRomNo9L_Regu_ttf_buf,
		&pdf_font_NimbusRomNo9L_Regu_ttf_len },
	{ "Times-Bold",
		pdf_font_NimbusRomNo9L_Medi_ttf_buf,
		&pdf_font_NimbusRomNo9L_Medi_ttf_len },
	{ "Times-Italic",
		pdf_font_NimbusRomNo9L_ReguItal_ttf_buf,
		&pdf_font_NimbusRomNo9L_ReguItal_ttf_len },
	{ "Times-BoldItalic",
		pdf_font_NimbusRomNo9L_MediItal_ttf_buf,
		&pdf_font_NimbusRomNo9L_MediItal_ttf_len },
	{ "Symbol",
		pdf_font_StandardSymL_ttf_buf,
		&pdf_font_StandardSymL_ttf_len },
	{ "ZapfDingbats",
		pdf_font_Dingbats_ttf_buf,
		&pdf_font_Dingbats_ttf_len },
	{ "Chancery",
		pdf_font_URWChanceryL_MediItal_ttf_buf,
		&pdf_font_URWChanceryL_MediItal_ttf_len },
	{ nil, nil, nil }
};

fz_error
pdf_loadbuiltinfont(pdf_fontdesc *font, char *fontname)
{
	fz_error error;
	unsigned char *data;
	unsigned int len;
	int i;

	for (i = 0; basefonts[i].name; i++)
		if (!strcmp(fontname, basefonts[i].name))
			goto found;

	return fz_throw("cannot find font: '%s'", fontname);

found:
	pdf_logfont("load builtin font %s\n", fontname);

	data = (unsigned char *) basefonts[i].ttf;
	len = *basefonts[i].len;

	error = fz_newfontfrombuffer(&font->font, data, len, 0);
	if (error)
		return fz_rethrow(error, "cannot load freetype font from buffer");

	return fz_okay;
}

static fz_error
loadsystemcidfont(pdf_fontdesc *font, int csi, int kind)
{
#ifndef NOCJK
	fz_error error;
	/* We only have one builtin fallback font, we'd really like
	 * to have one for each combination of CSI and Kind.
	 */
	pdf_logfont("loading builtin CJK font\n");
	error = fz_newfontfrombuffer(&font->font,
		(unsigned char *)pdf_font_DroidSansFallback_ttf_buf,
		pdf_font_DroidSansFallback_ttf_len, 0);
	if (error)
		return fz_rethrow(error, "cannot load builtin CJK font");
	font->font->ftsubstitute = 1; /* substitute font */
	return fz_okay;
#else
	return fz_throw("no builtin CJK font file");
#endif
}

fz_error
pdf_loadsystemfont(pdf_fontdesc *font, char *fontname, char *collection)
{
	fz_error error;
	char *name;

	int isbold = 0;
	int isitalic = 0;
	int isserif = 0;
	int isscript = 0;
	int isfixed = 0;

	if (strstr(fontname, "Bold"))
		isbold = 1;
	if (strstr(fontname, "Italic"))
		isitalic = 1;
	if (strstr(fontname, "Oblique"))
		isitalic = 1;

	if (font->flags & FD_FIXED)
		isfixed = 1;
	if (font->flags & FD_SERIF)
		isserif = 1;
	if (font->flags & FD_ITALIC)
		isitalic = 1;
	if (font->flags & FD_SCRIPT)
		isscript = 1;
	if (font->flags & FD_FORCEBOLD)
		isbold = 1;

	pdf_logfont("fixed-%d serif-%d italic-%d script-%d bold-%d\n",
		isfixed, isserif, isitalic, isscript, isbold);

	if (collection)
	{
		int kind;

		if (isserif)
			kind = MINCHO;
		else
			kind = GOTHIC;

		if (!strcmp(collection, "Adobe-CNS1"))
			return loadsystemcidfont(font, CNS, kind);
		else if (!strcmp(collection, "Adobe-GB1"))
			return loadsystemcidfont(font, GB, kind);
		else if (!strcmp(collection, "Adobe-Japan1"))
			return loadsystemcidfont(font, Japan, kind);
		else if (!strcmp(collection, "Adobe-Japan2"))
			return loadsystemcidfont(font, Japan, kind);
		else if (!strcmp(collection, "Adobe-Korea1"))
			return loadsystemcidfont(font, Korea, kind);

		fz_warn("unknown cid collection: %s", collection);
	}

	if (isscript)
		name = "Chancery";

	else if (isfixed)
	{
		if (isitalic) {
			if (isbold) name = "Courier-BoldOblique";
			else name = "Courier-Oblique";
		}
		else {
			if (isbold) name = "Courier-Bold";
			else name = "Courier";
		}
	}

	else if (isserif)
	{
		if (isitalic) {
			if (isbold) name = "Times-BoldItalic";
			else name = "Times-Italic";
		}
		else {
			if (isbold) name = "Times-Bold";
			else name = "Times-Roman";
		}
	}

	else
	{
		if (isitalic) {
			if (isbold) name = "Helvetica-BoldOblique";
			else name = "Helvetica-Oblique";
		}
		else {
			if (isbold) name = "Helvetica-Bold";
			else name = "Helvetica";
		}
	}

	error = pdf_loadbuiltinfont(font, name);
	if (error)
		return fz_throw("cannot load builtin substitute font: %s", name);

	/* it's a substitute font: override the metrics */
	font->font->ftsubstitute = 1;

	return fz_okay;
}

fz_error
pdf_loadembeddedfont(pdf_fontdesc *font, pdf_xref *xref, fz_obj *stmref)
{
	fz_error error;
	fz_buffer *buf;

	pdf_logfont("load embedded font\n");

	error = pdf_loadstream(&buf, xref, fz_tonum(stmref), fz_togen(stmref));
	if (error)
		return fz_rethrow(error, "cannot load font stream");

	error = fz_newfontfrombuffer(&font->font, buf->rp, buf->wp - buf->rp, 0);
	if (error)
	{
		fz_dropbuffer(buf);
		return fz_rethrow(error, "cannot load embedded font (%d %d R)", fz_tonum(stmref), fz_togen(stmref));
	}

	font->buffer = buf->rp; /* save the buffer so we can free it later */
	fz_free(buf); /* only free the fz_buffer struct, not the contained data */

	font->isembedded = 1;

	return fz_okay;
}