Last active
January 7, 2016 00:10
-
-
Save jrenner/9016497 to your computer and use it in GitHub Desktop.
font-generation-libgdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void generateFonts() { | |
// if fonts are already generated, just load from file | |
Preferences fontPrefs = Gdx.app.getPreferences("org.jrenner.superior.font"); | |
int displayWidth = fontPrefs.getInteger("display-width", 0); | |
int displayHeight = fontPrefs.getInteger("display-height", 0); | |
boolean loaded = false; | |
if (displayWidth != Gdx.graphics.getWidth() || displayHeight != Gdx.graphics.getHeight()) { | |
Tools.log.debug("Screen size change detected, regenerating fonts"); | |
} else { | |
try { | |
// try to load from file | |
Tools.log.debug("Loading generated fonts from file cache"); | |
smallGeneratedFont = new BitmapFont(getFontFile("exo-small.fnt")); | |
normalGeneratedFont = new BitmapFont(getFontFile("exo-normal.fnt")); | |
largeGeneratedFont = new BitmapFont(getFontFile("exo-large.fnt")); | |
monoGeneratedFont = new BitmapFont(getFontFile("lib-mono.fnt")); | |
loaded = true; | |
} catch (GdxRuntimeException e) { | |
Tools.log.debug("Couldn't load pre-generated fonts. Will generate fonts."); | |
} | |
} | |
if (!loaded || forceGeneration) { | |
forceGeneration = false; | |
float width = Gdx.graphics.getWidth(); | |
float ratio = width / 1280f; // use 1920x1280 as baseline, arbitrary | |
float baseSize = 28f; // for 28 sized fonts at baseline width above | |
// define other sizes | |
int size = (int) (baseSize * ratio); | |
int smallSize = (int) (size * 0.75f); | |
int largeSize = (int) (size * 1.5f); | |
// store screen width for detecting screen size change | |
// on later startups, which will require font regeneration | |
fontPrefs.putInteger("display-width", Gdx.graphics.getWidth()); | |
fontPrefs.putInteger("display-height", Gdx.graphics.getHeight()); | |
fontPrefs.flush(); | |
FileHandle exoFile = Gdx.files.internal("fonts/Exo-Regular.otf"); | |
int pageSize = 512; // size of atlas pages for font pngs | |
smallGeneratedFont = generateFontWriteFiles("exo-small", exoFile, smallSize, pageSize, pageSize); | |
normalGeneratedFont = generateFontWriteFiles("exo-normal", exoFile, size, pageSize, pageSize); | |
largeGeneratedFont = generateFontWriteFiles("exo-large", exoFile, largeSize, pageSize, pageSize); | |
FileHandle libMonoFile = Gdx.files.internal("fonts/LiberationMono-Regular.ttf"); | |
monoGeneratedFont = generateFontWriteFiles("lib-mono", libMonoFile, smallSize, pageSize, pageSize); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment