-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFontGenerator.java
138 lines (106 loc) · 4.32 KB
/
FontGenerator.java
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package xyz.theasylum.zendarva.rle;
import lombok.Getter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import xyz.theasylum.zendarva.rle.exception.MissingFont;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.Font;
import java.awt.font.LineMetrics;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class FontGenerator {
public static final int TILES_PER = 200;
BufferedImage junkImage;
@Getter BufferedImage finalImage;
@Getter Map<Character, Rectangle> fontMap;
@Getter int charWidth;
@Getter int charHeight;
private static final Logger LOG = LogManager.getLogger(FontGenerator.class);
public FontGenerator(File file, float size) throws MissingFont {
if (!file.exists()){
LOG.error("Attempting to load non-existant font: {}", file.getAbsolutePath());
throw new MissingFont();
}
Font fnt = null;
try {
fnt = Font.createFont(Font.TRUETYPE_FONT, file).deriveFont(size);
} catch (FontFormatException e) {
LOG.error("Error loading Font: {}",file.toPath());
throw new MissingFont(e);
} catch (IOException e) {
LOG.error("Error loading Font: {}",file.toPath());
throw new MissingFont(e);
}
junkImage = new BufferedImage(100,100,BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = junkImage.createGraphics();
g.setFont(fnt);
FontMetrics fm = g.getFontMetrics();
if (fm.charWidth('W')!= fm.charWidth('.')){
LOG.warn("Loading a non-monospaced font.");
}
drawFont(fnt, fm);
}
public FontGenerator(String resourcePath, float size) throws MissingFont {
InputStream stream = this.getClass().getClassLoader().getResourceAsStream(resourcePath);
if (stream == null){
LOG.error("Attempting to load a non-existant jarFont: {}",resourcePath);
throw new MissingFont();
}
Font fnt = null;
try {
fnt = Font.createFont(Font.TRUETYPE_FONT, stream).deriveFont(size);
} catch (FontFormatException e) {
LOG.error("Error loading jarFont: {}",resourcePath);
throw new MissingFont(e);
} catch (IOException e) {
LOG.error("Error loading jarFont: {}",resourcePath);
throw new MissingFont(e);
}
junkImage = new BufferedImage(100,100,BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = junkImage.createGraphics();
g.setFont(fnt);
FontMetrics fm = g.getFontMetrics();
if (fm.charWidth('W')!= fm.charWidth('.')){
LOG.warn("Loading a non-monospaced font.");
}
drawFont(fnt, fm);
}
private void drawFont(Font fnt, FontMetrics fm){
int width = fm.charWidth('A');
int height = fm.getHeight();
BufferedImage image = new BufferedImage(width* TILES_PER,height* TILES_PER,BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = (Graphics2D) image.getGraphics();
int cwidth = (int) fm.getMaxCharBounds(g).getWidth();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
g.setFont(fnt);
Point point = new Point(0,0);
fontMap= new HashMap<>();
g.setColor(Color.white);
for (int i = 0; i <TILES_PER*TILES_PER; i++) {
char c = (char) i;
if (fnt.canDisplay(i)){
g.drawString(Character.toString(c),point.x,point.y+fm.getAscent());
fontMap.put(c,new Rectangle(point.x,point.y,width,height));
point.x+=width;
}
if (point.x > width* TILES_PER){
point.x =0;
point.y+=height;
}
}
g.dispose();
finalImage = new BufferedImage(width* TILES_PER, point.y+height, BufferedImage.TYPE_4BYTE_ABGR);
g= (Graphics2D) finalImage.getGraphics();
g.drawImage(image,0,0,width* TILES_PER,point.y+height,0,0,width* TILES_PER,point.y+height,null);
g.dispose();
finalImage=Screen.makeCompatible(finalImage);
charWidth=width;
charHeight=height;
}
}