diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2022-09-12 12:01:08 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-12 12:01:08 -0600 |
| commit | edc2ea29d0d8e5e33615fa4e0757f933fad11f41 (patch) | |
| tree | ab50a2571c89ffeeebdf5fb65d098f4026c5c89e | |
| parent | 9d317ad9b213a0562402ff3994534bf4ccbd1d6c (diff) | |
resolves #2337 honor GS_OPTIONS environment variable in RGhost optimizer (PR #2338)
| -rw-r--r-- | CHANGELOG.adoc | 1 | ||||
| -rw-r--r-- | docs/modules/ROOT/pages/optimize-pdf.adoc | 9 | ||||
| -rw-r--r-- | lib/asciidoctor/pdf/optimizer/rghost.rb | 7 | ||||
| -rw-r--r-- | spec/fixtures/with-color.adoc | 16 | ||||
| -rw-r--r-- | spec/optimizer_spec.rb | 23 | ||||
| -rw-r--r-- | spec/spec_helper/inspectors/rect.rb | 11 | ||||
| -rw-r--r-- | spec/spec_helper/inspectors/text.rb | 1 |
7 files changed, 67 insertions, 1 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 391285e5..eaec6e18 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -24,6 +24,7 @@ Enhancements:: * sort using collation (asciibetical sorting) if ffi-icu gem is available on load path (#928) * 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) Improvements:: diff --git a/docs/modules/ROOT/pages/optimize-pdf.adoc b/docs/modules/ROOT/pages/optimize-pdf.adoc index 90abc678..3579073c 100644 --- a/docs/modules/ROOT/pages/optimize-pdf.adoc +++ b/docs/modules/ROOT/pages/optimize-pdf.adoc @@ -1,5 +1,6 @@ = Optimize the PDF -:url-hexapdf: https://hexapdf.gettalong.org/ +:url-hexapdf: https://hexapdf.gettalong.org +:url-gs-pdfwrite: https://ghostscript.com/doc/current/VectorDevices.htm#PDFWRITE By default, Asciidoctor PDF does not optimize the PDF it generates or compresses its streams. This page covers several approaches you can take to optimize your PDF. @@ -61,6 +62,12 @@ Here's how we set the `GS` environment variable using PowerShell in CI: $ echo "$(& where.exe /R 'C:\Program Files\gs' gswin64c.exe)" | Out-File -FilePath $env:GS -Encoding utf8 -Append +If you want to pass additional {url-gs-pdfwrite}[command-line arguments to Ghostscript], you can use the `GS_OPTIONS` environment variable. +The optimizer expects the value to be a space-separated list of arguments (e.g., `-dBlackText`). +For example, here's how you would get the optimizer to generate a grayscale PDF with vectorized text: + + $ GS_OPTIONS='-sColorConversionStrategy=Gray -dNoOutputFonts' asciidoctor-pdf -a optimize filename.adoc + 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 e86366e1..feea3efe 100644 --- a/lib/asciidoctor/pdf/optimizer/rghost.rb +++ b/lib/asciidoctor/pdf/optimizer/rghost.rb @@ -29,6 +29,8 @@ end) module Asciidoctor module PDF class Optimizer::RGhost < Optimizer::Base + DEFAULT_PARAMS = %w(gs -dNOPAUSE -dBATCH -dQUIET -dNOPAGEPROMPT) + # see https://www.ghostscript.com/doc/current/VectorDevices.htm#PSPDF_IN for details (QUALITY_NAMES = { 'default' => :default, @@ -43,6 +45,11 @@ module Asciidoctor if (gs_path = ::ENV['GS']) ::RGhost::Config::GS[:path] = gs_path end + default_params = DEFAULT_PARAMS.drop 0 + if (user_params = ::ENV['GS_OPTIONS']) + (default_params += user_params.split).uniq! + end + ::RGhost::Config::GS[:default_params] = default_params end def optimize_file target diff --git a/spec/fixtures/with-color.adoc b/spec/fixtures/with-color.adoc new file mode 100644 index 00000000..85594ed3 --- /dev/null +++ b/spec/fixtures/with-color.adoc @@ -0,0 +1,16 @@ += Document With Color +:source-highlighter: rouge + +This document has color. + +image::square.svg[] + +.Create a basic PDF document using Prawn +[,ruby] +---- +require 'prawn' + +Prawn::Document.generate 'example.pdf' do + text 'Hello, World!' +end +---- diff --git a/spec/optimizer_spec.rb b/spec/optimizer_spec.rb index 479426b4..e395bf16 100644 --- a/spec/optimizer_spec.rb +++ b/spec/optimizer_spec.rb @@ -151,6 +151,29 @@ describe 'Asciidoctor::PDF::Optimizer', if: (RSpec::ExampleGroupHelpers.gem_avai (expect pdf_info[:Producer]).to include 'Ghostscript' end + it 'should append parameter specified in GS_OPTIONS environment variable', cli: true, visual: true do + env = { 'GS_OPTIONS' => '-dNoOutputFonts' } + out, err, res = run_command asciidoctor_pdf_bin, '-a', 'optimize', '-o', (to_file = output_file 'optimizer-gs-options-single.pdf'), (example_file 'basic-example.adoc'), env: env + (expect res.exitstatus).to be 0 + (expect out).to be_empty + (expect err).to be_empty + pdf = TextInspector.analyze Pathname.new to_file + (expect pdf.text).to be_empty + end + + it 'should append all parameters specified in GS_OPTIONS environment variable', cli: true, visual: true do + env = { 'GS_OPTIONS' => '-sColorConversionStrategy=Gray -dBlackText' } + out, err, res = run_command asciidoctor_pdf_bin, '-a', 'optimize', '-o', (to_file = output_file 'optimizer-gs-options-multiple.pdf'), (fixture_file 'with-color.adoc'), env: env + (expect res.exitstatus).to be 0 + (expect out).to be_empty + (expect err).to be_empty + pdf = TextInspector.analyze Pathname.new to_file + (expect pdf.text.map {|it| it[:font_color] }.uniq).to eql [nil] + rects = (RectInspector.analyze Pathname.new to_file).rectangles + (expect rects).to have_size 1 + (expect rects[0][:fill_color]).to eql '818181' + end + it 'should not crash if quality passed to asciidoctor-pdf-optimize CLI is not recognized', cli: true do input_file = Pathname.new example_file 'basic-example.adoc' to_file = to_pdf_file input_file, 'optimizer-cli-fallback-quality.pdf' diff --git a/spec/spec_helper/inspectors/rect.rb b/spec/spec_helper/inspectors/rect.rb index 0b097cf9..a29f21d8 100644 --- a/spec/spec_helper/inspectors/rect.rb +++ b/spec/spec_helper/inspectors/rect.rb @@ -51,4 +51,15 @@ class RectInspector < PDF::Inspector def set_color_for_stroking_and_special *params @stroke_color = params.size == 4 ? params.map {|it| it * 100 } : params.map {|it| sprintf '%02X', (it.to_f * 255).round }.join end + + # rg + def set_rgb_color_for_nonstroking *params + @fill_color = params.size == 4 ? params.map {|it| it * 100 } : params.map {|it| sprintf '%02X', (it.to_f * 255).round }.join + end + + # g + def set_gray_for_nonstroking *params + params *= 3 + @fill_color = params.size == 4 ? params.map {|it| it * 100 } : params.map {|it| sprintf '%02X', (it.to_f * 255).round }.join + end end diff --git a/spec/spec_helper/inspectors/text.rb b/spec/spec_helper/inspectors/text.rb index a3204fc8..94d2bc0c 100644 --- a/spec/spec_helper/inspectors/text.rb +++ b/spec/spec_helper/inspectors/text.rb @@ -118,6 +118,7 @@ class TextInspector < PDF::Inspector @pages[-1][:text] << accum @cursor = nil else + @text << { string: '', width: 0 } if @text.empty? (accum = @text[-1])[:string] += string accum[:width] += text_width end |
