/
ttf.rb
358 lines (297 loc) · 10.7 KB
/
ttf.rb
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
# encoding: utf-8
# prawn/font/ttf.rb : Implements AFM font support for Prawn
#
# Copyright May 2008, Gregory Brown / James Healy / Jamis Buck
# All Rights Reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.
require 'ttfunk'
require 'ttfunk/subset_collection'
module Prawn
class Font
# @private
class TTF < Font
attr_reader :ttf, :subsets
def unicode?
true
end
def initialize(document, name, options = {})
super
@ttf = read_ttf_file
@subsets = TTFunk::SubsetCollection.new(@ttf)
@attributes = {}
@bounding_boxes = {}
@char_widths = {}
@has_kerning_data = @ttf.kerning.exists? && @ttf.kerning.tables.any?
@ascender = Integer(@ttf.ascent * scale_factor)
@descender = Integer(@ttf.descent * scale_factor)
@line_gap = Integer(@ttf.line_gap * scale_factor)
end
# NOTE: +string+ must be UTF8-encoded.
def compute_width_of(string, options = {}) #:nodoc:
scale = (options[:size] || size) / 1000.0
if options[:kerning]
kern(string).inject(0) do |s,r|
if r.is_a?(Numeric)
s - r
else
r.inject(s) { |s2, u| s2 + character_width_by_code(u) }
end
end * scale
else
string.codepoints.inject(0) do |s,r|
s + character_width_by_code(r)
end * scale
end
end
# The font bbox, as an array of integers
#
def bbox
@bbox ||= @ttf.bbox.map { |i| Integer(i * scale_factor) }
end
# Returns true if the font has kerning data, false otherwise
def has_kerning_data?
@has_kerning_data
end
# Perform any changes to the string that need to happen
# before it is rendered to the canvas. Returns an array of
# subset "chunks", where the even-numbered indices are the
# font subset number, and the following entry element is
# either a string or an array (for kerned text).
#
# The +text+ parameter must be UTF8-encoded.
#
def encode_text(text,options = {})
text = text.chomp
if options[:kerning]
last_subset = nil
kern(text).inject([]) do |result, element|
if element.is_a?(Numeric)
result.last[1] = [result.last[1]] unless result.last[1].is_a?(Array)
result.last[1] << element
result
else
encoded = @subsets.encode(element)
if encoded.first[0] == last_subset
result.last[1] << encoded.first[1]
encoded.shift
end
if encoded.any?
last_subset = encoded.last[0]
result + encoded
else
result
end
end
end
else
@subsets.encode(text.unpack("U*"))
end
end
def basename
@basename ||= @ttf.name.postscript_name
end
# not sure how to compute this for true-type fonts...
def stemV
0
end
def italic_angle
return @italic_angle if @italic_angle
if @ttf.postscript.exists?
raw = @ttf.postscript.italic_angle
hi, low = raw >> 16, raw & 0xFF
hi = -((hi ^ 0xFFFF) + 1) if hi & 0x8000 != 0
@italic_angle = "#{hi}.#{low}".to_f
else
@italic_angle = 0
end
@italic_angle
end
def cap_height
@cap_height ||= begin
height = @ttf.os2.exists? && @ttf.os2.cap_height || 0
height == 0 ? @ascender : height
end
end
def x_height
# FIXME: seems like if os2 table doesn't exist, we could
# just find the height of the lower-case 'x' glyph?
@ttf.os2.exists? && @ttf.os2.x_height || 0
end
def family_class
@family_class ||= (@ttf.os2.exists? && @ttf.os2.family_class || 0) >> 8
end
def serif?
@serif ||= [1,2,3,4,5,7].include?(family_class)
end
def script?
@script ||= family_class == 10
end
def pdf_flags
@flags ||= begin
flags = 0
flags |= 0x0001 if @ttf.postscript.fixed_pitch?
flags |= 0x0002 if serif?
flags |= 0x0008 if script?
flags |= 0x0040 if italic_angle != 0
flags |= 0x0004 # assume the font contains at least some non-latin characters
end
end
def normalize_encoding(text)
begin
text.encode(::Encoding::UTF_8)
rescue => e
puts e
raise Prawn::Errors::IncompatibleStringEncoding, "Encoding " \
"#{text.encoding} can not be transparently converted to UTF-8. " \
"Please ensure the encoding of the string you are attempting " \
"to use is set correctly"
end
end
def to_utf8(text)
text.encode("UTF-8")
end
def glyph_present?(char)
code = char.codepoints.first
cmap[code] > 0
end
# Returns the number of characters in +str+ (a UTF-8-encoded string).
#
def character_count(str)
str.length
end
private
def cmap
@cmap ||= @ttf.cmap.unicode.first or raise("no unicode cmap for font")
end
# +string+ must be UTF8-encoded.
#
# Returns an array. If an element is a numeric, it represents the
# kern amount to inject at that position. Otherwise, the element
# is an array of UTF-16 characters.
def kern(string)
a = []
string.each_codepoint do |r|
if a.empty?
a << [r]
elsif (kern = kern_pairs_table[[cmap[a.last.last], cmap[r]]])
kern *= scale_factor
a << -kern << [r]
else
a.last << r
end
end
a
end
def kern_pairs_table
@kerning_data ||= has_kerning_data? ? @ttf.kerning.tables.first.pairs : {}
end
def cid_to_gid_map
max = cmap.code_map.keys.max
(0..max).map { |cid| cmap[cid] }.pack("n*")
end
def hmtx
@hmtx ||= @ttf.horizontal_metrics
end
def character_width_by_code(code)
return 0 unless cmap[code]
# Some TTF fonts have nonzero widths for \n (UTF-8 / ASCII code: 10).
# Patch around this as we'll never be drawing a newline with a width.
return 0.0 if code == 10
@char_widths[code] ||= Integer(hmtx.widths[cmap[code]] * scale_factor)
end
def scale_factor
@scale ||= 1000.0 / @ttf.header.units_per_em
end
def register(subset)
temp_name = @ttf.name.postscript_name.gsub("\0","").to_sym
ref = @document.ref!(:Type => :Font, :BaseFont => temp_name)
# Embed the font metrics in the document after everything has been
# drawn, just before the document is emitted.
@document.renderer.before_render { |doc| embed(ref, subset) }
ref
end
def embed(reference, subset)
font_content = @subsets[subset].encode
# FIXME: we need postscript_name and glyph widths from the font
# subset. Perhaps this could be done by querying the subset,
# rather than by parsing the font that the subset produces?
font = TTFunk::File.new(font_content)
# empirically, it looks like Adobe Reader will not display fonts
# if their font name is more than 33 bytes long. Strange. But true.
basename = font.name.postscript_name[0, 33].gsub("\0","")
raise "Can't detect a postscript name for #{file}" if basename.nil?
fontfile = @document.ref!(:Length1 => font_content.size)
fontfile.stream << font_content
fontfile.stream.compress!
descriptor = @document.ref!(:Type => :FontDescriptor,
:FontName => basename.to_sym,
:FontFile2 => fontfile,
:FontBBox => bbox,
:Flags => pdf_flags,
:StemV => stemV,
:ItalicAngle => italic_angle,
:Ascent => @ascender,
:Descent => @descender,
:CapHeight => cap_height,
:XHeight => x_height)
hmtx = font.horizontal_metrics
widths = font.cmap.tables.first.code_map.map { |gid|
Integer(hmtx.widths[gid] * scale_factor) }[32..-1]
# It would be nice to have Encoding set for the macroman subsets,
# and only do a ToUnicode cmap for non-encoded unicode subsets.
# However, apparently Adobe Reader won't render MacRoman encoded
# subsets if original font contains unicode characters. (It has to
# be some flag or something that ttfunk is simply copying over...
# but I can't figure out which flag that is.)
#
# For now, it's simplest to just create a unicode cmap for every font.
# It offends my inner purist, but it'll do.
map = @subsets[subset].to_unicode_map
ranges = [[]]
map.keys.sort.inject("") do |s, code|
ranges << [] if ranges.last.length >= 100
unicode = map[code]
ranges.last << "<%02x><%04x>" % [code, unicode]
end
range_blocks = ranges.inject("") do |s, list|
s << "%d beginbfchar\n%s\nendbfchar\n" % [list.length, list.join("\n")]
end
to_unicode_cmap = UNICODE_CMAP_TEMPLATE % range_blocks.strip
cmap = @document.ref!({})
cmap << to_unicode_cmap
cmap.stream.compress!
reference.data.update(:Subtype => :TrueType,
:BaseFont => basename.to_sym,
:FontDescriptor => descriptor,
:FirstChar => 32,
:LastChar => 255,
:Widths => @document.ref!(widths),
:ToUnicode => cmap)
end
UNICODE_CMAP_TEMPLATE = <<-STR.strip.gsub(/^\s*/, "")
/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
%s
endcmap
CMapName currentdict /CMap defineresource pop
end
end
STR
def read_ttf_file
TTFunk::File.open(@name)
end
end
end
end