Skip to content

Commit

Permalink
Fixes font atlas corruption. The next line should be allocated in max…
Browse files Browse the repository at this point in the history
…Height pixels, not the height of the last character.
  • Loading branch information
mflerackers authored and lajbel committed Jan 15, 2025
1 parent 5270301 commit 79bb53c
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/gfx/formatText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { center } from "./stack";
type FontAtlas = {
font: BitmapFontData;
cursor: Vec2;
maxHeight: number;
outline: Outline | null;
};

Expand Down Expand Up @@ -85,12 +86,14 @@ export function compileStyledText(txt: string): {
if (x !== undefined) {
throw new Error(
"Styled text error: mismatched tags. "
+ `Expected [/${x}], got [/${gn}]`,
+ `Expected [/${x}], got [/${gn}]`,
);
}
else {throw new Error(
else {
throw new Error(
`Styled text error: stray end tag [/${gn}]`,
);}
);
}
}
}
else styleStack.push(gn);
Expand Down Expand Up @@ -143,14 +146,14 @@ export function formatText(opt: DrawTextOpt): FormattedText {
outline: Outline | null;
filter: TexFilter;
} = font instanceof FontData
? {
outline: font.outline,
filter: font.filter,
}
: {
outline: null,
filter: DEF_FONT_FILTER,
};
? {
outline: font.outline,
filter: font.filter,
}
: {
outline: null,
filter: DEF_FONT_FILTER,
};

// TODO: customizable font tex filter
const atlas: FontAtlas = fontAtlases[fontName] ?? {
Expand All @@ -167,6 +170,7 @@ export function formatText(opt: DrawTextOpt): FormattedText {
size: DEF_TEXT_CACHE_SIZE,
},
cursor: new Vec2(0),
maxHeight: 0,
outline: opts.outline,
};

Expand Down Expand Up @@ -237,7 +241,8 @@ export function formatText(opt: DrawTextOpt): FormattedText {
// if we are about to exceed the X axis of the texture, go to another line
if (atlas.cursor.x + w > FONT_ATLAS_WIDTH) {
atlas.cursor.x = 0;
atlas.cursor.y += h;
atlas.cursor.y += atlas.maxHeight;
atlas.maxHeight = 0;
if (atlas.cursor.y > FONT_ATLAS_HEIGHT) {
// TODO: create another atlas
throw new Error(
Expand All @@ -256,6 +261,7 @@ export function formatText(opt: DrawTextOpt): FormattedText {
);

atlas.cursor.x += w + 1;
atlas.maxHeight = Math.max(atlas.maxHeight, h);
}
}
}
Expand Down

0 comments on commit 79bb53c

Please sign in to comment.