# core/component/richtext (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/component/richtext/

Markup color name → HEX mapping. (compatible with Unity TMP)

## RichText extends Text

Rich text renderer component. (inherits Text)

- Automatically parses markup (Unity TMP compatible) and draws with font / size / color /
  bold / italic / underline / strikethrough applied differently per segment.
- Supported tags:
  - <b>...</b>                            : bold
  - <i>...</i>                            : italic
  - <u>...</u>                            : underline
  - <s>...</s>                            : strikethrough
  - <color=#RRGGBB[AA]>...</color>        : color (HEX)
  - <color=red>...</color>                : color (name)
  - <size=N>...</size>                    : font size
  - Unknown tags are output as text as is.
- Plain text without markup falls back to the parent (Text) draw behavior.
- Text direction: determines the paragraph direction from the joined segment text (this text's direction, or Graphic's direction if "inherit"), and places segments from the right if right to left.
  Each segment is baked in that direction, and alignment "start" / "end" is also resolved by that direction.
- Unspecified per-segment properties fall back to the parent Text's global properties.
- When disabled (isEnable() === false), skipped in draw / measurement.

Example: Style each piece of text with markup

```js
import { Engine, EngineConfiguration, Scene, Vector2, WorldNode, RichText } from "./build/vanilla.js";

class MainScene extends Scene {
	async load(engine) {
		await super.load(engine);
		const labelNode = new WorldNode();
		labelNode.setContentSize(Vector2.create(560, 60));
		labelNode.setLocalPosition(Vector2.create(320, 180));
		const richText = labelNode.addComponent(RichText);
		richText.setFontSize(28);
		richText.setTextColor("#ffffff");
		richText.setText("Get <color=#ffcc00><b>100</b></color> gold and <i>a <u>key</u></i>");
		const root = this.getRoot();
		root.addChild(labelNode);
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.2.1): Constructor.
- `calculateDrawnRect(rect)` (method, since 0.4.0): Writes the drawn rectangle to rect. (Override: same as Text without segments; with segments, the rectangle of the joined single line)
  - The summed width of each segment measured in its own font is placed by alignment (same as drawing) and grown by half the largest font size plus the thickest stroke on both sides.
    "start" and "end" become left or right depending on the direction at draw time, so both are covered.
  - Vertically, from the baseline position up and down by 1.5 times the largest font size plus the thickest stroke. (Underline and strikethrough fit inside)
  - false if it cannot be measured. (not culled)
  - `rect` Rect: Where to write the drawn rectangle. (origin at the top-left of the node's content area)
  - returns boolean: True if written. (False if it cannot be measured)

  Example: Seeing where segmented text draws

  ```js
  import { WorldNode, RichText, Rect, Vector2 } from "./build/vanilla.js";
  
  const node = new WorldNode();
  node.setContentSize(Vector2.create(200, 40));
  const richText = node.addComponent(RichText);
  richText.appendSegment("HP ", { bold: true });
  richText.appendSegment("42", { fontSize: 32 });
  const drawnRect = Rect.zero();
  const isKnown = richText.calculateDrawnRect(drawnRect);
  console.log(isKnown, drawnRect.size.y >= 32);
  ```

- `setText(text)` (method, since 0.2.1): Sets the text, automatically parsing markup.
  - If there are known tags, splits it into a segment array and stores it.
  - If there is no markup, clears the segments and falls back to plain text behavior.
  - `text` string: Text to show. (split into segments if it has known markup tags)
- `appendSegment(text, attributes)` (method, since 0.2.1): Adds 1 segment. (convenience API, for stacking segments programmatically besides markup)
  - `text` string: Segment text.
  - `attributes` object: Segment attributes. ({ fontFace, fontSize, textColor, strokeColor, strokeWidth, bold, italic, underline, strikethrough }, colors are Color, missing ones use the Text attributes)
  - returns RichText: This component. (calls can be chained)

  Example: Build segments in code

  ```js
  import { Engine, EngineConfiguration, Scene, Vector2, WorldNode, RichText, Color } from "./build/vanilla.js";
  
  class MainScene extends Scene {
  	async load(engine) {
  		await super.load(engine);
  		const labelNode = new WorldNode();
  		labelNode.setContentSize(Vector2.create(400, 60));
  		labelNode.setLocalPosition(Vector2.create(320, 180));
  		const richText = labelNode.addComponent(RichText);
  		richText.setFontSize(24);
  		const healthColor = Color.createFromHEX("#ff4444");
  		richText.appendSegment("HP ", { bold: true });
  		richText.appendSegment("42", { textColor: healthColor, fontSize: 32 });
  		richText.appendSegment(" / 100");
  		const segments = richText.getSegments();
  		console.log(segments.length);
  		const root = this.getRoot();
  		root.addChild(labelNode);
  	}
  }
  
  const engineConfiguration = new EngineConfiguration();
  engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
  const engine = new Engine(engineConfiguration);
  engine.run(new MainScene());
  ```

- `clearSegments()` (method, since 0.2.1): Removes all segments.
- `getSegments()` (method, since 0.2.1): Returns the segment list. (copy)
  - returns { text: string, attributes: object }[]: Copy of the segment list. (an array of { text, attributes })
- `hasSegments()` (method, since 0.2.1): Whether it has segments.
  - returns boolean: True if at least one segment has text.
- `draw(graphic)` (method, since 0.2.1): Draw. (skipped if disabled; the parent's plain text behavior if segments are empty)
  - `graphic` Graphic: Graphics.
- `drawSegments(graphic, contentSize)` (method, since 0.2.1): Draws cumulatively left→right applying font / color / style per segment.
  - Alignment / baseline are based on the total width / maximum font size.
  - `graphic` Graphic: Graphics.
  - `contentSize` Vector2: The node's content size.
- `resolveSegmentFontFace(input, fallback)` (method, since 0.2.1): Interprets a segment's fontFace input and converts it to an applicable FontFace.
  - `input` FontFace | FontAsset | null | undefined: Segment font value. (its font if a FontAsset, fallback if null or undefined)
  - `fallback` FontFace | null: Font to use instead.
  - returns FontFace | null: Font to use. (system font if null)
- `getIntrinsicContentSize()` (method, since 0.2.1): Returns the natural size of the text.
  - If there are segments, the total segment width + maximum font size.
  - Otherwise, the parent (Text) plain text measurement.
  - returns Vector2: Intrinsic size. (sum of the segment widths and the largest font size, (-1, -1) if it cannot be measured or is disabled)
