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,45 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
-
5
- describe "#text_rendering_mode" do
6
- it "should draw the text rendering mode to the document" do
7
- create_pdf
8
- @pdf.text_rendering_mode(:stroke) do
9
- @pdf.text("hello world")
10
- end
11
- contents = PDF::Inspector::Text.analyze(@pdf.render)
12
- expect(contents.text_rendering_mode.first).to eq(1)
13
- end
14
- it "should not draw the text rendering mode to the document" \
15
- " when the new mode matches the old" do
16
- create_pdf
17
- @pdf.text_rendering_mode(:fill) do
18
- @pdf.text("hello world")
19
- end
20
- contents = PDF::Inspector::Text.analyze(@pdf.render)
21
- expect(contents.text_rendering_mode).to be_empty
22
- end
23
- it "should restore character spacing to 0" do
24
- create_pdf
25
- @pdf.text_rendering_mode(:stroke) do
26
- @pdf.text("hello world")
27
- end
28
- contents = PDF::Inspector::Text.analyze(@pdf.render)
29
- expect(contents.text_rendering_mode).to eq([1, 0])
30
- end
31
- it "should function as an accessor when no parameter given" do
32
- create_pdf
33
- @pdf.text_rendering_mode(:fill_stroke) do
34
- @pdf.text("hello world")
35
- expect(@pdf.text_rendering_mode).to eq(:fill_stroke)
36
- end
37
- expect(@pdf.text_rendering_mode).to eq(:fill)
38
- end
39
- it "should raise_error an exception when passed an invalid mode" do
40
- create_pdf
41
- expect { @pdf.text_rendering_mode(-1) }.to raise_error(ArgumentError)
42
- expect { @pdf.text_rendering_mode(8) }.to raise_error(ArgumentError)
43
- expect { @pdf.text_rendering_mode(:flil) }.to raise_error(ArgumentError)
44
- end
45
- end
@@ -1,93 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
-
5
- describe "#character_spacing" do
6
- it "should draw the character spacing to the document" do
7
- create_pdf
8
- @pdf.character_spacing(10.555555) do
9
- @pdf.text("hello world")
10
- end
11
- contents = PDF::Inspector::Text.analyze(@pdf.render)
12
- expect(contents.character_spacing.first).to eq(10.5556)
13
- end
14
- it "should not draw the character spacing to the document" \
15
- " when the new character spacing matches the old" do
16
- create_pdf
17
- @pdf.character_spacing(0) do
18
- @pdf.text("hello world")
19
- end
20
- contents = PDF::Inspector::Text.analyze(@pdf.render)
21
- expect(contents.character_spacing).to be_empty
22
- end
23
- it "should restore character spacing to 0" do
24
- create_pdf
25
- @pdf.character_spacing(10.555555) do
26
- @pdf.text("hello world")
27
- end
28
- contents = PDF::Inspector::Text.analyze(@pdf.render)
29
- expect(contents.character_spacing.last).to eq(0)
30
- end
31
- it "should function as an accessor when no parameter given" do
32
- create_pdf
33
- @pdf.character_spacing(10.555555) do
34
- @pdf.text("hello world")
35
- expect(@pdf.character_spacing).to eq(10.555555)
36
- end
37
- expect(@pdf.character_spacing).to eq(0)
38
- end
39
-
40
- # ensure that we properly internationalize by using the number of characters
41
- # in a string, not the number of bytes, to insert character spaces
42
- #
43
- it "should calculate character spacing widths by characters, not bytes" do
44
- create_pdf
45
- @pdf.font("#{Prawn::DATADIR}/fonts/gkai00mp.ttf")
46
-
47
- str = "こんにちは世界"
48
- @pdf.character_spacing(0) do
49
- @raw_width = @pdf.width_of(str)
50
- end
51
-
52
- @pdf.character_spacing(10) do
53
- # the new width should include seven 10-pt character spaces.
54
- expect(@pdf.width_of(str)).to be_within(0.001).of(@raw_width + (10 * 7))
55
- end
56
- end
57
- end
58
-
59
- describe "#word_spacing" do
60
- it "should draw the word spacing to the document" do
61
- create_pdf
62
- @pdf.word_spacing(10.555555) do
63
- @pdf.text("hello world")
64
- end
65
- contents = PDF::Inspector::Text.analyze(@pdf.render)
66
- expect(contents.word_spacing.first).to eq(10.5556)
67
- end
68
- it "should draw the word spacing to the document" \
69
- " when the new word spacing matches the old" do
70
- create_pdf
71
- @pdf.word_spacing(0) do
72
- @pdf.text("hello world")
73
- end
74
- contents = PDF::Inspector::Text.analyze(@pdf.render)
75
- expect(contents.word_spacing).to be_empty
76
- end
77
- it "should restore word spacing to 0" do
78
- create_pdf
79
- @pdf.word_spacing(10.555555) do
80
- @pdf.text("hello world")
81
- end
82
- contents = PDF::Inspector::Text.analyze(@pdf.render)
83
- expect(contents.word_spacing.last).to eq(0)
84
- end
85
- it "should function as an accessor when no parameter given" do
86
- create_pdf
87
- @pdf.word_spacing(10.555555) do
88
- @pdf.text("hello world")
89
- expect(@pdf.word_spacing).to eq(10.555555)
90
- end
91
- expect(@pdf.word_spacing).to eq(0)
92
- end
93
- end
@@ -1,543 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
-
5
- describe "Prawn::Text::NBSP" do
6
- it "should be defined" do
7
- expect(Prawn::Text::NBSP).to eq(" ")
8
- end
9
- end
10
-
11
- describe "#height_of" do
12
- before(:each) { create_pdf }
13
-
14
- it "should return the height that would be required to print a" \
15
- "particular string of text" do
16
- original_y = @pdf.y
17
- @pdf.text("Foo")
18
- new_y = @pdf.y
19
- expect(@pdf.height_of("Foo")).to be_within(0.0001).of(original_y - new_y)
20
- end
21
-
22
- it "should omit the gap below the last descender if :final_gap => false " \
23
- "is given" do
24
- original_y = @pdf.y
25
- @pdf.text("Foo", :final_gap => false)
26
- new_y = @pdf.y
27
- expect(@pdf.height_of("Foo", :final_gap => false)).to be_within(0.0001).of(original_y - new_y)
28
- end
29
-
30
- it "should raise_error CannotFit if a too-small width is given" do
31
- expect do
32
- @pdf.height_of("text", :width => 1)
33
- end.to raise_error(Prawn::Errors::CannotFit)
34
- end
35
-
36
- it "should raise_error NotImplementedError if :indent_paragraphs option is provided" do
37
- expect {
38
- @pdf.height_of("hai", :width => 300,
39
- :indent_paragraphs => 60)
40
- }.to raise_error(NotImplementedError)
41
- end
42
-
43
- it "should_not raise_error Prawn::Errors::UnknownOption if :final_gap option is provided" do
44
- @pdf.height_of("hai", :width => 300, :final_gap => true)
45
- end
46
- end
47
-
48
- describe "#text" do
49
- before(:each) { create_pdf }
50
-
51
- it "should not fail when @output is nil when PDF::Core::Text::LineWrap#finalize_line is called" do
52
- # need a document with margins for these particulars to produce the
53
- # condition that was throwing the error
54
- pdf = Prawn::Document.new
55
- pdf.text "transparency " * 150, :size => 18
56
- end
57
-
58
- it "should allow drawing empty strings to the page" do
59
- @pdf.text " "
60
- text = PDF::Inspector::Text.analyze(@pdf.render)
61
- # If anything is rendered to the page, it should be whitespace.
62
- text.strings.each { |str| expect(str).to match(/\A\s*\z/) }
63
- end
64
-
65
- it "should ignore call when string is nil" do
66
- expect(@pdf.text(nil)).to eq false
67
- end
68
-
69
- it "should correctly render empty paragraphs" do
70
- @pdf.text "text\n\ntext"
71
- text = PDF::Inspector::Text.analyze(@pdf.render)
72
- expect(@pdf.page_count).to eq(1)
73
- expect(text.strings.reject(&:empty?)).to eq(["text", "text"])
74
- end
75
-
76
- it "should correctly render empty paragraphs with :indent_paragraphs" do
77
- @pdf.text "text\n\ntext", :indent_paragraphs => 5
78
- text = PDF::Inspector::Text.analyze(@pdf.render)
79
- expect(@pdf.page_count).to eq(1)
80
- expect(text.strings.reject(&:empty?)).to eq(["text", "text"])
81
- end
82
-
83
- it "should correctly render strings ending with empty paragraphs and " \
84
- ":inline_format and :indent_paragraphs" do
85
- @pdf.text "text\n\n", :inline_format => true, :indent_paragraphs => 5
86
- text = PDF::Inspector::Text.analyze(@pdf.render)
87
- expect(@pdf.page_count).to eq(1)
88
- expect(text.strings).to eq(["text"])
89
- end
90
-
91
- it "should default to use kerning information" do
92
- @pdf.text "hello world"
93
- text = PDF::Inspector::Text.analyze(@pdf.render)
94
- expect(text.kerned[0]).to eq true
95
- end
96
-
97
- it "should be able to disable kerning with an option" do
98
- @pdf.text "hello world", :kerning => false
99
- text = PDF::Inspector::Text.analyze(@pdf.render)
100
- expect(text.kerned[0]).to eq false
101
- end
102
-
103
- it "should be able to disable kerning document-wide" do
104
- @pdf.default_kerning(false)
105
- @pdf.default_kerning = false
106
- @pdf.text "hello world"
107
- text = PDF::Inspector::Text.analyze(@pdf.render)
108
- expect(text.kerned[0]).to eq false
109
- end
110
-
111
- it "option should be able to override document-wide kerning disabling" do
112
- @pdf.default_kerning = false
113
- @pdf.text "hello world", :kerning => true
114
- text = PDF::Inspector::Text.analyze(@pdf.render)
115
- expect(text.kerned[0]).to eq true
116
- end
117
-
118
- it "should raise_error ArgumentError if :at option included" do
119
- expect { @pdf.text("hai", :at => [0, 0]) }.to raise_error(ArgumentError)
120
- end
121
-
122
- it "should advance down the document based on font_height" do
123
- position = @pdf.y
124
- @pdf.text "Foo"
125
-
126
- expect(@pdf.y).to be_within(0.0001).of(position - @pdf.font.height)
127
-
128
- position = @pdf.y
129
- @pdf.text "Foo\nBar\nBaz"
130
- expect(@pdf.y).to be_within(0.0001).of(position - 3 * @pdf.font.height)
131
- end
132
-
133
- it "should advance down the document based on font_height with size option" do
134
- position = @pdf.y
135
- @pdf.text "Foo", :size => 15
136
-
137
- @pdf.font_size = 15
138
- expect(@pdf.y).to be_within(0.0001).of(position - @pdf.font.height)
139
-
140
- position = @pdf.y
141
- @pdf.text "Foo\nBar\nBaz"
142
- expect(@pdf.y).to be_within(0.0001).of(position - 3 * @pdf.font.height)
143
- end
144
-
145
- it "should advance down the document based on font_height with leading option" do
146
- position = @pdf.y
147
- leading = 2
148
- @pdf.text "Foo", :leading => leading
149
-
150
- expect(@pdf.y).to be_within(0.0001).of(position - @pdf.font.height - leading)
151
-
152
- position = @pdf.y
153
- @pdf.text "Foo\nBar\nBaz"
154
- expect(@pdf.y).to be_within(0.0001).of(position - 3 * @pdf.font.height)
155
- end
156
-
157
- it "should advance only to the bottom of the final descender if final_gap is false" do
158
- position = @pdf.y
159
- @pdf.text "Foo", :final_gap => false
160
-
161
- expect(@pdf.y).to be_within(0.0001).of(position - @pdf.font.ascender - @pdf.font.descender)
162
-
163
- position = @pdf.y
164
- @pdf.text "Foo\nBar\nBaz", :final_gap => false
165
- expect(@pdf.y).to be_within(0.0001).of(position - 2 * @pdf.font.height - @pdf.font.ascender - @pdf.font.descender)
166
- end
167
-
168
- it "should be able to print text starting at the last line of a page" do
169
- @pdf.move_cursor_to(@pdf.font.height)
170
- @pdf.text("hello world")
171
- pages = PDF::Inspector::Page.analyze(@pdf.render).pages
172
- expect(pages.size).to eq(1)
173
- end
174
-
175
- it "should default to 12 point helvetica" do
176
- @pdf.text "Blah"
177
- text = PDF::Inspector::Text.analyze(@pdf.render)
178
- expect(text.font_settings[0][:name]).to eq(:Helvetica)
179
- expect(text.font_settings[0][:size]).to eq(12)
180
- expect(text.strings.first).to eq("Blah")
181
- end
182
-
183
- it "should allow setting font size" do
184
- @pdf.text "Blah", :size => 16
185
- text = PDF::Inspector::Text.analyze(@pdf.render)
186
- expect(text.font_settings[0][:size]).to eq(16)
187
- end
188
-
189
- it "should allow setting a default font size" do
190
- @pdf.font_size = 16
191
- @pdf.text "Blah"
192
- text = PDF::Inspector::Text.analyze(@pdf.render)
193
- expect(text.font_settings[0][:size]).to eq(16)
194
- end
195
-
196
- it "should allow overriding default font for a single instance" do
197
- @pdf.font_size = 16
198
-
199
- @pdf.text "Blah", :size => 11
200
- @pdf.text "Blaz"
201
- text = PDF::Inspector::Text.analyze(@pdf.render)
202
- expect(text.font_settings[0][:size]).to eq(11)
203
- expect(text.font_settings[1][:size]).to eq(16)
204
- end
205
-
206
- it "should allow setting a font size transaction with a block" do
207
- @pdf.font_size 16 do
208
- @pdf.text 'Blah'
209
- end
210
-
211
- @pdf.text 'blah'
212
-
213
- text = PDF::Inspector::Text.analyze(@pdf.render)
214
- expect(text.font_settings[0][:size]).to eq(16)
215
- expect(text.font_settings[1][:size]).to eq(12)
216
- end
217
-
218
- it "should allow manual setting the font size when in a font size block" do
219
- @pdf.font_size(16) do
220
- @pdf.text 'Foo'
221
- @pdf.text 'Blah', :size => 11
222
- @pdf.text 'Blaz'
223
- end
224
- text = PDF::Inspector::Text.analyze(@pdf.render)
225
- expect(text.font_settings[0][:size]).to eq(16)
226
- expect(text.font_settings[1][:size]).to eq(11)
227
- expect(text.font_settings[2][:size]).to eq(16)
228
- end
229
-
230
- it "should allow registering of built-in font_settings on the fly" do
231
- @pdf.font "Times-Roman"
232
- @pdf.text "Blah"
233
- @pdf.font "Courier"
234
- @pdf.text "Blaz"
235
- text = PDF::Inspector::Text.analyze(@pdf.render)
236
- expect(text.font_settings[0][:name]).to eq(:"Times-Roman")
237
- expect(text.font_settings[1][:name]).to eq(:Courier)
238
- end
239
-
240
- it "should utilise the same default font across multiple pages" do
241
- @pdf.text "Blah"
242
- @pdf.start_new_page
243
- @pdf.text "Blaz"
244
- text = PDF::Inspector::Text.analyze(@pdf.render)
245
-
246
- expect(text.font_settings.size).to eq(2)
247
- expect(text.font_settings[0][:name]).to eq(:Helvetica)
248
- expect(text.font_settings[1][:name]).to eq(:Helvetica)
249
- end
250
-
251
- it "should raise_error an exception when an unknown font is used" do
252
- expect { @pdf.font "Pao bu" }.to raise_error(Prawn::Errors::UnknownFont)
253
- end
254
-
255
- it "should_not raise_error an exception when providing Pathname instance as font" do
256
- @pdf.font Pathname.new("#{Prawn::DATADIR}/fonts/DejaVuSans.ttf")
257
- end
258
-
259
- it "should correctly render a utf-8 string when using a built-in font" do
260
- str = "©" # copyright symbol
261
- @pdf.text str
262
-
263
- # grab the text from the rendered PDF and ensure it matches
264
- text = PDF::Inspector::Text.analyze(@pdf.render)
265
- expect(text.strings.first).to eq(str)
266
- end
267
-
268
- it "should correctly render a utf-8 string when using a TTF font" do
269
- str = "©" # copyright symbol
270
- @pdf.font "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf"
271
- @pdf.text str
272
-
273
- # grab the text from the rendered PDF and ensure it matches
274
- text = PDF::Inspector::Text.analyze(@pdf.render)
275
- expect(text.strings.first).to eq(str)
276
- end
277
-
278
- it "subsets mixed low-ASCII and non-ASCII characters when they can be subsetted together" do
279
- str = "It’s super effective!"
280
- @pdf.font "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf"
281
- @pdf.text str
282
-
283
- text = PDF::Inspector::Text.analyze(@pdf.render)
284
- expect(text.strings.first).to eq(str)
285
- end
286
-
287
- it "should correctly render a string with higher bit characters across a page break when using a built-in font" do
288
- str = "©"
289
- @pdf.move_cursor_to(@pdf.font.height)
290
- @pdf.text(str + "\n" + str)
291
-
292
- pages = PDF::Inspector::Page.analyze(@pdf.render).pages
293
- expect(pages.size).to eq(2)
294
- expect(pages[0][:strings]).to eq([str])
295
- expect(pages[1][:strings]).to eq([str])
296
- end
297
-
298
- it "should correctly render a string with higher bit characters across" \
299
- " a page break when using a built-in font and :indent_paragraphs option" do
300
- str = "©"
301
- @pdf.move_cursor_to(@pdf.font.height)
302
- @pdf.text(str + "\n" + str, :indent_paragraphs => 20)
303
-
304
- pages = PDF::Inspector::Page.analyze(@pdf.render).pages
305
- expect(pages.size).to eq(2)
306
- expect(pages[0][:strings]).to eq([str])
307
- expect(pages[1][:strings]).to eq([str])
308
- end
309
-
310
- it "should raise_error an exception when a utf-8 incompatible string is rendered" do
311
- str = "Blah \xDD"
312
- expect { @pdf.text str }.to raise_error(
313
- Prawn::Errors::IncompatibleStringEncoding)
314
- end
315
-
316
- it "should_not raise_error an exception when a shift-jis string is rendered" do
317
- datafile = "#{Prawn::DATADIR}/shift_jis_text.txt"
318
- sjis_str = File.open(datafile, "r:shift_jis") { |f| f.gets }
319
- @pdf.font("#{Prawn::DATADIR}/fonts/gkai00mp.ttf")
320
-
321
- # Expect that the call to text will not raise an encoding error
322
- @pdf.text(sjis_str)
323
- end
324
-
325
- it "should call move_past_bottom when printing more text than can fit" \
326
- " between the current document.y and bounds.bottom" do
327
- @pdf.y = @pdf.font.height
328
- @pdf.text "Hello"
329
- @pdf.text "World"
330
- pages = PDF::Inspector::Page.analyze(@pdf.render).pages
331
- expect(pages.size).to eq(2)
332
- expect(pages[0][:strings]).to eq(["Hello"])
333
- expect(pages[1][:strings]).to eq(["World"])
334
- end
335
-
336
- describe "with :indent_paragraphs option" do
337
- it "should indent the paragraphs" do
338
- hello = "hello " * 50
339
- hello2 = "hello " * 50
340
- @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
341
- text = PDF::Inspector::Text.analyze(@pdf.render)
342
- expect(text.strings[0]).to eq(("hello " * 19).strip)
343
- expect(text.strings[1]).to eq(("hello " * 21).strip)
344
- expect(text.strings[3]).to eq(("hello " * 19).strip)
345
- expect(text.strings[4]).to eq(("hello " * 21).strip)
346
- end
347
-
348
- it "should indent from right side when using :rtl direction" do
349
- para1 = "The rain in spain falls mainly on the plains " * 3
350
- para2 = "The rain in spain falls mainly on the plains " * 3
351
-
352
- @pdf.text(para1 + "\n" + para2, :indent_paragraphs => 60, :direction => :rtl)
353
-
354
- text = PDF::Inspector::Text.analyze(@pdf.render)
355
-
356
- lines = text.strings
357
- x_positions = text.positions.map { |e| e[0] }
358
-
359
- # NOTE: The code below reflects Prawn's current kerning behavior for RTL
360
- # text, which isn't necessarily correct. If we change that behavior,
361
- # this test will need to be updated.
362
-
363
- expect(x_positions[0]).to(
364
- be_within(0.001).of(@pdf.bounds.absolute_right - 60 -
365
- @pdf.width_of(lines[0].reverse, :kerning => true)))
366
-
367
- expect(x_positions[1]).to(
368
- be_within(0.001).of(@pdf.bounds.absolute_right -
369
- @pdf.width_of(lines[1].reverse, :kerning => true)))
370
-
371
- expect(x_positions[2]).to(
372
- be_within(0.001).of(@pdf.bounds.absolute_right - 60 -
373
- @pdf.width_of(lines[2].reverse, :kerning => true)))
374
-
375
- expect(x_positions[3]).to(
376
- be_within(0.001).of(@pdf.bounds.absolute_right -
377
- @pdf.width_of(lines[3].reverse, :kerning => true)))
378
- end
379
-
380
- it "should indent from right side when document has :rtl direction" do
381
- para1 = "The rain in spain falls mainly on the plains " * 3
382
- para2 = "The rain in spain falls mainly on the plains " * 3
383
-
384
- @pdf.text_direction = :rtl
385
- @pdf.text(para1 + "\n" + para2, :indent_paragraphs => 60)
386
-
387
- text = PDF::Inspector::Text.analyze(@pdf.render)
388
-
389
- lines = text.strings
390
- x_positions = text.positions.map { |e| e[0] }
391
-
392
- # NOTE: The code below reflects Prawn's current kerning behavior for RTL
393
- # text, which isn't necessarily correct. If we change that behavior,
394
- # this test will need to be updated.
395
-
396
- expect(x_positions[0]).to(
397
- be_within(0.001).of(@pdf.bounds.absolute_right - 60 -
398
- @pdf.width_of(lines[0].reverse, :kerning => true)))
399
-
400
- expect(x_positions[1]).to(
401
- be_within(0.001).of(@pdf.bounds.absolute_right -
402
- @pdf.width_of(lines[1].reverse, :kerning => true)))
403
-
404
- expect(x_positions[2]).to(
405
- be_within(0.001).of(@pdf.bounds.absolute_right - 60 -
406
- @pdf.width_of(lines[2].reverse, :kerning => true)))
407
-
408
- expect(x_positions[3]).to(
409
- be_within(0.001).of(@pdf.bounds.absolute_right -
410
- @pdf.width_of(lines[3].reverse, :kerning => true)))
411
- end
412
-
413
- it "should indent from left side when using :ltr direction" do
414
- para1 = "The rain in spain falls mainly on the plains " * 3
415
- para2 = "The rain in spain falls mainly on the plains " * 3
416
-
417
- @pdf.text(para1 + "\n" + para2, :indent_paragraphs => 60, :direction => :ltr)
418
-
419
- text = PDF::Inspector::Text.analyze(@pdf.render)
420
-
421
- x_positions = text.positions.map { |e| e[0] }
422
-
423
- expect(x_positions[0]).to eq(60)
424
- expect(x_positions[1]).to eq(0)
425
-
426
- expect(x_positions[2]).to eq(60)
427
- expect(x_positions[3]).to eq(0)
428
- end
429
-
430
- it "should indent from left side when document has :ltr direction" do
431
- para1 = "The rain in spain falls mainly on the plains " * 3
432
- para2 = "The rain in spain falls mainly on the plains " * 3
433
-
434
- @pdf.text_direction = :ltr
435
- @pdf.text(para1 + "\n" + para2, :indent_paragraphs => 60)
436
-
437
- text = PDF::Inspector::Text.analyze(@pdf.render)
438
-
439
- x_positions = text.positions.map { |e| e[0] }
440
-
441
- expect(x_positions[0]).to eq(60)
442
- expect(x_positions[1]).to eq(0)
443
-
444
- expect(x_positions[2]).to eq(60)
445
- expect(x_positions[3]).to eq(0)
446
- end
447
-
448
- describe "when wrap to new page, and first line of new page" \
449
- " is not the start of a new paragraph, that line should" \
450
- " not be indented" do
451
- it "should indent the paragraphs" do
452
- hello = "hello " * 50
453
- hello2 = "hello " * 50
454
- @pdf.move_cursor_to(@pdf.font.height)
455
- @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
456
- text = PDF::Inspector::Text.analyze(@pdf.render)
457
- expect(text.strings[0]).to eq(("hello " * 19).strip)
458
- expect(text.strings[1]).to eq(("hello " * 21).strip)
459
- expect(text.strings[3]).to eq(("hello " * 19).strip)
460
- expect(text.strings[4]).to eq(("hello " * 21).strip)
461
- end
462
- end
463
- describe "when wrap to new page, and first line of new page" \
464
- " is the start of a new paragraph, that line should" \
465
- " be indented" do
466
- it "should indent the paragraphs" do
467
- hello = "hello " * 50
468
- hello2 = "hello " * 50
469
- @pdf.move_cursor_to(@pdf.font.height * 3)
470
- @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
471
- text = PDF::Inspector::Text.analyze(@pdf.render)
472
- expect(text.strings[0]).to eq(("hello " * 19).strip)
473
- expect(text.strings[1]).to eq(("hello " * 21).strip)
474
- expect(text.strings[3]).to eq(("hello " * 19).strip)
475
- expect(text.strings[4]).to eq(("hello " * 21).strip)
476
- end
477
- end
478
- end
479
-
480
- describe "kerning" do
481
- it "should respect text kerning setting (document default)" do
482
- create_pdf
483
- expect(@pdf.font).to receive(:compute_width_of).with("VAT", hash_including(kerning: true)).and_return(10)
484
- @pdf.text "VAT"
485
- end
486
-
487
- it "should respect text kerning setting (kerning=true)" do
488
- create_pdf
489
- expect(@pdf.font).to receive(:compute_width_of).with("VAT", hash_including(kerning: true)).at_least(:once).and_return(10)
490
- @pdf.text "VAT", :kerning => true
491
- end
492
-
493
- it "should respect text kerning setting (kerning=false)" do
494
- create_pdf
495
- expect(@pdf.font).to receive(:compute_width_of).with("VAT", hash_including(kerning: false)).at_least(:once).and_return(10)
496
- @pdf.text "VAT", :kerning => false
497
- end
498
- end
499
-
500
- describe "#shrink_to_fit with special utf-8 text" do
501
- it "Should not throw an exception", :unresolved, :issue => 603 do
502
- pages = 0
503
- doc = Prawn::Document.new(page_size: 'A4', margin: [2, 2, 2, 2]) do |pdf|
504
- add_unicode_fonts(pdf)
505
- pdf.bounding_box([1, 1], :width => 90, :height => 50) do
506
- broken_text = " Sample Text\nSAMPLE SAMPLE SAMPLEoddělení ZMĚN\nSAMPLE"
507
- pdf.text broken_text, :overflow => :shrink_to_fit
508
- end
509
- end
510
- end
511
- end
512
-
513
- def add_unicode_fonts(pdf)
514
- dejavu = "#{::Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
515
- pdf.font_families.update(
516
- "dejavu" => {
517
- :normal => dejavu,
518
- :italic => dejavu,
519
- :bold => dejavu,
520
- :bold_italic => dejavu
521
- }
522
- )
523
- pdf.fallback_fonts = ["dejavu"]
524
- end
525
-
526
- describe "fallback_fonts" do
527
- it "should preserve font style" do
528
- create_pdf
529
-
530
- @pdf.fallback_fonts ["Helvetica"]
531
- @pdf.font "Times-Roman", :style => :italic do
532
- @pdf.text "hello"
533
- end
534
-
535
- text = PDF::Inspector::Text.analyze(@pdf.render)
536
- fonts_used = text.font_settings.map { |e| e[:name] }
537
-
538
- expect(fonts_used.length).to eq(1)
539
- expect(fonts_used[0]).to eq(:"Times-Italic")
540
- expect(text.strings[0]).to eq("hello")
541
- end
542
- end
543
- end