nurettin-prawn 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. data/COPYING +2 -0
  2. data/GPLv2 +340 -0
  3. data/GPLv3 +674 -0
  4. data/Gemfile +18 -0
  5. data/LICENSE +56 -0
  6. data/README.md +98 -0
  7. data/Rakefile +45 -0
  8. data/data/encodings/win_ansi.txt +29 -0
  9. data/data/fonts/Action Man.dfont +0 -0
  10. data/data/fonts/Activa.ttf +0 -0
  11. data/data/fonts/Chalkboard.ttf +0 -0
  12. data/data/fonts/Courier-Bold.afm +342 -0
  13. data/data/fonts/Courier-BoldOblique.afm +342 -0
  14. data/data/fonts/Courier-Oblique.afm +342 -0
  15. data/data/fonts/Courier.afm +342 -0
  16. data/data/fonts/DejaVuSans.ttf +0 -0
  17. data/data/fonts/Dustismo_Roman.ttf +0 -0
  18. data/data/fonts/Helvetica-Bold.afm +2827 -0
  19. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  20. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  21. data/data/fonts/Helvetica.afm +3051 -0
  22. data/data/fonts/MustRead.html +19 -0
  23. data/data/fonts/Symbol.afm +213 -0
  24. data/data/fonts/Times-Bold.afm +2588 -0
  25. data/data/fonts/Times-BoldItalic.afm +2384 -0
  26. data/data/fonts/Times-Italic.afm +2667 -0
  27. data/data/fonts/Times-Roman.afm +2419 -0
  28. data/data/fonts/ZapfDingbats.afm +225 -0
  29. data/data/fonts/comicsans.ttf +0 -0
  30. data/data/fonts/gkai00mp.ttf +0 -0
  31. data/data/images/16bit.alpha +0 -0
  32. data/data/images/16bit.dat +0 -0
  33. data/data/images/16bit.png +0 -0
  34. data/data/images/arrow.png +0 -0
  35. data/data/images/arrow2.png +0 -0
  36. data/data/images/barcode_issue.png +0 -0
  37. data/data/images/dice.alpha +0 -0
  38. data/data/images/dice.dat +0 -0
  39. data/data/images/dice.png +0 -0
  40. data/data/images/dice_interlaced.png +0 -0
  41. data/data/images/fractal.jpg +0 -0
  42. data/data/images/letterhead.jpg +0 -0
  43. data/data/images/page_white_text.alpha +0 -0
  44. data/data/images/page_white_text.dat +0 -0
  45. data/data/images/page_white_text.png +0 -0
  46. data/data/images/pigs.jpg +0 -0
  47. data/data/images/prawn.png +0 -0
  48. data/data/images/rails.dat +0 -0
  49. data/data/images/rails.png +0 -0
  50. data/data/images/ruport.png +0 -0
  51. data/data/images/ruport_data.dat +0 -0
  52. data/data/images/ruport_transparent.png +0 -0
  53. data/data/images/ruport_type0.png +0 -0
  54. data/data/images/stef.jpg +0 -0
  55. data/data/images/tru256.bmp +0 -0
  56. data/data/images/web-links.dat +1 -0
  57. data/data/images/web-links.png +0 -0
  58. data/data/pdfs/complex_template.pdf +0 -0
  59. data/data/pdfs/contains_ttf_font.pdf +0 -0
  60. data/data/pdfs/encrypted.pdf +0 -0
  61. data/data/pdfs/form.pdf +820 -0
  62. data/data/pdfs/hexagon.pdf +61 -0
  63. data/data/pdfs/indirect_reference.pdf +86 -0
  64. data/data/pdfs/multipage_template.pdf +127 -0
  65. data/data/pdfs/nested_pages.pdf +118 -0
  66. data/data/pdfs/page_without_mediabox.pdf +193 -0
  67. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  68. data/data/pdfs/two_hexagons.pdf +90 -0
  69. data/data/pdfs/version_1_6.pdf +61 -0
  70. data/data/shift_jis_text.txt +1 -0
  71. data/lib/prawn.rb +29 -0
  72. data/lib/prawn/compatibility.rb +87 -0
  73. data/lib/prawn/core.rb +87 -0
  74. data/lib/prawn/core/annotations.rb +61 -0
  75. data/lib/prawn/core/byte_string.rb +9 -0
  76. data/lib/prawn/core/destinations.rb +90 -0
  77. data/lib/prawn/core/document_state.rb +79 -0
  78. data/lib/prawn/core/literal_string.rb +16 -0
  79. data/lib/prawn/core/name_tree.rb +177 -0
  80. data/lib/prawn/core/object_store.rb +320 -0
  81. data/lib/prawn/core/page.rb +212 -0
  82. data/lib/prawn/core/pdf_object.rb +125 -0
  83. data/lib/prawn/core/reference.rb +119 -0
  84. data/lib/prawn/core/text.rb +268 -0
  85. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  86. data/lib/prawn/core/text/formatted/line_wrap.rb +288 -0
  87. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  88. data/lib/prawn/document.rb +707 -0
  89. data/lib/prawn/document/bounding_box.rb +510 -0
  90. data/lib/prawn/document/column_box.rb +132 -0
  91. data/lib/prawn/document/graphics_state.rb +136 -0
  92. data/lib/prawn/document/internals.rb +173 -0
  93. data/lib/prawn/document/page_geometry.rb +136 -0
  94. data/lib/prawn/document/snapshot.rb +89 -0
  95. data/lib/prawn/document/span.rb +55 -0
  96. data/lib/prawn/encoding.rb +121 -0
  97. data/lib/prawn/errors.rb +99 -0
  98. data/lib/prawn/font.rb +386 -0
  99. data/lib/prawn/font/afm.rb +203 -0
  100. data/lib/prawn/font/dfont.rb +42 -0
  101. data/lib/prawn/font/ttf.rb +343 -0
  102. data/lib/prawn/graphics.rb +523 -0
  103. data/lib/prawn/graphics/cap_style.rb +46 -0
  104. data/lib/prawn/graphics/color.rb +231 -0
  105. data/lib/prawn/graphics/dash.rb +82 -0
  106. data/lib/prawn/graphics/join_style.rb +47 -0
  107. data/lib/prawn/graphics/patterns.rb +137 -0
  108. data/lib/prawn/graphics/transformation.rb +156 -0
  109. data/lib/prawn/graphics/transparency.rb +99 -0
  110. data/lib/prawn/images.rb +196 -0
  111. data/lib/prawn/images/image.rb +65 -0
  112. data/lib/prawn/images/jpg.rb +85 -0
  113. data/lib/prawn/images/png.rb +362 -0
  114. data/lib/prawn/layout.rb +20 -0
  115. data/lib/prawn/layout/grid.rb +259 -0
  116. data/lib/prawn/measurement_extensions.rb +46 -0
  117. data/lib/prawn/measurements.rb +71 -0
  118. data/lib/prawn/outline.rb +326 -0
  119. data/lib/prawn/repeater.rb +122 -0
  120. data/lib/prawn/security.rb +269 -0
  121. data/lib/prawn/soft_mask.rb +94 -0
  122. data/lib/prawn/stamp.rb +134 -0
  123. data/lib/prawn/table.rb +609 -0
  124. data/lib/prawn/table/cell.rb +776 -0
  125. data/lib/prawn/table/cell/image.rb +70 -0
  126. data/lib/prawn/table/cell/in_table.rb +27 -0
  127. data/lib/prawn/table/cell/span_dummy.rb +88 -0
  128. data/lib/prawn/table/cell/subtable.rb +65 -0
  129. data/lib/prawn/table/cell/text.rb +152 -0
  130. data/lib/prawn/table/cells.rb +260 -0
  131. data/lib/prawn/text.rb +420 -0
  132. data/lib/prawn/text/box.rb +141 -0
  133. data/lib/prawn/text/formatted.rb +4 -0
  134. data/lib/prawn/text/formatted/box.rb +563 -0
  135. data/lib/prawn/text/formatted/fragment.rb +253 -0
  136. data/lib/prawn/text/formatted/parser.rb +217 -0
  137. data/lib/prawn/utilities.rb +44 -0
  138. data/manual/basic_concepts/adding_pages.rb +27 -0
  139. data/manual/basic_concepts/basic_concepts.rb +34 -0
  140. data/manual/basic_concepts/creation.rb +39 -0
  141. data/manual/basic_concepts/cursor.rb +33 -0
  142. data/manual/basic_concepts/measurement.rb +25 -0
  143. data/manual/basic_concepts/origin.rb +38 -0
  144. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  145. data/manual/bounding_box/bounding_box.rb +39 -0
  146. data/manual/bounding_box/bounds.rb +49 -0
  147. data/manual/bounding_box/canvas.rb +24 -0
  148. data/manual/bounding_box/creation.rb +23 -0
  149. data/manual/bounding_box/indentation.rb +46 -0
  150. data/manual/bounding_box/nesting.rb +45 -0
  151. data/manual/bounding_box/russian_boxes.rb +40 -0
  152. data/manual/bounding_box/stretchy.rb +31 -0
  153. data/manual/document_and_page_options/background.rb +27 -0
  154. data/manual/document_and_page_options/document_and_page_options.rb +31 -0
  155. data/manual/document_and_page_options/metadata.rb +23 -0
  156. data/manual/document_and_page_options/page_margins.rb +38 -0
  157. data/manual/document_and_page_options/page_size.rb +34 -0
  158. data/manual/example_file.rb +116 -0
  159. data/manual/example_helper.rb +430 -0
  160. data/manual/example_package.rb +53 -0
  161. data/manual/example_section.rb +46 -0
  162. data/manual/graphics/circle_and_ellipse.rb +22 -0
  163. data/manual/graphics/color.rb +24 -0
  164. data/manual/graphics/common_lines.rb +28 -0
  165. data/manual/graphics/fill_and_stroke.rb +42 -0
  166. data/manual/graphics/fill_rules.rb +37 -0
  167. data/manual/graphics/gradients.rb +37 -0
  168. data/manual/graphics/graphics.rb +58 -0
  169. data/manual/graphics/helper.rb +17 -0
  170. data/manual/graphics/line_width.rb +35 -0
  171. data/manual/graphics/lines_and_curves.rb +41 -0
  172. data/manual/graphics/polygon.rb +29 -0
  173. data/manual/graphics/rectangle.rb +21 -0
  174. data/manual/graphics/rotate.rb +28 -0
  175. data/manual/graphics/scale.rb +41 -0
  176. data/manual/graphics/soft_masks.rb +46 -0
  177. data/manual/graphics/stroke_cap.rb +31 -0
  178. data/manual/graphics/stroke_dash.rb +43 -0
  179. data/manual/graphics/stroke_join.rb +30 -0
  180. data/manual/graphics/translate.rb +29 -0
  181. data/manual/graphics/transparency.rb +35 -0
  182. data/manual/images/absolute_position.rb +23 -0
  183. data/manual/images/fit.rb +21 -0
  184. data/manual/images/horizontal.rb +25 -0
  185. data/manual/images/images.rb +40 -0
  186. data/manual/images/plain_image.rb +18 -0
  187. data/manual/images/scale.rb +22 -0
  188. data/manual/images/vertical.rb +28 -0
  189. data/manual/images/width_and_height.rb +25 -0
  190. data/manual/layout/boxes.rb +27 -0
  191. data/manual/layout/content.rb +25 -0
  192. data/manual/layout/layout.rb +28 -0
  193. data/manual/layout/simple_grid.rb +23 -0
  194. data/manual/manual/cover.rb +26 -0
  195. data/manual/manual/foreword.rb +13 -0
  196. data/manual/manual/how_to_read_this_manual.rb +41 -0
  197. data/manual/manual/manual.rb +36 -0
  198. data/manual/outline/add_subsection_to.rb +61 -0
  199. data/manual/outline/insert_section_after.rb +47 -0
  200. data/manual/outline/outline.rb +32 -0
  201. data/manual/outline/sections_and_pages.rb +67 -0
  202. data/manual/repeatable_content/page_numbering.rb +54 -0
  203. data/manual/repeatable_content/repeatable_content.rb +31 -0
  204. data/manual/repeatable_content/repeater.rb +55 -0
  205. data/manual/repeatable_content/stamp.rb +41 -0
  206. data/manual/security/encryption.rb +31 -0
  207. data/manual/security/permissions.rb +38 -0
  208. data/manual/security/security.rb +28 -0
  209. data/manual/syntax_highlight.rb +52 -0
  210. data/manual/table/basic_block.rb +53 -0
  211. data/manual/table/before_rendering_page.rb +26 -0
  212. data/manual/table/cell_border_lines.rb +24 -0
  213. data/manual/table/cell_borders_and_bg.rb +31 -0
  214. data/manual/table/cell_dimensions.rb +30 -0
  215. data/manual/table/cell_text.rb +38 -0
  216. data/manual/table/column_widths.rb +30 -0
  217. data/manual/table/content_and_subtables.rb +39 -0
  218. data/manual/table/creation.rb +27 -0
  219. data/manual/table/filtering.rb +36 -0
  220. data/manual/table/flow_and_header.rb +17 -0
  221. data/manual/table/image_cells.rb +33 -0
  222. data/manual/table/position.rb +29 -0
  223. data/manual/table/row_colors.rb +20 -0
  224. data/manual/table/span.rb +30 -0
  225. data/manual/table/style.rb +22 -0
  226. data/manual/table/table.rb +52 -0
  227. data/manual/table/width.rb +27 -0
  228. data/manual/templates/full_template.rb +23 -0
  229. data/manual/templates/page_template.rb +47 -0
  230. data/manual/templates/templates.rb +26 -0
  231. data/manual/text/alignment.rb +44 -0
  232. data/manual/text/color.rb +24 -0
  233. data/manual/text/column_box.rb +32 -0
  234. data/manual/text/fallback_fonts.rb +37 -0
  235. data/manual/text/font.rb +41 -0
  236. data/manual/text/font_size.rb +45 -0
  237. data/manual/text/font_style.rb +23 -0
  238. data/manual/text/formatted_callbacks.rb +60 -0
  239. data/manual/text/formatted_text.rb +50 -0
  240. data/manual/text/free_flowing_text.rb +51 -0
  241. data/manual/text/group.rb +29 -0
  242. data/manual/text/inline.rb +43 -0
  243. data/manual/text/kerning_and_character_spacing.rb +39 -0
  244. data/manual/text/leading.rb +25 -0
  245. data/manual/text/line_wrapping.rb +41 -0
  246. data/manual/text/paragraph_indentation.rb +26 -0
  247. data/manual/text/positioned_text.rb +38 -0
  248. data/manual/text/registering_families.rb +48 -0
  249. data/manual/text/rendering_and_color.rb +37 -0
  250. data/manual/text/right_to_left_text.rb +43 -0
  251. data/manual/text/rotation.rb +43 -0
  252. data/manual/text/single_usage.rb +37 -0
  253. data/manual/text/text.rb +75 -0
  254. data/manual/text/text_box_excess.rb +32 -0
  255. data/manual/text/text_box_extensions.rb +45 -0
  256. data/manual/text/text_box_overflow.rb +44 -0
  257. data/manual/text/utf8.rb +28 -0
  258. data/manual/text/win_ansi_charset.rb +59 -0
  259. data/prawn.gemspec +46 -0
  260. data/spec/annotations_spec.rb +90 -0
  261. data/spec/bounding_box_spec.rb +493 -0
  262. data/spec/cell_spec.rb +584 -0
  263. data/spec/column_box_spec.rb +33 -0
  264. data/spec/data/curves.pdf +66 -0
  265. data/spec/destinations_spec.rb +15 -0
  266. data/spec/document_spec.rb +736 -0
  267. data/spec/extensions/encoding_helpers.rb +6 -0
  268. data/spec/extensions/mocha.rb +44 -0
  269. data/spec/font_spec.rb +433 -0
  270. data/spec/formatted_text_arranger_spec.rb +421 -0
  271. data/spec/formatted_text_box_spec.rb +640 -0
  272. data/spec/formatted_text_fragment_spec.rb +298 -0
  273. data/spec/graphics_spec.rb +651 -0
  274. data/spec/grid_spec.rb +85 -0
  275. data/spec/images_spec.rb +140 -0
  276. data/spec/inline_formatted_text_parser_spec.rb +515 -0
  277. data/spec/jpg_spec.rb +25 -0
  278. data/spec/line_wrap_spec.rb +333 -0
  279. data/spec/measurement_units_spec.rb +23 -0
  280. data/spec/name_tree_spec.rb +112 -0
  281. data/spec/object_store_spec.rb +170 -0
  282. data/spec/outline_spec.rb +426 -0
  283. data/spec/pdf_object_spec.rb +172 -0
  284. data/spec/png_spec.rb +240 -0
  285. data/spec/reference_spec.rb +105 -0
  286. data/spec/repeater_spec.rb +158 -0
  287. data/spec/security_spec.rb +126 -0
  288. data/spec/snapshot_spec.rb +186 -0
  289. data/spec/soft_mask_spec.rb +117 -0
  290. data/spec/span_spec.rb +49 -0
  291. data/spec/spec_helper.rb +36 -0
  292. data/spec/stamp_spec.rb +159 -0
  293. data/spec/stroke_styles_spec.rb +193 -0
  294. data/spec/table_spec.rb +1209 -0
  295. data/spec/template_spec.rb +351 -0
  296. data/spec/text_at_spec.rb +129 -0
  297. data/spec/text_box_spec.rb +1029 -0
  298. data/spec/text_rendering_mode_spec.rb +45 -0
  299. data/spec/text_spacing_spec.rb +93 -0
  300. data/spec/text_spec.rb +421 -0
  301. data/spec/text_with_inline_formatting_spec.rb +35 -0
  302. data/spec/transparency_spec.rb +89 -0
  303. metadata +544 -0
@@ -0,0 +1,117 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
2
+
3
+ module SoftMaskHelper
4
+ def make_soft_mask
5
+ @pdf.save_graphics_state do
6
+ @pdf.soft_mask do
7
+ if block_given?
8
+ yield
9
+ else
10
+ @pdf.fill_color '808080'
11
+ @pdf.fill_rectangle [100, 100], 200, 200
12
+ end
13
+ end
14
+
15
+ @pdf.fill_color '000000'
16
+ @pdf.fill_rectangle [0, 0], 200, 200
17
+ end
18
+ end
19
+ end
20
+
21
+ describe "Document with soft masks" do
22
+
23
+ include SoftMaskHelper
24
+
25
+ it "should have PDF version at least 1.4" do
26
+ create_pdf
27
+ make_soft_mask
28
+ str = @pdf.render
29
+ str[0,8].should == "%PDF-1.4"
30
+ end
31
+
32
+ it "should create a new extended graphics state for each unique soft mask" do
33
+ create_pdf
34
+
35
+ make_soft_mask do
36
+ @pdf.fill_color '808080'
37
+ @pdf.fill_rectangle [100, 100], 200, 200
38
+ end
39
+
40
+ make_soft_mask do
41
+ @pdf.fill_color '808080'
42
+ @pdf.fill_rectangle [10, 10], 200, 200
43
+ end
44
+
45
+ extgstates = PDF::Inspector::ExtGState.analyze(@pdf.render).extgstates
46
+ extgstates.length.should == 2
47
+ end
48
+
49
+ it "a new extended graphics state should contain soft mask with drawing instructions" do
50
+ create_pdf
51
+
52
+ make_soft_mask do
53
+ @pdf.fill_color '808080'
54
+ @pdf.fill_rectangle [100, 100], 200, 200
55
+ end
56
+
57
+ extgstate = PDF::Inspector::ExtGState.analyze(@pdf.render).extgstates.first
58
+ extgstate[:soft_mask][:G].data.should == "q\n/DeviceRGB cs\n0.000 0.000 0.000 scn\n/DeviceRGB CS\n0.000 0.000 0.000 SCN\n1 w\n0 J\n0 j\n[ ] 0 d\n/DeviceRGB cs\n0.502 0.502 0.502 scn\n100.000 -100.000 200.000 200.000 re\nf\nQ\n"
59
+ end
60
+
61
+ it "should not create duplicate extended graphics states" do
62
+ create_pdf
63
+
64
+ make_soft_mask do
65
+ @pdf.fill_color '808080'
66
+ @pdf.fill_rectangle [100, 100], 200, 200
67
+ end
68
+
69
+ make_soft_mask do
70
+ @pdf.fill_color '808080'
71
+ @pdf.fill_rectangle [100, 100], 200, 200
72
+ end
73
+
74
+ extgstates = PDF::Inspector::ExtGState.analyze(@pdf.render).extgstates
75
+ extgstates.length.should == 1
76
+ end
77
+
78
+ it "should not have objects that are not used for extended graphic state" do
79
+ @pdf = Prawn::Document.new(:margin => 0, :optimize_objects => true)
80
+
81
+ make_soft_mask do
82
+ @pdf.fill_color '808080'
83
+ @pdf.fill_rectangle [100, 100], 200, 200
84
+ end
85
+
86
+ make_soft_mask do
87
+ @pdf.fill_color '808080'
88
+ @pdf.fill_rectangle [100, 100], 200, 200
89
+ end
90
+
91
+ reader = PDF::Reader.new(StringIO.open(@pdf.render))
92
+
93
+ groups = reader.objects.select { |obj|
94
+ o = obj[1]
95
+ o.is_a?(Hash) && o[:Type] == :Group
96
+ }
97
+ groups.length.should == 1
98
+
99
+ forms = reader.objects.select { |obj|
100
+ o = obj[1]
101
+ o.is_a?(PDF::Reader::Stream) && o.hash[:Type] == :XObject && o.hash[:Subtype] == :Form
102
+ }
103
+ forms.length.should == 1
104
+
105
+ masks = reader.objects.select { |obj|
106
+ o = obj[1]
107
+ o.is_a?(Hash) && o[:Type] == :Mask
108
+ }
109
+ masks.length.should == 1
110
+
111
+ ext_g_states = reader.objects.select { |obj|
112
+ o = obj[1]
113
+ o.is_a?(Hash) && o[:Type] == :ExtGState
114
+ }
115
+ ext_g_states.length.should == 1
116
+ end
117
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "drawing span" do
6
+
7
+ before do
8
+ Prawn.debug = false
9
+ create_pdf
10
+ end
11
+
12
+ after do
13
+ Prawn.debug = true
14
+ end
15
+
16
+ it "should only accept :position as option in debug mode" do
17
+ Prawn.debug = true
18
+ lambda { @pdf.span(350, {:x => 3}) {} }.should raise_error(Prawn::Errors::UnknownOption)
19
+ end
20
+
21
+ it "should have raise an error if :position is invalid" do
22
+ lambda { @pdf.span(350, :position => :x) {} }.should raise_error(ArgumentError)
23
+ end
24
+
25
+ it "should restore the margin box when bounding box exits" do
26
+ margin_box = @pdf.bounds
27
+
28
+ @pdf.span(350, :position => :center) do
29
+ @pdf.text "Here's some centered text in a 350 point column. " * 100
30
+ end
31
+
32
+ @pdf.bounds.should == margin_box
33
+
34
+ end
35
+
36
+ it "should do create a margin box" do
37
+ y = @pdf.y
38
+ margin_box = @pdf.span(350, :position => :center) do
39
+ @pdf.text "Here's some centered text in a 350 point column. " * 100
40
+ end
41
+
42
+ margin_box.top.should == 792.0
43
+ margin_box.bottom.should == 0
44
+
45
+ end
46
+
47
+ end
48
+
49
+
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ puts "Prawn specs: Running on Ruby Version: #{RUBY_VERSION}"
4
+
5
+ require "bundler"
6
+ Bundler.setup
7
+
8
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
9
+ require "prawn"
10
+
11
+ Prawn.debug = true
12
+
13
+ #require "test/spec"
14
+ require "rspec"
15
+ require "mocha/api"
16
+ require "pdf/reader"
17
+ require "pdf/inspector"
18
+
19
+ # Requires supporting ruby files with custom matchers and macros, etc,
20
+ # in spec/extensions/ and its subdirectories.
21
+ Dir[File.dirname(__FILE__) + "/extensions/**/*.rb"].each {|f| require f }
22
+
23
+ RSpec.configure do |config|
24
+ config.mock_framework = :mocha
25
+ config.include EncodingHelpers
26
+ end
27
+
28
+ def create_pdf(klass=Prawn::Document)
29
+ @pdf = klass.new(:margin => 0)
30
+ end
31
+
32
+ # Make some methods public to assist in testing
33
+ module Prawn::Graphics
34
+ public :map_to_absolute
35
+ end
36
+
@@ -0,0 +1,159 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
2
+
3
+ describe "create_stamp before any page is added" do
4
+ it "should work with the font class" do
5
+ @pdf = Prawn::Document.new(:skip_page_creation => true)
6
+ lambda {
7
+ @pdf.create_stamp("my_stamp") do
8
+ @pdf.font.height
9
+ end
10
+ }.should_not raise_error(Prawn::Errors::NotOnPage)
11
+ end
12
+ it "should work with setting color" do
13
+ @pdf = Prawn::Document.new(:skip_page_creation => true)
14
+ lambda {
15
+ @pdf.create_stamp("my_stamp") do
16
+ @pdf.fill_color = 'ff0000'
17
+ end
18
+ }.should_not raise_error(Prawn::Errors::NotOnPage)
19
+ end
20
+ end
21
+
22
+ describe "#stamp_at" do
23
+ it "should work" do
24
+ create_pdf
25
+ @pdf.create_stamp("MyStamp")
26
+ @pdf.stamp_at("MyStamp", [100, 200])
27
+ # I had modified PDF::Inspector::XObject to receive the
28
+ # invoke_xobject message and count the number of times it was
29
+ # called, but it was only called once, so I reverted checking the
30
+ # output with a regular expression
31
+ @pdf.render.should =~ /\/Stamp1 Do.*?/m
32
+ end
33
+ end
34
+
35
+ describe "Document with a stamp" do
36
+ it "should raise_error NameTaken error when attempt to create stamp "+
37
+ "with same name as an existing stamp" do
38
+ create_pdf
39
+ @pdf.create_stamp("MyStamp")
40
+ lambda {
41
+ @pdf.create_stamp("MyStamp")
42
+ }.should raise_error(Prawn::Errors::NameTaken)
43
+ end
44
+
45
+ it "should raise_error InvalidName error when attempt to create "+
46
+ "stamp with a blank name" do
47
+ create_pdf
48
+ lambda {
49
+ @pdf.create_stamp("")
50
+ }.should raise_error(Prawn::Errors::InvalidName)
51
+ end
52
+
53
+ it "a new XObject should be defined for each stamp created" do
54
+ create_pdf
55
+ @pdf.create_stamp("MyStamp")
56
+ @pdf.create_stamp("AnotherStamp")
57
+ @pdf.stamp("MyStamp")
58
+ @pdf.stamp("AnotherStamp")
59
+
60
+ inspector = PDF::Inspector::XObject.analyze(@pdf.render)
61
+ xobjects = inspector.page_xobjects.last
62
+ xobjects.length.should == 2
63
+ end
64
+
65
+ it "calling stamp with a name that does not match an existing stamp "+
66
+ "should raise_error UndefinedObjectName" do
67
+ create_pdf
68
+ @pdf.create_stamp("MyStamp")
69
+ lambda {
70
+ @pdf.stamp("OtherStamp")
71
+ }.should raise_error(Prawn::Errors::UndefinedObjectName)
72
+ end
73
+
74
+ it "stamp should be drawn into the document each time stamp is called" do
75
+ create_pdf
76
+ @pdf.create_stamp("MyStamp")
77
+ @pdf.stamp("MyStamp")
78
+ @pdf.stamp("MyStamp")
79
+ @pdf.stamp("MyStamp")
80
+ # I had modified PDF::Inspector::XObject to receive the
81
+ # invoke_xobject message and count the number of times it was
82
+ # called, but it was only called once, so I reverted checking the
83
+ # output with a regular expression
84
+ @pdf.render.should =~ /(\/Stamp1 Do.*?){3}/m
85
+ end
86
+
87
+ it "resources added during stamp creation should be added to the "+
88
+ "stamp XObject, not the page" do
89
+ create_pdf
90
+ @pdf.create_stamp("MyStamp") do
91
+ @pdf.transparent(0.5) { @pdf.circle([100, 100], 10)}
92
+ end
93
+ @pdf.stamp("MyStamp")
94
+
95
+ # Inspector::XObject does not give information about resources, so
96
+ # resorting to string matching
97
+
98
+ output = @pdf.render
99
+ objects = output.split("endobj")
100
+ objects.each do |object|
101
+ if object =~ /\/Type \/Page$/
102
+ object.should_not =~ /\/ExtGState/
103
+ elsif object =~ /\/Type \/XObject$/
104
+ object.should =~ /\/ExtGState/
105
+ end
106
+ end
107
+ end
108
+
109
+ it "stamp stream should be wrapped in a graphic state" do
110
+ create_pdf
111
+ @pdf.create_stamp("MyStamp") do
112
+ @pdf.text "This should have a 'q' before it and a 'Q' after it"
113
+ end
114
+ @pdf.stamp("MyStamp")
115
+ stamps = PDF::Inspector::XObject.analyze(@pdf.render)
116
+ stamps.xobject_streams[:Stamp1].data.chomp.should =~ /q(.|\s)*Q\Z/
117
+ end
118
+
119
+ it "should not add to the page graphic state stack " do
120
+ create_pdf
121
+ @pdf.state.page.stack.stack.size.should == 1
122
+
123
+ @pdf.create_stamp("MyStamp") do
124
+ @pdf.save_graphics_state
125
+ @pdf.save_graphics_state
126
+ @pdf.save_graphics_state
127
+ @pdf.text "This should have a 'q' before it and a 'Q' after it"
128
+ @pdf.restore_graphics_state
129
+ end
130
+ @pdf.state.page.stack.stack.size.should == 1
131
+ end
132
+
133
+ it "should be able to change fill and stroke colors within the stamp stream" do
134
+ create_pdf
135
+ @pdf.create_stamp("MyStamp") do
136
+ @pdf.fill_color(100, 100, 20, 0)
137
+ @pdf.stroke_color(100, 100, 20, 0)
138
+ end
139
+ @pdf.stamp("MyStamp")
140
+ stamps = PDF::Inspector::XObject.analyze(@pdf.render)
141
+ stamp_stream = stamps.xobject_streams[:Stamp1].data
142
+ stamp_stream.should include("/DeviceCMYK cs\n1.000 1.000 0.200 0.000 scn")
143
+ stamp_stream.should include("/DeviceCMYK CS\n1.000 1.000 0.200 0.000 SCN")
144
+ end
145
+
146
+ it "should save the color space even when same as current page color space" do
147
+ create_pdf
148
+ @pdf.stroke_color(100, 100, 20, 0)
149
+ @pdf.create_stamp("MyStamp") do
150
+ @pdf.stroke_color(100, 100, 20, 0)
151
+ end
152
+ @pdf.stamp("MyStamp")
153
+ stamps = PDF::Inspector::XObject.analyze(@pdf.render)
154
+ stamp_stream = stamps.xobject_streams[:Stamp1].data
155
+ stamp_stream.should include("/DeviceCMYK CS\n1.000 1.000 0.200 0.000 SCN")
156
+ end
157
+
158
+
159
+ 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 "When stroking with default settings" do
6
+ before(:each) { create_pdf }
7
+ it "cap_style should be :butt" do
8
+ @pdf.cap_style.should == :butt
9
+ end
10
+
11
+ it "join_style should be :miter" do
12
+ @pdf.join_style.should == :miter
13
+ end
14
+
15
+ it "dashed? should be_false" do
16
+ @pdf.should_not be_dashed
17
+ end
18
+ end
19
+
20
+ describe "Cap styles" do
21
+ before(:each) { create_pdf }
22
+
23
+ it "should be able to use assignment operator" do
24
+ @pdf.cap_style = :round
25
+ @pdf.cap_style.should == :round
26
+ end
27
+
28
+ describe "#cap_style(:butt)" do
29
+ it "rendered PDF should include butt style cap" do
30
+ @pdf.cap_style(:butt)
31
+ cap_style = PDF::Inspector::Graphics::CapStyle.analyze(@pdf.render).cap_style
32
+ cap_style.should == 0
33
+ end
34
+ end
35
+
36
+ describe "#cap_style(:round)" do
37
+ it "rendered PDF should include round style cap" do
38
+ @pdf.cap_style(:round)
39
+ cap_style = PDF::Inspector::Graphics::CapStyle.analyze(@pdf.render).cap_style
40
+ cap_style.should == 1
41
+ end
42
+ end
43
+
44
+ describe "#cap_style(:projecting_square)" do
45
+ it "rendered PDF should include projecting_square style cap" do
46
+ @pdf.cap_style(:projecting_square)
47
+ cap_style = PDF::Inspector::Graphics::CapStyle.analyze(@pdf.render).cap_style
48
+ cap_style.should == 2
49
+ end
50
+ end
51
+
52
+ it "should carry the current cap style settings over to new pages" do
53
+ @pdf.cap_style(:round)
54
+ @pdf.start_new_page
55
+ cap_styles = PDF::Inspector::Graphics::CapStyle.analyze(@pdf.render)
56
+ cap_styles.cap_style_count.should == 2
57
+ cap_styles.cap_style.should == 1
58
+ end
59
+ end
60
+
61
+ describe "Join styles" do
62
+ before(:each) { create_pdf }
63
+
64
+ it "should be able to use assignment operator" do
65
+ @pdf.join_style = :round
66
+ @pdf.join_style.should == :round
67
+ end
68
+
69
+ describe "#join_style(:miter)" do
70
+ it "rendered PDF should include miter style join" do
71
+ @pdf.join_style(:miter)
72
+ join_style = PDF::Inspector::Graphics::JoinStyle.analyze(@pdf.render).join_style
73
+ join_style.should == 0
74
+ end
75
+ end
76
+
77
+ describe "#join_style(:round)" do
78
+ it "rendered PDF should include round style join" do
79
+ @pdf.join_style(:round)
80
+ join_style = PDF::Inspector::Graphics::JoinStyle.analyze(@pdf.render).join_style
81
+ join_style.should == 1
82
+ end
83
+ end
84
+
85
+ describe "#join_style(:bevel)" do
86
+ it "rendered PDF should include bevel style join" do
87
+ @pdf.join_style(:bevel)
88
+ join_style = PDF::Inspector::Graphics::JoinStyle.analyze(@pdf.render).join_style
89
+ join_style.should == 2
90
+ end
91
+ end
92
+
93
+ it "should carry the current join style settings over to new pages" do
94
+ @pdf.join_style(:round)
95
+ @pdf.start_new_page
96
+ join_styles = PDF::Inspector::Graphics::JoinStyle.analyze(@pdf.render)
97
+ join_styles.join_style_count.should == 2
98
+ join_styles.join_style.should == 1
99
+ end
100
+ end
101
+
102
+ describe "Dashes" do
103
+ before(:each) { create_pdf }
104
+
105
+ it "should be able to use assignment operator" do
106
+ @pdf.dash = 2
107
+ @pdf.should be_dashed
108
+ end
109
+
110
+ describe "setting a dash" do
111
+ it "dashed? should be_true" do
112
+ @pdf.dash(2)
113
+ @pdf.should be_dashed
114
+ end
115
+ it "rendered PDF should include a stroked dash" do
116
+ @pdf.dash(2)
117
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
118
+ dashes.stroke_dash.should == [[2, 2], 0]
119
+ end
120
+ end
121
+
122
+ describe "setting a dash by passing a single argument" do
123
+ it "space between dashes should be the same length as the dash in the rendered PDF" do
124
+ @pdf.dash(2)
125
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
126
+ dashes.stroke_dash.should == [[2, 2], 0]
127
+ end
128
+ end
129
+
130
+ describe "with a space option that differs from the first argument" do
131
+ it "space between dashes in the rendered PDF should be different length than the length of the dash" do
132
+ @pdf.dash(2, :space => 3)
133
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
134
+ dashes.stroke_dash.should == [[2, 3], 0]
135
+ end
136
+ end
137
+
138
+ describe "with a non-zero phase option" do
139
+ it "rendered PDF should include a non-zero phase" do
140
+ @pdf.dash(2, :phase => 1)
141
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
142
+ dashes.stroke_dash.should == [[2, 2], 1]
143
+ end
144
+ end
145
+
146
+ describe "clearing stroke dash" do
147
+ it "should restore solid line" do
148
+ @pdf.dash(2)
149
+ @pdf.undash
150
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
151
+ dashes.stroke_dash.should == [[], 0]
152
+ end
153
+ end
154
+
155
+ it "should carry the current dash settings over to new pages" do
156
+ @pdf.dash(2)
157
+ @pdf.start_new_page
158
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
159
+ dashes.stroke_dash_count.should == 2
160
+ dashes.stroke_dash.should == [[2, 2], 0]
161
+ end
162
+
163
+ describe "#dashed?" do
164
+
165
+ it "an initial document should not be dashed" do
166
+ @pdf.dashed?.should == false
167
+ end
168
+
169
+ it "should return true if any of the currently active settings are dashed" do
170
+ @pdf.dash(2)
171
+ @pdf.save_graphics_state
172
+ @pdf.dashed?.should == true
173
+ end
174
+
175
+ it "should return false if the document was most recently undashed" do
176
+ @pdf.dash(2)
177
+ @pdf.save_graphics_state
178
+ @pdf.undash
179
+ @pdf.save_graphics_state
180
+ @pdf.dashed?.should == false
181
+ end
182
+
183
+ it "should return true when restoring to a state that was dashed" do
184
+ @pdf.dash(2)
185
+ @pdf.save_graphics_state
186
+ @pdf.undash
187
+ @pdf.restore_graphics_state
188
+ @pdf.dashed?.should == true
189
+ end
190
+
191
+ end
192
+
193
+ end