Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.codeuniverse.gdx.utils;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.files.FileHandle;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
- import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
- /**
- * My utilities to work with Fonts in LibGDX
- *
- */
- public final class GDXFontUtils {
- private final static String tag = GDXFontUtils.class.getName(); // for logging
- /**
- * Takes TTF Font and font size and returns Bitmap with the font scaled to that
- * size. Given TTF Font shall support Russian. Returned BitmapFont has Russian
- * (and English) characters enabled.
- *
- * Uses Gdx-freetype Genertor - see
- * https://github.com/libgdx/libgdx/wiki/Gdx-freetype
- *
- * @param ttfFontPathAndName - just "Roboto_Black.ttf" if inside assets folder
- * root, "fonts/Roboto_Black.ttf" if inside
- * assets/fonts, "mydir1/dir2/file.ttf" in any
- * non-assets folder
- */
- public static BitmapFont generateFont(String ttfFontPathAndName, int fontSize) {
- BitmapFont result = new BitmapFont();
- String ruCharacters = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ" + "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
- String engCharacters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
- String extraCharacters = "1234567890.,:;_!?\\\\";
- // https://github.com/libgdx/libgdx/wiki/Gdx-freetype
- FileHandle fontFileHandle = Gdx.files.internal(ttfFontPathAndName);
- Gdx.app.log(tag, "fontFileHandle is: " + fontFileHandle);
- FreeTypeFontGenerator fontGenerator = new FreeTypeFontGenerator(fontFileHandle);
- Gdx.app.log(tag, "fontGenerator is: " + fontGenerator);
- FreeTypeFontParameter fontParameters = new FreeTypeFontParameter();
- fontParameters.characters = ruCharacters + engCharacters + extraCharacters;
- fontParameters.size = fontSize;
- result = fontGenerator.generateFont(fontParameters);
- Gdx.app.log(tag, "generateFontFrom() given ttfFontPathAndName = " + ttfFontPathAndName + ", fontSize = " + fontSize
- + ", generated font: " + result);
- fontGenerator.dispose(); // don't forget to dispose to avoid memory leaks!
- return result;
- }
- /**
- * Like generateFont(), but allows to specify font color + adds some nice border
- * and shadow to font.
- */
- public static BitmapFont generateFontEx(String ttfFontPathAndName, int fontSize, Color gdxColor) {
- BitmapFont result = new BitmapFont();
- String ruCharacters = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ" + "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
- String engCharacters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
- String extraCharacters = "1234567890.,:;_!?\\\\";
- // https://github.com/libgdx/libgdx/wiki/Gdx-freetype
- FileHandle fontFileHandle = Gdx.files.internal(ttfFontPathAndName);
- Gdx.app.log(tag, "fontFileHandle is: " + fontFileHandle);
- FreeTypeFontGenerator fontGenerator = new FreeTypeFontGenerator(fontFileHandle);
- Gdx.app.log(tag, "fontGenerator is: " + fontGenerator);
- FreeTypeFontParameter fontParameters = new FreeTypeFontParameter();
- fontParameters.characters = ruCharacters + engCharacters + extraCharacters;
- fontParameters.size = fontSize;
- // nice enhancements
- fontParameters.color = gdxColor;
- // border
- fontParameters.borderColor = Color.BLACK;
- fontParameters.borderWidth = 3;
- //shadow
- fontParameters.shadowColor = Color.BLACK;
- fontParameters.shadowOffsetX = 3;
- fontParameters.shadowOffsetY = 3;
- result = fontGenerator.generateFont(fontParameters);
- Gdx.app.log(tag, "generateFontFrom() given ttfFontPathAndName = " + ttfFontPathAndName + ", fontSize = " + fontSize
- + ", generated font: " + result);
- fontGenerator.dispose(); // don't forget to dispose to avoid memory leaks!
- return result;
- }
- }
Add Comment
Please, Sign In to add comment