Advertisement
Guest User

Untitled

a guest
Nov 26th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. package com.wessles.MERCury.opengl;
  2.  
  3. import java.awt.FontMetrics;
  4. import java.awt.Graphics2D;
  5. import java.awt.RenderingHints;
  6. import java.awt.image.BufferedImage;
  7.  
  8. /**
  9. * @from MERCury
  10. * @author wessles
  11. * @website www.wessles.com
  12. */
  13.  
  14. public class JavaFont implements com.wessles.MERCury.opengl.Font {
  15.  
  16. private IntObject[] chars = new IntObject[256];
  17.  
  18. private boolean antialias;
  19.  
  20. private int font_size = 0;
  21. private int font_height = 0;
  22.  
  23. public Texture font_tex;
  24.  
  25. private int texw=1000;
  26. private int texh=512;
  27.  
  28. private java.awt.Font font;
  29. private FontMetrics fmetrics;
  30.  
  31. public JavaFont(java.awt.Font font, boolean antialias) {
  32. this.font = font;
  33. font_size = font.getSize();
  34.  
  35. this.antialias = antialias;
  36.  
  37. createSet();
  38. }
  39.  
  40. private void createSet() {
  41. BufferedImage imgTemp = new BufferedImage(texw, texh, BufferedImage.TYPE_INT_ARGB);
  42. Graphics2D g = (Graphics2D) imgTemp.getGraphics();
  43.  
  44. g.setColor(new java.awt.Color(255, 255, 255, 1));
  45. g.fillRect(0, 0, texw, texh);
  46.  
  47. int rowHeight = 0;
  48. int positionX = 0;
  49. int positionY = 0;
  50.  
  51. for (int i = 0; i < 256; i++) {
  52. char ch = (char) i;
  53.  
  54. BufferedImage fontImage = getFontImage(ch);
  55.  
  56. IntObject newIntObject = new IntObject();
  57.  
  58. newIntObject.width = fontImage.getWidth();
  59. newIntObject.height = fontImage.getHeight();
  60.  
  61. if (positionX + newIntObject.width >= texw) {
  62. positionX = 0;
  63. positionY += rowHeight;
  64. rowHeight = 0;
  65. }
  66.  
  67. newIntObject.storedX = positionX;
  68. newIntObject.storedY = positionY;
  69.  
  70. if (newIntObject.height > font_height)
  71. font_height = newIntObject.height;
  72.  
  73. if (newIntObject.height > rowHeight)
  74. rowHeight = newIntObject.height;
  75.  
  76. g.drawImage(fontImage, positionX, positionY, null);
  77.  
  78. positionX += newIntObject.width;
  79.  
  80. if (i < 256)
  81. chars[i] = newIntObject;
  82.  
  83. fontImage = null;
  84. }
  85.  
  86. font_tex = Texture.loadTexture(imgTemp);
  87. }
  88.  
  89. private BufferedImage getFontImage(char ch) {
  90. BufferedImage tempfontImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
  91. Graphics2D g = (Graphics2D) tempfontImage.getGraphics();
  92. if (antialias == true)
  93. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  94. g.setFont(font);
  95. fmetrics = g.getFontMetrics();
  96. int charwidth = fmetrics.charWidth(ch);
  97.  
  98. if (charwidth <= 0)
  99. charwidth = 1;
  100. int charheight = fmetrics.getHeight();
  101. if (charheight <= 0)
  102. charheight = font_size;
  103.  
  104. BufferedImage fontImage;
  105. fontImage = new BufferedImage(charwidth, charheight, BufferedImage.TYPE_INT_ARGB);
  106. Graphics2D gt = (Graphics2D) fontImage.getGraphics();
  107. if (antialias == true)
  108. gt.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  109. gt.setFont(font);
  110.  
  111. gt.setColor(java.awt.Color.WHITE);
  112. int charx = 0;
  113. int chary = 0;
  114. gt.drawString(String.valueOf(ch), charx, chary + fmetrics.getAscent());
  115.  
  116. return fontImage;
  117.  
  118. }
  119.  
  120. public int getWidth(CharSequence whatchars) {
  121. int totalwidth = 0;
  122. IntObject intObject = null;
  123. int currentChar = 0;
  124. for (int i = 0; i < whatchars.length(); i++) {
  125. currentChar = whatchars.charAt(i);
  126. if (currentChar < 256)
  127. intObject = chars[currentChar];
  128.  
  129. if (intObject != null)
  130. totalwidth += intObject.width;
  131. }
  132. return totalwidth;
  133. }
  134.  
  135. public int getHeight() {
  136. return font_height;
  137. }
  138.  
  139. public int getHeight(CharSequence HeightString) {
  140. return font_height;
  141. }
  142.  
  143. public int getLineHeight() {
  144. return font_height;
  145. }
  146.  
  147. public Texture getFontTexture() {
  148. return font_tex;
  149. }
  150.  
  151. private class IntObject {
  152. public int width;
  153. public int height;
  154. public int storedX;
  155. public int storedY;
  156. }
  157.  
  158. @Override
  159. public void clean() {
  160. font_tex.clean();
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement