Sha256: 4854dafa87c7074f99fe52054fb8e5ae61c8c27b2cfb4db4027cdf722fdf9c49

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

module Prawn::SVG
  class Gradients
    def initialize(document)
      @document = document
      @gradients_by_id = {}
    end

    def [](id)
      id &&= id.strip
      return unless id && id != ''

      if (element = @gradients_by_id[id])
        element
      elsif (raw_element = find_raw_gradient_element_by_id(id))
        create_gradient_element(raw_element)
      end
    end

    def []=(id, gradient)
      @gradients_by_id[id] = gradient
    end

    private

    def find_raw_gradient_element_by_id(id)
      raw_element = find_raw_element_by_id(id)
      raw_element if gradient_element?(raw_element)
    end

    def create_gradient_element(raw_element)
      Elements::Gradient.new(@document, raw_element, [], new_state).tap(&:process)
    end

    def find_raw_element_by_id(id)
      REXML::XPath.match(@document.root, %(//*[@id="#{id.gsub('"', '\"')}"])).first
    end

    def gradient_element?(raw_element)
      return false if raw_element.nil? || raw_element.name.nil?

      Elements::TAG_CLASS_MAPPING[raw_element.name.to_sym] == Elements::Gradient
    end

    def new_state
      State.new.tap do |state|
        state.viewport_sizing = @document.sizing
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
prawn-svg-0.36.0 lib/prawn/svg/gradients.rb
prawn-svg-0.35.1 lib/prawn/svg/gradients.rb
prawn-svg-0.35.0 lib/prawn/svg/gradients.rb