BitmapFont.c 19.6 KB
Newer Older
1
/** @file
V
vit9696 已提交
2
  This file is part of OpenCanopy, OpenCore GUI.
3 4 5 6 7 8 9 10 11 12 13 14

  Copyright (c) 2018-2019, Download-Fritz. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-3-Clause
**/

#include <Base.h>

#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/OcGuardLib.h>

V
vit9696 已提交
15
#include "OpenCanopy.h"
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#include "BmfLib.h"

CONST BMF_CHAR *
BmfGetChar (
  IN CONST BMF_CONTEXT  *Context,
  IN UINT32             Char
  )
{
  CONST BMF_CHAR *Chars;
  UINTN          Left;
  UINTN          Right;
  UINTN          Median;
  UINTN          Index;

  ASSERT (Context != NULL);

  Chars = Context->Chars;

  for (Index = 0; Index < 2; ++Index) {
    //
    // Binary Search for the character as the list is sorted.
    //
    Left  = 0;
    //
    // As duplicates are not allowed, Right can be ceiled with Char.
    //
    Right = MIN (Context->NumChars, Char) - 1;
    while (Left <= Right) {
      //
      // This cannot wrap around due to the file size limitation.
      //
      Median = (Left + Right) / 2;
      if (Chars[Median].id == Char) {
        return &Chars[Median];
      } else if (Chars[Median].id < Char) {
        Left  = Median + 1;
      } else {
        Right = Median - 1;
      }
    }

    //
    // Fallback to underscore on not found symbols.
    //
    Char = '_';
  }

  //
  // Supplied font does not support even underscores.
  //
  return NULL;
}

CONST BMF_KERNING_PAIR *
BmfGetKerningPair (
  IN CONST BMF_CONTEXT  *Context,
  IN CHAR16             Char1,
  IN CHAR16             Char2
  )
{
  CONST BMF_KERNING_PAIR *Pairs;

  UINTN                  Left;
  UINTN                  Right;
  UINTN                  Median;

  UINTN                  Index;

  ASSERT (Context != NULL);

  Pairs = Context->KerningPairs;
87 88 89 90 91

  if (Pairs == NULL) {
    return NULL;
  }

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  //
  // Binary Search for the first character as the list is sorted.
  //
  Left  = 0;
  Right = Context->NumKerningPairs - 1;
  while (Left <= Right) {
    //
    // This cannot wrap around due to the file size limitation.
    //
    Median = (Left + Right) / 2;
    if (Pairs[Median].first == Char1) {
      //
      // Flat search for the second character as these are usually a lot less,
      // the list is sorted (relative to the first character).
      //
      if (Pairs[Median].second == Char2) {
        return &Pairs[Median];
      } else if (Pairs[Median].second < Char2) {
        for (
          Index = Median + 1;
          Index < Context->NumKerningPairs && Pairs[Index].first == Char1;
          ++Index
          ) {
          if (Pairs[Index].second == Char2) {
            return &Pairs[Index];
          }
        }
      } else {
        Index = Median;
        while (Index > 0) {
          --Index;
          if (Pairs[Index].first != Char1) {
            break;
          }

          if (Pairs[Index].second == Char2) {
            return &Pairs[Index];
          }
        }
      }

      break;
    } else if (Pairs[Median].first < Char1) {
      Left  = Median + 1;
    } else {
      Right = Median - 1;
    }
  }

  return NULL;
}

BOOLEAN
BmfContextInitialize (
  OUT BMF_CONTEXT  *Context,
  IN  CONST VOID   *FileBuffer,
  IN  UINT32       FileSize
  )
{
  BOOLEAN                Result;

  CONST BMF_HEADER       *Header;
  CONST BMF_BLOCK_HEADER *Block;
  UINTN                  Index;

  INT16                  MinY;
  UINT16                 MaxY;
159
  INT32                  Height;
160
  INT32                  Width;
161
  INT32                  Advance;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  CONST BMF_CHAR         *Chars;
  CONST BMF_KERNING_PAIR *Pairs;

  CONST BMF_CHAR         *Char;

  ASSERT (Context    != NULL);
  ASSERT (FileBuffer != NULL);
  ASSERT (FileSize    > 0);

  Header = FileBuffer;
  //
  // Limit file size for sanity reason and to guarantee wrapping around in BS
  // is not going to happen.
  //
  if (FileSize < sizeof (*Header) || FileSize > BASE_2MB) {
    DEBUG ((DEBUG_WARN, "BMF: FileSize insane\n"));
    return FALSE;
  }

  if (Header->signature[0] != 'B'
   || Header->signature[1] != 'M'
   || Header->signature[2] != 'F'
   || Header->version      != 3) {
    DEBUG ((DEBUG_WARN, "BMF: Header insane\n"));
    return FALSE;
  }

  ZeroMem (Context, sizeof (*Context));

  FileSize -= sizeof (*Header);
  Block = (CONST BMF_BLOCK_HEADER *)(Header + 1);

  while (FileSize >= sizeof (*Block)) {
    FileSize -= sizeof (*Block);
    if (FileSize < Block->size) {
      DEBUG ((DEBUG_WARN, "BMF: Block insane %u vs %u\n", FileSize, Block->size));
      return FALSE;
    }
    FileSize -= Block->size;

    switch (Block->identifier) {
      case BMF_BLOCK_INFO_ID:
      {
        if (Block->size < sizeof (BMF_BLOCK_INFO)) {
          DEBUG ((DEBUG_WARN, "BMF: BlockInfo insane\n"));
          return FALSE;
        }

210
        Context->Info = (CONST BMF_BLOCK_INFO *)(Block + 1);
211 212 213

        DEBUG ((
          DEBUG_INFO,
214
          "OCUI: Info->fontSize %u Info->bitField %u Info->charSet %u Info->stretchH %u Info->aa %u\n",
215 216 217 218
          Context->Info->fontSize,
          Context->Info->bitField,
          Context->Info->charSet,
          Context->Info->stretchH,
219 220 221 222 223
          Context->Info->aa
          ));
        DEBUG ((
          DEBUG_INFO,
          "OCUI: Info->paddingUp %u Info->paddingRight %u Info->paddingDown %u Info->paddingLeft %u\n",
224 225 226
          Context->Info->paddingUp,
          Context->Info->paddingRight,
          Context->Info->paddingDown,
227 228 229 230 231
          Context->Info->paddingLeft
          ));
        DEBUG ((
          DEBUG_INFO,
          "OCUI: Info->spacingHoriz %u Info->spacingVert %u Info->outline %u Info->fontName %a\n",
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
          Context->Info->spacingHoriz,
          Context->Info->spacingVert,
          Context->Info->outline,
          Context->Info->fontName
          ));

        /*if ((Context->Info->bitField & BMF_BLOCK_INFO_BF_UNICODE) == 0) {
          DEBUG ((DEBUG_WARN, "BMF: Only Unicode is supported\n"));
          return FALSE;
        }*/

        break;
      }

      case BMF_BLOCK_COMMON_ID:
      {
        if (Block->size != sizeof (BMF_BLOCK_COMMON)) {
          DEBUG ((DEBUG_WARN, "BMF: Block Common insane\n"));
          return FALSE;
        }

        Context->Common = (CONST BMF_BLOCK_COMMON *)(Block + 1);
        if (Context->Common->pages != 1) {
          DEBUG ((DEBUG_WARN, "BMF: Only one page is supported\n"));
          return FALSE;
        }

        break;
      }

      case BMF_BLOCK_PAGES_ID:
      {
        Context->Pages = (CONST BMF_BLOCK_PAGES *)(Block + 1);
        break;
      }

      case BMF_BLOCK_CHARS_ID:
      {
        if (Block->size % sizeof (BMF_CHAR) != 0) {
          DEBUG ((DEBUG_WARN, "BMF: Block chars size unaligned\n"));
          return FALSE;
        }

        Context->Chars    = (CONST BMF_BLOCK_CHARS *)(Block + 1);
        Context->NumChars = Block->size / sizeof (BMF_CHAR);
        break;
      }

      case BMF_BLOCK_KERNING_PAIRS_ID:
      {
        if (Block->size % sizeof (BMF_KERNING_PAIR) != 0) {
          DEBUG ((DEBUG_WARN, "BMF: Block Pairs unaligned\n"));
          return FALSE;
        }

        Context->KerningPairs    = (CONST BMF_BLOCK_KERNING_PAIRS *)(Block + 1);
        Context->NumKerningPairs = Block->size / sizeof (BMF_KERNING_PAIR);
        break;
      }

      default:
      {
        //
        // Ignore potential trailer.
        //
        FileSize = 0;
        break;
      }
    }

    Block = (CONST BMF_BLOCK_HEADER *)((UINTN)(Block + 1) + Block->size);
  }

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  if (Context->Info == NULL) {
    DEBUG ((DEBUG_WARN, "BMF: Missing Info block\n"));
    return FALSE;
  }

  if (Context->Common == NULL) {
    DEBUG ((DEBUG_WARN, "BMF: Missing Common block\n"));
    return FALSE;
  }

  if (Context->Pages == NULL) {
    DEBUG ((DEBUG_WARN, "BMF: Missing Pages block\n"));
    return FALSE;
  }

  if (Context->Chars == NULL) {
    DEBUG ((DEBUG_WARN, "BMF: Missing Chars block\n"));
322 323 324 325 326 327 328 329
    return FALSE;
  }

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

  for (Index = 0; Index < Context->NumChars; ++Index) {
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    Result = OcOverflowAddS32 (
               Chars[Index].yoffset,
               Chars[Index].height,
               &Height
               );
    Result |= OcOverflowAddS32 (
               Chars[Index].xoffset,
               Chars[Index].width,
               &Width
               );
    Result |= OcOverflowAddS32 (
               Chars[Index].xoffset,
               Chars[Index].xadvance,
               &Advance
               );
    if (Result
     || 0 > Height || Height > MAX_UINT16
     || 0 > Width || Width > MAX_UINT16
     || 0 > Advance || Advance > MAX_UINT16
     || Chars[Index].xadvance < 0) {
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
      DEBUG ((
        DEBUG_WARN,
        "BMF: Char insane\n"
        " id %u\n"
        " x %u\n"
        " y %u\n"
        " width %u\n"
        " height %u\n"
        " xoffset %d\n"
        " yoffset %d\n"
        " xadvance %d\n"
        " page %u\n"
        " chnl %u\n",
        Chars[Index].id,
        Chars[Index].x,
        Chars[Index].y,
        Chars[Index].width,
        Chars[Index].height,
        Chars[Index].xoffset,
        Chars[Index].yoffset,
        Chars[Index].xadvance,
        Chars[Index].page,
        Chars[Index].chnl
        ));
      return FALSE;
    }

    MinY = MIN (MinY, Chars[Index].yoffset);
378
    MaxY = MAX (MaxY, (UINT16) Height);
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    //
    // This only yields unexpected but not undefined behaviour when not met,
    // hence it is fine verifying it only DEBUG mode.
    //
    DEBUG_CODE_BEGIN ();
    if (Index > 0) {
      if (Chars[Index - 1].id > Chars[Index].id) {
        DEBUG ((DEBUG_WARN, "BMF: Character IDs are not sorted\n"));
        return FALSE;
      } else if (Chars[Index - 1].id == Chars[Index].id) {
        DEBUG ((DEBUG_WARN, "BMF: Character ID duplicate\n"));
        return FALSE;
      }
    }
    DEBUG_CODE_END ();
  }

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  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;
  }

412
  Context->Height  = Context->Common->lineHeight;
413 414 415
  Context->OffsetY = -MinY;

  Pairs = Context->KerningPairs;
416 417 418 419 420 421 422 423 424
  if (Pairs != NULL) { // According to the docs, kerning pairs are optional
    for (Index = 0; Index < Context->NumKerningPairs; ++Index) {
      Char = BmfGetChar (Context, Pairs[Index].first);
      if (Char == NULL) {
        DEBUG ((
          DEBUG_WARN,
          "BMF: Pair char %u not found\n",
          Pairs[Index].first
          ));
425 426 427
        return FALSE;
      }

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
      Result = OcOverflowAddS32 (
                 Char->xoffset + Char->width,
                 Pairs[Index].amount,
                 &Width
                 );
      Result |= OcOverflowAddS32 (
                  Char->xoffset + Char->xadvance,
                  Pairs[Index].amount,
                  &Advance
                  );
      if (Result
       || 0 > Width || Width > MAX_UINT16
       || 0 > Advance || Advance > MAX_UINT16) {
         DEBUG ((
           DEBUG_WARN,
           "BMF: Pair at index %d insane: first %u, second %u, amount %d\n",
           Index,
           Pairs[Index].first,
           Pairs[Index].second,
           Pairs[Index].amount
           ));
        return FALSE;
      }
      //
      // This only yields unexpected but not undefined behaviour when not met,
      // hence it is fine verifying it only DEBUG mode.
      //
      DEBUG_CODE_BEGIN ();
      if (Index > 0) {
        if (Pairs[Index - 1].first > Pairs[Index].first) {
          DEBUG ((DEBUG_WARN, "BMF: First Character IDs are not sorted\n"));
459 460
          return FALSE;
        }
461 462 463 464 465 466 467 468 469 470 471

        if (Pairs[Index - 1].first == Pairs[Index].first) {
          if (Pairs[Index - 1].second > Pairs[Index].second) {
            DEBUG ((DEBUG_WARN, "BMF: Second Character IDs are not sorted\n"));
            return FALSE;
          }
          if (Pairs[Index - 1].second == Pairs[Index].second) {
            DEBUG ((DEBUG_WARN, "BMF: Second Character ID duplicate\n"));
            return FALSE;
          }
        }
472
      }
473
      DEBUG_CODE_END ();
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    }
  }

  return TRUE;
}

typedef struct {
  UINT16                 Width;
  UINT16                 Height;
  INT16                  OffsetY;
  CONST BMF_CHAR         *Chars[];
//CONST BMF_KERNING_PAIR *KerningPairs[];
} BMF_TEXT_INFO;

BMF_TEXT_INFO *
BmfGetTextInfo (
  IN CONST BMF_CONTEXT  *Context,
  IN CONST CHAR16       *String,
  IN UINTN              StringLen,
  IN UINTN              MaxWidth
  )
{
  BOOLEAN                Result;

  BMF_TEXT_INFO          *TextInfo;
  CONST BMF_KERNING_PAIR **InfoPairs;

  INT32                  Width;
  INT32                  CurWidth;

  CONST BMF_CHAR         *Char;
  CONST BMF_KERNING_PAIR *Pair;
  UINTN                  Index;

  ASSERT (Context != NULL);
  ASSERT (String  != NULL);

  if (StringLen == 0) {
    return NULL;
  }

  ASSERT (String[0] != 0);

  Char = BmfGetChar (Context, String[0]);
  if (Char == NULL) {
    DEBUG ((DEBUG_WARN, "BMF: Text Char not found\n"));
    return NULL;
  }

  TextInfo = AllocatePool (
               sizeof (*TextInfo)
                 + StringLen * sizeof (BMF_CHAR *)
                 + StringLen * sizeof (BMF_BLOCK_KERNING_PAIRS *)
               );
  if (TextInfo == NULL) {
    DEBUG ((DEBUG_WARN, "BMF: Out of res\n"));
    return NULL;
  }
  InfoPairs = (CONST BMF_KERNING_PAIR **)&TextInfo->Chars[StringLen];

  TextInfo->Chars[0] = Char;
  Width = Char->xadvance;

  for (Index = 1; Index < StringLen; ++Index) {
    ASSERT (String[Index] != 0);

    Char = BmfGetChar (Context, String[Index]);
    if (Char == NULL) {
      DEBUG ((DEBUG_WARN, "BMF: Text Char not found\n"));
      FreePool (TextInfo);
      return NULL;
    }
    CurWidth = Char->xadvance;

    Pair = BmfGetKerningPair (Context, String[Index - 1], String[Index]);
    if (Pair != NULL) {
      CurWidth += Pair->amount;
    }

    Result = OcOverflowAddS32 (Width, CurWidth, &Width);
    if (Result) {
      DEBUG ((DEBUG_WARN, "BMF: width overflows\n"));
      FreePool (TextInfo);
      return NULL;
    }

    /*if (Width > MaxWidth) {
      break;
    }*/

    TextInfo->Chars[Index] = Char;
    InfoPairs[Index - 1]   = Pair;
  }

  if (Index != StringLen) {
    CONST BMF_KERNING_PAIR *PeriodPair;

    Char = BmfGetChar (Context, '.');
    if (Char == NULL) {
      FreePool (TextInfo);
      return NULL;
    }

    CurWidth = 2 * (INT32)Char->xadvance + (INT32)Char->xoffset + (INT32)Char->width;

    PeriodPair = BmfGetKerningPair (Context, '.', '.');
    if (PeriodPair != NULL) {
      CurWidth += 2 * (INT32)PeriodPair->amount;
    }
  }

  Width += ((INT32)TextInfo->Chars[Index - 1]->xoffset + (INT32)TextInfo->Chars[Index - 1]->width - (INT32)TextInfo->Chars[Index - 1]->xadvance);

  InfoPairs[Index - 1] = NULL;

  if (Width > MAX_UINT16) {
    DEBUG ((DEBUG_WARN, "BMF: Width exceeds bounds\n"));
    FreePool (TextInfo);
    return NULL;
  }

  TextInfo->Width   = (UINT16)Width;
  TextInfo->Height  = Context->Height;
  TextInfo->OffsetY = Context->OffsetY;
  return TextInfo;
}

601 602 603 604 605 606 607 608
STATIC
EFI_GRAPHICS_OUTPUT_BLT_PIXEL
mBlack = { 0, 0, 0, 255 };

STATIC
EFI_GRAPHICS_OUTPUT_BLT_PIXEL
mWhite = { 255, 255, 255, 255 };

609 610
STATIC
VOID
611
BlendMem (
612
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Dst,
613 614
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL *AlphaSrc,
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Color,
615 616 617 618 619 620
  UINTN                         PixelCount
  )
{
  UINTN  Index;

  for (Index = 0; Index < PixelCount; ++Index) {
621 622 623 624 625
    //
    // We assume that the font is generated by dpFontBaker
    // and has only gray channel, which should be interpreted as alpha.
    //
    GuiBlendPixel(Dst, Color, AlphaSrc->Red);
626
    ++Dst;
627
    ++AlphaSrc;
628 629 630
  }
}

631 632 633 634 635
BOOLEAN
GuiGetLabel (
  OUT GUI_IMAGE               *LabelImage,
  IN  CONST GUI_FONT_CONTEXT  *Context,
  IN  CONST CHAR16            *String,
636 637
  IN  UINTN                   StringLen,
  IN  BOOLEAN                 Inverted
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
  )
{
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Buffer;
  BMF_TEXT_INFO                 *TextInfo;
  CONST BMF_KERNING_PAIR        **InfoPairs;
  UINTN                         Index;

  UINT16                        RowIndex;
  UINT32                        SourceRowOffset;
  UINT32                        TargetRowOffset;
  INT32                         TargetCharX;
  INT32                         InitialCharX;
  INT32                         InitialWidthOffset;

  ASSERT (LabelImage != NULL);
  ASSERT (Context    != NULL);
  ASSERT (String     != NULL);

  TextInfo = BmfGetTextInfo (&Context->BmfContext, String, StringLen, 0);
  if (TextInfo == NULL) {
    DEBUG ((DEBUG_WARN, "BMF: GetTextInfo failed\n"));
    return FALSE;
  }

662
  Buffer = AllocateZeroPool ((UINT32) TextInfo->Width * (UINT32) TextInfo->Height * sizeof (*Buffer));
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
  if (Buffer == NULL) {
    DEBUG ((DEBUG_WARN, "BMF: out of res\n"));
    FreePool (TextInfo);
    return FALSE;
  }

  InfoPairs   = (CONST BMF_KERNING_PAIR **)&TextInfo->Chars[StringLen];
  TargetCharX = 0;

  if (TextInfo->Chars[0]->xoffset >= 0) {
    InitialCharX       = 0;
    InitialWidthOffset = 0;
  } else {
    InitialCharX       = -TextInfo->Chars[0]->xoffset;
    InitialWidthOffset = TextInfo->Chars[0]->xoffset;
  }

  for (Index = 0; Index < StringLen; ++Index) {
    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;
      RowIndex < TextInfo->Chars[Index]->height;
      ++RowIndex,
        SourceRowOffset += Context->FontImage.Width,
        TargetRowOffset += TextInfo->Width
      ) {
692 693

      if (Inverted) {
694
        BlendMem (
695 696
          &Buffer[TargetRowOffset + TargetCharX + TextInfo->Chars[Index]->xoffset + InitialCharX],
          &Context->FontImage.Buffer[SourceRowOffset + TextInfo->Chars[Index]->x + InitialCharX],
697
          &mBlack,
698 699 700
          (TextInfo->Chars[Index]->width + InitialWidthOffset)
          );
      } else {
701
        BlendMem (
702 703
          &Buffer[TargetRowOffset + TargetCharX + TextInfo->Chars[Index]->xoffset + InitialCharX],
          &Context->FontImage.Buffer[SourceRowOffset + TextInfo->Chars[Index]->x + InitialCharX],
704 705
          &mWhite,
          (TextInfo->Chars[Index]->width + InitialWidthOffset)
706 707
          );
      }
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
    }

    TargetCharX += TextInfo->Chars[Index]->xadvance;

    if (InfoPairs[Index] != NULL) {
      TargetCharX += InfoPairs[Index]->amount;
    }

    InitialCharX       = 0;
    InitialWidthOffset = 0;
  }

  LabelImage->Width  = TextInfo->Width;
  LabelImage->Height = TextInfo->Height;
  LabelImage->Buffer = Buffer;
  FreePool (TextInfo);
  return TRUE;
}

BOOLEAN
GuiFontConstruct (
  OUT GUI_FONT_CONTEXT  *Context,
  IN  VOID              *FontImage,
  IN  UINTN             FontImageSize,
  IN  VOID              *FileBuffer,
  IN  UINT32            FileSize
  )
{
736
  EFI_STATUS    Status;
737 738 739 740 741 742 743 744 745 746 747 748 749 750
  BOOLEAN       Result;

  ASSERT (Context       != NULL);
  ASSERT (FontImage     != NULL);
  ASSERT (FontImageSize > 0);
  ASSERT (FileBuffer    != NULL);
  ASSERT (FileSize      > 0);

  ZeroMem (Context, sizeof (*Context));

  Context->KerningData = FileBuffer;
  Status = GuiPngToImage (
    &Context->FontImage,
    FontImage,
751 752
    FontImageSize,
    FALSE
753 754 755
    );
  FreePool (FontImage);

756
  if (EFI_ERROR (Status)) {
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
    GuiFontDestruct (Context);
    return FALSE;
  }

  Result = BmfContextInitialize (&Context->BmfContext, FileBuffer, FileSize);
  if (!Result) {
    GuiFontDestruct (Context);
    return FALSE;
  }

  // TODO: check file size
  return TRUE;
}

VOID
GuiFontDestruct (
  IN GUI_FONT_CONTEXT  *Context
  )
{
  ASSERT (Context != NULL);
  if (Context->FontImage.Buffer != NULL) {
    FreePool (Context->FontImage.Buffer);
779
    Context->FontImage.Buffer = NULL;
780 781 782
  }
  if (Context->KerningData != NULL) {
    FreePool (Context->KerningData);
783
    Context->KerningData = NULL;
784 785
  }
}