Skip to content

Commit

Permalink
OpenCanopy: Fix BMF font height calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
mhaeuser committed Apr 4, 2021
1 parent 3d0c818 commit 0cdde8d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
51 changes: 19 additions & 32 deletions Platform/OpenCanopy/BitmapFont.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ BmfContextInitialize (
CONST BMF_BLOCK_HEADER *Block;
UINTN Index;

INT16 MinY;
UINT16 MaxY;
UINT16 MaxHeight;
INT32 Height;
INT32 Width;
INT32 Advance;
Expand Down Expand Up @@ -323,8 +322,7 @@ BmfContextInitialize (
}

Chars = Context->Chars;
MinY = MAX_INT16;
MaxY = 0;
MaxHeight = 0;

for (Index = 0; Index < Context->NumChars; ++Index) {
Result = OcOverflowAddS32 (
Expand Down Expand Up @@ -374,8 +372,7 @@ BmfContextInitialize (
return FALSE;
}

MinY = MIN (MinY, Chars[Index].yoffset);
MaxY = MAX (MaxY, (UINT16) Height);
MaxHeight = MAX (MaxHeight, (UINT16) Height);
//
// This only yields unexpected but not undefined behaviour when not met,
// hence it is fine verifying it only DEBUG mode.
Expand All @@ -393,24 +390,7 @@ BmfContextInitialize (
DEBUG_CODE_END ();
}

Result = OcOverflowSubS32 (
MaxY,
MinY,
&Height
);
if (Result
|| 0 >= Height || Height > MAX_UINT16) {
DEBUG ((
DEBUG_WARN,
"BMF: Insane font Y info %d %d\n",
MaxY,
MinY
));
return FALSE;
}

Context->Height = Context->Common->lineHeight;
Context->OffsetY = -MinY;
Context->Height = (UINT16) MaxHeight;

Pairs = Context->KerningPairs;
if (Pairs != NULL) { // According to the docs, kerning pairs are optional
Expand Down Expand Up @@ -513,11 +493,6 @@ BmfGetTextInfo (
return NULL;
}

if (PosY < Context->OffsetY) {
DEBUG ((DEBUG_WARN, "BMF: Font has invalid minimum y offset.\n"));
return NULL;
}

ASSERT (String[0] != 0);

Char = BmfGetChar (Context, String[0]);
Expand All @@ -538,7 +513,7 @@ BmfGetTextInfo (
InfoPairs = (CONST BMF_KERNING_PAIR **)&TextInfo->Chars[StringLen];

TextInfo->Chars[0] = Char;
Width = PosX + Char->xadvance;
Width = (INT32) PosX + Char->xadvance;

for (Index = 1; Index < StringLen; ++Index) {
ASSERT (String[Index] != 0);
Expand Down Expand Up @@ -578,7 +553,8 @@ BmfGetTextInfo (
}

TextInfo->Width = (UINT16)Width;
TextInfo->Height = Context->Height;
ASSERT (PosY + Context->Height >= PosY);
TextInfo->Height = PosY + Context->Height;
TextInfo->OffsetY = PosY;
return TextInfo;
}
Expand Down Expand Up @@ -635,6 +611,7 @@ GuiGetLabel (
INT32 TargetCharX;
INT32 InitialCharX;
INT32 InitialWidthOffset;
INT32 OffsetY;

ASSERT (LabelImage != NULL);
ASSERT (Context != NULL);
Expand Down Expand Up @@ -666,12 +643,22 @@ GuiGetLabel (
InitialWidthOffset = TextInfo->Chars[0]->xoffset;

for (Index = 0; Index < StringLen; ++Index) {
OffsetY = TextInfo->Chars[Index]->yoffset + TextInfo->OffsetY;
if (OffsetY < 0) {
OffsetY = 0;
DEBUG ((
DEBUG_INFO,
"BMF: Char %d y-offset off-screen by %d pixels\n",
TextInfo->Chars[Index]->id,
-OffsetY
));
}
ASSERT (TextInfo->Chars[Index]->yoffset + TextInfo->OffsetY >= 0);

for (
RowIndex = 0,
SourceRowOffset = TextInfo->Chars[Index]->y * Context->FontImage.Width,
TargetRowOffset = (TextInfo->Chars[Index]->yoffset + TextInfo->OffsetY) * TextInfo->Width;
TargetRowOffset = OffsetY * TextInfo->Width;
RowIndex < TextInfo->Chars[Index]->height;
++RowIndex,
SourceRowOffset += Context->FontImage.Width,
Expand Down
1 change: 0 additions & 1 deletion Platform/OpenCanopy/BmfLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ typedef struct {
UINT32 NumChars;
UINT32 NumKerningPairs;
UINT16 Height;
INT16 OffsetY;
} BMF_CONTEXT;

typedef struct {
Expand Down

0 comments on commit 0cdde8d

Please sign in to comment.