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

📄 tagwriter.java

📁 java和flash混合编程
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        writeBitsLossless( id, format, width, height, colors, imageData, false );
    }
    
    /**
     * SWFTagTypes interface
     */ 
    public void tagDefineBitsLossless2( int id, int format, int width, int height,
                                        Color[] colors, byte[] imageData )
        throws IOException
    {
        writeBitsLossless( id, format, width, height, colors, imageData, true );
    }
    
    
    public void writeBitsLossless( int id, int format, int width, int height,
                                   Color[] colors, byte[] imageData, boolean hasAlpha )
        throws IOException
    {
        startTag( hasAlpha ? TAG_DEFINEBITSLOSSLESS2 : TAG_DEFINEBITSLOSSLESS,
                  id, true );

        out.writeUI8 ( format );
        out.writeUI16( width  );
        out.writeUI16( height );
        
        switch( format )
        {
            case BITMAP_FORMAT_8_BIT:  out.writeUI8 ( colors.length - 1 ); break;
            case BITMAP_FORMAT_16_BIT: out.writeUI16( colors.length - 1 ); break;
            case BITMAP_FORMAT_32_BIT: break;
            default: throw new IOException( "unknown bitmap format: " + format );
        }                
       
        //--zip up the colors and the bitmap data
        DeflaterOutputStream deflater = new DeflaterOutputStream( bytes );
        OutStream zipOut = new OutStream( deflater );
                
        if( format == BITMAP_FORMAT_8_BIT || format == BITMAP_FORMAT_16_BIT )
        {
            for( int i = 0; i < colors.length; i++ )
            {
                if( hasAlpha ) colors[i].writeWithAlpha( zipOut ); 
                else           colors[i].writeRGB( zipOut );
            }
        }
        zipOut.write( imageData );        
        zipOut.flush();
        deflater.finish();
        
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */ 
    public void tagDefineBitsJPEG2( int id, InputStream jpegImage ) throws IOException
    {
        startTag( TAG_DEFINEBITSJPEG2, id, true );
        
        //--Write stream terminator/header
        out.writeUI8( 0xff );
        out.writeUI8( 0xd9 );
        out.writeUI8( 0xff );
        out.writeUI8( 0xd8 );
        
        int read = 0;
        byte[] bytes = new byte[10000];
        
        while( (read = jpegImage.read( bytes )) >= 0 )
        {
            out.write( bytes, 0, read );
        }
        
        jpegImage.close();        
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */ 
    public SWFShape tagDefineMorphShape( int id, Rect startBounds, Rect endBounds ) 
        throws IOException
    {
        startTag( TAG_DEFINEMORPHSHAPE, id, true );
        startBounds.write( out );
        endBounds  .write( out );
        return new MorphShapeImpl( this );
    }
    
    //-----------------------------------------------------------------------

        
    protected static class ButtonActionWriter extends ActionWriter
    {
        //offsets is a vector of int[]{ offsetPtr, offsetValue }
        protected Vector offsets = new Vector();
        protected int lastPtr;
        protected OutStream tagout;
        
        public ButtonActionWriter( TagWriter tagWriter, 
                                   int flashVersion, 
                                   Vector buttonRecs )
            throws IOException 
        {
            super( tagWriter, flashVersion );

            tagout = tagWriter.getOutStream();
            
            //--save ptr to first offset location
            lastPtr = (int)tagout.getCount();
            
            tagout.writeUI16( 0 ); //will be calculated later
            
            //--write the button records
            ButtonRecord2.write( tagout, buttonRecs );
        }
        
        public SWFActionBlock start( int conditions ) throws IOException
        {
            //--save ptr to offset location and offset value
            int ptr    = (int)tagout.getCount();
            int offset = ptr - lastPtr;
            
            offsets.addElement( new int[] { lastPtr, offset } );
            
            lastPtr = ptr;
            
            tagout.writeUI16( 0 );  //will be calculated later
            tagout.writeUI16( conditions );            
        
            return super.start( conditions );
        }      
                
        public void done() throws IOException
        {
            //--save last offset
            offsets.addElement( new int[] { lastPtr, 0 } );
            
            tagout.flush();
            byte[] contents = mTagWriter.bytes.toByteArray();
        
            mTagWriter.out = null;
            mTagWriter.bytes = null;
        
            //--fix the offsets
            for( Enumeration e = offsets.elements(); e.hasMoreElements(); )
            {
                int[] offInfo = (int[])e.nextElement();
                int ptr = offInfo[0];
                int off = offInfo[1];
                
                byte[] offbytes = OutStream.uintTo2Bytes( off );
                contents[ ptr     ] = offbytes[0];
                contents[ ptr + 1 ] = offbytes[1];
            }
            
            mTagWriter.mTags.tag( mTagWriter.tagType, true, contents );
        }                
    }
    
    protected SWFText defineText( Rect bounds, Matrix matrix, boolean hasAlpha ) 
        throws IOException 
    {
        bounds.write( out );
        matrix.write( out );

        return new SWFTextImpl( hasAlpha );
    }
    
    protected class SWFTextImpl implements SWFText 
    {
        protected boolean hasAlpha;

        protected int maxGlyphIndex = 0;
        protected int maxAdvance    = 0;
        
        // Text records are stored as
        // Object[] { int[]{ font, height }, int[]{ x }, int[]{ y }, Color }, or
        // Object[] { int[] glyphs, int[] advances }
        protected Vector recs = new Vector();
        protected Object[] currentStyleRecord = null;
        
        
        public SWFTextImpl( boolean hasAlpha )
        {
            this.hasAlpha = hasAlpha;
        }
        
        protected Object[] getCurrentStyle()
        {
            if( currentStyleRecord == null )
            {
                currentStyleRecord = new Object[4];
                recs.addElement( currentStyleRecord );
            }

            return currentStyleRecord;
        }
        
        /**
         * SWFText interface
         */
        public void font( int fontId, int textHeight ) throws IOException
        {
            getCurrentStyle()[0] = new int[] { fontId, textHeight };
        }
    
        /**
         * SWFText interface
         */
        public void color( Color color ) throws IOException
        {
            getCurrentStyle()[3] = color;
        }
    
        /**
         * SWFText interface
         */
        public void setX( int x ) throws IOException
        {
            getCurrentStyle()[1] = new int[] { x };
        }

        /**
         * SWFText interface
         */
        public void setY( int y ) throws IOException
        {
            getCurrentStyle()[2] = new int[] { y };
        }
    
        /**
         * SWFText interface
         */
        public void text( int[] glyphIndices, int[] glyphAdvances ) throws IOException
        {
            currentStyleRecord = null;
            recs.addElement( new Object[] { glyphIndices, glyphAdvances } );
            
            for( int i = 0; i < glyphIndices.length; i++ )
            {
                if( glyphIndices[i] > maxGlyphIndex ) maxGlyphIndex = glyphIndices[i];
                if( glyphAdvances[i] > maxAdvance   ) maxAdvance    = glyphAdvances[i];
            }
        }
    
        /**
         * SWFText interface
         */
        public void done() throws IOException
        {
            int glyphBits   = OutStream.determineUnsignedBitSize( maxGlyphIndex );
            int advanceBits = OutStream.determineSignedBitSize  ( maxAdvance );               
               
            out.writeUI8( glyphBits );
            out.writeUI8( advanceBits );

            for( Enumeration e = recs.elements(); e.hasMoreElements(); )
            {
                Object[] rec = (Object[])e.nextElement();

                if( rec.length == 4 ) //style record
                {
                    boolean hasFont  = rec[0] != null;
                    boolean hasX     = rec[1] != null;
                    boolean hasY     = rec[2] != null;
                    boolean hasColor = rec[3] != null;
                    
                    int flags = 0x80;
                    if( hasFont  ) flags |= TEXT_HAS_FONT;
                    if( hasX     ) flags |= TEXT_HAS_XOFFSET;
                    if( hasY     ) flags |= TEXT_HAS_YOFFSET;
                    if( hasColor ) flags |= TEXT_HAS_COLOR;

                    out.writeUI8( flags );
            
                    if( hasFont )
                    {
                        out.writeUI16( ((int[])rec[0])[0] ); //fontId
                    }
            
                    if( hasColor )
                    {
                        Color color = (Color)rec[3];
                        
                        if( hasAlpha ) color.writeWithAlpha( out );
                        else           color.writeRGB( out );
                    }

                    if( hasX )
                    {
                        int xOffset = ((int[])rec[1])[0];
                        out.writeSI16( (short)xOffset );
                    }

                    if( hasY ) //x & y are in reverse order from flag bits
                    {
                        int yOffset = ((int[])rec[2])[0];
                        out.writeSI16( (short)yOffset );
                    }

                    if( hasFont )
                    {
                        out.writeUI16( ((int[])rec[0])[1] ); //textHeight
                    }                        
                }
                else //glyph record
                {
                    int[] glyphs   = (int[])rec[0];
                    int[] advances = (int[])rec[1];
                    
                    out.writeUI8( glyphs.length );
            
                    for( int i = 0; i < glyphs.length; i++ )
                    {
                        out.writeUBits( glyphBits,   glyphs[i]   );
                        out.writeSBits( advanceBits, advances[i] );
                    }                                
                }
            }

            out.writeUI8( 0 );  //record terminator

            completeTag();
        }        
    }
    
    
    protected class SpriteTags implements SWFTags 
    {
        protected int frameCount = 0;
        
        public void tag( int tagType2, boolean longTag2, 
                         byte[] contents ) throws IOException
        {
            int length = (contents != null ) ? contents.length : 0;
            longTag2 = ( length > 62 ) || longTag2;
                
            int hdr = ( tagType2 << 6 ) + ( longTag2 ? 0x3f : length );

            out.writeUI16( hdr );
                            
            if( longTag2 ) out.writeUI32( length );        
                
            if( contents != null ) out.write( contents );
                
            if( tagType2 == SWFConstants.TAG_SHOWFRAME ) frameCount++;
            
            if( tagType2 == SWFConstants.TAG_END )
            {
                out.flush();
                contents = bytes.toByteArray();
        
                out = null;
                bytes = null;
        
                byte[] fc = OutStream.uintTo2Bytes( frameCount );
                contents[2] = fc[0];
                contents[3] = fc[1];
                
                mTags.tag( tagType, longTag, contents );                  
            }
        }
        
        public void header( int version, long length,
                            int twipsWidth, int twipsHeight,
                            int frameRate, int frameCount ) throws IOException
        {
        }
    }    
    
    protected void startShape( int tagType, int id, Rect outline ) throws IOException
    {
        startTag( tagType, id, true );
        outline.write( out );
    }
    
    /** 
     * Implementation of the SWFShape interface
     */
    protected static class SWFShapeImpl implements SWFShape 
    {
        protected boolean hasAlpha;
        protected boolean hasStyle;
        protected boolean outstandingChanges = true;

⌨️ 快捷键说明

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