Where people come together to learn, code, and play. Custom-built HTTP server, site generator, and website from scratch using no external libraries. Goal is to be as minimalistic and fun as possible. http://projectdivar.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
SigPlace/readers/fonts/Font.java

78 lines
2.8 KiB

package readers.fonts;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.awt.Color;
public class Font {
Glyph[] data = new Glyph[9];
final static int TRANSPARENT = new Color(0,0,0,0).getRGB();
public static Font LoadFont(String fontName) {
Path f = Paths.get("readers","fonts",fontName);
if (Files.exists(f)) {
//TODO Read the file.
return new Font();
}
return new Font();
}
public static void TrainFont(String fontName,String expectedGlyphs,BufferedImage data) {
Path f = Paths.get("readers","fonts",fontName);
Font font = LoadFont(fontName);
try {
Files.createFile(f);
int startX=-1;
int endX=-1;
outer:
for (int x=0;x<data.getWidth();x++) {
for (int y=0;y<data.getHeight();y++) {
if (data.getRGB(x, y)!=TRANSPARENT) {
if (startX==-1) {
startX=x;
}
continue outer;
}
}
//If we got to here it means we found transparent on everything.
if (startX!=-1) {
endX=x;
//Create a Glyph out of this.
Glyph g = new Glyph();
g.width=endX-startX;
g.height=data.getHeight();
int[] arr = data.getRGB(startX,0,g.width,g.height,null,0,g.width);
//Find the min and max Y.
int startY=g.height;
int endY=0;
for (int X=0;X<g.width;X++) {
for (int Y=0;Y<g.height;Y++) {
if (arr[Y*g.width+X]!=TRANSPARENT&&Y<startY) {
startY=Y;
}
if (arr[Y*g.width+X]!=TRANSPARENT&&Y>endY) {
endY=Y;
}
}
}
g.height=endY-startY;
g.data = new boolean[g.width*g.height];
for (int X=0;X<g.width;X++) {
for (int Y=0;Y<g.height;Y++) {
g.data[Y*g.width+X]=arr[(Y+startY)*g.width+X]!=TRANSPARENT;
}
}
int index = expectedGlyphs.charAt(0)-'0';
font.data[index]=g;
System.out.println("Glyph for number "+index+" has been saved.");
expectedGlyphs=expectedGlyphs.substring(1);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}