From 3fb6d07975dafe7908d157dbfa268cc502d2b9c9 Mon Sep 17 00:00:00 2001 From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com> Date: Fri, 13 Dec 2024 23:40:15 -0500 Subject: [PATCH 1/2] feat: text wrap all lines using flag --- src/components/draw/text.ts | 6 ++++++ src/gfx/draw/drawText.ts | 5 +++++ src/gfx/formatText.ts | 7 ++++++- tests/playtests/textwrap.js | 24 ++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/playtests/textwrap.js diff --git a/src/components/draw/text.ts b/src/components/draw/text.ts index 2a3067d8..6c344345 100644 --- a/src/components/draw/text.ts +++ b/src/components/draw/text.ts @@ -129,6 +129,11 @@ export interface TextCompOpt { * @since v2000.2 */ styles?: Record; + /** + * If true, any (whitespace) indent on the first line of the paragraph + * will be copied to all of the lines for those parts that text-wrap. + */ + indentWholeParagraph?: boolean; } export function text(t: string, opt: TextCompOpt = {}): TextComp { @@ -144,6 +149,7 @@ export function text(t: string, opt: TextCompOpt = {}): TextComp { // TODO: shouldn't run when object / ancestor is paused transform: obj.textTransform, styles: obj.textStyles, + indentWholeParagraph: opt.indentWholeParagraph })); if (!opt.width) { diff --git a/src/gfx/draw/drawText.ts b/src/gfx/draw/drawText.ts index 6633ff41..1d9e5fca 100644 --- a/src/gfx/draw/drawText.ts +++ b/src/gfx/draw/drawText.ts @@ -67,6 +67,11 @@ export type DrawTextOpt = RenderProps & { * @since v2000.2 */ styles?: Record; + /** + * If true, any (whitespace) indent on the first line of the paragraph + * will be copied to all of the lines for those parts that text-wrap. + */ + indentWholeParagraph?: boolean }; /** diff --git a/src/gfx/formatText.ts b/src/gfx/formatText.ts index 5b54802d..bc8b8961 100644 --- a/src/gfx/formatText.ts +++ b/src/gfx/formatText.ts @@ -270,6 +270,7 @@ export function formatText(opt: DrawTextOpt): FormattedText { let cursor = 0; let lastSpace: number | null = null; let lastSpaceWidth: number = 0; + let paraIndentX: number | undefined = undefined; // TODO: word break while (cursor < chars.length) { @@ -288,6 +289,7 @@ export function formatText(opt: DrawTextOpt): FormattedText { lastSpaceWidth = 0; curX = 0; curLine = []; + paraIndentX = undefined; } else { let q = font.map[ch]; @@ -316,7 +318,7 @@ export function formatText(opt: DrawTextOpt): FormattedText { chars: curLine, }); - curX = 0; + curX = paraIndentX ?? 0; curLine = []; } @@ -344,6 +346,9 @@ export function formatText(opt: DrawTextOpt): FormattedText { lastSpace = curLine.length; lastSpaceWidth = curX; } + if (opt.indentWholeParagraph && paraIndentX === undefined && /\S/.test(ch)) { + paraIndentX = curX; + } curX += gw; tw = Math.max(tw, curX); diff --git a/tests/playtests/textwrap.js b/tests/playtests/textwrap.js new file mode 100644 index 00000000..a907fcd3 --- /dev/null +++ b/tests/playtests/textwrap.js @@ -0,0 +1,24 @@ +kaplay({ background: "#000000" }); + +const theText = `MAN PAGE + Very long description paragraph. Very long description paragraph. Very long description paragraph. Very long description paragraph. Very long description paragraph. Very long description paragraph. Very long description paragraph. + +ANOTHER SECTION + Yet some more text here. Yet some more text here. Yet some more text here. Yet some more text here.` + +add([ + pos(100, 100), + text(theText, { + size: 16, + width: 17 * 16, + }), +]); + +add([ + pos(400, 100), + text(theText, { + size: 16, + width: 17 * 16, + indentWholeParagraph: true + }), +]); From eb103a1c7049effecabd4bdb7123d1197142598e Mon Sep 17 00:00:00 2001 From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com> Date: Fri, 13 Dec 2024 23:54:58 -0500 Subject: [PATCH 2/2] indentWholeParagraph -> indentAll --- src/components/draw/text.ts | 4 ++-- src/gfx/draw/drawText.ts | 2 +- src/gfx/formatText.ts | 2 +- tests/playtests/textwrap.js | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/components/draw/text.ts b/src/components/draw/text.ts index 6c344345..1b2a37f6 100644 --- a/src/components/draw/text.ts +++ b/src/components/draw/text.ts @@ -133,7 +133,7 @@ export interface TextCompOpt { * If true, any (whitespace) indent on the first line of the paragraph * will be copied to all of the lines for those parts that text-wrap. */ - indentWholeParagraph?: boolean; + indentAll?: boolean; } export function text(t: string, opt: TextCompOpt = {}): TextComp { @@ -149,7 +149,7 @@ export function text(t: string, opt: TextCompOpt = {}): TextComp { // TODO: shouldn't run when object / ancestor is paused transform: obj.textTransform, styles: obj.textStyles, - indentWholeParagraph: opt.indentWholeParagraph + indentAll: opt.indentAll })); if (!opt.width) { diff --git a/src/gfx/draw/drawText.ts b/src/gfx/draw/drawText.ts index 1d9e5fca..bafb7b42 100644 --- a/src/gfx/draw/drawText.ts +++ b/src/gfx/draw/drawText.ts @@ -71,7 +71,7 @@ export type DrawTextOpt = RenderProps & { * If true, any (whitespace) indent on the first line of the paragraph * will be copied to all of the lines for those parts that text-wrap. */ - indentWholeParagraph?: boolean + indentAll?: boolean }; /** diff --git a/src/gfx/formatText.ts b/src/gfx/formatText.ts index bc8b8961..1340cb54 100644 --- a/src/gfx/formatText.ts +++ b/src/gfx/formatText.ts @@ -346,7 +346,7 @@ export function formatText(opt: DrawTextOpt): FormattedText { lastSpace = curLine.length; lastSpaceWidth = curX; } - if (opt.indentWholeParagraph && paraIndentX === undefined && /\S/.test(ch)) { + if (opt.indentAll && paraIndentX === undefined && /\S/.test(ch)) { paraIndentX = curX; } diff --git a/tests/playtests/textwrap.js b/tests/playtests/textwrap.js index a907fcd3..23c2f07b 100644 --- a/tests/playtests/textwrap.js +++ b/tests/playtests/textwrap.js @@ -19,6 +19,6 @@ add([ text(theText, { size: 16, width: 17 * 16, - indentWholeParagraph: true + indentAll: true }), ]);