@awayjs/graphics
Version:
AwayJS graphics classes
178 lines (177 loc) • 6.93 kB
JavaScript
import { BitmapImage2D } from '@awayjs/stage';
import { FillType } from '../data/FillType';
import { SegmentedPath } from '../data/SegmentedPath';
var IDENTITY_MATRIX = { a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0 };
var StyleUtils = /** @class */ (function () {
function StyleUtils() {
}
StyleUtils.processStyle = function (style, isLineStyle, isMorph, factory) {
var shapeStyle = style;
if (isMorph) {
shapeStyle.morph = this.processMorphStyle(style, isLineStyle);
}
if (isLineStyle) {
shapeStyle.miterLimit = (style.miterLimitFactor || 1.5) * 2;
if (!style.color && style.hasFill) {
var fillStyle = this.processStyle(style.fillStyle, false, false, factory);
shapeStyle.type = fillStyle.type;
shapeStyle.transform = fillStyle.transform;
shapeStyle.colors = fillStyle.colors;
shapeStyle.ratios = fillStyle.ratios;
shapeStyle.focalPoint = fillStyle.focalPoint;
shapeStyle.bitmapId = fillStyle.bitmapId;
shapeStyle.image = fillStyle.image;
shapeStyle.repeat = fillStyle.repeat;
style.fillStyle = null;
return shapeStyle;
}
else {
shapeStyle.type = FillType.Solid;
return shapeStyle;
}
}
if (style.type === undefined || style.type === FillType.Solid) {
return shapeStyle;
}
var scale = 1;
switch (style.type) {
case FillType.LinearGradient:
case FillType.RadialGradient:
case FillType.FocalRadialGradient: {
var records = style.records;
var colors = shapeStyle.colors = [];
var ratios = shapeStyle.ratios = [];
for (var i = 0; i < records.length; i++) {
var record = records[i];
if (ratios.length == 0 || ratios[ratios.length - 1] != record.ratio) {
colors.push(record.color);
ratios.push(record.ratio);
}
}
scale = 1;
break;
}
case FillType.RepeatingBitmap:
case FillType.ClippedBitmap:
case FillType.NonsmoothedRepeatingBitmap:
case FillType.NonsmoothedClippedBitmap:
shapeStyle.smooth =
(style.type !== FillType.NonsmoothedRepeatingBitmap &&
style.type !== FillType.NonsmoothedClippedBitmap);
shapeStyle.repeat =
(style.type !== FillType.ClippedBitmap &&
style.type !== FillType.NonsmoothedClippedBitmap);
/*var index = dependencies.indexOf(style.bitmapId);
if (index === -1) {
index = dependencies.length;
dependencies.push(style.bitmapId);
}*/
shapeStyle.image = this.getImage(style.bitmapId, factory);
scale = 1 / 20;
break;
default:
console.log('shape parser encountered invalid fill style ' + style.type);
}
if (!style.matrix) {
shapeStyle.transform = IDENTITY_MATRIX;
return shapeStyle;
}
var matrix = style.matrix;
shapeStyle.transform = {
a: matrix.a * scale,
b: matrix.b * scale,
c: matrix.c * scale,
d: matrix.d * scale,
tx: matrix.tx / 20,
ty: matrix.ty / 20
};
// null data that's unused from here on out
style.matrix = null;
return shapeStyle;
};
StyleUtils.getImage = function (bitmapIndex, factory) {
return factory.awaySymbols[bitmapIndex] || new BitmapImage2D(512, 512, true, 0xff0000ff, true);
};
StyleUtils.processMorphStyle = function (style, isLineStyle) {
var morphStyle = Object.create(style);
if (isLineStyle) {
morphStyle.width = style.widthMorph;
if (!style.color && style.hasFill) {
var fillStyle = this.processMorphStyle(style.fillStyle, false);
morphStyle.transform = fillStyle.transform;
morphStyle.colors = fillStyle.colors;
morphStyle.ratios = fillStyle.ratios;
return morphStyle;
}
else {
morphStyle.color = style.colorMorph;
return morphStyle;
}
}
if (style.type === undefined) {
return morphStyle;
}
if (style.type === FillType.Solid) {
morphStyle.color = style.colorMorph;
return morphStyle;
}
var scale = 1;
switch (style.type) {
case FillType.LinearGradient:
case FillType.RadialGradient:
case FillType.FocalRadialGradient: {
var records = style.records;
var colors = morphStyle.colors = [];
var ratios = morphStyle.ratios = [];
for (var i = 0; i < records.length; i++) {
var record = records[i];
colors.push(record.colorMorph);
ratios.push(record.ratioMorph);
}
scale = 1;
break;
}
case FillType.RepeatingBitmap:
case FillType.ClippedBitmap:
case FillType.NonsmoothedRepeatingBitmap:
case FillType.NonsmoothedClippedBitmap:
scale = 1 / 20;
break;
default:
console.log('shape parser encountered invalid fill style');
}
if (!style.matrix) {
morphStyle.transform = IDENTITY_MATRIX;
return morphStyle;
}
var matrix = style.matrixMorph;
morphStyle.transform = {
a: (matrix.a * scale),
b: (matrix.b * scale),
c: (matrix.c * scale),
d: (matrix.d * scale),
tx: matrix.tx / 20,
ty: matrix.ty / 20
};
return morphStyle;
};
/*
* Paths are stored in 2-dimensional arrays. Each of the inner arrays contains
* all the paths for a certain fill or line style.
*/
StyleUtils.createPathsList = function (styles, isLineStyle, isMorph, factory) {
var paths = [];
for (var i = 0; i < styles.length; i++) {
var style = this.processStyle(styles[i], isLineStyle, isMorph, factory);
if (!isLineStyle) {
paths[i] = new SegmentedPath(style, null);
}
else {
paths[i] = new SegmentedPath(null, style);
}
}
return paths;
};
return StyleUtils;
}());
export { StyleUtils };