Package

Source Code of FontRenderer

/*  This file is part of WeiLiYu.

    WeiLiYu is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    WeiLiYu is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with WeiLiYu; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Copyright (C) 2006-2008 Michael Jumper
*/

import java.io.InputStream;
import java.awt.image.BufferedImage;
import java.awt.font.GlyphVector;
import java.awt.geom.*;
import java.awt.*;

public class FontRenderer {

  private Font myfont;
  int image_size;
  int glyph_pad;

  public FontRenderer(InputStream is, int size, int pad) {
    try {
      myfont = Font.createFont(Font.TRUETYPE_FONT, is);
    } catch (Exception e) {
      System.err.println("FATAL: Could not load font.");
      System.exit(1);
    }

    image_size = size;
    glyph_pad = pad;
  }

  public BufferedImage getBufferedImage(String c) {

    // Get font rendering things
    BufferedImage bi = new BufferedImage(image_size, image_size, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bi.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
        RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    // Create glyph vector
    GlyphVector gv = myfont.createGlyphVector(graphics.getFontRenderContext(), c);

    // Calculate scale
    Rectangle2D bounds = gv.getLogicalBounds();
    double xscale = (image_size - glyph_pad) / bounds.getWidth();
    double yscale = (image_size - glyph_pad) / bounds.getHeight();
    double scale;  // Global scale to be applied (do not distort)
    if (xscale > yscale) scale = xscale;
    else scale = yscale;


    double midx = (bounds.getMinX() + bounds.getMaxX()) / 2 * scale;
    double midy = (bounds.getMinY() + bounds.getMaxY()) / 2 * scale;


    AffineTransform at = AffineTransform.getScaleInstance(scale, scale);

    // The commented code below does not work on Macs.
/*    //graphics.setTransform(at);
    gv.setGlyphTransform(0, at);

    Rectangle2D rbounds = gv.getGlyphPixelBounds(0, null, 0, 0)
          .getBounds2D();

    gv.setGlyphTransform(0, null);

    double midx = (rbounds.getMinX() + rbounds.getMaxX()) / 2;
    double midy = (rbounds.getMinY() + rbounds.getMaxY()) / 2;

    System.out.println(rbounds.getMinX() + " " + rbounds.getMinY()
      + " " + rbounds.getMaxX() + " " + rbounds.getMaxY());*/

    // Set transform
    at = AffineTransform.getTranslateInstance(image_size/2.0 -midx, image_size/2.0 -midy);
    at.concatenate(AffineTransform.getScaleInstance(scale, scale));
    graphics.setTransform(at);

    // Draw glyph
    graphics.setColor(new Color(0, 0, 0));
    graphics.fill(gv.getOutline());

    return bi;

  }

}
TOP

Related Classes of FontRenderer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.