SVGFile

Part of wslib


see also: SVG


** requires DOMDocument classes v0-2 from the swiki **

http://swiki.hfbk-hamburg.de/MusicTechnology/750


The SVGFile object contains all data of an SVG file. SVGFile can read and write SVG files and the data inside can be processed.


SVGFiles can be viewed in:

FireFox 2.0

Safari (only embedded)

browsers with Adobe SVG Viewer plugin installed

...


SVGFiles can be edited in:

Adobe Illustrator ®

InkScape (open source)

...


SVG is the standard vector file-type for WikiPedia


creation


SVGFile ( path, objects, width, height )

creates a new SVGFile object


path :  the path to the file. Nothing will be written to file until the write method is used. 

The path can be relative and doesn't need to have a valid extension.

objects : An Array of SVGObjects. May be empty or nil at creation

width, height :  the width and height of the file's artboard in pixels

(

f = SVGFile( "~/Desktop/test-sc", 

[ SVGEllipse( 200, 200, 75, 50, strokeColor: "red" ),

  SVGText( "circle", 200, 200, "Helvetica", 24 )

  .transform_( SVGTransform( "skewX( -0.25 )" ) ),

] );

)


f.plot;


SVGFile.read ( path )

reads an SVG file from disk


f = SVGFile.read( Document.current.path.dirname ++ "/example.svg" );


// example.svg is an SVG file created with Adobe Illustrator CS2 as SVG1.1

// containing a rect, a line, a text ("test svg file") and a filled circle

f.objects;


f.postTree; ""; // posts the tree of objects


f.plot; // plots the file in a window ( works for SwingOSC as well )


f.format;  // returns the (reformatted) xml data


f.path_( "~/Desktop/from-sc" ).write;  // write a new file to the desktop 


"~/Desktop/from-sc.svg".openWith( "Firefox.app" ); // in case you have FireFox 2.0 installed



SVGFile.parseXML ( xml, path )

parses raw SVG formatted XML data into a new SVGFile.


(

f = SVGFile.parseXML( 

"<svg width='300' height='250'>

<rect  stroke-width='1' x='10' y='32' width='281' fill='none' height='168' stroke='red' />

</svg>" );

)


f.plot;



instance variables


objects

all SVGObjects in an array. The order of the array is the order of execution; the last item is always the frontmost layer.


path

the file path. Can be relative and doesn't need to contain a ".svg" extension. The extension will be changed to that when write is called.


width, height

width and height


userUnit

defaults to 'px', which is the only supported unit right now



instance methods


write ( overwrite, ask,  postFormat )

writes the file to disk, on the path specified in the path variable.

overwrite : write checks if the file exists. 

true : existing file will be overwritten without notice

false (default) : behaviour depends on ask

ask : only used when overwrite == false

true (default) : will show a SCAlert with several renaming options

false : will automatically generate a new name and save to that

postFormat : if true (default) will post the formatted xml data as saved in the file

If the path variable is nil no file will be written, but the data will still be posted


f = SVGFile.read( Document.current.path.dirname ++ "/example.svg" );

f.write;  // a dialog should appear

read ( replaceObjects )

reads the file specified in the path variable. 

replaceObjects : if true (default) replaces the current object sin the SVGFile with the ones read from disk/

if false the objects read from disk are added to the objects that are already in the objects array.

parseXML ( xml, replaceObjects )

same as creation version, but overwrites or adds to objects in the SVGFile.


plot 

creates a SCWindow and plots the file in it.


draw

to be used inside a drawHook or drawFunc; draws all objects in the file.


asRect

returns a Rect derrived from the outer bounds of all objects inside the SVGFile


f = SVGFile.read( Document.current.path.dirname ++ "/example.svg" );

f.asRect;


asGUICode ( penClass )

returns a string with executable GUI-style pen code. If you copy the code to a file, enclose it in curly brackets ( { } ) and use it as a drawHook function for a GUI.window it will display the file again. Can also be called upon individual SVGObjects


asPostScript, writePostScript ( argPath )

returns postscript code as string or writes it to a file (.eps). If no path is specified the path will be the same as that of the SVGFile, but with a ".ps" extension. Note that PostScript doesn't support transparencies, and SVGPathSegments of type 'arc' are currently not supported. For all other objects it should work as well as the SVGFile:plot function works. Reading from PostScript files is not supported, and probably never will be.

PostScript files can be viewed in Preview.app (OSX) and from there converted to PDF.


Note: asPostScript and writePostScript can also be called upon regular drawHook functions, as long as they are written in GUI.pen style. This wslib feature is not yet documented (based on ColPen), but works very well.



writePDF ( argPath )

Based on the above method SFGFile can also create PDF files. But only if the GhostScript unix executable is installed. If it's not, or not found, this method will display a message in the postwindow and create a PostScript file instead. If the GhostScript excutable (/usr/local/bin/gs) is found however, this method will first create a PostScript file in the /tmp directory, then convert it to PDF and then delete the PostScript file again. Much faster then the conversion used by Preview.app, but sometimes less solid. 


The GhostScript library can be downloaded at http://www.ghostscript.com/awki


(

f = SVGFile( "~/Desktop/scribbly.svg", [ ] );

10.do({

f.add( SVGPath([[\M, 200, 200]] ++ ({ [\s] ++ ({50.rand2}!4) }!10), strokeColor: Color.rand) );

});

)


f.plot;

f.writePDF; // will produce a .ps file instead if GhostScript is not installed


Note: in the current version of gs (8.57) there seems to be a problem with the /findfont command, causing errors when text is involved. 




accessing / testing


at ( index ), copySeries ( first, second, last ), put (index, item )

calls .at and copySeries on objects, also to provide compatibility with the ..[..] syntax in SC


f = SVGFile.read( Document.current.path.dirname ++ "/example.svg" );


f[0];

f[1..];

f[[0,3]];


allObjects

returns a flat array with all SVGObjects including the ones inside SVGGroups.


hasCurves

tests if there are any SVGPath objects in the SVGFile which contain \curveTo or \sCurveTo operations


postTree

post a tre-like structure with all objects and groups contained in the objects array