summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2022-05-11 16:57:52 -0600
committerGitHub <noreply@github.com>2022-05-11 16:57:52 -0600
commit6a91de0db46c2715527140c70c0a9e17df7c358a (patch)
treedf567917bd28568d7b57847416a7c1aa46699f30
parenta0e2297b43d31b6ae26967342dc40a9631630ac5 (diff)
resolves #2156 honor caption-align when element align is not left and caption-max-width is % of element width (PR #2157)
-rw-r--r--CHANGELOG.adoc1
-rw-r--r--lib/asciidoctor/pdf/converter.rb26
-rw-r--r--spec/image_spec.rb33
3 files changed, 53 insertions, 7 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 4410d495..f3ea8f8d 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -17,6 +17,7 @@ Enhancements::
Bug Fixes::
+* honor `caption-align` when element align is not `left` and `caption-max-width` is % of element width (e.g., `fit-content(50%)`) (#2156)
* allow theme to set font style of first line of abstract to `normal_italic` (#2138)
* add support for `:color` option to `Prawn::Text::Formatted::Box` directly and remove workarounds
* preserve columns on subsequent pages in the index section (#2149)
diff --git a/lib/asciidoctor/pdf/converter.rb b/lib/asciidoctor/pdf/converter.rb
index 56c4cb26..5a9c1b95 100644
--- a/lib/asciidoctor/pdf/converter.rb
+++ b/lib/asciidoctor/pdf/converter.rb
@@ -3119,11 +3119,22 @@ module Asciidoctor
if (max_width = opts.delete :max_width) && max_width != 'none'
if ::String === max_width
if max_width.start_with? 'fit-content'
- if max_width.end_with? 't', '()'
- max_width = block_width || container_width
- else
- max_width = (block_width || container_width) * (max_width.slice 12, max_width.length - 1).to_f / 100.0
+ block_width ||= container_width
+ unless max_width.end_with? 't', '()'
+ max_width = block_width * (max_width.slice 12, max_width.length - 1).to_f / 100.0
+ if (caption_width_delta = block_width - max_width) > 0
+ case align
+ when :right
+ indent_by[0] += caption_width_delta
+ when :center
+ indent_by[0] += caption_width_delta * 0.5
+ indent_by[1] += caption_width_delta * 0.5
+ else # :left, nil
+ indent_by[1] += caption_width_delta
+ end
+ end
end
+ max_width = block_width
elsif max_width.end_with? '%'
max_width = [max_width.to_f / 100 * bounds.width, bounds.width].min
block_align = align
@@ -3138,11 +3149,12 @@ module Asciidoctor
if (remainder = container_width - max_width) > 0
case block_align
when :right
- indent_by = [remainder, 0]
+ indent_by[0] += remainder
when :center
- indent_by = [(side_margin = remainder * 0.5), side_margin]
+ indent_by[0] += remainder * 0.5
+ indent_by[1] += remainder * 0.5
else # :left, nil
- indent_by = [0, remainder]
+ indent_by[1] += remainder
end
end
end
diff --git a/spec/image_spec.rb b/spec/image_spec.rb
index 0b57e273..704a2211 100644
--- a/spec/image_spec.rb
+++ b/spec/image_spec.rb
@@ -2059,6 +2059,39 @@ describe 'Asciidoctor::PDF::Converter - Image' do
(expect caption_text_l1[:x]).to eql tux_image[:x]
(expect caption_text_l2[:x]).to eql caption_text_l1[:x]
end
+
+ it 'should align caption within width of image if alignment is fixed and max-width is percentage of image width' do
+ pdf_theme = {
+ image_caption_align: 'left',
+ image_caption_max_width: 'fit-content(50%)',
+ }
+
+ input = <<~'EOS'
+ .This is a picture of our beloved Tux.
+ image::tux.png[align=right]
+ EOS
+
+ pdf = to_pdf input, pdf_theme: pdf_theme, analyze: :image
+
+ images = pdf.images
+ (expect images).to have_size 1
+ tux_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 eql tux_image[:x]
+ (expect caption_text_l2[:x]).to eql caption_text_l1[:x]
+ (expect caption_text_l3[:x]).to eql caption_text_l2[:x]
+ end
end
context 'Border' do