File: media_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 (189 lines) | stat: -rw-r--r-- 5,930 bytes parent folder | download
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
# frozen_string_literal: true

require_relative 'spec_helper'

describe 'Asciidoctor::PDF::Converter - media' do
  context 'print' do
    it 'should use default-for-print theme if media is print and theme is not specified' do
      input = 'worth #marking#'
      text = (to_pdf input, attribute_overrides: { 'media' => 'print' }, analyze: true).text
      (expect text[0][:font_color]).to eql '000000'
      rects = (to_pdf input, attribute_overrides: { 'media' => 'print' }, analyze: :rect).rectangles
      (expect rects).to have_size 1
      (expect rects[0][:fill_color]).to eql 'CCCCCC'
    end
  end

  context 'prepress' do
    it 'should use default-for-print theme if media is prepress and theme is not specified' do
      input = 'worth #marking#'
      text = (to_pdf input, attribute_overrides: { 'media' => 'prepress' }, analyze: true).text
      (expect text[0][:font_color]).to eql '000000'
      rects = (to_pdf input, attribute_overrides: { 'media' => 'prepress' }, analyze: :rect).rectangles
      (expect rects).to have_size 1
      (expect rects[0][:fill_color]).to eql 'CCCCCC'
    end

    it 'should leave blank page after image cover page' do
      pdf = to_pdf <<~EOS
      = Document Title
      :doctype: book
      :media: prepress
      :front-cover-image: #{fixture_file 'cover.jpg', relative: true}

      == Chapter Title
      EOS

      (expect pdf.pages).to have_size 5
      (expect (pdf.page 3).text).to eql 'Document Title'
      (expect (pdf.page 5).text).to eql 'Chapter Title'
      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 leave blank page after PDF cover page' do
      pdf = to_pdf <<~EOS
      = Document Title
      :doctype: book
      :media: prepress
      :front-cover-image: #{fixture_file 'blue-letter.pdf', relative: true}

      == Chapter Title
      EOS

      (expect pdf.pages).to have_size 5
      (expect (pdf.page 1).text).to be_empty
      (expect (pdf.page 3).text).to eql 'Document Title'
      (expect (pdf.page 5).text).to eql 'Chapter Title'
      # TODO: add helper method to get content stream for page
      title_page_contents = pdf.objects[(pdf.page 1).page_object[:Contents][0]].data
      (expect (title_page_contents.split ?\n).slice 0, 3).to eql ['q', '/DeviceRGB cs', '0.0 0.0 1.0 scn']
    end

    it 'should insert blank page after TOC' do
      pdf = to_pdf <<~EOS, analyze: true
      = Document Title
      :doctype: book
      :media: prepress
      :toc:
      :front-cover-image: #{fixture_file 'cover.jpg', relative: true}

      == Beginning

      == Middle

      == End
      EOS

      (expect pdf.pages).to have_size 11
      (expect (pdf.find_text 'Document Title')[0][:page_number]).to be 3
      (expect (pdf.find_text 'Table of Contents')[0][:page_number]).to be 5
      (expect (pdf.find_text 'Beginning')[0][:page_number]).to be 5
      (expect (pdf.find_text 'Beginning')[1][:page_number]).to be 7
      (expect (pdf.find_text 'Middle')[0][:page_number]).to be 5
      (expect (pdf.find_text 'Middle')[1][:page_number]).to be 9
      (expect (pdf.find_text 'End')[0][:page_number]).to be 5
      (expect (pdf.find_text 'End')[1][:page_number]).to be 11
    end

    it 'should not insert blank page at start of document if document has no cover' do
      pdf = to_pdf <<~'EOS', analyze: true
      = Document Title
      :doctype: book
      :media: prepress

      Preface content.

      == Chapter Title

      Chapter content.
      EOS

      (expect pdf.pages).to have_size 5
      doctitle_text = (pdf.find_text 'Document Title')[0]
      (expect doctitle_text[:page_number]).to be 1
      preface_text = (pdf.find_text 'Preface content.')[0]
      (expect preface_text[:page_number]).to be 3
      chapter_title_text = (pdf.find_text 'Chapter Title')[0]
      (expect chapter_title_text[:page_number]).to be 5
    end

    it 'should not insert blank page at start of document with toc if title page is disabled' do
      pdf = to_pdf <<~'EOS', analyze: true
      = Document Title
      :doctype: book
      :media: prepress
      :notitle:
      :toc:

      == Chapter Title

      Chapter content.
      EOS

      (expect pdf.pages).to have_size 3
      toc_text = (pdf.find_text 'Table of Contents')[0]
      (expect toc_text[:page_number]).to be 1
      chapter_title_texts = pdf.find_text 'Chapter Title'
      (expect chapter_title_texts).to have_size 2
      (expect chapter_title_texts[0][:page_number]).to be 1
      (expect chapter_title_texts[1][:page_number]).to be 3
    end

    it 'should not insert blank page before chapter that follows preamble if chapter has nonfacing option' do
      pdf = to_pdf <<~'EOS', analyze: true
      = Document Title
      :doctype: book
      :media: prepress

      preamble

      [%nonfacing]
      == Chapter
      EOS

      chapter_text = (pdf.find_text 'Chapter')[0]
      (expect chapter_text[:page_number]).to be 4
    end

    it 'should not insert blank page before chapter that follows document title if chapter has nonfacing option' do
      pdf = to_pdf <<~'EOS', analyze: true
      = Document Title
      :doctype: book
      :media: prepress

      [%nonfacing]
      == Chapter
      EOS

      chapter_text = (pdf.find_text 'Chapter')[0]
      (expect chapter_text[:page_number]).to be 2
    end

    it 'should initialize color space on empty verso pages', cli: true do
      to_file = to_pdf_file <<~'EOS', 'running-content-on-empty-verso-pages.pdf', enable_footer: true
      = Document Title
      :doctype: book
      :media: prepress

      == Beginning

      content

      == Middle

      content

      == End

      content
      EOS

      out, err, res = run_command 'pdftotext', to_file, '-'
      (expect res.exitstatus).to be 0
      (expect out).to include '4'
      (expect err).to be_empty
    end
  end
end