1 /** @file
2   This module provide help function for displaying unicode string.
3 
4   Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 
10 
11 
12 #include "UefiLibInternal.h"
13 
14 typedef struct {
15   CHAR16  WChar;
16   UINT32  Width;
17 } UNICODE_WIDTH_ENTRY;
18 
19 #define NARROW_CHAR         0xFFF0
20 #define WIDE_CHAR           0xFFF1
21 
22 GLOBAL_REMOVE_IF_UNREFERENCED CONST UNICODE_WIDTH_ENTRY mUnicodeWidthTable[] = {
23   //
24   // General script area
25   //
26   {(CHAR16)0x1FFF,  1},
27   /*
28    * Merge the blocks and replace them with the above entry as they fall to
29    * the same category and they are all narrow glyph. This will reduce search
30    * time and table size. The merge will omit the reserved code.
31    *
32    * Remove the above item if below is un-commented.
33    *
34   {(CHAR16)0x007F,  1},       // C0 controls and basic Latin. 0x0000-0x007F
35   {(CHAR16)0x00FF,  1},       // C1 controls and Latin-1 support. 0x0080-0x00FF
36   {(CHAR16)0x017F,  1},       // Latin extended-A. 0x0100-0x017F
37   {(CHAR16)0x024F,  1},       // Latin extended-B. 0x0180-0x024F
38   {(CHAR16)0x02AF,  1},       // IPA extensions. 0x0250-0x02AF
39   {(CHAR16)0x02FF,  1},       // Spacing modifier letters. 0x02B0-0x02FF
40   {(CHAR16)0x036F,  1},       // Combining diacritical marks. 0x0300-0x036F
41   {(CHAR16)0x03FF,  1},       // Greek. 0x0370-0x03FF
42   {(CHAR16)0x04FF,  1},       // Cyrillic. 0x0400-0x04FF
43   {(CHAR16)0x052F,  0},       // Unassigned. As Armenian in ver3.0. 0x0500-0x052F
44   {(CHAR16)0x058F,  1},       // Armenian. 0x0530-0x058F
45   {(CHAR16)0x05FF,  1},       // Hebrew. 0x0590-0x05FF
46   {(CHAR16)0x06FF,  1},       // Arabic. 0x0600-0x06FF
47   {(CHAR16)0x08FF,  0},       // Unassigned. 0x0700-0x08FF
48   {(CHAR16)0x097F,  1},       // Devanagari. 0x0900-0x097F
49   {(CHAR16)0x09FF,  1},       // Bengali. 0x0980-0x09FF
50   {(CHAR16)0x0A7F,  1},       // Gurmukhi. 0x0A00-0x0A7F
51   {(CHAR16)0x0AFF,  1},       // Gujarati. 0x0A80-0x0AFF
52   {(CHAR16)0x0B7F,  1},       // Oriya. 0x0B00-0x0B7F
53   {(CHAR16)0x0BFF,  1},       // Tamil. (See page 7-92). 0x0B80-0x0BFF
54   {(CHAR16)0x0C7F,  1},       // Telugu. 0x0C00-0x0C7F
55   {(CHAR16)0x0CFF,  1},       // Kannada. (See page 7-100). 0x0C80-0x0CFF
56   {(CHAR16)0x0D7F,  1},       // Malayalam (See page 7-104). 0x0D00-0x0D7F
57   {(CHAR16)0x0DFF,  0},       // Unassigned. 0x0D80-0x0DFF
58   {(CHAR16)0x0E7F,  1},       // Thai. 0x0E00-0x0E7F
59   {(CHAR16)0x0EFF,  1},       // Lao. 0x0E80-0x0EFF
60   {(CHAR16)0x0FBF,  1},       // Tibetan. 0x0F00-0x0FBF
61   {(CHAR16)0x109F,  0},       // Unassigned. 0x0FC0-0x109F
62   {(CHAR16)0x10FF,  1},       // Georgian. 0x10A0-0x10FF
63   {(CHAR16)0x11FF,  1},       // Hangul Jamo. 0x1100-0x11FF
64   {(CHAR16)0x1DFF,  0},       // Unassigned. 0x1200-0x1DFF
65   {(CHAR16)0x1EFF,  1},       // Latin extended additional. 0x1E00-0x1EFF
66   {(CHAR16)0x1FFF,  1},       // Greek extended. 0x1F00-0x1FFF
67   *
68   */
69 
70   //
71   // Symbol area
72   //
73   {(CHAR16)0x2FFF,  1},
74   /*
75    * Merge the blocks and replace them with the above entry as they fall to
76    * the same category and they are all narrow glyph. This will reduce search
77    * time and table size. The merge will omit the reserved code.
78    *
79    * Remove the above item if below is un-commented.
80    *
81   {(CHAR16)0x206F,  1},       // General punctuation. (See page7-154). 0x200-0x206F
82   {(CHAR16)0x209F,  1},       // Superscripts and subscripts. 0x2070-0x209F
83   {(CHAR16)0x20CF,  1},       // Currency symbols. 0x20A0-0x20CF
84   {(CHAR16)0x20FF,  1},       // Combining diacritical marks for symbols. 0x20D0-0x20FF
85   {(CHAR16)0x214F,  1},       // Letterlike sympbols. 0x2100-0x214F
86   {(CHAR16)0x218F,  1},       // Number forms. 0x2150-0x218F
87   {(CHAR16)0x21FF,  1},       // Arrows. 0x2190-0x21FF
88   {(CHAR16)0x22FF,  1},       // Mathematical operators. 0x2200-0x22FF
89   {(CHAR16)0x23FF,  1},       // Miscellaneous technical. 0x2300-0x23FF
90   {(CHAR16)0x243F,  1},       // Control pictures. 0x2400-0x243F
91   {(CHAR16)0x245F,  1},       // Optical character recognition. 0x2440-0x245F
92   {(CHAR16)0x24FF,  1},       // Enclosed alphanumerics. 0x2460-0x24FF
93   {(CHAR16)0x257F,  1},       // Box drawing. 0x2500-0x257F
94   {(CHAR16)0x259F,  1},       // Block elements. 0x2580-0x259F
95   {(CHAR16)0x25FF,  1},       // Geometric shapes. 0x25A0-0x25FF
96   {(CHAR16)0x26FF,  1},       // Miscellaneous symbols. 0x2600-0x26FF
97   {(CHAR16)0x27BF,  1},       // Dingbats. 0x2700-0x27BF
98   {(CHAR16)0x2FFF,  0},       // Reserved. 0x27C0-0x2FFF
99   *
100   */
101 
102   //
103   // CJK phonetics and symbol area
104   //
105   {(CHAR16)0x33FF,  2},
106   /*
107    * Merge the blocks and replace them with the above entry as they fall to
108    * the same category and they are all wide glyph. This will reduce search
109    * time and table size. The merge will omit the reserved code.
110    *
111    * Remove the above item if below is un-commented.
112    *
113   {(CHAR16)0x303F,  2},       // CJK symbols and punctuation. 0x3000-0x303F
114   {(CHAR16)0x309F,  2},       // Hiragana. 0x3040-0x309F
115   {(CHAR16)0x30FF,  2},       // Katakana. 0x30A0-0x30FF
116   {(CHAR16)0x312F,  2},       // Bopomofo. 0x3100-0x312F
117   {(CHAR16)0x318F,  2},       // Hangul compatibility jamo. 0x3130-0x318F
118   {(CHAR16)0x319F,  2},       // Kanbun. 0x3190-0x319F
119   {(CHAR16)0x31FF,  0},       // Reserved. As Bopomofo extended in ver3.0. 0x31A0-0x31FF
120   {(CHAR16)0x32FF,  2},       // Enclosed CJK letters and months. 0x3200-0x32FF
121   {(CHAR16)0x33FF,  2},       // CJK compatibility. 0x3300-0x33FF
122   *
123   */
124 
125   //
126   // CJK ideograph area
127   //
128   {(CHAR16)0x9FFF,  2},
129   /*
130    * Merge the blocks and replace them with the above entry as they fall to
131    * the same category and they are all wide glyph. This will reduce search
132    * time and table size. The merge will omit the reserved code.
133    *
134    * Remove the above item if below is un-commented.
135    *
136   {(CHAR16)0x4DFF,  0},       // Reserved. 0x3400-0x4DBF as CJK unified ideographs
137                       // extension A in ver3.0. 0x3400-0x4DFF
138   {(CHAR16)0x9FFF,  2},       // CJK unified ideographs. 0x4E00-0x9FFF
139   *
140   */
141 
142   //
143   // Reserved
144   //
145   {(CHAR16)0xABFF,  0},       // Reserved. 0xA000-0xA490 as Yi syllables. 0xA490-0xA4D0
146   // as Yi radicals in ver3.0. 0xA000-0xABFF
147   //
148   // Hangul syllables
149   //
150   {(CHAR16)0xD7FF,  2},
151   /*
152    * Merge the blocks and replace them with the above entry as they fall to
153    * the same category and they are all wide glyph. This will reduce search
154    * time and table size. The merge will omit the reserved code.
155    *
156    * Remove the above item if below is un-commented.
157    *
158   {(CHAR16)0xD7A3,  2},       // Hangul syllables. 0xAC00-0xD7A3
159   {(CHAR16)0xD7FF,  0},       // Reserved. 0xD7A3-0xD7FF
160   *
161   */
162 
163   //
164   // Surrogates area
165   //
166   {(CHAR16)0xDFFF,  0},       // Surrogates, not used now. 0xD800-0xDFFF
167 
168   //
169   // Private use area
170   //
171   {(CHAR16)0xF8FF,  0},       // Private use area. 0xE000-0xF8FF
172 
173   //
174   // Compatibility area and specials
175   //
176   {(CHAR16)0xFAFF,  2},       // CJK compatibility ideographs. 0xF900-0xFAFF
177   {(CHAR16)0xFB4F,  1},       // Alphabetic presentation forms. 0xFB00-0xFB4F
178   {(CHAR16)0xFDFF,  1},       // Arabic presentation forms-A. 0xFB50-0xFDFF
179   {(CHAR16)0xFE1F,  0},       // Reserved. As variation selectors in ver3.0. 0xFE00-0xFE1F
180   {(CHAR16)0xFE2F,  1},       // Combining half marks. 0xFE20-0xFE2F
181   {(CHAR16)0xFE4F,  2},       // CJK compatibility forms. 0xFE30-0xFE4F
182   {(CHAR16)0xFE6F,  1},       // Small Form Variants. 0xFE50-0xFE6F
183   {(CHAR16)0xFEFF,  1},       // Arabic presentation forms-B. 0xFE70-0xFEFF
184   {(CHAR16)0xFFEF,  1},       // Half width and full width forms. 0xFF00-0xFFEF
185   {(CHAR16)0xFFFF,  0},       // Speicials. 0xFFF0-0xFFFF
186 };
187 
188 /**
189   Retrieves the width of a Unicode character.
190 
191   This function computes and returns the width of the Unicode character specified
192   by UnicodeChar.
193 
194   @param  UnicodeChar   A Unicode character.
195 
196   @retval 0             The width if UnicodeChar could not be determined.
197   @retval 1             UnicodeChar is a narrow glyph.
198   @retval 2             UnicodeChar is a wide glyph.
199 
200 **/
201 UINTN
202 EFIAPI
GetGlyphWidth(IN CHAR16 UnicodeChar)203 GetGlyphWidth (
204   IN CHAR16  UnicodeChar
205   )
206 {
207   UINTN                     Index;
208   UINTN                     Low;
209   UINTN                     High;
210   CONST UNICODE_WIDTH_ENTRY *Item;
211 
212   Item  = NULL;
213   Low   = 0;
214   High  = (sizeof (mUnicodeWidthTable)) / (sizeof (UNICODE_WIDTH_ENTRY)) - 1;
215   while (Low <= High) {
216     Index = (Low + High) >> 1;
217     Item  = &(mUnicodeWidthTable[Index]);
218     if (Index == 0) {
219       if (UnicodeChar <= Item->WChar) {
220         break;
221       }
222 
223       return 0;
224     }
225 
226     if (UnicodeChar > Item->WChar) {
227       Low = Index + 1;
228     } else if (UnicodeChar <= mUnicodeWidthTable[Index - 1].WChar) {
229       High = Index - 1;
230     } else {
231       //
232       // Index - 1 < UnicodeChar <= Index. Found
233       //
234       break;
235     }
236   }
237 
238   if (Low <= High) {
239     return Item->Width;
240   }
241 
242   return 0;
243 }
244 
245 /**
246   Computes the display length of a Null-terminated Unicode String.
247 
248   This function computes and returns the display length of the Null-terminated
249   Unicode string specified by String.  If String is NULL then 0 is returned. If
250   any of the widths of the Unicode characters in String can not be determined,
251   then 0 is returned. The display width of String can be computed by summing the
252   display widths of each Unicode character in String.  Unicode characters that
253   are narrow glyphs have a width of 1, and Unicode characters that are width glyphs
254   have a width of 2.  If String is not aligned on a 16-bit boundary, then ASSERT().
255 
256   @param  String      A pointer to a Null-terminated Unicode string.
257 
258   @return The display length of the Null-terminated Unicode string specified by String.
259 
260 **/
261 UINTN
262 EFIAPI
UnicodeStringDisplayLength(IN CONST CHAR16 * String)263 UnicodeStringDisplayLength (
264   IN CONST CHAR16  *String
265   )
266 {
267   UINTN      Length;
268   UINTN      Width;
269 
270   if (String == NULL) {
271     return 0;
272   }
273 
274   Length = 0;
275   while (*String != 0) {
276     Width = GetGlyphWidth (*String);
277     if (Width == 0) {
278       return 0;
279     }
280 
281     Length += Width;
282     String++;
283   }
284 
285   return Length;
286 }
287 
288 /**
289   Count the storage space of a Unicode string.
290 
291   This function handles the Unicode string with NARROW_CHAR
292   and WIDE_CHAR control characters. NARROW_HCAR and WIDE_CHAR
293   does not count in the resultant output. If a WIDE_CHAR is
294   hit, then 2 Unicode character will consume an output storage
295   space with size of CHAR16 till a NARROW_CHAR is hit.
296 
297   @param String          The input string to be counted.
298   @param LimitLen        Whether need to limit the string length.
299   @param MaxWidth        The max length this function supported.
300   @param Offset          The max index of the string can be show out.
301 
302   @return Storage space for the input string.
303 
304 **/
305 UINTN
UefiLibGetStringWidth(IN CHAR16 * String,IN BOOLEAN LimitLen,IN UINTN MaxWidth,OUT UINTN * Offset)306 UefiLibGetStringWidth (
307   IN  CHAR16               *String,
308   IN  BOOLEAN              LimitLen,
309   IN  UINTN                MaxWidth,
310   OUT UINTN                *Offset
311   )
312 {
313   UINTN Index;
314   UINTN Count;
315   UINTN IncrementValue;
316 
317   if (String == NULL) {
318     return 0;
319   }
320 
321   Index           = 0;
322   Count           = 0;
323   IncrementValue  = 1;
324 
325   do {
326     //
327     // Advance to the null-terminator or to the first width directive
328     //
329     for (;(String[Index] != NARROW_CHAR) && (String[Index] != WIDE_CHAR) && (String[Index] != 0); Index++) {
330       Count = Count + IncrementValue;
331 
332       if (LimitLen && Count > MaxWidth) {
333         break;
334       }
335     }
336 
337     //
338     // We hit the null-terminator, we now have a count
339     //
340     if (String[Index] == 0) {
341       break;
342     }
343 
344     if (LimitLen && Count > MaxWidth) {
345       *Offset = Index;
346       break;
347     }
348 
349     //
350     // We encountered a narrow directive - strip it from the size calculation since it doesn't get printed
351     // and also set the flag that determines what we increment by.(if narrow, increment by 1, if wide increment by 2)
352     //
353     if (String[Index] == NARROW_CHAR) {
354       //
355       // Skip to the next character
356       //
357       Index++;
358       IncrementValue = 1;
359     } else {
360       //
361       // Skip to the next character
362       //
363       Index++;
364       IncrementValue = 2;
365     }
366   } while (String[Index] != 0);
367 
368   return Count * sizeof (CHAR16);
369 }
370 
371 /**
372   Draws a dialog box to the console output device specified by
373   ConOut defined in the EFI_SYSTEM_TABLE and waits for a keystroke
374   from the console input device specified by ConIn defined in the
375   EFI_SYSTEM_TABLE.
376 
377   If there are no strings in the variable argument list, then ASSERT().
378   If all the strings in the variable argument list are empty, then ASSERT().
379 
380   @param[in]   Attribute  Specifies the foreground and background color of the popup.
381   @param[out]  Key        A pointer to the EFI_KEY value of the key that was
382                           pressed.  This is an optional parameter that may be NULL.
383                           If it is NULL then no wait for a keypress will be performed.
384   @param[in]  ...         The variable argument list that contains pointers to Null-
385                           terminated Unicode strings to display in the dialog box.
386                           The variable argument list is terminated by a NULL.
387 
388 **/
389 VOID
390 EFIAPI
CreatePopUp(IN UINTN Attribute,OUT EFI_INPUT_KEY * Key,OPTIONAL...)391 CreatePopUp (
392   IN  UINTN          Attribute,
393   OUT EFI_INPUT_KEY  *Key,      OPTIONAL
394   ...
395   )
396 {
397   EFI_STATUS                       Status;
398   VA_LIST                          Args;
399   EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *ConOut;
400   EFI_SIMPLE_TEXT_OUTPUT_MODE      SavedConsoleMode;
401   UINTN                            Columns;
402   UINTN                            Rows;
403   UINTN                            Column;
404   UINTN                            Row;
405   UINTN                            NumberOfLines;
406   UINTN                            MaxLength;
407   CHAR16                           *String;
408   UINTN                            Length;
409   CHAR16                           *Line;
410   UINTN                            EventIndex;
411   CHAR16                           *TmpString;
412 
413   //
414   // Determine the length of the longest line in the popup and the the total
415   // number of lines in the popup
416   //
417   VA_START (Args, Key);
418   MaxLength = 0;
419   NumberOfLines = 0;
420   while ((String = VA_ARG (Args, CHAR16 *)) != NULL) {
421     MaxLength = MAX (MaxLength, UefiLibGetStringWidth (String, FALSE, 0, NULL) / 2);
422     NumberOfLines++;
423   }
424   VA_END (Args);
425 
426   //
427   // If the total number of lines in the popup is zero, then ASSERT()
428   //
429   ASSERT (NumberOfLines != 0);
430 
431   //
432   // If the maximum length of all the strings is zero, then ASSERT()
433   //
434   ASSERT (MaxLength != 0);
435 
436   //
437   // Cache a pointer to the Simple Text Output Protocol in the EFI System Table
438   //
439   ConOut = gST->ConOut;
440 
441   //
442   // Save the current console cursor position and attributes
443   //
444   CopyMem (&SavedConsoleMode, ConOut->Mode, sizeof (SavedConsoleMode));
445 
446   //
447   // Retrieve the number of columns and rows in the current console mode
448   //
449   ConOut->QueryMode (ConOut, SavedConsoleMode.Mode, &Columns, &Rows);
450 
451   //
452   // Disable cursor and set the foreground and background colors specified by Attribute
453   //
454   ConOut->EnableCursor (ConOut, FALSE);
455   ConOut->SetAttribute (ConOut, Attribute);
456 
457   //
458   // Limit NumberOfLines to height of the screen minus 3 rows for the box itself
459   //
460   NumberOfLines = MIN (NumberOfLines, Rows - 3);
461 
462   //
463   // Limit MaxLength to width of the screen minus 2 columns for the box itself
464   //
465   MaxLength = MIN (MaxLength, Columns - 2);
466 
467   //
468   // Compute the starting row and starting column for the popup
469   //
470   Row    = (Rows - (NumberOfLines + 3)) / 2;
471   Column = (Columns - (MaxLength + 2)) / 2;
472 
473   //
474   // Allocate a buffer for a single line of the popup with borders and a Null-terminator
475   //
476   Line = AllocateZeroPool ((MaxLength + 3) * sizeof (CHAR16));
477   ASSERT (Line != NULL);
478 
479   //
480   // Draw top of popup box
481   //
482   SetMem16 (Line, (MaxLength + 2) * 2, BOXDRAW_HORIZONTAL);
483   Line[0]             = BOXDRAW_DOWN_RIGHT;
484   Line[MaxLength + 1] = BOXDRAW_DOWN_LEFT;
485   Line[MaxLength + 2] = L'\0';
486   ConOut->SetCursorPosition (ConOut, Column, Row++);
487   ConOut->OutputString (ConOut, Line);
488 
489   //
490   // Draw middle of the popup with strings
491   //
492   VA_START (Args, Key);
493   while ((String = VA_ARG (Args, CHAR16 *)) != NULL && NumberOfLines > 0) {
494     SetMem16 (Line, (MaxLength + 2) * 2, L' ');
495     Line[0]             = BOXDRAW_VERTICAL;
496     Line[MaxLength + 1] = BOXDRAW_VERTICAL;
497     Line[MaxLength + 2] = L'\0';
498     ConOut->SetCursorPosition (ConOut, Column, Row);
499     ConOut->OutputString (ConOut, Line);
500     Length = UefiLibGetStringWidth (String, FALSE, 0, NULL) / 2;
501     if (Length <= MaxLength) {
502       //
503       // Length <= MaxLength
504       //
505       ConOut->SetCursorPosition (ConOut, Column + 1 + (MaxLength - Length) / 2, Row++);
506       ConOut->OutputString (ConOut, String);
507     } else {
508       //
509       // Length > MaxLength
510       //
511       UefiLibGetStringWidth (String, TRUE, MaxLength, &Length);
512       TmpString = AllocateZeroPool ((Length + 1) * sizeof (CHAR16));
513       ASSERT (TmpString != NULL);
514       StrnCpyS (TmpString, Length + 1, String, Length - 3);
515       StrCatS (TmpString, Length + 1, L"...");
516 
517       ConOut->SetCursorPosition (ConOut, Column + 1, Row++);
518       ConOut->OutputString (ConOut, TmpString);
519       FreePool (TmpString);
520     }
521     NumberOfLines--;
522   }
523   VA_END (Args);
524 
525   //
526   // Draw bottom of popup box
527   //
528   SetMem16 (Line, (MaxLength + 2) * 2, BOXDRAW_HORIZONTAL);
529   Line[0]             = BOXDRAW_UP_RIGHT;
530   Line[MaxLength + 1] = BOXDRAW_UP_LEFT;
531   Line[MaxLength + 2] = L'\0';
532   ConOut->SetCursorPosition (ConOut, Column, Row++);
533   ConOut->OutputString (ConOut, Line);
534 
535   //
536   // Free the allocated line buffer
537   //
538   FreePool (Line);
539 
540   //
541   // Restore the cursor visibility, position, and attributes
542   //
543   ConOut->EnableCursor      (ConOut, SavedConsoleMode.CursorVisible);
544   ConOut->SetCursorPosition (ConOut, SavedConsoleMode.CursorColumn, SavedConsoleMode.CursorRow);
545   ConOut->SetAttribute      (ConOut, SavedConsoleMode.Attribute);
546 
547   //
548   // Wait for a keystroke
549   //
550   if (Key != NULL) {
551     while (TRUE) {
552       Status = gST->ConIn->ReadKeyStroke (gST->ConIn, Key);
553       if (!EFI_ERROR (Status)) {
554         break;
555       }
556 
557       //
558       // If we encounter error, continue to read another key in.
559       //
560       if (Status != EFI_NOT_READY) {
561         continue;
562       }
563       gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
564     }
565   }
566 }
567