summaryrefslogtreecommitdiff
path: root/spec/index_spec.rb
blob: d30d9f1ed3ac76b7a812a9de1fcae360b8946b74 (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
# frozen_string_literal: true

require_relative 'spec_helper'

describe 'Asciidoctor::PDF::Converter - Index' do
  it 'should not add index entries to the document if an index section is not present' do
    pdf = to_pdf <<~'EOS', analyze: true
    You can add a ((visible index entry)) to your document by enclosing it in double round brackets.
    EOS

    (expect pdf.find_text %r/visible index entry/).to have_size 1
    (expect pdf.lines).to eql ['You can add a visible index entry to your document by enclosing it in double round brackets.']
  end

  it 'should collapse space in front of hidden index terms' do
    pdf = to_pdf <<~'EOS', analyze: true
    before
    (((yin)))
    (((yang)))
    (((foobar)))
    after
    EOS

    (expect pdf.lines).to eql ['before after']
  end

  it 'should not add index section if there are no index entries' do
    pdf = to_pdf <<~'EOS', analyze: true
    == About

    This document has no index entries.

    [index]
    == Index
    EOS

    (expect pdf.pages).to have_size 1
    (expect pdf.find_text 'Index').to be_empty
  end

  it 'should not include index section in TOC if index is empty' do
    pdf = to_pdf <<~'EOS'
    = Document Title
    :doctype: book
    :toc:

    == Chapter

    content

    [index]
    == Index
    EOS

    (expect (pdf.page 2).text).not_to include 'Index'
  end

  it 'should add the index entries to the section with the index style' do
    pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
    = Document Title

    == Chapter About Cats

    We know that ((cats)) control the internet.
    But they sort of run nature too.
    (((cats,big cats,lion)))
    After all, the ((king of the jungle)) is the lion, which is a big cat.

    == Chapter About Dogs

    Cats may rule, well, everything.
    But ((dogs)) are a human's best friend.

    [index]
    == Index
    EOS

    index_text = pdf.find_text 'Index', page_number: 4, font_size: 22
    (expect index_text).to have_size 1
    category_c_text = pdf.find_text 'C', page_number: 4
    (expect category_c_text).to have_size 1
    (expect category_c_text[0][:font_name].downcase).to include 'bold'
    category_d_text = pdf.find_text 'D', page_number: 4
    (expect category_d_text).to have_size 1
    (expect category_d_text[0][:font_name].downcase).to include 'bold'
    category_k_text = pdf.find_text 'K', page_number: 4
    (expect category_k_text).to have_size 1
    (expect category_k_text[0][:font_name].downcase).to include 'bold'
    (expect (pdf.lines pdf.find_text page_number: 4).join ?\n).to eql <<~'EOS'.chomp
    Index
    C
    cats, 1
    big cats
    lion, 1
    D
    dogs, 2
    K
    king of the jungle, 1
    EOS
  end

  it 'should add index terms to index defined in section title with autogenerated ID' do
    pdf = to_pdf <<~'EOS', analyze: true
    = Guide
    :doctype: book

    == Working with ((Custom Pipelines))

    (((custom behavior)))
    Custom pipelines allow you to add custom behavior to the application.

    [index]
    == Index
    EOS

    index_title_text = pdf.find_unique_text 'Index'
    (expect index_title_text[:page_number]).to be 3
    c_text = pdf.find_unique_text 'C'
    (expect c_text[:page_number]).to be 3
    index_lines = pdf.lines pdf.find_text page_number: 3
    (expect index_lines).to include 'Custom Pipelines, 1'
    (expect index_lines).to include 'custom behavior, 1'
  end

  it 'should not add index entries inside delimited block to index twice' do
    pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
    = Document Title

    == Chapter about Cats

    We know that ((cats)) control the internet.
    But they sort of run nature too.
    (((cats,big cats,lion)))
    After all, the ((king of the jungle)) is the lion, which is a big cat.

    .Dogs
    ****
    Cats may rule, well, everything.
    But ((dogs)) are a (((humans)))human's best friend.
    ****

    [index]
    == Index
    EOS

    index_heading_text = pdf.find_unique_text 'Index', page_number: 3, font_size: 22
    (expect index_heading_text).not_to be_nil
    index_lines = pdf.lines pdf.find_text page_number: 3
    (expect index_lines).to include 'dogs, 1'
    (expect index_lines).to include 'humans, 1'
  end

  it 'should create link from entry in index to location of term' do
    input = <<~'EOS'
    = Document Title
    :doctype: book

    == Chapter About Dogs

    Cats may rule, well, everything.
    But ((dogs)) are a human's best friend.

    [index]
    == Index
    EOS

    pdf = to_pdf input, analyze: true
    dogs_text = (pdf.find_text 'dogs are a human’s best friend.')[0]

    pdf = to_pdf input
    annotations = get_annotations pdf, 3
    (expect annotations).to have_size 1
    dest_name = annotations[0][:Dest]
    (expect (dest = get_dest pdf, dest_name)).not_to be_nil
    (expect dest[:x]).to eql dogs_text[:x]
    (expect dest[:page_number]).to be 2
  end

  it 'should target first occurance of index term, not in xreftext' do
    pdf = to_pdf <<~'EOS', analyze: true
    = Document Title
    :doctype: book
    :notitle:

    == Install

    .((node.install))
    [[node-install]]
     $ nvm install node

    == Version

    .((node.version))
    [[node-version]]
     $ node -v

    <<node-install>>

    == Uninstall

    .((node.uninstall))
    [[node-uninstall]]
     $ nvm uninstall node

    <<node-install>>

    [index]
    == Index
    EOS

    index_pgnum = (pdf.find_text 'Index')[0][:page_number]
    index_lines = pdf.lines pdf.find_text page_number: index_pgnum
    (expect index_lines).to eql ['Index', 'N', 'node.install, 1', 'node.uninstall, 3', 'node.version, 2']
  end

  it 'should not assign number or chapter label to index section' do
    pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
    = Cats & Dogs
    :sectnums:

    == Cats

    We know that ((cats)) control the internet.

    == Dogs

    Cats may rule, well, everything.
    But ((dogs)) are a human's best friend.

    [index]
    == Index
    EOS

    index_text = pdf.find_text 'Chapter 3. Index', page_number: 4
    (expect index_text).to be_empty
    index_text = pdf.find_text 'Index', page_number: 4
    (expect index_text).to have_size 1
  end

  it 'should generate anchor names for indexterms which are reproducible between runs' do
    input = <<~'EOS'
    = Cats & Dogs
    :reproducible:

    == Cats

    We know that ((cats)) control the internet.

    == Dogs

    Cats may rule, well, everything.
    But ((dogs)) are a human's best friend.

    [index]
    == Index
    EOS

    to_file_a = to_pdf_file input, 'index-reproducible-a.pdf', doctype: :book
    to_file_b = to_pdf_file input, 'index-reproducible-b.pdf', doctype: :book
    (expect FileUtils.compare_file to_file_a, to_file_b).to be true
  end

  it 'should not automatically promote nested index terms' do
    pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
    = Document Title

    == Big Cats

    (((cats,big cats)))
    (((cats,big cats,lion)))
    The king of the jungle is the lion, which is a big cat.

    [index]
    == Index
    EOS

    category_c_text = pdf.find_text 'C', page_number: 3
    (expect category_c_text).to have_size 1
    (expect category_c_text[0][:font_name].downcase).to include 'bold'
    category_b_text = pdf.find_text 'B', page_number: 3
    (expect category_b_text).to be_empty
    category_l_text = pdf.find_text 'L', page_number: 3
    (expect category_l_text).to be_empty
    (expect (pdf.lines pdf.find_text page_number: 3).join ?\n).to eql <<~'EOS'.chomp
    Index
    C
    cats
    big cats, 1
    lion, 1
    EOS
  end

  it 'should ignore empty list of terms' do
    pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
    Not worth indexing.
    indexterm:[ ]

    [index]
    == Index
    EOS

    (expect pdf.pages).to have_size 1
    (expect pdf.find_text 'Index').to be_empty
  end

  it 'should not group terms with different casing' do
    pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
    = Document Title

    ((This)) is not the same as ((this)) or ((that)).

    [index]
    == Index
    EOS

    (expect (pdf.lines pdf.find_text page_number: 3).join ?\n).to eql <<~'EOS'.chomp
    Index
    T
    that, 1
    This, 1
    this, 1
    EOS
  end

  it 'should sort capitalized terms ahead of non-capitalized terms' do
    pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
    = Document Title

    ((O.A.R.)) is a band, whereas ((oar)) is something you use to propel a boat.

    [index]
    == Index
    EOS

    (expect (pdf.lines pdf.find_text page_number: 3).join ?\n).to eql <<~'EOS'.chomp
    Index
    O
    O.A.R., 1
    oar, 1
    EOS
  end

  it 'should group index entries that start with symbol under symbol category' do
    pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
    = Document Title

    == Symbols

    Use the ((@Transactional)) annotation to mark a bean as transactional.

    Use the ((&#169;)) symbol to indicate the copyright.

    [index]
    == Index
    EOS

    (expect (pdf.lines pdf.find_text page_number: 3).join ?\n).to eql <<~'EOS'.chomp
    Index
    @
    @Transactional, 1
    ©, 1
    EOS
  end

  it 'should not put letters outside of ASCII charset in symbol category' do
    pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
    = Document Title

    == Unicode Party

    ((étudier)) means to study.

    Use a ((λ)) to define a lambda function.

    [index]
    == Index
    EOS

    (expect (pdf.lines pdf.find_text page_number: 3).join ?\n).to eql <<~'EOS'.chomp
    Index
    É
    étudier, 1
    Λ
    λ, 1
    EOS
  end

  it 'should sort terms in index, ignoring case' do
    pdf = to_pdf <<~'EOS', analyze: true
    = Document Title
    :doctype: book

    == Chapter A

    ((AsciiDoc)) is a lightweight markup language.
    It is used for content ((authoring)).

    == Chapter B

    ((Asciidoctor)) is an AsciiDoc processor.

    == Chapter C

    If an element has an ((anchor)), you can link to it.

    [index]
    == Index
    EOS

    index_pagenum = (pdf.find_text 'Index')[0][:page_number]
    index_page_lines = pdf.lines pdf.find_text page_number: index_pagenum
    terms = index_page_lines.select {|it| it.include? ',' }.map {|it| (it.split ',', 2)[0] }
    (expect terms).to eql %w(anchor AsciiDoc Asciidoctor authoring)
  end

  it 'should start with no categories' do
    index = Asciidoctor::PDF::IndexCatalog.new
    (expect index).to be_empty
  end

  it 'should initiate category with no terms' do
    index = Asciidoctor::PDF::IndexCatalog.new
    index.init_category 'C'
    (expect (index.find_category 'C').terms).to be_empty
  end

  it 'should sort arabic page numbers in index term numerically' do
    index = Asciidoctor::PDF::IndexCatalog.new
    [11, 10, 100].each do |pgnum|
      index.store_primary_term 'monkey', { anchor: (anchor_name = index.next_anchor_name) }
      index.link_dest_to_page anchor_name, pgnum
    end
    monkey_term = index.categories[0].terms[0]
    (expect monkey_term.dests.map {|it| it[:page] }).to eql %w(10 11 100)
  end

  it 'should sort roman page numbers in index term numerically' do
    index = Asciidoctor::PDF::IndexCatalog.new
    index.start_page_number = 101
    [11, 10, 100].each do |pgnum|
      index.store_primary_term 'monkey', { anchor: (anchor_name = index.next_anchor_name) }
      index.link_dest_to_page anchor_name, pgnum
    end
    monkey_term = index.categories[0].terms[0]
    (expect monkey_term.dests.map {|it| it[:page] }).to eql %w(x xi c)
  end

  it 'should sort mixed page numbers in index term numerically' do
    index = Asciidoctor::PDF::IndexCatalog.new
    index.start_page_number = 101
    [11, 10, 100, 101].each do |pgnum|
      index.store_primary_term 'monkey', { anchor: (anchor_name = index.next_anchor_name) }
      index.link_dest_to_page anchor_name, pgnum
    end
    monkey_term = index.categories[0].terms[0]
    (expect monkey_term.dests.map {|it| it[:page] }).to eql %w(x xi c 1)
  end

  it 'should not combine deduplicate page numbers if same index entry occurs on the same page when media is screen' do
    input = <<~'EOS'
    = Document Title

    == First Chapter

    ((coming soon))

    == Second Chapter

    ((coming soon))

    ((coming soon)), no really

    == Third Chapter

    ((coming soon)) means ((coming soon))

    [index]
    == Index
    EOS

    pdf = to_pdf input, doctype: :book, analyze: true
    (expect (pdf.lines pdf.find_text page_number: 5).join ?\n).to include 'coming soon, 1, 2, 2, 3, 3'
    pdf = to_pdf input, doctype: :book
    annots = get_annotations pdf, 5
    (expect annots).to have_size 5
    (expect annots.uniq {|it| it[:Dest] }).to have_size 5
  end

  it 'should not combine range if same index entry occurs on sequential pages when media is screen' do
    pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
    = Document Title

    == First Chapter

    ((coming soon))

    == Second Chapter

    ((coming soon))

    == Third Chapter

    ((coming soon))

    [index]
    == Index
    EOS

    (expect (pdf.lines pdf.find_text page_number: 5).join ?\n).to include 'coming soon, 1, 2, 3'
  end

  it 'should remove duplicate sequential pages and link to first occurrence when index-pagenum-sequence-style is page' do
    input = <<~'EOS'
    = Document Title
    :doctype: book
    :index-pagenum-sequence-style: page

    == First Chapter

    ((coming soon)) ((almost here))

    ((coming soon)), really

    == Second Chapter

    ((coming soon)) ((in draft))

    I promise, ((coming soon))

    == Third Chapter

    ((coming soon)) ((almost here))

    [index]
    == Index
    EOS

    pdf = to_pdf input, analyze: true
    index_lines = pdf.lines pdf.find_text page_number: 5
    (expect index_lines).to include 'coming soon, 1, 2, 3'
    (expect index_lines).to include 'in draft, 2'
    (expect index_lines).to include 'almost here, 1, 3'
    pdf = to_pdf input
    annots = get_annotations pdf, 5
    (expect annots).to have_size 6
    [[2, 2], [3, 3], [4, 4]].each do |idx, expected_pagenum|
      dest = get_dest pdf, annots[idx][:Dest]
      (expect dest[:page_number]).to eql expected_pagenum
    end
  end

  it 'should combine range if same index entry occurs on sequential pages when index-pagenum-sequence-style is range' do
    input = <<~'EOS'
    = Document Title
    :doctype: book
    :index-pagenum-sequence-style: range

    == First Chapter

    ((coming soon)) ((almost here))

    == Second Chapter

    ((coming soon)) ((in draft))

    == Third Chapter

    ((coming soon)) ((almost here))

    [index]
    == Index
    EOS

    pdf = to_pdf input, analyze: true
    index_lines = pdf.lines pdf.find_text page_number: 5
    (expect index_lines).to include 'coming soon, 1-3'
    (expect index_lines).to include 'in draft, 2'
    (expect index_lines).to include 'almost here, 1, 3'
    pdf = to_pdf input
    annots = get_annotations pdf, 5
    (expect annots).to have_size 4
    coming_soon_dest = get_dest pdf, annots[2][:Dest]
    (expect coming_soon_dest[:page_number]).to eql 2
  end

  it 'should combine range if same index entry occurs on sequential pages when media is not screen' do
    pdf = to_pdf <<~'EOS', doctype: :book, attribute_overrides: { 'media' => 'print' }, analyze: true
    = Document Title

    == First Chapter

    ((coming soon)) ((almost here))

    == Second Chapter

    ((coming soon)) ((in draft))

    == Third Chapter

    ((coming soon)) ((almost here))

    [index]
    == Index
    EOS

    index_lines = pdf.lines pdf.find_text page_number: 5
    (expect index_lines).to include 'coming soon, 1-3'
    (expect index_lines).to include 'in draft, 2'
    (expect index_lines).to include 'almost here, 1, 3'
  end

  it 'should sort page ranges using first page in sequence when media=print' do
    indexterm_pagenums = [1, 10, 11, 13, 15, 16, 100, 150]
    pagebreak = %(\n\n<<<\n\n)
    input_lines = (1.upto 150).map {|pagenum| (indexterm_pagenums.include? pagenum) ? '((monkey))' : 'business' }
    pdf = to_pdf <<~EOS, analyze: true
    :doctype: book
    :media: print

    #{input_lines.join pagebreak}

    [index]
    == Index
    EOS

    (expect pdf.lines pdf.pages[-1][:text]).to include 'monkey, 1, 10-11, 13, 15-16, 100, 150'
  end

  it 'should apply hanging indent to wrapped lines equal to twice level indent' do
    pdf = to_pdf <<~'EOS', analyze: true
    = Document Title
    :doctype: book

    text(((searching,for fun and profit)))(((searching,when you have absolutely no clue where to begin)))

    [index]
    == Index
    EOS

    searching_text = (pdf.find_text page_number: 3, string: 'searching')[0]
    fun_profit_text = (pdf.find_text page_number: 3, string: /^for fun/)[0]
    begin_text = (pdf.find_text page_number: 3, string: /^begin/)[0]
    left_margin = searching_text[:x]
    level_indent = fun_profit_text[:x] - left_margin
    hanging_indent = begin_text[:x] - fun_profit_text[:x]
    (expect hanging_indent.round).to eql (level_indent * 2).round
  end

  it 'should not insert blank line if index term is forced to break' do
    pdf = to_pdf <<~'EOS', analyze: true
    = Document Title
    :doctype: book
    :notitle:

    text(((flags,SHORT_FLAG)))(((flags,SUPER_LONG_FLAG_THAT_IS_FORCED_TO_BREAK)))

    [index]
    == Index
    EOS

    flags_text = (pdf.find_text 'flags', page_number: 2)[0]
    short_flag_text = (pdf.find_text %r/^SHORT_FLAG/, page_number: 2)[0]
    long_flag_text = (pdf.find_text %r/^SUPER_LONG_FLAG/, page_number: 2)[0]
    line_gap = (flags_text[:y] - short_flag_text[:y]).round 2
    (expect short_flag_text[:x]).to eql long_flag_text[:x]
    (expect (short_flag_text[:y] - long_flag_text[:y]).round 2).to eql line_gap
  end

  it 'should arrange index entries into two columns by default' do
    pdf = to_pdf <<~EOS, analyze: true
    = Document Title
    :doctype: book
    :notitle:

    #{('a'..'z').map {|it| %(((#{it}-term))) }.join}

    [index]
    == Index
    EOS

    category_a_text = (pdf.find_text 'A')[0]
    category_p_text = (pdf.find_text 'P')[0]
    (expect category_a_text[:page_number]).to be 2
    (expect category_p_text[:page_number]).to be 2
    (expect category_p_text[:y]).to eql category_a_text[:y]
    (expect category_p_text[:x]).to be > category_a_text[:x]
  end

  it 'should allow theme to configure number of columns' do
    pdf = to_pdf <<~EOS, pdf_theme: { index_columns: 3 }, analyze: true
    = Document Title
    :doctype: book
    :notitle:

    #{('a'..'z').map {|it| %(((#{it}-keyword))((#{it}-term))) }.join}

    [index]
    == Index
    EOS

    category_a_text = (pdf.find_text 'A')[0]
    category_l_text = (pdf.find_text 'L')[0]
    category_w_text = (pdf.find_text 'W')[0]
    (expect category_a_text[:page_number]).to be 2
    (expect category_l_text[:page_number]).to be 2
    (expect category_w_text[:page_number]).to be 2
    (expect category_l_text[:y]).to eql category_a_text[:y]
    (expect category_w_text[:y]).to eql category_a_text[:y]
    (expect category_w_text[:x]).to be > category_l_text[:x]
    (expect category_l_text[:x]).to be > category_a_text[:x]
  end

  it 'should not allocate space for anchor if font is missing glyph for null character' do
    pdf_theme = {
      extends: 'default',
      font_catalog: {
        'Missing Null' => {
          'normal' => (fixture_file 'mplus1mn-regular-ascii.ttf'),
          'bold' => (fixture_file 'mplus1mn-regular-ascii.ttf'),
        },
      },
      base_font_family: 'Missing Null',
    }

    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    foo ((bar)) #baz#

    foo bar #baz#

    [index]
    == Index
    EOS

    baz_texts = pdf.find_text 'baz'
    (expect baz_texts).to have_size 2
    (expect baz_texts[0][:x]).to eql baz_texts[1][:x]
  end

  it 'should not distribute excess bottom margin at top of next column' do
    pdf = to_pdf <<~'EOS', analyze: true
    = Document Title
    :doctype: book
    :pdf-page-size: A6

    ((foo))((bar))((baz))((boom))((bang))((fee))((fi))((fo))((fum))((fan))((fool))((ying))((yang))((zed))

    [index]
    == Index
    EOS

    b_category_text = pdf.find_unique_text 'B', page_number: 3
    z_category_text = pdf.find_unique_text 'Z', page_number: 3
    (expect b_category_text[:y]).to eql z_category_text[:y]
  end

  it 'should apply prepress margins to subsequent pages in index' do
    pdf_theme = {
      page_margin: [48.24, 48.24, 48.24, 48.24],
      page_margin_inner: 72,
      page_margin_outer: 36,
      index_columns: 1,
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    = Document Title
    :doctype: book
    :notitle:
    :media: prepress
    :pdf-page-size: A5

    == Chapter

    ((foo)) and ((bar))

    ((yin)) and ((yang))

    ((tea)) and ((coffee))

    ((left)) and ((right))

    ((salt)) and ((pepper))

    ((up)) and ((down))

    ((sugar)) and ((spice))

    ((day)) and ((night))

    ((melody)) and ((harmony))

    [index]
    == Index
    EOS

    d_category_text = pdf.find_unique_text 'D', page_number: 3
    s_category_text = pdf.find_unique_text 'S', page_number: 4
    (expect d_category_text).not_to be_nil
    (expect s_category_text).not_to be_nil
    (expect d_category_text[:x]).to eql 72.0
    (expect s_category_text[:x]).to eql 36.0
  end

  it 'should preserve column count on subsequent pages' do
    pdf_theme = {
      page_margin: 36,
      page_margin_inner: 54,
      page_margin_outer: 18,
    }
    pdf = to_pdf <<~EOS, pdf_theme: pdf_theme, analyze: true
    = Document Title
    :doctype: book
    :notitle:
    :media: prepress
    :pdf-page-size: A5

    == Chapter

    #{('a'..'z').map {|l| [l, l * 2, l * 3, l * 4] }.flatten.map {|it| '((' + it + '))' }.join ' '}

    [index]
    == Index
    EOS

    (expect pdf.pages).to have_size 5
    midpoint_recto = 54 + (pdf.pages[0][:size][0] - 72) * 0.5
    midpoint_verso = 18 + (pdf.pages[0][:size][0] - 72) * 0.5

    a_category_text = pdf.find_unique_text 'A', page_number: 3
    f_category_text = pdf.find_unique_text 'F', page_number: 3
    k_category_text = pdf.find_unique_text 'K', page_number: 4
    q_category_text = pdf.find_unique_text 'Q', page_number: 4
    z_category_text = pdf.find_unique_text 'Z', page_number: 5
    (expect a_category_text).not_to be_nil
    (expect a_category_text[:x]).to eql 54.0
    (expect f_category_text).not_to be_nil
    (expect f_category_text[:x]).to be > midpoint_recto
    (expect k_category_text).not_to be_nil
    (expect k_category_text[:x]).to eql 18.0
    (expect q_category_text).not_to be_nil
    (expect q_category_text[:x]).to be > midpoint_verso
    (expect q_category_text[:x]).to be < midpoint_recto
    (expect z_category_text).not_to be_nil
    (expect z_category_text[:x]).to eql 54.0
  end

  it 'should indent TOC title properly when index exceeds a page and section indent is positive' do
    pdf = to_pdf <<~EOS, pdf_theme: { section_indent: 50 }, analyze: true
    = Document Title
    :doctype: book
    :toc:

    == Chapter A

    This is the first chapter.
    #{(0...50).map {|it| %[(((A#{it})))] }.join}

    == Chapter B

    This is the second and last chapter.
    #{(0...50).map {|it| %[(((B#{it})))] }.join}

    [index]
    == Index
    EOS

    toc_title_text = pdf.find_unique_text 'Table of Contents'
    (expect toc_title_text[:x]).to eql 48.24

    chapter_a_toc_text = pdf.find_unique_text 'Chapter A', page_number: 2
    (expect chapter_a_toc_text[:x]).to eql 98.24
  end
end