Skip to content

Instantly share code, notes, and snippets.

@gyuque
Created October 15, 2010 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyuque/627854 to your computer and use it in GitHub Desktop.
Save gyuque/627854 to your computer and use it in GitHub Desktop.
draw.rb
require 'rubygems'
require 'json'
require 'cairo'
require 'uri'
format = Cairo::FORMAT_RGB24
width = 1200
height = 1600
def draw_rects(g, ls)
ls.each{|elem|
x = elem["x"]
y = elem["y"]
w = elem["w"]
h = elem["h"]
is_img = (elem['tagname'] == 'img')
if is_img
g.set_source_rgb(0.1, 0.2, 0.4)
else
g.set_source_rgb(0.1, 0.4, 0.2)
end
g.rectangle(x, y, w, h)
g.fill
if is_img
g.set_source_rgb(0.7, 0.9, 1)
else
g.set_source_rgb(0.7, 1, 0.9)
end
g.rectangle(x, y, w, h)
g.stroke
g.select_font_face("Arial",0,0)
2.times{|i|
g.save
g.translate(x+2-i, y+h/2+8-i)
i==0 ? g.set_source_rgb(0, 0, 0) : g.set_source_rgb(1, 1, 1)
if is_img
g.set_font_size(16)
g.text_path(URI.parse(elem["url"]).path)
else
g.set_font_size(26)
g.text_path("[Object]")
end
g.fill
g.restore
}
STDERR.print('.')
}
STDERR.puts("done")
end
infile = ARGV[0]
surface = Cairo::ImageSurface.new(format, width, height)
g = Cairo::Context.new(surface)
elem_list = nil
File.open(infile || "test.json") {|f|
elem_list = JSON.parse(f.read)
}
draw_rects(g, elem_list)
surface.write_to_png("boxes.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment