Class: Batik::Transcoders::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-batik/transcoders/abstract.rb

Direct Known Subclasses

JPEG, PNG, TIFF

Class Method Summary collapse

Class Method Details

.default_optionsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby-batik/transcoders/abstract.rb', line 18

def self.default_options
  {
    :execute_onload => nil, # true/false
    :allowed_script_types => nil, # text/ecmascript, application/java-archive
    :constrain_script_origin => nil, # true/false
    :snapshot_time => nil,
    :pixel_unit_to_millimeter => 0.264583, # Size of a px in millimeters
    :user_stylesheet_uri => nil,
    :alternate_stylesheet => nil,
    :default_font_family => nil,
    :media => nil,
    :language => nil,
    :max_height => nil,
    :max_width => nil,
    :height => nil,
    :width => nil,
  }
end

.java_typesObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/ruby-batik/transcoders/abstract.rb', line 7

def self.java_types
  {
    :snapshot_time => Float,
    :pixel_unit_to_millimeter => Float,
    :max_height => Float,
    :max_width => Float,
    :height => Float,
    :width => Float,
  }
end

.transcode(document, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby-batik/transcoders/abstract.rb', line 37

def self.transcode(document, options={})
  options = default_options.merge(process_options(options.dup))
  in_io = StringIO.new(document)
  out_io = StringIO.new('','w+b')

  transcoder = klass.new
  transcoder.setErrorHandler(Batik::DefaultErrorHandler.new)
  options.each_pair do |key, val|
    next if val.nil?

    transcoder.addTranscodingHint(transcoder.class.const_get("key_#{key}".upcase), val.to_java(java_type_for(key)))
  end

  transcoder.transcode(TranscoderInput.new(in_io.to_inputstream), TranscoderOutput.new(out_io.to_outputstream))
  out_io.flush

  # Sometimes this is closed for read. If so, we must use the #string method
  if out_io.closed_read?
    out_io.string
  else
    out_io.rewind
    out_io.read
  end
end