diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2022-05-31 03:31:02 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-31 03:31:02 -0600 |
| commit | 4c71f04baf6c06c4fbebc2967953b98087f2704d (patch) | |
| tree | 00c5d41e00c718c3b65e60f46e3a89d9987d403f | |
| parent | 4222c2a0902b42aaf21bcdde4b79c361a39df3f0 (diff) | |
tune prawn-gmagick recommendation (PR #2222)
| -rw-r--r-- | CHANGELOG.adoc | 7 | ||||
| -rw-r--r-- | lib/asciidoctor/pdf/converter.rb | 2 | ||||
| -rw-r--r-- | lib/asciidoctor/pdf/ext/prawn/images.rb | 4 | ||||
| -rw-r--r-- | lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb | 4 | ||||
| -rw-r--r-- | spec/image_spec.rb | 22 |
5 files changed, 34 insertions, 5 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 1690933c..7b4dec88 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -5,6 +5,13 @@ This document provides a high-level view of the changes to the {project-name} by release. For a detailed view of what has changed, refer to the {url-repo}/commits/main[commit history] on GitHub. +== Unreleased + +Improvements:: + +* don't recommend prawn-gmagick if PNG or JPG is corrupt or incomplete +* add helper method to determine when to recommend the prawn-gmagick gem + == 2.0.6 (2022-05-30) - @mojavelinux Bug Fixes:: diff --git a/lib/asciidoctor/pdf/converter.rb b/lib/asciidoctor/pdf/converter.rb index 79cc0c87..5c5ef13e 100644 --- a/lib/asciidoctor/pdf/converter.rb +++ b/lib/asciidoctor/pdf/converter.rb @@ -1811,7 +1811,7 @@ module Asciidoctor end rescue => e raise if ::StopIteration === e - 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' : ''})) + on_image_error :exception, node, target, (opts.merge align: alignment, message: %(could not embed image: #{image_path}; #{e.message}#{(recommend_prawn_gmagick? e, image_format) ? %(; install prawn-gmagick gem to add support for #{image_format&.upcase || 'unknown'} image format) : ''})) end end diff --git a/lib/asciidoctor/pdf/ext/prawn/images.rb b/lib/asciidoctor/pdf/ext/prawn/images.rb index 8bae4cdb..23dffde2 100644 --- a/lib/asciidoctor/pdf/ext/prawn/images.rb +++ b/lib/asciidoctor/pdf/ext/prawn/images.rb @@ -53,6 +53,10 @@ module Asciidoctor # NOTE: image cannot be read, so it won't be used anyway { width: 0, height: 0 } end + + def recommend_prawn_gmagick? err, image_format + ::Prawn::Errors::UnsupportedImageType === err && !(defined? ::GMagick::Image) && ((err.message.include? 'PNG') || (%w(jpg png).none? image_format)) + end end ::Prawn::Document.extensions << Images diff --git a/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb b/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb index e563a43a..dc5512e4 100644 --- a/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb +++ b/lib/asciidoctor/pdf/formatted_text/inline_image_arranger.rb @@ -62,7 +62,7 @@ module Asciidoctor::PDF::FormattedText max_image_h = fragment[:image_fit] == 'line' ? [available_h, doc.font.height].min : available_h # TODO: make helper method to calculate width and height of image - if fragment[:image_format] == 'svg' + if (image_format = fragment[:image_format]) == 'svg' svg_obj = ::Prawn::SVG::Interface.new ::File.read(image_path, mode: 'r:UTF-8'), doc, at: doc.bounds.top_left, width: image_w, @@ -117,7 +117,7 @@ module Asciidoctor::PDF::FormattedText fragment[:image_width] = fragment[:width] = image_w fragment[:image_height] = image_h rescue - logger.warn %(could not embed image: #{image_path}; #{$!.message}#{::Prawn::Errors::UnsupportedImageType === $! && !(defined? ::GMagick::Image) ? '; install prawn-gmagick gem to add support' : ''}) unless scratch + logger.warn %(could not embed image: #{image_path}; #{$!.message}#{(doc.recommend_prawn_gmagick? $!, image_format) ? %(; install prawn-gmagick gem to add support for #{image_format&.upcase || 'unknown'} image format) : ''}) unless scratch drop = true # delegate to cleanup logic in ensure block ensure # NOTE: skip rendering image in scratch document or if image can't be loaded diff --git a/spec/image_spec.rb b/spec/image_spec.rb index 90113db4..c0973900 100644 --- a/spec/image_spec.rb +++ b/spec/image_spec.rb @@ -917,6 +917,24 @@ describe 'Asciidoctor::PDF::Converter - Image' do (expect pdf.images[0][:width]).to eql reference_width end + it 'should fail to embed incomplete PNG with warning' do + Tempfile.create %w(incomplete- .png), output_dir do |tmp_file| + tmp_file.binmode + tmp_file.write [137, 80, 78, 71, 10].pack 'C*' # make a PNG with incomplete data + tmp_file.close + image_path = tmp_file.path + { '::' => %([Incomplete PNG] | #{image_path}), ':' => '[Incomplete PNG]' }.each do |macro_delim, alt_text| + (expect do + input = <<~EOS + image#{macro_delim}#{image_path}[Incomplete PNG] + EOS + pdf = to_pdf input, analyze: true + (expect pdf.lines).to eql [alt_text] + end).to log_message severity: :WARN, message: %(could not embed image: #{image_path}; image file is an unrecognised format) + end + end + end + it 'should fail to embed interlaced PNG image with warning', unless: (defined? GMagick::Image) do { '::' => '[Interlaced PNG] | interlaced.png', ':' => '[Interlaced PNG]' }.each do |macro_delim, alt_text| (expect do @@ -928,7 +946,7 @@ describe 'Asciidoctor::PDF::Converter - Image' do EOS pdf = to_pdf input, analyze: true (expect pdf.lines).to eql [alt_text] - end).to log_message severity: :WARN, message: %(could not embed image: #{fixture_file 'interlaced.png'}; PNG uses unsupported interlace method; install prawn-gmagick gem to add support) + end).to log_message severity: :WARN, message: %(could not embed image: #{fixture_file 'interlaced.png'}; PNG uses unsupported interlace method; install prawn-gmagick gem to add support for PNG image format) end end @@ -1051,7 +1069,7 @@ describe 'Asciidoctor::PDF::Converter - Image' do (expect do pdf = to_pdf 'image:waterfall.bmp[waterfall,16] is not agile.', analyze: true (expect pdf.lines).to eql ['[waterfall] is not agile.'] - end).to log_message severity: :WARN, message: '~image file is an unrecognised format; install prawn-gmagick gem to add support' + end).to log_message severity: :WARN, message: '~image file is an unrecognised format; install prawn-gmagick gem to add support for BMP image format' ensure GMagick.const_set :Image, old_gmagick_image Gmagick.singleton_class.remove_method :can_render? |
