Skip to content

Conversation

maliut
Copy link
Contributor

@maliut maliut commented Jun 9, 2025

Description of change

In the Text component, if the text contains Windows line ending (\r\n), setting wordWrap to true will cause \r\n to be treated as two lines.

import { Application, Text } from "pixi.js";

(async () => {
  // Create a new application
  const app = new Application();

  // Initialize the application
  await app.init({ background: "#ffffff", resizeTo: window });

  // Append the application canvas to the document body
  document.getElementById("pixi-container").appendChild(app.canvas);

  // Create a text including \r\n, and set wordWrap: true
  const text = new Text({ text: 'Hello\r\nPixiJS!', style: { wordWrap: true } })

  app.stage.addChild(text);
})();

When wordWrap is false:

image

When wordWrap is true:

image

View the source code in CanvasTextMetrics.ts:

const outputText = wordWrap ? CanvasTextMetrics._wordWrap(text, style, canvas) : text;
const lines = outputText.split(/(?:\r\n|\r|\n)/);

Although the \r\n case is already considered when splitting text into line, when wordWrap is true, the CanvasTextMetrics._wordWrap method is called first, which internally invokes the _tokenize method. In this method, \r\n is split into two separate tokens.

In this pull request, we implemented special handling for \r\n, treating it as a single token, consistent with the behavior of a single \n, to avoid generating extra line breaks.

Pre-Merge Checklist
  • Tests and/or benchmarks are included
  • Lint process passed (npm run lint)
  • Tests passed (npm run test)

visuals.test.ts fails even though I haven't modified any files. Maybe an issue with environment.

Copy link

codesandbox-ci bot commented Jun 9, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 58c404c:

Sandbox Source
pixi.js-sandbox Configuration

Copy link
Member

@GoodBoyDigital GoodBoyDigital left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@Zyie Zyie added the ✅ Ready To Merge Helpful when issues are in the queue waiting to get merged. This means the PR is completed and has t label Jun 30, 2025
@Zyie Zyie added this pull request to the merge queue Jun 30, 2025
Merged via the queue into pixijs:dev with commit 1bfeadb Jun 30, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✅ Ready To Merge Helpful when issues are in the queue waiting to get merged. This means the PR is completed and has t
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants