Class: SVG::Picture

Inherits:
Object
  • Object
show all
Includes:
ArrayMixin
Defined in:
lib/ruby-svg/core.rb

Overview

#

Picture Class

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ArrayMixin

#<<, #[], #[]=, #clear, #each, #first, #last

Constructor Details

#initialize(width, height, view_box = nil) ⇒ Picture

Returns a new instance of Picture.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby-svg/core.rb', line 21

def initialize(width, height, view_box = nil)
  @width    = width
  @height   = height
  @x        = nil
  @y        = nil
  @view_box = view_box
  @title    = nil
  @desc     = nil

  @elements = []
  @styles   = []
  @scripts  = []
end

Instance Attribute Details

#descObject

Returns the value of attribute desc.



36
37
38
# File 'lib/ruby-svg/core.rb', line 36

def desc
  @desc
end

#elementsObject (readonly)

Returns the value of attribute elements.



35
36
37
# File 'lib/ruby-svg/core.rb', line 35

def elements
  @elements
end

#heightObject

Returns the value of attribute height.



36
37
38
# File 'lib/ruby-svg/core.rb', line 36

def height
  @height
end

#scriptsObject (readonly)

Returns the value of attribute scripts.



35
36
37
# File 'lib/ruby-svg/core.rb', line 35

def scripts
  @scripts
end

#stylesObject (readonly)

Returns the value of attribute styles.



35
36
37
# File 'lib/ruby-svg/core.rb', line 35

def styles
  @styles
end

#titleObject

Returns the value of attribute title.



36
37
38
# File 'lib/ruby-svg/core.rb', line 36

def title
  @title
end

#view_boxObject

Returns the value of attribute view_box.



36
37
38
# File 'lib/ruby-svg/core.rb', line 36

def view_box
  @view_box
end

#widthObject

Returns the value of attribute width.



36
37
38
# File 'lib/ruby-svg/core.rb', line 36

def width
  @width
end

#xObject

Returns the value of attribute x.



36
37
38
# File 'lib/ruby-svg/core.rb', line 36

def x
  @x
end

#yObject

Returns the value of attribute y.



36
37
38
# File 'lib/ruby-svg/core.rb', line 36

def y
  @y
end

Instance Method Details

#define_style(class_name, style) ⇒ Object



43
44
45
# File 'lib/ruby-svg/core.rb', line 43

def define_style(class_name, style)
  @styles << DefineStyle.new(class_name, style)
end

#mime_typeObject



82
83
84
# File 'lib/ruby-svg/core.rb', line 82

def mime_type
  return 'image/svg+xml'
end

#svgObject



73
74
75
# File 'lib/ruby-svg/core.rb', line 73

def svg
  return self.to_s
end

#svgzObject



77
78
79
80
# File 'lib/ruby-svg/core.rb', line 77

def svgz
  require 'zlib'
  return Deflate.deflate(self.to_s, Deflate::BEST_COMPRESSION)
end

#to_sObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby-svg/core.rb', line 47

def to_s
  text  = %|<?xml version="1.0" standalone="no"?>\n|
  text << %|<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">\n|
  text << %|<svg width="#{@width}" height="#{@height}"|
  text << %| viewBox="#{@view_box}"| if @view_box
  text << %|>\n|

  @scripts.each { |script|
    text << script.to_s
  }

  unless @styles.empty?
    text << %|<defs>\n|
    text << %|<style type="text/css"><![CDATA[\n|
    text << @styles.collect { |define| define.to_s + "\n" }.join
    text << %|]]></style>\n|
    text << %|</defs>\n|
  end

  text << %|<title>#{@title}</title>\n| if @title
  text << %|<desc>#{@desc}</desc>\n|    if @desc
  text << @elements.collect { |element| element.to_s + "\n" }.join
  text << %|</svg>\n|
  return text
end