LIBGDX: FreeTypeFontGenerator and BitmapTrueFont

 

    在刚开始敲I;P游戏的时候, 字体使用的是BitmapFont, 只要有一套字体的PNG文件就可以显示字体数据了. 并且通过BitmapFont对象可以很好的跟Label结合在一起使用. BitmapFont中的字体从PNG文件中截取出来有很多方便的地方, 比如, 一来只需要准备使用的字符即可, 这样字体文件比较小; 二来可以定义任意的图案来代替字符, 灵活性定制方便. 但反过来, 由于基于PNG文件, 也导致了BitmapFont有个两个主要的缺陷 --

        1. 标量字体, 放大会失真;

        2. 字符集有限, 尤其是使用中文时, 改动几率大;

 

    好在LIBGDX还在其扩展库(gdx-freetype)中提供了另外一个好用的对象 --- FreeTypeFontGenerator. 通过这个对象可以加载TTF (TrueType Font), 然后产生BitmapFont对象就可以使用了.

    下面是FreeTypeFontGenerator对象的主要函数原型.

/** Generates a new {@link BitmapFont}, containing glyphs for the given characters. The size is expressed in pixels. Throws a
 * GdxRuntimeException in case the font could not be generated. Using big sizes might cause such an exception. All characters
 * need to fit onto a single texture.
 * @param size the size in pixels
 * @param characters the characters the font should contain
 * @param flip whether to flip the font horizontally, see {@link BitmapFont#BitmapFont(FileHandle, TextureRegion, boolean)} */
public BitmapFont generateFont (int size, String characters, boolean flip) {}
/** Generates a new {@link BitmapFont}. The size is expressed in pixels. Throws a GdxRuntimeException in case the font could not
 * be generated. Using big sizes might cause such an exception. All characters need to fit onto a single texture.
 * 
 * @param size the size of the font in pixels */
public BitmapFont generateFont (int size) {}

 

    在LIBGDX中, AssetManager是个很实用的对象, 通过它可以在游戏初始时就将资源加载或者初始化好, 使用时只需要传递资源名称即可获取相关资源. 比如, 下面代码加载了TextureAtlas和Sound的资源.

assetManager.load(PackConfig.SCREEN_PLAY, TextureAtlas.class);
assetManager.load(PackConfig.SCREEN_MENU, TextureAtlas.class);

assetManager.load(AudioConfig.MENU_CLICK, Sound.class);
assetManager.load(AudioConfig.TRAY_CATCH, Sound.class);

    字体也是一种资源, 因此也可以通过AssetManager来加载TTF字体. 为了能使得两者无缝链接, 可以模仿BitmapFont对象创建个BitmapTrueFont来实现.

 

package jie.android.ip.common.ttf;

import java.util.HashMap;

import com.badlogic.gdx.assets.AssetLoaderParameters;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.utils.Disposable;

public class BitmapTrueFont implements Disposable {

	private final HashMap<Integer, BitmapFont> fontMap = new HashMap<Integer, BitmapFont>();
	
	private final FreeTypeFontGenerator generator;
	private final String chars;
	
	public BitmapTrueFont(final FreeTypeFontGenerator generator, final BitmapTrueFontParameter parameter) {
		this.generator = generator;
		this.chars = parameter.getChars();
	}
	
	@Override
	public void dispose() {
		for (final BitmapFont font : fontMap.values()) {
			if (font != null) {
				font.dispose();
			}
		}

		if (generator != null) {
			generator.dispose();
		}
	}
	
	public final BitmapFont getBitmapFont(int size) {
		if (generator == null) {
			return null;
		}
		
		BitmapFont font = fontMap.get(Integer.valueOf(size));
		if (font == null) {
			if (chars == null) {
				font = generator.generateFont(size);
			} else {
				font = generator.generateFont(size, chars, false);
			}
			fontMap.put(Integer.valueOf(size), font);
		}
		return font;
	}
	
	static public class BitmapTrueFontParameter extends AssetLoaderParameters<BitmapTrueFont> {
		private String chars = null;
		
		public BitmapTrueFontParameter() {			
		}
		
		public BitmapTrueFontParameter(final String chars) {
			this.chars = chars;
		}
		
		public final String getChars() {
			return chars;
		}
	}	
}

    OK, 现在可以使用下面代码加载TTF资源了.

assetManager.load("example.ttf", BitmapTrueFont.class, new BitmapTrueFont.BitmapTrueFontParameter(null));

转载于:https://www.cnblogs.com/codejie/p/3590620.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值