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

📄 tagparser.java

📁 java和flash混合编程
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        
    protected void parsePlaceObject( InStream in, int length ) throws IOException
    {
        mTagtypes.tagPlaceObject( 
            in.readUI16(),  //char id
            in.readUI16(),  //depth
            new Matrix( in ),
            ( in.getBytesRead() < length ) ? new AlphaTransform( in ) : null );
    }
    
    protected void parseDoAction( InStream in ) throws IOException 
    {
        SWFActions actions = mTagtypes.tagDoAction();
        
        if( actions == null ) return;
        
        SWFActionBlock block = actions.start( 0 );  //no conditions
        ActionParser parser = new ActionParser( block, mFlashVersion );
        parser.parse( in );
        actions.done();
    }

	protected void parseDoInitAction( InStream in ) throws IOException 
	{
		int spriteId = in.readUI16();
		SWFActions actions = mTagtypes.tagDoInitAction( spriteId );
        
		if( actions == null ) return;
        
		SWFActionBlock block = actions.start( 0 );  //no conditions
		ActionParser parser = new ActionParser( block, mFlashVersion );
		parser.parse( in );
		actions.done();
	}
    
    protected void parseDefineShape( int type, InStream in ) throws IOException
    {
        int  id    = in.readUI16();
        Rect rect  = new Rect( in );
        
        SWFShape shape = null;
        
        switch( type )
        {
            case TAG_DEFINESHAPE : shape = mTagtypes.tagDefineShape( id, rect ); break;
            case TAG_DEFINESHAPE2: shape = mTagtypes.tagDefineShape2( id, rect ); break;
            case TAG_DEFINESHAPE3: shape = mTagtypes.tagDefineShape3( id, rect ); break;
            default: break;
        }
        
        if( shape == null ) return;
        
        parseShape( in, shape,
                    true /*has style*/, 
                    type == TAG_DEFINESHAPE3 /*has alpha*/ );        
    }
    
    protected void parseShape( InStream in, SWFVectors vectors,
                               boolean hasStyle, boolean hasAlpha )
        throws IOException
    {       
        SWFShape shape = (vectors instanceof SWFShape) ?
                            (SWFShape)vectors : 
                            null;
        
        in.synchBits();
        
        if( hasStyle ) parseStyles( in, shape, hasAlpha );
        
        in.synchBits();
        
        int[] numFillBits = new int[] { (int)in.readUBits(4) };
        int[] numLineBits = new int[] { (int)in.readUBits(4) };  
        
        //--Read the shape records
        while( true )
        {
            int type = (int)in.readUBits(1);
            
            if( type == 1 ) // Edge shape-record
            {
                boolean isCurved = in.readUBits(1) == 0L;
                    
                if( isCurved )  //curve
                {
                    int numBits  = ((int)in.readUBits(4)) + 2;

                    int cx = in.readSBits( numBits );
                    int cy = in.readSBits( numBits );
                    int dx = in.readSBits( numBits );
                    int dy = in.readSBits( numBits );                    
                    
                    vectors.curve( cx, cy, dx, dy );
                }
                else  //line
                {
                    int numBits  = ((int)in.readUBits(4)) + 2;
            
                    boolean generalLine = in.readUBits(1) == 1;
                       
                    int dx = 0;
                    int dy = 0;
                    
                    if( generalLine )
                    {
                        dx = in.readSBits( numBits );
                        dy = in.readSBits( numBits );
                    }
                    else // a non-general line
                    {
                        boolean vertLine = in.readUBits(1) == 1;
                                    
                        if( vertLine )
                        {
                            dy = in.readSBits( numBits );
                        }
                        else //horizontal line
                        {
                            dx = in.readSBits( numBits );
                        }
                    }
                    
                    vectors.line( dx, dy );
                }                
            }
            else //End of records or Change Record
            {
                int flags = (int)in.readUBits(5);
                
                if( flags == 0 ) break; //end of records
                
                parseChangeRecord( in, flags, vectors, shape, hasAlpha,
                                   numFillBits, numLineBits );                
            }            
        }      
        
        vectors.done();
    }
    
    protected void parseChangeRecord( InStream in, int flags, SWFVectors vectors,
                                      SWFShape shape, boolean hasAlpha,
                                      int[] numFillBits, int[] numLineBits )
        throws IOException
    {
        //System.out.println( "In Change --> " + Integer.toBinaryString( flags ));
        
        boolean hasNewStyles  = (flags & 0x10) != 0;
        boolean hasLineStyle  = (flags & 0x08) != 0;
        boolean hasFillStyle1 = (flags & 0x04) != 0; //note reverse order
        boolean hasFillStyle0 = (flags & 0x02) != 0; //note reverse order
        boolean hasMoveTo     = (flags & 0x01) != 0;
        
        if( hasMoveTo )
        {
            int moveBits = (int)in.readUBits(5);
            int moveX = in.readSBits( moveBits );
            int moveY = in.readSBits( moveBits );
            
            //System.out.println( "X=" + moveX + ", Y=" + moveY );
            
            vectors.move( moveX, moveY );
        }
                
        if( hasFillStyle0 )
        {
            int fillStyle0 = (int)in.readUBits( numFillBits[0] );

            //System.out.println( "fill0=" + fillStyle0 );
            
            if( shape != null ) shape.setFillStyle0( fillStyle0 );
        }
                
        if( hasFillStyle1 )
        {
            int fillStyle1 = (int)in.readUBits( numFillBits[0] );

            //System.out.println( "fill1=" + fillStyle1 );
            
            if( shape != null ) shape.setFillStyle1( fillStyle1 );
        }
                
        if( hasLineStyle )
        {
            int lineStyle = (int)in.readUBits( numLineBits[0] );

            //System.out.println( "line=" + lineStyle ); 

            if( shape != null ) shape.setLineStyle( lineStyle );
        }
                                
        if( hasNewStyles )
        {
            parseStyles( in, shape, hasAlpha );
                
            numFillBits[0] = (int)in.readUBits(4);
            numLineBits[0] = (int)in.readUBits(4);  
        }        
    }
    
    protected void parseStyles( InStream in, SWFShape shape, boolean hasAlpha ) 
        throws IOException
    {
        int numFillStyles = in.readUI8();
        if( numFillStyles == 0xff )  //larger number format
        {
            numFillStyles = in.readUI16();
        }
            
        for( int i = 0; i < numFillStyles; i++ )
        {
            parseFillStyle( in, shape, hasAlpha );
        }            

        int numLineStyles = in.readUI8();
        if( numLineStyles == 0xff )  //larger number format
        {
            numLineStyles = in.readUI16();
        }
               
        for( int i = 0; i < numLineStyles; i++ )
        {
            parseLineStyle( in, shape, hasAlpha );
        }
    }
    
    public void parseLineStyle( InStream in, SWFShape shape, boolean hasAlpha )
        throws IOException 
    {
        int width = in.readUI16();
        Color color = hasAlpha ? new AlphaColor(in) : new Color(in);

        if( shape != null ) shape.defineLineStyle( width, color );
    }

    public void parseFillStyle( InStream in, SWFShape shape, boolean hasAlpha )
        throws IOException 
    {
        int fillType = in.readUI8();
        
        if( fillType == FILL_SOLID )
        {
            Color color = hasAlpha ? new AlphaColor(in) : new Color(in);
            
            if( shape != null ) shape.defineFillStyle( color );
        }
        else if( fillType == FILL_LINEAR_GRADIENT 
              || fillType == FILL_RADIAL_GRADIENT )
        {
            Matrix matrix   = new Matrix( in );            
            
            int numRatios = in.readUI8();
            
            int[]   ratios = new int[ numRatios ];
            Color[] colors = new Color[ numRatios ];
            
            for( int i = 0; i < numRatios; i++ )
            {
                ratios[i] = in.readUI8();
                colors[i] = hasAlpha ? new AlphaColor(in) : new Color(in);
            }            
            
            if( shape != null )
            {
                shape.defineFillStyle( matrix, ratios, colors, 
                                       fillType == FILL_RADIAL_GRADIENT );
            }                        
        }
        else if( fillType == FILL_TILED_BITMAP 
              || fillType == FILL_CLIPPED_BITMAP )
        {
            int bitmapId = in.readUI16();
            Matrix matrix = new Matrix( in );
            
            if( shape != null )
            {
                shape.defineFillStyle( bitmapId, matrix, 
                                       fillType == FILL_CLIPPED_BITMAP );
            }            
        }                    
    }
    
    public static void main( String[] args ) throws IOException
    {
        FileInputStream  in  = new FileInputStream ( args[0] );
        FileOutputStream out = new FileOutputStream( args[1] );
        
        SWFWriter writer = new SWFWriter( out );
        TagWriter tagwtr = new TagWriter( writer );
        
        TagParser parser = new TagParser( tagwtr );
        SWFReader reader = new SWFReader( parser, in );
        
        reader.readFile();
        out.flush();
        out.close();
        in.close();
    }    
}

⌨️ 快捷键说明

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