prawn 0.15.0 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. data/COPYING +2 -2
  2. data/LICENSE +1 -1
  3. data/README.md +96 -0
  4. data/Rakefile +27 -30
  5. data/data/fonts/Action Man.dfont +0 -0
  6. data/data/fonts/Activa.ttf +0 -0
  7. data/data/fonts/Chalkboard.ttf +0 -0
  8. data/data/fonts/DejaVuSans.ttf +0 -0
  9. data/data/fonts/Dustismo_Roman.ttf +0 -0
  10. data/data/fonts/comicsans.ttf +0 -0
  11. data/data/fonts/gkai00mp.ttf +0 -0
  12. data/data/images/16bit.alpha +0 -0
  13. data/data/images/16bit.dat +0 -0
  14. data/data/images/dice.alpha +0 -0
  15. data/data/images/dice.dat +0 -0
  16. data/data/images/page_white_text.alpha +0 -0
  17. data/data/images/page_white_text.dat +0 -0
  18. data/data/images/rails.dat +0 -0
  19. data/data/images/rails.png +0 -0
  20. data/data/pdfs/nested_pages.pdf +13 -13
  21. data/lib/prawn.rb +21 -85
  22. data/lib/prawn/compatibility.rb +51 -0
  23. data/lib/prawn/core.rb +85 -0
  24. data/lib/prawn/core/annotations.rb +61 -0
  25. data/lib/prawn/core/byte_string.rb +9 -0
  26. data/lib/prawn/core/destinations.rb +90 -0
  27. data/lib/prawn/core/document_state.rb +78 -0
  28. data/lib/prawn/core/literal_string.rb +16 -0
  29. data/lib/prawn/core/name_tree.rb +177 -0
  30. data/lib/prawn/core/object_store.rb +264 -0
  31. data/lib/prawn/core/page.rb +215 -0
  32. data/lib/prawn/core/pdf_object.rb +108 -0
  33. data/lib/prawn/core/reference.rb +115 -0
  34. data/lib/prawn/core/text.rb +268 -0
  35. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  36. data/lib/prawn/core/text/formatted/line_wrap.rb +273 -0
  37. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  38. data/lib/prawn/document.rb +122 -155
  39. data/lib/prawn/document/bounding_box.rb +7 -36
  40. data/lib/prawn/document/column_box.rb +10 -38
  41. data/lib/prawn/document/graphics_state.rb +74 -11
  42. data/lib/prawn/document/internals.rb +23 -24
  43. data/lib/prawn/document/page_geometry.rb +136 -0
  44. data/lib/prawn/document/snapshot.rb +6 -7
  45. data/lib/prawn/document/span.rb +10 -12
  46. data/lib/prawn/encoding.rb +10 -9
  47. data/lib/prawn/errors.rb +30 -15
  48. data/lib/prawn/font.rb +104 -136
  49. data/lib/prawn/font/afm.rb +44 -46
  50. data/lib/prawn/font/dfont.rb +3 -4
  51. data/lib/prawn/font/ttf.rb +50 -31
  52. data/lib/prawn/graphics.rb +57 -302
  53. data/lib/prawn/graphics/cap_style.rb +3 -4
  54. data/lib/prawn/graphics/color.rb +5 -13
  55. data/lib/prawn/graphics/dash.rb +31 -53
  56. data/lib/prawn/graphics/gradient.rb +84 -0
  57. data/lib/prawn/graphics/join_style.rb +7 -9
  58. data/lib/prawn/graphics/transformation.rb +9 -10
  59. data/lib/prawn/graphics/transparency.rb +1 -3
  60. data/lib/prawn/images.rb +59 -69
  61. data/lib/prawn/images/image.rb +22 -6
  62. data/lib/prawn/images/jpg.rb +14 -20
  63. data/lib/prawn/images/png.rb +118 -61
  64. data/lib/prawn/layout.rb +15 -10
  65. data/lib/prawn/layout/grid.rb +54 -66
  66. data/lib/prawn/measurement_extensions.rb +6 -10
  67. data/lib/prawn/measurements.rb +21 -27
  68. data/lib/prawn/outline.rb +308 -6
  69. data/lib/prawn/repeater.rb +8 -10
  70. data/lib/prawn/security.rb +33 -55
  71. data/lib/prawn/security/arcfour.rb +0 -1
  72. data/lib/prawn/stamp.rb +3 -5
  73. data/lib/prawn/table.rb +60 -188
  74. data/lib/prawn/table/cell.rb +44 -272
  75. data/lib/prawn/table/cell/image.rb +3 -2
  76. data/lib/prawn/table/cell/in_table.rb +2 -4
  77. data/lib/prawn/table/cell/subtable.rb +2 -2
  78. data/lib/prawn/table/cell/text.rb +18 -41
  79. data/lib/prawn/table/cells.rb +48 -142
  80. data/lib/prawn/text.rb +25 -32
  81. data/lib/prawn/text/box.rb +6 -12
  82. data/lib/prawn/text/formatted.rb +4 -5
  83. data/lib/prawn/text/formatted/box.rb +59 -96
  84. data/lib/prawn/text/formatted/fragment.rb +23 -34
  85. data/lib/prawn/text/formatted/parser.rb +5 -15
  86. data/prawn.gemspec +13 -24
  87. data/spec/annotations_spec.rb +32 -16
  88. data/spec/bounding_box_spec.rb +17 -119
  89. data/spec/cell_spec.rb +42 -112
  90. data/spec/destinations_spec.rb +5 -5
  91. data/spec/document_spec.rb +111 -155
  92. data/spec/extensions/mocha.rb +0 -1
  93. data/spec/font_spec.rb +99 -149
  94. data/spec/formatted_text_arranger_spec.rb +43 -43
  95. data/spec/formatted_text_box_spec.rb +44 -43
  96. data/spec/formatted_text_fragment_spec.rb +8 -8
  97. data/spec/graphics_spec.rb +68 -151
  98. data/spec/grid_spec.rb +15 -26
  99. data/spec/images_spec.rb +30 -51
  100. data/spec/inline_formatted_text_parser_spec.rb +20 -69
  101. data/spec/jpg_spec.rb +4 -4
  102. data/spec/line_wrap_spec.rb +28 -28
  103. data/spec/measurement_units_spec.rb +6 -6
  104. data/spec/name_tree_spec.rb +112 -0
  105. data/spec/object_store_spec.rb +106 -17
  106. data/spec/outline_spec.rb +63 -103
  107. data/spec/pdf_object_spec.rb +170 -0
  108. data/spec/png_spec.rb +25 -25
  109. data/spec/reference_spec.rb +65 -8
  110. data/spec/repeater_spec.rb +10 -10
  111. data/spec/security_spec.rb +12 -44
  112. data/spec/snapshot_spec.rb +7 -7
  113. data/spec/span_spec.rb +15 -10
  114. data/spec/spec_helper.rb +8 -32
  115. data/spec/stamp_spec.rb +30 -29
  116. data/spec/stroke_styles_spec.rb +18 -36
  117. data/spec/table_spec.rb +111 -706
  118. data/spec/template_spec.rb +297 -0
  119. data/spec/text_at_spec.rb +33 -19
  120. data/spec/text_box_spec.rb +64 -100
  121. data/spec/text_rendering_mode_spec.rb +5 -5
  122. data/spec/text_spacing_spec.rb +4 -4
  123. data/spec/text_spec.rb +64 -84
  124. data/spec/transparency_spec.rb +5 -5
  125. metadata +290 -463
  126. checksums.yaml +0 -7
  127. data/.yardopts +0 -10
  128. data/Gemfile +0 -11
  129. data/data/images/16bit.color +0 -0
  130. data/data/images/dice.color +0 -0
  131. data/data/images/indexed_color.dat +0 -0
  132. data/data/images/indexed_color.png +0 -0
  133. data/data/images/page_white_text.color +0 -0
  134. data/lib/prawn/font_metric_cache.rb +0 -47
  135. data/lib/prawn/graphics/patterns.rb +0 -138
  136. data/lib/prawn/image_handler.rb +0 -36
  137. data/lib/prawn/soft_mask.rb +0 -96
  138. data/lib/prawn/table/cell/span_dummy.rb +0 -93
  139. data/lib/prawn/table/column_width_calculator.rb +0 -61
  140. data/lib/prawn/text/formatted/arranger.rb +0 -290
  141. data/lib/prawn/text/formatted/line_wrap.rb +0 -266
  142. data/lib/prawn/text/formatted/wrap.rb +0 -150
  143. data/lib/prawn/utilities.rb +0 -46
  144. data/manual/basic_concepts/adding_pages.rb +0 -27
  145. data/manual/basic_concepts/basic_concepts.rb +0 -34
  146. data/manual/basic_concepts/creation.rb +0 -39
  147. data/manual/basic_concepts/cursor.rb +0 -33
  148. data/manual/basic_concepts/measurement.rb +0 -25
  149. data/manual/basic_concepts/origin.rb +0 -38
  150. data/manual/basic_concepts/other_cursor_helpers.rb +0 -40
  151. data/manual/bounding_box/bounding_box.rb +0 -39
  152. data/manual/bounding_box/bounds.rb +0 -49
  153. data/manual/bounding_box/canvas.rb +0 -24
  154. data/manual/bounding_box/creation.rb +0 -23
  155. data/manual/bounding_box/indentation.rb +0 -46
  156. data/manual/bounding_box/nesting.rb +0 -45
  157. data/manual/bounding_box/russian_boxes.rb +0 -40
  158. data/manual/bounding_box/stretchy.rb +0 -31
  159. data/manual/document_and_page_options/background.rb +0 -27
  160. data/manual/document_and_page_options/document_and_page_options.rb +0 -32
  161. data/manual/document_and_page_options/metadata.rb +0 -23
  162. data/manual/document_and_page_options/page_margins.rb +0 -38
  163. data/manual/document_and_page_options/page_size.rb +0 -34
  164. data/manual/document_and_page_options/print_scaling.rb +0 -20
  165. data/manual/example_file.rb +0 -111
  166. data/manual/example_helper.rb +0 -411
  167. data/manual/example_package.rb +0 -53
  168. data/manual/example_section.rb +0 -46
  169. data/manual/graphics/circle_and_ellipse.rb +0 -22
  170. data/manual/graphics/color.rb +0 -24
  171. data/manual/graphics/common_lines.rb +0 -30
  172. data/manual/graphics/fill_and_stroke.rb +0 -42
  173. data/manual/graphics/fill_rules.rb +0 -37
  174. data/manual/graphics/gradients.rb +0 -37
  175. data/manual/graphics/graphics.rb +0 -58
  176. data/manual/graphics/helper.rb +0 -24
  177. data/manual/graphics/line_width.rb +0 -35
  178. data/manual/graphics/lines_and_curves.rb +0 -41
  179. data/manual/graphics/polygon.rb +0 -29
  180. data/manual/graphics/rectangle.rb +0 -21
  181. data/manual/graphics/rotate.rb +0 -28
  182. data/manual/graphics/scale.rb +0 -41
  183. data/manual/graphics/soft_masks.rb +0 -46
  184. data/manual/graphics/stroke_cap.rb +0 -31
  185. data/manual/graphics/stroke_dash.rb +0 -48
  186. data/manual/graphics/stroke_join.rb +0 -30
  187. data/manual/graphics/translate.rb +0 -29
  188. data/manual/graphics/transparency.rb +0 -35
  189. data/manual/images/absolute_position.rb +0 -23
  190. data/manual/images/fit.rb +0 -21
  191. data/manual/images/horizontal.rb +0 -25
  192. data/manual/images/images.rb +0 -40
  193. data/manual/images/plain_image.rb +0 -18
  194. data/manual/images/scale.rb +0 -22
  195. data/manual/images/vertical.rb +0 -28
  196. data/manual/images/width_and_height.rb +0 -25
  197. data/manual/layout/boxes.rb +0 -27
  198. data/manual/layout/content.rb +0 -25
  199. data/manual/layout/layout.rb +0 -28
  200. data/manual/layout/simple_grid.rb +0 -23
  201. data/manual/manual/cover.rb +0 -36
  202. data/manual/manual/foreword.rb +0 -85
  203. data/manual/manual/how_to_read_this_manual.rb +0 -41
  204. data/manual/manual/manual.rb +0 -34
  205. data/manual/outline/add_subsection_to.rb +0 -61
  206. data/manual/outline/insert_section_after.rb +0 -47
  207. data/manual/outline/outline.rb +0 -32
  208. data/manual/outline/sections_and_pages.rb +0 -67
  209. data/manual/repeatable_content/page_numbering.rb +0 -54
  210. data/manual/repeatable_content/repeatable_content.rb +0 -31
  211. data/manual/repeatable_content/repeater.rb +0 -55
  212. data/manual/repeatable_content/stamp.rb +0 -41
  213. data/manual/security/encryption.rb +0 -31
  214. data/manual/security/permissions.rb +0 -38
  215. data/manual/security/security.rb +0 -28
  216. data/manual/syntax_highlight.rb +0 -52
  217. data/manual/table/basic_block.rb +0 -53
  218. data/manual/table/before_rendering_page.rb +0 -26
  219. data/manual/table/cell_border_lines.rb +0 -24
  220. data/manual/table/cell_borders_and_bg.rb +0 -31
  221. data/manual/table/cell_dimensions.rb +0 -30
  222. data/manual/table/cell_text.rb +0 -38
  223. data/manual/table/column_widths.rb +0 -30
  224. data/manual/table/content_and_subtables.rb +0 -39
  225. data/manual/table/creation.rb +0 -27
  226. data/manual/table/filtering.rb +0 -36
  227. data/manual/table/flow_and_header.rb +0 -17
  228. data/manual/table/image_cells.rb +0 -33
  229. data/manual/table/position.rb +0 -29
  230. data/manual/table/row_colors.rb +0 -20
  231. data/manual/table/span.rb +0 -30
  232. data/manual/table/style.rb +0 -22
  233. data/manual/table/table.rb +0 -52
  234. data/manual/table/width.rb +0 -27
  235. data/manual/text/alignment.rb +0 -44
  236. data/manual/text/color.rb +0 -24
  237. data/manual/text/column_box.rb +0 -32
  238. data/manual/text/fallback_fonts.rb +0 -37
  239. data/manual/text/font.rb +0 -41
  240. data/manual/text/font_size.rb +0 -45
  241. data/manual/text/font_style.rb +0 -23
  242. data/manual/text/formatted_callbacks.rb +0 -60
  243. data/manual/text/formatted_text.rb +0 -54
  244. data/manual/text/free_flowing_text.rb +0 -51
  245. data/manual/text/group.rb +0 -31
  246. data/manual/text/inline.rb +0 -43
  247. data/manual/text/kerning_and_character_spacing.rb +0 -39
  248. data/manual/text/leading.rb +0 -25
  249. data/manual/text/line_wrapping.rb +0 -41
  250. data/manual/text/paragraph_indentation.rb +0 -26
  251. data/manual/text/positioned_text.rb +0 -38
  252. data/manual/text/registering_families.rb +0 -48
  253. data/manual/text/rendering_and_color.rb +0 -37
  254. data/manual/text/right_to_left_text.rb +0 -43
  255. data/manual/text/rotation.rb +0 -43
  256. data/manual/text/single_usage.rb +0 -37
  257. data/manual/text/text.rb +0 -75
  258. data/manual/text/text_box_excess.rb +0 -32
  259. data/manual/text/text_box_extensions.rb +0 -45
  260. data/manual/text/text_box_overflow.rb +0 -44
  261. data/manual/text/utf8.rb +0 -28
  262. data/manual/text/win_ansi_charset.rb +0 -59
  263. data/spec/acceptance/png.rb +0 -23
  264. data/spec/column_box_spec.rb +0 -65
  265. data/spec/extensions/encoding_helpers.rb +0 -9
  266. data/spec/font_metric_cache_spec.rb +0 -52
  267. data/spec/image_handler_spec.rb +0 -54
  268. data/spec/soft_mask_spec.rb +0 -117
  269. data/spec/table/span_dummy_spec.rb +0 -17
@@ -0,0 +1,297 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
2
+
3
+ describe "Document built from a template" do
4
+ it "should have the same page count as the source document" do
5
+ filename = "#{Prawn::BASEDIR}/spec/data/curves.pdf"
6
+ @pdf = Prawn::Document.new(:template => filename)
7
+ page_counter = PDF::Inspector::Page.analyze(@pdf.render)
8
+
9
+ page_counter.pages.size.should == 1
10
+ end
11
+
12
+ it "should have start with the Y cursor at the top of the document" do
13
+ filename = "#{Prawn::BASEDIR}/spec/data/curves.pdf"
14
+
15
+ @pdf = Prawn::Document.new(:template => filename)
16
+ (@pdf.y == nil).should == false
17
+ end
18
+
19
+ it "should respect margins set by Prawn" do
20
+ filename = "#{Prawn::BASEDIR}/spec/data/curves.pdf"
21
+
22
+ @pdf = Prawn::Document.new(:template => filename, :margin => 0)
23
+ assert_equal @pdf.page.margins, { :left => 0,
24
+ :right => 0,
25
+ :top => 0,
26
+ :bottom => 0 }
27
+
28
+ @pdf = Prawn::Document.new(:template => filename, :left_margin => 0)
29
+
30
+ assert_equal @pdf.page.margins, { :left => 0,
31
+ :right => 36,
32
+ :top => 36,
33
+ :bottom => 36 }
34
+
35
+ @pdf.start_new_page(:right_margin => 0)
36
+
37
+ assert_equal @pdf.page.margins, { :left => 0,
38
+ :right => 0,
39
+ :top => 36,
40
+ :bottom => 36 }
41
+
42
+
43
+
44
+ end
45
+
46
+ it "should not add an extra restore_graphics_state operator to the end of any content stream" do
47
+ filename = "#{Prawn::BASEDIR}/spec/data/curves.pdf"
48
+
49
+ @pdf = Prawn::Document.new(:template => filename)
50
+ output = StringIO.new(@pdf.render)
51
+ hash = PDF::Reader::ObjectHash.new(output)
52
+
53
+ hash.each_value do |obj|
54
+ next unless obj.kind_of?(PDF::Reader::Stream)
55
+
56
+ data = obj.data.tr(" \n\r","")
57
+ data.include?("QQ").should == false
58
+ end
59
+ end
60
+
61
+ it "should have a single page object if importing a single page template" do
62
+ filename = "#{Prawn::BASEDIR}/data/pdfs/hexagon.pdf"
63
+
64
+ @pdf = Prawn::Document.new(:template => filename)
65
+ output = StringIO.new(@pdf.render)
66
+ hash = PDF::Reader::ObjectHash.new(output)
67
+
68
+ pages = hash.values.select { |obj| obj.kind_of?(Hash) && obj[:Type] == :Page }
69
+
70
+ pages.size.should == 1
71
+ end
72
+
73
+ it "should have two content streams if importing a single page template" do
74
+ filename = "#{Prawn::BASEDIR}/data/pdfs/hexagon.pdf"
75
+
76
+ @pdf = Prawn::Document.new(:template => filename)
77
+ output = StringIO.new(@pdf.render)
78
+ hash = PDF::Reader::ObjectHash.new(output)
79
+
80
+ streams = hash.values.select { |obj| obj.kind_of?(PDF::Reader::Stream) }
81
+
82
+ streams.size.should == 2
83
+ end
84
+
85
+ it "should not die if using this PDF as a template" do
86
+ filename = "#{Prawn::BASEDIR}/data/pdfs/complex_template.pdf"
87
+
88
+ assert_nothing_raised do
89
+ @pdf = Prawn::Document.new(:template => filename)
90
+ end
91
+ end
92
+
93
+
94
+ it "should have balance q/Q operators on all content streams" do
95
+ filename = "#{Prawn::BASEDIR}/data/pdfs/hexagon.pdf"
96
+
97
+ @pdf = Prawn::Document.new(:template => filename)
98
+ output = StringIO.new(@pdf.render)
99
+ hash = PDF::Reader::ObjectHash.new(output)
100
+
101
+ streams = hash.values.select { |obj| obj.kind_of?(PDF::Reader::Stream) }
102
+
103
+ streams.each do |stream|
104
+ data = stream.unfiltered_data
105
+ data.scan("q").size.should == 1
106
+ data.scan("Q").size.should == 1
107
+ end
108
+ end
109
+
110
+ it "should allow text to be added to a single page template" do
111
+ filename = "#{Prawn::BASEDIR}/data/pdfs/hexagon.pdf"
112
+
113
+ @pdf = Prawn::Document.new(:template => filename)
114
+
115
+ @pdf.text "Adding some text"
116
+
117
+ text = PDF::Inspector::Text.analyze(@pdf.render)
118
+ text.strings.first.should == "Adding some text"
119
+ end
120
+
121
+ it "should allow PDFs with page resources behind an indirect object to be used as templates" do
122
+ filename = "#{Prawn::BASEDIR}/data/pdfs/resources_as_indirect_object.pdf"
123
+
124
+ @pdf = Prawn::Document.new(:template => filename)
125
+
126
+ @pdf.text "Adding some text"
127
+
128
+ text = PDF::Inspector::Text.analyze(@pdf.render)
129
+ all_text = text.strings.join
130
+ all_text.include?("Adding some text").should == true
131
+ end
132
+
133
+ it "should copy the PDF version from the template file" do
134
+ filename = "#{Prawn::BASEDIR}/data/pdfs/version_1_6.pdf"
135
+
136
+ @pdf = Prawn::Document.new(:template => filename)
137
+ str = @pdf.render
138
+ str[0,8].should == "%PDF-1.6"
139
+ end
140
+
141
+ it "should correctly add a TTF font to a template that has existing fonts" do
142
+ filename = "#{Prawn::BASEDIR}/data/pdfs/contains_ttf_font.pdf"
143
+ @pdf = Prawn::Document.new(:template => filename)
144
+ @pdf.font "#{Prawn::BASEDIR}/data/fonts/Chalkboard.ttf"
145
+ @pdf.move_down(40)
146
+ @pdf.text "Hi There"
147
+
148
+ output = StringIO.new(@pdf.render)
149
+ hash = PDF::Reader::ObjectHash.new(output)
150
+
151
+ page_dict = hash.values.detect{ |obj| obj.is_a?(Hash) && obj[:Type] == :Page }
152
+ resources = page_dict[:Resources]
153
+ fonts = resources[:Font]
154
+ fonts.size.should == 2
155
+ end
156
+
157
+ it "should correctly import a template file that is missing a MediaBox entry" do
158
+ filename = "#{Prawn::BASEDIR}/data/pdfs/page_without_mediabox.pdf"
159
+
160
+ @pdf = Prawn::Document.new(:template => filename)
161
+ str = @pdf.render
162
+ str[0,4].should == "%PDF"
163
+ end
164
+
165
+ end
166
+
167
+ describe "Document#start_new_page with :template option" do
168
+ filename = "#{Prawn::BASEDIR}/spec/data/curves.pdf"
169
+
170
+ it "should set the imported page's parent to the document pages catalog" do
171
+ @pdf = Prawn::Document.new()
172
+ @pdf.start_new_page(:template => filename)
173
+ @pdf.state.page.dictionary.data[:Parent].should == @pdf.state.store.pages
174
+ end
175
+
176
+ it "should set start the Y cursor at the top of the page" do
177
+ @pdf = Prawn::Document.new()
178
+ @pdf.start_new_page(:template => filename)
179
+ (@pdf.y == nil).should == false
180
+ end
181
+
182
+ it "should respect margins set by Prawn" do
183
+ @pdf = Prawn::Document.new(:margin => 0)
184
+ @pdf.start_new_page(:template => filename)
185
+ assert_equal @pdf.page.margins, { :left => 0,
186
+ :right => 0,
187
+ :top => 0,
188
+ :bottom => 0 }
189
+
190
+ @pdf = Prawn::Document.new(:left_margin => 0)
191
+ @pdf.start_new_page(:template => filename)
192
+ assert_equal @pdf.page.margins, { :left => 0,
193
+ :right => 36,
194
+ :top => 36,
195
+ :bottom => 36 }
196
+ @pdf.start_new_page(:template => filename, :right_margin => 0)
197
+ assert_equal @pdf.page.margins, { :left => 0,
198
+ :right => 0,
199
+ :top => 36,
200
+ :bottom => 36 }
201
+ end
202
+
203
+ it "should not add an extra restore_graphics_state operator to the end of any content stream" do
204
+ @pdf = Prawn::Document.new
205
+ @pdf.start_new_page(:template => filename)
206
+ output = StringIO.new(@pdf.render)
207
+ hash = PDF::Reader::ObjectHash.new(output)
208
+
209
+ hash.each_value do |obj|
210
+ next unless obj.kind_of?(PDF::Reader::Stream)
211
+
212
+ data = obj.data.tr(" \n\r","")
213
+ data.include?("QQ").should == false
214
+ end
215
+ end
216
+
217
+ it "should have two content streams if importing a single page template" do
218
+ filename = "#{Prawn::BASEDIR}/data/pdfs/hexagon.pdf"
219
+ @pdf = Prawn::Document.new()
220
+ @pdf.start_new_page(:template => filename)
221
+ output = StringIO.new(@pdf.render)
222
+ hash = PDF::Reader::ObjectHash.new(output)
223
+ pages = hash.values.find {|obj| obj.is_a?(Hash) && obj[:Type] == :Pages}[:Kids]
224
+ template_page = hash[pages[1]]
225
+ template_page[:Contents].size.should == 2
226
+ end
227
+
228
+ it "should have balance q/Q operators on all content streams" do
229
+ filename = "#{Prawn::BASEDIR}/data/pdfs/hexagon.pdf"
230
+
231
+ @pdf = Prawn::Document.new()
232
+ @pdf.start_new_page(:template => filename)
233
+ output = StringIO.new(@pdf.render)
234
+ hash = PDF::Reader::ObjectHash.new(output)
235
+
236
+ streams = hash.values.select { |obj| obj.kind_of?(PDF::Reader::Stream) }
237
+
238
+ streams.each do |stream|
239
+ data = stream.unfiltered_data
240
+ data.scan("q").size.should == 1
241
+ data.scan("Q").size.should == 1
242
+ end
243
+ end
244
+
245
+ it "should allow text to be added to a single page template" do
246
+
247
+ @pdf = Prawn::Document.new()
248
+ @pdf.start_new_page(:template => filename)
249
+
250
+ @pdf.text "Adding some text"
251
+
252
+ text = PDF::Inspector::Text.analyze(@pdf.render)
253
+ text.strings.first.should == "Adding some text"
254
+ end
255
+
256
+ it "should allow PDFs with page resources behind an indirect object to be used as templates" do
257
+ filename = "#{Prawn::BASEDIR}/data/pdfs/resources_as_indirect_object.pdf"
258
+
259
+ @pdf = Prawn::Document.new()
260
+ @pdf.start_new_page(:template => filename)
261
+
262
+ @pdf.text "Adding some text"
263
+
264
+ text = PDF::Inspector::Text.analyze(@pdf.render)
265
+ all_text = text.strings.join
266
+ all_text.include?("Adding some text").should == true
267
+ end
268
+
269
+ it "should correctly add a TTF font to a template that has existing fonts" do
270
+ filename = "#{Prawn::BASEDIR}/data/pdfs/contains_ttf_font.pdf"
271
+ @pdf = Prawn::Document.new()
272
+ @pdf.start_new_page(:template => filename)
273
+ @pdf.font "#{Prawn::BASEDIR}/data/fonts/Chalkboard.ttf"
274
+ @pdf.move_down(40)
275
+ @pdf.text "Hi There"
276
+
277
+ output = StringIO.new(@pdf.render)
278
+ hash = PDF::Reader::ObjectHash.new(output)
279
+ hash = PDF::Reader::ObjectHash.new(output)
280
+ pages = hash.values.find {|obj| obj.is_a?(Hash) && obj[:Type] == :Pages}[:Kids]
281
+ template_page = hash[pages[1]]
282
+ resources = template_page[:Resources]
283
+ fonts = resources[:Font]
284
+ fonts.size.should == 2
285
+ end
286
+
287
+ context "using template_page option" do
288
+ it "uses the specified page option" do
289
+ filename = "#{Prawn::BASEDIR}/data/pdfs/multipage_template.pdf"
290
+ @pdf = Prawn::Document.new()
291
+ @pdf.start_new_page(:template => filename, :template_page => 2)
292
+ text = PDF::Inspector::Text.analyze(@pdf.render)
293
+ text.strings.first.should == "This is template page 2"
294
+ end
295
+ end
296
+
297
+ end
data/spec/text_at_spec.rb CHANGED
@@ -5,12 +5,12 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
5
5
  describe "#draw_text" do
6
6
  before(:each) { create_pdf }
7
7
 
8
- it "should raise_error ArgumentError if :at option omitted" do
9
- lambda { @pdf.draw_text("hai", { }) }.should raise_error(ArgumentError)
8
+ it "should raise ArgumentError if :at option omitted" do
9
+ lambda { @pdf.draw_text("hai", { }) }.should.raise(ArgumentError)
10
10
  end
11
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)
12
+ it "should raise ArgumentError if :align option included" do
13
+ lambda { @pdf.draw_text("hai", :at => [0, 0], :align => :center) }.should.raise(ArgumentError)
14
14
  end
15
15
 
16
16
  it "should allow drawing empty strings to the page" do
@@ -85,8 +85,8 @@ describe "#draw_text" do
85
85
  text.font_settings[1][:name].should == :Courier
86
86
  end
87
87
 
88
- it "should raise_error an exception when an unknown font is used" do
89
- lambda { @pdf.font "Pao bu" }.should raise_error(Prawn::Errors::UnknownFont)
88
+ it "should raise an exception when an unknown font is used" do
89
+ lambda { @pdf.font "Pao bu" }.should.raise(Prawn::Errors::UnknownFont)
90
90
  end
91
91
 
92
92
  it "should correctly render a utf-8 string when using a built-in font" do
@@ -98,18 +98,32 @@ describe "#draw_text" do
98
98
  text.strings.first.should == str
99
99
  end
100
100
 
101
- it "should raise_error an exception when a utf-8 incompatible string is rendered" do
102
- str = "Blah \xDD"
103
- str.force_encoding(Encoding::ASCII_8BIT)
104
- lambda { @pdf.draw_text(str, :at => [0, 0]) }.should raise_error(
105
- Prawn::Errors::IncompatibleStringEncoding)
106
- end
107
-
108
- it "should not raise an exception when a shift-jis string is rendered" do
109
- datafile = "#{Prawn::DATADIR}/shift_jis_text.txt"
110
- sjis_str = File.open(datafile, "r:shift_jis") { |f| f.gets }
111
- @pdf.font("#{Prawn::DATADIR}/fonts/gkai00mp.ttf")
112
-
113
- @pdf.draw_text(sjis_str, :at => [0, 0])
101
+ if "spec".respond_to?(:encode!)
102
+ # Handle non utf-8 string encodings in a sane way on M17N aware VMs
103
+ it "should raise an exception when a utf-8 incompatible string is rendered" do
104
+ str = "Blah \xDD"
105
+ str.force_encoding("ASCII-8BIT")
106
+ lambda { @pdf.draw_text(str, :at => [0, 0]) }.should.raise(
107
+ Prawn::Errors::IncompatibleStringEncoding)
108
+ end
109
+ it "should not raise an exception when a shift-jis string is rendered" do
110
+ datafile = "#{Prawn::BASEDIR}/data/shift_jis_text.txt"
111
+ sjis_str = File.open(datafile, "r:shift_jis") { |f| f.gets }
112
+ @pdf.font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf")
113
+ lambda { @pdf.draw_text(sjis_str, :at => [0, 0]) }.should.not.raise(
114
+ Prawn::Errors::IncompatibleStringEncoding)
115
+ end
116
+ else
117
+ # Handle non utf-8 string encodings in a sane way on non-M17N aware VMs
118
+ it "should raise an exception when a corrupt utf-8 string is rendered" do
119
+ str = "Blah \xDD"
120
+ lambda { @pdf.draw_text(str, :at => [0, 0]) }.should.raise(
121
+ Prawn::Errors::IncompatibleStringEncoding)
122
+ end
123
+ it "should raise an exception when a shift-jis string is rendered" do
124
+ sjis_str = File.read("#{Prawn::BASEDIR}/data/shift_jis_text.txt")
125
+ lambda { @pdf.draw_text(sjis_str, :at => [0, 0]) }.should.raise(
126
+ Prawn::Errors::IncompatibleStringEncoding)
127
+ end
114
128
  end
115
129
  end
@@ -3,55 +3,55 @@
3
3
  require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
4
 
5
5
  describe "Text::Box#nothing_printed?" do
6
- it "should be_true when nothing printed" do
6
+ it "should be true when nothing printed" do
7
7
  create_pdf
8
8
  string = "Hello world, how are you?\nI'm fine, thank you."
9
9
  text_box = Prawn::Text::Box.new(string,
10
10
  :height => 2,
11
11
  :document => @pdf)
12
12
  text_box.render
13
- text_box.nothing_printed?.should be_true
13
+ text_box.nothing_printed?.should.be true
14
14
  end
15
- it "should be_false when something printed" do
15
+ it "should be false when something printed" do
16
16
  create_pdf
17
17
  string = "Hello world, how are you?\nI'm fine, thank you."
18
18
  text_box = Prawn::Text::Box.new(string,
19
19
  :height => 14,
20
20
  :document => @pdf)
21
21
  text_box.render
22
- text_box.nothing_printed?.should be_false
22
+ text_box.nothing_printed?.should.be false
23
23
  end
24
24
  end
25
25
 
26
26
  describe "Text::Box#everything_printed?" do
27
- it "should be_false when not everything printed" do
27
+ it "should be false when not everything printed" do
28
28
  create_pdf
29
29
  string = "Hello world, how are you?\nI'm fine, thank you."
30
30
  text_box = Prawn::Text::Box.new(string,
31
31
  :height => 14,
32
32
  :document => @pdf)
33
33
  text_box.render
34
- text_box.everything_printed?.should be_false
34
+ text_box.everything_printed?.should.be false
35
35
  end
36
- it "should be_true when everything printed" do
36
+ it "should be true when everything printed" do
37
37
  create_pdf
38
38
  string = "Hello world, how are you?\nI'm fine, thank you."
39
39
  text_box = Prawn::Text::Box.new(string,
40
40
  :document => @pdf)
41
41
  text_box.render
42
- text_box.everything_printed?.should be_true
42
+ text_box.everything_printed?.should.be true
43
43
  end
44
44
  end
45
45
 
46
46
  describe "Text::Box#line_gap" do
47
- it "should == the line gap of the font when using a single " +
47
+ it "should equal the line gap of the font when using a single " +
48
48
  "font and font size" do
49
49
  create_pdf
50
50
  string = "Hello world, how are you?\nI'm fine, thank you."
51
51
  text_box = Prawn::Text::Box.new(string,
52
52
  :document => @pdf)
53
53
  text_box.render
54
- text_box.line_gap.should be_within(0.0001).of(@pdf.font.line_gap)
54
+ text_box.line_gap.should.be.close(@pdf.font.line_gap, 0.0001)
55
55
  end
56
56
  end
57
57
 
@@ -74,7 +74,7 @@ describe "Text::Box" do
74
74
  @pdf.text_direction(:rtl)
75
75
  @pdf.text_direction = :rtl
76
76
  @pdf.text_direction = :rtl
77
- @pdf.font("#{Prawn::DATADIR}/fonts/gkai00mp.ttf", :size => 16) do
77
+ @pdf.font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf", :size => 16) do
78
78
  @pdf.text "写个小"
79
79
  end
80
80
  text = PDF::Inspector::Text.analyze(@pdf.render)
@@ -116,7 +116,7 @@ describe "Text::Box" do
116
116
  end
117
117
  it "should default to document-wide leading if no" +
118
118
  "leading option is provided" do
119
-
119
+
120
120
  end
121
121
  end
122
122
 
@@ -128,7 +128,7 @@ describe "Text::Box#render with :align => :justify" do
128
128
  text_box = Prawn::Text::Box.new(string, options)
129
129
  text_box.render
130
130
  contents = PDF::Inspector::Text.analyze(@pdf.render)
131
- contents.word_spacing[0].should be > 0
131
+ contents.word_spacing[0].should.be > 0
132
132
  end
133
133
  it "should not justify the last line of a paragraph" do
134
134
  create_pdf
@@ -137,7 +137,7 @@ describe "Text::Box#render with :align => :justify" do
137
137
  text_box = Prawn::Text::Box.new(string, options)
138
138
  text_box.render
139
139
  contents = PDF::Inspector::Text.analyze(@pdf.render)
140
- contents.word_spacing.should be_empty
140
+ contents.word_spacing.should.be.empty
141
141
  end
142
142
  end
143
143
 
@@ -160,19 +160,19 @@ describe "Text::Box" do
160
160
  end
161
161
 
162
162
  describe "Text::Box#height without leading" do
163
- it "should == the sum of the height of each line, " +
163
+ it "should equal the sum of the height of each line, " +
164
164
  "not including the space below the last line" do
165
165
  create_pdf
166
166
  text = "Oh hai text rect.\nOh hai text rect."
167
167
  options = { :document => @pdf }
168
168
  text_box = Prawn::Text::Box.new(text, options)
169
169
  text_box.render
170
- text_box.height.should be_within(0.001).of(@pdf.font.height * 2 - @pdf.font.line_gap)
170
+ text_box.height.should.be.close(@pdf.font.height * 2 - @pdf.font.line_gap, 0.001)
171
171
  end
172
172
  end
173
173
 
174
174
  describe "Text::Box#height with leading" do
175
- it "should == the sum of the height of each line plus leading, " +
175
+ it "should equal the sum of the height of each line plus leading, " +
176
176
  "but not including the space below the last line" do
177
177
  create_pdf
178
178
  text = "Oh hai text rect.\nOh hai text rect."
@@ -180,38 +180,7 @@ describe "Text::Box#height with leading" do
180
180
  options = { :document => @pdf, :leading => leading }
181
181
  text_box = Prawn::Text::Box.new(text, options)
182
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 { |_, _| }
183
+ text_box.height.should.be.close((@pdf.font.height + leading) * 2 - @pdf.font.line_gap - leading, 0.001)
215
184
  end
216
185
  end
217
186
 
@@ -219,7 +188,7 @@ describe "Text::Box#valid_options" do
219
188
  it "should return an array" do
220
189
  create_pdf
221
190
  text_box = Prawn::Text::Box.new("", :document => @pdf)
222
- text_box.valid_options.should be_a_kind_of(Array)
191
+ text_box.valid_options.should.be.kind_of(Array)
223
192
  end
224
193
  end
225
194
 
@@ -242,7 +211,7 @@ describe "Text::Box#render" do
242
211
  text_box = Prawn::Text::Box.new(@text, @options)
243
212
  text_box.render
244
213
  text = PDF::Inspector::Text.analyze(@pdf.render)
245
- text.strings.should_not be_empty
214
+ text.strings.should.not.be.empty
246
215
  end
247
216
  it "should not draw a transformation matrix" do
248
217
  create_pdf
@@ -276,17 +245,16 @@ describe "Text::Box#render(:dry_run => true)" do
276
245
  text_box = Prawn::Text::Box.new(@text, @options)
277
246
  text_box.render(:dry_run => true)
278
247
  text = PDF::Inspector::Text.analyze(@pdf.render)
279
- text.strings.should be_empty
248
+ text.strings.should.be.empty
280
249
  end
281
-
282
- it "subsequent calls to render should_not raise_error an ArgumentError exception" do
250
+ it "subsequent calls to render should not raise an ArgumentError exception" do
283
251
  create_pdf
284
252
  @text = "™©"
285
253
  @options = { :document => @pdf }
286
254
  text_box = Prawn::Text::Box.new(@text, @options)
287
255
  text_box.render(:dry_run => true)
288
-
289
- text_box.render
256
+ lambda { text_box.render }.should.not.raise(
257
+ Prawn::Errors::IncompatibleStringEncoding)
290
258
  end
291
259
  end
292
260
 
@@ -361,7 +329,7 @@ describe "Text::Box#render with :rotate option of 30)" do
361
329
  reduce_precision(@cos), 0, 0]
362
330
 
363
331
  text = PDF::Inspector::Text.analyze(@pdf.render)
364
- text.strings.should_not be_empty
332
+ text.strings.should.not.be.empty
365
333
  end
366
334
  end
367
335
  context ":rotate_around option of :upper_left" do
@@ -384,7 +352,7 @@ describe "Text::Box#render with :rotate option of 30)" do
384
352
  reduce_precision(@cos), 0, 0]
385
353
 
386
354
  text = PDF::Inspector::Text.analyze(@pdf.render)
387
- text.strings.should_not be_empty
355
+ text.strings.should.not.be.empty
388
356
  end
389
357
  end
390
358
  context "default :rotate_around" do
@@ -406,7 +374,7 @@ describe "Text::Box#render with :rotate option of 30)" do
406
374
  reduce_precision(@cos), 0, 0]
407
375
 
408
376
  text = PDF::Inspector::Text.analyze(@pdf.render)
409
- text.strings.should_not be_empty
377
+ text.strings.should.not.be.empty
410
378
  end
411
379
  end
412
380
  context ":rotate_around option of :upper_right" do
@@ -429,7 +397,7 @@ describe "Text::Box#render with :rotate option of 30)" do
429
397
  reduce_precision(@cos), 0, 0]
430
398
 
431
399
  text = PDF::Inspector::Text.analyze(@pdf.render)
432
- text.strings.should_not be_empty
400
+ text.strings.should.not.be.empty
433
401
  end
434
402
  end
435
403
  context ":rotate_around option of :lower_right" do
@@ -452,7 +420,7 @@ describe "Text::Box#render with :rotate option of 30)" do
452
420
  reduce_precision(@cos), 0, 0]
453
421
 
454
422
  text = PDF::Inspector::Text.analyze(@pdf.render)
455
- text.strings.should_not be_empty
423
+ text.strings.should.not.be.empty
456
424
  end
457
425
  end
458
426
  context ":rotate_around option of :lower_left" do
@@ -475,7 +443,7 @@ describe "Text::Box#render with :rotate option of 30)" do
475
443
  reduce_precision(@cos), 0, 0]
476
444
 
477
445
  text = PDF::Inspector::Text.analyze(@pdf.render)
478
- text.strings.should_not be_empty
446
+ text.strings.should.not.be.empty
479
447
  end
480
448
  end
481
449
  end
@@ -488,7 +456,7 @@ describe "Text::Box default height" do
488
456
  @text = "Oh hai\n" * 60
489
457
  text_box = Prawn::Text::Box.new(@text, :document => @pdf)
490
458
  text_box.render
491
- text_box.height.should be_within(@pdf.font.height).of(target_height)
459
+ text_box.height.should.be.close(target_height, @pdf.font.height)
492
460
  end
493
461
 
494
462
  it "should use the margin-box bottom if only in a stretchy bbox" do
@@ -497,7 +465,7 @@ describe "Text::Box default height" do
497
465
  @text = "Oh hai\n" * 60
498
466
  text_box = Prawn::Text::Box.new(@text, :document => @pdf)
499
467
  text_box.render
500
- text_box.height.should be_within(@pdf.font.height).of(target_height)
468
+ text_box.height.should.be.close(target_height, @pdf.font.height)
501
469
  end
502
470
  end
503
471
 
@@ -509,7 +477,7 @@ describe "Text::Box default height" do
509
477
  text_box = Prawn::Text::Box.new(@text, :document => @pdf,
510
478
  :height => 100, :overflow => :expand)
511
479
  text_box.render
512
- text_box.height.should be_within(@pdf.font.height).of(target_height)
480
+ text_box.height.should.be.close(target_height, @pdf.font.height)
513
481
  end
514
482
  end
515
483
 
@@ -520,7 +488,7 @@ describe "Text::Box default height" do
520
488
  @text = "Oh hai\n" * 60
521
489
  text_box = Prawn::Text::Box.new(@text, :document => @pdf)
522
490
  text_box.render
523
- text_box.height.should be_within(@pdf.font.height).of(200)
491
+ text_box.height.should.be.close(200, @pdf.font.height)
524
492
  end
525
493
  end
526
494
  end
@@ -549,7 +517,7 @@ describe "Text::Box with text than can fit in the box" do
549
517
  :document => @pdf
550
518
  }
551
519
  end
552
-
520
+
553
521
  it "printed text should match requested text, except that preceding and " +
554
522
  "trailing white space will be stripped from each line, and newlines may " +
555
523
  "be inserted" do
@@ -557,7 +525,7 @@ describe "Text::Box with text than can fit in the box" do
557
525
  text_box.render
558
526
  text_box.text.gsub("\n", " ").should == @text.strip
559
527
  end
560
-
528
+
561
529
  it "render should return an empty string because no text remains unprinted" do
562
530
  text_box = Prawn::Text::Box.new(@text, @options)
563
531
  text_box.render.should == ""
@@ -567,7 +535,7 @@ describe "Text::Box with text than can fit in the box" do
567
535
  @options[:leading] = 40
568
536
  text_box = Prawn::Text::Box.new(@text, @options)
569
537
  text_box.render
570
- text_box.text.gsub("\n", " ").should_not == @text.strip
538
+ text_box.text.gsub("\n", " ").should.not == @text.strip
571
539
  end
572
540
  end
573
541
 
@@ -583,12 +551,12 @@ describe "Text::Box with text that fits exactly in the box" do
583
551
  :document => @pdf
584
552
  }
585
553
  end
586
-
554
+
587
555
  it "should have the expected height" do
588
556
  expected_height = @options.delete(:height)
589
557
  text_box = Prawn::Text::Box.new(@text, @options)
590
558
  text_box.render
591
- text_box.height.should be_within(0.0001).of(expected_height)
559
+ text_box.height.should.be.close(expected_height, 0.0001)
592
560
  end
593
561
 
594
562
  it "should print everything" do
@@ -613,7 +581,7 @@ describe "Text::Box with text that fits exactly in the box" do
613
581
  @options[:height] += @options[:leading] * @interlines - 1
614
582
  text_box = Prawn::Text::Box.new(@text, @options)
615
583
  text_box.render
616
- text_box.text.should_not == @text
584
+ text_box.text.should.not == @text
617
585
  end
618
586
  end
619
587
 
@@ -633,14 +601,14 @@ describe "Text::Box with text that fits exactly in the box" do
633
601
  @options[:height] += @options[:leading] * @interlines - 1
634
602
  text_box = Prawn::Text::Box.new(@text, @options)
635
603
  text_box.render
636
- text_box.text.should_not == @text
604
+ text_box.text.should.not == @text
637
605
  end
638
606
  end
639
607
  end
640
608
 
641
609
  describe "Text::Box printing UTF-8 string with higher bit characters" do
642
610
  before(:each) do
643
- create_pdf
611
+ create_pdf
644
612
  @text = "©"
645
613
  # not enough height to print any text, so we can directly compare against
646
614
  # the input string
@@ -649,7 +617,7 @@ describe "Text::Box printing UTF-8 string with higher bit characters" do
649
617
  :height => bounding_height,
650
618
  :document => @pdf
651
619
  }
652
- file = "#{Prawn::DATADIR}/fonts/Action Man.dfont"
620
+ file = "#{Prawn::BASEDIR}/data/fonts/Action Man.dfont"
653
621
  @pdf.font_families["Action Man"] = {
654
622
  :normal => { :file => file, :font => "ActionMan" },
655
623
  :italic => { :file => file, :font => "ActionMan-Italic" },
@@ -664,14 +632,13 @@ describe "Text::Box printing UTF-8 string with higher bit characters" do
664
632
  remaining_text = @text_box.render
665
633
  remaining_text.should == @text
666
634
  end
667
-
668
635
  it "subsequent calls to Text::Box need not include the" +
669
636
  " :skip_encoding => true option" do
670
637
  @pdf.font("Action Man")
671
638
  remaining_text = @text_box.render
672
-
673
- # expect that calling text_box will not raise an encoding error
674
- @pdf.text_box(remaining_text, :document => @pdf)
639
+ lambda {
640
+ @pdf.text_box(remaining_text, :document => @pdf)
641
+ }.should.not.raise(Prawn::Errors::IncompatibleStringEncoding)
675
642
  end
676
643
  end
677
644
 
@@ -685,17 +652,18 @@ describe "Text::Box printing UTF-8 string with higher bit characters" do
685
652
  remaining_text = @text_box.render
686
653
  lambda {
687
654
  @pdf.text_box(remaining_text, :document => @pdf)
688
- }.should raise_error(Prawn::Errors::IncompatibleStringEncoding)
689
-
690
- @pdf.text_box(remaining_text, :skip_encoding => true,
691
- :document => @pdf)
655
+ }.should.raise(Prawn::Errors::IncompatibleStringEncoding)
656
+ lambda {
657
+ @pdf.text_box(remaining_text, :skip_encoding => true,
658
+ :document => @pdf)
659
+ }.should.not.raise(Prawn::Errors::IncompatibleStringEncoding)
692
660
  end
693
661
  end
694
662
  end
695
663
 
696
664
  describe "Text::Box with more text than can fit in the box" do
697
665
  before(:each) do
698
- create_pdf
666
+ create_pdf
699
667
  @text = "Oh hai text rect. " * 30
700
668
  @bounding_height = 162.0
701
669
  @options = {
@@ -704,7 +672,7 @@ describe "Text::Box with more text than can fit in the box" do
704
672
  :document => @pdf
705
673
  }
706
674
  end
707
-
675
+
708
676
  context "truncated overflow" do
709
677
  before(:each) do
710
678
  @options[:overflow] = :truncate
@@ -712,18 +680,18 @@ describe "Text::Box with more text than can fit in the box" do
712
680
  end
713
681
  it "should be truncated" do
714
682
  @text_box.render
715
- @text_box.text.gsub("\n", " ").should_not == @text.strip
683
+ @text_box.text.gsub("\n", " ").should.not == @text.strip
716
684
  end
717
685
  it "render should not return an empty string because some text remains unprinted" do
718
- @text_box.render.should_not be_empty
686
+ @text_box.render.should.not.be.empty
719
687
  end
720
688
  it "#height should be no taller than the specified height" do
721
689
  @text_box.render
722
- @text_box.height.should be <= @bounding_height
690
+ @text_box.height.should.be <= @bounding_height
723
691
  end
724
692
  it "#height should be within one font height of the specified height" do
725
693
  @text_box.render
726
- @bounding_height.should be_within(@pdf.font.height).of(@text_box.height)
694
+ @bounding_height.should.be.close(@text_box.height, @pdf.font.height)
727
695
  end
728
696
  context "with :rotate option" do
729
697
  it "unrendered text should be the same as when not rotated" do
@@ -742,7 +710,7 @@ describe "Text::Box with more text than can fit in the box" do
742
710
  end
743
711
  end
744
712
  end
745
-
713
+
746
714
  context "truncated with text and size taken from the manual" do
747
715
  it "should return the right text" do
748
716
  @text = "This is the beginning of the text. It will be cut somewhere and " +
@@ -832,7 +800,7 @@ describe "Text::Box with a solid block of Chinese characters" do
832
800
  :height => 162.0,
833
801
  :document => @pdf
834
802
  }
835
- @pdf.font "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
803
+ @pdf.font "#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf"
836
804
  @options[:overflow] = :truncate
837
805
  text_box = Prawn::Text::Box.new(@text, @options)
838
806
  text_box.render
@@ -841,8 +809,8 @@ describe "Text::Box with a solid block of Chinese characters" do
841
809
  end
842
810
 
843
811
 
844
- describe "drawing bounding boxes" do
845
- before(:each) { create_pdf }
812
+ describe "drawing bounding boxes" do
813
+ before(:each) { create_pdf }
846
814
 
847
815
  it "should restore the margin box when bounding box exits" do
848
816
  margin_box = @pdf.bounds
@@ -877,7 +845,7 @@ describe "Text::Box#render with :character_spacing option" do
877
845
  text_box.text.should == "hello\nworld"
878
846
  end
879
847
  end
880
-
848
+
881
849
  describe "Text::Box wrapping" do
882
850
  before(:each) do
883
851
  create_pdf
@@ -992,7 +960,7 @@ describe "Text::Box wrapping" do
992
960
 
993
961
  expected = "©" * 25 + "\n" + "©" * 5
994
962
  @pdf.font.normalize_encoding!(expected)
995
- expected = expected.force_encoding(Encoding::UTF_8)
963
+ expected = expected.force_encoding("utf-8") if expected.respond_to?(:force_encoding)
996
964
  text_box.text.should == expected
997
965
  end
998
966
 
@@ -1009,7 +977,7 @@ describe "Text::Box wrapping" do
1009
977
  text_box.render
1010
978
  results_without_accent = text_box.text
1011
979
 
1012
- first_line(results_with_accent).length.should == first_line(results_without_accent).length
980
+ results_with_accent.first_line.length.should == results_without_accent.first_line.length
1013
981
  end
1014
982
  end
1015
983
 
@@ -1028,7 +996,3 @@ end
1028
996
  def reduce_precision(float)
1029
997
  ("%.5f" % float).to_f
1030
998
  end
1031
-
1032
- def first_line(str)
1033
- str.each_line { |line| return line }
1034
- end