biancii
biancii

Reputation: 13

Exported Processing sketch that uses Fontastic library not creating TrueType like it does in Processing

The original file is WaveFont.pde, a Processing sketch and an example of creating characters with bezier curves. It uses Fontastic library(a Processing library). I exported this .pde file to android and it automatically generated codes. But the problem is that it always force closes whenever I try to run it on my Android device and it also does not generate the ttf like it does in Processing. Please take a look on the following lines of code. Thank you.

This is the MainActivity.java

import android.app.Activity;
import android.os.Bundle;   

public class MainActivity extends Activity {
public static Wave font;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    font = new Wave();
    font.setup();
}
}

and this is the class named Wave.java

import fontastic.FPoint;
import fontastic.Fontastic;
import processing.core.PApplet;
import processing.core.PFont;

public class Wave extends PApplet {
    Fontastic f;
    float charWidth = 512;
    PFont myFont;
    int version = 0;
    boolean fontBuilt = false;

    public void setup(){
        randomize();
        create();
    }

    public void randomize(){
        version++;
        if (f != null) { f.cleanup(); }
        f = new Fontastic(this, "WaveFont" + nf(version,4));
        f.setAdvanceWidth(PApplet.parseInt(charWidth));
        for (int i=0; i<Fontastic.alphabet.length; i++) {
            char c = Fontastic.alphabet[i];
            FPoint[] points = new FPoint[4];
            float rectSize = charWidth * 0.5f;
            float rnd = charWidth * 0.2f;
            points[0] = new FPoint(charWidth / 2 - rectSize / 2, charWidth / 2 - rectSize / 2);
            points[1] = new FPoint(charWidth / 2 - rectSize / 2, charWidth / 2 + rectSize / 2);
            points[2] = new FPoint(charWidth / 2 + rectSize / 2, charWidth / 2 + rectSize / 2);
            points[3] = new FPoint(charWidth / 2 + rectSize / 2, charWidth / 2 - rectSize / 2);
            points[0].setControlPoint1(points[0].x + rnd, points[0].y + random(-rnd, rnd));
            points[1].setControlPoint1(points[1].x + random(-rnd, rnd), points[1].y - rnd);
            points[2].setControlPoint1(points[2].x - rnd, points[2].y + random(-rnd, rnd));
            points[3].setControlPoint1(points[3].x - random(-rnd, rnd), points[3].y + rnd);
            points[0].setControlPoint2(points[0].x + random(-rnd, rnd), points[0].y + rnd);
            points[1].setControlPoint2(points[1].x + rnd, points[1].y + random(-rnd, rnd));
            points[2].setControlPoint2(points[2].x + random(-rnd, rnd), points[2].y - rnd);
            points[3].setControlPoint2(points[3].x - rnd, points[3].y + random(-rnd, rnd));
            f.addGlyph(c).addContour(points);
        }
    }

    public void create(){
        f.buildFont();
        f.cleanup();
        myFont = createFont(f.getTTFfilename(), 200);
        fontBuilt = true;
    }
}

The Processing sketch (WaveFont.pde) is:

import fontastic.*;
import java.util.List;

Fontastic f;

float charWidth = 512;

PFont myFont;
int version = 0;

boolean fontBuilt = false;

void setup() {

  size(1400, 600);
  fill(0);

  randomizeFont();

  createFont();

}


void draw() {

  background(255);

  strokeWeight(2);
  textSize(25); // for small numbers at bezier lines

  for (int i=0; i<5; i++) {
    pushMatrix();
    translate(width/2, height/4);
    scale(0.5);
    translate(-4*charWidth / 2 + i*charWidth, 0);
    translate(-charWidth/2, charWidth/2);
    renderGlyphOutline(Fontastic.alphabet[i],color(255,0,0), color(100)); 
    // fill(0,255,0,80);
    // renderGlyphSolid(Fontastic.alphabet[i]);
    popMatrix();
  }

  // fill(0,255,0,80);
  // noStroke();
  // renderGlyphSolid('A');

  if(fontBuilt) {
    pushMatrix();
    textFont(myFont);
    textAlign(CENTER, CENTER);
    fill(0);
    textSize(charWidth);
    text("ABCDE", width/2, height*0.6);
    popMatrix();
  }

}

void randomizeFont() {

  version++;

  if (f != null) { f.cleanup(); }

  f = new Fontastic(this, "WaveFont" + nf(version,4));

  f.setAdvanceWidth(int(charWidth));

  for (int i=0; i<Fontastic.alphabet.length; i++) {

    char c = Fontastic.alphabet[i];

    FPoint[] points = new FPoint[4];

    float rectSize = charWidth*0.5;
    float rnd = charWidth*0.2;

    points[0] = new FPoint(charWidth/2 - rectSize/2, charWidth/2 - rectSize/2);
    points[1] = new FPoint(charWidth/2 - rectSize/2, charWidth/2 + rectSize/2);
    points[2] = new FPoint(charWidth/2 + rectSize/2, charWidth/2 + rectSize/2);
    points[3] = new FPoint(charWidth/2 + rectSize/2, charWidth/2 - rectSize/2);

    points[0].setControlPoint1(points[0].x + rnd, points[0].y + random(-rnd, rnd));
    points[1].setControlPoint1(points[1].x + random(-rnd, rnd), points[1].y - rnd);
    points[2].setControlPoint1(points[2].x - rnd, points[2].y + random(-rnd, rnd));
    points[3].setControlPoint1(points[3].x - random(-rnd, rnd), points[3].y + rnd);

    points[0].setControlPoint2(points[0].x + random(-rnd, rnd), points[0].y + rnd);
    points[1].setControlPoint2(points[1].x + rnd, points[1].y + random(-rnd, rnd));
    points[2].setControlPoint2(points[2].x + random(-rnd, rnd), points[2].y - rnd);
    points[3].setControlPoint2(points[3].x - rnd, points[3].y + random(-rnd, rnd));

    f.addGlyph(c).addContour(points);

  }

}

void renderGlyphOutline(char c, color linecolor, color handlecolor) {

    FPoint[] points = f.getGlyph(c).getContour(0).getPointsArray();

    // Draw the outline in Processing 
    for (int i=0; i<points.length; i++) {
      FPoint p1 = points[i];
      FPoint p2 = points[(i+1)%points.length];
      bezierWithHandles(i+"", p1.x, -p1.y, p1.controlPoint2.x, -p1.controlPoint2.y, p2.controlPoint1.x, -p2.controlPoint1.y, p2.x, -p2.y, linecolor, handlecolor);
    }

}

void renderGlyphSolid(char c) {

  FContour[] contours = f.getGlyph(c).getContoursArray();

  for (int j=0; j<contours.length; j++) {

    FPoint[] points = f.getGlyph(c).getContour(j).getPointsArray();

    if (points.length > 0) { //just to be sure    
      // Draw the solid shape in Processing
      beginShape();      
      for (int i=0; i<points.length; i++) {
        FPoint p1 = points[i];
        FPoint p2 = points[(i+1)%points.length];
        if (p1.hasControlPoint2() && p2.hasControlPoint1()) {
          if (i == 0) { 
            vertex(points[0].x, -points[0].y);
          }
          bezierVertex(p1.controlPoint2.x, -p1.controlPoint2.y, p2.controlPoint1.x, -p2.controlPoint1.y, p2.x, -p2.y);
        }
        else {
          vertex(p1.x, -p1.y);
        }
      }
      endShape();
    }
  }

}


void createFont() {

  f.buildFont();
  f.cleanup();

  myFont = createFont(f.getTTFfilename(), 200);

  fontBuilt = true;

}



void bezierWithHandles(String legend, float p1x, float p1y, float cp1x, float cp1y, float cp2x, float cp2y, float p2x, float p2y, color linecolor, color handlecolor) {

  stroke(handlecolor);
  line(p1x, p1y, cp1x, cp1y);
  line(p2x, p2y, cp2x, cp2y);

  fill(handlecolor);
  noStroke();
  float ellipseSize = 10;
  ellipse(cp1x, cp1y, ellipseSize,ellipseSize);
  ellipse(cp2x, cp2y, ellipseSize,ellipseSize);

  stroke(linecolor);
  noFill();
  bezier(p1x, p1y, cp1x, cp1y, cp2x, cp2y, p2x, p2y);

  stroke(0);
  textAlign(CENTER,CENTER);
  text(legend, p1x, p1y+25);

}

void keyPressed() {

  if (key == ' ') {
    randomizeFont();
    fontBuilt = false;
  }

  if (key == 's') {
    createFont();
  }
}

Upvotes: 0

Views: 209

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42176

Your error is complaining that the code uses java.awt.Point, which makes sense because the Android API doesn't implement that class.

It looks like you're using a Java library, not an Android library. Android and Java are almost the same, but Android does not include things like Swing or AWT, which is where the java.awt.Point class comes from.

To fix this problem, you need to find a version of the API built specifically for Android, not for general Java.

Upvotes: 0

Related Questions