Class: Vectory::Datauri

Inherits:
Image
  • Object
show all
Defined in:
lib/vectory/datauri.rb

Constant Summary collapse

REGEX =
%r{^
  data:
  (?<mimetype>[^;]+);
  (?:charset=[^;]+;)?
  base64,
  (?<data>.+)
$}x.freeze

Instance Attribute Summary

Attributes inherited from Image

#content

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Image

from_content, from_path, #initialize

Constructor Details

This class inherits a constructor from Vectory::Image

Class Method Details

.from_vector(vector) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vectory/datauri.rb', line 13

def self.from_vector(vector)
  mimetype = vector.class.mimetype
  content = vector.content

  # Normalize line endings for text-based types before encoding
  # Apply to PS, EPS, and SVG. EMF is binary, so skip it.
  if ["application/postscript", "image/svg+xml"].include?(vector.mime)
    content = content.gsub("\r\n", "\n")
  end

  data = Base64.strict_encode64(content)

  new("data:#{mimetype};base64,#{data}")
end

Instance Method Details

#heightObject



33
34
35
# File 'lib/vectory/datauri.rb', line 33

def height
  to_vector.height
end

#mimeObject



28
29
30
31
# File 'lib/vectory/datauri.rb', line 28

def mime
  match = parse_datauri(@content)
  match[:mimetype]
end

#to_vectorObject



41
42
43
44
45
46
47
# File 'lib/vectory/datauri.rb', line 41

def to_vector
  match = parse_datauri(@content)
  content = Base64.strict_decode64(match[:data])
  image_class = detect_image_class(match[:mimetype])

  image_class.from_content(content)
end

#widthObject



37
38
39
# File 'lib/vectory/datauri.rb', line 37

def width
  to_vector.width
end