Skip to content

Commit

Permalink
resolves asciidoctor#2176 look for block align roles on image instead…
Browse files Browse the repository at this point in the history
… of text align roles
  • Loading branch information
mojavelinux committed May 16, 2022
1 parent c67f403 commit dffec2a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Bug Fixes::
* apply top line height padding to first line of text when text runs to top of next page (#2173)
* don't add entry to outline for notitle section if no content follows it
* don't add entry to TOC for notitle section if no content follows it
* look for block align roles on image instead of text align roles (#2176)

== 2.0.0.beta.2 (2022-05-14) - @mojavelinux

Expand Down
26 changes: 13 additions & 13 deletions lib/asciidoctor/pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,16 @@ def convert_list_item node, list, opts = {}
def convert_image node, opts = {}
target, image_format = (node.extend ::Asciidoctor::Image).target_and_format

unless image_format == 'pdf'
if (float_to = node.attr 'float') && ((BlockFloatNames.include? float_to) ? float_to : (float_to = nil))
alignment = float_to.to_sym
elsif (alignment = node.attr 'align')
alignment = (BlockAlignmentNames.include? alignment) ? alignment.to_sym : :left
elsif !(alignment = node.roles.reverse.find {|r| BlockAlignmentNames.include? r }&.to_sym)
alignment = @theme.image_align&.to_sym || :left
end
end

if image_format == 'gif' && !(defined? ::GMagick::Image)
log :warn, %(GIF image format not supported. Install the prawn-gmagick gem or convert #{target} to PNG.)
image_path = nil
Expand Down Expand Up @@ -1666,15 +1676,8 @@ def convert_image node, opts = {}
end
end

return on_image_error :missing, node, target, opts unless image_path
return on_image_error :missing, node, target, (opts.merge align: alignment) unless image_path

if (float_to = node.attr 'float') && ((BlockFloatNames.include? float_to) ? float_to : (float_to = nil))
alignment = float_to.to_sym
elsif (alignment = node.attr 'align')
alignment = (BlockAlignmentNames.include? alignment) ? alignment.to_sym : :left
else
alignment = (resolve_text_align_from_role node.roles) || @theme.image_align&.to_sym || :left
end
# TODO: support cover (aka canvas) image layout using "canvas" (or "cover") role
width = resolve_explicit_width node.attributes, bounds_width: (available_w = bounds.width), support_vw: true, use_fallback: true, constrain_to_bounds: true
# TODO: add `to_pt page_width` method to ViewportWidth type
Expand Down Expand Up @@ -1778,7 +1781,7 @@ def convert_image node, opts = {}
end
rescue => e
raise if ::StopIteration === e
on_image_error :exception, node, target, (opts.merge message: %(could not embed image: #{image_path}; #{e.message}#{::Prawn::Errors::UnsupportedImageType === e && !(defined? ::GMagick::Image) ? '; install prawn-gmagick gem to add support' : ''}))
on_image_error :exception, node, target, (opts.merge align: alignment, message: %(could not embed image: #{image_path}; #{e.message}#{::Prawn::Errors::UnsupportedImageType === e && !(defined? ::GMagick::Image) ? '; install prawn-gmagick gem to add support' : ''}))
end
end

Expand Down Expand Up @@ -4801,10 +4804,7 @@ def on_image_error _reason, node, target, opts
alt_text_vars[:'/link'] = ''
end
theme_font :image_alt do
alignment = (alignment = node.attr 'align') ?
((BlockAlignmentNames.include? alignment) ? alignment.to_sym : :left) :
(resolve_text_align_from_role node.roles) || (@theme.image_align&.to_sym || :left)
ink_prose alt_text_template % alt_text_vars, align: alignment, margin: 0, normalize: false, single_line: true
ink_prose alt_text_template % alt_text_vars, align: opts[:align], margin: 0, normalize: false, single_line: true
end
ink_caption node, category: :image, end: :bottom if node.title?
theme_margin :block, :bottom, (next_enclosed_block node) unless opts[:pinned]
Expand Down
8 changes: 4 additions & 4 deletions spec/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
it 'should align alt text using alignment specified on image' do
[
['', image_align: nil],
[',align=center', {}],
[',role=text-center', {}],
['align=center', {}],
['role=center', {}],
['', image_align: 'center'],
].each do |attrlist, pdf_theme|
(expect do
Expand Down Expand Up @@ -172,9 +172,9 @@
(expect images[0][:x]).to eql 48.24
end

it 'should align block image as indicated by text alignment role on macro', visual: true do
it 'should align block image as indicated by block alignment role on macro', visual: true do
to_file = to_pdf_file <<~'EOS', 'image-align-right-attribute.pdf', attribute_overrides: { 'imagesdir' => examples_dir }
[.text-right]
[.right]
image::wolpertinger.jpg[]
EOS

Expand Down

0 comments on commit dffec2a

Please sign in to comment.