prawn-table-continued 1.0.0.rc1

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 (47) hide show
  1. checksums.yaml +7 -0
  2. data/COPYING +2 -0
  3. data/GPLv2 +340 -0
  4. data/GPLv3 +674 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE +56 -0
  7. data/lib/prawn/table/cell/image.rb +69 -0
  8. data/lib/prawn/table/cell/in_table.rb +33 -0
  9. data/lib/prawn/table/cell/span_dummy.rb +93 -0
  10. data/lib/prawn/table/cell/subtable.rb +66 -0
  11. data/lib/prawn/table/cell/text.rb +155 -0
  12. data/lib/prawn/table/cell.rb +787 -0
  13. data/lib/prawn/table/cells.rb +261 -0
  14. data/lib/prawn/table/column_width_calculator.rb +182 -0
  15. data/lib/prawn/table/version.rb +5 -0
  16. data/lib/prawn/table.rb +711 -0
  17. data/manual/contents.rb +13 -0
  18. data/manual/example_helper.rb +8 -0
  19. data/manual/images/prawn.png +0 -0
  20. data/manual/images/stef.jpg +0 -0
  21. data/manual/table/basic_block.rb +53 -0
  22. data/manual/table/before_rendering_page.rb +26 -0
  23. data/manual/table/cell_border_lines.rb +24 -0
  24. data/manual/table/cell_borders_and_bg.rb +31 -0
  25. data/manual/table/cell_dimensions.rb +36 -0
  26. data/manual/table/cell_text.rb +38 -0
  27. data/manual/table/column_widths.rb +30 -0
  28. data/manual/table/content_and_subtables.rb +39 -0
  29. data/manual/table/creation.rb +27 -0
  30. data/manual/table/filtering.rb +36 -0
  31. data/manual/table/flow_and_header.rb +17 -0
  32. data/manual/table/image_cells.rb +33 -0
  33. data/manual/table/position.rb +29 -0
  34. data/manual/table/row_colors.rb +20 -0
  35. data/manual/table/span.rb +30 -0
  36. data/manual/table/style.rb +33 -0
  37. data/manual/table/table.rb +52 -0
  38. data/manual/table/width.rb +27 -0
  39. data/prawn-table.gemspec +36 -0
  40. data/spec/cell_spec.rb +652 -0
  41. data/spec/extensions/encoding_helpers.rb +11 -0
  42. data/spec/extensions/file_fixture_helper.rb +15 -0
  43. data/spec/fixtures/files/prawn.png +0 -0
  44. data/spec/spec_helper.rb +50 -0
  45. data/spec/table/span_dummy_spec.rb +26 -0
  46. data/spec/table_spec.rb +1626 -0
  47. metadata +234 -0
@@ -0,0 +1,36 @@
1
+ basedir = File.expand_path(File.dirname(__FILE__))
2
+ require "#{basedir}/lib/prawn/table/version"
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "prawn-table-continued"
6
+ spec.version = Prawn::Table::VERSION
7
+ spec.platform = Gem::Platform::RUBY
8
+ spec.summary = "Provides tables for PrawnPDF"
9
+ spec.files = Dir.glob("{examples,lib,spec,manual}/**/**/*") +
10
+ ["prawn-table.gemspec", "Gemfile",
11
+ "COPYING", "LICENSE", "GPLv2", "GPLv3"]
12
+ spec.require_path = "lib"
13
+ spec.required_ruby_version = '>= 2.6'
14
+ spec.required_rubygems_version = ">= 2.0.0"
15
+
16
+ spec.test_files = Dir[ "spec/*_spec.rb" ]
17
+ spec.authors = ["Gregory Brown","Brad Ediger","Daniel Nelson","Jonathan Greenberg","James Healy", "Hartwig Brandl"]
18
+ spec.email = ["gregory.t.brown@gmail.com","brad@bradediger.com","dnelson@bluejade.com","greenberg@entryway.net","jimmy@deefa.com", "mail@hartwigbrandl.at"]
19
+ spec.licenses = %w(PRAWN GPL-2.0 GPL-3.0)
20
+
21
+ spec.add_dependency('prawn', '>= 1.3.0', '< 3.0.0')
22
+
23
+ spec.add_development_dependency('pdf-inspector', '~> 1.1.0')
24
+ spec.add_development_dependency('yard')
25
+ spec.add_development_dependency('rspec', '~> 3.0')
26
+ spec.add_development_dependency('rake')
27
+ spec.add_development_dependency('simplecov')
28
+ spec.add_development_dependency('prawn-dev', '~> 0.3.0')
29
+ spec.add_development_dependency('prawn-manual_builder', ">= 0.2.0")
30
+ spec.add_development_dependency('pdf-reader', '~>1.2')
31
+
32
+ spec.homepage = "https://github.com/prawnpdf/prawn-table"
33
+ spec.description = <<END_DESC
34
+ Prawn::Table provides tables for the Prawn PDF toolkit
35
+ END_DESC
36
+ end
data/spec/cell_spec.rb ADDED
@@ -0,0 +1,652 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+ require_relative "../lib/prawn/table"
5
+
6
+ module CellHelpers
7
+
8
+ # Build, but do not draw, a cell on @pdf.
9
+ def cell(options={})
10
+ at = options[:at] || [0, @pdf.cursor]
11
+ Prawn::Table::Cell::Text.new(@pdf, at, options)
12
+ end
13
+
14
+ end
15
+
16
+ describe "Prawn::Table::Cell" do
17
+ before(:each) do
18
+ @pdf = Prawn::Document.new
19
+ end
20
+
21
+ describe "Prawn::Document#cell" do
22
+ include CellHelpers
23
+
24
+ it "should draw the cell" do
25
+ expect_any_instance_of(Prawn::Table::Cell::Text).to receive(:draw).once
26
+ @pdf.cell(:content => "text")
27
+ end
28
+
29
+ it "should return a Cell" do
30
+ expect(@pdf.cell(:content => "text")).to be_a_kind_of Prawn::Table::Cell
31
+ end
32
+
33
+ it "accepts :content => nil in a hash" do
34
+ expect(@pdf.cell(:content => nil)).to be_a_kind_of(Prawn::Table::Cell::Text)
35
+ expect(@pdf.make_cell(:content => nil)).to be_a_kind_of(Prawn::Table::Cell::Text)
36
+ end
37
+
38
+ it "should convert nil, Numeric, and Date values to strings" do
39
+ [nil, 123, 123.45, Date.today, Time.new].each do |value|
40
+ c = @pdf.cell(:content => value)
41
+ expect(c).to be_a_kind_of Prawn::Table::Cell::Text
42
+ expect(c.content).to eq value.to_s
43
+ end
44
+ end
45
+
46
+ it "should convert nil, Numeric, and Date values to strings when value is extracted from options" do
47
+ [nil, 123, 123.45, Date.today, Time.new].each do |value|
48
+ c = Prawn::Table::Cell.make(@pdf, {}, { :content => value })
49
+ expect(c).to be_a_kind_of Prawn::Table::Cell::Text
50
+ expect(c.content).to eq value.to_s
51
+ end
52
+ end
53
+
54
+ it "should allow inline styling with a hash argument" do
55
+ # used for table([[{:text => "...", :font_style => :bold, ...}, ...]])
56
+ c = Prawn::Table::Cell.make(@pdf,
57
+ {:content => 'hello', :font_style => :bold})
58
+ expect(c).to be_a_kind_of Prawn::Table::Cell::Text
59
+ expect(c.content).to eq "hello"
60
+ expect(c.font.name).to eq 'Helvetica-Bold'
61
+ end
62
+
63
+ it "should draw text at the given point plus padding, with the given " +
64
+ "size and style" do
65
+ expect(@pdf).to receive(:bounding_box).and_yield
66
+ expect(@pdf).to receive(:move_down)
67
+ expect(@pdf).to receive(:draw_text!).with("hello world", anything)
68
+
69
+ @pdf.cell(:content => "hello world",
70
+ :at => [10, 20],
71
+ :padding => [30, 40],
72
+ :size => 7,
73
+ :font_style => :bold)
74
+ end
75
+ end
76
+
77
+ describe "Prawn::Document#make_cell" do
78
+ it "should not draw the cell" do
79
+ expect_any_instance_of(Prawn::Table::Cell::Text).to_not receive(:draw)
80
+ @pdf.make_cell("text")
81
+ end
82
+
83
+ it "should return a Cell" do
84
+ expect(@pdf.make_cell("text", :size => 7)).to be_a_kind_of Prawn::Table::Cell
85
+ end
86
+ end
87
+
88
+ describe "#style" do
89
+ include CellHelpers
90
+
91
+ it "should set each property in turn" do
92
+ c = cell(:content => "text")
93
+
94
+ expect(c).to receive(:padding=).with(50)
95
+ expect(c).to receive(:size=).with(7)
96
+
97
+ c.style(:padding => 50, :size => 7)
98
+ end
99
+
100
+ it "ignores unknown properties" do
101
+ c = cell(:content => 'text')
102
+
103
+ c.style(:foobarbaz => 'frobnitz')
104
+ end
105
+ end
106
+
107
+ describe "cell width" do
108
+ include CellHelpers
109
+
110
+ it "should be calculated for text" do
111
+ c = cell(:content => "text")
112
+ expect(c.width).to eq @pdf.width_of("text") + c.padding[1] + c.padding[3]
113
+ end
114
+
115
+ it "should be overridden by manual :width" do
116
+ c = cell(:content => "text", :width => 400)
117
+ expect(c.width).to eq 400
118
+ end
119
+
120
+ it "should incorporate padding when specified" do
121
+ c = cell(:content => "text", :padding => [1, 2, 3, 4])
122
+ expect(c.width).to be_within(0.01).of(@pdf.width_of("text") + 6)
123
+ end
124
+
125
+ it "should allow width to be reset after it has been calculated" do
126
+ # to ensure that if we memoize width, it can still be overridden
127
+ c = cell(:content => "text")
128
+ c.width
129
+ c.width = 400
130
+ expect(c.width).to eq 400
131
+ end
132
+
133
+ it "should return proper width with size set" do
134
+ text = "text " * 4
135
+ c = cell(:content => text, :size => 7)
136
+ expect(c.width).to eq @pdf.width_of(text, :size => 7) + c.padding[1] + c.padding[3]
137
+ end
138
+
139
+ it "content_width should exclude padding" do
140
+ c = cell(:content => "text", :padding => 10)
141
+ expect(c.content_width).to eq @pdf.width_of("text")
142
+ end
143
+
144
+ it "content_width should exclude padding even with manual :width" do
145
+ c = cell(:content => "text", :padding => 10, :width => 400)
146
+ expect(c.content_width).to be_within(0.01).of(380)
147
+ end
148
+
149
+ it "should have a reasonable minimum width that can fit @content" do
150
+ c = cell(:content => "text", :padding => 10)
151
+ min_content_width = c.min_width - c.padding[1] - c.padding[3]
152
+
153
+ expect(@pdf.height_of("text", :width => min_content_width)).to be <
154
+ (5 * @pdf.height_of("text"))
155
+ end
156
+
157
+ it "should defer min_width's evaluation of padding" do
158
+ c = cell(:content => "text", :padding => 100)
159
+ c.padding = 0
160
+
161
+ # Make sure we use the new value of padding in calculating min_width
162
+ expect(c.min_width).to be < 100
163
+ end
164
+
165
+ it "should defer min_width's evaluation of size" do
166
+ c = cell(:content => "text", :size => 50)
167
+ c.size = 8
168
+ c.padding = 0
169
+ expect(c.min_width).to be < 10
170
+ end
171
+
172
+ end
173
+
174
+ describe "cell height" do
175
+ include CellHelpers
176
+
177
+ it "should be calculated for text" do
178
+ c = cell(:content => "text")
179
+ expect(c.height).to eq(
180
+ @pdf.height_of("text", :width => @pdf.width_of("text")) +
181
+ c.padding[0] + c.padding[3]
182
+ )
183
+ end
184
+
185
+ it "should be overridden by manual :height" do
186
+ c = cell(:content => "text", :height => 400)
187
+ expect(c.height).to eq 400
188
+ end
189
+
190
+ it "should incorporate :padding when specified" do
191
+ c = cell(:content => "text", :padding => [1, 2, 3, 4])
192
+ expect(c.height).to be_within(0.01).of(1 + 3 +
193
+ @pdf.height_of("text", :width => @pdf.width_of("text")))
194
+ end
195
+
196
+ it "should allow height to be reset after it has been calculated" do
197
+ # to ensure that if we memoize height, it can still be overridden
198
+ c = cell(:content => "text")
199
+ c.height
200
+ c.height = 400
201
+ expect(c.height).to eq 400
202
+ end
203
+
204
+ it "should return proper height for blocks of text" do
205
+ content = "words " * 10
206
+ c = cell(:content => content, :width => 100)
207
+ expect(c.height).to eq @pdf.height_of(content, :width => 100) +
208
+ c.padding[0] + c.padding[2]
209
+ end
210
+
211
+ it "should return proper height for blocks of text with size set" do
212
+ content = "words " * 10
213
+ c = cell(:content => content, :width => 100, :size => 7)
214
+
215
+ correct_content_height = nil
216
+ @pdf.font_size(7) do
217
+ correct_content_height = @pdf.height_of(content, :width => 100)
218
+ end
219
+
220
+ expect(c.height).to eq correct_content_height + c.padding[0] + c.padding[2]
221
+ end
222
+
223
+ it "content_height should exclude padding" do
224
+ c = cell(:content => "text", :padding => 10)
225
+ expect(c.content_height).to eq @pdf.height_of("text")
226
+ end
227
+
228
+ it "content_height should exclude padding even with manual :height" do
229
+ c = cell(:content => "text", :padding => 10, :height => 400)
230
+ expect(c.content_height).to be_within(0.01).of(380)
231
+ end
232
+ end
233
+
234
+ describe "cell padding" do
235
+ include CellHelpers
236
+
237
+ it "should default to zero" do
238
+ c = cell(:content => "text")
239
+ expect(c.padding).to eq [5, 5, 5, 5]
240
+ end
241
+
242
+ it "should accept a numeric value, setting all padding" do
243
+ c = cell(:content => "text", :padding => 10)
244
+ expect(c.padding).to eq [10, 10, 10, 10]
245
+ end
246
+
247
+ it "should accept [v,h]" do
248
+ c = cell(:content => "text", :padding => [20, 30])
249
+ expect(c.padding).to eq [20, 30, 20, 30]
250
+ end
251
+
252
+ it "should accept [t,h,b]" do
253
+ c = cell(:content => "text", :padding => [10, 20, 30])
254
+ expect(c.padding).to eq [10, 20, 30, 20]
255
+ end
256
+
257
+ it "should accept [t,l,b,r]" do
258
+ c = cell(:content => "text", :padding => [10, 20, 30, 40])
259
+ expect(c.padding).to eq [10, 20, 30, 40]
260
+ end
261
+
262
+ it "should reject other formats" do
263
+ expect {
264
+ cell(:content => "text", :padding => [10])
265
+ }.to raise_error(ArgumentError)
266
+ end
267
+ end
268
+
269
+ describe "background_color" do
270
+ include CellHelpers
271
+
272
+ it "should fill a rectangle with the given background color" do
273
+ allow(@pdf).to receive(:mask).and_yield
274
+ expect(@pdf).to receive(:mask).with(:fill_color).and_yield
275
+
276
+ allow(@pdf).to receive(:fill_color)
277
+ expect(@pdf).to receive(:fill_color).with('123456')
278
+ expect(@pdf).to receive(:fill_rectangle).with([0, @pdf.cursor], 29.344, 23.872)
279
+ @pdf.cell(:content => "text", :background_color => '123456')
280
+ end
281
+
282
+ it "should draw the background in the right place if cell is drawn at a " +
283
+ "different location" do
284
+ allow(@pdf).to receive(:mask).and_yield
285
+ expect(@pdf).to receive(:mask).with(:fill_color).and_yield
286
+
287
+ allow(@pdf).to receive(:fill_color)
288
+ expect(@pdf).to receive(:fill_color).with('123456')
289
+ expect(@pdf).to receive(:fill_rectangle).with([12.0, 34.0], 29.344, 23.872)
290
+ # .checking do |(x, y), w, h|
291
+ # expect(x).to be_within(0.01).of(12.0)
292
+ # expect(y).to be_within(0.01).of(34.0)
293
+ # expect(w).to be_within(0.01).of(29.344)
294
+ # expect(h).to be_within(0.01).of(23.872)
295
+ #end
296
+ c = @pdf.make_cell(:content => "text", :background_color => '123456')
297
+ c.draw([12.0, 34.0])
298
+ end
299
+ end
300
+
301
+ describe "color" do
302
+ it "should set fill color when :text_color is provided" do
303
+ pdf = Prawn::Document.new
304
+ allow(pdf).to receive(:fill_color)
305
+ expect(pdf).to receive(:fill_color).with('555555')
306
+ pdf.cell :content => 'foo', :text_color => '555555'
307
+ end
308
+
309
+ it "should reset the fill color to the original one" do
310
+ pdf = Prawn::Document.new
311
+ pdf.fill_color = '333333'
312
+ pdf.cell :content => 'foo', :text_color => '555555'
313
+ expect(pdf.fill_color).to eq '333333'
314
+ end
315
+ end
316
+
317
+ describe "Borders" do
318
+ it "should draw all borders by default" do
319
+ expect(@pdf).to receive(:stroke_line).exactly(4).times
320
+ @pdf.cell(:content => "text")
321
+ end
322
+
323
+ it "should draw all borders when requested" do
324
+ expect(@pdf).to receive(:stroke_line).exactly(4).times
325
+ @pdf.cell(:content => "text", :borders => [:top, :right, :bottom, :left])
326
+ end
327
+
328
+ # Only roughly verifying the integer coordinates so that we don't have to
329
+ # do any FP closeness arithmetic. Can plug in that math later if this goes
330
+ # wrong.
331
+ it "should draw top border when requested" do
332
+ expect(@pdf).to receive(:stroke_line)
333
+ .and_wrap_original do |original_method, *args, &block|
334
+ from, to, = args
335
+ expect(@pdf.map_to_absolute(from).map{|x| x.round}).to eq [36, 756]
336
+ expect(@pdf.map_to_absolute(to).map{|x| x.round}).to eq [65, 756]
337
+
338
+ original_method.call(*args, &block)
339
+ end
340
+ @pdf.cell(:content => "text", :borders => [:top])
341
+ end
342
+
343
+ it "should draw bottom border when requested" do
344
+ expect(@pdf).to receive(:stroke_line)
345
+ .and_wrap_original do |original_method, *args, &block|
346
+ from, to, = args
347
+ expect(@pdf.map_to_absolute(from).map{|x| x.round}).to eq [36, 732]
348
+ expect(@pdf.map_to_absolute(to).map{|x| x.round}).to eq [65, 732]
349
+
350
+ original_method.call(*args, &block)
351
+ end
352
+ @pdf.cell(:content => "text", :borders => [:bottom])
353
+ end
354
+
355
+ it "should draw left border when requested" do
356
+ expect(@pdf).to receive(:stroke_line)
357
+ .and_wrap_original do |original_method, *args, &block|
358
+ from, to, = args
359
+ expect(@pdf.map_to_absolute(from).map{|x| x.round}).to eq [36, 756]
360
+ expect(@pdf.map_to_absolute(to).map{|x| x.round}).to eq [36, 732]
361
+
362
+ original_method.call(*args, &block)
363
+ end
364
+ @pdf.cell(:content => "text", :borders => [:left])
365
+ end
366
+
367
+ it "should draw right border when requested" do
368
+ expect(@pdf).to receive(:stroke_line)
369
+ .and_wrap_original do |original_method, *args, &block|
370
+ from, to, = args
371
+ expect(@pdf.map_to_absolute(from).map{|x| x.round}).to eq [65, 756]
372
+ expect(@pdf.map_to_absolute(to).map{|x| x.round}).to eq [65, 732]
373
+
374
+ original_method.call(*args, &block)
375
+ end
376
+ @pdf.cell(:content => "text", :borders => [:right])
377
+ end
378
+
379
+ it "should draw borders at the same location when in or out of bbox" do
380
+ expect(@pdf).to receive(:stroke_line)
381
+ .and_wrap_original do |original_method, *args, &block|
382
+ from, to, = args
383
+ expect(@pdf.map_to_absolute(from).map{|x| x.round}).to eq [36, 756]
384
+ expect(@pdf.map_to_absolute(to).map{|x| x.round}).to eq [65, 756]
385
+
386
+ original_method.call(*args, &block)
387
+ end
388
+ @pdf.bounding_box([0, @pdf.cursor], :width => @pdf.bounds.width) do
389
+ @pdf.cell(:content => "text", :borders => [:top])
390
+ end
391
+ end
392
+
393
+ it "should set border color with :border_..._color" do
394
+ allow(@pdf).to receive(:stroke_color=).with("000000")
395
+ expect(@pdf).to receive(:stroke_color=).with("ff0000")
396
+
397
+ c = @pdf.cell(:content => "text", :border_top_color => "ff0000")
398
+ expect(c.border_top_color).to eq "ff0000"
399
+ expect(c.border_colors[0]).to eq "ff0000"
400
+ end
401
+
402
+ it "should set border colors with :border_color" do
403
+ allow(@pdf).to receive(:stroke_color=).with("000000")
404
+ expect(@pdf).to receive(:stroke_color=).with("ff0000")
405
+ expect(@pdf).to receive(:stroke_color=).with("00ff00")
406
+ expect(@pdf).to receive(:stroke_color=).with("0000ff")
407
+ expect(@pdf).to receive(:stroke_color=).with("ff00ff")
408
+
409
+ c = @pdf.cell(:content => "text",
410
+ :border_color => %w[ff0000 00ff00 0000ff ff00ff])
411
+
412
+ expect(c.border_colors).to eq %w[ff0000 00ff00 0000ff ff00ff]
413
+ end
414
+
415
+ it "border_..._width should return 0 if border not selected" do
416
+ c = @pdf.cell(:content => "text", :borders => [:top])
417
+ expect(c.border_bottom_width).to eq 0
418
+ end
419
+
420
+ it "should set border width with :border_..._width" do
421
+ allow(@pdf).to receive(:line_width=).with(1)
422
+ expect(@pdf).to receive(:line_width=).with(2)
423
+
424
+ c = @pdf.cell(:content => "text", :border_bottom_width => 2)
425
+ expect(c.border_bottom_width).to eq 2
426
+ expect(c.border_widths[2]).to eq 2
427
+ end
428
+
429
+ it "should set border widths with :border_width" do
430
+ allow(@pdf).to receive(:line_width=).with(1)
431
+ expect(@pdf).to receive(:line_width=).with(2)
432
+ expect(@pdf).to receive(:line_width=).with(3)
433
+ expect(@pdf).to receive(:line_width=).with(4)
434
+ expect(@pdf).to receive(:line_width=).with(5)
435
+
436
+ c = @pdf.cell(:content => "text",
437
+ :border_width => [2, 3, 4, 5])
438
+ expect(c.border_widths).to eq [2, 3, 4, 5]
439
+ end
440
+
441
+ it "should set default border lines to :solid" do
442
+ c = @pdf.cell(:content => "text")
443
+ expect(c.border_top_line).to eq :solid
444
+ expect(c.border_right_line).to eq :solid
445
+ expect(c.border_bottom_line).to eq :solid
446
+ expect(c.border_left_line).to eq :solid
447
+ expect(c.border_lines).to eq [:solid] * 4
448
+ end
449
+
450
+ it "should set border line with :border_..._line" do
451
+ c = @pdf.cell(:content => "text", :border_bottom_line => :dotted)
452
+ expect(c.border_bottom_line).to eq :dotted
453
+ expect(c.border_lines[2]).to eq :dotted
454
+ end
455
+
456
+ it "should set border lines with :border_lines" do
457
+ c = @pdf.cell(:content => "text",
458
+ :border_lines => [:solid, :dotted, :dashed, :solid])
459
+ expect(c.border_lines).to eq [:solid, :dotted, :dashed, :solid]
460
+ end
461
+ end
462
+
463
+
464
+
465
+
466
+
467
+
468
+ describe "Text cell attributes" do
469
+ include CellHelpers
470
+
471
+ it "should pass through text options like :align to Text::Box" do
472
+ c = cell(:content => "text", :align => :right)
473
+
474
+ box = Prawn::Text::Box.new("text", :document => @pdf)
475
+
476
+ expect(Prawn::Text::Box).to receive(:new).with("text", hash_including(align: :right))
477
+ .at_least(:once).and_return(box)
478
+
479
+ c.draw
480
+ end
481
+
482
+ it "should use font_style for Text::Box#style" do
483
+ c = cell(:content => "text", :font_style => :bold)
484
+
485
+ box = Prawn::Text::Box.new("text", :document => @pdf)
486
+
487
+ expect(Prawn::Text::Box).to receive(:new).with("text", hash_including(style: :bold))
488
+ .at_least(:once).and_return(box)
489
+
490
+ c.draw
491
+ end
492
+
493
+ it "supports variant styles of the current font" do
494
+ @pdf.font "Courier"
495
+
496
+ c = cell(:content => "text", :font_style => :bold)
497
+
498
+ box = Prawn::Text::Box.new("text", :document => @pdf)
499
+ expect(Prawn::Text::Box).to receive(:new)
500
+ .and_wrap_original do |original_method, *args, &block|
501
+ text, options, = args
502
+ expect(text).to eq "text"
503
+ expect(options[:style]).to eq :bold
504
+ expect(@pdf.font.family).to eq 'Courier'
505
+ box
506
+ end.at_least(:once)
507
+
508
+ c.draw
509
+ end
510
+
511
+
512
+ it "uses the style of the current font if none given" do
513
+ @pdf.font "Courier", :style => :bold
514
+
515
+ c = cell(:content => "text")
516
+
517
+ box = Prawn::Text::Box.new("text", :document => @pdf)
518
+ expect(Prawn::Text::Box).to receive(:new)
519
+ .and_wrap_original do |original_method, *args, &block|
520
+ text = args.first
521
+ expect(text).to eq "text"
522
+ expect(@pdf.font.family).to eq 'Courier'
523
+ expect(@pdf.font.options[:style]).to eq :bold
524
+ box
525
+ end.at_least(:once)
526
+
527
+ c.draw
528
+ end
529
+
530
+ it "should allow inline formatting in cells" do
531
+ c = cell(:content => "foo <b>bar</b> baz", :inline_format => true)
532
+
533
+ box = Prawn::Text::Formatted::Box.new([], :document => @pdf)
534
+
535
+ expect(Prawn::Text::Formatted::Box).to receive(:new).with(
536
+ [
537
+ hash_including(text: "foo ", styles: []),
538
+ hash_including(text: "bar", styles: [:bold]),
539
+ hash_including(text: " baz", styles: [])
540
+ ],
541
+ kind_of(Hash)
542
+ ).at_least(:once).and_return(box)
543
+
544
+ c.draw
545
+ end
546
+
547
+ end
548
+
549
+ describe "Font handling" do
550
+ include CellHelpers
551
+
552
+ it "should allow only :font_style to be specified, defaulting to the " +
553
+ "document's font" do
554
+ c = cell(:content => "text", :font_style => :bold)
555
+ expect(c.font.name).to eq 'Helvetica-Bold'
556
+ end
557
+
558
+ it "should accept a font name for :font" do
559
+ c = cell(:content => "text", :font => 'Helvetica-Bold')
560
+ expect(c.font.name).to eq 'Helvetica-Bold'
561
+ end
562
+
563
+ it "should use the specified font to determine font metrics" do
564
+ c = cell(:content => 'text', :font => 'Courier', :font_style => :bold)
565
+ font = @pdf.find_font('Courier-Bold')
566
+ expect(c.content_width).to eq font.compute_width_of("text")
567
+ end
568
+
569
+ it "should allow style to be changed after initialize" do
570
+ c = cell(:content => "text")
571
+ c.font_style = :bold
572
+ expect(c.font.name).to eq 'Helvetica-Bold'
573
+ end
574
+
575
+ it "should default to the document's font, if none is specified" do
576
+ c = cell(:content => "text")
577
+ expect(c.font).to eq @pdf.font
578
+ end
579
+
580
+ it "should use the metrics of the selected font (even if it is a variant " +
581
+ "of the document's font) to calculate width" do
582
+ c = cell(:content => "text", :font_style => :bold)
583
+ font = @pdf.find_font('Helvetica-Bold')
584
+ expect(c.content_width).to eq font.compute_width_of("text")
585
+ end
586
+
587
+ it "should properly calculate inline-formatted text" do
588
+ c = cell(:content => "<b>text</b>", :inline_format => true)
589
+ font = @pdf.find_font('Helvetica-Bold')
590
+ expect(c.content_width).to eq font.compute_width_of("text")
591
+ end
592
+ end
593
+ end
594
+
595
+ describe "Image cells" do
596
+ before(:each) do
597
+ create_pdf
598
+ end
599
+
600
+ describe "with default options" do
601
+ before(:each) do
602
+ @cell = Prawn::Table::Cell.make(@pdf,
603
+ { :image => file_fixture("prawn.png").to_s })
604
+ end
605
+
606
+ it "should create a Cell::Image" do
607
+ expect(@cell).to be_a_kind_of(Prawn::Table::Cell::Image)
608
+ end
609
+
610
+ it "should pull the natural width and height from the image" do
611
+ expect(@cell.natural_content_width).to eq 141
612
+ expect(@cell.natural_content_height).to eq 142
613
+ end
614
+ end
615
+
616
+ describe "hash syntax" do
617
+ before(:each) do
618
+ @table = @pdf.make_table([[{
619
+ :image => file_fixture("prawn.png").to_s,
620
+ :scale => 2,
621
+ :fit => [100, 200],
622
+ :image_width => 123,
623
+ :image_height => 456,
624
+ :position => :center,
625
+ :vposition => :center
626
+ }]])
627
+ @cell = @table.cells[0, 0]
628
+ end
629
+
630
+
631
+ it "should create a Cell::Image" do
632
+ expect(@cell).to be_a_kind_of(Prawn::Table::Cell::Image)
633
+ end
634
+
635
+ it "should pass through image options" do
636
+ expect(@pdf).to receive(:embed_image).with(
637
+ anything, anything,
638
+ hash_including(
639
+ scale: 2,
640
+ fit: [100, 200],
641
+ width: 123,
642
+ height: 456,
643
+ position: :center,
644
+ vposition: :center
645
+ )
646
+ )
647
+
648
+ @table.draw
649
+ end
650
+ end
651
+
652
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ module EncodingHelpers
4
+ def win1252_string(str)
5
+ str.force_encoding(Encoding::Windows_1252)
6
+ end
7
+
8
+ def bin_string(str)
9
+ str.force_encoding(Encoding::ASCII_8BIT)
10
+ end
11
+ end