Class: Vectory::Svg

Inherits:
Vector show all
Defined in:
lib/vectory/svg.rb

Constant Summary collapse

SVG_NS =
"http://www.w3.org/2000/svg"

Instance Attribute Summary

Attributes inherited from Vector

#initial_path

Attributes inherited from Image

#content

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Vector

#file_size, from_datauri, from_path, #mime, #path, #size, #to_uri, #write

Methods inherited from Image

from_content, from_path

Constructor Details

#initialize(content = nil, initial_path = nil) ⇒ Svg

Returns a new instance of Svg.



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

def initialize(content = nil, initial_path = nil)
  super

  self.content = content
end

Class Method Details

.default_extensionObject



9
10
11
# File 'lib/vectory/svg.rb', line 9

def self.default_extension
  "svg"
end

.from_node(node) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/vectory/svg.rb', line 17

def self.from_node(node)
  if node.elements&.first&.name == "svg"
    return from_content(node.children.to_xml)
  end

  uri = node["src"]
  return Vectory::Datauri.new(uri).to_vector if %r{^data:}.match?(uri)

  from_path(uri)
end

.mimetypeObject



13
14
15
# File 'lib/vectory/svg.rb', line 13

def self.mimetype
  "image/svg+xml"
end

Instance Method Details

#heightObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vectory/svg.rb', line 61

def height
  # Try to read height from SVG attributes first
  doc = Nokogiri::XML(content)
  svg_element = doc.at_xpath("//svg:svg",
                             "svg" => SVG_NS) || doc.at_xpath("//svg")

  if svg_element && svg_element["height"]
    svg_element["height"].to_f.round
  else
    # Fall back to Inkscape query if no height attribute
    super
  end
end

#to_emfObject



34
35
36
37
38
39
40
41
# File 'lib/vectory/svg.rb', line 34

def to_emf
  InkscapeWrapper.convert(
    content: content,
    input_format: :svg,
    output_format: :emf,
    output_class: Emf,
  )
end

#to_epsObject



43
44
45
46
47
48
49
50
# File 'lib/vectory/svg.rb', line 43

def to_eps
  InkscapeWrapper.convert(
    content: content,
    input_format: :svg,
    output_format: :eps,
    output_class: Eps,
  )
end

#to_psObject



52
53
54
55
56
57
58
59
# File 'lib/vectory/svg.rb', line 52

def to_ps
  InkscapeWrapper.convert(
    content: content,
    input_format: :svg,
    output_format: :ps,
    output_class: Ps,
  )
end

#widthObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vectory/svg.rb', line 75

def width
  # Try to read width from SVG attributes first
  doc = Nokogiri::XML(content)
  svg_element = doc.at_xpath("//svg:svg",
                             "svg" => SVG_NS) || doc.at_xpath("//svg")

  if svg_element && svg_element["width"]
    svg_element["width"].to_f.round
  else
    # Fall back to Inkscape query if no width attribute
    super
  end
end