簡體   English   中英

使用Slick Util的非英文字母

[英]Non-english letters with Slick Util

我在游戲中使用帶有Slick Util的LWJGL。 但是現在我無法使用俄語語言環境,因為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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM