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
|
#include "fitz-internal.h"
#include "mupdf-internal.h"
static void
pdf_run_glyph_func(void *doc, void *rdb, fz_buffer *contents, fz_device *dev, const fz_matrix *ctm, void *gstate, int nested_depth)
{
pdf_run_glyph(doc, (pdf_obj *)rdb, contents, dev, ctm, gstate, nested_depth);
}
static void
pdf_t3_free_resources(void *doc, void *rdb_)
{
pdf_obj *rdb = (pdf_obj *)rdb_;
pdf_drop_obj(rdb);
}
pdf_font_desc *
pdf_load_type3_font(pdf_document *xref, pdf_obj *rdb, pdf_obj *dict)
{
char buf[256];
char *estrings[256];
pdf_font_desc *fontdesc = NULL;
pdf_obj *encoding;
pdf_obj *widths;
pdf_obj *charprocs;
pdf_obj *obj;
int first, last;
int i, k, n;
fz_rect bbox;
fz_matrix matrix;
fz_context *ctx = xref->ctx;
fz_var(fontdesc);
fz_try(ctx)
{
obj = pdf_dict_gets(dict, "Name");
if (pdf_is_name(obj))
fz_strlcpy(buf, pdf_to_name(obj), sizeof buf);
else
sprintf(buf, "Unnamed-T3");
fontdesc = pdf_new_font_desc(ctx);
obj = pdf_dict_gets(dict, "FontMatrix");
pdf_to_matrix(ctx, obj, &matrix);
obj = pdf_dict_gets(dict, "FontBBox");
fz_transform_rect(pdf_to_rect(ctx, obj, &bbox), &matrix);
fontdesc->font = fz_new_type3_font(ctx, buf, &matrix);
fontdesc->size += sizeof(fz_font) + 256 * (sizeof(fz_buffer*) + sizeof(float));
fz_set_font_bbox(ctx, fontdesc->font, bbox.x0, bbox.y0, bbox.x1, bbox.y1);
/* Encoding */
for (i = 0; i < 256; i++)
estrings[i] = NULL;
encoding = pdf_dict_gets(dict, "Encoding");
if (!encoding)
{
fz_throw(ctx, "syntaxerror: Type3 font missing Encoding");
}
if (pdf_is_name(encoding))
pdf_load_encoding(estrings, pdf_to_name(encoding));
if (pdf_is_dict(encoding))
{
pdf_obj *base, *diff, *item;
base = pdf_dict_gets(encoding, "BaseEncoding");
if (pdf_is_name(base))
pdf_load_encoding(estrings, pdf_to_name(base));
diff = pdf_dict_gets(encoding, "Differences");
if (pdf_is_array(diff))
{
n = pdf_array_len(diff);
k = 0;
for (i = 0; i < n; i++)
{
item = pdf_array_get(diff, i);
if (pdf_is_int(item))
k = pdf_to_int(item);
if (pdf_is_name(item) && k >= 0 && k < nelem(estrings))
estrings[k++] = pdf_to_name(item);
}
}
}
fontdesc->encoding = pdf_new_identity_cmap(ctx, 0, 1);
fontdesc->size += pdf_cmap_size(ctx, fontdesc->encoding);
pdf_load_to_unicode(xref, fontdesc, estrings, NULL, pdf_dict_gets(dict, "ToUnicode"));
/* Widths */
pdf_set_default_hmtx(ctx, fontdesc, 0);
first = pdf_to_int(pdf_dict_gets(dict, "FirstChar"));
last = pdf_to_int(pdf_dict_gets(dict, "LastChar"));
if (first < 0 || last > 255 || first > last)
first = last = 0;
widths = pdf_dict_gets(dict, "Widths");
if (!widths)
{
fz_throw(ctx, "syntaxerror: Type3 font missing Widths");
}
for (i = first; i <= last; i++)
{
float w = pdf_to_real(pdf_array_get(widths, i - first));
w = fontdesc->font->t3matrix.a * w * 1000;
fontdesc->font->t3widths[i] = w * 0.001f;
pdf_add_hmtx(ctx, fontdesc, i, i, w);
}
pdf_end_hmtx(ctx, fontdesc);
/* Resources -- inherit page resources if the font doesn't have its own */
fontdesc->font->t3freeres = pdf_t3_free_resources;
fontdesc->font->t3resources = pdf_dict_gets(dict, "Resources");
if (!fontdesc->font->t3resources)
fontdesc->font->t3resources = rdb;
if (fontdesc->font->t3resources)
pdf_keep_obj(fontdesc->font->t3resources);
if (!fontdesc->font->t3resources)
fz_warn(ctx, "no resource dictionary for type 3 font!");
fontdesc->font->t3doc = xref;
fontdesc->font->t3run = pdf_run_glyph_func;
/* CharProcs */
charprocs = pdf_dict_gets(dict, "CharProcs");
if (!charprocs)
{
fz_throw(ctx, "syntaxerror: Type3 font missing CharProcs");
}
for (i = 0; i < 256; i++)
{
if (estrings[i])
{
obj = pdf_dict_gets(charprocs, estrings[i]);
if (pdf_is_stream(xref, pdf_to_num(obj), pdf_to_gen(obj)))
{
fontdesc->font->t3procs[i] = pdf_load_stream(xref, pdf_to_num(obj), pdf_to_gen(obj));
fontdesc->size += fontdesc->font->t3procs[i]->cap;
fontdesc->size += 0; // TODO: display list size calculation
}
}
}
}
fz_catch(ctx)
{
if (fontdesc)
pdf_drop_font(ctx, fontdesc);
fz_throw(ctx, "cannot load type3 font (%d %d R)", pdf_to_num(dict), pdf_to_gen(dict));
}
return fontdesc;
}
void pdf_load_type3_glyphs(pdf_document *xref, pdf_font_desc *fontdesc, int nested_depth)
{
int i;
fz_context *ctx = xref->ctx;
fz_try(ctx)
{
for (i = 0; i < 256; i++)
{
if (fontdesc->font->t3procs[i])
{
fz_prepare_t3_glyph(ctx, fontdesc->font, i, nested_depth);
fontdesc->size += 0; // TODO: display list size calculation
}
}
}
fz_catch(ctx)
{
fz_warn(ctx, "Type3 glyph load failed: %s", fz_caught(ctx));
}
}
|