summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2023-06-24 19:28:30 -0600
committerDan Allen <dan.j.allen@gmail.com>2023-06-24 19:32:34 -0600
commit01a8612f66e4beb341425e01618dd8a0b27681e0 (patch)
tree71fe18e94e0f14c421fd57cabf56b48aa50040d6
parenta2c62d5d10070cc7eb52212caff63deec533932c (diff)
backport fix for #2428 prevent inline image from being scaled down in normal table cell; support fit=none attribute on inline image
-rw-r--r--CHANGELOG.adoc5
-rw-r--r--docs/modules/ROOT/pages/image-scaling.adoc26
-rw-r--r--docs/modules/ROOT/pages/inline-images.adoc1
-rw-r--r--lib/asciidoctor/pdf/ext/prawn-table/cell/text.rb2
-rw-r--r--lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb10
-rw-r--r--spec/table_spec.rb32
6 files changed, 62 insertions, 14 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 0571ac6b..4f681f16 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -7,6 +7,11 @@ For a detailed view of what has changed, refer to the {url-repo}/commits/main[co
== Unreleased
+Improvements::
+
+* do not attempt to fit inline image in normal table cell to computed height of cell (#2428)
+* support `fit=none` attribute on inline image to prevent image from being scaled down to fit available height (#2428)
+
Bug Fixes::
* remove null character enclosed in XML tag when santizing text; fixes invisible text in outline when heading contains index term (#2430)
diff --git a/docs/modules/ROOT/pages/image-scaling.adoc b/docs/modules/ROOT/pages/image-scaling.adoc
index dfc0ff3f..24006f67 100644
--- a/docs/modules/ROOT/pages/image-scaling.adoc
+++ b/docs/modules/ROOT/pages/image-scaling.adoc
@@ -51,24 +51,26 @@ The computed height of the image is honored unless this value exceeds the height
In this case, the image is scaled down to fit within the content area.
The height of the content area is typically the space between the top and bottom margins of the page.
-However, the content area is different for table cells.
+However, the content area in table cells is handled in two phases.
=== Inline image sizing in table cells
In tables, each cell is considered its own content area.
+That content area is computed based on how much space the content requires, though still limited to the space between the top and bottom margins of the page.
+Inline images in table cells are permitted to increase the height of the cell.
+However, there can be differences in how the image is sized in a normal cell compared to an AsciiDoc table cell.
-The content area of a normal table cell is computed based on how much space the content needs.
-In this regard, inline images in normal table cells are permitted to increase the height of the cell.
-However, the calculation of the content's height can be inaccurate, and is often impacted by the font used.
+The calculation of the content's height in a normal table cell can be inaccurate, often impacted by the font used.
When the table cell is rendered, its height is restricted to the computed height.
-This can impact the sizing of inline images in a normal table cell.
-If the computed height ends up being less than the height the inline image needs, the image will be scaled down to fit.
-This behavior is a side effect of how the PDF generator used by Asciidoctor PDF works and cannot be fixed.
-
-The content area of an AsciiDoc table cell is less restrictive.
-An AsciiDoc table cell will expand to use all available height on the page.
-Thus, when you specify the `a` style on a table cell, the scaling of an inline image will be different.
-In this case, the inline image will not be scaled down if it fits within the available height on the page.
+This can impact the sizing of inline images in a normal table cell if `fit=line` is set on the image.
+If the computed height ends up being less than the line height, the image will be scaled down to fit.
+If you want to prevent the converter from attempting to scale down the height of the inline image in a normal table cell, don't use `fit=line` or set `fit=none` on the inline image.
+
+The content area of an AsciiDoc table cell is computed more accurately.
+// NOTE TO AUTHORS: This is because Asciidoctor PDF calculates the content height itself and does so accurately
+Thus, when you specify the `a` style on a table cell, the scaling of an inline image is true.
+An inline image will never be scaled down if it fits within the maximum available height of a single page.
+In this scenario, the `fit=line` attribute will work in the same way it does outside a table cell.
[#pdfwidth]
== pdfwidth attribute
diff --git a/docs/modules/ROOT/pages/inline-images.adoc b/docs/modules/ROOT/pages/inline-images.adoc
index 77de2c27..0c86cce1 100644
--- a/docs/modules/ROOT/pages/inline-images.adoc
+++ b/docs/modules/ROOT/pages/inline-images.adoc
@@ -9,6 +9,7 @@ Inline images can be sized in much the same way as block images, using the `pdfw
* Inline images do not currently support a default width controlled from the theme.
To confine the inline image to the height of the line while preserving the aspect ratio, use the attribute `fit=line`.
+If you want to prevent the inline image from being fit to the content area, such as in a normal table cell, use `fit=none`.
If the resolved height of the image is less than or equal to 1.5 times the line height, the image won't disrupt the line height and is centered vertically in the line.
This is done to maximize the use of available space.
diff --git a/lib/asciidoctor/pdf/ext/prawn-table/cell/text.rb b/lib/asciidoctor/pdf/ext/prawn-table/cell/text.rb
index 00d82f0d..50a24b69 100644
--- a/lib/asciidoctor/pdf/ext/prawn-table/cell/text.rb
+++ b/lib/asciidoctor/pdf/ext/prawn-table/cell/text.rb
@@ -10,7 +10,7 @@ class Prawn::Table::Cell::Text
def draw_content
with_font do
self.valign = [:center, -font.descender * 0.5] if valign == :center
- bounds = @pdf.bounds
+ (bounds = @pdf.bounds).instance_variable_set :@table_cell, true
remaining_text = with_text_color do
(text_box width: bounds.width, height: bounds.height, at: [0, @pdf.cursor]).render
end
diff --git a/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb b/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb
index c70ed3b8..f9cd46ba 100644
--- a/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb
+++ b/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb
@@ -63,7 +63,15 @@ module Asciidoctor::PDF::FormattedText
image_w = [available_w, pctidx ? (image_w.to_f / 100 * available_w) : image_w.to_f].min
end
- max_image_h = fragment[:image_fit] == 'line' ? [available_h, doc.font.height].min : available_h
+ if (fit = fragment[:image_fit]) === 'line'
+ max_image_h = [available_h, doc.font.height].min
+ elsif fit == 'none'
+ max_image_h = doc.margin_box.height
+ elsif doc.bounds.instance_variable_get :@table_cell
+ max_image_h = doc.bounds.parent.height
+ else
+ max_image_h = available_h
+ end
# TODO: make helper method to calculate width and height of image
if image_format == 'svg'
diff --git a/spec/table_spec.rb b/spec/table_spec.rb
index 99c44f63..116213f9 100644
--- a/spec/table_spec.rb
+++ b/spec/table_spec.rb
@@ -907,6 +907,38 @@ describe 'Asciidoctor::PDF::Converter - Table' do
(expect images[0][:y]).to be > text[1][:y]
end
+ it 'should not attempt to fit image to computed height of normal table cell' do
+ pdf_theme = { table_font_family: 'M+ 1mn', table_font_size: 9 }
+
+ pdf = to_pdf <<~'END', pdf_theme: pdf_theme, analyze: :image
+ [cols=2*]
+ |===
+ a|image:square.png[pdfwidth=16px] text
+ |image:square.png[pdfwidth=16px] text
+ |===
+ END
+ images = pdf.images
+ (expect images).to have_size 2
+ (expect images[0][:width]).to eql 12.0
+ (expect images[1][:width]).to eql 12.0
+ end
+
+ it 'should not attempt to fit image to computed height of normal table cell if fit=none is set' do
+ pdf_theme = { table_font_family: 'M+ 1mn', table_font_size: 9 }
+
+ pdf = to_pdf <<~'END', pdf_theme: pdf_theme, analyze: :image
+ [cols=2*]
+ |===
+ a|image:square.png[pdfwidth=16px] text
+ |image:square.png[pdfwidth=16px,fit=none] text
+ |===
+ END
+ images = pdf.images
+ (expect images).to have_size 2
+ (expect images[0][:width]).to eql 12.0
+ (expect images[1][:width]).to eql 12.0
+ end
+
it 'should not break words in head row when autowidth option is set' do
pdf = to_pdf <<~'EOS', analyze: true
[%autowidth]