Processing Bug Fixes: SVG, PDF Problems Solved

Import and export is, of course, a huge workflow factor for anyone, especially anyone from a graphic design background. Some of my students at MassArt have been kicking the tires on Processing’s SVG and PDF libraries and running into hitches. There’s nothing more frustrating than getting an obscure error message when you think you’re doing things right. Happily, we’ve got some solutions.

Of course, it’d be great to hear from others with similar issues.

The following libraries might also be of use:
http://www.texone.org/prosvg/
http://processing.unlekker.net/SimplePostscript/index.html

PDF

Several students had problems with font embedding using the built-in library. Symptoms: strange, blocky letters. The issues seem to occur on Mac OS X (all the students in this case I believe were on Mac exclusively.) The reference documentation for the PDF library offers this clue:

Starting in release 0120, text is no longer treated as shape data by default, meaning that the font will need to be installed to view the PDF that’s created. The upside is that the PDF will render better (particularly in light of the Mac OS X bug noted here). To force text to be treated as shape data, use textMode(SHAPE), immediately after size(). See the developer reference for textMode() inside PGraphicsPDF for more specifics.

But so far, forcing SHAPE hasn’t been very helpful.

Colin Owens has this tip instead:

Placing:

hint(ENABLE_NATIVE_FONTS);

before text();

does the trick.

This needs to happen before all your other font/text code. (I believe setup() is optional.)

I also suggested, for those on the Mac, at least, that people get fonts working on Mac and try re-saving in Preview.app for cross-platform / cross-machine compatibility as needed. (The same would be true with Acrobat or any PDF builder on any platform, just in case the library is misbehaving.)

Oddly, people are still having issues in which the first couple of characters are garbled, so they’ve taken to hiding the first two characters used. Bizarre.

SVG

Mahesh Gudapakkam was having some problems with the SVG graphics format, even following examples from Ben Fry. Nicely enough, he solved it himself.

Basically, the issue was tag information being handled in a way other than he intended, thus triggering an array out of bounds error.

Ignite: Visual Code Literacy with Processing, in Five Minutes

For Ignite NYC, I made a five minute presentation demonstrating basic programming concepts, as illustrated in Processing. The point: basic code literacy is as important today as basic arithmetic knowledge, and there’s no reason to be afraid — by working with code visually, you can learn to program, even with “advanced” concepts like object oriented code.

I’ll elaborate on this more for those not there, but if you did make the presentation, these additional code examples should be helpful (in case you missed some of what I was saying in the 15 seconds allotted each slide!)

Referenced Code Examples

Putting something on the screen: Using commands like “line” means everything you do can give you immediate visual response. Line does what it sounds like it does, and takes four simple arguments: x and y coordinates for the start point and end point.

line(0,0,200,200);

An interactive line: Add a draw() loop, and the line you draw becomes interactive, working with the mouse.

void setup() {
  size(400,400);
}
void draw() {
  background(255); //try moving this line to setup and watch what happens!
  line(0,0,mouseX,mouseY);
}

Make values visible: The numbers can also be meaningful values — in this case, from the internal clock.

void setup() {
  size(400,400);
  strokeWeight(50);
  strokeCap(SQUARE);
}

void draw() {
  smooth();
  background(255);
  line(100,0,100,(hour()/24.0)*height);
  line(200,0,200,(minute()/60.0)*height);
  line(300,0,300,(second()/60.0)*height);
}

Variables: Using variables in place of fixed number values allows you to adjust that value — over time for animation, to transform the value aesthetically, and to experiment with how code works.

In fact, the best way to start to figure out code — even before you’re sure how it works — is often to start messing around with values. Sometimes “breaking” code you don’t understand can be a great learning experience. For instance, see what happens when students start to “mess up” the game Breakout to transform it aesthetically and make new art.

Changing values can also help create surprise. You don’t have to hook up variables the way someone would expect. Watch what happens when you move mouseX and mouseY with our interactive line:

void setup() {
  size(400,400);
}

void draw() {
  background(200);
  line(mouseY,height/2,width/2,mouseX);
}

Loops: Seeing a loop happen — even a simple one — illustrates the way the process of iteration can work in more complex problems.

int numOfLines = 10;
int distance = 10;

size(200,200);
background(0);
stroke(255);
strokeWeight(3);

int yPos = 20;
for(int i=0; i<numOfLines; i++) {
  line(20,yPos,width-20,yPos);
  yPos += distance;
}

//bonus!
/*
int yPos = 20;
for(int i=0; i<numOfLines; i++) {
  line(20,yPos,width-20,yPos);
  yPos += distance;
  distance += 2;
}
*/

Loops and arrays: Using arrays (lists) with loops can help to simplify code, even in a simple example — loops and lists are a natural match for one another, since the former lets you iterate through the latter.

int[] circles = {170,113,78,30,13};
size(400,400);
background(150);
noStroke();
smooth();

for (int i=0;i<circles.length;i++){
  fill(random(0,255),random(0,255),random(0,255));
  ellipse(width/2,height/2,circles[i],circles[i]);
}

Loops within loops: You can also place a loop inside a loop. What’s that mean? It helps to see the result.

int[] circles = {170,113,78,30,13};
size(400,400);
background(150);
noStroke();
smooth();

for (int j=0;j<4;j++) {
  for (int i=0;i<circles.length;i++){
    fill(random(0,255),random(0,255),random(0,255));
    ellipse((width/4)*j,(height/4)*j,circles[i],circles[i]);
  }
}

You can extend the loops within loops concept to more involved iteration, such as iterating through columns, then rows — essential to processing the individual pixels in an image.

Java is quick enough for doing this with smaller images and even video, though for better performance, it’s also possible to perform some tasks using shaders on the GPU, thanks to Processing’s support for OpenGL. (Image processing, video processing, and OpenGL are best left to a separate discussion!)

Functions can be understood as recipes. In pseudo-code:

makes cake "Cakemaker" (eggs, flour, ingredients, etc...) {
  step 1
  step 2
  step 3
  return cake, serves 8
}

Objects: Object-oriented programming may seem mystical, but it’s really just a way to better organize code functions. For instance, in the case of the cake, we might consider what the attributes of a cake are, what can be done to it (make a cake, eat a cake, throw a cake at someone’s face), and then organize it relative to smaller categories of related things (cupcakes) and bigger categories of things (dessert).

In visual terms, that allows us to create fast, readable, powerful systems for visual expression, like particle systems. In the included Processing example Topics > Simulate > MultipleParticleSystem, for instance, you’ll see particles and systems of particles defined as objects.

Code for Making the Presentation

For Ignite, I wrote a quick Processing sketch that allowed me to quickly generate the slides I would use. The event needed a PowerPoint file, but I could at least output images to drop into another program for PPT export (hence the saveFrame() function, which saves image files). And for my own purposes, I don’t even need PowerPoint.

Note that in addition to the Processing file, the data folder contains:

  • image assets
  • fonts (generated in Processing with Tools > Create font…
  • a text file with the text for the slides

To create the text file, I simply separated bullets with the “@” character and put a tab between titles and body text. The split() command in Processing then divides up that text into necessary Strings. So, for instance, the first slide looks like this:
visual code literacy Code literacy == math literacy @Technologists, artists (non-specialists)

What this means is that very little code is needed for the presentation — and it’d basically work for any presentation of this template. I saved time versus working in PowerPoint.

You need some Java classes, listed at top, for this to work.

import java.util.*;
import java.text.*;

String[] slides;

PFont font;
int currSlide = 0;

void setup() {
  size(800,600);
  slides = loadStrings("p5_5slides.txt");
  background(0);
  font = loadFont("TimesNewRomanPSMT-14.vlw");
}

void draw() {
  String[] headerString = split(slides[currSlide], TAB);
  String[] bullets = split(headerString[1], '*');
  println("Header: " + headerString[0]);
  for (int i=0;i<bullets.length;i++) {
    println("Bullet: " + bullets[i]);
  }
  saveFrame();
  if(currSlide < slides.length-1) {
  currSlide++;
  }
  else {
    //currSlide = 0;
    stop();
  }
}

Image Credits

In the presentation…

Dinner check by Jim Reynolds:
http://flickr.com/photos/revjim5000/2445394705/

LOGO-generated image graphic created by remi, logo source code created by Arbol01, via Wikimedia:
http://en.wikipedia.org/wiki/Image:Remi_turtlegrafik.png

Apple II image by Florian Eckerstorfer
http://flickr.com/photos/florianeckerstorfer/1870727397/

Breakout art, as featured in Create Digital Motion:
http://createdigitalmotion.com/2007/04/17/breakout-hacked-into-art-in-processing/
See also Game Mod:
http://www.trsp.net/teaching/gamemod/

NOAA climate information from the National Weather Service:
http://www.weather.gov/climate/

Vintage cookbooks by Patrick Q:
http://flickr.com/photos/patrick_q/199986515/

All other images by the author

Massaging Media Processing Workshop

Presentation Slides

[SlideShare embed]

PDF download

Examples Download

I’ve been working on creating a set of stupidly simple examples for Processing — even simpler than the ones that come with Processing itself. The idea is to put as little as humanly possible in them, so that you can demonstrate the basics of programming with code examples, even in very short workshops for people who may not have any programming experience (and I mean really short — what if you want to show something in 5 minutes?)

Download the sample files:

Simple Examples 0.01 [ZIP archive for Processing 0135]

Consider this an early preview, and I welcome feedback. Major thanks to Ben Fry for some of the inspiration about how to go about this.

Must-Have Processing Libraries for Multimedia

To a beginning Processing user, the libraries section of the Processing site can cause a "kid in the candy store" effect. I actually recommend going a little easy, working with one library at a time. But, at the same time, working with libraries really is essential to working with Processing, because Processing "core" is kept as conservative as possible. This is especially true if you’re working with multimedia. Here are the libraries I recommend for specific multimedia applications, as a starting point:

Tablets + Processing: Now on Mac, Too

There’s been a lot of interest in using tablets with Processing, for obvious reasons — it allows Processing to be a real-time graphics tool (or graphics for generating audio, or whatever it is you want). And Wacom tablets are cheap, high-resolution inputs. The superb SuperDraw project works with tablet input.

Unfortunately, the best library for tablets has been Windows-only (works with any Java app and — very nice — with applets in a browser):

JTablet

Now, there’s a Mac alternative, which eventually will wrap JTablet on Windows so you can deploy across platforms:

libTablet 0.1

The wonderful Marcus Wendt put this together — thanks! Let me know how it goes.

New Library: OpenSoundControl with Processing

Jesse Kriss, who built MaxLink for embedding Processing sketches in Max/MSP/Jitter or communicating between Processing and Max, has a new library called EasyOSC. It uses the OpenSoundControl protocol, which is useful for communicating between apps, computers, and some hardware (like the Monome).

What’s great about it is how easy it is to communicate with other software — ideal if you’re using Processing alongside a sound tool like Reaktor or (in Jesse’s case) ChucK, or with multiple computers running.

http://jklabs.net/easyosc/

You use simple syntax like osc.send("values", values) to transmit data.

Audiovisualists, this should help your mayhem across media. If I come up with some examples, I’ll post them to CDM, perhaps with a basic tutorial for those unfamiliar with how OSC works (and there are some tricks to it in other languages).

How to Read Your Email with the JavaMail API, Starring Gmail IMAP

Computer musicians are often accused of looking like they’re checking their email when they’re playing. (Occasionally, I have seen someone doing just that!) But I wanted to experiment with playing music by checking my email. Java is an easy way to do that; with the free JavaMail API (part of the Java platform), you can access just about any IMAP or POP account, even a secure one, like Gmail.

What you need

JavaMail API (part of the enterprise Java platform, but available for free to desktop Java users as well)

Get the download, refer to the JavaDocs for reference (note that if you’re not using Java 6, there’s an extra support download for the "JavaBeans Activation Framework" — don’t actually need to know what it does, but you will need it with earlier Java versions)

jGuru: Fundamentals of the JavaMail API: A decent step-by-step tutorial, though you may want to compare it to some of the sample code (installed to the "demo" folder when you install JavaMail) to see how this fits together in a completed example.

Connecting to your favorite email provider

The JavaMail API FAQ specifically addresses Gmail and Yahoo! Mail.

IMAP is more fun, because it allows access to folders other than the inbox. To connect to Gmail IMAP, you need to use the "imaps" protocol for a secure SSL connection, rather than plain "imap".

Accessing folders is a little wonky in Gmail. Aside from the Inbox, labels and folders are accessed like this: store.getFolder("[Gmail]/Spam").

Examples

It takes just a few lines to connect and retrieve some basic information about messages. I’ll post code examples soon.

It’s helpful to know something about the java.util.Date class. Specifically, if you want to compare sent dates or otherwise manipulate time, first use message.getSentDate(), then take that date information and use getTime() to translate into milliseconds.

Processing Class Outline

I’ll be posting class notes for my Processing intro at New York’s Harvestworks online. Based on student feedback about what interests people, here’s the breakdown for the three classes.

Week One

What’s Processing?

A little bit of history

Processing is Java

How is Processing different from things that aren’t Processing?

Hello, world, Processing style

Install Processing, Java

Draw "hello"

Say "hello"

Processing syntax

Using the IDE

Using help and documentation

Putting stuff on the screen

Coordinates

Drawing

Color

3D coordinates

Textures

Load an image

Load a video

What next

Where to find inspiration

Where to find libraries

Where to find code