summaryrefslogtreecommitdiff
path: root/spec/admonition_spec.rb
blob: 1e34c5817739a141bc61ffdb1f3732e4b5607df8 (plain) (blame)
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# frozen_string_literal: true

require_relative 'spec_helper'

describe 'Asciidoctor::PDF::Converter - Admonition' do
  it 'should advance block to next page to avoid splitting it if it will fit on page' do
    pdf = to_pdf <<~EOS, analyze: true
    #{(['paragraph'] * 20).join %(\n\n)}

    [NOTE]
    ====
    #{(['admonition'] * 20).join %(\n\n)}
    ====
    EOS

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

  it 'should vertically center label on first page if block is split across pages' do
    pdf = to_pdf <<~EOS, pdf_theme: { page_margin: '0.5in' }, analyze: true
    [NOTE]
    ====
    #{(['admonition'] * 40).join %(\n\n)}
    ====
    EOS

    (expect pdf.pages).to have_size 2
    page_height = (get_page_size pdf)[1]
    label_text = (pdf.find_text 'NOTE')[0]
    label_text_midpoint = label_text[:y] + (label_text[:font_size] * 0.5)
    (expect label_text_midpoint).to be_within(2).of(page_height * 0.5)
  end

  it 'should draw vertical rule on all pages if block is split across pages' do
    pdf = to_pdf <<~EOS, pdf_theme: { page_margin: '0.5in' }, analyze: :line
    [NOTE]
    ====
    #{(['admonition'] * 40).join %(\n\n)}
    ====
    EOS

    lines = pdf.lines
    (expect lines).to have_size 2
    (expect lines.map {|it| it[:page_number] }).to eql [1, 2]
    (expect lines[0][:to][:y]).to eql 36.0
    (expect lines[1][:to][:y]).to be > 36.0
  end

  it 'should draw border and background on all pages if block is split across pages', visual: true do
    pdf_theme = {
      admonition_background_color: 'F5A9A9',
      admonition_border_width: 0.5,
      admonition_border_color: '333333',
      admonition_rule_color: 'FFFFFF',
    }

    to_file = to_pdf_file <<~EOS, 'admonition-page-split.pdf', pdf_theme: pdf_theme
    before

    [NOTE]
    ====
    #{(['admonition'] * 40).join %(\n\n)}
    ====

    after
    EOS

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

  it 'should allow theme to configure properties of caption' do
    pdf_theme = {
      admonition_caption_font_color: '00AA00',
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    .Admonition title
    [NOTE]
    ====
    There's something you should know.
    ====
    EOS

    title_text = (pdf.find_text 'Admonition title')[0]
    (expect title_text[:font_color]).to eql '00AA00'
  end

  it 'should use value of caption attribute as label' do
    pdf = to_pdf <<~'EOS', analyze: true
    [NOTE,caption=Pro Tip]
    Use bundler!
    EOS

    label_text = pdf.text[0]
    (expect label_text[:font_name]).to eql 'NotoSerif-Bold'
    (expect label_text[:string]).to eql 'PRO TIP'
  end

  it 'should not transform label text if admonition_label_text_transform key is nil, none, or invalid' do
    [nil, 'none', 'invalid'].each do |transform|
      pdf = to_pdf <<~'EOS', pdf_theme: { admonition_label_text_transform: transform }, analyze: true
      [NOTE,caption=Pro Tip]
      Use bundler!
      EOS

      label_text = pdf.text[0]
      (expect label_text[:font_name]).to eql 'NotoSerif-Bold'
      (expect label_text[:string]).to eql 'Pro Tip'
    end
  end

  # TODO: this could use a deeper assertion
  it 'should compute width of label even when glyph is missing' do
    pdf = to_pdf <<~'EOS', analyze: true
    [TIP,caption=⏻ Tip]
    Use bundler!
    EOS

    label_text = pdf.text[0]
    (expect label_text[:font_name]).to eql 'NotoSerif-Bold'
    (expect label_text[:string]).to eql '⏻ TIP'
  end

  it 'should resize label text to fit if it overflows available space' do
    pdf = to_pdf <<~'EOS', pdf_theme: { admonition_label_font_size: 18 }, analyze: true
    [IMPORTANT]
    Make sure the device is powered off before servicing it.
    EOS

    label_text = pdf.find_unique_text 'IMPORTANT'
    (expect label_text).not_to be_nil
    (expect label_text[:font_size]).to be < 18
  end

  it 'should allow padding to be specified for label and content using single value' do
    input = <<~'EOS'
    [IMPORTANT]
    Make sure the device is powered off before servicing it.
    EOS

    pdf = to_pdf input, pdf_theme: { admonition_padding: 0, admonition_label_padding: 0 }, analyze: true
    ref_label_text = pdf.find_unique_text 'IMPORTANT'
    ref_content_text = pdf.find_unique_text 'Make sure the device is powered off before servicing it.'

    pdf = to_pdf input, pdf_theme: { admonition_padding: 10, admonition_label_padding: 10 }, analyze: true
    label_text = pdf.find_unique_text 'IMPORTANT'
    content_text = pdf.find_unique_text 'Make sure the device is powered off before servicing it.'

    (expect label_text[:x] - ref_label_text[:x]).to eql 10.0
    (expect content_text[:x] - ref_content_text[:x]).to eql 30.0
  end

  it 'should not move cursor below block if block ends at top of page' do
    pdf = to_pdf <<~'EOS', analyze: true
    top of page

    [NOTE]
    ====
    something to remember

    <<<
    ====

    top of page
    EOS

    top_of_page_texts = pdf.find_text 'top of page'
    (expect top_of_page_texts).to have_size 2
    (expect top_of_page_texts[0][:y]).to eql top_of_page_texts[0][:y]
  end

  it 'should pad content box based on prose_margin_bottom value' do
    input = <<~'EOS'
    NOTE: The prose_margin_bottom value controls the padding around the content box.
    EOS

    pdf = to_pdf input, pdf_theme: { prose_margin_bottom: 12 }, analyze: :line
    reference_line = pdf.lines[0]
    reference_text = (to_pdf input, pdf_theme: { prose_margin_bottom: 12 }, analyze: true).text[0]

    pdf = to_pdf input, pdf_theme: { prose_margin_bottom: 0 }, analyze: :line
    line = pdf.lines[0]
    text = (to_pdf input, pdf_theme: { prose_margin_bottom: 0 }, analyze: true).text[0]

    (expect line[:from][:y]).to eql reference_line[:from][:y]
    (expect line[:to][:y]).to eql (reference_line[:to][:y] + 8)
    # NOTE: verify text moves up by half the change in line size
    (expect text[:y]).to eql (reference_text[:y] + 4)
  end

  context 'Text' do
    it 'should show admonition label in bold by default' do
      pdf = to_pdf <<~'EOS', analyze: true
      TIP: Look for the warp zone under the bridge.
      EOS

      lines = pdf.lines
      (expect lines).to have_size 1
      (expect lines[0]).to eql 'TIP Look for the warp zone under the bridge.'
      text = pdf.text
      (expect text).to have_size 2
      label_text = text[0]
      (expect label_text[:string]).to eql 'TIP'
      (expect label_text[:font_name]).to eql 'NotoSerif-Bold'
      content_text = text[1]
      (expect content_text[:string]).to eql 'Look for the warp zone under the bridge.'
    end

    it 'should allow the theme to specify a minimum width for the text-based label' do
      pdf_theme = {
        admonition_label_min_width: '75',
        admonition_label_align: 'right',
      }

      pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
      NOTE: Remember the milk.

      TIP: Look for the warp zone under the bridge.

      CAUTION: Slippery when wet.

      WARNING: Beware of dog!

      IMPORTANT: Sign off before stepping away from the computer.
      EOS

      label_texts = pdf.find_text font_name: 'NotoSerif-Bold'
      label_right_reference = nil
      label_texts.each do |it|
        label_right_reference ||= it[:x] + it[:width]
        (expect it[:x] + it[:width]).to be_within(1.5).of(label_right_reference)
      end

      content_texts = pdf.find_text font_name: 'NotoSerif'
      (expect content_texts.map {|it| it[:x] }.uniq).to eql [content_texts[0][:x]]
    end

    it 'should allow theme to control vertical alignment of label' do
      pdf = to_pdf <<~'EOS', pdf_theme: { admonition_label_vertical_align: 'top' }, analyze: true
      [NOTE]
      ====
      There are lots of things you need to know.
      Then there are the things that you already know.
      And those things that you don't know that you do not know.
      This documentation seeks to close the gaps between them.
      ====
      EOS

      label_text = (pdf.find_text 'NOTE')[0]
      content_text = (pdf.find_text font_name: 'NotoSerif')[0]
      (expect label_text[:y]).to be > content_text[:y]
    end

    it 'should resolve character references in label' do
      pdf = to_pdf <<~'EOS', pdf_theme: { admonition_label_font_color: '000000' }, analyze: true
      [NOTE,caption=&#174;]
      ====
      Christoph and sons.
      ====
      EOS

      label_text = (pdf.find_text font_color: '000000')[0]
      (expect label_text[:string]).to eql ?\u00ae
      (expect label_text[:width]).to be < label_text[:font_size]
      (expect label_text[:font_name]).to eql 'NotoSerif-Bold'
    end
  end

  context 'Icon' do
    it 'should show font-based icon in place of label when icons=font' do
      pdf = to_pdf <<~'EOS', analyze: true
      :icons: font

      TIP: Look for the warp zone under the bridge.
      EOS

      lines = pdf.lines
      (expect lines).to have_size 1
      (expect lines[0]).to eql %(\uf0eb Look for the warp zone under the bridge.)
      text = pdf.text
      (expect text).to have_size 2
      label_text = text[0]
      (expect label_text[:string]).to eql ?\uf0eb
      (expect label_text[:font_name]).to eql 'FontAwesome5Free-Regular'
      # NOTE: font size is reduced to fit within available space
      (expect label_text[:font_size]).to be < 24
      content_text = text[1]
      (expect content_text[:string]).to eql 'Look for the warp zone under the bridge.'
    end

    it 'should not reduce font size of icon if specified size fits within available space' do
      pdf = to_pdf <<~'EOS', pdf_theme: { admonition_icon_important: { size: 50 } }, analyze: true
      :icons: font

      [IMPORTANT]
      ====
      Always do this.

      And when you do that, always do this too!
      ====
      EOS

      label_text = pdf.find_unique_text ?\uf06a
      (expect label_text[:font_size]).to eql 50
    end

    it 'should allow the theme to specify a minimum width for the font-based icon label' do
      pdf_theme = {
        admonition_label_min_width: '75',
        admonition_label_align: 'right',
      }

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

      NOTE: Remember the milk.

      TIP: Look for the warp zone under the bridge.

      CAUTION: Slippery when wet.

      WARNING: Beware of dog!

      IMPORTANT: Sign off before stepping away from the computer.
      EOS

      label_texts = pdf.text.select {|it| it[:font_name].start_with? 'FontAwesome' }
      (expect label_texts[0][:x]).to be > 100
      label_right_reference = nil
      label_texts.each do |it|
        label_right_reference ||= it[:x] + it[:width]
        (expect it[:x] + it[:width]).to be_within(1.5).of(label_right_reference)
      end

      content_texts = pdf.find_text font_name: 'NotoSerif'
      (expect content_texts.map {|it| it[:x] }.uniq).to eql [content_texts[0][:x]]
    end

    it 'should allow theme to control vertical alignment of icon' do
      pdf = to_pdf <<~'EOS', pdf_theme: { admonition_label_vertical_align: 'top' }, analyze: true
      :icons: font

      [NOTE]
      ====
      There are lots of things you need to know.
      Then there are the things that you already know.
      And those things that you don't know that you do not know.
      This documentation seeks to close the gaps between them.
      ====
      EOS

      icon_text = (pdf.find_text ?\uf05a)[0]
      content_text = (pdf.find_text font_color: '333333')[1]
      (expect icon_text[:y]).to be > content_text[:y]
    end

    it 'should assume icon name with no icon set prefix is a legacy FontAwesome icon name' do
      pdf = to_pdf <<~'EOS', pdf_theme: { admonition_icon_tip: { name: 'smile-wink' } }, analyze: true
      :icons: font

      TIP: Time to upgrade your icon set.
      EOS

      icon_text = pdf.text[0]
      (expect icon_text[:font_name]).to eql 'FontAwesome5Free-Solid'
      (expect icon_text[:string]).to eql ?\uf4da
    end

    it 'should be able to use fa- prefix to reference icon in legacy FontAwesome set' do
      (expect do
        pdf = to_pdf <<~'EOS', pdf_theme: { admonition_icon_tip: { name: 'fa-smile-wink' } }, analyze: true
        :icons: font

        TIP: Time to upgrade your icon set.
        EOS

        icon_text = pdf.text[0]
        (expect icon_text[:font_name]).to eql 'FontAwesome5Free-Solid'
        (expect icon_text[:string]).to eql ?\uf4da
      end).to log_message severity: :INFO, message: 'tip admonition in theme uses icon from deprecated fa icon set; use fas, far, or fab instead', using_log_level: :INFO
    end

    it 'should allow icon to come from Foundation icon set' do
      pdf = to_pdf <<~'EOS', pdf_theme: { admonition_icon_warning: { name: 'fi-alert' } }, analyze: true
      :icons: font

      WARNING: Just don't do it.
      EOS

      icon_text = pdf.text[0]
      (expect icon_text[:font_name]).to eql 'fontcustom'
      (expect icon_text[:string]).to eql ?\uf101
    end

    it 'should fall back to note icon if icon name cannot be resolved' do
      pdf = to_pdf <<~'EOS', pdf_theme: { admonition_icon_warning: { name: nil } }, analyze: true
      :icons: font

      WARNING: If the icon name is nil, the default note icon will be used.
      EOS

      icon_text = pdf.text[0]
      (expect icon_text[:font_name]).to eql 'FontAwesome5Free-Solid'
      (expect icon_text[:string]).to eql ?\uf05a
    end

    it 'should set color of icon to value of stroke_color key specified in theme' do
      pdf = to_pdf <<~'EOS', pdf_theme: { admonition_icon_note: { stroke_color: '00ff00' } }, analyze: true
      :icons: font

      NOTE: This icon is green.
      EOS

      icon_text = (pdf.find_text ?\uf05a)[0]
      (expect icon_text[:font_color]).to eql '00FF00'
    end

    it 'should use icon glyph specified in theme' do
      pdf = to_pdf <<~'EOS', pdf_theme: { admonition_icon_tip: { name: 'far-money-bill-alt' } }, analyze: true
      :icons: font

      TIP: Look for the warp zone under the bridge.
      EOS

      lines = pdf.lines
      (expect lines).to have_size 1
      (expect lines[0]).to eql %(\uf3d1 Look for the warp zone under the bridge.)
      text = pdf.text
      (expect text).to have_size 2
      label_text = text[0]
      (expect label_text[:string]).to eql ?\uf3d1
      (expect label_text[:font_name]).to eql 'FontAwesome5Free-Regular'
      content_text = text[1]
      (expect content_text[:string]).to eql 'Look for the warp zone under the bridge.'
    end

    it 'should use SVG icon specified by icon attribute when icons attribute is set', visual: true do
      to_file = to_pdf_file <<~'EOS', 'admonition-custom-svg-icon.pdf', attribute_overrides: { 'docdir' => fixtures_dir }
      :icons: font
      :iconsdir:
      :icontype: svg

      [TIP,icon=square]
      ====
      Use the icon attribute to customize the image for an admonition block.
      ====
      EOS

      (expect to_file).to visually_match 'admonition-custom-svg-icon.pdf'
    end

    it 'should warn if SVG icon specified by icon attribute has errors' do
      (expect do
        pdf = to_pdf <<~'EOS', attribute_overrides: { 'iconsdir' => fixtures_dir }, analyze: :rect
        :icons: font
        :icontype: svg

        [TIP,icon=faulty]
        ====
        Use the icon attribute to customize the image for an admonition block.
        ====
        EOS
        (expect pdf.rectangles).to have_size 1
        # NOTE: width and height of rectangle match what's defined in SVG
        (expect pdf.rectangles[0][:width]).to eql 200.0
        (expect pdf.rectangles[0][:height]).to eql 200.0
      end).to log_message severity: :WARN, message: %(~problem encountered in image: #{fixture_file 'faulty.svg'}; Unknown tag 'foobar')
    end

    it 'should warn and fall back to admonition label if SVG icon cannot be found' do
      (expect do
        pdf = to_pdf <<~'EOS', attribute_overrides: { 'iconsdir' => fixtures_dir }, analyze: true
        :icons:
        :icontype: svg

        [WARNING]
        ====
        The admonition label will be used if the image cannot be resolved.
        ====
        EOS
        label_text = pdf.find_unique_text 'WARNING'
        (expect label_text).not_to be_nil
        (expect label_text[:font_name]).to include 'Bold'
      end).to log_message severity: :WARN, message: %(admonition icon not found or not readable: #{fixture_file 'warning.svg'})
    end

    it 'should warn and fall back to admonition label if SVG icon specified by icon attribute cannot be embedded' do
      (expect do
        pdf = to_pdf <<~'EOS', attribute_overrides: { 'iconsdir' => fixtures_dir }, analyze: true
        :icons: font
        :icontype: svg

        [TIP,icon=broken]
        ====
        Use the icon attribute to customize the image for an admonition block.
        ====
        EOS
        label_text = pdf.find_unique_text 'TIP'
        (expect label_text).not_to be_nil
        (expect label_text[:font_name]).to include 'Bold'
      end).to log_message severity: :WARN, message: %(~could not embed admonition icon: #{fixture_file 'broken.svg'}; Missing end tag for 'rect')
    end

    it 'should resize fallback admonition label to fit in available space if icon fails to embed' do
      (expect do
        pdf = to_pdf <<~'EOS', attribute_overrides: { 'iconsdir' => fixtures_dir }, analyze: true
        :icons: font
        :icontype: svg

        [WARNING,icon=broken]
        ====
        Use the icon attribute to customize the image for an admonition block.
        ====
        EOS
        label_text = pdf.find_unique_text 'WARNING'
        (expect label_text).not_to be_nil
        (expect label_text[:font_size]).to be < 10.5
      end).to log_message severity: :WARN, message: %(~could not embed admonition icon: #{fixture_file 'broken.svg'}; Missing end tag for 'rect')
    end

    it 'should warn and fall back to admonition label if raster icon cannot be found' do
      (expect do
        pdf = to_pdf <<~'EOS', attribute_overrides: { 'iconsdir' => fixtures_dir }, analyze: true
        :icons:

        [WARNING]
        ====
        The admonition label will be used if the image cannot be resolved.
        ====
        EOS
        label_text = pdf.find_unique_text 'WARNING'
        (expect label_text).not_to be_nil
        (expect label_text[:font_name]).to include 'Bold'
      end).to log_message severity: :WARN, message: %(admonition icon not found or not readable: #{fixture_file 'warning.png'})
    end

    it 'should warn and fall back to admonition label if raster icon specified by icon attribute cannot be embedded' do
      (expect do
        pdf = to_pdf <<~'EOS', attribute_overrides: { 'iconsdir' => fixtures_dir }, analyze: true
        :icons:

        [TIP,icon=corrupt.png]
        ====
        Use the icon attribute to customize the image for an admonition block.
        ====
        EOS
        label_text = pdf.find_unique_text 'TIP'
        (expect label_text).not_to be_nil
        (expect label_text[:font_name]).to include 'Bold'
      end).to log_message severity: :WARN, message: %(~could not embed admonition icon: #{fixture_file 'corrupt.png'}; image file is an unrecognised format)
    end

    it 'should embed remote image in icon if allow-uri-read attribute is set', visual: true do
      to_file = to_pdf_file <<~'EOS', 'admonition-custom-svg-icon-with-remote-image.pdf', attribute_overrides: { 'docdir' => fixtures_dir, 'allow-uri-read' => '' }
      :icons: font
      :iconsdir:

      [NOTE,icon=svg-with-remote-image.svg]
      ====
      AsciiDoc is awesome!
      ====
      EOS

      (expect to_file).to visually_match 'admonition-custom-svg-icon-with-remote-image.pdf'
    end

    it 'should use original width of SVG icon if height is less than height of admonition block', visual: true do
      to_file = to_pdf_file <<~'EOS', 'admonition-custom-svg-fit.pdf', attribute_overrides: { 'docdir' => fixtures_dir }, analyze: :rect
      :icons: font
      :iconsdir:

      [NOTE,icon=green-bar.svg]
      ====
      When you see this icon, it means there's additional advice about passing tests.
      ====
      EOS

      (expect to_file).to visually_match 'admonition-custom-svg-fit.pdf'
    end

    it 'should use raster icon specified by icon attribute when icons attribute is set', visual: true do
      to_file = to_pdf_file <<~'EOS', 'admonition-custom-raster-icon.pdf', attribute_overrides: { 'docdir' => fixtures_dir }
      :icons: font
      :iconsdir:

      [TIP,icon=tux.png]
      ====
      Use the icon attribute to customize the image for an admonition block.
      ====
      EOS

      (expect to_file).to visually_match 'admonition-custom-raster-icon.pdf'
    end

    # NOTE: this is a pretty flimsy feature and probably needs some rethink
    it 'should allow theme to control width of admonition icon image using admonition_label_min_width key' do
      pdf_theme = { admonition_label_min_width: 40 }
      pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, attribute_overrides: { 'docdir' => fixtures_dir }, analyze: :image
      :icons: font
      :iconsdir:

      [TIP,icon=logo.png]
      ====
      Use the icon attribute to customize the image for an admonition block.

      Use the admonition_label_min_width key to control the image width.
      ====
      EOS

      images = pdf.images
      (expect images).to have_size 1
      (expect images[0][:width]).to eql 40.0
    end

    it 'should resolve icon when icons attribute is set to image', visual: true do
      to_file = to_pdf_file <<~'EOS', 'admonition-image-icon.pdf', attribute_overrides: { 'docdir' => fixtures_dir }
      :icons: image
      :iconsdir:

      [TIP]
      ====
      Use the icon attribute to customize the image for an admonition block.
      ====
      EOS

      (expect to_file).to visually_match 'admonition-custom-raster-icon.pdf'
    end

    it 'should not unset data-uri attribute when resolving icon image if already unset', visual: true do
      doc = Asciidoctor.load <<~'EOS', backend: :pdf, safe: :safe, standalone: true, attributes: { 'docdir' => fixtures_dir, 'nofooter' => '' }
      :icons: image
      :iconsdir:

      [TIP]
      ====
      Use the icon attribute to customize the image for an admonition block.
      ====
      EOS

      (expect doc.converter).not_to be_nil
      doc.remove_attr 'data-uri'
      to_file = File.join output_dir, 'admonition-image-icon-no-data-uri.pdf'
      doc.converter.write doc.convert, to_file

      (expect to_file).to visually_match 'admonition-custom-raster-icon.pdf'
    end

    it 'should not resolve remote icon when icons attribute is set to image and allow-uri-read is not set' do
      with_local_webserver do |base_url|
        (expect do
          pdf = to_pdf <<~'EOS', attribute_overrides: { 'iconsdir' => base_url }, analyze: true
          :icons: image

          [TIP]
          ====
          Use the icon attribute to customize the image for an admonition block.
          ====
          EOS

          label_text = pdf.find_unique_text 'TIP'
          (expect label_text).not_to be_nil
          (expect pdf.lines).to eql ['TIP Use the icon attribute to customize the image for an admonition block.']
        end).to log_messages [[
          { severity: :WARN, message: %(allow-uri-read is not enabled; cannot embed remote image: #{base_url}/tip.png) },
          { severity: :WARN, message: %(admonition icon not found or not readable: #{base_url}/tip.png) },
        ]]
      end
    end

    it 'should not resolve remote icon when icons attribute is set to image, allow-uri-read is set, and image is missing' do
      with_local_webserver do |base_url|
        base_url += '/nada'
        (expect do
          pdf = to_pdf <<~'EOS', attribute_overrides: { 'allow-uri-read' => '', 'iconsdir' => base_url }, analyze: true
          :icons: image

          [TIP]
          ====
          Use the icon attribute to customize the image for an admonition block.
          ====
          EOS

          label_text = pdf.find_unique_text 'TIP'
          (expect label_text).not_to be_nil
          (expect pdf.lines).to eql ['TIP Use the icon attribute to customize the image for an admonition block.']
        end).to log_messages [[
          { severity: :WARN, message: %(could not retrieve remote image: #{base_url}/tip.png; 404 Not Found) },
          { severity: :WARN, message: %(admonition icon not found or not readable: #{base_url}/tip.png) },
        ]]
      end
    end

    it 'should resolve remote icon when icons attribute is set to image and allow-uri-read is set', visual: true do
      to_file = with_local_webserver do |base_url|
        to_pdf_file <<~'EOS', 'admonition-remote-image-icon.pdf', attribute_overrides: { 'allow-uri-read' => '', 'iconsdir' => base_url }
        :icons: image

        [TIP]
        ====
        Use the icon attribute to customize the image for an admonition block.
        ====
        EOS
      end

      (expect to_file).to visually_match 'admonition-custom-raster-icon.pdf'
    end

    it 'should resize icon only if it does not fit within the available space' do
      pdf = to_pdf <<~'EOS', attribute_overrides: { 'docdir' => fixtures_dir }, analyze: :image
      :icons: image
      :iconsdir:

      [TIP]
      This is Tux.
      He's the Linux mascot.
      EOS

      images = pdf.images
      (expect images).to have_size 1
      (expect images[0][:width]).to be < 36.0
      (expect images[0][:height]).to be < 42.3529

      pdf = to_pdf <<~'EOS', attribute_overrides: { 'docdir' => fixtures_dir }, analyze: :image
      :icons: image
      :iconsdir:

      [TIP]
      ====
      This is Tux.
      If you spend any amount of time in the Linux world, you'll see him a lot.
      He's the Linux mascot.

      Thanks to Linux, penguins have receive a lot more attention.
      Technology can sometimes be a force for good like that.
      ====
      EOS

      images = pdf.images
      (expect images).to have_size 1
      (expect images[0][:width]).to eql 36.0
      (expect images[0][:height]).to eql 42.3529
    end

    it 'should warn and fall back to admonition label if image icon cannot be resolved' do
      (expect do
        pdf = to_pdf <<~'EOS', attribute_overrides: { 'docdir' => fixtures_dir }, analyze: true
        :icons: image
        :iconsdir:

        [NOTE]
        ====
        Use the icon attribute to customize the image for an admonition block.
        ====
        EOS

        note_text = pdf.find_unique_text 'NOTE'
        (expect note_text).not_to be_nil
        (expect note_text[:font_name]).to include 'Bold'
      end).to log_message severity: :WARN, message: '~admonition icon not found or not readable'
    end

    it 'should allow theme to specify icon for custom admonition type' do
      require 'asciidoctor/extensions'

      extensions = proc do
        block :QUESTION do
          on_context :example
          process do |parent, reader, attrs|
            attrs['name'] = 'question'
            attrs['caption'] = 'Question'
            create_block parent, :admonition, reader.lines, attrs, content_model: :compound
          end
        end
      end

      pdf_theme = {
        admonition_icon_question: { name: 'question-circle' },
      }

      pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, extensions: extensions, analyze: true
      :icons: font

      [QUESTION]
      ====
      Are you following along?

      Just checking ;)
      ====
      EOS

      icon_text = pdf.find_unique_text ?\uf059
      (expect icon_text).not_to be_nil
      (expect icon_text[:font_name]).to eql 'FontAwesome5Free-Solid'
      (expect icon_text[:font_size]).to be 24
      (expect pdf.find_unique_text 'Are you following along?').not_to be_nil
      (expect pdf.find_unique_text 'Just checking ;)').not_to be_nil
    end

    it 'should use note icon for custom admonition type if theme does not specify icon name' do
      require 'asciidoctor/extensions'

      extensions = proc do
        block :FACT do
          on_context :example
          process do |parent, reader, attrs|
            attrs['name'] = 'fact'
            attrs['caption'] = 'Fact'
            create_block parent, :admonition, reader.lines, attrs, content_model: :compound
          end
        end
      end

      pdf = to_pdf <<~'EOS', extensions: extensions, analyze: true
      :icons: font

      [FACT]
      ====
      Like all planetary bodies, the Earth is spherical.
      ====
      EOS

      (expect pdf.lines).to eql [%(\uf05a Like all planetary bodies, the Earth is spherical.)]
      icon_text = pdf.find_unique_text ?\uf05a
      (expect icon_text[:font_color]).to eql '333333'
    end
  end

  context 'Background & Lines' do
    it 'should allow theme to customize color, width, and style of column rule' do
      %w(dotted dashed).each do |style|
        pdf_theme = {
          admonition_column_rule_color: '222222',
          admonition_column_rule_width: 1,
          admonition_column_rule_style: style,
        }
        pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: :line
        TIP: You can use the theme to customize the color and width of the column rule.
        EOS

        lines = pdf.lines
        (expect lines).to have_size 1
        column_rule = lines[0]
        (expect column_rule[:from][:x]).to eql column_rule[:to][:x]
        (expect column_rule[:color]).to eql '222222'
        (expect column_rule[:width]).to eql 1
        (expect column_rule[:style]).to eql style.to_sym
      end
    end

    it 'should allow theme to specify double style for column rule' do
      pdf_theme = {
        admonition_column_rule_color: '222222',
        admonition_column_rule_width: 1,
        admonition_column_rule_style: 'double',
      }
      pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: :line
      TIP: You can use the theme to customize the color and width of the column rule.
      EOS

      lines = pdf.lines
      (expect lines).to have_size 2
      column_rule1 = lines[0]
      (expect column_rule1[:from][:x]).to eql column_rule1[:to][:x]
      (expect column_rule1[:color]).to eql '222222'
      (expect column_rule1[:width]).to eql 1
      (expect column_rule1[:style]).to eql :solid
      column_rule2 = lines[1]
      (expect column_rule2[:from][:x]).to eql column_rule2[:to][:x]
      (expect column_rule2[:color]).to eql '222222'
      (expect column_rule2[:width]).to eql 1
      (expect column_rule2[:style]).to eql :solid
      (expect column_rule2[:from][:x] - column_rule1[:from][:x]).to eql 2.0
    end

    it 'should use base border width for column rule if column rule width is nil' do
      pdf_theme = {
        base_border_width: 2,
        admonition_column_rule_color: '222222',
        admonition_column_rule_width: nil,
      }
      pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: :line
      TIP: You can use the theme to customize the color and width of the column rule.
      EOS

      lines = pdf.lines
      (expect lines).to have_size 1
      column_rule = lines[0]
      (expect column_rule[:from][:x]).to eql column_rule[:to][:x]
      (expect column_rule[:color]).to eql '222222'
      (expect column_rule[:width]).to eql 2
    end

    it 'should allow theme to add border', visual: true do
      pdf_theme = {
        admonition_border_width: 0.5,
        admonition_border_radius: 5,
        admonition_border_color: 'e0e0e0',
        admonition_column_rule_color: 'e0e0e0',
      }
      to_file = to_pdf_file <<~'EOS', 'admonition-border.pdf', pdf_theme: pdf_theme
      TIP: You can use the theme to add a border.
      EOS

      (expect to_file).to visually_match 'admonition-border.pdf'
    end

    it 'should allow theme to add background color', visual: true do
      pdf_theme = {
        admonition_background_color: 'eeeeee',
        admonition_border_radius: 3,
        admonition_column_rule_width: 0,
      }
      to_file = to_pdf_file <<~'EOS', 'admonition-background-color.pdf', pdf_theme: pdf_theme
      TIP: You can use the theme to add a background color.
      EOS

      (expect to_file).to visually_match 'admonition-background-color.pdf'
    end

    it 'should allow theme to disable column rule by setting color to nil' do
      pdf_theme = { admonition_column_rule_color: nil }
      pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: :line
      TIP: You can use the theme to add a background color.
      EOS

      (expect pdf.lines).to be_empty
    end
  end
end