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

📄 tagwriter.java

📁 java和flash混合编程
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        protected boolean initialStyles = false;
        
        protected int fill0Index = -1;
        protected int fill1Index = -1;
        protected int lineIndex  = -1;
        
        protected int[] moveXY;
        protected Vector lineStyles = new Vector();
        protected Vector fillStyles = new Vector();
        
        protected int fillBits;
        protected int lineBits;
        
        protected int    glyphCount = 0;
        protected Vector glyphByteArrays;
        
        protected OutStream out;
        protected TagWriter writer;
        protected ByteArrayOutputStream bout;
        
        /**
         * For shapes (other than glyphs)
         */
        public SWFShapeImpl( TagWriter writer, boolean hasAlpha, boolean hasStyle )
        {
            this.hasAlpha  = hasAlpha;
            this.hasStyle  = hasStyle;
            this.writer    = writer;
            out = writer.getOutStream();
        }

        /**
         * For glyphs
         */
        public SWFShapeImpl( TagWriter writer, int glyphCount ) 
        {
            this( writer, false, false );
            this.glyphCount = glyphCount;
            bout = new ByteArrayOutputStream();
            out  = new OutStream( bout );
            glyphByteArrays = new Vector();
            
            fill1Index = 1;                
            lineIndex  = 0;            
        }

       
        public void done() throws IOException
        {
            if( ! initialStyles )
            {
                writeInitialStyles();
                initialStyles = true;
            }
            
            out.writeUBits( 6, 0 );  //end record            
            out.flushBits();
         
            if( bout != null && glyphCount > 0 ) //capturing bytes internally
            {
                byte[] glyphBytes = bout.toByteArray();
                glyphByteArrays.addElement( glyphBytes );
            }
            
            if( glyphCount > 1 )
            {
                bout = new ByteArrayOutputStream();
                out  = new OutStream( bout );
                glyphCount--;
                
                fill1Index = 1;
                lineIndex  = 0;
                outstandingChanges = true;
                initialStyles = false;
            }
            else
            {
                if( bout != null ) finishFont();
                writer.completeTag();
            }
        }
    
        protected void finishFont() throws IOException
        {
            out = writer.getOutStream();
            
            int glyphCount = glyphByteArrays.size();
            
            //--Write first shape offset
            int offset = glyphCount * 2;
            out.writeUI16( offset );
        
            //--Write subsequent shape offsets
            for( int i = 0; i < glyphCount - 1; i++ )
            {
                offset += ((byte[])glyphByteArrays.elementAt(i)).length;
                out.writeUI16( offset );        
            }

            //--Write shapes        
            for( int i = 0; i < glyphCount; i++ )
            {
                out.write( (byte[])glyphByteArrays.elementAt(i) );
            }                            
        }
        
        public void setFillStyle0( int styleIndex ) throws IOException
        {
            fill0Index = styleIndex;
            outstandingChanges = true;
        }
    
        public void setFillStyle1( int styleIndex ) throws IOException
        {
            fill1Index = styleIndex;
            outstandingChanges = true;
        }
    
        public void setLineStyle( int styleIndex ) throws IOException
        {
            lineIndex = styleIndex;
            outstandingChanges = true;
        }

        public void defineFillStyle( Color color ) throws IOException
        {
            fillStyles.addElement( new FillStyle( color ) );
            outstandingChanges = true;
        }

        public void defineFillStyle( Matrix matrix, int[] ratios,
                                     Color[] colors, boolean radial )
            throws IOException
        {
            fillStyles.addElement( new FillStyle( matrix, ratios, colors, radial ) );
            outstandingChanges = true;
        }

        public void defineFillStyle( int bitmapId, Matrix matrix, boolean clipped )
            throws IOException
        {
            fillStyles.addElement( new FillStyle( bitmapId, matrix, clipped ) );
            outstandingChanges = true;
        }
    
        public void defineLineStyle( int width, Color color ) throws IOException
        {
            lineStyles.addElement( new LineStyle( width, color ) );
            outstandingChanges = true;            
        }

        public void line( int deltaX, int deltaY ) throws IOException
        {
            if( outstandingChanges ) flushChangeRecords();

            int numBits = OutStream.determineSignedBitSize( deltaX );
            int dyBits  = OutStream.determineSignedBitSize( deltaY );
            
            if( dyBits  > numBits ) numBits = dyBits;            
            if( numBits < 2 ) numBits = 2;
            
            out.writeUBits( 2, 3 );  //11b = line record
            
            out.writeUBits( 4, numBits - 2 );
            
            if( deltaX != 0 && deltaY != 0 ) //general line
            {
                out.writeUBits( 1, 1 );
                out.writeSBits( numBits, deltaX );
                out.writeSBits( numBits, deltaY );
            }
            else //horz or vert line
            {
                out.writeUBits( 1, 0 );
                
                if( deltaY != 0 ) //vert line
                {
                    out.writeUBits( 1, 1 );
                    out.writeSBits( numBits, deltaY );
                }
                else ///horz line
                {
                    out.writeUBits( 1, 0 );
                    out.writeSBits( numBits, deltaX );
                }
            }        
        }
    
        public void curve( int cx, int cy, int dx, int dy ) throws IOException
        {
            if( outstandingChanges ) flushChangeRecords();
            
            int numBits = OutStream.determineSignedBitSize( cx );
            int dyBits  = OutStream.determineSignedBitSize( cy );
            int adxBits = OutStream.determineSignedBitSize( dx );
            int adyBits = OutStream.determineSignedBitSize( dy );
            
            if( dyBits  > numBits ) numBits = dyBits;
            if( adxBits > numBits ) numBits = adxBits;
            if( adyBits > numBits ) numBits = adyBits;
            
            if( numBits < 2 ) numBits = 2;
            
            out.writeUBits( 2, 2 );  //10b = curve record
            
            out.writeUBits( 4, numBits - 2 );
            
            out.writeSBits( numBits, cx );
            out.writeSBits( numBits, cy );
            out.writeSBits( numBits, dx );
            out.writeSBits( numBits, dy );
        }
    
        public void move( int x, int y ) throws IOException
        {
            moveXY = new int[] { x, y };
            outstandingChanges = true;
        }
        
        protected void flushChangeRecords() throws IOException
        {            
            if( ! initialStyles )
            {
                writeInitialStyles();
                initialStyles = true;
            }

            writeChangeRecord();
            
            outstandingChanges = false;            
        }
        
        protected void writeInitialStyles() throws IOException
        {   
            out.flushBits();       
        
            fillBits = OutStream.determineUnsignedBitSize( fillStyles.size() );
            lineBits = OutStream.determineUnsignedBitSize( lineStyles.size() );

            //--For fonts - the fillstyle bits must be one
            if( ! hasStyle )
            {
                fillBits = 1;
            }
            else
            {
                writeStyles( fillStyles );
                writeStyles( lineStyles );        
                out.flushBits(); 
            }
       
                        
            out.writeUBits( 4, fillBits );
            out.writeUBits( 4, lineBits );        
        }
        
        protected void writeChangeRecord() throws IOException
        {
            boolean hasNewStyles = hasStyle &&
                       ( fillStyles.size() > 0 || lineStyles.size() > 0 );
            
            boolean hasMoveTo = ( moveXY != null );
            boolean hasFillStyle0 = fill0Index >= 0;
            boolean hasFillStyle1 = fill1Index >= 0;
            boolean hasLineStyle  = lineIndex >= 0;
            
            if( (! hasStyle) && hasFillStyle0 ) hasFillStyle1 = false;
            
            if( hasNewStyles )
            {
                out.writeUBits( 1, 0 );  //non-edge record
                out.writeUBits( 1, 1 );  //defines new styles
                out.writeUBits( 1, 1 );
                out.writeUBits( 1, 1 );
                out.writeUBits( 1, 1 );
                out.writeUBits( 1, 1 );
                
                //--Clear existing styles
                writeMoveXY( 0, 0 );
                out.writeUBits( fillBits, 0 );
                out.writeUBits( fillBits, 0 );
                out.writeUBits( lineBits, 0 );
                
                if( fill0Index == 0 ) fill0Index = -1;
                if( fill1Index == 0 ) fill1Index = -1;
                if( lineIndex  == 0 ) lineIndex  = -1;
                
                fillBits = OutStream.determineUnsignedBitSize( fillStyles.size() );
                lineBits = OutStream.determineUnsignedBitSize( lineStyles.size() );

                writeStyles( fillStyles );
                writeStyles( lineStyles );
                
                out.writeUBits( 4, fillBits );
                out.writeUBits( 4, lineBits ); 
                
                writeChangeRecord();
                return;
            }               
            
            if( hasFillStyle0 || hasFillStyle1 || hasLineStyle || hasMoveTo )
            {
                out.writeUBits( 1, 0 );  //non-edge record
                out.writeUBits( 1, 0 );
                out.writeUBits( 1, hasLineStyle  ? 1 : 0 );
                out.writeUBits( 1, hasFillStyle1 ? 1 : 0 );
                out.writeUBits( 1, hasFillStyle0 ? 1 : 0 );
                out.writeUBits( 1, hasMoveTo     ? 1 : 0 );
            
                if( hasMoveTo )
                {
                    int moveX = moveXY[0];
                    int moveY = moveXY[1];
                    writeMoveXY( moveX, moveY );
                }
                    
                if( hasFillStyle0 )
                {
                    out.writeUBits( fillBits, fill0Index );
                }
                    
                if( hasFillStyle1 )
                {
                    out.writeUBits( fillBits, fill1Index );
                }
                    
                if( hasLineStyle )
                {
                    out.writeUBits( lineBits, lineIndex );
                }

                moveXY = null;
                fill0Index = -1;
                fill1Index = -1;
                lineIndex  = -1;            
            }
        }
        
        protected void writeMoveXY( int moveX, int moveY ) throws IOException
        {
            int moveBits  = OutStream.determineSignedBitSize( moveX );
            int moveYBits = OutStream.determineSignedBitSize( moveY );
            if( moveYBits > moveBits ) moveBits = moveYBits;              
                    
            out.writeUBits( 5,  moveBits );
            out.writeSBits( moveBits, moveX );
            out.writeSBits( moveBits, moveY );            
        }
        
        protected void writeStyles( Vector styles ) throws IOException
        {    
            int numStyles = (styles != null) ? styles.size() : 0;
            
            if( numStyles < 0xff )
            {
                out.writeUI8( numStyles );

⌨️ 快捷键说明

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