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
|
# frozen_string_literal: true
require_relative 'spec_helper'
describe 'Asciidoctor::PDF::Converter - PDF Info' do
context 'compliance' do
it 'should generate a PDF 1.4-compatible document by default' do
(expect (to_pdf 'hello').pdf_version).to eql 1.4
end
it 'should set PDF version specified by pdf-version attribute if valid' do
(expect (to_pdf 'hello', attributes: { 'pdf-version' => '1.6' }).pdf_version).to eql 1.6
end
it 'should generate a PDF 1.4-compatible document if value of pdf-version attribute is not recognized' do
(expect (to_pdf 'hello', attributes: { 'pdf-version' => '3.0' }).pdf_version).to eql 1.4
end
end
context 'attribution' do
it 'should include Asciidoctor PDF and Prawn versions in Creator field' do
creator = (to_pdf 'hello').info[:Creator]
(expect creator).not_to be_nil
(expect creator).to include %(Asciidoctor PDF #{Asciidoctor::PDF::VERSION})
(expect creator).to include %(Prawn #{Prawn::VERSION})
end
it 'should set Producer field to value of Creator field by default' do
pdf = to_pdf 'hello'
(expect pdf.info[:Producer]).not_to be_nil
(expect pdf.info[:Producer]).to eql pdf.info[:Creator]
end
it 'should set Author and Producer field to value of author attribute if set' do
['Author Name', ':author: Author Name'].each do |author_line|
pdf = to_pdf <<~EOS
= Document Title
#{author_line}
content
EOS
(expect pdf.info[:Producer]).to eql pdf.info[:Author]
(expect pdf.info[:Author]).to eql 'Author Name'
end
end
it 'should set Author and Producer field to value of author attribute if set to multiple authors' do
['Author Name; Assistant Name', ':authors: Author Name; Assistant Name'].each do |author_line|
pdf = to_pdf <<~EOS
= Document Title
#{author_line}
[%hardbreaks]
First Author: {author_1}
Second Author: {author_2}
EOS
lines = ((pdf.page 1).text.split ?\n).map(&:strip)
(expect pdf.info[:Producer]).to eql pdf.info[:Author]
(expect pdf.info[:Author]).to eql 'Author Name, Assistant Name'
(expect lines).to include 'First Author: Author Name'
(expect lines).to include 'Second Author: Assistant Name'
end
end
it 'should set Author and Producer field using authors attribute with non-Latin characters' do
['Doc Writer; Antonín Dvořák', ':authors: Doc Writer; Antonín Dvořák'].each do |author_line|
pdf = to_pdf <<~EOS
= Document Title
#{author_line}
[%hardbreaks]
First Author: {author_1}
Second Author: {author_2}
EOS
lines = ((pdf.page 1).text.split ?\n).map(&:strip)
(expect pdf.info[:Producer]).to eql pdf.info[:Author]
(expect pdf.info[:Author]).to eql 'Doc Writer, Antonín Dvořák'
(expect lines).to include 'First Author: Doc Writer'
(expect lines).to include 'Second Author: Antonín Dvořák'
end
end
it 'should set Author field to value of author attribute if locked by the API' do
pdf = to_pdf <<~'EOS', attribute_overrides: { 'author' => 'Doc Writer' }
= Document Title
Author Name
content
EOS
(expect pdf.info[:Author]).to eql 'Doc Writer'
end
it 'should set Author field to value of authors attribute if locked by the API' do
pdf = to_pdf <<~'EOS', attribute_overrides: { 'authors' => 'Doc Writer' }
= Document Title
Author Name
content
EOS
(expect pdf.info[:Author]).to eql 'Doc Writer'
end
it 'should set Author field to value of authors attribute if both author and authors attributes are locked by the API' do
pdf = to_pdf <<~'EOS', attribute_overrides: { 'authors' => 'Doc Writer', 'author' => 'Anonymous' }
= Document Title
Author Name
content
EOS
(expect pdf.info[:Author]).to eql 'Doc Writer'
end
it 'should set Author field to value of author attribute if document has no doctitle' do
pdf = to_pdf <<~'EOS'
:author: Author Name
== Section Title
content
EOS
(expect pdf.info[:Author]).to eql 'Author Name'
end
it 'should set Author field to value of authors attribute if document has no doctitle' do
pdf = to_pdf <<~'EOS'
:authors: Author Name
== Section Title
content
EOS
(expect pdf.info[:Author]).to eql 'Author Name'
end
it 'should set Producer field to value of publisher attribute if set' do
pdf = to_pdf <<~'EOS'
= Document Title
Author Name
:publisher: Big Cheese
content
EOS
(expect pdf.info[:Author]).to eql 'Author Name'
(expect pdf.info[:Producer]).to eql 'Big Cheese'
end
it 'should set Subject field to value of subject attribute if set' do
pdf = to_pdf <<~'EOS'
= Document Title
:subject: Cooking
content
EOS
(expect pdf.info[:Subject]).to eql 'Cooking'
end
it 'should set Keywords field to value of subject attribute if set' do
pdf = to_pdf <<~'EOS'
= Document Title
:keywords: cooking, diet, plants
content
EOS
(expect pdf.info[:Keywords]).to eql 'cooking, diet, plants'
end
it 'should sanitize values of Author, Subject, Keywords, and Producer fields' do
pdf = to_pdf <<~'EOS'
= Document Title
D_J Allen
:subject: Science & Math
:keywords: mass–energy equivalence
:publisher: Schrödinger’s Cat
content
EOS
pdf_info = pdf.info
(expect pdf_info[:Author]).to eql 'D_J Allen'
(expect pdf_info[:Subject]).to eql 'Science & Math'
(expect pdf_info[:Keywords]).to eql 'mass–energy equivalence'
(expect pdf_info[:Producer]).to eql 'Schrödinger’s Cat'
end
it 'should parse date attributes as local date objects' do
pdf = to_pdf 'content', attribute_overrides: { 'docdatetime' => '2019-01-15', 'localdatetime' => '2019-01-15' }
(expect pdf.info[:ModDate]).not_to be_nil
(expect pdf.info[:ModDate]).to start_with 'D:20190115000000'
(expect pdf.info[:CreationDate]).not_to be_nil
(expect pdf.info[:CreationDate]).to start_with 'D:20190115000000'
end
it 'should set date attributes to same current date when date attributes cannot be parsed' do
pdf = to_pdf 'content', attribute_overrides: { 'docdatetime' => 'garbage', 'localdatetime' => 'garbage' }
(expect pdf.info[:ModDate]).not_to be_nil
(expect pdf.info[:ModDate]).to start_with 'D:'
(expect pdf.info[:CreationDate]).not_to be_nil
(expect pdf.info[:CreationDate]).to start_with 'D:'
(expect pdf.info[:ModDate]).to eql pdf.info[:CreationDate]
end
it 'should use current date as fallback when docdatetime cannot be parsed' do
expected = (Time.now.strftime '%s').to_i
pdf = to_pdf 'content', attribute_overrides: { 'docdatetime' => 'garbage' }
mod_date = pdf.info[:ModDate]
(expect mod_date).not_to be_nil
(expect mod_date).to start_with 'D:'
actual = ((DateTime.parse (mod_date.slice 2, mod_date.length).gsub ?', '').strftime '%s').to_i
(expect actual).to be_within(1).of(expected)
end
it 'should use current date as fallback when localdatetime cannot be parsed' do
expected = (Time.now.strftime '%s').to_i
pdf = to_pdf 'content', attribute_overrides: { 'localdatetime' => 'garbage' }
creation_date = pdf.info[:CreationDate]
(expect creation_date).not_to be_nil
(expect creation_date).to start_with 'D:'
actual = ((DateTime.parse (creation_date.slice 2, creation_date.length).gsub ?', '').strftime '%s').to_i
(expect actual).to be_within(1).of(expected)
end
it 'should not add dates to document if reproducible attribute is set' do
pdf = to_pdf <<~'EOS', attribute_overrides: { 'reproducible' => '' }
= Document Title
Author Name
content
EOS
(expect pdf.info[:ModDate]).to be_nil
(expect pdf.info[:CreationDate]).to be_nil
end
it 'should not add software versions to document if reproducible attribute is set' do
pdf = to_pdf <<~'EOS', attribute_overrides: { 'reproducible' => '' }
= Document Title
Author Name
content
EOS
(expect pdf.info[:Creator]).to eql 'Asciidoctor PDF, based on Prawn'
end
it 'should set mod and creation dates to match SOURCE_DATE_EPOCH environment variable' do
old_source_date_epoch = ENV.delete 'SOURCE_DATE_EPOCH'
ENV['SOURCE_DATE_EPOCH'] = '1234123412'
pdf = to_pdf 'content'
(expect pdf.info[:ModDate]).to eql 'D:20090208200332+00\'00\''
(expect pdf.info[:CreationDate]).to eql 'D:20090208200332+00\'00\''
ensure
if old_source_date_epoch
ENV['SOURCE_DATE_EPOCH'] = old_source_date_epoch
else
ENV.delete 'SOURCE_DATE_EPOCH'
end
end
end
context 'document title' do
it 'should set Title field to value of untitled-label attribute if doctitle is not set' do
pdf = to_pdf 'body'
(expect pdf.info[:Title]).to eql 'Untitled'
end
it 'should not set Title field if untitled-label attribute is unset and doctitle is not set' do
pdf = to_pdf 'body', attribute_overrides: { 'untitled-label' => nil }
(expect pdf.info).not_to have_key :Title
end
it 'should set Title field to value of document title if set' do
pdf = to_pdf '= Document Title'
(expect pdf.info[:Title]).to eql 'Document Title'
end
it 'should remove text formatting from document title before assigning to Title field' do
pdf = to_pdf '= *Document* _Title_'
(expect pdf.info[:Title]).to eql 'Document Title'
end
it 'should decode character references in document title before assigning to Title field' do
pdf = to_pdf '= ACME(TM) Catalog <№ 1>'
(expect pdf.info[:Title]).to eql %(ACME\u2122 Catalog <\u2116 1>)
end
it 'should hex encode non-ASCII characters in Title field' do
doctitle = 'Guide de démarrage rapide'
pdf = to_pdf %(= #{doctitle})
(expect pdf.info[:Title]).to eql doctitle
encoded_doctitle = pdf.objects[pdf.objects.trailer[:Info]][:Title].unpack 'H*'
(expect encoded_doctitle).to eql (doctitle.encode Encoding::UTF_16).unpack 'H*'
end
end
context 'compress' do
it 'should not compress streams by default' do
pdf = to_pdf 'foobar'
objects = pdf.objects
pages = pdf.objects.values.find {|it| Hash === it && it[:Type] == :Pages }
stream = objects[objects[pages[:Kids][0]][:Contents]]
(expect stream.hash[:Filter]).to be_nil
(expect stream.data).to include '/DeviceRGB'
end
it 'should compress streams if compress attribute is set on document' do
pdf = to_pdf 'foobar', attribute_overrides: { 'compress' => '' }
objects = pdf.objects
pages = pdf.objects.values.find {|it| Hash === it && it[:Type] == :Pages }
stream = objects[objects[pages[:Kids][0]][:Contents]]
(expect stream.hash[:Filter]).to eql [:FlateDecode]
(expect stream.data).not_to include '/DeviceRGB'
end
it 'should not compress streams when compress attribute is set on document and page is imported' do
pdf = to_pdf <<~'EOS', attribute_overrides: { 'compress' => '' }
before
image::red-green-blue.pdf[page=1]
after
EOS
objects = pdf.objects
pages = pdf.objects.values.find {|it| Hash === it && it[:Type] == :Pages }
objects[pages[:Kids][1]][:Contents].map {|it| objects[it] }.each do |stream|
(expect stream.hash[:Filter]).to be_nil
(expect stream.data).to include '/DeviceRGB'
end
stream = objects[objects[pages[:Kids][0]][:Contents]]
(expect stream.hash[:Filter]).to be_nil
(expect stream.data).to include '/DeviceRGB'
stream = objects[objects[pages[:Kids][2]][:Contents]]
(expect stream.hash[:Filter]).to be_nil
(expect stream.data).to include '/DeviceRGB'
end
end
end
|