piglop-prawn 0.10.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (276) hide show
  1. data/COPYING +340 -0
  2. data/HACKING +49 -0
  3. data/LICENSE +56 -0
  4. data/README +140 -0
  5. data/Rakefile +52 -0
  6. data/data/encodings/win_ansi.txt +29 -0
  7. data/data/fonts/Action Man.dfont +0 -0
  8. data/data/fonts/Activa.ttf +0 -0
  9. data/data/fonts/Chalkboard.ttf +0 -0
  10. data/data/fonts/Courier-Bold.afm +342 -0
  11. data/data/fonts/Courier-BoldOblique.afm +342 -0
  12. data/data/fonts/Courier-Oblique.afm +342 -0
  13. data/data/fonts/Courier.afm +342 -0
  14. data/data/fonts/DejaVuSans.ttf +0 -0
  15. data/data/fonts/Dustismo_Roman.ttf +0 -0
  16. data/data/fonts/Helvetica-Bold.afm +2827 -0
  17. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  18. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  19. data/data/fonts/Helvetica.afm +3051 -0
  20. data/data/fonts/MustRead.html +19 -0
  21. data/data/fonts/Symbol.afm +213 -0
  22. data/data/fonts/Times-Bold.afm +2588 -0
  23. data/data/fonts/Times-BoldItalic.afm +2384 -0
  24. data/data/fonts/Times-Italic.afm +2667 -0
  25. data/data/fonts/Times-Roman.afm +2419 -0
  26. data/data/fonts/ZapfDingbats.afm +225 -0
  27. data/data/fonts/comicsans.ttf +0 -0
  28. data/data/fonts/gkai00mp.ttf +0 -0
  29. data/data/images/16bit.alpha +0 -0
  30. data/data/images/16bit.dat +0 -0
  31. data/data/images/16bit.png +0 -0
  32. data/data/images/arrow.png +0 -0
  33. data/data/images/arrow2.png +0 -0
  34. data/data/images/barcode_issue.png +0 -0
  35. data/data/images/dice.alpha +0 -0
  36. data/data/images/dice.dat +0 -0
  37. data/data/images/dice.png +0 -0
  38. data/data/images/dice_interlaced.png +0 -0
  39. data/data/images/fractal.jpg +0 -0
  40. data/data/images/letterhead.jpg +0 -0
  41. data/data/images/page_white_text.alpha +0 -0
  42. data/data/images/page_white_text.dat +0 -0
  43. data/data/images/page_white_text.png +0 -0
  44. data/data/images/pigs.jpg +0 -0
  45. data/data/images/rails.dat +0 -0
  46. data/data/images/rails.png +0 -0
  47. data/data/images/ruport.png +0 -0
  48. data/data/images/ruport_data.dat +0 -0
  49. data/data/images/ruport_transparent.png +0 -0
  50. data/data/images/ruport_type0.png +0 -0
  51. data/data/images/stef.jpg +0 -0
  52. data/data/images/tru256.bmp +0 -0
  53. data/data/images/web-links.dat +1 -0
  54. data/data/images/web-links.png +0 -0
  55. data/data/pdfs/complex_template.pdf +0 -0
  56. data/data/pdfs/contains_ttf_font.pdf +0 -0
  57. data/data/pdfs/encrypted.pdf +0 -0
  58. data/data/pdfs/hexagon.pdf +61 -0
  59. data/data/pdfs/indirect_reference.pdf +86 -0
  60. data/data/pdfs/nested_pages.pdf +118 -0
  61. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  62. data/data/pdfs/two_hexagons.pdf +90 -0
  63. data/data/pdfs/version_1_6.pdf +61 -0
  64. data/data/shift_jis_text.txt +1 -0
  65. data/examples/bounding_box/bounding_boxes.rb +43 -0
  66. data/examples/bounding_box/indentation.rb +34 -0
  67. data/examples/bounding_box/russian_boxes.rb +36 -0
  68. data/examples/bounding_box/stretched_nesting.rb +67 -0
  69. data/examples/example_helper.rb +4 -0
  70. data/examples/general/background.rb +23 -0
  71. data/examples/general/canvas.rb +15 -0
  72. data/examples/general/context_sensitive_headers.rb +37 -0
  73. data/examples/general/float.rb +11 -0
  74. data/examples/general/margin.rb +36 -0
  75. data/examples/general/measurement_units.rb +51 -0
  76. data/examples/general/metadata-info.rb +16 -0
  77. data/examples/general/multi_page_layout.rb +18 -0
  78. data/examples/general/outlines.rb +50 -0
  79. data/examples/general/page_geometry.rb +31 -0
  80. data/examples/general/page_numbering.rb +15 -0
  81. data/examples/general/repeaters.rb +47 -0
  82. data/examples/general/stamp.rb +41 -0
  83. data/examples/general/templates.rb +13 -0
  84. data/examples/graphics/basic_images.rb +23 -0
  85. data/examples/graphics/cmyk.rb +12 -0
  86. data/examples/graphics/curves.rb +11 -0
  87. data/examples/graphics/hexagon.rb +13 -0
  88. data/examples/graphics/image_fit.rb +15 -0
  89. data/examples/graphics/image_flow.rb +37 -0
  90. data/examples/graphics/image_position.rb +17 -0
  91. data/examples/graphics/line.rb +32 -0
  92. data/examples/graphics/png_types.rb +22 -0
  93. data/examples/graphics/polygons.rb +16 -0
  94. data/examples/graphics/remote_images.rb +12 -0
  95. data/examples/graphics/rounded_polygons.rb +19 -0
  96. data/examples/graphics/rounded_rectangle.rb +20 -0
  97. data/examples/graphics/ruport_style_helpers.rb +19 -0
  98. data/examples/graphics/stroke_bounds.rb +20 -0
  99. data/examples/graphics/stroke_cap_and_join.rb +45 -0
  100. data/examples/graphics/stroke_dash.rb +42 -0
  101. data/examples/graphics/transformations.rb +52 -0
  102. data/examples/graphics/transparency.rb +26 -0
  103. data/examples/m17n/chinese_text_wrapping.rb +17 -0
  104. data/examples/m17n/euro.rb +15 -0
  105. data/examples/m17n/sjis.rb +28 -0
  106. data/examples/m17n/utf8.rb +13 -0
  107. data/examples/m17n/win_ansi_charset.rb +54 -0
  108. data/examples/security/hello_foo.rb +8 -0
  109. data/examples/table/bill.rb +53 -0
  110. data/examples/table/cell.rb +12 -0
  111. data/examples/table/checkerboard.rb +22 -0
  112. data/examples/table/header.rb +14 -0
  113. data/examples/table/inline_format_table.rb +12 -0
  114. data/examples/table/multi_page_table.rb +9 -0
  115. data/examples/table/simple_table.rb +24 -0
  116. data/examples/table/subtable.rb +12 -0
  117. data/examples/table/widths.rb +20 -0
  118. data/examples/text/alignment.rb +18 -0
  119. data/examples/text/dfont.rb +48 -0
  120. data/examples/text/family_based_styling.rb +24 -0
  121. data/examples/text/font_calculations.rb +91 -0
  122. data/examples/text/font_size.rb +33 -0
  123. data/examples/text/hyphenation.rb +45 -0
  124. data/examples/text/indent_paragraphs.rb +18 -0
  125. data/examples/text/inline_format.rb +72 -0
  126. data/examples/text/kerning.rb +30 -0
  127. data/examples/text/rotated.rb +98 -0
  128. data/examples/text/shaped_text_box.rb +31 -0
  129. data/examples/text/simple_text.rb +17 -0
  130. data/examples/text/simple_text_ttf.rb +17 -0
  131. data/examples/text/text_box.rb +88 -0
  132. data/examples/text/text_box_returning_excess.rb +51 -0
  133. data/examples/text/text_flow.rb +67 -0
  134. data/lib/prawn/compatibility.rb +51 -0
  135. data/lib/prawn/core/annotations.rb +61 -0
  136. data/lib/prawn/core/byte_string.rb +9 -0
  137. data/lib/prawn/core/destinations.rb +90 -0
  138. data/lib/prawn/core/document_state.rb +84 -0
  139. data/lib/prawn/core/literal_string.rb +16 -0
  140. data/lib/prawn/core/name_tree.rb +165 -0
  141. data/lib/prawn/core/object_store.rb +219 -0
  142. data/lib/prawn/core/page.rb +179 -0
  143. data/lib/prawn/core/pdf_object.rb +83 -0
  144. data/lib/prawn/core/reference.rb +112 -0
  145. data/lib/prawn/core/text/formatted/arranger.rb +260 -0
  146. data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
  147. data/lib/prawn/core/text/formatted/wrap.rb +113 -0
  148. data/lib/prawn/core/text/line_wrap.rb +198 -0
  149. data/lib/prawn/core/text/wrap.rb +80 -0
  150. data/lib/prawn/core/text.rb +115 -0
  151. data/lib/prawn/core.rb +89 -0
  152. data/lib/prawn/document/bounding_box.rb +425 -0
  153. data/lib/prawn/document/graphics_state.rb +48 -0
  154. data/lib/prawn/document/internals.rb +176 -0
  155. data/lib/prawn/document/page_geometry.rb +136 -0
  156. data/lib/prawn/document/snapshot.rb +87 -0
  157. data/lib/prawn/document.rb +573 -0
  158. data/lib/prawn/encoding.rb +121 -0
  159. data/lib/prawn/errors.rb +94 -0
  160. data/lib/prawn/font/afm.rb +225 -0
  161. data/lib/prawn/font/dfont.rb +42 -0
  162. data/lib/prawn/font/ttf.rb +350 -0
  163. data/lib/prawn/font.rb +342 -0
  164. data/lib/prawn/graphics/cap_style.rb +38 -0
  165. data/lib/prawn/graphics/color.rb +205 -0
  166. data/lib/prawn/graphics/dash.rb +71 -0
  167. data/lib/prawn/graphics/join_style.rb +38 -0
  168. data/lib/prawn/graphics/transformation.rb +156 -0
  169. data/lib/prawn/graphics/transparency.rb +99 -0
  170. data/lib/prawn/graphics.rb +321 -0
  171. data/lib/prawn/images/jpg.rb +46 -0
  172. data/lib/prawn/images/png.rb +226 -0
  173. data/lib/prawn/images.rb +348 -0
  174. data/lib/prawn/measurement_extensions.rb +46 -0
  175. data/lib/prawn/measurements.rb +71 -0
  176. data/lib/prawn/outline.rb +278 -0
  177. data/lib/prawn/repeater.rb +129 -0
  178. data/lib/prawn/security/arcfour.rb +51 -0
  179. data/lib/prawn/security.rb +262 -0
  180. data/lib/prawn/stamp.rb +126 -0
  181. data/lib/prawn/table/accessors.rb +180 -0
  182. data/lib/prawn/table/cell/in_table.rb +27 -0
  183. data/lib/prawn/table/cell/subtable.rb +65 -0
  184. data/lib/prawn/table/cell/text.rb +128 -0
  185. data/lib/prawn/table/cell.rb +330 -0
  186. data/lib/prawn/table.rb +421 -0
  187. data/lib/prawn/text/box.rb +380 -0
  188. data/lib/prawn/text/formatted/box.rb +215 -0
  189. data/lib/prawn/text/formatted/fragment.rb +127 -0
  190. data/lib/prawn/text/formatted/parser.rb +202 -0
  191. data/lib/prawn/text/formatted.rb +4 -0
  192. data/lib/prawn/text.rb +442 -0
  193. data/lib/prawn.rb +25 -0
  194. data/spec/annotations_spec.rb +90 -0
  195. data/spec/bounding_box_spec.rb +190 -0
  196. data/spec/cell_spec.rb +337 -0
  197. data/spec/destinations_spec.rb +15 -0
  198. data/spec/document_spec.rb +461 -0
  199. data/spec/font_spec.rb +314 -0
  200. data/spec/formatted_text_arranger_spec.rb +400 -0
  201. data/spec/formatted_text_box_spec.rb +659 -0
  202. data/spec/formatted_text_fragment_spec.rb +211 -0
  203. data/spec/graphics_spec.rb +446 -0
  204. data/spec/images_spec.rb +96 -0
  205. data/spec/inline_formatted_text_parser_spec.rb +446 -0
  206. data/spec/jpg_spec.rb +25 -0
  207. data/spec/line_wrap_spec.rb +341 -0
  208. data/spec/measurement_units_spec.rb +23 -0
  209. data/spec/name_tree_spec.rb +112 -0
  210. data/spec/object_store_spec.rb +160 -0
  211. data/spec/outline_spec.rb +229 -0
  212. data/spec/pdf_object_spec.rb +127 -0
  213. data/spec/png_spec.rb +237 -0
  214. data/spec/reference_spec.rb +82 -0
  215. data/spec/repeater_spec.rb +96 -0
  216. data/spec/security_spec.rb +120 -0
  217. data/spec/snapshot_spec.rb +138 -0
  218. data/spec/spec_helper.rb +26 -0
  219. data/spec/stamp_spec.rb +108 -0
  220. data/spec/stroke_styles_spec.rb +163 -0
  221. data/spec/table_spec.rb +583 -0
  222. data/spec/template_spec.rb +158 -0
  223. data/spec/text_at_spec.rb +119 -0
  224. data/spec/text_box_spec.rb +675 -0
  225. data/spec/text_spacing_spec.rb +57 -0
  226. data/spec/text_spec.rb +328 -0
  227. data/spec/text_with_inline_formatting_spec.rb +193 -0
  228. data/spec/transparency_spec.rb +75 -0
  229. data/vendor/pdf-inspector/README +18 -0
  230. data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
  231. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
  232. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
  233. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
  234. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
  235. data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
  236. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  237. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  238. data/vendor/ttfunk/example.rb +45 -0
  239. data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
  240. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
  241. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
  242. data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
  243. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
  244. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
  245. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
  246. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
  247. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
  248. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
  249. data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
  250. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
  251. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
  252. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
  253. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
  254. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
  255. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
  256. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
  257. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
  258. data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
  259. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
  260. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
  261. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
  262. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
  263. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
  264. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
  265. data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
  266. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
  267. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
  268. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
  269. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
  270. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
  271. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
  272. data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
  273. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
  274. data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
  275. data/vendor/ttfunk/lib/ttfunk.rb +102 -0
  276. metadata +364 -0
@@ -0,0 +1,57 @@
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
+ contents.character_spacing[0].should == 10.556
13
+ end
14
+ it "should restore character spacing to 0" do
15
+ create_pdf
16
+ @pdf.character_spacing(10.555555) do
17
+ @pdf.text("hello world")
18
+ end
19
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
20
+ contents.character_spacing[1].should == 0
21
+ end
22
+ it "should function as an accessor when no parameter given" do
23
+ create_pdf
24
+ @pdf.character_spacing(10.555555) do
25
+ @pdf.text("hello world")
26
+ @pdf.character_spacing.should == 10.555555
27
+ end
28
+ @pdf.character_spacing.should == 0
29
+ end
30
+ end
31
+
32
+ describe "#word_spacing" do
33
+ it "should draw the word spacing to the document" do
34
+ create_pdf
35
+ @pdf.word_spacing(10.555555) do
36
+ @pdf.text("hello world")
37
+ end
38
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
39
+ contents.word_spacing[0].should == 10.556
40
+ end
41
+ it "should restore word spacing to 0" do
42
+ create_pdf
43
+ @pdf.word_spacing(10.555555) do
44
+ @pdf.text("hello world")
45
+ end
46
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
47
+ contents.word_spacing[1].should == 0
48
+ end
49
+ it "should function as an accessor when no parameter given" do
50
+ create_pdf
51
+ @pdf.word_spacing(10.555555) do
52
+ @pdf.text("hello world")
53
+ @pdf.word_spacing.should == 10.555555
54
+ end
55
+ @pdf.word_spacing.should == 0
56
+ end
57
+ end
data/spec/text_spec.rb ADDED
@@ -0,0 +1,328 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "#height_of" do
6
+ before(:each) { create_pdf }
7
+
8
+ it "should return the height that would be required to print a" +
9
+ "particular string of text" do
10
+ original_y = @pdf.y
11
+ @pdf.text("Foo")
12
+ new_y = @pdf.y
13
+ @pdf.height_of("Foo", :width => 300).should.be.close(original_y - new_y, 0.0001)
14
+ end
15
+
16
+ it "should raise CannotFit if a too-small width is given" do
17
+ lambda do
18
+ @pdf.height_of("text", :width => 1)
19
+ end.should.raise(Prawn::Errors::CannotFit)
20
+ end
21
+
22
+ it "should raise NotImplementedError if :indent_paragraphs option is provided" do
23
+ lambda {
24
+ @pdf.height_of("hai", :width => 300,
25
+ :indent_paragraphs => 60)
26
+ }.should.raise(NotImplementedError)
27
+ end
28
+
29
+ it "should not raise Prawn::Errors::UnknownOption if :final_gap option is provided" do
30
+ lambda {
31
+ @pdf.height_of("hai", :width => 300,
32
+ :final_gap => true)
33
+ }.should.not.raise(Prawn::Errors::UnknownOption)
34
+ end
35
+
36
+ end
37
+
38
+ describe "#text" do
39
+ before(:each) { create_pdf }
40
+
41
+ it "should default to use kerning information" do
42
+ @pdf.text "hello world"
43
+ text = PDF::Inspector::Text.analyze(@pdf.render)
44
+ text.kerned[0].should.be true
45
+ end
46
+
47
+ it "should be able to disable kerning with an option" do
48
+ @pdf.text "hello world", :kerning => false
49
+ text = PDF::Inspector::Text.analyze(@pdf.render)
50
+ text.kerned[0].should.be false
51
+ end
52
+
53
+ it "should be able to disable kerning document wide" do
54
+ @pdf.default_kerning(false)
55
+ @pdf.default_kerning = false
56
+ @pdf.text "hello world"
57
+ text = PDF::Inspector::Text.analyze(@pdf.render)
58
+ text.kerned[0].should.be false
59
+ end
60
+
61
+ it "option should be able to override document wide kerning disabling" do
62
+ @pdf.default_kerning = false
63
+ @pdf.text "hello world", :kerning => true
64
+ text = PDF::Inspector::Text.analyze(@pdf.render)
65
+ text.kerned[0].should.be true
66
+ end
67
+
68
+ it "should raise ArgumentError if :at option included" do
69
+ lambda { @pdf.text("hai", :at => [0, 0]) }.should.raise(ArgumentError)
70
+ end
71
+
72
+ it "should advance down the document based on font_height" do
73
+ position = @pdf.y
74
+ @pdf.text "Foo"
75
+
76
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
77
+
78
+ position = @pdf.y
79
+ @pdf.text "Foo\nBar\nBaz"
80
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
81
+ end
82
+
83
+ it "should advance down the document based on font_height" +
84
+ " with size option" do
85
+ position = @pdf.y
86
+ @pdf.text "Foo", :size => 15
87
+
88
+ @pdf.font_size = 15
89
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
90
+
91
+ position = @pdf.y
92
+ @pdf.text "Foo\nBar\nBaz"
93
+ @pdf.y.should.be.close(position - 3 * @pdf.font.height, 0.0001)
94
+ end
95
+
96
+ it "should advance down the document based on font_height" +
97
+ " with leading option" do
98
+ position = @pdf.y
99
+ leading = 2
100
+ @pdf.text "Foo", :leading => leading
101
+
102
+ @pdf.y.should.be.close(position - @pdf.font.height - leading, 0.0001)
103
+
104
+ position = @pdf.y
105
+ @pdf.text "Foo\nBar\nBaz"
106
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
107
+ end
108
+
109
+ it "should advance down the document based on font ascender only "+
110
+ "if final_gap is given" do
111
+ position = @pdf.y
112
+ @pdf.text "Foo", :final_gap => false
113
+
114
+ @pdf.y.should.be.close(position - @pdf.font.ascender, 0.0001)
115
+
116
+ position = @pdf.y
117
+ @pdf.text "Foo\nBar\nBaz", :final_gap => false
118
+ @pdf.y.should.be.close(position - 2*@pdf.font.height - @pdf.font.ascender, 0.0001)
119
+ end
120
+
121
+ it "should be able to print text starting at the last line of a page" do
122
+ @pdf.move_cursor_to(@pdf.font.height)
123
+ @pdf.text("hello world")
124
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
125
+ pages.size.should == 1
126
+ end
127
+
128
+ it "should default to 12 point helvetica" do
129
+ @pdf.text "Blah"
130
+ text = PDF::Inspector::Text.analyze(@pdf.render)
131
+ text.font_settings[0][:name].should == :Helvetica
132
+ text.font_settings[0][:size].should == 12
133
+ text.strings.first.should == "Blah"
134
+ end
135
+
136
+ it "should allow setting font size" do
137
+ @pdf.text "Blah", :size => 16
138
+ text = PDF::Inspector::Text.analyze(@pdf.render)
139
+ text.font_settings[0][:size].should == 16
140
+ end
141
+
142
+ it "should allow setting a default font size" do
143
+ @pdf.font_size = 16
144
+ @pdf.text "Blah"
145
+ text = PDF::Inspector::Text.analyze(@pdf.render)
146
+ text.font_settings[0][:size].should == 16
147
+ end
148
+
149
+ it "should allow overriding default font for a single instance" do
150
+ @pdf.font_size = 16
151
+
152
+ @pdf.text "Blah", :size => 11
153
+ @pdf.text "Blaz"
154
+ text = PDF::Inspector::Text.analyze(@pdf.render)
155
+ text.font_settings[0][:size].should == 11
156
+ text.font_settings[1][:size].should == 16
157
+ end
158
+
159
+ it "should allow setting a font size transaction with a block" do
160
+ @pdf.font_size 16 do
161
+ @pdf.text 'Blah'
162
+ end
163
+
164
+ @pdf.text 'blah'
165
+
166
+ text = PDF::Inspector::Text.analyze(@pdf.render)
167
+ text.font_settings[0][:size].should == 16
168
+ text.font_settings[1][:size].should == 12
169
+ end
170
+
171
+ it "should allow manual setting the font size " +
172
+ "when in a font size block" do
173
+ @pdf.font_size(16) do
174
+ @pdf.text 'Foo'
175
+ @pdf.text 'Blah', :size => 11
176
+ @pdf.text 'Blaz'
177
+ end
178
+ text = PDF::Inspector::Text.analyze(@pdf.render)
179
+ text.font_settings[0][:size].should == 16
180
+ text.font_settings[1][:size].should == 11
181
+ text.font_settings[2][:size].should == 16
182
+ end
183
+
184
+ it "should allow registering of built-in font_settings on the fly" do
185
+ @pdf.font "Times-Roman"
186
+ @pdf.text "Blah"
187
+ @pdf.font "Courier"
188
+ @pdf.text "Blaz"
189
+ text = PDF::Inspector::Text.analyze(@pdf.render)
190
+ text.font_settings[0][:name].should == :"Times-Roman"
191
+ text.font_settings[1][:name].should == :Courier
192
+ end
193
+
194
+ it "should utilise the same default font across multiple pages" do
195
+ @pdf.text "Blah"
196
+ @pdf.start_new_page
197
+ @pdf.text "Blaz"
198
+ text = PDF::Inspector::Text.analyze(@pdf.render)
199
+
200
+ text.font_settings.size.should == 2
201
+ text.font_settings[0][:name].should == :Helvetica
202
+ text.font_settings[1][:name].should == :Helvetica
203
+ end
204
+
205
+ it "should raise an exception when an unknown font is used" do
206
+ lambda { @pdf.font "Pao bu" }.should.raise(Prawn::Errors::UnknownFont)
207
+ end
208
+
209
+ it "should correctly render a utf-8 string when using a built-in font" do
210
+ str = "©" # copyright symbol
211
+ @pdf.text str
212
+
213
+ # grab the text from the rendered PDF and ensure it matches
214
+ text = PDF::Inspector::Text.analyze(@pdf.render)
215
+ text.strings.first.should == str
216
+ end
217
+
218
+ it "should correctly render a utf-8 string when using a TTF font" do
219
+ str = "©" # copyright symbol
220
+ @pdf.font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
221
+ @pdf.text str
222
+
223
+ # grab the text from the rendered PDF and ensure it matches
224
+ text = PDF::Inspector::Text.analyze(@pdf.render)
225
+ text.strings.first.should == str
226
+ end
227
+
228
+ it "should correctly render a string with higher bit characters across" +
229
+ " a page break when using a built-in font" do
230
+ str = "©"
231
+ @pdf.move_cursor_to(@pdf.font.height)
232
+ @pdf.text(str + "\n" + str)
233
+
234
+ # grab the text from the rendered PDF and ensure it matches
235
+ text = PDF::Inspector::Text.analyze(@pdf.render)
236
+ text.strings[1].should == str.strip
237
+ end
238
+
239
+ it "should correctly render a string with higher bit characters across" +
240
+ " a page break when using a built-in font and :indent_paragraphs option" do
241
+ str = "©"
242
+ @pdf.move_cursor_to(@pdf.font.height)
243
+ @pdf.text(str + "\n" + str, :indent_paragraphs => 20)
244
+
245
+ # grab the text from the rendered PDF and ensure it matches
246
+ text = PDF::Inspector::Text.analyze(@pdf.render)
247
+ text.strings[1].should == str.strip
248
+ end
249
+
250
+ if "spec".respond_to?(:encode!)
251
+ # Handle non utf-8 string encodings in a sane way on M17N aware VMs
252
+ it "should raise an exception when a utf-8 incompatible string is rendered" do
253
+ str = "Blah \xDD"
254
+ str.force_encoding("ASCII-8BIT")
255
+ lambda { @pdf.text str }.should.raise(ArgumentError)
256
+ end
257
+ it "should not raise an exception when a shift-jis string is rendered" do
258
+ datafile = "#{Prawn::BASEDIR}/data/shift_jis_text.txt"
259
+ sjis_str = File.open(datafile, "r:shift_jis") { |f| f.gets }
260
+ @pdf.font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf")
261
+ lambda { @pdf.text sjis_str }.should.not.raise(ArgumentError)
262
+ end
263
+ else
264
+ # Handle non utf-8 string encodings in a sane way on non-M17N aware VMs
265
+ it "should raise an exception when a corrupt utf-8 string is rendered" do
266
+ str = "Blah \xDD"
267
+ lambda { @pdf.text str }.should.raise(ArgumentError)
268
+ end
269
+ it "should raise an exception when a shift-jis string is rendered" do
270
+ sjis_str = File.read("#{Prawn::BASEDIR}/data/shift_jis_text.txt")
271
+ lambda { @pdf.text sjis_str }.should.raise(ArgumentError)
272
+ end
273
+ end
274
+
275
+ it "should call move_past_bottom when printing more text than can fit" +
276
+ " between the current document.y and bounds.bottom" do
277
+ @pdf.y = @pdf.font.height
278
+ @pdf.text "Hello"
279
+ @pdf.text "World"
280
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
281
+ pages.size.should == 2
282
+ pages[0][:strings].should == ["Hello"]
283
+ pages[1][:strings].should == ["World"]
284
+ end
285
+
286
+ describe "with :indent_paragraphs option" do
287
+ it "should indent the paragraphs" do
288
+ hello = "hello " * 50
289
+ hello2 = "hello " * 50
290
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
291
+ text = PDF::Inspector::Text.analyze(@pdf.render)
292
+ text.strings[0].should == ("hello " * 19).strip
293
+ text.strings[1].should == ("hello " * 21).strip
294
+ text.strings[3].should == ("hello " * 19).strip
295
+ text.strings[4].should == ("hello " * 21).strip
296
+ end
297
+ describe "when wrap to new page, and first line of new page" +
298
+ " is not the start of a new paragraph, that line should" +
299
+ " not be indented" do
300
+ it "should indent the paragraphs" do
301
+ hello = "hello " * 50
302
+ hello2 = "hello " * 50
303
+ @pdf.move_cursor_to(@pdf.font.height)
304
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
305
+ text = PDF::Inspector::Text.analyze(@pdf.render)
306
+ text.strings[0].should == ("hello " * 19).strip
307
+ text.strings[1].should == ("hello " * 21).strip
308
+ text.strings[3].should == ("hello " * 19).strip
309
+ text.strings[4].should == ("hello " * 21).strip
310
+ end
311
+ end
312
+ describe "when wrap to new page, and first line of new page" +
313
+ " is the start of a new paragraph, that line should" +
314
+ " be indented" do
315
+ it "should indent the paragraphs" do
316
+ hello = "hello " * 50
317
+ hello2 = "hello " * 50
318
+ @pdf.move_cursor_to(@pdf.font.height * 3)
319
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
320
+ text = PDF::Inspector::Text.analyze(@pdf.render)
321
+ text.strings[0].should == ("hello " * 19).strip
322
+ text.strings[1].should == ("hello " * 21).strip
323
+ text.strings[3].should == ("hello " * 19).strip
324
+ text.strings[4].should == ("hello " * 21).strip
325
+ end
326
+ end
327
+ end
328
+ end
@@ -0,0 +1,193 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "#height_of_formatted with inline styling" do
6
+ before(:each) { create_pdf }
7
+
8
+ it "should return the height that would be required to print a" +
9
+ "particular string of text" do
10
+ original_y = @pdf.y
11
+ array = [:text => "Foo"]
12
+ @pdf.formatted_text(array)
13
+ new_y = @pdf.y
14
+ @pdf.height_of_formatted(array,
15
+ :width => 300).should.be.close(original_y - new_y, 0.0001)
16
+ end
17
+
18
+ it "should raise CannotFit if a too-small width is given" do
19
+ lambda do
20
+ @pdf.height_of_formatted([:text => "hai"], :width => 1)
21
+ end.should.raise(Prawn::Errors::CannotFit)
22
+ end
23
+
24
+ it "should raise NotImplementedError if :indent_paragraphs option is" +
25
+ "provided" do
26
+ lambda {
27
+ @pdf.height_of_formatted([:text => "hai"], :width => 300,
28
+ :indent_paragraphs => 60)
29
+ }.should.raise(NotImplementedError)
30
+ end
31
+
32
+ it "should not raise Prawn::Errors::UnknownOption if :final_gap option" +
33
+ "is provided" do
34
+ lambda {
35
+ @pdf.height_of_formatted([:text => "hai"], :width => 300,
36
+ :final_gap => true)
37
+ }.should.not.raise(Prawn::Errors::UnknownOption)
38
+ end
39
+ end
40
+
41
+ describe "#formatted_text" do
42
+ it "should draw text" do
43
+ create_pdf
44
+ string = "hello world"
45
+ format_array = [:text => string]
46
+ @pdf.formatted_text(format_array)
47
+ # grab the text from the rendered PDF and ensure it matches
48
+ text = PDF::Inspector::Text.analyze(@pdf.render)
49
+ text.strings.first.should == string
50
+ end
51
+ end
52
+
53
+ describe "#text with inline styling" do
54
+ before(:each) { create_pdf }
55
+
56
+ it "should advance down the document based on font_height" do
57
+ position = @pdf.y
58
+ @pdf.text "Foo", :inline_format => true
59
+
60
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
61
+
62
+ position = @pdf.y
63
+ @pdf.text "Foo\nBar\nBaz", :inline_format => true
64
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
65
+ end
66
+
67
+ it "should advance down the document based on font_height" +
68
+ " with size option" do
69
+ position = @pdf.y
70
+ @pdf.text "Foo", :size => 15, :inline_format => true
71
+
72
+ @pdf.font_size = 15
73
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
74
+
75
+ position = @pdf.y
76
+ @pdf.text "Foo\nBar\nBaz", :inline_format => true
77
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
78
+ end
79
+
80
+ it "should advance down the document based on font_height" +
81
+ " with leading option" do
82
+ position = @pdf.y
83
+ leading = 2
84
+ @pdf.text "Foo", :leading => leading, :inline_format => true
85
+
86
+ @pdf.y.should.be.close(position - @pdf.font.height - leading, 0.0001)
87
+
88
+ position = @pdf.y
89
+ @pdf.text "Foo\nBar\nBaz", :inline_format => true
90
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
91
+ end
92
+
93
+ it "should advance down the document based on font ascender only "+
94
+ "if final_gap is given" do
95
+ position = @pdf.y
96
+ @pdf.text "Foo", :final_gap => false, :inline_format => true
97
+
98
+ @pdf.y.should.be.close(position - @pdf.font.ascender, 0.0001)
99
+
100
+ position = @pdf.y
101
+ @pdf.text "Foo\nBar\nBaz", :final_gap => false, :inline_format => true
102
+ @pdf.y.should.be.close(position -
103
+ 2*@pdf.font.height -
104
+ @pdf.font.ascender, 0.0001)
105
+ end
106
+
107
+ it "should be able to print text starting at the last line of a page" do
108
+ @pdf.move_cursor_to(@pdf.font.height)
109
+ @pdf.text("hello world", :inline_format => true)
110
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
111
+ pages.size.should == 1
112
+ end
113
+
114
+ it "should automatically move to a new page if the tallest fragment" +
115
+ " on the next line won't fit in the available space" do
116
+ create_pdf
117
+ @pdf.move_cursor_to(@pdf.font.height)
118
+ formatted = "this contains <font size='24'>sized</font> text"
119
+ @pdf.text(formatted, :inline_format => true)
120
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
121
+ pages.size.should == 2
122
+ end
123
+
124
+ it "should raise an exception when an unknown font is used" do
125
+ lambda { @pdf.font "Pao bu" }.should.raise(Prawn::Errors::UnknownFont)
126
+ end
127
+
128
+ it "should correctly render a utf-8 string when using a built-in font" do
129
+ str = "©" # copyright symbol
130
+ @pdf.text str, :inline_format => true
131
+
132
+ # grab the text from the rendered PDF and ensure it matches
133
+ text = PDF::Inspector::Text.analyze(@pdf.render)
134
+ text.strings.first.should == str
135
+ end
136
+
137
+ it "should correctly render a string with higher bit characters across" +
138
+ " a page break when using a built-in font" do
139
+ str = "©"
140
+ @pdf.move_cursor_to(@pdf.font.height)
141
+ @pdf.text(str + "\n" + str, :inline_format => true)
142
+
143
+ # grab the text from the rendered PDF and ensure it matches
144
+ text = PDF::Inspector::Text.analyze(@pdf.render)
145
+ text.strings[1].should == str.strip
146
+ end
147
+
148
+ it "should correctly render a string with higher bit characters across" +
149
+ " a page break when using a built-in font and :indent_paragraphs option" do
150
+ str = "©"
151
+ @pdf.move_cursor_to(@pdf.font.height)
152
+ @pdf.text(str + "\n" + str,
153
+ :indent_paragraphs => 20,
154
+ :inline_format => true)
155
+
156
+ # grab the text from the rendered PDF and ensure it matches
157
+ text = PDF::Inspector::Text.analyze(@pdf.render)
158
+ text.strings[1].should == str.strip
159
+ end
160
+
161
+ if "spec".respond_to?(:encode!)
162
+ # Handle non utf-8 string encodings in a sane way on M17N aware VMs
163
+ it "should raise an exception when a utf-8 incompatible string is rendered" do
164
+ str = "Blah \xDD"
165
+ str.force_encoding("ASCII-8BIT")
166
+ lambda { @pdf.text str,
167
+ :inline_format => true }.should.raise(ArgumentError)
168
+ end
169
+ it "should not raise an exception when a shift-jis string is rendered" do
170
+ datafile = "#{Prawn::BASEDIR}/data/shift_jis_text.txt"
171
+ sjis_str = File.open(datafile, "r:shift_jis") { |f| f.gets }
172
+ @pdf.font_families["gkai00mp"] = {
173
+ :normal => { :file => "#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf",
174
+ :font => "gkai00mp" }
175
+ }
176
+ @pdf.font("gkai00mp")
177
+ lambda { @pdf.text sjis_str,
178
+ :inline_format => true }.should.not.raise(ArgumentError)
179
+ end
180
+ else
181
+ # Handle non utf-8 string encodings in a sane way on non-M17N aware VMs
182
+ it "should raise an exception when a corrupt utf-8 string is rendered" do
183
+ str = "Blah \xDD"
184
+ lambda { @pdf.text str,
185
+ :inline_format => true }.should.raise(ArgumentError)
186
+ end
187
+ it "should raise an exception when a shift-jis string is rendered" do
188
+ sjis_str = File.read("#{Prawn::BASEDIR}/data/shift_jis_text.txt")
189
+ lambda { @pdf.text sjis_str,
190
+ :inline_format => true }.should.raise(ArgumentError)
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,75 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
2
+
3
+ describe "Document with transparency" do
4
+ it "the PDF version should be at least 1.4" do
5
+ create_pdf
6
+ @pdf.transparent(0.5)
7
+ str = @pdf.render
8
+ str[0,8].should == "%PDF-1.4"
9
+ end
10
+
11
+ it "a new extended graphics state should be created for "+
12
+ "each unique transparency setting" do
13
+ create_pdf
14
+ @pdf.transparent(0.5, 0.2)
15
+ @pdf.transparent(0.5, 0.75)
16
+ extgstates = PDF::Inspector::ExtGState.analyze(@pdf.render).extgstates
17
+ extgstates.length.should == 2
18
+ end
19
+
20
+ it "a new extended graphics state should not be created for "+
21
+ "each duplicate transparency setting" do
22
+ create_pdf
23
+ @pdf.transparent(0.5, 0.75)
24
+ @pdf.transparent(0.5, 0.75)
25
+ extgstates = PDF::Inspector::ExtGState.analyze(@pdf.render).extgstates
26
+ extgstates.length.should == 1
27
+ end
28
+
29
+ it "setting the transparency with only one parameter sets the transparency"+
30
+ " for both the fill and the stroke" do
31
+ create_pdf
32
+ @pdf.transparent(0.5)
33
+ extgstate = PDF::Inspector::ExtGState.analyze(@pdf.render).extgstates[0]
34
+ extgstate[:opacity].should == 0.5
35
+ extgstate[:stroke_opacity].should == 0.5
36
+ end
37
+
38
+ it "setting the transparency with a numerical parameter and "+
39
+ "a :stroke should set the fill transparency to the numerical parameter "+
40
+ "and the stroke transparency to the option" do
41
+ create_pdf
42
+ @pdf.transparent(0.5, 0.2)
43
+ extgstate = PDF::Inspector::ExtGState.analyze(@pdf.render).extgstates[0]
44
+ extgstate[:opacity].should == 0.5
45
+ extgstate[:stroke_opacity].should == 0.2
46
+ end
47
+
48
+ it "should enforce the valid range of 0.0 to 1.0" do
49
+ create_pdf
50
+ @pdf.transparent(-0.5, -0.2)
51
+ extgstate = PDF::Inspector::ExtGState.analyze(@pdf.render).extgstates[0]
52
+ extgstate[:opacity].should == 0.0
53
+ extgstate[:stroke_opacity].should == 0.0
54
+
55
+ create_pdf
56
+ @pdf.transparent(2.0, 3.0)
57
+ extgstate = PDF::Inspector::ExtGState.analyze(@pdf.render).extgstates[0]
58
+ extgstate[:opacity].should == 1.0
59
+ extgstate[:stroke_opacity].should == 1.0
60
+ end
61
+
62
+ describe "with more than one page" do
63
+ it "the extended graphic state resource should be added to both pages" do
64
+ create_pdf
65
+ @pdf.transparent(0.5, 0.2)
66
+ @pdf.start_new_page
67
+ @pdf.transparent(0.5, 0.2)
68
+ extgstates = PDF::Inspector::ExtGState.analyze(@pdf.render).extgstates
69
+ extgstate = extgstates[0]
70
+ extgstates.length.should == 2
71
+ extgstate[:opacity].should == 0.5
72
+ extgstate[:stroke_opacity].should == 0.2
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,18 @@
1
+ PDF::Inspector : A tool for analyzing PDF output
2
+
3
+ This library provides a number of PDF::Reader[0] based tools for use in testing
4
+ PDF output. Presently, the primary purpose of this tool is to support the
5
+ tests found in Prawn[1], a pure Ruby PDF generation library.
6
+
7
+ However, it may be useful to others, so we have made it available on Github[2]
8
+
9
+ Questions can be directed to the Prawn mailing list[3], but please remember
10
+ that this code is not necessarily suitable for production and has no officially
11
+ planned release date.
12
+
13
+ That having been said, patches are welcome!
14
+
15
+ [0] http://github.com/yob/pdf-reader
16
+ [1] http://github.com/sandal/prawn
17
+ [2] http://github.com/sandal/pdf-inspector
18
+ [3] http://groups.google.com/group/prawn-ruby
@@ -0,0 +1,18 @@
1
+ module PDF
2
+ class Inspector
3
+ class ExtGState < Inspector
4
+ attr_accessor :extgstates
5
+
6
+ def initialize
7
+ @extgstates = []
8
+ end
9
+
10
+ def resource_extgstate(*params)
11
+ @extgstates << {
12
+ :opacity => params[1][:ca],
13
+ :stroke_opacity => params[1][:CA]
14
+ }
15
+ end
16
+ end
17
+ end
18
+ end