# This file is part of Metasm, the Ruby assembly manipulation suite
# Copyright (C) 2006-2009 Yoann GUILLOT
#
# Licence is LGPL, see LICENCE in the top-level directory
# metasm dasm plugin: create a function to export the currently displayed
# dasm graph to a .svg file
# in the gui, type E to export. Tested only for graph-style view, *may* work in other cases
raise 'gui only' if not gui
def graph_to_svg
gw = gui.curview.dup
class << gw
attr_accessor :svgbuf, :svgcol
def draw_color(col)
col = @default_color_association.fetch(col, col)
col = BasicColor.fetch(col, col)
@svgcol = "##{col}"
end
def draw_line(x1, y1, x2, y2)
bb(x1, y1, x2, y2)
svgbuf << %Q{\n}
end
def draw_rectangle(x, y, w, h)
bb(x, y, x+w, y+h)
svgbuf << %Q{\n}
end
def draw_string(x, y, str)
bb(x, y, x+str.length*@font_width, y+@font_height)
stre = str.gsub('<', '<').gsub('>', '>')
svgbuf << %Q{#{stre}\n}
end
def draw_rectangle_color(c, *a)
draw_color(c)
draw_rectangle(*a)
end
def draw_line_color(c, *a)
draw_color(c)
draw_line(*a)
end
def draw_string_color(c, *a)
draw_color(c)
draw_string(*a)
end
def focus?; false; end
def view_x; @svgvx ||= @curcontext.boundingbox[0]-20; end
def view_y; @svgvy ||= @curcontext.boundingbox[1]-20; end
def width; @svgvw ||= (@curcontext ? (@curcontext.boundingbox[2]-@curcontext.boundingbox[0])*@zoom+20 : 800); end
def height; @svgvh ||= (@curcontext ? (@curcontext.boundingbox[3]-@curcontext.boundingbox[1])*@zoom+20 : 600); end
def svgcuraddr; @curcontext ? @curcontext.root_addrs.first : current_address; end
# drawing bounding box (for the background rectangle)
attr_accessor :bbx, :bby, :bbxm, :bbym
def bb(x1, y1, x2, y2)
@bbx = [x1, x2, @bbx].compact.min
@bbxm = [x1, x2, @bbxm].compact.max
@bby = [y1, y2, @bby].compact.min
@bbym = [y1, y2, @bbym].compact.max
end
end
ret = gw.svgbuf = ''
gw.paint
ret[0, 0] = <
}
end
gui.keyboard_callback[?E] = lambda { |*a|
gui.savefile('svg target') { |f|
svg = graph_to_svg
File.open(f, 'w') { |fd| fd.write svg }
}
true
}