SciTextRenderer.java

Go to the documentation of this file.
00001 /*
00002  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
00003  * Copyright (C) 2007 - INRIA - Jean-Baptiste Silvy
00004  * desc : TextRender to use with Scilab. Provides text rendering without aliasing
00005  * 
00006  * This file must be used under the terms of the CeCILL.
00007  * This source file is licensed as described in the file COPYING, which
00008  * you should have received as part of this distribution.  The terms
00009  * are also available at    
00010  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
00011  *
00012  */
00013 
00014 package org.scilab.modules.renderer.utils.textRendering;
00015 
00016 import java.awt.Font;
00017 import java.awt.geom.Rectangle2D;
00018 
00019 import javax.media.opengl.GL;
00020 
00021 import com.sun.opengl.util.j2d.TextRenderer;
00022 
00027 public class SciTextRenderer {
00028 
00029         private static final float EPSILON = 1.0e-4f; 
00030 
00032         private float fontSize;
00033         
00035         private TextRenderer renderer;
00036         
00038         private float scaleFactor;
00039         
00040         private boolean useFractionalMetrics;
00041         
00047         public SciTextRenderer(TextRenderer renderer, float fontSize) {
00048                 this.fontSize = fontSize;
00049                 this.renderer = renderer;
00050                 setUseFractionalMetrics(true);
00051                 updateScaleFactor();
00052         }
00053         
00057         private void updateScaleFactor() {
00058                 //if (useFractionalMetrics) {
00059                         this.scaleFactor = fontSize / renderer.getFont().getSize2D();
00060 //                      
00061 //              } else {
00062 //                      this.scaleFactor = 1.0f;
00063 //              }
00064         }
00065         
00066         
00067         
00078         public void draw3D(GL gl, String str, double x, double y, double z, double angle) {
00079                 // move position
00080                 //gl.glPushMatrix();
00081                 //gl.glTranslated(x, y, z);
00082 //              gl.glScalef(fontSize / TextRendererManager.DEFAULT_FONT_SIZE,
00083 //                                      fontSize / TextRendererManager.DEFAULT_FONT_SIZE,
00084 //                                      fontSize / TextRendererManager.DEFAULT_FONT_SIZE);
00085                 // with OpenGL strings, angle is already set
00086                 if (useFractionalMetrics) {
00087                         renderer.draw3D(str, (float) x, (float) y, (float) z, scaleFactor);
00088                 } else {
00089                         // we need to add a little offset othrwise texture interpolation
00090                         // sometimes (especially for title) leads to jaggy text.
00091                         renderer.draw3D(str, (float) x, (float) y, (float) z, 1.0f + EPSILON);
00092                 }
00093                 
00094                 // flush since we moved rendering position
00095                 //renderer.flush();
00096                 //gl.glPopMatrix();
00097         }
00098         
00104         public void setUseFractionalMetrics(boolean useFractionalMetrics) {
00105                 this.useFractionalMetrics = useFractionalMetrics;
00106                 if (useFractionalMetrics) {
00107                         renderer.setSmoothing(true);
00108                 } else {
00109                         renderer.setSmoothing(false);
00110                 }
00111                 updateScaleFactor();
00112         }
00113         
00117         public void begin3DRendering() {
00118                 renderer.begin3DRendering();
00119         }
00120         
00124         public void end3DRendering() {
00125                 renderer.end3DRendering();
00126         }
00127         
00131         public Font getFont() {
00132                 // renderer font is from the same family but does not have the same size.
00133                 return renderer.getFont().deriveFont(fontSize);
00134         }
00135         
00141         public void setFontSize(float newFontSize) {
00142                 this.fontSize = newFontSize;
00143                 updateScaleFactor();
00144         }
00145         
00152         public void setColor(double red, double green, double blue) {
00153                 renderer.setColor((float) red, (float) green, (float) blue, 1.0f);
00154         }
00155         
00160         public void setColor(double[] color) {
00161                 renderer.setColor((float) color[0], (float) color[1], (float) color[2], 1.0f);
00162         }
00163         
00169         public Rectangle2D getBounds(String str) {
00170                 Rectangle2D res = renderer.getBounds(str);
00171                 
00172                 // apply scale factor to the bounds
00173                 res.setRect(res.getX(), res.getY(),
00174                                         res.getWidth() * scaleFactor,
00175                                         res.getHeight() * scaleFactor);
00176                 return res;
00177         }
00178         
00179 
00180 }

Generated on Tue Sep 9 17:48:31 2008 for Scilab [trunk] by  doxygen 1.5.5