prawn 2.0.2 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/GPLv2 +20 -21
  5. data/Gemfile +3 -9
  6. data/Rakefile +20 -23
  7. data/lib/prawn.rb +37 -49
  8. data/lib/prawn/document.rb +181 -133
  9. data/lib/prawn/document/bounding_box.rb +41 -29
  10. data/lib/prawn/document/column_box.rb +7 -7
  11. data/lib/prawn/document/internals.rb +18 -8
  12. data/lib/prawn/document/span.rb +21 -16
  13. data/lib/prawn/encoding.rb +69 -68
  14. data/lib/prawn/errors.rb +12 -7
  15. data/lib/prawn/font.rb +115 -69
  16. data/lib/prawn/font_metric_cache.rb +14 -8
  17. data/lib/prawn/{font → fonts}/afm.rb +102 -68
  18. data/lib/prawn/{font → fonts}/dfont.rb +5 -11
  19. data/lib/prawn/fonts/otf.rb +11 -0
  20. data/lib/prawn/fonts/ttc.rb +36 -0
  21. data/lib/prawn/{font → fonts}/ttf.rb +87 -68
  22. data/lib/prawn/graphics.rb +120 -80
  23. data/lib/prawn/graphics/blend_mode.rb +65 -0
  24. data/lib/prawn/graphics/cap_style.rb +3 -3
  25. data/lib/prawn/graphics/color.rb +27 -25
  26. data/lib/prawn/graphics/dash.rb +23 -11
  27. data/lib/prawn/graphics/join_style.rb +9 -3
  28. data/lib/prawn/graphics/patterns.rb +197 -67
  29. data/lib/prawn/graphics/transformation.rb +17 -8
  30. data/lib/prawn/graphics/transparency.rb +17 -13
  31. data/lib/prawn/grid.rb +48 -47
  32. data/lib/prawn/image_handler.rb +5 -5
  33. data/lib/prawn/images.rb +39 -30
  34. data/lib/prawn/images/image.rb +2 -1
  35. data/lib/prawn/images/jpg.rb +28 -22
  36. data/lib/prawn/images/png.rb +107 -66
  37. data/lib/prawn/measurement_extensions.rb +10 -9
  38. data/lib/prawn/measurements.rb +19 -15
  39. data/lib/prawn/outline.rb +97 -77
  40. data/lib/prawn/repeater.rb +14 -10
  41. data/lib/prawn/security.rb +81 -61
  42. data/lib/prawn/security/arcfour.rb +2 -2
  43. data/lib/prawn/soft_mask.rb +26 -26
  44. data/lib/prawn/stamp.rb +20 -13
  45. data/lib/prawn/text.rb +68 -52
  46. data/lib/prawn/text/box.rb +11 -8
  47. data/lib/prawn/text/formatted.rb +5 -5
  48. data/lib/prawn/text/formatted/arranger.rb +78 -49
  49. data/lib/prawn/text/formatted/box.rb +134 -100
  50. data/lib/prawn/text/formatted/fragment.rb +11 -14
  51. data/lib/prawn/text/formatted/line_wrap.rb +121 -63
  52. data/lib/prawn/text/formatted/parser.rb +139 -117
  53. data/lib/prawn/text/formatted/wrap.rb +43 -31
  54. data/lib/prawn/transformation_stack.rb +44 -0
  55. data/lib/prawn/utilities.rb +7 -22
  56. data/lib/prawn/version.rb +2 -2
  57. data/lib/prawn/view.rb +17 -7
  58. data/manual/basic_concepts/adding_pages.rb +6 -7
  59. data/manual/basic_concepts/basic_concepts.rb +31 -22
  60. data/manual/basic_concepts/creation.rb +10 -11
  61. data/manual/basic_concepts/cursor.rb +4 -5
  62. data/manual/basic_concepts/measurement.rb +6 -7
  63. data/manual/basic_concepts/origin.rb +5 -6
  64. data/manual/basic_concepts/other_cursor_helpers.rb +11 -12
  65. data/manual/basic_concepts/view.rb +22 -16
  66. data/manual/bounding_box/bounding_box.rb +29 -24
  67. data/manual/bounding_box/bounds.rb +11 -12
  68. data/manual/bounding_box/canvas.rb +4 -5
  69. data/manual/bounding_box/creation.rb +6 -7
  70. data/manual/bounding_box/indentation.rb +14 -15
  71. data/manual/bounding_box/nesting.rb +24 -17
  72. data/manual/bounding_box/russian_boxes.rb +14 -13
  73. data/manual/bounding_box/stretchy.rb +12 -13
  74. data/manual/contents.rb +28 -22
  75. data/manual/cover.rb +33 -28
  76. data/manual/document_and_page_options/background.rb +11 -13
  77. data/manual/document_and_page_options/document_and_page_options.rb +25 -20
  78. data/manual/document_and_page_options/metadata.rb +18 -16
  79. data/manual/document_and_page_options/page_margins.rb +18 -20
  80. data/manual/document_and_page_options/page_size.rb +13 -12
  81. data/manual/document_and_page_options/print_scaling.rb +17 -15
  82. data/manual/example_helper.rb +5 -4
  83. data/manual/graphics/blend_mode.rb +52 -0
  84. data/manual/graphics/circle_and_ellipse.rb +4 -5
  85. data/manual/graphics/color.rb +7 -9
  86. data/manual/graphics/common_lines.rb +7 -8
  87. data/manual/graphics/fill_and_stroke.rb +4 -5
  88. data/manual/graphics/fill_rules.rb +9 -10
  89. data/manual/graphics/gradients.rb +27 -21
  90. data/manual/graphics/graphics.rb +48 -39
  91. data/manual/graphics/helper.rb +12 -9
  92. data/manual/graphics/line_width.rb +8 -7
  93. data/manual/graphics/lines_and_curves.rb +7 -8
  94. data/manual/graphics/polygon.rb +6 -8
  95. data/manual/graphics/rectangle.rb +4 -5
  96. data/manual/graphics/rotate.rb +6 -7
  97. data/manual/graphics/scale.rb +14 -15
  98. data/manual/graphics/soft_masks.rb +4 -5
  99. data/manual/graphics/stroke_cap.rb +6 -7
  100. data/manual/graphics/stroke_dash.rb +11 -12
  101. data/manual/graphics/stroke_join.rb +5 -6
  102. data/manual/graphics/translate.rb +9 -10
  103. data/manual/graphics/transparency.rb +7 -8
  104. data/manual/how_to_read_this_manual.rb +6 -6
  105. data/manual/images/absolute_position.rb +6 -7
  106. data/manual/images/fit.rb +7 -8
  107. data/manual/images/horizontal.rb +9 -10
  108. data/manual/images/images.rb +28 -24
  109. data/manual/images/plain_image.rb +5 -6
  110. data/manual/images/scale.rb +9 -10
  111. data/manual/images/vertical.rb +13 -14
  112. data/manual/images/width_and_height.rb +10 -11
  113. data/manual/layout/boxes.rb +5 -6
  114. data/manual/layout/content.rb +7 -8
  115. data/manual/layout/layout.rb +18 -16
  116. data/manual/layout/simple_grid.rb +6 -7
  117. data/manual/outline/add_subsection_to.rb +20 -21
  118. data/manual/outline/insert_section_after.rb +15 -16
  119. data/manual/outline/outline.rb +21 -17
  120. data/manual/outline/sections_and_pages.rb +17 -18
  121. data/manual/repeatable_content/alternate_page_numbering.rb +21 -17
  122. data/manual/repeatable_content/page_numbering.rb +17 -16
  123. data/manual/repeatable_content/repeatable_content.rb +25 -19
  124. data/manual/repeatable_content/repeater.rb +14 -15
  125. data/manual/repeatable_content/stamp.rb +14 -15
  126. data/manual/security/encryption.rb +9 -10
  127. data/manual/security/permissions.rb +19 -14
  128. data/manual/security/security.rb +19 -16
  129. data/manual/table.rb +3 -3
  130. data/manual/text/alignment.rb +16 -17
  131. data/manual/text/color.rb +12 -11
  132. data/manual/text/column_box.rb +9 -10
  133. data/manual/text/fallback_fonts.rb +25 -21
  134. data/manual/text/font.rb +11 -12
  135. data/manual/text/font_size.rb +13 -14
  136. data/manual/text/font_style.rb +7 -8
  137. data/manual/text/formatted_callbacks.rb +25 -21
  138. data/manual/text/formatted_text.rb +33 -25
  139. data/manual/text/free_flowing_text.rb +20 -21
  140. data/manual/text/inline.rb +18 -19
  141. data/manual/text/kerning_and_character_spacing.rb +14 -15
  142. data/manual/text/leading.rb +7 -8
  143. data/manual/text/line_wrapping.rb +37 -18
  144. data/manual/text/paragraph_indentation.rb +13 -14
  145. data/manual/text/positioned_text.rb +15 -16
  146. data/manual/text/registering_families.rb +20 -21
  147. data/manual/text/rendering_and_color.rb +9 -10
  148. data/manual/text/right_to_left_text.rb +26 -19
  149. data/manual/text/rotation.rb +28 -23
  150. data/manual/text/single_usage.rb +8 -9
  151. data/manual/text/text.rb +57 -52
  152. data/manual/text/text_box_excess.rb +20 -17
  153. data/manual/text/text_box_extensions.rb +18 -15
  154. data/manual/text/text_box_overflow.rb +18 -19
  155. data/manual/text/utf8.rb +11 -12
  156. data/manual/text/win_ansi_charset.rb +21 -19
  157. data/prawn.gemspec +45 -33
  158. data/spec/extensions/encoding_helpers.rb +3 -3
  159. data/spec/prawn/document/bounding_box_spec.rb +546 -0
  160. data/spec/prawn/document/column_box_spec.rb +75 -0
  161. data/spec/prawn/document/security_spec.rb +176 -0
  162. data/spec/prawn/document_annotations_spec.rb +76 -0
  163. data/spec/prawn/document_destinations_spec.rb +15 -0
  164. data/spec/prawn/document_grid_spec.rb +99 -0
  165. data/spec/prawn/document_reference_spec.rb +27 -0
  166. data/spec/prawn/document_span_spec.rb +36 -0
  167. data/spec/prawn/document_spec.rb +802 -0
  168. data/spec/prawn/font_metric_cache_spec.rb +54 -0
  169. data/spec/prawn/font_spec.rb +542 -0
  170. data/spec/prawn/graphics/blend_mode_spec.rb +63 -0
  171. data/spec/prawn/graphics/transparency_spec.rb +81 -0
  172. data/spec/prawn/graphics_spec.rb +837 -0
  173. data/spec/prawn/graphics_stroke_styles_spec.rb +229 -0
  174. data/spec/prawn/image_handler_spec.rb +53 -0
  175. data/spec/prawn/images/jpg_spec.rb +20 -0
  176. data/spec/prawn/images/png_spec.rb +283 -0
  177. data/spec/prawn/images_spec.rb +224 -0
  178. data/spec/prawn/measurements_extensions_spec.rb +24 -0
  179. data/spec/prawn/outline_spec.rb +412 -0
  180. data/spec/prawn/repeater_spec.rb +165 -0
  181. data/spec/prawn/soft_mask_spec.rb +74 -0
  182. data/spec/prawn/stamp_spec.rb +172 -0
  183. data/spec/prawn/text/box_spec.rb +1112 -0
  184. data/spec/prawn/text/formatted/arranger_spec.rb +466 -0
  185. data/spec/prawn/text/formatted/box_spec.rb +846 -0
  186. data/spec/prawn/text/formatted/fragment_spec.rb +343 -0
  187. data/spec/prawn/text/formatted/line_wrap_spec.rb +494 -0
  188. data/spec/prawn/text/formatted/parser_spec.rb +697 -0
  189. data/spec/prawn/text_draw_text_spec.rb +149 -0
  190. data/spec/prawn/text_rendering_mode_spec.rb +48 -0
  191. data/spec/prawn/text_spacing_spec.rb +95 -0
  192. data/spec/prawn/text_spec.rb +603 -0
  193. data/spec/prawn/text_with_inline_formatting_spec.rb +35 -0
  194. data/spec/prawn/transformation_stack_spec.rb +66 -0
  195. data/spec/prawn/view_spec.rb +63 -0
  196. data/spec/prawn_manual_spec.rb +35 -0
  197. data/spec/spec_helper.rb +19 -23
  198. metadata +145 -185
  199. metadata.gz.sig +4 -0
  200. data/data/images/16bit.alpha +0 -0
  201. data/data/images/16bit.color +0 -0
  202. data/data/images/16bit.png +0 -0
  203. data/data/images/arrow.png +0 -0
  204. data/data/images/arrow2.png +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/letterhead.jpg +0 -0
  213. data/data/images/license.md +0 -8
  214. data/data/images/page_white_text.alpha +0 -0
  215. data/data/images/page_white_text.color +0 -0
  216. data/data/images/page_white_text.png +0 -0
  217. data/data/images/pal_bk.png +0 -0
  218. data/data/images/pigs.jpg +0 -0
  219. data/data/images/prawn.png +0 -0
  220. data/data/images/ruport.png +0 -0
  221. data/data/images/ruport_data.dat +0 -0
  222. data/data/images/ruport_transparent.png +0 -0
  223. data/data/images/ruport_type0.png +0 -0
  224. data/data/images/stef.jpg +0 -0
  225. data/data/images/tru256.bmp +0 -0
  226. data/data/images/web-links.dat +0 -1
  227. data/data/images/web-links.png +0 -0
  228. data/data/pdfs/complex_template.pdf +0 -0
  229. data/data/pdfs/contains_ttf_font.pdf +0 -0
  230. data/data/pdfs/encrypted.pdf +0 -0
  231. data/data/pdfs/form.pdf +1 -819
  232. data/data/pdfs/hexagon.pdf +0 -61
  233. data/data/pdfs/indirect_reference.pdf +0 -86
  234. data/data/pdfs/multipage_template.pdf +0 -127
  235. data/data/pdfs/nested_pages.pdf +0 -118
  236. data/data/pdfs/page_without_mediabox.pdf +0 -193
  237. data/data/pdfs/resources_as_indirect_object.pdf +0 -83
  238. data/data/pdfs/two_hexagons.pdf +0 -90
  239. data/data/pdfs/version_1_6.pdf +0 -61
  240. data/data/shift_jis_text.txt +0 -1
  241. data/spec/acceptance/png.rb +0 -24
  242. data/spec/annotations_spec.rb +0 -67
  243. data/spec/bounding_box_spec.rb +0 -501
  244. data/spec/column_box_spec.rb +0 -59
  245. data/spec/destinations_spec.rb +0 -13
  246. data/spec/document_spec.rb +0 -742
  247. data/spec/extensions/mocha.rb +0 -45
  248. data/spec/font_metric_cache_spec.rb +0 -52
  249. data/spec/font_spec.rb +0 -475
  250. data/spec/formatted_text_arranger_spec.rb +0 -423
  251. data/spec/formatted_text_box_spec.rb +0 -716
  252. data/spec/formatted_text_fragment_spec.rb +0 -299
  253. data/spec/graphics_spec.rb +0 -666
  254. data/spec/grid_spec.rb +0 -95
  255. data/spec/image_handler_spec.rb +0 -53
  256. data/spec/images_spec.rb +0 -167
  257. data/spec/inline_formatted_text_parser_spec.rb +0 -568
  258. data/spec/jpg_spec.rb +0 -23
  259. data/spec/line_wrap_spec.rb +0 -366
  260. data/spec/measurement_units_spec.rb +0 -22
  261. data/spec/outline_spec.rb +0 -409
  262. data/spec/png_spec.rb +0 -235
  263. data/spec/reference_spec.rb +0 -25
  264. data/spec/repeater_spec.rb +0 -154
  265. data/spec/security_spec.rb +0 -151
  266. data/spec/soft_mask_spec.rb +0 -78
  267. data/spec/span_spec.rb +0 -43
  268. data/spec/stamp_spec.rb +0 -179
  269. data/spec/stroke_styles_spec.rb +0 -208
  270. data/spec/text_at_spec.rb +0 -142
  271. data/spec/text_box_spec.rb +0 -1038
  272. data/spec/text_rendering_mode_spec.rb +0 -45
  273. data/spec/text_spacing_spec.rb +0 -93
  274. data/spec/text_spec.rb +0 -549
  275. data/spec/text_with_inline_formatting_spec.rb +0 -35
  276. data/spec/transparency_spec.rb +0 -91
  277. data/spec/view_spec.rb +0 -42
@@ -1,299 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
-
5
- describe "Text::Formatted::Fragment#space_count" do
6
- it "should return the number of spaces in the fragment" do
7
- create_pdf
8
- format_state = {}
9
- fragment = Prawn::Text::Formatted::Fragment.new("hello world ",
10
- format_state,
11
- @pdf)
12
- expect(fragment.space_count).to eq(2)
13
- end
14
- it "should exclude trailing spaces from the count when " \
15
- ":exclude_trailing_white_space => true" do
16
- create_pdf
17
- format_state = { :exclude_trailing_white_space => true }
18
- fragment = Prawn::Text::Formatted::Fragment.new("hello world ",
19
- format_state,
20
- @pdf)
21
- expect(fragment.space_count).to eq(1)
22
- end
23
- end
24
-
25
- describe "Text::Formatted::Fragment#include_trailing_white_space!" do
26
- it "should make the fragment include trailing white space" do
27
- create_pdf
28
- format_state = { :exclude_trailing_white_space => true }
29
- fragment = Prawn::Text::Formatted::Fragment.new("hello world ",
30
- format_state,
31
- @pdf)
32
- expect(fragment.space_count).to eq(1)
33
- fragment.include_trailing_white_space!
34
- expect(fragment.space_count).to eq(2)
35
- end
36
- end
37
-
38
- describe "Text::Formatted::Fragment#text" do
39
- it "should return the fragment text" do
40
- create_pdf
41
- format_state = {}
42
- fragment = Prawn::Text::Formatted::Fragment.new("hello world ",
43
- format_state,
44
- @pdf)
45
- expect(fragment.text).to eq("hello world ")
46
- end
47
- it "should return the fragment text without trailing spaces when " \
48
- ":exclude_trailing_white_space => true" do
49
- create_pdf
50
- format_state = { :exclude_trailing_white_space => true }
51
- fragment = Prawn::Text::Formatted::Fragment.new("hello world ",
52
- format_state,
53
- @pdf)
54
- expect(fragment.text).to eq("hello world")
55
- end
56
- end
57
-
58
- describe "Text::Formatted::Fragment#word_spacing=" do
59
- before(:each) do
60
- create_pdf
61
- format_state = { :styles => [:bold, :italic],
62
- :color => nil,
63
- :link => nil,
64
- :anchor => nil,
65
- :font => nil,
66
- :size => nil }
67
- @fragment = Prawn::Text::Formatted::Fragment.new("hello world",
68
- format_state,
69
- @pdf)
70
- @fragment.width = 100
71
- @fragment.left = 50
72
- @fragment.baseline = 200
73
- @fragment.line_height = 27
74
- @fragment.descender = 7
75
- @fragment.ascender = 17
76
- @fragment.word_spacing = 10
77
- end
78
-
79
- it "should account for word_spacing in #width" do
80
- expect(@fragment.width).to eq(110)
81
- end
82
- it "should account for word_spacing in #bounding_box" do
83
- target_box = [50, 193, 160, 217]
84
- expect(@fragment.bounding_box).to eq(target_box)
85
- end
86
- it "should account for word_spacing in #absolute_bounding_box" do
87
- target_box = [50, 193, 160, 217]
88
- target_box[0] += @pdf.bounds.absolute_left
89
- target_box[1] += @pdf.bounds.absolute_bottom
90
- target_box[2] += @pdf.bounds.absolute_left
91
- target_box[3] += @pdf.bounds.absolute_bottom
92
- expect(@fragment.absolute_bounding_box).to eq(target_box)
93
- end
94
- it "should account for word_spacing in #underline_points" do
95
- y = 198.75
96
- target_points = [[50, y], [160, y]]
97
- expect(@fragment.underline_points).to eq(target_points)
98
- end
99
- it "should account for word_spacing in #strikethrough_points" do
100
- y = 200 + @fragment.ascender * 0.3
101
- target_points = [[50, y], [160, y]]
102
- expect(@fragment.strikethrough_points).to eq(target_points)
103
- end
104
- end
105
-
106
- describe "Text::Formatted::Fragment" do
107
- before(:each) do
108
- create_pdf
109
- format_state = { :styles => [:bold, :italic],
110
- :color => nil,
111
- :link => nil,
112
- :anchor => nil,
113
- :font => nil,
114
- :size => nil }
115
- @fragment = Prawn::Text::Formatted::Fragment.new("hello world",
116
- format_state,
117
- @pdf)
118
- @fragment.width = 100
119
- @fragment.left = 50
120
- @fragment.baseline = 200
121
- @fragment.line_height = 27
122
- @fragment.descender = 7
123
- @fragment.ascender = 17
124
- end
125
-
126
- describe "#width" do
127
- it "should return the width" do
128
- expect(@fragment.width).to eq(100)
129
- end
130
- end
131
-
132
- describe "#styles" do
133
- it "should return the styles array" do
134
- expect(@fragment.styles).to eq([:bold, :italic])
135
- end
136
- it "should never return nil" do
137
- format_state = { :styles => nil,
138
- :color => nil,
139
- :link => nil,
140
- :anchor => nil,
141
- :font => nil,
142
- :size => nil }
143
- fragment = Prawn::Text::Formatted::Fragment.new("hello world",
144
- format_state,
145
- @pdf)
146
- expect(fragment.styles).to eq([])
147
- end
148
- end
149
-
150
- describe "#line_height" do
151
- it "should return the line_height" do
152
- expect(@fragment.line_height).to eq(27)
153
- end
154
- end
155
-
156
- describe "#ascender" do
157
- it "should return the ascender" do
158
- expect(@fragment.ascender).to eq(17)
159
- end
160
- end
161
-
162
- describe "#descender" do
163
- it "should return the descender" do
164
- expect(@fragment.descender).to eq(7)
165
- end
166
- end
167
-
168
- describe "#y_offset" do
169
- it "should be zero" do
170
- expect(@fragment.y_offset).to eq(0)
171
- end
172
- end
173
-
174
- describe "#bounding_box" do
175
- it "should return the bounding box surrounding the fragment" do
176
- target_box = [50, 193, 150, 217]
177
- expect(@fragment.bounding_box).to eq(target_box)
178
- end
179
- end
180
-
181
- describe "#absolute_bounding_box" do
182
- it "should return the bounding box surrounding the fragment" \
183
- " in absolute coordinates" do
184
- target_box = [50, 193, 150, 217]
185
- target_box[0] += @pdf.bounds.absolute_left
186
- target_box[1] += @pdf.bounds.absolute_bottom
187
- target_box[2] += @pdf.bounds.absolute_left
188
- target_box[3] += @pdf.bounds.absolute_bottom
189
-
190
- expect(@fragment.absolute_bounding_box).to eq(target_box)
191
- end
192
- end
193
-
194
- describe "#underline_points" do
195
- it "should define a line under the fragment" do
196
- y = 198.75
197
- target_points = [[50, y], [150, y]]
198
- expect(@fragment.underline_points).to eq(target_points)
199
- end
200
- end
201
-
202
- describe "#strikethrough_points" do
203
- it "should define a line through the fragment" do
204
- y = 200 + @fragment.ascender * 0.3
205
- target_points = [[50, y], [150, y]]
206
- expect(@fragment.strikethrough_points).to eq(target_points)
207
- end
208
- end
209
- end
210
-
211
- describe "Text::Formatted::Fragment that is a subscript" do
212
- before(:each) do
213
- create_pdf
214
- format_state = { :styles => [:subscript],
215
- :color => nil,
216
- :link => nil,
217
- :anchor => nil,
218
- :font => nil,
219
- :size => nil }
220
- @fragment = Prawn::Text::Formatted::Fragment.new("hello world",
221
- format_state,
222
- @pdf)
223
- @fragment.line_height = 27
224
- @fragment.descender = 7
225
- @fragment.ascender = 17
226
- end
227
- describe "#subscript?" do
228
- it "should be_true" do
229
- expect(@fragment).to be_subscript
230
- end
231
- end
232
- describe "#y_offset" do
233
- it "should return a negative value" do
234
- expect(@fragment.y_offset).to be < 0
235
- end
236
- end
237
- end
238
-
239
- describe "Text::Formatted::Fragment that is a superscript" do
240
- before(:each) do
241
- create_pdf
242
- format_state = { :styles => [:superscript],
243
- :color => nil,
244
- :link => nil,
245
- :anchor => nil,
246
- :font => nil,
247
- :size => nil }
248
- @fragment = Prawn::Text::Formatted::Fragment.new("hello world",
249
- format_state,
250
- @pdf)
251
- @fragment.line_height = 27
252
- @fragment.descender = 7
253
- @fragment.ascender = 17
254
- end
255
- describe "#superscript?" do
256
- it "should be_true" do
257
- expect(@fragment).to be_superscript
258
- end
259
- end
260
- describe "#y_offset" do
261
- it "should return a positive value" do
262
- expect(@fragment.y_offset).to be > 0
263
- end
264
- end
265
- end
266
-
267
- describe "Text::Formatted::Fragment with :direction => :rtl" do
268
- it "#text should be reversed" do
269
- create_pdf
270
- format_state = { :direction => :rtl }
271
- fragment = Prawn::Text::Formatted::Fragment.new("hello world",
272
- format_state,
273
- @pdf)
274
- expect(fragment.text).to eq("dlrow olleh")
275
- end
276
- end
277
-
278
- describe "Text::Formatted::Fragment default_direction=" do
279
- it "should set the direction if there is no fragment level direction " \
280
- "specification" do
281
- create_pdf
282
- format_state = {}
283
- fragment = Prawn::Text::Formatted::Fragment.new("hello world",
284
- format_state,
285
- @pdf)
286
- fragment.default_direction = :rtl
287
- expect(fragment.direction).to eq(:rtl)
288
- end
289
- it "should not set the direction if there is a fragment level direction " \
290
- "specification" do
291
- create_pdf
292
- format_state = { :direction => :rtl }
293
- fragment = Prawn::Text::Formatted::Fragment.new("hello world",
294
- format_state,
295
- @pdf)
296
- fragment.default_direction = :ltr
297
- expect(fragment.direction).to eq(:rtl)
298
- end
299
- end
@@ -1,666 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
-
5
- describe "When drawing a line" do
6
- before(:each) { create_pdf }
7
-
8
- it "should draw a line from (100,600) to (100,500)" do
9
- @pdf.line([100, 600], [100, 500])
10
-
11
- line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
12
-
13
- expect(line_drawing.points).to eq([[100, 600], [100, 500]])
14
- end
15
-
16
- it "should draw two lines at (100,600) to (100,500) " \
17
- "and (75,100) to (50,125)" do
18
- @pdf.line(100, 600, 100, 500)
19
- @pdf.line(75, 100, 50, 125)
20
-
21
- line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
22
-
23
- expect(line_drawing.points).to eq(
24
- [[100.0, 600.0], [100.0, 500.0], [75.0, 100.0], [50.0, 125.0]]
25
- )
26
- end
27
-
28
- it "should properly set line width via line_width=" do
29
- @pdf.line_width = 10
30
- line = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
31
- expect(line.widths.first).to eq(10)
32
- end
33
-
34
- it "should properly set line width via line_width(width)" do
35
- @pdf.line_width(10)
36
- line = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
37
- expect(line.widths.first).to eq(10)
38
- end
39
-
40
- it "should carry the current line width settings over to new pages" do
41
- @pdf.line_width(10)
42
- @pdf.start_new_page
43
- line = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
44
- expect(line.widths.length).to eq(2)
45
- expect(line.widths[1]).to eq(10)
46
- end
47
-
48
- describe "(Horizontally)" do
49
- it "should draw from [x1,pdf.y],[x2,pdf.y]" do
50
- @pdf.horizontal_line(100, 150)
51
- @line = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
52
- expect(@line.points).to eq([[100.0 + @pdf.bounds.absolute_left, @pdf.y],
53
- [150.0 + @pdf.bounds.absolute_left, @pdf.y]])
54
- end
55
-
56
- it "should draw a line from (200, 250) to (300, 250)" do
57
- @pdf.horizontal_line(200, 300, :at => 250)
58
- line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
59
- expect(line_drawing.points).to eq([[200, 250], [300, 250]])
60
- end
61
- end
62
-
63
- describe "(Vertically)" do
64
- it "should draw a line from (350, 300) to (350, 400)" do
65
- @pdf.vertical_line(300, 400, :at => 350)
66
- line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
67
- expect(line_drawing.points).to eq([[350, 300], [350, 400]])
68
- end
69
- it "should require a y coordinate" do
70
- expect { @pdf.vertical_line(400, 500) }.
71
- to raise_error(ArgumentError)
72
- end
73
- end
74
- end
75
-
76
- describe "When drawing a polygon" do
77
- before(:each) { create_pdf }
78
-
79
- it "should draw each line passed to polygon()" do
80
- @pdf.polygon([100, 500], [100, 400], [200, 400])
81
-
82
- line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
83
- expect(line_drawing.points).to eq([[100, 500], [100, 400], [200, 400], [100, 500]])
84
- end
85
- end
86
-
87
- describe "When drawing a rectangle" do
88
- before(:each) { create_pdf }
89
-
90
- it "should use a point, width, and height for coords" do
91
- @pdf.rectangle [200, 200], 50, 100
92
-
93
- rectangles = PDF::Inspector::Graphics::Rectangle.analyze(@pdf.render).rectangles
94
- # PDF uses bottom left corner
95
- expect(rectangles[0][:point]).to eq([200, 100])
96
- expect(rectangles[0][:width]).to eq(50)
97
- expect(rectangles[0][:height]).to eq(100)
98
- end
99
- end
100
-
101
- describe "When drawing a curve" do
102
- before(:each) { create_pdf }
103
-
104
- it "should draw a bezier curve from 50,50 to 100,100" do
105
- @pdf.move_to [50, 50]
106
- @pdf.curve_to [100, 100], :bounds => [[20, 90], [90, 70]]
107
- curve = PDF::Inspector::Graphics::Curve.analyze(@pdf.render)
108
- expect(curve.coords).to eq([50.0, 50.0, 20.0, 90.0, 90.0, 70.0, 100.0, 100.0])
109
- end
110
-
111
- it "should draw a bezier curve from 100,100 to 50,50" do
112
- @pdf.curve [100, 100], [50, 50], :bounds => [[20, 90], [90, 75]]
113
- curve = PDF::Inspector::Graphics::Curve.analyze(@pdf.render)
114
- expect(curve.coords).to eq([100.0, 100.0, 20.0, 90.0, 90.0, 75.0, 50.0, 50.0])
115
- end
116
- end
117
-
118
- describe "When drawing a rounded rectangle" do
119
- before(:each) do
120
- create_pdf
121
- @pdf.rounded_rectangle([50, 550], 50, 100, 10)
122
- curve = PDF::Inspector::Graphics::Curve.analyze(@pdf.render)
123
- curve_points = []
124
- curve.coords.each_slice(2) { |p| curve_points << p }
125
- @original_point = curve_points.shift
126
- curves = []
127
- curve_points.each_slice(3) { |c| curves << c }
128
- line_points = PDF::Inspector::Graphics::Line.analyze(@pdf.render).points
129
- line_points.shift
130
- @all_coords = []
131
- line_points.zip(curves).flatten.each_slice(2) { |p| @all_coords << p }
132
- @all_coords.unshift @original_point
133
- end
134
-
135
- it "should draw a rectangle by connecting lines with rounded bezier curves" do
136
- expect(@all_coords).to eq([[60.0, 550.0], [90.0, 550.0], [95.5228, 550.0],
137
- [100.0, 545.5228], [100.0, 540.0], [100.0, 460.0],
138
- [100.0, 454.4772], [95.5228, 450.0], [90.0, 450.0],
139
- [60.0, 450.0], [54.4772, 450.0], [50.0, 454.4772],
140
- [50.0, 460.0], [50.0, 540.0], [50.0, 545.5228],
141
- [54.4772, 550.0], [60.0, 550.0]])
142
- end
143
-
144
- it "should start and end with the same point" do
145
- expect(@original_point).to eq(@all_coords.last)
146
- end
147
- end
148
-
149
- describe "When drawing an ellipse" do
150
- before(:each) do
151
- create_pdf
152
- @pdf.ellipse [100, 100], 25, 50
153
- @curve = PDF::Inspector::Graphics::Curve.analyze(@pdf.render)
154
- end
155
-
156
- it "should use a Bézier approximation" do
157
- expect(@curve.coords).to eq(
158
- [125.0, 100.0,
159
-
160
- 125.0, 127.6142,
161
- 113.8071, 150,
162
- 100.0, 150.0,
163
-
164
- 86.1929, 150.0,
165
- 75.0, 127.6142,
166
- 75.0, 100.0,
167
-
168
- 75.0, 72.3858,
169
- 86.1929, 50.0,
170
- 100.0, 50.0,
171
-
172
- 113.8071, 50.0,
173
- 125.0, 72.3858,
174
- 125.0, 100.0,
175
-
176
- 100.0, 100.0]
177
- )
178
- end
179
-
180
- it "should move the pointer to the center of the ellipse after drawing" do
181
- expect(@curve.coords[-2..-1]).to eq([100, 100])
182
- end
183
- end
184
-
185
- describe "When drawing a circle" do
186
- before(:each) do
187
- create_pdf
188
- @pdf.circle [100, 100], 25
189
- @pdf.ellipse [100, 100], 25, 25
190
- @curve = PDF::Inspector::Graphics::Curve.analyze(@pdf.render)
191
- end
192
-
193
- it "should stroke the same path as the equivalent ellipse" do
194
- middle = @curve.coords.length / 2
195
- expect(@curve.coords[0...middle]).to eq(@curve.coords[middle..-1])
196
- end
197
- end
198
-
199
- describe "When filling" do
200
- before(:each) { create_pdf }
201
-
202
- it "should default to the f operator (nonzero winding number rule)" do
203
- @pdf.renderer.expects(:add_content).with("f")
204
- @pdf.fill
205
- end
206
-
207
- it "should use f* for :fill_rule => :even_odd" do
208
- @pdf.renderer.expects(:add_content).with("f*")
209
- @pdf.fill(:fill_rule => :even_odd)
210
- end
211
-
212
- it "should use b by default for fill_and_stroke (nonzero winding number)" do
213
- @pdf.renderer.expects(:add_content).with("b")
214
- @pdf.fill_and_stroke
215
- end
216
-
217
- it "should use b* for fill_and_stroke(:fill_rule => :even_odd)" do
218
- @pdf.renderer.expects(:add_content).with("b*")
219
- @pdf.fill_and_stroke(:fill_rule => :even_odd)
220
- end
221
- end
222
-
223
- describe "When setting colors" do
224
- before(:each) { create_pdf }
225
-
226
- it "should set stroke colors" do
227
- @pdf.stroke_color "ffcccc"
228
- colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
229
- # 100% red, 80% green, 80% blue
230
- expect(colors.stroke_color).to eq([1.0, 0.8, 0.8])
231
- end
232
-
233
- it "should set fill colors" do
234
- @pdf.fill_color "ccff00"
235
- colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
236
- # 80% red, 100% green, 0% blue
237
- expect(colors.fill_color).to eq([0.8, 1.0, 0])
238
- end
239
-
240
- it "should reset the colors on each new page if they have been defined" do
241
- @pdf.fill_color "ccff00"
242
-
243
- @pdf.start_new_page
244
- @pdf.stroke_color "ff00cc"
245
-
246
- @pdf.start_new_page
247
- colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
248
- expect(colors.fill_color_count).to eq(3)
249
- expect(colors.stroke_color_count).to eq(2)
250
-
251
- expect(colors.fill_color).to eq([0.8, 1.0, 0.0])
252
- expect(colors.stroke_color).to eq([1.0, 0.0, 0.8])
253
- end
254
-
255
- it "should set the color space when setting colors on new pages to please fussy readers" do
256
- @pdf.stroke_color "000000"
257
- @pdf.stroke { @pdf.rectangle([10, 10], 10, 10) }
258
- @pdf.start_new_page
259
- @pdf.stroke_color "000000"
260
- @pdf.stroke { @pdf.rectangle([10, 10], 10, 10) }
261
- colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
262
- expect(colors.stroke_color_space_count[:DeviceRGB]).to eq(2)
263
- end
264
- end
265
-
266
- describe "Patterns" do
267
- before(:each) { create_pdf }
268
-
269
- describe 'linear gradients' do
270
- it "should create a /Pattern resource" do
271
- @pdf.fill_gradient [0, @pdf.bounds.height],
272
- [@pdf.bounds.width, @pdf.bounds.height],
273
- 'FF0000', '0000FF'
274
-
275
- grad = PDF::Inspector::Graphics::Pattern.analyze(@pdf.render)
276
- pattern = grad.patterns.values.first
277
-
278
- expect(pattern).not_to be_nil
279
- expect(pattern[:Shading][:ShadingType]).to eq(2)
280
- expect(pattern[:Shading][:Coords]).to eq([0, 0, @pdf.bounds.width, 0])
281
- expect(pattern[:Shading][:Function][:C0].zip([1, 0, 0]).all?{ |x1, x2|
282
- (x1 - x2).abs < 0.01
283
- }).to be_true
284
- expect(pattern[:Shading][:Function][:C1].zip([0, 0, 1]).all?{ |x1, x2|
285
- (x1 - x2).abs < 0.01
286
- }).to be_true
287
- end
288
-
289
- it "fill_gradient should set fill color to the pattern" do
290
- @pdf.fill_gradient [0, @pdf.bounds.height],
291
- [@pdf.bounds.width, @pdf.bounds.height],
292
- 'FF0000', '0000FF'
293
-
294
- str = @pdf.render
295
- expect(str).to match(%r{/Pattern\s+cs\s*/SP-?\d+\s+scn})
296
- end
297
-
298
- it "stroke_gradient should set stroke color to the pattern" do
299
- @pdf.stroke_gradient [0, @pdf.bounds.height],
300
- [@pdf.bounds.width, @pdf.bounds.height],
301
- 'FF0000', '0000FF'
302
-
303
- str = @pdf.render
304
- expect(str).to match(%r{/Pattern\s+CS\s*/SP-?\d+\s+SCN})
305
- end
306
- end
307
-
308
- describe 'radial gradients' do
309
- it "should create a /Pattern resource" do
310
- @pdf.fill_gradient [0, @pdf.bounds.height], 10,
311
- [@pdf.bounds.width, @pdf.bounds.height], 20,
312
- 'FF0000', '0000FF'
313
-
314
- grad = PDF::Inspector::Graphics::Pattern.analyze(@pdf.render)
315
- pattern = grad.patterns.values.first
316
-
317
- expect(pattern).not_to be_nil
318
- expect(pattern[:Shading][:ShadingType]).to eq(3)
319
- expect(pattern[:Shading][:Coords]).to eq([0, 0, 10, @pdf.bounds.width, 0, 20])
320
- expect(pattern[:Shading][:Function][:C0].zip([1, 0, 0]).all?{ |x1, x2|
321
- (x1 - x2).abs < 0.01
322
- }).to be_true
323
- expect(pattern[:Shading][:Function][:C1].zip([0, 0, 1]).all?{ |x1, x2|
324
- (x1 - x2).abs < 0.01
325
- }).to be_true
326
- end
327
-
328
- it "fill_gradient should set fill color to the pattern" do
329
- @pdf.fill_gradient [0, @pdf.bounds.height], 10,
330
- [@pdf.bounds.width, @pdf.bounds.height], 20,
331
- 'FF0000', '0000FF'
332
-
333
- str = @pdf.render
334
- expect(str).to match(%r{/Pattern\s+cs\s*/SP-?\d+\s+scn})
335
- end
336
-
337
- it "stroke_gradient should set stroke color to the pattern" do
338
- @pdf.stroke_gradient [0, @pdf.bounds.height], 10,
339
- [@pdf.bounds.width, @pdf.bounds.height], 20,
340
- 'FF0000', '0000FF'
341
-
342
- str = @pdf.render
343
- expect(str).to match(%r{/Pattern\s+CS\s*/SP-?\d+\s+SCN})
344
- end
345
- end
346
- end
347
-
348
- describe "When using painting shortcuts" do
349
- before(:each) { create_pdf }
350
-
351
- it "should convert stroke_some_method(args) into some_method(args); stroke" do
352
- @pdf.expects(:line_to).with([100, 100])
353
- @pdf.expects(:stroke)
354
-
355
- @pdf.stroke_line_to [100, 100]
356
- end
357
-
358
- it "should convert fill_some_method(args) into some_method(args); fill" do
359
- @pdf.expects(:line_to).with([100, 100])
360
- @pdf.expects(:fill)
361
-
362
- @pdf.fill_line_to [100, 100]
363
- end
364
-
365
- it "should not break method_missing" do
366
- expect { @pdf.i_have_a_pretty_girlfriend_named_jia }.
367
- to raise_error(NoMethodError)
368
- end
369
- end
370
-
371
- describe "When using graphics states" do
372
- before(:each) { create_pdf }
373
-
374
- it "should add the right content on save_graphics_state" do
375
- @pdf.renderer.expects(:add_content).with('q')
376
-
377
- @pdf.save_graphics_state
378
- end
379
-
380
- it "should add the right content on restore_graphics_state" do
381
- @pdf.renderer.expects(:add_content).with('Q')
382
-
383
- @pdf.restore_graphics_state
384
- end
385
-
386
- it "should save and restore when save_graphics_state is used with a block" do
387
- state = sequence "state"
388
- @pdf.renderer.expects(:add_content).with('q').in_sequence(state)
389
- @pdf.expects(:foo).in_sequence(state)
390
- @pdf.renderer.expects(:add_content).with('Q').in_sequence(state)
391
-
392
- @pdf.save_graphics_state do
393
- @pdf.foo
394
- end
395
- end
396
-
397
- it "should add the previous color space when restoring to a graphic state with different color space" do
398
- @pdf.stroke_color '000000'
399
- @pdf.save_graphics_state
400
- @pdf.stroke_color 0, 0, 0, 0
401
- @pdf.restore_graphics_state
402
- @pdf.stroke_color 0, 0, 100, 0
403
- expect(@pdf.graphic_state.color_space).to eq(:stroke => :DeviceCMYK)
404
- colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
405
- expect(colors.color_space).to eq(:DeviceCMYK)
406
- expect(colors.stroke_color_space_count[:DeviceCMYK]).to eq(2)
407
- end
408
-
409
- it "should use the correct dash setting after restoring and starting new page" do
410
- @pdf.dash 5
411
- @pdf.save_graphics_state
412
- @pdf.dash 10
413
- expect(@pdf.graphic_state.dash[:dash]).to eq(10)
414
- @pdf.restore_graphics_state
415
- @pdf.start_new_page
416
- expect(@pdf.graphic_state.dash[:dash]).to eq(5)
417
- end
418
-
419
- it "should round dash values to four decimal places" do
420
- @pdf.dash 5.12345
421
- expect(@pdf.graphic_state.dash_setting).to eq("[5.1235 5.1235] 0.0 d")
422
- end
423
-
424
- it "should raise an error when dash is called w. a zero length or space" do
425
- expect { @pdf.dash(0) }.to raise_error(ArgumentError)
426
- expect { @pdf.dash([0]) }.to raise_error(ArgumentError)
427
- expect { @pdf.dash([0, 0]) }.to raise_error(ArgumentError)
428
- expect { @pdf.dash([0, 0, 0, 1]) }.to raise_error(ArgumentError)
429
- end
430
-
431
- it "the current graphic state should keep track of previous unchanged settings" do
432
- @pdf.stroke_color '000000'
433
- @pdf.save_graphics_state
434
- @pdf.dash 5
435
- @pdf.save_graphics_state
436
- @pdf.cap_style :round
437
- @pdf.save_graphics_state
438
- @pdf.fill_color 0, 0, 100, 0
439
- @pdf.save_graphics_state
440
-
441
- expect(@pdf.graphic_state.stroke_color).to eq("000000")
442
- expect(@pdf.graphic_state.join_style).to eq(:miter)
443
- expect(@pdf.graphic_state.fill_color).to eq([0, 0, 100, 0])
444
- expect(@pdf.graphic_state.cap_style).to eq(:round)
445
- expect(@pdf.graphic_state.color_space).to eq(:fill => :DeviceCMYK, :stroke => :DeviceRGB)
446
- expect(@pdf.graphic_state.dash).to eq(:space => 5, :phase => 0, :dash => 5)
447
- expect(@pdf.graphic_state.line_width).to eq(1)
448
- end
449
-
450
- it "should not add extra graphic space closings when rendering multiple times" do
451
- @pdf.render
452
- state = PDF::Inspector::Graphics::State.analyze(@pdf.render)
453
- expect(state.save_graphics_state_count).to eq(1)
454
- expect(state.restore_graphics_state_count).to eq(1)
455
- end
456
-
457
- it "should add extra graphic state enclosings when content is added on multiple renderings" do
458
- @pdf.render
459
- @pdf.text "Adding a bit more content"
460
- state = PDF::Inspector::Graphics::State.analyze(@pdf.render)
461
- expect(state.save_graphics_state_count).to eq(2)
462
- expect(state.restore_graphics_state_count).to eq(2)
463
- end
464
-
465
- it "adds extra graphic state enclosings when new settings are applied on multiple renderings" do
466
- @pdf.render
467
- @pdf.stroke_color 0, 0, 0, 0
468
- state = PDF::Inspector::Graphics::State.analyze(@pdf.render)
469
- expect(state.save_graphics_state_count).to eq(2)
470
- expect(state.restore_graphics_state_count).to eq(2)
471
- end
472
-
473
- it "should raise_error error if closing an empty graphic stack" do
474
- expect {
475
- @pdf.render
476
- @pdf.restore_graphics_state
477
- }.to raise_error(PDF::Core::Errors::EmptyGraphicStateStack)
478
- end
479
-
480
- it "should copy mutable attributes when passing a previous_state to the initializer" do
481
- new_state = PDF::Core::GraphicState.new(@pdf.graphic_state)
482
-
483
- [:color_space, :dash, :fill_color, :stroke_color].each do |attr|
484
- expect(new_state.send(attr)).to eq(@pdf.graphic_state.send(attr))
485
- expect(new_state.send(attr)).not_to equal(@pdf.graphic_state.send(attr))
486
- end
487
- end
488
-
489
- it "should copy mutable attributes when duping" do
490
- new_state = @pdf.graphic_state.dup
491
-
492
- [:color_space, :dash, :fill_color, :stroke_color].each do |attr|
493
- expect(new_state.send(attr)).to eq(@pdf.graphic_state.send(attr))
494
- expect(new_state.send(attr)).not_to equal(@pdf.graphic_state.send(attr))
495
- end
496
- end
497
- end
498
-
499
- describe "When using transformation matrix" do
500
- before(:each) { create_pdf }
501
-
502
- # Note: The (approximate) number of significant decimal digits of precision in fractional
503
- # part is 5 (PDF Reference, Third Edition, p. 706)
504
-
505
- it "should send the right content on transformation_matrix" do
506
- @pdf.renderer.expects(:add_content).with('1.00000 0.00000 0.12346 -1.00000 5.50000 20.00000 cm')
507
- @pdf.transformation_matrix 1, 0, 0.123456789, -1.0, 5.5, 20
508
- end
509
-
510
- it "should use fixed digits with very small number" do
511
- values = Array.new(6, 0.000000000001)
512
- string = Array.new(6, "0.00000").join " "
513
- @pdf.renderer.expects(:add_content).with("#{string} cm")
514
- @pdf.transformation_matrix(*values)
515
- end
516
-
517
- it "should be received by the inspector" do
518
- @pdf.transformation_matrix 1, 0, 0, -1, 5.5, 20
519
- matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
520
- expect(matrices.matrices).to eq([[1, 0, 0, -1, 5.5, 20]])
521
- end
522
-
523
- it "should save the graphics state inside the given block" do
524
- values = Array.new(6, 0.000000000001)
525
- string = Array.new(6, "0.00000").join " "
526
- process = sequence "process"
527
-
528
- @pdf.expects(:save_graphics_state).with.in_sequence(process)
529
- @pdf.renderer.expects(:add_content).with("#{string} cm").in_sequence(process)
530
- @pdf.expects(:do_something).with.in_sequence(process)
531
- @pdf.expects(:restore_graphics_state).with.in_sequence(process)
532
- @pdf.transformation_matrix(*values) do
533
- @pdf.do_something
534
- end
535
- end
536
- end
537
-
538
- describe "When using transformations shortcuts" do
539
- before(:each) do
540
- create_pdf
541
- @x, @y = 12, 54.32
542
- @angle = 12.32
543
- @cos = Math.cos(@angle * Math::PI / 180)
544
- @sin = Math.sin(@angle * Math::PI / 180)
545
- @factor = 0.12
546
- end
547
-
548
- describe "#rotate" do
549
- it "should rotate" do
550
- @pdf.expects(:transformation_matrix).with(@cos, @sin, -@sin, @cos, 0, 0)
551
- @pdf.rotate(@angle)
552
- end
553
- end
554
-
555
- describe "#rotate with :origin option" do
556
- it "should rotate around the origin" do
557
- x_prime = @x * @cos - @y * @sin
558
- y_prime = @x * @sin + @y * @cos
559
-
560
- @pdf.rotate(@angle, :origin => [@x, @y]) { @pdf.text('hello world') }
561
-
562
- matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
563
- expect(matrices.matrices[0]).to eq([1, 0, 0, 1,
564
- reduce_precision(@x - x_prime),
565
- reduce_precision(@y - y_prime)])
566
- expect(matrices.matrices[1]).to eq([reduce_precision(@cos),
567
- reduce_precision(@sin),
568
- reduce_precision(-@sin),
569
- reduce_precision(@cos), 0, 0])
570
- end
571
-
572
- it "should rotate around the origin in a document with a margin" do
573
- @pdf = Prawn::Document.new
574
-
575
- @pdf.rotate(@angle, :origin => [@x, @y]) { @pdf.text('hello world') }
576
-
577
- x = @x + @pdf.bounds.absolute_left
578
- y = @y + @pdf.bounds.absolute_bottom
579
- x_prime = x * @cos - y * @sin
580
- y_prime = x * @sin + y * @cos
581
-
582
- matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
583
- expect(matrices.matrices[0]).to eq([1, 0, 0, 1,
584
- reduce_precision(x - x_prime),
585
- reduce_precision(y - y_prime)])
586
- expect(matrices.matrices[1]).to eq([reduce_precision(@cos),
587
- reduce_precision(@sin),
588
- reduce_precision(-@sin),
589
- reduce_precision(@cos), 0, 0])
590
- end
591
-
592
- it "should raise_error BlockRequired if no block is given" do
593
- expect {
594
- @pdf.rotate(@angle, :origin => [@x, @y])
595
- }.to raise_error(Prawn::Errors::BlockRequired)
596
- end
597
-
598
- def reduce_precision(float)
599
- ("%.5f" % float).to_f
600
- end
601
- end
602
-
603
- describe "#translate" do
604
- it "should translate" do
605
- x, y = 12, 54.32
606
- @pdf.expects(:transformation_matrix).with(1, 0, 0, 1, x, y)
607
- @pdf.translate(x, y)
608
- end
609
- end
610
-
611
- describe "#scale" do
612
- it "should scale" do
613
- @pdf.expects(:transformation_matrix).with(@factor, 0, 0, @factor, 0, 0)
614
- @pdf.scale(@factor)
615
- end
616
- end
617
-
618
- describe "#scale with :origin option" do
619
- it "should scale from the origin" do
620
- x_prime = @factor * @x
621
- y_prime = @factor * @y
622
-
623
- @pdf.scale(@factor, :origin => [@x, @y]) { @pdf.text('hello world') }
624
-
625
- matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
626
- expect(matrices.matrices[0]).to eq([1, 0, 0, 1,
627
- reduce_precision(@x - x_prime),
628
- reduce_precision(@y - y_prime)])
629
- expect(matrices.matrices[1]).to eq([@factor, 0, 0, @factor, 0, 0])
630
- end
631
-
632
- it "should scale from the origin in a document with a margin" do
633
- @pdf = Prawn::Document.new
634
- x = @x + @pdf.bounds.absolute_left
635
- y = @y + @pdf.bounds.absolute_bottom
636
- x_prime = @factor * x
637
- y_prime = @factor * y
638
-
639
- @pdf.scale(@factor, :origin => [@x, @y]) { @pdf.text('hello world') }
640
-
641
- matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
642
- expect(matrices.matrices[0]).to eq([1, 0, 0, 1,
643
- reduce_precision(x - x_prime),
644
- reduce_precision(y - y_prime)])
645
- expect(matrices.matrices[1]).to eq([@factor, 0, 0, @factor, 0, 0])
646
- end
647
-
648
- it "should raise_error BlockRequired if no block is given" do
649
- expect {
650
- @pdf.scale(@factor, :origin => [@x, @y])
651
- }.to raise_error(Prawn::Errors::BlockRequired)
652
- end
653
-
654
- def reduce_precision(float)
655
- ("%.5f" % float).to_f
656
- end
657
- end
658
-
659
- # describe "skew" do
660
- # it "should skew" do
661
- # a, b = 30, 50.2
662
- # @pdf.expects(:transformation_matrix).with(1, Math.tan(a * Math::PI / 180), Math.tan(b * Math::PI / 180), 1, 0, 0)
663
- # @pdf.skew(a, b)
664
- # end
665
- # end
666
- end