Class: Prawn::Svg::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/svg/parser.rb

Overview

Prawn::Svg::Parser is responsible for parsing an SVG file and converting it into a tree of prawn-compatible method calls.

You probably do not want to use this class directly. Instead, use Prawn::Svg to draw SVG data to your Prawn::Document object.

This class is not passed the prawn object, so knows nothing about prawn specifically - this might be useful if you want to take this code and use it to convert SVG to another format.

Defined Under Namespace

Classes: Image, Path, Text

Constant Summary collapse

CONTAINER_TAGS =
%w(g svg symbol defs clipPath)

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Parser

Construct a Parser object.

The data argument is SVG data.

bounds is a tuple [width, height] that specifies the bounds of the drawing space in points.

options can optionally contain the key :width or :height. If both are specified, only :width will be used.


27
28
29
# File 'lib/prawn/svg/parser.rb', line 27

def initialize(document)
  @document = document
end

Instance Method Details

#parseObject

Parse the SVG data and return a call tree. The returned Array is in the format:

[
  ['prawn_method_name', ['argument1', 'argument2'], []],
  ['method_that_takes_a_block', ['argument1', 'argument2'], [
    ['method_called_inside_block', ['argument'], []]
  ]
]

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/prawn/svg/parser.rb', line 41

def parse
  @document.warnings.clear

  calls = [['fill_color', '000000', []]]

  calls << [
    'transformation_matrix',
    [@document.sizing.x_scale, 0, 0, @document.sizing.y_scale, 0, 0],
    []
  ]

  calls << [
    'transformation_matrix',
    [1, 0, 0, 1, @document.sizing.x_offset, @document.sizing.y_offset],
    []
  ]

  root_element = Prawn::Svg::Element.new(@document, @document.root, calls, :ids => {}, :fill => true)

  parse_element(root_element)
  calls
end