nurettin-prawn 1.0.0.rc1

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