Class: Postsvg::Svg::ClipPathRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/postsvg/svg/clip_path_registry.rb

Overview

Indexes definitions in a document so handlers can resolve url(#id) references without re-walking the tree.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(by_id = {}) ⇒ ClipPathRegistry

Returns a new instance of ClipPathRegistry.



10
11
12
13
# File 'lib/postsvg/svg/clip_path_registry.rb', line 10

def initialize(by_id = {})
  @by_id = by_id.dup.freeze
  freeze
end

Instance Attribute Details

#by_idObject (readonly)

Returns the value of attribute by_id.



8
9
10
# File 'lib/postsvg/svg/clip_path_registry.rb', line 8

def by_id
  @by_id
end

Class Method Details

.emptyObject



15
16
17
# File 'lib/postsvg/svg/clip_path_registry.rb', line 15

def self.empty
  @empty ||= new({})
end

.from_node(root) ⇒ Object

Build a registry by scanning root for children.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/postsvg/svg/clip_path_registry.rb', line 28

def self.from_node(root)
  registry = {}
  root.xpath(".//*[local-name()='clipPath']").each do |node|
    id = node["id"]
    next unless id

    # Concatenate the d attributes of all child <path> elements
    # in document order. Other shape children are converted to
    # path data lazily; for now we only support <path>.
    ds = node.xpath(".//*[local-name()='path']").map { |p| p["d"] }.compact
    registry[id] = ds.join(" ") unless ds.empty?
  end
  new(registry)
end

Instance Method Details

#key?(id) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/postsvg/svg/clip_path_registry.rb', line 23

def key?(id)
  @by_id.key?(id)
end

#lookup(id) ⇒ Object



19
20
21
# File 'lib/postsvg/svg/clip_path_registry.rb', line 19

def lookup(id)
  @by_id[id]
end