-
Notifications
You must be signed in to change notification settings - Fork 1
/
RetroFont.pde
103 lines (80 loc) · 2.41 KB
/
RetroFont.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
*/
public class RetroFont{
private PImage chars[];
private PImage trimmedChars[];
private int glyphWidth;
private int glyphHeight;
/*
Removes the transparent pixels from the left and right sides of
the glyph.
*/
private PImage truncateImage(PImage glyph){
int startX = 0;
int endX = glyph.width - 1;
int x, y;
// Find the starting X coord of the image.
for(x = glyph.width; x >= 0 ; x--){
for(y = 0; y < glyph.height; y++){
color testColor = glyph.get(x, y);
if( alpha(testColor) > 0.0){
startX = x;
}
}
}
// Find the ending coord
for(x = 0; x < glyph.width; x++){
for(y = 0; y < glyph.height; y++){
color testColor = glyph.get(x,y);
if( alpha(testColor) > 0.0){
endX = x;
}
}
}
return glyph.get(startX, 0, endX - startX + 1, glyph.height);
}
// Do not instantiate directly
public RetroFont(String imageFilename, int glyphWidth, int glyphHeight, int borderSize){
this.glyphWidth = glyphWidth;
this.glyphHeight = glyphHeight;
PImage fontSheet = loadImage(imageFilename);
chars = new PImage[96];
trimmedChars = new PImage[96];
int x = 0;
int y = 0;
//
//
for(int currChar = 0; currChar < 96; currChar++){
chars[currChar] = fontSheet.get(x, y, glyphWidth, glyphHeight);
trimmedChars[currChar] = truncateImage(fontSheet.get(x, y, glyphWidth, glyphHeight));
x += glyphWidth + borderSize;
if(x >= fontSheet.width){
x = 0;
y += glyphHeight + borderSize;
}
}
// For each character, truncate the x margin
//for(int currChar = 0; currChar < 96; currChar++){
//chars[currChar] = truncateImage( chars[currChar] );
//}
}
//public static void create(String imageFilename, int charWidth, int charHeight, int borderSize){
//PImage fontSheet = loadImage(imageFilename);
public PImage getGlyph(char ch){
int asciiCode = Utils.charCodeAt(ch);
if(asciiCode-32 >= 96 || asciiCode-32 <= 0){
return chars[0];
}
return chars[asciiCode-32];
}
public PImage getTrimmedGlyph(char ch){
int asciiCode = Utils.charCodeAt(ch);
return trimmedChars[asciiCode-32];
}
public int getGlyphWidth(){
return glyphWidth;
}
public int getGlyphHeight(){
return glyphHeight;
}
}