processing.core
Class PShapeSVG

java.lang.Object
  extended by processing.core.PShape
      extended by processing.core.PShapeSVG
All Implemented Interfaces:
PConstants

public class PShapeSVG
extends PShape

SVG stands for Scalable Vector Graphics, a portable graphics format. It is a vector format so it allows for infinite resolution and relatively small file sizes. Most modern media software can view SVG files, including Adobe products, Firefox, etc. Illustrator and Inkscape can 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. Starting with release 0149, this library has been incorporated into the core via the loadShape() command, because vector shape data is just as important as the image data from loadImage().

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.


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

 PShape moo;

 void setup() {
   size(400, 400);
   moo = loadShape("moo.svg");
 }
 void draw() {
   background(255);
   shape(moo, mouseX, mouseY);
 }
 
This code is based on the Candy library written by Michael Chang, which was later revised and expanded for use as a Processing core library by Ben Fry. Thanks to Ricard Marxer Pinon for help with better Inkscape support in 0154.


Late October 2008 revisions from ricardmp, incorporated by fry (0154)

October 2008 revisions by fry (Processing 0149, pre-1.0) August 2008 revisions by fry (Processing 0149) 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.


Field Summary
 
Fields inherited from class processing.core.PShape
BEZIER_VERTEX, BREAK, CURVE_VERTEX, GEOMETRY, GROUP, height, PATH, PRIMITIVE, VERTEX, width
 
Fields inherited from interface processing.core.PConstants
A, AB, ADD, AG, ALPHA, ALPHA_MASK, ALT, AMBIENT, AR, ARC, ARGB, ARROW, B, BACKSPACE, BASELINE, BEEN_LIT, BEVEL, BLEND, BLUE_MASK, BLUR, BOTTOM, BOX, BURN, CENTER, CENTER_DIAMETER, CENTER_RADIUS, CHATTER, CLOSE, CMYK, CODED, COMPLAINT, CONTROL, CORNER, CORNERS, CROSS, CUSTOM, DA, DARKEST, DB, DEG_TO_RAD, DELETE, DG, DIAMETER, DIFFERENCE, DILATE, DIRECTIONAL, DISABLE_ACCURATE_TEXTURES, DISABLE_DEPTH_SORT, DISABLE_DEPTH_TEST, DISABLE_OPENGL_2X_SMOOTH, DISABLE_OPENGL_ERROR_REPORT, DODGE, DOWN, DR, DXF, EB, EDGE, EG, ELLIPSE, ENABLE_ACCURATE_TEXTURES, ENABLE_DEPTH_SORT, ENABLE_DEPTH_TEST, ENABLE_NATIVE_FONTS, ENABLE_OPENGL_2X_SMOOTH, ENABLE_OPENGL_4X_SMOOTH, ENABLE_OPENGL_ERROR_REPORT, ENTER, EPSILON, ER, ERODE, ERROR_BACKGROUND_IMAGE_FORMAT, ERROR_BACKGROUND_IMAGE_SIZE, ERROR_PUSHMATRIX_OVERFLOW, ERROR_PUSHMATRIX_UNDERFLOW, ERROR_TEXTFONT_NULL_PFONT, ESC, EXCLUSION, G, GIF, GRAY, GREEN_MASK, HALF_PI, HAND, HARD_LIGHT, HINT_COUNT, HSB, IMAGE, INVERT, JAVA2D, JPEG, LEFT, LIGHTEST, LINE, LINES, LINUX, MACOSX, MAX_FLOAT, MAX_INT, MIN_FLOAT, MIN_INT, MITER, MODEL, MOVE, MULTIPLY, NORMAL, NORMALIZED, NX, NY, NZ, OPAQUE, OPEN, OPENGL, ORTHOGRAPHIC, OTHER, OVERLAY, P2D, P3D, PDF, PERSPECTIVE, PI, platformNames, POINT, POINTS, POLYGON, POSTERIZE, PROBLEM, PROJECT, QUAD, QUAD_STRIP, QUADS, QUARTER_PI, R, RAD_TO_DEG, RADIUS, RECT, RED_MASK, REPLACE, RETURN, RGB, RIGHT, ROUND, SA, SB, SCREEN, SG, SHAPE, SHIFT, SHINE, SOFT_LIGHT, SPB, SPG, SPHERE, SPOT, SPR, SQUARE, SR, SUBTRACT, SW, TAB, TARGA, TEXT, THIRD_PI, THRESHOLD, TIFF, TOP, TRIANGLE, TRIANGLE_FAN, TRIANGLE_STRIP, TRIANGLES, TWO_PI, TX, TY, TZ, U, UP, V, VERTEX_FIELD_COUNT, VW, VX, VY, VZ, WAIT, WHITESPACE, WINDOWS, X, Y, Z
 
Constructor Summary
PShapeSVG(PApplet parent, java.lang.String filename)
          Initializes a new SVG Object with the given filename.
PShapeSVG(PShapeSVG parent, XMLElement properties)
           
PShapeSVG(XMLElement svg)
          Initializes a new SVG Object from the given XMLElement.
 
Method Summary
 PShape getChild(java.lang.String name)
          Get a particular element based on its SVG ID.
 void print()
          Prints out the SVG document.
 
Methods inherited from class processing.core.PShape
addChild, apply, applyMatrix, applyMatrix, applyMatrix, applyMatrix, disableStyle, draw, drawImpl, enableStyle, findChild, getChild, getChildCount, getHeight, getName, getWidth, isVisible, post, resetMatrix, rotate, rotate, rotateX, rotateY, rotateZ, scale, scale, scale, setName, setVisible, translate, translate
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PShapeSVG

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


PShapeSVG

public PShapeSVG(XMLElement svg)
Initializes a new SVG Object from the given XMLElement.


PShapeSVG

public PShapeSVG(PShapeSVG parent,
                 XMLElement properties)
Method Detail

getChild

public PShape getChild(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.getChild("Layer 3");
 

Overrides:
getChild in class PShape

print

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