Skip to content

Commit

Permalink
resolves #1533 place dots on correct page when section title in TOC w…
Browse files Browse the repository at this point in the history
…raps across a page boundary (PR #1534)
  • Loading branch information
mojavelinux authored Feb 2, 2020
1 parent 687e8e1 commit 36dc014
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
* allow font path to be declared once for all font styles (#1507)
* continue border, background, and column rule of admonition block on subsequent pages when block gets split (#1287)
* allow max-width on caption be specified as a percentage (of the container width) (#1484)
* place dots on correct page when section title in TOC wraps across a page boundary (#1533)
* add destination to top of imported PDF if ID is specified on image block
* log reason if theme file cannot be parsed or compiled (#1491)
* bundle emoji font and use as fallback in default-with-fallback-font theme (#1129)
Expand Down
6 changes: 5 additions & 1 deletion lib/asciidoctor/pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3027,7 +3027,11 @@ def layout_toc_level sections, num_levels, line_metrics, dot_leader, num_front_m
start_dots = last_fragment_pos.right + hanging_indent
last_fragment_cursor = last_fragment_pos.top + line_metrics.padding_top
# NOTE this will be incorrect if wrapped line is all monospace
start_cursor = last_fragment_cursor if start_cursor - last_fragment_cursor > line_metrics.height
if (last_fragment_page_number = last_fragment_pos.page_number) > start_page_number ||
(start_cursor - last_fragment_cursor) > line_metrics.height
start_page_number = last_fragment_page_number
start_cursor = last_fragment_cursor
end
end
end_page_number = page_number
end_cursor = cursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

module Asciidoctor::PDF::FormattedText
class FragmentPositionRenderer
attr_reader :top, :right, :bottom, :left
attr_reader :top, :right, :bottom, :left, :page_number

def render_behind fragment
@top = fragment.top
@right = (@left = fragment.left) + fragment.width
@bottom = fragment.bottom
@page_number = fragment.document.page_number
end
end
end
20 changes: 20 additions & 0 deletions spec/toc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,26 @@
(expect page_number_text).to have_size 1
end

it 'should line up dots and page number with wrapped line when section title gets split across a page boundary' do
sections = (1..37).map {|num| %(\n\n== Section #{num}) }.join
pdf = to_pdf <<~EOS, doctype: :book, analyze: true
= Document Title
:toc:
#{sections}
== This is a unbelievably long section title that probably shouldn't be a section title at all but here we are
content
EOS

page_2_lines = pdf.lines pdf.find_text page_number: 2
(expect page_2_lines).to include 'Table of Contents'
(expect page_2_lines[-1]).to end_with 'but here'
page_3_lines = pdf.lines pdf.find_text page_number: 3
(expect page_3_lines).to have_size 1
(expect page_3_lines[0]).to match %r/we are(\. )+.*38$/
end

it 'should allow hanging indent to be applied to lines that wrap' do
pdf = to_pdf <<~'EOS', doctype: :book, pdf_theme: { toc_hanging_indent: 36 }, analyze: true
= Document Title
Expand Down

0 comments on commit 36dc014

Please sign in to comment.