Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: text wrap all lines using flag #560

Merged
merged 2 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/draw/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export interface TextCompOpt {
* @since v2000.2
*/
styles?: Record<string, CharTransform | CharTransformFunc>;
/**
* 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.
*/
indentAll?: boolean;
}

export function text(t: string, opt: TextCompOpt = {}): TextComp {
Expand All @@ -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,
indentAll: opt.indentAll
}));

if (!opt.width) {
Expand Down
5 changes: 5 additions & 0 deletions src/gfx/draw/drawText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export type DrawTextOpt = RenderProps & {
* @since v2000.2
*/
styles?: Record<string, CharTransform | CharTransformFunc>;
/**
* 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.
*/
indentAll?: boolean
};

/**
Expand Down
7 changes: 6 additions & 1 deletion src/gfx/formatText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -288,6 +289,7 @@ export function formatText(opt: DrawTextOpt): FormattedText {
lastSpaceWidth = 0;
curX = 0;
curLine = [];
paraIndentX = undefined;
}
else {
let q = font.map[ch];
Expand Down Expand Up @@ -316,7 +318,7 @@ export function formatText(opt: DrawTextOpt): FormattedText {
chars: curLine,
});

curX = 0;
curX = paraIndentX ?? 0;
curLine = [];
}

Expand Down Expand Up @@ -344,6 +346,9 @@ export function formatText(opt: DrawTextOpt): FormattedText {
lastSpace = curLine.length;
lastSpaceWidth = curX;
}
if (opt.indentAll && paraIndentX === undefined && /\S/.test(ch)) {
paraIndentX = curX;
}

curX += gw;
tw = Math.max(tw, curX);
Expand Down
24 changes: 24 additions & 0 deletions tests/playtests/textwrap.js
Original file line number Diff line number Diff line change
@@ -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,
indentAll: true
}),
]);
Loading