Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.wessles.MERCury.opengl;
- import java.awt.FontMetrics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.image.BufferedImage;
- /**
- * @from MERCury
- * @author wessles
- * @website www.wessles.com
- */
- public class JavaFont implements com.wessles.MERCury.opengl.Font {
- private IntObject[] chars = new IntObject[256];
- private boolean antialias;
- private int font_size = 0;
- private int font_height = 0;
- public Texture font_tex;
- private int texw=1000;
- private int texh=512;
- private java.awt.Font font;
- private FontMetrics fmetrics;
- public JavaFont(java.awt.Font font, boolean antialias) {
- this.font = font;
- font_size = font.getSize();
- this.antialias = antialias;
- createSet();
- }
- private void createSet() {
- BufferedImage imgTemp = new BufferedImage(texw, texh, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g = (Graphics2D) imgTemp.getGraphics();
- g.setColor(new java.awt.Color(255, 255, 255, 1));
- g.fillRect(0, 0, texw, texh);
- int rowHeight = 0;
- int positionX = 0;
- int positionY = 0;
- for (int i = 0; i < 256; i++) {
- char ch = (char) i;
- BufferedImage fontImage = getFontImage(ch);
- IntObject newIntObject = new IntObject();
- newIntObject.width = fontImage.getWidth();
- newIntObject.height = fontImage.getHeight();
- if (positionX + newIntObject.width >= texw) {
- positionX = 0;
- positionY += rowHeight;
- rowHeight = 0;
- }
- newIntObject.storedX = positionX;
- newIntObject.storedY = positionY;
- if (newIntObject.height > font_height)
- font_height = newIntObject.height;
- if (newIntObject.height > rowHeight)
- rowHeight = newIntObject.height;
- g.drawImage(fontImage, positionX, positionY, null);
- positionX += newIntObject.width;
- if (i < 256)
- chars[i] = newIntObject;
- fontImage = null;
- }
- font_tex = Texture.loadTexture(imgTemp);
- }
- private BufferedImage getFontImage(char ch) {
- BufferedImage tempfontImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g = (Graphics2D) tempfontImage.getGraphics();
- if (antialias == true)
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g.setFont(font);
- fmetrics = g.getFontMetrics();
- int charwidth = fmetrics.charWidth(ch);
- if (charwidth <= 0)
- charwidth = 1;
- int charheight = fmetrics.getHeight();
- if (charheight <= 0)
- charheight = font_size;
- BufferedImage fontImage;
- fontImage = new BufferedImage(charwidth, charheight, BufferedImage.TYPE_INT_ARGB);
- Graphics2D gt = (Graphics2D) fontImage.getGraphics();
- if (antialias == true)
- gt.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- gt.setFont(font);
- gt.setColor(java.awt.Color.WHITE);
- int charx = 0;
- int chary = 0;
- gt.drawString(String.valueOf(ch), charx, chary + fmetrics.getAscent());
- return fontImage;
- }
- public int getWidth(CharSequence whatchars) {
- int totalwidth = 0;
- IntObject intObject = null;
- int currentChar = 0;
- for (int i = 0; i < whatchars.length(); i++) {
- currentChar = whatchars.charAt(i);
- if (currentChar < 256)
- intObject = chars[currentChar];
- if (intObject != null)
- totalwidth += intObject.width;
- }
- return totalwidth;
- }
- public int getHeight() {
- return font_height;
- }
- public int getHeight(CharSequence HeightString) {
- return font_height;
- }
- public int getLineHeight() {
- return font_height;
- }
- public Texture getFontTexture() {
- return font_tex;
- }
- private class IntObject {
- public int width;
- public int height;
- public int storedX;
- public int storedY;
- }
- @Override
- public void clean() {
- font_tex.clean();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement