/
PDFonts.jl
394 lines (359 loc) · 12.2 KB
/
PDFonts.jl
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import ..Cos: CosXString
using IntervalTrees
#=
Sample CMaps are available now as 8.cmap and 16.cmap in the test/files directory for 8 and
16-bit toUnicode CMaps.
CMaps can have both 8 and 16 bit ranges in the same CMap file as well.
=#
const beginbfchar = b"beginbfchar"
const endbfchar = b"endbfchar"
const beginbfrange = b"beginbfrange"
const endbfrange = b"endbfrange"
const begincodespacerange = b"begincodespacerange"
const endcodespacerange = b"endcodespacerange"
mutable struct CMap
code_space::IntervalMap{UInt8, Union{CosNullType, IntervalMap{UInt8, CosNullType}}}
range_map::IntervalMap{UInt8, Union{CosObject, IntervalMap{UInt8, CosObject}}}
CMap() = new(IntervalMap{UInt8, Union{CosNullType, IntervalMap{UInt8, CosNullType}}}(),
IntervalMap{UInt8, Union{CosObject, IntervalMap{UInt8, CosObject}}}())
end
function show(io::IO, cmap::CMap)
show(io, "Code Space:\n")
show(io, cmap.code_space)
show(io, "Range Map:\n")
show(io, cmap.range_map)
end
mutable struct FontUnicodeMapping
encoding::Dict
cmap::CMap
hasCMap::Bool
FontUnicodeMapping() = new(Dict(), CMap(), false)
end
function merge_encoding!(fum::FontUnicodeMapping, encoding::CosName,
doc::CosDoc, font::CosObject)
encoding_mapping = encoding == cn"WinAnsiEncoding" ? WINEncoding_to_Unicode :
encoding == cn"MacRomanEncoding" ? MACEncoding_to_Unicode :
encoding == cn"MacExpertEncoding" ? MEXEncoding_to_Unicode :
STDEncoding_to_Unicode
merge!(fum.encoding, encoding_mapping)
return fum
end
# for type 0 use cmap.
# for symbol and zapfdingbats - use font encoding
# for others use STD Encoding
# Reading encoding from the font files in case of Symbolic fonts are not supported.
# Font subset is addressed with font name identification.
function merge_encoding!(fum::FontUnicodeMapping, encoding::CosNullType,
doc::CosDoc, font::CosObject)
subtype = cosDocGetObject(doc, font, cn"Subtype")
(subtype != cn"Type1") && (subtype != cn"MMType1") && return fum
basefont = cosDocGetObject(doc, font, cn"BaseFont")
basefont_with_subset = CDTextString(basefont)
basefont_str = rsplit(basefont_with_subset, '+';limit=2)[end]
enc = (basefont_str == "Symbol") ? SYMEncoding_to_Unicode :
(basefont_str == "ZapfDingbats") ? ZAPEncoding_to_Unicode :
STDEncoding_to_Unicode
merge!(fum.encoding, enc)
return fum
end
function merge_encoding!(fum::FontUnicodeMapping,
encoding::Union{CosDict, CosIndirectObject{CosDict}},
doc::CosDoc, font::CosObject)
baseenc = cosDocGetObject(doc, encoding, cn"BaseEncoding")
merge_encoding!(fum, baseenc, doc, font)
# Add the Differences
diff = cosDocGetObject(doc, encoding, cn"Differences")
diff === CosNull && return fum
values = get(diff)
d = Dict()
cid = -1
for v in values
if v isa CosInt
cid = get(v)
else
@assert cid != -1
d[cid] = v
cid += 1
end
end
dict_to_unicode = dict_remap(d, AGL_Glyph_to_Unicode)
merge!(fum.encoding, dict_to_unicode)
return fum
end
function merge_encoding!(fum::FontUnicodeMapping, doc::CosDoc, font::CosObject)
encoding = cosDocGetObject(doc, font, cn"Encoding")
merge_encoding!(fum, encoding, doc, font)
toUnicode = cosDocGetObject(doc, font, cn"ToUnicode")
toUnicode == CosNull && return fum
merge_encoding!(fum, toUnicode, doc, font)
end
function merge_encoding!(fum::FontUnicodeMapping, cmap::CosIndirectObject{CosStream},
doc::CosDoc, font::CosObject)
stm_cmap = get(cmap)
try
fum.cmap = read_cmap(stm_cmap)
fum.hasCMap = true
finally
close(stm_cmap)
end
return fum
end
function get_glyph_id_mapping(cosdoc::CosDoc, cosfont::CosObject)
glyph_name_id = Dict{CosName, UInt8}()
(cosfont === CosNull) && return glyph_name_id
subtype = get(cosfont, cn"Subtype")
(subtype === cn"Type0") && return glyph_name_id
baseenc = cosDocGetObject(cosdoc, cosfont, cn"BaseEncoding")
encoding_mapping = baseenc == cn"WinAnsiEncoding" ? GlyphName_to_WINEncoding :
baseenc == cn"MacRomanEncoding" ? GlyphName_to_MACEncoding :
baseenc == cn"MacExpertEncoding" ? Glyphname_to_MEXEncoding :
GlyphName_to_STDEncoding
merge!(glyph_name_id, encoding_mapping)
diff = cosDocGetObject(cosdoc, cosfont, cn"Differences")
diff === CosNull && return glyph_name_id
values = get(diff)
d = Dict()
cid = -1
for v in values
if v isa CosInt
cid = get(v)
else
@assert cid != -1
glyph_name_id[v] = cid
cid += 1
end
end
return glyph_name_id
end
get_encoded_string(s::CosString, fum::Void) = CDTextString(s)
function get_encoded_string(s::CosString, fum::FontUnicodeMapping)
v = Vector{UInt8}(s)
length(v) == 0 && return ""
fum.hasCMap && return get_encoded_string(s, fum.cmap)
carr = NativeEncodingToUnicode(Vector{UInt8}(s), fum.encoding)
return String(carr)
end
function get_unicode_chars(b::UInt8, itv::IntervalValue)
f = first(itv)
l = last(itv)
v = value(itv)
if v isa CosXString
bytes = Vector{UInt8}(v)
carr = get_unicode_chars(bytes)
carr[1] += (b - f) # Only one char should be generated here
elseif v isa CosArray
@assert v isa CosArray
arr = get(v)
xstr = arr[b - f + 1]
@assert xstr isa CosXString
bytes = Vector{UInt8}(xstr)
carr = get_unicode_chars(bytes)
else
@assert 1 == 0
end
return carr
end
function get_unicode_chars(barr::Vector{UInt8})
l = length(barr)
nb = 0
retarr = Vector{Char}()
while nb < l
b1 = barr[1]
b2 = barr[2]
nb += 2
c::UInt32 = 0
if 0xD8 <= b1 <= 0xDB
# UTF-16 Supplementary plane = 4 bytes
b1 -= 0xD8
c = b1
c = (c << 8) + b2
b3 = barr[3]
b4 = barr[4]
nb += 2
if 0xDC <= b3 <= 0xDF
b3 -= 0xDC
c1 = b3
c1 = (c1 << 8) + b4
c = (c << 10) + c1
c += 0x10000
end
else
c = b1
c = (c << 8) + b2
end
push!(retarr, Char(c))
end
return retarr
end
function get_encoded_string(s::CosString, cmap::CMap)
cs = cmap.code_space
rm = cmap.range_map
barr = Vector{UInt8}(s)
l = length(barr)
b1 = b2 = 0x0
carr = Vector{Char}()
retarr = Vector{Char}()
i = 0
while i < l
b1 = barr[i+=1]
if hasintersection(cs, b1)
itree = value(collect(intersect(cs, (b1,b1)))[1])
if itree === CosNull
itv = collect(intersect(rm, (b1,b1)))
if length(itv) > 0
carr = get_unicode_chars(b1, itv[1])
else
push!(carr, Char(0))
end
else
b2 = barr[i+=1]
itree1 = value(collect(intersect(rm, (b1,b1)))[1])
itv = collect(intersect(itree1, (b2,b2)))
if length(itv) > 0
carr = get_unicode_chars(b2, itv[1])
else
push!(carr, Char(0))
end
end
append!(retarr, carr)
end
end
return retarr
end
function cmap_command(b::Vector{UInt8})
b != beginbfchar && b != beginbfrange && b != begincodespacerange && return nothing
return Symbol(String(b))
end
function on_cmap_command!(stm::BufferedInputStream, command::Symbol,
params::Vector{CosInt}, cmap::CMap)
n = get(pop!(params))
o1, o2, o3 = CosNull, CosNull, CosNull
for i = 1:n
o1 = parse_value(stm)
@assert isa(o1, CosXString)
d1 = Vector{UInt8}(o1)
o2 = (command == :beginbfchar) ? o1 : parse_value(stm)
@assert isa(o2, CosXString)
d2 = Vector{UInt8}(o2)
if (command != :begincodespacerange)
o3 = parse_value(stm)
@assert isa(o3, CosXString) || isa(o3, CosArray)
l = length(d1)
if l == 1
cmap.range_map[(d1[1],d2[1])] = o3
else
if hasintersection(cmap.range_map, d1[1])
imap = value(collect(intersect(cmap.range_map, (d1[1], d2[1])))[1])
else
imap = IntervalMap{UInt8, CosObject}()
cmap.range_map[(d1[1],d2[1])] = imap
end
imap[(d1[2], d2[2])] = o3
end
else
l = length(d1)
if l == 1
cmap.code_space[(d1[1],d2[1])] = CosNull
else
imap = IntervalMap{UInt8, CosNullType}()
imap[(d1[2], d2[2])] = CosNull
cmap.code_space[(d1[1],d2[1])] = imap
end
end
end
return cmap
end
on_cmap_command!(stm::BufferedInputStream, command::CosObject,
params::Vector{CosInt}, cmap::CMap) = nothing
function read_cmap(stm::BufferedInputStream)
tcmap = CMap()
params = Vector{CosInt}()
while !eof(stm)
obj = parse_value(stm, cmap_command)
if isa(obj, CosInt)
push!(params, obj)
end
(obj == :beginbfchar || obj == :beginbfrange || obj == :begincodespacerange) &&
on_cmap_command!(stm, obj, params, tcmap)
end
return tcmap
end
struct CIDWidth
imap::IntervalMap{UInt16, Int}
dw::Int
CIDWidth(m::IntervalMap{UInt16, Int}, tdw::Int) = new(m, tdw)
end
CIDWidth(m::IntervalMap{UInt16, Int}) = CIDWidth(m, 1000)
CIDWidth(tdw::Int) = CIDWidth(IntervalMap{UInt16, Int}(), tdw)
CIDWidth() = CIDWidth(1000)
mutable struct PDFont
doc::PDDoc
obj::CosObject
widths::Union{AdobeFontMetrics, Vector{Int}, CIDWidth}
fum::FontUnicodeMapping
glyph_name_id::Dict{CosName, UInt8}
end
INIT_CODE(::CIDWidth) = 0x0000
SPACE_CODE(w::CIDWidth) = get_character_code(cn"space", w)
INIT_CODE(x) = 0x00
SPACE_CODE(x) = get_character_code(cn"space", x)
function get_character_code(name::CosName, pdfont::PDFont)
length(pdfont.glyph_name_id) > 0 &&
return get(pdfont.glyph_name_id, name, INIT_CODE(pdfont.widths))
return get_character_code(name, pdfont.widths)
end
get_character_code(name::CosName, w::CIDWidth) =
UInt16(get(AGL_Glyph_to_Unicode, name, INIT_CODE(w)))
get_character_code(name::CosName, w) =
get(GlyphName_to_STDEncoding, name, INIT_CODE(w))
get_encoded_string(s, pdfont::PDFont) = get_encoded_string(s, pdfont.fum)
function get_char(barr, state, w::CIDWidth)
(b1, state) = next(barr, state)
(b2, state) = next(barr, state)
return (b1*0x0100 + b2, state)
end
get_char(barr, state, w) = next(barr, state)
function get_string_width(barr::Vector{UInt8}, widths, pc, tfs, tj, tc, tw)
totalw = 0.0
st = start(barr)
while !done(barr, st)
c, st = get_char(barr, st, widths)
w = get_character_width(c, widths)
kw = get_kern_width(pc, c, widths)
w = (w - tj)*tfs / 1000.0 + ((c == SPACE_CODE(widths)) ? tc : tw)
w += kw
pc = c
tj = 0.0
totalw += w
end
return totalw
end
function get_TextBox(ss::Vector{Union{CosString,CosNumeric}},
pdfont::PDFont, tfs, tc, tw, th)
totalw = 0.0
tj = 0.0
text = ""
for s in ss
if s isa CosString
prev_char = INIT_CODE(pdfont.widths)
t = String(get_encoded_string(s, pdfont))
if (-tj) > 180 && length(t) > 0 && t[1] != ' ' &&
length(text) > 0 && text[end] != ' '
text *= " "
end
text *= t
barr = Vector{UInt8}(s)
totalw += get_string_width(barr, pdfont.widths, prev_char, tfs, tj, tc, tw)
end
if s isa CosNumeric
tj = s |> get |> Float32
end
end
totalw *= th
return text, totalw, tfs
end
function get_character_width(cid::UInt16, w::CIDWidth)
itv = collect(intersect(w.imap, (cid,cid)))
(length(itv) == 0) && return w.dw
return value(itv[1])
end