File: floating_title_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 (343 lines) | stat: -rw-r--r-- 9,901 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
# frozen_string_literal: true

require_relative 'spec_helper'

describe 'Asciidoctor::PDF::Converter - Floating Title' do
  it 'should apply alignment defined for headings in theme' do
    pdf_theme = {
      heading_text_align: 'center',
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    [discrete]
    == Discrete Heading

    main content
    EOS

    discrete_heading_text = pdf.find_unique_text 'Discrete Heading'
    main_text = pdf.find_unique_text 'main content'
    (expect discrete_heading_text[:x]).to be > main_text[:x]
  end

  it 'should apply alignment defined for heading level in theme' do
    pdf_theme = {
      heading_text_align: 'left',
      heading_h2_text_align: 'center',
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    [discrete]
    == Discrete Heading

    main content
    EOS

    discrete_heading_text = pdf.find_unique_text 'Discrete Heading'
    main_text = pdf.find_unique_text 'main content'
    (expect discrete_heading_text[:x]).to be > main_text[:x]
  end

  it 'should use base text align to align floating title if theme does not specify alignemnt' do
    pdf_theme = {
      base_text_align: 'center',
      heading_h2_text_align: nil,
      heading_text_align: nil,
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    [discrete]
    == Discrete Heading

    [.text-left]
    main content
    EOS

    discrete_heading_text = pdf.find_unique_text 'Discrete Heading'
    main_text = pdf.find_unique_text 'main content'
    (expect discrete_heading_text[:x]).to be > main_text[:x]
  end

  it 'should force discrete heading to next page if space below is less than heading-min-height-after value' do
    pdf = with_content_spacer 10, 690 do |spacer_path|
      to_pdf <<~EOS
      image::#{spacer_path}[]

      [discrete#buddy]
      == Discrete Heading

      Don't abandon me!
      EOS
    end

    (expect pdf.pages).to have_size 2
    p2_text = (pdf.page 2).text
    (expect p2_text).to include 'Discrete Heading'
    (expect get_names pdf).to have_key 'buddy'
    (expect (get_dest pdf, 'buddy')[:page_number]).to eql 2
  end

  it 'should force discrete heading with breakable option to next page if no content is inked below it' do
    pdf = with_content_spacer 10, 675 do |spacer_path|
      to_pdf <<~EOS, pdf_theme: { heading_min_height_after: nil }
      image::#{spacer_path}[]

      [discrete#buddy%breakable]
      == Discrete Heading

      ----
      Do it like this.
      ----
      EOS
    end

    (expect pdf.pages).to have_size 2
    p2_text = (pdf.page 2).text
    (expect p2_text).to include 'Discrete Heading'
    (expect get_names pdf).to have_key 'buddy'
    (expect (get_dest pdf, 'buddy')[:page_number]).to eql 2
  end

  it 'should force discrete heading to next page when heading-min-height-after is auto if no content is inked below it' do
    pdf = with_content_spacer 10, 675 do |spacer_path|
      to_pdf <<~EOS, pdf_theme: { heading_min_height_after: 'auto' }
      image::#{spacer_path}[]

      [discrete#buddy]
      == Discrete Heading

      ----
      Do it like this.
      ----
      EOS
    end

    (expect pdf.pages).to have_size 2
    p2_text = (pdf.page 2).text
    (expect p2_text).to include 'Discrete Heading'
    (expect get_names pdf).to have_key 'buddy'
    (expect (get_dest pdf, 'buddy')[:page_number]).to eql 2
  end

  it 'should ignore heading-min-height-after if heading is last child' do
    pdf = with_content_spacer 10, 650 do |spacer_path|
      to_pdf <<~EOS, pdf_theme: { heading_min_height_after: 100, heading_font_color: 'AA0000' }, analyze: true
      image::#{spacer_path}[]

      [discrete]
      == Heading Fits
      EOS
    end

    (expect pdf.pages).to have_size 1
    heading_text = pdf.find_unique_text font_color: 'AA0000'
    (expect heading_text[:page_number]).to eql 1
  end

  it 'should allow arrange_heading to be reimplemented to always keep heading with content that follows it' do
    source_file = doc_file 'modules/extend/examples/pdf-converter-avoid-break-after-heading.rb'
    source_lines = (File.readlines source_file).select {|l| l == ?\n || (l.start_with? ' ') }
    ext_class = create_class Asciidoctor::Converter.for 'pdf'
    backend = %(pdf#{ext_class.object_id})
    source_lines[0] = %(  register_for '#{backend}'\n)
    ext_class.class_eval source_lines.join, source_file
    pdf = to_pdf <<~EOS, backend: backend, analyze: true
    [discrete]
    == Heading A

    [discrete]
    == Heading B

    image::tall.svg[pdfwidth=65mm]

    [discrete]
    == Heading C

    [%unbreakable]
    --
    keep

    this

    together
    --
    EOS

    heading_c_text = pdf.find_unique_text 'Heading C'
    (expect heading_c_text[:page_number]).to be 2
    content_text = pdf.find_unique_text 'keep'
    (expect content_text[:page_number]).to be 2
  end

  it 'should not force discrete heading to next page if heading-min-height-after value is not set' do
    pdf = with_content_spacer 10, 690 do |spacer_path|
      to_pdf <<~EOS, pdf_theme: { heading_min_height_after: nil }
      image::#{spacer_path}[]

      [discrete#buddy]
      == Discrete Heading

      Don't abandon me!
      EOS
    end

    (expect pdf.pages).to have_size 2
    p1_text = (pdf.page 1).text
    (expect p1_text).to include 'Discrete Heading'
    p2_text = (pdf.page 2).text
    (expect p2_text).to include 'abandon'
    (expect get_names pdf).to have_key 'buddy'
    (expect (get_dest pdf, 'buddy')[:page_number]).to eql 1
  end

  it 'should not force discrete heading without breakable option to next page if no content is inked below it' do
    pdf = with_content_spacer 10, 675 do |spacer_path|
      to_pdf <<~EOS
      image::#{spacer_path}[]

      [discrete#buddy]
      == Discrete Heading

      ----
      Do it like this.
      ----
      EOS
    end

    (expect pdf.pages).to have_size 2
    p1_text = (pdf.page 1).text
    (expect p1_text).to include 'Discrete Heading'
    p2_text = (pdf.page 2).text
    (expect p2_text).to include 'like this'
    (expect get_names pdf).to have_key 'buddy'
    (expect (get_dest pdf, 'buddy')[:page_number]).to eql 1
  end

  it 'should not force discrete heading to next page if it has no next sibling' do
    pdf = with_content_spacer 10, 690 do |spacer_path|
      to_pdf <<~EOS
      image::#{spacer_path}[]

      [discrete#buddy]
      == Discrete Heading
      EOS
    end

    (expect pdf.pages).to have_size 1
    p1_text = (pdf.page 1).text
    (expect p1_text).to include 'Discrete Heading'
    (expect get_names pdf).to have_key 'buddy'
    (expect (get_dest pdf, 'buddy')[:page_number]).to eql 1
  end

  it 'should outdent discrete heading' do
    pdf = to_pdf <<~'EOS', pdf_theme: { section_indent: 36 }, analyze: true
    = Document Title

    == Section

    paragraph

    [discrete]
    === Discrete Heading

    paragraph

    === Nested Section

    paragraph

    [discrete]
    ==== Another Discrete Heading

    paragraph
    EOS

    discrete_heading_texts = pdf.find_text %r/Discrete/
    (expect discrete_heading_texts).to have_size 2
    (expect discrete_heading_texts[0][:x]).to eql 48.24
    (expect discrete_heading_texts[1][:x]).to eql 48.24
    paragraph_texts = pdf.find_text 'paragraph'
    (expect paragraph_texts.map {|it| it[:x] }.uniq).to eql [84.24]
  end

  it 'should not outdent discrete heading inside block' do
    pdf = to_pdf <<~'EOS', pdf_theme: { section_indent: 36 }, analyze: true
    == Section

    ****
    sidebar content

    [discrete]
    == Discrete Heading
    ****
    EOS

    sidebar_content_text = (pdf.find_text 'sidebar content')[0]
    discrete_heading_text = (pdf.find_text 'Discrete Heading')[0]
    (expect sidebar_content_text[:x]).to eql discrete_heading_text[:x]
  end

  it 'should honor text alignment role on discrete heading' do
    pdf = to_pdf <<~'EOS', analyze: true
    [discrete]
    == Discrete Heading
    EOS
    left_x = (pdf.find_text 'Discrete Heading')[0][:x]

    pdf = to_pdf <<~'EOS', analyze: true
    [discrete.text-right]
    == Discrete Heading
    EOS
    right_x = (pdf.find_text 'Discrete Heading')[0][:x]

    (expect right_x).to be > left_x
  end

  it 'should allow theme to add borders and padding to specific heading levels' do
    pdf_theme = {
      heading_line_height: 1,
      heading_font_family: 'Times-Roman',
      heading_h2_border_width: [2, 0],
      heading_h2_border_color: 'AA0000',
      heading_h2_padding: [10, 0],
      heading_h3_border_width: [0, 0, 1, 0],
      heading_h3_border_style: 'dashed',
      heading_h3_border_color: 'DDDDDD',
      heading_h3_padding: [0, 0, 5],
    }

    input = <<~'EOS'
    [discrete]
    == Heading Level 1

    content

    [discrete]
    === Heading Level 2

    content
    EOS

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

    (expect lines).to have_size 3
    (expect lines[0][:color]).to eql 'AA0000'
    (expect lines[0][:width]).to eql 2
    (expect lines[0][:style]).to eql :solid
    (expect lines[1][:color]).to eql 'AA0000'
    (expect lines[1][:width]).to eql 2
    (expect lines[1][:style]).to eql :solid
    (expect lines[2][:color]).to eql 'DDDDDD'
    (expect lines[2][:width]).to eql 1
    (expect lines[2][:style]).to eql :dashed
    lines.each do |line|
      (expect line[:from][:y]).to eql line[:to][:y]
    end
    heading_level_1_text = pdf.find_unique_text 'Heading Level 1'
    heading_level_2_text = pdf.find_unique_text 'Heading Level 2'
    (expect lines[0][:from][:y]).to be > heading_level_1_text[:y]
    (expect lines[1][:from][:y]).to be < heading_level_1_text[:y]
    (expect lines[2][:from][:y]).to be < heading_level_2_text[:y]
    (expect heading_level_1_text[:y] - lines[1][:from][:y]).to be > 10
    (expect heading_level_2_text[:y] - lines[2][:from][:y]).to be > 5
  end
end