summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2022-06-01 02:24:36 -0600
committerGitHub <noreply@github.com>2022-06-01 02:24:36 -0600
commit58a2ba9cfbdb152be45dd4217a238ad9c06133ca (patch)
tree17fabd3a1730bdc0e810858c83392a2457ba2f39
parentd43483eda862d67b10827ca840b2e845818c1fbc (diff)
resolves #2223 use prawn-gmagick, if available, to read raster image referenced by SVG (PR #2224)
-rw-r--r--CHANGELOG.adoc4
-rw-r--r--lib/asciidoctor/pdf/ext/prawn-svg.rb1
-rw-r--r--lib/asciidoctor/pdf/ext/prawn-svg/elements/image.rb10
-rw-r--r--spec/fixtures/svg-with-gif-image.svg3
-rw-r--r--spec/image_spec.rb22
5 files changed, 36 insertions, 4 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 7b4dec88..488c3dd7 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -12,6 +12,10 @@ 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
+Bug Fixes::
+
+* use prawn-gmagick, if available, to read raster image referenced by SVG (#2223)
+
== 2.0.6 (2022-05-30) - @mojavelinux
Bug Fixes::
diff --git a/lib/asciidoctor/pdf/ext/prawn-svg.rb b/lib/asciidoctor/pdf/ext/prawn-svg.rb
index 0f9e2a68..7a4b7884 100644
--- a/lib/asciidoctor/pdf/ext/prawn-svg.rb
+++ b/lib/asciidoctor/pdf/ext/prawn-svg.rb
@@ -2,6 +2,7 @@
require 'prawn-svg'
require_relative 'prawn-svg/calculators/document_sizing'
+require_relative 'prawn-svg/elements/image'
require_relative 'prawn-svg/loaders/data'
require_relative 'prawn-svg/loaders/web'
require_relative 'prawn-svg/url_loader'
diff --git a/lib/asciidoctor/pdf/ext/prawn-svg/elements/image.rb b/lib/asciidoctor/pdf/ext/prawn-svg/elements/image.rb
new file mode 100644
index 00000000..e20b2bc7
--- /dev/null
+++ b/lib/asciidoctor/pdf/ext/prawn-svg/elements/image.rb
@@ -0,0 +1,10 @@
+# frozen_string_literal: true
+
+Prawn::SVG::Elements::Image.prepend (Module.new do
+ def image_dimensions data
+ image = (Prawn.image_handler.find data).new data
+ [image.width.to_f, image.height.to_f]
+ rescue
+ raise ::Prawn::SVG::Elements::Base::SkipElementError, 'image supplied to image tag is an unrecognised format'
+ end
+end)
diff --git a/spec/fixtures/svg-with-gif-image.svg b/spec/fixtures/svg-with-gif-image.svg
new file mode 100644
index 00000000..03bf2887
--- /dev/null
+++ b/spec/fixtures/svg-with-gif-image.svg
@@ -0,0 +1,3 @@
+<svg width="204" height="240" version="1.1" viewBox="0 0 204 240" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<image x="0" y="0" width="204" height="240" xlink:href="tux.gif"/>
+</svg>
diff --git a/spec/image_spec.rb b/spec/image_spec.rb
index c0973900..941ecf67 100644
--- a/spec/image_spec.rb
+++ b/spec/image_spec.rb
@@ -664,6 +664,20 @@ describe 'Asciidoctor::PDF::Converter - Image' do
(expect to_file).to visually_match 'image-block-svg-with-image.pdf'
end
+ it 'should embed GIF image if prawn-gmagick is available', if: (defined? GMagick::Image) do
+ pdf = to_pdf 'image::svg-with-gif-image.svg[]', analyze: :image
+ (expect pdf.images).to have_size 1
+ image = pdf.images[0]
+ (expect image[:intrinsic_width]).to eql 204
+ end
+
+ it 'should log warning for GIF image if prawn-gmagick is not available', unless: (defined? GMagick::Image) do
+ (expect do
+ pdf = to_pdf 'image::svg-with-gif-image.svg[]', analyze: :image
+ (expect pdf.images).to be_empty
+ end).to log_message severity: :WARN, message: %(problem encountered in image: #{fixture_file 'svg-with-gif-image.svg'}; image supplied to image tag is an unrecognised format)
+ end
+
it 'should embed image from data-uri in inline image', visual: true do
to_file = to_pdf_file <<~'EOS', 'image-svg-with-data-uri-image.pdf'
A sign of a good writer: image:svg-with-data-uri-image.svg[pdfwidth=1.27cm]
@@ -826,23 +840,23 @@ describe 'Asciidoctor::PDF::Converter - Image' do
end
end
- it 'should not embed local SVG in inline image', visual: true do
+ it '.only should not embed local SVG in inline image', visual: true do
(expect do
to_file = to_pdf_file <<~'EOS', 'image-inline-svg-with-local-svg.pdf'
image:svg-with-local-svg.svg[pdfwidth=1.27cm] lacks the red square.
EOS
(expect to_file).to visually_match 'image-inline-svg-with-local-svg.pdf'
- end).to log_message severity: :WARN, message: %(~problem encountered in image: #{fixture_file 'svg-with-local-svg.svg'}; Unsupported image type supplied to image tag; Prawn only supports JPG and PNG)
+ end).to log_message severity: :WARN, message: %(~problem encountered in image: #{fixture_file 'svg-with-local-svg.svg'}; image supplied to image tag is an unrecognised format)
end
- it 'should not embed local SVG in block image', visual: true do
+ it '.only should not embed local SVG in block image', visual: true do
(expect do
to_file = to_pdf_file <<~'EOS', 'image-block-svg-with-local-svg.pdf'
.Lacks the red square
image::svg-with-local-svg.svg[pdfwidth=5in]
EOS
(expect to_file).to visually_match 'image-block-svg-with-local-svg.pdf'
- end).to log_message severity: :WARN, message: %(~problem encountered in image: #{fixture_file 'svg-with-local-svg.svg'}; Unsupported image type supplied to image tag; Prawn only supports JPG and PNG)
+ end).to log_message severity: :WARN, message: %(~problem encountered in image: #{fixture_file 'svg-with-local-svg.svg'}; image supplied to image tag is an unrecognised format)
end
end