spec/prawn/text/box_spec.rb in prawn-2.3.0 vs spec/prawn/text/box_spec.rb in prawn-2.4.0
- old
+ new
@@ -181,22 +181,24 @@
it 'hits the callback once per fragment for :inline_format' do
draw_block = instance_spy('Draw block')
pdf.text_box 'this text has <b>fancy</b> formatting',
- inline_format: true, width: 500,
+ inline_format: true,
+ width: 500,
draw_text_callback: ->(text, _) { draw_block.kick(text) }
expect(draw_block).to have_received(:kick).with('this text has ')
expect(draw_block).to have_received(:kick).with('fancy')
expect(draw_block).to have_received(:kick).with(' formatting')
end
it 'does not call #draw_text!' do
allow(pdf).to receive(:draw_text!)
- pdf.text_box 'some text', width: 500,
- draw_text_callback: ->(_, _) {}
+ pdf.text_box 'some text',
+ width: 500,
+ draw_text_callback: ->(_, _) {}
expect(pdf).to_not have_received(:draw_text!)
end
end
describe '#valid_options' do
@@ -308,43 +310,38 @@
expect(text_box.at).to eq(original_at)
end
end
describe '#render with :rotate option of 30)' do
- let(:angle) { 30 }
- let(:x) { 300 }
- let(:y) { 70 }
- let(:width) { 100 }
- let(:height) { 50 }
- let(:cos) { Math.cos(angle * Math::PI / 180) }
- let(:sin) { Math.sin(angle * Math::PI / 180) }
+ let(:cos) { Math.cos(30 * Math::PI / 180) }
+ let(:sin) { Math.sin(30 * Math::PI / 180) }
let(:text) { 'Oh hai text rect. ' * 10 }
let(:options) do
{
document: pdf,
- rotate: angle,
- at: [x, y],
- width: width,
- height: height
+ rotate: 30,
+ at: [300, 70],
+ width: 100,
+ height: 50
}
end
context 'with :rotate_around option of :center' do
it 'draws content to the page rotated about the center of the text' do
options[:rotate_around] = :center
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
- x_ = x + width / 2
- y_ = y - height / 2
- x_prime = x_ * cos - y_ * sin
- y_prime = x_ * sin + y_ * cos
+ x = 350
+ y = 45
+ x_prime = x * cos - y * sin
+ y_prime = x * sin + y * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
- reduce_precision(x_ - x_prime),
- reduce_precision(y_ - y_prime)
+ reduce_precision(x - x_prime),
+ reduce_precision(y - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
@@ -363,16 +360,16 @@
options[:rotate_around] = :upper_left
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
- x_prime = x * cos - y * sin
- y_prime = x * sin + y * cos
+ x_prime = 300 * cos - 70 * sin
+ y_prime = 300 * sin + 70 * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
- reduce_precision(x - x_prime),
- reduce_precision(y - y_prime)
+ reduce_precision(300 - x_prime),
+ reduce_precision(70 - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
@@ -390,16 +387,16 @@
'the text' do
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
- x_prime = x * cos - y * sin
- y_prime = x * sin + y * cos
+ x_prime = 300 * cos - 70 * sin
+ y_prime = 300 * sin + 70 * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
- reduce_precision(x - x_prime),
- reduce_precision(y - y_prime)
+ reduce_precision(300 - x_prime),
+ reduce_precision(70 - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
@@ -418,18 +415,18 @@
options[:rotate_around] = :upper_right
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
- x_ = x + width
- y_ = y
- x_prime = x_ * cos - y_ * sin
- y_prime = x_ * sin + y_ * cos
+ x = 400
+ y = 70
+ x_prime = x * cos - y * sin
+ y_prime = x * sin + y * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
- reduce_precision(x_ - x_prime),
- reduce_precision(y_ - y_prime)
+ reduce_precision(x - x_prime),
+ reduce_precision(y - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
@@ -448,18 +445,18 @@
options[:rotate_around] = :lower_right
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
- x_ = x + width
- y_ = y - height
- x_prime = x_ * cos - y_ * sin
- y_prime = x_ * sin + y_ * cos
+ x = 400
+ y = 20
+ x_prime = x * cos - y * sin
+ y_prime = x * sin + y * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
- reduce_precision(x_ - x_prime),
- reduce_precision(y_ - y_prime)
+ reduce_precision(x - x_prime),
+ reduce_precision(y - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
@@ -478,18 +475,18 @@
options[:rotate_around] = :lower_left
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
- x_ = x
- y_ = y - height
- x_prime = x_ * cos - y_ * sin
- y_prime = x_ * sin + y_ * cos
+ x = 300
+ y = 20
+ x_prime = x * cos - y * sin
+ y_prime = x * sin + y * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
- reduce_precision(x_ - x_prime),
- reduce_precision(y_ - y_prime)
+ reduce_precision(x - x_prime),
+ reduce_precision(y - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
@@ -576,11 +573,11 @@
end
it 'printed text should match requested text, except that preceding and ' \
'trailing white space will be stripped from each line, and newlines ' \
'may be inserted' do
- text_box = described_class.new(' ' + text, options)
+ text_box = described_class.new(" #{text}", options)
text_box.render
expect(text_box.text.tr("\n", ' ')).to eq(text.strip)
end
it 'render returns an empty string because no text remains unprinted' do
@@ -744,15 +741,13 @@
context 'with :rotate option' do
it 'unrendered text should be the same as when not rotated' do
remaining_text = text_box.render
rotate = 30
- x = 300
- y = 70
options[:document] = pdf
options[:rotate] = rotate
- options[:at] = [x, y]
+ options[:at] = [300, 70]
rotated_text_box = described_class.new(text, options)
expect(rotated_text_box.render).to eq(remaining_text)
end
end
end
@@ -1021,36 +1016,39 @@
'wrapping text' do
text = '©' * 30
pdf.font 'Courier'
text_box = described_class.new(
- text, width: 180,
- overflow: :expand,
- document: pdf
+ text,
+ width: 180,
+ overflow: :expand,
+ document: pdf
)
text_box.render
- expected = +'©' * 25 + "\n" + '©' * 5
+ expected = "#{'©' * 25}\n#{'©' * 5}"
expected = pdf.font.normalize_encoding(expected)
expected = expected.force_encoding(Encoding::UTF_8)
expect(text_box.text).to eq(expected)
end
it 'wraps non-unicode strings using single-byte word-wrapping' do
text = 'continúa esforzandote ' * 5
text_box = described_class.new(
- text, width: 180,
- document: pdf
+ text,
+ width: 180,
+ document: pdf
)
text_box.render
results_with_accent = text_box.text
text = 'continua esforzandote ' * 5
text_box = described_class.new(
- text, width: 180,
- document: pdf
+ text,
+ width: 180,
+ document: pdf
)
text_box.render
results_without_accent = text_box.text
expect(first_line(results_with_accent).length)
@@ -1105,8 +1103,8 @@
def reduce_precision(float)
float.round(5)
end
def first_line(str)
- str.each_line { |line| return line }
+ str.lines.first
end
end