kavu-prawn-core 0.4.99
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +340 -0
- data/LICENSE +56 -0
- data/README +40 -0
- data/Rakefile +73 -0
- data/data/encodings/win_ansi.txt +29 -0
- data/data/fonts/Action Man.dfont +0 -0
- data/data/fonts/Activa.ttf +0 -0
- data/data/fonts/Chalkboard.ttf +0 -0
- data/data/fonts/Courier-Bold.afm +342 -0
- data/data/fonts/Courier-BoldOblique.afm +342 -0
- data/data/fonts/Courier-Oblique.afm +342 -0
- data/data/fonts/Courier.afm +342 -0
- data/data/fonts/DejaVuSans.ttf +0 -0
- data/data/fonts/Dustismo_Roman.ttf +0 -0
- data/data/fonts/Helvetica-Bold.afm +2827 -0
- data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
- data/data/fonts/Helvetica-Oblique.afm +3051 -0
- data/data/fonts/Helvetica.afm +3051 -0
- data/data/fonts/MustRead.html +19 -0
- data/data/fonts/Symbol.afm +213 -0
- data/data/fonts/Times-Bold.afm +2588 -0
- data/data/fonts/Times-BoldItalic.afm +2384 -0
- data/data/fonts/Times-Italic.afm +2667 -0
- data/data/fonts/Times-Roman.afm +2419 -0
- data/data/fonts/ZapfDingbats.afm +225 -0
- data/data/fonts/comicsans.ttf +0 -0
- data/data/fonts/gkai00mp.ttf +0 -0
- data/data/images/arrow.png +0 -0
- data/data/images/arrow2.png +0 -0
- data/data/images/barcode_issue.png +0 -0
- data/data/images/dice.alpha +0 -0
- data/data/images/dice.dat +0 -0
- data/data/images/dice.png +0 -0
- data/data/images/fractal.jpg +0 -0
- data/data/images/letterhead.jpg +0 -0
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.dat +0 -0
- data/data/images/page_white_text.png +0 -0
- data/data/images/pigs.jpg +0 -0
- data/data/images/rails.dat +0 -0
- data/data/images/rails.png +0 -0
- data/data/images/ruport.png +0 -0
- data/data/images/ruport_data.dat +0 -0
- data/data/images/ruport_transparent.png +0 -0
- data/data/images/ruport_type0.png +0 -0
- data/data/images/stef.jpg +0 -0
- data/data/images/web-links.dat +1 -0
- data/data/images/web-links.png +0 -0
- data/data/shift_jis_text.txt +1 -0
- data/examples/bounding_box/bounding_boxes.rb +44 -0
- data/examples/bounding_box/russian_boxes.rb +37 -0
- data/examples/general/background.rb +20 -0
- data/examples/general/canvas.rb +16 -0
- data/examples/general/measurement_units.rb +52 -0
- data/examples/general/multi_page_layout.rb +17 -0
- data/examples/general/page_geometry.rb +32 -0
- data/examples/graphics/basic_images.rb +27 -0
- data/examples/graphics/cmyk.rb +13 -0
- data/examples/graphics/curves.rb +12 -0
- data/examples/graphics/hexagon.rb +14 -0
- data/examples/graphics/image_fit.rb +16 -0
- data/examples/graphics/image_flow.rb +38 -0
- data/examples/graphics/image_position.rb +18 -0
- data/examples/graphics/line.rb +33 -0
- data/examples/graphics/png_types.rb +23 -0
- data/examples/graphics/polygons.rb +17 -0
- data/examples/graphics/remote_images.rb +12 -0
- data/examples/graphics/ruport_style_helpers.rb +20 -0
- data/examples/graphics/stroke_bounds.rb +21 -0
- data/examples/m17n/chinese_text_wrapping.rb +20 -0
- data/examples/m17n/euro.rb +16 -0
- data/examples/m17n/sjis.rb +29 -0
- data/examples/m17n/utf8.rb +14 -0
- data/examples/m17n/win_ansi_charset.rb +55 -0
- data/examples/text/alignment.rb +19 -0
- data/examples/text/dfont.rb +49 -0
- data/examples/text/family_based_styling.rb +25 -0
- data/examples/text/font_calculations.rb +92 -0
- data/examples/text/font_size.rb +34 -0
- data/examples/text/kerning.rb +31 -0
- data/examples/text/simple_text.rb +18 -0
- data/examples/text/simple_text_ttf.rb +18 -0
- data/examples/text/span.rb +30 -0
- data/examples/text/text_box.rb +26 -0
- data/examples/text/text_flow.rb +68 -0
- data/lib/prawn/compatibility.rb +38 -0
- data/lib/prawn/document/annotations.rb +63 -0
- data/lib/prawn/document/bounding_box.rb +263 -0
- data/lib/prawn/document/destinations.rb +81 -0
- data/lib/prawn/document/internals.rb +126 -0
- data/lib/prawn/document/page_geometry.rb +79 -0
- data/lib/prawn/document/span.rb +55 -0
- data/lib/prawn/document/text/box.rb +76 -0
- data/lib/prawn/document/text/wrapping.rb +59 -0
- data/lib/prawn/document/text.rb +185 -0
- data/lib/prawn/document.rb +309 -0
- data/lib/prawn/encoding.rb +121 -0
- data/lib/prawn/errors.rb +45 -0
- data/lib/prawn/font/afm.rb +202 -0
- data/lib/prawn/font/dfont.rb +31 -0
- data/lib/prawn/font/ttf.rb +326 -0
- data/lib/prawn/font.rb +288 -0
- data/lib/prawn/graphics/color.rb +141 -0
- data/lib/prawn/graphics.rb +257 -0
- data/lib/prawn/images/jpg.rb +45 -0
- data/lib/prawn/images/png.rb +199 -0
- data/lib/prawn/images.rb +336 -0
- data/lib/prawn/literal_string.rb +14 -0
- data/lib/prawn/measurement_extensions.rb +46 -0
- data/lib/prawn/measurements.rb +71 -0
- data/lib/prawn/name_tree.rb +165 -0
- data/lib/prawn/pdf_object.rb +73 -0
- data/lib/prawn/reference.rb +59 -0
- data/lib/prawn.rb +70 -0
- data/spec/annotations_spec.rb +90 -0
- data/spec/bounding_box_spec.rb +141 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +163 -0
- data/spec/font_spec.rb +241 -0
- data/spec/graphics_spec.rb +209 -0
- data/spec/images_spec.rb +68 -0
- data/spec/jpg_spec.rb +25 -0
- data/spec/measurement_units_spec.rb +23 -0
- data/spec/name_tree_spec.rb +103 -0
- data/spec/pdf_object_spec.rb +112 -0
- data/spec/png_spec.rb +196 -0
- data/spec/reference_spec.rb +42 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/text_spec.rb +178 -0
- metadata +259 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'prawn/font/ttf'
|
2
|
+
|
3
|
+
module Prawn
|
4
|
+
class Font
|
5
|
+
class DFont < TTF
|
6
|
+
# Returns a list of the names of all named fonts in the given dfont file.
|
7
|
+
# Note that fonts are not required to be named in a dfont file, so the
|
8
|
+
# list may be empty even if the file does contain fonts. Also, note that
|
9
|
+
# the list is returned in no particular order, so the first font in the
|
10
|
+
# list is not necessarily the font at index 0 in the file.
|
11
|
+
def self.named_fonts(file)
|
12
|
+
TTFunk::ResourceFile.open(file) do |file|
|
13
|
+
return file.resources_for("sfnt")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns the number of fonts contained in the dfont file.
|
18
|
+
def self.font_count(file)
|
19
|
+
TTFunk::ResourceFile.open(file) do |file|
|
20
|
+
return file.map["sfnt"][:list].length
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def read_ttf_file
|
27
|
+
TTFunk::File.from_dfont(@name, @options[:font] || 0)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,326 @@
|
|
1
|
+
require 'ttfunk'
|
2
|
+
require 'ttfunk/subset_collection'
|
3
|
+
|
4
|
+
module Prawn
|
5
|
+
class Font
|
6
|
+
class TTF < Font
|
7
|
+
attr_reader :ttf, :subsets
|
8
|
+
|
9
|
+
def initialize(document, name, options={})
|
10
|
+
super
|
11
|
+
|
12
|
+
@ttf = read_ttf_file
|
13
|
+
@subsets = TTFunk::SubsetCollection.new(@ttf)
|
14
|
+
|
15
|
+
@attributes = {}
|
16
|
+
@bounding_boxes = {}
|
17
|
+
@char_widths = {}
|
18
|
+
@has_kerning_data = @ttf.kerning.exists? && @ttf.kerning.tables.any?
|
19
|
+
|
20
|
+
@ascender = Integer(@ttf.ascent * scale_factor)
|
21
|
+
@descender = Integer(@ttf.descent * scale_factor)
|
22
|
+
@line_gap = Integer(@ttf.line_gap * scale_factor)
|
23
|
+
end
|
24
|
+
|
25
|
+
# +string+ must be UTF8-encoded.
|
26
|
+
def compute_width_of(string, options={})
|
27
|
+
scale = (options[:size] || size) / 1000.0
|
28
|
+
if options[:kerning]
|
29
|
+
kern(string).inject(0) do |s,r|
|
30
|
+
if r.is_a?(Numeric)
|
31
|
+
s - r
|
32
|
+
else
|
33
|
+
r.inject(s) { |s2, u| s2 + character_width_by_code(u) }
|
34
|
+
end
|
35
|
+
end * scale
|
36
|
+
else
|
37
|
+
string.unpack("U*").inject(0) do |s,r|
|
38
|
+
s + character_width_by_code(r)
|
39
|
+
end * scale
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def bbox
|
44
|
+
@bbox ||= @ttf.bbox.map { |i| Integer(i * scale_factor) }
|
45
|
+
end
|
46
|
+
|
47
|
+
def has_kerning_data?
|
48
|
+
@has_kerning_data
|
49
|
+
end
|
50
|
+
|
51
|
+
# Perform any changes to the string that need to happen
|
52
|
+
# before it is rendered to the canvas. Returns an array of
|
53
|
+
# subset "chunks", where the even-numbered indices are the
|
54
|
+
# font subset number, and the following entry element is
|
55
|
+
# either a string or an array (for kerned text).
|
56
|
+
#
|
57
|
+
# The +text+ parameter must be UTF8-encoded.
|
58
|
+
def encode_text(text,options={})
|
59
|
+
text = text.chomp
|
60
|
+
|
61
|
+
if options[:kerning]
|
62
|
+
last_subset = nil
|
63
|
+
kern(text).inject([]) do |result, element|
|
64
|
+
if element.is_a?(Numeric)
|
65
|
+
result.last[1] = [result.last[1]] unless result.last[1].is_a?(Array)
|
66
|
+
result.last[1] << element
|
67
|
+
result
|
68
|
+
else
|
69
|
+
encoded = @subsets.encode(element)
|
70
|
+
|
71
|
+
if encoded.first[0] == last_subset
|
72
|
+
result.last[1] << encoded.first[1]
|
73
|
+
encoded.shift
|
74
|
+
end
|
75
|
+
|
76
|
+
if encoded.any?
|
77
|
+
last_subset = encoded.last[0]
|
78
|
+
result + encoded
|
79
|
+
else
|
80
|
+
result
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
else
|
85
|
+
@subsets.encode(text.unpack("U*"))
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def basename
|
90
|
+
@basename ||= @ttf.name.postscript_name
|
91
|
+
end
|
92
|
+
|
93
|
+
# not sure how to compute this for true-type fonts...
|
94
|
+
def stemV
|
95
|
+
0
|
96
|
+
end
|
97
|
+
|
98
|
+
def italic_angle
|
99
|
+
@italic_angle ||= if @ttf.postscript.exists?
|
100
|
+
raw = @ttf.postscript.italic_angle
|
101
|
+
hi, low = raw >> 16, raw & 0xFF
|
102
|
+
hi = -((hi ^ 0xFFFF) + 1) if hi & 0x8000 != 0
|
103
|
+
"#{hi}.#{low}".to_f
|
104
|
+
else
|
105
|
+
0
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def cap_height
|
110
|
+
@cap_height ||= begin
|
111
|
+
height = @ttf.os2.exists? && @ttf.os2.cap_height || 0
|
112
|
+
height == 0 ? ascender : height
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def x_height
|
117
|
+
# FIXME: seems like if os2 table doesn't exist, we could
|
118
|
+
# just find the height of the lower-case 'x' glyph?
|
119
|
+
@ttf.os2.exists? && @ttf.os2.x_height || 0
|
120
|
+
end
|
121
|
+
|
122
|
+
def family_class
|
123
|
+
@family_class ||= (@ttf.os2.exists? && @ttf.os2.family_class || 0) >> 8
|
124
|
+
end
|
125
|
+
|
126
|
+
def serif?
|
127
|
+
@serif ||= [1,2,3,4,5,7].include?(family_class)
|
128
|
+
end
|
129
|
+
|
130
|
+
def script?
|
131
|
+
@script ||= family_class == 10
|
132
|
+
end
|
133
|
+
|
134
|
+
def pdf_flags
|
135
|
+
@flags ||= begin
|
136
|
+
flags = 0
|
137
|
+
flags |= 0x0001 if @ttf.postscript.fixed_pitch?
|
138
|
+
flags |= 0x0002 if serif?
|
139
|
+
flags |= 0x0008 if script?
|
140
|
+
flags |= 0x0040 if italic_angle != 0
|
141
|
+
flags |= 0x0004 # assume the font contains at least some non-latin characters
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def normalize_encoding(text)
|
146
|
+
if text.respond_to?(:encode!)
|
147
|
+
# if we're running under a M17n aware VM, ensure the string provided is
|
148
|
+
# UTF-8 (by converting it if necessary)
|
149
|
+
begin
|
150
|
+
text.encode!("UTF-8")
|
151
|
+
rescue
|
152
|
+
raise Prawn::Errors::IncompatibleStringEncoding, "Encoding " +
|
153
|
+
"#{text.encoding} can not be transparently converted to UTF-8. " +
|
154
|
+
"Please ensure the encoding of the string you are attempting " +
|
155
|
+
"to use is set correctly"
|
156
|
+
end
|
157
|
+
else
|
158
|
+
# on a non M17N aware VM, use unpack as a hackish way to verify the
|
159
|
+
# string is valid utf-8. I thought it was better than loading iconv
|
160
|
+
# though.
|
161
|
+
begin
|
162
|
+
text.unpack("U*")
|
163
|
+
rescue
|
164
|
+
raise Prawn::Errors::IncompatibleStringEncoding, "The string you " +
|
165
|
+
"are attempting to render is not encoded in valid UTF-8."
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
private
|
171
|
+
|
172
|
+
def cmap
|
173
|
+
@cmap ||= @ttf.cmap.unicode.first or raise("no unicode cmap for font")
|
174
|
+
end
|
175
|
+
|
176
|
+
# +string+ must be UTF8-encoded.
|
177
|
+
#
|
178
|
+
# Returns an array. If an element is a numeric, it represents the
|
179
|
+
# kern amount to inject at that position. Otherwise, the element
|
180
|
+
# is an array of UTF-16 characters.
|
181
|
+
def kern(string)
|
182
|
+
a = []
|
183
|
+
|
184
|
+
string.unpack("U*").each do |r|
|
185
|
+
if a.empty?
|
186
|
+
a << [r]
|
187
|
+
elsif (kern = kern_pairs_table[[cmap[a.last.last], cmap[r]]])
|
188
|
+
kern *= scale_factor
|
189
|
+
a << -kern << [r]
|
190
|
+
else
|
191
|
+
a.last << r
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
a
|
196
|
+
end
|
197
|
+
|
198
|
+
def kern_pairs_table
|
199
|
+
@kerning_data ||= has_kerning_data? ? @ttf.kerning.tables.first.pairs : {}
|
200
|
+
end
|
201
|
+
|
202
|
+
def cid_to_gid_map
|
203
|
+
max = cmap.code_map.keys.max
|
204
|
+
(0..max).map { |cid| cmap[cid] }.pack("n*")
|
205
|
+
end
|
206
|
+
|
207
|
+
def hmtx
|
208
|
+
@hmtx ||= @ttf.horizontal_metrics
|
209
|
+
end
|
210
|
+
|
211
|
+
def character_width_by_code(code)
|
212
|
+
return 0 unless cmap[code]
|
213
|
+
@char_widths[code] ||= Integer(hmtx.widths[cmap[code]] * scale_factor)
|
214
|
+
end
|
215
|
+
|
216
|
+
def scale_factor
|
217
|
+
@scale ||= 1000.0 / @ttf.header.units_per_em
|
218
|
+
end
|
219
|
+
|
220
|
+
def register(subset)
|
221
|
+
temp_name = @ttf.name.postscript_name
|
222
|
+
@document.ref(:Type => :Font, :BaseFont => temp_name) { |ref| embed(ref, subset) }
|
223
|
+
end
|
224
|
+
|
225
|
+
def embed(reference, subset)
|
226
|
+
font_content = @subsets[subset].encode
|
227
|
+
|
228
|
+
# FIXME: we need postscript_name and glyph widths from the font
|
229
|
+
# subset. Perhaps this could be done by querying the subset,
|
230
|
+
# rather than by parsing the font that the subset produces?
|
231
|
+
font = TTFunk::File.new(font_content)
|
232
|
+
|
233
|
+
# empirically, it looks like Adobe Reader will not display fonts
|
234
|
+
# if their font name is more than 33 bytes long. Strange. But true.
|
235
|
+
basename = font.name.postscript_name[0, 33]
|
236
|
+
|
237
|
+
raise "Can't detect a postscript name for #{file}" if basename.nil?
|
238
|
+
|
239
|
+
compressed_font = Zlib::Deflate.deflate(font_content)
|
240
|
+
|
241
|
+
fontfile = @document.ref(:Length => compressed_font.size,
|
242
|
+
:Length1 => font_content.size,
|
243
|
+
:Filter => :FlateDecode )
|
244
|
+
fontfile << compressed_font
|
245
|
+
|
246
|
+
descriptor = @document.ref(:Type => :FontDescriptor,
|
247
|
+
:FontName => basename,
|
248
|
+
:FontFile2 => fontfile,
|
249
|
+
:FontBBox => bbox,
|
250
|
+
:Flags => pdf_flags,
|
251
|
+
:StemV => stemV,
|
252
|
+
:ItalicAngle => italic_angle,
|
253
|
+
:Ascent => ascender,
|
254
|
+
:Descent => descender,
|
255
|
+
:CapHeight => cap_height,
|
256
|
+
:XHeight => x_height)
|
257
|
+
|
258
|
+
hmtx = font.horizontal_metrics
|
259
|
+
widths = font.cmap.tables.first.code_map.map { |gid|
|
260
|
+
Integer(hmtx.widths[gid] * scale_factor) }[32..-1]
|
261
|
+
|
262
|
+
# It would be nice to have Encoding set for the macroman subsets,
|
263
|
+
# and only do a ToUnicode cmap for non-encoded unicode subsets.
|
264
|
+
# However, apparently Adobe Reader won't render MacRoman encoded
|
265
|
+
# subsets if original font contains unicode characters. (It has to
|
266
|
+
# be some flag or something that ttfunk is simply copying over...
|
267
|
+
# but I can't figure out which flag that is.)
|
268
|
+
#
|
269
|
+
# For now, it's simplest to just create a unicode cmap for every font.
|
270
|
+
# It offends my inner purist, but it'll do.
|
271
|
+
|
272
|
+
map = @subsets[subset].to_unicode_map
|
273
|
+
|
274
|
+
ranges = [[]]
|
275
|
+
lines = map.keys.sort.inject("") do |s, code|
|
276
|
+
ranges << [] if ranges.last.length >= 100
|
277
|
+
unicode = map[code]
|
278
|
+
ranges.last << "<%02x><%04x>" % [code, unicode]
|
279
|
+
end
|
280
|
+
|
281
|
+
range_blocks = ranges.inject("") do |s, list|
|
282
|
+
s << "%d beginbfchar\n%s\nendbfchar\n" % [list.length, list.join("\n")]
|
283
|
+
end
|
284
|
+
|
285
|
+
to_unicode_cmap = UNICODE_CMAP_TEMPLATE % range_blocks.strip
|
286
|
+
|
287
|
+
cmap = @document.ref({})
|
288
|
+
cmap << to_unicode_cmap
|
289
|
+
cmap.compress_stream
|
290
|
+
|
291
|
+
reference.data.update(:Subtype => :TrueType,
|
292
|
+
:BaseFont => basename,
|
293
|
+
:FontDescriptor => descriptor,
|
294
|
+
:FirstChar => 32,
|
295
|
+
:LastChar => 255,
|
296
|
+
:Widths => @document.ref(widths),
|
297
|
+
:ToUnicode => cmap)
|
298
|
+
end
|
299
|
+
|
300
|
+
UNICODE_CMAP_TEMPLATE = <<-STR.strip.gsub(/^\s*/, "")
|
301
|
+
/CIDInit /ProcSet findresource begin
|
302
|
+
12 dict begin
|
303
|
+
begincmap
|
304
|
+
/CIDSystemInfo <<
|
305
|
+
/Registry (Adobe)
|
306
|
+
/Ordering (UCS)
|
307
|
+
/Supplement 0
|
308
|
+
>> def
|
309
|
+
/CMapName /Adobe-Identity-UCS def
|
310
|
+
/CMapType 2 def
|
311
|
+
1 begincodespacerange
|
312
|
+
<00><ff>
|
313
|
+
endcodespacerange
|
314
|
+
%s
|
315
|
+
endcmap
|
316
|
+
CMapName currentdict /CMap defineresource pop
|
317
|
+
end
|
318
|
+
end
|
319
|
+
STR
|
320
|
+
|
321
|
+
def read_ttf_file
|
322
|
+
TTFunk::File.open(@name)
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
326
|
+
end
|
data/lib/prawn/font.rb
ADDED
@@ -0,0 +1,288 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# font.rb : The Prawn font class
|
4
|
+
#
|
5
|
+
# Copyright May 2008, Gregory Brown / James Healy. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
require "prawn/font/afm"
|
9
|
+
require "prawn/font/ttf"
|
10
|
+
require "prawn/font/dfont"
|
11
|
+
|
12
|
+
module Prawn
|
13
|
+
|
14
|
+
class Document
|
15
|
+
# Without arguments, this returns the currently selected font. Otherwise,
|
16
|
+
# it sets the current font.
|
17
|
+
#
|
18
|
+
# The single parameter must be a string. It can be one of the 14 built-in
|
19
|
+
# fonts supported by PDF, or the location of a TTF file. The Font::AFM::BUILT_INS
|
20
|
+
# array specifies the valid built in font values.
|
21
|
+
#
|
22
|
+
# pdf.font "Times-Roman"
|
23
|
+
# pdf.font "Chalkboard.ttf"
|
24
|
+
#
|
25
|
+
# If a ttf font is specified, the glyphs necessary to render your document
|
26
|
+
# will be embedded in the rendered PDF. This should be your preferred option
|
27
|
+
# in most cases. It will increase the size of the resulting file, but also
|
28
|
+
# make it more portable.
|
29
|
+
#
|
30
|
+
def font(name=nil, options={})
|
31
|
+
return @font || font("Helvetica") if name.nil?
|
32
|
+
|
33
|
+
raise Errors::NotOnPage unless @current_page
|
34
|
+
new_font = find_font(name, options)
|
35
|
+
|
36
|
+
if block_given?
|
37
|
+
save_font do
|
38
|
+
set_font(new_font, options[:size])
|
39
|
+
yield
|
40
|
+
end
|
41
|
+
else
|
42
|
+
set_font(new_font, options[:size])
|
43
|
+
end
|
44
|
+
|
45
|
+
@font
|
46
|
+
end
|
47
|
+
|
48
|
+
# When called with no argument, returns the current font size.
|
49
|
+
# When called with a single argument but no block, sets the current font
|
50
|
+
# size. When a block is used, the font size is applied transactionally and
|
51
|
+
# is rolled back when the block exits. You may still change the font size
|
52
|
+
# within a transactional block for individual text segments, or nested calls
|
53
|
+
# to font_size.
|
54
|
+
#
|
55
|
+
# Prawn::Document.generate("font_size.pdf") do
|
56
|
+
# font_size 16
|
57
|
+
# text "At size 16"
|
58
|
+
#
|
59
|
+
# font_size(10) do
|
60
|
+
# text "At size 10"
|
61
|
+
# text "At size 6", :size => 6
|
62
|
+
# text "At size 10"
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# text "At size 16"
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# When called without an argument, this method returns the current font
|
69
|
+
# size.
|
70
|
+
#
|
71
|
+
def font_size(points=nil)
|
72
|
+
return @font_size unless points
|
73
|
+
size_before_yield = @font_size
|
74
|
+
@font_size = points
|
75
|
+
block_given? ? yield : return
|
76
|
+
@font_size = size_before_yield
|
77
|
+
end
|
78
|
+
|
79
|
+
# Sets the font directly, given an actual Font object
|
80
|
+
# and size.
|
81
|
+
def set_font(font, size=nil) # :nodoc:
|
82
|
+
@font = font
|
83
|
+
@font_size = size if size
|
84
|
+
end
|
85
|
+
|
86
|
+
# Saves the current font, and then yields. When the block
|
87
|
+
# finishes, the original font is restored.
|
88
|
+
def save_font
|
89
|
+
@font ||= find_font("Helvetica")
|
90
|
+
original_font = @font
|
91
|
+
original_size = @font_size
|
92
|
+
|
93
|
+
yield
|
94
|
+
ensure
|
95
|
+
set_font(original_font, original_size) if original_font
|
96
|
+
end
|
97
|
+
|
98
|
+
# Looks up the given font using the given criteria. Once a font has been
|
99
|
+
# found by that matches the criteria, it will be cached to subsequent lookups
|
100
|
+
# for that font will return the same object.
|
101
|
+
#--
|
102
|
+
# Challenges involved: the name alone is not sufficient to uniquely identify
|
103
|
+
# a font (think dfont suitcases that can hold multiple different fonts in a
|
104
|
+
# single file). Thus, the :name key is included in the cache key.
|
105
|
+
#
|
106
|
+
# It is further complicated, however, since fonts in some formats (like the
|
107
|
+
# dfont suitcases) can be identified either by numeric index, OR by their
|
108
|
+
# name within the suitcase, and both should hash to the same font object
|
109
|
+
# (to avoid the font being embedded multiple times). This is not yet implemented,
|
110
|
+
# which means if someone selects a font both by name, and by index, the
|
111
|
+
# font will be embedded twice. Since we do font subsetting, this double
|
112
|
+
# embedding won't be catastrophic, just annoying.
|
113
|
+
# ++
|
114
|
+
#
|
115
|
+
def find_font(name, options={}) #:nodoc:
|
116
|
+
if font_families.key?(name)
|
117
|
+
family, name = name, font_families[name][options[:style] || :normal]
|
118
|
+
|
119
|
+
if name.is_a?(Hash)
|
120
|
+
options = options.merge(name)
|
121
|
+
name = options[:file]
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
key = "#{name}:#{options[:font] || 0}"
|
126
|
+
font_registry[key] ||= Font.load(self, name, options.merge(:family => family))
|
127
|
+
end
|
128
|
+
|
129
|
+
# Hash of Font objects keyed by names
|
130
|
+
#
|
131
|
+
def font_registry #:nodoc:
|
132
|
+
@font_registry ||= {}
|
133
|
+
end
|
134
|
+
|
135
|
+
# Hash that maps font family names to their styled individual font names
|
136
|
+
#
|
137
|
+
# To add support for another font family, append to this hash, e.g:
|
138
|
+
#
|
139
|
+
# pdf.font_families.update(
|
140
|
+
# "MyTrueTypeFamily" => { :bold => "foo-bold.ttf",
|
141
|
+
# :italic => "foo-italic.ttf",
|
142
|
+
# :bold_italic => "foo-bold-italic.ttf",
|
143
|
+
# :normal => "foo.ttf" })
|
144
|
+
#
|
145
|
+
# This will then allow you to use the fonts like so:
|
146
|
+
#
|
147
|
+
# pdf.font("MyTrueTypeFamily", :style => :bold)
|
148
|
+
# pdf.text "Some bold text"
|
149
|
+
# pdf.font("MyTrueTypeFamily")
|
150
|
+
# pdf.text "Some normal text"
|
151
|
+
#
|
152
|
+
# This assumes that you have appropriate TTF fonts for each style you
|
153
|
+
# wish to support.
|
154
|
+
#
|
155
|
+
def font_families
|
156
|
+
@font_families ||= Hash.new { |h,k| h[k] = {} }.merge!(
|
157
|
+
{ "Courier" => { :bold => "Courier-Bold",
|
158
|
+
:italic => "Courier-Oblique",
|
159
|
+
:bold_italic => "Courier-BoldOblique",
|
160
|
+
:normal => "Courier" },
|
161
|
+
|
162
|
+
"Times-Roman" => { :bold => "Times-Bold",
|
163
|
+
:italic => "Times-Italic",
|
164
|
+
:bold_italic => "Times-BoldItalic",
|
165
|
+
:normal => "Times-Roman" },
|
166
|
+
|
167
|
+
"Helvetica" => { :bold => "Helvetica-Bold",
|
168
|
+
:italic => "Helvetica-Oblique",
|
169
|
+
:bold_italic => "Helvetica-BoldOblique",
|
170
|
+
:normal => "Helvetica" }
|
171
|
+
})
|
172
|
+
end
|
173
|
+
|
174
|
+
# Returns the width of the given string using the given font. If :size is not
|
175
|
+
# specified as one of the options, the string is measured using the current
|
176
|
+
# font size. You can also pass :kerning as an option to indicate whether
|
177
|
+
# kerning should be used when measuring the width (defaults to +false+).
|
178
|
+
#
|
179
|
+
# Note that the string _must_ be encoded properly for the font being used.
|
180
|
+
# For AFM fonts, this is WinAnsi. For TTF, make sure the font is encoded as
|
181
|
+
# UTF-8. You can use the Font#normalize_encoding method to make sure strings
|
182
|
+
# are in an encoding appropriate for the current font.
|
183
|
+
#--
|
184
|
+
# For the record, this method used to be a method of Font (and still delegates
|
185
|
+
# to width computations on Font). However, having the primary interface for
|
186
|
+
# calculating string widths exist on Font made it tricky to write extensions
|
187
|
+
# for Prawn in which widths are computed differently (e.g., taking formatting
|
188
|
+
# tags into account, or the like).
|
189
|
+
#
|
190
|
+
# By putting width_of here, on Document itself, extensions may easily override
|
191
|
+
# it and redefine the width calculation behavior.
|
192
|
+
#++
|
193
|
+
def width_of(string, options={})
|
194
|
+
font.compute_width_of(string, options)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# Provides font information and helper functions.
|
199
|
+
#
|
200
|
+
class Font
|
201
|
+
|
202
|
+
# The current font name
|
203
|
+
attr_reader :name
|
204
|
+
|
205
|
+
# The current font family
|
206
|
+
attr_reader :family
|
207
|
+
|
208
|
+
# The options hash used to initialize the font
|
209
|
+
attr_reader :options
|
210
|
+
|
211
|
+
def self.load(document,name,options={})
|
212
|
+
case name
|
213
|
+
when /\.ttf$/ then TTF.new(document, name, options)
|
214
|
+
when /\.dfont$/ then DFont.new(document, name, options)
|
215
|
+
when /\.afm$/ then AFM.new(document, name, options)
|
216
|
+
else AFM.new(document, name, options)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def initialize(document,name,options={}) #:nodoc:
|
221
|
+
@document = document
|
222
|
+
@name = name
|
223
|
+
@options = options
|
224
|
+
|
225
|
+
@family = options[:family]
|
226
|
+
|
227
|
+
@document.proc_set :PDF, :Text
|
228
|
+
@identifier = :"F#{@document.font_registry.size + 1}"
|
229
|
+
|
230
|
+
@references = {}
|
231
|
+
end
|
232
|
+
|
233
|
+
def ascender
|
234
|
+
@ascender / 1000.0 * size
|
235
|
+
end
|
236
|
+
|
237
|
+
def descender
|
238
|
+
@descender / 1000.0 * size
|
239
|
+
end
|
240
|
+
|
241
|
+
def line_gap
|
242
|
+
@line_gap / 1000.0 * size
|
243
|
+
end
|
244
|
+
|
245
|
+
def identifier_for(subset)
|
246
|
+
"#{@identifier}.#{subset}"
|
247
|
+
end
|
248
|
+
|
249
|
+
def inspect
|
250
|
+
"#{self.class.name}< #{name}: #{size} >"
|
251
|
+
end
|
252
|
+
|
253
|
+
# Normalizes the encoding of the string to an encoding supported by the font.
|
254
|
+
# The string is expected to be UTF-8 going in, and will be reencoded in-place
|
255
|
+
# (the argument will be modified directly). The return value is not defined.
|
256
|
+
def normalize_encoding(string)
|
257
|
+
raise NotImplementedError, "subclasses of Prawn::Font must implement #normalize_encoding"
|
258
|
+
end
|
259
|
+
|
260
|
+
def height_at(size)
|
261
|
+
@normalized_height ||= (@ascender - @descender + @line_gap) / 1000.0
|
262
|
+
@normalized_height * size
|
263
|
+
end
|
264
|
+
|
265
|
+
# Gets height of current font in PDF points at current font size
|
266
|
+
#
|
267
|
+
def height
|
268
|
+
height_at(size)
|
269
|
+
end
|
270
|
+
|
271
|
+
# Registers the given subset of the current font with the current PDF
|
272
|
+
# page. This is safe to call multiple times for a given font and subset,
|
273
|
+
# as it will only add the font the first time it is called.
|
274
|
+
#
|
275
|
+
def add_to_current_page(subset)
|
276
|
+
@references[subset] ||= register(subset)
|
277
|
+
@document.page_fonts.merge!(identifier_for(subset) => @references[subset])
|
278
|
+
end
|
279
|
+
|
280
|
+
private
|
281
|
+
|
282
|
+
def size
|
283
|
+
@document.font_size
|
284
|
+
end
|
285
|
+
|
286
|
+
end
|
287
|
+
|
288
|
+
end
|