OSDN Git Service

[added] Native font rendering to Hiero.
authornathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Mon, 1 Nov 2010 00:08:33 +0000 (00:08 +0000)
committernathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Mon, 1 Nov 2010 00:08:33 +0000 (00:08 +0000)
extensions/hiero/src/com/badlogic/gdx/hiero/Hiero.java
extensions/hiero/src/com/badlogic/gdx/hiero/unicodefont/GlyphPage.java
extensions/hiero/src/com/badlogic/gdx/hiero/unicodefont/HieroSettings.java
extensions/hiero/src/com/badlogic/gdx/hiero/unicodefont/UnicodeFont.java

index 99043e8..46738af 100644 (file)
@@ -136,6 +136,8 @@ public class Hiero extends JFrame {
        JSpinner padAdvanceYSpinner;\r
        JCheckBox italicCheckBox;\r
        JCheckBox boldCheckBox;\r
+       JRadioButton javaRadio;\r
+       JRadioButton nativeRadio;\r
        JLabel glyphsTotalLabel;\r
        JLabel glyphPagesTotalLabel;\r
        JComboBox glyphPageHeightCombo;\r
@@ -156,7 +158,7 @@ public class Hiero extends JFrame {
        File saveBmFontFile;\r
 \r
        public Hiero () {\r
-               super("Hiero v2.0 - Bitmap Font Tool");\r
+               super("Hiero v3.0 - Bitmap Font Tool");\r
                Splash splash = new Splash(this, "/splash.jpg", 2000);\r
                initialize();\r
                splash.close();\r
@@ -262,6 +264,7 @@ public class Hiero extends JFrame {
                unicodeFont.setPaddingAdvanceY(((Integer)padAdvanceYSpinner.getValue()).intValue());\r
                unicodeFont.setGlyphPageWidth(((Integer)glyphPageWidthCombo.getSelectedItem()).intValue());\r
                unicodeFont.setGlyphPageHeight(((Integer)glyphPageHeightCombo.getSelectedItem()).intValue());\r
+               unicodeFont.setNativeRendering(nativeRadio.isSelected());\r
 \r
                for (Iterator iter = effectPanels.iterator(); iter.hasNext();) {\r
                        EffectPanel panel = (EffectPanel)iter.next();\r
@@ -380,6 +383,8 @@ public class Hiero extends JFrame {
                boldCheckBox.addActionListener(listener);\r
                italicCheckBox.addActionListener(listener);\r
                resetCacheButton.addActionListener(listener);\r
+               javaRadio.addActionListener(listener);\r
+               nativeRadio.addActionListener(listener);\r
 \r
                sampleTextRadio.addActionListener(new ActionListener() {\r
                        public void actionPerformed (ActionEvent evt) {\r
@@ -610,9 +615,27 @@ public class Hiero extends JFrame {
                                        GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));\r
                                browseButton.setMargin(new Insets(0, 0, 0, 0));\r
                        }\r
+                       {\r
+                               fontPanel.add(new JLabel("Rendering:"), new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.EAST,\r
+                                       GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));\r
+                       }\r
+                       {\r
+                               javaRadio = new JRadioButton("Java");\r
+                               fontPanel.add(javaRadio, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,\r
+                                       GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));\r
+                               javaRadio.setSelected(true);\r
+                       }\r
+                       {\r
+                               nativeRadio = new JRadioButton("Native");\r
+                               fontPanel.add(nativeRadio, new GridBagConstraints(2, 4, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,\r
+                                       GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));\r
+                       }\r
                        ButtonGroup buttonGroup = new ButtonGroup();\r
                        buttonGroup.add(systemFontRadio);\r
                        buttonGroup.add(fontFileRadio);\r
+                       buttonGroup = new ButtonGroup();\r
+                       buttonGroup.add(javaRadio);\r
+                       buttonGroup.add(nativeRadio);\r
                }\r
                {\r
                        JPanel samplePanel = new JPanel();\r
@@ -694,18 +717,18 @@ public class Hiero extends JFrame {
                                                GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));\r
                                }\r
                                {\r
-                                       glyphPageWidthCombo = new JComboBox(new DefaultComboBoxModel(new Integer[] {new Integer(256), new Integer(512),\r
-                                               new Integer(1024), new Integer(2048)}));\r
+                                       glyphPageWidthCombo = new JComboBox(new DefaultComboBoxModel(new Integer[] {new Integer(32), new Integer(64),\r
+                                               new Integer(128), new Integer(256), new Integer(512), new Integer(1024), new Integer(2048)}));\r
                                        glyphCachePanel.add(glyphPageWidthCombo, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,\r
                                                GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));\r
-                                       glyphPageWidthCombo.setSelectedIndex(1);\r
+                                       glyphPageWidthCombo.setSelectedIndex(4);\r
                                }\r
                                {\r
-                                       glyphPageHeightCombo = new JComboBox(new DefaultComboBoxModel(new Integer[] {new Integer(256), new Integer(512),\r
-                                               new Integer(1024), new Integer(2048)}));\r
+                                       glyphPageHeightCombo = new JComboBox(new DefaultComboBoxModel(new Integer[] {new Integer(32), new Integer(64),\r
+                                               new Integer(128), new Integer(256), new Integer(512), new Integer(1024), new Integer(2048)}));\r
                                        glyphCachePanel.add(glyphPageHeightCombo, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,\r
                                                GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));\r
-                                       glyphPageHeightCombo.setSelectedIndex(1);\r
+                                       glyphPageHeightCombo.setSelectedIndex(4);\r
                                }\r
                                {\r
                                        resetCacheButton = new JButton("Reset Cache");\r
@@ -1155,8 +1178,7 @@ public class Hiero extends JFrame {
                                newUnicodeFont = null;\r
                        }\r
 \r
-                       // BOZO - Fix no effects.\r
-                       if (unicodeFont.loadGlyphs(25)) {\r
+                       if (!unicodeFont.getEffects().isEmpty() && unicodeFont.loadGlyphs(25)) {\r
                                glyphPageComboModel.removeAllElements();\r
                                int pageCount = unicodeFont.getGlyphPages().size();\r
                                int glyphCount = 0;\r
@@ -1188,14 +1210,14 @@ public class Hiero extends JFrame {
                        }\r
 \r
                        if (sampleTextRadio.isSelected()) {\r
-                               GL11.glColor4f(renderingBackgroundColor.r, renderingBackgroundColor.g, renderingBackgroundColor.b,\r
+                               GL11.glClearColor(renderingBackgroundColor.r, renderingBackgroundColor.g, renderingBackgroundColor.b,\r
                                        renderingBackgroundColor.a);\r
                                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);\r
                                int offset = unicodeFont.getYOffset(sampleText);\r
                                if (offset > 0) offset = 0;\r
                                unicodeFont.drawString(0, -offset, sampleText, Color.WHITE, 0, sampleText.length());\r
                        } else {\r
-                               GL11.glColor4f(1, 1, 1, 1);\r
+                               GL11.glClearColor(1, 1, 1, 1);\r
                                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);\r
                                unicodeFont.addGlyphs(sampleText);\r
                                // GL11.glColor4f(renderingBackgroundColor.r, renderingBackgroundColor.g, renderingBackgroundColor.b,\r
@@ -1205,16 +1227,33 @@ public class Hiero extends JFrame {
                                List pages = unicodeFont.getGlyphPages();\r
                                if (index >= 0 && index < pages.size()) {\r
                                        Texture texture = ((GlyphPage)pages.get(glyphPageCombo.getSelectedIndex())).getTexture();\r
-                                       GL11.glBegin(GL11.GL_QUADS);\r
-                                       GL11.glTexCoord2f(0, 0);\r
-                                       GL11.glVertex3f(0, 0, 0);\r
-                                       GL11.glTexCoord2f(0, 1);\r
-                                       GL11.glVertex3f(0, texture.getHeight(), 0);\r
-                                       GL11.glTexCoord2f(1, 1);\r
-                                       GL11.glVertex3f(texture.getWidth(), texture.getHeight(), 0);\r
-                                       GL11.glTexCoord2f(1, 0);\r
-                                       GL11.glVertex3f(texture.getWidth(), 0, 0);\r
-                                       GL11.glEnd();\r
+\r
+                                       glDisable(GL_TEXTURE_2D);\r
+                                       glColor4f(renderingBackgroundColor.r, renderingBackgroundColor.g, renderingBackgroundColor.b,\r
+                                               renderingBackgroundColor.a);\r
+                                       glBegin(GL_QUADS);\r
+                                       glVertex3f(0, 0, 0);\r
+                                       glVertex3f(0, texture.getHeight(), 0);\r
+                                       glVertex3f(texture.getWidth(), texture.getHeight(), 0);\r
+                                       glVertex3f(texture.getWidth(), 0, 0);\r
+                                       glEnd();\r
+                                       glEnable(GL_TEXTURE_2D);\r
+\r
+                                       texture.bind();\r
+                                       glColor4f(1, 1, 1, 1);\r
+                                       glBegin(GL_QUADS);\r
+                                       glTexCoord2f(0, 0);\r
+                                       glVertex3f(0, 0, 0);\r
+\r
+                                       glTexCoord2f(0, 1);\r
+                                       glVertex3f(0, texture.getHeight(), 0);\r
+\r
+                                       glTexCoord2f(1, 1);\r
+                                       glVertex3f(texture.getWidth(), texture.getHeight(), 0);\r
+\r
+                                       glTexCoord2f(1, 0);\r
+                                       glVertex3f(texture.getWidth(), 0, 0);\r
+                                       glEnd();\r
                                }\r
                        }\r
                }\r
index cf80858..c12703f 100644 (file)
@@ -2,6 +2,7 @@
 package com.badlogic.gdx.hiero.unicodefont;\r
 \r
 import java.awt.AlphaComposite;\r
+import java.awt.FontMetrics;\r
 import java.awt.Graphics2D;\r
 import java.awt.RenderingHints;\r
 import java.awt.font.FontRenderContext;\r
@@ -27,6 +28,7 @@ import com.badlogic.gdx.graphics.Sprite;
 import com.badlogic.gdx.graphics.Texture;\r
 import com.badlogic.gdx.graphics.Texture.TextureFilter;\r
 import com.badlogic.gdx.graphics.Texture.TextureWrap;\r
+import com.badlogic.gdx.hiero.unicodefont.effects.ColorEffect;\r
 import com.badlogic.gdx.hiero.unicodefont.effects.Effect;\r
 \r
 /**\r
@@ -139,10 +141,20 @@ public class GlyphPage {
                scratchGraphics.setComposite(AlphaComposite.Clear);\r
                scratchGraphics.fillRect(0, 0, MAX_GLYPH_SIZE, MAX_GLYPH_SIZE);\r
                scratchGraphics.setComposite(AlphaComposite.SrcOver);\r
-               scratchGraphics.setColor(java.awt.Color.white);\r
-               for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext();)\r
-                       ((Effect)iter.next()).draw(scratchImage, scratchGraphics, unicodeFont, glyph);\r
-               glyph.setShape(null); // The shape will never be needed again.\r
+               if (unicodeFont.getNativeRendering()) {\r
+                       for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext();) {\r
+                               Effect effect = (Effect)iter.next();\r
+                               if (effect instanceof ColorEffect) scratchGraphics.setColor(((ColorEffect)effect).getColor());\r
+                       }\r
+                       scratchGraphics.setColor(java.awt.Color.white);\r
+                       scratchGraphics.setFont(unicodeFont.getFont());\r
+                       scratchGraphics.drawString("" + (char)glyph.getCodePoint(), 0, unicodeFont.getAscent());\r
+               } else {\r
+                       scratchGraphics.setColor(java.awt.Color.white);\r
+                       for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext();)\r
+                               ((Effect)iter.next()).draw(scratchImage, scratchGraphics, unicodeFont, glyph);\r
+                       glyph.setShape(null); // The shape will never be needed again.\r
+               }\r
 \r
                WritableRaster raster = scratchImage.getRaster();\r
                int[] row = new int[width];\r
index d93d9a1..90a9103 100644 (file)
@@ -27,6 +27,7 @@ public class HieroSettings {
        private int paddingTop, paddingLeft, paddingBottom, paddingRight, paddingAdvanceX, paddingAdvanceY;\r
        private int glyphPageWidth = 512, glyphPageHeight = 512;\r
        private final List effects = new ArrayList();\r
+       private boolean nativeRendering;\r
 \r
        public HieroSettings () {\r
        }\r
@@ -67,6 +68,8 @@ public class HieroSettings {
                                        glyphPageWidth = Integer.parseInt(value);\r
                                } else if (name.equals("glyph.page.height")) {\r
                                        glyphPageHeight = Integer.parseInt(value);\r
+                               } else if (name.equals("glyph.native.rendering")) {\r
+                                       nativeRendering = Boolean.parseBoolean(value);\r
                                } else if (name.equals("effect.class")) {\r
                                        try {\r
                                                effects.add(Class.forName(value).newInstance());\r
@@ -261,6 +264,14 @@ public class HieroSettings {
                return effects;\r
        }\r
 \r
+       public boolean getNativeRendering () {\r
+               return nativeRendering;\r
+       }\r
+\r
+       public void setNativeRendering (boolean nativeRendering) {\r
+               this.nativeRendering = nativeRendering;\r
+       }\r
+\r
        /**\r
         * Saves the settings to a file.\r
         * @throws IOException if the file could not be saved.\r
@@ -278,6 +289,7 @@ public class HieroSettings {
                out.println("pad.advance.x=" + paddingAdvanceX);\r
                out.println("pad.advance.y=" + paddingAdvanceY);\r
                out.println();\r
+               out.println("glyph.native.rendering=" + nativeRendering);\r
                out.println("glyph.page.width=" + glyphPageWidth);\r
                out.println("glyph.page.height=" + glyphPageHeight);\r
                out.println();\r
index 3c632ef..67b1567 100644 (file)
@@ -7,6 +7,7 @@ import java.awt.FontMetrics;
 import java.awt.Rectangle;\r
 import java.awt.font.GlyphVector;\r
 import java.awt.font.TextAttribute;\r
+import java.awt.geom.Rectangle2D;\r
 import java.io.IOException;\r
 import java.lang.reflect.Field;\r
 import java.util.ArrayList;\r
@@ -41,6 +42,7 @@ public class UnicodeFont {
        static private final int PAGES = MAX_GLYPH_CODE / PAGE_SIZE;\r
 \r
        private Font font;\r
+       private FontMetrics metrics;\r
        private String ttfFileRef;\r
        private int ascent, descent, leading, spaceWidth;\r
        private final Glyph[][] glyphs = new Glyph[PAGES][];\r
@@ -51,6 +53,7 @@ public class UnicodeFont {
        private Glyph missingGlyph;\r
        private int glyphPageWidth = 512, glyphPageHeight = 512;\r
        private final DisplayList emptyDisplayList = new DisplayList();\r
+       private boolean nativeRendering;\r
 \r
        private boolean displayListCaching = true;\r
        private int baseDisplayListID = -1;\r
@@ -131,7 +134,7 @@ public class UnicodeFont {
                }\r
                font = baseFont.deriveFont(attributes);\r
 \r
-               FontMetrics metrics = GlyphPage.scratchGraphics.getFontMetrics(font);\r
+               metrics = GlyphPage.scratchGraphics.getFontMetrics(font);\r
                ascent = metrics.getAscent();\r
                descent = metrics.getDescent();\r
                leading = metrics.getLeading();\r
@@ -348,6 +351,7 @@ public class UnicodeFont {
                int extraX = 0, extraY = ascent;\r
                boolean startNewLine = false;\r
                Texture lastBind = null;\r
+               int offsetX = 0;\r
                for (int glyphIndex = 0, n = vector.getNumGlyphs(); glyphIndex < n; glyphIndex++) {\r
                        int charIndex = vector.getGlyphCharIndex(glyphIndex);\r
                        if (charIndex < startIndex) continue;\r
@@ -356,6 +360,7 @@ public class UnicodeFont {
                        int codePoint = text.codePointAt(charIndex);\r
 \r
                        Rectangle bounds = getGlyphBounds(vector, glyphIndex, codePoint);\r
+                       bounds.x += offsetX;\r
                        Glyph glyph = getGlyph(vector.getGlyphCode(glyphIndex), codePoint, bounds, vector, glyphIndex);\r
 \r
                        if (startNewLine && codePoint != '\n') {\r
@@ -397,7 +402,8 @@ public class UnicodeFont {
                                extraY += getLineHeight();\r
                                lines++;\r
                                totalHeight = 0;\r
-                       }\r
+                       } else if (nativeRendering)\r
+                               offsetX += bounds.width;\r
                }\r
                if (lastBind != null) GL11.glEnd();\r
 \r
@@ -455,7 +461,11 @@ public class UnicodeFont {
        }\r
 \r
        private Rectangle getGlyphBounds (GlyphVector vector, int index, int codePoint) {\r
-               Rectangle bounds = vector.getGlyphPixelBounds(index, GlyphPage.renderContext, 0, 0);\r
+               Rectangle bounds;\r
+               if (nativeRendering)\r
+                       bounds = metrics.getStringBounds("" + (char)codePoint, GlyphPage.scratchGraphics).getBounds();\r
+               else\r
+                       bounds = vector.getGlyphPixelBounds(index, GlyphPage.renderContext, 0, 0);\r
                if (codePoint == ' ') bounds.width = spaceWidth;\r
                return bounds;\r
        }\r
@@ -726,6 +736,14 @@ public class UnicodeFont {
                this.displayListCaching = displayListCaching;\r
        }\r
 \r
+       public void setNativeRendering (boolean nativeRendering) {\r
+               this.nativeRendering = nativeRendering;\r
+       }\r
+\r
+       public boolean getNativeRendering () {\r
+               return nativeRendering;\r
+       }\r
+\r
        /**\r
         * Returns the path to the TTF file for this UnicodeFont, or null. If this UnicodeFont was created without specifying the TTF\r
         * file, it will try to determine the path using Sun classes. If this fails, null is returned.\r