导出的处理草图使用Fontastic库不像处理中那样创建TrueType

时间:2016-02-15 14:05:12

标签: java android processing fontastic

原始文件是WaveFont.pde,处理草图和使用贝塞尔曲线创建字符的示例。它使用Fontastic库(Processing库)。我将这个.pde文件导出到android并自动生成代码。但问题是,每当我尝试在我的Android设备上运行它时,它总是强制关闭,并且它也不像处理中那样生成ttf。请查看以下代码行。谢谢。

这是 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();
}
}

这是名为 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;
    }
}

处理草图(WaveFont.pde)是:

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();
  }
}

1 个答案:

答案 0 :(得分:0)

您的错误是抱怨代码使用java.awt.Point,这是有道理的,因为Android API没有实现该类。

您好像使用的是Java库,而不是Android库。 Android和Java 几乎相同,但Android不包括Swing或AWT之类的东西,这是java.awt.Point类的来源。

要解决此问题,您需要找到专门为Android构建的API版本,而不是普通Java。