prawn-core 0.7.2 → 0.8.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/examples/general/background.rb +1 -1
- data/examples/general/measurement_units.rb +2 -2
- data/examples/general/outlines.rb +50 -0
- data/examples/general/repeaters.rb +11 -7
- data/examples/general/stamp.rb +6 -6
- data/examples/graphics/basic_images.rb +1 -1
- data/examples/graphics/curves.rb +1 -1
- data/examples/graphics/rounded_polygons.rb +19 -0
- data/examples/graphics/rounded_rectangle.rb +20 -0
- data/examples/graphics/transformations.rb +52 -0
- data/examples/m17n/win_ansi_charset.rb +1 -1
- data/examples/text/font_calculations.rb +3 -3
- data/examples/text/indent_paragraphs.rb +18 -0
- data/examples/text/kerning.rb +4 -4
- data/examples/text/rotated.rb +98 -0
- data/examples/text/simple_text.rb +3 -3
- data/examples/text/simple_text_ttf.rb +1 -1
- data/lib/prawn/byte_string.rb +1 -0
- data/lib/prawn/core.rb +12 -5
- data/lib/prawn/core/object_store.rb +99 -0
- data/lib/prawn/core/page.rb +96 -0
- data/lib/prawn/core/text.rb +75 -0
- data/lib/prawn/document.rb +71 -78
- data/lib/prawn/document/annotations.rb +2 -2
- data/lib/prawn/document/bounding_box.rb +19 -9
- data/lib/prawn/document/column_box.rb +13 -12
- data/lib/prawn/document/graphics_state.rb +49 -0
- data/lib/prawn/document/internals.rb +5 -40
- data/lib/prawn/document/page_geometry.rb +1 -18
- data/lib/prawn/document/snapshot.rb +12 -7
- data/lib/prawn/errors.rb +18 -0
- data/lib/prawn/font.rb +4 -2
- data/lib/prawn/font/afm.rb +8 -0
- data/lib/prawn/font/dfont.rb +12 -4
- data/lib/prawn/font/ttf.rb +9 -0
- data/lib/prawn/graphics.rb +66 -9
- data/lib/prawn/graphics/color.rb +1 -1
- data/lib/prawn/graphics/transformation.rb +156 -0
- data/lib/prawn/graphics/transparency.rb +3 -7
- data/lib/prawn/images.rb +4 -3
- data/lib/prawn/images/png.rb +2 -2
- data/lib/prawn/outline.rb +278 -0
- data/lib/prawn/pdf_object.rb +5 -3
- data/lib/prawn/repeater.rb +25 -13
- data/lib/prawn/stamp.rb +6 -29
- data/lib/prawn/text.rb +139 -121
- data/lib/prawn/text/box.rb +168 -102
- data/spec/bounding_box_spec.rb +7 -2
- data/spec/document_spec.rb +7 -5
- data/spec/font_spec.rb +9 -1
- data/spec/graphics_spec.rb +229 -0
- data/spec/object_store_spec.rb +5 -5
- data/spec/outline_spec.rb +229 -0
- data/spec/repeater_spec.rb +18 -1
- data/spec/snapshot_spec.rb +7 -7
- data/spec/span_spec.rb +6 -2
- data/spec/spec_helper.rb +7 -3
- data/spec/stamp_spec.rb +13 -0
- data/spec/text_at_spec.rb +119 -0
- data/spec/text_box_spec.rb +257 -4
- data/spec/text_spec.rb +278 -180
- data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +12 -0
- metadata +16 -3
- data/lib/prawn/object_store.rb +0 -92
data/spec/repeater_spec.rb
CHANGED
@@ -64,11 +64,28 @@ describe "Repeaters" do
|
|
64
64
|
doc.expects(:stamp).never
|
65
65
|
repeater(doc, :odd).run(2)
|
66
66
|
end
|
67
|
+
|
68
|
+
it "must not try to run a stamp if dynamic is selected" do
|
69
|
+
doc = sample_document
|
70
|
+
|
71
|
+
doc.expects(:stamp).never
|
72
|
+
(1..10).each { |p| repeater(doc, :all, true){:do_nothing}.run(p) }
|
73
|
+
end
|
74
|
+
|
75
|
+
it "must render the block in context of page when dynamic is selected" do
|
76
|
+
doc = sample_document
|
77
|
+
|
78
|
+
doc.repeat(:all, :dynamic => true) do
|
79
|
+
draw_text page_number, :at => [500, 0]
|
80
|
+
end
|
81
|
+
|
82
|
+
text = PDF::Inspector::Text.analyze(doc.render)
|
83
|
+
assert_equal (1..10).to_a.map{|p| p.to_s}, text.strings
|
84
|
+
end
|
67
85
|
|
68
86
|
def sample_document
|
69
87
|
doc = Prawn::Document.new(:skip_page_creation => true)
|
70
88
|
10.times { |e| doc.start_new_page }
|
71
|
-
|
72
89
|
doc
|
73
90
|
end
|
74
91
|
|
data/spec/snapshot_spec.rb
CHANGED
@@ -69,16 +69,16 @@ describe "Prawn::Document#transaction" do
|
|
69
69
|
# or else old pages will appear in the post-rollback document.
|
70
70
|
it "should restore the pages into the same objects" do
|
71
71
|
Prawn::Document.new do
|
72
|
-
old_page_object_id =
|
73
|
-
old_page_content_id =
|
72
|
+
old_page_object_id = page.dictionary.identifier
|
73
|
+
old_page_content_id = page.content.identifier
|
74
74
|
|
75
75
|
transaction do
|
76
76
|
start_new_page
|
77
77
|
rollback
|
78
78
|
end
|
79
79
|
|
80
|
-
|
81
|
-
|
80
|
+
page.dictionary.identifier.should == old_page_object_id
|
81
|
+
page.content.identifier.should == old_page_content_id
|
82
82
|
end
|
83
83
|
|
84
84
|
end
|
@@ -92,7 +92,7 @@ describe "Prawn::Document#transaction" do
|
|
92
92
|
end
|
93
93
|
|
94
94
|
# should be the exact same object, not a clone
|
95
|
-
|
95
|
+
page.dictionary.data[:Contents].should == page.content
|
96
96
|
end
|
97
97
|
|
98
98
|
end
|
@@ -101,7 +101,7 @@ describe "Prawn::Document#transaction" do
|
|
101
101
|
|
102
102
|
it "should properly commit if no error is raised" do
|
103
103
|
pdf = Prawn::Document.new do
|
104
|
-
create_stamp("test_stamp") {
|
104
|
+
create_stamp("test_stamp") { draw_text "This is shown", :at => [0,0] }
|
105
105
|
transaction do
|
106
106
|
stamp("test_stamp")
|
107
107
|
end
|
@@ -111,7 +111,7 @@ describe "Prawn::Document#transaction" do
|
|
111
111
|
|
112
112
|
it "should properly rollback when #rollback is called" do
|
113
113
|
pdf = Prawn::Document.new do
|
114
|
-
create_stamp("test_stamp") {
|
114
|
+
create_stamp("test_stamp") { draw_text "This is not shown", :at => [0,0] }
|
115
115
|
|
116
116
|
transaction do
|
117
117
|
stamp("test_stamp")
|
data/spec/span_spec.rb
CHANGED
@@ -4,10 +4,14 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
|
|
4
4
|
|
5
5
|
describe "drawing span" do
|
6
6
|
|
7
|
-
|
7
|
+
def setup
|
8
8
|
Prawn.debug = false
|
9
9
|
create_pdf
|
10
|
-
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
Prawn.debug = true
|
14
|
+
end
|
11
15
|
|
12
16
|
it "should only accept :position as option in debug mode" do
|
13
17
|
Prawn.debug = true
|
data/spec/spec_helper.rb
CHANGED
@@ -3,15 +3,19 @@
|
|
3
3
|
puts "Prawn specs: Running on Ruby Version: #{RUBY_VERSION}"
|
4
4
|
|
5
5
|
require "rubygems"
|
6
|
-
require "test/spec"
|
7
|
-
require "mocha"
|
8
6
|
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
9
7
|
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'vendor','pdf-inspector','lib')
|
10
8
|
require "prawn/core"
|
11
9
|
|
12
10
|
Prawn.debug = true
|
13
11
|
|
14
|
-
|
12
|
+
ruby_19 do
|
13
|
+
gem "test-unit", "=1.2.3"
|
14
|
+
end
|
15
|
+
require "test/spec"
|
16
|
+
require "mocha"
|
17
|
+
|
18
|
+
gem 'pdf-reader', ">=0.8"
|
15
19
|
require "pdf/reader"
|
16
20
|
require "pdf/inspector"
|
17
21
|
|
data/spec/stamp_spec.rb
CHANGED
@@ -19,6 +19,19 @@ describe "create_stamp before any page is added" do
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
describe "#stamp_at" do
|
23
|
+
it "should work" do
|
24
|
+
create_pdf
|
25
|
+
@pdf.create_stamp("MyStamp")
|
26
|
+
@pdf.stamp_at("MyStamp", [100, 200])
|
27
|
+
# I had modified PDF::Inspector::XObject to receive the
|
28
|
+
# invoke_xobject message and count the number of times it was
|
29
|
+
# called, but it was only called once, so I reverted checking the
|
30
|
+
# output with a regular expression
|
31
|
+
@pdf.render.should =~ /\/Stamp1 Do.*?/m
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
22
35
|
describe "Document with a stamp" do
|
23
36
|
it "should raise NameTaken error when attempt to create stamp "+
|
24
37
|
"with same name as an existing stamp" do
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
|
4
|
+
|
5
|
+
describe "#draw_text" do
|
6
|
+
before(:each) { create_pdf }
|
7
|
+
|
8
|
+
it "should raise ArgumentError if :at option omitted" do
|
9
|
+
lambda { @pdf.draw_text("hai", { }) }.should.raise(ArgumentError)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should raise ArgumentError if :align option included" do
|
13
|
+
lambda { @pdf.draw_text("hai", :at => [0, 0], :align => :center) }.should.raise(ArgumentError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should default to 12 point helvetica" do
|
17
|
+
@pdf.draw_text("Blah", :at => [100,100])
|
18
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
19
|
+
text.font_settings[0][:name].should == :Helvetica
|
20
|
+
text.font_settings[0][:size].should == 12
|
21
|
+
text.strings.first.should == "Blah"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should allow setting font size" do
|
25
|
+
@pdf.draw_text("Blah", :at => [100,100], :size => 16)
|
26
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
27
|
+
text.font_settings[0][:size].should == 16
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should allow setting a default font size" do
|
31
|
+
@pdf.font_size = 16
|
32
|
+
@pdf.draw_text("Blah", :at => [0, 0])
|
33
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
34
|
+
text.font_settings[0][:size].should == 16
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should allow overriding default font for a single instance" do
|
38
|
+
@pdf.font_size = 16
|
39
|
+
|
40
|
+
@pdf.draw_text("Blah", :size => 11, :at => [0, 0])
|
41
|
+
@pdf.draw_text("Blaz", :at => [0, 0])
|
42
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
43
|
+
text.font_settings[0][:size].should == 11
|
44
|
+
text.font_settings[1][:size].should == 16
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should allow setting a font size transaction with a block" do
|
48
|
+
@pdf.font_size 16 do
|
49
|
+
@pdf.draw_text('Blah', :at => [0, 0])
|
50
|
+
end
|
51
|
+
|
52
|
+
@pdf.draw_text('blah', :at => [0, 0])
|
53
|
+
|
54
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
55
|
+
text.font_settings[0][:size].should == 16
|
56
|
+
text.font_settings[1][:size].should == 12
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should allow manual setting the font size " +
|
60
|
+
"when in a font size block" do
|
61
|
+
@pdf.font_size(16) do
|
62
|
+
@pdf.draw_text('Foo', :at => [0, 0])
|
63
|
+
@pdf.draw_text('Blah', :size => 11, :at => [0, 0])
|
64
|
+
@pdf.draw_text('Blaz', :at => [0, 0])
|
65
|
+
end
|
66
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
67
|
+
text.font_settings[0][:size].should == 16
|
68
|
+
text.font_settings[1][:size].should == 11
|
69
|
+
text.font_settings[2][:size].should == 16
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should allow registering of built-in font_settings on the fly" do
|
73
|
+
@pdf.font "Times-Roman"
|
74
|
+
@pdf.draw_text("Blah", :at => [100,100], :at => [0, 0])
|
75
|
+
@pdf.font "Courier"
|
76
|
+
@pdf.draw_text("Blaz", :at => [150,150], :at => [0, 0])
|
77
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
78
|
+
text.font_settings[0][:name].should == :"Times-Roman"
|
79
|
+
text.font_settings[1][:name].should == :Courier
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should raise an exception when an unknown font is used" do
|
83
|
+
lambda { @pdf.font "Pao bu" }.should.raise(Prawn::Errors::UnknownFont)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should correctly render a utf-8 string when using a built-in font" do
|
87
|
+
str = "©" # copyright symbol
|
88
|
+
@pdf.draw_text(str, :at => [0, 0])
|
89
|
+
|
90
|
+
# grab the text from the rendered PDF and ensure it matches
|
91
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
92
|
+
text.strings.first.should == str
|
93
|
+
end
|
94
|
+
|
95
|
+
if "spec".respond_to?(:encode!)
|
96
|
+
# Handle non utf-8 string encodings in a sane way on M17N aware VMs
|
97
|
+
it "should raise an exception when a utf-8 incompatible string is rendered" do
|
98
|
+
str = "Blah \xDD"
|
99
|
+
str.force_encoding("ASCII-8BIT")
|
100
|
+
lambda { @pdf.draw_text(str, :at => [0, 0]) }.should.raise(ArgumentError)
|
101
|
+
end
|
102
|
+
it "should not raise an exception when a shift-jis string is rendered" do
|
103
|
+
datafile = "#{Prawn::BASEDIR}/data/shift_jis_text.txt"
|
104
|
+
sjis_str = File.open(datafile, "r:shift_jis") { |f| f.gets }
|
105
|
+
@pdf.font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf")
|
106
|
+
lambda { @pdf.draw_text(sjis_str, :at => [0, 0]) }.should.not.raise(ArgumentError)
|
107
|
+
end
|
108
|
+
else
|
109
|
+
# Handle non utf-8 string encodings in a sane way on non-M17N aware VMs
|
110
|
+
it "should raise an exception when a corrupt utf-8 string is rendered" do
|
111
|
+
str = "Blah \xDD"
|
112
|
+
lambda { @pdf.draw_text(str, :at => [0, 0]) }.should.raise(ArgumentError)
|
113
|
+
end
|
114
|
+
it "should raise an exception when a shift-jis string is rendered" do
|
115
|
+
sjis_str = File.read("#{Prawn::BASEDIR}/data/shift_jis_text.txt")
|
116
|
+
lambda { @pdf.draw_text(sjis_str, :at => [0, 0]) }.should.raise(ArgumentError)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/spec/text_box_spec.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
|
4
4
|
|
5
5
|
|
6
|
-
describe "Text::Box" do
|
6
|
+
describe "Text::Box#render" do
|
7
7
|
it "should not fail if height is smaller than 1 line" do
|
8
8
|
create_pdf
|
9
9
|
@text = "Oh hai text rect. " * 10
|
@@ -15,9 +15,6 @@ describe "Text::Box" do
|
|
15
15
|
text_box.render
|
16
16
|
text_box.text.should == ""
|
17
17
|
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe "Text::Box#render" do
|
21
18
|
it "should draw content to the page" do
|
22
19
|
create_pdf
|
23
20
|
@text = "Oh hai text rect. " * 10
|
@@ -27,6 +24,28 @@ describe "Text::Box#render" do
|
|
27
24
|
text = PDF::Inspector::Text.analyze(@pdf.render)
|
28
25
|
text.strings.should.not.be.empty
|
29
26
|
end
|
27
|
+
it "should not draw a transformation matrix" do
|
28
|
+
create_pdf
|
29
|
+
@text = "Oh hai text rect. " * 10
|
30
|
+
@options = { :document => @pdf }
|
31
|
+
text_box = Prawn::Text::Box.new(@text, @options)
|
32
|
+
text_box.render()
|
33
|
+
matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
|
34
|
+
matrices.matrices.length.should == 0
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "Text::Box#render(:single_line => true)" do
|
39
|
+
it "should draw only one line to the page" do
|
40
|
+
create_pdf
|
41
|
+
@text = "Oh hai text rect. " * 10
|
42
|
+
@options = { :document => @pdf,
|
43
|
+
:single_line => true }
|
44
|
+
text_box = Prawn::Text::Box.new(@text, @options)
|
45
|
+
text_box.render()
|
46
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
47
|
+
text.strings.length.should == 1
|
48
|
+
end
|
30
49
|
end
|
31
50
|
|
32
51
|
describe "Text::Box#render(:dry_run => true)" do
|
@@ -39,6 +58,170 @@ describe "Text::Box#render(:dry_run => true)" do
|
|
39
58
|
text = PDF::Inspector::Text.analyze(@pdf.render)
|
40
59
|
text.strings.should.be.empty
|
41
60
|
end
|
61
|
+
it "subsequent calls to render should not raise an ArgumentError exception" do
|
62
|
+
create_pdf
|
63
|
+
@text = "™©"
|
64
|
+
@options = { :document => @pdf }
|
65
|
+
text_box = Prawn::Text::Box.new(@text, @options)
|
66
|
+
text_box.render(:dry_run => true)
|
67
|
+
lambda { text_box.render }.should.not.raise(ArgumentError)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "Text::Box#render with :rotate option of 30)" do
|
72
|
+
before(:each) do
|
73
|
+
create_pdf
|
74
|
+
rotate = 30
|
75
|
+
@x = 300
|
76
|
+
@y = 70
|
77
|
+
@width = 100
|
78
|
+
@height = 50
|
79
|
+
@cos = Math.cos(rotate * Math::PI / 180)
|
80
|
+
@sin = Math.sin(rotate * Math::PI / 180)
|
81
|
+
@text = "Oh hai text rect. " * 10
|
82
|
+
@options = { :document => @pdf,
|
83
|
+
:rotate => rotate,
|
84
|
+
:at => [@x, @y],
|
85
|
+
:width => @width,
|
86
|
+
:height => @height }
|
87
|
+
end
|
88
|
+
context ":rotate_around option of :center" do
|
89
|
+
it "should draw content to the page rotated about the center of the text" do
|
90
|
+
@options[:rotate_around] = :center
|
91
|
+
text_box = Prawn::Text::Box.new(@text, @options)
|
92
|
+
text_box.render()
|
93
|
+
|
94
|
+
matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
|
95
|
+
x = @x + @width / 2
|
96
|
+
y = @y - @height / 2
|
97
|
+
x_prime = x * @cos - y * @sin
|
98
|
+
y_prime = x * @sin + y * @cos
|
99
|
+
matrices.matrices[0].should == [1, 0, 0, 1,
|
100
|
+
reduce_precision(x - x_prime),
|
101
|
+
reduce_precision(y - y_prime)]
|
102
|
+
matrices.matrices[1].should == [reduce_precision(@cos),
|
103
|
+
reduce_precision(@sin),
|
104
|
+
reduce_precision(-@sin),
|
105
|
+
reduce_precision(@cos), 0, 0]
|
106
|
+
|
107
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
108
|
+
text.strings.should.not.be.empty
|
109
|
+
end
|
110
|
+
end
|
111
|
+
context ":rotate_around option of :upper_left" do
|
112
|
+
it "should draw content to the page rotated about the upper left corner of the text" do
|
113
|
+
@options[:rotate_around] = :upper_left
|
114
|
+
text_box = Prawn::Text::Box.new(@text, @options)
|
115
|
+
text_box.render()
|
116
|
+
|
117
|
+
matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
|
118
|
+
x = @x
|
119
|
+
y = @y
|
120
|
+
x_prime = x * @cos - y * @sin
|
121
|
+
y_prime = x * @sin + y * @cos
|
122
|
+
matrices.matrices[0].should == [1, 0, 0, 1,
|
123
|
+
reduce_precision(x - x_prime),
|
124
|
+
reduce_precision(y - y_prime)]
|
125
|
+
matrices.matrices[1].should == [reduce_precision(@cos),
|
126
|
+
reduce_precision(@sin),
|
127
|
+
reduce_precision(-@sin),
|
128
|
+
reduce_precision(@cos), 0, 0]
|
129
|
+
|
130
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
131
|
+
text.strings.should.not.be.empty
|
132
|
+
end
|
133
|
+
end
|
134
|
+
context "default :rotate_around" do
|
135
|
+
it "should draw content to the page rotated about the upper left corner of the text" do
|
136
|
+
text_box = Prawn::Text::Box.new(@text, @options)
|
137
|
+
text_box.render()
|
138
|
+
|
139
|
+
matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
|
140
|
+
x = @x
|
141
|
+
y = @y
|
142
|
+
x_prime = x * @cos - y * @sin
|
143
|
+
y_prime = x * @sin + y * @cos
|
144
|
+
matrices.matrices[0].should == [1, 0, 0, 1,
|
145
|
+
reduce_precision(x - x_prime),
|
146
|
+
reduce_precision(y - y_prime)]
|
147
|
+
matrices.matrices[1].should == [reduce_precision(@cos),
|
148
|
+
reduce_precision(@sin),
|
149
|
+
reduce_precision(-@sin),
|
150
|
+
reduce_precision(@cos), 0, 0]
|
151
|
+
|
152
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
153
|
+
text.strings.should.not.be.empty
|
154
|
+
end
|
155
|
+
end
|
156
|
+
context ":rotate_around option of :upper_right" do
|
157
|
+
it "should draw content to the page rotated about the upper right corner of the text" do
|
158
|
+
@options[:rotate_around] = :upper_right
|
159
|
+
text_box = Prawn::Text::Box.new(@text, @options)
|
160
|
+
text_box.render()
|
161
|
+
|
162
|
+
matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
|
163
|
+
x = @x + @width
|
164
|
+
y = @y
|
165
|
+
x_prime = x * @cos - y * @sin
|
166
|
+
y_prime = x * @sin + y * @cos
|
167
|
+
matrices.matrices[0].should == [1, 0, 0, 1,
|
168
|
+
reduce_precision(x - x_prime),
|
169
|
+
reduce_precision(y - y_prime)]
|
170
|
+
matrices.matrices[1].should == [reduce_precision(@cos),
|
171
|
+
reduce_precision(@sin),
|
172
|
+
reduce_precision(-@sin),
|
173
|
+
reduce_precision(@cos), 0, 0]
|
174
|
+
|
175
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
176
|
+
text.strings.should.not.be.empty
|
177
|
+
end
|
178
|
+
end
|
179
|
+
context ":rotate_around option of :lower_right" do
|
180
|
+
it "should draw content to the page rotated about the lower right corner of the text" do
|
181
|
+
@options[:rotate_around] = :lower_right
|
182
|
+
text_box = Prawn::Text::Box.new(@text, @options)
|
183
|
+
text_box.render()
|
184
|
+
|
185
|
+
matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
|
186
|
+
x = @x + @width
|
187
|
+
y = @y - @height
|
188
|
+
x_prime = x * @cos - y * @sin
|
189
|
+
y_prime = x * @sin + y * @cos
|
190
|
+
matrices.matrices[0].should == [1, 0, 0, 1,
|
191
|
+
reduce_precision(x - x_prime),
|
192
|
+
reduce_precision(y - y_prime)]
|
193
|
+
matrices.matrices[1].should == [reduce_precision(@cos),
|
194
|
+
reduce_precision(@sin),
|
195
|
+
reduce_precision(-@sin),
|
196
|
+
reduce_precision(@cos), 0, 0]
|
197
|
+
|
198
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
199
|
+
text.strings.should.not.be.empty
|
200
|
+
end
|
201
|
+
end
|
202
|
+
context ":rotate_around option of :lower_left" do
|
203
|
+
it "should draw content to the page rotated about the lower left corner of the text" do
|
204
|
+
@options[:rotate_around] = :lower_left
|
205
|
+
text_box = Prawn::Text::Box.new(@text, @options)
|
206
|
+
text_box.render()
|
207
|
+
|
208
|
+
matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
|
209
|
+
x = @x
|
210
|
+
y = @y - @height
|
211
|
+
x_prime = x * @cos - y * @sin
|
212
|
+
y_prime = x * @sin + y * @cos
|
213
|
+
matrices.matrices[0].should == [1, 0, 0, 1,
|
214
|
+
reduce_precision(x - x_prime),
|
215
|
+
reduce_precision(y - y_prime)]
|
216
|
+
matrices.matrices[1].should == [reduce_precision(@cos),
|
217
|
+
reduce_precision(@sin),
|
218
|
+
reduce_precision(-@sin),
|
219
|
+
reduce_precision(@cos), 0, 0]
|
220
|
+
|
221
|
+
text = PDF::Inspector::Text.analyze(@pdf.render)
|
222
|
+
text.strings.should.not.be.empty
|
223
|
+
end
|
224
|
+
end
|
42
225
|
end
|
43
226
|
|
44
227
|
describe "Text::Box default height" do
|
@@ -112,6 +295,56 @@ describe "Text::Box with text than can fit in the box with :ellipses overflow an
|
|
112
295
|
end
|
113
296
|
end
|
114
297
|
|
298
|
+
|
299
|
+
|
300
|
+
describe "Text::Box printing UTF-8 string with higher bit characters" do
|
301
|
+
before(:each) do
|
302
|
+
create_pdf
|
303
|
+
@text = "©"
|
304
|
+
# not enough height to print any text, so we can directly compare against
|
305
|
+
# the input string
|
306
|
+
bounding_height = 1.0
|
307
|
+
options = {
|
308
|
+
:height => bounding_height,
|
309
|
+
:document => @pdf
|
310
|
+
}
|
311
|
+
@text_box = Prawn::Text::Box.new(@text, options)
|
312
|
+
end
|
313
|
+
describe "when using a TTF font" do
|
314
|
+
it "unprinted text should be in UTF-8 encoding" do
|
315
|
+
@pdf.font("#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf")
|
316
|
+
remaining_text = @text_box.render
|
317
|
+
remaining_text.should == @text
|
318
|
+
end
|
319
|
+
it "subsequent calls to Text::Box need not include the" +
|
320
|
+
" :skip_encoding => true option" do
|
321
|
+
@pdf.font("#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf")
|
322
|
+
remaining_text = @text_box.render
|
323
|
+
lambda {
|
324
|
+
@pdf.text_box(remaining_text, :document => @pdf)
|
325
|
+
}.should.not.raise(ArgumentError)
|
326
|
+
end
|
327
|
+
end
|
328
|
+
describe "when using an AFM font" do
|
329
|
+
it "unprinted text should be in WinAnsi encoding" do
|
330
|
+
remaining_text = @text_box.render
|
331
|
+
remaining_text.should == @pdf.font.normalize_encoding(@text)
|
332
|
+
end
|
333
|
+
it "subsequent calls to Text::Box must include the" +
|
334
|
+
" :skip_encoding => true option" do
|
335
|
+
remaining_text = @text_box.render
|
336
|
+
lambda {
|
337
|
+
@pdf.text_box(remaining_text, :document => @pdf)
|
338
|
+
}.should.raise(ArgumentError)
|
339
|
+
lambda {
|
340
|
+
@pdf.text_box(remaining_text, :skip_encoding => true,
|
341
|
+
:document => @pdf)
|
342
|
+
}.should.not.raise(ArgumentError)
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
|
115
348
|
describe "Text::Box with more text than can fit in the box" do
|
116
349
|
before(:each) do
|
117
350
|
create_pdf
|
@@ -148,6 +381,22 @@ describe "Text::Box with more text than can fit in the box" do
|
|
148
381
|
@text_box.render
|
149
382
|
@bounding_height.should.be.close(@text_box.height, @pdf.font.height)
|
150
383
|
end
|
384
|
+
context "with :rotate option" do
|
385
|
+
it "unrendered text should be the same as when not rotated" do
|
386
|
+
remaining_text = @text_box.render
|
387
|
+
|
388
|
+
rotate = 30
|
389
|
+
x = 300
|
390
|
+
y = 70
|
391
|
+
width = @options[:width]
|
392
|
+
height = @options[:height]
|
393
|
+
@options[:document] = @pdf
|
394
|
+
@options[:rotate] = rotate
|
395
|
+
@options[:at] = [x, y]
|
396
|
+
rotated_text_box = Prawn::Text::Box.new(@text, @options)
|
397
|
+
rotated_text_box.render.should == remaining_text
|
398
|
+
end
|
399
|
+
end
|
151
400
|
end
|
152
401
|
|
153
402
|
context "ellipses overflow" do
|
@@ -348,3 +597,7 @@ describe 'Text::Box wrapping' do
|
|
348
597
|
end
|
349
598
|
|
350
599
|
end
|
600
|
+
|
601
|
+
def reduce_precision(float)
|
602
|
+
("%.5f" % float).to_f
|
603
|
+
end
|