Sha256: 2a885459d4d8f3658b491e01fce749387c194a98b1ac24bdd08cbe6fabef47fc

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'pathname'
require 'rghost'
require 'tmpdir'

module Asciidoctor
  module PDF
    class Optimizer
      (QUALITY_NAMES = {
        'default' => :default,
        'screen' => :screen,
        'ebook' => :ebook,
        'printer' => :printer,
        'prepress' => :prepress,
      }).default = :default

      def initialize quality = 'default', compatibility_level = '1.4'
        @quality = QUALITY_NAMES[quality]
        @compatibility_level = compatibility_level
      end

      def generate_file target
        ::Dir::Tmpname.create ['asciidoctor-pdf-', '.pdf'] do |tmpfile|
          filename = Pathname.new target
          filename_o = Pathname.new tmpfile
          pdfmark = filename.sub_ext '.pdfmark'
          inputs = pdfmark.file? ? [target, pdfmark.to_s] : target
          (::RGhost::Convert.new inputs).to :pdf,
              filename: filename_o.to_s,
              quality: @quality,
              d: { Printed: false, CannotEmbedFontPolicy: '/Warning', CompatibilityLevel: @compatibility_level }
          begin
            filename_o.rename target
          rescue ::Errno::EXDEV
            filename.binwrite filename_o.binread
            filename_o.unlink
          end
        end
        nil
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
asciidoctor-pdf-1.5.4 lib/asciidoctor/pdf/optimizer.rb
asciidoctor-pdf-1.5.3 lib/asciidoctor/pdf/optimizer.rb
asciidoctor-pdf-1.5.2 lib/asciidoctor/pdf/optimizer.rb
asciidoctor-pdf-1.5.1 lib/asciidoctor/pdf/optimizer.rb
asciidoctor-pdf-1.5.0 lib/asciidoctor/pdf/optimizer.rb
asciidoctor-pdf-1.5.0.rc.3 lib/asciidoctor/pdf/optimizer.rb
asciidoctor-pdf-1.5.0.rc.2 lib/asciidoctor/pdf/optimizer.rb
asciidoctor-pdf-1.5.0.rc.1 lib/asciidoctor/pdf/optimizer.rb