[Richard Suchenwirth] 2002-11-05 - [SVG] (Scalable Vector Graphics) is
an application of [XML] to describe images in terms of elements, which
often resemble Tk's [canvas] items.
The following code attempts to dump canvas contents into a well-formed SVG string.
Having neither the complete SVG spec nor a viewer at hand,
it is just a first shot - no warranty, feel free to improve and edit this page!
}
----
proc canvas2svg c {
set res ""
}
proc att {name value {default -}} {
if {$value != $default} {return " $name=\"$value\""}
}
proc rgb2xcolor rgb {
if {$rgb == ""} {return none}
foreach {r g b} [winfo rgb . $rgb] break
format #%02x%02x%02x [expr {$r/256}] [expr {$g/256}] [expr {$b/256}]
}
# Test code:
if {[file tail [info script]] == [file tail $argv0]} {
catch {console show} ;# for Win and Mac
pack [canvas .c]
.c create rect 10 10 90 90 -fill red -outline yellow -width 2
.c create oval 110 10 190 90 -fill blue
.c create poly 175 37 190 80 235 80 190 107 212 150 175 125 \
138 150 152 107 115 80 160 80 -fill yellow
.c create line 50 50 150 50
.c create text 100 100 -text Hello -font {Helvetica 18}
foreach item [.c find all] {
puts $item:[.c type $item],[.c itemconfig $item]\n
}
puts [canvas2svg .c]
bind . {exec wish $argv0 &; exit}
}
if 0 {This is what comes out of the test:
For a more elaborate dumper see http://hem.fyristorg.com/matben/download/can2svg.tcl
----
2007-09-11 [VI] Just using that in an svg file causes Firefox to display
say - No style information. And it displays the xml source rather than the graphic.
The minimum I required to add was an xmlns parameter to the svg tag, like this (only
first line is below).