File: listing_spec.rb

package info (click to toggle)
ruby-asciidoctor-pdf 2.3.19-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,972 kB
  • sloc: ruby: 44,316; sh: 133; java: 45; makefile: 4
file content (853 lines) | stat: -rw-r--r-- 24,858 bytes parent folder | download | duplicates (2)
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
# frozen_string_literal: true

require_relative 'spec_helper'

describe 'Asciidoctor::PDF::Converter - Listing' do
  it 'should render empty block if listing block is empty' do
    pdf_theme = {
      code_line_height: 1,
      code_padding: 0,
      code_border_width: 1,
      code_border_radius: 0,
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: :line
    ----
    ----
    EOS

    lines = pdf.lines
    (expect lines).to have_size 4
    (expect lines[1][:from][:y] - lines[1][:to][:y]).to be <= 1
  end

  it 'should wrap text consistently regardless of whether the characters contain diacritics' do
    pdf = to_pdf <<~'EOS', analyze: true
    :pdf-page-size: A5

    ....
    aàbècìdòeùf gáhéiíjókúlým nâoêpîqôrûs tñuõvãw xäyëzïaöbücÿd
    aabbccddeef gghhiijjkkllm nnooppqqrrs ttuuvvw xxyyzzaabbccd
    ....
    EOS

    text = pdf.text
    (expect text).to have_size 4
    (expect text[1][:string]).to start_with 'x'
    (expect text[3][:string]).to start_with 'x'
  end

  it 'should move unbreakable block shorter than page to next page to avoid splitting it' do
    pdf = to_pdf <<~EOS, analyze: true
    #{(['paragraph'] * 20).join (?\n * 2)}

    [%unbreakable]
    ----
    #{(['listing'] * 20).join ?\n}
    ----
    EOS

    listing_page_numbers = (pdf.find_text 'listing').map {|it| it[:page_number] }.uniq
    (expect listing_page_numbers).to eql [2]
  end

  it 'should keep anchor together with block when block is moved to next page' do
    pdf = to_pdf <<~EOS
    #{(['paragraph'] * 20).join (?\n * 2)}

    [#listing-1%unbreakable]
    ----
    #{(['listing'] * 20).join ?\n}
    ----
    EOS

    (expect (pdf.page 1).text).not_to include 'listing'
    (expect (pdf.page 2).text).to include 'listing'
    (expect (dest = get_dest pdf, 'listing-1')).not_to be_nil
    (expect dest[:page_number]).to be 2
    (expect dest[:y]).to eql 805.89
  end

  it 'should place anchor directly at top of block' do
    input = <<~'EOS'
    paragraph

    [#listing-1]
    ----
    listing
    ----
    EOS

    lines = (to_pdf input, analyze: :line).lines
    pdf = to_pdf input
    (expect (dest = get_dest pdf, 'listing-1')).not_to be_nil
    (expect dest[:page_number]).to be 1
    (expect dest[:y]).to eql lines[0][:from][:y]
  end

  it 'should offset anchor from top of block by value of block_anchor_top' do
    input = <<~'EOS'
    paragraph

    [#listing-1]
    ----
    listing
    ----
    EOS

    pdf_theme = { block_anchor_top: -12 }

    lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines
    pdf = to_pdf input, pdf_theme: pdf_theme
    (expect (dest = get_dest pdf, 'listing-1')).not_to be_nil
    (expect dest[:page_number]).to be 1
    (expect dest[:y]).to eql (lines[0][:from][:y] + -pdf_theme[:block_anchor_top])
  end

  it 'should place anchor at top of block if advanced to next page' do
    input = <<~EOS
    paragraph

    [#listing-1%unbreakable]
    ----
    #{(['filler'] * 25).join %(\n\n)}
    ----
    EOS

    lines = (to_pdf input, analyze: :line).lines
    pdf = to_pdf input
    (expect (dest = get_dest pdf, 'listing-1')).not_to be_nil
    (expect dest[:page_number]).to be 2
    (expect dest[:y]).to eql lines[0][:from][:y]
  end

  it 'should split block if it cannot fit on a whole page' do
    pdf = to_pdf <<~EOS, analyze: true
    #{(['paragraph'] * 20).join (?\n * 2)}

    ----
    #{(['listing'] * 60).join ?\n}
    ----
    EOS

    (expect pdf.pages).to have_size 2
    listing_texts = pdf.find_text 'listing'
    (expect listing_texts[0][:page_number]).to be 1
    (expect listing_texts[-1][:page_number]).to be 2
  end

  it 'should use dashed border to indicate where block is split across a page boundary', visual: true do
    to_file = to_pdf_file <<~EOS, 'listing-page-split.pdf'
    ----
    #{(['listing'] * 60).join ?\n}
    ----

    ----
    #{(['more listing'] * 2).join ?\n}
    ----
    EOS

    (expect to_file).to visually_match 'listing-page-split.pdf'
  end

  it 'should not collapse bottom padding if block ends near bottom of page' do
    pdf_theme = {
      code_padding: 11,
      code_background_color: 'EEEEEE',
      code_border_width: 0,
      code_border_radius: 0,
    }
    pdf = with_content_spacer 10, 695 do |spacer_path|
      to_pdf <<~EOS, pdf_theme: pdf_theme, analyze: true
      image::#{spacer_path}[]

      ----
      $ gem install asciidoctor-pdf
      $ asciidoctor-pdf doc.adoc
      ----
      EOS
    end

    pages = pdf.pages
    (expect pages).to have_size 1
    gs = pdf.extract_graphic_states pages[0][:raw_content]
    (expect gs[1]).to have_background color: 'EEEEEE', top_left: [48.24, 98.89], bottom_right: [48.24, 48.24]
    last_text_y = pdf.text[-1][:y]
    (expect last_text_y - pdf_theme[:code_padding]).to be > 48.24

    pdf = with_content_spacer 10, 696 do |spacer_path|
      to_pdf <<~EOS, pdf_theme: pdf_theme, analyze: true
      image::#{spacer_path}[]

      ----
      $ gem install asciidoctor-pdf
      $ asciidoctor-pdf doc.adoc
      ----
      EOS
    end

    pages = pdf.pages
    (expect pages).to have_size 2
    gs = pdf.extract_graphic_states pages[0][:raw_content]
    (expect gs[1]).to have_background color: 'EEEEEE', top_left: [48.24, 97.89], bottom_right: [48.24, 48.24]
    (expect pdf.text[0][:page_number]).to eql 1
    (expect pdf.text[1][:page_number]).to eql 2
    (expect pdf.text[0][:y] - pdf_theme[:code_padding]).to be > 48.24
  end

  it 'should break line if wider than content area of block and still compute height correctly' do
    pdf_theme = {
      code_border_radius: 0,
      code_border_color: 'CCCCCC',
      code_border_width: [1, 0],
      code_background_color: 'transparent',
      sidebar_border_radius: 0,
      sidebar_border_width: [1, 0],
      sidebar_border_color: '0000EE',
      sidebar_background_color: 'transparent',
    }
    input = <<~EOS
    ****
    before

    ----
    one
    tw#{'o' * 250}
    three
    ----

    after
    ****
    EOS

    pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
    (expect pdf.find_text %r/^ooo/).to have_size 3
    lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines.sort_by {|it| -it[:from][:y] }
    (expect lines).to have_size 4
    (expect lines[0][:color]).to eql '0000EE'
    (expect lines[1][:color]).to eql 'CCCCCC'
    (expect lines[2][:color]).to eql 'CCCCCC'
    (expect lines[3][:color]).to eql '0000EE'
    (expect (lines[0][:from][:y] - lines[1][:from][:y]).round 5).to eql ((lines[2][:from][:y] - lines[3][:from][:y]).round 5)
  end

  it 'should resize font to prevent wrapping if autofit option is set' do
    pdf = to_pdf <<~'EOS', pdf_theme: { code_font_size: 12 }, analyze: true
    [%autofit]
    ----
    @themesdir = ::File.expand_path theme.__dir__ || (doc.attr 'pdf-themesdir') || ::Dir.pwd
    ----
    EOS

    (expect pdf.text).to have_size 1
    (expect pdf.text[0][:font_size]).to be < 12
  end

  it 'should not resize font if not necessary' do
    pdf = to_pdf <<~'EOS', analyze: true
    [%autofit]
    ----
    puts 'Hello, World!'
    ----
    EOS

    (expect pdf.text).to have_size 1
    (expect pdf.text[0][:font_size]).to eql 11
  end

  it 'should not resize font more than base minimum font size' do
    pdf = to_pdf <<~'EOS', pdf_theme: { base_font_size_min: 8 }, analyze: true
    [%autofit]
    ----
    play_symbol = (node.document.attr? 'icons', 'font') ? %(<font name="fas">#{(icon_font_data 'fas').unicode 'play'}</font>) : RightPointer
    ----
    EOS

    (expect pdf.text).to have_size 2
    (expect pdf.text[0][:font_size]).to be 8
  end

  it 'should not resize font more than code minimum font size' do
    pdf = to_pdf <<~'EOS', pdf_theme: { base_font_size_min: 0, code_font_size_min: 8 }, analyze: true
    [%autofit]
    ----
    play_symbol = (node.document.attr? 'icons', 'font') ? %(<font name="fas">#{(icon_font_data 'fas').unicode 'play'}</font>) : RightPointer
    ----
    EOS

    (expect pdf.text).to have_size 2
    (expect pdf.text[0][:font_size]).to be 8
  end

  it 'should allow autofit to shrink text as much as it needs if the minimum font size is 0 or nil' do
    [0, nil].each do |size|
      pdf = to_pdf <<~'EOS', pdf_theme: { base_font_size_min: size }, analyze: true
      [%autofit]
      ----
      +--------------------------------------+----------------------------------------------------+-----------------------------------------------------+
      | id                                   | name                                               | subnets                                             |
      +--------------------------------------+----------------------------------------------------+-----------------------------------------------------+
      ----
      EOS

      expected_line = '+--------------------------------------+----------------------------------------------------+-----------------------------------------------------+'
      lines = pdf.lines
      (expect lines).to have_size 3
      (expect lines[0]).to eql expected_line
      (expect lines[2]).to eql expected_line
    end
  end

  it 'should use base font color if font color is not specified' do
    pdf = to_pdf <<~'EOS', pdf_theme: { base_font_color: 'AA0000', code_font_color: nil }, analyze: true
    before

    ----
    in the mix
    ----
    EOS

    before_text = pdf.find_unique_text 'before'
    (expect before_text[:font_color]).to eql 'AA0000'

    code_text = pdf.find_unique_text 'in the mix'
    (expect code_text[:font_color]).to eql 'AA0000'
  end

  it 'should allow theme to set different padding per edge when autofit is enabled' do
    pdf_theme = {
      code_border_radius: 0,
      code_padding: [5, 10, 15, 20],
      code_background_color: nil,
    }

    input = <<~EOS
    [%autofit]
    ----
    downloading#{(%w(.) * 100).join}
    done
    ----
    EOS

    text = (to_pdf input, pdf_theme: pdf_theme, analyze: true).text
    lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines

    (expect text).to have_size 2

    left = lines[0][:from][:x]
    top = lines[0][:to][:y]
    bottom = lines[1][:to][:y]
    (expect text[0][:x]).to eql (left + 20.0).round 2
    (expect text[0][:y] + text[0][:font_size]).to be_within(2).of(top - 5)
    (expect text[1][:y]).to be_within(5).of(bottom + 15)
  end

  it 'should guard indentation using no-break space character' do
    pdf = to_pdf <<~'EOS', analyze: true
    ----
    flush
      indented
    flush
    ----
    EOS

    (expect pdf.lines).to eql ['flush', %(\u00a0 indented), 'flush']
  end

  it 'should guard indentation using no-break space character if string starts with indented line' do
    pdf = to_pdf <<~'EOS', analyze: true
    ----
      indented
    flush
      indented
    ----
    EOS

    (expect pdf.lines).to eql [%(\u00a0 indented), 'flush', %(\u00a0 indented)]
  end

  it 'should expand tabs if tabsize attribute is not specified' do
    pdf = to_pdf <<~EOS, analyze: true
    ----
    flush
      lead space
    \tlead tab
    \tlead tab\tcolumn tab
      lead space\tcolumn tab

    flush\t\t\tcolumn tab
    ----
    EOS

    expected_lines = [
      'flush',
      %(\u00a0 lead space),
      %(\u00a0   lead tab),
      %(\u00a0   lead tab    column tab),
      %(\u00a0 lead space    column tab),
      'flush           column tab',
    ]

    (expect pdf.lines).to eql expected_lines
    lines = pdf.text
    line_gaps = 1.upto(lines.size - 1).map {|idx| (lines[idx - 1][:y] - lines[idx][:y]).round 2 }
    (expect line_gaps[-1]).to eql line_gaps[-2] * 2
    (expect line_gaps[-2]).to eql line_gaps[-3]
  end

  it 'should expand tabs if tabsize is specified as block attribute' do
    pdf = to_pdf <<~EOS, analyze: true
    [tabsize=4]
    ----
    flush
      lead space
    \tlead tab
    \tlead tab\tcolumn tab
      lead space\tcolumn tab

    flush\t\t\tcolumn tab
    ----
    EOS

    expected_lines = [
      'flush',
      %(\u00a0 lead space),
      %(\u00a0   lead tab),
      %(\u00a0   lead tab    column tab),
      %(\u00a0 lead space    column tab),
      'flush           column tab',
    ]

    (expect pdf.lines).to eql expected_lines
    lines = pdf.text
    line_gaps = 1.upto(lines.size - 1).map {|idx| (lines[idx - 1][:y] - lines[idx][:y]).round 2 }
    (expect line_gaps[-1]).to eql line_gaps[-2] * 2
    (expect line_gaps[-2]).to eql line_gaps[-3]
  end

  it 'should expand tabs if tabsize is specified as document attribute' do
    pdf = to_pdf <<~EOS, analyze: true
    :tabsize: 4

    ----
    flush
      lead space
    \tlead tab
    \tlead tab\tcolumn tab
      lead space\tcolumn tab

    flush\t\t\tcolumn tab
    ----
    EOS

    expected_lines = [
      'flush',
      %(\u00a0 lead space),
      %(\u00a0   lead tab),
      %(\u00a0   lead tab    column tab),
      %(\u00a0 lead space    column tab),
      'flush           column tab',
    ]

    (expect pdf.lines).to eql expected_lines
    lines = pdf.text
    line_gaps = 1.upto(lines.size - 1).map {|idx| (lines[idx - 1][:y] - lines[idx][:y]).round 2 }
    (expect line_gaps[-1]).to eql line_gaps[-2] * 2
    (expect line_gaps[-2]).to eql line_gaps[-3]
  end

  it 'should add numbered label to block title if listing-caption attribute is set' do
    pdf = to_pdf <<~'EOS', analyze: true
    :listing-caption: Listing

    .Title
    ----
    content
    ----
    EOS

    title_text = pdf.find_unique_text font_name: 'NotoSerif-Italic'
    (expect title_text[:string]).to eql 'Listing 1. Title'
  end

  it 'should allow theme to override caption for code blocks' do
    pdf_theme = {
      caption_font_color: '0000ff',
      code_caption_font_style: 'bold',
    }

    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    .Title
    ----
    content
    ----
    EOS

    title_text = (pdf.find_text 'Title')[0]
    (expect title_text[:font_color]).to eql '0000FF'
    (expect title_text[:font_name]).to eql 'NotoSerif-Bold'
  end

  it 'should allow theme to set background color on caption' do
    pdf_theme = {
      code_caption_font_color: 'ffffff',
      code_caption_font_style: 'bold',
      code_caption_background_color: 'AA0000',
      code_caption_margin_outside: 10,
      code_background_color: 'transparent',
      code_border_radius: 0,
    }

    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    .Caption with background color
    ----
    content
    ----
    EOS

    title_text = pdf.find_unique_text 'Caption with background color'
    (expect title_text[:font_color]).to eql 'FFFFFF'
    (expect title_text[:font_name]).to eql 'NotoSerif-Bold'
    (expect pdf.pages[0][:raw_content]).to include %(/DeviceRGB cs\n0.66667 0.0 0.0 scn\n48.24 790.899 498.8 14.991 re)
    (expect title_text[:y]).to be > 790.899
    (expect title_text[:y]).to (be_within 5).of 790.899
    (expect title_text[:font_size].round).to eql 10
  end

  it 'should allow theme to set background color on caption with outside margin that follows other text' do
    pdf_theme = {
      code_caption_font_color: 'ffffff',
      code_caption_font_style: 'bold',
      code_caption_background_color: 'AA0000',
      code_caption_margin_outside: 10,
      code_background_color: 'transparent',
      code_border_radius: 0,
    }

    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    before

    .Caption with background color
    ----
    content
    ----
    EOS

    title_text = pdf.find_unique_text 'Caption with background color'
    (expect title_text[:font_color]).to eql 'FFFFFF'
    (expect title_text[:font_name]).to eql 'NotoSerif-Bold'
    (expect pdf.pages[0][:raw_content]).to include %(\n0.66667 0.0 0.0 scn\n48.24 753.119 498.8 14.991 re)
    (expect title_text[:y]).to be > 753.119
    (expect title_text[:y]).to (be_within 5).of 753.119
    (expect title_text[:font_size].round).to eql 10
  end

  it 'should apply text transform when computing height of background on caption' do
    pdf_theme = {
      code_caption_font_color: 'ffffff',
      code_caption_font_style: 'normal',
      code_caption_background_color: '3399FF',
      code_caption_text_transform: 'uppercase',
      code_caption_margin_outside: 10,
    }

    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    .Caption with background color that spans multiple lines because of the text transform
    ----
    content
    ----
    EOS

    title_text = pdf.find_unique_text %r/^CAPTION WITH BACKGROUND COLOR/
    (expect title_text[:font_color]).to eql 'FFFFFF'
    (expect title_text[:font_name]).to eql 'NotoSerif'
    (expect pdf.pages[0][:raw_content]).to include %(/DeviceRGB cs\n0.2 0.6 1.0 scn\n48.24 775.908 498.8 29.982 re)
    (expect title_text[:y]).to be > 790.899
    (expect title_text[:y]).to (be_within 5).of 790.899
    (expect title_text[:font_size].round).to eql 10
  end

  it 'should apply text formatting when computing height of background on caption' do
    pdf_theme = {
      code_caption_font_color: 'ffffff',
      code_caption_font_style: 'normal',
      code_caption_background_color: '3399FF',
      code_caption_margin_outside: 10,
    }

    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    .Caption with background color that contains _inline formatting_ but does not wrap
    ----
    content
    ----
    EOS

    title_text = pdf.find_unique_text %r/^Caption with background color/
    (expect title_text[:font_color]).to eql 'FFFFFF'
    (expect title_text[:font_name]).to eql 'NotoSerif'
    (expect (pdf.find_unique_text 'inline formatting')[:font_name]).to eql 'NotoSerif-Italic'
    (expect pdf.pages[0][:raw_content]).to include %(\n0.2 0.6 1.0 scn\n48.24 790.899 498.8 14.991 re)
    (expect title_text[:y]).to be > 790.899
    (expect title_text[:y]).to (be_within 5).of 790.899
    (expect title_text[:font_size].round).to eql 10
  end

  it 'should allow theme to place caption below block' do
    pdf_theme = { code_caption_end: 'bottom' }

    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    .Look out below!
    ----
    code
    ----
    EOS

    content_text = pdf.find_unique_text 'code'
    title_text = pdf.find_unique_text 'Look out below!'
    (expect title_text[:y]).to be < content_text[:y]
  end

  it 'should apply inline formatting if quotes subs is enabled' do
    pdf = to_pdf <<~'EOS', analyze: true
    [subs=+quotes]
    ----
    _1_ skipped
    *99* passing
    ----
    EOS

    italic_text = (pdf.find_text '1')[0]
    (expect italic_text[:font_name]).to eql 'mplus1mn-italic'
    bold_text = (pdf.find_text '99')[0]
    (expect bold_text[:font_name]).to eql 'mplus1mn-bold'
  end

  it 'should honor font family set on conum category in theme for conum in listing block' do
    pdf = to_pdf <<~'EOS', pdf_theme: { code_font_family: 'Courier' }, analyze: true
    ----
    fe <1>
    fi <2>
    fo <3>
    ----
    EOS

    lines = pdf.lines
    (expect lines[0]).to end_with ' ①'
    (expect lines[1]).to end_with ' ②'
    (expect lines[2]).to end_with ' ③'
    conum_text = (pdf.find_text '①')[0]
    (expect conum_text[:font_name]).not_to eql 'Courier'
  end

  it 'should allow theme to set conum color using CMYK value' do
    cmyk_color = [0, 100, 100, 60].extend Asciidoctor::PDF::ThemeLoader::CMYKColorValue
    pdf = to_pdf <<~'EOS', pdf_theme: { conum_font_color: cmyk_color }, analyze: true
    ----
    foo <1>
    ----
    <1> the counterpart of bar
    EOS

    conum_texts = pdf.find_text '①'
    (expect conum_texts).to have_size 2
    # NOTE: yes, the hex color is all weird here; could be a parser issue
    (expect conum_texts[0][:font_color]).to eql cmyk_color.map(&:to_f)
    (expect conum_texts[1][:font_color]).to eql cmyk_color.map(&:to_f)
  end

  it 'should allow width of border to be set only on ends' do
    pdf_theme = {
      code_border_color: 'AA0000',
      code_border_width: [1, nil],
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: :line
    ----
    foo
    bar
    baz
    ----
    EOS

    lines = pdf.lines
    (expect lines).to have_size 2
    (expect lines[0][:from][:y]).to eql lines[0][:to][:y]
    (expect lines[1][:from][:y]).to eql lines[1][:to][:y]
  end

  it 'should allow width of border to be set only on sides' do
    pdf_theme = {
      code_border_color: 'AA0000',
      code_border_width: [nil, 1],
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: :line
    ----
    foo
    bar
    baz
    ----
    EOS

    lines = pdf.lines
    (expect lines).to have_size 2
    (expect lines[0][:from][:x]).to eql lines[0][:to][:x]
    (expect lines[1][:from][:x]).to eql lines[1][:to][:x]
  end

  it 'should allow width of border on ends and sides to be different' do
    pdf_theme = {
      code_border_color: 'AA0000',
      code_border_width: [2, 1],
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: :line
    ----
    foo
    bar
    baz
    ----
    EOS

    lines = pdf.lines
    (expect lines).to have_size 4
    (expect lines[0][:from][:y]).to eql lines[0][:to][:y]
    (expect lines[0][:width]).to eql 2
    (expect lines[1][:from][:x]).to eql lines[1][:to][:x]
    (expect lines[1][:width]).to eql 1
  end

  it 'should allow width of border to be only set on one end' do
    pdf_theme = {
      code_border_color: 'AA0000',
      code_border_width: [1, 0, 0, 0],
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: :line
    ----
    foo
    bar
    baz
    ----
    EOS

    lines = pdf.lines
    (expect lines).to have_size 1
    (expect lines[0][:from][:y]).to eql lines[0][:to][:y]
    (expect lines[0][:width]).to eql 1
  end

  it 'should allow max width of border with different ends and sides to be less than 1' do
    pdf_theme = {
      code_border_color: 'AA0000',
      code_border_width: [0.5, 0],
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: :line
    ----
    foo
    bar
    baz
    ----
    EOS

    lines = pdf.lines
    (expect lines).to have_size 2
    (expect lines[0][:from][:y]).to eql lines[0][:to][:y]
    (expect lines[0][:width]).to eql 0.5
    (expect lines[1][:from][:y]).to eql lines[1][:to][:y]
    (expect lines[1][:width]).to eql 0.5
  end

  it 'should use dashed border to indicate where block is split across a page boundary when border is only on ends', visual: true do
    pdf_theme = {
      code_border_color: 'AA0000',
      code_border_width: [1, 0],
    }

    to_file = to_pdf_file <<~EOS, 'listing-page-split-border-ends.pdf', pdf_theme: pdf_theme
    ----
    #{(['listing'] * 60).join ?\n}
    ----
    EOS

    (expect to_file).to visually_match 'listing-page-split-border-ends.pdf'
  end

  it 'should allow theme to set different padding per edge' do
    pdf_theme = {
      code_border_radius: 0,
      code_padding: [5, 10, 15, 20],
      code_background_color: nil,
    }

    input = <<~EOS
    ----
    downloading#{(%w(.) * 100).join}done
    ----
    EOS

    text = (to_pdf input, pdf_theme: pdf_theme, analyze: true).text
    lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines

    left = lines[0][:from][:x]
    top = lines[0][:to][:y]
    bottom = lines[1][:to][:y]
    (expect text[0][:x]).to eql (left + 20.0).round 2
    (expect text[0][:y] + text[0][:font_size]).to be_within(1).of(top - 5)
    (expect text[1][:y]).to be_within(5).of(bottom + 15)
  end

  it 'should allow theme to set different padding for ends and sides' do
    pdf_theme = {
      code_border_radius: 0,
      code_padding: [10, 5],
      code_background_color: nil,
    }

    input = <<~EOS
    ----
    source code here
    ----
    EOS

    text = (to_pdf input, pdf_theme: pdf_theme, analyze: true).text
    lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines

    left = lines[0][:from][:x]
    top = lines[0][:to][:y]
    bottom = lines[1][:to][:y]
    (expect text[0][:x]).to eql (left + 5.0).round 2
    (expect text[0][:y] + text[0][:font_size]).to be_within(1).of(top - 10)
    (expect text[0][:y]).to be_within(3).of(bottom + 10)
  end

  it 'should allow theme to set 3-value padding that contains a nil value' do
    pdf_theme = {
      code_border_radius: 0,
      code_padding: [10, nil, 5],
      code_background_color: nil,
      code_border_width: [1, 0],
    }

    input = <<~EOS
    ----
    source code here
    ----
    EOS

    lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines
    text = (to_pdf input, pdf_theme: pdf_theme, analyze: true).text

    (expect lines).to have_size 2
    (expect text).to have_size 1
    (expect lines[0][:from][:x]).to eql 48.24
    (expect text[0][:x]).to eql 48.24
  end

  it 'should not substitute conums if callouts sub is absent' do
    pdf = to_pdf <<~'EOS', analyze: true
    [subs=-callouts]
    ----
    not a conum <1>
    ----
    EOS

    (expect pdf.lines).to include 'not a conum <1>'
    (expect pdf.find_text '①').to be_empty
  end
end