Skip to content

Commit

Permalink
resolves #2248 apply page layout from main document to new page in sc…
Browse files Browse the repository at this point in the history
…ratch document (PR #2249)
  • Loading branch information
mojavelinux authored Jun 17, 2022
1 parent 1974870 commit 3a7c783
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ For a detailed view of what has changed, refer to the {url-repo}/commits/main[co

Bug Fixes::

* apply page layout from main document to new page in scratch document (#2248)
* use correct logic to insert page before TOC with automatic placement when doctype=book and media=prepress
* use `get_entries_for_toc` to determine if the TOC is non-empty rather than `Document#sections?`

Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor/pdf/ext/prawn/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ def tare_first_page_content_stream
#
# Returns an Extent or ScratchExtent object that describes the bounds of the content block.
def dry_run keep_together: nil, pages_advanced: 0, single_page: nil, onto: nil, &block
(scratch_pdf = scratch).start_new_page
(scratch_pdf = scratch).start_new_page layout: page.layout
saved_bounds = scratch_pdf.bounds
scratch_pdf.bounds = bounds.dup.tap do |bounds_copy|
bounds_copy.instance_variable_set :@document, scratch_pdf
Expand Down
103 changes: 103 additions & 0 deletions spec/arrange_block_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,37 @@ def content
(expect image[:page_number]).to be 2
(expect image[:y]).to eql 742.0
end

it 'should split block across pages that starts at top of rotated page' do
pdf_theme.update \
code_border_width: [1, 0],
code_border_color: '0000FF',
code_border_radius: 0,
code_background_color: 'transparent'
block_content = ['block content with very long lines that do not wrap because the page layout is rotated to landscape'] * 20 * %(\n\n)
input = <<~EOS
first page
[page-layout=landscape]
<<<
....
#{block_content}
....
EOS

block_lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines.select {|it| it[:color] == '0000FF' }
pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
(expect pdf.pages).to have_size 3
block_text = pdf.find_text %r/very long lines/
(expect block_lines[0][:from][:y]).to eql (pdf.pages[1][:size][1] - 50).to_f
(expect block_lines[0][:page_number]).to eql 2
(expect block_lines[0][:from][:y]).to be > block_text[0][:y]
(expect (block_lines[0][:from][:y] - (block_text[0][:y] + block_text[0][:font_size]))).to be < 12
(expect block_lines[-1][:page_number]).to eql 3
(expect block_lines[-1][:from][:y]).to be < block_text[-1][:y]
(expect block_text[-1][:y] - block_lines[-1][:from][:y]).to be < 14
end
end

describe 'below top' do
Expand Down Expand Up @@ -1364,6 +1395,78 @@ def content
(expect block_title[:page_number]).to be 1
(expect (pdf.find_unique_text 'content')[:page_number]).to be 2
end

it 'should split block across pages that starts partway down rotated page' do
pdf_theme.update \
code_border_width: [1, 0],
code_border_color: '0000FF',
code_border_radius: 0,
code_background_color: 'transparent'
before_block_content = ['before block'] * 15 * %(\n\n)
block_content = ['block content with very long lines that do not wrap because the page layout is rotated to landscape'] * 15 * %(\n\n)
input = <<~EOS
first page
[page-layout=landscape]
<<<
#{before_block_content}
....
#{block_content}
....
EOS

block_lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines.select {|it| it[:color] == '0000FF' }
pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
(expect pdf.pages).to have_size 3
block_text = pdf.find_text %r/very long lines/
(expect block_lines[0][:page_number]).to eql 2
(expect block_lines[0][:from][:y]).to be > block_text[0][:y]
(expect (block_lines[0][:from][:y] - (block_text[0][:y] + block_text[0][:font_size]))).to be < 12
(expect block_lines[-1][:page_number]).to eql 3
(expect block_lines[-1][:from][:y]).to be < block_text[-1][:y]
(expect block_text[-1][:y] - block_lines[-1][:from][:y]).to be < 14
end

it 'should restore page layout in scratch document after it has been toggled in main document' do
pdf_theme.update \
code_border_width: [1, 0],
code_border_color: '0000FF',
code_border_radius: 0,
code_background_color: 'transparent'
before_block_content = ['before block'] * 15 * %(\n\n)
block_content = ['block content with very long lines that wrap because the page layout is not rotated to landscape'] * 15 * %(\n\n)
input = <<~EOS
first page
[page-layout=landscape]
<<<
rotated page
[page-layout=portrait]
<<<
#{before_block_content}
....
#{block_content}
....
EOS

block_lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines.select {|it| it[:color] == '0000FF' }
pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
(expect pdf.pages).to have_size 4
block_text = pdf.find_text %r/very long lines/
(expect block_lines[0][:page_number]).to eql 3
(expect block_lines[0][:from][:y]).to be > block_text[0][:y]
(expect (block_lines[0][:from][:y] - (block_text[0][:y] + block_text[0][:font_size]))).to be < 12
block_text = pdf.find_text %r/landscape/
(expect block_lines[-1][:page_number]).to eql 4
(expect block_lines[-1][:from][:y]).to be < block_text[-1][:y]
(expect block_text[-1][:y] - block_lines[-1][:from][:y]).to be < 14
end
end
end

Expand Down

0 comments on commit 3a7c783

Please sign in to comment.