1
1
import .. Cos: CosXString
2
2
3
- export pdFontIsBold,
3
+ export
4
+ pdFontIsBold,
4
5
pdFontIsItalic,
5
6
pdFontIsFixedW,
6
7
pdFontIsAllCap,
7
8
pdFontIsSmallCap
8
9
9
10
using Rectangle
10
11
11
-
12
12
#=
13
13
Sample CMaps are available now as 8.cmap and 16.cmap in the test/files directory
14
14
for 8 and 16-bit toUnicode CMaps.
@@ -44,50 +44,81 @@ function show(io::IO, cmap::CMap)
44
44
show (io, cmap. range_map)
45
45
end
46
46
47
+
48
+ const FontUnicodeMapping = Union{Dict{UInt8, Char}, CMap, Nothing}
49
+
50
+ #=
47
51
mutable struct FontUnicodeMapping
48
52
encoding::Dict{UInt8, Char}
49
53
cmap::CMap
50
54
hasCMap::Bool
51
55
FontUnicodeMapping() = new(Dict{UInt8, Char}(), CMap(), false)
52
56
end
57
+ =#
53
58
54
- function merge_encoding! (fum:: FontUnicodeMapping , encoding:: CosName ,
59
+ function merge_encoding! (fum:: Dict{UInt8, Char} , encoding:: CosName ,
55
60
doc:: CosDoc , font:: IDDRef{CosDict} )
56
61
encoding_mapping =
57
62
encoding == cn " WinAnsiEncoding" ? WINEncoding_to_Unicode :
58
63
encoding == cn " MacRomanEncoding" ? MACEncoding_to_Unicode :
59
64
encoding == cn " MacExpertEncoding" ? MEXEncoding_to_Unicode :
60
65
STDEncoding_to_Unicode
61
- merge! (fum. encoding , encoding_mapping)
66
+ merge! (fum, encoding_mapping)
62
67
return fum
63
68
end
64
69
65
- # for type 0 use cmap.
66
- # for symbol and zapfdingbats - use font encoding
67
- # for others use STD Encoding
68
- # Reading encoding from the font files in case of Symbolic fonts are not
69
- # supported.
70
- # Font subset is addressed with font name identification.
71
- function merge_encoding! (fum:: FontUnicodeMapping , encoding:: CosNullType ,
72
- doc:: CosDoc , font:: IDDRef{CosDict} )
73
- subtype = cosDocGetObject (doc, font, cn " Subtype" )
74
- subtype != = cn " Type1" && subtype != = cn " MMType1" && return fum
70
+ abstract type FontType end
71
+ struct FontType1 <: FontType end
72
+ struct FontType3 <: FontType end
73
+ struct FontMMType1 <: FontType end
74
+ struct FontTrueType <: FontType end
75
+ struct FontDefType <: FontType end
76
+
77
+ function FontType (subtype:: CosName )
78
+ subtype === cn " Type1" && return FontType1 ()
79
+ subtype === cn " Type3" && return FontType3 ()
80
+ subtype === cn " TrueType" && return FontTrueType ()
81
+ subtype === cn " MMType1" && return FontMMType1 ()
82
+ return FontDefType ()
83
+ end
84
+
85
+ merge_encoding! (fum:: FontUnicodeMapping , ftype:: FontType ,
86
+ doc:: CosDoc , font:: IDDRef{CosDict} ) = fum
87
+
88
+ function merge_encoding! (fum:: Dict{UInt8, Char} ,
89
+ ftype:: Union{FontType1, FontMMType1} ,
90
+ doc:: CosDoc , font:: IDDRef{CosDict} )
75
91
basefont = cosDocGetObject (doc, font, cn " BaseFont" )
76
92
basefont_with_subset = CDTextString (basefont)
77
93
basefont_str = rsplit (basefont_with_subset, ' +' ;limit= 2 )[end ]
78
94
enc = basefont_str == " Symbol" ? SYMEncoding_to_Unicode :
79
95
basefont_str == " ZapfDingbats" ? ZAPEncoding_to_Unicode :
80
96
STDEncoding_to_Unicode
81
- merge! (fum. encoding , enc)
97
+ merge! (fum, enc)
82
98
return fum
99
+ end
100
+
101
+ # for type 0 use cmap.
102
+ # for symbol and zapfdingbats - use font encoding
103
+ # for others use STD Encoding
104
+ # Reading encoding from the font files in case of Symbolic fonts are not
105
+ # supported.
106
+ # Font subset is addressed with font name identification.
107
+ function merge_encoding! (fum:: Dict{UInt8, Char} , encoding:: CosNullType ,
108
+ doc:: CosDoc , font:: IDDRef{CosDict} )
109
+ subtype = cosDocGetObject (doc, font, cn " Subtype" )
110
+ subtype === CosNull && return fum
111
+ return merge_encoding! (fum, FontType (subtype), doc, font)
83
112
end
84
113
85
- function merge_encoding! (fum:: FontUnicodeMapping ,
114
+ function merge_encoding! (fum:: Dict{UInt8, Char} ,
86
115
encoding:: IDD{CosDict} ,
87
116
doc:: CosDoc , font:: IDDRef{CosDict} )
88
117
baseenc = cosDocGetObject (doc, encoding, cn " BaseEncoding" )
89
118
merge_encoding! (fum, baseenc, doc, font)
90
119
# Add the Differences
120
+ subtype = cosDocGetObject (doc, font, cn " Subtype" )
121
+ subtype === cn " Type3" && return fum
91
122
diff = cosDocGetObject (doc, encoding, cn " Differences" )
92
123
diff === CosNull && return fum
93
124
values = get (diff)
@@ -101,31 +132,28 @@ function merge_encoding!(fum::FontUnicodeMapping,
101
132
cid += 1
102
133
end
103
134
end
135
+
104
136
dict_to_unicode = dict_remap (d, AGL_Glyph_to_Unicode)
105
- merge! (fum. encoding , dict_to_unicode)
137
+ merge! (fum, dict_to_unicode)
106
138
return fum
107
139
end
108
140
109
- function merge_encoding! (fum:: FontUnicodeMapping , doc:: CosDoc ,
110
- font:: IDDRef{CosDict} )
111
- encoding = cosDocGetObject (doc, font, cn " Encoding" )
112
- merge_encoding! (fum, encoding, doc, font)
141
+ function get_unicode_mapping (doc:: CosDoc , font:: IDDRef{CosDict} )
113
142
toUnicode = cosDocGetObject (doc, font, cn " ToUnicode" )
114
- toUnicode == CosNull && return fum
115
- merge_encoding! (fum, toUnicode, doc, font)
143
+ toUnicode != = CosNull &&
144
+ return get_unicode_mapping (toUnicode)
145
+ encoding = cosDocGetObject (doc, font, cn " Encoding" )
146
+ d = merge_encoding! (Dict {UInt8, Char} (), encoding, doc, font)
147
+ return length (d) == 0 ? nothing : d
116
148
end
117
149
118
- function merge_encoding! (fum:: FontUnicodeMapping ,
119
- cmap:: CosIndirectObject{CosStream} ,
120
- doc:: CosDoc , font:: IDDRef{CosDict} )
121
- stm_cmap = get (cmap)
150
+ function get_unicode_mapping (cmap_stm:: CosIndirectObject{CosStream} )
151
+ io = get (cmap_stm)
122
152
try
123
- fum. cmap = read_cmap (stm_cmap)
124
- fum. hasCMap = true
153
+ return read_cmap (io)
125
154
finally
126
- util_close (stm_cmap )
155
+ util_close (io )
127
156
end
128
- return fum
129
157
end
130
158
131
159
function update_glyph_id_std_14 (cosdoc, cosfont,
@@ -185,22 +213,19 @@ function get_glyph_id_mapping(cosdoc::CosDoc, cosfont::IDD{CosDict})
185
213
else
186
214
glyph_name_to_cid[v] = cid
187
215
cid_to_glyph_name[cid] = v
188
- cid += 1
216
+ cid += 0x1
189
217
end
190
218
end
191
219
return glyph_name_to_cid, cid_to_glyph_name
192
220
end
193
221
194
- get_encoded_string (s:: CosString , fum:: Nothing ) = CDTextString (s)
195
-
196
- get_encoded_string (s:: CosString , fum:: FontUnicodeMapping ) =
222
+ get_encoded_string (s:: CosString , fum:: Union{Dict{UInt8, Char}, CMap} ) =
197
223
get_encoded_string (Vector {UInt8} (s), fum)
198
224
199
- @inline function get_encoded_string (v:: Union{Vector{UInt8}, NTuple{N, UInt8}} ,
200
- fum:: FontUnicodeMapping ) where N
225
+ function get_encoded_string (v:: Union{Vector{UInt8}, NTuple{N, UInt8}} ,
226
+ fum:: Dict{UInt8, Char} ) where N
201
227
length (v) == 0 && return " "
202
- fum. hasCMap && return get_encoded_string (v, fum. cmap)
203
- return String (NativeEncodingToUnicode (v, fum. encoding))
228
+ return String (NativeEncodingToUnicode (v, fum))
204
229
end
205
230
206
231
function get_unicode_chars (b:: UInt8 , i:: Interval , v:: Union{CosXString, CosArray} )
@@ -225,28 +250,26 @@ function get_unicode_chars(barr::Vector{UInt8})
225
250
nb = 0
226
251
retarr = Vector {Char} ()
227
252
while nb < l
228
- b1 = barr[1 ]
229
- b2 = barr[2 ]
253
+ b1, b2 = barr[1 ], barr[2 ]
230
254
nb += 2
231
- c:: UInt32 = 0
255
+ c = 0
232
256
if 0xD8 <= b1 <= 0xDB
233
257
# UTF-16 Supplementary plane = 4 bytes
234
258
b1 -= 0xD8
235
259
c = b1
236
- c = (c << 8 ) + b2
260
+ c = c * 256 + b2
237
261
b3 = barr[3 ]
238
262
b4 = barr[4 ]
239
263
nb += 2
240
264
if 0xDC <= b3 <= 0xDF
241
265
b3 -= 0xDC
242
266
c1 = b3
243
- c1 = (c1 << 8 ) + b4
244
- c = (c << 10 ) + c1
267
+ c1 = c1 * 256 + b4
268
+ c = c * 1024 + c1
245
269
c += 0x10000
246
270
end
247
271
else
248
- c = b1
249
- c = (c << 8 ) + b2
272
+ c = b1* 256 + b2
250
273
end
251
274
push! (retarr, Char (c))
252
275
end
@@ -385,16 +408,17 @@ mutable struct PDFont
385
408
cid_to_glyph_name:: Dict{UInt8, CosName}
386
409
flags:: UInt32
387
410
fontname:: CosName
411
+ props:: Dict
388
412
@inline function PDFont (doc:: PDDoc , cosfont:: IDD{CosDict} )
389
- fum = FontUnicodeMapping ()
390
- merge_encoding! (fum, doc. cosDoc, cosfont)
413
+ fum = get_unicode_mapping (doc. cosDoc, cosfont)
391
414
widths = get_font_widths (doc. cosDoc, cosfont)
392
415
glyph_name_to_cid, cid_to_glyph_name =
393
416
get_glyph_id_mapping (doc. cosDoc, cosfont)
394
417
flags = get_font_flags (doc, cosfont, widths)
395
418
fontname = get_font_name (doc, cosfont, widths)
419
+ props = Dict ()
396
420
return new (doc, cosfont, widths, fum, glyph_name_to_cid,
397
- cid_to_glyph_name, flags, fontname)
421
+ cid_to_glyph_name, flags, fontname, props )
398
422
end
399
423
end
400
424
@@ -403,6 +427,12 @@ SPACE_CODE(w::CIDWidth) = get_character_code(cn"space", w)
403
427
INIT_CODE (x) = 0x00
404
428
SPACE_CODE (x) = get_character_code (cn " space" , x)
405
429
430
+ function FontType (font:: PDFont )
431
+ subtype = cosDocGetObject (font. doc. cosDoc, font. obj, cn " Subtype" )
432
+ subtype === CosNull && return FontDefType ()
433
+ return FontType (subtype)
434
+ end
435
+
406
436
"""
407
437
```
408
438
pdFontIsBold(pdfont::PDFont) ->Bool
@@ -495,6 +525,8 @@ get_character_code(name::CosName, w) =
495
525
496
526
get_encoded_string (s, pdfont:: PDFont ) = get_encoded_string (s, pdfont. fum)
497
527
528
+ get_encoded_string (s, pdfont:: Nothing ) = CDTextString (s)
529
+
498
530
get_char (barr, w) = iterate (barr)
499
531
function get_char (barr, w:: CIDWidth )
500
532
next = iterate (barr)
0 commit comments