prawn 1.0.0.rc2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +9 -0
- data/COPYING +2 -2
- data/Gemfile +8 -15
- data/LICENSE +1 -1
- data/Rakefile +25 -16
- data/data/images/16bit.alpha +0 -0
- data/data/images/16bit.color +0 -0
- data/data/images/dice.alpha +0 -0
- data/data/images/dice.color +0 -0
- data/data/images/indexed_color.dat +0 -0
- data/data/images/indexed_color.png +0 -0
- data/data/images/license.md +8 -0
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.color +0 -0
- data/lib/prawn.rb +85 -23
- data/lib/prawn/document.rb +134 -116
- data/lib/prawn/document/bounding_box.rb +33 -4
- data/lib/prawn/document/column_box.rb +18 -6
- data/lib/prawn/document/graphics_state.rb +11 -74
- data/lib/prawn/document/internals.rb +24 -23
- data/lib/prawn/document/span.rb +12 -10
- data/lib/prawn/encoding.rb +8 -9
- data/lib/prawn/errors.rb +13 -32
- data/lib/prawn/font.rb +137 -105
- data/lib/prawn/font/afm.rb +76 -32
- data/lib/prawn/font/dfont.rb +4 -3
- data/lib/prawn/font/ttf.rb +33 -25
- data/lib/prawn/font_metric_cache.rb +47 -0
- data/lib/prawn/graphics.rb +177 -57
- data/lib/prawn/graphics/cap_style.rb +4 -3
- data/lib/prawn/graphics/color.rb +5 -4
- data/lib/prawn/graphics/dash.rb +53 -31
- data/lib/prawn/graphics/join_style.rb +9 -7
- data/lib/prawn/graphics/patterns.rb +4 -15
- data/lib/prawn/graphics/transformation.rb +10 -9
- data/lib/prawn/graphics/transparency.rb +3 -1
- data/lib/prawn/{layout/grid.rb → grid.rb} +72 -54
- data/lib/prawn/image_handler.rb +42 -0
- data/lib/prawn/images.rb +58 -54
- data/lib/prawn/images/image.rb +6 -22
- data/lib/prawn/images/jpg.rb +20 -14
- data/lib/prawn/images/png.rb +58 -121
- data/lib/prawn/layout.rb +12 -15
- data/lib/prawn/measurement_extensions.rb +10 -6
- data/lib/prawn/measurements.rb +27 -21
- data/lib/prawn/outline.rb +108 -147
- data/lib/prawn/repeater.rb +10 -8
- data/lib/prawn/security.rb +59 -40
- data/lib/prawn/security/arcfour.rb +52 -0
- data/lib/prawn/soft_mask.rb +4 -4
- data/lib/prawn/stamp.rb +5 -3
- data/lib/prawn/table.rb +83 -60
- data/lib/prawn/table/cell.rb +17 -21
- data/lib/prawn/table/cell/image.rb +2 -3
- data/lib/prawn/table/cell/in_table.rb +8 -2
- data/lib/prawn/table/cell/span_dummy.rb +5 -0
- data/lib/prawn/table/cell/subtable.rb +3 -2
- data/lib/prawn/table/cell/text.rb +14 -12
- data/lib/prawn/table/cells.rb +58 -14
- data/lib/prawn/table/column_width_calculator.rb +61 -0
- data/lib/prawn/text.rb +27 -26
- data/lib/prawn/text/box.rb +12 -6
- data/lib/prawn/text/formatted.rb +5 -4
- data/lib/prawn/text/formatted/arranger.rb +290 -0
- data/lib/prawn/text/formatted/box.rb +85 -57
- data/lib/prawn/text/formatted/fragment.rb +11 -11
- data/lib/prawn/text/formatted/line_wrap.rb +266 -0
- data/lib/prawn/text/formatted/parser.rb +11 -4
- data/lib/prawn/text/formatted/wrap.rb +156 -0
- data/lib/prawn/utilities.rb +5 -3
- data/manual/document_and_page_options/document_and_page_options.rb +2 -1
- data/manual/document_and_page_options/metadata.rb +3 -3
- data/manual/document_and_page_options/page_size.rb +2 -2
- data/manual/document_and_page_options/print_scaling.rb +20 -0
- data/manual/example_file.rb +2 -7
- data/manual/example_helper.rb +62 -81
- data/manual/graphics/common_lines.rb +2 -0
- data/manual/graphics/helper.rb +11 -4
- data/manual/graphics/stroke_dash.rb +19 -14
- data/manual/manual/cover.rb +16 -0
- data/manual/manual/manual.rb +1 -5
- data/manual/text/fallback_fonts.rb +4 -4
- data/manual/text/formatted_text.rb +5 -5
- data/manual/text/inline.rb +2 -4
- data/manual/text/registering_families.rb +12 -12
- data/manual/text/single_usage.rb +4 -4
- data/manual/text/text.rb +0 -2
- data/prawn.gemspec +21 -13
- data/spec/acceptance/png.rb +23 -0
- data/spec/annotations_spec.rb +16 -32
- data/spec/bounding_box_spec.rb +22 -5
- data/spec/cell_spec.rb +49 -5
- data/spec/column_box_spec.rb +32 -0
- data/spec/destinations_spec.rb +5 -5
- data/spec/document_spec.rb +112 -118
- data/spec/extensions/encoding_helpers.rb +5 -2
- data/spec/font_metric_cache_spec.rb +52 -0
- data/spec/font_spec.rb +121 -120
- data/spec/formatted_text_arranger_spec.rb +24 -24
- data/spec/formatted_text_box_spec.rb +31 -32
- data/spec/formatted_text_fragment_spec.rb +2 -2
- data/spec/graphics_spec.rb +63 -45
- data/spec/grid_spec.rb +24 -13
- data/spec/image_handler_spec.rb +54 -0
- data/spec/images_spec.rb +34 -21
- data/spec/inline_formatted_text_parser_spec.rb +69 -20
- data/spec/jpg_spec.rb +3 -3
- data/spec/line_wrap_spec.rb +25 -14
- data/spec/measurement_units_spec.rb +5 -5
- data/spec/outline_spec.rb +68 -64
- data/spec/png_spec.rb +15 -18
- data/spec/reference_spec.rb +2 -82
- data/spec/repeater_spec.rb +1 -1
- data/spec/security_spec.rb +41 -9
- data/spec/soft_mask_spec.rb +0 -40
- data/spec/span_spec.rb +6 -11
- data/spec/spec_helper.rb +20 -2
- data/spec/stamp_spec.rb +19 -20
- data/spec/stroke_styles_spec.rb +31 -13
- data/spec/table/span_dummy_spec.rb +17 -0
- data/spec/table_spec.rb +268 -43
- data/spec/text_at_spec.rb +13 -27
- data/spec/text_box_spec.rb +35 -30
- data/spec/text_spec.rb +56 -40
- data/spec/transparency_spec.rb +5 -5
- metadata +214 -217
- data/README.md +0 -98
- 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/DejaVuSans.ttf +0 -0
- data/data/fonts/Dustismo_Roman.ttf +0 -0
- data/data/fonts/comicsans.ttf +0 -0
- data/data/fonts/gkai00mp.ttf +0 -0
- data/data/images/16bit.dat +0 -0
- data/data/images/barcode_issue.png +0 -0
- data/data/images/dice.dat +0 -0
- data/data/images/page_white_text.dat +0 -0
- data/data/images/rails.dat +0 -0
- data/data/images/rails.png +0 -0
- data/lib/prawn/compatibility.rb +0 -87
- data/lib/prawn/core.rb +0 -87
- data/lib/prawn/core/annotations.rb +0 -61
- data/lib/prawn/core/byte_string.rb +0 -9
- data/lib/prawn/core/destinations.rb +0 -90
- data/lib/prawn/core/document_state.rb +0 -79
- data/lib/prawn/core/literal_string.rb +0 -16
- data/lib/prawn/core/name_tree.rb +0 -177
- data/lib/prawn/core/object_store.rb +0 -320
- data/lib/prawn/core/page.rb +0 -212
- data/lib/prawn/core/pdf_object.rb +0 -125
- data/lib/prawn/core/reference.rb +0 -119
- data/lib/prawn/core/text.rb +0 -268
- data/lib/prawn/core/text/formatted/arranger.rb +0 -294
- data/lib/prawn/core/text/formatted/line_wrap.rb +0 -288
- data/lib/prawn/core/text/formatted/wrap.rb +0 -153
- data/lib/prawn/document/page_geometry.rb +0 -136
- data/lib/prawn/document/snapshot.rb +0 -89
- data/manual/manual/foreword.rb +0 -13
- data/manual/templates/full_template.rb +0 -23
- data/manual/templates/page_template.rb +0 -47
- data/manual/templates/templates.rb +0 -26
- data/manual/text/group.rb +0 -29
- data/spec/name_tree_spec.rb +0 -112
- data/spec/object_store_spec.rb +0 -170
- data/spec/pdf_object_spec.rb +0 -172
- data/spec/snapshot_spec.rb +0 -186
- data/spec/template_spec.rb +0 -351
data/lib/prawn/core/text.rb
DELETED
@@ -1,268 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# prawn/core/text.rb : Implements low level text helpers for Prawn
|
4
|
-
#
|
5
|
-
# Copyright January 2010, Daniel Nelson. All Rights Reserved.
|
6
|
-
#
|
7
|
-
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
-
|
9
|
-
module Prawn
|
10
|
-
module Core
|
11
|
-
module Text #:nodoc:
|
12
|
-
|
13
|
-
# These should be used as a base. Extensions may build on this list
|
14
|
-
#
|
15
|
-
VALID_OPTIONS = [:kerning, :size, :style]
|
16
|
-
MODES = { :fill => 0, :stroke => 1, :fill_stroke => 2, :invisible => 3,
|
17
|
-
:fill_clip => 4, :stroke_clip => 5, :fill_stroke_clip => 6,
|
18
|
-
:clip => 7 }
|
19
|
-
|
20
|
-
attr_reader :skip_encoding
|
21
|
-
|
22
|
-
# Low level text placement method. All font and size alterations
|
23
|
-
# should already be set
|
24
|
-
#
|
25
|
-
def draw_text!(text, options)
|
26
|
-
x,y = map_to_absolute(options[:at])
|
27
|
-
add_text_content(text,x,y,options)
|
28
|
-
end
|
29
|
-
|
30
|
-
# Low level call to set the current font style and extract text options from
|
31
|
-
# an options hash. Should be called from within a save_font block
|
32
|
-
#
|
33
|
-
def process_text_options(options)
|
34
|
-
if options[:style]
|
35
|
-
raise "Bad font family" unless font.family
|
36
|
-
font(font.family, :style => options[:style])
|
37
|
-
end
|
38
|
-
|
39
|
-
# must compare against false to keep kerning on as default
|
40
|
-
unless options[:kerning] == false
|
41
|
-
options[:kerning] = font.has_kerning_data?
|
42
|
-
end
|
43
|
-
|
44
|
-
options[:size] ||= font_size
|
45
|
-
end
|
46
|
-
|
47
|
-
# Retrieve the current default kerning setting.
|
48
|
-
#
|
49
|
-
# Defaults to true
|
50
|
-
#
|
51
|
-
def default_kerning?
|
52
|
-
return true if @default_kerning.nil?
|
53
|
-
@default_kerning
|
54
|
-
end
|
55
|
-
|
56
|
-
# Call with a boolean to set the document-wide kerning setting. This can be
|
57
|
-
# overridden using the :kerning text option when drawing text or a text
|
58
|
-
# box.
|
59
|
-
#
|
60
|
-
# pdf.default_kerning = false
|
61
|
-
# pdf.text("hello world") # text is not kerned
|
62
|
-
# pdf.text("hello world", :kerning => true) # text is kerned
|
63
|
-
#
|
64
|
-
def default_kerning(boolean)
|
65
|
-
@default_kerning = boolean
|
66
|
-
end
|
67
|
-
|
68
|
-
alias_method :default_kerning=, :default_kerning
|
69
|
-
|
70
|
-
# Call with no argument to retrieve the current default leading.
|
71
|
-
#
|
72
|
-
# Call with a number to set the document-wide text leading. This can be
|
73
|
-
# overridden using the :leading text option when drawing text or a text
|
74
|
-
# box.
|
75
|
-
#
|
76
|
-
# pdf.default_leading = 7
|
77
|
-
# pdf.text("hello world") # a leading of 7 is used
|
78
|
-
# pdf.text("hello world", :leading => 0) # a leading of 0 is used
|
79
|
-
#
|
80
|
-
# Defaults to 0
|
81
|
-
#
|
82
|
-
def default_leading(number=nil)
|
83
|
-
if number.nil?
|
84
|
-
return 0 if @default_leading.nil?
|
85
|
-
@default_leading
|
86
|
-
else
|
87
|
-
@default_leading = number
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
alias_method :default_leading=, :default_leading
|
92
|
-
|
93
|
-
# Call with no argument to retrieve the current text direction.
|
94
|
-
#
|
95
|
-
# Call with a symbol to set the document-wide text direction. This can be
|
96
|
-
# overridden using the :direction text option when drawing text or a text
|
97
|
-
# box.
|
98
|
-
#
|
99
|
-
# pdf.text_direction = :rtl
|
100
|
-
# pdf.text("hello world") # prints "dlrow olleh"
|
101
|
-
# pdf.text("hello world", :direction => :ltr) # prints "hello world"
|
102
|
-
#
|
103
|
-
# Valid directions are:
|
104
|
-
#
|
105
|
-
# * :ltr - left-to-right (default)
|
106
|
-
# * :rtl - right-to-left
|
107
|
-
#
|
108
|
-
# Side effects:
|
109
|
-
#
|
110
|
-
# * When printing left-to-right, the default text alignment is :left
|
111
|
-
# * When printing right-to-left, the default text alignment is :right
|
112
|
-
#
|
113
|
-
def text_direction(direction=nil)
|
114
|
-
if direction.nil?
|
115
|
-
return :ltr if @text_direction.nil?
|
116
|
-
@text_direction
|
117
|
-
else
|
118
|
-
@text_direction = direction
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
alias_method :text_direction=, :text_direction
|
123
|
-
|
124
|
-
# Call with no argument to retrieve the current fallback fonts.
|
125
|
-
#
|
126
|
-
# Call with an array of font names. Each name must be the name of an AFM
|
127
|
-
# font or the name that was used to register a family of TTF fonts (see
|
128
|
-
# Prawn::Document#font_families). If present, then each glyph will be
|
129
|
-
# rendered using the first font that includes the glyph, starting with the
|
130
|
-
# current font and then moving through :fallback_fonts from left to right.
|
131
|
-
#
|
132
|
-
# Call with an empty array to turn off fallback fonts
|
133
|
-
#
|
134
|
-
# file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
|
135
|
-
# font_families["Kai"] = {
|
136
|
-
# :normal => { :file => file, :font => "Kai" }
|
137
|
-
# }
|
138
|
-
# file = "#{Prawn::DATADIR}/fonts/Action Man.dfont"
|
139
|
-
# font_families["Action Man"] = {
|
140
|
-
# :normal => { :file => file, :font => "ActionMan" },
|
141
|
-
# }
|
142
|
-
# fallback_fonts ["Times-Roman", "Kai"]
|
143
|
-
# font "Action Man"
|
144
|
-
# text "hello ƒ 你好"
|
145
|
-
# > hello prints in Action Man
|
146
|
-
# > ƒ prints in Times-Roman
|
147
|
-
# > 你好 prints in Kai
|
148
|
-
#
|
149
|
-
# fallback_fonts [] # clears document-wide fallback fonts
|
150
|
-
#
|
151
|
-
# Side effects:
|
152
|
-
#
|
153
|
-
# * Increased overhead when fallback fonts are declared as each glyph is
|
154
|
-
# checked to see whether it exists in the current font
|
155
|
-
#
|
156
|
-
def fallback_fonts(fallback_fonts=nil)
|
157
|
-
if fallback_fonts.nil?
|
158
|
-
return [] if @fallback_fonts.nil?
|
159
|
-
@fallback_fonts
|
160
|
-
else
|
161
|
-
@fallback_fonts = fallback_fonts
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
alias_method :fallback_fonts=, :fallback_fonts
|
166
|
-
|
167
|
-
# Call with no argument to retrieve the current text rendering mode.
|
168
|
-
#
|
169
|
-
# Call with a symbol and block to temporarily change the current
|
170
|
-
# text rendering mode.
|
171
|
-
#
|
172
|
-
# pdf.text_rendering_mode(:stroke) do
|
173
|
-
# pdf.text("Outlined Text")
|
174
|
-
# end
|
175
|
-
#
|
176
|
-
# Valid modes are:
|
177
|
-
#
|
178
|
-
# * :fill - fill text (default)
|
179
|
-
# * :stroke - stroke text
|
180
|
-
# * :fill_stroke - fill, then stroke text
|
181
|
-
# * :invisible - invisible text
|
182
|
-
# * :fill_clip - fill text then add to path for clipping
|
183
|
-
# * :stroke_clip - stroke text then add to path for clipping
|
184
|
-
# * :fill_stroke_clip - fill then stroke text, then add to path for clipping
|
185
|
-
# * :clip - add text to path for clipping
|
186
|
-
#
|
187
|
-
def text_rendering_mode(mode=nil)
|
188
|
-
return @text_rendering_mode || :fill if mode.nil?
|
189
|
-
unless MODES.key?(mode)
|
190
|
-
raise ArgumentError, "mode must be between one of #{MODES.keys.join(', ')} (#{mode})"
|
191
|
-
end
|
192
|
-
original_mode = @text_rendering_mode || :fill
|
193
|
-
if original_mode == mode
|
194
|
-
yield
|
195
|
-
else
|
196
|
-
@text_rendering_mode = mode
|
197
|
-
add_content "\n#{MODES[mode]} Tr"
|
198
|
-
yield
|
199
|
-
add_content "\n#{MODES[original_mode]} Tr"
|
200
|
-
@text_rendering_mode = original_mode
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
# Increases or decreases the space between characters.
|
205
|
-
# For horizontal text, a positive value will increase the space.
|
206
|
-
# For veritical text, a positive value will decrease the space.
|
207
|
-
#
|
208
|
-
def character_spacing(amount=nil)
|
209
|
-
return @character_spacing || 0 if amount.nil?
|
210
|
-
original_character_spacing = character_spacing
|
211
|
-
if original_character_spacing == amount
|
212
|
-
yield
|
213
|
-
else
|
214
|
-
@character_spacing = amount
|
215
|
-
add_content "\n%.3f Tc" % amount
|
216
|
-
yield
|
217
|
-
add_content "\n%.3f Tc" % original_character_spacing
|
218
|
-
@character_spacing = original_character_spacing
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
# Increases or decreases the space between words.
|
223
|
-
# For horizontal text, a positive value will increase the space.
|
224
|
-
# For veritical text, a positive value will decrease the space.
|
225
|
-
#
|
226
|
-
def word_spacing(amount=nil)
|
227
|
-
return @word_spacing || 0 if amount.nil?
|
228
|
-
original_word_spacing = word_spacing
|
229
|
-
if original_word_spacing == amount
|
230
|
-
yield
|
231
|
-
else
|
232
|
-
@word_spacing = amount
|
233
|
-
add_content "\n%.3f Tw" % amount
|
234
|
-
yield
|
235
|
-
add_content "\n%.3f Tw" % original_word_spacing
|
236
|
-
@word_spacing = original_word_spacing
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
private
|
241
|
-
|
242
|
-
def add_text_content(text, x, y, options)
|
243
|
-
chunks = font.encode_text(text,options)
|
244
|
-
|
245
|
-
add_content "\nBT"
|
246
|
-
|
247
|
-
if options[:rotate]
|
248
|
-
rad = options[:rotate].to_f * Math::PI / 180
|
249
|
-
arr = [ Math.cos(rad), Math.sin(rad), -Math.sin(rad), Math.cos(rad), x, y ]
|
250
|
-
add_content "%.3f %.3f %.3f %.3f %.3f %.3f Tm" % arr
|
251
|
-
else
|
252
|
-
add_content "#{x} #{y} Td"
|
253
|
-
end
|
254
|
-
|
255
|
-
chunks.each do |(subset, string)|
|
256
|
-
font.add_to_current_page(subset)
|
257
|
-
add_content "/#{font.identifier_for(subset)} #{font_size} Tf"
|
258
|
-
|
259
|
-
operation = options[:kerning] && string.is_a?(Array) ? "TJ" : "Tj"
|
260
|
-
add_content Prawn::Core::PdfObject(string, true) << " " << operation
|
261
|
-
end
|
262
|
-
|
263
|
-
add_content "ET\n"
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
end
|
268
|
-
end
|
@@ -1,294 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# core/text/formatted/arranger.rb : Implements a data structure for 2-stage
|
4
|
-
# processing of lines of formatted text
|
5
|
-
#
|
6
|
-
# Copyright February 2010, Daniel Nelson. All Rights Reserved.
|
7
|
-
#
|
8
|
-
# This is free software. Please see the LICENSE and COPYING files for details.
|
9
|
-
|
10
|
-
module Prawn
|
11
|
-
module Core
|
12
|
-
module Text
|
13
|
-
module Formatted #:nodoc:
|
14
|
-
|
15
|
-
class Arranger #:nodoc:
|
16
|
-
attr_reader :max_line_height
|
17
|
-
attr_reader :max_descender
|
18
|
-
attr_reader :max_ascender
|
19
|
-
attr_accessor :consumed
|
20
|
-
|
21
|
-
# The following present only for testing purposes
|
22
|
-
attr_reader :unconsumed
|
23
|
-
attr_reader :fragments
|
24
|
-
attr_reader :current_format_state
|
25
|
-
|
26
|
-
def initialize(document, options={})
|
27
|
-
@document = document
|
28
|
-
@fragments = []
|
29
|
-
@unconsumed = []
|
30
|
-
@kerning = options[:kerning]
|
31
|
-
end
|
32
|
-
|
33
|
-
def space_count
|
34
|
-
if @unfinalized_line
|
35
|
-
raise "Lines must be finalized before calling #space_count"
|
36
|
-
end
|
37
|
-
@fragments.inject(0) do |sum, fragment|
|
38
|
-
sum + fragment.space_count
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def line_width
|
43
|
-
if @unfinalized_line
|
44
|
-
raise "Lines must be finalized before calling #line_width"
|
45
|
-
end
|
46
|
-
@fragments.inject(0) do |sum, fragment|
|
47
|
-
sum + fragment.width
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def line
|
52
|
-
if @unfinalized_line
|
53
|
-
raise "Lines must be finalized before calling #line"
|
54
|
-
end
|
55
|
-
@fragments.collect do |fragment|
|
56
|
-
if ruby_18 { true }
|
57
|
-
fragment.text
|
58
|
-
else
|
59
|
-
fragment.text.dup.force_encoding("utf-8")
|
60
|
-
end
|
61
|
-
end.join
|
62
|
-
end
|
63
|
-
|
64
|
-
def finalize_line
|
65
|
-
@unfinalized_line = false
|
66
|
-
omit_trailing_whitespace_from_line_width
|
67
|
-
@fragments = []
|
68
|
-
@consumed.each do |hash|
|
69
|
-
text = hash[:text]
|
70
|
-
format_state = hash.dup
|
71
|
-
format_state.delete(:text)
|
72
|
-
fragment = Prawn::Text::Formatted::Fragment.new(text,
|
73
|
-
format_state,
|
74
|
-
@document)
|
75
|
-
@fragments << fragment
|
76
|
-
set_fragment_measurements(fragment)
|
77
|
-
set_line_measurement_maximums(fragment)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def format_array=(array)
|
82
|
-
initialize_line
|
83
|
-
@unconsumed = []
|
84
|
-
array.each do |hash|
|
85
|
-
hash[:text].scan(/[^\n]+|\n/) do |line|
|
86
|
-
@unconsumed << hash.merge(:text => line)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def initialize_line
|
92
|
-
@unfinalized_line = true
|
93
|
-
@max_line_height = 0
|
94
|
-
@max_descender = 0
|
95
|
-
@max_ascender = 0
|
96
|
-
|
97
|
-
@consumed = []
|
98
|
-
@fragments = []
|
99
|
-
end
|
100
|
-
|
101
|
-
def finished?
|
102
|
-
@unconsumed.length == 0
|
103
|
-
end
|
104
|
-
|
105
|
-
def next_string
|
106
|
-
unless @unfinalized_line
|
107
|
-
raise "Lines must not be finalized when calling #next_string"
|
108
|
-
end
|
109
|
-
hash = @unconsumed.shift
|
110
|
-
if hash.nil?
|
111
|
-
nil
|
112
|
-
else
|
113
|
-
@consumed << hash.dup
|
114
|
-
@current_format_state = hash.dup
|
115
|
-
@current_format_state.delete(:text)
|
116
|
-
hash[:text]
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
def preview_next_string
|
121
|
-
hash = @unconsumed.first
|
122
|
-
if hash.nil? then nil
|
123
|
-
else hash[:text]
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
def apply_color_and_font_settings(fragment, &block)
|
128
|
-
if fragment.color
|
129
|
-
original_fill_color = @document.fill_color
|
130
|
-
original_stroke_color = @document.stroke_color
|
131
|
-
@document.fill_color(*fragment.color)
|
132
|
-
@document.stroke_color(*fragment.color)
|
133
|
-
apply_font_settings(fragment, &block)
|
134
|
-
@document.stroke_color = original_stroke_color
|
135
|
-
@document.fill_color = original_fill_color
|
136
|
-
else
|
137
|
-
apply_font_settings(fragment, &block)
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
def apply_font_settings(fragment=nil, &block)
|
142
|
-
if fragment.nil?
|
143
|
-
font = current_format_state[:font]
|
144
|
-
size = current_format_state[:size]
|
145
|
-
character_spacing = current_format_state[:character_spacing] ||
|
146
|
-
@document.character_spacing
|
147
|
-
styles = current_format_state[:styles]
|
148
|
-
font_style = font_style(styles)
|
149
|
-
else
|
150
|
-
font = fragment.font
|
151
|
-
size = fragment.size
|
152
|
-
character_spacing = fragment.character_spacing
|
153
|
-
styles = fragment.styles
|
154
|
-
font_style = font_style(styles)
|
155
|
-
end
|
156
|
-
|
157
|
-
@document.character_spacing(character_spacing) do
|
158
|
-
if font || font_style != :normal
|
159
|
-
raise "Bad font family" unless @document.font.family
|
160
|
-
@document.font(font || @document.font.family, :style => font_style) do
|
161
|
-
apply_font_size(size, styles, &block)
|
162
|
-
end
|
163
|
-
else
|
164
|
-
apply_font_size(size, styles, &block)
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
def update_last_string(printed, unprinted, normalized_soft_hyphen=nil)
|
170
|
-
return if printed.nil?
|
171
|
-
if printed.empty?
|
172
|
-
@consumed.pop
|
173
|
-
else
|
174
|
-
@consumed.last[:text] = printed
|
175
|
-
if normalized_soft_hyphen
|
176
|
-
@consumed.last[:normalized_soft_hyphen] = normalized_soft_hyphen
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
unless unprinted.empty?
|
181
|
-
@unconsumed.unshift(@current_format_state.merge(:text => unprinted))
|
182
|
-
end
|
183
|
-
|
184
|
-
load_previous_format_state if printed.empty?
|
185
|
-
end
|
186
|
-
|
187
|
-
def retrieve_fragment
|
188
|
-
if @unfinalized_line
|
189
|
-
raise "Lines must be finalized before fragments can be retrieved"
|
190
|
-
end
|
191
|
-
@fragments.shift
|
192
|
-
end
|
193
|
-
|
194
|
-
def repack_unretrieved
|
195
|
-
new_unconsumed = []
|
196
|
-
while fragment = retrieve_fragment
|
197
|
-
fragment.include_trailing_white_space!
|
198
|
-
new_unconsumed << fragment.format_state.merge(:text => fragment.text)
|
199
|
-
end
|
200
|
-
@unconsumed = new_unconsumed.concat(@unconsumed)
|
201
|
-
end
|
202
|
-
|
203
|
-
def font_style(styles)
|
204
|
-
if styles.nil?
|
205
|
-
:normal
|
206
|
-
elsif styles.include?(:bold) && styles.include?(:italic)
|
207
|
-
:bold_italic
|
208
|
-
elsif styles.include?(:bold)
|
209
|
-
:bold
|
210
|
-
elsif styles.include?(:italic)
|
211
|
-
:italic
|
212
|
-
else
|
213
|
-
:normal
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
private
|
218
|
-
|
219
|
-
def load_previous_format_state
|
220
|
-
if @consumed.empty?
|
221
|
-
@current_format_state = {}
|
222
|
-
else
|
223
|
-
hash = @consumed.last
|
224
|
-
@current_format_state = hash.dup
|
225
|
-
@current_format_state.delete(:text)
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
def apply_font_size(size, styles)
|
230
|
-
if subscript?(styles) || superscript?(styles)
|
231
|
-
relative_size = 0.583
|
232
|
-
if size.nil?
|
233
|
-
size = @document.font_size * relative_size
|
234
|
-
else
|
235
|
-
size = size * relative_size
|
236
|
-
end
|
237
|
-
end
|
238
|
-
if size.nil?
|
239
|
-
yield
|
240
|
-
else
|
241
|
-
@document.font_size(size) { yield }
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
def subscript?(styles)
|
246
|
-
if styles.nil? then false
|
247
|
-
else styles.include?(:subscript)
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
def superscript?(styles)
|
252
|
-
if styles.nil? then false
|
253
|
-
else styles.include?(:superscript)
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
def omit_trailing_whitespace_from_line_width
|
258
|
-
@consumed.reverse_each do |hash|
|
259
|
-
if hash[:text] == "\n"
|
260
|
-
break
|
261
|
-
elsif hash[:text].strip.empty? && @consumed.length > 1
|
262
|
-
# this entire fragment is trailing white space
|
263
|
-
hash[:exclude_trailing_white_space] = true
|
264
|
-
else
|
265
|
-
# this fragment contains the first non-white space we have
|
266
|
-
# encountered since the end of the line
|
267
|
-
hash[:exclude_trailing_white_space] = true
|
268
|
-
break
|
269
|
-
end
|
270
|
-
end
|
271
|
-
end
|
272
|
-
|
273
|
-
def set_fragment_measurements(fragment)
|
274
|
-
apply_font_settings(fragment) do
|
275
|
-
fragment.width = @document.width_of(fragment.text,
|
276
|
-
:kerning => @kerning)
|
277
|
-
fragment.line_height = @document.font.height
|
278
|
-
fragment.descender = @document.font.descender
|
279
|
-
fragment.ascender = @document.font.ascender
|
280
|
-
end
|
281
|
-
end
|
282
|
-
|
283
|
-
def set_line_measurement_maximums(fragment)
|
284
|
-
@max_line_height = [@max_line_height, fragment.line_height].compact.max
|
285
|
-
@max_descender = [@max_descender, fragment.descender].compact.max
|
286
|
-
@max_ascender = [@max_ascender, fragment.ascender].compact.max
|
287
|
-
end
|
288
|
-
|
289
|
-
end
|
290
|
-
|
291
|
-
end
|
292
|
-
end
|
293
|
-
end
|
294
|
-
end
|