OSDN Git Service

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