First, I loaded the skin as a json file. In the file, I put two BitmapFont defintions ("font" and "big-font") and left them practically blank (I did load a dummy ".fnt" file to prevent the parser from complaining). I then generated two FreeType Fonts using the following code:
//start font generator
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/button-font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter();
//default font (the sizes are arbitrary, since it did not work...)
parameters.size = 10;
BitmapFont defaultFont = generator.generateFont(parameters);
//big font
parameters.size = 100;
BitmapFont bigFont = generator.generateFont(parameters);
And set them as the skin fonts like so:
skin.add("font", defaultFont, BitmapFont.class);
skin.add("big-font", bigFont, BitmapFont.class);
The problem is that the gui (particularly the TextButton, which uses the "font" font) does not seem to care about what I did programmatically and keeps using the dummy font that I've set in the json file.
What am I doing wrong? Is there a better way to integrate ttf fonts and json skin parsing?
Thanks in advance for any help.
Follow these steps :
First, create your font from a TTF file
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/button-font.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 10;
BitmapFont defaultFont = generator.generateFont(parameter);
generator.dispose(); // don't forget to dispose to avoid memory leaks!
Create a blank skin using default constructor and add your font to the skin.
Skin skin = new Skin();
skin.add("font",defaultFont);
Add your atlas file to your skin.
skin.addRegion(new TextureAtlas(Gdx.files.internal("mySkin.atlas")));
Load your Json file to your skin.
skin.load(Gdx.files.internal("mySkin.json"));
then, in your JSON file you can reference “font”
{
font: font
}
Related
When creating a BitmapFont in libGDX from a .ttf file with FreeTypeFontGenerator the resulting font has serious artifacts around it:
Those artifacts only appear when using mipmapping, so does it have something to do with transparent pixels? I have tried adding padding to the resulting font, but it does not work. I also tried using linear filtering but the resulting font looks pixelated.
Code I am using to generate the fonts:
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 300;
parameter.color = Color.WHITE;
parameter.characters = "0123456789";
parameter.kerning = true;
parameter.magFilter = Texture.TextureFilter.MipMapLinearLinear;
parameter.minFilter = Texture.TextureFilter.MipMapLinearLinear;
parameter.genMipMaps = true;
font = generator.generateFont(parameter);
I am using a TextButton to create a dialog bubble effect in my game.
I have created the nine patch file as on the picture
everything seems to be fine until I try to use this bubble.9 with LibGDX, the TextButton is not stretching correctly based on the text height.
You can see that a multilines text extends the TextButton height but not correctly.
Here is the code
Load the bitmap font
FileHandleResolver resolver = new InternalFileHandleResolver();
assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
FreetypeFontLoader.FreeTypeFontLoaderParameter dialogFont = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
dialogFont.fontFileName = Conf.ASSET_FONT;
dialogFont.fontParameters.size = 8;
Initialize the TextButtonStyle
NinePatch patch = atlas.createPatch("bubble");
NinePatchDrawable dialogBoxPatch = new NinePatchDrawable(patch);
buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.up = dialogBoxPatch;
buttonStyle.down = dialogBoxPatch;
buttonStyle.checked = dialogBoxPatch;
buttonStyle.over = dialogBoxPatch;
buttonStyle.font = dialogFont;
buttonStyle.fontColor = Color.BLACK;
actual rendering
dialogBox = new TextButton("some\ntext", buttonStyle);
dialogBox.draw(batch, 1);
Does someone see what the problem is?
I finally found the problem.
I have decided to test the ninepatch with another font and it worked like a charm. To understand what was going on, I have opened the two fonts with glyphrstudio. It appears that the problematic font is not correctly designed.
Bad font
Good font
You can see that the base line and capheight are not correctly setted on the bad font.
I hope this will help other people facing the same problem.
Hi I'm making a asteroids clone using java and libGDX and have had some trouble trying to load in a ttf font and simply displaying it and i have no idea what I'm doing wrong I've looked at many sites but they all have outdated information (I'm using the latest version of freetype).
Here is code:
SpriteBatch sb;
BitmapFont font;
sb = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("fonts/Hyperspace_Bold.ttf"), true);
sb.setColor(1,1,1,1);
sb.begin();
font.draw(sb, Long.toString(player.getScore()), 40, 390);
sb.end();
I've created a folder in my assets folder called fonts fyi.
P.S. players score is just 0
EDIT: the sb.setColor and everything preceding is in its own render method but i just put it here for simplicity purposes. The purpose of this post is that I'm not sure HOW to use a ttf font type and display it in the latest version of libGDX.
Ok i figured it out. Finally! I needed to use a freeTypeFontGenerator like this:
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/Hyperspace_Bold.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 12;
BitmapFont font = generator.generateFont(parameter);
generator.dispose();
then just this in the draw/render method!:
sb.begin();
font.draw(sb, Long.toString(player.getScore()), 40, 390);
sb.end();
Using AWT graphics, I am attempting to draw text with a .ttf font. But when drawn on the screen it only displays text in what looks to be font size 1.
Font initialization code:
font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf"));
font.deriveFont(24F);
fontSmall = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf"));
fontSmall.deriveFont(16F);
And yes, those statements are inside of a try statement.
And the code that is supposed to correctly draw text with the font:
g.setFont(font);
g.setColor(RED);
g.drawString("Test123",10,10);
g.setFont(fontSmall);
g.drawString("Test123SMALL",10,10);
Thanks in advance :)
Try with:
font = font.deriveFont(24F);
And:
fontSmall = fontSmall.deriveFont(16F);
font = font.deriveFont(24F);
This should work, but when you try:
font = font.deriveFont(24);
The font looks to be size 1.
Is there any way to get the system default font name in Java? The default font can differ from os. So it can create trouble if we use font Arial and the jar is running in Linux without having Arial font installed.
Try this:
private final Font FONT = new JLabel().getFont();
JavaFX makes this a lot easier:
import javafx.scene.text.Font;
then use:
Font defaultFont = Font.getDefault();
or
// Where 14 is the font size
Font defaultFont = new Font(14);
Use the defined Font constants such as SERIF/SANS_SERIF etc.
I am currently using this to get the default font, although I would rather not need to use a graphics object to get it:
private final Font getFont()
{
Graphics g = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB).getGraphics();
Font font = new Font(g.getFont().toString(), 0, 12);
g.dispose();
return font;
}
I don't think there is a way of retrieving a system default font(in Swing/AWT the font is normally associated with the current LAF and component, for instance), but if your concern is font compatibility - you could check the font you are using against all the system fonts:
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] allFonts = e.getAllFonts();
and make a "fail-over" choice if it doesn't exist.
Take a look at public static Font decode(String str) here. When the decode method receives a null pointer as a parameter it returns the "Dialog" font which is usually the system font.
getFont() returns the current font, which is (usually?) the default.
I did this to increase font size.
public MyTextArea(){
Font currentFont = super.getFont();
String fontName = currentFont.getFontName();
int fontStyle = currentFont.getStyle();
int fontSize = currentFont.getSize() + 4;
super.setFont(new Font(fontName, fontStyle, fontSize));
}
In Windows, Segoe UI
Visit http://www.apaddedcell.com/sites/www.apaddedcell.com/files/fonts-article/final/index.html to see a list of preinstalled fonts.
I chose Verdana