Skip to content

Commit

Permalink
resolves asciidoctor#1204 use consistent line height for list items a…
Browse files Browse the repository at this point in the history
…nd toc entries if text is entirely monospace
  • Loading branch information
mojavelinux committed Sep 6, 2019
1 parent 853a648 commit 7470099
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[

== Unreleased

* Use consistent line height for list items and toc entries if text is entirely monospace (#1204)
* Fix spacing between items in qanda list

== 1.5.0.beta.4 (2019-09-04) - @mojavelinux
Expand Down
Binary file modified examples/chronicles-example.pdf
Binary file not shown.
18 changes: 10 additions & 8 deletions lib/asciidoctor-pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ def convert_colist_item node
end

indent marker_width do
convert_content_for_list_item node, :colist, margin_bottom: @theme.outline_list_item_spacing
convert_content_for_list_item node, :colist, margin_bottom: @theme.outline_list_item_spacing, normalize_line_height: true
end
end

Expand Down Expand Up @@ -1113,10 +1113,10 @@ def convert_dlist node
else
term_text = term.text
end
layout_prose term_text, margin_top: 0, margin_bottom: @theme.description_list_term_spacing, align: :left
layout_prose term_text, margin_top: 0, margin_bottom: @theme.description_list_term_spacing, align: :left, normalize_line_height: true
end
indent(@theme.description_list_description_indent || 0) do
convert_content_for_list_item desc, :dlist_desc
convert_content_for_list_item desc, :dlist_desc, normalize_line_height: true
end if desc
end
end
Expand Down Expand Up @@ -1313,9 +1313,9 @@ def convert_outline_list_item node, list, opts = {}
end

if complex
convert_content_for_list_item node, list_type, opts
convert_content_for_list_item node, list_type, (opts.merge normalize_line_height: true)
else
convert_content_for_list_item node, list_type, (opts.merge margin_bottom: @theme.outline_list_item_spacing)
convert_content_for_list_item node, list_type, (opts.merge margin_bottom: @theme.outline_list_item_spacing, normalize_line_height: true)
end
end

Expand Down Expand Up @@ -2236,7 +2236,7 @@ def convert_index_list_item term
end
text = %(#{text}, #{pagenums.join ', '})
end
layout_prose text, align: :left, margin: 0
layout_prose text, align: :left, margin: 0, normalize_line_height: true

term.subterms.each do |subterm|
indent @theme.description_list_description_indent do
Expand Down Expand Up @@ -2700,6 +2700,7 @@ def layout_prose string, opts = {}
string = %(<a anchor="#{anchor}">#{string}</a>)
end
margin_top top_margin
string = ZeroWidthSpace + string if opts.delete :normalize_line_height
typeset_text string, calc_line_metrics((opts.delete :line_height) || @theme.base_line_height), {
color: @font_color,
# NOTE normalize makes endlines soft (replaces "\n" with ' ')
Expand Down Expand Up @@ -2748,7 +2749,8 @@ def layout_caption subject, opts = {}
margin_top: margin[:top],
margin_bottom: margin[:bottom],
align: (@theme.caption_align || @base_align).to_sym,
normalize: false
normalize: false,
normalize_line_height: true
}.merge(opts)
if side == :top && @theme.caption_border_bottom_color
stroke_horizontal_rule @theme.caption_border_bottom_color
Expand Down Expand Up @@ -2830,7 +2832,7 @@ def layout_toc_level sections, num_levels, line_metrics, dot_leader, num_front_m
end
sections.each do |sect|
theme_font :toc, level: (sect.level + 1) do
sect_title = @text_transform ? (transform_text sect.numbered_title, @text_transform) : sect.numbered_title
sect_title = ZeroWidthSpace + (@text_transform ? (transform_text sect.numbered_title, @text_transform) : sect.numbered_title)
# NOTE only write section title (excluding dots and page number) if this is a dry run
if scratch?
# FIXME use layout_prose
Expand Down
28 changes: 28 additions & 0 deletions spec/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@
(expect pdf.lines).to eql [%(\u25cadiamond), %(\u25ccdotted circle), %(\u25a1white square)]
end

it 'should use consistent line height even if list item is entirely monospace' do
pdf = to_pdf <<~'EOS', analyze: true
* foo
* `mono`
* bar
EOS

mark_texts = pdf.find_text '•'
(expect mark_texts).to have_size 3
first_to_second_spacing = (mark_texts[0][:y] - mark_texts[1][:y]).round 2
second_to_third_spacing = (mark_texts[1][:y] - mark_texts[2][:y]).round 2
(expect first_to_second_spacing).to eql second_to_third_spacing
end

it 'should apply correct margin if primary text of list item is blank' do
pdf = to_pdf <<~'EOS', analyze: true
* foo
Expand Down Expand Up @@ -188,6 +202,20 @@
(expect pdf.lines).to eql ['i.one', 'ii.two', 'iii.three']
end

it 'should use consistent line height even if list item is entirely monospace' do
pdf = to_pdf <<~'EOS', analyze: true
. foo
. `mono`
. bar
EOS

mark_texts = pdf.text.select {|it| it[:string].end_with? '.' }
(expect mark_texts).to have_size 3
first_to_second_spacing = (mark_texts[0][:y] - mark_texts[1][:y]).round 2
second_to_third_spacing = (mark_texts[1][:y] - mark_texts[2][:y]).round 2
(expect first_to_second_spacing).to eql second_to_third_spacing
end

it 'should align list numbers to right and extend towards left margin' do
pdf = to_pdf <<~'EOS', analyze: true
. one
Expand Down
20 changes: 20 additions & 0 deletions spec/toc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@
(expect pdf.find_text string: 'Introduction', page_number: 3).not_to be_empty
end

it 'should space items in toc evently even if title is entirely monospace' do
pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
= Document Title
:toc:
== Beginning
== `Middle`
== End
EOS
(expect pdf.find_text string: 'Table of Contents', page_number: 2).not_to be_empty
beginning_pagenum_text = (pdf.find_text string: '1', page_number: 2)[0]
middle_pagenum_text = (pdf.find_text string: '2', page_number: 2)[0]
end_pagenum_text = (pdf.find_text string: '3', page_number: 2)[0]
beginning_to_middle_spacing = (beginning_pagenum_text[:y] - middle_pagenum_text[:y]).round 2
middle_to_end_spacing = (middle_pagenum_text[:y] - end_pagenum_text[:y]).round 2
(expect beginning_to_middle_spacing).to eql middle_to_end_spacing
end

it 'should only include preface in toc if preface-title is set' do
input = <<~'EOS'
= Document Title
Expand Down

0 comments on commit 7470099

Please sign in to comment.