Is it possible to change the size of Heiro generated font in the code?-LibGdx - libgdx

I am using a font generated by Heiro tool.
game.scoreFont = new BitmapFont(Gdx.files.internal("fonts/score.fnt"), false);
I applied it for a label;
private void drawScore() {
Label.LabelStyle scoreStyle = new Label.LabelStyle();// style
scoreStyle.font = game.scoreFont;
scoreLabel2 = new Label(scoreController.getScoreString(), scoreStyle);// label
scoreLabel2.setPosition(scoreBox.getX()+scoreBox.getWidth()/2.7f,scoreBox.getY()+scoreBox.getHeight()/2);
scoreLabel2.setAlignment( Align.center);
}
While generating font,I gave a size of 32.
But same font I want to use for a label somewhere else in the game ,with a little bit bigger size.
Is it possible to increase the size then?or Should I generate a new font of required size with Heiro tool again?

You can scale your BitmapFont by
bitmapFont.getData().setScale(2);
and then use this font in LabelStyle, however you can also scale in this way
labelStyle.font.getData().setScale(2);
It works on reference of BitmapFontso it scaled everywhere, where you're using that BitmapFont in your game.
So you can create two object of BitmapFont with same file and scale one font and use where you want bigger font.
I'll recommend you to create two font with different size using your tools. Scaling is never a good solution.

Related

How to get the natural fontsize of bitmapfont?

If I have a bitmapfont such as;
fonttexture = new Texture(Gdx.files.internal("data/fonttest.png"), true);
font= new BitmapFont(Gdx.files.internal("data/fonttest.fnt"),
new TextureRegion(fonttexture ), true);
Is it possible to retrieve or deduce the natural fontsize used on the associated png texture file?
The *.fnt file itself specifies this on the first line;
info face="Calibri Light" size=32 bold=0 italic=0 charset=""
Its that 32 I am after in code.
I couldn't see a method on BitmapFonts class to directly get it, but there was various other height related attributes possible to get.
I tried recreating the size from those but didn't have much luck.
float baseToAssent = font.getCapHeight()+font.getAscent();
float baseToDecent = -font.getDescent();Decent);
float fontHeight = baseToAssent+baseToDecent; //too small, like 11 not 32
Not being a fonttographer? fontophile?...umm...graphic designer, I am probably putting the pieces together wrong, or maybe just lacking one.
In either case, any way to work out the font size used in the fonttest.png would be helpful.
Thanks
Usecase;
I am manually using that texture with a distance field shader. Knowing the textures natural font size is useful in order to accurately scale the text and lineheight to any requested font size without ever resizing a image and thus losing quality.
Not sure how to get the font size specific, however you can easily get the String width and height of a font using a GlyphLayout, like this:
BitmapFont font = new BitmapFont(Gdx.files.internal("data/fonttest.fnt", false);
GlyphLayout glyph = new GlyphLayout(font, "TextMessage");
float width = glyph.width;
float height = glyph.height;
Read this topic if you want to learn more about GlyphLayout and BitmapFont.
Experimented quite a bit with a 32px font, like yours. The String "W" seemed to be 29px wide, which was the widest I could find, so it's definately not precise if what you want to archive is the size exactly.
You could do a little hacky method, reading the .fnt file as a text file and looking for the Integer after "size=", pretty much like a properties file work, it will be less efficient but will return exact value.

libgdx BitmapFont display text on small scale

I have OrthographicCamera with viewport 16x9.
Lets say for example that devices real resolution is 1024x576
this.cam = new OrthographicCamera(16, 9);
I create BitmapFont like this:
FreetypeFontLoader.FreeTypeFontLoaderParameter params = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
params.fontFileName = "font.ttf";
params.fontParameters.size = 19;
Font size in pixels iz 19. That is calculated like this: screenHeight(576)/cameraViewportHeight(9)*0.3
0.3 is how big font should be in camera units
Since font is rendered on camera mentioned above, we have to scale font to our camera size:
font.getData().setScale(0.015f);// 0.3/19
I dont know if my math is ok, but this works fine on libgdx version 1.7.1. If i update to any higher version (currently 1.9.2, but i also tried 1.7.2), font just dont display
What am i doing wrong?

text in ImageTextButton bigger on higher resolution devices

I am having some trouble specifying the size of text in ImageTextButton. I didn't found any method or property to define text size. I used the code below,but got very small text. I expect text to be bigger on higher resolution devices, since all my code is relative to the dimensions of screen. Is bitmap font the correct approach? How does one code text in ImageTextButton to be bigger on bigger devices?
font = new BitmapFont(Gdx.files.internal("font.fnt"));
ImageTextButton.ImageTextButtonStyle bts = new ImageTextButton.ImageTextButtonStyle();
bts.up = ninePatchDrawable;
bts.font=font;
bts.fontColor= Color.WHITE;
checkButton=new ImageTextButton("Text",bts);
checkButton.setBounds(Gdx.graphics.getWidth() * 0.2f, Gdx.graphics.getHeight() * 0.01f,Gdx.graphics.getWidth() * 0.24f,Gdx.graphics.getHeight()*0.08f);
stage.addActor(checkButton);
Turns out I had to choose different keywords for google search. This solves my problem How to draw smooth text in libgdx?

Unable to play Sprite Images with aspect ratio in Windows Phone 8

I am trying to play sprite images on windows phone 8. But its not playing correctly, if the single sprite image is larger than screen dimensions. I try to reduce the image with aspect ratio by using rectangle and ImageBrush. Still I am facing the same problem. Please find the below scenario.
Scenario :
Sprite Width/Height : 22100 / 516
Single Sprite Width/Height : 850/516
Mobile Dimensions : 800/480
Could anyone can help me out.
So, if I understood your question, all you need to do is:
Create a BitMapImage, don't use ImageBrush or Rectangle..
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri(imagePathOrUrl, UriKind.RelativeOrAbsolute);
I suggest you to add also this instruction to prevent memory issues:
bitmapImage.DecodePixelWidth = imageWidth;
Add the BitMapImage to an Image control and set the **Stretch** property for the aspect ratio:
Image img = new Image();
img.Source=bitmapImage;
img.Stretch = Stretch.Uniform;
Stretch property has also other values, so be sure to do some test with the other values to see what value adapts better to your needs.
Let me know if it works!

How get a string width in Libgdx?

I wonder know how to get the width of my string in pixels
BitmapFont API < 1.5.6
To mesure the width of a String you use your Font and get the bounds of the String, you are going to draw.
BitmapFont.getBounds(String str).width
BitmapFont API
You can get the height to for the right offset for drawing too. Just replace width with height.
In addition for multiline texts use getMultiLineBounds(someString).width to get the bounds.
BitmapFont API >= 1.5.6
The BitmapFont API changed in 1.5.7 so there is a different way to get the bounds now:
BitmapFont.TextBounds and getBounds are done. Instead, give the string to GlyphLayout and get the bounds using its width and height fields. You can then draw the text by passing the same GlyphLayout to BitmapFont, which means the glyphs don’t have to be laid out twice like they used to.
Source (Web archive)
Example:
GlyphLayout layout = new GlyphLayout(); //dont do this every frame! Store it as member
layout.setText("meow");
float width = layout.width;// contains the width of the current set text
float height = layout.height; // contains the height of the current set text
According to #Nates answer: https://stackoverflow.com/a/20759876/619673 calling method
BitmapFont.getBounds(String str).width
not always returns proper width! Especially when you are reusing font.
If you want draw text for example in the center of part of view port, you can avoid this problem by using another method
BitmapFont.drawWrapped(...)
Example code:
font.drawWrapped(spriteBatch, "text", x_pos, y_pos, your_area_for_text, BitmapFont.HAlignment.CENTER);
If you use skins in UI it is a hassle to find out the correct font to feed into the GlyphLayout.
In this case I use a throw away instance of Label to figure everything out for me then ask the Label for the width.
Skin skin = getMySkin();
Label cellExample = new Label("888.88888", skin);
cellExample.layout();
float cellWidth = cellExample.getWidth();
Table table = new Table(skin);
table.defaults().width(cellWidth);
// fill table width cells ...
This is not the answer if you want to position the text yourself, but it is useful to make a UI layout stable and less dependent on the actual content of the cells.