Class: EmbedXMP::SVG

Inherits:
ImageFile show all
Defined in:
lib/embed_xmp/svg.rb

Overview

SVG images

Constant Summary collapse

SVG_NAMESPACE =
'http://www.w3.org/2000/svg'

Instance Method Summary collapse

Methods inherited from ImageFile

#insert_into_file, #read_io_or_string

Constructor Details

#initialize(input_file) ⇒ SVG

Returns a new instance of SVG.



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

def initialize(input_file)
  file = read_io_or_string(input_file)
  svg = Nokogiri::XML(file) do |conf|
    conf.options = Nokogiri::XML::ParseOptions::NOBLANKS
  end

  raise 'NoSVGnamespace' if SVG_NAMESPACE != svg.root.namespace.href

  @image_file = svg
end

Instance Method Details

#join_sidecar(sidecar_file, xpacked: false) ⇒ Object

Join an XMP sidecar file into an SVG image file.



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

def join_sidecar(sidecar_file, xpacked: false)
  remove_xmp

  xmp_data = read_io_or_string(sidecar_file)
  ((xmp_data, xpacked))
end

#remove_xmpObject

Removes XMP metadata in /svg/metadata/rdf:RDF. Will only detect XMP data wrapped in an XPACKET processing instruction.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/embed_xmp/svg.rb', line 37

def remove_xmp
  @image_file.root.xpath('//svg:metadata', 'svg' => 'http://www.w3.org/2000/svg')
    .each do ||
    xml_pi = .xpath('processing-instruction()')

    if xml_pi.empty? ||
       xml_pi.first.content != EmbedXMP::XMP::XPACKET_PDATA
      next
    end

    .remove
  end
end

#write(output_file) ⇒ Object



51
52
53
54
# File 'lib/embed_xmp/svg.rb', line 51

def write(output_file)
  data = @image_file.to_xml(encoding: 'utf-8', indent: 2)
  super(output_file, data: data)
end