逐步完成前后端服务器

This commit is contained in:
2025-09-09 15:00:30 +08:00
parent fcb09432e9
commit c7dfa1e9fc
2937 changed files with 1477567 additions and 0 deletions

View File

@ -0,0 +1,6 @@
import { ImageLike } from '../../core/types';
export declare function findExistImage(newImageOrSrc: string | ImageLike): ImageLike;
export declare function createOrUpdateImage<T>(newImageOrSrc: string | ImageLike, image: ImageLike, hostEl: {
dirty: () => void;
}, onload?: (image: ImageLike, payload: T) => void, cbPayload?: T): ImageLike;
export declare function isImageReady(image: ImageLike): number;

View File

@ -0,0 +1,54 @@
import LRU from '../../core/LRU.js';
import { platformApi } from '../../core/platform.js';
var globalImageCache = new LRU(50);
export function findExistImage(newImageOrSrc) {
if (typeof newImageOrSrc === 'string') {
var cachedImgObj = globalImageCache.get(newImageOrSrc);
return cachedImgObj && cachedImgObj.image;
}
else {
return newImageOrSrc;
}
}
export function createOrUpdateImage(newImageOrSrc, image, hostEl, onload, cbPayload) {
if (!newImageOrSrc) {
return image;
}
else if (typeof newImageOrSrc === 'string') {
if ((image && image.__zrImageSrc === newImageOrSrc) || !hostEl) {
return image;
}
var cachedImgObj = globalImageCache.get(newImageOrSrc);
var pendingWrap = { hostEl: hostEl, cb: onload, cbPayload: cbPayload };
if (cachedImgObj) {
image = cachedImgObj.image;
!isImageReady(image) && cachedImgObj.pending.push(pendingWrap);
}
else {
image = platformApi.loadImage(newImageOrSrc, imageOnLoad, imageOnLoad);
image.__zrImageSrc = newImageOrSrc;
globalImageCache.put(newImageOrSrc, image.__cachedImgObj = {
image: image,
pending: [pendingWrap]
});
}
return image;
}
else {
return newImageOrSrc;
}
}
function imageOnLoad() {
var cachedImgObj = this.__cachedImgObj;
this.onload = this.onerror = this.__cachedImgObj = null;
for (var i = 0; i < cachedImgObj.pending.length; i++) {
var pendingWrap = cachedImgObj.pending[i];
var cb = pendingWrap.cb;
cb && cb(this, pendingWrap.cbPayload);
pendingWrap.hostEl.dirty();
}
cachedImgObj.pending.length = 0;
}
export function isImageReady(image) {
return image && image.width && image.height;
}

View File

@ -0,0 +1,56 @@
import { TextAlign, TextVerticalAlign } from '../../core/types';
import { TextStyleProps } from '../Text';
interface InnerTruncateOption {
maxIteration?: number;
minChar?: number;
placeholder?: string;
maxIterations?: number;
}
export declare function truncateText(text: string, containerWidth: number, font: string, ellipsis?: string, options?: InnerTruncateOption): string;
export interface PlainTextContentBlock {
lineHeight: number;
calculatedLineHeight: number;
contentWidth: number;
contentHeight: number;
width: number;
height: number;
outerWidth: number;
outerHeight: number;
lines: string[];
isTruncated: boolean;
}
export declare function parsePlainText(text: string, style?: TextStyleProps): PlainTextContentBlock;
declare class RichTextToken {
styleName: string;
text: string;
width: number;
height: number;
innerHeight: number;
contentHeight: number;
contentWidth: number;
lineHeight: number;
font: string;
align: TextAlign;
verticalAlign: TextVerticalAlign;
textPadding: number[];
percentWidth?: string;
isLineHolder: boolean;
}
declare class RichTextLine {
lineHeight: number;
width: number;
tokens: RichTextToken[];
constructor(tokens?: RichTextToken[]);
}
export declare class RichTextContentBlock {
width: number;
height: number;
contentWidth: number;
contentHeight: number;
outerWidth: number;
outerHeight: number;
lines: RichTextLine[];
isTruncated: boolean;
}
export declare function parseRichText(text: string, style: TextStyleProps): RichTextContentBlock;
export {};

View File

@ -0,0 +1,498 @@
import * as imageHelper from '../helper/image.js';
import { extend, retrieve2, retrieve3, reduce } from '../../core/util.js';
import { getLineHeight, getWidth, parsePercent } from '../../contain/text.js';
var STYLE_REG = /\{([a-zA-Z0-9_]+)\|([^}]*)\}/g;
export function truncateText(text, containerWidth, font, ellipsis, options) {
var out = {};
truncateText2(out, text, containerWidth, font, ellipsis, options);
return out.text;
}
function truncateText2(out, text, containerWidth, font, ellipsis, options) {
if (!containerWidth) {
out.text = '';
out.isTruncated = false;
return;
}
var textLines = (text + '').split('\n');
options = prepareTruncateOptions(containerWidth, font, ellipsis, options);
var isTruncated = false;
var truncateOut = {};
for (var i = 0, len = textLines.length; i < len; i++) {
truncateSingleLine(truncateOut, textLines[i], options);
textLines[i] = truncateOut.textLine;
isTruncated = isTruncated || truncateOut.isTruncated;
}
out.text = textLines.join('\n');
out.isTruncated = isTruncated;
}
function prepareTruncateOptions(containerWidth, font, ellipsis, options) {
options = options || {};
var preparedOpts = extend({}, options);
preparedOpts.font = font;
ellipsis = retrieve2(ellipsis, '...');
preparedOpts.maxIterations = retrieve2(options.maxIterations, 2);
var minChar = preparedOpts.minChar = retrieve2(options.minChar, 0);
preparedOpts.cnCharWidth = getWidth('国', font);
var ascCharWidth = preparedOpts.ascCharWidth = getWidth('a', font);
preparedOpts.placeholder = retrieve2(options.placeholder, '');
var contentWidth = containerWidth = Math.max(0, containerWidth - 1);
for (var i = 0; i < minChar && contentWidth >= ascCharWidth; i++) {
contentWidth -= ascCharWidth;
}
var ellipsisWidth = getWidth(ellipsis, font);
if (ellipsisWidth > contentWidth) {
ellipsis = '';
ellipsisWidth = 0;
}
contentWidth = containerWidth - ellipsisWidth;
preparedOpts.ellipsis = ellipsis;
preparedOpts.ellipsisWidth = ellipsisWidth;
preparedOpts.contentWidth = contentWidth;
preparedOpts.containerWidth = containerWidth;
return preparedOpts;
}
function truncateSingleLine(out, textLine, options) {
var containerWidth = options.containerWidth;
var font = options.font;
var contentWidth = options.contentWidth;
if (!containerWidth) {
out.textLine = '';
out.isTruncated = false;
return;
}
var lineWidth = getWidth(textLine, font);
if (lineWidth <= containerWidth) {
out.textLine = textLine;
out.isTruncated = false;
return;
}
for (var j = 0;; j++) {
if (lineWidth <= contentWidth || j >= options.maxIterations) {
textLine += options.ellipsis;
break;
}
var subLength = j === 0
? estimateLength(textLine, contentWidth, options.ascCharWidth, options.cnCharWidth)
: lineWidth > 0
? Math.floor(textLine.length * contentWidth / lineWidth)
: 0;
textLine = textLine.substr(0, subLength);
lineWidth = getWidth(textLine, font);
}
if (textLine === '') {
textLine = options.placeholder;
}
out.textLine = textLine;
out.isTruncated = true;
}
function estimateLength(text, contentWidth, ascCharWidth, cnCharWidth) {
var width = 0;
var i = 0;
for (var len = text.length; i < len && width < contentWidth; i++) {
var charCode = text.charCodeAt(i);
width += (0 <= charCode && charCode <= 127) ? ascCharWidth : cnCharWidth;
}
return i;
}
export function parsePlainText(text, style) {
text != null && (text += '');
var overflow = style.overflow;
var padding = style.padding;
var font = style.font;
var truncate = overflow === 'truncate';
var calculatedLineHeight = getLineHeight(font);
var lineHeight = retrieve2(style.lineHeight, calculatedLineHeight);
var bgColorDrawn = !!(style.backgroundColor);
var truncateLineOverflow = style.lineOverflow === 'truncate';
var isTruncated = false;
var width = style.width;
var lines;
if (width != null && (overflow === 'break' || overflow === 'breakAll')) {
lines = text ? wrapText(text, style.font, width, overflow === 'breakAll', 0).lines : [];
}
else {
lines = text ? text.split('\n') : [];
}
var contentHeight = lines.length * lineHeight;
var height = retrieve2(style.height, contentHeight);
if (contentHeight > height && truncateLineOverflow) {
var lineCount = Math.floor(height / lineHeight);
isTruncated = isTruncated || (lines.length > lineCount);
lines = lines.slice(0, lineCount);
}
if (text && truncate && width != null) {
var options = prepareTruncateOptions(width, font, style.ellipsis, {
minChar: style.truncateMinChar,
placeholder: style.placeholder
});
var singleOut = {};
for (var i = 0; i < lines.length; i++) {
truncateSingleLine(singleOut, lines[i], options);
lines[i] = singleOut.textLine;
isTruncated = isTruncated || singleOut.isTruncated;
}
}
var outerHeight = height;
var contentWidth = 0;
for (var i = 0; i < lines.length; i++) {
contentWidth = Math.max(getWidth(lines[i], font), contentWidth);
}
if (width == null) {
width = contentWidth;
}
var outerWidth = contentWidth;
if (padding) {
outerHeight += padding[0] + padding[2];
outerWidth += padding[1] + padding[3];
width += padding[1] + padding[3];
}
if (bgColorDrawn) {
outerWidth = width;
}
return {
lines: lines,
height: height,
outerWidth: outerWidth,
outerHeight: outerHeight,
lineHeight: lineHeight,
calculatedLineHeight: calculatedLineHeight,
contentWidth: contentWidth,
contentHeight: contentHeight,
width: width,
isTruncated: isTruncated
};
}
var RichTextToken = (function () {
function RichTextToken() {
}
return RichTextToken;
}());
var RichTextLine = (function () {
function RichTextLine(tokens) {
this.tokens = [];
if (tokens) {
this.tokens = tokens;
}
}
return RichTextLine;
}());
var RichTextContentBlock = (function () {
function RichTextContentBlock() {
this.width = 0;
this.height = 0;
this.contentWidth = 0;
this.contentHeight = 0;
this.outerWidth = 0;
this.outerHeight = 0;
this.lines = [];
this.isTruncated = false;
}
return RichTextContentBlock;
}());
export { RichTextContentBlock };
export function parseRichText(text, style) {
var contentBlock = new RichTextContentBlock();
text != null && (text += '');
if (!text) {
return contentBlock;
}
var topWidth = style.width;
var topHeight = style.height;
var overflow = style.overflow;
var wrapInfo = (overflow === 'break' || overflow === 'breakAll') && topWidth != null
? { width: topWidth, accumWidth: 0, breakAll: overflow === 'breakAll' }
: null;
var lastIndex = STYLE_REG.lastIndex = 0;
var result;
while ((result = STYLE_REG.exec(text)) != null) {
var matchedIndex = result.index;
if (matchedIndex > lastIndex) {
pushTokens(contentBlock, text.substring(lastIndex, matchedIndex), style, wrapInfo);
}
pushTokens(contentBlock, result[2], style, wrapInfo, result[1]);
lastIndex = STYLE_REG.lastIndex;
}
if (lastIndex < text.length) {
pushTokens(contentBlock, text.substring(lastIndex, text.length), style, wrapInfo);
}
var pendingList = [];
var calculatedHeight = 0;
var calculatedWidth = 0;
var stlPadding = style.padding;
var truncate = overflow === 'truncate';
var truncateLine = style.lineOverflow === 'truncate';
var tmpTruncateOut = {};
function finishLine(line, lineWidth, lineHeight) {
line.width = lineWidth;
line.lineHeight = lineHeight;
calculatedHeight += lineHeight;
calculatedWidth = Math.max(calculatedWidth, lineWidth);
}
outer: for (var i = 0; i < contentBlock.lines.length; i++) {
var line = contentBlock.lines[i];
var lineHeight = 0;
var lineWidth = 0;
for (var j = 0; j < line.tokens.length; j++) {
var token = line.tokens[j];
var tokenStyle = token.styleName && style.rich[token.styleName] || {};
var textPadding = token.textPadding = tokenStyle.padding;
var paddingH = textPadding ? textPadding[1] + textPadding[3] : 0;
var font = token.font = tokenStyle.font || style.font;
token.contentHeight = getLineHeight(font);
var tokenHeight = retrieve2(tokenStyle.height, token.contentHeight);
token.innerHeight = tokenHeight;
textPadding && (tokenHeight += textPadding[0] + textPadding[2]);
token.height = tokenHeight;
token.lineHeight = retrieve3(tokenStyle.lineHeight, style.lineHeight, tokenHeight);
token.align = tokenStyle && tokenStyle.align || style.align;
token.verticalAlign = tokenStyle && tokenStyle.verticalAlign || 'middle';
if (truncateLine && topHeight != null && calculatedHeight + token.lineHeight > topHeight) {
var originalLength = contentBlock.lines.length;
if (j > 0) {
line.tokens = line.tokens.slice(0, j);
finishLine(line, lineWidth, lineHeight);
contentBlock.lines = contentBlock.lines.slice(0, i + 1);
}
else {
contentBlock.lines = contentBlock.lines.slice(0, i);
}
contentBlock.isTruncated = contentBlock.isTruncated || (contentBlock.lines.length < originalLength);
break outer;
}
var styleTokenWidth = tokenStyle.width;
var tokenWidthNotSpecified = styleTokenWidth == null || styleTokenWidth === 'auto';
if (typeof styleTokenWidth === 'string' && styleTokenWidth.charAt(styleTokenWidth.length - 1) === '%') {
token.percentWidth = styleTokenWidth;
pendingList.push(token);
token.contentWidth = getWidth(token.text, font);
}
else {
if (tokenWidthNotSpecified) {
var textBackgroundColor = tokenStyle.backgroundColor;
var bgImg = textBackgroundColor && textBackgroundColor.image;
if (bgImg) {
bgImg = imageHelper.findExistImage(bgImg);
if (imageHelper.isImageReady(bgImg)) {
token.width = Math.max(token.width, bgImg.width * tokenHeight / bgImg.height);
}
}
}
var remainTruncWidth = truncate && topWidth != null
? topWidth - lineWidth : null;
if (remainTruncWidth != null && remainTruncWidth < token.width) {
if (!tokenWidthNotSpecified || remainTruncWidth < paddingH) {
token.text = '';
token.width = token.contentWidth = 0;
}
else {
truncateText2(tmpTruncateOut, token.text, remainTruncWidth - paddingH, font, style.ellipsis, { minChar: style.truncateMinChar });
token.text = tmpTruncateOut.text;
contentBlock.isTruncated = contentBlock.isTruncated || tmpTruncateOut.isTruncated;
token.width = token.contentWidth = getWidth(token.text, font);
}
}
else {
token.contentWidth = getWidth(token.text, font);
}
}
token.width += paddingH;
lineWidth += token.width;
tokenStyle && (lineHeight = Math.max(lineHeight, token.lineHeight));
}
finishLine(line, lineWidth, lineHeight);
}
contentBlock.outerWidth = contentBlock.width = retrieve2(topWidth, calculatedWidth);
contentBlock.outerHeight = contentBlock.height = retrieve2(topHeight, calculatedHeight);
contentBlock.contentHeight = calculatedHeight;
contentBlock.contentWidth = calculatedWidth;
if (stlPadding) {
contentBlock.outerWidth += stlPadding[1] + stlPadding[3];
contentBlock.outerHeight += stlPadding[0] + stlPadding[2];
}
for (var i = 0; i < pendingList.length; i++) {
var token = pendingList[i];
var percentWidth = token.percentWidth;
token.width = parseInt(percentWidth, 10) / 100 * contentBlock.width;
}
return contentBlock;
}
function pushTokens(block, str, style, wrapInfo, styleName) {
var isEmptyStr = str === '';
var tokenStyle = styleName && style.rich[styleName] || {};
var lines = block.lines;
var font = tokenStyle.font || style.font;
var newLine = false;
var strLines;
var linesWidths;
if (wrapInfo) {
var tokenPadding = tokenStyle.padding;
var tokenPaddingH = tokenPadding ? tokenPadding[1] + tokenPadding[3] : 0;
if (tokenStyle.width != null && tokenStyle.width !== 'auto') {
var outerWidth_1 = parsePercent(tokenStyle.width, wrapInfo.width) + tokenPaddingH;
if (lines.length > 0) {
if (outerWidth_1 + wrapInfo.accumWidth > wrapInfo.width) {
strLines = str.split('\n');
newLine = true;
}
}
wrapInfo.accumWidth = outerWidth_1;
}
else {
var res = wrapText(str, font, wrapInfo.width, wrapInfo.breakAll, wrapInfo.accumWidth);
wrapInfo.accumWidth = res.accumWidth + tokenPaddingH;
linesWidths = res.linesWidths;
strLines = res.lines;
}
}
else {
strLines = str.split('\n');
}
for (var i = 0; i < strLines.length; i++) {
var text = strLines[i];
var token = new RichTextToken();
token.styleName = styleName;
token.text = text;
token.isLineHolder = !text && !isEmptyStr;
if (typeof tokenStyle.width === 'number') {
token.width = tokenStyle.width;
}
else {
token.width = linesWidths
? linesWidths[i]
: getWidth(text, font);
}
if (!i && !newLine) {
var tokens = (lines[lines.length - 1] || (lines[0] = new RichTextLine())).tokens;
var tokensLen = tokens.length;
(tokensLen === 1 && tokens[0].isLineHolder)
? (tokens[0] = token)
: ((text || !tokensLen || isEmptyStr) && tokens.push(token));
}
else {
lines.push(new RichTextLine([token]));
}
}
}
function isAlphabeticLetter(ch) {
var code = ch.charCodeAt(0);
return code >= 0x20 && code <= 0x24F
|| code >= 0x370 && code <= 0x10FF
|| code >= 0x1200 && code <= 0x13FF
|| code >= 0x1E00 && code <= 0x206F;
}
var breakCharMap = reduce(',&?/;] '.split(''), function (obj, ch) {
obj[ch] = true;
return obj;
}, {});
function isWordBreakChar(ch) {
if (isAlphabeticLetter(ch)) {
if (breakCharMap[ch]) {
return true;
}
return false;
}
return true;
}
function wrapText(text, font, lineWidth, isBreakAll, lastAccumWidth) {
var lines = [];
var linesWidths = [];
var line = '';
var currentWord = '';
var currentWordWidth = 0;
var accumWidth = 0;
for (var i = 0; i < text.length; i++) {
var ch = text.charAt(i);
if (ch === '\n') {
if (currentWord) {
line += currentWord;
accumWidth += currentWordWidth;
}
lines.push(line);
linesWidths.push(accumWidth);
line = '';
currentWord = '';
currentWordWidth = 0;
accumWidth = 0;
continue;
}
var chWidth = getWidth(ch, font);
var inWord = isBreakAll ? false : !isWordBreakChar(ch);
if (!lines.length
? lastAccumWidth + accumWidth + chWidth > lineWidth
: accumWidth + chWidth > lineWidth) {
if (!accumWidth) {
if (inWord) {
lines.push(currentWord);
linesWidths.push(currentWordWidth);
currentWord = ch;
currentWordWidth = chWidth;
}
else {
lines.push(ch);
linesWidths.push(chWidth);
}
}
else if (line || currentWord) {
if (inWord) {
if (!line) {
line = currentWord;
currentWord = '';
currentWordWidth = 0;
accumWidth = currentWordWidth;
}
lines.push(line);
linesWidths.push(accumWidth - currentWordWidth);
currentWord += ch;
currentWordWidth += chWidth;
line = '';
accumWidth = currentWordWidth;
}
else {
if (currentWord) {
line += currentWord;
currentWord = '';
currentWordWidth = 0;
}
lines.push(line);
linesWidths.push(accumWidth);
line = ch;
accumWidth = chWidth;
}
}
continue;
}
accumWidth += chWidth;
if (inWord) {
currentWord += ch;
currentWordWidth += chWidth;
}
else {
if (currentWord) {
line += currentWord;
currentWord = '';
currentWordWidth = 0;
}
line += ch;
}
}
if (!lines.length && !line) {
line = text;
currentWord = '';
currentWordWidth = 0;
}
if (currentWord) {
line += currentWord;
}
if (line) {
lines.push(line);
linesWidths.push(accumWidth);
}
if (lines.length === 1) {
accumWidth += lastAccumWidth;
}
return {
accumWidth: accumWidth,
lines: lines,
linesWidths: linesWidths
};
}

View File

@ -0,0 +1,7 @@
import { VectorArray } from '../../core/vector';
import PathProxy from '../../core/PathProxy';
export declare function buildPath(ctx: CanvasRenderingContext2D | PathProxy, shape: {
points: VectorArray[];
smooth?: number;
smoothConstraint?: VectorArray[];
}, closePath: boolean): void;

View File

@ -0,0 +1,25 @@
import smoothBezier from './smoothBezier.js';
export function buildPath(ctx, shape, closePath) {
var smooth = shape.smooth;
var points = shape.points;
if (points && points.length >= 2) {
if (smooth) {
var controlPoints = smoothBezier(points, smooth, closePath, shape.smoothConstraint);
ctx.moveTo(points[0][0], points[0][1]);
var len = points.length;
for (var i = 0; i < (closePath ? len : len - 1); i++) {
var cp1 = controlPoints[i * 2];
var cp2 = controlPoints[i * 2 + 1];
var p = points[(i + 1) % len];
ctx.bezierCurveTo(cp1[0], cp1[1], cp2[0], cp2[1], p[0], p[1]);
}
}
else {
ctx.moveTo(points[0][0], points[0][1]);
for (var i = 1, l = points.length; i < l; i++) {
ctx.lineTo(points[i][0], points[i][1]);
}
}
closePath && ctx.closePath();
}
}

View File

@ -0,0 +1,8 @@
import PathProxy from '../../core/PathProxy';
export declare function buildPath(ctx: CanvasRenderingContext2D | PathProxy, shape: {
x: number;
y: number;
width: number;
height: number;
r?: number | number[];
}): void;

View File

@ -0,0 +1,75 @@
export function buildPath(ctx, shape) {
var x = shape.x;
var y = shape.y;
var width = shape.width;
var height = shape.height;
var r = shape.r;
var r1;
var r2;
var r3;
var r4;
if (width < 0) {
x = x + width;
width = -width;
}
if (height < 0) {
y = y + height;
height = -height;
}
if (typeof r === 'number') {
r1 = r2 = r3 = r4 = r;
}
else if (r instanceof Array) {
if (r.length === 1) {
r1 = r2 = r3 = r4 = r[0];
}
else if (r.length === 2) {
r1 = r3 = r[0];
r2 = r4 = r[1];
}
else if (r.length === 3) {
r1 = r[0];
r2 = r4 = r[1];
r3 = r[2];
}
else {
r1 = r[0];
r2 = r[1];
r3 = r[2];
r4 = r[3];
}
}
else {
r1 = r2 = r3 = r4 = 0;
}
var total;
if (r1 + r2 > width) {
total = r1 + r2;
r1 *= width / total;
r2 *= width / total;
}
if (r3 + r4 > width) {
total = r3 + r4;
r3 *= width / total;
r4 *= width / total;
}
if (r2 + r3 > height) {
total = r2 + r3;
r2 *= height / total;
r3 *= height / total;
}
if (r1 + r4 > height) {
total = r1 + r4;
r1 *= height / total;
r4 *= height / total;
}
ctx.moveTo(x + r1, y);
ctx.lineTo(x + width - r2, y);
r2 !== 0 && ctx.arc(x + width - r2, y + r2, r2, -Math.PI / 2, 0);
ctx.lineTo(x + width, y + height - r3);
r3 !== 0 && ctx.arc(x + width - r3, y + height - r3, r3, 0, Math.PI / 2);
ctx.lineTo(x + r4, y + height);
r4 !== 0 && ctx.arc(x + r4, y + height - r4, r4, Math.PI / 2, Math.PI);
ctx.lineTo(x, y + r1);
r1 !== 0 && ctx.arc(x + r1, y + r1, r1, Math.PI, Math.PI * 1.5);
}

View File

@ -0,0 +1,11 @@
import PathProxy from '../../core/PathProxy';
export declare function buildPath(ctx: CanvasRenderingContext2D | PathProxy, shape: {
cx: number;
cy: number;
startAngle: number;
endAngle: number;
clockwise?: boolean;
r?: number;
r0?: number;
cornerRadius?: number | number[];
}): void;

View File

@ -0,0 +1,227 @@
import { isArray } from '../../core/util.js';
var PI = Math.PI;
var PI2 = PI * 2;
var mathSin = Math.sin;
var mathCos = Math.cos;
var mathACos = Math.acos;
var mathATan2 = Math.atan2;
var mathAbs = Math.abs;
var mathSqrt = Math.sqrt;
var mathMax = Math.max;
var mathMin = Math.min;
var e = 1e-4;
function intersect(x0, y0, x1, y1, x2, y2, x3, y3) {
var dx10 = x1 - x0;
var dy10 = y1 - y0;
var dx32 = x3 - x2;
var dy32 = y3 - y2;
var t = dy32 * dx10 - dx32 * dy10;
if (t * t < e) {
return;
}
t = (dx32 * (y0 - y2) - dy32 * (x0 - x2)) / t;
return [x0 + t * dx10, y0 + t * dy10];
}
function computeCornerTangents(x0, y0, x1, y1, radius, cr, clockwise) {
var x01 = x0 - x1;
var y01 = y0 - y1;
var lo = (clockwise ? cr : -cr) / mathSqrt(x01 * x01 + y01 * y01);
var ox = lo * y01;
var oy = -lo * x01;
var x11 = x0 + ox;
var y11 = y0 + oy;
var x10 = x1 + ox;
var y10 = y1 + oy;
var x00 = (x11 + x10) / 2;
var y00 = (y11 + y10) / 2;
var dx = x10 - x11;
var dy = y10 - y11;
var d2 = dx * dx + dy * dy;
var r = radius - cr;
var s = x11 * y10 - x10 * y11;
var d = (dy < 0 ? -1 : 1) * mathSqrt(mathMax(0, r * r * d2 - s * s));
var cx0 = (s * dy - dx * d) / d2;
var cy0 = (-s * dx - dy * d) / d2;
var cx1 = (s * dy + dx * d) / d2;
var cy1 = (-s * dx + dy * d) / d2;
var dx0 = cx0 - x00;
var dy0 = cy0 - y00;
var dx1 = cx1 - x00;
var dy1 = cy1 - y00;
if (dx0 * dx0 + dy0 * dy0 > dx1 * dx1 + dy1 * dy1) {
cx0 = cx1;
cy0 = cy1;
}
return {
cx: cx0,
cy: cy0,
x0: -ox,
y0: -oy,
x1: cx0 * (radius / r - 1),
y1: cy0 * (radius / r - 1)
};
}
function normalizeCornerRadius(cr) {
var arr;
if (isArray(cr)) {
var len = cr.length;
if (!len) {
return cr;
}
if (len === 1) {
arr = [cr[0], cr[0], 0, 0];
}
else if (len === 2) {
arr = [cr[0], cr[0], cr[1], cr[1]];
}
else if (len === 3) {
arr = cr.concat(cr[2]);
}
else {
arr = cr;
}
}
else {
arr = [cr, cr, cr, cr];
}
return arr;
}
export function buildPath(ctx, shape) {
var _a;
var radius = mathMax(shape.r, 0);
var innerRadius = mathMax(shape.r0 || 0, 0);
var hasRadius = radius > 0;
var hasInnerRadius = innerRadius > 0;
if (!hasRadius && !hasInnerRadius) {
return;
}
if (!hasRadius) {
radius = innerRadius;
innerRadius = 0;
}
if (innerRadius > radius) {
var tmp = radius;
radius = innerRadius;
innerRadius = tmp;
}
var startAngle = shape.startAngle, endAngle = shape.endAngle;
if (isNaN(startAngle) || isNaN(endAngle)) {
return;
}
var cx = shape.cx, cy = shape.cy;
var clockwise = !!shape.clockwise;
var arc = mathAbs(endAngle - startAngle);
var mod = arc > PI2 && arc % PI2;
mod > e && (arc = mod);
if (!(radius > e)) {
ctx.moveTo(cx, cy);
}
else if (arc > PI2 - e) {
ctx.moveTo(cx + radius * mathCos(startAngle), cy + radius * mathSin(startAngle));
ctx.arc(cx, cy, radius, startAngle, endAngle, !clockwise);
if (innerRadius > e) {
ctx.moveTo(cx + innerRadius * mathCos(endAngle), cy + innerRadius * mathSin(endAngle));
ctx.arc(cx, cy, innerRadius, endAngle, startAngle, clockwise);
}
}
else {
var icrStart = void 0;
var icrEnd = void 0;
var ocrStart = void 0;
var ocrEnd = void 0;
var ocrs = void 0;
var ocre = void 0;
var icrs = void 0;
var icre = void 0;
var ocrMax = void 0;
var icrMax = void 0;
var limitedOcrMax = void 0;
var limitedIcrMax = void 0;
var xre = void 0;
var yre = void 0;
var xirs = void 0;
var yirs = void 0;
var xrs = radius * mathCos(startAngle);
var yrs = radius * mathSin(startAngle);
var xire = innerRadius * mathCos(endAngle);
var yire = innerRadius * mathSin(endAngle);
var hasArc = arc > e;
if (hasArc) {
var cornerRadius = shape.cornerRadius;
if (cornerRadius) {
_a = normalizeCornerRadius(cornerRadius), icrStart = _a[0], icrEnd = _a[1], ocrStart = _a[2], ocrEnd = _a[3];
}
var halfRd = mathAbs(radius - innerRadius) / 2;
ocrs = mathMin(halfRd, ocrStart);
ocre = mathMin(halfRd, ocrEnd);
icrs = mathMin(halfRd, icrStart);
icre = mathMin(halfRd, icrEnd);
limitedOcrMax = ocrMax = mathMax(ocrs, ocre);
limitedIcrMax = icrMax = mathMax(icrs, icre);
if (ocrMax > e || icrMax > e) {
xre = radius * mathCos(endAngle);
yre = radius * mathSin(endAngle);
xirs = innerRadius * mathCos(startAngle);
yirs = innerRadius * mathSin(startAngle);
if (arc < PI) {
var it_1 = intersect(xrs, yrs, xirs, yirs, xre, yre, xire, yire);
if (it_1) {
var x0 = xrs - it_1[0];
var y0 = yrs - it_1[1];
var x1 = xre - it_1[0];
var y1 = yre - it_1[1];
var a = 1 / mathSin(mathACos((x0 * x1 + y0 * y1) / (mathSqrt(x0 * x0 + y0 * y0) * mathSqrt(x1 * x1 + y1 * y1))) / 2);
var b = mathSqrt(it_1[0] * it_1[0] + it_1[1] * it_1[1]);
limitedOcrMax = mathMin(ocrMax, (radius - b) / (a + 1));
limitedIcrMax = mathMin(icrMax, (innerRadius - b) / (a - 1));
}
}
}
}
if (!hasArc) {
ctx.moveTo(cx + xrs, cy + yrs);
}
else if (limitedOcrMax > e) {
var crStart = mathMin(ocrStart, limitedOcrMax);
var crEnd = mathMin(ocrEnd, limitedOcrMax);
var ct0 = computeCornerTangents(xirs, yirs, xrs, yrs, radius, crStart, clockwise);
var ct1 = computeCornerTangents(xre, yre, xire, yire, radius, crEnd, clockwise);
ctx.moveTo(cx + ct0.cx + ct0.x0, cy + ct0.cy + ct0.y0);
if (limitedOcrMax < ocrMax && crStart === crEnd) {
ctx.arc(cx + ct0.cx, cy + ct0.cy, limitedOcrMax, mathATan2(ct0.y0, ct0.x0), mathATan2(ct1.y0, ct1.x0), !clockwise);
}
else {
crStart > 0 && ctx.arc(cx + ct0.cx, cy + ct0.cy, crStart, mathATan2(ct0.y0, ct0.x0), mathATan2(ct0.y1, ct0.x1), !clockwise);
ctx.arc(cx, cy, radius, mathATan2(ct0.cy + ct0.y1, ct0.cx + ct0.x1), mathATan2(ct1.cy + ct1.y1, ct1.cx + ct1.x1), !clockwise);
crEnd > 0 && ctx.arc(cx + ct1.cx, cy + ct1.cy, crEnd, mathATan2(ct1.y1, ct1.x1), mathATan2(ct1.y0, ct1.x0), !clockwise);
}
}
else {
ctx.moveTo(cx + xrs, cy + yrs);
ctx.arc(cx, cy, radius, startAngle, endAngle, !clockwise);
}
if (!(innerRadius > e) || !hasArc) {
ctx.lineTo(cx + xire, cy + yire);
}
else if (limitedIcrMax > e) {
var crStart = mathMin(icrStart, limitedIcrMax);
var crEnd = mathMin(icrEnd, limitedIcrMax);
var ct0 = computeCornerTangents(xire, yire, xre, yre, innerRadius, -crEnd, clockwise);
var ct1 = computeCornerTangents(xrs, yrs, xirs, yirs, innerRadius, -crStart, clockwise);
ctx.lineTo(cx + ct0.cx + ct0.x0, cy + ct0.cy + ct0.y0);
if (limitedIcrMax < icrMax && crStart === crEnd) {
ctx.arc(cx + ct0.cx, cy + ct0.cy, limitedIcrMax, mathATan2(ct0.y0, ct0.x0), mathATan2(ct1.y0, ct1.x0), !clockwise);
}
else {
crEnd > 0 && ctx.arc(cx + ct0.cx, cy + ct0.cy, crEnd, mathATan2(ct0.y0, ct0.x0), mathATan2(ct0.y1, ct0.x1), !clockwise);
ctx.arc(cx, cy, innerRadius, mathATan2(ct0.cy + ct0.y1, ct0.cx + ct0.x1), mathATan2(ct1.cy + ct1.y1, ct1.cx + ct1.x1), clockwise);
crStart > 0 && ctx.arc(cx + ct1.cx, cy + ct1.cy, crStart, mathATan2(ct1.y1, ct1.x1), mathATan2(ct1.y0, ct1.x0), !clockwise);
}
}
else {
ctx.lineTo(cx + xire, cy + yire);
ctx.arc(cx, cy, innerRadius, endAngle, startAngle, clockwise);
}
}
ctx.closePath();
}

View File

@ -0,0 +1,2 @@
import { VectorArray } from '../../core/vector';
export default function smoothBezier(points: VectorArray[], smooth?: number, isLoop?: boolean, constraint?: VectorArray[]): any[][];

View File

@ -0,0 +1,63 @@
import { min as v2Min, max as v2Max, scale as v2Scale, distance as v2Distance, add as v2Add, clone as v2Clone, sub as v2Sub } from '../../core/vector.js';
export default function smoothBezier(points, smooth, isLoop, constraint) {
var cps = [];
var v = [];
var v1 = [];
var v2 = [];
var prevPoint;
var nextPoint;
var min;
var max;
if (constraint) {
min = [Infinity, Infinity];
max = [-Infinity, -Infinity];
for (var i = 0, len = points.length; i < len; i++) {
v2Min(min, min, points[i]);
v2Max(max, max, points[i]);
}
v2Min(min, min, constraint[0]);
v2Max(max, max, constraint[1]);
}
for (var i = 0, len = points.length; i < len; i++) {
var point = points[i];
if (isLoop) {
prevPoint = points[i ? i - 1 : len - 1];
nextPoint = points[(i + 1) % len];
}
else {
if (i === 0 || i === len - 1) {
cps.push(v2Clone(points[i]));
continue;
}
else {
prevPoint = points[i - 1];
nextPoint = points[i + 1];
}
}
v2Sub(v, nextPoint, prevPoint);
v2Scale(v, v, smooth);
var d0 = v2Distance(point, prevPoint);
var d1 = v2Distance(point, nextPoint);
var sum = d0 + d1;
if (sum !== 0) {
d0 /= sum;
d1 /= sum;
}
v2Scale(v1, v, -d0);
v2Scale(v2, v, d1);
var cp0 = v2Add([], point, v1);
var cp1 = v2Add([], point, v2);
if (constraint) {
v2Max(cp0, cp0, min);
v2Min(cp0, cp0, max);
v2Max(cp1, cp1, min);
v2Min(cp1, cp1, max);
}
cps.push(cp0);
cps.push(cp1);
}
if (isLoop) {
cps.push(cps.shift());
}
return cps;
}

View File

@ -0,0 +1,2 @@
import { VectorArray } from '../../core/vector';
export default function smoothSpline(points: VectorArray[], isLoop?: boolean): VectorArray[];

View File

@ -0,0 +1,44 @@
import { distance as v2Distance } from '../../core/vector.js';
function interpolate(p0, p1, p2, p3, t, t2, t3) {
var v0 = (p2 - p0) * 0.5;
var v1 = (p3 - p1) * 0.5;
return (2 * (p1 - p2) + v0 + v1) * t3
+ (-3 * (p1 - p2) - 2 * v0 - v1) * t2
+ v0 * t + p1;
}
export default function smoothSpline(points, isLoop) {
var len = points.length;
var ret = [];
var distance = 0;
for (var i = 1; i < len; i++) {
distance += v2Distance(points[i - 1], points[i]);
}
var segs = distance / 2;
segs = segs < len ? len : segs;
for (var i = 0; i < segs; i++) {
var pos = i / (segs - 1) * (isLoop ? len : len - 1);
var idx = Math.floor(pos);
var w = pos - idx;
var p0 = void 0;
var p1 = points[idx % len];
var p2 = void 0;
var p3 = void 0;
if (!isLoop) {
p0 = points[idx === 0 ? idx : idx - 1];
p2 = points[idx > len - 2 ? len - 1 : idx + 1];
p3 = points[idx > len - 3 ? len - 1 : idx + 2];
}
else {
p0 = points[(idx - 1 + len) % len];
p2 = points[(idx + 1) % len];
p3 = points[(idx + 2) % len];
}
var w2 = w * w;
var w3 = w * w2;
ret.push([
interpolate(p0[0], p1[0], p2[0], p3[0], w, w2, w3),
interpolate(p0[1], p1[1], p2[1], p3[1], w, w2, w3)
]);
}
return ret;
}

View File

@ -0,0 +1,18 @@
import { PathStyleProps } from '../Path';
declare type LineShape = {
x1: number;
y1: number;
x2: number;
y2: number;
};
declare type RectShape = {
x: number;
y: number;
width: number;
height: number;
r?: number | number[];
};
export declare function subPixelOptimizeLine(outputShape: Partial<LineShape>, inputShape: LineShape, style: Pick<PathStyleProps, 'lineWidth'>): LineShape;
export declare function subPixelOptimizeRect(outputShape: Partial<RectShape>, inputShape: RectShape, style: Pick<PathStyleProps, 'lineWidth'>): RectShape;
export declare function subPixelOptimize(position: number, lineWidth?: number, positiveOrNegative?: boolean): number;
export {};

View File

@ -0,0 +1,56 @@
var round = Math.round;
export function subPixelOptimizeLine(outputShape, inputShape, style) {
if (!inputShape) {
return;
}
var x1 = inputShape.x1;
var x2 = inputShape.x2;
var y1 = inputShape.y1;
var y2 = inputShape.y2;
outputShape.x1 = x1;
outputShape.x2 = x2;
outputShape.y1 = y1;
outputShape.y2 = y2;
var lineWidth = style && style.lineWidth;
if (!lineWidth) {
return outputShape;
}
if (round(x1 * 2) === round(x2 * 2)) {
outputShape.x1 = outputShape.x2 = subPixelOptimize(x1, lineWidth, true);
}
if (round(y1 * 2) === round(y2 * 2)) {
outputShape.y1 = outputShape.y2 = subPixelOptimize(y1, lineWidth, true);
}
return outputShape;
}
export function subPixelOptimizeRect(outputShape, inputShape, style) {
if (!inputShape) {
return;
}
var originX = inputShape.x;
var originY = inputShape.y;
var originWidth = inputShape.width;
var originHeight = inputShape.height;
outputShape.x = originX;
outputShape.y = originY;
outputShape.width = originWidth;
outputShape.height = originHeight;
var lineWidth = style && style.lineWidth;
if (!lineWidth) {
return outputShape;
}
outputShape.x = subPixelOptimize(originX, lineWidth, true);
outputShape.y = subPixelOptimize(originY, lineWidth, true);
outputShape.width = Math.max(subPixelOptimize(originX + originWidth, lineWidth, false) - outputShape.x, originWidth === 0 ? 0 : 1);
outputShape.height = Math.max(subPixelOptimize(originY + originHeight, lineWidth, false) - outputShape.y, originHeight === 0 ? 0 : 1);
return outputShape;
}
export function subPixelOptimize(position, lineWidth, positiveOrNegative) {
if (!lineWidth) {
return position;
}
var doubledPosition = round(position * 2);
return (doubledPosition + round(lineWidth)) % 2 === 0
? doubledPosition / 2
: (doubledPosition + (positiveOrNegative ? 1 : -1)) / 2;
}