223 lines
14 KiB
JavaScript
223 lines
14 KiB
JavaScript
shadow$provide.module$node_modules$pixi_DOT_js$lib$scene$text$canvas$CanvasTextGenerator = function(global, require, module, exports) {
|
|
function countSpaces(text) {
|
|
let count = 0;
|
|
for (let i = 0; i < text.length; i++) {
|
|
32 === text.charCodeAt(i) && count++;
|
|
}
|
|
return count;
|
|
}
|
|
var Color = require("module$node_modules$pixi_DOT_js$lib$color$Color");
|
|
global = require("module$node_modules$pixi_DOT_js$lib$maths$shapes$Rectangle");
|
|
var CanvasPool = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$texture$CanvasPool"), getCanvasBoundingBox = require("module$node_modules$pixi_DOT_js$lib$utils$canvas$getCanvasBoundingBox"), CanvasTextMetrics = require("module$node_modules$pixi_DOT_js$lib$scene$text$canvas$CanvasTextMetrics"), fontStringFromTextStyle = require("module$node_modules$pixi_DOT_js$lib$scene$text$canvas$utils$fontStringFromTextStyle"), getCanvasFillStyle = require("module$node_modules$pixi_DOT_js$lib$scene$text$canvas$utils$getCanvasFillStyle");
|
|
"use strict";
|
|
const tempRect = new global.Rectangle();
|
|
class CanvasTextGeneratorClass {
|
|
getCanvasAndContext(options) {
|
|
const {text, style, resolution = 1} = options;
|
|
var padding = style._getFinalPadding();
|
|
const measured = CanvasTextMetrics.CanvasTextMetrics.measureText(text || " ", style), width = Math.ceil(Math.ceil(Math.max(1, measured.width) + 2 * padding) * resolution), height = Math.ceil(Math.ceil(Math.max(1, measured.height) + 2 * padding) * resolution);
|
|
options = CanvasPool.CanvasPool.getOptimalCanvasAndContext(width, height);
|
|
this._renderTextToCanvas(style, padding, resolution, options, measured);
|
|
padding = style.trim ? getCanvasBoundingBox.getCanvasBoundingBox({canvas:options.canvas, width, height, resolution:1, output:tempRect}) : tempRect.set(0, 0, width, height);
|
|
return {canvasAndContext:options, frame:padding};
|
|
}
|
|
returnCanvasAndContext(canvasAndContext) {
|
|
CanvasPool.CanvasPool.returnCanvasAndContext(canvasAndContext);
|
|
}
|
|
_renderTextToCanvas(style, padding, resolution, canvasAndContext, measured) {
|
|
if (measured.runsByLine && 0 < measured.runsByLine.length) {
|
|
this._renderTaggedTextToCanvas(measured, style, padding, resolution, canvasAndContext);
|
|
} else {
|
|
var {canvas, context} = canvasAndContext, font = fontStringFromTextStyle.fontStringFromTextStyle(style), lines = measured.lines, lineHeight = measured.lineHeight, lineWidths = measured.lineWidths, maxLineWidth = measured.maxLineWidth, fontProperties = measured.fontProperties, height = canvas.height;
|
|
context.resetTransform();
|
|
context.scale(resolution, resolution);
|
|
context.textBaseline = style.textBaseline;
|
|
if (style._stroke?.width) {
|
|
var strokeStyle = style._stroke;
|
|
context.lineWidth = strokeStyle.width;
|
|
context.miterLimit = strokeStyle.miterLimit;
|
|
context.lineJoin = strokeStyle.join;
|
|
context.lineCap = strokeStyle.cap;
|
|
}
|
|
context.font = font;
|
|
var linePositionX;
|
|
font = style.dropShadow ? 2 : 1;
|
|
strokeStyle = (style._stroke?.width ?? 0) / 2;
|
|
var linePositionYShift = (lineHeight - fontProperties.fontSize) / 2;
|
|
0 > lineHeight - fontProperties.fontSize && (linePositionYShift = 0);
|
|
for (let i = 0; i < font; ++i) {
|
|
const dsOffsetText = (linePositionX = style.dropShadow && 0 === i) ? Math.ceil(Math.max(1, height) + 2 * padding) : 0;
|
|
var linePositionY = dsOffsetText * resolution;
|
|
linePositionX ? this._setupDropShadow(context, style, resolution, linePositionY) : (linePositionX = style._gradientBounds, linePositionY = style._gradientOffset, linePositionX ? this._setFillAndStrokeStyles(context, style, {width:linePositionX.width, height:linePositionX.height, lineHeight:linePositionX.height, lines:measured.lines}, padding, strokeStyle, linePositionY?.x ?? 0, linePositionY?.y ?? 0) : linePositionY ? this._setFillAndStrokeStyles(context, style, measured, padding, strokeStyle,
|
|
linePositionY.x, linePositionY.y) : this._setFillAndStrokeStyles(context, style, measured, padding, strokeStyle), context.shadowColor = "rgba(0,0,0,0)");
|
|
for (let j = 0; j < lines.length; j++) {
|
|
linePositionX = strokeStyle;
|
|
linePositionY = strokeStyle + j * lineHeight + fontProperties.ascent + linePositionYShift;
|
|
linePositionX += this._getAlignmentOffset(lineWidths[j], maxLineWidth, style.align);
|
|
let wordSpacing = 0;
|
|
if ("justify" === style.align && style.wordWrap && j < lines.length - 1) {
|
|
const spaces = countSpaces(lines[j]);
|
|
0 < spaces && (wordSpacing = (maxLineWidth - lineWidths[j]) / spaces);
|
|
}
|
|
style._stroke?.width && this._drawLetterSpacing(lines[j], style, canvasAndContext, linePositionX + padding, linePositionY + padding - dsOffsetText, !0, wordSpacing);
|
|
void 0 !== style._fill && this._drawLetterSpacing(lines[j], style, canvasAndContext, linePositionX + padding, linePositionY + padding - dsOffsetText, !1, wordSpacing);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
_renderTaggedTextToCanvas(measured, style, padding, resolution, canvasAndContext) {
|
|
const {canvas, context} = canvasAndContext, {runsByLine, lineWidths, maxLineWidth, lineAscents, lineHeights, hasDropShadow} = measured;
|
|
measured = canvas.height;
|
|
context.resetTransform();
|
|
context.scale(resolution, resolution);
|
|
context.textBaseline = style.textBaseline;
|
|
const passesCount = hasDropShadow ? 2 : 1;
|
|
var maxStrokeWidth = style._stroke?.width ?? 0;
|
|
for (var lineRuns$jscomp$0 of runsByLine) {
|
|
for (var run of lineRuns$jscomp$0) {
|
|
var w = run.style._stroke?.width ?? 0;
|
|
w > maxStrokeWidth && (maxStrokeWidth = w);
|
|
}
|
|
}
|
|
lineRuns$jscomp$0 = maxStrokeWidth / 2;
|
|
run = [];
|
|
for (maxStrokeWidth = 0; maxStrokeWidth < runsByLine.length; maxStrokeWidth++) {
|
|
var lineRuns$jscomp$1 = runsByLine[maxStrokeWidth];
|
|
w = [];
|
|
for (var run$jscomp$0 of lineRuns$jscomp$1) {
|
|
lineRuns$jscomp$1 = fontStringFromTextStyle.fontStringFromTextStyle(run$jscomp$0.style), context.font = lineRuns$jscomp$1, w.push({width:CanvasTextMetrics.CanvasTextMetrics._measureText(run$jscomp$0.text, run$jscomp$0.style.letterSpacing, context), font:lineRuns$jscomp$1});
|
|
}
|
|
run.push(w);
|
|
}
|
|
for (run$jscomp$0 = 0; run$jscomp$0 < passesCount; ++run$jscomp$0) {
|
|
w = (maxStrokeWidth = hasDropShadow && 0 === run$jscomp$0) ? Math.ceil(Math.max(1, measured) + 2 * padding) : 0;
|
|
lineRuns$jscomp$1 = w * resolution;
|
|
maxStrokeWidth || (context.shadowColor = "rgba(0,0,0,0)");
|
|
let currentY = lineRuns$jscomp$0;
|
|
for (let lineIndex = 0; lineIndex < runsByLine.length; lineIndex++) {
|
|
const lineRuns = runsByLine[lineIndex];
|
|
var lineWidth = lineWidths[lineIndex], lineAscent = lineAscents[lineIndex];
|
|
const currentLineHeight = lineHeights[lineIndex], lineRunData = run[lineIndex];
|
|
var linePositionX = lineRuns$jscomp$0;
|
|
linePositionX += this._getAlignmentOffset(lineWidth, maxLineWidth, style.align);
|
|
let wordSpacing = 0;
|
|
if ("justify" === style.align && style.wordWrap && lineIndex < runsByLine.length - 1) {
|
|
var totalSpaces = 0;
|
|
for (const run of lineRuns) {
|
|
totalSpaces += countSpaces(run.text);
|
|
}
|
|
0 < totalSpaces && (wordSpacing = (maxLineWidth - lineWidth) / totalSpaces);
|
|
}
|
|
lineWidth = currentY + lineAscent;
|
|
lineAscent = linePositionX + padding;
|
|
for (totalSpaces = 0; totalSpaces < lineRuns.length; totalSpaces++) {
|
|
var run$jscomp$1 = lineRuns[totalSpaces];
|
|
const {width:runWidth, font:runFont} = lineRunData[totalSpaces];
|
|
context.font = runFont;
|
|
context.textBaseline = run$jscomp$1.style.textBaseline;
|
|
if (run$jscomp$1.style._stroke?.width) {
|
|
const runStroke = run$jscomp$1.style._stroke;
|
|
context.lineWidth = runStroke.width;
|
|
context.miterLimit = runStroke.miterLimit;
|
|
context.lineJoin = runStroke.join;
|
|
context.lineCap = runStroke.cap;
|
|
if (maxStrokeWidth) {
|
|
if (run$jscomp$1.style.dropShadow) {
|
|
this._setupDropShadow(context, run$jscomp$1.style, resolution, lineRuns$jscomp$1);
|
|
} else {
|
|
run$jscomp$1 = countSpaces(run$jscomp$1.text);
|
|
lineAscent += runWidth + run$jscomp$1 * wordSpacing;
|
|
continue;
|
|
}
|
|
} else {
|
|
var runFontProps = CanvasTextMetrics.CanvasTextMetrics.measureFont(runFont);
|
|
runFontProps = run$jscomp$1.style.lineHeight || runFontProps.fontSize;
|
|
context.strokeStyle = getCanvasFillStyle.getCanvasFillStyle(runStroke, context, {width:runWidth, height:runFontProps, lineHeight:runFontProps, lines:[run$jscomp$1.text]}, 2 * padding, lineAscent - padding, currentY);
|
|
}
|
|
this._drawLetterSpacing(run$jscomp$1.text, run$jscomp$1.style, canvasAndContext, lineAscent, lineWidth + padding - w, !0, wordSpacing);
|
|
}
|
|
run$jscomp$1 = countSpaces(run$jscomp$1.text);
|
|
lineAscent += runWidth + run$jscomp$1 * wordSpacing;
|
|
}
|
|
lineAscent = linePositionX + padding;
|
|
for (linePositionX = 0; linePositionX < lineRuns.length; linePositionX++) {
|
|
totalSpaces = lineRuns[linePositionX];
|
|
const {width:runWidth, font:runFont} = lineRunData[linePositionX];
|
|
context.font = runFont;
|
|
context.textBaseline = totalSpaces.style.textBaseline;
|
|
if (void 0 !== totalSpaces.style._fill) {
|
|
if (maxStrokeWidth) {
|
|
if (totalSpaces.style.dropShadow) {
|
|
this._setupDropShadow(context, totalSpaces.style, resolution, lineRuns$jscomp$1);
|
|
} else {
|
|
totalSpaces = countSpaces(totalSpaces.text);
|
|
lineAscent += runWidth + totalSpaces * wordSpacing;
|
|
continue;
|
|
}
|
|
} else {
|
|
run$jscomp$1 = CanvasTextMetrics.CanvasTextMetrics.measureFont(runFont), run$jscomp$1 = totalSpaces.style.lineHeight || run$jscomp$1.fontSize, context.fillStyle = getCanvasFillStyle.getCanvasFillStyle(totalSpaces.style._fill, context, {width:runWidth, height:run$jscomp$1, lineHeight:run$jscomp$1, lines:[totalSpaces.text]}, 2 * padding, lineAscent - padding, currentY);
|
|
}
|
|
this._drawLetterSpacing(totalSpaces.text, totalSpaces.style, canvasAndContext, lineAscent, lineWidth + padding - w, !1, wordSpacing);
|
|
}
|
|
totalSpaces = countSpaces(totalSpaces.text);
|
|
lineAscent += runWidth + totalSpaces * wordSpacing;
|
|
}
|
|
currentY += currentLineHeight;
|
|
}
|
|
}
|
|
}
|
|
_setFillAndStrokeStyles(context, style, metrics, padding, halfStroke, offsetX = 0, offsetY = 0) {
|
|
context.fillStyle = style._fill ? getCanvasFillStyle.getCanvasFillStyle(style._fill, context, metrics, 2 * padding, offsetX, offsetY) : null;
|
|
style._stroke?.width && (context.strokeStyle = getCanvasFillStyle.getCanvasFillStyle(style._stroke, context, metrics, halfStroke + 2 * padding, offsetX, offsetY));
|
|
}
|
|
_setupDropShadow(context, style, resolution, dsOffsetShadow) {
|
|
context.fillStyle = "black";
|
|
context.strokeStyle = "black";
|
|
style = style.dropShadow;
|
|
var dropShadowAlpha = style.alpha;
|
|
context.shadowColor = Color.Color.shared.setValue(style.color).setAlpha(dropShadowAlpha).toRgbaString();
|
|
dropShadowAlpha = style.distance * resolution;
|
|
context.shadowBlur = style.blur * resolution;
|
|
context.shadowOffsetX = Math.cos(style.angle) * dropShadowAlpha;
|
|
context.shadowOffsetY = Math.sin(style.angle) * dropShadowAlpha + dsOffsetShadow;
|
|
}
|
|
_getAlignmentOffset(lineWidth, alignWidth, align) {
|
|
return "right" === align ? alignWidth - lineWidth : "center" === align ? (alignWidth - lineWidth) / 2 : 0;
|
|
}
|
|
_drawLetterSpacing(text, style, canvasAndContext, x, y, isStroke = !1, wordSpacing = 0) {
|
|
({context:canvasAndContext} = canvasAndContext);
|
|
style = style.letterSpacing;
|
|
var useExperimentalLetterSpacing = !1;
|
|
CanvasTextMetrics.CanvasTextMetrics.experimentalLetterSpacingSupported && (CanvasTextMetrics.CanvasTextMetrics.experimentalLetterSpacing ? (canvasAndContext.letterSpacing = `${style}px`, canvasAndContext.textLetterSpacing = `${style}px`, useExperimentalLetterSpacing = !0) : (canvasAndContext.letterSpacing = "0px", canvasAndContext.textLetterSpacing = "0px"));
|
|
if (0 !== style && !useExperimentalLetterSpacing || 0 !== wordSpacing) {
|
|
if (0 === wordSpacing || 0 !== style && !useExperimentalLetterSpacing) {
|
|
useExperimentalLetterSpacing = CanvasTextMetrics.CanvasTextMetrics.graphemeSegmenter(text);
|
|
text = canvasAndContext.measureText(text).width;
|
|
for (let i = 0; i < useExperimentalLetterSpacing.length; ++i) {
|
|
const currentChar = useExperimentalLetterSpacing[i];
|
|
isStroke ? canvasAndContext.strokeText(currentChar, x, y) : canvasAndContext.fillText(currentChar, x, y);
|
|
var currentWidth = "";
|
|
for (let j = i + 1; j < useExperimentalLetterSpacing.length; ++j) {
|
|
currentWidth += useExperimentalLetterSpacing[j];
|
|
}
|
|
currentWidth = canvasAndContext.measureText(currentWidth).width;
|
|
x += text - currentWidth + style;
|
|
" " === currentChar && (x += wordSpacing);
|
|
text = currentWidth;
|
|
}
|
|
} else {
|
|
for (style = text.split(" "), useExperimentalLetterSpacing = canvasAndContext.measureText(" ").width, text = 0; text < style.length; text++) {
|
|
isStroke ? canvasAndContext.strokeText(style[text], x, y) : canvasAndContext.fillText(style[text], x, y), x += canvasAndContext.measureText(style[text]).width + useExperimentalLetterSpacing + wordSpacing;
|
|
}
|
|
}
|
|
} else {
|
|
isStroke ? canvasAndContext.strokeText(text, x, y) : canvasAndContext.fillText(text, x, y);
|
|
}
|
|
}
|
|
}
|
|
require = new CanvasTextGeneratorClass();
|
|
exports.CanvasTextGenerator = require;
|
|
};
|
|
|
|
//# sourceMappingURL=module$node_modules$pixi_DOT_js$lib$scene$text$canvas$CanvasTextGenerator.js.map
|