processing.candy
Class SVG

java.lang.Object
  extended by processing.candy.SVG

public class SVG
extends java.lang.Object

Candy is a minimal SVG import library for Processing. Candy was written by Michael Chang, and later revised and expanded for use as a Processing core library by Ben Fry.

SVG stands for Scalable Vector Graphics, a portable graphics format. It is a vector format so it allows for infinite resolution and relatively minute file sizes. Most modern media software can view SVG files, including Firefox, Adobe products, etc. You can use something like Illustrator to edit SVG files.

We have no intention of turning this into a full-featured SVG library. The goal of this project is a basic shape importer that is small enough to be included with applets, meaning that its download size should be in the neighborhood of 25-30k. Because of this size, it is not made part of processing.core, because it would increase the download size of any applet by 20%, and it's not a feature that will be used by the majority of our audience. For more sophisticated import/export, consider the Batik library from the Apache Software Foundation. Future improvements to this library may focus on this properly supporting a specific subset of SVG, for instance the simpler SVG profiles known as SVG Tiny or Basic, although we still would not support the interactivity options.

This library was specifically tested under SVG files created with Adobe Illustrator. We can't guarantee that it will work for any SVGs created with other software. In the future we would like to improve compatibility with Open Source software such as InkScape, however initial tests show its base implementation produces more complicated files, and this will require more time.

An SVG created under Illustrator must be created in one of two ways:

Saving it any other way will most likely break Candy.


A minimal example program using Candy: (assuming a working moo.svg is in your data folder)

 import processing.candy.*;
 import processing.xml.*;

 SVG moo;
 void setup() {
   size(400,400);
   moo = new SVG("moo.svg",this);
 }
 void draw() {
   moo.draw();
 }
 
Note that processing.xml needs to be imported as well. This may not be required when running code within the Processing environment, but when exported it may cause a NoClassDefError. This will be fixed in later releases of Processing (Bug 518).


February 2008 revisions by fry (Processing 0136)

Revisions for "Candy 2" November 2006 by fry Revision 10/31/06 by flux Some SVG objects and features may not yet be supported. Here is a partial list of non-included features For those interested, the SVG specification can be found here.


Nested Class Summary
 class SVG.Path
           
 
Field Summary
 float height
           
 float width
           
 
Constructor Summary
SVG(PApplet parent, java.lang.String filename)
          Initializes a new SVG Object with the given filename.
SVG(PApplet parent, XMLElement svg)
          Initializes a new SVG Object with the given filename.
 
Method Summary
 void draw()
          Temporary hack for gradient handling.
 void draw(float x, float y)
          Convenience method to draw at a particular location.
 void draw(float x, float y, float c, float d)
           
 void drawImpl()
          Draws the SVG document.
 void drawMode(int which)
          Set the orientation for drawn objects, similar to PImage.imageMode().
 void fillGradient(java.lang.String name, float x, float y, float r)
           
 void fillGradient(java.lang.String name, float x1, float y1, float x2, float y2)
           
 SVG get(java.lang.String name)
          Get a particular element based on its SVG ID.
 void ignoreStyles()
          Overrides SVG-set styles and uses PGraphics styles and colors.
 void ignoreStyles(boolean state)
          Enables or disables style information (fill and stroke) set in the file.
 float parseUnitSize(java.lang.String text)
          Parse a size that may have a suffix for its units.
 void print()
          Prints out the SVG document useful for parsing
 void strokeGradient(java.lang.String name, float x, float y, float r)
           
 void strokeGradient(java.lang.String name, float x1, float y1, float x2, float y2)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

width

public float width

height

public float height
Constructor Detail

SVG

public SVG(PApplet parent,
           java.lang.String filename)
Initializes a new SVG Object with the given filename.


SVG

public SVG(PApplet parent,
           XMLElement svg)
Initializes a new SVG Object with the given filename.

Method Detail

parseUnitSize

public float parseUnitSize(java.lang.String text)
Parse a size that may have a suffix for its units. Ignoring cases where this could also be a percentage. The units spec:


get

public SVG get(java.lang.String name)
Get a particular element based on its SVG ID. When editing SVG by hand, this is the id="" tag on any SVG element. When editing from Illustrator, these IDs can be edited by expanding the layers palette. The names used in the layers palette, both for the layers or the shapes and groups beneath them can be used here.
 // This code grabs "Layer 3" and the shapes beneath it.
 SVG layer3 = svg.get("Layer 3");
 


strokeGradient

public void strokeGradient(java.lang.String name,
                           float x,
                           float y,
                           float r)

strokeGradient

public void strokeGradient(java.lang.String name,
                           float x1,
                           float y1,
                           float x2,
                           float y2)

fillGradient

public void fillGradient(java.lang.String name,
                         float x,
                         float y,
                         float r)

fillGradient

public void fillGradient(java.lang.String name,
                         float x1,
                         float y1,
                         float x2,
                         float y2)

draw

public void draw()
Temporary hack for gradient handling. This is not supported and will be removed from future releases.


draw

public void draw(float x,
                 float y)
Convenience method to draw at a particular location.


draw

public void draw(float x,
                 float y,
                 float c,
                 float d)

drawImpl

public void drawImpl()
Draws the SVG document.


drawMode

public void drawMode(int which)
Set the orientation for drawn objects, similar to PImage.imageMode().

Parameters:
which - Either CORNER, CORNERS, or CENTER.

ignoreStyles

public void ignoreStyles()
Overrides SVG-set styles and uses PGraphics styles and colors. Identical to ignoreStyles(true).


ignoreStyles

public void ignoreStyles(boolean state)
Enables or disables style information (fill and stroke) set in the file.

Parameters:
state - true to use user-specified stroke/fill, false for svg version

print

public void print()
Prints out the SVG document useful for parsing