engine icon indicating copy to clipboard operation
engine copied to clipboard

Support word split

Open singlecoder opened this issue 2 years ago • 1 comments

Please check if the PR fulfills these requirements

  • [ ] The commit message follows our guidelines
  • [ ] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)

singlecoder avatar Sep 21 '22 10:09 singlecoder

Codecov Report

Patch coverage has no change and project coverage change: -0.33 :warning:

Comparison is base (5dc198f) 49.24% compared to head (ebd2d88) 48.91%.

:exclamation: Current head ebd2d88 differs from pull request most recent head 3e25595. Consider uploading reports for the commit 3e25595 to get more accurate results

:mega: This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@             Coverage Diff             @@
##           dev/1.0    #1066      +/-   ##
===========================================
- Coverage    49.24%   48.91%   -0.33%     
===========================================
  Files          363      363              
  Lines        18394    18448      +54     
  Branches      2637     2657      +20     
===========================================
- Hits          9058     9024      -34     
- Misses        8404     8493      +89     
+ Partials       932      931       -1     
Impacted Files Coverage Δ
packages/core/src/2d/text/TextUtils.ts 1.85% <0.00%> (-0.62%) :arrow_down:

... and 7 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

codecov-commenter avatar Sep 21 '22 10:09 codecov-commenter

  • From the second line, a space cannot be the first character of a line
  • Cannot be displayed when wordWrapWidth is 0

cptbtptpbcptdtptp avatar Feb 10 '23 03:02 cptbtptpbcptdtptp

When enableWrapping is true, width is 0, display nothing is expected.

singlecoder avatar Feb 13 '23 02:02 singlecoder

image Arranged according to your logic:

    static measureTextWithWrap(renderer: TextRenderer): TextMetrics {
    const { fontSize, fontStyle, _subFont: subFont } = renderer;
    const { name } = renderer.font;
    const fontString = TextUtils.getNativeFontString(name, fontSize, fontStyle);
    const fontSizeInfo = TextUtils.measureFont(fontString);
    const subTexts = renderer.text.split(/(?:\r\n|\r|\n)/);
    const lines = new Array<string>();
    const lineWidths = new Array<number>();
    const lineMaxSizes = new Array<FontSizeInfo>();
    const { _pixelsPerUnit } = Engine;
    const lineHeight = fontSizeInfo.size + renderer.lineSpacing * _pixelsPerUnit;
    const wrapWidth = renderer.width * _pixelsPerUnit;
    let width = 0;
    for (let i = 0, n = subTexts.length; i < n; ++i) {
      const subText = subTexts[i];
      let lineChars = "";
      let lineWidth = 0;
      let lineAscent = 0;
      let lineDescent = 0;
      let wordsChars = "";
      let wordsWidth = 0;
      let wordsAscent = 0;
      let wordsDescent = 0;
      let isNotFirstLine = false;
      for (let j = 0, m = subText.length; j < m; j++) {
        const char = subText[j];
        const bSpace = char === " ";
        if (bSpace && lineWidth === 0 && wordsWidth === 0 && isNotFirstLine) {
          continue;
        }
        const charInfo = TextUtils._getCharInfo(char, fontString, subFont);
        const { w: charWidth, offsetY } = charInfo;
        const halfH = charInfo.h >> 1;
        const ascent = halfH + offsetY;
        const descent = halfH - offsetY;
        const canFormWords = !bSpace && char.charCodeAt(0) <= 255;
        if (canFormWords) {
          if (wordsWidth + charWidth > wrapWidth) {
            if (lineWidth > 0) {
              this._pushCharsToLines(lines, lineWidths, lineMaxSizes, lineChars, lineWidth, lineAscent, lineDescent);
              lineChars = "";
              lineWidth = lineAscent = lineDescent = 0;
            }
            this._pushCharsToLines(lines, lineWidths, lineMaxSizes, wordsChars, wordsWidth, wordsAscent, wordsDescent);
            isNotFirstLine = true;
            wordsChars = char;
            wordsWidth = charWidth;
            wordsAscent = ascent;
            wordsDescent = descent;
          } else {
            wordsChars += char;
            wordsWidth += charWidth;
            wordsAscent = Math.max(wordsAscent, ascent);
            wordsDescent = Math.max(wordsDescent, descent);
          }
        } else {
          if (wordsWidth > 0) {
            if (wordsWidth + lineWidth > wrapWidth) {
              this._pushCharsToLines(lines, lineWidths, lineMaxSizes, lineChars, lineWidth, lineAscent, lineDescent);
              isNotFirstLine = true;
              lineChars = wordsChars;
              lineWidth = wordsWidth;
              lineAscent = wordsAscent;
              lineDescent = wordsDescent;
            } else {
              lineChars += wordsChars;
              lineWidth += wordsWidth;
              lineAscent = Math.max(lineAscent, wordsAscent);
              lineDescent = Math.max(lineDescent, wordsDescent);
            }
            wordsChars = "";
            wordsWidth = wordsAscent = wordsDescent = 0;
          }
          if (lineWidth + charWidth > wrapWidth) {
            this._pushCharsToLines(lines, lineWidths, lineMaxSizes, lineChars, lineWidth, lineAscent, lineDescent);
            isNotFirstLine = true;
            lineChars = "";
            lineWidth = lineAscent = lineDescent = 0;
          }
          if (!bSpace || lineWidth !== 0 || !isNotFirstLine) {
            lineChars += char;
            lineWidth += charWidth;
            lineAscent = Math.max(ascent, lineAscent);
            lineDescent = Math.max(descent, lineDescent);
          }
        }
      }
      if (wordsWidth > 0) {
        if (wordsWidth + lineWidth > wrapWidth) {
          this._pushCharsToLines(lines, lineWidths, lineMaxSizes, lineChars, lineWidth, lineAscent, lineDescent);
          lineChars = wordsChars;
          lineWidth = wordsWidth;
          lineAscent = wordsAscent;
          lineDescent = wordsDescent;
        } else {
          lineChars += wordsChars;
          lineWidth += wordsWidth;
          lineAscent = Math.max(lineAscent, wordsAscent);
          lineDescent = Math.max(lineDescent, wordsDescent);
        }
        wordsChars = "";
        wordsWidth = wordsAscent = wordsDescent = 0;
      }
      if (lineWidth > 0) {
        this._pushCharsToLines(lines, lineWidths, lineMaxSizes, lineChars, lineWidth, lineAscent, lineDescent);
        lineChars = "";
        lineWidth = lineAscent = lineDescent = 0;
      }
    }

    let height = renderer.height * _pixelsPerUnit;
    if (renderer.overflowMode === OverflowMode.Overflow) {
      height = lineHeight * lines.length;
    }

    return {
      width,
      height,
      lines,
      lineWidths,
      lineHeight,
      lineMaxSizes
    };
  }

cptbtptpbcptdtptp avatar Mar 02 '23 08:03 cptbtptpbcptdtptp

The current text alignment is centered, I think left alignment and top alignment are more commonly used

GuoLei1990 avatar Mar 23 '23 15:03 GuoLei1990