package org.rsg.interfascia_v2;
import java.util.ArrayList;
import java.util.HashMap;
import processing.core.PApplet;
import processing.core.PFont;
public class Font {
//font filenames should be like this: "JCkg-24.vlw"
//build them using the Processing tool. we need all sizes in the data folder
public static final String PATH = "fonts/";
public static final String[] FACES = new String[] {"JCkg", "GillSans"};
public static final int[] SIZES = new int[] {10, 12, 14, 18, 24, 48};
public static final int H1 = 48;
public static final int H2 = 24;
public static final int H3 = 18;
public static final int NORMAL = 14;
public static final int SMALL = 12;
public static final int TINY = 10;
public final static int LEADING = Constants.LEADING;
public static final String SANS_SERIF = FACES[1];
public static final String SERIF = FACES[0];
public static final String DEFAULT_FACE = FACES[1];
public static final int DEFAULT_SIZE = SIZES[2];
private static String face = DEFAULT_FACE;
private static int size = DEFAULT_SIZE;
private static final String SUFFIX = ".vlw";
public static HashMap<String, ArrayList> library = new HashMap<String, ArrayList>();
private static boolean isInitialized = false;
//PRIVATE
private static void grow2size(ArrayList<PFont> a, int newsize) {
while(a.size() <= newsize) { a.add(null); } //just fill intervening font sizes w/ null
}
//PUBLIC
public static void init(PApplet papplet) {
System.out.println("[Font] init");
for (int n = 0; n < FACES.length; n++) { //add all font names
ArrayList<PFont> suitcase = new ArrayList<PFont>();
for (int s = 0; s < SIZES.length; s++) { //add all sizes for each font
PFont pfont = papplet.loadFont(PATH + FACES[n] + "-" + SIZES[s] + SUFFIX);
grow2size(suitcase, SIZES[s]);
suitcase.set(SIZES[s], pfont);
}
library.put(FACES[n], suitcase);
}
isInitialized = true;
setToDefault(papplet);
}
public static void setToDefault(PApplet papplet) {
setFont(papplet, DEFAULT_FACE, DEFAULT_SIZE);
}
private static void setFont(PApplet papplet) {
setFont(papplet, face, size);
}
public static void setFont(PApplet papplet, String face, int size) {
if(!isInitialized) { init(papplet); }
try {
PFont pfont = (PFont) library.get(face).get(size); // pfonts.get(i);
papplet.textFont(pfont);
Font.face = face;
Font.size = size;
} catch(Exception e) {
System.err.println("[Font] setToSize("+papplet+", "+size+") Exception: " + e.toString());
Font.face = Font.DEFAULT_FACE;
Font.size = Font.DEFAULT_SIZE;
}
}
public static String getFace() { return face; }
public static int getSize() { return size; }
public static void setFace(PApplet papplet, String face) {
Font.face = face;
setFont(papplet);
}
public static void setSize(PApplet papplet, int size) {
Font.size = size;
setFont(papplet);
}
}