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.
59 lines
1.1 KiB
59 lines
1.1 KiB
11 years ago
|
package mightypork.rogue.fonts;
|
||
|
|
||
|
|
||
|
import mightypork.rogue.render.Render;
|
||
|
import mightypork.utils.math.color.RGB;
|
||
|
import mightypork.utils.math.coord.Coord;
|
||
|
import mightypork.utils.math.coord.Rect;
|
||
|
|
||
|
|
||
|
public class FontRenderer {
|
||
|
|
||
|
private final GLFont font;
|
||
|
|
||
|
|
||
|
public FontRenderer(GLFont font) {
|
||
|
|
||
|
if (font == null) throw new NullPointerException("Font cannot be null.");
|
||
|
|
||
|
this.font = font;
|
||
|
}
|
||
|
|
||
|
|
||
|
public void draw(String text, Coord pos, double height, RGB color)
|
||
|
{
|
||
|
Render.pushState();
|
||
|
|
||
|
Render.translate(pos.round());
|
||
|
Render.scaleXY(getScale(height));
|
||
|
|
||
|
font.draw(text, color);
|
||
|
|
||
|
Render.popState();
|
||
|
}
|
||
|
|
||
|
|
||
|
public Coord getNeededSpace(String text, double height)
|
||
|
{
|
||
|
return font.getNeededSpace(text).mul(getScale(height));
|
||
|
}
|
||
|
|
||
|
|
||
|
public double getWidth(String text, double height)
|
||
|
{
|
||
|
return getNeededSpace(text, height).x;
|
||
|
}
|
||
|
|
||
|
|
||
|
public Rect getBounds(String text, Coord pos, double height)
|
||
|
{
|
||
|
return Rect.fromSize(getNeededSpace(text, height)).add(pos);
|
||
|
}
|
||
|
|
||
|
|
||
|
private double getScale(double height)
|
||
|
{
|
||
|
return height / font.getHeight();
|
||
|
}
|
||
|
}
|