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

相关文章

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U

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文档,包括设置首行缩进、字体、字号、行间距、段落对齐方式等,需... 目录关键代码一级标题设置  正文设置完整代码运行结果最近关于批处理格式的问题我查了很多资料,但是都没