forked from genail/pulpcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreFont.java
More file actions
383 lines (327 loc) · 13.2 KB
/
CoreFont.java
File metadata and controls
383 lines (327 loc) · 13.2 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
Copyright (c) 2008, Interactive Pulp, LLC
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Interactive Pulp, LLC nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package pulpcore.image;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import pulpcore.Build;
import pulpcore.CoreSystem;
import pulpcore.util.ByteArray;
/**
The CoreFont class is a image-based font used for drawing text.
*/
public class CoreFont {
private static final long MAGIC = 0x70756c70666e740bL; // "pulpfnt" 0.11
// HashMap<String, WeakReference<CoreFont>>
private static HashMap loadedFonts = new HashMap();
private static CoreFont systemFont;
private CoreImage image;
private char firstChar;
private char lastChar;
/* package-private */ int[] charPositions;
private boolean uppercaseOnly;
/** Extra horizontal pixel space between chars. Can be negative. */
private int tracking;
private int[] bearingLeft;
private int[] bearingRight;
// HashMap<Integer, SoftReference<CoreFont>>
private HashMap tintedFonts;
public static CoreFont getSystemFont() {
if (systemFont == null) {
systemFont = load("system.font.png");
}
return systemFont;
}
/**
Loads a PNG-based image font from the asset catalog.
<p>
This method never returns {@code null}.
If the image is not found, or the PNG image does not have font information,
an error is printed to the log
and the system font is returned. If the system font cannot be found, an Error is thrown.
<p>
PNG-based image fonts are created from a font.properties file. See the Text example
in the distribution archive.
<p>
Fonts are internally cached (using a WeakReference), and if the font was previously
loaded, this method may return the same reference.
@param fontAsset The name of a PNG image file with font information.
@return The font, or a broken image if the font cannot be found.
*/
public static CoreFont load(String fontAsset) {
// Attempt to load from the cache
WeakReference fontRef = (WeakReference)loadedFonts.get(fontAsset);
if (fontRef != null) {
CoreFont font = (CoreFont)fontRef.get();
if (font != null) {
return font;
}
else {
loadedFonts.remove(fontAsset);
}
}
CoreFont font = new CoreFont();
CoreImage.load(fontAsset, font);
if (font.image != null) {
loadedFonts.put(fontAsset, new WeakReference(font));
return font;
}
else if ("system.font.png".equals(fontAsset)) {
if (Build.DEBUG) CoreSystem.print("Can't load system.font.png. Quitting");
throw new Error();
}
else {
return getSystemFont();
}
}
private CoreFont() { }
private CoreFont(CoreFont font) {
this.image = font.image;
this.firstChar = font.firstChar;
this.lastChar = font.lastChar;
this.charPositions = font.charPositions;
this.bearingLeft = font.bearingLeft;
this.bearingRight = font.bearingRight;
this.uppercaseOnly = font.uppercaseOnly;
this.tracking = font.tracking;
}
// Callback from CoreImage.load()
void set(CoreImage image, ByteArray in) throws IOException {
long magic = in.readLong();
if (magic != MAGIC) {
if (Build.DEBUG) {
CoreSystem.print("CoreFont.set() - Bad magic number (try recompiling game assets)");
}
throw new IOException();
}
firstChar = (char)in.readShort();
lastChar = (char)in.readShort();
tracking = in.readShort();
boolean hasBearing = in.readBoolean();
int numChars = lastChar - firstChar + 1;
charPositions = new int[numChars + 1];
bearingLeft = new int[numChars];
bearingRight = new int[numChars];
for (int i=0; i<charPositions.length; i++) {
charPositions[i] = in.readShort() & 0xffff;
}
if (hasBearing) {
for (int i = 0; i < bearingLeft.length; i++) {
bearingLeft[i] = in.readShort();
bearingRight[i] = in.readShort();
}
}
uppercaseOnly = (lastChar < 'a');
this.image = image;
}
public boolean canDisplay(char ch) {
if (uppercaseOnly && ch >= 'a' && ch <= 'z') {
ch += 'A' - 'a';
}
if (ch < firstChar || ch > lastChar) {
return false;
}
int index = ch - firstChar;
int charWidth = charPositions[index+1] - charPositions[index];
return (charWidth > 0);
}
/**
Gets the total width of all the characters in a string. Tracking between
characters is included.
If a character isn't valid for this font, the last character in the set is used.
<p>
Equivalent to calling getStringWidth(s, 0, s.length())
*/
public int getStringWidth(String s) {
return getStringWidth(s, 0, s.length());
}
/**
Gets the total width of a range of characters in a string. Tracking
and kerning between characters is included.
If a character isn't valid for this font, the last character in the set is used.
@param beginIndex the beginning index, inclusive.
@param endIndex the ending index, exclusive.
*/
public int getStringWidth(String s, int beginIndex, int endIndex) {
if (endIndex <= beginIndex) {
return 0;
}
int stringWidth = 0;
int lastIndex = -1;
for (int i = beginIndex; i < endIndex; i++) {
int index = getCharIndex(s.charAt(i));
int charWidth = charPositions[index+1] - charPositions[index];
if (lastIndex != -1) {
stringWidth += getKerning(lastIndex, index);
}
stringWidth += charWidth;
lastIndex = index;
}
return stringWidth;
}
public int getCharPosition(String s, int beginIndex, int charIndex) {
if (charIndex <= beginIndex) {
return 0;
}
return getStringWidth(s, beginIndex, charIndex + 1) - getCharWidth(s.charAt(charIndex));
}
/* package-private */ int getCharIndex(char ch) {
if (uppercaseOnly && ch >= 'a' && ch <= 'z') {
ch += 'A' - 'a';
}
if (ch < firstChar || ch > lastChar) {
ch = lastChar;
}
return ch - firstChar;
}
/**
Gets the width of the specified character. Tracking and kerning is not included.
If a character isn't valid for this font, the last character in the set is used.
*/
public int getCharWidth(char ch) {
int index = getCharIndex(ch);
return (charPositions[index+1] - charPositions[index]);
}
public int getKerning(char left, char right) {
return getKerning(getCharIndex(left), getCharIndex(right));
}
/* package-private */ int getKerning(int leftIndex, int rightIndex) {
// Future versions of this method might handle kerning pairs, like "WA" and "Yo"
if (tracking != 0 && (shouldIgnoreTracking(rightIndex) ||
shouldIgnoreTracking(leftIndex)))
{
return bearingRight[leftIndex] + bearingLeft[rightIndex];
}
else {
return bearingRight[leftIndex] + tracking + bearingLeft[rightIndex];
}
}
public int getHeight() {
return image.getHeight();
}
public CoreImage getImage() {
return image;
}
private boolean shouldIgnoreTracking(int index) {
int width = (charPositions[index+1] - charPositions[index]);
int lsb = bearingLeft[index];
int rsb = bearingRight[index];
int advance = width + lsb + rsb;
return advance < width/2;
}
//
//
//
/**
Returns a new CoreFont with every pixel set to the specified color,
without changing the alpha of each pixel.
<p>
Tinted fonts are internally cached using SoftReferences.
*/
public CoreFont tint(int rgbColor) {
Integer color = new Integer(rgbColor);
if (tintedFonts == null) {
tintedFonts = new HashMap();
}
else {
// Attempt to load from the cache
SoftReference fontRef = (SoftReference)tintedFonts.get(color);
if (fontRef != null) {
CoreFont font = (CoreFont)fontRef.get();
if (font != null) {
return font;
}
else {
tintedFonts.remove(color);
}
}
}
// Create a new tinted font
CoreFont tintedFont = new CoreFont(this);
tintedFont.image = image.tint(rgbColor);
tintedFonts.put(color, new SoftReference(tintedFont));
return tintedFont;
}
/**
@deprecated Background colors do not appear correctly
*/
public CoreFont background(int argbColor) {
CoreFont newFont = new CoreFont(this);
newFont.image = image.background(argbColor);
return newFont;
}
/**
@deprecated Fade dynamically using a Label instead
*/
public CoreFont fade(int alpha) {
CoreFont fadedFont = new CoreFont(this);
fadedFont.image = image.fade(alpha);
return fadedFont;
}
/**
Creates a scaled instance of this font.
@deprecated Scale dynamically using a Label instead
*/
public CoreFont scale(double scale) {
int numChars = lastChar - firstChar + 1;
// Determine new char positions
int[] scaledCharPositions = new int[charPositions.length];
int position = 0;
for (int i = 0; i < numChars; i++) {
scaledCharPositions[i] = position;
int charWidth = charPositions[i+1] - charPositions[i];
int scaledWidth = (int)Math.round(charWidth * scale);
position += scaledWidth;
}
scaledCharPositions[numChars] = position;
// Scale each character image
CoreImage scaledImage = new CoreImage(position,
(int)Math.round(getHeight() * scale), false);
CoreGraphics g = scaledImage.createGraphics();
for (int i = 0; i < numChars; i++) {
int oldWidth = charPositions[i+1] - charPositions[i];
int newWidth = scaledCharPositions[i+1] - scaledCharPositions[i];
g.drawScaledImage(image, scaledCharPositions[i], 0, newWidth, scaledImage.getHeight(),
charPositions[i], 0, oldWidth, getHeight());
}
// Scale the bearing
int[] scaledBearingLeft = new int[bearingLeft.length];
int[] scaledBearingRight = new int[bearingRight.length];
for (int i = 0; i < bearingLeft.length; i++) {
scaledBearingLeft[i] = (int)Math.round(bearingLeft[i] * scale);
scaledBearingRight[i] = (int)Math.round(bearingRight[i] * scale);
}
// Create the new font
CoreFont scaledFont = new CoreFont(this);
scaledFont.charPositions = scaledCharPositions;
scaledFont.bearingLeft = scaledBearingLeft;
scaledFont.bearingRight = scaledBearingRight;
scaledFont.image = scaledImage;
scaledFont.tracking = (int)Math.round(tracking * scale);
return scaledFont;
}
}