File: spline_type.rb

package info (click to toggle)
ruby-graphviz 1.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,124 kB
  • ctags: 695
  • sloc: ruby: 7,656; xml: 26; makefile: 17
file content (77 lines) | stat: -rw-r--r-- 1,973 bytes parent folder | download | duplicates (5)
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
class SplineTypeException < RuntimeError
end

# spliteType or point
#
# spline ( ';' spline )*
# where spline =  (endp)? (startp)? point (triple)+
# and triple   =  point point point
# and endp  =  "e,%f,%f"
# and startp   =  "s,%f,%f"
#
# If a spline has points p1 p2 p3 ... pn, (n = 1 (mod 3)), the points correspond
# to the control points of a B-spline from p1 to pn. If startp is given, it touches
# one node of the edge, and the arrowhead goes from p1 to startp. If startp is not
# given, p1 touches a node. Similarly for pn and endp.
class GraphViz
  class Types
    class SplineType < Common
      FLOAT_MASK = /[-+]?(?:[0-9]*\.[0-9]+|[0-9]+)/
      ENDP_MASK = /e\s*,\s*#{FLOAT_MASK}\s*,\s*#{FLOAT_MASK}/
      STARTP_MASK = /s\s*,\s*#{FLOAT_MASK}\s*,\s*#{FLOAT_MASK}/
      POINT_MASK = /#{FLOAT_MASK}\s*,\s*#{FLOAT_MASK}(?:\s*,\s*#{FLOAT_MASK})?!?/
      TRIPLE_MASK = /#{POINT_MASK}\s+#{POINT_MASK}\s+#{POINT_MASK}/
      SPLINE_MASK = /(?:#{ENDP_MASK}\s+)?(?:#{STARTP_MASK}\s+)?#{POINT_MASK}(?:\s*#{TRIPLE_MASK})+/

      FINAL_SPLINE_MASK = /^#{SPLINE_MASK}(?:\s*;\s*#{SPLINE_MASK})*$/
      FINAL_POINT_MASK = /^#{POINT_MASK}$/

      def check(data)
        unless SPLINE_MASK.match(data).nil?
           @is_spline = true
           return data
        end
        unless FINAL_POINT_MASK.match(data).nil?
           @is_spline = false
           return data
        end
        return nil if data.empty?

        raise SplineTypeException, "Invalid spline type value"
      end

      def output
        return @data.to_s.inspect.gsub( "\\\\", "\\" )
      end

      alias :to_gv :output
      alias :to_s :output

      def point
        if point?
          "[#{@data}]".to_ruby
        else
          # TODO!
        end
      end

      def endp
      end

      def startp
      end

      def triples
      end

      private
      def point?
         not @is_spline
      end

      def splite_type?
         @is_spline
      end
    end
  end
end