prawn 2.1.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (280) 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 +36 -49
  8. data/lib/prawn/document.rb +180 -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 +8 -6
  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 +102 -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 +119 -81
  23. data/lib/prawn/graphics/blend_mode.rb +9 -8
  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 +190 -96
  29. data/lib/prawn/graphics/transformation.rb +15 -9
  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 +65 -62
  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 +53 -32
  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 +122 -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 +7 -5
  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 +12 -9
  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 -40
  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 +3 -4
  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 +44 -31
  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/{image_handler_spec.rb → prawn/image_handler_spec.rb} +14 -14
  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/{transformation_stack_spec.rb → prawn/transformation_stack_spec.rb} +22 -19
  195. data/spec/prawn/view_spec.rb +63 -0
  196. data/spec/prawn_manual_spec.rb +35 -0
  197. data/spec/spec_helper.rb +18 -19
  198. metadata +144 -180
  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/blend_modes_bottom_layer.jpg +0 -0
  206. data/data/images/blend_modes_top_layer.jpg +0 -0
  207. data/data/images/dice.alpha +0 -0
  208. data/data/images/dice.color +0 -0
  209. data/data/images/dice.png +0 -0
  210. data/data/images/dice_interlaced.png +0 -0
  211. data/data/images/fractal.jpg +0 -0
  212. data/data/images/indexed_color.dat +0 -0
  213. data/data/images/indexed_color.png +0 -0
  214. data/data/images/indexed_transparency.png +0 -0
  215. data/data/images/indexed_transparency_alpha.dat +0 -0
  216. data/data/images/indexed_transparency_color.dat +0 -0
  217. data/data/images/letterhead.jpg +0 -0
  218. data/data/images/license.md +0 -8
  219. data/data/images/page_white_text.alpha +0 -0
  220. data/data/images/page_white_text.color +0 -0
  221. data/data/images/page_white_text.png +0 -0
  222. data/data/images/pigs.jpg +0 -0
  223. data/data/images/prawn.png +0 -0
  224. data/data/images/ruport.png +0 -0
  225. data/data/images/ruport_data.dat +0 -0
  226. data/data/images/ruport_transparent.png +0 -0
  227. data/data/images/ruport_type0.png +0 -0
  228. data/data/images/stef.jpg +0 -0
  229. data/data/images/tru256.bmp +0 -0
  230. data/data/images/web-links.dat +0 -1
  231. data/data/images/web-links.png +0 -0
  232. data/data/pdfs/complex_template.pdf +0 -0
  233. data/data/pdfs/contains_ttf_font.pdf +0 -0
  234. data/data/pdfs/encrypted.pdf +0 -0
  235. data/data/pdfs/form.pdf +1 -819
  236. data/data/pdfs/hexagon.pdf +0 -61
  237. data/data/pdfs/indirect_reference.pdf +0 -86
  238. data/data/pdfs/multipage_template.pdf +0 -127
  239. data/data/pdfs/nested_pages.pdf +0 -118
  240. data/data/pdfs/page_without_mediabox.pdf +0 -193
  241. data/data/pdfs/resources_as_indirect_object.pdf +0 -83
  242. data/data/pdfs/two_hexagons.pdf +0 -90
  243. data/data/pdfs/version_1_6.pdf +0 -61
  244. data/data/shift_jis_text.txt +0 -1
  245. data/spec/acceptance/png_spec.rb +0 -35
  246. data/spec/annotations_spec.rb +0 -67
  247. data/spec/blend_mode_spec.rb +0 -71
  248. data/spec/bounding_box_spec.rb +0 -501
  249. data/spec/column_box_spec.rb +0 -59
  250. data/spec/destinations_spec.rb +0 -13
  251. data/spec/document_spec.rb +0 -738
  252. data/spec/font_metric_cache_spec.rb +0 -52
  253. data/spec/font_spec.rb +0 -475
  254. data/spec/formatted_text_arranger_spec.rb +0 -452
  255. data/spec/formatted_text_box_spec.rb +0 -716
  256. data/spec/formatted_text_fragment_spec.rb +0 -299
  257. data/spec/graphics_spec.rb +0 -705
  258. data/spec/grid_spec.rb +0 -95
  259. data/spec/images_spec.rb +0 -167
  260. data/spec/inline_formatted_text_parser_spec.rb +0 -568
  261. data/spec/jpg_spec.rb +0 -23
  262. data/spec/line_wrap_spec.rb +0 -366
  263. data/spec/measurement_units_spec.rb +0 -22
  264. data/spec/outline_spec.rb +0 -409
  265. data/spec/png_spec.rb +0 -257
  266. data/spec/reference_spec.rb +0 -25
  267. data/spec/repeater_spec.rb +0 -154
  268. data/spec/security_spec.rb +0 -151
  269. data/spec/soft_mask_spec.rb +0 -78
  270. data/spec/span_spec.rb +0 -43
  271. data/spec/stamp_spec.rb +0 -179
  272. data/spec/stroke_styles_spec.rb +0 -208
  273. data/spec/text_at_spec.rb +0 -142
  274. data/spec/text_box_spec.rb +0 -1042
  275. data/spec/text_rendering_mode_spec.rb +0 -45
  276. data/spec/text_spacing_spec.rb +0 -93
  277. data/spec/text_spec.rb +0 -543
  278. data/spec/text_with_inline_formatting_spec.rb +0 -35
  279. data/spec/transparency_spec.rb +0 -91
  280. 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,705 +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
- expect(@pdf.renderer).to receive(:add_content).with("f")
204
- @pdf.fill
205
- end
206
-
207
- it "should use f* for :fill_rule => :even_odd" do
208
- expect(@pdf.renderer).to receive(: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
- expect(@pdf.renderer).to receive(: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
- expect(@pdf.renderer).to receive(: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 eq true
284
- expect(pattern[:Shading][:Function][:C1].zip([0, 0, 1]).all?{ |x1, x2|
285
- (x1 - x2).abs < 0.01
286
- }).to eq 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 eq true
323
- expect(pattern[:Shading][:Function][:C1].zip([0, 0, 1]).all?{ |x1, x2|
324
- (x1 - x2).abs < 0.01
325
- }).to eq 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
-
347
- describe "gradient transformations" do
348
- subject do
349
- @pdf.scale 2 do
350
- @pdf.translate 40, 40 do
351
- @pdf.fill_gradient [0, 10], [15, 15], 'FF0000', '0000FF', opts
352
- @pdf.fill_gradient [0, 10], 15, [15, 15], 25, 'FF0000', '0000FF', opts
353
- end
354
- end
355
-
356
- grad = PDF::Inspector::Graphics::Pattern.analyze(@pdf.render)
357
- grad.patterns.values.map { |pattern| pattern[:Matrix] }.uniq
358
- end
359
-
360
- context "when :apply_transformations is true" do
361
- let(:opts) { { apply_transformations: true } }
362
-
363
- it "uses the transformation stack to translate user co-ordinates to document co-ordinates required by /Pattern" do
364
- expect(subject).to eq([[2, 0, 0, 2, 80, 100]])
365
- end
366
- end
367
-
368
- context "when :apply_transformations is false" do
369
- let(:opts) { { apply_transformations: false } }
370
-
371
- it "doesn't transform the gradient" do
372
- expect(subject).to eq([[1, 0, 0, 1, 0, 10]])
373
- end
374
- end
375
-
376
- context "when :apply_transformations is unset" do
377
- let(:opts) { {} }
378
-
379
- it "doesn't transform the gradient and displays a warning" do
380
- expect(@pdf).to receive(:warn).twice
381
- expect(subject).to eq([[1, 0, 0, 1, 0, 10]])
382
- end
383
- end
384
- end
385
- end
386
-
387
- describe "When using painting shortcuts" do
388
- before(:each) { create_pdf }
389
-
390
- it "should convert stroke_some_method(args) into some_method(args); stroke" do
391
- expect(@pdf).to receive(:line_to).with([100, 100])
392
- expect(@pdf).to receive(:stroke)
393
-
394
- @pdf.stroke_line_to [100, 100]
395
- end
396
-
397
- it "should convert fill_some_method(args) into some_method(args); fill" do
398
- expect(@pdf).to receive(:line_to).with([100, 100])
399
- expect(@pdf).to receive(:fill)
400
-
401
- @pdf.fill_line_to [100, 100]
402
- end
403
-
404
- it "should not break method_missing" do
405
- expect { @pdf.i_have_a_pretty_girlfriend_named_jia }.
406
- to raise_error(NoMethodError)
407
- end
408
- end
409
-
410
- describe "When using graphics states" do
411
- before(:each) { create_pdf }
412
-
413
- it "should add the right content on save_graphics_state" do
414
- expect(@pdf.renderer).to receive(:add_content).with('q')
415
-
416
- @pdf.save_graphics_state
417
- end
418
-
419
- it "should add the right content on restore_graphics_state" do
420
- expect(@pdf.renderer).to receive(:add_content).with('Q')
421
-
422
- @pdf.restore_graphics_state
423
- end
424
-
425
- it "should save and restore when save_graphics_state is used with a block" do
426
- expect(@pdf.renderer).to receive(:add_content).with('q').ordered
427
- allow(@pdf).to receive(:foo).ordered
428
- expect(@pdf.renderer).to receive(:add_content).with('Q').ordered
429
-
430
- @pdf.save_graphics_state do
431
- @pdf.foo
432
- end
433
- end
434
-
435
- it "should add the previous color space when restoring to a graphic state with different color space" do
436
- @pdf.stroke_color '000000'
437
- @pdf.save_graphics_state
438
- @pdf.stroke_color 0, 0, 0, 0
439
- @pdf.restore_graphics_state
440
- @pdf.stroke_color 0, 0, 100, 0
441
- expect(@pdf.graphic_state.color_space).to eq(:stroke => :DeviceCMYK)
442
- colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
443
- expect(colors.color_space).to eq(:DeviceCMYK)
444
- expect(colors.stroke_color_space_count[:DeviceCMYK]).to eq(2)
445
- end
446
-
447
- it "should use the correct dash setting after restoring and starting new page" do
448
- @pdf.dash 5
449
- @pdf.save_graphics_state
450
- @pdf.dash 10
451
- expect(@pdf.graphic_state.dash[:dash]).to eq(10)
452
- @pdf.restore_graphics_state
453
- @pdf.start_new_page
454
- expect(@pdf.graphic_state.dash[:dash]).to eq(5)
455
- end
456
-
457
- it "should round dash values to four decimal places" do
458
- @pdf.dash 5.12345
459
- expect(@pdf.graphic_state.dash_setting).to eq("[5.1235 5.1235] 0.0 d")
460
- end
461
-
462
- it "should raise an error when dash is called w. a zero length or space" do
463
- expect { @pdf.dash(0) }.to raise_error(ArgumentError)
464
- expect { @pdf.dash([0]) }.to raise_error(ArgumentError)
465
- expect { @pdf.dash([0, 0]) }.to raise_error(ArgumentError)
466
- expect { @pdf.dash([0, 0, 0, 1]) }.to raise_error(ArgumentError)
467
- end
468
-
469
- it "the current graphic state should keep track of previous unchanged settings" do
470
- @pdf.stroke_color '000000'
471
- @pdf.save_graphics_state
472
- @pdf.dash 5
473
- @pdf.save_graphics_state
474
- @pdf.cap_style :round
475
- @pdf.save_graphics_state
476
- @pdf.fill_color 0, 0, 100, 0
477
- @pdf.save_graphics_state
478
-
479
- expect(@pdf.graphic_state.stroke_color).to eq("000000")
480
- expect(@pdf.graphic_state.join_style).to eq(:miter)
481
- expect(@pdf.graphic_state.fill_color).to eq([0, 0, 100, 0])
482
- expect(@pdf.graphic_state.cap_style).to eq(:round)
483
- expect(@pdf.graphic_state.color_space).to eq(:fill => :DeviceCMYK, :stroke => :DeviceRGB)
484
- expect(@pdf.graphic_state.dash).to eq(:space => 5, :phase => 0, :dash => 5)
485
- expect(@pdf.graphic_state.line_width).to eq(1)
486
- end
487
-
488
- it "should not add extra graphic space closings when rendering multiple times" do
489
- @pdf.render
490
- state = PDF::Inspector::Graphics::State.analyze(@pdf.render)
491
- expect(state.save_graphics_state_count).to eq(1)
492
- expect(state.restore_graphics_state_count).to eq(1)
493
- end
494
-
495
- it "should add extra graphic state enclosings when content is added on multiple renderings" do
496
- @pdf.render
497
- @pdf.text "Adding a bit more content"
498
- state = PDF::Inspector::Graphics::State.analyze(@pdf.render)
499
- expect(state.save_graphics_state_count).to eq(2)
500
- expect(state.restore_graphics_state_count).to eq(2)
501
- end
502
-
503
- it "adds extra graphic state enclosings when new settings are applied on multiple renderings" do
504
- @pdf.render
505
- @pdf.stroke_color 0, 0, 0, 0
506
- state = PDF::Inspector::Graphics::State.analyze(@pdf.render)
507
- expect(state.save_graphics_state_count).to eq(2)
508
- expect(state.restore_graphics_state_count).to eq(2)
509
- end
510
-
511
- it "should raise_error error if closing an empty graphic stack" do
512
- expect {
513
- @pdf.render
514
- @pdf.restore_graphics_state
515
- }.to raise_error(PDF::Core::Errors::EmptyGraphicStateStack)
516
- end
517
-
518
- it "should copy mutable attributes when passing a previous_state to the initializer" do
519
- new_state = PDF::Core::GraphicState.new(@pdf.graphic_state)
520
-
521
- [:color_space, :dash, :fill_color, :stroke_color].each do |attr|
522
- expect(new_state.send(attr)).to eq(@pdf.graphic_state.send(attr))
523
- expect(new_state.send(attr)).not_to equal(@pdf.graphic_state.send(attr))
524
- end
525
- end
526
-
527
- it "should copy mutable attributes when duping" do
528
- new_state = @pdf.graphic_state.dup
529
-
530
- [:color_space, :dash, :fill_color, :stroke_color].each do |attr|
531
- expect(new_state.send(attr)).to eq(@pdf.graphic_state.send(attr))
532
- expect(new_state.send(attr)).not_to equal(@pdf.graphic_state.send(attr))
533
- end
534
- end
535
- end
536
-
537
- describe "When using transformation matrix" do
538
- before(:each) { create_pdf }
539
-
540
- # Note: The (approximate) number of significant decimal digits of precision in fractional
541
- # part is 5 (PDF Reference, Third Edition, p. 706)
542
-
543
- it "should send the right content on transformation_matrix" do
544
- expect(@pdf.renderer).to receive(:add_content).with('1.00000 0.00000 0.12346 -1.00000 5.50000 20.00000 cm')
545
- @pdf.transformation_matrix 1, 0, 0.123456789, -1.0, 5.5, 20
546
- end
547
-
548
- it "should use fixed digits with very small number" do
549
- values = Array.new(6, 0.000000000001)
550
- string = Array.new(6, "0.00000").join " "
551
- expect(@pdf.renderer).to receive(:add_content).with("#{string} cm")
552
- @pdf.transformation_matrix(*values)
553
- end
554
-
555
- it "should be received by the inspector" do
556
- @pdf.transformation_matrix 1, 0, 0, -1, 5.5, 20
557
- matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
558
- expect(matrices.matrices).to eq([[1, 0, 0, -1, 5.5, 20]])
559
- end
560
-
561
- it "should save the graphics state inside the given block" do
562
- values = Array.new(6, 0.000000000001)
563
- string = Array.new(6, "0.00000").join " "
564
-
565
- expect(@pdf).to receive(:save_graphics_state).with(no_args).ordered
566
- allow(@pdf.renderer).to receive(:add_content).with(any_args).twice
567
- expect(@pdf.renderer).to receive(:add_content).with("#{string} cm").ordered
568
- allow(@pdf).to receive(:do_something).ordered
569
- expect(@pdf).to receive(:restore_graphics_state).with(no_args).ordered
570
-
571
- @pdf.transformation_matrix(*values) do
572
- @pdf.do_something
573
- end
574
- end
575
- end
576
-
577
- describe "When using transformations shortcuts" do
578
- before(:each) do
579
- create_pdf
580
- @x, @y = 12, 54.32
581
- @angle = 12.32
582
- @cos = Math.cos(@angle * Math::PI / 180)
583
- @sin = Math.sin(@angle * Math::PI / 180)
584
- @factor = 0.12
585
- end
586
-
587
- describe "#rotate" do
588
- it "should rotate" do
589
- expect(@pdf).to receive(:transformation_matrix).with(@cos, @sin, -@sin, @cos, 0, 0)
590
- @pdf.rotate(@angle)
591
- end
592
- end
593
-
594
- describe "#rotate with :origin option" do
595
- it "should rotate around the origin" do
596
- x_prime = @x * @cos - @y * @sin
597
- y_prime = @x * @sin + @y * @cos
598
-
599
- @pdf.rotate(@angle, :origin => [@x, @y]) { @pdf.text('hello world') }
600
-
601
- matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
602
- expect(matrices.matrices[0]).to eq([1, 0, 0, 1,
603
- reduce_precision(@x - x_prime),
604
- reduce_precision(@y - y_prime)])
605
- expect(matrices.matrices[1]).to eq([reduce_precision(@cos),
606
- reduce_precision(@sin),
607
- reduce_precision(-@sin),
608
- reduce_precision(@cos), 0, 0])
609
- end
610
-
611
- it "should rotate around the origin in a document with a margin" do
612
- @pdf = Prawn::Document.new
613
-
614
- @pdf.rotate(@angle, :origin => [@x, @y]) { @pdf.text('hello world') }
615
-
616
- x = @x + @pdf.bounds.absolute_left
617
- y = @y + @pdf.bounds.absolute_bottom
618
- x_prime = x * @cos - y * @sin
619
- y_prime = x * @sin + y * @cos
620
-
621
- matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
622
- expect(matrices.matrices[0]).to eq([1, 0, 0, 1,
623
- reduce_precision(x - x_prime),
624
- reduce_precision(y - y_prime)])
625
- expect(matrices.matrices[1]).to eq([reduce_precision(@cos),
626
- reduce_precision(@sin),
627
- reduce_precision(-@sin),
628
- reduce_precision(@cos), 0, 0])
629
- end
630
-
631
- it "should raise_error BlockRequired if no block is given" do
632
- expect {
633
- @pdf.rotate(@angle, :origin => [@x, @y])
634
- }.to raise_error(Prawn::Errors::BlockRequired)
635
- end
636
-
637
- def reduce_precision(float)
638
- ("%.5f" % float).to_f
639
- end
640
- end
641
-
642
- describe "#translate" do
643
- it "should translate" do
644
- x, y = 12, 54.32
645
- expect(@pdf).to receive(:transformation_matrix).with(1, 0, 0, 1, x, y)
646
- @pdf.translate(x, y)
647
- end
648
- end
649
-
650
- describe "#scale" do
651
- it "should scale" do
652
- expect(@pdf).to receive(:transformation_matrix).with(@factor, 0, 0, @factor, 0, 0)
653
- @pdf.scale(@factor)
654
- end
655
- end
656
-
657
- describe "#scale with :origin option" do
658
- it "should scale from the origin" do
659
- x_prime = @factor * @x
660
- y_prime = @factor * @y
661
-
662
- @pdf.scale(@factor, :origin => [@x, @y]) { @pdf.text('hello world') }
663
-
664
- matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
665
- expect(matrices.matrices[0]).to eq([1, 0, 0, 1,
666
- reduce_precision(@x - x_prime),
667
- reduce_precision(@y - y_prime)])
668
- expect(matrices.matrices[1]).to eq([@factor, 0, 0, @factor, 0, 0])
669
- end
670
-
671
- it "should scale from the origin in a document with a margin" do
672
- @pdf = Prawn::Document.new
673
- x = @x + @pdf.bounds.absolute_left
674
- y = @y + @pdf.bounds.absolute_bottom
675
- x_prime = @factor * x
676
- y_prime = @factor * y
677
-
678
- @pdf.scale(@factor, :origin => [@x, @y]) { @pdf.text('hello world') }
679
-
680
- matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
681
- expect(matrices.matrices[0]).to eq([1, 0, 0, 1,
682
- reduce_precision(x - x_prime),
683
- reduce_precision(y - y_prime)])
684
- expect(matrices.matrices[1]).to eq([@factor, 0, 0, @factor, 0, 0])
685
- end
686
-
687
- it "should raise_error BlockRequired if no block is given" do
688
- expect {
689
- @pdf.scale(@factor, :origin => [@x, @y])
690
- }.to raise_error(Prawn::Errors::BlockRequired)
691
- end
692
-
693
- def reduce_precision(float)
694
- ("%.5f" % float).to_f
695
- end
696
- end
697
-
698
- # describe "skew" do
699
- # it "should skew" do
700
- # a, b = 30, 50.2
701
- # @pdf.expects(:transformation_matrix).with(1, Math.tan(a * Math::PI / 180), Math.tan(b * Math::PI / 180), 1, 0, 0)
702
- # @pdf.skew(a, b)
703
- # end
704
- # end
705
- end