diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2022-04-19 04:26:12 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-19 04:26:12 -0600 |
| commit | 2e28527af18791e92cf22ef77ac8f1a51e6eda0e (patch) | |
| tree | 6894489970a0120da3bc3684b4380d3e4d1e980b | |
| parent | a013ea45e7ed69250887782747259b7e803093d5 (diff) | |
resolves #1918 prepress page margins should honor value of pdf-folio-placement (PR #2058)
| -rw-r--r-- | CHANGELOG.adoc | 1 | ||||
| -rw-r--r-- | lib/asciidoctor/pdf/converter.rb | 39 | ||||
| -rw-r--r-- | spec/page_spec.rb | 49 |
3 files changed, 70 insertions, 19 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index f167d758..a3277d1b 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -193,6 +193,7 @@ Bug Fixes:: * don't render borders and backgrounds in scratch document * don't insert blank page after document title if first block (chapter or toc macro) has nonfacing option (#1988) * coerce negated variable reference to number if value of variable is numeric +* prepress page margins should honor value of `pdf-folio-placement` (#1918) Compliance:: diff --git a/lib/asciidoctor/pdf/converter.rb b/lib/asciidoctor/pdf/converter.rb index 38fd9747..dbcef793 100644 --- a/lib/asciidoctor/pdf/converter.rb +++ b/lib/asciidoctor/pdf/converter.rb @@ -354,8 +354,19 @@ module Asciidoctor #@page_opts = { size: pdf_opts[:page_size], layout: pdf_opts[:page_layout] } ((::Prawn::Document.instance_method :initialize).bind self).call pdf_opts renderer.min_version (@pdf_version = PDFVersions[doc.attr 'pdf-version']) + @media = doc.attr 'media', 'screen' @page_margin_by_side = { recto: (page_margin_recto = page_margin), verso: (page_margin_verso = page_margin), cover: page_margin } - if (@media = doc.attr 'media', 'screen') == 'prepress' + case doc.attr 'pdf-folio-placement', (@media == 'prepress' ? 'physical' : 'virtual') + when 'physical' + @folio_placement = { basis: :physical } + when 'physical-inverted' + @folio_placement = { basis: :physical, inverted: true } + when 'virtual-inverted' + @folio_placement = { basis: :virtual, inverted: true } + else + @folio_placement = { basis: :virtual } + end + if @media == 'prepress' @ppbook = doc.doctype == 'book' if (page_margin_outer = theme.page_margin_outer) page_margin_recto[1] = page_margin_verso[3] = page_margin_outer @@ -605,15 +616,15 @@ module Asciidoctor # NOTE: init_page is called within a float context; this will suppress prawn-svg messing with the cursor # NOTE: init_page is not called for imported pages, front and back cover pages, and other image pages def init_page *_args - # NOTE: we assume in prepress that physical page number reflects page side - if @media == 'prepress' && (next_page_margin = @page_margin_by_side[page_number == 1 ? :cover : page_side]) != page_margin + next_page_side = page_side nil, @folio_placement[:inverted] + if @media == 'prepress' && (next_page_margin = @page_margin_by_side[page_number == 1 ? :cover : next_page_side]) != page_margin set_page_margin next_page_margin end unless @page_bg_color == 'FFFFFF' tare = true fill_absolute_bounds @page_bg_color end - if (bg_image_path, bg_image_opts = @page_bg_image[page_side]) + if (bg_image_path, bg_image_opts = @page_bg_image[next_page_side]) tare = true begin if bg_image_opts[:format] == 'pdf' @@ -624,9 +635,9 @@ module Asciidoctor canvas { image bg_image_path, ({ position: :center, vposition: :center }.merge bg_image_opts) } end rescue - facing_page_side = (PageSides - [page_side])[0] - @page_bg_image[facing_page_side] = nil if @page_bg_image[facing_page_side] == @page_bg_image[page_side] - @page_bg_image[page_side] = nil + facing_page_side = (PageSides - [next_page_side])[0] + @page_bg_image[facing_page_side] = nil if @page_bg_image[facing_page_side] == @page_bg_image[next_page_side] + @page_bg_image[next_page_side] = nil log :warn, %(could not embed page background image: #{bg_image_path}; #{$!.message}) end end @@ -2696,7 +2707,7 @@ module Asciidoctor start_new_page end - side = recycle ? page_side : (page_side page_number + 1) + side = page_side (recycle ? nil : page_number + 1), @folio_placement[:inverted] prev_bg_image = @page_bg_image[side] prev_bg_color = @page_bg_color if (bg_image = resolve_background_image doc, @theme, 'title-page-background-image') @@ -3313,16 +3324,6 @@ module Asciidoctor doc.set_attr 'page-count', (num_pages - skip_pagenums) pagenums_enabled = doc.attr? 'pagenums' - case doc.attr 'pdf-folio-placement', (@media == 'prepress' ? 'physical' : 'virtual') - when 'physical' - folio_basis, invert_folio = :physical, false - when 'physical-inverted' - folio_basis, invert_folio = :physical, true - when 'virtual-inverted' - folio_basis, invert_folio = :virtual, true - else - folio_basis, invert_folio = :virtual, false - end periphery_layout_cache = {} # NOTE: this block is invoked during PDF generation, after #write -> #render_file and thus after #convert_document repeat (content_start_page..num_pages), dynamic: true do @@ -3331,7 +3332,7 @@ module Asciidoctor next if page.imported_page? || (disable_on_pages.include? pgnum) virtual_pgnum = pgnum - skip_pagenums pgnum_label = (virtual_pgnum < 1 ? (RomanNumeral.new pgnum, :lower) : virtual_pgnum).to_s - side = page_side((folio_basis == :physical ? pgnum : virtual_pgnum), invert_folio) + side = page_side((@folio_placement[:basis] == :physical ? pgnum : virtual_pgnum), @folio_placement[:inverted]) doc.set_attr 'page-layout', page.layout.to_s # NOTE: running content is cached per page layout diff --git a/spec/page_spec.rb b/spec/page_spec.rb index 80b87627..baa66ca4 100644 --- a/spec/page_spec.rb +++ b/spec/page_spec.rb @@ -487,6 +487,55 @@ describe 'Asciidoctor::PDF::Converter - Page' do (expect to_file).to visually_match 'page-prepress-margins-body-only.pdf' end + + it 'should invert recto/verso margins when pdf-folio-placement is inverted' do + pdf_theme = { + page_margin_inner: 72, + page_margin_outer: 54, + footer_recto_right_content: nil, + footer_recto_left_content: 'page {page-number}', + footer_verso_right_content: nil, + footer_verso_left_content: 'p{page-number}', + footer_padding: [6, 0, 0, 0], + } + pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, enable_footer: true, analyze: true + = Book Title + :media: prepress + :doctype: book + :pdf-folio-placement: physical-inverted + + == First Chapter + + content + + <<< + + more content + + == Last Chapter + + content + EOS + + first_chapter_text = pdf.find_unique_text 'First Chapter' + (expect first_chapter_text[:page_number]).to eql 3 + (expect first_chapter_text[:x]).to eql 54.0 + pgnum_1_text = pdf.find_unique_text 'p1' + (expect pgnum_1_text[:x]).to eql 54.0 + (expect pgnum_1_text[:page_number]).to eql 3 + more_content_text = pdf.find_unique_text 'more content' + (expect more_content_text[:x]).to eql 72.0 + (expect more_content_text[:page_number]).to eql 4 + pgnum_2_text = pdf.find_unique_text 'page 2' + (expect pgnum_2_text[:x]).to eql 72.0 + (expect pgnum_2_text[:page_number]).to eql 4 + last_chapter_text = pdf.find_unique_text 'Last Chapter' + (expect last_chapter_text[:x]).to eql 54.0 + (expect last_chapter_text[:page_number]).to eql 5 + pgnum_3_text = pdf.find_unique_text 'p3' + (expect pgnum_3_text[:x]).to eql 54.0 + (expect pgnum_3_text[:page_number]).to eql 5 + end end context 'Background' do |
