UNPKG

@awayjs/graphics

Version:
245 lines (244 loc) 12 kB
import { DataBuffer } from './DataBuffer'; import { assert, ensureTypedArrayCapacity } from './utilities'; var PlainObjectShapeData = /** @class */ (function () { function PlainObjectShapeData(commands, commandsPosition, coordinates, morphCoordinates, coordinatesPosition, styles, stylesLength, morphStyles, morphStylesLength, hasFills, hasLines) { this.commands = commands; this.commandsPosition = commandsPosition; this.coordinates = coordinates; this.morphCoordinates = morphCoordinates; this.coordinatesPosition = coordinatesPosition; this.styles = styles; this.stylesLength = stylesLength; this.morphStyles = morphStyles; this.morphStylesLength = morphStylesLength; this.hasFills = hasFills; this.hasLines = hasLines; } return PlainObjectShapeData; }()); export { PlainObjectShapeData }; var DefaultSize; (function (DefaultSize) { DefaultSize[DefaultSize["Commands"] = 32] = "Commands"; DefaultSize[DefaultSize["Coordinates"] = 128] = "Coordinates"; DefaultSize[DefaultSize["Styles"] = 16] = "Styles"; })(DefaultSize || (DefaultSize = {})); var ShapeData = /** @class */ (function () { function ShapeData(initialize) { if (initialize === void 0) { initialize = true; } if (initialize) { this.clear(); } } ShapeData.FromPlainObject = function (source) { var data = new ShapeData(false); data.commands = source.commands; data.coordinates = source.coordinates; data.morphCoordinates = source.morphCoordinates; data.commandsPosition = source.commandsPosition; data.coordinatesPosition = source.coordinatesPosition; data.styles = DataBuffer.FromArrayBuffer(source.styles, source.stylesLength); data.styles.endian = 'auto'; if (source.morphStyles) { data.morphStyles = DataBuffer.FromArrayBuffer(source.morphStyles, source.morphStylesLength); data.morphStyles.endian = 'auto'; } data.hasFills = source.hasFills; data.hasLines = source.hasLines; return data; }; ShapeData.prototype.moveTo = function (x, y) { this.ensurePathCapacities(1, 2); this.commands[this.commandsPosition++] = 9 /* PathCommand.MoveTo */; this.coordinates[this.coordinatesPosition++] = x; this.coordinates[this.coordinatesPosition++] = y; }; ShapeData.prototype.lineTo = function (x, y) { this.ensurePathCapacities(1, 2); this.commands[this.commandsPosition++] = 10 /* PathCommand.LineTo */; this.coordinates[this.coordinatesPosition++] = x; this.coordinates[this.coordinatesPosition++] = y; }; ShapeData.prototype.curveTo = function (controlX, controlY, anchorX, anchorY) { this.ensurePathCapacities(1, 4); this.commands[this.commandsPosition++] = 11 /* PathCommand.CurveTo */; this.coordinates[this.coordinatesPosition++] = controlX; this.coordinates[this.coordinatesPosition++] = controlY; this.coordinates[this.coordinatesPosition++] = anchorX; this.coordinates[this.coordinatesPosition++] = anchorY; }; ShapeData.prototype.cubicCurveTo = function (controlX1, controlY1, controlX2, controlY2, anchorX, anchorY) { this.ensurePathCapacities(1, 6); this.commands[this.commandsPosition++] = 12 /* PathCommand.CubicCurveTo */; this.coordinates[this.coordinatesPosition++] = controlX1; this.coordinates[this.coordinatesPosition++] = controlY1; this.coordinates[this.coordinatesPosition++] = controlX2; this.coordinates[this.coordinatesPosition++] = controlY2; this.coordinates[this.coordinatesPosition++] = anchorX; this.coordinates[this.coordinatesPosition++] = anchorY; }; ShapeData.prototype.beginFill = function (color) { this.ensurePathCapacities(1, 0); this.commands[this.commandsPosition++] = 1 /* PathCommand.BeginSolidFill */; this.styles.writeUnsignedInt(color); this.hasFills = true; }; ShapeData.prototype.writeMorphFill = function (color) { this.morphStyles.writeUnsignedInt(color); }; ShapeData.prototype.endFill = function () { this.ensurePathCapacities(1, 0); this.commands[this.commandsPosition++] = 4 /* PathCommand.EndFill */; }; ShapeData.prototype.endLine = function () { this.ensurePathCapacities(1, 0); this.commands[this.commandsPosition++] = 8 /* PathCommand.LineEnd */; }; ShapeData.prototype.lineStyle = function (thickness, color, pixelHinting, scaleMode, caps, joints, miterLimit) { assert(thickness === (thickness | 0), thickness >= 0 && thickness <= 0xff * 20); this.ensurePathCapacities(2, 0); this.commands[this.commandsPosition++] = 5 /* PathCommand.LineStyleSolid */; this.coordinates[this.coordinatesPosition++] = thickness; var styles = this.styles; styles.writeUnsignedInt(color); styles.writeBoolean(pixelHinting); styles.writeUnsignedByte(scaleMode); styles.writeUnsignedByte(caps); styles.writeUnsignedByte(joints); styles.writeUnsignedByte(miterLimit); this.hasLines = true; }; ShapeData.prototype.writeMorphLineStyle = function (thickness, color) { this.morphCoordinates[this.coordinatesPosition - 1] = thickness; this.morphStyles.writeUnsignedInt(color); }; /** * Bitmaps are specified the same for fills and strokes, so we only need to serialize them * once. The Parameter `pathCommand` is treated as the actual command to serialize, and must * be one of BeginBitmapFill and LineStyleBitmap. */ ShapeData.prototype.beginBitmap = function (pathCommand, bitmapId, matrix, repeat, smooth) { assert(pathCommand === 3 /* PathCommand.BeginBitmapFill */ || pathCommand === 7 /* PathCommand.LineStyleBitmap */); this.ensurePathCapacities(1, 0); this.commands[this.commandsPosition++] = pathCommand; var styles = this.styles; styles.writeUnsignedInt(bitmapId); this._writeStyleMatrix(matrix, false); styles.writeBoolean(repeat); styles.writeBoolean(smooth); this.hasFills = true; }; ShapeData.prototype.writeMorphBitmap = function (matrix) { this._writeStyleMatrix(matrix, true); }; /** * Gradients are specified the same for fills and strokes, so we only need to serialize them * once. The Parameter `pathCommand` is treated as the actual command to serialize, and must * be one of BeginGradientFill and LineStyleGradient. */ ShapeData.prototype.beginGradient = function (pathCommand, colors, ratios, gradientType, matrix, spread, interpolation, focalPointRatio) { assert(pathCommand === 2 /* PathCommand.BeginGradientFill */ || pathCommand === 6 /* PathCommand.LineStyleGradient */); this.ensurePathCapacities(1, 0); this.commands[this.commandsPosition++] = pathCommand; var styles = this.styles; styles.writeUnsignedByte(gradientType); assert(focalPointRatio === (focalPointRatio | 0)); styles.writeShort(focalPointRatio); this._writeStyleMatrix(matrix, false); var colorStops = colors.length; styles.writeByte(colorStops); for (var i = 0; i < colorStops; i++) { // Ratio must be valid, otherwise we'd have bailed above. styles.writeUnsignedByte(ratios[i]); // Colors are coerced to uint32, with the highest byte stripped. styles.writeUnsignedInt(colors[i]); } styles.writeUnsignedByte(spread); styles.writeUnsignedByte(interpolation); this.hasFills = true; }; ShapeData.prototype.writeMorphGradient = function (colors, ratios, matrix) { this._writeStyleMatrix(matrix, true); var styles = this.morphStyles; for (var i = 0; i < colors.length; i++) { // Ratio must be valid, otherwise we'd have bailed above. styles.writeUnsignedByte(ratios[i]); // Colors are coerced to uint32, with the highest byte stripped. styles.writeUnsignedInt(colors[i]); } }; ShapeData.prototype.writeCommandAndCoordinates = function (command, x, y) { this.ensurePathCapacities(1, 2); this.commands[this.commandsPosition++] = command; this.coordinates[this.coordinatesPosition++] = x; this.coordinates[this.coordinatesPosition++] = y; }; ShapeData.prototype.writeCoordinates = function (x, y) { this.ensurePathCapacities(0, 2); this.coordinates[this.coordinatesPosition++] = x; this.coordinates[this.coordinatesPosition++] = y; }; ShapeData.prototype.writeMorphCoordinates = function (x, y) { this.morphCoordinates = ensureTypedArrayCapacity(this.morphCoordinates, this.coordinatesPosition); this.morphCoordinates[this.coordinatesPosition - 2] = x; this.morphCoordinates[this.coordinatesPosition - 1] = y; }; ShapeData.prototype.clear = function () { this.commandsPosition = this.coordinatesPosition = 0; this.commands = new Uint8Array(DefaultSize.Commands); this.coordinates = new Int32Array(DefaultSize.Coordinates); this.styles = new DataBuffer(DefaultSize.Styles); this.styles.endian = 'auto'; this.hasFills = this.hasLines = false; }; ShapeData.prototype.isEmpty = function () { return this.commandsPosition === 0; }; ShapeData.prototype.clone = function () { var copy = new ShapeData(false); copy.commands = new Uint8Array(this.commands); copy.commandsPosition = this.commandsPosition; copy.coordinates = new Int32Array(this.coordinates); copy.coordinatesPosition = this.coordinatesPosition; copy.styles = new DataBuffer(this.styles.length); copy.styles.writeRawBytes(this.styles.bytes.subarray(0, this.styles.length)); if (this.morphStyles) { copy.morphStyles = new DataBuffer(this.morphStyles.length); copy.morphStyles.writeRawBytes(this.morphStyles.bytes.subarray(0, this.morphStyles.length)); } copy.hasFills = this.hasFills; copy.hasLines = this.hasLines; return copy; }; ShapeData.prototype.toPlainObject = function () { return new PlainObjectShapeData(this.commands, this.commandsPosition, this.coordinates, this.morphCoordinates, this.coordinatesPosition, this.styles.buffer, this.styles.length, this.morphStyles && this.morphStyles.buffer, this.morphStyles ? this.morphStyles.length : 0, this.hasFills, this.hasLines); }; Object.defineProperty(ShapeData.prototype, "buffers", { get: function () { var buffers = [this.commands.buffer, this.coordinates.buffer, this.styles.buffer]; if (this.morphCoordinates) { buffers.push(this.morphCoordinates.buffer); } if (this.morphStyles) { buffers.push(this.morphStyles.buffer); } return buffers; }, enumerable: false, configurable: true }); ShapeData.prototype._writeStyleMatrix = function (matrix, isMorph) { var styles = isMorph ? this.morphStyles : this.styles; styles.write6Floats(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty); }; ShapeData.prototype.ensurePathCapacities = function (numCommands, numCoordinates) { // ensureTypedArrayCapacity will hopefully be inlined, in which case the field writes // will be optimized out. this.commands = ensureTypedArrayCapacity(this.commands, this.commandsPosition + numCommands); this.coordinates = ensureTypedArrayCapacity(this.coordinates, this.coordinatesPosition + numCoordinates); }; return ShapeData; }()); export { ShapeData };