gtk2html 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9c5ab0a506063f7cb0b41a80b527a8865c64a721
4
+ data.tar.gz: 2fcf59b16e5c4415c652474733501ed7588de53f
5
+ SHA512:
6
+ metadata.gz: 8237d4275b5115807d8e00a9b4d104e6aea077f6acfe730e4436aa8acc346d82b606b40878520bb6fe9eff3a7996857e4f43d90aea20eb338f4bf79207e35c10
7
+ data.tar.gz: 0bec2392905037ccbf3d796a34b9db8f55ac04c663db39554979435d1817abd07d9986ad3d9450c624abb5a9e1c44fafbc174ea24255d9c6993dc274fec8e31e
checksums.yaml.gz.sig ADDED
@@ -0,0 +1,3 @@
1
+ ���↜*mW�1r�D�~)�+9S�u3�Ai����T�nO�CC�lmu� ����&p�O����>N��p
2
+ K�y3�1��Eٲ?��v�̈́�0Sj�"�$ĐOc�r
3
+ �/e��sL=�s� L`��\T���^�I��l�ĝ �������j�v�&>��Й
data.tar.gz.sig ADDED
Binary file
data/lib/gtk2html.rb ADDED
@@ -0,0 +1,283 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: gtk2html.rb
4
+
5
+ require 'gtk2svg'
6
+ require 'htmle'
7
+
8
+
9
+ module Gtk2HTML
10
+
11
+ class Render < DomRender
12
+
13
+ def initialize(x, width, height)
14
+
15
+ @width, @height = width, height
16
+ super x
17
+
18
+ end
19
+
20
+ def body(e, attributes, raw_style)
21
+
22
+ style = style_filter(attributes).merge(raw_style)
23
+
24
+ h = attributes
25
+
26
+ [:draw_box, [x1,y1,x2,y2], style, render_all(e)]
27
+ end
28
+
29
+ def div(e, attributes, raw_style)
30
+
31
+ style = style_filter(attributes).merge(raw_style)
32
+ margin = style[:margin].values
33
+ coords = [0, nil, @width, @height/2]
34
+ padding = style[:padding].values
35
+
36
+ [:draw_box, margin, coords, padding, style, render_all(e)]
37
+ end
38
+
39
+ def html(e, attributes, style)
40
+
41
+ margin = style[:margin].values
42
+ coords = [0, 0, @width, @height]
43
+ padding = style[:padding].values
44
+
45
+ [:draw_box, margin, coords, padding, style, render_all(e)]
46
+ end
47
+
48
+ private
49
+
50
+ def fetch_style(attribute)
51
+
52
+ h = super attribute
53
+
54
+ %i(margin padding).inject(h) do |r,x|
55
+
56
+ if h.has_key? x then
57
+
58
+ a = expand_shorthand(h[x])
59
+
60
+ r.merge!(x => Hash[%i(top right bottom left).zip(a)])
61
+ else
62
+ r
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ def style_filter(attributes)
70
+
71
+ %i(bgcolor).inject({}) do |r,x|
72
+ attributes.has_key?(x) ? r.merge(x => attributes[x]) : r
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+
79
+ class DrawingInstructions
80
+
81
+ attr_accessor :area
82
+
83
+
84
+ def initialize(area=nil)
85
+
86
+ @area = area if area
87
+
88
+ end
89
+
90
+ def draw_box(args)
91
+
92
+ margin, raw_coords, padding, style = args
93
+ coords = raw_coords.map.with_index {|x,i| x ? x : @curpos[i] }
94
+
95
+ h2 = style.clone
96
+ h2.delete :color
97
+ x1, y1, x2, y2 = coords
98
+
99
+ @curpos = coords
100
+
101
+ gc = gc_ini(h2)
102
+ @area.window.draw_rectangle(gc, 1, x1, y1, x2, y2)
103
+ end
104
+
105
+ def draw_layout(args)
106
+
107
+ text, style = args
108
+
109
+ x, y = @curpos
110
+
111
+ text ||= ''
112
+ h2 = style.clone
113
+ h2.delete :'background-color'
114
+
115
+ layout = Pango::Layout.new(Gdk::Pango.context)
116
+ layout.font_description = Pango::FontDescription.\
117
+ new('Sans ' + style[:'font-size'])
118
+ layout.text = text
119
+
120
+ gc = gc_ini(h2)
121
+ @area.window.draw_layout(gc, x, y, layout)
122
+ end
123
+
124
+ def window(args)
125
+ end
126
+
127
+ def render(a)
128
+ @latest_style = a[4]
129
+ method(a[0]).call(args=a[1..4])
130
+ draw a[5]
131
+ end
132
+
133
+ def script(args)
134
+
135
+ end
136
+
137
+
138
+ private
139
+
140
+ def draw(a)
141
+
142
+ a.each do |rawx|
143
+
144
+ x, *remaining = rawx
145
+
146
+ if x.is_a? Symbol then
147
+
148
+ @latest_style = remaining[3]
149
+ method(x).call(args=remaining.shift(4))
150
+ draw remaining
151
+ elsif x.is_a? String then
152
+
153
+ next unless x.length > 0
154
+
155
+ method(:draw_layout).call(args=[x, @latest_style]) unless x.empty?
156
+
157
+ elsif x.is_a? Array
158
+
159
+ draw remaining
160
+ else
161
+ method(x).call(remaining.take 4)
162
+ end
163
+
164
+ end
165
+
166
+ end
167
+
168
+ def set_colour(c)
169
+
170
+ colour = case c
171
+ when /^rgb/
172
+ regex = /rgb\((\d{1,3}), *(\d{1,3}), *(\d{1,3})\)$/
173
+ r, g, b = c.match(regex).captures.map {|x| x.to_i * 257}
174
+ colour = Gdk::Color.new(r, g, b)
175
+ when /^#/
176
+ Gdk::Color.parse(c)
177
+ else
178
+ Gdk::Color.parse(c)
179
+ end
180
+
181
+ colormap = Gdk::Colormap.system
182
+ colormap.alloc_color(colour, false, true)
183
+
184
+ colour
185
+ end
186
+
187
+ def gc_ini(style)
188
+
189
+ gc = Gdk::GC.new(@area.window)
190
+
191
+ color, bgcolor = style[:color], style[:'background-color']
192
+
193
+ gc.set_foreground(set_colour(color)) if color
194
+ gc.set_foreground(set_colour(bgcolor)) if bgcolor
195
+
196
+ gc
197
+ end
198
+
199
+ end
200
+
201
+ class Main
202
+
203
+
204
+ attr_accessor :doc, :html
205
+ attr_reader :width, :height
206
+
207
+ def initialize(html, irb: false)
208
+
209
+ @html = html
210
+ @doc = Htmle.new(html, callback: self)
211
+
212
+ @area = area = Gtk::DrawingArea.new
213
+ client_code = []
214
+
215
+ window = Gtk::Window.new
216
+
217
+ @width = 320
218
+ @height = 240
219
+ window.set_default_size(@width, @height)
220
+
221
+ @dirty = true
222
+
223
+
224
+ area.signal_connect("expose_event") do
225
+
226
+ if @dirty then
227
+
228
+ Thread.new { @doc.root.xpath('//script').each {|x| eval x.text.unescape } }
229
+
230
+ @instructions = Gtk2HTML::Render.new(@doc, @width, @height).to_a
231
+ end
232
+
233
+ drawing = DrawingInstructions.new area
234
+ drawing.render @instructions
235
+ @dirty = false
236
+
237
+ end
238
+
239
+ area.add_events(Gdk::Event::POINTER_MOTION_MASK)
240
+
241
+ area.signal_connect('motion_notify_event') do |item, event|
242
+
243
+ @doc.root.xpath('//*[@onmousemove]').each do |x|
244
+
245
+ eval x.onmousemove() if x.hotspot? event.x, event.y
246
+
247
+ end
248
+ end
249
+
250
+ area.add_events(Gdk::Event::BUTTON_PRESS_MASK)
251
+
252
+ area.signal_connect "button_press_event" do |item,event|
253
+
254
+ @doc.root.xpath('//*[@onmousedown]').each do |x|
255
+
256
+ eval x.onmousedown() if x.hotspot? event.x, event.y
257
+
258
+ end
259
+ end
260
+
261
+ window.add(area).show_all
262
+ window.show_all.signal_connect("destroy"){Gtk.main_quit}
263
+
264
+ irb ? Thread.new {Gtk.main } : Gtk.main
265
+ end
266
+
267
+ def onmousemove(x,y)
268
+
269
+ end
270
+
271
+ def refresh()
272
+ @dirty = true
273
+ @area.queue_draw
274
+ end
275
+
276
+ def html=(html)
277
+ @html = html
278
+ @doc = Htmle.new(svg, callback: self)
279
+ end
280
+
281
+ end
282
+
283
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gtk2html
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Robertson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRIwEAYDVQQDDAlnZW1t
14
+ YXN0ZXIxHjAcBgoJkiaJk/IsZAEZFg5qYW1lc3JvYmVydHNvbjESMBAGCgmSJomT
15
+ 8ixkARkWAmV1MB4XDTE1MTEyNTIyNTk1OVoXDTE2MTEyNDI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4J0/5Y8BLyxXWUTTXHeLXwfiIfffKOUtTUjlivG+dvqexj8Qbbv9
32
+ bW25tpA1jigdbQ==
33
+ -----END CERTIFICATE-----
34
+ date: 2015-11-25 00:00:00.000000000 Z
35
+ dependencies:
36
+ - !ruby/object:Gem::Dependency
37
+ name: gtk2svg
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '0.3'
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 0.3.11
46
+ type: :runtime
47
+ prerelease: false
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '0.3'
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 0.3.11
56
+ - !ruby/object:Gem::Dependency
57
+ name: htmle
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.1'
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 0.1.0
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: '0.1'
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.1.0
76
+ description:
77
+ email: james@r0bertson.co.uk
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - lib/gtk2html.rb
83
+ homepage: https://github.com/jrobertson/gtk2html
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.4.8
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Experimental gem to render HTML using Gtk2
107
+ test_files: []
metadata.gz.sig ADDED
Binary file