Graph SVG SVG command

Commands in this namespace document graph SVG output.

SVG is used below as a placeholder for:

GRAPHINST svg

The SVG component exports the complete graph, including elements, axes, grid, markers, legend, title and margins. It is available on graph, stripchart, barchart and polar widgets without requiring Cairo. There is one SVG configuration per widget, independent of PostScript and the screen renderer. There are no named SVG objects.

Most of the embedded images in current documentation are generated as an .svg images and could be found under docs/images.

Generating SVG

Return the document as a Tcl string or write it directly to a file:

set document [.g svg output]
.g svg output plot.svg
.g svg output portable.svg -embedfonts yes
.g svg output plot.svg -width 1200 -height 800 -decorations no

File output uses UTF-8 with LF line endings. A filename starting with a dash must be prefixed with a directory, for example ./-plot.svg. There is no -- filename separator.

Option database

SVG is a named component with resource name svg and class Svg, independent of the widget and other export formats. Defaults are read once when the graph widget is created, for all graph types. Later database changes affect new widgets only. Explicit svg configure and svg output options override the initialized values, with the existing persistent output-option behaviour unchanged.

Option

Resource name

Resource class

-width

width

Width

-height

height

Height

-decorations

decorations

Decorations

-embedfonts

embedFonts

EmbedFonts

option add *Svg.width 800
option add *svg.embedFonts true
rbc::graph .g
.g svg configure -width 900

Dimensions accept Tk screen distances; zero selects the widget dimension. Invalid database values fail widget creation, as for other components. Configuration descriptions continue to report the built-in default separately from the current value. Use a component-qualified pattern so export dimensions do not also change the graph widget’s size. No child window is created for this component.

SVG options

Option

Default

Description

-width distance

0

Non-negative output width. Zero uses the graph width.

-height distance

0

Non-negative output height. Zero uses the graph height.

-decorations boolean

1

Include graph/legend backgrounds and relief borders. False uses white backgrounds; axes, labels and symbols remain visible.

-embedfonts boolean

0

Embed native outline fonts and fallback faces as CSS webfonts. Disabled by default.

Dimensions accept pixels or Tk screen distances, such as 6i or 15c, and are stored as integer pixels. Zero uses the current widget dimension, or its requested dimension before layout is available. The graph is laid out again for the export dimensions; the SVG canvas has a matching viewBox. Export does not resize the on-screen widget.

.g svg configure -width 6i -height 4i
puts [.g svg cget -width]
puts [.g svg configure -decorations]
.g svg configure -width 0 -height 0

Configuration queries use five-element lists:

{option name class default current}

Names/classes are width/Width, height/Height, decorations/Decorations, and embedFonts/EmbedFonts. These describe the query result only: SVG settings are not read from Tk’s option database. Set them with svg configure or svg output.

Invalid option/value lists leave all SVG settings unchanged. Once an output option list has been validated, its values remain configured even if subsequent rendering or file writing fails.

Supported content and limitations

SVG export supports traces, error bars, X/Y error bands, grids, axes, solid areas and bars, standard geometric symbols, line/polygon/text markers, legends, borders and editable text. Polar/Smith labels use the same layout and formatting as PostScript. Export remaps full source geometry rather than screen-decimated traces. Solid area fills preserve -areaopacity; solid error bands preserve -errorbandopacity. By default, fonts are referenced by family; text appearance depends on installed fonts in the SVG viewer. Text rotation, baseline positions, measured widths and shadows are retained.

With -embedfonts yes, SVG stores native TrueType/OpenType fonts as base64 CSS @font-face data URLs. Text remains Unicode and editable, including mathematical symbols and fallback characters. Native font lookup, supported outline formats and embedding restrictions are the same as PDF’s embedded mode, described below. The exporter preserves Tk character positions; legacy zero-width missing glyphs receive the native fallback advance. Each used face is stored once per document. All outlines are retained, while the embedded character map covers the characters used in this export, including legacy Symbol mappings. Adding different characters later may require re-exporting or another font.

Embedding increases file size and requires a viewer supporting CSS webfonts and data URLs. Some SVG importers ignore embedded webfonts; browser security policies may also block them. Such viewers can substitute fonts. Unlike converting text to paths, embedding preserves text selection and searching. A missing glyph or a restricted/unsupported font fails before the output file is opened. Use -embedfonts no for the original viewer-dependent font behavior.

Photo image markers embed PNG data directly in the SVG, preserving RGB and alpha without external image files. Mapped scaling and plot clipping are retained. Bitmap markers (including rotation and explicit masks) export as vector pixel shapes. Bitmap element, active-pen and legend symbols reuse vector definitions within each symbol drawing pass; their colors, masks and aspect ratio are retained.

Stipple areas, polygon markers, bars and bar legend samples export as vector patterns. Stipples retain foreground/background colors and transparent gaps. Photo area tiles embed a repeating PNG with alpha and take precedence over stipples. Stipples align with graph pixels; photo tiles retain the screen renderer’s toplevel-relative phase. Bar legend patterns restart at each sample’s corner. Pattern sizes are in export pixels. -areaopacity affects solid fills only, not stipples or tiles.

Non-photo Tk image markers and tiles are drawn off-screen and embedded as PNG. Transparency is recovered from black/white renders, which assumes stable source-over drawing; custom image types whose output depends on destination contents may differ. No external image files are required.

Mapped window markers embed their current native window pixels as opaque PNG. Hidden or unmapped window markers are omitted. Window contents are raster snapshots at their current widget size, not vector drawings; native capture availability and visibility constraints apply. Export does not run the event loop to refresh a window. Refresh pending display work before exporting when needed.

Capture failures and invalid XML text report an error before the output file is opened. PostScript printer options (paper size, landscape, preview, font/color maps) are not SVG options. The SVG canvas uses pixel dimensions and a matching viewBox.

Commands

cget

Queries the current value of one SVG configuration option. See SVG options.

cget option

Parameters

option:

SVG configuration option.

Return value

Current option value, as integer pixels for dimensions or 0/1 for boolean options.

configure

Queries or modifies SVG configuration options. With no arguments, returns a list of all option descriptions. With one option, returns its five-element description. Otherwise accepts option/value pairs and updates the settings after validating the entire list. See SVG options.

configure option
configure option value ?option value ...?

Parameters

option:

SVG configuration option.

value:

New value for the option.

Return value

Configuration information when querying, otherwise nothing.

output

Generates the SVG document. Without a filename, returns its XML text. With a filename, writes the document to that file, replacing existing contents. Optional settings remain configured after their option/value list is validated, including when a later rendering or file operation fails. Rendering and XML validation complete before the file is opened; an I/O failure can still leave a partial file. See Supported content and limitations.

output ?fileName? ?option value ...?

Parameters

fileName:

Optional file to create or replace; prefix dash-leading names with ./.

option:

Optional SVG configuration option.

value:

Value for the configuration option.

Return value

SVG XML when no filename is supplied, otherwise nothing.


Copyright (c) George Yashin