|
@@ -24,117 +24,99 @@ public abstract class FontCreator {
|
|
|
public FontCreator() {
|
|
|
}
|
|
|
|
|
|
- public static BufferedImage buildFont(String fontName) {
|
|
|
+ public static AngelFont buildFont(String fontName) {
|
|
|
return buildFont(fontName, 512);
|
|
|
}
|
|
|
|
|
|
- public static BufferedImage buildFont(String fontName, int bitmapSize) {
|
|
|
- return buildFont(fontName, bitmapSize, 63);
|
|
|
+ public static AngelFont buildFont(String fontName, int bitmapSize) {
|
|
|
+ return buildFont(fontName, bitmapSize, 16, false);
|
|
|
}
|
|
|
|
|
|
- public static BufferedImage buildFont(String fontName, int bitmapSize, int offset) {
|
|
|
+ public static AngelFont buildFont(String fontName, int bitmapSize, int fontSize) {
|
|
|
+ return buildFont(fontName, bitmapSize, fontSize, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static AngelFont buildFont(String fontName, int bitmapSize, int fontSize, boolean debug) {
|
|
|
BufferedImage fontImage;
|
|
|
Font font;
|
|
|
|
|
|
- boolean sizeFound = false;
|
|
|
- boolean directionSet = false;
|
|
|
- int delta = 0;
|
|
|
- int fontSize = 24;
|
|
|
-
|
|
|
String charLocs = "";
|
|
|
|
|
|
- /*
|
|
|
- * To find out how much space a Font takes, you need to use a the
|
|
|
- * FontMetrics class. To get the FontMetrics, you need to get it from a
|
|
|
- * Graphics context. A Graphics context is only available from a
|
|
|
- * displayable surface, ie any class that subclasses Component or any
|
|
|
- * Image. First the font is set on a Graphics object. Then get the
|
|
|
- * FontMetrics and find out the width and height of the widest character
|
|
|
- * (W). Then take the largest of the 2 values and find the maximum size
|
|
|
- * font that will fit in the size allocated.
|
|
|
- */
|
|
|
- while (!sizeFound) {
|
|
|
- font = new Font(fontName, Font.PLAIN, fontSize); // Font Name
|
|
|
- // use BufferedImage.TYPE_4BYTE_ABGR to allow alpha blending
|
|
|
- fontImage = new BufferedImage(bitmapSize, bitmapSize,
|
|
|
- BufferedImage.TYPE_4BYTE_ABGR);
|
|
|
- Graphics2D g = (Graphics2D) fontImage.getGraphics();
|
|
|
- g.setFont(font);
|
|
|
- FontMetrics fm = g.getFontMetrics();
|
|
|
- int width = fm.stringWidth("W");
|
|
|
- int height = fm.getHeight();
|
|
|
- int lineWidth = (width > height) ? width * 16 : height * 16;
|
|
|
- if (!directionSet) {
|
|
|
- if (lineWidth > bitmapSize) {
|
|
|
- delta = -2;
|
|
|
- } else {
|
|
|
- delta = 2;
|
|
|
- }
|
|
|
- directionSet = true;
|
|
|
- }
|
|
|
- if (delta > 0) {
|
|
|
- if (lineWidth < bitmapSize) {
|
|
|
- fontSize += delta;
|
|
|
- } else {
|
|
|
- sizeFound = true;
|
|
|
- fontSize -= delta;
|
|
|
- }
|
|
|
- } else if (delta < 0) {
|
|
|
- if (lineWidth > bitmapSize) {
|
|
|
- fontSize += delta;
|
|
|
- } else {
|
|
|
- sizeFound = true;
|
|
|
- fontSize -= delta;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /*
|
|
|
- * Now that a font size has been determined, create the final image, set
|
|
|
- * the font and draw the standard/extended ASCII character set for that
|
|
|
- * font.
|
|
|
- */
|
|
|
font = new Font(fontName, Font.BOLD, fontSize); // Font Name
|
|
|
// use BufferedImage.TYPE_4BYTE_ABGR to allow alpha blending
|
|
|
fontImage = new BufferedImage(bitmapSize, bitmapSize,
|
|
|
BufferedImage.TYPE_4BYTE_ABGR);
|
|
|
Graphics2D g = (Graphics2D) fontImage.getGraphics();
|
|
|
g.setFont(font);
|
|
|
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
|
- RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
+ if (!debug) {
|
|
|
+ g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
|
+ RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
+ }
|
|
|
g.setColor(OPAQUE_WHITE);
|
|
|
g.setBackground(TRANSPARENT_BLACK);
|
|
|
|
|
|
FontMetrics fm = g.getFontMetrics();
|
|
|
|
|
|
+ if (debug) {
|
|
|
+ g.setColor(Color.WHITE);
|
|
|
+ g.drawRect(0, 0, bitmapSize - 1, bitmapSize - 1);
|
|
|
+ }
|
|
|
+ int xPos = 0;
|
|
|
+ int yPos = 0;
|
|
|
+ int height = 0;
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
- int x = i % 16;
|
|
|
- int y = i / 16;
|
|
|
char ch[] = {(char) i};
|
|
|
String temp = new String(ch);
|
|
|
Rectangle2D bounds = fm.getStringBounds(temp, g);
|
|
|
-
|
|
|
- int xPos = (int) ((x * 32) + (16 - (bounds.getWidth() / 2)));
|
|
|
- int yPos = (y * 32) + fm.getAscent() - offset;
|
|
|
+ height = fm.getDescent() + fm.getAscent();
|
|
|
+ if (yPos == 0) {
|
|
|
+ yPos = height;
|
|
|
+ }
|
|
|
+ if (xPos + bounds.getWidth() > bitmapSize) {
|
|
|
+ xPos = 0;
|
|
|
+ yPos += height;
|
|
|
+ }
|
|
|
g.drawString(temp, xPos, yPos);
|
|
|
- //TODO: AngelFont support!
|
|
|
-// g.setColor(Color.BLUE);
|
|
|
-// g.drawRect(xPos, yPos-(int)bounds.getHeight(), (int)bounds.getWidth(), (int)bounds.getHeight()+fm.getDescent());
|
|
|
-// g.setColor(Color.WHITE);
|
|
|
-//
|
|
|
-// charLocs=charLocs+
|
|
|
-// "char id="+i+
|
|
|
-// " x="+xPos +
|
|
|
-// " y="+(yPos-(int)bounds.getHeight()-fm.getAscent())+
|
|
|
-// " width="+(int)bounds.getWidth()+
|
|
|
-// " height="+(int)bounds.getHeight()+
|
|
|
-// " xoffset=0" +
|
|
|
-// " yoffset=0" +
|
|
|
-// " xadvance=0" +
|
|
|
-// " page=0" +
|
|
|
-// " chnl=15\n";
|
|
|
+ if (debug) {
|
|
|
+ g.setColor(Color.BLUE);
|
|
|
+ g.drawRect(xPos, yPos - fm.getAscent(), (int) bounds.getWidth(), height);
|
|
|
+ g.setColor(Color.WHITE);
|
|
|
+ }
|
|
|
+ charLocs = charLocs
|
|
|
+ + "char id=" + i
|
|
|
+ + " x=" + xPos
|
|
|
+ + " y=" + (yPos - fm.getAscent())
|
|
|
+ + " width=" + (int) bounds.getWidth()
|
|
|
+ + " height=" + (int) bounds.getHeight()
|
|
|
+ + " xoffset=0"
|
|
|
+ + " yoffset=0"
|
|
|
+ + " xadvance=" + ((int) bounds.getWidth() - 1) + " "
|
|
|
+ + " page=0"
|
|
|
+ + " chnl=0\n";
|
|
|
+ xPos += bounds.getWidth();
|
|
|
}
|
|
|
-// System.out.println(charLocs);
|
|
|
- return fontImage;
|
|
|
+ charLocs = "info face=null "
|
|
|
+ + "size=" + fontSize + " "
|
|
|
+ + "bold=0 "
|
|
|
+ + "italic=0 "
|
|
|
+ + "charset=ASCII "
|
|
|
+ + "unicode=0 "
|
|
|
+ + "stretchH=100 "
|
|
|
+ + "smooth=1 "
|
|
|
+ + "aa=1 "
|
|
|
+ + "paiing=0,0,0,0 "
|
|
|
+ + "spacing=1,1 "
|
|
|
+ + "\n"
|
|
|
+ + "common lineHeight=" + height + " "
|
|
|
+ + "base=26 "
|
|
|
+ + "scaleW=" + bitmapSize + " "
|
|
|
+ + "scaleH=" + bitmapSize + " "
|
|
|
+ + "pages=1 "
|
|
|
+ + "packed=0 "
|
|
|
+ + "\n"
|
|
|
+ + "page id=0 file=\"" + fontName.replaceAll(" ", "") + ".png\"\n"
|
|
|
+ + "chars count=255\n"
|
|
|
+ + charLocs;
|
|
|
+ return new AngelFont(fontImage, charLocs);
|
|
|
}
|
|
|
}
|