⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fstextconstructor.java

📁 利用opensource的开源jar实现生成flash文件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     */
    public FSDefineShape3 defineShape(int anIdentifier, String text, int fontSize, FSColor aColor)
    {
        FSShapeConstructor path = new FSShapeConstructor();
        
        path.add(new FSSolidFill(aColor));
        path.selectFillStyle(0);
        
        float scaleFactor = ((float)fontSize) / 1024.0f;
        
        int[] glyphCodes = glyphIndicesForString(text);
        int[] glyphAdvances = advancesForGlyphIndices(glyphCodes, scaleFactor);
        
        int xOffset = 0;
        
        for (int i=0; i<text.length(); i++)
        {
            ArrayList array = glyphTable[orderTable[glyphCodes[i]]].shape.getObjects();
    
            for (Iterator j = array.iterator(); j.hasNext();)
            {
                FSTransformObject currentObject = (FSTransformObject) j.next();

                if (currentObject instanceof FSShapeStyle)
                {
                    FSShapeStyle style = (FSShapeStyle) currentObject;
                    int moveX = (int)(style.getMoveX()*scaleFactor + ((style.getMoveX() < 0) ? (-0.5) : 0.5));
                    int moveY = (int)(style.getMoveY()*scaleFactor + ((style.getMoveY() < 0) ? (-0.5) : 0.5));
                    
                    path.closePath();
                    path.move(moveX + xOffset, moveY);
                }
                else if (currentObject instanceof FSLine)
                {
                    FSLine line = (FSLine) currentObject;
                    int x = (int)(line.getX()*scaleFactor + ((line.getX() < 0) ? (-0.5) : 0.5));
                    int y = (int)(line.getY()*scaleFactor + ((line.getY() < 0) ? (-0.5) : 0.5));

                    path.rline(x, y);
                }
                else if (currentObject instanceof FSCurve)
                {
                    FSCurve curve = (FSCurve) currentObject;
                    int cx = (int)(curve.getControlX()*scaleFactor + ((curve.getControlX() < 0) ? (-0.5) : 0.5));
                    int cy = (int)(curve.getControlY()*scaleFactor + ((curve.getControlY() < 0) ? (-0.5) : 0.5));
                    int ax = (int)(curve.getAnchorX()*scaleFactor + ((curve.getAnchorX() < 0) ? (-0.5) : 0.5));
                    int ay = (int)(curve.getAnchorY()*scaleFactor + ((curve.getAnchorY() < 0) ? (-0.5) : 0.5));

                    path.rcurve(cx, cy, ax, ay);
                }
            }
            path.closePath();
            xOffset += glyphAdvances[i];
        }

        return path.defineTransparentShape(anIdentifier);
    }

    private void decodeSWFFont(String filename) throws IOException, DataFormatException
    {
        FSMovie fontMovie = new FSMovie(filename);

        FSDefineFont font = null;
        FSFontInfo fontInfo = null;
        FSDefineText text = null;
        FSText textRecord = null;

        for (Iterator i = fontMovie.getObjects().iterator(); i.hasNext();)
        {
            FSMovieObject currentObject = (FSMovieObject) i.next();

            if (currentObject instanceof FSDefineFont)
            {
                font = (FSDefineFont) currentObject;
            }
            else if (currentObject instanceof FSFontInfo)
            {
                fontInfo = (FSFontInfo) currentObject;
            }
            else if (currentObject instanceof FSDefineText)
            {
                text = (FSDefineText) currentObject;
            }
        }

        textRecord = (FSText) text.getObjects().get(0);

        name = fontInfo.getName();
        encoding = fontInfo.getEncoding();
        size = (float) (textRecord.getHeight());
        isBold = fontInfo.isBold();
        isItalic = fontInfo.isItalic();
        
        /*
         * Change the encoding for ASCII to Unicode since ASCII is compatible
         * with UTF8.
         */
        if (encoding == FSText.ANSI)
            encoding = FSText.Unicode;

        glyphTable = new FSGlyph[font.getShapes().size()];
        
        int glyphIndex = 0;

        for (Iterator j = font.getShapes().iterator(); j.hasNext(); glyphIndex++)
            glyphTable[glyphIndex] = new FSGlyph((FSShape) j.next(), new FSBounds(0, 0, 0, 0));

        glyphIndex = 0;

        for (Iterator k = fontInfo.getCodes().iterator(); k.hasNext(); glyphIndex++)
            characterTable[((Integer) (k.next())).intValue()] = (short) glyphIndex;

        for (Iterator l = textRecord.getCharacters().iterator(); l.hasNext();)
        {
            FSCharacter character = (FSCharacter) l.next();

            glyphTable[character.getGlyphIndex()].advance = (int) (character.getAdvance() * (1024.0 / size));
        }
        orderTable[0] = 0;
    }  
    /*
     * This method will be superceded by decodeAWTFont(Font font) in a future 
     * release.
     */
    private void decodeAWTFont(String fontName)
    {
        FontRenderContext fontContext = new FontRenderContext(new AffineTransform(), true, true);

        /*
         Create an initial 1 point font of the specified name. The PLAIN font style is 
         used as the fontName if it contains a suffix such as "BOLD" or "Italic" will 
         override the style.
         */
        Font font = new Font(fontName, Font.PLAIN, 1);

        if (font == null)
            throw new IllegalArgumentException("No such font: " + fontName);
        
        name = fontName;
        encoding = FSText.Unicode;

        Rectangle2D transform = transformToEMSquare(font, fontContext);

        double scaleY = 1024.0; // Math.abs(1024.0 / bounds.getY());
        double scaleX = scaleY;
        double translateX = 1024.0 - (transform.getX() * 1024.0);
        double translateY = 1024.0 - (transform.getY() * 1024.0);

        size = (float) scaleY;

        /*
         The new font scaled to the EM Square must be derived using the size as well 
         as the transform used for the glyphs otherwise the advance values are not 
         scaled accordingly.
         */
//        AffineTransform at = AffineTransform.getTranslateInstance(translateX, translateY);
//        font = font.deriveFont(at);
        font = font.deriveFont((float)scaleX);

        missingGlyph = font.getMissingGlyphCode();

        isBold = font.isBold();
        isItalic = font.isItalic();

        int numGlyphs = font.getNumGlyphs();
        int glyphIndex = 0;
        int characterCode = 0;
    
        glyphTable = new FSGlyph[numGlyphs];
    
        /*
         * Run through all the unicode character codes looking for a corresponding glyph.
         */
        while ((glyphIndex < numGlyphs) && (characterCode < 65535))
        {
            char currentChar = (char) characterCode;
    
            if (font.canDisplay(currentChar))
            {
                GlyphVector glyphVector = font.createGlyphVector(fontContext,
                       new char[] { currentChar });
    
                Shape outline = glyphVector.getGlyphOutline(0);
                int advance = (int) (glyphVector.getGlyphMetrics(0).getAdvance());
    
                characterTable[currentChar] = (short)glyphIndex;
                glyphTable[glyphIndex] = new FSGlyph(convertShape(outline), new FSBounds(0, 0, 0, 0));
                glyphTable[glyphIndex].advance = advance;
    
                if (font.hasUniformLineMetrics() == false)
                {
                    LineMetrics lineMetrics = font.getLineMetrics(new char[]
                             { (char) currentChar }, 0, 1, fontContext);
    
                    ascent = 0; // Math.max(lineMetrics.getAscent() * 20, ascent);
                       descent = 0; // Math.max(lineMetrics.getDescent() * 20, descent);
                       leading = 0; // Math.max(lineMetrics.getLeading() * 20, leading);
                }
           }
           else
           {
                GlyphVector glyphVector = font.createGlyphVector(fontContext, new char[] { (char)missingGlyph });
            
                Shape outline = glyphVector.getGlyphOutline(0);
                int advance = (int) (glyphVector.getGlyphMetrics(0).getAdvance());
    
                characterTable[currentChar] = (short)glyphIndex;
                glyphTable[glyphIndex] = new FSGlyph(convertShape(outline), new FSBounds(0, 0, 0, 0));
                glyphTable[glyphIndex].advance = advance;
    
                if (font.hasUniformLineMetrics() == false)
                {
                    LineMetrics lineMetrics = font.getLineMetrics(new char[]
                             {(char)currentChar }, 0, 1, fontContext);
    
                    ascent = 0; // Math.max(lineMetrics.getAscent() * 20, ascent);
                    descent = 0; // Math.max(lineMetrics.getDescent() * 20, descent);
                    leading = 0; // Math.max(lineMetrics.getLeading() * 20, leading);
                }
           }
           glyphIndex++;
           characterCode++;
       }
       orderTable[0] = (short)missingGlyph;
    }
    private void decodeAWTFont(Font font)
    {
        FontRenderContext fontContext = new FontRenderContext(new AffineTransform(), true, true);
        font = font.deriveFont(1.0f);

        name = font.getName();
        encoding = FSText.Unicode;

        Rectangle2D transform = transformToEMSquare(font, fontContext);

        double scaleY = 1024.0; // Math.abs(1024.0 / bounds.getY());
        double scaleX = scaleY;
        double translateX = 1024.0 - (transform.getX() * 1024.0);
        double translateY = 1024.0 - (transform.getY() * 1024.0);

        size = (float) scaleY;

        /*
         The new font scaled to the EM Square must be derived using the size as well 
         as the transform used for the glyphs otherwise the advance values are not 
         scaled accordingly.
         */
        AffineTransform at = AffineTransform.getTranslateInstance(translateX, translateY);
        font = font.deriveFont(at);
        font = font.deriveFont((float)scaleX);

        missingGlyph = font.getMissingGlyphCode();

        isBold = font.isBold();
        isItalic = font.isItalic();

        int numGlyphs = font.getNumGlyphs();
        int glyphIndex = 0;
        int characterCode = 0;
    
        glyphTable = new FSGlyph[numGlyphs];
    
        /*
         * Run through all the unicode character codes looking for a corresponding glyph.
         */
        while ((glyphIndex < numGlyphs) && (characterCode < 65535))
        {
            char currentChar = (char) characterCode;
    
            if (font.canDisplay(currentChar))
            {
                GlyphVector glyphVector = font.createGlyphVector(fontContext,
                       new char[] { currentChar });
    
                Shape outline = glyphVector.getGlyphOutline(0);
                int advance = (int) (glyphVector.getGlyphMetrics(0).getAdvance());
    
                characterTable[currentChar] = (short)glyphIndex;
                glyphTable[glyphIndex] = new FSGlyph(convertShape(outline), new FSBounds(0, 0, 0, 0));
                glyphTable[glyphIndex].advance = advance;
    
                if (font.hasUniformLineMetrics() == false)
                {
                    LineMetrics lineMetrics = font.getLineMetrics(new char[]
                             { (char) currentChar }, 0, 1, fontContext);
    
                       ascent = 0; // Math.max(lineMetrics.getAscent() * 20, ascent);
                       descent = 0; // Math.max(lineMetrics.getDescent() * 20, descent);
                       leading = 0; // Math.max(lineMetrics.getLeading() * 20, leading);
                }
           }
           else
           {
                GlyphVector glyphVector = font.createGlyphVector(fontContext, new char[] { (char)missingGlyph });
            
                Shape outline = glyphVector.getGlyphOutline(0);
                int advance = (int) (glyphVector.getGlyphMetrics(0).getAdvance());
    
                characterTable[currentChar] = (short)glyphIndex;
                glyphTable[glyphIndex] = new FSGlyph(convertShape(outline), new FSBounds(0, 0, 0, 0));
                glyphTable[glyphIndex].advance = advance;
    
                if (font.hasUniformLineMetrics() == false)
                {
                    LineMetrics lineMetrics = font.getLineMetrics(new char[]
                             {(char)currentChar }, 0, 1, fontContext);
    
                    ascent = 0; // Math.max(lineMetrics.getAscent() * 20, ascent);
                    descent = 0; // Math.max(lineMetrics.getDescent() * 20, descent);
                    leading = 0; // Math.max(lineMetrics.getLeading() * 20, leading);
                }
           }
           glyphIndex++;
           characterCode++;
       }
       orderTable[0] = (short)missingGlyph;
    }
    private Rectangle2D transformToEMSquare(Font font, FontRenderContext fontContext)
    {
        int numGlyphs = font.getNumGlyphs();
        int characterCode = 0;
        int glyphIndex = 0;

        double x = 0.0;
        double y = 0.0;
        double w = 0.0;
        double h = 0.0;

        /*
         * Scan through all the glyphs looking for glyphs that will fall outside 
         * the left or bottom side of the EM Square once the glyph has been scaled.
         */
        while ((glyphIndex < numGlyphs) && (characterCode < 65535)) 
        {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -