Skip to content

Add justified text support to text elements - #9185

Merged
mvaligursky merged 3 commits into
mainfrom
mv-text-justify
Aug 17, 2026
Merged

Add justified text support to text elements#9185
mvaligursky merged 3 commits into
mainfrom
mv-text-justify

Conversation

@mvaligursky

@mvaligursky mvaligursky commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #4633.

Adds justified text to element components, fixing #4633. Wrapped lines are stretched to be flush with both edges of the element by widening the gaps between their words.

Changes:

  • New justify property on text elements, off by default. It only has an effect when wrapLines is true and the element has a width of its own to wrap against.
  • Lines that must not be stretched are excluded: the last line of the text, lines ended by an explicit line break, and lines with no gaps to widen (a single long word). Those keep following alignment, which is what decides where they sit — the same split as CSS text-align and text-align-last.
  • Line breaking is unaffected. Lines are broken at their natural widths first and justification is applied afterwards as a positioning pass, so turning it on never reflows the text.
  • Works with right-to-left text, where a justified line is flush with both edges in the same way.
  • Plumbed through addComponent data and clone() alongside the other text properties.

API Changes:

  • Added ElementComponent#justify (boolean, defaults to false).
entity.addComponent('element', {
    type: pc.ELEMENTTYPE_TEXT,
    text: paragraph,
    wrapLines: true,
    autoWidth: false,
    width: 620,
    justify: true,
    alignment: new pc.Vec2(0, 1)  // still positions the last line
});

Examples:

  • New user-interface/text-justify example, with a toggle for justify and a selector for alignment so the two can be combined, and a readout of the resulting values.

Performance:

  • No effect when justify is false: the per-glyph gap offset is only read for lines that are actually being stretched.

@github-actions

Copy link
Copy Markdown

Public API report

This PR changes the public API surface (+2 / −0), per the docs' rules (@ignore / @Private / undocumented are excluded).

Show API diff
+ElementComponent.get justify(): boolean
+ElementComponent.set justify(arg: boolean)

Informational only — this never fails the build.

@github-actions

github-actions Bot commented Aug 17, 2026

Copy link
Copy Markdown

Build size report

This PR changes the size of the minified bundles.

Bundle Minified Gzip Brotli
playcanvas.min.js 2367.2 KB (+0.7 KB, +0.03%) 608.0 KB (+0.3 KB, +0.06%) 472.2 KB (+0.4 KB, +0.08%)
playcanvas.min.mjs 2364.6 KB (+0.7 KB, +0.03%) 606.9 KB (+0.3 KB, +0.05%) 471.3 KB (+0.2 KB, +0.04%)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds justified wrapping support to text element components.

Changes:

  • Adds the public justify property and layout logic.
  • Supports initialization, cloning, RTL layout, and tests.
  • Adds an interactive justification example.

Reviewed changes

Copilot reviewed 6 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/framework/components/element/text-element.js Implements justified glyph positioning.
src/framework/components/element/component.js Exposes the public API.
src/framework/components/element/system.js Supports initialization and cloning.
test/framework/components/element/text-element.test.mjs Tests justification behavior.
examples/src/examples/user-interface/text-justify.example.mjs Demonstrates the feature.
examples/src/examples/user-interface/text-justify.controls.jsx Adds example controls.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread test/framework/components/element/text-element.test.mjs
Comment thread src/framework/components/element/text-element.js Outdated
Comment thread examples/src/examples/user-interface/text-justify.example.mjs Outdated
Martin Valigursky added 2 commits August 17, 2026 15:16
- gate the gap counting and the per-quad gapIndices storage on justify, so
  text that never justifies does not pay for the default-off feature
- assert that the rtl justify test really does enable rtl layout
- tidy up a comment in the example

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 8 changed files in this pull request and generated no new comments.

@mvaligursky
mvaligursky merged commit 7ebe22a into main Aug 17, 2026
11 checks passed
@mvaligursky
mvaligursky deleted the mv-text-justify branch August 17, 2026 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Justify text

2 participants