File: svg_scene.rb

package info (click to toggle)
ruby-rubyvis 0.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,808 kB
  • ctags: 679
  • sloc: ruby: 11,114; makefile: 2
file content (217 lines) | stat: -rw-r--r-- 5,501 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
require 'rubyvis/scene/svg_panel'
require 'rubyvis/scene/svg_bar'
require 'rubyvis/scene/svg_rule'
require 'rubyvis/scene/svg_label'
require 'rubyvis/scene/svg_line'
require 'rubyvis/scene/svg_dot'
require 'rubyvis/scene/svg_area'
require 'rubyvis/scene/svg_wedge'
require 'rubyvis/scene/svg_image'
require 'rubyvis/scene/svg_curve'

class REXML::Element #:nodoc:
  attr_accessor :_scene
  # 1 number based
  def get_element(i)
    elements[i]
  end
  def set_attributes(h)
    h.each do |k,v|
      attributes[k]=v
    end
  end
  #private :attributes
  #private :elements
end

module Nokogiri
  module XML
    class Node
      attr_accessor :_scene
      def add_element(c)
        add_child(c)
      end
  
      def set_attributes(h)
        h.each do |k,v|
          set_attribute(k,v.to_s)
        end
      end
      def get_element(i)
        elements.empty? ? nil : elements[i-1]
      end
      #private :elements
      #private :attributes
      def next_sibling_node
        next_sibling
      end
      def delete_attribute(name)
        remove_attribute(name)
      end
      def text
        content
      end
      def text=(v)
        self.content=v
      end
    end
  end
end

module Rubyvis
  module SvgScene # :nodoc:
    #include REXML
    def self.svg
      "http://www.w3.org/2000/svg"
    end;
    def self.xmlns
      "http://www.w3.org/2000/xmlns"
    end
    def self.xlink
      "http://www.w3.org/1999/xlink"
    end
    def self.xhtml
      "http://www.w3.org/1999/xhtml"
    end
    @scale=1
    def self.scale
      @scale
    end
    def self.scale=(v)
      @scale=v
    end
    IMPLICIT={:svg=>{
        "shape-rendering"=> "auto",
        "pointer-events"=> "painted",
        "x"=> 0,
        "y"=> 0,
        "dy"=> 0,
        "text-anchor"=> "start",
        "transform"=> "translate(0,0)",
        #"fill"=> "none",
        "fill-opacity"=> 1,
        "stroke"=> "none",
        "stroke-opacity"=> 1,
        "stroke-width"=> 1.5,
        "stroke-linejoin"=> "miter"
      },:css=>{"font"=>"10px sans-serif"}
    }
    
    def self.update_all(scenes)
      puts "update_all: #{scenes.inspect}" if $DEBUG
      if (scenes.size>0 and scenes[0].reverse and scenes.type!='line' and scenes.type!='area')
        scenes=scenes.reverse
      end
      self.remove_siblings(self.send(scenes.type, scenes))
    end
    def self.remove_siblings(e)
      while(e)
        n=e.next_sibling_node
        e.remove
        e=n
      end
    end
    def self.create(type)
      if Rubyvis.xml_engine==:nokogiri
        el=Rubyvis.nokogiri_document.create_element("#{type}")
        if type=='svg'
          el.add_namespace(nil, self.svg)
          el.add_namespace('xlink', self.xlink)
        end
      else
        el=REXML::Element.new "#{type}"
        if type=='svg'
          el.add_namespace(self.svg)
          #el.add_namespace("xmlns:xmlns", self.xmlns)
          el.add_namespace("xmlns:xlink", self.xlink)
        end
      end
      el
    end

    def self.append(e,scenes,index)
      e._scene=OpenStruct.new({:scenes=>scenes, :index=>index})
      e=self.title(e, scenes[index])
      if(!e.parent)
        scenes._g.add_element(e)
      end
      e.next_sibling_node
    end
    
    # Applies a title tooltip to the specified element <tt>e</tt>, using the
    # <tt>title</tt> property of the specified scene node <tt>s</tt>. Note that
    # this implementation does not create an SVG <tt>title</tt> element as a child
    # of <tt>e</tt>; although this is the recommended standard, it is only
    # supported in Opera. Instead, an anchor element is created around the element
    # <tt>e</tt>, and the <tt>xlink:title</tt> attribute is set accordingly.
    #
    # @param e an SVG element.
    # @param s a scene node.
    
    def self.title(e,s)
      a = e.parent
      a=nil if (a and (a.name != "a"))
      if (s.title) 
        if (!a) 
          a = self.create("a")
          e.parent.replace_child(a, e) if (e.parent)
          a.add_element(e)
        end
        #a.add_attribute('xlink:title',s.title)
        a.set_attributes('xlink:title' => s.title)

        return a;
      end
      a.parent_node.replace_child(e, a) if (a) 
      e
    end
    
    def self.expect(e, type, attributes, style=nil)

      if (e)
        #e = e.elements[1] if (e.name == "a")
        e=e.get_element(1) if (e.name == 'a')
        if (e.name != type)
          n = self.create(type);
          e.parent.replace_child(e, n);
          e = n
        end
      else
        e = self.create(type)
      end
      attributes.each {|name,value|
        value = nil if (value == IMPLICIT[:svg][name])
        if (value.nil?)
          e.delete_attribute(name)
        else
          e.set_attributes(name=>value)
          #e.attributes[name]=value
        end
      }
      
      if(style)
        base_style=e.attributes['style']
        base_style||=""
        array_styles={}
        base_style.split(";").each {|v|
          v=~/\s*(.+)\s*:\s*(.+)/
          array_styles[$1]=$2
        }
        style.each {|name,value|
          value=nil if value==IMPLICIT[:css][name]
          if (value.nil?)
            array_styles.delete(name)
          else
            array_styles[name]=value
          end
        }
        if array_styles.size>0
          #e.attributes["style"]=array_styles.map {|k,v| "#{k}:#{v}"}.join(";")
          e.set_attributes('style'=> array_styles.map {|k,v| "#{k}:#{v}"}.join(";"))
        end
      end
      e
    end

  end
end