@galacean/effects-plugin-rich-text 2.7.4 → 2.8.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/alipay.js +1 -1
- package/dist/alipay.js.map +1 -1
- package/dist/alipay.mjs +1 -1
- package/dist/alipay.mjs.map +1 -1
- package/dist/douyin.js +1 -1
- package/dist/douyin.js.map +1 -1
- package/dist/douyin.mjs +1 -1
- package/dist/douyin.mjs.map +1 -1
- package/dist/index.js +992 -272
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +1 -1
- package/dist/index.mjs +993 -273
- package/dist/index.mjs.map +1 -1
- package/dist/rich-text-component.d.ts +49 -17
- package/dist/rich-text-layout.d.ts +54 -0
- package/dist/strategies/align/rich-horizontal-align.d.ts +10 -0
- package/dist/strategies/align/rich-vertical-align.d.ts +10 -0
- package/dist/strategies/overflow/rich-clipped-overflow.d.ts +10 -0
- package/dist/strategies/overflow/rich-display-overflow.d.ts +10 -0
- package/dist/strategies/overflow/rich-visible-overflow.d.ts +10 -0
- package/dist/strategies/rich-text-factory.d.ts +26 -0
- package/dist/strategies/rich-text-interfaces.d.ts +104 -0
- package/dist/strategies/wrap/rich-wrap-disabled.d.ts +15 -0
- package/dist/strategies/wrap/rich-wrap-enabled.d.ts +15 -0
- package/dist/weapp.js +1 -1
- package/dist/weapp.js.map +1 -1
- package/dist/weapp.mjs +1 -1
- package/dist/weapp.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
* Description: Galacean Effects player rich text plugin
|
|
4
4
|
* Author: Ant Group CO., Ltd.
|
|
5
5
|
* Contributors: 云垣
|
|
6
|
-
* Version: v2.
|
|
6
|
+
* Version: v2.8.0-alpha.0
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import * as EFFECTS from '@galacean/effects';
|
|
10
|
-
import { AbstractPlugin,
|
|
10
|
+
import { AbstractPlugin, spec, effectsClass, applyMixins, TextComponentBase, TextStyle, math, assertExist, MaskableGraphic, registerPlugin, VFXItem, logger } from '@galacean/effects';
|
|
11
11
|
|
|
12
12
|
function _set_prototype_of(o, p) {
|
|
13
13
|
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
|
|
@@ -330,6 +330,119 @@ typeof SuppressedError === "function" ? SuppressedError : function _SuppressedEr
|
|
|
330
330
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
331
331
|
};
|
|
332
332
|
|
|
333
|
+
var RichTextLayout = /*#__PURE__*/ function() {
|
|
334
|
+
function RichTextLayout(options) {
|
|
335
|
+
this.width = 0;
|
|
336
|
+
this.height = 0;
|
|
337
|
+
var size = options.size, _options_textOverflow = options.textOverflow, textOverflow = _options_textOverflow === void 0 ? spec.TextOverflow.clip : _options_textOverflow, _options_textVerticalAlign = options.textVerticalAlign, textVerticalAlign = _options_textVerticalAlign === void 0 ? spec.TextVerticalAlign.middle : _options_textVerticalAlign, _options_textAlign = options.textAlign, textAlign = _options_textAlign === void 0 ? spec.TextAlignment.left : _options_textAlign, _options_letterSpace = options.letterSpace, letterSpace = _options_letterSpace === void 0 ? 0 : _options_letterSpace, _options_lineHeight = options.lineHeight, lineHeight = _options_lineHeight === void 0 ? 0.571 : _options_lineHeight, _options_wrapEnabled = options.wrapEnabled, wrapEnabled = _options_wrapEnabled === void 0 ? false : _options_wrapEnabled, _options_maxTextWidth = options.maxTextWidth, maxTextWidth = _options_maxTextWidth === void 0 ? 350 : _options_maxTextWidth, _options_maxTextHeight = options.maxTextHeight, maxTextHeight = _options_maxTextHeight === void 0 ? 1000 : _options_maxTextHeight, _options_sizeMode = options.sizeMode, sizeMode = _options_sizeMode === void 0 ? spec.TextSizeMode.autoWidth : _options_sizeMode, _options_useLegacyRichText = options.// @ts-expect-error 兼容旧版
|
|
338
|
+
useLegacyRichText, useLegacyRichText = _options_useLegacyRichText === void 0 ? false : _options_useLegacyRichText;
|
|
339
|
+
this.letterSpace = letterSpace;
|
|
340
|
+
this.lineHeight = lineHeight * 300;
|
|
341
|
+
this.useLegacyRichText = useLegacyRichText;
|
|
342
|
+
this.overflow = textOverflow;
|
|
343
|
+
this.textVerticalAlign = textVerticalAlign;
|
|
344
|
+
this.textAlign = textAlign;
|
|
345
|
+
this.width = size ? size[0] : 100;
|
|
346
|
+
this.height = size ? size[1] : 100;
|
|
347
|
+
this.wrapEnabled = wrapEnabled;
|
|
348
|
+
this.maxTextWidth = maxTextWidth;
|
|
349
|
+
this.maxTextHeight = maxTextHeight;
|
|
350
|
+
this.sizeMode = sizeMode;
|
|
351
|
+
}
|
|
352
|
+
var _proto = RichTextLayout.prototype;
|
|
353
|
+
_proto.getOffsetY = function getOffsetY(style, lineCount, lineHeight, fontSize, totalLineHeight) {
|
|
354
|
+
var outlineWidth = style.outlineWidth, fontScale = style.fontScale;
|
|
355
|
+
// /3 计算Y轴偏移量,以匹配编辑器行为
|
|
356
|
+
var offsetY = (lineHeight - fontSize) / 3;
|
|
357
|
+
// 计算基础偏移量
|
|
358
|
+
var baseOffset = fontSize + outlineWidth * fontScale;
|
|
359
|
+
var commonCalculation = totalLineHeight !== undefined ? totalLineHeight : lineHeight * (lineCount - 1);
|
|
360
|
+
var offsetResult = 0;
|
|
361
|
+
switch(this.textVerticalAlign){
|
|
362
|
+
case spec.TextVerticalAlign.top:
|
|
363
|
+
offsetResult = baseOffset + offsetY;
|
|
364
|
+
break;
|
|
365
|
+
case spec.TextVerticalAlign.middle:
|
|
366
|
+
offsetResult = (this.height * fontScale - commonCalculation + baseOffset) / 2;
|
|
367
|
+
break;
|
|
368
|
+
case spec.TextVerticalAlign.bottom:
|
|
369
|
+
offsetResult = this.height * fontScale - commonCalculation - offsetY;
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
return offsetResult;
|
|
373
|
+
};
|
|
374
|
+
_proto.getOffsetX = function getOffsetX(style, maxWidth) {
|
|
375
|
+
var offsetX = 0;
|
|
376
|
+
switch(this.textAlign){
|
|
377
|
+
case spec.TextAlignment.left:
|
|
378
|
+
offsetX = style.outlineWidth * style.fontScale;
|
|
379
|
+
break;
|
|
380
|
+
case spec.TextAlignment.middle:
|
|
381
|
+
offsetX = (this.width * style.fontScale - maxWidth) / 2;
|
|
382
|
+
break;
|
|
383
|
+
case spec.TextAlignment.right:
|
|
384
|
+
offsetX = this.width * style.fontScale - maxWidth;
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
return offsetX;
|
|
388
|
+
};
|
|
389
|
+
/**
|
|
390
|
+
* 富文本垂直对齐计算
|
|
391
|
+
* @param style - 字体样式
|
|
392
|
+
* @param lineHeights - 每行高度数组
|
|
393
|
+
* @param fontSize - 字体大小
|
|
394
|
+
* @returns 第一行基线的Y坐标
|
|
395
|
+
*/ _proto.getOffsetYRich = function getOffsetYRich(style, lineHeights, fontSize) {
|
|
396
|
+
if (lineHeights.length === 0) {
|
|
397
|
+
// 空文本时返回 0,避免计算异常;后续策略可按需要再调整
|
|
398
|
+
return 0;
|
|
399
|
+
}
|
|
400
|
+
var outlineWidth = style.outlineWidth, fontScale = style.fontScale;
|
|
401
|
+
var total = lineHeights.reduce(function(a, b) {
|
|
402
|
+
return a + b;
|
|
403
|
+
}, 0);
|
|
404
|
+
var offsetY = (lineHeights[0] - fontSize) / 3;
|
|
405
|
+
var baseOffset = fontSize + outlineWidth * fontScale;
|
|
406
|
+
var commonCalculation = total - lineHeights[0];
|
|
407
|
+
var offsetResult = 0;
|
|
408
|
+
switch(this.textVerticalAlign){
|
|
409
|
+
case spec.TextVerticalAlign.top:
|
|
410
|
+
offsetResult = baseOffset + offsetY;
|
|
411
|
+
break;
|
|
412
|
+
case spec.TextVerticalAlign.middle:
|
|
413
|
+
offsetResult = (this.height * fontScale - total) / 2 + baseOffset;
|
|
414
|
+
break;
|
|
415
|
+
case spec.TextVerticalAlign.bottom:
|
|
416
|
+
offsetResult = this.height * fontScale - commonCalculation - offsetY;
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
return offsetResult;
|
|
420
|
+
};
|
|
421
|
+
/**
|
|
422
|
+
* 富文本横向对齐计算
|
|
423
|
+
* @param style - 字体样式
|
|
424
|
+
* @param maxWidth - 文本框最大宽度
|
|
425
|
+
* @param contentW - 文本实际宽度
|
|
426
|
+
* @returns 水平偏移量
|
|
427
|
+
*/ _proto.getOffsetXRich = function getOffsetXRich(style, maxWidth, contentW) {
|
|
428
|
+
switch(this.textAlign){
|
|
429
|
+
case spec.TextAlignment.left:
|
|
430
|
+
return style.outlineWidth;
|
|
431
|
+
case spec.TextAlignment.middle:
|
|
432
|
+
return (maxWidth - contentW) / 2;
|
|
433
|
+
case spec.TextAlignment.right:
|
|
434
|
+
return maxWidth - contentW;
|
|
435
|
+
default:
|
|
436
|
+
return 0;
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
_proto.setSize = function setSize(width, height) {
|
|
440
|
+
this.width = width;
|
|
441
|
+
this.height = height;
|
|
442
|
+
};
|
|
443
|
+
return RichTextLayout;
|
|
444
|
+
}();
|
|
445
|
+
|
|
333
446
|
/**
|
|
334
447
|
* 将颜色名称转换为 RGBA
|
|
335
448
|
* @param colorName - 颜色名称
|
|
@@ -402,31 +515,620 @@ typeof SuppressedError === "function" ? SuppressedError : function _SuppressedEr
|
|
|
402
515
|
}
|
|
403
516
|
}
|
|
404
517
|
|
|
518
|
+
/**
|
|
519
|
+
* 富文本换行禁用策略
|
|
520
|
+
* 实现逻辑:仅基于\n换行,无自动换行
|
|
521
|
+
*/ var RichWrapDisabledStrategy = /*#__PURE__*/ function() {
|
|
522
|
+
function RichWrapDisabledStrategy() {}
|
|
523
|
+
var _proto = RichWrapDisabledStrategy.prototype;
|
|
524
|
+
_proto.computeLines = function computeLines(processedOptions, context, style, layout, singleLineHeight, fontScale, letterSpace, scaleFactor) {
|
|
525
|
+
var _this = this;
|
|
526
|
+
var lines = [];
|
|
527
|
+
var baselines = [];
|
|
528
|
+
var gapPx = (layout.lineHeight || 0) * fontScale;
|
|
529
|
+
var currentLine = this.createNewLine();
|
|
530
|
+
var maxLineWidth = 0;
|
|
531
|
+
var totalHeight = 0;
|
|
532
|
+
var finishCurrentLine = function() {
|
|
533
|
+
if (currentLine.chars.length === 0) {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
// 第1行用真实首行高度,其余行用 gapPx
|
|
537
|
+
currentLine.lineHeight = lines.length === 0 ? (currentLine.lineAscent || 0) + (currentLine.lineDescent || 0) : gapPx;
|
|
538
|
+
// 记录本行基线
|
|
539
|
+
var baseline = lines.length === 0 ? 0 : baselines[baselines.length - 1] + gapPx;
|
|
540
|
+
baselines.push(baseline);
|
|
541
|
+
totalHeight += currentLine.lineHeight;
|
|
542
|
+
lines.push(currentLine);
|
|
543
|
+
maxLineWidth = Math.max(maxLineWidth, currentLine.width);
|
|
544
|
+
currentLine = _this.createNewLine();
|
|
545
|
+
};
|
|
546
|
+
processedOptions.forEach(function(options) {
|
|
547
|
+
var text = options.text, isNewLine = options.isNewLine, fontSize = options.fontSize;
|
|
548
|
+
if (isNewLine) {
|
|
549
|
+
// 先结束上一行
|
|
550
|
+
finishCurrentLine();
|
|
551
|
+
}
|
|
552
|
+
// 用当前片段字体(权重/家族)设置测量字体
|
|
553
|
+
var fontStyle = options.fontStyle || style.fontStyle || "normal";
|
|
554
|
+
context.font = fontStyle + " " + (options.fontWeight || style.textWeight) + " 10px " + (options.fontFamily || style.fontFamily);
|
|
555
|
+
// 记录段起始 x
|
|
556
|
+
currentLine.offsetX.push(currentLine.width);
|
|
557
|
+
// 逐字宽度和高度测量(asc/desc 测量)
|
|
558
|
+
var segmentInnerX = 0;
|
|
559
|
+
var charArr = [];
|
|
560
|
+
var lineAscent = 0;
|
|
561
|
+
var lineDescent = 0;
|
|
562
|
+
for(var i = 0; i < text.length; i++){
|
|
563
|
+
var ch = text[i];
|
|
564
|
+
var m = context.measureText(ch);
|
|
565
|
+
var w = m.width;
|
|
566
|
+
var charWidth = (w <= 0 ? 0 : w) * fontSize * scaleFactor * fontScale;
|
|
567
|
+
// 测量 asc/desc 并按目标字号缩放
|
|
568
|
+
var scale = fontSize * fontScale * scaleFactor;
|
|
569
|
+
var asc = m.actualBoundingBoxAscent * scale;
|
|
570
|
+
var desc = m.actualBoundingBoxDescent * scale;
|
|
571
|
+
lineAscent = Math.max(lineAscent, asc);
|
|
572
|
+
lineDescent = Math.max(lineDescent, desc);
|
|
573
|
+
if (i > 0) {
|
|
574
|
+
segmentInnerX += letterSpace; // 先加"前一个字符与当前字符之间"的间距
|
|
575
|
+
}
|
|
576
|
+
charArr.push({
|
|
577
|
+
char: ch,
|
|
578
|
+
x: segmentInnerX
|
|
579
|
+
});
|
|
580
|
+
segmentInnerX += charWidth;
|
|
581
|
+
}
|
|
582
|
+
currentLine.chars.push(charArr);
|
|
583
|
+
currentLine.width += segmentInnerX;
|
|
584
|
+
currentLine.richOptions.push(options);
|
|
585
|
+
// 累计行级 asc/desc
|
|
586
|
+
currentLine.lineAscent = Math.max(currentLine.lineAscent || 0, lineAscent);
|
|
587
|
+
currentLine.lineDescent = Math.max(currentLine.lineDescent || 0, lineDescent);
|
|
588
|
+
});
|
|
589
|
+
// 结束最后一行
|
|
590
|
+
finishCurrentLine();
|
|
591
|
+
// 计算 bbox
|
|
592
|
+
var bboxTop = Infinity;
|
|
593
|
+
var bboxBottom = -Infinity;
|
|
594
|
+
if (lines.length === 0) {
|
|
595
|
+
return {
|
|
596
|
+
lines: lines,
|
|
597
|
+
maxLineWidth: maxLineWidth,
|
|
598
|
+
totalHeight: totalHeight,
|
|
599
|
+
bboxTop: 0,
|
|
600
|
+
bboxBottom: 0,
|
|
601
|
+
bboxHeight: 0
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
for(var i = 0; i < lines.length; i++){
|
|
605
|
+
bboxTop = Math.min(bboxTop, baselines[i] - (lines[i].lineAscent || 0));
|
|
606
|
+
bboxBottom = Math.max(bboxBottom, baselines[i] + (lines[i].lineDescent || 0));
|
|
607
|
+
}
|
|
608
|
+
var bboxHeight = bboxBottom - bboxTop;
|
|
609
|
+
return {
|
|
610
|
+
lines: lines,
|
|
611
|
+
maxLineWidth: maxLineWidth,
|
|
612
|
+
totalHeight: totalHeight,
|
|
613
|
+
bboxTop: bboxTop,
|
|
614
|
+
bboxBottom: bboxBottom,
|
|
615
|
+
bboxHeight: bboxHeight
|
|
616
|
+
};
|
|
617
|
+
};
|
|
618
|
+
/**
|
|
619
|
+
* 创建新行
|
|
620
|
+
*/ _proto.createNewLine = function createNewLine() {
|
|
621
|
+
return {
|
|
622
|
+
richOptions: [],
|
|
623
|
+
offsetX: [],
|
|
624
|
+
width: 0,
|
|
625
|
+
lineHeight: 0,
|
|
626
|
+
offsetY: 0,
|
|
627
|
+
chars: [],
|
|
628
|
+
lineAscent: 0,
|
|
629
|
+
lineDescent: 0
|
|
630
|
+
};
|
|
631
|
+
};
|
|
632
|
+
return RichWrapDisabledStrategy;
|
|
633
|
+
}();
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* 富文本自动换行策略
|
|
637
|
+
* 实现逻辑:基于\n换行及自动换行
|
|
638
|
+
*/ var RichWrapEnabledStrategy = /*#__PURE__*/ function() {
|
|
639
|
+
function RichWrapEnabledStrategy() {}
|
|
640
|
+
var _proto = RichWrapEnabledStrategy.prototype;
|
|
641
|
+
_proto.computeLines = function computeLines(processedOptions, context, style, layout, singleLineHeight, fontScale, letterSpace, scaleFactor) {
|
|
642
|
+
var _this = this;
|
|
643
|
+
var lines = [];
|
|
644
|
+
var baselines = [];
|
|
645
|
+
var gapPx = (layout.lineHeight || 0) * fontScale;
|
|
646
|
+
var currentLine = this.createNewLine();
|
|
647
|
+
var maxLineWidth = 0;
|
|
648
|
+
var totalHeight = 0;
|
|
649
|
+
// 切块缓冲变量
|
|
650
|
+
var segStartX = null;
|
|
651
|
+
var segmentInnerX = 0;
|
|
652
|
+
var chunkChars = [];
|
|
653
|
+
var chunkOptions = null; // 跟踪当前切块所属options
|
|
654
|
+
// 结束当前行(含缓冲切块)
|
|
655
|
+
var finishCurrentLine = function() {
|
|
656
|
+
flushChunk();
|
|
657
|
+
if (currentLine.chars.length === 0) {
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
// 第1行用真实首行高度,其余行用 gapPx
|
|
661
|
+
currentLine.lineHeight = lines.length === 0 ? (currentLine.lineAscent || 0) + (currentLine.lineDescent || 0) : gapPx;
|
|
662
|
+
// 记录本行基线
|
|
663
|
+
var baseline = lines.length === 0 ? 0 : baselines[baselines.length - 1] + gapPx;
|
|
664
|
+
baselines.push(baseline);
|
|
665
|
+
totalHeight += currentLine.lineHeight;
|
|
666
|
+
lines.push(currentLine);
|
|
667
|
+
maxLineWidth = Math.max(maxLineWidth, currentLine.width);
|
|
668
|
+
currentLine = _this.createNewLine();
|
|
669
|
+
segStartX = null;
|
|
670
|
+
segmentInnerX = 0;
|
|
671
|
+
chunkChars = [];
|
|
672
|
+
chunkOptions = null;
|
|
673
|
+
};
|
|
674
|
+
// 刷新当前切块
|
|
675
|
+
var flushChunk = function() {
|
|
676
|
+
if (chunkChars.length === 0) {
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
if (segStartX === null) {
|
|
680
|
+
segStartX = currentLine.width;
|
|
681
|
+
}
|
|
682
|
+
currentLine.offsetX.push(segStartX);
|
|
683
|
+
currentLine.chars.push(chunkChars);
|
|
684
|
+
if (chunkOptions) {
|
|
685
|
+
currentLine.richOptions.push(chunkOptions);
|
|
686
|
+
}
|
|
687
|
+
// 重置缓冲
|
|
688
|
+
segStartX = null;
|
|
689
|
+
segmentInnerX = 0;
|
|
690
|
+
chunkChars = [];
|
|
691
|
+
chunkOptions = null;
|
|
692
|
+
};
|
|
693
|
+
processedOptions.forEach(function(options) {
|
|
694
|
+
var text = options.text, isNewLine = options.isNewLine, fontSize = options.fontSize;
|
|
695
|
+
// 设置测量字体(包含fontStyle)
|
|
696
|
+
var fontStyle = options.fontStyle || style.fontStyle || "normal";
|
|
697
|
+
context.font = fontStyle + " " + (options.fontWeight || style.textWeight) + " 10px " + (options.fontFamily || style.fontFamily);
|
|
698
|
+
// 显式换行符优先处理
|
|
699
|
+
if (isNewLine) {
|
|
700
|
+
flushChunk();
|
|
701
|
+
finishCurrentLine();
|
|
702
|
+
}
|
|
703
|
+
// 逐字符处理
|
|
704
|
+
for(var i = 0; i < text.length; i++){
|
|
705
|
+
var ch = text[i];
|
|
706
|
+
// 获取基础宽度并计算实际宽度(动态缩放)
|
|
707
|
+
var m = context.measureText(ch);
|
|
708
|
+
var baseW = m.width;
|
|
709
|
+
var charWidth = (baseW <= 0 ? 0 : baseW) * fontSize * scaleFactor * fontScale;
|
|
710
|
+
// 测量 asc/desc 并按目标字号缩放
|
|
711
|
+
var scale = fontSize * fontScale * scaleFactor;
|
|
712
|
+
var asc = m.actualBoundingBoxAscent * scale;
|
|
713
|
+
var desc = m.actualBoundingBoxDescent * scale;
|
|
714
|
+
// 计算预期宽度(包含字符间距)
|
|
715
|
+
var spacing = chunkChars.length > 0 ? letterSpace : 0;
|
|
716
|
+
var willWidth = currentLine.width + spacing + charWidth;
|
|
717
|
+
// 自动换行判断
|
|
718
|
+
if (willWidth > (layout.maxTextWidth || Infinity)) {
|
|
719
|
+
flushChunk();
|
|
720
|
+
finishCurrentLine();
|
|
721
|
+
}
|
|
722
|
+
// 初始化切块起点和所属options
|
|
723
|
+
if (segStartX === null) {
|
|
724
|
+
segStartX = currentLine.width;
|
|
725
|
+
chunkOptions = options; // 绑定当前切块到options
|
|
726
|
+
}
|
|
727
|
+
// 添加字符间距(非首字符)
|
|
728
|
+
if (chunkChars.length > 0) {
|
|
729
|
+
segmentInnerX += letterSpace;
|
|
730
|
+
currentLine.width += letterSpace;
|
|
731
|
+
}
|
|
732
|
+
// 添加字符到切块(x为切块内相对坐标)
|
|
733
|
+
chunkChars.push({
|
|
734
|
+
char: ch,
|
|
735
|
+
x: segmentInnerX
|
|
736
|
+
});
|
|
737
|
+
// 累计行级 asc/desc
|
|
738
|
+
currentLine.lineAscent = Math.max(currentLine.lineAscent || 0, asc);
|
|
739
|
+
currentLine.lineDescent = Math.max(currentLine.lineDescent || 0, desc);
|
|
740
|
+
// 更新位置和宽度
|
|
741
|
+
segmentInnerX += charWidth;
|
|
742
|
+
currentLine.width += charWidth;
|
|
743
|
+
}
|
|
744
|
+
// 文本段结束,落盘当前切块
|
|
745
|
+
flushChunk();
|
|
746
|
+
});
|
|
747
|
+
// 结束最后一行
|
|
748
|
+
finishCurrentLine();
|
|
749
|
+
// 计算 bbox
|
|
750
|
+
var bboxTop = Infinity;
|
|
751
|
+
var bboxBottom = -Infinity;
|
|
752
|
+
for(var i = 0; i < lines.length; i++){
|
|
753
|
+
bboxTop = Math.min(bboxTop, baselines[i] - (lines[i].lineAscent || 0));
|
|
754
|
+
bboxBottom = Math.max(bboxBottom, baselines[i] + (lines[i].lineDescent || 0));
|
|
755
|
+
}
|
|
756
|
+
var bboxHeight = bboxBottom - bboxTop;
|
|
757
|
+
return {
|
|
758
|
+
lines: lines,
|
|
759
|
+
maxLineWidth: maxLineWidth,
|
|
760
|
+
totalHeight: totalHeight,
|
|
761
|
+
bboxTop: bboxTop,
|
|
762
|
+
bboxBottom: bboxBottom,
|
|
763
|
+
bboxHeight: bboxHeight
|
|
764
|
+
};
|
|
765
|
+
};
|
|
766
|
+
/**
|
|
767
|
+
* 创建新行
|
|
768
|
+
*/ _proto.createNewLine = function createNewLine() {
|
|
769
|
+
return {
|
|
770
|
+
richOptions: [],
|
|
771
|
+
offsetX: [],
|
|
772
|
+
width: 0,
|
|
773
|
+
lineHeight: 0,
|
|
774
|
+
offsetY: 0,
|
|
775
|
+
chars: [],
|
|
776
|
+
lineAscent: 0,
|
|
777
|
+
lineDescent: 0
|
|
778
|
+
};
|
|
779
|
+
};
|
|
780
|
+
return RichWrapEnabledStrategy;
|
|
781
|
+
}();
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* 富文本 Display 溢出策略
|
|
785
|
+
* 在溢出策略中对所有几何量进行一次性缩放
|
|
786
|
+
*/ var RichDisplayOverflowStrategy = /*#__PURE__*/ function() {
|
|
787
|
+
function RichDisplayOverflowStrategy() {}
|
|
788
|
+
var _proto = RichDisplayOverflowStrategy.prototype;
|
|
789
|
+
_proto.apply = function apply(lines, sizeResult, layout, style) {
|
|
790
|
+
// 安全处理除零情况
|
|
791
|
+
var safeDiv = function(a, b) {
|
|
792
|
+
return Math.abs(b) > 1e-5 ? a / b : 1;
|
|
793
|
+
};
|
|
794
|
+
var availableW = Math.max(1, layout.maxTextWidth || sizeResult.canvasWidth);
|
|
795
|
+
var availableH = Math.max(1, layout.maxTextHeight || sizeResult.canvasHeight);
|
|
796
|
+
var _sizeResult_contentWidth;
|
|
797
|
+
var contentW = Math.max(1, (_sizeResult_contentWidth = sizeResult.contentWidth) != null ? _sizeResult_contentWidth : sizeResult.canvasWidth);
|
|
798
|
+
var _sizeResult_bboxHeight;
|
|
799
|
+
var contentH = Math.max(1, (_sizeResult_bboxHeight = sizeResult.bboxHeight) != null ? _sizeResult_bboxHeight : sizeResult.canvasHeight);
|
|
800
|
+
// 只缩小不放大
|
|
801
|
+
var s = Math.min(1, safeDiv(availableW, contentW), safeDiv(availableH, contentH));
|
|
802
|
+
// 浮点精度处理
|
|
803
|
+
if (s > 0.9999) {
|
|
804
|
+
return {
|
|
805
|
+
globalScale: 1
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
// 统一缩放所有几何量
|
|
809
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(lines), _step; !(_step = _iterator()).done;){
|
|
810
|
+
var line = _step.value;
|
|
811
|
+
line.width *= s;
|
|
812
|
+
line.lineHeight *= s; // 缩小行距(你的 gap-only 模式行高=gapPx)
|
|
813
|
+
if (line.offsetX) {
|
|
814
|
+
for(var i = 0; i < line.offsetX.length; i++){
|
|
815
|
+
line.offsetX[i] *= s;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
for(var _iterator1 = _create_for_of_iterator_helper_loose(line.chars || []), _step1; !(_step1 = _iterator1()).done;){
|
|
819
|
+
var seg = _step1.value;
|
|
820
|
+
for(var _iterator2 = _create_for_of_iterator_helper_loose(seg), _step2; !(_step2 = _iterator2()).done;){
|
|
821
|
+
var ch = _step2.value;
|
|
822
|
+
ch.x *= s;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
for(var _iterator3 = _create_for_of_iterator_helper_loose(line.richOptions || []), _step3; !(_step3 = _iterator3()).done;){
|
|
826
|
+
var opt = _step3.value;
|
|
827
|
+
opt.fontSize *= s; // 缩小字形
|
|
828
|
+
}
|
|
829
|
+
// 同步缩放 asc/desc
|
|
830
|
+
if (line.lineAscent != null) {
|
|
831
|
+
line.lineAscent *= s;
|
|
832
|
+
}
|
|
833
|
+
if (line.lineDescent != null) {
|
|
834
|
+
line.lineDescent *= s;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
return {
|
|
838
|
+
globalScale: s
|
|
839
|
+
};
|
|
840
|
+
};
|
|
841
|
+
return RichDisplayOverflowStrategy;
|
|
842
|
+
}();
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* Clip 溢出策略
|
|
846
|
+
* 超出画布部分自然裁切
|
|
847
|
+
*/ var RichClippedOverflowStrategy = /*#__PURE__*/ function() {
|
|
848
|
+
function RichClippedOverflowStrategy() {}
|
|
849
|
+
var _proto = RichClippedOverflowStrategy.prototype;
|
|
850
|
+
_proto.apply = function apply(lines, sizeResult, layout, style) {
|
|
851
|
+
// 不进行任何缩放,直接返回单位缩放系数
|
|
852
|
+
return {
|
|
853
|
+
globalScale: 1
|
|
854
|
+
};
|
|
855
|
+
};
|
|
856
|
+
return RichClippedOverflowStrategy;
|
|
857
|
+
}();
|
|
858
|
+
|
|
859
|
+
/**
|
|
860
|
+
* Visible 溢出策略
|
|
861
|
+
* 允许内容超出画布可见
|
|
862
|
+
*/ var RichVisibleOverflowStrategy = /*#__PURE__*/ function() {
|
|
863
|
+
function RichVisibleOverflowStrategy() {}
|
|
864
|
+
var _proto = RichVisibleOverflowStrategy.prototype;
|
|
865
|
+
_proto.apply = function apply(lines, sizeResult, layout, style) {
|
|
866
|
+
// 不进行任何缩放或裁剪
|
|
867
|
+
return {
|
|
868
|
+
globalScale: 1
|
|
869
|
+
};
|
|
870
|
+
};
|
|
871
|
+
return RichVisibleOverflowStrategy;
|
|
872
|
+
}();
|
|
873
|
+
|
|
874
|
+
/**
|
|
875
|
+
* 富文本水平对齐策略
|
|
876
|
+
* 直接使用已缩放的行宽计算偏移量
|
|
877
|
+
*/ var RichHorizontalAlignStrategyImpl = /*#__PURE__*/ function() {
|
|
878
|
+
function RichHorizontalAlignStrategyImpl() {}
|
|
879
|
+
var _proto = RichHorizontalAlignStrategyImpl.prototype;
|
|
880
|
+
_proto.getHorizontalOffsets = function getHorizontalOffsets(lines, sizeResult, overflowResult, layout, style) {
|
|
881
|
+
var frameW = layout.maxTextWidth;
|
|
882
|
+
var compX = sizeResult.baselineCompensationX || 0;
|
|
883
|
+
// 统一用 frame 宽做容器
|
|
884
|
+
var baseOffsets = lines.map(function(line) {
|
|
885
|
+
return layout.getOffsetXRich(style, frameW, line.width);
|
|
886
|
+
});
|
|
887
|
+
// 仅 visible 叠加水平补偿,clip/display 不加
|
|
888
|
+
var lineOffsets = layout.overflow === spec.TextOverflow.visible ? baseOffsets.map(function(x) {
|
|
889
|
+
return x + compX;
|
|
890
|
+
}) : baseOffsets;
|
|
891
|
+
return {
|
|
892
|
+
lineOffsets: lineOffsets
|
|
893
|
+
};
|
|
894
|
+
};
|
|
895
|
+
return RichHorizontalAlignStrategyImpl;
|
|
896
|
+
}();
|
|
897
|
+
|
|
898
|
+
/**
|
|
899
|
+
* 富文本垂直对齐策略
|
|
900
|
+
* 直接使用已缩放的行高
|
|
901
|
+
*/ var RichVerticalAlignStrategyImpl = /*#__PURE__*/ function() {
|
|
902
|
+
function RichVerticalAlignStrategyImpl() {}
|
|
903
|
+
var _proto = RichVerticalAlignStrategyImpl.prototype;
|
|
904
|
+
_proto.getVerticalOffsets = function getVerticalOffsets(lines, sizeResult, overflowResult, layout, style, singleLineHeight) {
|
|
905
|
+
var lineHeights = lines.map(function(line) {
|
|
906
|
+
return line.lineHeight;
|
|
907
|
+
});
|
|
908
|
+
var compY = sizeResult.baselineCompensationY || 0;
|
|
909
|
+
var baselineY = 0;
|
|
910
|
+
if (lines.length === 0) {
|
|
911
|
+
return {
|
|
912
|
+
baselineY: 0,
|
|
913
|
+
lineYOffsets: []
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
switch(layout.overflow){
|
|
917
|
+
case spec.TextOverflow.visible:
|
|
918
|
+
{
|
|
919
|
+
// frame-based 计算
|
|
920
|
+
var frameH = layout.maxTextHeight;
|
|
921
|
+
var _sizeResult_bboxTop;
|
|
922
|
+
var bboxTop = (_sizeResult_bboxTop = sizeResult.bboxTop) != null ? _sizeResult_bboxTop : 0;
|
|
923
|
+
var _sizeResult_bboxHeight, _sizeResult_bboxBottom;
|
|
924
|
+
var bboxBottom = (_sizeResult_bboxBottom = sizeResult.bboxBottom) != null ? _sizeResult_bboxBottom : bboxTop + ((_sizeResult_bboxHeight = sizeResult.bboxHeight) != null ? _sizeResult_bboxHeight : 0);
|
|
925
|
+
var _sizeResult_bboxHeight1;
|
|
926
|
+
var bboxHeight = (_sizeResult_bboxHeight1 = sizeResult.bboxHeight) != null ? _sizeResult_bboxHeight1 : bboxBottom - bboxTop;
|
|
927
|
+
// 计算 frame 基线
|
|
928
|
+
var baselineYFrame = 0;
|
|
929
|
+
switch(layout.textVerticalAlign){
|
|
930
|
+
case spec.TextVerticalAlign.top:
|
|
931
|
+
baselineYFrame = -bboxTop;
|
|
932
|
+
break;
|
|
933
|
+
case spec.TextVerticalAlign.middle:
|
|
934
|
+
baselineYFrame = (frameH - bboxHeight) / 2 - bboxTop;
|
|
935
|
+
break;
|
|
936
|
+
case spec.TextVerticalAlign.bottom:
|
|
937
|
+
baselineYFrame = frameH - bboxHeight - bboxTop;
|
|
938
|
+
break;
|
|
939
|
+
}
|
|
940
|
+
baselineY = baselineYFrame + compY; // 关键:叠加"向下移动 E"
|
|
941
|
+
break;
|
|
942
|
+
}
|
|
943
|
+
case spec.TextOverflow.clip:
|
|
944
|
+
{
|
|
945
|
+
var firstLine = lines[0];
|
|
946
|
+
if (firstLine) {
|
|
947
|
+
var _Math;
|
|
948
|
+
var richSizes = firstLine.richOptions.map(function(o) {
|
|
949
|
+
return o.fontSize;
|
|
950
|
+
});
|
|
951
|
+
var firstMax = (_Math = Math).max.apply(_Math, [].concat(richSizes.length ? richSizes : [
|
|
952
|
+
style.fontSize
|
|
953
|
+
]));
|
|
954
|
+
var fontSizeForOffset = firstMax * style.fontScale * singleLineHeight;
|
|
955
|
+
baselineY = layout.getOffsetYRich(style, lineHeights, fontSizeForOffset) + compY;
|
|
956
|
+
} else {
|
|
957
|
+
baselineY = layout.getOffsetYRich(style, lineHeights, style.fontSize * style.fontScale) + compY;
|
|
958
|
+
}
|
|
959
|
+
break;
|
|
960
|
+
}
|
|
961
|
+
case spec.TextOverflow.display:
|
|
962
|
+
{
|
|
963
|
+
// display 垂直对齐改为基于 bbox,而不是 getOffsetYRich
|
|
964
|
+
var frameH1 = layout.maxTextHeight;
|
|
965
|
+
// 用缩放后的行数据重建 baselines 和 bbox
|
|
966
|
+
var baselines = [
|
|
967
|
+
0
|
|
968
|
+
];
|
|
969
|
+
for(var i = 1; i < lines.length; i++){
|
|
970
|
+
baselines[i] = baselines[i - 1] + lines[i].lineHeight;
|
|
971
|
+
}
|
|
972
|
+
// 计算 bboxTop / bboxBottom(使用行的测量值 lineAscent/lineDescent)
|
|
973
|
+
var bboxTop1 = Infinity;
|
|
974
|
+
var bboxBottom1 = -Infinity;
|
|
975
|
+
for(var i1 = 0; i1 < lines.length; i1++){
|
|
976
|
+
var _lines_i_lineAscent;
|
|
977
|
+
var asc = (_lines_i_lineAscent = lines[i1].lineAscent) != null ? _lines_i_lineAscent : 0;
|
|
978
|
+
var _lines_i_lineDescent;
|
|
979
|
+
var desc = (_lines_i_lineDescent = lines[i1].lineDescent) != null ? _lines_i_lineDescent : 0;
|
|
980
|
+
bboxTop1 = Math.min(bboxTop1, baselines[i1] - asc);
|
|
981
|
+
bboxBottom1 = Math.max(bboxBottom1, baselines[i1] + desc);
|
|
982
|
+
}
|
|
983
|
+
var bboxHeight1 = bboxBottom1 - bboxTop1;
|
|
984
|
+
// 计算 display 模式的基线
|
|
985
|
+
var baselineDisplayY = 0;
|
|
986
|
+
switch(layout.textVerticalAlign){
|
|
987
|
+
case spec.TextVerticalAlign.top:
|
|
988
|
+
baselineDisplayY = -bboxTop1;
|
|
989
|
+
break;
|
|
990
|
+
case spec.TextVerticalAlign.middle:
|
|
991
|
+
baselineDisplayY = (frameH1 - bboxHeight1) / 2 - bboxTop1;
|
|
992
|
+
break;
|
|
993
|
+
case spec.TextVerticalAlign.bottom:
|
|
994
|
+
baselineDisplayY = frameH1 - bboxHeight1 - bboxTop1;
|
|
995
|
+
break;
|
|
996
|
+
}
|
|
997
|
+
// 后续 lineYOffsets 保持按行高累计
|
|
998
|
+
var lineYOffsets = [];
|
|
999
|
+
var currentY = baselineDisplayY;
|
|
1000
|
+
for(var i2 = 0; i2 < lines.length; i2++){
|
|
1001
|
+
lineYOffsets.push(currentY);
|
|
1002
|
+
if (i2 < lines.length - 1) {
|
|
1003
|
+
currentY += lines[i2 + 1].lineHeight;
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
return {
|
|
1007
|
+
baselineY: baselineDisplayY,
|
|
1008
|
+
lineYOffsets: lineYOffsets
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
// 下面行偏移保持不变
|
|
1013
|
+
var lineYOffsets1 = [];
|
|
1014
|
+
var currentY1 = baselineY;
|
|
1015
|
+
lines.forEach(function(line, index) {
|
|
1016
|
+
lineYOffsets1.push(currentY1);
|
|
1017
|
+
if (index < lines.length - 1) {
|
|
1018
|
+
currentY1 += lines[index + 1].lineHeight;
|
|
1019
|
+
}
|
|
1020
|
+
});
|
|
1021
|
+
return {
|
|
1022
|
+
baselineY: baselineY,
|
|
1023
|
+
lineYOffsets: lineYOffsets1
|
|
1024
|
+
};
|
|
1025
|
+
};
|
|
1026
|
+
return RichVerticalAlignStrategyImpl;
|
|
1027
|
+
}();
|
|
1028
|
+
|
|
1029
|
+
/**
|
|
1030
|
+
* 富文本策略工厂
|
|
1031
|
+
* 负责创建各种策略实例
|
|
1032
|
+
*/ var RichTextStrategyFactory = /*#__PURE__*/ function() {
|
|
1033
|
+
function RichTextStrategyFactory() {}
|
|
1034
|
+
/**
|
|
1035
|
+
* 创建换行策略
|
|
1036
|
+
* 根据wrapEnabled参数决定使用哪种策略
|
|
1037
|
+
*/ RichTextStrategyFactory.createWrapStrategy = function createWrapStrategy(wrapEnabled) {
|
|
1038
|
+
if (wrapEnabled) {
|
|
1039
|
+
return new RichWrapEnabledStrategy();
|
|
1040
|
+
} else {
|
|
1041
|
+
return new RichWrapDisabledStrategy();
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
/**
|
|
1045
|
+
* 创建溢出策略
|
|
1046
|
+
* 支持三种模式:clip(裁切)、display(行级缩放)、visible(不缩放不裁剪)
|
|
1047
|
+
*/ RichTextStrategyFactory.createOverflowStrategy = function createOverflowStrategy(mode) {
|
|
1048
|
+
switch(mode){
|
|
1049
|
+
case spec.TextOverflow.clip:
|
|
1050
|
+
return new RichClippedOverflowStrategy();
|
|
1051
|
+
case spec.TextOverflow.display:
|
|
1052
|
+
return new RichDisplayOverflowStrategy();
|
|
1053
|
+
case spec.TextOverflow.visible:
|
|
1054
|
+
default:
|
|
1055
|
+
return new RichVisibleOverflowStrategy();
|
|
1056
|
+
}
|
|
1057
|
+
};
|
|
1058
|
+
/**
|
|
1059
|
+
* 创建水平对齐策略
|
|
1060
|
+
*/ RichTextStrategyFactory.createHorizontalAlignStrategy = function createHorizontalAlignStrategy() {
|
|
1061
|
+
return new RichHorizontalAlignStrategyImpl();
|
|
1062
|
+
};
|
|
1063
|
+
/**
|
|
1064
|
+
* 创建垂直对齐策略
|
|
1065
|
+
*/ RichTextStrategyFactory.createVerticalAlignStrategy = function createVerticalAlignStrategy() {
|
|
1066
|
+
return new RichVerticalAlignStrategyImpl();
|
|
1067
|
+
};
|
|
1068
|
+
return RichTextStrategyFactory;
|
|
1069
|
+
}();
|
|
1070
|
+
|
|
405
1071
|
var seed = 0;
|
|
406
|
-
var RichTextComponent = /*#__PURE__*/ function(
|
|
407
|
-
_inherits(RichTextComponent,
|
|
1072
|
+
var RichTextComponent = /*#__PURE__*/ function(MaskableGraphic) {
|
|
1073
|
+
_inherits(RichTextComponent, MaskableGraphic);
|
|
408
1074
|
function RichTextComponent(engine) {
|
|
409
1075
|
var _this;
|
|
410
|
-
_this =
|
|
1076
|
+
_this = MaskableGraphic.call(this, engine) || this;
|
|
1077
|
+
_this.isDirty = true;
|
|
1078
|
+
_this.text = "";
|
|
411
1079
|
_this.processedTextOptions = [];
|
|
412
|
-
// 字体高度的倍数(字体高度上下多出来的部分就是行间距)
|
|
413
1080
|
_this.singleLineHeight = 1.571;
|
|
414
1081
|
_this.size = null;
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
* canvas 大小
|
|
420
|
-
*/ _this.canvasSize = null;
|
|
1082
|
+
_this.initialized = false;
|
|
1083
|
+
_this.canvasSize = null;
|
|
1084
|
+
_this.SCALE_FACTOR = 0.1;
|
|
1085
|
+
_this.ALPHA_FIX_VALUE = 1 / 255;
|
|
421
1086
|
_this.name = "MRichText" + seed++;
|
|
1087
|
+
_this.initTextBase(engine);
|
|
1088
|
+
// 延迟初始化策略,等到textLayout被赋值后再初始化
|
|
1089
|
+
_this.richWrapStrategy = RichTextStrategyFactory.createWrapStrategy();
|
|
1090
|
+
_this.richOverflowStrategy = RichTextStrategyFactory.createOverflowStrategy(spec.TextOverflow.display);
|
|
1091
|
+
_this.richHorizontalAlignStrategy = RichTextStrategyFactory.createHorizontalAlignStrategy();
|
|
1092
|
+
_this.richVerticalAlignStrategy = RichTextStrategyFactory.createVerticalAlignStrategy();
|
|
422
1093
|
return _this;
|
|
423
1094
|
}
|
|
424
1095
|
var _proto = RichTextComponent.prototype;
|
|
1096
|
+
_proto.onUpdate = function onUpdate(dt) {
|
|
1097
|
+
MaskableGraphic.prototype.onUpdate.call(this, dt);
|
|
1098
|
+
this.updateTexture();
|
|
1099
|
+
};
|
|
1100
|
+
_proto.onDestroy = function onDestroy() {
|
|
1101
|
+
MaskableGraphic.prototype.onDestroy.call(this);
|
|
1102
|
+
this.disposeTextTexture();
|
|
1103
|
+
};
|
|
1104
|
+
_proto.fromData = function fromData(data) {
|
|
1105
|
+
MaskableGraphic.prototype.fromData.call(this, data);
|
|
1106
|
+
var interaction = data.interaction, options = data.options;
|
|
1107
|
+
this.interaction = interaction;
|
|
1108
|
+
this.textStyle = new TextStyle(options);
|
|
1109
|
+
this.textLayout = new RichTextLayout(options);
|
|
1110
|
+
this.text = options.text ? options.text.toString() : " ";
|
|
1111
|
+
if (this.textLayout.useLegacyRichText) {
|
|
1112
|
+
this.textLayout.textVerticalAlign = spec.TextVerticalAlign.middle;
|
|
1113
|
+
}
|
|
1114
|
+
this.updateStrategies();
|
|
1115
|
+
this.renderText(options);
|
|
1116
|
+
// 设置默认颜色(math.Color)
|
|
1117
|
+
this.material.setColor("_Color", new math.Color(1, 1, 1, 1));
|
|
1118
|
+
};
|
|
1119
|
+
/**
|
|
1120
|
+
* 使用策略结果绘制文本
|
|
1121
|
+
*/ _proto.updateStrategies = function updateStrategies() {
|
|
1122
|
+
var layout = this.textLayout;
|
|
1123
|
+
if (layout) {
|
|
1124
|
+
this.richWrapStrategy = RichTextStrategyFactory.createWrapStrategy(layout.wrapEnabled);
|
|
1125
|
+
this.richOverflowStrategy = RichTextStrategyFactory.createOverflowStrategy(layout.overflow);
|
|
1126
|
+
}
|
|
1127
|
+
};
|
|
425
1128
|
_proto.generateTextProgram = function generateTextProgram(text) {
|
|
426
1129
|
var _this = this;
|
|
427
1130
|
this.processedTextOptions = [];
|
|
428
1131
|
var program = generateProgram(function(text, context) {
|
|
429
|
-
// 如果富文本仅包含换行符,则在每个换行符后添加一个空格
|
|
430
1132
|
if (/^\n+$/.test(text)) {
|
|
431
1133
|
text = text.replace(/\n/g, "\n ");
|
|
432
1134
|
}
|
|
@@ -457,48 +1159,64 @@ var RichTextComponent = /*#__PURE__*/ function(TextComponent) {
|
|
|
457
1159
|
});
|
|
458
1160
|
program(text);
|
|
459
1161
|
};
|
|
1162
|
+
_proto.updateWithOptions = function updateWithOptions(options) {
|
|
1163
|
+
this.textStyle = new TextStyle(options);
|
|
1164
|
+
this.textLayout = new RichTextLayout(options);
|
|
1165
|
+
this.text = options.text ? options.text.toString() : " ";
|
|
1166
|
+
// TextLayout 构造函数已经正确处理了 textVerticalAlign,这里不需要再设置
|
|
1167
|
+
if (this.textLayout.useLegacyRichText) {
|
|
1168
|
+
this.textLayout.textVerticalAlign = spec.TextVerticalAlign.middle;
|
|
1169
|
+
}
|
|
1170
|
+
this.updateStrategies();
|
|
1171
|
+
this.isDirty = true;
|
|
1172
|
+
};
|
|
1173
|
+
_proto.renderText = function renderText(options) {
|
|
1174
|
+
var size = options.size;
|
|
1175
|
+
if (size) {
|
|
1176
|
+
this.canvasSize = new math.Vector2(size[0], size[1]);
|
|
1177
|
+
}
|
|
1178
|
+
this.updateTexture();
|
|
1179
|
+
};
|
|
460
1180
|
_proto.updateTexture = function updateTexture(flipY) {
|
|
461
1181
|
if (flipY === void 0) flipY = true;
|
|
462
|
-
if (!this.isDirty || !this.context || !this.canvas) {
|
|
1182
|
+
if (!this.isDirty || !this.context || !this.canvas || !this.textStyle || !this.textLayout) {
|
|
463
1183
|
return;
|
|
464
1184
|
}
|
|
465
|
-
|
|
466
|
-
var useLegacy =
|
|
1185
|
+
var layout = this.textLayout;
|
|
1186
|
+
var useLegacy = layout.useLegacyRichText === true;
|
|
467
1187
|
this.singleLineHeight = useLegacy ? 1.571 : 1.0;
|
|
1188
|
+
// 根据useLegacyRichText字段来判断使用哪种渲染模式
|
|
468
1189
|
if (useLegacy) {
|
|
469
1190
|
this.updateTextureLegacy(flipY);
|
|
470
1191
|
} else {
|
|
471
|
-
this.
|
|
1192
|
+
this.updateTextureWithStrategies(flipY);
|
|
472
1193
|
}
|
|
473
1194
|
};
|
|
474
|
-
|
|
1195
|
+
/**
|
|
1196
|
+
* 解析富文本
|
|
1197
|
+
*/ _proto.updateTextureLegacy = function updateTextureLegacy(flipY) {
|
|
475
1198
|
var _this = this;
|
|
476
|
-
if (!this.isDirty || !this.context || !this.canvas) {
|
|
1199
|
+
if (!this.isDirty || !this.context || !this.canvas || !this.textStyle) {
|
|
477
1200
|
return;
|
|
478
1201
|
}
|
|
479
|
-
// 解析富文本
|
|
480
1202
|
this.generateTextProgram(this.text);
|
|
481
|
-
var
|
|
482
|
-
var
|
|
1203
|
+
var width = 0, height = 0;
|
|
1204
|
+
var layout = this.textLayout;
|
|
1205
|
+
var textStyle = this.textStyle;
|
|
1206
|
+
var overflow = layout.overflow, _layout_letterSpace = layout.letterSpace, letterSpace = _layout_letterSpace === void 0 ? 0 : _layout_letterSpace;
|
|
483
1207
|
var context = this.context;
|
|
484
|
-
var width = 0;
|
|
485
|
-
var height = 0;
|
|
486
|
-
context.save();
|
|
487
1208
|
var charsInfo = [];
|
|
488
|
-
var fontHeight = textStyle.fontSize *
|
|
1209
|
+
var fontHeight = textStyle.fontSize * textStyle.fontScale;
|
|
489
1210
|
var charInfo = {
|
|
490
1211
|
richOptions: [],
|
|
491
1212
|
offsetX: [],
|
|
492
1213
|
width: 0,
|
|
493
|
-
// 包括字体和上下行间距的高度
|
|
494
1214
|
lineHeight: fontHeight * this.singleLineHeight,
|
|
495
|
-
// 字体偏移高度(也就是行间距)
|
|
496
1215
|
offsetY: fontHeight * (this.singleLineHeight - 1) / 2
|
|
497
1216
|
};
|
|
498
1217
|
// 遍历解析后的文本选项
|
|
499
1218
|
this.processedTextOptions.forEach(function(options) {
|
|
500
1219
|
var text = options.text, isNewLine = options.isNewLine, fontSize = options.fontSize;
|
|
501
|
-
// 如果是新行,则将之前行的信息存入 charsInfo 并且初始化新行的 charInfo
|
|
502
1220
|
if (isNewLine) {
|
|
503
1221
|
charsInfo.push(charInfo);
|
|
504
1222
|
width = Math.max(width, charInfo.width);
|
|
@@ -509,12 +1227,9 @@ var RichTextComponent = /*#__PURE__*/ function(TextComponent) {
|
|
|
509
1227
|
lineHeight: fontHeight * _this.singleLineHeight,
|
|
510
1228
|
offsetY: fontHeight * (_this.singleLineHeight - 1) / 2
|
|
511
1229
|
};
|
|
512
|
-
// 管理行的总高度调整 canvas 尺寸
|
|
513
1230
|
height += charInfo.lineHeight;
|
|
514
1231
|
}
|
|
515
|
-
// 恢复默认设置
|
|
516
1232
|
context.font = (options.fontWeight || textStyle.textWeight) + " 10px " + (options.fontFamily || textStyle.fontFamily);
|
|
517
|
-
// 计算得到每个字段的宽度 textWidth
|
|
518
1233
|
var textMetrics = context.measureText(text);
|
|
519
1234
|
var textWidth = textMetrics.width;
|
|
520
1235
|
if (textMetrics.actualBoundingBoxLeft !== undefined && textMetrics.actualBoundingBoxRight !== undefined) {
|
|
@@ -523,32 +1238,28 @@ var RichTextComponent = /*#__PURE__*/ function(TextComponent) {
|
|
|
523
1238
|
textWidth = Math.max(textWidth, actualWidth);
|
|
524
1239
|
}
|
|
525
1240
|
}
|
|
526
|
-
var textHeight = fontSize * _this.singleLineHeight *
|
|
1241
|
+
var textHeight = fontSize * _this.singleLineHeight * textStyle.fontScale;
|
|
527
1242
|
if (textHeight > charInfo.lineHeight) {
|
|
528
1243
|
height += textHeight - charInfo.lineHeight;
|
|
529
1244
|
charInfo.lineHeight = textHeight;
|
|
530
|
-
charInfo.offsetY = fontSize *
|
|
1245
|
+
charInfo.offsetY = fontSize * textStyle.fontScale * (_this.singleLineHeight - 1) / 2;
|
|
531
1246
|
}
|
|
532
1247
|
charInfo.offsetX.push(charInfo.width);
|
|
533
|
-
|
|
534
|
-
charInfo.width += (textWidth <= 0 ? 0 : textWidth) * fontSize * _this.SCALE_FACTOR * _this.textStyle.fontScale + text.length * letterSpace;
|
|
535
|
-
// 将富文本数据存入 charInfo
|
|
1248
|
+
charInfo.width += (textWidth <= 0 ? 0 : textWidth) * fontSize * _this.SCALE_FACTOR * textStyle.fontScale + text.length * letterSpace;
|
|
536
1249
|
charInfo.richOptions.push(options);
|
|
537
1250
|
});
|
|
538
|
-
// 存储最后一行的字符信息,并且更新最终的宽度和高度用于确定 canvas 尺寸
|
|
539
1251
|
charsInfo.push(charInfo);
|
|
540
1252
|
width = Math.max(width, charInfo.width);
|
|
541
1253
|
height += charInfo.lineHeight;
|
|
1254
|
+
// 存储最后一行的字符信息,并且更新最终的宽度和高度用于确定canvas尺寸
|
|
542
1255
|
if (width === 0 || height === 0) {
|
|
543
1256
|
this.isDirty = false;
|
|
544
|
-
context.restore();
|
|
545
1257
|
return;
|
|
546
1258
|
}
|
|
547
1259
|
if (this.size === undefined || this.size === null) {
|
|
548
1260
|
this.size = this.item.transform.size.clone();
|
|
549
1261
|
}
|
|
550
1262
|
var _this_size = this.size, _this_size_x = _this_size.x, x = _this_size_x === void 0 ? 1 : _this_size_x, _this_size_y = _this_size.y, y = _this_size_y === void 0 ? 1 : _this_size_y;
|
|
551
|
-
// 首次渲染时初始化 canvas 尺寸和组件变换
|
|
552
1263
|
if (!this.initialized) {
|
|
553
1264
|
this.canvasSize = !this.canvasSize ? new math.Vector2(width, height) : this.canvasSize;
|
|
554
1265
|
var _this_canvasSize = this.canvasSize, canvasWidth = _this_canvasSize.x, canvasHeight = _this_canvasSize.y;
|
|
@@ -558,318 +1269,327 @@ var RichTextComponent = /*#__PURE__*/ function(TextComponent) {
|
|
|
558
1269
|
}
|
|
559
1270
|
assertExist(this.canvasSize);
|
|
560
1271
|
var _this_canvasSize1 = this.canvasSize, canvasWidth1 = _this_canvasSize1.x, canvasHeight1 = _this_canvasSize1.y;
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
this.
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
// fix bug 1/255
|
|
567
|
-
context.fillStyle = "rgba(255, 255, 255, " + this.ALPHA_FIX_VALUE + ")";
|
|
568
|
-
if (!flipY) {
|
|
569
|
-
context.translate(0, canvasHeight1);
|
|
570
|
-
context.scale(1, -1);
|
|
571
|
-
}
|
|
572
|
-
if (charsInfo.length === 0) {
|
|
573
|
-
context.restore();
|
|
574
|
-
return;
|
|
575
|
-
}
|
|
576
|
-
var charsLineHeight = textLayout.getOffsetY(textStyle, charsInfo.length, fontHeight * this.singleLineHeight, textStyle.fontSize);
|
|
577
|
-
charsInfo.forEach(function(charInfo, index) {
|
|
578
|
-
var richOptions = charInfo.richOptions, offsetX = charInfo.offsetX, width = charInfo.width;
|
|
579
|
-
var charWidth = width;
|
|
580
|
-
var offset = offsetX;
|
|
581
|
-
if (overflow === spec.TextOverflow.display) {
|
|
582
|
-
if (width > canvasWidth1) {
|
|
583
|
-
var canvasScale = canvasWidth1 / width;
|
|
584
|
-
charWidth *= canvasScale;
|
|
585
|
-
offset = offsetX.map(function(x) {
|
|
586
|
-
return x * canvasScale;
|
|
587
|
-
});
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
var x = _this.textLayout.getOffsetX(textStyle, charWidth);
|
|
591
|
-
if (index > 0) {
|
|
592
|
-
charsLineHeight += charInfo.lineHeight - charInfo.offsetY;
|
|
1272
|
+
layout.width = canvasWidth1 / textStyle.fontScale;
|
|
1273
|
+
layout.height = canvasHeight1 / textStyle.fontScale;
|
|
1274
|
+
this.renderToTexture(canvasWidth1, canvasHeight1, flipY, function(context) {
|
|
1275
|
+
if (charsInfo.length === 0) {
|
|
1276
|
+
return;
|
|
593
1277
|
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
var
|
|
597
|
-
var
|
|
1278
|
+
var charsLineHeight = layout.getOffsetY(textStyle, charsInfo.length, fontHeight * _this.singleLineHeight, textStyle.fontSize);
|
|
1279
|
+
charsInfo.forEach(function(charInfo, index) {
|
|
1280
|
+
var richOptions = charInfo.richOptions, offsetX = charInfo.offsetX, width = charInfo.width;
|
|
1281
|
+
var charWidth = width;
|
|
1282
|
+
var offset = offsetX;
|
|
598
1283
|
if (overflow === spec.TextOverflow.display) {
|
|
599
1284
|
if (width > canvasWidth1) {
|
|
600
|
-
|
|
1285
|
+
var canvasScale = canvasWidth1 / width;
|
|
1286
|
+
charWidth *= canvasScale;
|
|
1287
|
+
offset = offsetX.map(function(x) {
|
|
1288
|
+
return x * canvasScale;
|
|
1289
|
+
});
|
|
601
1290
|
}
|
|
602
1291
|
}
|
|
603
|
-
var
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
1292
|
+
var x = layout.getOffsetX(textStyle, charWidth);
|
|
1293
|
+
if (index > 0) {
|
|
1294
|
+
charsLineHeight += charInfo.lineHeight - charInfo.offsetY;
|
|
1295
|
+
}
|
|
1296
|
+
richOptions.forEach(function(options, index) {
|
|
1297
|
+
var fontScale = textStyle.fontScale, textColor = textStyle.textColor, textFamily = textStyle.fontFamily, textWeight = textStyle.textWeight, richStyle = textStyle.fontStyle;
|
|
1298
|
+
var text = options.text, fontSize = options.fontSize, _options_fontColor = options.fontColor, fontColor = _options_fontColor === void 0 ? textColor : _options_fontColor, _options_fontFamily = options.fontFamily, fontFamily = _options_fontFamily === void 0 ? textFamily : _options_fontFamily, _options_fontWeight = options.fontWeight, fontWeight = _options_fontWeight === void 0 ? textWeight : _options_fontWeight, _options_fontStyle = options.fontStyle, fontStyle = _options_fontStyle === void 0 ? richStyle : _options_fontStyle;
|
|
1299
|
+
var textSize = fontSize;
|
|
1300
|
+
if (overflow === spec.TextOverflow.display) {
|
|
1301
|
+
if (width > canvasWidth1) {
|
|
1302
|
+
textSize /= width / canvasWidth1;
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
var strOffsetX = offset[index] + x;
|
|
1306
|
+
// fix bug 1/255
|
|
1307
|
+
context.font = fontStyle + " " + fontWeight + " " + textSize * fontScale + "px " + fontFamily;
|
|
1308
|
+
var r = fontColor[0], g = fontColor[1], b = fontColor[2], a = fontColor[3];
|
|
1309
|
+
context.fillStyle = "rgba(" + r + ", " + g + ", " + b + ", " + a + ")";
|
|
1310
|
+
context.fillText(text, strOffsetX, charsLineHeight);
|
|
1311
|
+
});
|
|
607
1312
|
});
|
|
608
|
-
});
|
|
609
|
-
// 与 toDataURL() 两种方式都需要像素读取操作
|
|
610
|
-
var imageData = context.getImageData(0, 0, this.canvas.width, this.canvas.height);
|
|
611
|
-
var texture = Texture.createWithData(this.engine, {
|
|
612
|
-
data: new Uint8Array(imageData.data),
|
|
613
|
-
width: imageData.width,
|
|
614
|
-
height: imageData.height
|
|
615
1313
|
}, {
|
|
616
|
-
|
|
617
|
-
magFilter: glContext.LINEAR,
|
|
618
|
-
minFilter: glContext.LINEAR,
|
|
619
|
-
wrapS: glContext.CLAMP_TO_EDGE,
|
|
620
|
-
wrapT: glContext.CLAMP_TO_EDGE
|
|
1314
|
+
disposeOld: false
|
|
621
1315
|
});
|
|
622
|
-
|
|
623
|
-
this.renderer.texture = texture;
|
|
624
|
-
this.material.setTexture("_MainTex", texture);
|
|
1316
|
+
// 与 toDataURL() 两种方式都需要像素读取操作
|
|
625
1317
|
this.isDirty = false;
|
|
626
|
-
context.restore();
|
|
627
1318
|
};
|
|
628
|
-
|
|
1319
|
+
/**
|
|
1320
|
+
* 使用策略管线路径的渲染方法
|
|
1321
|
+
*/ _proto.updateTextureWithStrategies = function updateTextureWithStrategies(flipY) {
|
|
629
1322
|
var _this = this;
|
|
630
|
-
|
|
631
|
-
|
|
1323
|
+
if (!this.isDirty || !this.context || !this.canvas) {
|
|
1324
|
+
return;
|
|
1325
|
+
}
|
|
632
1326
|
// 解析富文本
|
|
633
1327
|
this.generateTextProgram(this.text);
|
|
634
|
-
var
|
|
635
|
-
var
|
|
1328
|
+
var layout = this.textLayout;
|
|
1329
|
+
var _layout_letterSpace = layout.letterSpace, letterSpace = _layout_letterSpace === void 0 ? 0 : _layout_letterSpace;
|
|
636
1330
|
var context = this.context;
|
|
637
|
-
var width = 0;
|
|
638
|
-
var height = 0;
|
|
639
1331
|
if (!context) {
|
|
640
1332
|
return;
|
|
641
1333
|
}
|
|
642
|
-
|
|
643
|
-
var
|
|
644
|
-
|
|
645
|
-
var charInfo = {
|
|
646
|
-
richOptions: [],
|
|
647
|
-
offsetX: [],
|
|
648
|
-
width: 0,
|
|
649
|
-
// 包括字体和上下行间距的高度
|
|
650
|
-
lineHeight: fontHeight * this.singleLineHeight + (this.textLayout.lineGap || 0) * this.textStyle.fontScale,
|
|
651
|
-
// 字体偏移高度(也就是行间距)
|
|
652
|
-
offsetY: (this.textLayout.lineGap || 0) * this.textStyle.fontScale / 2,
|
|
653
|
-
chars: []
|
|
654
|
-
};
|
|
655
|
-
// 遍历解析后的文本选项
|
|
656
|
-
this.processedTextOptions.forEach(function(options) {
|
|
657
|
-
var text = options.text, isNewLine = options.isNewLine, fontSize = options.fontSize;
|
|
658
|
-
// 如果是新行,则结束上一行并开始新行
|
|
659
|
-
if (isNewLine) {
|
|
660
|
-
// 结束上一行:累加行高并重置charInfo
|
|
661
|
-
height += charInfo.lineHeight;
|
|
662
|
-
charsInfo.push(charInfo);
|
|
663
|
-
width = Math.max(width, charInfo.width);
|
|
664
|
-
// 初始化新行
|
|
665
|
-
charInfo = {
|
|
666
|
-
richOptions: [],
|
|
667
|
-
offsetX: [],
|
|
668
|
-
width: 0,
|
|
669
|
-
lineHeight: fontHeight * _this.singleLineHeight + (_this.textLayout.lineGap || 0) * _this.textStyle.fontScale,
|
|
670
|
-
offsetY: (_this.textLayout.lineGap || 0) * _this.textStyle.fontScale / 2,
|
|
671
|
-
chars: []
|
|
672
|
-
};
|
|
673
|
-
}
|
|
674
|
-
// 恢复默认设置
|
|
675
|
-
context.font = (options.fontWeight || textStyle.textWeight) + " 10px " + (options.fontFamily || textStyle.fontFamily);
|
|
676
|
-
var textHeight = fontSize * _this.singleLineHeight * _this.textStyle.fontScale + (_this.textLayout.lineGap || 0) * _this.textStyle.fontScale;
|
|
677
|
-
// 更新当前行的最大行高
|
|
678
|
-
if (textHeight > charInfo.lineHeight) {
|
|
679
|
-
charInfo.lineHeight = textHeight;
|
|
680
|
-
charInfo.offsetY = (_this.textLayout.lineGap || 0) * _this.textStyle.fontScale / 2;
|
|
681
|
-
}
|
|
682
|
-
charInfo.offsetX.push(charInfo.width);
|
|
683
|
-
// 逐字计算宽度以实现字符间距
|
|
684
|
-
var segmentInnerX = 0;
|
|
685
|
-
var charArr = [];
|
|
686
|
-
for(var i = 0; i < text.length; i++){
|
|
687
|
-
var char = text[i];
|
|
688
|
-
var tempcharWidth = context.measureText(char).width;
|
|
689
|
-
var charWidth = (tempcharWidth <= 0 ? 0 : tempcharWidth) * fontSize * _this.SCALE_FACTOR * _this.textStyle.fontScale;
|
|
690
|
-
charArr.push({
|
|
691
|
-
char: char,
|
|
692
|
-
x: segmentInnerX,
|
|
693
|
-
width: charWidth
|
|
694
|
-
});
|
|
695
|
-
segmentInnerX += charWidth + letterSpace;
|
|
696
|
-
}
|
|
697
|
-
charInfo.chars.push(charArr);
|
|
698
|
-
charInfo.width += segmentInnerX;
|
|
699
|
-
charInfo.richOptions.push(options);
|
|
700
|
-
});
|
|
701
|
-
// 结束最后一行
|
|
702
|
-
height += charInfo.lineHeight;
|
|
703
|
-
charsInfo.push(charInfo);
|
|
704
|
-
width = Math.max(width, charInfo.width);
|
|
705
|
-
if (width === 0 || height === 0) {
|
|
1334
|
+
// 步骤1: 换行策略计算行信息
|
|
1335
|
+
var wrapResult = this.richWrapStrategy.computeLines(this.processedTextOptions, context, this.textStyle, layout, this.singleLineHeight, this.textStyle.fontScale, letterSpace, this.SCALE_FACTOR);
|
|
1336
|
+
if (wrapResult.lines.length === 0 || wrapResult.maxLineWidth === 0 || wrapResult.totalHeight === 0) {
|
|
706
1337
|
this.isDirty = false;
|
|
707
|
-
context.restore();
|
|
708
1338
|
return;
|
|
709
1339
|
}
|
|
1340
|
+
// 步骤2: 尺寸处理
|
|
1341
|
+
var sizeResult = this.resolveCanvasSize(wrapResult, layout, this.textStyle, this.singleLineHeight);
|
|
1342
|
+
// 首次渲染时初始化 canvas 尺寸和组件变换
|
|
1343
|
+
this.setCanvasSize(sizeResult);
|
|
1344
|
+
// 步骤3: 溢出策略处理
|
|
1345
|
+
var overflowResult = this.richOverflowStrategy.apply(wrapResult.lines, sizeResult, layout, this.textStyle);
|
|
1346
|
+
// 步骤4: 水平对齐策略
|
|
1347
|
+
var horizontalAlignResult = this.richHorizontalAlignStrategy.getHorizontalOffsets(wrapResult.lines, sizeResult, overflowResult, layout, this.textStyle);
|
|
1348
|
+
// 步骤5: 垂直对齐策略
|
|
1349
|
+
var TextVerticalAlignResult = this.richVerticalAlignStrategy.getVerticalOffsets(wrapResult.lines, sizeResult, overflowResult, layout, this.textStyle, this.singleLineHeight);
|
|
1350
|
+
// 使用 this.canvasSize 统一设置画布尺寸
|
|
1351
|
+
assertExist(this.canvasSize);
|
|
1352
|
+
var _this_canvasSize = this.canvasSize, canvasWidth = _this_canvasSize.x, canvasHeight = _this_canvasSize.y;
|
|
1353
|
+
layout.width = canvasWidth / this.textStyle.fontScale;
|
|
1354
|
+
layout.height = canvasHeight / this.textStyle.fontScale;
|
|
1355
|
+
this.renderToTexture(canvasWidth, canvasHeight, flipY, function(context) {
|
|
1356
|
+
// 步骤6: 绘制文本
|
|
1357
|
+
_this.drawTextWithStrategies(context, wrapResult.lines, horizontalAlignResult, TextVerticalAlignResult, overflowResult, _this.textStyle);
|
|
1358
|
+
}, {
|
|
1359
|
+
disposeOld: false
|
|
1360
|
+
});
|
|
1361
|
+
this.isDirty = false;
|
|
1362
|
+
};
|
|
1363
|
+
_proto.setCanvasSize = function setCanvasSize(sizeResult) {
|
|
1364
|
+
var _this = this;
|
|
710
1365
|
if (this.size === undefined || this.size === null) {
|
|
711
1366
|
this.size = this.item.transform.size.clone();
|
|
712
1367
|
}
|
|
713
1368
|
var _this_size = this.size, _this_size_x = _this_size.x, x = _this_size_x === void 0 ? 1 : _this_size_x, _this_size_y = _this_size.y, y = _this_size_y === void 0 ? 1 : _this_size_y;
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
1369
|
+
var layout = this.textLayout;
|
|
1370
|
+
switch(layout.overflow){
|
|
1371
|
+
case spec.TextOverflow.visible:
|
|
1372
|
+
{
|
|
1373
|
+
var _Math;
|
|
1374
|
+
var frameW = layout.maxTextWidth;
|
|
1375
|
+
var frameH = layout.maxTextHeight;
|
|
1376
|
+
var _sizeResult_bboxTop;
|
|
1377
|
+
var bboxTop = (_sizeResult_bboxTop = sizeResult.bboxTop) != null ? _sizeResult_bboxTop : 0;
|
|
1378
|
+
var _sizeResult_bboxHeight, _sizeResult_bboxBottom;
|
|
1379
|
+
var bboxBottom = (_sizeResult_bboxBottom = sizeResult.bboxBottom) != null ? _sizeResult_bboxBottom : bboxTop + ((_sizeResult_bboxHeight = sizeResult.bboxHeight) != null ? _sizeResult_bboxHeight : 0);
|
|
1380
|
+
var _sizeResult_bboxHeight1;
|
|
1381
|
+
var bboxHeight = (_sizeResult_bboxHeight1 = sizeResult.bboxHeight) != null ? _sizeResult_bboxHeight1 : bboxBottom - bboxTop;
|
|
1382
|
+
// 计算 frame 基线
|
|
1383
|
+
var baselineYFrame = 0;
|
|
1384
|
+
switch(layout.textVerticalAlign){
|
|
1385
|
+
case spec.TextVerticalAlign.top:
|
|
1386
|
+
baselineYFrame = -bboxTop;
|
|
1387
|
+
break;
|
|
1388
|
+
case spec.TextVerticalAlign.middle:
|
|
1389
|
+
baselineYFrame = (frameH - bboxHeight) / 2 - bboxTop;
|
|
1390
|
+
break;
|
|
1391
|
+
case spec.TextVerticalAlign.bottom:
|
|
1392
|
+
baselineYFrame = frameH - bboxHeight - bboxTop;
|
|
1393
|
+
break;
|
|
1394
|
+
}
|
|
1395
|
+
// 上下溢出检测
|
|
1396
|
+
var contentTopInFrame = baselineYFrame + bboxTop;
|
|
1397
|
+
var contentBottomInFrame = baselineYFrame + bboxBottom;
|
|
1398
|
+
var overflowTop = Math.max(0, -contentTopInFrame);
|
|
1399
|
+
var overflowBottom = Math.max(0, contentBottomInFrame - frameH);
|
|
1400
|
+
// 垂直扩张
|
|
1401
|
+
var expandTop = overflowTop;
|
|
1402
|
+
var expandBottom = overflowBottom;
|
|
1403
|
+
switch(layout.textVerticalAlign){
|
|
1404
|
+
case spec.TextVerticalAlign.top:
|
|
1405
|
+
{
|
|
1406
|
+
var E = overflowBottom;
|
|
1407
|
+
expandTop = E;
|
|
1408
|
+
expandBottom = E;
|
|
1409
|
+
break;
|
|
1410
|
+
}
|
|
1411
|
+
case spec.TextVerticalAlign.bottom:
|
|
1412
|
+
{
|
|
1413
|
+
var E1 = overflowTop;
|
|
1414
|
+
expandTop = E1;
|
|
1415
|
+
expandBottom = E1;
|
|
1416
|
+
break;
|
|
1417
|
+
}
|
|
1418
|
+
case spec.TextVerticalAlign.middle:
|
|
1419
|
+
{
|
|
1420
|
+
// 保持非对称:上扩 overflowTop,下扩 overflowBottom
|
|
1421
|
+
expandTop = overflowTop;
|
|
1422
|
+
expandBottom = overflowBottom;
|
|
1423
|
+
break;
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
// 位移补偿:始终使用 expandTop
|
|
1427
|
+
var compY = expandTop;
|
|
1428
|
+
// 水平扩张
|
|
1429
|
+
var lines = sizeResult.lines || [];
|
|
1430
|
+
var xOffsetsFrame = lines.map(function(line) {
|
|
1431
|
+
return layout.getOffsetXRich(_this.textStyle, frameW, line.width);
|
|
771
1432
|
});
|
|
1433
|
+
var leftMost = xOffsetsFrame.length > 0 ? (_Math = Math).min.apply(_Math, [].concat(xOffsetsFrame)) : 0;
|
|
1434
|
+
var ex = Math.max(0, -leftMost);
|
|
1435
|
+
var expandLeft = ex;
|
|
1436
|
+
var expandRight = ex;
|
|
1437
|
+
var finalW = frameW + expandLeft + expandRight;
|
|
1438
|
+
var finalH = frameH + expandTop + expandBottom;
|
|
1439
|
+
// 记录补偿,供垂直对齐策略叠加
|
|
1440
|
+
sizeResult.baselineCompensationX = expandLeft;
|
|
1441
|
+
sizeResult.baselineCompensationY = compY;
|
|
1442
|
+
sizeResult.canvasWidth = finalW;
|
|
1443
|
+
sizeResult.canvasHeight = finalH;
|
|
1444
|
+
this.canvasSize = new math.Vector2(finalW, finalH);
|
|
1445
|
+
var _this_size1;
|
|
1446
|
+
var _ref = (_this_size1 = this.size) != null ? _this_size1 : this.item.transform.size, _ref_x = _ref.x, x1 = _ref_x === void 0 ? 1 : _ref_x, _ref_y = _ref.y, y1 = _ref_y === void 0 ? 1 : _ref_y;
|
|
1447
|
+
this.item.transform.size.set(x1 * finalW * this.SCALE_FACTOR * this.SCALE_FACTOR, y1 * finalH * this.SCALE_FACTOR * this.SCALE_FACTOR);
|
|
1448
|
+
this.size = this.item.transform.size.clone();
|
|
1449
|
+
this.initialized = true;
|
|
1450
|
+
break;
|
|
772
1451
|
}
|
|
773
|
-
|
|
774
|
-
|
|
1452
|
+
case spec.TextOverflow.clip:
|
|
1453
|
+
{
|
|
1454
|
+
var frameW1 = layout.maxTextWidth;
|
|
1455
|
+
var frameH1 = layout.maxTextHeight;
|
|
1456
|
+
// 直接使用 frame 尺寸作为画布尺寸
|
|
1457
|
+
sizeResult.canvasWidth = frameW1;
|
|
1458
|
+
sizeResult.canvasHeight = frameH1;
|
|
1459
|
+
// clip 模式不需要任何补偿
|
|
1460
|
+
sizeResult.baselineCompensationX = 0;
|
|
1461
|
+
sizeResult.baselineCompensationY = 0;
|
|
1462
|
+
// 设置 canvas 和节点变换
|
|
1463
|
+
this.canvasSize = new math.Vector2(frameW1, frameH1);
|
|
1464
|
+
// 把 layout 的尺寸更新为 frame 尺寸
|
|
1465
|
+
layout.width = frameW1 / this.textStyle.fontScale;
|
|
1466
|
+
layout.height = frameH1 / this.textStyle.fontScale;
|
|
1467
|
+
var _this_size2;
|
|
1468
|
+
var _ref1 = (_this_size2 = this.size) != null ? _this_size2 : this.item.transform.size, _ref_x1 = _ref1.x, x2 = _ref_x1 === void 0 ? 1 : _ref_x1, _ref_y1 = _ref1.y, y2 = _ref_y1 === void 0 ? 1 : _ref_y1;
|
|
1469
|
+
this.item.transform.size.set(x2 * frameW1 * this.SCALE_FACTOR * this.SCALE_FACTOR, y2 * frameH1 * this.SCALE_FACTOR * this.SCALE_FACTOR);
|
|
1470
|
+
this.size = this.item.transform.size.clone();
|
|
1471
|
+
this.initialized = true;
|
|
1472
|
+
break;
|
|
1473
|
+
}
|
|
1474
|
+
case spec.TextOverflow.display:
|
|
1475
|
+
{
|
|
1476
|
+
if (!this.initialized) {
|
|
1477
|
+
this.canvasSize = new math.Vector2(layout.maxTextWidth, layout.maxTextHeight);
|
|
1478
|
+
this.item.transform.size.set(x * this.canvasSize.x * this.SCALE_FACTOR * this.SCALE_FACTOR, y * this.canvasSize.y * this.SCALE_FACTOR * this.SCALE_FACTOR);
|
|
1479
|
+
this.size = this.item.transform.size.clone();
|
|
1480
|
+
this.initialized = true;
|
|
1481
|
+
}
|
|
1482
|
+
break;
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
};
|
|
1486
|
+
/**
|
|
1487
|
+
* 尺寸处理
|
|
1488
|
+
*/ _proto.resolveCanvasSize = function resolveCanvasSize(wrapResult, layout, style, singleLineHeight) {
|
|
1489
|
+
var canvasWidth = Math.max(1, wrapResult.maxLineWidth || 0);
|
|
1490
|
+
var canvasHeight = Math.max(1, wrapResult.totalHeight || 0); // stepTotalHeight
|
|
1491
|
+
layout.width = canvasWidth / style.fontScale;
|
|
1492
|
+
layout.height = canvasHeight / style.fontScale;
|
|
1493
|
+
return {
|
|
1494
|
+
canvasWidth: canvasWidth,
|
|
1495
|
+
canvasHeight: canvasHeight,
|
|
1496
|
+
contentWidth: wrapResult.maxLineWidth,
|
|
1497
|
+
bboxTop: wrapResult.bboxTop,
|
|
1498
|
+
bboxBottom: wrapResult.bboxBottom,
|
|
1499
|
+
bboxHeight: wrapResult.bboxHeight,
|
|
1500
|
+
// 为 visible 模式的画布尺寸计算保留行信息
|
|
1501
|
+
lines: wrapResult.lines
|
|
1502
|
+
};
|
|
1503
|
+
};
|
|
1504
|
+
/**
|
|
1505
|
+
* 使用策略结果绘制文本
|
|
1506
|
+
*/ _proto.drawTextWithStrategies = function drawTextWithStrategies(context, lines, horizontalAlignResult, TextVerticalAlignResult, overflowResult, textStyle) {
|
|
1507
|
+
var currentBaselineY = TextVerticalAlignResult.baselineY;
|
|
1508
|
+
var lineOffsets = horizontalAlignResult.lineOffsets;
|
|
1509
|
+
lines.forEach(function(line, index) {
|
|
1510
|
+
var richOptions = line.richOptions, chars = line.chars;
|
|
1511
|
+
var xOffset = lineOffsets[index];
|
|
1512
|
+
var yOffset = currentBaselineY;
|
|
775
1513
|
richOptions.forEach(function(options, segIndex) {
|
|
776
1514
|
var fontScale = textStyle.fontScale, textColor = textStyle.textColor, textFamily = textStyle.fontFamily, textWeight = textStyle.textWeight, richStyle = textStyle.fontStyle;
|
|
777
1515
|
var fontSize = options.fontSize, _options_fontColor = options.fontColor, fontColor = _options_fontColor === void 0 ? textColor : _options_fontColor, _options_fontFamily = options.fontFamily, fontFamily = _options_fontFamily === void 0 ? textFamily : _options_fontFamily, _options_fontWeight = options.fontWeight, fontWeight = _options_fontWeight === void 0 ? textWeight : _options_fontWeight, _options_fontStyle = options.fontStyle, fontStyle = _options_fontStyle === void 0 ? richStyle : _options_fontStyle;
|
|
1516
|
+
// 直接使用原始字体大小(已在行数据中预缩放)
|
|
778
1517
|
var textSize = fontSize;
|
|
779
|
-
if (overflow === spec.TextOverflow.display && width > canvasWidth1) {
|
|
780
|
-
textSize /= width / canvasWidth1;
|
|
781
|
-
}
|
|
782
|
-
var strOffsetX = offset[segIndex] + x;
|
|
783
1518
|
context.font = fontStyle + " " + fontWeight + " " + textSize * fontScale + "px " + fontFamily;
|
|
784
|
-
|
|
1519
|
+
var r = fontColor[0], g = fontColor[1], b = fontColor[2], a = fontColor[3];
|
|
1520
|
+
context.fillStyle = "rgba(" + r + ", " + g + ", " + b + ", " + a + ")";
|
|
785
1521
|
// 逐字绘制
|
|
786
|
-
var
|
|
1522
|
+
var segStartX = line.offsetX && line.offsetX[segIndex] ? line.offsetX[segIndex] : 0;
|
|
1523
|
+
var charArr = chars[segIndex];
|
|
787
1524
|
charArr.forEach(function(charDetail) {
|
|
788
|
-
|
|
1525
|
+
// 直接使用缩放后的字符位置
|
|
1526
|
+
context.fillText(charDetail.char, xOffset + segStartX + charDetail.x, yOffset);
|
|
789
1527
|
});
|
|
790
1528
|
});
|
|
791
1529
|
// 推进到下一行
|
|
792
|
-
if (index <
|
|
793
|
-
|
|
1530
|
+
if (index < lines.length - 1) {
|
|
1531
|
+
currentBaselineY += lines[index + 1].lineHeight;
|
|
794
1532
|
}
|
|
795
1533
|
});
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
width: imageData.width,
|
|
800
|
-
height: imageData.height
|
|
801
|
-
}, {
|
|
802
|
-
flipY: flipY,
|
|
803
|
-
magFilter: glContext.LINEAR,
|
|
804
|
-
minFilter: glContext.LINEAR,
|
|
805
|
-
wrapS: glContext.CLAMP_TO_EDGE,
|
|
806
|
-
wrapT: glContext.CLAMP_TO_EDGE
|
|
807
|
-
});
|
|
808
|
-
this.renderer.texture = texture;
|
|
809
|
-
this.material.setTexture("_MainTex", texture);
|
|
810
|
-
this.isDirty = false;
|
|
811
|
-
context.restore();
|
|
1534
|
+
};
|
|
1535
|
+
_proto.unsupported = function unsupported(name) {
|
|
1536
|
+
throw new Error("RichTextComponent does not support " + name + " at runtime.");
|
|
812
1537
|
};
|
|
813
1538
|
/**
|
|
814
1539
|
* 该方法富文本组件不支持
|
|
815
1540
|
* @param value - 水平偏移距离
|
|
816
1541
|
* @returns
|
|
817
1542
|
*/ _proto.setShadowOffsetY = function setShadowOffsetY(value) {
|
|
818
|
-
|
|
1543
|
+
this.unsupported("setShadowOffsetY");
|
|
819
1544
|
};
|
|
820
1545
|
/**
|
|
821
1546
|
* 该方法富文本组件不支持
|
|
822
1547
|
* @param value - 模糊程度
|
|
823
1548
|
*/ _proto.setShadowBlur = function setShadowBlur(value) {
|
|
824
|
-
|
|
1549
|
+
this.unsupported("setShadowBlur");
|
|
825
1550
|
};
|
|
826
1551
|
/**
|
|
827
1552
|
* 该方法富文本组件不支持
|
|
828
1553
|
* @param value - 水平偏移距离
|
|
829
1554
|
*/ _proto.setShadowOffsetX = function setShadowOffsetX(value) {
|
|
830
|
-
|
|
1555
|
+
this.unsupported("setShadowOffsetX");
|
|
831
1556
|
};
|
|
832
1557
|
/**
|
|
833
1558
|
* 该方法富文本组件不支持
|
|
834
1559
|
* @param value - 阴影颜色
|
|
835
1560
|
*/ _proto.setShadowColor = function setShadowColor(value) {
|
|
836
|
-
|
|
1561
|
+
this.unsupported("setShadowColor");
|
|
837
1562
|
};
|
|
838
1563
|
/**
|
|
839
1564
|
* 该方法富文本组件不支持
|
|
840
1565
|
* @param value - 外描边宽度
|
|
841
1566
|
* @returns
|
|
842
1567
|
*/ _proto.setOutlineWidth = function setOutlineWidth(value) {
|
|
843
|
-
|
|
1568
|
+
this.unsupported("setOutlineWidth");
|
|
844
1569
|
};
|
|
845
1570
|
/**
|
|
846
1571
|
* 该方法富文本组件不支持
|
|
847
1572
|
* @param value - 是否自动设置宽度
|
|
848
1573
|
*/ _proto.setAutoWidth = function setAutoWidth(value) {
|
|
849
|
-
|
|
850
|
-
};
|
|
851
|
-
_proto.updateWithOptions = function updateWithOptions(options) {
|
|
852
|
-
this.textStyle = new TextStyle(options);
|
|
853
|
-
this.textLayout = new TextLayout(options);
|
|
854
|
-
this.textLayout.textBaseline = options.textBaseline || spec.TextBaseline.middle;
|
|
855
|
-
this.text = options.text ? options.text.toString() : " ";
|
|
1574
|
+
this.unsupported("setAutoWidth");
|
|
856
1575
|
};
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
}
|
|
862
|
-
this.updateTexture();
|
|
1576
|
+
/**
|
|
1577
|
+
* 该方法富文本组件不支持
|
|
1578
|
+
*/ _proto.setFontSize = function setFontSize(value) {
|
|
1579
|
+
this.unsupported("setFontSize");
|
|
863
1580
|
};
|
|
864
1581
|
return RichTextComponent;
|
|
865
|
-
}(
|
|
1582
|
+
}(MaskableGraphic);
|
|
866
1583
|
RichTextComponent = __decorate([
|
|
867
1584
|
effectsClass(spec.DataType.RichTextComponent)
|
|
868
1585
|
], RichTextComponent);
|
|
1586
|
+
applyMixins(RichTextComponent, [
|
|
1587
|
+
TextComponentBase
|
|
1588
|
+
]);
|
|
869
1589
|
|
|
870
1590
|
/**
|
|
871
1591
|
* 插件版本号
|
|
872
|
-
*/ var version = "2.
|
|
1592
|
+
*/ var version = "2.8.0-alpha.0";
|
|
873
1593
|
registerPlugin("rich-text", RichTextLoader, VFXItem);
|
|
874
1594
|
logger.info("Plugin rich text version: " + version + ".");
|
|
875
1595
|
if (version !== EFFECTS.version) {
|