Skip to content

Commit

Permalink
Fixed text wrap obtaining the incorrect glyph width (#5620)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsparagusEduardo authored Nov 1, 2024
1 parent 56331ab commit ac1dd85
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion include/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ bool32 TextPrinterWait(struct TextPrinter *textPrinter);
void DrawDownArrow(u8 windowId, u16 x, u16 y, u8 bgColor, bool32 drawArrow, u8 *counter, u8 *yCoordIndex);
s32 GetGlyphWidth(u16 glyphId, bool32 isJapanese, u8 fontId);
s32 GetStringWidth(u8 fontId, const u8 *str, s16 letterSpacing);
s32 GetStringLineWidth(u8 fontId, const u8 *str, s16 letterSpacing, u32 lineNum, u32 strSize, bool32 printDebug);
s32 GetStringLineWidth(u8 fontId, const u8 *str, s16 letterSpacing, u32 lineNum, u32 strSize);
u8 RenderTextHandleBold(u8 *pixels, u8 fontId, u8 *str);
u8 DrawKeypadIcon(u8 windowId, u8 keypadIconId, u16 x, u16 y);
u8 GetKeypadIconTileOffset(u8 keypadIconId);
Expand Down
14 changes: 7 additions & 7 deletions src/battle_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -3097,7 +3097,7 @@ u32 BattleStringExpandPlaceholdersToDisplayedString(const u8 *src)
u32 dstID = BattleStringExpandPlaceholders(src, gDisplayedStringBattle, sizeof(gDisplayedStringBattle));
for (j = 1;; j++)
{
strWidth = GetStringLineWidth(0, gDisplayedStringBattle, 0, j, sizeof(gDisplayedStringBattle), TRUE);
strWidth = GetStringLineWidth(0, gDisplayedStringBattle, 0, j, sizeof(gDisplayedStringBattle));
if (strWidth == 0)
break;
}
Expand Down Expand Up @@ -3164,7 +3164,7 @@ static void GetBattlerNick(u32 battler, u8 *dst)
} \
GetBattlerNick(battler, text); \
toCpy = text; \
dstWidth = GetStringLineWidth(fontId, dst, letterSpacing, lineNum, dstSize, FALSE);
dstWidth = GetStringLineWidth(fontId, dst, letterSpacing, lineNum, dstSize);

#define HANDLE_NICKNAME_STRING_LOWERCASE(battler) \
if (GetBattlerSide(battler) != B_SIDE_PLAYER) \
Expand All @@ -3182,7 +3182,7 @@ static void GetBattlerNick(u32 battler, u8 *dst)
} \
GetBattlerNick(battler, text); \
toCpy = text; \
dstWidth = GetStringLineWidth(fontId, dst, letterSpacing, lineNum, dstSize, FALSE);
dstWidth = GetStringLineWidth(fontId, dst, letterSpacing, lineNum, dstSize);

static const u8 *BattleStringGetOpponentNameByTrainerId(u16 trainerId, u8 *text, u8 multiplayerId, u8 battler)
{
Expand Down Expand Up @@ -3357,7 +3357,7 @@ u32 BattleStringExpandPlaceholders(const u8 *src, u8 *dst, u32 dstSize)
while (*src != EOS)
{
toCpy = NULL;
dstWidth = GetStringLineWidth(fontId, dst, letterSpacing, lineNum, dstSize, FALSE);
dstWidth = GetStringLineWidth(fontId, dst, letterSpacing, lineNum, dstSize);

if (*src == PLACEHOLDER_BEGIN)
{
Expand Down Expand Up @@ -3749,12 +3749,12 @@ u32 BattleStringExpandPlaceholders(const u8 *src, u8 *dst, u32 dstSize)

if (toCpy != NULL)
{
toCpyWidth = GetStringLineWidth(fontId, toCpy, letterSpacing, 1, dstSize, FALSE);
toCpyWidth = GetStringLineWidth(fontId, toCpy, letterSpacing, 1, dstSize);

if (dstWidth + toCpyWidth > BATTLE_MSG_MAX_WIDTH)
{
dst[lastValidSkip] = lineNum == 1 ? CHAR_NEWLINE : CHAR_PROMPT_SCROLL;
dstWidth = GetStringLineWidth(fontId, dst, letterSpacing, lineNum, dstSize, FALSE);
dstWidth = GetStringLineWidth(fontId, dst, letterSpacing, lineNum, dstSize);
lineNum++;
}
while (*toCpy != EOS)
Expand All @@ -3776,7 +3776,7 @@ u32 BattleStringExpandPlaceholders(const u8 *src, u8 *dst, u32 dstSize)
}
else
{
toCpyWidth = GetGlyphWidth(dst[dstID + 1], FALSE, fontId);
toCpyWidth = GetGlyphWidth(*src, FALSE, fontId);
dst[dstID] = *src;
if (dstWidth + toCpyWidth > BATTLE_MSG_MAX_WIDTH)
{
Expand Down
16 changes: 8 additions & 8 deletions src/fonts.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ALIGNED(4) const u8 gFontSmallNarrowLatinGlyphWidths[] = {
5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5,
5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 5,
4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7,
3, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 8, 3, 3, 3, 3,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
Expand Down Expand Up @@ -53,7 +53,7 @@ ALIGNED(4) const u8 gFontSmallLatinGlyphWidths[] = {
5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5,
5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 8,
7, 5, 5, 5, 5, 5, 5, 9, 3, 3, 3, 3, 3, 3, 3, 3,
7, 5, 5, 5, 5, 5, 5, 9, 3, 3, 3, 8, 3, 3, 3, 3,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
Expand Down Expand Up @@ -89,7 +89,7 @@ ALIGNED(4) const u8 gFontNarrowLatinGlyphWidths[] = {
5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5,
4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 8,
3, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 8, 3, 3, 3, 3,
10, 10, 10, 10, 8, 8, 10, 8, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
Expand Down Expand Up @@ -125,7 +125,7 @@ ALIGNED(4) const u8 gFontShortLatinGlyphWidths[] = {
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 4, 6, 5,
5, 6, 5, 6, 6, 6, 5, 5, 5, 6, 6, 6, 6, 6, 6, 8,
5, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3,
5, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 8, 3, 3, 3, 3,
12, 12, 12, 12, 8, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
Expand Down Expand Up @@ -161,7 +161,7 @@ ALIGNED(4) const u8 gFontNormalLatinGlyphWidths[] = {
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 5, 6,
4, 6, 6, 6, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 8,
3, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 8, 3, 3, 3, 3,
10, 10, 10, 10, 8, 10, 10, 8, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
Expand Down Expand Up @@ -197,7 +197,7 @@ ALIGNED(4) const u8 gFontNarrowerLatinGlyphWidths[] = {
4, 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 4, 4, 4, 4,
4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4,
2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8,
4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 8, 3, 3, 3, 3,
10, 10, 10, 10, 8, 8, 10, 8, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
Expand Down Expand Up @@ -233,7 +233,7 @@ ALIGNED(4) const u8 gFontSmallNarrowerLatinGlyphWidths[] = {
4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 3, 4,
2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 7,
4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 8, 3, 3, 3, 3,
8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
Expand Down Expand Up @@ -269,7 +269,7 @@ ALIGNED(4) const u8 gFontShortNarrowLatinGlyphWidths[] = {
5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5,
5, 5, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5,
4, 6, 5, 5, 5, 5, 5, 5, 4, 5, 5, 6, 4, 5, 5, 8,
5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3,
5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 8, 3, 3, 3, 3,
12, 12, 12, 12, 8, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
Expand Down
6 changes: 1 addition & 5 deletions src/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ s32 GetStringWidth(u8 fontId, const u8 *str, s16 letterSpacing)
return width;
}

s32 GetStringLineWidth(u8 fontId, const u8 *str, s16 letterSpacing, u32 lineNum, u32 strSize, bool32 printDebug)
s32 GetStringLineWidth(u8 fontId, const u8 *str, s16 letterSpacing, u32 lineNum, u32 strSize)
{
u32 strWidth = 0, strLen, currLine;
u8 strCopy[strSize];
Expand All @@ -1621,10 +1621,6 @@ s32 GetStringLineWidth(u8 fontId, const u8 *str, s16 letterSpacing, u32 lineNum,
strWidth = GetStringWidth(fontId, strCopy, letterSpacing);
strLen = StringLineLength(strCopy);
StringAppend(strCopy, gText_EmptyString3);
#ifndef NDEBUG
if (printDebug && strWidth != 0)
DebugPrintf(" Line %d, len:%d, width:%d, \"%S\"", currLine, strLen, strWidth, strCopy);
#endif
}
str += strLen + 1;
}
Expand Down
2 changes: 1 addition & 1 deletion test/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ TEST("Battle strings fit on the battle message window")
DebugPrintf("Battle String ID %d: %S", battleStringId + BATTLESTRINGS_TABLE_START, battleString);
for (j = 1;; j++)
{
strWidth = GetStringLineWidth(fontId, battleString, 0, j, BATTLE_STRING_BUFFER_SIZE, TRUE);
strWidth = GetStringLineWidth(fontId, battleString, 0, j, BATTLE_STRING_BUFFER_SIZE);
if (strWidth == 0)
break;
EXPECT_LE(strWidth - 1, BATTLE_MSG_MAX_WIDTH); // -1 because there's a pixel-wide space that doesn't visually look like it's out of frame when using FONT_NORMAL.
Expand Down

0 comments on commit ac1dd85

Please sign in to comment.