]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/HiiDatabaseDxe/Font.c
MdeModulePkg/HiiDatabase: Remove the incorrect calculation of font baseline
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / Font.c
index f3fb43af7529ec3a21df048e46e2b8036bc75b28..5ecd6bd4a1e4f0d32e9c857a8451c7d7660b5824 100644 (file)
@@ -2,7 +2,7 @@
 Implementation for EFI_HII_FONT_PROTOCOL.\r
 \r
 \r
-Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -704,10 +704,6 @@ FindGlyphBlock (
       (UINT8 *) FontPackage->FontPkgHdr + 3 * sizeof (UINT32),\r
       sizeof (EFI_HII_GLYPH_INFO)\r
       );\r
-    BaseLine = (UINT16) (LocalCell.Height + LocalCell.OffsetY);\r
-    if (MinOffsetY > LocalCell.OffsetY) {\r
-      MinOffsetY = LocalCell.OffsetY;\r
-    }\r
   }\r
 \r
   BlockPtr    = FontPackage->GlyphBlock;\r
@@ -757,12 +753,12 @@ FindGlyphBlock (
       break;\r
 \r
     case EFI_HII_GIBT_EXT1:\r
-      BlockPtr += *(BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8));\r
+      BlockPtr += *(UINT8*)((UINTN)BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8));\r
       break;\r
     case EFI_HII_GIBT_EXT2:\r
       CopyMem (\r
         &Length16,\r
-        BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8),\r
+        (UINT8*)((UINTN)BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8)),\r
         sizeof (UINT16)\r
         );\r
       BlockPtr += Length16;\r
@@ -770,7 +766,7 @@ FindGlyphBlock (
     case EFI_HII_GIBT_EXT4:\r
       CopyMem (\r
         &Length32,\r
-        BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8),\r
+        (UINT8*)((UINTN)BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8)),\r
         sizeof (UINT32)\r
         );\r
       BlockPtr += Length32;\r
@@ -793,7 +789,7 @@ FindGlyphBlock (
       BufferLen = BITMAP_LEN_1_BIT (LocalCell.Width, LocalCell.Height);\r
       if (CharCurrent == CharValue) {\r
         return WriteOutputParam (\r
-                 BlockPtr + sizeof (EFI_HII_GIBT_GLYPH_BLOCK) - sizeof (UINT8),\r
+                 (UINT8*)((UINTN)BlockPtr + sizeof (EFI_HII_GIBT_GLYPH_BLOCK) - sizeof (UINT8)),\r
                  BufferLen,\r
                  &LocalCell,\r
                  GlyphBuffer,\r
@@ -933,16 +929,18 @@ SaveFontName (
   )\r
 {\r
   UINTN         FontInfoLen;\r
+  UINTN         NameSize;\r
 \r
   ASSERT (FontName != NULL && FontInfo != NULL);\r
 \r
-  FontInfoLen = sizeof (EFI_FONT_INFO) - sizeof (CHAR16) + StrSize (FontName);\r
+  NameSize = StrSize (FontName);\r
+  FontInfoLen = sizeof (EFI_FONT_INFO) - sizeof (CHAR16) + NameSize;\r
   *FontInfo = (EFI_FONT_INFO *) AllocateZeroPool (FontInfoLen);\r
   if (*FontInfo == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  StrCpy ((*FontInfo)->FontName, FontName);\r
+  StrCpyS ((*FontInfo)->FontName, NameSize / sizeof (CHAR16), FontName);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -971,6 +969,7 @@ GetSystemFont (
 {\r
   EFI_FONT_DISPLAY_INFO              *Info;\r
   UINTN                              InfoSize;\r
+  UINTN                              NameSize;\r
 \r
   if (Private == NULL || Private->Signature != HII_DATABASE_PRIVATE_DATA_SIGNATURE) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -982,7 +981,8 @@ GetSystemFont (
   //\r
   // The standard font always has the name "sysdefault".\r
   //\r
-  InfoSize = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + StrSize (L"sysdefault");\r
+  NameSize = StrSize (L"sysdefault");\r
+  InfoSize = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + NameSize;\r
   Info = (EFI_FONT_DISPLAY_INFO *) AllocateZeroPool (InfoSize);\r
   if (Info == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -993,7 +993,7 @@ GetSystemFont (
   Info->FontInfoMask       = EFI_FONT_INFO_SYS_FONT | EFI_FONT_INFO_SYS_SIZE | EFI_FONT_INFO_SYS_STYLE;\r
   Info->FontInfo.FontStyle = 0;\r
   Info->FontInfo.FontSize  = EFI_GLYPH_HEIGHT;\r
-  StrCpy (Info->FontInfo.FontName, L"sysdefault");\r
+  StrCpyS (Info->FontInfo.FontName, NameSize / sizeof (CHAR16), L"sysdefault");\r
 \r
   *FontInfo = Info;\r
   if (FontInfoSize != NULL) {\r
@@ -1040,8 +1040,12 @@ IsSystemFontInfo (
     return TRUE;\r
   }\r
 \r
+  SystemDefault = NULL;\r
+  DefaultLen    = 0;\r
+\r
   Status = GetSystemFont (Private, &SystemDefault, &DefaultLen);\r
   ASSERT_EFI_ERROR (Status);\r
+  ASSERT ((SystemDefault != NULL) && (DefaultLen != 0));\r
 \r
   //\r
   // Record the system default info.\r
@@ -1440,6 +1444,7 @@ IsLineBreak (
     //\r
     // Mandatory line break characters, which force a line-break\r
     //\r
+    case 0x000A:\r
     case 0x000C:\r
     case 0x000D:\r
     case 0x2028:\r
@@ -1604,6 +1609,7 @@ HiiStringToImage (
   UINTN                               StrLength;\r
   EFI_GRAPHICS_OUTPUT_BLT_PIXEL       *RowBufferPtr;\r
   HII_GLOBAL_FONT_INFO                *GlobalFont;\r
+  UINT32                              PreInitBkgnd;\r
 \r
   //\r
   // Check incoming parameters.\r
@@ -1687,6 +1693,7 @@ HiiStringToImage (
   SysFontFlag   = IsSystemFontInfo (Private, (EFI_FONT_DISPLAY_INFO *) StringInfo, &SystemDefault, NULL);\r
 \r
   if (SysFontFlag) {\r
+    ASSERT (SystemDefault != NULL);\r
     FontInfo   = NULL;\r
     Height     = SystemDefault->FontInfo.FontSize;\r
     BaseLine   = SystemDefault->FontInfo.FontSize;\r
@@ -1819,7 +1826,13 @@ HiiStringToImage (
   //\r
   Image     = *Blt;\r
   BufferPtr = Image->Image.Bitmap + Image->Width * BltY + BltX;\r
-  ASSERT (Image->Height >= BltY);\r
+  if (Image->Height < BltY) {\r
+    //\r
+    // the top edge of the image should be in Image resolution scope.\r
+    //\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Exit;\r
+  }\r
   MaxRowNum = (UINT16) ((Image->Height - BltY) / Height);\r
   if ((Image->Height - BltY) % Height != 0) {\r
     LastLineHeight = (Image->Height - BltY) % Height;\r
@@ -1917,7 +1930,7 @@ HiiStringToImage (
       // When EFI_HII_OUT_FLAG_CLEAN_X is set, it will not draw a character\r
       // if its right-most on pixel cannot fit.\r
       //\r
-      if (Index > 0) {\r
+      if (Index > RowInfo[RowIndex].StartIndex) {\r
         //\r
         // Don't draw the last char on this row. And, don't draw the second last char (AdvanceX - Width - OffsetX).\r
         //\r
@@ -1929,10 +1942,11 @@ HiiStringToImage (
         RowInfo[RowIndex].BaselineOffset = BaseLineOffset;\r
       } else {\r
         //\r
-        // There is only one column and it can not be drawn so that return directly.\r
+        // There is no enough column to draw any character, so set current line width to zero.\r
+        // And go to draw Next line if LineBreak is set.\r
         //\r
-        Status = EFI_SUCCESS;\r
-        goto Exit;\r
+        RowInfo[RowIndex].LineWidth      = 0;\r
+        goto NextLine;\r
       }\r
     }\r
 \r
@@ -1941,7 +1955,9 @@ HiiStringToImage (
     // opportunity prior to a character whose right-most extent would exceed Width.\r
     // Search the right-most line-break opportunity here.\r
     //\r
-    if ((Flags & EFI_HII_OUT_FLAG_WRAP) == EFI_HII_OUT_FLAG_WRAP && StringPtr[NextIndex] != 0 && !LineBreak) {\r
+    if ((Flags & EFI_HII_OUT_FLAG_WRAP) == EFI_HII_OUT_FLAG_WRAP && \r
+        (RowInfo[RowIndex].LineWidth + BltX > Image->Width || StringPtr[NextIndex] != 0) && \r
+        !LineBreak) {\r
       if ((Flags & EFI_HII_IGNORE_LINE_BREAK) == 0) {\r
         LineWidth = RowInfo[RowIndex].LineWidth;\r
         for (Index1 = RowInfo[RowIndex].EndIndex; Index1 >= RowInfo[RowIndex].StartIndex; Index1--) {\r
@@ -1986,8 +2002,26 @@ HiiStringToImage (
       // behave as if EFI_HII_OUT_FLAG_CLEAN_X is set.\r
       //\r
       if (!LineBreak) {\r
-        Flags &= (~ (EFI_HII_OUT_FLAGS) EFI_HII_OUT_FLAG_WRAP);\r
-        Flags |= EFI_HII_OUT_FLAG_CLIP_CLEAN_X;\r
+        LineWidth = RowInfo[RowIndex].LineWidth;\r
+        Index1    = RowInfo[RowIndex].EndIndex;\r
+        if (LineWidth + BltX > Image->Width) {\r
+          if (Index1 > RowInfo[RowIndex].StartIndex) {\r
+            //\r
+            // Don't draw the last char on this row. And, don't draw the second last char (AdvanceX - Width - OffsetX).\r
+            //\r
+            LineWidth -= (UINTN) (Cell[Index1].Width + Cell[Index1].OffsetX);\r
+            LineWidth -= (UINTN) (Cell[Index1 - 1].AdvanceX - Cell[Index1 - 1].Width - Cell[Index1 - 1].OffsetX);\r
+            RowInfo[RowIndex].EndIndex       = Index1 - 1;\r
+            RowInfo[RowIndex].LineWidth      = LineWidth;\r
+          } else {\r
+            //\r
+            // There is no enough column to draw any character, so set current line width to zero.\r
+            // And go to draw Next line if LineBreak is set.\r
+            //\r
+            RowInfo[RowIndex].LineWidth = 0;\r
+            goto NextLine;\r
+          }\r
+        }\r
       }\r
     }\r
     \r
@@ -2012,6 +2046,11 @@ HiiStringToImage (
           goto Exit;\r
         }\r
         //\r
+        // Initialize the background color.\r
+        //\r
+        PreInitBkgnd = Background.Blue | Background.Green << 8 | Background.Red << 16;\r
+        SetMem32 (BltBuffer,RowInfo[RowIndex].LineWidth * RowInfo[RowIndex].LineHeight * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL),PreInitBkgnd);\r
+        //\r
         // Set BufferPtr to Origin by adding baseline to the starting position.\r
         //\r
         BufferPtr = BltBuffer + BaseLine * RowInfo[RowIndex].LineWidth;\r
@@ -2117,10 +2156,10 @@ HiiStringToImage (
       }\r
     }\r
 \r
+NextLine:\r
     //\r
-    // Recalculate the start point of X/Y axis to draw multi-lines with the order of top-to-down\r
+    // Recalculate the start point of Y axis to draw multi-lines with the order of top-to-down\r
     //\r
-    BltX = 0;\r
     BltY += RowInfo[RowIndex].LineHeight;\r
 \r
     RowIndex++;\r
@@ -2276,6 +2315,7 @@ HiiStringIdToImage (
   EFI_STRING                          String;\r
   UINTN                               StringSize;\r
   UINTN                               FontLen;\r
+  UINTN                               NameSize;\r
   EFI_FONT_INFO                       *StringFontInfo;\r
   EFI_FONT_DISPLAY_INFO               *NewStringInfo;\r
   CHAR8                               TempSupportedLanguages;\r
@@ -2299,7 +2339,6 @@ HiiStringIdToImage (
   CurrentLanguage    = NULL;\r
   BestLanguage       = NULL;\r
   String             = NULL;\r
-  StringInfo         = NULL;\r
   StringFontInfo     = NULL;\r
   NewStringInfo      = NULL;\r
 \r
@@ -2341,7 +2380,7 @@ HiiStringIdToImage (
   if (Language == NULL) {\r
     Language = "";\r
   }\r
-  CurrentLanguage = GetEfiGlobalVariable (L"PlatformLang");\r
+  GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&CurrentLanguage, NULL);\r
   BestLanguage = GetBestLanguage (\r
                    SupportedLanguages,\r
                    FALSE,\r
@@ -2399,7 +2438,8 @@ HiiStringIdToImage (
   // StringFontInfo equals NULL means system default font attaches with the string block.\r
   //\r
   if (StringFontInfo != NULL && IsSystemFontInfo (Private, (EFI_FONT_DISPLAY_INFO *) StringInfo, NULL, NULL)) {\r
-    FontLen = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + StrSize (StringFontInfo->FontName);\r
+    NameSize = StrSize (StringFontInfo->FontName);\r
+    FontLen = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + NameSize;\r
     NewStringInfo = AllocateZeroPool (FontLen);\r
     if (NewStringInfo == NULL) {      \r
       Status = EFI_OUT_OF_RESOURCES;\r
@@ -2408,7 +2448,7 @@ HiiStringIdToImage (
     NewStringInfo->FontInfoMask       = EFI_FONT_INFO_SYS_FORE_COLOR | EFI_FONT_INFO_SYS_BACK_COLOR;\r
     NewStringInfo->FontInfo.FontStyle = StringFontInfo->FontStyle;\r
     NewStringInfo->FontInfo.FontSize  = StringFontInfo->FontSize;    \r
-    StrCpy (NewStringInfo->FontInfo.FontName, StringFontInfo->FontName);\r
+    StrCpyS (NewStringInfo->FontInfo.FontName, NameSize / sizeof (CHAR16), StringFontInfo->FontName);\r
   \r
     Status = HiiStringToImage (\r
                This, \r
@@ -2553,6 +2593,7 @@ HiiGetGlyph (
     Foreground = StringInfoOut->ForegroundColor;\r
     Background = StringInfoOut->BackgroundColor;\r
   } else {\r
+    ASSERT (SystemDefault != NULL);\r
     Foreground = SystemDefault->ForegroundColor;\r
     Background = SystemDefault->BackgroundColor;\r
   }\r
@@ -2652,14 +2693,15 @@ Exit:
                                   with the  first font. On return, points to the\r
                                   returned font handle or points to NULL if there\r
                                   are no more matching fonts.\r
-  @param  StringInfoIn            Upon entry, points to the font to return\r
-                                  information about. \r
-                                  If NULL, then the information about the system default \r
-                                  font will be returned.\r
-  @param  StringInfoOut           Upon return, contains the matching font's\r
-                                  information.  If NULL, then no information is\r
-                                  returned. It's caller's responsibility to free\r
-                                  this buffer.\r
+  @param  StringInfoIn            Upon entry, points to the font to return information\r
+                                  about. If NULL, then the information about the system\r
+                                  default font will be returned.\r
+  @param  StringInfoOut           Upon return, contains the matching font's information.\r
+                                  If NULL, then no information is returned. This buffer\r
+                                  is allocated with a call to the Boot Service AllocatePool().\r
+                                  It is the caller's responsibility to call the Boot \r
+                                  Service FreePool() when the caller no longer requires\r
+                                  the contents of StringInfoOut.\r
   @param  String                  Points to the string which will be tested to\r
                                   determine  if all characters are available. If\r
                                   NULL, then any font  is acceptable.\r
@@ -2695,6 +2737,7 @@ HiiGetFontInfo (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  StringInfoOutLen = 0;\r
   FontInfo        = NULL;\r
   SystemDefault   = NULL;\r
   LocalFontHandle = NULL;\r