prawn 2.4.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/prawn/document/bounding_box.rb +213 -141
- data/lib/prawn/document/column_box.rb +61 -26
- data/lib/prawn/document/internals.rb +25 -16
- data/lib/prawn/document/span.rb +20 -18
- data/lib/prawn/document.rb +257 -171
- data/lib/prawn/encoding.rb +2 -5
- data/lib/prawn/errors.rb +23 -34
- data/lib/prawn/font.rb +248 -135
- data/lib/prawn/font_metric_cache.rb +11 -10
- data/lib/prawn/fonts/afm.rb +85 -45
- data/lib/prawn/fonts/dfont.rb +7 -1
- data/lib/prawn/fonts/otf.rb +4 -1
- data/lib/prawn/fonts/to_unicode_cmap.rb +151 -0
- data/lib/prawn/fonts/ttc.rb +7 -2
- data/lib/prawn/fonts/ttf.rb +305 -93
- data/lib/prawn/fonts.rb +14 -0
- data/lib/prawn/graphics/blend_mode.rb +25 -28
- data/lib/prawn/graphics/cap_style.rb +9 -12
- data/lib/prawn/graphics/color.rb +57 -34
- data/lib/prawn/graphics/dash.rb +45 -42
- data/lib/prawn/graphics/join_style.rb +17 -11
- data/lib/prawn/graphics/patterns.rb +190 -69
- data/lib/prawn/graphics/transformation.rb +48 -41
- data/lib/prawn/graphics/transparency.rb +16 -40
- data/lib/prawn/graphics.rb +363 -253
- data/lib/prawn/grid.rb +184 -57
- data/lib/prawn/image_handler.rb +27 -10
- data/lib/prawn/images/image.rb +8 -10
- data/lib/prawn/images/jpg.rb +42 -19
- data/lib/prawn/images/png.rb +92 -41
- data/lib/prawn/images.rb +44 -57
- data/lib/prawn/measurement_extensions.rb +39 -8
- data/lib/prawn/measurements.rb +60 -5
- data/lib/prawn/outline.rb +114 -108
- data/lib/prawn/repeater.rb +51 -35
- data/lib/prawn/security/arcfour.rb +4 -4
- data/lib/prawn/security.rb +75 -70
- data/lib/prawn/soft_mask.rb +42 -30
- data/lib/prawn/stamp.rb +38 -42
- data/lib/prawn/text/box.rb +146 -96
- data/lib/prawn/text/formatted/arranger.rb +87 -26
- data/lib/prawn/text/formatted/box.rb +221 -150
- data/lib/prawn/text/formatted/fragment.rb +130 -14
- data/lib/prawn/text/formatted/line_wrap.rb +33 -24
- data/lib/prawn/text/formatted/parser.rb +112 -72
- data/lib/prawn/text/formatted/wrap.rb +12 -17
- data/lib/prawn/text/formatted.rb +75 -0
- data/lib/prawn/text.rb +441 -196
- data/lib/prawn/transformation_stack.rb +29 -10
- data/lib/prawn/utilities.rb +13 -13
- data/lib/prawn/version.rb +2 -1
- data/lib/prawn/view.rb +68 -53
- data/lib/prawn.rb +23 -18
- data.tar.gz.sig +0 -0
- metadata +54 -177
- metadata.gz.sig +0 -0
- data/.yardopts +0 -10
- data/Gemfile +0 -5
- data/Rakefile +0 -25
- data/manual/absolute_position.pdf +0 -0
- data/manual/basic_concepts/adding_pages.rb +0 -26
- data/manual/basic_concepts/basic_concepts.rb +0 -43
- data/manual/basic_concepts/creation.rb +0 -38
- data/manual/basic_concepts/cursor.rb +0 -32
- data/manual/basic_concepts/measurement.rb +0 -24
- data/manual/basic_concepts/origin.rb +0 -37
- data/manual/basic_concepts/other_cursor_helpers.rb +0 -39
- data/manual/basic_concepts/view.rb +0 -48
- data/manual/bounding_box/bounding_box.rb +0 -41
- data/manual/bounding_box/bounds.rb +0 -48
- data/manual/bounding_box/canvas.rb +0 -23
- data/manual/bounding_box/creation.rb +0 -22
- data/manual/bounding_box/indentation.rb +0 -45
- data/manual/bounding_box/nesting.rb +0 -52
- data/manual/bounding_box/russian_boxes.rb +0 -40
- data/manual/bounding_box/stretchy.rb +0 -29
- data/manual/contents.rb +0 -35
- data/manual/cover.rb +0 -43
- data/manual/document_and_page_options/background.rb +0 -29
- data/manual/document_and_page_options/document_and_page_options.rb +0 -34
- data/manual/document_and_page_options/metadata.rb +0 -25
- data/manual/document_and_page_options/page_margins.rb +0 -36
- data/manual/document_and_page_options/page_size.rb +0 -34
- data/manual/document_and_page_options/print_scaling.rb +0 -23
- data/manual/example_helper.rb +0 -8
- data/manual/graphics/blend_mode.rb +0 -52
- data/manual/graphics/circle_and_ellipse.rb +0 -21
- data/manual/graphics/color.rb +0 -22
- data/manual/graphics/common_lines.rb +0 -29
- data/manual/graphics/fill_and_stroke.rb +0 -41
- data/manual/graphics/fill_rules.rb +0 -38
- data/manual/graphics/gradients.rb +0 -43
- data/manual/graphics/graphics.rb +0 -64
- data/manual/graphics/helper.rb +0 -34
- data/manual/graphics/line_width.rb +0 -36
- data/manual/graphics/lines_and_curves.rb +0 -40
- data/manual/graphics/polygon.rb +0 -27
- data/manual/graphics/rectangle.rb +0 -20
- data/manual/graphics/rotate.rb +0 -25
- data/manual/graphics/scale.rb +0 -42
- data/manual/graphics/soft_masks.rb +0 -44
- data/manual/graphics/stroke_cap.rb +0 -30
- data/manual/graphics/stroke_dash.rb +0 -47
- data/manual/graphics/stroke_join.rb +0 -29
- data/manual/graphics/translate.rb +0 -29
- data/manual/graphics/transparency.rb +0 -33
- data/manual/how_to_read_this_manual.rb +0 -39
- data/manual/images/absolute_position.rb +0 -22
- data/manual/images/fit.rb +0 -20
- data/manual/images/horizontal.rb +0 -24
- data/manual/images/images.rb +0 -41
- data/manual/images/plain_image.rb +0 -17
- data/manual/images/scale.rb +0 -21
- data/manual/images/vertical.rb +0 -30
- data/manual/images/width_and_height.rb +0 -24
- data/manual/layout/boxes.rb +0 -26
- data/manual/layout/content.rb +0 -24
- data/manual/layout/layout.rb +0 -27
- data/manual/layout/simple_grid.rb +0 -22
- data/manual/outline/add_subsection_to.rb +0 -60
- data/manual/outline/insert_section_after.rb +0 -46
- data/manual/outline/outline.rb +0 -33
- data/manual/outline/sections_and_pages.rb +0 -66
- data/manual/repeatable_content/alternate_page_numbering.rb +0 -36
- data/manual/repeatable_content/page_numbering.rb +0 -55
- data/manual/repeatable_content/repeatable_content.rb +0 -35
- data/manual/repeatable_content/repeater.rb +0 -54
- data/manual/repeatable_content/stamp.rb +0 -40
- data/manual/security/encryption.rb +0 -28
- data/manual/security/permissions.rb +0 -43
- data/manual/security/security.rb +0 -28
- data/manual/table.rb +0 -16
- data/manual/text/alignment.rb +0 -43
- data/manual/text/color.rb +0 -24
- data/manual/text/column_box.rb +0 -30
- data/manual/text/fallback_fonts.rb +0 -41
- data/manual/text/font.rb +0 -40
- data/manual/text/font_size.rb +0 -44
- data/manual/text/font_style.rb +0 -25
- data/manual/text/formatted_callbacks.rb +0 -70
- data/manual/text/formatted_text.rb +0 -61
- data/manual/text/free_flowing_text.rb +0 -50
- data/manual/text/inline.rb +0 -40
- data/manual/text/kerning_and_character_spacing.rb +0 -38
- data/manual/text/leading.rb +0 -24
- data/manual/text/line_wrapping.rb +0 -60
- data/manual/text/paragraph_indentation.rb +0 -31
- data/manual/text/positioned_text.rb +0 -37
- data/manual/text/registering_families.rb +0 -51
- data/manual/text/rendering_and_color.rb +0 -36
- data/manual/text/right_to_left_text.rb +0 -54
- data/manual/text/rotation.rb +0 -52
- data/manual/text/single_usage.rb +0 -36
- data/manual/text/text.rb +0 -75
- data/manual/text/text_box_excess.rb +0 -35
- data/manual/text/text_box_extensions.rb +0 -48
- data/manual/text/text_box_overflow.rb +0 -51
- data/manual/text/utf8.rb +0 -27
- data/manual/text/win_ansi_charset.rb +0 -62
- data/prawn.gemspec +0 -51
- data/spec/data/curves.pdf +0 -66
- data/spec/extensions/encoding_helpers.rb +0 -11
- data/spec/prawn/document/bounding_box_spec.rb +0 -550
- data/spec/prawn/document/column_box_spec.rb +0 -75
- data/spec/prawn/document/security_spec.rb +0 -176
- data/spec/prawn/document_annotations_spec.rb +0 -76
- data/spec/prawn/document_destinations_spec.rb +0 -15
- data/spec/prawn/document_grid_spec.rb +0 -99
- data/spec/prawn/document_reference_spec.rb +0 -27
- data/spec/prawn/document_span_spec.rb +0 -44
- data/spec/prawn/document_spec.rb +0 -805
- data/spec/prawn/font_metric_cache_spec.rb +0 -54
- data/spec/prawn/font_spec.rb +0 -544
- data/spec/prawn/graphics/blend_mode_spec.rb +0 -63
- data/spec/prawn/graphics/transparency_spec.rb +0 -81
- data/spec/prawn/graphics_spec.rb +0 -872
- data/spec/prawn/graphics_stroke_styles_spec.rb +0 -229
- data/spec/prawn/image_handler_spec.rb +0 -53
- data/spec/prawn/images/jpg_spec.rb +0 -20
- data/spec/prawn/images/png_spec.rb +0 -283
- data/spec/prawn/images_spec.rb +0 -229
- data/spec/prawn/measurements_extensions_spec.rb +0 -24
- data/spec/prawn/outline_spec.rb +0 -512
- data/spec/prawn/repeater_spec.rb +0 -166
- data/spec/prawn/soft_mask_spec.rb +0 -74
- data/spec/prawn/stamp_spec.rb +0 -173
- data/spec/prawn/text/box_spec.rb +0 -1110
- data/spec/prawn/text/formatted/arranger_spec.rb +0 -466
- data/spec/prawn/text/formatted/box_spec.rb +0 -849
- data/spec/prawn/text/formatted/fragment_spec.rb +0 -343
- data/spec/prawn/text/formatted/line_wrap_spec.rb +0 -495
- data/spec/prawn/text/formatted/parser_spec.rb +0 -697
- data/spec/prawn/text_draw_text_spec.rb +0 -150
- data/spec/prawn/text_rendering_mode_spec.rb +0 -48
- data/spec/prawn/text_spacing_spec.rb +0 -95
- data/spec/prawn/text_spec.rb +0 -603
- data/spec/prawn/text_with_inline_formatting_spec.rb +0 -35
- data/spec/prawn/transformation_stack_spec.rb +0 -66
- data/spec/prawn/view_spec.rb +0 -63
- data/spec/prawn_manual_spec.rb +0 -35
- data/spec/spec_helper.rb +0 -48
data/spec/prawn/text/box_spec.rb
DELETED
@@ -1,1110 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Prawn::Text::Box do
|
6
|
-
let(:pdf) { create_pdf }
|
7
|
-
|
8
|
-
it 'is able to set leading document-wide' do
|
9
|
-
pdf.default_leading(7)
|
10
|
-
pdf.default_leading = 7
|
11
|
-
text_box = described_class.new('hello world', document: pdf)
|
12
|
-
expect(text_box.leading).to eq(7)
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'option should be able to override document-wide leading' do
|
16
|
-
pdf.default_leading = 7
|
17
|
-
text_box = described_class.new(
|
18
|
-
'hello world',
|
19
|
-
document: pdf,
|
20
|
-
leading: 20
|
21
|
-
)
|
22
|
-
expect(text_box.leading).to eq(20)
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'is able to set text direction document-wide' do
|
26
|
-
pdf.text_direction(:rtl)
|
27
|
-
pdf.text_direction = :rtl
|
28
|
-
string = "Hello world, how are you?\nI'm fine, thank you."
|
29
|
-
text_box = described_class.new(string, document: pdf)
|
30
|
-
text_box.render
|
31
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
32
|
-
expect(text.strings[0]).to eq('?uoy era woh ,dlrow olleH')
|
33
|
-
expect(text.strings[1]).to eq(".uoy knaht ,enif m'I")
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'is able to reverse multi-byte text' do
|
37
|
-
pdf.text_direction(:rtl)
|
38
|
-
pdf.text_direction = :rtl
|
39
|
-
pdf.text_direction = :rtl
|
40
|
-
pdf.font("#{Prawn::DATADIR}/fonts/gkai00mp.ttf", size: 16) do
|
41
|
-
pdf.text '写个小'
|
42
|
-
end
|
43
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
44
|
-
expect(text.strings[0]).to eq('小个写')
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'option should be able to override document-wide text direction' do
|
48
|
-
pdf.text_direction = :rtl
|
49
|
-
string = "Hello world, how are you?\nI'm fine, thank you."
|
50
|
-
text_box = described_class.new(
|
51
|
-
string,
|
52
|
-
document: pdf,
|
53
|
-
direction: :ltr
|
54
|
-
)
|
55
|
-
text_box.render
|
56
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
57
|
-
expect(text.strings[0]).to eq('Hello world, how are you?')
|
58
|
-
expect(text.strings[1]).to eq("I'm fine, thank you.")
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'only requires enough space for the descender and the ascender '\
|
62
|
-
'when determining whether a line can fit' do
|
63
|
-
text = 'Oh hai text rect'
|
64
|
-
options = {
|
65
|
-
document: pdf,
|
66
|
-
height: pdf.font.ascender + pdf.font.descender
|
67
|
-
}
|
68
|
-
text_box = described_class.new(text, options)
|
69
|
-
text_box.render
|
70
|
-
expect(text_box.text).to eq('Oh hai text rect')
|
71
|
-
|
72
|
-
text = "Oh hai text rect\nOh hai text rect"
|
73
|
-
options = {
|
74
|
-
document: pdf,
|
75
|
-
height: pdf.font.height + pdf.font.ascender + pdf.font.descender
|
76
|
-
}
|
77
|
-
text_box = described_class.new(text, options)
|
78
|
-
text_box.render
|
79
|
-
expect(text_box.text).to eq("Oh hai text rect\nOh hai text rect")
|
80
|
-
end
|
81
|
-
|
82
|
-
describe '#nothing_printed?' do
|
83
|
-
it 'returns true when nothing printed' do
|
84
|
-
string = "Hello world, how are you?\nI'm fine, thank you."
|
85
|
-
text_box = described_class.new(string, height: 2, document: pdf)
|
86
|
-
text_box.render
|
87
|
-
expect(text_box.nothing_printed?).to eq true
|
88
|
-
end
|
89
|
-
|
90
|
-
it 'returns false when something printed' do
|
91
|
-
string = "Hello world, how are you?\nI'm fine, thank you."
|
92
|
-
text_box = described_class.new(string, height: 14, document: pdf)
|
93
|
-
text_box.render
|
94
|
-
expect(text_box.nothing_printed?).to eq false
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
describe '#everything_printed?' do
|
99
|
-
it 'returns false when not everything printed' do
|
100
|
-
string = "Hello world, how are you?\nI'm fine, thank you."
|
101
|
-
text_box = described_class.new(string, height: 14, document: pdf)
|
102
|
-
text_box.render
|
103
|
-
expect(text_box.everything_printed?).to eq false
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'returns true when everything printed' do
|
107
|
-
string = "Hello world, how are you?\nI'm fine, thank you."
|
108
|
-
text_box = described_class.new(string, document: pdf)
|
109
|
-
text_box.render
|
110
|
-
expect(text_box.everything_printed?).to eq true
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
describe '#line_gap' do
|
115
|
-
it '==S the line gap of the font when using a single font and font size' do
|
116
|
-
string = "Hello world, how are you?\nI'm fine, thank you."
|
117
|
-
text_box = described_class.new(string, document: pdf)
|
118
|
-
text_box.render
|
119
|
-
expect(text_box.line_gap).to be_within(0.0001).of(pdf.font.line_gap)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
describe '#render with :align => :justify' do
|
124
|
-
it 'draws the word spacing to the document' do
|
125
|
-
string = 'hello world ' * 20
|
126
|
-
options = { document: pdf, align: :justify }
|
127
|
-
text_box = described_class.new(string, options)
|
128
|
-
text_box.render
|
129
|
-
contents = PDF::Inspector::Text.analyze(pdf.render)
|
130
|
-
expect(contents.word_spacing[0]).to be > 0
|
131
|
-
end
|
132
|
-
|
133
|
-
it 'does not justify the last line of a paragraph' do
|
134
|
-
string = 'hello world '
|
135
|
-
options = { document: pdf, align: :justify }
|
136
|
-
text_box = described_class.new(string, options)
|
137
|
-
text_box.render
|
138
|
-
contents = PDF::Inspector::Text.analyze(pdf.render)
|
139
|
-
expect(contents.word_spacing).to be_empty
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
describe '#height without leading' do
|
144
|
-
it 'is the sum of the height of each line, not including the space below '\
|
145
|
-
'the last line' do
|
146
|
-
text = "Oh hai text rect.\nOh hai text rect."
|
147
|
-
options = { document: pdf }
|
148
|
-
text_box = described_class.new(text, options)
|
149
|
-
text_box.render
|
150
|
-
expect(text_box.height).to be_within(0.001)
|
151
|
-
.of(pdf.font.height * 2 - pdf.font.line_gap)
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
describe '#height with leading' do
|
156
|
-
it 'is the sum of the height of each line plus leading, but not including '\
|
157
|
-
'the space below the last line' do
|
158
|
-
text = "Oh hai text rect.\nOh hai text rect."
|
159
|
-
leading = 12
|
160
|
-
options = { document: pdf, leading: leading }
|
161
|
-
text_box = described_class.new(text, options)
|
162
|
-
text_box.render
|
163
|
-
expect(text_box.height).to be_within(0.001).of(
|
164
|
-
(pdf.font.height + leading) * 2 - pdf.font.line_gap - leading
|
165
|
-
)
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
context 'with :draw_text_callback' do
|
170
|
-
it 'hits the callback whenever text is drawn' do
|
171
|
-
draw_block = instance_spy('Draw block')
|
172
|
-
|
173
|
-
pdf.text_box 'this text is long enough to span two lines',
|
174
|
-
width: 150,
|
175
|
-
draw_text_callback: ->(text, _) { draw_block.kick(text) }
|
176
|
-
|
177
|
-
expect(draw_block).to have_received(:kick)
|
178
|
-
.with('this text is long enough to')
|
179
|
-
expect(draw_block).to have_received(:kick).with('span two lines')
|
180
|
-
end
|
181
|
-
|
182
|
-
it 'hits the callback once per fragment for :inline_format' do
|
183
|
-
draw_block = instance_spy('Draw block')
|
184
|
-
|
185
|
-
pdf.text_box 'this text has <b>fancy</b> formatting',
|
186
|
-
inline_format: true,
|
187
|
-
width: 500,
|
188
|
-
draw_text_callback: ->(text, _) { draw_block.kick(text) }
|
189
|
-
|
190
|
-
expect(draw_block).to have_received(:kick).with('this text has ')
|
191
|
-
expect(draw_block).to have_received(:kick).with('fancy')
|
192
|
-
expect(draw_block).to have_received(:kick).with(' formatting')
|
193
|
-
end
|
194
|
-
|
195
|
-
it 'does not call #draw_text!' do
|
196
|
-
allow(pdf).to receive(:draw_text!)
|
197
|
-
pdf.text_box 'some text',
|
198
|
-
width: 500,
|
199
|
-
draw_text_callback: ->(_, _) {}
|
200
|
-
expect(pdf).to_not have_received(:draw_text!)
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
describe '#valid_options' do
|
205
|
-
it 'returns an array' do
|
206
|
-
text_box = described_class.new('', document: pdf)
|
207
|
-
expect(text_box.valid_options).to be_a_kind_of(Array)
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
describe '#render' do
|
212
|
-
it 'does not fail if height is smaller than 1 line' do
|
213
|
-
text = 'Oh hai text rect. ' * 10
|
214
|
-
options = {
|
215
|
-
height: pdf.font.height * 0.5,
|
216
|
-
document: pdf
|
217
|
-
}
|
218
|
-
text_box = described_class.new(text, options)
|
219
|
-
text_box.render
|
220
|
-
expect(text_box.text).to eq('')
|
221
|
-
end
|
222
|
-
|
223
|
-
it 'draws content to the page' do
|
224
|
-
text = 'Oh hai text rect. ' * 10
|
225
|
-
options = { document: pdf }
|
226
|
-
text_box = described_class.new(text, options)
|
227
|
-
text_box.render
|
228
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
229
|
-
expect(text.strings).to_not be_empty
|
230
|
-
end
|
231
|
-
|
232
|
-
it 'does not draw a transformation matrix' do
|
233
|
-
text = 'Oh hai text rect. ' * 10
|
234
|
-
options = { document: pdf }
|
235
|
-
text_box = described_class.new(text, options)
|
236
|
-
text_box.render
|
237
|
-
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
|
238
|
-
expect(matrices.matrices.length).to eq(0)
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
describe '#render(:single_line => true)' do
|
243
|
-
it 'draws only one line to the page' do
|
244
|
-
text = 'Oh hai text rect. ' * 10
|
245
|
-
options = {
|
246
|
-
document: pdf,
|
247
|
-
single_line: true
|
248
|
-
}
|
249
|
-
text_box = described_class.new(text, options)
|
250
|
-
text_box.render
|
251
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
252
|
-
expect(text.strings.length).to eq(1)
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
describe '#render(:dry_run => true)' do
|
257
|
-
it 'does not draw any content to the page' do
|
258
|
-
text = 'Oh hai text rect. ' * 10
|
259
|
-
options = { document: pdf }
|
260
|
-
text_box = described_class.new(text, options)
|
261
|
-
text_box.render(dry_run: true)
|
262
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
263
|
-
expect(text.strings).to be_empty
|
264
|
-
end
|
265
|
-
|
266
|
-
it 'subsequent calls to render do not raise an ArgumentError exception' do
|
267
|
-
text = '™©'
|
268
|
-
options = { document: pdf }
|
269
|
-
text_box = described_class.new(text, options)
|
270
|
-
text_box.render(dry_run: true)
|
271
|
-
|
272
|
-
expect do
|
273
|
-
text_box.render
|
274
|
-
end.to_not raise_exception
|
275
|
-
end
|
276
|
-
end
|
277
|
-
|
278
|
-
describe '#render(:valign => :bottom)' do
|
279
|
-
it '#at should be the same from one dry run to the next' do
|
280
|
-
text = 'this is center text ' * 12
|
281
|
-
options = {
|
282
|
-
width: 162,
|
283
|
-
valign: :bottom,
|
284
|
-
document: pdf
|
285
|
-
}
|
286
|
-
text_box = described_class.new(text, options)
|
287
|
-
|
288
|
-
text_box.render(dry_run: true)
|
289
|
-
original_at = text_box.at.dup
|
290
|
-
|
291
|
-
text_box.render(dry_run: true)
|
292
|
-
expect(text_box.at).to eq(original_at)
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
describe '#render(:valign => :center)' do
|
297
|
-
it '#at should be the same from one dry run to the next' do
|
298
|
-
text = 'this is center text ' * 12
|
299
|
-
options = {
|
300
|
-
width: 162,
|
301
|
-
valign: :center,
|
302
|
-
document: pdf
|
303
|
-
}
|
304
|
-
text_box = described_class.new(text, options)
|
305
|
-
|
306
|
-
text_box.render(dry_run: true)
|
307
|
-
original_at = text_box.at.dup
|
308
|
-
|
309
|
-
text_box.render(dry_run: true)
|
310
|
-
expect(text_box.at).to eq(original_at)
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
describe '#render with :rotate option of 30)' do
|
315
|
-
let(:cos) { Math.cos(30 * Math::PI / 180) }
|
316
|
-
let(:sin) { Math.sin(30 * Math::PI / 180) }
|
317
|
-
let(:text) { 'Oh hai text rect. ' * 10 }
|
318
|
-
let(:options) do
|
319
|
-
{
|
320
|
-
document: pdf,
|
321
|
-
rotate: 30,
|
322
|
-
at: [300, 70],
|
323
|
-
width: 100,
|
324
|
-
height: 50
|
325
|
-
}
|
326
|
-
end
|
327
|
-
|
328
|
-
context 'with :rotate_around option of :center' do
|
329
|
-
it 'draws content to the page rotated about the center of the text' do
|
330
|
-
options[:rotate_around] = :center
|
331
|
-
text_box = described_class.new(text, options)
|
332
|
-
text_box.render
|
333
|
-
|
334
|
-
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
|
335
|
-
x = 350
|
336
|
-
y = 45
|
337
|
-
x_prime = x * cos - y * sin
|
338
|
-
y_prime = x * sin + y * cos
|
339
|
-
expect(matrices.matrices[0]).to eq([
|
340
|
-
1, 0, 0, 1,
|
341
|
-
reduce_precision(x - x_prime),
|
342
|
-
reduce_precision(y - y_prime)
|
343
|
-
])
|
344
|
-
expect(matrices.matrices[1]).to eq([
|
345
|
-
reduce_precision(cos),
|
346
|
-
reduce_precision(sin),
|
347
|
-
reduce_precision(-sin),
|
348
|
-
reduce_precision(cos),
|
349
|
-
0, 0
|
350
|
-
])
|
351
|
-
|
352
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
353
|
-
expect(text.strings).to_not be_empty
|
354
|
-
end
|
355
|
-
end
|
356
|
-
|
357
|
-
context 'with :rotate_around option of :upper_left' do
|
358
|
-
it 'draws content to the page rotated about the upper left corner of '\
|
359
|
-
'the text' do
|
360
|
-
options[:rotate_around] = :upper_left
|
361
|
-
text_box = described_class.new(text, options)
|
362
|
-
text_box.render
|
363
|
-
|
364
|
-
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
|
365
|
-
x_prime = 300 * cos - 70 * sin
|
366
|
-
y_prime = 300 * sin + 70 * cos
|
367
|
-
expect(matrices.matrices[0]).to eq([
|
368
|
-
1, 0, 0, 1,
|
369
|
-
reduce_precision(300 - x_prime),
|
370
|
-
reduce_precision(70 - y_prime)
|
371
|
-
])
|
372
|
-
expect(matrices.matrices[1]).to eq([
|
373
|
-
reduce_precision(cos),
|
374
|
-
reduce_precision(sin),
|
375
|
-
reduce_precision(-sin),
|
376
|
-
reduce_precision(cos),
|
377
|
-
0, 0
|
378
|
-
])
|
379
|
-
|
380
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
381
|
-
expect(text.strings).to_not be_empty
|
382
|
-
end
|
383
|
-
end
|
384
|
-
|
385
|
-
context 'with default :rotate_around' do
|
386
|
-
it 'draws content to the page rotated about the upper left corner of '\
|
387
|
-
'the text' do
|
388
|
-
text_box = described_class.new(text, options)
|
389
|
-
text_box.render
|
390
|
-
|
391
|
-
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
|
392
|
-
x_prime = 300 * cos - 70 * sin
|
393
|
-
y_prime = 300 * sin + 70 * cos
|
394
|
-
expect(matrices.matrices[0]).to eq([
|
395
|
-
1, 0, 0, 1,
|
396
|
-
reduce_precision(300 - x_prime),
|
397
|
-
reduce_precision(70 - y_prime)
|
398
|
-
])
|
399
|
-
expect(matrices.matrices[1]).to eq([
|
400
|
-
reduce_precision(cos),
|
401
|
-
reduce_precision(sin),
|
402
|
-
reduce_precision(-sin),
|
403
|
-
reduce_precision(cos),
|
404
|
-
0, 0
|
405
|
-
])
|
406
|
-
|
407
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
408
|
-
expect(text.strings).to_not be_empty
|
409
|
-
end
|
410
|
-
end
|
411
|
-
|
412
|
-
context 'with :rotate_around option of :upper_right' do
|
413
|
-
it 'draws content to the page rotated about the upper right corner of '\
|
414
|
-
'the text' do
|
415
|
-
options[:rotate_around] = :upper_right
|
416
|
-
text_box = described_class.new(text, options)
|
417
|
-
text_box.render
|
418
|
-
|
419
|
-
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
|
420
|
-
x = 400
|
421
|
-
y = 70
|
422
|
-
x_prime = x * cos - y * sin
|
423
|
-
y_prime = x * sin + y * cos
|
424
|
-
expect(matrices.matrices[0]).to eq([
|
425
|
-
1, 0, 0, 1,
|
426
|
-
reduce_precision(x - x_prime),
|
427
|
-
reduce_precision(y - y_prime)
|
428
|
-
])
|
429
|
-
expect(matrices.matrices[1]).to eq([
|
430
|
-
reduce_precision(cos),
|
431
|
-
reduce_precision(sin),
|
432
|
-
reduce_precision(-sin),
|
433
|
-
reduce_precision(cos),
|
434
|
-
0, 0
|
435
|
-
])
|
436
|
-
|
437
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
438
|
-
expect(text.strings).to_not be_empty
|
439
|
-
end
|
440
|
-
end
|
441
|
-
|
442
|
-
context 'with :rotate_around option of :lower_right' do
|
443
|
-
it 'draws content to the page rotated about the lower right corner of '\
|
444
|
-
'the text' do
|
445
|
-
options[:rotate_around] = :lower_right
|
446
|
-
text_box = described_class.new(text, options)
|
447
|
-
text_box.render
|
448
|
-
|
449
|
-
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
|
450
|
-
x = 400
|
451
|
-
y = 20
|
452
|
-
x_prime = x * cos - y * sin
|
453
|
-
y_prime = x * sin + y * cos
|
454
|
-
expect(matrices.matrices[0]).to eq([
|
455
|
-
1, 0, 0, 1,
|
456
|
-
reduce_precision(x - x_prime),
|
457
|
-
reduce_precision(y - y_prime)
|
458
|
-
])
|
459
|
-
expect(matrices.matrices[1]).to eq([
|
460
|
-
reduce_precision(cos),
|
461
|
-
reduce_precision(sin),
|
462
|
-
reduce_precision(-sin),
|
463
|
-
reduce_precision(cos),
|
464
|
-
0, 0
|
465
|
-
])
|
466
|
-
|
467
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
468
|
-
expect(text.strings).to_not be_empty
|
469
|
-
end
|
470
|
-
end
|
471
|
-
|
472
|
-
context 'with :rotate_around option of :lower_left' do
|
473
|
-
it 'draws content to the page rotated about the lower left corner of '\
|
474
|
-
'the text' do
|
475
|
-
options[:rotate_around] = :lower_left
|
476
|
-
text_box = described_class.new(text, options)
|
477
|
-
text_box.render
|
478
|
-
|
479
|
-
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
|
480
|
-
x = 300
|
481
|
-
y = 20
|
482
|
-
x_prime = x * cos - y * sin
|
483
|
-
y_prime = x * sin + y * cos
|
484
|
-
expect(matrices.matrices[0]).to eq([
|
485
|
-
1, 0, 0, 1,
|
486
|
-
reduce_precision(x - x_prime),
|
487
|
-
reduce_precision(y - y_prime)
|
488
|
-
])
|
489
|
-
expect(matrices.matrices[1]).to eq([
|
490
|
-
reduce_precision(cos),
|
491
|
-
reduce_precision(sin),
|
492
|
-
reduce_precision(-sin),
|
493
|
-
reduce_precision(cos),
|
494
|
-
0, 0
|
495
|
-
])
|
496
|
-
|
497
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
498
|
-
expect(text.strings).to_not be_empty
|
499
|
-
end
|
500
|
-
end
|
501
|
-
end
|
502
|
-
|
503
|
-
describe 'default height' do
|
504
|
-
it 'is the height from the bottom bound to document.y' do
|
505
|
-
target_height = pdf.y - pdf.bounds.bottom
|
506
|
-
text = "Oh hai\n" * 60
|
507
|
-
text_box = described_class.new(text, document: pdf)
|
508
|
-
text_box.render
|
509
|
-
expect(text_box.height).to be_within(pdf.font.height).of(target_height)
|
510
|
-
end
|
511
|
-
|
512
|
-
it 'uses the margin-box bottom if only in a stretchy bbox' do
|
513
|
-
pdf.bounding_box([0, pdf.cursor], width: pdf.bounds.width) do
|
514
|
-
target_height = pdf.y - pdf.bounds.bottom
|
515
|
-
text = "Oh hai\n" * 60
|
516
|
-
text_box = described_class.new(text, document: pdf)
|
517
|
-
text_box.render
|
518
|
-
expect(text_box.height).to be_within(pdf.font.height).of(target_height)
|
519
|
-
end
|
520
|
-
end
|
521
|
-
|
522
|
-
it 'uses the parent-box bottom if in a stretchy bbox and overflow is '\
|
523
|
-
':expand, even with an explicit height' do
|
524
|
-
pdf.bounding_box([0, pdf.cursor], width: pdf.bounds.width) do
|
525
|
-
target_height = pdf.y - pdf.bounds.bottom
|
526
|
-
text = "Oh hai\n" * 60
|
527
|
-
text_box = described_class.new(
|
528
|
-
text,
|
529
|
-
document: pdf,
|
530
|
-
height: 100,
|
531
|
-
overflow: :expand
|
532
|
-
)
|
533
|
-
text_box.render
|
534
|
-
expect(text_box.height).to be_within(pdf.font.height).of(target_height)
|
535
|
-
end
|
536
|
-
end
|
537
|
-
|
538
|
-
it 'uses the innermost non-stretchy bbox, not the margin box' do
|
539
|
-
pdf.bounding_box(
|
540
|
-
[0, pdf.cursor],
|
541
|
-
width: pdf.bounds.width,
|
542
|
-
height: 200
|
543
|
-
) do
|
544
|
-
pdf.bounding_box([0, pdf.cursor], width: pdf.bounds.width) do
|
545
|
-
text = "Oh hai\n" * 60
|
546
|
-
text_box = described_class.new(text, document: pdf)
|
547
|
-
text_box.render
|
548
|
-
expect(text_box.height).to be_within(pdf.font.height).of(200)
|
549
|
-
end
|
550
|
-
end
|
551
|
-
end
|
552
|
-
end
|
553
|
-
|
554
|
-
describe 'default at' do
|
555
|
-
it 'is the left corner of the bounds, and the current document.y' do
|
556
|
-
target_at = [pdf.bounds.left, pdf.y]
|
557
|
-
text = 'Oh hai text rect. ' * 100
|
558
|
-
options = { document: pdf }
|
559
|
-
text_box = described_class.new(text, options)
|
560
|
-
text_box.render
|
561
|
-
expect(text_box.at).to eq(target_at)
|
562
|
-
end
|
563
|
-
end
|
564
|
-
|
565
|
-
context 'with text than can fit in the box' do
|
566
|
-
let(:text) { 'Oh hai text rect. ' * 10 }
|
567
|
-
let(:options) do
|
568
|
-
{
|
569
|
-
width: 162.0,
|
570
|
-
height: 162.0,
|
571
|
-
document: pdf
|
572
|
-
}
|
573
|
-
end
|
574
|
-
|
575
|
-
it 'printed text should match requested text, except that preceding and ' \
|
576
|
-
'trailing white space will be stripped from each line, and newlines ' \
|
577
|
-
'may be inserted' do
|
578
|
-
text_box = described_class.new(" #{text}", options)
|
579
|
-
text_box.render
|
580
|
-
expect(text_box.text.tr("\n", ' ')).to eq(text.strip)
|
581
|
-
end
|
582
|
-
|
583
|
-
it 'render returns an empty string because no text remains unprinted' do
|
584
|
-
text_box = described_class.new(text, options)
|
585
|
-
expect(text_box.render).to eq('')
|
586
|
-
end
|
587
|
-
|
588
|
-
it 'is truncated when the leading is set high enough to prevent all the '\
|
589
|
-
'lines from being printed' do
|
590
|
-
options[:leading] = 40
|
591
|
-
text_box = described_class.new(text, options)
|
592
|
-
text_box.render
|
593
|
-
expect(text_box.text.tr("\n", ' ')).to_not eq(text.strip)
|
594
|
-
end
|
595
|
-
end
|
596
|
-
|
597
|
-
context 'with text that fits exactly in the box' do
|
598
|
-
let(:lines) { 3 }
|
599
|
-
let(:interlines) { lines - 1 }
|
600
|
-
let(:text) { (1..lines).to_a.join("\n") }
|
601
|
-
let(:options) do
|
602
|
-
{
|
603
|
-
width: 162.0,
|
604
|
-
height: pdf.font.ascender + pdf.font.height * interlines +
|
605
|
-
pdf.font.descender,
|
606
|
-
document: pdf
|
607
|
-
}
|
608
|
-
end
|
609
|
-
|
610
|
-
it 'has the expected height' do
|
611
|
-
expected_height = options.delete(:height)
|
612
|
-
text_box = described_class.new(text, options)
|
613
|
-
text_box.render
|
614
|
-
expect(text_box.height).to be_within(0.0001).of(expected_height)
|
615
|
-
end
|
616
|
-
|
617
|
-
it 'prints everything' do
|
618
|
-
text_box = described_class.new(text, options)
|
619
|
-
text_box.render
|
620
|
-
expect(text_box.text).to eq(text)
|
621
|
-
end
|
622
|
-
|
623
|
-
describe 'with leading' do
|
624
|
-
before do
|
625
|
-
options[:leading] = 15
|
626
|
-
end
|
627
|
-
|
628
|
-
it 'does not overflow when enough height is added' do
|
629
|
-
options[:height] += options[:leading] * interlines
|
630
|
-
text_box = described_class.new(text, options)
|
631
|
-
text_box.render
|
632
|
-
expect(text_box.text).to eq(text)
|
633
|
-
end
|
634
|
-
|
635
|
-
it 'overflows when insufficient height is added' do
|
636
|
-
options[:height] += options[:leading] * interlines - 1
|
637
|
-
text_box = described_class.new(text, options)
|
638
|
-
text_box.render
|
639
|
-
expect(text_box.text).to_not eq(text)
|
640
|
-
end
|
641
|
-
end
|
642
|
-
|
643
|
-
context 'with negative leading' do
|
644
|
-
before do
|
645
|
-
options[:leading] = -4
|
646
|
-
end
|
647
|
-
|
648
|
-
it 'does not overflow when enough height is removed' do
|
649
|
-
options[:height] += options[:leading] * interlines
|
650
|
-
text_box = described_class.new(text, options)
|
651
|
-
text_box.render
|
652
|
-
expect(text_box.text).to eq(text)
|
653
|
-
end
|
654
|
-
|
655
|
-
it 'overflows when too much height is removed' do
|
656
|
-
options[:height] += options[:leading] * interlines - 1
|
657
|
-
text_box = described_class.new(text, options)
|
658
|
-
text_box.render
|
659
|
-
expect(text_box.text).to_not eq(text)
|
660
|
-
end
|
661
|
-
end
|
662
|
-
end
|
663
|
-
|
664
|
-
context 'when printing UTF-8 string with higher bit characters' do
|
665
|
-
let(:text) { '©' }
|
666
|
-
|
667
|
-
let(:text_box) do
|
668
|
-
# not enough height to print any text, so we can directly compare against
|
669
|
-
# the input string
|
670
|
-
bounding_height = 1.0
|
671
|
-
options = {
|
672
|
-
height: bounding_height,
|
673
|
-
document: pdf
|
674
|
-
}
|
675
|
-
described_class.new(text, options)
|
676
|
-
end
|
677
|
-
|
678
|
-
before do
|
679
|
-
file = "#{Prawn::DATADIR}/fonts/Panic+Sans.dfont"
|
680
|
-
pdf.font_families['Panic Sans'] = {
|
681
|
-
normal: { file: file, font: 'PanicSans' },
|
682
|
-
italic: { file: file, font: 'PanicSans-Italic' },
|
683
|
-
bold: { file: file, font: 'PanicSans-Bold' },
|
684
|
-
bold_italic: { file: file, font: 'PanicSans-BoldItalic' }
|
685
|
-
}
|
686
|
-
end
|
687
|
-
|
688
|
-
describe 'when using a TTF font' do
|
689
|
-
it 'unprinted text should be in UTF-8 encoding' do
|
690
|
-
pdf.font('Panic Sans')
|
691
|
-
remaining_text = text_box.render
|
692
|
-
expect(remaining_text).to eq(text)
|
693
|
-
end
|
694
|
-
end
|
695
|
-
|
696
|
-
describe 'when using an AFM font' do
|
697
|
-
it 'unprinted text should be in UTF-8 encoding' do
|
698
|
-
remaining_text = text_box.render
|
699
|
-
expect(remaining_text).to eq(text)
|
700
|
-
end
|
701
|
-
end
|
702
|
-
end
|
703
|
-
|
704
|
-
context 'with more text than can fit in the box' do
|
705
|
-
let(:text) { 'Oh hai text rect. ' * 30 }
|
706
|
-
let(:bounding_height) { 162.0 }
|
707
|
-
let(:options) do
|
708
|
-
{
|
709
|
-
width: 162.0,
|
710
|
-
height: bounding_height,
|
711
|
-
document: pdf
|
712
|
-
}
|
713
|
-
end
|
714
|
-
|
715
|
-
context 'when truncated overflow' do
|
716
|
-
let(:text_box) do
|
717
|
-
described_class.new(text, options.merge(overflow: :truncate))
|
718
|
-
end
|
719
|
-
|
720
|
-
it 'is truncated' do
|
721
|
-
text_box.render
|
722
|
-
expect(text_box.text.tr("\n", ' ')).to_not eq(text.strip)
|
723
|
-
end
|
724
|
-
|
725
|
-
it 'render does not return an empty string because some text remains '\
|
726
|
-
'unprinted' do
|
727
|
-
expect(text_box.render).to_not be_empty
|
728
|
-
end
|
729
|
-
|
730
|
-
it '#height should be no taller than the specified height' do
|
731
|
-
text_box.render
|
732
|
-
expect(text_box.height).to be <= bounding_height
|
733
|
-
end
|
734
|
-
|
735
|
-
it '#height should be within one font height of the specified height' do
|
736
|
-
text_box.render
|
737
|
-
expect(bounding_height).to be_within(pdf.font.height)
|
738
|
-
.of(text_box.height)
|
739
|
-
end
|
740
|
-
|
741
|
-
context 'with :rotate option' do
|
742
|
-
it 'unrendered text should be the same as when not rotated' do
|
743
|
-
remaining_text = text_box.render
|
744
|
-
|
745
|
-
rotate = 30
|
746
|
-
options[:document] = pdf
|
747
|
-
options[:rotate] = rotate
|
748
|
-
options[:at] = [300, 70]
|
749
|
-
rotated_text_box = described_class.new(text, options)
|
750
|
-
expect(rotated_text_box.render).to eq(remaining_text)
|
751
|
-
end
|
752
|
-
end
|
753
|
-
end
|
754
|
-
|
755
|
-
context 'when truncated with text and size taken from the manual' do
|
756
|
-
it 'returns the right text' do
|
757
|
-
text = 'This is the beginning of the text. It will be cut somewhere ' \
|
758
|
-
'and the rest of the text will procede to be rendered this time by '\
|
759
|
-
'calling another method.' + ' . ' * 50
|
760
|
-
options[:width] = 300
|
761
|
-
options[:height] = 50
|
762
|
-
options[:size] = 18
|
763
|
-
text_box = described_class.new(text, options)
|
764
|
-
remaining_text = text_box.render
|
765
|
-
expect(remaining_text).to eq(
|
766
|
-
'text will procede to be rendered this time by calling another ' \
|
767
|
-
'method. . . . . . . . . . . . . . . . . . . . ' \
|
768
|
-
'. . . . . . . . . . . . . . . . . . . . . . ' \
|
769
|
-
'. . . . . . . . . '
|
770
|
-
)
|
771
|
-
end
|
772
|
-
end
|
773
|
-
|
774
|
-
context 'when expand overflow' do
|
775
|
-
let(:text_box) do
|
776
|
-
described_class.new(text, options.merge(overflow: :expand))
|
777
|
-
end
|
778
|
-
|
779
|
-
it 'height expands to encompass all the text '\
|
780
|
-
'(but not exceed the height of the page)' do
|
781
|
-
text_box.render
|
782
|
-
expect(text_box.height).to be > bounding_height
|
783
|
-
end
|
784
|
-
|
785
|
-
it 'displays the entire string (as long as there was space remaining on '\
|
786
|
-
'the page to print all the text)' do
|
787
|
-
text_box.render
|
788
|
-
expect(text_box.text.tr("\n", ' ')).to eq(text.strip)
|
789
|
-
end
|
790
|
-
|
791
|
-
it 'render returns an empty string because no text remains unprinted '\
|
792
|
-
'(as long as there was space remaining on the page to print all '\
|
793
|
-
'the text)' do
|
794
|
-
expect(text_box.render).to eq('')
|
795
|
-
end
|
796
|
-
end
|
797
|
-
|
798
|
-
context 'when shrink_to_fit overflow' do
|
799
|
-
let(:text_box) do
|
800
|
-
described_class.new(
|
801
|
-
text,
|
802
|
-
options.merge(
|
803
|
-
overflow: :shrink_to_fit,
|
804
|
-
min_font_size: 2
|
805
|
-
)
|
806
|
-
)
|
807
|
-
end
|
808
|
-
|
809
|
-
it 'displays the entire text' do
|
810
|
-
text_box.render
|
811
|
-
expect(text_box.text.tr("\n", ' ')).to eq(text.strip)
|
812
|
-
end
|
813
|
-
|
814
|
-
it 'render returns an empty string because no text remains unprinted' do
|
815
|
-
expect(text_box.render).to eq('')
|
816
|
-
end
|
817
|
-
|
818
|
-
it 'does not drop below the minimum font size' do
|
819
|
-
options[:overflow] = :shrink_to_fit
|
820
|
-
options[:min_font_size] = 10.1
|
821
|
-
text_box = described_class.new(text, options)
|
822
|
-
text_box.render
|
823
|
-
|
824
|
-
actual_text = PDF::Inspector::Text.analyze(pdf.render)
|
825
|
-
expect(actual_text.font_settings[0][:size]).to eq(10.1)
|
826
|
-
end
|
827
|
-
end
|
828
|
-
end
|
829
|
-
|
830
|
-
context 'with enough space to fit the text but using the ' \
|
831
|
-
'shrink_to_fit overflow' do
|
832
|
-
it 'does not shrink the text when there is no need to' do
|
833
|
-
bounding_height = 162.0
|
834
|
-
options = {
|
835
|
-
width: 162.0,
|
836
|
-
height: bounding_height,
|
837
|
-
overflow: :shrink_to_fit,
|
838
|
-
min_font_size: 5,
|
839
|
-
document: pdf
|
840
|
-
}
|
841
|
-
text_box = described_class.new("hello\nworld", options)
|
842
|
-
text_box.render
|
843
|
-
|
844
|
-
text = PDF::Inspector::Text.analyze(pdf.render)
|
845
|
-
expect(text.font_settings[0][:size]).to eq(12)
|
846
|
-
end
|
847
|
-
end
|
848
|
-
|
849
|
-
context 'with a solid block of Chinese characters' do
|
850
|
-
it 'printed text should match requested text, except for newlines' do
|
851
|
-
text = '写中国字' * 10
|
852
|
-
options = {
|
853
|
-
width: 162.0,
|
854
|
-
height: 162.0,
|
855
|
-
document: pdf,
|
856
|
-
overflow: :truncate
|
857
|
-
}
|
858
|
-
pdf.font "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
|
859
|
-
text_box = described_class.new(text, options)
|
860
|
-
text_box.render
|
861
|
-
expect(text_box.text.delete("\n")).to eq(text)
|
862
|
-
end
|
863
|
-
end
|
864
|
-
|
865
|
-
describe 'drawing bounding boxes' do
|
866
|
-
it 'restores the margin box when bounding box exits' do
|
867
|
-
margin_box = pdf.bounds
|
868
|
-
|
869
|
-
pdf.text_box 'Oh hai text box. ' * 11, height: pdf.font.height * 10
|
870
|
-
|
871
|
-
expect(pdf.bounds).to eq(margin_box)
|
872
|
-
end
|
873
|
-
end
|
874
|
-
|
875
|
-
describe '#render with :character_spacing option' do
|
876
|
-
it 'draws the character spacing to the document' do
|
877
|
-
string = 'hello world'
|
878
|
-
options = { document: pdf, character_spacing: 10 }
|
879
|
-
text_box = described_class.new(string, options)
|
880
|
-
text_box.render
|
881
|
-
contents = PDF::Inspector::Text.analyze(pdf.render)
|
882
|
-
expect(contents.character_spacing[0]).to eq(10)
|
883
|
-
end
|
884
|
-
|
885
|
-
it 'takes character spacing into account when wrapping' do
|
886
|
-
pdf.font 'Courier'
|
887
|
-
text_box = described_class.new(
|
888
|
-
'hello world',
|
889
|
-
width: 100,
|
890
|
-
overflow: :expand,
|
891
|
-
character_spacing: 10,
|
892
|
-
document: pdf
|
893
|
-
)
|
894
|
-
text_box.render
|
895
|
-
expect(text_box.text).to eq("hello\nworld")
|
896
|
-
end
|
897
|
-
end
|
898
|
-
|
899
|
-
describe 'wrapping' do
|
900
|
-
it 'wraps text' do
|
901
|
-
text = 'Please wrap this text about HERE. ' \
|
902
|
-
'More text that should be wrapped'
|
903
|
-
expect = "Please wrap this text about\n"\
|
904
|
-
"HERE. More text that should be\nwrapped"
|
905
|
-
|
906
|
-
pdf.font 'Courier'
|
907
|
-
text_box = described_class.new(
|
908
|
-
text,
|
909
|
-
width: 220,
|
910
|
-
overflow: :expand,
|
911
|
-
document: pdf
|
912
|
-
)
|
913
|
-
text_box.render
|
914
|
-
expect(text_box.text).to eq(expect)
|
915
|
-
end
|
916
|
-
|
917
|
-
# white space was being stripped after the entire line was generated,
|
918
|
-
# meaning that leading white space characters reduced the amount of space on
|
919
|
-
# the line for other characters, so wrapping "hello hello" resulted in
|
920
|
-
# "hello\n\nhello", rather than "hello\nhello"
|
921
|
-
#
|
922
|
-
it 'white space at beginning of line should not be taken into account ' \
|
923
|
-
'when computing line width' do
|
924
|
-
text = 'hello hello'
|
925
|
-
expect = "hello\nhello"
|
926
|
-
|
927
|
-
pdf.font 'Courier'
|
928
|
-
text_box = described_class.new(
|
929
|
-
text,
|
930
|
-
width: 40,
|
931
|
-
overflow: :expand,
|
932
|
-
document: pdf
|
933
|
-
)
|
934
|
-
text_box.render
|
935
|
-
expect(text_box.text).to eq(expect)
|
936
|
-
end
|
937
|
-
|
938
|
-
it 'respects end of line when wrapping text' do
|
939
|
-
text = "Please wrap only before\nTHIS word. Don't wrap this"
|
940
|
-
expect = text
|
941
|
-
|
942
|
-
pdf.font 'Courier'
|
943
|
-
text_box = described_class.new(
|
944
|
-
text,
|
945
|
-
width: 220,
|
946
|
-
overflow: :expand,
|
947
|
-
document: pdf
|
948
|
-
)
|
949
|
-
text_box.render
|
950
|
-
expect(text_box.text).to eq(expect)
|
951
|
-
end
|
952
|
-
|
953
|
-
it 'respects multiple newlines when wrapping text' do
|
954
|
-
text = "Please wrap only before THIS\n\nword. Don't wrap this"
|
955
|
-
expect = "Please wrap only before\nTHIS\n\nword. Don't wrap this"
|
956
|
-
|
957
|
-
pdf.font 'Courier'
|
958
|
-
text_box = described_class.new(
|
959
|
-
text,
|
960
|
-
width: 200,
|
961
|
-
overflow: :expand,
|
962
|
-
document: pdf
|
963
|
-
)
|
964
|
-
text_box.render
|
965
|
-
expect(text_box.text).to eq(expect)
|
966
|
-
end
|
967
|
-
|
968
|
-
it 'respects multiple newlines when wrapping text when those newlines '\
|
969
|
-
'coincide with a line break' do
|
970
|
-
text = "Please wrap only before\n\nTHIS word. Don't wrap this"
|
971
|
-
expect = text
|
972
|
-
|
973
|
-
pdf.font 'Courier'
|
974
|
-
text_box = described_class.new(
|
975
|
-
text,
|
976
|
-
width: 220,
|
977
|
-
overflow: :expand,
|
978
|
-
document: pdf
|
979
|
-
)
|
980
|
-
text_box.render
|
981
|
-
expect(text_box.text).to eq(expect)
|
982
|
-
end
|
983
|
-
|
984
|
-
it 'respects initial newlines' do
|
985
|
-
text = "\nThis should be on line 2"
|
986
|
-
expect = text
|
987
|
-
|
988
|
-
pdf.font 'Courier'
|
989
|
-
text_box = described_class.new(
|
990
|
-
text,
|
991
|
-
width: 220,
|
992
|
-
overflow: :expand,
|
993
|
-
document: pdf
|
994
|
-
)
|
995
|
-
text_box.render
|
996
|
-
expect(text_box.text).to eq(expect)
|
997
|
-
end
|
998
|
-
|
999
|
-
it 'wraps lines comprised of a single word of the bounds when '\
|
1000
|
-
'wrapping text' do
|
1001
|
-
text = 'You_can_wrap_this_text_HERE'
|
1002
|
-
expect = "You_can_wrap_this_text_HE\nRE"
|
1003
|
-
|
1004
|
-
pdf.font 'Courier'
|
1005
|
-
text_box = described_class.new(
|
1006
|
-
text,
|
1007
|
-
width: 180,
|
1008
|
-
overflow: :expand,
|
1009
|
-
document: pdf
|
1010
|
-
)
|
1011
|
-
text_box.render
|
1012
|
-
expect(text_box.text).to eq(expect)
|
1013
|
-
end
|
1014
|
-
|
1015
|
-
it 'wraps lines comprised of a single non-alpha word of the bounds when '\
|
1016
|
-
'wrapping text' do
|
1017
|
-
text = '©' * 30
|
1018
|
-
|
1019
|
-
pdf.font 'Courier'
|
1020
|
-
text_box = described_class.new(
|
1021
|
-
text,
|
1022
|
-
width: 180,
|
1023
|
-
overflow: :expand,
|
1024
|
-
document: pdf
|
1025
|
-
)
|
1026
|
-
|
1027
|
-
text_box.render
|
1028
|
-
|
1029
|
-
expected = "#{'©' * 25}\n#{'©' * 5}"
|
1030
|
-
expected = pdf.font.normalize_encoding(expected)
|
1031
|
-
expected = expected.force_encoding(Encoding::UTF_8)
|
1032
|
-
expect(text_box.text).to eq(expected)
|
1033
|
-
end
|
1034
|
-
|
1035
|
-
it 'wraps non-unicode strings using single-byte word-wrapping' do
|
1036
|
-
text = 'continúa esforzandote ' * 5
|
1037
|
-
text_box = described_class.new(
|
1038
|
-
text,
|
1039
|
-
width: 180,
|
1040
|
-
document: pdf
|
1041
|
-
)
|
1042
|
-
text_box.render
|
1043
|
-
results_with_accent = text_box.text
|
1044
|
-
|
1045
|
-
text = 'continua esforzandote ' * 5
|
1046
|
-
text_box = described_class.new(
|
1047
|
-
text,
|
1048
|
-
width: 180,
|
1049
|
-
document: pdf
|
1050
|
-
)
|
1051
|
-
text_box.render
|
1052
|
-
results_without_accent = text_box.text
|
1053
|
-
|
1054
|
-
expect(first_line(results_with_accent).length)
|
1055
|
-
.to eq(first_line(results_without_accent).length)
|
1056
|
-
end
|
1057
|
-
|
1058
|
-
it 'allows you to disable wrapping by char' do
|
1059
|
-
text = 'You_cannot_wrap_this_text_at_all_because_we_are_disabling_' \
|
1060
|
-
'wrapping_by_char_and_there_are_no_word_breaks'
|
1061
|
-
|
1062
|
-
pdf.font 'Courier'
|
1063
|
-
text_box = described_class.new(
|
1064
|
-
text,
|
1065
|
-
width: 180,
|
1066
|
-
overflow: :shrink_to_fit,
|
1067
|
-
disable_wrap_by_char: true,
|
1068
|
-
document: pdf
|
1069
|
-
)
|
1070
|
-
expect { text_box.render }.to raise_error(Prawn::Errors::CannotFit)
|
1071
|
-
end
|
1072
|
-
|
1073
|
-
it 'retains full words with :shrink_to_fit if char wrapping is disabled' do
|
1074
|
-
text = 'Wrapped_words'
|
1075
|
-
expect = 'Wrapped_words'
|
1076
|
-
|
1077
|
-
pdf.font 'Courier'
|
1078
|
-
text_box = described_class.new(
|
1079
|
-
text,
|
1080
|
-
width: 50,
|
1081
|
-
height: 50,
|
1082
|
-
size: 50,
|
1083
|
-
overflow: :shrink_to_fit,
|
1084
|
-
disable_wrap_by_char: true,
|
1085
|
-
document: pdf
|
1086
|
-
)
|
1087
|
-
text_box.render
|
1088
|
-
expect(text_box.text).to eq(expect)
|
1089
|
-
end
|
1090
|
-
end
|
1091
|
-
|
1092
|
-
describe 'Text::Box#render with :mode option' do
|
1093
|
-
it 'alters the text rendering mode of the document' do
|
1094
|
-
string = 'hello world'
|
1095
|
-
options = { document: pdf, mode: :fill_stroke }
|
1096
|
-
text_box = described_class.new(string, options)
|
1097
|
-
text_box.render
|
1098
|
-
contents = PDF::Inspector::Text.analyze(pdf.render)
|
1099
|
-
expect(contents.text_rendering_mode).to eq([2, 0])
|
1100
|
-
end
|
1101
|
-
end
|
1102
|
-
|
1103
|
-
def reduce_precision(float)
|
1104
|
-
float.round(5)
|
1105
|
-
end
|
1106
|
-
|
1107
|
-
def first_line(str)
|
1108
|
-
str.lines.first
|
1109
|
-
end
|
1110
|
-
end
|