summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2023-06-28 04:12:23 -0600
committerGitHub <noreply@github.com>2023-06-28 04:12:23 -0600
commit6f34121565b4bf21dac11d377de259083525f745 (patch)
tree970c2c0383ef6c6df22846d05bdd0c018608cb8e
parent5ae2ea41688b03e84df4581d9fd1ab6b6c1026ef (diff)
resolves #2433 correctly align block image in raster format in column when align is right or center (PR #2436)
-rw-r--r--CHANGELOG.adoc1
-rw-r--r--lib/asciidoctor/pdf/converter.rb11
-rw-r--r--spec/image_spec.rb85
3 files changed, 95 insertions, 2 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 1435f8c5..5e389475 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -40,6 +40,7 @@ Improvements::
Bug Fixes::
+* correctly align block image in raster format in column when align is right or center and page columns are enabled (#2433)
* correctly map all icons from FontAwesome 4 (#2373)
* resolve remote image in document title or section title with autogenerated ID
* keep caret between items in menu macro with previous item if items wrap
diff --git a/lib/asciidoctor/pdf/converter.rb b/lib/asciidoctor/pdf/converter.rb
index e3542a34..7d094fd6 100644
--- a/lib/asciidoctor/pdf/converter.rb
+++ b/lib/asciidoctor/pdf/converter.rb
@@ -1872,9 +1872,16 @@ module Asciidoctor
update_colors if graphic_state.color_space.empty?
ink_caption node, category: :image, end: :top, block_align: alignment, block_width: rendered_w, max_width: caption_max_width if caption_end == :top && node.title?
image_y = y
- image_cursor = cursor
+ left = bounds.left
+ # NOTE: prawn does not compute :at for alignment correctly in column box, so resort to our own logic
+ case alignment
+ when :center
+ left += (available_w - rendered_w) * 0.5
+ when :right
+ left += available_w - rendered_w
+ end
# NOTE: specify both width and height to avoid recalculation
- embed_image image_obj, image_info, width: rendered_w, height: rendered_h, position: alignment
+ embed_image image_obj, image_info, at: [left, (image_cursor = cursor)], height: rendered_h, width: rendered_w
draw_image_border image_cursor, rendered_w, rendered_h, alignment unless pinned || (node.role? && (node.has_role? 'noborder'))
if (link = node.attr 'link')
add_link_to_image link, { width: rendered_w, height: rendered_h }, position: alignment, y: image_y
diff --git a/spec/image_spec.rb b/spec/image_spec.rb
index 5d47df19..8aaa837d 100644
--- a/spec/image_spec.rb
+++ b/spec/image_spec.rb
@@ -1381,6 +1381,91 @@ describe 'Asciidoctor::PDF::Converter - Image' do
page_contents = pdf.objects[(pdf.page 2).page_object[:Contents]].data
(expect (page_contents.split ?\n).slice 0, 3).to eql ['q', '/DeviceRGB cs', '0.0 0.0 0.0 scn']
end
+
+ it 'should place raster image in correct column when page columns are enabled' do
+ pdf_theme = {
+ page_columns: 2,
+ page_column_gap: 12,
+ thematic_break_border_color: '0000FF',
+ thematic_break_border_width: 1,
+ }
+ input = <<~'END'
+ left column
+
+ [.column]
+ <<<
+
+ ---
+
+ image::tux.jpg[pdfwidth=50%]
+ END
+
+ lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines
+ thematic_break_line = lines.find {|it| it[:color] == '0000FF' && it[:width] == 1 }
+ column_left = thematic_break_line[:from][:x]
+ images = (to_pdf input, pdf_theme: pdf_theme, analyze: :image).images
+ (expect images).to have_size 1
+ (expect images[0][:page_number]).to eql 1
+ (expect images[0][:x]).to eql column_left
+ end
+
+ it 'should align raster image to right of column when page columns are enabled' do
+ pdf_theme = {
+ page_columns: 2,
+ page_column_gap: 12,
+ thematic_break_border_color: '0000FF',
+ thematic_break_border_width: 1,
+ }
+ input = <<~'END'
+ left column
+
+ [.column]
+ <<<
+
+ ---
+
+ image::tux.jpg[align=right,pdfwidth=50%]
+ END
+
+ lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines
+ thematic_break_line = lines.find {|it| it[:color] == '0000FF' && it[:width] == 1 }
+ column_right = thematic_break_line[:to][:x]
+ images = (to_pdf input, pdf_theme: pdf_theme, analyze: :image).images
+ (expect images).to have_size 1
+ (expect images[0][:page_number]).to eql 1
+ (expect images[0][:width]).to eql 121.7
+ (expect images[0][:x]).to eql (column_right - images[0][:width])
+ end
+
+ it 'should align raster image to center of column when page columns are enabled' do
+ pdf_theme = {
+ page_columns: 2,
+ page_column_gap: 12,
+ thematic_break_border_color: '0000FF',
+ thematic_break_border_width: 1,
+ }
+ input = <<~'END'
+ left column
+
+ [.column]
+ <<<
+
+ ---
+
+ image::tux.jpg[align=center,pdfwidth=50%]
+ END
+
+ lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines
+ thematic_break_line = lines.find {|it| it[:color] == '0000FF' && it[:width] == 1 }
+ column_left = thematic_break_line[:from][:x]
+ column_right = thematic_break_line[:to][:x]
+ images = (to_pdf input, pdf_theme: pdf_theme, analyze: :image).images
+ (expect images).to have_size 1
+ (expect images[0][:page_number]).to eql 1
+ (expect images[0][:width]).to eql 121.7
+ (expect images[0][:x]).to be > column_left
+ (expect images[0][:x] + images[0][:width]).to be < column_right
+ end
end
context 'BMP' do