JavaScript で DefineShape を分解してみる


  • 肝になるコード
var SWFSHAPERECORDS = function(bs, tag_code, currentNumBits) {
    if (bs) {
	var first5Bits = bs.getUIBits(5);
	this.TypeFlag = (first5Bits >> 4) & 1;
	if (this.TypeFlag) { // Edge records
	    this.StraightFlag = (first5Bits >> 3) & 1;
	    var numBits = ((first5Bits & 0x07) << 1) | bs.getUIBit();
	    this.NumBits = numBits;
	    if (this.StraightFlag) { // StraightEdgeRecord
		this.GeneralLineFlag = bs.getUIBit();
		if (this.GeneralLineFlag) {
		    this.DeltaX = bs.getSIBits(numBits + 2);
		    this.DeltaY = bs.getSIBits(numBits + 2);
		} else {
		    this.VertLineFlag = bs.getUIBit();
		    if (this.VertLineFlag) {
			this.DeltaX = 0;
			this.DeltaY = bs.getSIBits(numBits + 2);
		    } else {
			this.DeltaX = bs.getSIBits(numBits + 2);
			this.DeltaY = 0;
		    }
		}
	    } else { // CurvedEdgeRecord
		this.ControlDeltaX = bs.getUIBits(numBits + 2);
		this.ControlDeltaY = bs.getUIBits(numBits + 2);
		this.AnchoeDeltaX = bs.getUIBits(numBits + 2);
		this.AnchoeDeltaY = bs.getUIBits(numBits + 2);
	    }
	} else if (first5Bits) { // StypeChangeRecord
	    this.StateNewStyles  = (first5Bits >> 3) & 1;
	    this.StateLineStyle  = (first5Bits >> 2) & 1;
	    this.StateFillStyle1 = (first5Bits >> 1) & 1;
	    this.StateFillStyle0 =  first5Bits       & 1;
	    this.StateMoveTo = bs.getUIBit();
	    if (this.StateMoveTo) {
		moveBits = bs.getUIBits(5);
		this.MoveBits = moveBits;
		this.MoveX = bs.getSIBits(moveBits); // MoveDeltaX
		this.MoveY = bs.getSIBits(moveBits); // MoveDeltaY
	    }
	    if (this.StateFillStyle0) {
		this.FillStyle0 = bs.getUIBits(currentNumBits.FillBits);
	    }
	    if (this.StateFillStyle1) {
		this.FillStyle0 = bs.getUIBits(currentNumBits.FillBits);
	    }
	    ; 
	    if (this.StateLineStyle) {
		this.LineStyle = bs.getUIBits(currentNumBits.LineBits);
	    }
	    ; 
	    if (this.StateNewStyles) {
		this.FillStyles = new SWFFILLSTYLEARRAY(bs, tag_code);
		this.LineStyles = new SWFLINESTYLEARRAY(bs, tag_code);
		var numBits = bs.getUI8();
		currentNumBits.FillBits = this.NumFillBits = numBits >> 4;
		currentNumBits.LineBits = this.NumLineBits = numBits & 0x0f;
	    }
	} else { // EndShapeRecord
	    this.EndOfShape = 0;
	}
    }
}