• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2012 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package com.jme3.niftygui;
34 
35 import com.jme3.font.BitmapFont;
36 import com.jme3.font.BitmapText;
37 import de.lessvoid.nifty.spi.render.RenderFont;
38 
39 public class RenderFontJme implements RenderFont {
40 
41     private NiftyJmeDisplay display;
42     private BitmapFont font;
43     private BitmapText text;
44     private float actualSize;
45 
46     /**
47      * Initialize the font.
48      * @param name font filename
49      */
RenderFontJme(String name, NiftyJmeDisplay display)50     public RenderFontJme(String name, NiftyJmeDisplay display) {
51         this.display = display;
52         font = display.getAssetManager().loadFont(name);
53         if (font == null) {
54             throw new RuntimeException( "Font not loaded:" + name );
55         }
56         text = new BitmapText(font);
57         actualSize = font.getPreferredSize();
58         text.setSize(actualSize);
59     }
60 
createText()61     public BitmapText createText() {
62       return new BitmapText(font);
63     }
64 
getText()65     public BitmapText getText(){
66         return text;
67     }
68 
69     /**
70      * get font height.
71      * @return height
72      */
getHeight()73     public int getHeight() {
74         return (int) text.getLineHeight();
75     }
76 
77     /**
78      * get font width of the given string.
79      * @param str text
80      * @return width of the given text for the current font
81      */
getWidth(final String str)82     public int getWidth(final String str) {
83         if (str.length() == 0)
84             return 0;
85 
86         // Note: BitmapFont is now fixed to return the proper line width
87         //       at least for now.  The older commented out (by someone else, not me)
88         //       code below is arguably 'more accurate' if BitmapFont gets
89         //       buggy again.  The issue is that the BitmapText and BitmapFont
90         //       use a different algorithm for calculating size and both must
91         //       be modified in sync.
92         int result = (int) font.getLineWidth(str);
93 //        text.setText(str);
94 //        text.updateLogicalState(0);
95 //        int result = (int) text.getLineWidth();
96 
97         return result;
98     }
99 
getWidth(final String str, final float size)100     public int getWidth(final String str, final float size) {
101       // Note: This is supposed to return the width of the String when scaled
102       //       with the size factor. Since I don't know how to do that with
103       //       the font rendering in jme this will only work correctly with
104       //       a size value of 1.f and will return inaccurate values otherwise.
105       return getWidth(str);
106     }
107 
108     /**
109      * Return the width of the given character including kerning information.
110      * @param currentCharacter current character
111      * @param nextCharacter next character
112      * @param size font size
113      * @return width of the character or null when no information for the character is available
114      */
getCharacterAdvance(final char currentCharacter, final char nextCharacter, final float size)115     public int getCharacterAdvance(final char currentCharacter, final char nextCharacter, final float size) {
116         return Math.round(font.getCharacterAdvance(currentCharacter, nextCharacter, size));
117     }
118 
dispose()119     public void dispose() {
120     }
121 }
122