libgdx中文输出、bitmapFont输出中文、bitmapFont输出文字、输出字体文字

2023-11-01 16:01

本文主要是介绍libgdx中文输出、bitmapFont输出中文、bitmapFont输出文字、输出字体文字,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

libgdx中文输出、bitmapFont输出中文、bitmapFont输出文字、输出字体文字

libgdx中文输出、bitmapFont输出中文、bitmapFont输出文字、输出字体文字。jdk17+2023年11月1日14:08:44最新、

转自:https://lingkang.top/archives/libgdx-zhong-wen-shu-chu

Maven依赖

    <properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><gdx.version>1.12.0</gdx.version></properties><dependencies><!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx --><dependency><groupId>com.badlogicgames.gdx</groupId><artifactId>gdx</artifactId><version>${gdx.version}</version></dependency><!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-backend-lwjgl3 --><dependency><groupId>com.badlogicgames.gdx</groupId><artifactId>gdx-backend-lwjgl3</artifactId><version>${gdx.version}</version></dependency><!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-platform --><dependency><groupId>com.badlogicgames.gdx</groupId><artifactId>gdx-platform</artifactId><version>${gdx.version}</version><classifier>natives-desktop</classifier></dependency><!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-freetype --><dependency><groupId>com.badlogicgames.gdx</groupId><artifactId>gdx-freetype</artifactId><version>${gdx.version}</version></dependency><!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-freetype-platform --><dependency><groupId>com.badlogicgames.gdx</groupId><artifactId>gdx-freetype-platform</artifactId><version>${gdx.version}</version><classifier>natives-desktop</classifier></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>2.0.9</version></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>2.0.9</version></dependency><!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version><scope>provided</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>17</source><target>17</target><!-- 编译后保持方法形参名称不变 --><!--<compilerArgs><arg>-parameters</arg></compilerArgs>--></configuration></plugin></plugins></build><repositories><repository><id>tencent</id><name>tencent</name><layout>default</layout><url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url><snapshots><enabled>false</enabled></snapshots><releases><enabled>true</enabled></releases></repository><repository><id>nexus</id><name>Nexus</name><layout>default</layout><url>https://s01.oss.sonatype.org/content/repositories/snapshots</url><snapshots><enabled>true</enabled></snapshots><releases><enabled>true</enabled></releases></repository><repository><id>aliyunmaven</id><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository></repositories>

代码调用

      protected BitmapFont bitmapFont;  //加载 仿宋字体FreeTypeFontGenerator generator = new FreeTypeFontGenerator(new FileHandle("C:\\Windows\\Fonts\\simfang.ttf"));bitmapFont = new SmartBitmapFont(generator,50);// 绘制bitmapFont.draw(batch, "欢迎", 0, 300);// 左下角起点,向上300个高度

通过重写 BitmapFont 进行读取字体输出

package top.lingkang.yzcy.utils;import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeType;
import com.badlogic.gdx.graphics.g2d.freetype.FreeType.Face;
import com.badlogic.gdx.graphics.g2d.freetype.FreeType.SizeMetrics;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.GlyphAndBitmap;
import com.badlogic.gdx.utils.GdxRuntimeException;import java.lang.reflect.Field;/*** 对BitmapFont进行改写,让文字获取直接从字体中获取* 调用方式:* <pre>*     protected BitmapFont bitmapFont; <br/>*     //加载 仿宋字体*     FreeTypeFontGenerator generator = new FreeTypeFontGenerator(new FileHandle("C:\\Windows\\Fonts\\simfang.ttf"));*     <br/> <br/>*     bitmapFont = new SmartBitmapFont(generator,50);**     <br/>*     bitmapFont.draw(batch, "欢迎", 0, 300);// 左下角起点,向上300个高度* </pre>*/
public class SmartBitmapFont extends BitmapFont {// 不应该在此对此进行 dispose ,让上层进行 disposeprivate FreeTypeFontGenerator generator;private FreeTypeBitmapFontData data;private FreeTypeFontParameter parameter;public SmartBitmapFont(FreeTypeFontGenerator generator, int fontSize) {if (generator == null)throw new GdxRuntimeException("lazyBitmapFont global generator must be not null to use this constructor.");this.generator = generator;FreeTypeFontParameter param = new FreeTypeFontParameter();param.size = fontSize;this.parameter = param;this.data = new LazyBitmapFontData(generator, fontSize, this);try {Field f = getClass().getSuperclass().getDeclaredField("data");f.setAccessible(true);f.set(this, data);} catch (Exception e) {e.printStackTrace();}genrateData();}private void genrateData() {Face face = null;try {Field field = generator.getClass().getDeclaredField("face");field.setAccessible(true);face = (Face) field.get(generator);} catch (Exception e) {e.printStackTrace();return;}// set general font dataSizeMetrics fontMetrics = face.getSize().getMetrics();// Set space glyph.Glyph spaceGlyph = data.getGlyph(' ');if (spaceGlyph == null) {spaceGlyph = new Glyph();spaceGlyph.xadvance = (int) data.scaleX;//spaceWidthspaceGlyph.id = (int) ' ';data.setGlyph(' ', spaceGlyph);}if (spaceGlyph.width == 0)spaceGlyph.width = (int) (spaceGlyph.xadvance + data.padRight);// set general font datadata.flipped = parameter.flip;data.ascent = FreeType.toInt(fontMetrics.getAscender());data.descent = FreeType.toInt(fontMetrics.getDescender());data.lineHeight = FreeType.toInt(fontMetrics.getHeight());// determine x-heightfor (char xChar : data.xChars) {if (!face.loadChar(xChar, FreeType.FT_LOAD_DEFAULT))continue;data.xHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight());break;}if (data.xHeight == 0)throw new GdxRuntimeException("No x-height character found in font");for (char capChar : data.capChars) {if (!face.loadChar(capChar, FreeType.FT_LOAD_DEFAULT))continue;data.capHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight());break;}// determine cap heightif (data.capHeight == 1)throw new GdxRuntimeException("No cap character found in font");data.ascent = data.ascent - data.capHeight;data.down = -data.lineHeight;if (parameter.flip) {data.ascent = -data.ascent;data.down = -data.down;}}@Overridepublic void dispose() {setOwnsTexture(true);super.dispose();data.dispose();}class LazyBitmapFontData extends FreeTypeBitmapFontData {private FreeTypeFontGenerator generator;private int fontSize;private SmartBitmapFont font;private int page = 1;public LazyBitmapFontData(FreeTypeFontGenerator generator, int fontSize, SmartBitmapFont lbf) {this.generator = generator;this.fontSize = fontSize;this.font = lbf;}public Glyph getGlyph(char ch) {Glyph glyph = super.getGlyph(ch);if (glyph == null)glyph = generateGlyph(ch);return glyph;}protected Glyph generateGlyph(char ch) {GlyphAndBitmap gab = generator.generateGlyphAndBitmap(ch, fontSize, false);if (gab == null || gab.bitmap == null)// 未找到文字字符: chreturn null;Pixmap map = gab.bitmap.getPixmap(Format.RGBA8888, Color.WHITE, 9);TextureRegion rg = new TextureRegion(new Texture(map));map.dispose();font.getRegions().add(rg);gab.glyph.page = page++;super.setGlyph(ch, gab.glyph);setGlyphRegion(gab.glyph, rg);return gab.glyph;}}
}

效果

在这里插入图片描述

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

这篇关于libgdx中文输出、bitmapFont输出中文、bitmapFont输出文字、输出字体文字的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/324141

相关文章

python多种数据类型输出为Excel文件

《python多种数据类型输出为Excel文件》本文主要介绍了将Python中的列表、元组、字典和集合等数据类型输出到Excel文件中,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一.列表List二.字典dict三.集合set四.元组tuplepython中的列表、元组、字典

一文教你解决Python不支持中文路径的问题

《一文教你解决Python不支持中文路径的问题》Python是一种广泛使用的高级编程语言,然而在处理包含中文字符的文件路径时,Python有时会表现出一些不友好的行为,下面小编就来为大家介绍一下具体的... 目录问题背景解决方案1. 设置正确的文件编码2. 使用pathlib模块3. 转换路径为Unicod

Python批量调整Word文档中的字体、段落间距及格式

《Python批量调整Word文档中的字体、段落间距及格式》这篇文章主要为大家详细介绍了如何使用Python的docx库来批量处理Word文档,包括设置首行缩进、字体、字号、行间距、段落对齐方式等,需... 目录关键代码一级标题设置  正文设置完整代码运行结果最近关于批处理格式的问题我查了很多资料,但是都没

基于Python实现一个PDF特殊字体提取工具

《基于Python实现一个PDF特殊字体提取工具》在PDF文档处理场景中,我们常常需要针对特定格式的文本内容进行提取分析,本文介绍的PDF特殊字体提取器是一款基于Python开发的桌面应用程序感兴趣的... 目录一、应用背景与功能概述二、技术架构与核心组件2.1 技术选型2.2 系统架构三、核心功能实现解析

Spring AI集成DeepSeek实现流式输出的操作方法

《SpringAI集成DeepSeek实现流式输出的操作方法》本文介绍了如何在SpringBoot中使用Sse(Server-SentEvents)技术实现流式输出,后端使用SpringMVC中的S... 目录一、后端代码二、前端代码三、运行项目小天有话说题外话参考资料前面一篇文章我们实现了《Spring

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式

Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)

《Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)》本文介绍了如何使用Python和Selenium结合ddddocr库实现图片验证码的识别和点击功能,感兴趣的朋友一起看... 目录1.获取图片2.目标识别3.背景坐标识别3.1 ddddocr3.2 打码平台4.坐标点击5.图

Java下载文件中文文件名乱码的解决方案(文件名包含很多%)

《Java下载文件中文文件名乱码的解决方案(文件名包含很多%)》Java下载文件时,文件名中文乱码问题通常是由于编码不正确导致的,使用`URLEncoder.encode(filepath,UTF-8... 目录Java下载文件中文文件名乱码问题一般情况下,大家都是这样为了解决这个问题最终解决总结Java下

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

电脑没有仿宋GB2312字体怎么办? 仿宋GB2312字体下载安装及调出来的教程

《电脑没有仿宋GB2312字体怎么办?仿宋GB2312字体下载安装及调出来的教程》仿宋字体gb2312作为一种经典且常用的字体,广泛应用于各种场合,如何在计算机中调出仿宋字体gb2312?本文将为您... 仿宋_GB2312是公文标准字体之一,仿China编程宋是字体名称,GB2312是字php符编码标准名称(简