class Prawn::SVG::Elements::Base

Constants

COMMA_WSP_REGEXP
MissingAttributesError
PAINT_TYPES
SkipElementError
SkipElementQuietly

Attributes

attributes[R]
base_calls[R]
calls[RW]
document[R]
parent_calls[R]
properties[R]
source[R]
state[R]

Public Class Methods

new(document, source, parent_calls, state) click to toggle source
# File lib/prawn/svg/elements/base.rb, line 27
def initialize(document, source, parent_calls, state)
  @document = document
  @source = source
  @parent_calls = parent_calls
  @state = state
  @base_calls = @calls = []
  @attributes = {}
  @properties = Prawn::SVG::Properties.new

  if source && id = source.attributes["id"]
    document.elements_by_id[id] = self
  end
end

Public Instance Methods

name() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 61
def name
  @name ||= source ? source.name : "???"
end
parse_and_apply() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 46
def parse_and_apply
  parse_standard_attributes
  parse

  apply_calls_from_standard_attributes
  apply

  process_child_elements if container?

  append_calls_to_parent unless computed_properties.display == 'none'
rescue SkipElementQuietly
rescue SkipElementError => e
  @document.warnings << e.message
end
process() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 41
def process
  extract_attributes_and_properties
  parse_and_apply
end

Protected Instance Methods

add_call(name, *arguments) click to toggle source
# File lib/prawn/svg/elements/base.rb, line 88
def add_call(name, *arguments)
  @calls << [name.to_s, arguments, []]
end
add_call_and_enter(name, *arguments) click to toggle source
# File lib/prawn/svg/elements/base.rb, line 92
def add_call_and_enter(name, *arguments)
  @calls << [name.to_s, arguments, []]
  @calls = @calls.last.last
end
add_calls_from_element(other) click to toggle source
# File lib/prawn/svg/elements/base.rb, line 110
def add_calls_from_element(other)
  @calls.concat duplicate_calls(other.base_calls)
end
append_calls_to_parent() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 106
def append_calls_to_parent
  @parent_calls.concat(@base_calls)
end
apply() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 70
def apply
end
apply_calls_from_standard_attributes() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 138
def apply_calls_from_standard_attributes
  parse_transform_attribute_and_call
  parse_opacity_attributes_and_call
  parse_clip_path_attribute_and_call
  apply_colors
  parse_stroke_attributes_and_call
  apply_drawing_call
end
apply_colors() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 159
def apply_colors
  PAINT_TYPES.each do |type|
    color = properties.send(type)

    next if [nil, 'inherit', 'none'].include?(color)

    if color == 'currentColor'
      color = computed_properties.color
    end

    results = Prawn::SVG::Color.parse(color, document.gradients)

    success = results.detect do |result|
      case result
      when Prawn::SVG::Color::Hex
        add_call "#{type}_color", result.value
        true
      when Prawn::SVG::Elements::Gradient
        arguments = result.gradient_arguments(self)
        if arguments
          add_call "#{type}_gradient", **arguments
          true
        end
      end
    end

    # If we were unable to find a suitable color candidate,
    # we turn off this type of paint.
    if success.nil?
      computed_properties.set(type, 'none')
    end
  end
end
apply_drawing_call() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 147
def apply_drawing_call
  if !state.disable_drawing && drawable?
    draw_types = PAINT_TYPES.select { |property| computed_properties.send(property) != 'none' }

    if draw_types.empty?
      add_call_and_enter("end_path")
    else
      add_call_and_enter(draw_types.join("_and_"))
    end
  end
end
bounding_box() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 73
def bounding_box
end
clamp(value, min_value, max_value) click to toggle source
# File lib/prawn/svg/elements/base.rb, line 193
def clamp(value, min_value, max_value)
  [[value, min_value].max, max_value].min
end
container?() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 76
def container?
  false
end
drawable?() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 80
def drawable?
  !container?
end
extract_attributes_and_properties() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 197
def extract_attributes_and_properties
  if @document && @document.css_parser
    tag_style = @document.css_parser.find_by_selector(source.name)
    id_style = @document.css_parser.find_by_selector("##{source.attributes["id"]}") if source.attributes["id"]

    if classes = source.attributes["class"]
      class_styles = classes.strip.split(/\s+/).collect do |class_name|
        @document.css_parser.find_by_selector(".#{class_name}")
      end
    end

    element_style = source.attributes['style']

    style = [tag_style, class_styles, id_style, element_style].flatten.collect do |s|
      s.nil? || s.strip == "" ? "" : "#{s}#{";" unless s.match(/;\s*\z/)}"
    end.join
  else
    style = source.attributes['style'] || ""
  end

  source.attributes.each do |name, value|
    # Properties#set returns nil if it's not a recognised property name
    @properties.set(name, value) or @attributes[name] = value
  end

  @properties.load_hash(parse_css_declarations(style))

  state.computed_properties.compute_properties(@properties)
end
extract_element_from_url_id_reference(value, expected_type = nil) click to toggle source
# File lib/prawn/svg/elements/base.rb, line 254
def extract_element_from_url_id_reference(value, expected_type = nil)
  matches = value.strip.match(/\Aurl\(\s*#(\S+)\s*\)\z/i) if value
  element = document.elements_by_id[matches[1]] if matches
  element if element && (expected_type.nil? || element.name == expected_type)
end
new_call_context_from_base() { || ... } click to toggle source
# File lib/prawn/svg/elements/base.rb, line 114
def new_call_context_from_base
  old_calls = @calls
  @calls = @base_calls
  yield
  @calls = old_calls
end
parse() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 67
def parse
end
parse_css_declarations(declarations) click to toggle source
# File lib/prawn/svg/elements/base.rb, line 227
def parse_css_declarations(declarations)
  # copied from css_parser
  declarations.gsub!(/(^[\s]*)|([\s]*$)/, '')

  output = {}
  declarations.split(/[\;$]+/m).each do |decs|
    if matches = decs.match(/\s*(.[^:]*)\s*\:\s*(.[^;]*)\s*(;|\Z)/i)
      property, value, _ = matches.captures
      output[property.downcase] = value
    end
  end
  output
end
parse_standard_attributes() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 84
def parse_standard_attributes
  parse_xml_space_attribute
end
pop_call_position() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 102
def pop_call_position
  @calls = @call_positions.pop
end
process_child_elements() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 121
def process_child_elements
  return unless source

  source.elements.each do |elem|
    if element_class = Prawn::SVG::Elements::TAG_CLASS_MAPPING[elem.name.to_sym]
      add_call "save"

      child = element_class.new(@document, elem, @calls, state.dup)
      child.process

      add_call "restore"
    else
      @document.warnings << "Unknown tag '#{elem.name}'; ignoring"
    end
  end
end
push_call_position() click to toggle source
# File lib/prawn/svg/elements/base.rb, line 97
def push_call_position
  @call_positions ||= []
  @call_positions << @calls
end
require_attributes(*names) click to toggle source
# File lib/prawn/svg/elements/base.rb, line 241
def require_attributes(*names)
  missing_attrs = names - attributes.keys
  if missing_attrs.any?
    raise MissingAttributesError, "Must have attributes #{missing_attrs.join(", ")} on tag #{name}; skipping tag"
  end
end
require_positive_value(*args) click to toggle source
# File lib/prawn/svg/elements/base.rb, line 248
def require_positive_value(*args)
  if args.any? {|arg| arg.nil? || arg <= 0}
    raise SkipElementError, "Invalid attributes on tag #{name}; skipping tag"
  end
end