Permalink
Browse files

Add ViewerConnection#image_data

  • Loading branch information...
jarib committed Aug 22, 2009
1 parent 6a87a19 commit 8405bf171818a52846df92485ea3c80b5e0cdd95
Showing with 30 additions and 5 deletions.
  1. +30 −5 lib/celerity/viewer_connection.rb
@@ -21,7 +21,7 @@ def initialize(socket)
#
def render_html(html, url)
- send_data({'method' => 'page_changed', 'html' => html, 'url' => url}.to_json)
+ send_data('method' => 'page_changed', 'html' => html, 'url' => url)
end
#
@@ -30,16 +30,28 @@ def render_html(html, url)
#
def save(path)
- send_data({'method' => 'save', 'path' => path}.to_json)
+ send_data('method' => 'save', 'path' => path)
end
+
#
# Tells the viewer to dump the render tree to the given path.
- # Only available on the Qt viewer.
+ # Only available in the Qt viewer.
#
def save_render_tree(path)
- send_data({'method' => 'save_render_tree', 'path' => path}.to_json)
+ send_data('method' => 'save_render_tree', 'path' => path)
+ end
+
+ #
+ # Get the currently rendered page as a Base64-encoded PNG image.
+ # Only available in the Qt viewer.
+ #
+
+ def image_data
+ send_data('method' => 'image_data')
+ data = read_data
+ data['image'] || data['error']
end
#
@@ -52,13 +64,26 @@ def close
private
- def send_data(json)
+ def send_data(msg)
+ json = msg.to_json
data = "Content-Length: #{json.size}\n\n#{json}"
@socket.write data
@socket.flush
nil
end
+ def read_data
+ buf = ''
+ until buf =~ /\n\n\z/ || @socket.eof? || @socket.closed?
+ buf << @socket.read(1).to_s
+ end
+
+ return if buf.empty?
+
+ length = buf[/Content-Length: (\d+)/, 1].to_i
+ JSON.parse @socket.read(length)
+ end
+
end
end

0 comments on commit 8405bf1

Please sign in to comment.