Skip to content

Commit

Permalink
resolves #2150 coerce image-caption-max-width to fit-content if float…
Browse files Browse the repository at this point in the history
… attribute is set on block image (PR #2160)
  • Loading branch information
mojavelinux committed May 12, 2022
1 parent 023dbcb commit ba5e288
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Enhancements::
* add support for orphan avoidance to discrete headings to match behavior of section titles (using call to `arrange_heading`) (#2151)
* rename `arrange_section` to `arrange_heading` to reflect proper terminology and purpose
* add `index-column-gap` key to theme to control size of gap between columns
* coerce `image-caption-max-width` to `fit-content` if `float` attribute is set on block image (#2150)

Bug Fixes::

Expand Down
1 change: 1 addition & 0 deletions lib/asciidoctor/pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,7 @@ def convert_image node, opts = {}

caption_end = @theme.image_caption_end&.to_sym || :bottom
caption_max_width = @theme.image_caption_max_width
caption_max_width = 'fit-content' if (node.attr? 'float') && !(caption_max_width&.start_with? 'fit-content')
# NOTE: if width is not set explicitly and max-width is fit-content, caption height may not be accurate
caption_h = node.title? ? (ink_caption node, category: :image, end: caption_end, block_align: alignment, block_width: width, max_width: caption_max_width, dry_run: true, force_top_margin: caption_end == :bottom) : 0

Expand Down
40 changes: 40 additions & 0 deletions spec/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,46 @@
(expect caption_text_l1[:x] + caption_text_l1[:width]).to be_within(1).of caption_text_l2[:x] + caption_text_l2[:width]
end

it 'should configure caption width to fit image width if float attribute is set on image' do
input = <<~'EOS'
.This is a picture of our beloved Tux.
image::tux.png[align=right,float=right]
EOS

tux_image = (to_pdf input, analyze: :image).images[0]
pdf = to_pdf input, analyze: true
caption_texts = pdf.text
(expect caption_texts).to have_size 2
caption_text_l1, caption_text_l2 = caption_texts
(expect caption_text_l1[:y]).to be > caption_text_l2[:y]
(expect caption_text_l1[:string]).to start_with 'Figure 1.'
(expect caption_text_l1[:width]).to be < tux_image[:width]
(expect caption_text_l2[:width]).to be < tux_image[:width]
(expect caption_text_l1[:x]).to eql tux_image[:x]
(expect caption_text_l2[:x]).to eql caption_text_l1[:x]
end

it 'should not change caption width if float attribute is set on image and caption max width is fit-content' do
pdf_theme = { image_caption_align: 'inherit', image_caption_max_width: 'fit-content(50%)' }
input = <<~'EOS'
.This is a picture of our beloved Tux.
image::tux.png[align=right,float=right]
EOS

tux_image = (to_pdf input, pdf_theme: pdf_theme, analyze: :image).images[0]
pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
caption_texts = pdf.text
(expect caption_texts).to have_size 3
caption_text_l1, caption_text_l2, caption_text_l3 = caption_texts
(expect caption_text_l1[:y]).to be > caption_text_l2[:y]
(expect caption_text_l2[:y]).to be > caption_text_l3[:y]
(expect caption_text_l1[:string]).to start_with 'Figure 1.'
(expect caption_text_l1[:width]).to be < (tux_image[:width] * 0.5)
(expect caption_text_l2[:width]).to be < (tux_image[:width] * 0.5)
(expect caption_text_l3[:width]).to be < (tux_image[:width] * 0.5)
(expect caption_text_l1[:x]).to be > (tux_image[:x] + tux_image[:width] * 0.5)
end

it 'should restrict caption width to percentage of image width if max-width is fit-content function' do
pdf_theme = {
image_caption_align: 'inherit',
Expand Down

0 comments on commit ba5e288

Please sign in to comment.