1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
require_relative 'spec_helper'
describe 'Asciidoctor::PDF::Converter - Table' do
context 'Decoration' do
it 'should apply frame all and grid all by default' do
pdf = to_pdf <<~'EOS', analyze: :line
|===
|1 |2
|3 |4
|===
EOS
(expect pdf.lines.uniq).to have_size 12
end
it 'should allow frame and grid to be specified on table using frame and grid attributes' do
pdf = to_pdf <<~'EOS', analyze: :line
[frame=ends,grid=cols]
|===
|1 |2
|3 |4
|===
EOS
(expect pdf.lines.uniq).to have_size 6
end
it 'should treat topbot value of frame attribute as an alias for ends' do
pdf_a = to_pdf <<~'EOS', analyze: :line
[frame=ends]
|===
|1 |2
|3 |4
|===
EOS
pdf_b = to_pdf <<~'EOS', analyze: :line
[frame=topbot]
|===
|1 |2
|3 |4
|===
EOS
(expect pdf_a.lines).to eql pdf_b.lines
end
it 'should allow frame and grid to be set globally using table-frame and table-grid attributes' do
pdf = to_pdf <<~'EOS', analyze: :line
:table-frame: ends
:table-grid: cols
|===
|1 |2
|3 |4
|===
EOS
(expect pdf.lines.uniq).to have_size 6
end if asciidoctor_2_or_better?
it 'should apply stripes to specified group of rows as specified by stripes attribute', integration: true do
to_file = to_pdf_file <<~'EOS', 'table-stripes-odd.pdf'
[cols=3*,stripes=odd]
|===
|A1 |B1 |C1
|A2 |B2 |C2
|A3 |B3 |C3
|===
EOS
(expect to_file).to visually_match 'table-stripes-odd.pdf'
end if asciidoctor_1_5_7_or_better?
it 'should apply thicker bottom border to table head row' do
pdf = to_pdf <<~'EOS', analyze: :line
[frame=none,grid=rows]
|===
| Col A | Col B
| A1
| B1
| A2
| B2
|===
EOS
lines = pdf.lines.uniq
(expect lines).to have_size 4
(expect lines[0][:width]).to eql 1.25
(expect lines[1][:width]).to eql 1.25
(expect lines[0][:from][:y]).to eql lines[0][:to][:y]
(expect lines[1][:from][:y]).to eql lines[1][:to][:y]
(expect lines[0][:from][:y]).to eql lines[1][:from][:y]
(expect lines[2][:width]).to eql 0.5
(expect lines[3][:width]).to eql 0.5
end
it 'should allow theme to customize bottom border of table head row', integration: true do
theme_overrides = {
table_head_border_bottom_width: 0.5,
table_head_border_bottom_style: 'dashed',
table_head_border_bottom_color: 'a9a9a9',
}
to_file = to_pdf_file <<~'EOS', 'table-head-border-bottom.pdf', pdf_theme: theme_overrides
[frame=none,grid=rows]
|===
| Col A | Col B
| A1
| B1
| A2
| B2
|===
EOS
(expect to_file).to visually_match 'table-head-border-bottom.pdf'
end
it 'should allow theme to set table border color to transparent' do
theme_overrides = {
table_border_color: 'transparent',
table_head_border_bottom_color: 'transparent',
}
pdf = to_pdf <<~'EOS', analyze: :line, pdf_theme: theme_overrides
[frame=none,grid=rows]
|===
| Col A | Col B
| A1
| B1
| A2
| B2
|===
EOS
pdf.lines.uniq.each do |line|
(expect line[:color]).to eql '00000000'
end
end
end
context 'Dimensions' do
it 'should not fail to fit text in cell' do
pdf = to_pdf <<~'EOS', analyze: true
|===
|Aaaaa Bbbbb Ccccc |*{zwsp}* Aaaaa_Bbbbb_Ccccc |Aaaaa_Bbbbb_Ccccc |Aaaaa_Bbbbb_Ccccc |A
|===
EOS
(expect pdf.strings.index 'Aaaaa Bbbbb').to eql 0
(expect pdf.strings.index 'Ccccc').to eql 1
end
it 'should not break words in head row when autowidth option is set' do
pdf = to_pdf <<~'EOS', analyze: true
[%autowidth]
|===
|Operation |Operator
|add
|+
|subtract
|-
|multiply
|*
|divide
|/
|===
EOS
(expect pdf.find_text 'Operation').not_to be_empty
(expect pdf.find_text 'Operator').not_to be_empty
end
it 'should not break words in body rows when autowidth option is set' do
pdf = to_pdf <<~'EOS', analyze: true
[%autowidth]
|===
|Op
|add
|subtract
|multiply
|divide
|===
EOS
(expect pdf.find_text 'add').not_to be_empty
(expect pdf.find_text 'subtract').not_to be_empty
(expect pdf.find_text 'multiply').not_to be_empty
(expect pdf.find_text 'divide').not_to be_empty
end
it 'should stretch table to width of bounds by default' do
pdf = to_pdf <<~'EOS', analyze: :line
[grid=none,frame=sides]
|===
|A |B
|===
EOS
lines = pdf.lines
(expect lines).to have_size 2
(expect lines[0][:from][:x]).to eql 48.24
(expect lines[1][:from][:x]).to eql 547.04
end
it 'should not stretch autowidth table to width of bounds by default' do
pdf = to_pdf <<~'EOS', analyze: :line
[%autowidth,grid=none,frame=sides]
|===
|A |B
|===
EOS
lines = pdf.lines
(expect lines).to have_size 2
(expect lines[0][:from][:x]).to eql 48.24
(expect lines[1][:from][:x]).to be < 100
end
it 'should stretch autowidth table with stretch role to width of bounds' do
pdf = to_pdf <<~'EOS', analyze: :line
[%autowidth.stretch,grid=none,frame=sides]
|===
|A |B
|===
EOS
lines = pdf.lines
(expect lines).to have_size 2
(expect lines[0][:from][:x]).to eql 48.24
(expect lines[1][:from][:x]).to eql 547.04
end
it 'should allocate remaining width to autowidth column' do
pdf = to_pdf <<~'EOS', analyze: true
[cols="10,~"]
|===
|0x00
|UNSPECIFIED
|0x01
|OK
|===
EOS
(expect pdf.strings).to eql %w(0x00 UNSPECIFIED 0x01 OK)
unspecified_text = (pdf.find_text 'UNSPECIFIED')[0]
(expect unspecified_text[:x]).to eql 101.12
ok_text = (pdf.find_text 'OK')[0]
(expect ok_text[:x]).to eql 101.12
end if asciidoctor_1_5_7_or_better?
it 'should not accumulate cell padding between tables' do
pdf = to_pdf <<~'EOS', pdf_theme: { table_cell_padding: [5, 5, 5, 5] }, analyze: true
|===
|A |B
|A1
|B1
|A2
|B2
|===
|===
|A |B
|A1
|B1
|A2
|B2
|===
|===
|A |B
|A1
|B1
|A2
|B2
|===
EOS
first_a1_text = (pdf.find_text 'A1')[0]
first_a2_text = (pdf.find_text 'A2')[0]
last_a1_text = (pdf.find_text 'A1')[-1]
last_a2_text = (pdf.find_text 'A2')[-1]
(expect first_a1_text[:y] - first_a2_text[:y]).to eql (last_a1_text[:y] - last_a2_text[:y])
end
it 'should set padding on head cells the same as body cells by default' do
input = <<~'EOS'
[frame=none,grid=rows]
|===
| Column A | Column B
| A1
| B1
|===
EOS
reference_pdf = to_pdf input, analyze: :line
pdf = to_pdf input, pdf_theme: { table_cell_padding: [10, 3, 10, 3] }, analyze: :line
# NOTE the line under the head row should moved down
(expect pdf.lines[0][:from][:y]).to be < reference_pdf.lines[0][:from][:y]
end
it 'should set padding on head cells as specified by table_head_cell_padding theme key' do
input = <<~'EOS'
[frame=none,grid=rows]
|===
| Column A | Column B
| A1
| B1
|===
EOS
reference_pdf = to_pdf input, analyze: true
pdf = to_pdf input, pdf_theme: { table_head_cell_padding: [10, 3, 10, 3] }, analyze: true
reference_a1_text = (reference_pdf.find_text 'A1')[0]
a1_text = (pdf.find_text 'A1')[0]
# NOTE the first body row should be moved down
(expect a1_text[:y]).to be < reference_a1_text[:y]
end
it 'should not split cells in head row across pages' do
hard_line_break = %( +\n)
filler = (['filler'] * 40).join hard_line_break
head_cell_1 = %w(this is a very tall cell in the head row of this table).join hard_line_break
head_cell_2 = %w(this is an even taller cell also in the head row of this table).join hard_line_break
pdf = to_pdf <<~EOS, analyze: true
#{filler}
[%header,cols=2*]
|===
|#{head_cell_1}
|#{head_cell_2}
|body cell
|body cell
|===
EOS
filler_page_nums = (pdf.find_text 'filler').map {|it| it[:page_number] }
(expect filler_page_nums.uniq).to have_size 1
(expect filler_page_nums[0]).to eql 1
table_cell_page_nums = pdf.text.reject {|it| it[:string] == 'filler' }.map {|it| it[:page_number] }
(expect table_cell_page_nums.uniq).to have_size 1
(expect table_cell_page_nums[0]).to eql 2
end
end
context 'Basic table cell' do
it 'should keep paragraphs in table cell separate' do
pdf = to_pdf <<~'EOS', analyze: true
|===
|all one line
|line 1 +
line 2
|paragraph 1
paragraph 2
|===
EOS
cell_1_text = pdf.find_text 'all one line'
(expect cell_1_text).not_to be_empty
cell_2_text = pdf.find_text %r/^line (?:1|2)/
(expect cell_2_text).to have_size 2
(expect cell_2_text[0][:y]).to be > cell_2_text[1][:y]
cell_3_text = pdf.find_text %r/^paragraph (?:1|2)/
(expect cell_3_text).to have_size 2
(expect cell_3_text[0][:y]).to be > cell_3_text[1][:y]
(expect cell_3_text[0][:y] - cell_3_text[1][:y]).to be > (cell_2_text[0][:y] - cell_2_text[1][:y])
end
it 'should normalize newlines and whitespace' do
pdf = to_pdf <<~EOS, analyze: true
|===
|He's a real nowhere man,
Sitting in his nowhere land,
Making all his nowhere plans\tfor nobody.
|===
EOS
(expect pdf.text).to have_size 1
text = pdf.text[0][:string]
(expect text).not_to include ' '
(expect text).not_to include ?\t
(expect text).not_to include ?\n
(expect text).to include 'man, Sitting'
end
end
context 'AsciiDoc table cell' do
it 'should convert blocks in an AsciiDoc table cell' do
pdf = to_pdf <<~'EOS', analyze: true
|===
a|
[start=10]
. ten
. eleven
. twelve
[%hardbreaks]
buckle
my
shoe
|===
EOS
(expect pdf.lines).to eql ['10.ten', '11.eleven', '12.twelve', 'buckle', 'my', 'shoe']
end
it 'should convert nested table' do
pdf = to_pdf <<~'EOS', analyze: true
[cols="1,2a"]
|===
|Normal cell
|Cell with nested table
[cols="2,1"]
!===
!Nested table cell 1 !Nested table cell 2
!===
|===
EOS
(expect pdf.lines.find {|l| l.include? '!' }).to be_nil
(expect pdf.lines).to have_size 2
(expect pdf.lines[1]).to eql 'Nested table cell 1Nested table cell 2'
nested_cell_1 = (pdf.find_text 'Nested table cell 1')[0]
nested_cell_2 = (pdf.find_text 'Nested table cell 2')[0]
(expect nested_cell_1[:y]).to eql nested_cell_2[:y]
(expect nested_cell_1[:x]).to be < nested_cell_2[:x]
end
end
context 'Caption' do
it 'should add title as caption above table by default' do
pdf = to_pdf <<~'EOS', analyze: true
.Table description
|===
| Col A | Col B
| A1
| B1
| A2
| B2
|===
EOS
caption_text = pdf.text[0]
(expect caption_text[:string]).to eql 'Table 1. Table description'
(expect caption_text[:font_name]).to eql 'NotoSerif-Italic'
(expect caption_text[:y]).to be > (pdf.find_text 'Col A')[0][:y]
end
it 'should add title as caption below table if table_caption_side key in theme is bottom' do
pdf = to_pdf <<~'EOS', pdf_theme: { table_caption_side: 'bottom' }, analyze: true
.Table description
|===
| Col A | Col B
| A1
| B1
| A2
| B2
|===
EOS
caption_text = pdf.text[-1]
(expect caption_text[:string]).to eql 'Table 1. Table description'
(expect caption_text[:y]).to be < (pdf.find_text 'B2')[0][:y]
end
it 'should confine caption to width of table by default', integration: true do
to_file = to_pdf_file <<~'EOS', 'table-caption-width.pdf', pdf_theme: { caption_align: 'center' }
.A rather long description for this table
[%header%autowidth]
|===
| Col A | Col B
| A1
| B1
| A2
| B2
|===
.A rather long description for this table
[%header%autowidth,align=center]
|===
| Col C | Col D
| C1
| D1
| C2
| D2
|===
.A rather long description for this table
[%header%autowidth,align=right]
|===
| Col E | Col F
| E1
| F1
| E2
| F2
|===
EOS
(expect to_file).to visually_match 'table-caption-width.pdf'
end
it 'should not confine caption to width of table if table_caption_max_width key in theme is none' do
pdf = to_pdf <<~'EOS', pdf_theme: { caption_align: 'center', table_caption_max_width: 'none' }, analyze: true
:table-caption!:
.A rather long description for this table
[%autowidth]
|===
| Col A | Col B
| A1
| B1
| A2
| B2
|===
.A rather long description for this table
[%autowidth,align=center]
|===
| Col C | Col D
| C1
| D1
| C2
| D2
|===
.A rather long description for this table
[%autowidth,align=right]
|===
| Col E | Col F
| E1
| F1
| E2
| F2
|===
EOS
caption_texts = pdf.find_text 'A rather long description for this table'
(expect caption_texts).to have_size 3
(expect caption_texts.map {|it| it[:x] }.uniq).to have_size 1
end
end
context 'Table alignment' do
it 'should allow theme to customize default alignment of table ' do
pdf = to_pdf <<~'EOS', pdf_theme: { table_align: 'right' }, analyze: true
[cols=3*,width=50%]
|===
|RIGHT |B1 |C1
|A2 |B2 |C2
|A3 |B3 |C3
|===
[cols=3*,width=50%,align=left]
|===
|LEFT |B1 |C1
|A2 |B2 |C2
|A3 |B3 |C3
|===
EOS
cell_right = (pdf.find_text 'RIGHT')[0]
cell_left = (pdf.find_text 'LEFT')[0]
(expect cell_right[:x]).to be > cell_left[:x]
end
it 'should allow position of table to be set using align attribute on table' do
pdf = to_pdf <<~'EOS', analyze: true
[cols=3*,width=50%]
|===
|LEFT |B1 |C1
|A2 |B2 |C2
|A3 |B3 |C3
|===
[cols=3*,width=50%,align=right]
|===
|RIGHT |B1 |C1
|A2 |B2 |C2
|A3 |B3 |C3
|===
EOS
cell_right_text = (pdf.find_text 'RIGHT')[0]
cell_left_text = (pdf.find_text 'LEFT')[0]
(expect cell_right_text[:x]).to be > cell_left_text[:x]
end
it 'should not mangle margin box on subsequent pages if table with alignment crosses page boundary' do
blank_line = %(\n\n)
pdf = to_pdf <<~EOS, analyze: true
#{(['filler'] * 25).join blank_line}
[%autowidth,align=right]
|===
|A | B
|A1
|B1
|A2
|B2
|A3
|B3
|===
#{(['filler'] * 22).join blank_line}
#{(['* list item'] * 6).join ?\n}
EOS
page_width = pdf.pages[0][:size][0]
a1_text = (pdf.find_text 'A1')[0]
a3_text = (pdf.find_text 'A3')[0]
(expect a1_text[:x]).to be > (page_width * 0.5)
(expect a1_text[:page_number]).to eql 1
(expect a3_text[:x]).to be > (page_width * 0.5)
(expect a3_text[:page_number]).to eql 2
first_list_item_text = (pdf.find_text string: 'list item', page_number: 2)[0]
last_list_item_text = (pdf.find_text string: 'list item', page_number: 3)[-1]
# NOTE if this is off, the margin box got mangled
(expect last_list_item_text[:x]).to eql first_list_item_text[:x]
end
it 'should set width of aligned table relative to bounds' do
pdf = to_pdf <<~EOS, analyze: true
[width=25%,align=right]
|===
|A | B
|A1
|B1
|A2
|B2
|===
====
****
[width=25%,align=right]
|===
|A | B
|A1
|B1
|A2
|B2
|===
****
====
EOS
page_width = pdf.pages[0][:size][0]
first_a1_text = (pdf.find_text 'A1')[0]
second_a1_text = (pdf.find_text 'A1')[1]
(expect first_a1_text[:x]).to be > (page_width * 0.5)
(expect second_a1_text[:x]).to be > (page_width * 0.5)
(expect first_a1_text[:x]).to be > second_a1_text[:x]
end
end
end
|