r2dsvg 0.1.1 → 0.2.0

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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/lib/r2dsvg.rb +106 -25
  5. metadata +3 -3
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8296973a57c753bc725b879e70c9479f439a50feb5d7b5330be4694f54b5605e
4
- data.tar.gz: 2ef0ea5d588963e2e429817ae0e044f258e3f2af08b8dca29171998595357ae8
3
+ metadata.gz: 3e718ce8f5b3d4197dd32f6e8a683e798d6584392ecc9887fe06c407e66c09b8
4
+ data.tar.gz: 774d9213286656b2e5e07187ae003e2779069a1f739c4b69daedd9d3613659ff
5
5
  SHA512:
6
- metadata.gz: 55f66186da14d03535b3c1c5a201e8aa9f2c8c331ec5c7df5b13269e3357a0bd083ddebcaeeb3a9b21beed39a13d65638074f324dec8abd5b434a71bf23af335
7
- data.tar.gz: ea0e41cfb9a3751f53692ba1890f433ba80cfbc39698ccd500b9a58cd99bff9d31885671831664a5707ad6bd1c3feaae9e67cb96b8dbfec3191d36bf49c4e0e3
6
+ metadata.gz: 4f9aabe5d38b712ecdc68e578e8161d1cb88e2f18507b830078f8dea4ae43da99b121982381ce688e6fd2270e4da399f9dac94b7885cffe83cfb4a164e5f4ce2
7
+ data.tar.gz: 295f9df35ebe9e3e755c771b52ebe86d16ab19ed298c00dd46797f94957b3a38571b80c5acbf74a98e8feff02437a54e2555953e7bbe3c9eac203e831d112f46
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/r2dsvg.rb CHANGED
@@ -18,10 +18,26 @@ text {fill: red, size: 20}
18
18
 
19
19
  CSS
20
20
 
21
+ module SvgleX
22
+
23
+ refine Svgle::Element do
24
+
25
+ def obj=(object)
26
+ @obj = object
27
+ end
28
+
29
+ def obj()
30
+ @obj
31
+ end
32
+
33
+ end
34
+
35
+ end
21
36
 
22
37
  class R2dSvg
23
38
  include Ruby2D
24
39
  using ColouredText
40
+ using SvgleX
25
41
 
26
42
  class Render < DomRender
27
43
 
@@ -37,8 +53,19 @@ class R2dSvg
37
53
  h
38
54
  end
39
55
 
40
- [:embed_audio, sources ]
56
+ [:embed_audio, sources, e]
41
57
  end
58
+
59
+ def image(e, attributes, raw_style)
60
+
61
+ style = style_filter(attributes).merge(raw_style)
62
+ h = attributes
63
+
64
+ x, y, width, height = %i(x y width height).map{|x| h[x] ? h[x].to_i : nil }
65
+ src = h[:'xlink:href']
66
+
67
+ [:draw_image, [x, y, width, height], src, style, e, render_all(e)]
68
+ end
42
69
 
43
70
  def rect(e, attributes, raw_style)
44
71
 
@@ -49,7 +76,7 @@ class R2dSvg
49
76
  x1, y1, width, height = %i(x y width height).map{|x| h[x].to_i }
50
77
  x2, y2, = x1 + width, y1 + height
51
78
 
52
- [:draw_rectangle, [x1, y1, x2, y2], style, render_all(e)]
79
+ [:draw_rectangle, [x1, y1, x2, y2], style, e, render_all(e)]
53
80
  end
54
81
 
55
82
  def script(e, attributes, style)
@@ -73,7 +100,7 @@ class R2dSvg
73
100
 
74
101
  x, y = %i(x y).map{|x| attributes[x].to_i }
75
102
 
76
- [:draw_text, [x, y], e.text, style, render_all(e)]
103
+ [:draw_text, [x, y], e.text, style, e, render_all(e)]
77
104
  end
78
105
 
79
106
  private
@@ -100,9 +127,35 @@ class R2dSvg
100
127
 
101
128
  end
102
129
 
130
+ def draw_image(args)
131
+
132
+ dimensions, src, style, e = args
133
+
134
+ x, y, width, height = dimensions
135
+
136
+ filepath = Tempfile.new('r2dsvg').path + File.basename(src)
137
+ puts 'filepath: ' + filepath.inspect if @debug
138
+ File.write filepath, RXFHelper.read(src).first
139
+
140
+
141
+ if File.exists? filepath then
142
+
143
+ obj = Image.new(
144
+ filepath,
145
+ x: x, y: y,
146
+ width: width, height: height,
147
+ color: style[:fill],
148
+ z: style[:"z-index"].to_i
149
+ )
150
+
151
+ e.obj = obj if e.respond_to? :obj=
152
+ @window.add obj
153
+ end
154
+ end
155
+
103
156
  def draw_rectangle(args)
104
157
 
105
- coords, style = args
158
+ coords, style, e = args
106
159
 
107
160
  x1, y1, x2, y2 = coords
108
161
 
@@ -111,18 +164,20 @@ class R2dSvg
111
164
  puts ('style: ' + style.inspect).debug
112
165
  end
113
166
 
114
- @window.add Rectangle.new(
167
+ obj = Rectangle.new(
115
168
  x: x1, y: y1,
116
169
  width: x2 - x1, height: y2 - y1,
117
170
  color: style[:fill],
118
171
  z: style[:"z-index"].to_i
119
172
  )
173
+ e.obj = obj if e.respond_to? :obj=
174
+ @window.add obj
120
175
 
121
176
  end
122
177
 
123
178
  def draw_text(args)
124
179
 
125
- coords, text, style = args
180
+ coords, text, style, e = args
126
181
 
127
182
  x, y = coords
128
183
 
@@ -131,14 +186,16 @@ class R2dSvg
131
186
  puts ('style: ' + style.inspect).debug
132
187
  end
133
188
 
134
- @window.add Text.new(
189
+ obj = Text.new(
135
190
  text,
136
191
  x: x, y: y,
137
192
  #font: 'vera.ttf',
138
193
  size: style[:font_size].to_i,
139
194
  color: style[:color],
140
195
  z: style[:"z-index"].to_i
141
- )
196
+ )
197
+ e.obj = obj
198
+ @window.add obj
142
199
 
143
200
  end
144
201
 
@@ -147,21 +204,33 @@ class R2dSvg
147
204
 
148
205
  def embed_audio(args)
149
206
 
150
- sources, _ = args
207
+ sources, e = args
151
208
 
152
209
  if @debug then
153
210
  puts 'sources: ' + sources.inspect if @debug
154
211
  puts 'inside embed_audio'.info
155
212
  end
156
213
 
157
- audio = sources.find do |source|
158
- File.exists? source[:src]
214
+
215
+ audio_files = sources.map do |source|
216
+
217
+ filepath = Tempfile.new('r2dsvg').path + File.basename(source[:src])
218
+ File.write filepath, RXFHelper.read(source[:src]).first
219
+ filepath
159
220
  end
160
221
 
222
+ audio = audio_files.find {|file| File.exists? file }
223
+
161
224
  return unless audio
162
225
 
163
- file = File.exists? audio[:src]? audio[:src] : nil
164
- @window.add = Sound.new(file)
226
+ file = File.exists?(audio) ? audio : nil
227
+ puts 'file: ' + file.inspect if @debug
228
+ file = '/tmp/echo.wav'
229
+ obj = Sound.new(file)
230
+ e.obj = obj
231
+
232
+ def e.play() self.obj.play() end
233
+ #@window.add obj
165
234
 
166
235
  end
167
236
 
@@ -214,27 +283,39 @@ class R2dSvg
214
283
  @width, @height = %i(width height).map{|x| doc.root.attributes[x].to_i }
215
284
  window.set title: title, width: @width, height: @height
216
285
 
217
- def doc.element_by_id(id)
218
- self.root.element("//*[@id='#{id}']")
219
- end
220
286
 
221
287
  doc.root.xpath('//script').each {|x| eval x.text.unescape }
222
288
 
223
289
  drawing.render instructions
224
-
225
- window.on :mouse_move do |event|
226
-
227
- @doc.root.xpath('//*[@onmousemove]').each do |x|
228
-
229
- eval x.onmousemove() if x.hotspot? event.x, event.y
230
-
231
- end
232
290
 
291
+ @doc = doc
292
+
293
+ window.on(:mouse_move) {|event| mouse(:move, event) }
294
+
295
+ window.on(:mouse_down) do |event|
296
+ mouse :down, event if event.button == :left
233
297
  end
234
298
 
235
- @doc = doc
299
+
236
300
  window.show
237
301
  end
302
+
303
+ private
304
+
305
+ def mouse(action, event)
306
+
307
+ doc = @doc
308
+
309
+ @doc.root.xpath("//*[@onmouse#{action}]").each do |x|
310
+
311
+ #puts 'x.class: ' + x.inspect if @debug
312
+ if x.obj and x.obj.contains? event.x, event.y then
313
+ eval x.method(('onmouse' + action.to_s).to_sym).call()
314
+ end
315
+
316
+ end
317
+
318
+ end
238
319
  end
239
320
 
240
321
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: r2dsvg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -46,7 +46,7 @@ dependencies:
46
46
  version: '0.4'
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: 0.4.4
49
+ version: 0.4.5
50
50
  type: :runtime
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
@@ -56,7 +56,7 @@ dependencies:
56
56
  version: '0.4'
57
57
  - - ">="
58
58
  - !ruby/object:Gem::Version
59
- version: 0.4.4
59
+ version: 0.4.5
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: ruby2d
62
62
  requirement: !ruby/object:Gem::Requirement
metadata.gz.sig CHANGED
Binary file