summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2022-09-28 03:00:49 -0600
committerGitHub <noreply@github.com>2022-09-28 03:00:49 -0600
commit4df03898faefcda99bc4cd3a751a95dba3b4d690 (patch)
treef6f90dbc55fa999066ca6bc4263f9fe1f28fd973
parent02fabe0da4e5aa8ac7deb9b7325adf0430f6e028 (diff)
resolves #2347 add support for passing a color mode to the default optimizer (PR #2348)
-rw-r--r--CHANGELOG.adoc1
-rw-r--r--docs/modules/ROOT/pages/optimize-pdf.adoc8
-rw-r--r--lib/asciidoctor/pdf/optimizer/rghost.rb14
-rw-r--r--spec/optimizer_spec.rb25
-rw-r--r--spec/reference/optimizer-bw.pdfbin0 -> 30194 bytes
-rw-r--r--spec/reference/optimizer-gray.pdfbin0 -> 30240 bytes
6 files changed, 47 insertions, 1 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 824c2351..d296b059 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -25,6 +25,7 @@ Enhancements::
* restrict categories to ASCII letters (using transliteration) if ffi-icu gem is available on load path (#928)
* upgrade to prawn-icon 3.1.x to add support for the Material Design Icons (`mdi`) as an available font-based icon set (PR #2334)
* honor `GS_OPTIONS` environment variable for supplying additional parameters to command called by RGhost optimizer (#2337)
+* add support for passing a color mode to the default optimizer (#2347)
Improvements::
diff --git a/docs/modules/ROOT/pages/optimize-pdf.adoc b/docs/modules/ROOT/pages/optimize-pdf.adoc
index 4fdfedb7..3006d500 100644
--- a/docs/modules/ROOT/pages/optimize-pdf.adoc
+++ b/docs/modules/ROOT/pages/optimize-pdf.adoc
@@ -78,6 +78,14 @@ Force all text to be converted to paths (linework) so it's not selectable (in ex
-sOwnerPassword=xyx -sUserPassword=xyx::
Require a password to open the document (owner for edit, user for view).
+As a shorthand for using the `-sColorConversionStrategy=Gray` option, you can pass the `gray` color mode after the quality value:
+
+ -a optimize=screen:gray
+
+You can also force all text to render in black in the grayscale document using the `bw` (i.e., black and white) color mode:
+
+ -a optimize=print:bw
+
In addition to optimizing the PDF file, you can also configure the optimizer to convert the document from standard PDF to PDF/A or PDF/X.
To do so, you can pass one of the following compliance keywords in the value of the optimize attribute: `PDF/A`, `PDF/A-1`, `PDF/A-2`, `PDF/A-3`, `PDF/X`, `PDF/X-1`, or `PDF/X-3`.
diff --git a/lib/asciidoctor/pdf/optimizer/rghost.rb b/lib/asciidoctor/pdf/optimizer/rghost.rb
index feea3efe..3d65ab85 100644
--- a/lib/asciidoctor/pdf/optimizer/rghost.rb
+++ b/lib/asciidoctor/pdf/optimizer/rghost.rb
@@ -42,6 +42,11 @@ module Asciidoctor
def initialize *_args
super
+ if @quality&.include? ':'
+ @quality, @color_mode = @quality.split ':', 2
+ else
+ @color_mode = nil
+ end
if (gs_path = ::ENV['GS'])
::RGhost::Config::GS[:path] = gs_path
end
@@ -70,7 +75,14 @@ module Asciidoctor
d[:PDFX] = true
d[:ShowAnnots] = false
end
- (::RGhost::Convert.new inputs).to :pdf, filename: filename_tmp.to_s, quality: QUALITY_NAMES[@quality], d: d
+ case @color_mode
+ when 'gray', 'grayscale'
+ s = { ColorConversionStrategy: 'Gray' }
+ when 'bw'
+ d[:BlackText] = true
+ s = { ColorConversionStrategy: 'Gray' }
+ end
+ (::RGhost::Convert.new inputs).to :pdf, filename: filename_tmp.to_s, quality: QUALITY_NAMES[@quality], d: d, s: s
filename_o.binwrite filename_tmp.binread
end
nil
diff --git a/spec/optimizer_spec.rb b/spec/optimizer_spec.rb
index 3ac9c0ba..caaa7ee7 100644
--- a/spec/optimizer_spec.rb
+++ b/spec/optimizer_spec.rb
@@ -88,6 +88,31 @@ describe 'Asciidoctor::PDF::Optimizer', if: (RSpec::ExampleGroupHelpers.gem_avai
end).to not_raise_exception
end
+ it 'should apply grayscale color mode modifier on quality when optimizing output file', visual: true do
+ %w(gray grayscale).each do |mode|
+ input_file = Pathname.new fixture_file 'with-color.adoc'
+ to_file = to_pdf_file input_file, 'optimizer-gray.pdf', attribute_overrides: { 'optimize' => %(screen:#{mode}) }
+ (expect to_file).to visually_match 'optimizer-gray.pdf'
+ end
+ end
+
+ #it 'should apply bw color mode modifier on quality when optimizing output file', visual: true do
+ # input_file = Pathname.new fixture_file 'with-color.adoc'
+ # to_file = to_pdf_file input_file, 'optimizer-bw.pdf', attribute_overrides: { 'optimize' => ':bw' }
+ # (expect to_file).to visually_match 'optimizer-bw.pdf'
+ #end
+
+ it 'should apply bw color mode modifier on quality when optimizing output file', visual: true do
+ input_file = Pathname.new fixture_file 'with-color.adoc'
+ attribute_overrides = { 'optimize' => ':bw' }
+ pdf = to_pdf input_file, attribute_overrides: attribute_overrides, analyze: true
+ (expect pdf.text.map {|it| it[:font_color] }.uniq).to eql [nil]
+ pdf = to_pdf input_file, attribute_overrides: attribute_overrides, analyze: :rect
+ rects = pdf.rectangles
+ (expect rects).to have_size 1
+ (expect rects[0][:fill_color]).to eql '818181'
+ end
+
it 'should generate PDF that conforms to specified compliance' do
input_file = Pathname.new example_file 'basic-example.adoc'
to_file = to_pdf_file input_file, 'optimizer-screen-pdf-a.pdf', attribute_overrides: { 'optimize' => 'PDF/A' }
diff --git a/spec/reference/optimizer-bw.pdf b/spec/reference/optimizer-bw.pdf
new file mode 100644
index 00000000..2b5cde75
--- /dev/null
+++ b/spec/reference/optimizer-bw.pdf
Binary files differ
diff --git a/spec/reference/optimizer-gray.pdf b/spec/reference/optimizer-gray.pdf
new file mode 100644
index 00000000..32d19c67
--- /dev/null
+++ b/spec/reference/optimizer-gray.pdf
Binary files differ