1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests.extensions; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.files.FileHandle; 21 import com.badlogic.gdx.graphics.Color; 22 import com.badlogic.gdx.graphics.GL20; 23 import com.badlogic.gdx.graphics.OrthographicCamera; 24 import com.badlogic.gdx.graphics.g2d.BitmapFont; 25 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 26 import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; 27 import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData; 28 import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter; 29 import com.badlogic.gdx.tests.utils.GdxTest; 30 31 public class FreeTypeTest extends GdxTest { 32 BitmapFont font; 33 SpriteBatch batch; 34 BitmapFont ftFont; 35 36 @Override create()37 public void create () { 38 boolean flip = false; 39 batch = new SpriteBatch(); 40 if (flip) { 41 OrthographicCamera cam = new OrthographicCamera(); 42 cam.setToOrtho(flip); 43 cam.update(); 44 batch.setProjectionMatrix(cam.combined); 45 } 46 font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), flip); 47 FileHandle fontFile = Gdx.files.internal("data/arial.ttf"); 48 49 FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); 50 51 FreeTypeFontParameter parameter = new FreeTypeFontParameter(); 52 parameter.size = 15; 53 parameter.flip = flip; 54 parameter.genMipMaps = true; 55 // parameter.shadowOffsetX = 1; 56 // parameter.shadowOffsetY = 1; 57 // parameter.shadowColor = Color.GREEN; 58 // parameter.borderWidth = 1f; 59 // parameter.borderColor = Color.PURPLE; 60 61 FreeTypeBitmapFontData fontData = generator.generateData(parameter); 62 ftFont = generator.generateFont(parameter); 63 generator.dispose(); 64 } 65 66 @Override render()67 public void render () { 68 Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1); 69 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 70 71 batch.begin(); 72 font.setColor(Color.RED); 73 font.draw(batch, "This is a test\nAnd another line\n()����$%&/!12390#", 100, 112); 74 ftFont.draw(batch, "This is a test\nAnd another line\n()����$%&/!12390#", 100, 112); 75 // batch.disableBlending(); 76 batch.draw(ftFont.getRegion(), 300, 0); 77 // batch.enableBlending(); 78 batch.end(); 79 } 80 } 81