prawn-git 2.0.1

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