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.
238 lines
4.6 KiB
238 lines
4.6 KiB
10 years ago
|
package mightypork.gamecore.resources.fonts.impl;
|
||
10 years ago
|
|
||
|
|
||
|
import java.awt.Font;
|
||
|
import java.awt.FontFormatException;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
|
||
10 years ago
|
import mightypork.gamecore.logging.LogAlias;
|
||
10 years ago
|
import mightypork.gamecore.resources.BaseLazyResource;
|
||
|
import mightypork.gamecore.resources.TextureBasedResource;
|
||
10 years ago
|
import mightypork.gamecore.resources.fonts.GLFont;
|
||
|
import mightypork.gamecore.resources.textures.FilterMode;
|
||
|
import mightypork.gamecore.util.files.FileUtils;
|
||
|
import mightypork.gamecore.util.math.color.Color;
|
||
|
import mightypork.gamecore.util.math.constraints.vect.Vect;
|
||
10 years ago
|
|
||
|
|
||
|
/**
|
||
|
* Font obtained from a resource file (TTF).
|
||
|
*
|
||
|
* @author MightyPork
|
||
|
*/
|
||
10 years ago
|
@TextureBasedResource
|
||
10 years ago
|
@LogAlias(name = "Font")
|
||
10 years ago
|
public class LazyFont extends BaseLazyResource implements GLFont {
|
||
10 years ago
|
|
||
|
public static enum FontStyle
|
||
|
{
|
||
|
PLAIN(Font.PLAIN), BOLD(Font.BOLD), ITALIC(Font.ITALIC), BOLD_ITALIC(Font.BOLD + Font.ITALIC);
|
||
|
|
||
10 years ago
|
int numval;
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
private FontStyle(int style)
|
||
|
{
|
||
10 years ago
|
this.numval = style;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
private GLFont font = null;
|
||
|
private double size;
|
||
|
private FontStyle style;
|
||
|
private String chars;
|
||
10 years ago
|
private FilterMode filter;
|
||
10 years ago
|
private boolean antialias;
|
||
10 years ago
|
private double discardTop;
|
||
|
private double discardBottom;
|
||
10 years ago
|
|
||
|
|
||
|
/**
|
||
10 years ago
|
* A font from resource; setters shall be used to specify parameters in
|
||
|
* greater detail.
|
||
10 years ago
|
*
|
||
|
* @param resourcePath resource to load
|
||
10 years ago
|
* @param chars chars to load; null to load basic chars only
|
||
10 years ago
|
* @param size size (px)
|
||
10 years ago
|
*/
|
||
10 years ago
|
public LazyFont(String resourcePath, String chars, double size)
|
||
10 years ago
|
{
|
||
10 years ago
|
this(resourcePath, chars, size, FontStyle.PLAIN, false, FilterMode.NEAREST);
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* A font from resource
|
||
|
*
|
||
|
* @param resourcePath resource to load
|
||
10 years ago
|
* @param chars chars to load; null to load basic chars only
|
||
|
* @param size size (px)
|
||
10 years ago
|
* @param style font style
|
||
10 years ago
|
* @param antialias use antialiasing for caching texture
|
||
|
* @param filter gl filtering mode
|
||
10 years ago
|
*/
|
||
10 years ago
|
public LazyFont(String resourcePath, String chars, double size, FontStyle style, boolean antialias, FilterMode filter)
|
||
10 years ago
|
{
|
||
10 years ago
|
super(resourcePath);
|
||
|
this.size = size;
|
||
|
this.style = style;
|
||
|
this.chars = chars;
|
||
|
this.filter = filter;
|
||
|
this.antialias = antialias;
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
10 years ago
|
public synchronized void setSize(double size)
|
||
10 years ago
|
{
|
||
10 years ago
|
this.size = size;
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
10 years ago
|
public synchronized void setStyle(FontStyle style)
|
||
10 years ago
|
{
|
||
10 years ago
|
this.style = style;
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
10 years ago
|
public synchronized void setChars(String chars)
|
||
10 years ago
|
{
|
||
|
this.chars = chars;
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
public synchronized void setFilter(FilterMode filter)
|
||
10 years ago
|
{
|
||
10 years ago
|
this.filter = filter;
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
10 years ago
|
public synchronized void setAntialias(boolean antialias)
|
||
10 years ago
|
{
|
||
|
this.antialias = antialias;
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
@Override
|
||
10 years ago
|
protected synchronized final void loadResource(String path) throws IOException
|
||
10 years ago
|
{
|
||
10 years ago
|
final Font awtFont = getAwtFont(path, (float) size, style.numval);
|
||
10 years ago
|
|
||
10 years ago
|
font = new TextureBackedFont(awtFont, antialias, filter, chars);
|
||
10 years ago
|
font.setDiscardRatio(discardTop, discardBottom);
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Get a font for a resource path / name
|
||
|
*
|
||
|
* @param resource resource to load
|
||
|
* @param size font size (pt)
|
||
|
* @param style font style
|
||
|
* @return the {@link Font}
|
||
|
* @throws IOException
|
||
|
*/
|
||
10 years ago
|
protected Font getAwtFont(String resource, float size, int style) throws IOException
|
||
10 years ago
|
{
|
||
10 years ago
|
try(InputStream in = FileUtils.getResource(resource)) {
|
||
10 years ago
|
|
||
|
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, in);
|
||
|
|
||
|
awtFont = awtFont.deriveFont(size);
|
||
|
awtFont = awtFont.deriveFont(style);
|
||
|
|
||
|
return awtFont;
|
||
10 years ago
|
} catch (final FontFormatException e) {
|
||
|
throw new IOException("Could not load font, bad format.", e);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Draw string
|
||
|
*
|
||
|
* @param str string to draw
|
||
|
* @param color draw color
|
||
|
*/
|
||
|
@Override
|
||
10 years ago
|
public void draw(String str, Color color)
|
||
10 years ago
|
{
|
||
|
if (!ensureLoaded()) return;
|
||
|
|
||
|
font.draw(str, color);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Get size needed to render give string
|
||
|
*
|
||
|
* @param text string to check
|
||
|
* @return coord (width, height)
|
||
|
*/
|
||
|
@Override
|
||
10 years ago
|
public Vect getNeededSpace(String text)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (!ensureLoaded()) return Vect.ZERO;
|
||
10 years ago
|
|
||
|
return font.getNeededSpace(text);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @return font height
|
||
|
*/
|
||
|
@Override
|
||
10 years ago
|
public int getLineHeight()
|
||
10 years ago
|
{
|
||
|
if (!ensureLoaded()) return 0;
|
||
|
|
||
10 years ago
|
return font.getLineHeight();
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
|
||
10 years ago
|
@Override
|
||
10 years ago
|
public int getFontSize()
|
||
10 years ago
|
{
|
||
|
if (!ensureLoaded()) return 0;
|
||
|
|
||
10 years ago
|
return font.getFontSize();
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public int getWidth(String text)
|
||
|
{
|
||
|
return font.getWidth(text);
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public void destroy()
|
||
|
{
|
||
|
// this will have to suffice
|
||
|
font = null;
|
||
|
}
|
||
10 years ago
|
|
||
|
|
||
|
@Override
|
||
|
public void setDiscardRatio(double top, double bottom)
|
||
|
{
|
||
|
discardTop = top;
|
||
|
discardBottom = bottom;
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public double getTopDiscardRatio()
|
||
|
{
|
||
|
return discardTop;
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public double getBottomDiscardRatio()
|
||
|
{
|
||
|
return discardBottom;
|
||
|
}
|
||
10 years ago
|
}
|