svg-render 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ def render_canvas(template_name)
2
+ url = "http://rorbuilder.info/r/heroku/erb/canvas.erb"
3
+ t = open(url, "UserAgent" => "Sinatra-Rscript").read
4
+ build_templates(t)
5
+
6
+ @@templates[template_name] = yield.strip
7
+ @content_type = 'text/xml'
8
+ erb @@templates[template_name]
9
+ end
10
+
11
+
12
+
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: canvas_shape.rb
4
+
5
+ require 'shape_base'
6
+
7
+ module CanvasShape
8
+
9
+ class Shape < ShapeBase
10
+
11
+ def initialize()
12
+ super
13
+ end
14
+
15
+ private
16
+
17
+ def load_shapes()
18
+
19
+ line = Proc.new do |h|
20
+ x1, y1, x2, y2 = h[:x1], h[:y1], h[:x2], h[:y2]
21
+ #x2, y2 = (h[:width] - x1), (h[:height] - y1)
22
+ stroke = h[:stroke]
23
+ stroke_width = stroke[:width]
24
+ colour = stroke[:colour]
25
+
26
+ Proc.new do
27
+ puts "*** drawing a line"
28
+ "
29
+ ctx.strokeStyle = '#{colour}';
30
+ ctx.lineWidth = #{stroke_width};
31
+ ctx.beginPath();
32
+ ctx.moveTo(#{x1},#{y1});
33
+ ctx.lineTo(#{x2},#{y2});
34
+ ctx.stroke();"
35
+
36
+ end
37
+ end
38
+
39
+ rectangle = Proc.new do |h|
40
+ x, y = h[:x], h[:y]
41
+ width, height= h[:width], h[:height]
42
+
43
+ style = h[:style]
44
+ fill = style[:fill]
45
+ stroke = style[:stroke]
46
+ stroke_width = style['stroke-width']
47
+
48
+ Proc.new do
49
+ puts "*** drawing a rectangle"
50
+
51
+ <<CANVAS
52
+
53
+ ctx.fillStyle = "#{fill}";
54
+ ctx.fillRect (#{x}, #{y}, #{width}, #{height});
55
+
56
+ CANVAS
57
+
58
+ #canvas.rect(x2, y2, x1, y1).styles(:fill => fill, :stroke => stroke, :stroke_width => stroke_width)
59
+ end
60
+ end
61
+
62
+
63
+ text = lambda do |h|
64
+ x, y = h[:x], h[:y]
65
+ font_size = h[:font][:size] # ???
66
+ fill = h[:style][:fill]
67
+ text2 = h[:text]
68
+ text_align = h[:style][:text_align]
69
+
70
+ #jr031209 stroke = h[:stroke]
71
+ #jr031209 r,g,b = stroke[:rgb]
72
+
73
+ return Proc.new {} if text2.length < 1
74
+ Proc.new do
75
+ puts "*** drawing text x %s y %s text: %s" % [x,y,text2]
76
+ <<CANVAS
77
+
78
+ ctx.font = "#{font_size}em Times New Roman";
79
+ ctx.fillStyle = "#{fill}";
80
+ ctx.textAlign = '#{text_align}';
81
+ ctx.fillText("#{text2}", #{x}, #{y});
82
+
83
+ CANVAS
84
+ end
85
+ end
86
+
87
+ @shape[:line] = line
88
+ @shape[:rectangle] = rectangle
89
+ @shape[:text] = text
90
+
91
+ end
92
+ end
93
+
94
+ end
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: elements.rb
4
+
5
+ # this code extends REXML::Document::Element
6
+ REXML::Element.class_eval do
7
+
8
+ def ui_element=(object)
9
+ @ui_element = object
10
+ end
11
+
12
+ def ui_element()
13
+ @ui_element
14
+ end
15
+
16
+ def render()
17
+ @ui_element.invoke
18
+ end
19
+
20
+ def load_ui_element
21
+
22
+ text = self.text.to_s.strip.chomp
23
+ @ui_element.text = text.length > 0 ? text : ''
24
+
25
+ # must load the parent properties
26
+ # inherit the parent properties
27
+ @ui_element.load_properties(self.parent.attributes)
28
+ # get the style
29
+ @ui_element.load_properties(self.attributes)
30
+ end
31
+
32
+ end
33
+
34
+
@@ -0,0 +1,265 @@
1
+ #!/usr/bin/ruby
2
+ # file: render_svg3.rb
3
+
4
+ require 'elements'
5
+
6
+ class SvgEnvironment
7
+
8
+ def initialize()
9
+ @@scale = 0.5
10
+ end
11
+
12
+ def load_renderer(renderer)
13
+ @@shape2 = renderer
14
+ end
15
+
16
+ def scale()
17
+ @@scale
18
+ end
19
+
20
+ def scale=(val)
21
+ @@scale = val
22
+ end
23
+ end
24
+
25
+ class ElementSvg #< SvgEnvironment
26
+ #include RVGShape
27
+
28
+ # this initialize method should be overridden with a class_eval from the calling RSF job
29
+ def initialize()
30
+ initialize_variables()
31
+ #@@shape = RVGShape::Shape.new
32
+ #@@shape = Shape.new
33
+ end
34
+
35
+ def initialize_variables()
36
+ @property = {'style' => ''}
37
+ @style_property = {}
38
+ end
39
+
40
+ def text=(val)
41
+ @text = val
42
+ end
43
+
44
+ def text()
45
+ @text
46
+ end
47
+
48
+ def scale=(val); @@scale = val; end
49
+ def scale() @@scale end
50
+
51
+ def x_offset=(val); @@x_offset = val; end
52
+ def x_offset(); @@x_offset; end
53
+
54
+ def y_offset=(val); @@y_offset = val; end
55
+ def y_offset(); @@y_offset; end
56
+
57
+ def render_element()
58
+ #parse_style()
59
+ properties = yield
60
+ @@shape.draw @type, properties
61
+ end
62
+
63
+ def parse_style()
64
+ if @property['style'] then
65
+ properties = get_style_properties(@property['style'])
66
+
67
+ #h = Hash[*properties.join(';').split(';')]
68
+ properties.each do |name, value|
69
+ @style_property[name] = value if @style_property.has_key? name
70
+ end
71
+
72
+ end
73
+ end
74
+
75
+ def get_style_properties(properties_string)
76
+ properties_string.split(/;/).map do |a|
77
+ a.match(/^(.[^:]+):([^$]+$)/).captures.map {|x| x.strip}
78
+ end
79
+ end
80
+
81
+ def load_properties(attributes)
82
+ attributes.each_attribute do |attribute|
83
+ @property[attribute.name] = attribute.value if @property.has_key? attribute.name
84
+ end
85
+ parse_style()
86
+ end
87
+
88
+ def load_renderer(renderer)
89
+ @@shape = renderer
90
+ end
91
+ end
92
+
93
+ class ElementEllipse < ElementSvg
94
+ def initialize()
95
+ super
96
+ end
97
+
98
+ def invoke()
99
+ puts " -- render the 'ellipse' element"
100
+ render_element()
101
+ end
102
+ end
103
+
104
+
105
+ class ElementRect < ElementSvg
106
+
107
+ def initialize()
108
+ super
109
+ local_properties = {'x' => '','y' => '', 'width' => '', 'height' => ''}
110
+ @property.merge!(local_properties)
111
+ @style_property = {'fill' => '', 'stroke' => '', 'stroke-width' => ''}
112
+ @type = :rectangle
113
+ end
114
+
115
+ def invoke()
116
+ puts " -- render the 'rect' element"
117
+ render_element()
118
+ end
119
+
120
+ def render_element()
121
+ super do
122
+ h = @property; hs = @style_property
123
+ x1, y1, x2, y2 = h['x'].to_i, h['y'].to_i, h['x'].to_i + h['width'].to_i, h['y'].to_i + h['height'].to_i
124
+ #r,g,b = hs['fill'].match(/rgb\((\d{1,5}),(\d{1,5}),(\d{1,5})\)/).captures
125
+ style = {:fill => hs['fill'], :stroke => hs['stroke'], :stroke_width => hs['stroke-width']}
126
+
127
+ #scale = run_projectx('registry', 'get-key', :path => 'app/svg_viewer/scale').first.to_f
128
+ x1, y1, x2, y2 = [x1, y1, x2, y2].map {|x| x * @@scale}
129
+ x1, x2 = [x1, x2].map {|x| x + @@x_offset * @@scale}
130
+ y1, y2 = [y1, y2].map {|x| x + @@y_offset * @@scale}
131
+
132
+ properties = {:x => x1, :y => y1, :width => x2 - x1, :height => y2 - y1, :style => style}
133
+ #properties = {:x => x1, :y => y1, :width => x2 - x1, :height => y2 - y1}
134
+ #draw properties
135
+ end
136
+ end
137
+
138
+ end
139
+
140
+
141
+ class ElementLine < ElementSvg
142
+
143
+ def initialize()
144
+ super
145
+ local_properties = {'x1' => '','y1' => '', 'x2' => '', 'y2' => ''}
146
+ @property.merge!(local_properties)
147
+ @style_property = {'stroke' => '', 'stroke-width' => ''}
148
+ @type = :line
149
+ end
150
+
151
+ def invoke()
152
+ puts " -- render the 'line' element " + @text
153
+ render_element()
154
+ end
155
+
156
+ def render_element()
157
+ super do
158
+ h = @property; hs = @style_property
159
+ x1, y1, x2, y2 = h['x1'].to_i, h['y1'].to_i, h['x2'].to_i, h['y2'].to_i
160
+ stroke_width = hs['stroke-width'].to_i
161
+ colour = hs['stroke']
162
+
163
+ #scale = run_projectx('registry', 'get-key', :path => 'app/svg_viewer/scale').first.to_f
164
+ x1, y1, x2, y2 = [x1, y1, x2, y2].map {|x| x * @@scale}
165
+ x1, x2 = [x1, x2].map {|x| x + @@x_offset * @@scale}
166
+ y1, y2 = [y1, y2].map {|x| x + @@y_offset * @@scale}
167
+
168
+ stroke = {:width => stroke_width, :colour => colour}
169
+ {:x1 => x1, :y1 => y1, :x2 => x2, :y2 => y2, :stroke => stroke}
170
+ #draw properties
171
+ end
172
+ end
173
+
174
+ end
175
+
176
+ class ElementText < ElementSvg
177
+ def initialize()
178
+ super
179
+ local_properties = {'x' => '','y' => '', 'fill' => '', 'font-size' => '15'}
180
+ @property.merge!(local_properties)
181
+ @style_property = {'font-size' => '', 'font-style' => '', 'font-weight' => '',
182
+ 'fill' => '', 'fill-opacity' => '', 'stroke' => '', 'font-family' => '', 'text-align' => 'start'}
183
+ @type = :text
184
+ end
185
+
186
+ def invoke()
187
+ puts " -- render the 'text' element"
188
+ render_element()
189
+ end
190
+
191
+ def render_element()
192
+ super do
193
+ h = @property; style = @style_property
194
+ style.delete_if {|key, value| value.empty?}
195
+ h.merge! style
196
+ x, y = h['x'].to_i, h['y'].to_i
197
+ font_size = (h['font-size'].to_f * 0.0625) unless h['font-size'].empty?
198
+
199
+
200
+ fill = h['fill']
201
+ text2 = self.text
202
+ text_align = h['text-align']
203
+ @style_property['text-align'] = 'start'
204
+ #@style_property = {'font-size' => '', 'font-style' => '', 'font-weight' => '',
205
+ # 'fill' => '', 'fill-opacity' => '', 'stroke' => '', 'font-family' => '', 'text-align' => 'start'}
206
+ #puts 'xzx ' + text2 + 'xzx'
207
+
208
+ #stroke_width = hs['stroke-width'].to_i
209
+ #r,g,b = hs['stroke'].match(/rgb\((\d{1,5}),(\d{1,5}),(\d{1,5})\)/).captures
210
+ #puts 'x0xx ' + font_size.to_s + ' y0yy'
211
+ #scale = run_projectx('registry', 'get-key', :path => 'app/svg_viewer/scale').first.to_f
212
+ x, y, font_size = [x, y, font_size].map {|x| x * @@scale }
213
+ #x_offset = -40
214
+ x += @@x_offset * @@scale
215
+ y += @@y_offset * @@scale
216
+ #puts 'x1xx ' + font_size.to_s + ' y1yy'
217
+
218
+ {:x => x, :y => y, :text => text2, :style => {:fill => fill, :text_align => text_align}, :font => {:size => font_size}}
219
+ end
220
+ end
221
+
222
+ end
223
+
224
+
225
+ class RenderSvg
226
+ def initialize()
227
+ @dom = {}
228
+ @svg_procs = []
229
+ puts 'render ...'
230
+
231
+ load_elements
232
+ #@dom = render_document(doc.root)
233
+ end
234
+
235
+ def load_elements()
236
+ @h = {}
237
+ @h['ellipse'] = ElementEllipse.new
238
+ @h['line'] = ElementLine.new
239
+ @h['rect'] = ElementRect.new
240
+ @h['text'] = ElementText.new
241
+ @h['tspan'] = ElementText.new
242
+
243
+ end
244
+
245
+ def render(doc)
246
+ @svg_procs = []
247
+ render_all doc
248
+ end
249
+
250
+ def render_all( doc)
251
+ if @h.has_key? doc.name then
252
+ doc.ui_element = @h[doc.name]
253
+ doc.load_ui_element
254
+ @svg_procs << doc.ui_element.invoke
255
+ end
256
+
257
+ doc.elements.each { |node| render_all node}
258
+ end
259
+
260
+ def svg_procs()
261
+ @svg_procs
262
+ end
263
+
264
+ end
265
+
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: rvg_shape.rb
4
+
5
+ require 'shape_base'
6
+
7
+ module RVGShape
8
+
9
+ class Shape < ShapeBase
10
+
11
+ def initialize()
12
+ super
13
+ end
14
+
15
+ private
16
+
17
+ def load_shapes()
18
+
19
+ line = Proc.new do |h|
20
+ x1, y1, x2, y2 = h[:x1], h[:y1], h[:x2], h[:y2]
21
+ #x2, y2 = (h[:width] - x1), (h[:height] - y1)
22
+ stroke = h[:stroke]
23
+ stroke_width = stroke[:width]
24
+ colour = stroke[:colour]
25
+
26
+ Proc.new do |canvas|
27
+ puts "*** drawing a line"
28
+ canvas.line(x1, y1, x2, y2).styles(:stroke=> "#{colour}" , :stroke_width => stroke_width)
29
+ end
30
+
31
+ end
32
+
33
+ rectangle = Proc.new do |h|
34
+ x, y = h[:x], h[:y]
35
+ width, height = h[:width], h[:height]
36
+
37
+ style = h[:style]
38
+ fill = style[:fill]
39
+ stroke = style[:stroke]
40
+ stroke_width = style['stroke-width']
41
+
42
+ Proc.new do |canvas|
43
+ puts "*** drawing a rectangle x: %s y: %s" % [x, y]
44
+ canvas.rect(width, height, x, y).styles(:fill => fill, :stroke => stroke, :stroke_width => stroke_width)
45
+ end
46
+ end
47
+
48
+
49
+ text = lambda do |h|
50
+
51
+ x, y = h[:x], h[:y]
52
+ font_size = (h[:font][:size] * 16) # ???
53
+ fill = h[:style][:fill]
54
+ text2 = h[:text]
55
+ text_align = h[:style][:text_align]
56
+
57
+ #jr031209 stroke = h[:stroke]
58
+ #jr031209 r,g,b = stroke[:rgb]
59
+ return Proc.new {} if text2.length < 1
60
+
61
+ Proc.new do |canvas|
62
+ puts "*** drawing text x: %s y: %s" % [x, y]
63
+ canvas.text(x, y, text2).styles(:text_anchor => text_align, :font_size=> font_size, :font_family=>'helvetica', :fill => fill)
64
+ end
65
+
66
+ end
67
+
68
+ @shape[:line] = line
69
+ @shape[:rectangle] = rectangle
70
+ @shape[:text] = text
71
+
72
+ end
73
+ end
74
+
75
+ end
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: shape_base.rb
4
+
5
+ class ShapeBase
6
+
7
+ def initialize(scale=1)
8
+ @shape = {}
9
+ load_shapes()
10
+ end
11
+
12
+ def draw(name, properties)
13
+ @shape[name].call(properties)
14
+ end
15
+
16
+ private
17
+
18
+ def load_shapes()
19
+
20
+ end
21
+
22
+ end
23
+
@@ -0,0 +1,36 @@
1
+ require 'render_svg3'
2
+ require 'rcscript'
3
+
4
+ class Browser
5
+
6
+ attr_accessor :doc
7
+
8
+ def initialize(o={})
9
+
10
+ h = {scale:0.5, x_offset:0, y_offset:0}.merge o
11
+ svg_environ = ElementSvg.new
12
+ yield(svg_environ)
13
+
14
+ svg_environ.scale = h[:scale]
15
+ svg_environ.x_offset = h[:x_offset]
16
+ svg_environ.y_offset = h[:y_offset]
17
+
18
+ @svg_engine, @script = RenderSvg.new(), RScriptBase.new()
19
+
20
+ end
21
+
22
+ def load_page(url)
23
+ #url = 'http://rorbuilder.info/r/heroku/image/' + svg_file_name
24
+ buffer = open(url, 'UserAgent' => 'Sinatra-Rscript').read
25
+ #puts 'buffer : ' + buffer
26
+ doc = Document.new(buffer)
27
+ yield(doc) if block_given?
28
+ #puts '@doc : ' + @doc.to_s
29
+ script = @script.run(doc)
30
+ #eval(script) # de-activated for now
31
+ @svg_engine.render doc
32
+ @svg_engine.svg_procs
33
+ end
34
+
35
+ end
36
+
@@ -0,0 +1,17 @@
1
+ require 'svg_browser'
2
+ require 'canvas_shape'
3
+
4
+ class CanvasBrowser < Browser
5
+
6
+ def initialize(o={})
7
+ super(o) {|env| env.load_renderer CanvasShape::Shape.new}
8
+ end
9
+
10
+ def load_page(svg_file_name)
11
+ super(svg_file_name) do |doc|
12
+ yield(doc)
13
+ end
14
+ end
15
+
16
+ end
17
+
@@ -0,0 +1,10 @@
1
+ def build_templates(t)
2
+ a = []
3
+ t.gsub!(/\#\{/,'\#\\{')
4
+ t.scan(/@@ (\w+)/){ a << [$1,$']}
5
+ code = a.map {|x| i = x[1] =~ /@@/; r = i.nil? ? x[1] : x[1][0,i] ; [x[0], r] }
6
+ template_code = code.map {|f,c| "@@templates[:%s] = \"%s\"" % [f, c.gsub(/"/,'\\"')]}
7
+ instance_eval(template_code.join("\n"))
8
+ instance_eval("template :layout do \"%s\" end" % @@templates[:layout].strip) if @@templates.has_key? :layout
9
+ end
10
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svg-render
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -22,7 +22,16 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
+ - lib/elements.rb
26
+ - lib/shape_base.rb
27
+ - lib/rvg_shape.rb
28
+ - lib/template_builder.rb
29
+ - lib/canvas_shape.rb
30
+ - lib/canvas_enclosure.rb
25
31
  - lib/svg-render.rb
32
+ - lib/svg_browser.rb
33
+ - lib/render_svg3.rb
34
+ - lib/svg_to_canvas_browser.rb
26
35
  has_rdoc: true
27
36
  homepage:
28
37
  licenses: []