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