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.graphics.GL20; 21 import com.badlogic.gdx.graphics.OrthographicCamera; 22 import com.badlogic.gdx.graphics.g2d.BitmapFont; 23 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 24 import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; 25 import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter; 26 import com.badlogic.gdx.tests.utils.GdxTest; 27 28 /** Shows how to use fonts for languages other than english. Note that only alphabets with a humble amount of glyphs can be used 29 * (100 is about the maximum we'd recommend). All western languages fit into that category, some asian languages might work as 30 * well, e.g. Korean. Any right-to-left languages like arabic won't work, neither do languages like Japanese or Chinese due to 31 * their huge amount of glyphs which don't fit into a single texture.</p> 32 * 33 * Note that you don't have to use the FreeType extension for this, you can generate fonts with Hiero as well. The FreeType 34 * extension allows you to generate fonts at runtime for different screen densities. It is not portable to GWT!</p> 35 * 36 * For each script examplified below we only use a few characters from the respective alphabet. You'll have to pass in a string 37 * containing all the printable characters of that language that you want to use! 38 * 39 * @author mzechner */ 40 public class InternationalFontsTest extends GdxTest { 41 OrthographicCamera cam; 42 SpriteBatch batch; 43 BitmapFont koreanFont; 44 BitmapFont cyrillicFont; 45 BitmapFont thaiFont; 46 47 @Override create()48 public void create () { 49 FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/unbom.ttf")); 50 51 FreeTypeFontParameter parameter = new FreeTypeFontParameter(); 52 parameter.size = 18; 53 parameter.characters = "한국어/조선�?"; 54 55 koreanFont = generator.generateFont(parameter); 56 generator.dispose(); 57 58 parameter.characters = FreeTypeFontGenerator.DEFAULT_CHARS; 59 60 generator = new FreeTypeFontGenerator(Gdx.files.internal("data/russkij.ttf")); 61 cyrillicFont = generator.generateFont(parameter); 62 generator.dispose(); 63 64 parameter.characters = "วรณยุ�?ต์"; 65 66 generator = new FreeTypeFontGenerator(Gdx.files.internal("data/garuda.ttf")); 67 thaiFont = generator.generateFont(parameter); 68 generator.dispose(); 69 70 batch = new SpriteBatch(); 71 72 cam = new OrthographicCamera(); 73 cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 74 cam.update(); 75 } 76 77 @Override render()78 public void render () { 79 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 80 81 batch.setProjectionMatrix(cam.combined); 82 batch.begin(); 83 koreanFont.draw(batch, "한국어/조선�?", 0, 22); 84 cyrillicFont.draw(batch, "cyrillic text", 0, 44); 85 thaiFont.draw(batch, "วรรณยุ�?ต์", 0, 66); 86 batch.end(); 87 } 88 89 @Override resize(int width, int height)90 public void resize (int width, int height) { 91 cam.setToOrtho(false, width, height); 92 } 93 94 } 95