]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Enable wide string support for CreatePopUp function in UefiLib.
authorydong10 <ydong10@6f19259b-4bc3-4df7-8a09-765794883524>
Mon, 6 Aug 2012 09:14:18 +0000 (09:14 +0000)
committerydong10 <ydong10@6f19259b-4bc3-4df7-8a09-765794883524>
Mon, 6 Aug 2012 09:14:18 +0000 (09:14 +0000)
Signed-off-by: Eric Dong <eric.dong@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13594 6f19259b-4bc3-4df7-8a09-765794883524

MdePkg/Library/UefiLib/Console.c

index 5a7684ecff585d9e79dc24b291fbc27088e26028..41a9f3db97ee7eea6b4e8b6d4bee879a58499961 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   This module provide help function for displaying unicode string.\r
 \r
-  Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2012, 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
@@ -22,6 +22,9 @@ typedef struct {
   UINT32  Width;\r
 } UNICODE_WIDTH_ENTRY;\r
 \r
+#define NARROW_CHAR         0xFFF0\r
+#define WIDE_CHAR           0xFFF1\r
+\r
 GLOBAL_REMOVE_IF_UNREFERENCED CONST UNICODE_WIDTH_ENTRY mUnicodeWidthTable[] = {\r
   //\r
   // General script area\r
@@ -288,6 +291,95 @@ UnicodeStringDisplayLength (
   return Length;\r
 }\r
 \r
+/**\r
+  Count the storage space of a Unicode string. \r
+\r
+  This function handles the Unicode string with NARROW_CHAR\r
+  and WIDE_CHAR control characters. NARROW_HCAR and WIDE_CHAR\r
+  does not count in the resultant output. If a WIDE_CHAR is\r
+  hit, then 2 Unicode character will consume an output storage\r
+  space with size of CHAR16 till a NARROW_CHAR is hit.\r
+\r
+  @param String          The input string to be counted.\r
+  @param LimitLen        Whether need to limit the string length.\r
+  @param MaxWidth        The max length this function supported.\r
+  @param Offset          The max index of the string can be show out. \r
+\r
+  @return Storage space for the input string.\r
+\r
+**/\r
+UINTN\r
+UefiLibGetStringWidth (\r
+  IN  CHAR16               *String,\r
+  IN  BOOLEAN              LimitLen,\r
+  IN  UINTN                MaxWidth,\r
+  OUT UINTN                *Offset\r
+  )\r
+{\r
+  UINTN Index;\r
+  UINTN Count;\r
+  UINTN IncrementValue;\r
+\r
+  if (String == NULL) {\r
+    return 0;\r
+  }\r
+\r
+  Index           = 0;\r
+  Count           = 0;\r
+  IncrementValue  = 1;\r
+\r
+  do {\r
+    //\r
+    // Advance to the null-terminator or to the first width directive\r
+    //\r
+    for (;(String[Index] != NARROW_CHAR) && (String[Index] != WIDE_CHAR) && (String[Index] != 0);\r
+         Index++, Count = Count + IncrementValue) {\r
+      if (LimitLen && Count > MaxWidth) {\r
+        break;\r
+      }\r
+    }\r
+\r
+    //\r
+    // We hit the null-terminator, we now have a count\r
+    //\r
+    if (String[Index] == 0) {\r
+      break;\r
+    }\r
+\r
+    if (LimitLen && Count > MaxWidth) {\r
+      *Offset = Index - 1;\r
+      break;\r
+    }\r
+\r
+    //\r
+    // We encountered a narrow directive - strip it from the size calculation since it doesn't get printed\r
+    // and also set the flag that determines what we increment by.(if narrow, increment by 1, if wide increment by 2)\r
+    //\r
+    if (String[Index] == NARROW_CHAR) {\r
+      //\r
+      // Skip to the next character\r
+      //\r
+      Index++;\r
+      IncrementValue = 1;\r
+    } else {\r
+      //\r
+      // Skip to the next character\r
+      //\r
+      Index++;\r
+      IncrementValue = 2;\r
+    }\r
+  } while (String[Index] != 0);\r
+\r
+  //\r
+  // Increment by one to include the null-terminator in the size\r
+  //\r
+  if (!LimitLen) {\r
+    Count++;\r
+  }\r
+\r
+  return Count * sizeof (CHAR16);\r
+}\r
+\r
 /**\r
   Draws a dialog box to the console output device specified by \r
   ConOut defined in the EFI_SYSTEM_TABLE and waits for a keystroke\r
@@ -336,7 +428,7 @@ CreatePopUp (
   MaxLength = 0;\r
   NumberOfLines = 0;\r
   while ((String = VA_ARG (Args, CHAR16 *)) != NULL) {\r
-    MaxLength = MAX (MaxLength, StrLen (String));\r
+    MaxLength = MAX (MaxLength, UefiLibGetStringWidth (String, FALSE, 0, NULL) / 2);\r
     NumberOfLines++;\r
   }\r
   VA_END (Args);\r
@@ -409,24 +501,29 @@ CreatePopUp (
   //\r
   VA_START (Args, Key);\r
   while ((String = VA_ARG (Args, CHAR16 *)) != NULL && NumberOfLines > 0) {\r
-    Length = StrLen (String);\r
     SetMem16 (Line, (MaxLength + 2) * 2, L' ');\r
+    Line[0]             = BOXDRAW_VERTICAL;\r
+    Line[MaxLength + 1] = BOXDRAW_VERTICAL;\r
+    Line[MaxLength + 2] = L'\0';\r
+    ConOut->SetCursorPosition (ConOut, Column, Row);\r
+    ConOut->OutputString (ConOut, Line);\r
+    Length = UefiLibGetStringWidth (String, FALSE, 0, NULL) / 2;\r
     if (Length <= MaxLength) {\r
       //\r
       // Length <= MaxLength\r
       //\r
-      CopyMem (Line + 1 + (MaxLength - Length) / 2, String , Length * sizeof (CHAR16));\r
+      ConOut->SetCursorPosition (ConOut, Column + 1 + (MaxLength - Length) / 2, Row++);\r
+      ConOut->OutputString (ConOut, String);\r
     } else {\r
       //\r
       // Length > MaxLength\r
       //\r
-      CopyMem (Line + 1, String + (Length - MaxLength) / 2 , MaxLength * sizeof (CHAR16));\r
+      UefiLibGetStringWidth (String, TRUE, MaxLength, &Length);\r
+      String[Length] = L'\0';\r
+\r
+      ConOut->SetCursorPosition (ConOut, Column + 1, Row++);\r
+      ConOut->OutputString (ConOut, String);\r
     }\r
-    Line[0]             = BOXDRAW_VERTICAL;\r
-    Line[MaxLength + 1] = BOXDRAW_VERTICAL;\r
-    Line[MaxLength + 2] = L'\0';\r
-    ConOut->SetCursorPosition (ConOut, Column, Row++);\r
-    ConOut->OutputString (ConOut, Line);\r
     NumberOfLines--;\r
   }\r
   VA_END (Args);\r