Class: Quilt::ImageSVG

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

Instance Method Summary collapse

Constructor Details

#initialize(width, height, opt = {}) ⇒ ImageSVG

Returns a new instance of ImageSVG.



7
8
9
10
11
12
13
14
15
# File 'lib/quilt.rb', line 7

def initialize width, height, opt = {}
  @transparent = !!opt[:transparent]
  @mask = @transparent ? %(mask="clip") : ''
  @masks = ''
  @image = ''
  @width = width
  @height = height
  @head = %(<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 #{width} #{height}">\n)
end

Instance Method Details

#color(r, g, b) ⇒ Object



17
18
19
# File 'lib/quilt.rb', line 17

def color r, g, b
  "rgb(%d, %d, %d)" % [r, g, b]
end

#fill_rect(x, y, _x, _y, color) ⇒ Object



21
22
23
# File 'lib/quilt.rb', line 21

def fill_rect x, y, _x, _y, color
  @image << %(<rect x="#{x}" y="#{y}" width="#{_x - x}" height="#{_y - y}" fill="#{color}" stroke-width="0" stroke="white" mask="url(#clip)"/>\n)
end

#polygon(points, color) ⇒ Object



25
26
27
28
29
30
# File 'lib/quilt.rb', line 25

def polygon points, color
  unless points.empty?
    ps = points.map { |i| i.join(',') }.join(' ')
    @image << %(<polygon points="#{ps}" stroke-width="0" fill="#{color}" mask="(#clip)"/>\n)
  end
end

#polygon_clip(points) ⇒ Object



32
33
34
35
36
37
# File 'lib/quilt.rb', line 32

def polygon_clip points
  unless points.empty?
    ps = points.map { |i| i.join(',') }.join(' ')
    @masks << %(<polygon points="#{ps}" stroke-width="0"/>\n)
  end
end

#to_blobObject



43
44
45
46
47
48
49
# File 'lib/quilt.rb', line 43

def to_blob
  if @transparent
    @head + build_mask + @image + "\n</svg>"
  else
    @head + @image + "\n</svg>"
  end
end

#write(path) ⇒ Object



39
40
41
# File 'lib/quilt.rb', line 39

def write path
  open(path, 'wb') {|f| f.write self.to_blob }
end