summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2022-05-20 03:19:45 -0600
committerDan Allen <dan.j.allen@gmail.com>2022-05-20 14:47:43 -0600
commitb50d46be56b803d66c056838b7ffab51d9d5ea59 (patch)
treeada917162fee4abb0470e0a15edc5970f6d4a157
parent734b06c39f12f77f51952a2eee47eff5ca28087c (diff)
resolves #2193 fix size and position of full height inline image so it fits within available height of page
- always use bounds height as max height for inline image - subtract top padding from line metrics from ascender for full height image - don't protect bottom gutter if line contains a full height image - add tests
-rw-r--r--CHANGELOG.adoc1
-rw-r--r--lib/asciidoctor/pdf/ext/prawn/formatted_text/protect_bottom_gutter.rb13
-rw-r--r--lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb6
-rw-r--r--spec/image_spec.rb46
-rw-r--r--spec/reference/image-inline-in-block-scale-down-height.pdfbin0 -> 26063 bytes
-rw-r--r--spec/reference/image-inline-scale-down-height.pdfbin24180 -> 23834 bytes
6 files changed, 59 insertions, 7 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 3d9d39c2..cd693bda 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -9,6 +9,7 @@ For a detailed view of what has changed, refer to the {url-repo}/commits/main[co
Bug Fixes::
+* scale inline image to fit within available height of page, accounting for the top padding of the line and the bottom gutter (#2193)
* short-circuit formatted_text routine and log error if fragments in first line cannot fit on a new page
== 2.0.0 (2022-05-18) - @mojavelinux
diff --git a/lib/asciidoctor/pdf/ext/prawn/formatted_text/protect_bottom_gutter.rb b/lib/asciidoctor/pdf/ext/prawn/formatted_text/protect_bottom_gutter.rb
index 4b3ef5cd..a1a1e24b 100644
--- a/lib/asciidoctor/pdf/ext/prawn/formatted_text/protect_bottom_gutter.rb
+++ b/lib/asciidoctor/pdf/ext/prawn/formatted_text/protect_bottom_gutter.rb
@@ -2,12 +2,15 @@
module Prawn::Text::Formatted::ProtectBottomGutter
def enough_height_for_this_line?
- return super unless @arranger.finished?
- begin
- @height -= @bottom_gutter
+ if @arranger.finished? && @arranger.fragments.none? {|it| it.format_state[:full_height] }
+ begin
+ @height -= @bottom_gutter
+ super
+ ensure
+ @height += @bottom_gutter
+ end
+ else
super
- ensure
- @height += @bottom_gutter
end
end
end
diff --git a/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb b/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb
index 2c23791c..e563a43a 100644
--- a/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb
+++ b/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb
@@ -38,7 +38,7 @@ module Asciidoctor::PDF::FormattedText
return if (raw_image_fragments = fragments.select {|f| (f.key? :image_path) && !(f.key? :image_obj) }).empty?
scratch = doc.scratch?
available_w = available_width
- available_h = doc.page.empty? ? doc.cursor : doc.bounds.height
+ available_h = doc.bounds.height
last_fragment = {}
raw_image_fragments.each do |fragment|
if fragment[:object_id] == last_fragment[:object_id]
@@ -96,6 +96,10 @@ module Asciidoctor::PDF::FormattedText
if (f_height = image_h) > (line_font = doc.font).height * 1.5
# align with descender (equivalent to vertical-align: bottom in CSS)
fragment[:ascender] = f_height - (fragment[:descender] = line_font.descender)
+ if f_height == available_h
+ fragment[:ascender] -= (doc.calc_line_metrics (doc.instance_variable_get :@base_line_height), line_font, doc.font_size).padding_top
+ fragment[:full_height] = true
+ end
doc.font_size (fragment[:size] = f_height * (doc.font_size / line_font.height))
# align with baseline (roughly equivalent to vertical-align: baseline in CSS)
#fragment[:ascender] = f_height
diff --git a/spec/image_spec.rb b/spec/image_spec.rb
index d0b7a2ec..4305c2bb 100644
--- a/spec/image_spec.rb
+++ b/spec/image_spec.rb
@@ -507,7 +507,23 @@ describe 'Asciidoctor::PDF::Converter - Image' do
pdf = to_pdf input, analyze: :line
image_h = pdf.lines[1][:to][:y] - pdf.lines[1][:from][:y]
- (expect image_h).to be_within(1).of(350)
+ (expect image_h).to eql 350.0
+ end
+
+ it 'should scale down inline SVG to fit height of next page' do
+ input = <<~'EOS'
+ :pdf-page-size: 200x350
+ :pdf-page-margin: 0
+
+ before
+
+ image:tall.svg[]
+ EOS
+
+ pdf = to_pdf input, analyze: :line
+ (expect pdf.lines.map {|it| it[:page_number] }.uniq).to eql [2]
+ image_h = pdf.lines[1][:to][:y] - pdf.lines[1][:from][:y]
+ (expect image_h).to eql 350.0
end
it 'should display text inside link' do
@@ -1615,6 +1631,34 @@ describe 'Asciidoctor::PDF::Converter - Image' do
(expect line1_spacing).to eql line2_spacing
end
+ it 'should scale image down to fit available height on next page', visual: true do
+ to_file = to_pdf_file <<~'EOS', 'image-inline-pushed-scale-down-height.pdf'
+ :pdf-page-size: A6
+ :pdf-page-layout: landscape
+
+ before
+
+ image:cover.jpg[]
+ EOS
+
+ to_file = to_pdf_file %(image::#{to_file}[page=2]), 'image-inline-pushed-scale-down-height-2.pdf'
+
+ (expect to_file).to visually_match 'image-inline-scale-down-height.pdf'
+ end
+
+ it 'should scale image down to fit available height inside delimited block', visual: true do
+ to_file = to_pdf_file <<~'EOS', 'image-inline-in-block-scale-down-height.pdf'
+ :pdf-page-size: A6
+ :pdf-page-layout: landscape
+
+ ****
+ image:cover.jpg[]
+ ****
+ EOS
+
+ (expect to_file).to visually_match 'image-inline-in-block-scale-down-height.pdf'
+ end
+
it 'should not scale image if pdfwidth matches intrinsic width' do
pdf = to_pdf <<~'EOS', analyze: :image
see image:tux.png[pdfwidth=204] run
diff --git a/spec/reference/image-inline-in-block-scale-down-height.pdf b/spec/reference/image-inline-in-block-scale-down-height.pdf
new file mode 100644
index 00000000..1db693d4
--- /dev/null
+++ b/spec/reference/image-inline-in-block-scale-down-height.pdf
Binary files differ
diff --git a/spec/reference/image-inline-scale-down-height.pdf b/spec/reference/image-inline-scale-down-height.pdf
index 562c1914..4cbc9a2f 100644
--- a/spec/reference/image-inline-scale-down-height.pdf
+++ b/spec/reference/image-inline-scale-down-height.pdf
Binary files differ