prawn 1.1.0 → 2.4.0

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