Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: wessles (posted 2013-11-28 23:37:31, viewed 130 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203  
package com.wessles.MERCury.font;

import java.awt.FontFormatException;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.wessles.MERCury.opengl.Texture;

/**
 * @from MERCury
 * @author wessles
 * @website www.wessles.com
 */

public class TrueTypeFont implements com.wessles.MERCury.font.Font {

   public final 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 = 512;
   private int texh = 512;

   private java.awt.Font font;
   private FontMetrics fmetrics;

   private TrueTypeFont(java.awt.Font font, boolean antialias) {
      this.font = font;
      this.font_size = font.getSize();
      this.antialias = antialias;

      createSet();
   }

   private void createSet() {
      // Make a graphics object for the buffered image.
      BufferedImage imgTemp = new BufferedImage(texw, texh, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g = (Graphics2D) imgTemp.getGraphics();

      // Set the color to transparent
      g.setColor(new java.awt.Color(255, 255, 255, 1));
      g.fillRect(0, 0, texw, texh);

      // Initialize temporary vars
      int rowHeight = 0;
      int positionX = 0;
      int positionY = 0;

      // Loop through all standard characters (256 of em')
      for (int i = 0; i < 256; i++) {
         char ch = (char) i;

         // BufferedImage for the character
         BufferedImage fontImage = getFontImage(ch);

         // New IntObject with width and height of fontImage.
         IntObject newIntObject = new IntObject();
         newIntObject.w = fontImage.getWidth();
         newIntObject.h = fontImage.getHeight();

         // Go to next row if there is no room on x axis.
         if (positionX + newIntObject.w >= texw) {
            positionX = 0;
            positionY += rowHeight;
            rowHeight = 0;
         }

         // Set the positions
         newIntObject.x = positionX;
         newIntObject.y = positionY;

         // Set the highest height of the font.
         if (newIntObject.h > font_height)
            font_height = newIntObject.h;

         // Set the row_height to the highest one in the row.
         if (newIntObject.h > rowHeight)
            rowHeight = newIntObject.h;

         // Draw the character onto the font image.
         g.drawImage(fontImage, positionX, positionY, null);

         // Next position on x axis.
         positionX += newIntObject.w;

         // Set the IntObject of the character
         chars[i] = newIntObject;

         fontImage = null;
      }

      // Load texture!
      font_tex = Texture.loadTexture(imgTemp);
   }

   private BufferedImage getFontImage(char ch) {
      // Make and init graphics for character image.
      BufferedImage tempfontImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g = (Graphics2D) tempfontImage.getGraphics();
      if (antialias)
         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setFont(font);
      // Font preperation
      fmetrics = g.getFontMetrics();
      int charwidth = fmetrics.charWidth(ch);

      // Safety guards just in case.
      if (charwidth <= 0)
         charwidth = 1;
      int charheight = fmetrics.getHeight();
      if (charheight <= 0)
         charheight = font_size;

      // Now to the actual image!
      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);

      // Set the text color to white, set x and y, and return to font image!
      gt.setColor(java.awt.Color.WHITE);
      int charx = 0;
      int chary = 0;
      gt.drawString(String.valueOf(ch), charx, chary + fmetrics.getAscent());

      return fontImage;
   }

   @Override
   public int getWidth(char[] what) {
      int totalwidth = 0;
      IntObject intObject = null;
      int currentChar = 0;
      for (char element : what) {
         currentChar = element;
         if (currentChar < 256)
            intObject = chars[currentChar];

         if (intObject != null)
            totalwidth += intObject.w;
      }
      return totalwidth;
   }

   @Override
   public Font deriveFont(float size) {
      return new TrueTypeFont(font.deriveFont(size), antialias);
   }

   @Override
   public Font deriveFont(int style) {
      return new TrueTypeFont(font.deriveFont(style), antialias);
   }

   @Override
   public int getHeight() {
      return font_height;
   }

   @Override
   public int getLineHeight() {
      return font_height;
   }

   @Override
   public Texture getFontTexture() {
      return font_tex;
   }

   public static class IntObject {
      public int w;
      public int h;
      public int x;
      public int y;
   }

   @Override
   public void clean() {
      font_tex.clean();
   }

   public static TrueTypeFont loadTrueTypeFont(String location, float size, int style, boolean antialias) throws FileNotFoundException, FontFormatException, IOException {
      java.awt.Font font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new FileInputStream(location));
      font = font.deriveFont(size);
      return loadTrueTypeFont(font, antialias);
   }

   public static TrueTypeFont loadTrueTypeFont(java.awt.Font font, boolean antialias) {
      return new TrueTypeFont(font, antialias);
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!