prawn 2.1.0 → 2.2.0

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