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
|
# frozen_string_literal: true
require_relative 'spec_helper'
describe 'Asciidoctor::PDF::Converter - Cover Page' do
it 'should add front cover page if front-cover-image attribute is set to bare path' do
pdf = to_pdf <<~EOS
= Document Title
:front-cover-image: #{fixture_file 'cover.jpg', relative: true}
content page
EOS
(expect pdf.pages).to have_size 2
(expect pdf.pages[0].text).to be_empty
images = get_images pdf, 1
(expect images).to have_size 1
(expect images[0].data).to eql File.binread fixture_file 'cover.jpg'
end
it 'should add front cover page if front-cover-image attribute is set to image macro' do
pdf = to_pdf <<~'EOS'
= Document Title
:front-cover-image: image:cover.jpg[]
content page
EOS
(expect pdf.pages).to have_size 2
(expect pdf.pages[0].text).to be_empty
images = get_images pdf, 1
(expect images).to have_size 1
(expect images[0].data).to eql File.binread fixture_file 'cover.jpg'
end
it 'should add front cover page if front-cover-image attribute is set to data URI' do
image_data = File.binread fixture_file 'cover.jpg'
encoded_image_data = Base64.strict_encode64 image_data
pdf = to_pdf <<~EOS
= Document Title
:front-cover-image: image:data:image/jpg;base64,#{encoded_image_data}[]
content page
EOS
(expect pdf.pages).to have_size 2
(expect pdf.pages[0].text).to be_empty
images = get_images pdf, 1
(expect images).to have_size 1
(expect images[0].data).to eql File.binread fixture_file 'cover.jpg'
end
it 'should not add cover page if file cannot be resolved' do
(expect do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:front-cover-image: image:no-such-file.jpg[]
content page
EOS
(expect pdf.pages).to have_size 2
(expect pdf.lines pdf.find_text page_number: 1).to eql ['Document Title']
end).to log_message severity: :WARN, message: %(front cover image not found or readable: #{fixture_file 'no-such-file.jpg'})
end
it 'should not add cover page if image cannot be embedded' do
(expect do
pdf = to_pdf <<~'EOS', analyze: true
:front-cover-image: image:broken.svg[]
content page
EOS
(expect pdf.pages).to have_size 1
(expect pdf.lines pdf.find_text page_number: 1).to eql ['content page']
end).to log_message severity: :WARN, message: %(~could not embed front cover image: #{fixture_file 'broken.svg'}; Missing end tag for 'rect')
end
it 'should not add cover page if value is ~' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:front-cover-image: ~
content page
EOS
(expect pdf.pages).to have_size 2
(expect pdf.lines pdf.find_text page_number: 1).to eql ['Document Title']
end
it 'should add front cover page if cover_front_image theme key is set' do
pdf_theme = { cover_front_image: (fixture_file 'cover.jpg') }
pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme
= Document Title
content page
EOS
(expect pdf.pages).to have_size 2
(expect pdf.pages[0].text).to be_empty
images = get_images pdf, 1
(expect images).to have_size 1
(expect images[0].data).to eql File.binread fixture_file 'cover.jpg'
end
it 'should add back cover page if back-cover-image attribute is set to raw path' do
pdf = to_pdf <<~EOS
= Document Title
:front-cover-image: #{fixture_file 'cover.jpg', relative: true}
:back-cover-image: #{fixture_file 'cover.jpg', relative: true}
content page
EOS
(expect pdf.pages).to have_size 3
(expect pdf.pages[0].text).to be_empty
(expect pdf.pages[2].text).to be_empty
images = get_images pdf, 3
(expect images).to have_size 1
(expect images[0].data).to eql File.binread fixture_file 'cover.jpg'
end
it 'should add back cover page if back-cover-image attribute is set to image macro' do
pdf = to_pdf <<~'EOS'
= Document Title
:front-cover-image: image:cover.jpg[]
:back-cover-image: image:cover.jpg[]
content page
EOS
(expect pdf.pages).to have_size 3
(expect pdf.pages[0].text).to be_empty
(expect pdf.pages[2].text).to be_empty
images = get_images pdf, 3
(expect images).to have_size 1
(expect images[0].data).to eql File.binread fixture_file 'cover.jpg'
end
it 'should add back cover page if back-cover-image attribute is set to data URI' do
image_data = File.binread fixture_file 'cover.jpg'
encoded_image_data = Base64.strict_encode64 image_data
pdf = to_pdf <<~EOS
= Document Title
:front-cover-image: image:data:image/jpg;base64,#{encoded_image_data}[]
:back-cover-image: image:data:image/jpg;base64,#{encoded_image_data}[]
content page
EOS
(expect pdf.pages).to have_size 3
(expect pdf.pages[0].text).to be_empty
(expect pdf.pages[2].text).to be_empty
images = get_images pdf, 3
(expect images).to have_size 1
(expect images[0].data).to eql File.binread fixture_file 'cover.jpg'
end
it 'should add back cover page if cover_back_image theme key is set' do
pdf_theme = {
cover_front_image: (fixture_file 'cover.jpg'),
cover_back_image: (fixture_file 'cover.jpg'),
}
pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme
= Document Title
content page
EOS
(expect pdf.pages).to have_size 3
(expect pdf.pages[0].text).to be_empty
(expect pdf.pages[2].text).to be_empty
images = get_images pdf, 3
(expect images).to have_size 1
(expect images[0].data).to eql File.binread fixture_file 'cover.jpg'
end
it 'should prefer attribute over theme key' do
pdf_theme = { cover_back_image: (fixture_file 'not-this-one.jpg') }
pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme
= Document Title
:back-cover-image: image:cover.jpg[]
content page
EOS
(expect pdf.pages).to have_size 2
(expect pdf.pages[1].text).to be_empty
images = get_images pdf, 2
(expect images).to have_size 1
(expect images[0].data).to eql File.binread fixture_file 'cover.jpg'
end
it 'should create blank page if front or back cover image is empty' do
pdf = to_pdf <<~'EOS'
= Book Title
:doctype: book
:front-cover-image:
:back-cover-image:
== Chapter
text
EOS
(expect pdf.pages).to have_size 4
(expect (pdf.page 1).text).to be_empty
(expect (pdf.page 2).text).to include 'Book Title'
(expect (pdf.page 4).text).to be_empty
end
it 'should create document with cover page only if front-cover-image is set and document has no content' do
%w(article book).each do |doctype|
pdf = to_pdf %(:front-cover-image: #{fixture_file 'cover.jpg', relative: true}), doctype: doctype
(expect pdf.pages).to have_size 1
(expect pdf.pages[0].text).to be_empty
images = get_images pdf, 1
(expect images).to have_size 1
(expect images[0].data).to eql File.binread fixture_file 'cover.jpg'
(expect extract_outline pdf).to be_empty
end
end
it 'should not crash if front cover image is a URI and the allow-uri-read attribute is not set' do
pdf = nil
(expect do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:front-cover-image: https://example.org/cover.svg
content
EOS
end).to not_raise_exception & (log_message severity: :WARN, message: '~allow-uri-read attribute not enabled')
(expect pdf.pages).to have_size 1
(expect pdf.find_text 'Document Title').to have_size 1
end
it 'should recognize attribute value that uses image macro syntax and resolve relative to imagesdir', visual: true do
%w(block inline).each do |type|
to_file = to_pdf_file <<~EOS, %(cover-page-front-cover-#{type}-image-macro.pdf)
= Document Title
:doctype: book
:front-cover-image: image:#{type == 'block' ? ':' : ''}cover.jpg[]
content page
EOS
(expect to_file).to visually_match 'cover-page-front-cover-image-contain.pdf'
end
end
it 'should resolve bare image path relative to docdir', visual: true do
input_file = Pathname.new fixture_file 'hello.adoc'
to_file = to_pdf_file input_file, 'cover-page-front-cover-image-path.pdf', attribute_overrides: { 'imagesdir' => 'does-not-exist', 'front-cover-image' => 'cover.jpg' }
(expect to_file).to visually_match 'cover-page-front-cover-image-path.pdf'
end
it 'should scale front cover image to boundaries of page by default', visual: true do
['', 'fit=contain'].each do |image_opts|
to_file = to_pdf_file <<~EOS, %(cover-page-front-cover-image-#{image_opts.empty? ? 'default' : 'contain'}.pdf)
= Document Title
:doctype: book
:front-cover-image: image:cover.jpg[#{image_opts}]
content page
EOS
(expect to_file).to visually_match 'cover-page-front-cover-image-contain.pdf'
end
end
it 'should stretch front cover image to boundaries of page if fit=fill', visual: true do
to_file = to_pdf_file <<~'EOS', 'cover-page-front-cover-image-fill.pdf'
= Document Title
:doctype: book
:front-cover-image: image:cover.jpg[fit=fill]
:pdf-page-size: Letter
content page
EOS
(expect to_file).to visually_match 'cover-page-front-cover-image-fill.pdf'
end
it 'should not scale front cover image to fit page if fit is none', visual: true do
to_file = to_pdf_file <<~'EOS', 'cover-page-front-cover-image-unscaled.pdf'
= Document Title
:doctype: book
:front-cover-image: image:cover.jpg[fit=none]
content page
EOS
(expect to_file).to visually_match 'cover-page-front-cover-image-unscaled.pdf'
end
it 'should scale front cover down until it is contained within the boundaries of the page', visual: true do
['', 'fit=scale-down'].each do |image_opts|
to_file = to_pdf_file <<~EOS, %(cover-page-front-cover-image-#{image_opts.empty? ? 'max' : 'scale-down'}.pdf)
:front-cover-image: image:cover.jpg[#{image_opts}]
:pdf-page-size: A7
content page
EOS
(expect to_file).to visually_match 'cover-page-front-cover-image-max.pdf'
end
end
it 'should scale front cover image until it covers page if fit=cover', visual: true do
to_file = to_pdf_file <<~'EOS', 'cover-page-front-cover-image-cover.pdf'
= Document Title
:front-cover-image: image:cover.jpg[fit=cover]
content page
EOS
(expect to_file).to visually_match 'cover-page-front-cover-image-cover.pdf'
end
it 'should scale front cover image with aspect ratio taller than page until it covers page if fit=cover' do
pdf_page_size = get_page_size (to_pdf 'content', attribute_overrides: { 'pdf-page-size' => 'Letter' }), 1
pdf = to_pdf <<~'EOS', analyze: :image
= Document Title
:pdf-page-size: Letter
:front-cover-image: image:cover.jpg[fit=cover]
content page
EOS
images = pdf.images
(expect images).to have_size 1
cover_image = images[0]
(expect cover_image[:x].to_f).to eql 0.0
(expect cover_image[:width]).to eql pdf_page_size[0].to_f
(expect cover_image[:height]).to be > pdf_page_size[1]
(expect cover_image[:y]).to be > pdf_page_size[1]
end
it 'should position front cover image as specified by position attribute', visual: true do
to_file = to_pdf_file <<~'EOS', 'cover-page-front-cover-image-positioned.pdf'
= Document Title
:front-cover-image: image:square.svg[fit=none,pdfwidth=50%,position=top right]
content page
EOS
(expect to_file).to visually_match 'cover-page-front-cover-image-positioned.pdf'
end
it 'should use specified image format', visual: true do
source_file = (dest_file = fixture_file 'square') + '.svg'
FileUtils.cp source_file, dest_file
to_file = to_pdf_file <<~'EOS', 'cover-page-front-cover-image-format.pdf'
= Document Title
:front-cover-image: image:square[format=svg]
content page
EOS
(expect to_file).to visually_match 'cover-page-front-cover-image-format.pdf'
ensure
File.unlink dest_file
end
it 'should set the base font for a book when front cover image is a PDF and title page is off' do
pdf = to_pdf <<~EOS, analyze: true
= Document Title
:front-cover-image: #{fixture_file 'blue-letter.pdf', relative: true}
:doctype: book
:notitle:
content
EOS
(expect (pdf.find_unique_text 'content')[:font_name]).to eql 'NotoSerif'
end
it 'should set the base font for an article when front cover image is a PDF and title page is off' do
pdf = to_pdf <<~EOS, analyze: true
= Document Title
:front-cover-image: #{fixture_file 'blue-letter.pdf', relative: true}
content
EOS
(expect (pdf.find_unique_text 'content')[:font_name]).to eql 'NotoSerif'
end
it 'should not allow page size of PDF cover page to affect page size of document' do
input = <<~EOS
= Document Title
:front-cover-image: #{fixture_file 'blue-letter.pdf', relative: true}
content
EOS
pdf = to_pdf input, analyze: :rect
rects = pdf.rectangles
(expect rects).to have_size 1
(expect rects[0]).to eql point: [0.0, 0.0], width: 612.0, height: 792.0
pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 2
(expect pdf.pages[0][:text]).to be_empty
(expect pdf.pages[0][:size].map(&:to_f)).to eql PDF::Core::PageGeometry::SIZES['LETTER']
(expect pdf.pages[1][:size]).to eql PDF::Core::PageGeometry::SIZES['A4']
(expect pdf.pages[1][:text]).not_to be_empty
end
it 'should import specified page from PDF file defined using front-cover-image attribute' do
pdf = to_pdf <<~'EOS'
:front-cover-image: image:red-green-blue.pdf[page=3]
content
EOS
(expect pdf.pages).to have_size 2
page_contents = pdf.objects[(pdf.page 1).page_object[:Contents][0]].data
(expect (page_contents.split ?\n).slice 0, 3).to eql ['q', '/DeviceRGB cs', '0.0 0.0 1.0 scn']
end
it 'should import specified page from PDF file defined using cover_front_image theme key' do
pdf_theme = { cover_front_image: %(image:#{fixture_file 'red-green-blue.pdf'}[page=3]) }
pdf = to_pdf 'content', pdf_theme: pdf_theme
(expect pdf.pages).to have_size 2
page_contents = pdf.objects[(pdf.page 1).page_object[:Contents][0]].data
(expect (page_contents.split ?\n).slice 0, 3).to eql ['q', '/DeviceRGB cs', '0.0 0.0 1.0 scn']
end
it 'should not add front cover if reference page in PDF file does not exist' do
pdf = to_pdf <<~'EOS'
:front-cover-image: image:red-green-blue.pdf[page=10]
one
<<<
two
EOS
(expect pdf.pages).to have_size 2
(expect (pdf.page 1).text).to eql 'one'
outline = extract_outline pdf
(expect outline[0][:title]).to eql 'Untitled'
(expect outline[0][:dest][:label]).to eql '1'
end
it 'should add back cover using referenced page in PDF file' do
pdf = to_pdf <<~'EOS'
:back-cover-image: image:red-green-blue.pdf[page=3]
content
EOS
(expect pdf.pages).to have_size 2
(expect (pdf.page 1).text).to eql 'content'
page_contents = pdf.objects[(pdf.page 2).page_object[:Contents][0]].data
(expect (page_contents.split ?\n).slice 0, 3).to eql ['q', '/DeviceRGB cs', '0.0 0.0 1.0 scn']
end
it 'should not add back cover if referenced page in PDF file does not exist' do
pdf = to_pdf <<~'EOS'
:back-cover-image: image:red-green-blue.pdf[page=10]
content
EOS
(expect pdf.pages).to have_size 1
(expect (pdf.page 1).text).to eql 'content'
end
it 'should not add front cover if PDF file has no pages' do
pdf = to_pdf <<~'EOS'
:front-cover-image: image:no-pages.pdf[]
one
<<<
two
EOS
(expect pdf.pages).to have_size 2
(expect (pdf.page 1).text).to eql 'one'
outline = extract_outline pdf
(expect outline[0][:title]).to eql 'Untitled'
(expect outline[0][:dest][:label]).to eql '1'
end
it 'should not add back cover if PDF file has no pages' do
pdf = to_pdf <<~'EOS'
:back-cover-image: image:no-pages.pdf[]
content
EOS
(expect pdf.pages).to have_size 1
(expect (pdf.page 1).text).to eql 'content'
end
end
|