Class: Prawn::SVG::Elements::Use

Inherits:
Base
  • Object
show all
Defined in:
lib/prawn/svg/elements/use.rb

Constant Summary

Constants inherited from Base

Base::COMMA_WSP_REGEXP, Base::MissingAttributesError, Base::PAINT_TYPES, Base::SVG_NAMESPACE, Base::SkipElementError, Base::SkipElementQuietly

Constants included from Attributes::Stroke

Attributes::Stroke::CAP_STYLE_TRANSLATIONS, Attributes::Stroke::JOIN_STYLE_TRANSLATIONS

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes, #base_calls, #calls, #document, #parent_calls, #properties, #source, #state

Instance Method Summary collapse

Methods inherited from Base

#initialize, #name, #parse_and_apply, #process

Methods included from TransformParser

#parse_transform_attribute

Methods included from Attributes::Space

#parse_xml_space_attribute

Methods included from Attributes::Stroke

#parse_stroke_attributes_and_call

Methods included from Attributes::ClipPath

#parse_clip_path_attribute_and_call

Methods included from Attributes::Opacity

#parse_opacity_attributes_and_call

Methods included from Attributes::Transform

#parse_transform_attribute_and_call

Constructor Details

This class inherits a constructor from Prawn::SVG::Elements::Base

Instance Attribute Details

#referenced_element_classObject (readonly)

Returns the value of attribute referenced_element_class.



2
3
4
# File 'lib/prawn/svg/elements/use.rb', line 2

def referenced_element_class
  @referenced_element_class
end

#referenced_element_sourceObject (readonly)

Returns the value of attribute referenced_element_source.



3
4
5
# File 'lib/prawn/svg/elements/use.rb', line 3

def referenced_element_source
  @referenced_element_source
end

Instance Method Details

#applyObject



46
47
48
49
50
# File 'lib/prawn/svg/elements/use.rb', line 46

def apply
  if @x || @y
    add_call_and_enter "translate", x_pixels(@x || 0), -y_pixels(@y || 0)
  end
end

#container?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/prawn/svg/elements/use.rb', line 42

def container?
  true
end

#parseObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/prawn/svg/elements/use.rb', line 5

def parse
  href = href_attribute
  if href.nil?
    raise SkipElementError, "use tag must have an href or xlink:href"
  end

  if href[0..0] != '#'
    raise SkipElementError, "use tag has an href that is not a reference to an id; this is not supported"
  end

  id = href[1..-1]
  referenced_element = @document.elements_by_id[id]

  if referenced_element
    @referenced_element_class = referenced_element.class
    @referenced_element_source = referenced_element.source
  else
    # Perhaps the element is defined further down in the document.  This is not recommended but still valid SVG,
    # so we'll support it with an exception case that's not particularly performant.
    raw_element = REXML::XPath.match(@document.root, %(//*[@id="#{id.gsub('"', '\"')}"])).first

    if raw_element
      @referenced_element_class = Prawn::SVG::Elements::TAG_CLASS_MAPPING[raw_element.name.to_sym]
      @referenced_element_source = raw_element
    end
  end

  if referenced_element_class.nil?
    raise SkipElementError, "no tag with ID '#{id}' was found, referenced by use tag"
  end

  state.inside_use = true

  @x = attributes['x']
  @y = attributes['y']
end

#process_child_elementsObject



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

def process_child_elements
  add_call "save"

  child = referenced_element_class.new(document, referenced_element_source, calls, state.dup)
  child.process

  add_call "restore"
end