Class: Emfsvg::Svg::Elements::Image

Inherits:
Emfsvg::Svg::Element show all
Defined in:
lib/emfsvg/svg/elements/image.rb

Overview

v1: data: URIs only (PNG, JPEG, base64-encoded). File/http URIs are out of scope.

Constant Summary collapse

ELEMENT_NAME =
"image"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Emfsvg::Svg::Element

#children, #element_name, register

Constructor Details

#initialize(x:, y:, width:, height:, href:, fill: nil, stroke: nil, clip_path: nil) ⇒ Image

Returns a new instance of Image.



15
16
17
18
19
20
21
22
23
24
# File 'lib/emfsvg/svg/elements/image.rb', line 15

def initialize(x:, y:, width:, height:, href:, fill: nil, stroke: nil, clip_path: nil)
  @x = x
  @y = y
  @width = width
  @height = height
  @href = href
  @fill = fill
  @stroke = stroke
  @clip_path = clip_path
end

Instance Attribute Details

#clip_pathObject (readonly)

Returns the value of attribute clip_path.



13
14
15
# File 'lib/emfsvg/svg/elements/image.rb', line 13

def clip_path
  @clip_path
end

#fillObject (readonly)

Returns the value of attribute fill.



13
14
15
# File 'lib/emfsvg/svg/elements/image.rb', line 13

def fill
  @fill
end

#heightObject (readonly)

Returns the value of attribute height.



13
14
15
# File 'lib/emfsvg/svg/elements/image.rb', line 13

def height
  @height
end

#hrefObject (readonly)

Returns the value of attribute href.



13
14
15
# File 'lib/emfsvg/svg/elements/image.rb', line 13

def href
  @href
end

#strokeObject (readonly)

Returns the value of attribute stroke.



13
14
15
# File 'lib/emfsvg/svg/elements/image.rb', line 13

def stroke
  @stroke
end

#widthObject (readonly)

Returns the value of attribute width.



13
14
15
# File 'lib/emfsvg/svg/elements/image.rb', line 13

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



13
14
15
# File 'lib/emfsvg/svg/elements/image.rb', line 13

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



13
14
15
# File 'lib/emfsvg/svg/elements/image.rb', line 13

def y
  @y
end

Class Method Details

.from_node(node) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/emfsvg/svg/elements/image.rb', line 26

def self.from_node(node)
  href = node["xlink:href"] || node["href"]
  new(
    x: AttributeParser.float(node["x"]),
    y: AttributeParser.float(node["y"]),
    width: AttributeParser.float(node["width"]),
    height: AttributeParser.float(node["height"]),
    href: href,
    **Stylable.parse_style(node)
  )
end

Instance Method Details

#base64?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/emfsvg/svg/elements/image.rb', line 59

def base64?
  href.include?(";base64,")
end

#data_uri?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/emfsvg/svg/elements/image.rb', line 38

def data_uri?
  href&.start_with?("data:")
end

#decoded_bytesObject



49
50
51
52
53
54
55
56
57
# File 'lib/emfsvg/svg/elements/image.rb', line 49

def decoded_bytes
  return nil unless data_uri?

  match = href.match(/\Adata:[^;,]+(?:;[^,]*)*,(.*)\z/m)
  return nil unless match

  payload = match[1]
  base64? ? Base64.decode64(payload) : payload
end

#mime_typeObject



42
43
44
45
46
47
# File 'lib/emfsvg/svg/elements/image.rb', line 42

def mime_type
  return nil unless data_uri?

  match = href.match(/\Adata:([^;,]+)/)
  match ? match[1] : nil
end