Class: Nutils::Filters::SvgToPng

Inherits:
Nanoc3::Filter
  • Object
show all
Includes:
FileUtils
Defined in:
lib/nutils/filters/svg2png.rb

Overview

Note:

Requires «rjb» and «batik»

Author:

  • Arnau Siches

  • Choan Gálvez

Version:

  • 0.3.1

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ SvgToPng

Returns a new instance of SvgToPng.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nutils/filters/svg2png.rb', line 19

def initialize(hash = {})
  
  mkdir_p 'tmp'
  
  Rjb::load(classpath = '.', [ '-Djava.awt.headless=true', '-Dapple.awt.graphics.UseQuartz=false' ])
  @fileOutputStream = Rjb::import("java.io.FileOutputStream")
  @pngTranscoder = Rjb::import("org.apache.batik.transcoder.image.PNGTranscoder")
  @transcoderInput = Rjb::import("org.apache.batik.transcoder.TranscoderInput")
  @transcoderOutput = Rjb::import("org.apache.batik.transcoder.TranscoderOutput")
  @float = Rjb::import("java.lang.Float")
  @integer = Rjb::import("java.lang.Integer")
  @color = Rjb::import("java.awt.Color")
  @@_initialized = true
  super
end

Instance Method Details

#run(content, params = {}) ⇒ String

Runs the content through / Batik. Batik must be in the ‘CLASSPATH`.

Parameters:

  • content (String)

    The content to filter.

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :background_color (String) — default: nil

    The background color with the form #FFCC00 or FC0 of the result. Mask for the KEY_BACKGROUND_COLOR key.

  • :dpi (Integer) — default: 72

    This parameter lets you use the pixel to millimeter conversion factor. This factor is used to determine how units are converted into pixels. Mask for the KEY_PIXEL_TO_MM key.

  • :depth (Integer) — default: nil

    The color bit depth (i.e. 1, 2, 4, 8). The resultant PNG will be an indexed PNG with color bit depth specified. The height of the result. Mask for the KEY_INDEXED key.

  • :gamma (Float) — default: 0

    The gamma correction of the result. Mask for the KEY_GAMMA key.

  • :heigth (Integer) — default: nil

    The height of the result. Mask for the KEY_HEIGHT key.

  • :width (Integer) — default: nil

    The width of the result. Mask for the KEY_WIDTH key.

Returns:

  • (String)

    The filtered content.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/nutils/filters/svg2png.rb', line 63

def run(content, params = {})
  t = @pngTranscoder.new

  opts = {
    :background_color => nil,
    :dpi => 72,
    :height => nil,
    :width => nil,
    :gamma => 0,
    :depth => nil,
  }.merge(params)

  t.addTranscodingHint(@pngTranscoder.KEY_WIDTH, @float.new(opts[:width])) if opts[:width]
  t.addTranscodingHint(@pngTranscoder.KEY_HEIGHT, @float.new(opts[:height])) if opts[:height]
  t.addTranscodingHint(@pngTranscoder.KEY_PIXEL_TO_MM, @float.new(254 / opts[:dpi])) if opts[:dpi]
  t.addTranscodingHint(@pngTranscoder.KEY_BACKGROUND_COLOR, @color.new(hex2int(opts[:background_color]))) if opts[:background_color] and opts[:background_color] != 'transparent'
  # t.addTranscodingHint(@pngTranscoder.KEY_BACKGROUND_COLOR, @color.decode('#FC0'))

  t.addTranscodingHint(@pngTranscoder.KEY_GAMMA, @float.new(opts[:gamma]))
  t.addTranscodingHint(@pngTranscoder.KEY_INDEXED, @integer.new(opts[:depth])) if opts[:depth]


  tempfile = Tempfile.new(filename.gsub(/[^a-zA-Z0-9]/, '-'), 'tmp')
  temppath = tempfile.path
  tempfile.puts content
  tempfile.close

  uri = "file:" + File.expand_path(temppath)

  input = @transcoderInput.new(uri)
  ostream = @fileOutputStream.new(output_filename)
  output = @transcoderOutput.new(ostream)
  t.transcode(input, output)

  ostream.flush()
  ostream.close()
end