Class: Postsvg::PathBuilder

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

Overview

Builds SVG path strings with optional coordinate transformations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePathBuilder

Returns a new instance of PathBuilder.



10
11
12
13
14
# File 'lib/postsvg/path_builder.rb', line 10

def initialize
  @parts = []
  @use_local_coords = false
  @ctm = nil
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



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

def parts
  @parts
end

Instance Method Details

#clearObject



77
78
79
# File 'lib/postsvg/path_builder.rb', line 77

def clear
  @parts = []
end

#closeObject



65
66
67
# File 'lib/postsvg/path_builder.rb', line 65

def close
  @parts << "Z"
end

#curve_to(x1, y1, x2, y2, x, y) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/postsvg/path_builder.rb', line 41

def curve_to(x1, y1, x2, y2, x, y)
  p1 = transform_point(x1, y1)
  p2 = transform_point(x2, y2)
  p = transform_point(x, y)
  @parts << "C #{num_fmt(p1[:x])} #{num_fmt(p1[:y])} " \
            "#{num_fmt(p2[:x])} #{num_fmt(p2[:y])} " \
            "#{num_fmt(p[:x])} #{num_fmt(p[:y])}"
end

#curve_to_rel(dx1, dy1, dx2, dy2, dx, dy) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/postsvg/path_builder.rb', line 50

def curve_to_rel(dx1, dy1, dx2, dy2, dx, dy)
  p1 = transform_point(dx1, dy1)
  p2 = transform_point(dx2, dy2)
  p = transform_point(dx, dy)
  @parts << "c #{num_fmt(p1[:x])} #{num_fmt(p1[:y])} " \
            "#{num_fmt(p2[:x])} #{num_fmt(p2[:y])} " \
            "#{num_fmt(p[:x])} #{num_fmt(p[:y])}"
end

#ellipse_to(rx, ry, rotation, large_arc, sweep, x, y) ⇒ Object



59
60
61
62
63
# File 'lib/postsvg/path_builder.rb', line 59

def ellipse_to(rx, ry, rotation, large_arc, sweep, x, y)
  p = transform_point(x, y)
  @parts << "A #{num_fmt(rx)} #{num_fmt(ry)} #{num_fmt(rotation)} " \
            "#{large_arc} #{sweep} #{num_fmt(p[:x])} #{num_fmt(p[:y])}"
end

#lengthObject



73
74
75
# File 'lib/postsvg/path_builder.rb', line 73

def length
  @parts.length
end

#line_to(x, y) ⇒ Object



31
32
33
34
# File 'lib/postsvg/path_builder.rb', line 31

def line_to(x, y)
  p = transform_point(x, y)
  @parts << "L #{num_fmt(p[:x])} #{num_fmt(p[:y])}"
end

#line_to_rel(dx, dy) ⇒ Object



36
37
38
39
# File 'lib/postsvg/path_builder.rb', line 36

def line_to_rel(dx, dy)
  p = transform_point(dx, dy)
  @parts << "l #{num_fmt(p[:x])} #{num_fmt(p[:y])}"
end

#move_to(x, y) ⇒ Object



21
22
23
24
# File 'lib/postsvg/path_builder.rb', line 21

def move_to(x, y)
  p = transform_point(x, y)
  @parts << "M #{num_fmt(p[:x])} #{num_fmt(p[:y])}"
end

#move_to_rel(dx, dy) ⇒ Object



26
27
28
29
# File 'lib/postsvg/path_builder.rb', line 26

def move_to_rel(dx, dy)
  p = transform_point(dx, dy)
  @parts << "m #{num_fmt(p[:x])} #{num_fmt(p[:y])}"
end

#resetObject



81
82
83
84
85
# File 'lib/postsvg/path_builder.rb', line 81

def reset
  new_path = PathBuilder.new
  new_path.set_transform_mode(@use_local_coords, @ctm)
  new_path
end

#set_transform_mode(use_local, transform = nil) ⇒ Object



16
17
18
19
# File 'lib/postsvg/path_builder.rb', line 16

def set_transform_mode(use_local, transform = nil)
  @use_local_coords = use_local
  @ctm = transform
end

#to_pathObject



69
70
71
# File 'lib/postsvg/path_builder.rb', line 69

def to_path
  @parts.join(" ")
end