-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
font.coffee
224 lines (181 loc) · 6.9 KB
/
font.coffee
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
###
PDFFont - embeds fonts in PDF documents
By Devon Govett
###
TTFFont = require './font/ttf'
AFMFont = require './font/afm'
Subset = require './font/subset'
fs = require 'fs'
class PDFFont
constructor: (@document, src, family, @id) ->
if typeof src is 'string'
if src of STANDARD_FONTS
@isAFM = true
@font = new AFMFont STANDARD_FONTS[src]()
@registerAFM src
return
else if /\.(ttf|ttc)$/i.test src
@font = TTFFont.open src, family
else if /\.dfont$/i.test src
@font = TTFFont.fromDFont src, family
else
throw new Error 'Not a supported font format or standard PDF font.'
else if Buffer.isBuffer(src)
@font = TTFFont.fromBuffer src, family
else if src instanceof Uint8Array
@font = TTFFont.fromBuffer new Buffer(src), family
else if src instanceof ArrayBuffer
@font = TTFFont.fromBuffer new Buffer(new Uint8Array(src)), family
else
throw new Error 'Not a supported font format or standard PDF font.'
# create a subset for the font and register
@subset = new Subset @font
@registerTTF()
# This insanity is so browserify can inline the font files
STANDARD_FONTS =
"Courier": -> fs.readFileSync __dirname + "/font/data/Courier.afm", 'utf8'
"Courier-Bold": -> fs.readFileSync __dirname + "/font/data/Courier-Bold.afm", 'utf8'
"Courier-Oblique": -> fs.readFileSync __dirname + "/font/data/Courier-Oblique.afm", 'utf8'
"Courier-BoldOblique": -> fs.readFileSync __dirname + "/font/data/Courier-BoldOblique.afm", 'utf8'
"Helvetica": -> fs.readFileSync __dirname + "/font/data/Helvetica.afm", 'utf8'
"Helvetica-Bold": -> fs.readFileSync __dirname + "/font/data/Helvetica-Bold.afm", 'utf8'
"Helvetica-Oblique": -> fs.readFileSync __dirname + "/font/data/Helvetica-Oblique.afm", 'utf8'
"Helvetica-BoldOblique": -> fs.readFileSync __dirname + "/font/data/Helvetica-BoldOblique.afm", 'utf8'
"Times-Roman": -> fs.readFileSync __dirname + "/font/data/Times-Roman.afm", 'utf8'
"Times-Bold": -> fs.readFileSync __dirname + "/font/data/Times-Bold.afm", 'utf8'
"Times-Italic": -> fs.readFileSync __dirname + "/font/data/Times-Italic.afm", 'utf8'
"Times-BoldItalic": -> fs.readFileSync __dirname + "/font/data/Times-BoldItalic.afm", 'utf8'
"Symbol": -> fs.readFileSync __dirname + "/font/data/Symbol.afm", 'utf8'
"ZapfDingbats": -> fs.readFileSync __dirname + "/font/data/ZapfDingbats.afm", 'utf8'
use: (characters) ->
@subset?.use characters
embed: ->
return if @embedded or not @dictionary?
if @isAFM
@embedAFM()
else
@embedTTF()
@embedded = true
encode: (text) ->
if @isAFM
@font.encodeText text
else
@subset?.encodeText(text) or text
ref: ->
@dictionary ?= @document.ref()
registerTTF: ->
@name = @font.name.postscriptName
@scaleFactor = 1000.0 / @font.head.unitsPerEm
@bbox = (Math.round e * @scaleFactor for e in @font.bbox)
@stemV = 0 # not sure how to compute this for true-type fonts...
if @font.post.exists
raw = @font.post.italic_angle
hi = raw >> 16
low = raw & 0xFF
hi = -((hi ^ 0xFFFF) + 1) if hi & 0x8000 isnt 0
@italicAngle = +"#{hi}.#{low}"
else
@italicAngle = 0
@ascender = Math.round @font.ascender * @scaleFactor
@decender = Math.round @font.decender * @scaleFactor
@lineGap = Math.round @font.lineGap * @scaleFactor
@capHeight = (@font.os2.exists and @font.os2.capHeight) or @ascender
@xHeight = (@font.os2.exists and @font.os2.xHeight) or 0
@familyClass = (@font.os2.exists and @font.os2.familyClass or 0) >> 8
@isSerif = @familyClass in [1,2,3,4,5,7]
@isScript = @familyClass is 10
@flags = 0
@flags |= 1 << 0 if @font.post.isFixedPitch
@flags |= 1 << 1 if @isSerif
@flags |= 1 << 3 if @isScript
@flags |= 1 << 6 if @italicAngle isnt 0
@flags |= 1 << 5 # assume the font is nonsymbolic...
throw new Error 'No unicode cmap for font' if not @font.cmap.unicode
embedTTF: ->
data = @subset.encode()
fontfile = @document.ref()
fontfile.write data
fontfile.data.Length1 = fontfile.uncompressedLength
fontfile.end()
descriptor = @document.ref
Type: 'FontDescriptor'
FontName: @subset.postscriptName
FontFile2: fontfile
FontBBox: @bbox
Flags: @flags
StemV: @stemV
ItalicAngle: @italicAngle
Ascent: @ascender
Descent: @decender
CapHeight: @capHeight
XHeight: @xHeight
descriptor.end()
firstChar = +Object.keys(@subset.cmap)[0]
charWidths = for code, glyph of @subset.cmap
Math.round @font.widthOfGlyph(glyph)
cmap = @document.ref()
cmap.end toUnicodeCmap(@subset.subset)
@dictionary.data =
Type: 'Font'
BaseFont: @subset.postscriptName
Subtype: 'TrueType'
FontDescriptor: descriptor
FirstChar: firstChar
LastChar: firstChar + charWidths.length - 1
Widths: charWidths
Encoding: 'MacRomanEncoding'
ToUnicode: cmap
@dictionary.end()
toUnicodeCmap = (map) ->
unicodeMap = '''
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo <<
/Registry (Adobe)
/Ordering (UCS)
/Supplement 0
>> def
/CMapName /Adobe-Identity-UCS def
/CMapType 2 def
1 begincodespacerange
<00><ff>
endcodespacerange
'''
codes = Object.keys(map).sort (a, b) -> a - b
range = []
for code in codes
if range.length >= 100
unicodeMap += "\n#{range.length} beginbfchar\n#{range.join('\n')}\nendbfchar"
range = []
unicode = ('0000' + map[code].toString(16)).slice(-4)
code = (+code).toString(16)
range.push "<#{code}><#{unicode}>"
unicodeMap += "\n#{range.length} beginbfchar\n#{range.join('\n')}\nendbfchar\n" if range.length
unicodeMap += '''
endcmap
CMapName currentdict /CMap defineresource pop
end
end
'''
registerAFM: (@name) ->
{@ascender,@decender,@bbox,@lineGap} = @font
embedAFM: ->
@dictionary.data =
Type: 'Font'
BaseFont: @name
Subtype: 'Type1'
Encoding: 'WinAnsiEncoding'
@dictionary.end()
widthOfString: (string, size) ->
string = '' + string
width = 0
for i in [0...string.length]
charCode = string.charCodeAt(i)
width += @font.widthOfGlyph(@font.characterToGlyph(charCode)) or 0
scale = size / 1000
return width * scale
lineHeight: (size, includeGap = false) ->
gap = if includeGap then @lineGap else 0
(@ascender + gap - @decender) / 1000 * size
module.exports = PDFFont