@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
187 lines • 7.38 kB
JavaScript
import { Collection } from "@aurigma/design-atoms-model/Collection";
import { RgbColors } from "@aurigma/design-atoms-model/Colors";
import { BoundedTextItem, CurvedTextItem, PathBoundedTextItem, PlainTextItem } from "@aurigma/design-atoms-model/Product/Items";
import { OpenTypeFeature } from "@aurigma/design-atoms-text/Model";
import { NewBoundedTextItemHandler, NewCurvedTextItemHandler, NewPlainTextItemHandler } from "../ItemHandlers";
import { ItemUtils } from "../Utils/ItemUtils";
/**
* Data required to initialize the text engine wrapper.
* @remarks The TextWhizz is used as a text engine.
*/
export class TextWhizzInitData {
constructor(itemHandler, colorPreviewService) {
this._itemHandler = itemHandler;
this._colorPreviewService = colorPreviewService;
}
get text() {
return this._text;
}
set text(value) {
this._text = value;
}
get rectangle() {
return this._rectangle;
}
get previewScale() {
return this._previewScale;
}
get frames() {
return this._frames;
}
get wrappingPath() {
return this._wrappingPath;
}
get colorPalette() {
return this._colorPalette;
}
get defaultCharStyle() {
return this._defaultCharStyle;
}
get defaultParagraphStyle() {
return this._defaultParagraphStyle;
}
get isTextVertical() {
return this._isTextVertical;
}
get isTextBounded() {
return this._isTextBounded;
}
get verticalAlignment() {
return this._verticalAlignment;
}
get overflowStrategy() {
return this._overflowStrategy;
}
get shrinkMode() {
return this._shrinkMode;
}
get firstBaselineOffset() {
return this._firstBaselineOffset;
}
get firstBaselineMinOffset() {
return this._firstBaselineMinOffset;
}
get isFitToPath() {
return this._isFitToPath;
}
get start() {
return this._start;
}
get end() {
return this._end;
}
getActualFrames() {
return this._itemHandler.getFramesData();
}
async update(customText = null) {
const item = this._itemHandler.item;
const itemHandler = this._itemHandler;
const isTextBounded = itemHandler instanceof NewBoundedTextItemHandler && item instanceof BoundedTextItem;
const isTextCurved = itemHandler instanceof NewCurvedTextItemHandler && item instanceof CurvedTextItem;
const isTextPlain = itemHandler instanceof NewPlainTextItemHandler && item instanceof PlainTextItem;
this._text = customText != null ? customText : itemHandler.getTextForRendering();
this._rectangle = itemHandler.rectangle.toRectangleF();
this._previewScale = item.previewScale;
this._frames = itemHandler.getFramesData();
this._wrappingPath = itemHandler.getWrappingPathData();
this._colorPalette = new Collection(await ItemUtils.getColorPalette(item, this._colorPreviewService));
this._colorPalette.add_itemAdded((data) => itemHandler.item.colorPalette.add(data.item.color));
this._defaultCharStyle = await TextWhizzInitData.getDefaultInlineStyle(item, this._colorPreviewService);
this._defaultParagraphStyle = TextWhizzInitData.getDefaultParagraphStyle(item);
this._isTextBounded = isTextBounded;
// default values
let isTextVertical = false;
let overflowStrategy = null;
let verticalAlignment = null;
let shrinkMode = null;
let firstBaselineOffset = null;
let firstBaselineMinOffset = null;
let isFitToPath = null;
let start = null;
let end = null;
if (isTextBounded) {
const boundedTextItem = item;
overflowStrategy = boundedTextItem.overflowStrategy;
verticalAlignment = boundedTextItem.verticalAlignment;
shrinkMode = boundedTextItem.shrinkMode;
firstBaselineOffset = boundedTextItem.firstBaselineOffset;
firstBaselineMinOffset = boundedTextItem.firstBaselineMinOffset;
isTextVertical = boundedTextItem.isVertical;
}
if (isTextCurved) {
const curvedTextItem = item;
isFitToPath = curvedTextItem.fitToPath;
start = curvedTextItem.start;
end = curvedTextItem.end;
}
if (isTextPlain) {
const plainTextItem = item;
isTextVertical = plainTextItem.isVertical;
}
this._isTextVertical = isTextVertical;
this._verticalAlignment = verticalAlignment;
this._overflowStrategy = overflowStrategy;
this._shrinkMode = shrinkMode;
this._firstBaselineOffset = firstBaselineOffset;
this._firstBaselineMinOffset = firstBaselineMinOffset;
this._isFitToPath = isFitToPath;
this._start = start;
this._end = end;
}
static async getDefaultInlineStyle(item, colorPreviewService) {
const style = {
font: {
postScriptName: item.font.postScriptName,
size: item.font.size,
fauxBold: item.font.fauxBold,
fauxItalic: item.font.fauxItalic,
allCaps: item.font.allCaps
},
underline: item.underline,
leading: item.leading,
tracking: item.tracking,
color: {
color: item.color.clone(),
preview: await colorPreviewService.getPreviewAsync(item.color)
}
};
style.baselineShift = item.baselineShift;
style.horizontalScale = item.horizontalScale;
style.verticalScale = item.verticalScale;
const stroke = {};
if (item.stroke != null) {
stroke.color = {
color: item.stroke.color.clone(),
preview: await colorPreviewService.getPreviewAsync(item.stroke.color)
};
stroke.size = item.stroke.size;
stroke.enabled = stroke.size > 0 && !stroke.color.color.isTransparent;
}
else {
stroke.color = {
color: RgbColors.transparent,
preview: RgbColors.transparent
};
stroke.size = 0;
stroke.enabled = false;
}
style.stroke = stroke;
if (item.font.openTypeFeatures.length > 0)
style.openType = item.font.openTypeFeatures.map(({ tag, value }) => new OpenTypeFeature(tag, value));
return style;
}
static getDefaultParagraphStyle(item) {
const style = {
alignment: item.alignment
};
if (item instanceof BoundedTextItem || item instanceof PathBoundedTextItem) {
style.firstLineIndent = item.paragraphSettings.FirstLineIndent;
style.spaceAfter = item.paragraphSettings.SpaceAfter;
style.spaceBefore = item.paragraphSettings.SpaceBefore;
style.leftIndent = item.paragraphSettings.LeftIndent;
style.rightIndent = item.paragraphSettings.RightIndent;
}
return style;
}
}
//# sourceMappingURL=TextWhizzInitData.js.map