Skip to content

Instantly share code, notes, and snippets.

@Tom-Ski
Created January 17, 2019 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tom-Ski/1fdc60ebd3530953a73d88ba9397f75e to your computer and use it in GitHub Desktop.
Save Tom-Ski/1fdc60ebd3530953a73d88ba9397f75e to your computer and use it in GitHub Desktop.
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.tests;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.PixmapPacker;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.tests.utils.GdxTest;
import com.badlogic.gdx.tools.bmfont.BitmapFontWriter;
import com.badlogic.gdx.tools.hiero.Hiero;
import com.badlogic.gdx.utils.GdxRuntimeException;
public class BitmapFontWriterTest extends GdxTest {
@Override
public void create () {
BitmapFontWriter.FontInfo info = new BitmapFontWriter.FontInfo();
info.padding = new BitmapFontWriter.Padding(0, 0, 0, 0);
FreeTypeFontGenerator.FreeTypeFontParameter param = new FreeTypeFontGenerator.FreeTypeFontParameter();
param.size = 13;
param.shadowOffsetY = 50;
param.renderCount = 3;
param.shadowColor = new Color(0, 0, 0, 0.45f);
param.characters = Hiero.EXTENDED_CHARS;
param.packer = new PixmapPacker(512, 512, Pixmap.Format.RGBA8888, 2, false, new PixmapPacker.SkylineStrategy());
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.absolute("data/unbom.ttf"));
final BitmapFont bitmapFont = generator.generateFont(param);
BitmapFontWriter.writeFont(bitmapFont.getData(), new String[] {"bitmapWrittenFont.png"}, Gdx.files.local("bitmapWrittenFont.fnt"), info, 512, 512);
BitmapFontWriter.writePixmaps(param.packer.getPages(), Gdx.files.local(""), "bitmapWrittenFont");
final float ascent = bitmapFont.getAscent();
final float descent = bitmapFont.getDescent();
final float capHeight = bitmapFont.getCapHeight();
final float lineHeight = bitmapFont.getLineHeight();
final float spaceXadvance = bitmapFont.getSpaceXadvance();
final float xHeight = bitmapFont.getXHeight();
BitmapFont loadedFont = new BitmapFont(Gdx.files.local("bitmapWrittenFont.fnt"));
final float loadedFontascent = loadedFont.getAscent();
final float loadedFontdescent = loadedFont.getDescent();
final float loadedFontcapHeight = loadedFont.getCapHeight();
final float loadedFontlineHeight = loadedFont.getLineHeight();
final float loadedFontspaceXadvance = loadedFont.getSpaceXadvance();
final float loadedFontxHeight = loadedFont.getXHeight();
System.out.println("Ascent: " + ascent + " : " + loadedFontascent);
System.out.println("Descent: " + descent + " : " + loadedFontdescent);
System.out.println("Cap Height: " + capHeight + " : " + loadedFontcapHeight);
System.out.println("Line height: " + lineHeight + " : " + loadedFontlineHeight);
System.out.println("Space X advance: " + spaceXadvance + " : " + loadedFontspaceXadvance);
System.out.println("xHeight: " + xHeight + " : " + loadedFontxHeight);
if (!MathUtils.isEqual(ascent, loadedFontascent)) throw new GdxRuntimeException("Ascent is not equal");
if (!MathUtils.isEqual(descent, loadedFontdescent)) throw new GdxRuntimeException("Descent is not equal");
if (!MathUtils.isEqual(capHeight, loadedFontcapHeight)) throw new GdxRuntimeException("Cap height is not equal");
if (!MathUtils.isEqual(lineHeight, loadedFontlineHeight)) throw new GdxRuntimeException("Line Height is not equal");
if (!MathUtils.isEqual(spaceXadvance, loadedFontspaceXadvance)) throw new GdxRuntimeException("spaceXAdvance is not equal");
if (!MathUtils.isEqual(xHeight, loadedFontxHeight)) throw new GdxRuntimeException("xHeight is not equal");
}
@Override
public void render () {
}
@Override
public void dispose () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment