prawn-core 0.5.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. data/COPYING +340 -0
  2. data/LICENSE +56 -0
  3. data/README +121 -0
  4. data/Rakefile +74 -0
  5. data/data/encodings/win_ansi.txt +29 -0
  6. data/data/fonts/Action Man.dfont +0 -0
  7. data/data/fonts/Activa.ttf +0 -0
  8. data/data/fonts/Chalkboard.ttf +0 -0
  9. data/data/fonts/Courier-Bold.afm +342 -0
  10. data/data/fonts/Courier-BoldOblique.afm +342 -0
  11. data/data/fonts/Courier-Oblique.afm +342 -0
  12. data/data/fonts/Courier.afm +342 -0
  13. data/data/fonts/DejaVuSans.ttf +0 -0
  14. data/data/fonts/Dustismo_Roman.ttf +0 -0
  15. data/data/fonts/Helvetica-Bold.afm +2827 -0
  16. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  17. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  18. data/data/fonts/Helvetica.afm +3051 -0
  19. data/data/fonts/MustRead.html +19 -0
  20. data/data/fonts/Symbol.afm +213 -0
  21. data/data/fonts/Times-Bold.afm +2588 -0
  22. data/data/fonts/Times-BoldItalic.afm +2384 -0
  23. data/data/fonts/Times-Italic.afm +2667 -0
  24. data/data/fonts/Times-Roman.afm +2419 -0
  25. data/data/fonts/ZapfDingbats.afm +225 -0
  26. data/data/fonts/comicsans.ttf +0 -0
  27. data/data/fonts/gkai00mp.ttf +0 -0
  28. data/data/images/16bit.alpha +0 -0
  29. data/data/images/16bit.dat +0 -0
  30. data/data/images/16bit.png +0 -0
  31. data/data/images/arrow.png +0 -0
  32. data/data/images/arrow2.png +0 -0
  33. data/data/images/barcode_issue.png +0 -0
  34. data/data/images/dice.alpha +0 -0
  35. data/data/images/dice.dat +0 -0
  36. data/data/images/dice.png +0 -0
  37. data/data/images/dice_interlaced.png +0 -0
  38. data/data/images/fractal.jpg +0 -0
  39. data/data/images/letterhead.jpg +0 -0
  40. data/data/images/page_white_text.alpha +0 -0
  41. data/data/images/page_white_text.dat +0 -0
  42. data/data/images/page_white_text.png +0 -0
  43. data/data/images/pigs.jpg +0 -0
  44. data/data/images/rails.dat +0 -0
  45. data/data/images/rails.png +0 -0
  46. data/data/images/ruport.png +0 -0
  47. data/data/images/ruport_data.dat +0 -0
  48. data/data/images/ruport_transparent.png +0 -0
  49. data/data/images/ruport_type0.png +0 -0
  50. data/data/images/stef.jpg +0 -0
  51. data/data/images/tru256.bmp +0 -0
  52. data/data/images/web-links.dat +1 -0
  53. data/data/images/web-links.png +0 -0
  54. data/data/shift_jis_text.txt +1 -0
  55. data/examples/bounding_box/bounding_boxes.rb +44 -0
  56. data/examples/bounding_box/russian_boxes.rb +37 -0
  57. data/examples/column_box/column_box_example.rb +44 -0
  58. data/examples/general/background.rb +20 -0
  59. data/examples/general/canvas.rb +16 -0
  60. data/examples/general/measurement_units.rb +52 -0
  61. data/examples/general/metadata-info.rb +17 -0
  62. data/examples/general/multi_page_layout.rb +17 -0
  63. data/examples/general/page_geometry.rb +32 -0
  64. data/examples/graphics/basic_images.rb +24 -0
  65. data/examples/graphics/cmyk.rb +13 -0
  66. data/examples/graphics/curves.rb +12 -0
  67. data/examples/graphics/hexagon.rb +14 -0
  68. data/examples/graphics/image_fit.rb +16 -0
  69. data/examples/graphics/image_flow.rb +38 -0
  70. data/examples/graphics/image_position.rb +18 -0
  71. data/examples/graphics/line.rb +33 -0
  72. data/examples/graphics/png_types.rb +23 -0
  73. data/examples/graphics/polygons.rb +17 -0
  74. data/examples/graphics/remote_images.rb +12 -0
  75. data/examples/graphics/ruport_style_helpers.rb +20 -0
  76. data/examples/graphics/stroke_bounds.rb +21 -0
  77. data/examples/m17n/chinese_text_wrapping.rb +20 -0
  78. data/examples/m17n/euro.rb +16 -0
  79. data/examples/m17n/sjis.rb +29 -0
  80. data/examples/m17n/utf8.rb +14 -0
  81. data/examples/m17n/win_ansi_charset.rb +55 -0
  82. data/examples/text/alignment.rb +19 -0
  83. data/examples/text/dfont.rb +49 -0
  84. data/examples/text/family_based_styling.rb +25 -0
  85. data/examples/text/font_calculations.rb +92 -0
  86. data/examples/text/font_size.rb +34 -0
  87. data/examples/text/kerning.rb +31 -0
  88. data/examples/text/simple_text.rb +18 -0
  89. data/examples/text/simple_text_ttf.rb +18 -0
  90. data/examples/text/span.rb +30 -0
  91. data/examples/text/text_box.rb +26 -0
  92. data/examples/text/text_flow.rb +68 -0
  93. data/lib/prawn/compatibility.rb +38 -0
  94. data/lib/prawn/core.rb +79 -0
  95. data/lib/prawn/document.rb +399 -0
  96. data/lib/prawn/document/annotations.rb +63 -0
  97. data/lib/prawn/document/bounding_box.rb +377 -0
  98. data/lib/prawn/document/column_box.rb +89 -0
  99. data/lib/prawn/document/destinations.rb +81 -0
  100. data/lib/prawn/document/internals.rb +133 -0
  101. data/lib/prawn/document/page_geometry.rb +149 -0
  102. data/lib/prawn/document/span.rb +55 -0
  103. data/lib/prawn/document/text.rb +186 -0
  104. data/lib/prawn/document/text/box.rb +83 -0
  105. data/lib/prawn/document/text/wrapping.rb +59 -0
  106. data/lib/prawn/encoding.rb +121 -0
  107. data/lib/prawn/errors.rb +49 -0
  108. data/lib/prawn/font.rb +292 -0
  109. data/lib/prawn/font/afm.rb +202 -0
  110. data/lib/prawn/font/dfont.rb +31 -0
  111. data/lib/prawn/font/ttf.rb +327 -0
  112. data/lib/prawn/graphics.rb +257 -0
  113. data/lib/prawn/graphics/color.rb +141 -0
  114. data/lib/prawn/images.rb +339 -0
  115. data/lib/prawn/images/jpg.rb +45 -0
  116. data/lib/prawn/images/png.rb +217 -0
  117. data/lib/prawn/literal_string.rb +14 -0
  118. data/lib/prawn/measurement_extensions.rb +46 -0
  119. data/lib/prawn/measurements.rb +71 -0
  120. data/lib/prawn/name_tree.rb +165 -0
  121. data/lib/prawn/pdf_object.rb +77 -0
  122. data/lib/prawn/reference.rb +59 -0
  123. data/spec/annotations_spec.rb +90 -0
  124. data/spec/bounding_box_spec.rb +141 -0
  125. data/spec/destinations_spec.rb +15 -0
  126. data/spec/document_spec.rb +178 -0
  127. data/spec/font_spec.rb +274 -0
  128. data/spec/graphics_spec.rb +209 -0
  129. data/spec/images_spec.rb +79 -0
  130. data/spec/jpg_spec.rb +25 -0
  131. data/spec/measurement_units_spec.rb +23 -0
  132. data/spec/name_tree_spec.rb +103 -0
  133. data/spec/pdf_object_spec.rb +117 -0
  134. data/spec/png_spec.rb +236 -0
  135. data/spec/reference_spec.rb +42 -0
  136. data/spec/span_spec.rb +45 -0
  137. data/spec/spec_helper.rb +23 -0
  138. data/spec/text_box_spec.rb +83 -0
  139. data/spec/text_spec.rb +178 -0
  140. data/vendor/pdf-inspector/README +18 -0
  141. data/vendor/pdf-inspector/lib/pdf/inspector.rb +25 -0
  142. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +80 -0
  143. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +16 -0
  144. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +31 -0
  145. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
  146. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  147. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  148. data/vendor/ttfunk/example.rb +45 -0
  149. data/vendor/ttfunk/lib/ttfunk.rb +102 -0
  150. data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
  151. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
  152. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
  153. data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
  154. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
  155. data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
  156. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
  157. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +46 -0
  158. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
  159. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
  160. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +51 -0
  161. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
  162. data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
  163. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
  164. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
  165. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
  166. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
  167. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
  168. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
  169. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
  170. data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
  171. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
  172. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
  173. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
  174. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
  175. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
  176. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
  177. data/vendor/ttfunk/lib/ttfunk/table/name.rb +119 -0
  178. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
  179. data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
  180. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
  181. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
  182. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
  183. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
  184. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
  185. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
  186. metadata +245 -0
@@ -0,0 +1,274 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+ require 'iconv'
5
+
6
+ describe "Font behavior" do
7
+
8
+ it "should default to Helvetica if no font is specified" do
9
+ @pdf = Prawn::Document.new
10
+ @pdf.font.name.should == "Helvetica"
11
+ end
12
+
13
+ end
14
+
15
+ describe "font style support" do
16
+ before(:each) { create_pdf }
17
+
18
+ it "should complain if there is no @current_page" do
19
+ pdf_without_page = Prawn::Document.new(:skip_page_creation => true)
20
+
21
+ lambda{ pdf_without_page.font "Helvetica" }.
22
+ should.raise(Prawn::Errors::NotOnPage)
23
+ end
24
+
25
+ it "should allow specifying font style by style name and font family" do
26
+ @pdf.font "Courier", :style => :bold
27
+ @pdf.text "In Courier bold"
28
+
29
+ @pdf.font "Courier", :style => :bold_italic
30
+ @pdf.text "In Courier bold-italic"
31
+
32
+ @pdf.font "Courier", :style => :italic
33
+ @pdf.text "In Courier italic"
34
+
35
+ @pdf.font "Courier", :style => :normal
36
+ @pdf.text "In Normal Courier"
37
+
38
+ @pdf.font "Helvetica"
39
+ @pdf.text "In Normal Helvetica"
40
+
41
+ text = PDF::Inspector::Text.analyze(@pdf.render)
42
+ text.font_settings.map { |e| e[:name] }.should ==
43
+ [:"Courier-Bold", :"Courier-BoldOblique", :"Courier-Oblique",
44
+ :Courier, :Helvetica]
45
+ end
46
+
47
+ it "should allow font familes to be defined in a single dfont" do
48
+ file = "#{Prawn::BASEDIR}/data/fonts/Action Man.dfont"
49
+ @pdf.font_families["Action Man"] = {
50
+ :normal => { :file => file, :font => "ActionMan" },
51
+ :italic => { :file => file, :font => "ActionMan-Italic" },
52
+ :bold => { :file => file, :font => "ActionMan-Bold" },
53
+ :bold_italic => { :file => file, :font => "ActionMan-BoldItalic" }
54
+ }
55
+
56
+ @pdf.font "Action Man", :style => :italic
57
+ @pdf.text "In ActionMan-Italic"
58
+
59
+ text = PDF::Inspector::Text.analyze(@pdf.render)
60
+ name = text.font_settings.map { |e| e[:name] }.first
61
+ name = name.unpack("n*")[2..-1].pack("U*")
62
+ name = name.sub(/\w+\+/, "subset+")
63
+ name.should == "subset+ActionMan-Italic"
64
+ end
65
+ end
66
+
67
+ describe "Transactional font handling" do
68
+ before(:each) { create_pdf }
69
+
70
+ it "should allow setting of size directly when font is created" do
71
+ @pdf.font "Courier", :size => 16
72
+ @pdf.font_size.should == 16
73
+ end
74
+
75
+ it "should allow temporary setting of a new font using a transaction" do
76
+ @pdf.font "Helvetica", :size => 12
77
+
78
+ @pdf.font "Courier", :size => 16 do
79
+ @pdf.font.name.should == "Courier"
80
+ @pdf.font_size.should == 16
81
+ end
82
+
83
+ @pdf.font.name.should == "Helvetica"
84
+ @pdf.font_size.should == 12
85
+ end
86
+
87
+ it "should mask font size when using a transacation" do
88
+ @pdf.font "Courier", :size => 16 do
89
+ @pdf.font_size.should == 16
90
+ end
91
+
92
+ @pdf.font "Times-Roman"
93
+ @pdf.font "Courier"
94
+
95
+ @pdf.font_size.should == 12
96
+ end
97
+
98
+ end
99
+
100
+ describe "Document#page_fonts" do
101
+ before(:each) { create_pdf }
102
+
103
+ it "should register fonts properly by page" do
104
+ @pdf.font "Courier"; @pdf.text("hello")
105
+ @pdf.font "Helvetica"; @pdf.text("hello")
106
+ @pdf.font "Times-Roman"; @pdf.text("hello")
107
+ ["Courier","Helvetica","Times-Roman"].each { |f|
108
+ page_should_include_font(f)
109
+ }
110
+
111
+ @pdf.start_new_page
112
+ @pdf.font "Helvetica"; @pdf.text("hello")
113
+ page_should_include_font("Helvetica")
114
+ page_should_not_include_font("Courier")
115
+ page_should_not_include_font("Times-Roman")
116
+ end
117
+
118
+ def page_includes_font?(font)
119
+ @pdf.page_fonts.values.map { |e| e.data[:BaseFont] }.include?(font.to_sym)
120
+ end
121
+
122
+ def page_should_include_font(font)
123
+ assert_block("Expected page to include font: #{font}") do
124
+ page_includes_font?(font)
125
+ end
126
+ end
127
+
128
+ def page_should_not_include_font(font)
129
+ assert_block("Did not expect page to include font: #{font}") do
130
+ not page_includes_font?(font)
131
+ end
132
+ end
133
+
134
+ end
135
+
136
+ describe "AFM fonts" do
137
+
138
+ setup do
139
+ create_pdf
140
+ @times = @pdf.find_font "Times-Roman"
141
+ @iconv = ::Iconv.new('Windows-1252', 'utf-8')
142
+ end
143
+
144
+ it "should calculate string width taking into account accented characters" do
145
+ @times.compute_width_of(@iconv.iconv("é"), :size => 12).should == @times.compute_width_of("e", :size => 12)
146
+ end
147
+
148
+ it "should calculate string width taking into account kerning pairs" do
149
+ @times.compute_width_of(@iconv.iconv("To"), :size => 12).should == 13.332
150
+ @times.compute_width_of(@iconv.iconv("To"), :size => 12, :kerning => true).should == 12.372
151
+ @times.compute_width_of(@iconv.iconv("Tö"), :size => 12, :kerning => true).should == 12.372
152
+ end
153
+
154
+ it "should encode text without kerning by default" do
155
+ @times.encode_text(@iconv.iconv("To")).should == [[0, "To"]]
156
+ @times.encode_text(@iconv.iconv("Télé")).should == [[0, @iconv.iconv("Télé")]]
157
+ @times.encode_text(@iconv.iconv("Technology")).should == [[0, "Technology"]]
158
+ @times.encode_text(@iconv.iconv("Technology...")).should == [[0, "Technology..."]]
159
+ end
160
+
161
+ it "should encode text with kerning if requested" do
162
+ @times.encode_text(@iconv.iconv("To"), :kerning => true).should == [[0, ["T", 80, "o"]]]
163
+ @times.encode_text(@iconv.iconv("Télé"), :kerning => true).should == [[0, ["T", 70, @iconv.iconv("élé")]]]
164
+ @times.encode_text(@iconv.iconv("Technology"), :kerning => true).should == [[0, ["T", 70, "echnology"]]]
165
+ @times.encode_text(@iconv.iconv("Technology..."), :kerning => true).should == [[0, ["T", 70, "echnology", 65, "..."]]]
166
+ end
167
+
168
+ describe "when normalizing encoding" do
169
+
170
+ it "should not modify the original string when normalize_encoding() is used" do
171
+ original = "Foo"
172
+ normalized = @times.normalize_encoding(original)
173
+ assert ! original.equal?(normalized)
174
+ end
175
+
176
+ it "should modify the original string when normalize_encoding!() is used" do
177
+ original = "Foo"
178
+ normalized = @times.normalize_encoding!(original)
179
+ assert original.equal?(normalized)
180
+ end
181
+
182
+ end
183
+
184
+ end
185
+
186
+ describe "TTF fonts" do
187
+
188
+ setup do
189
+ create_pdf
190
+ @activa = @pdf.find_font "#{Prawn::BASEDIR}/data/fonts/Activa.ttf"
191
+ end
192
+
193
+ it "should calculate string width taking into account accented characters" do
194
+ @activa.compute_width_of("é", :size => 12).should == @activa.compute_width_of("e", :size => 12)
195
+ end
196
+
197
+ it "should calculate string width taking into account kerning pairs" do
198
+ @activa.compute_width_of("To", :size => 12).should == 15.228
199
+ @activa.compute_width_of("To", :size => 12, :kerning => true).should == 12.996
200
+ end
201
+
202
+ it "should encode text without kerning by default" do
203
+ @activa.encode_text("To").should == [[0, "To"]]
204
+ @activa.encode_text("Télé").should == [[0, "T\216l\216"]]
205
+ @activa.encode_text("Technology").should == [[0, "Technology"]]
206
+ @activa.encode_text("Technology...").should == [[0, "Technology..."]]
207
+ @activa.encode_text("Teχnology...").should == [[0, "Te"], [1, "!"], [0, "nology..."]]
208
+ end
209
+
210
+ it "should encode text with kerning if requested" do
211
+ @activa.encode_text("To", :kerning => true).should == [[0, ["T", 186.0, "o"]]]
212
+ @activa.encode_text("To", :kerning => true).should == [[0, ["T", 186.0, "o"]]]
213
+ @activa.encode_text("Technology", :kerning => true).should == [[0, ["T", 186.0, "echnology"]]]
214
+ @activa.encode_text("Technology...", :kerning => true).should == [[0, ["T", 186.0, "echnology", 88.0, "..."]]]
215
+ @activa.encode_text("Teχnology...", :kerning => true).should == [[0, ["T", 186.0, "e"]], [1, "!"], [0, ["nology", 88.0, "..."]]]
216
+ end
217
+
218
+ describe "when normalizing encoding" do
219
+
220
+ it "should not modify the original string when normalize_encoding() is used" do
221
+ original = "Foo"
222
+ normalized = @activa.normalize_encoding(original)
223
+ assert ! original.equal?(normalized)
224
+ end
225
+
226
+ it "should modify the original string when normalize_encoding!() is used" do
227
+ original = "Foo"
228
+ normalized = @activa.normalize_encoding!(original)
229
+ assert original.equal?(normalized)
230
+ end
231
+
232
+ end
233
+
234
+
235
+ end
236
+
237
+ describe "DFont fonts" do
238
+ setup do
239
+ create_pdf
240
+ @file = "#{Prawn::BASEDIR}/data/fonts/Action Man.dfont"
241
+ end
242
+
243
+ it "should list all named fonts" do
244
+ list = Prawn::Font::DFont.named_fonts(@file)
245
+ list.sort.should == %w(ActionMan ActionMan-Italic ActionMan-Bold ActionMan-BoldItalic).sort
246
+ end
247
+
248
+ it "should count the number of fonts in the file" do
249
+ Prawn::Font::DFont.font_count(@file).should == 4
250
+ end
251
+
252
+ it "should default selected font to the first one if not specified" do
253
+ font = @pdf.find_font(@file)
254
+ font.basename.should == "ActionMan"
255
+ end
256
+
257
+ it "should allow font to be selected by index" do
258
+ font = @pdf.find_font(@file, :font => 2)
259
+ font.basename.should == "ActionMan-Italic"
260
+ end
261
+
262
+ it "should allow font to be selected by name" do
263
+ font = @pdf.find_font(@file, :font => "ActionMan-BoldItalic")
264
+ font.basename.should == "ActionMan-BoldItalic"
265
+ end
266
+
267
+ it "should cache font object based on selected font" do
268
+ f1 = @pdf.find_font(@file, :font => "ActionMan")
269
+ f2 = @pdf.find_font(@file, :font => "ActionMan-Bold")
270
+ assert_not_equal f1.object_id, f2.object_id
271
+ assert_equal f1.object_id, @pdf.find_font(@file, :font => "ActionMan").object_id
272
+ assert_equal f2.object_id, @pdf.find_font(@file, :font => "ActionMan-Bold").object_id
273
+ end
274
+ end
@@ -0,0 +1,209 @@
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
+
7
+ before(:each) { create_pdf }
8
+
9
+ it "should draw a line from (100,600) to (100,500)" do
10
+ @pdf.line([100,600],[100,500])
11
+
12
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
13
+
14
+ line_drawing.points.should == [[100,600],[100,500]]
15
+ end
16
+
17
+ it "should draw two lines at (100,600) to (100,500) " +
18
+ "and (75,100) to (50,125)" do
19
+ @pdf.line(100,600,100,500)
20
+ @pdf.line(75,100,50,125)
21
+
22
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
23
+
24
+ line_drawing.points.should ==
25
+ [[100.0, 600.0], [100.0, 500.0], [75.0, 100.0], [50.0, 125.0]]
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
+ line.widths.first.should == 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
+ line.widths.first.should == 10
38
+ end
39
+
40
+ describe "(Horizontally)" do
41
+ it "should draw from [x1,pdf.y],[x2,pdf.y]" do
42
+ @pdf.horizontal_line(100,150)
43
+ @line = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
44
+ @line.points.should == [[100.0 + @pdf.bounds.absolute_left, @pdf.y],
45
+ [150.0 + @pdf.bounds.absolute_left, @pdf.y]]
46
+ end
47
+
48
+ it "should draw a line from (200, 250) to (300, 250)" do
49
+ @pdf.horizontal_line(200,300,:at => 250)
50
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
51
+ line_drawing.points.should == [[200,250],[300,250]]
52
+ end
53
+ end
54
+
55
+ describe "(Vertically)" do
56
+ it "should draw a line from (350, 300) to (350, 400)" do
57
+ @pdf.vertical_line(300,400,:at => 350)
58
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
59
+ line_drawing.points.should == [[350,300],[350,400]]
60
+ end
61
+ it "should require a y coordinate" do
62
+ lambda { @pdf.vertical_line(400,500) }.
63
+ should.raise(ArgumentError)
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ describe "When drawing a polygon" do
70
+
71
+ before(:each) { create_pdf }
72
+
73
+ it "should draw each line passed to polygon()" do
74
+ @pdf.polygon([100,500],[100,400],[200,400])
75
+
76
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
77
+ line_drawing.points.should == [[100,500],[100,400],[200,400],[100,500]]
78
+ end
79
+
80
+ end
81
+
82
+ describe "When drawing a rectangle" do
83
+
84
+ before(:each) { create_pdf }
85
+
86
+ it "should use a point, width, and height for coords" do
87
+ @pdf.rectangle [200,200], 50, 100
88
+
89
+ rectangles = PDF::Inspector::Graphics::Rectangle.
90
+ analyze(@pdf.render).rectangles
91
+ # PDF uses bottom left corner
92
+ rectangles[0][:point].should == [200,100]
93
+ rectangles[0][:width].should == 50
94
+ rectangles[0][:height].should == 100
95
+
96
+ end
97
+
98
+ end
99
+
100
+ describe "When drawing a curve" do
101
+
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
+ curve.coords.should == [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
+ curve.coords.should == [100.0, 100.0, 20.0, 90.0, 90.0, 75.0, 50.0, 50.0]
115
+ end
116
+
117
+ end
118
+
119
+ describe "When drawing an ellipse" do
120
+ before(:each) do
121
+ create_pdf
122
+ @pdf.ellipse_at [100,100], 25, 50
123
+ @curve = PDF::Inspector::Graphics::Curve.analyze(@pdf.render)
124
+ end
125
+
126
+ it "should move the pointer to the center of the ellipse after drawing" do
127
+ @curve.coords[-2..-1].should == [100,100]
128
+ end
129
+
130
+ end
131
+
132
+ describe "When drawing a circle" do
133
+ before(:each) do
134
+ create_pdf
135
+ @pdf.circle_at [100,100], :radius => 25
136
+ @pdf.ellipse_at [100,100], 25, 25
137
+ @curve = PDF::Inspector::Graphics::Curve.analyze(@pdf.render)
138
+ end
139
+
140
+ it "should stroke the same path as the equivalent ellipse" do
141
+ middle = @curve.coords.length / 2
142
+ @curve.coords[0...middle].should == @curve.coords[middle..-1]
143
+ end
144
+ end
145
+
146
+ describe "When setting colors" do
147
+
148
+ before(:each) { create_pdf }
149
+
150
+ it "should set stroke colors" do
151
+ @pdf.stroke_color "ffcccc"
152
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
153
+ # 100% red, 80% green, 80% blue
154
+ colors.stroke_color.should == [1.0, 0.8, 0.8]
155
+ end
156
+
157
+ it "should set fill colors" do
158
+ @pdf.fill_color "ccff00"
159
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
160
+ # 80% red, 100% green, 0% blue
161
+ colors.fill_color.should == [0.8,1.0,0]
162
+ end
163
+
164
+ it "should reset the colors on each new page if they have been defined" do
165
+ @pdf.fill_color "ccff00"
166
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
167
+
168
+ colors.fill_color_count.should == 2
169
+ colors.stroke_color_count.should == 1
170
+ @pdf.start_new_page
171
+ @pdf.stroke_color "ff00cc"
172
+
173
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
174
+ colors.fill_color_count.should == 3
175
+ colors.stroke_color_count.should == 3
176
+
177
+ @pdf.start_new_page
178
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
179
+ colors.fill_color_count.should == 4
180
+ colors.stroke_color_count.should == 4
181
+
182
+ colors.fill_color.should == [0.8,1.0,0.0]
183
+ colors.stroke_color.should == [1.0,0.0,0.8]
184
+ end
185
+
186
+ end
187
+
188
+ describe "When using painting shortcuts" do
189
+ before(:each) { create_pdf }
190
+
191
+ it "should convert stroke_some_method(args) into some_method(args); stroke" do
192
+ @pdf.expects(:line_to).with([100,100])
193
+ @pdf.expects(:stroke)
194
+
195
+ @pdf.stroke_line_to [100,100]
196
+ end
197
+
198
+ it "should convert fill_some_method(args) into some_method(args); fill" do
199
+ @pdf.expects(:line_to).with([100,100])
200
+ @pdf.expects(:fill)
201
+
202
+ @pdf.fill_line_to [100,100]
203
+ end
204
+
205
+ it "should not break method_missing" do
206
+ lambda { @pdf.i_have_a_pretty_girlfriend_named_jia }.
207
+ should.raise(NoMethodError)
208
+ end
209
+ end