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.
Files changed (65) hide show
  1. data/Rakefile +1 -1
  2. data/examples/general/background.rb +1 -1
  3. data/examples/general/measurement_units.rb +2 -2
  4. data/examples/general/outlines.rb +50 -0
  5. data/examples/general/repeaters.rb +11 -7
  6. data/examples/general/stamp.rb +6 -6
  7. data/examples/graphics/basic_images.rb +1 -1
  8. data/examples/graphics/curves.rb +1 -1
  9. data/examples/graphics/rounded_polygons.rb +19 -0
  10. data/examples/graphics/rounded_rectangle.rb +20 -0
  11. data/examples/graphics/transformations.rb +52 -0
  12. data/examples/m17n/win_ansi_charset.rb +1 -1
  13. data/examples/text/font_calculations.rb +3 -3
  14. data/examples/text/indent_paragraphs.rb +18 -0
  15. data/examples/text/kerning.rb +4 -4
  16. data/examples/text/rotated.rb +98 -0
  17. data/examples/text/simple_text.rb +3 -3
  18. data/examples/text/simple_text_ttf.rb +1 -1
  19. data/lib/prawn/byte_string.rb +1 -0
  20. data/lib/prawn/core.rb +12 -5
  21. data/lib/prawn/core/object_store.rb +99 -0
  22. data/lib/prawn/core/page.rb +96 -0
  23. data/lib/prawn/core/text.rb +75 -0
  24. data/lib/prawn/document.rb +71 -78
  25. data/lib/prawn/document/annotations.rb +2 -2
  26. data/lib/prawn/document/bounding_box.rb +19 -9
  27. data/lib/prawn/document/column_box.rb +13 -12
  28. data/lib/prawn/document/graphics_state.rb +49 -0
  29. data/lib/prawn/document/internals.rb +5 -40
  30. data/lib/prawn/document/page_geometry.rb +1 -18
  31. data/lib/prawn/document/snapshot.rb +12 -7
  32. data/lib/prawn/errors.rb +18 -0
  33. data/lib/prawn/font.rb +4 -2
  34. data/lib/prawn/font/afm.rb +8 -0
  35. data/lib/prawn/font/dfont.rb +12 -4
  36. data/lib/prawn/font/ttf.rb +9 -0
  37. data/lib/prawn/graphics.rb +66 -9
  38. data/lib/prawn/graphics/color.rb +1 -1
  39. data/lib/prawn/graphics/transformation.rb +156 -0
  40. data/lib/prawn/graphics/transparency.rb +3 -7
  41. data/lib/prawn/images.rb +4 -3
  42. data/lib/prawn/images/png.rb +2 -2
  43. data/lib/prawn/outline.rb +278 -0
  44. data/lib/prawn/pdf_object.rb +5 -3
  45. data/lib/prawn/repeater.rb +25 -13
  46. data/lib/prawn/stamp.rb +6 -29
  47. data/lib/prawn/text.rb +139 -121
  48. data/lib/prawn/text/box.rb +168 -102
  49. data/spec/bounding_box_spec.rb +7 -2
  50. data/spec/document_spec.rb +7 -5
  51. data/spec/font_spec.rb +9 -1
  52. data/spec/graphics_spec.rb +229 -0
  53. data/spec/object_store_spec.rb +5 -5
  54. data/spec/outline_spec.rb +229 -0
  55. data/spec/repeater_spec.rb +18 -1
  56. data/spec/snapshot_spec.rb +7 -7
  57. data/spec/span_spec.rb +6 -2
  58. data/spec/spec_helper.rb +7 -3
  59. data/spec/stamp_spec.rb +13 -0
  60. data/spec/text_at_spec.rb +119 -0
  61. data/spec/text_box_spec.rb +257 -4
  62. data/spec/text_spec.rb +278 -180
  63. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +12 -0
  64. metadata +16 -3
  65. data/lib/prawn/object_store.rb +0 -92
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rake/testtask'
4
4
  require "rake/rdoctask"
5
5
  require "rake/gempackagetask"
6
6
 
7
- PRAWN_VERSION = "0.7.2"
7
+ PRAWN_VERSION = "0.8.4"
8
8
 
9
9
  task :default => [:test]
10
10
 
@@ -8,7 +8,7 @@ require "#{File.dirname(__FILE__)}/../example_helper.rb"
8
8
 
9
9
  img = "#{Prawn::BASEDIR}/data/images/letterhead.jpg"
10
10
 
11
- Prawn::Document.generate("background.pdf", :background => img) do
11
+ Prawn::Document.generate("background.pdf", :background => img, :margin => 100) do
12
12
  text "My report caption", :size => 18, :align => :right
13
13
 
14
14
  move_down font.height * 2
@@ -34,13 +34,13 @@ units.each_with_index do |unit, unit_index| #iterate through all units that make
34
34
  temp << "1#{unit} => #{one_unit_in_pt}pt\n" #puts converted unit in points
35
35
 
36
36
  offset = offset_multiplier * unit_index
37
- pdf.text units[unit_index], :at => [offset + 0.5.mm, pdf.bounds.top - 2.mm]
37
+ pdf.draw_text units[unit_index], :at => [offset + 0.5.mm, pdf.bounds.top - 2.mm]
38
38
 
39
39
  pdf.stroke_line(offset, pdf.bounds.top, offset, pdf.bounds.bottom)
40
40
 
41
41
  0.upto(((pdf.bounds.height - 5.mm) / one_unit_in_pt).to_i) do |i| # checks, how many strokes can be drawn
42
42
  pdf.stroke_line(offset, i * one_unit_in_pt, (i % 5 == 0 ? 6.mm : 3.mm) + offset, i * one_unit_in_pt) # every fifth stroke is twice as large like on a real ruler
43
- pdf.text "#{i}#{unit}", :at => [7.mm + offset, i * one_unit_in_pt] unless unit == "mm" && i % 5 != 0 || unit == "pt" && i % 10 != 0 # avoid text too close to each other
43
+ pdf.draw_text "#{i}#{unit}", :at => [7.mm + offset, i * one_unit_in_pt] unless unit == "mm" && i % 5 != 0 || unit == "pt" && i % 10 != 0 # avoid text too close to each other
44
44
  end
45
45
  end
46
46
 
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This example demonstrates the use of the the outlines option for a new document
4
+ # it sets an initial outline item with a title
5
+ #
6
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
7
+
8
+ Prawn::Document.generate('outlines.pdf') do
9
+ text "Page 1. This is the first Chapter. "
10
+ start_new_page
11
+ text "Page 2. More in the first Chapter. "
12
+ start_new_page
13
+ text "Page 3. This is the second Chapter. It has a subsection. "
14
+ start_new_page
15
+ text "Page 4. More in the second Chapter. "
16
+ define_outline do
17
+ section 'Chapter 1', :page => 1, :closed => true do
18
+ page 1, :title => 'Page 1'
19
+ page 2, :title => 'Page 2'
20
+ end
21
+ section 'Chapter 2', :page => 3 do
22
+ section 'Chapter 2 Subsection' do
23
+ page nil, :title => 'Page 3'
24
+ end
25
+ page 4, :title => 'Page 4'
26
+ end
27
+ end
28
+ start_new_page
29
+ text "Page 5. Appendix"
30
+ start_new_page
31
+ text "Page 6. More in the Appendix"
32
+ outline.add_section do
33
+ section 'Appendix', :page => 5 do
34
+ page 5, :title => 'Page 5'
35
+ page 6, :title => 'Page 6'
36
+ end
37
+ end
38
+ go_to_page 4
39
+ start_new_page
40
+ text "inserted before the Appendix"
41
+ outline.insert_section_after 'Chapter 2' do
42
+ page page_number, :title => "Pre-Appendix"
43
+ end
44
+ go_to_page 7
45
+ start_new_page
46
+ text "One last page"
47
+ outline.insert_section_after 'Page 6' do
48
+ page page_number, :title => "Inserted after 6"
49
+ end
50
+ end
@@ -10,32 +10,36 @@ require "#{File.dirname(__FILE__)}/../example_helper.rb"
10
10
  Prawn::Document.generate("repeat.pdf", :skip_page_creation => true) do
11
11
 
12
12
  repeat :all do
13
- text "ALLLLLL", :at => bounds.top_left
13
+ draw_text "ALLLLLL", :at => bounds.top_left
14
14
  end
15
15
 
16
16
  repeat :odd do
17
- text "ODD", :at => [0,0]
17
+ draw_text "ODD", :at => [0,0]
18
18
  end
19
19
 
20
20
  repeat :even do
21
- text "EVEN", :at => [0,0]
21
+ draw_text "EVEN", :at => [0,0]
22
22
  end
23
23
 
24
24
  repeat [1,2] do
25
- text "[1,2]", :at => [100,0]
25
+ draw_text "[1,2]", :at => [100,0]
26
26
  end
27
27
 
28
28
  repeat 2..4 do
29
- text "2..4", :at => [200,0]
29
+ draw_text "2..4", :at => [200,0]
30
30
  end
31
31
 
32
32
  repeat(lambda { |pg| pg % 3 == 0 }) do
33
- text "Every third", :at => [250, 20]
33
+ draw_text "Every third", :at => [250, 20]
34
+ end
35
+
36
+ repeat(:all, :dynamic => true) do
37
+ draw_text page_number, :at => [500, 0]
34
38
  end
35
39
 
36
40
  10.times do
37
41
  start_new_page
38
- text "A wonderful page", :at => [400,400]
42
+ draw_text "A wonderful page", :at => [400,400]
39
43
  end
40
44
 
41
45
  end
@@ -7,15 +7,15 @@ require "#{File.dirname(__FILE__)}/../example_helper.rb"
7
7
  Prawn::Document.generate("stamp.pdf", :skip_page_creation => true) do
8
8
 
9
9
  create_stamp("odd_page_template") do
10
- text "This is the odd page template",
11
- :at => [0, bounds.top - font.height]
12
- text "This is also in the odd page template", :at => [0, 0]
10
+ draw_text "This is the odd page template",
11
+ :at => [0, bounds.top - font.height]
12
+ draw_text "This is also in the odd page template", :at => [0, 0]
13
13
  end
14
14
 
15
15
  create_stamp("even_page_template") do
16
- text "This is the even page template",
17
- :at => [0, bounds.top - font.height]
18
- text "This is also in the even page template", :at => [0, 0]
16
+ draw_text "This is the even page template",
17
+ :at => [0, bounds.top - font.height]
18
+ draw_text "This is also in the even page template", :at => [0, 0]
19
19
  end
20
20
 
21
21
  start_new_page
@@ -13,7 +13,7 @@ Prawn::Document.generate("basic_images.pdf", :page_layout => :landscape) do
13
13
  stef = "#{Prawn::BASEDIR}/data/images/stef.jpg"
14
14
  image stef, :at => [500, 400], :width => 200, :height => 200
15
15
 
16
- text "Please enjoy the pigs", :size => 36, :at => [200,15]
16
+ draw_text "Please enjoy the pigs", :size => 36, :at => [200,15]
17
17
 
18
18
  ruport = "#{Prawn::BASEDIR}/data/images/ruport.png"
19
19
  image ruport, :at => [400,200], :width => 150
@@ -6,6 +6,6 @@ require "#{File.dirname(__FILE__)}/../example_helper.rb"
6
6
 
7
7
  pdf = Prawn::Document.new
8
8
  pdf.move_to [100,100]
9
- pdf.stroke_curve_to [50,50], :bounds => [[20,90], [90,90]]
9
+ pdf.stroke_curve_to [50,50], :bounds => [[60,90], [60, 90]]
10
10
  pdf.fill_circle_at [200,200], :radius => 10
11
11
  pdf.render_file "curves.pdf"
@@ -0,0 +1,19 @@
1
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
2
+
3
+ def radian(degree)
4
+ Math::PI/180*degree
5
+ end
6
+
7
+ def point_on_circle(center, radius, degrees)
8
+ [center[0] + radius*(Math.cos(radian(degrees))), center[1] - radius*(Math.sin(radian(degrees)))]
9
+ end
10
+
11
+ pdf = Prawn::Document.new
12
+
13
+ pentagon_points = (0..4).map{|i| point_on_circle([200, 400], 100, i * 72)}
14
+ pentagram_points = [0, 2, 4, 1, 3].map{|i| pentagon_points[i]}
15
+ pdf.stroke_rounded_polygon(20, *pentagram_points)
16
+ pdf.fill_and_stroke_rounded_polygon(10, [100, 250], [200, 300], [300, 250],
17
+ [300, 150], [200, 100], [100, 150])
18
+
19
+ pdf.render_file "rounded_polygon.pdf"
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Rounded rectangle example demonstrating both stroke and stroke and fill.
4
+ # A rectangle with rounded join_style is added just for comparison.
5
+ #
6
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
7
+
8
+ pdf = Prawn::Document.new
9
+ pdf.font_size 8
10
+ pdf.draw_text "a stroked rounded rectangle:", :at => [30, 575]
11
+ pdf.stroke_rounded_rectangle([50, 550], 50, 100, 10)
12
+ pdf.draw_text "a stroked and filled rounded rectangle:", :at => [180, 575]
13
+ pdf.fill_and_stroke_rounded_rectangle([200, 550], 50, 100, 10)
14
+ pdf.draw_text "a regular rectangle with rounded join style;", :at => [330, 575]
15
+ pdf.draw_text "needs thick line width for similar result:", :at => [330, 565]
16
+ pdf.join_style :round
17
+ pdf.line_width 10
18
+ pdf.stroke_rectangle([350, 550], 50, 100)
19
+
20
+ pdf.render_file "rounded_rectangle.pdf"
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Demonstrates transformations
4
+ #
5
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
6
+
7
+ Prawn::Document.generate "transformations.pdf" do |pdf|
8
+ width = 50
9
+ height = 100
10
+
11
+ # ROTATION
12
+ x = 50
13
+ y = pdf.bounds.top - 50
14
+
15
+ pdf.stroke_rectangle([x, y], width, height)
16
+ pdf.draw_text("reference rectangle", :at => [x + width, y - height])
17
+ pdf.rotate(30, :origin => [x, y]) do
18
+ pdf.stroke_rectangle([x, y], width, height)
19
+ pdf.draw_text("rectangle rotated around upper-left corner", :at => [x + width, y - height])
20
+ end
21
+
22
+ x = 50
23
+ y = pdf.bounds.top - 200
24
+
25
+ pdf.stroke_rectangle([x, y], width, height)
26
+ pdf.draw_text("reference rectangle", :at => [x + width, y - height])
27
+ pdf.rotate(30, :origin => [x + width / 2, y - height / 2]) do
28
+ pdf.stroke_rectangle([x, y], width, height)
29
+ pdf.draw_text("rectangle rotated around center", :at => [x + width, y - height])
30
+ end
31
+
32
+ # SCALE
33
+ x = 0
34
+ y = pdf.bounds.top - 500
35
+
36
+ pdf.stroke_rectangle([x, y], width, height)
37
+ pdf.draw_text("reference rectangle", :at => [x + width, y - height])
38
+ pdf.scale(2, :origin => [x, y]) do
39
+ pdf.stroke_rectangle([x, y], width, height)
40
+ pdf.draw_text("rectangle scaled from upper-left corner", :at => [x + width, y - height])
41
+ end
42
+
43
+ x = 150
44
+ y = pdf.bounds.top - 400
45
+
46
+ pdf.stroke_rectangle([x, y], width, height)
47
+ pdf.draw_text("reference rectangle", :at => [x + width, y - height])
48
+ pdf.scale(2, :origin => [x + width / 2, y - height / 2]) do
49
+ pdf.stroke_rectangle([x, y], width, height)
50
+ pdf.draw_text("rectangle scaled from center", :at => [x + width, y - height])
51
+ end
52
+ end
@@ -45,7 +45,7 @@ Prawn::Document.generate("win-ansi.pdf") do
45
45
  when :center then offset = (total_width - width)/2
46
46
  end
47
47
 
48
- text(field, :at => [dx + offset, y])
48
+ draw_text(field, :at => [dx + offset, y])
49
49
  end
50
50
 
51
51
  dx += total_width
@@ -25,9 +25,9 @@ Prawn::Document.generate('font_calculations.pdf') do
25
25
  bl = y - bounds.absolute_bottom
26
26
 
27
27
  stroke_horizontal_rule
28
- text "When using text positioned with :at, the baseline is used", :at => [0, bl]
28
+ draw_text "When using text positioned with :at, the baseline is used", :at => [0, bl]
29
29
 
30
- text "(and the Y-cursor is not moved)", :at => [350, bl]
30
+ draw_text "(and the Y-cursor is not moved)", :at => [350, bl]
31
31
 
32
32
  colors = { :ascender => "ff0000",
33
33
  :descender => "00ff00",
@@ -57,7 +57,7 @@ Prawn::Document.generate('font_calculations.pdf') do
57
57
  move_down 40
58
58
 
59
59
  bl = y - bounds.absolute_bottom
60
- text "The quick brown fox jumps over the lazy dog.", :at => [0, bl]
60
+ draw_text "The quick brown fox jumps over the lazy dog.", :at => [0, bl]
61
61
 
62
62
  stroke_color colors[:ascender]
63
63
  stroke_line [0, bl], [0, bl + font.ascender]
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ #
3
+ # An early example of basic text generation at absolute positions.
4
+ # Mostly kept for nostalgia.
5
+ #
6
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
7
+
8
+ Prawn::Document.generate "indent_paragraphs.pdf" do |pdf|
9
+ hello = "hello " * 50
10
+ world = "world " * 50
11
+ pdf.text(hello + "\n" + world, :indent_paragraphs => 60)
12
+
13
+ pdf.move_cursor_to(pdf.font.height)
14
+ pdf.text(hello + "\n" + world, :indent_paragraphs => 60)
15
+
16
+ pdf.move_cursor_to(pdf.font.height * 3)
17
+ pdf.text(hello + "\n" + world, :indent_paragraphs => 60)
18
+ end
@@ -5,8 +5,8 @@
5
5
  require "#{File.dirname(__FILE__)}/../example_helper.rb"
6
6
 
7
7
  Prawn::Document.generate "kerning.pdf" do
8
- text "To kern?", :at => [200,720], :size => 24, :kerning => true
9
- text "To not kern?", :at => [200,690], :size => 24, :kerning => false
8
+ draw_text "To kern?", :at => [200,720], :size => 24, :kerning => true
9
+ draw_text "To not kern?", :at => [200,690], :size => 24, :kerning => false
10
10
 
11
11
  move_down 100
12
12
 
@@ -18,8 +18,8 @@ Prawn::Document.generate "kerning.pdf" do
18
18
 
19
19
  font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
20
20
 
21
- text "To kern?", :at => [200,660], :size => 24, :kerning => true
22
- text "To not kern?", :at => [200,630], :size => 24, :kerning => false
21
+ draw_text "To kern?", :at => [200,660], :size => 24, :kerning => true
22
+ draw_text "To not kern?", :at => [200,630], :size => 24, :kerning => false
23
23
 
24
24
  pad(30) do
25
25
  text "To kern and wrap. " * 5, :size => 24, :kerning => true
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Demonstrates transformations
4
+ #
5
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
6
+
7
+ Prawn::Document.generate "rotated_text.pdf" do |pdf|
8
+ pdf.line_width = 1
9
+ width = 150
10
+ height = 200
11
+ half_width = width / 2
12
+ half_height = height / 2
13
+ angle = 30
14
+
15
+
16
+ # AROUND THE CENTER
17
+
18
+ x = pdf.bounds.width / 2 - half_width
19
+ y = pdf.bounds.height / 2 + half_height
20
+
21
+ pdf.stroke_rectangle([x, y], width, height)
22
+ pdf.rotate(angle, :origin => [x + half_width, y - half_height]) do
23
+ pdf.stroke_rectangle([x, y], width, height)
24
+ end
25
+ pdf.text_box("rotated around the center " * 10,
26
+ :at => [x, y],
27
+ :width => width,
28
+ :height => height,
29
+ :rotate => angle,
30
+ :rotate_around => :center)
31
+
32
+
33
+ # AROUND THE UPPER_LEFT_CORNER
34
+
35
+ x = pdf.bounds.width - width
36
+ y = height
37
+
38
+ pdf.stroke_rectangle([x, y], width, height)
39
+ pdf.rotate(angle, :origin => [x, y]) do
40
+ pdf.stroke_rectangle([x, y], width, height)
41
+ end
42
+ pdf.text_box("rotated around upper left corner " * 10,
43
+ :at => [x, y],
44
+ :width => width,
45
+ :height => height,
46
+ :rotate => angle)
47
+
48
+
49
+ # AROUND THE UPPER_RIGHT_CORNER
50
+
51
+ x = 0
52
+ y = height
53
+
54
+ pdf.stroke_rectangle([x, y], width, height)
55
+ pdf.rotate(angle, :origin => [x + width, y]) do
56
+ pdf.stroke_rectangle([x, y], width, height)
57
+ end
58
+ pdf.text_box("rotated around upper right corner " * 10,
59
+ :at => [x, y],
60
+ :width => width,
61
+ :height => height,
62
+ :rotate => angle,
63
+ :rotate_around => :upper_right)
64
+
65
+
66
+ # AROUND THE LOWER_RIGHT_CORNER
67
+
68
+ x = 0
69
+ y = pdf.bounds.height
70
+
71
+ pdf.stroke_rectangle([x, y], width, height)
72
+ pdf.rotate(angle, :origin => [x + width, y - height]) do
73
+ pdf.stroke_rectangle([x, y], width, height)
74
+ end
75
+ pdf.text_box("rotated around lower right corner " * 10,
76
+ :at => [x, y],
77
+ :width => width,
78
+ :height => height,
79
+ :rotate => angle,
80
+ :rotate_around => :lower_right)
81
+
82
+
83
+ # AROUND THE LOWER_LEFT_CORNER
84
+
85
+ x = pdf.bounds.width - width
86
+ y = pdf.bounds.height
87
+
88
+ pdf.stroke_rectangle([x, y], width, height)
89
+ pdf.rotate(angle, :origin => [x, y - height]) do
90
+ pdf.stroke_rectangle([x, y], width, height)
91
+ end
92
+ pdf.text_box("rotated around lower left corner " * 10,
93
+ :at => [x, y],
94
+ :width => width,
95
+ :height => height,
96
+ :rotate => angle,
97
+ :rotate_around => :lower_left)
98
+ end
@@ -7,11 +7,11 @@ require "#{File.dirname(__FILE__)}/../example_helper.rb"
7
7
 
8
8
  Prawn::Document.generate "simple_text.pdf" do
9
9
  fill_color "0000ff"
10
- text "Hello World", :at => [200,420], :size => 32, :rotate => 45
10
+ draw_text "Hello World", :at => [200,420], :size => 32, :rotate => 45
11
11
  font "Times-Roman"
12
12
  fill_color "ff0000"
13
- text "Using Another Font", :at => [5,5]
13
+ draw_text "Using Another Font", :at => [5,5]
14
14
  start_new_page
15
15
  font "Courier"
16
- text "Goodbye World", :at => [288,50]
16
+ draw_text "Goodbye World", :at => [288,50]
17
17
  end