gpt4 book ai didi

java - 使用 Slick Util 处理非英文字母

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:55 25 4
gpt4 key购买 nike

我在游戏中使用 LWJGL 和 Slick Util。但现在我无法制作俄语语言环境,因为 slick.TrueTypeFont 不会呈现西里尔字母(Яиssiaп Vodка、Сомяаd、Уер)。有人知道如何解决吗?

最佳答案

为了解决这个问题,我采用了这个方法:

private Texture letter(char i) {
if (letter[(int) i] != null) {
return letter[(int) i];
} else {
/*
Making empty buffer to get character's width.
*/
BufferedImage im = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY);
Graphics g = im.getGraphics();
FontMetrics fm = g.getFontMetrics(f);
int w = fm.charWidth(i);
int h = fm.getHeight() * 5 / 4;
/*
Making the java.awt.BufferedImage with character.
*/
if (w == 0) {
w = 1;
}
im = new BufferedImage(w, h*2, BufferedImage.TYPE_4BYTE_ABGR);

Graphics2D g2 = (Graphics2D) im.getGraphics();
g2.setFont(f);
g2.setColor(java.awt.Color.WHITE);

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);

g2.drawString(i + "", 0, h);

{
/*
Converting image to the Texture.
*/
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(im, "png", os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
Texture t = TextureLoader.getTexture("PNG", is);
letter[(int) i] = t;
return t;
} catch (IOException ex) {
System.out.println("\"" + i + "\" isn't loaded.");
}
return null;

}
}
}

这个textrender类:

package TheTimeless.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import static org.lwjgl.opengl.GL11.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

/**
*
* @author yew_mentzaki
*/
public final class VTextRender {

private final Texture letter[] = new Texture[Short.MAX_VALUE];
private Font f;

public VTextRender(int size, String name){
f= new Font(name, 0, size);
getWidth("ABCCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`'[](){}<>:,-.?\";/|\\!@#$%^&*_+-*=§АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя");
}

private Texture letter(char i) {
if (letter[(int) i] != null) {
return letter[(int) i];
} else {
/*
Making empty buffer to get character's width.
*/
BufferedImage im = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY);
Graphics g = im.getGraphics();
FontMetrics fm = g.getFontMetrics(f);
int w = fm.charWidth(i);
int h = f.getSize()+5;
/*
Making the java.awt.BufferedImage with character.
*/
if (w == 0) {
w = 1; h = 1;
}
im = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);

Graphics2D g2 = (Graphics2D) im.getGraphics();
g2.setFont(f);
g2.setColor(Color.WHITE);

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);

g2.drawString(i + "", 0, f.getSize());

{
/*
Converting image to the Texture.
*/
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(im, "png", os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
Texture t = TextureLoader.getTexture("PNG", is);
letter[(int) i] = t;
return t;
} catch (IOException ex) {
System.out.println("\"" + i + "\" isn't loaded.");
}
return null;

}
}
}

public int getWidth(String text) {
int width=0;
String[] fText=text.split("\n");
for(String str:fText) {
int w = 0;
for (char c : str.toCharArray()) {
Texture t = letter(c);
if (t == null) {
continue;
}

w += t.getImageWidth();
}
if(w>width){
width=w;
}
}
return width;
}
public String splitString(String text,int maxWidth)
{
try {
if (getWidth(text) > maxWidth && !text.contains(" "))
throw new Exception("the word is to big, insert space");
}catch(Exception e) {
e.printStackTrace();
}

int w = 0;
String[] fText = text.split(" ");
String result = "";
for (String str : fText) {
if (str.contains("\n"))
w = 0;
if (getWidth(str + " ") + w <= maxWidth) {
result += (str + " ");
w += getWidth(str + " ");
} else if(getWidth(str + " ") + w >= maxWidth){
w = 0;
result += "\n";
}
}
return result;
}
public void drawString(String text, int fx, int fy, org.newdawn.slick.Color clr) {
int x=fx, y=fy;
glColor4f(clr.r,clr.g,clr.b,clr.a);
for (char c : text.toCharArray()) {
Texture t = letter(c);
if (t == null) {
continue;
}
t.bind();
if(c=='\n')
{
y+=f.getSize()+5;
x=fx;
}
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(x, y);
glTexCoord2f(t.getWidth(), 0);
glVertex2i(x + t.getImageWidth(), y);
glTexCoord2f(t.getWidth(), t.getHeight());
glVertex2i(x + t.getImageWidth(), y + t.getImageHeight());
glTexCoord2f(0, t.getHeight());
glVertex2i(x, y + t.getImageHeight());
glEnd();
x += t.getImageWidth();
}
}
public int getHeight()
{
return f.getSize()+5;
}
}

关于java - 使用 Slick Util 处理非英文字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26917468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com