1 /*++
2 
3 Copyright (c) 2004 - 2006, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution.  The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8 
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 
12 
13 Module Name:
14 
15   Console.c
16 
17 Abstract:
18 
19   UEFI library functions.
20 
21 --*/
22 
23 #include "EdkIIGlueUefi.h"
24 
25 typedef struct {
26   CHAR16  WChar;
27   UINT32  Width;
28 } UNICODE_WIDTH_ENTRY;
29 
30 UNICODE_WIDTH_ENTRY mUnicodeWidthTable[] = {
31   //
32   // General script area
33   //
34   {(CHAR16)0x1FFF,  1},
35   /*
36    * Merge the blocks and replace them with the above entry as they fall to
37    * the same category and they are all narrow glyph. This will reduce search
38    * time and table size. The merge will omit the reserved code.
39    *
40    * Remove the above item if below is un-commented.
41    *
42   {(CHAR16)0x007F,  1},       // C0 controls and basic Latin. 0x0000-0x007F
43   {(CHAR16)0x00FF,  1},       // C1 controls and Latin-1 support. 0x0080-0x00FF
44   {(CHAR16)0x017F,  1},       // Latin extended-A. 0x0100-0x017F
45   {(CHAR16)0x024F,  1},       // Latin extended-B. 0x0180-0x024F
46   {(CHAR16)0x02AF,  1},       // IPA extensions. 0x0250-0x02AF
47   {(CHAR16)0x02FF,  1},       // Spacing modifier letters. 0x02B0-0x02FF
48   {(CHAR16)0x036F,  1},       // Combining diacritical marks. 0x0300-0x036F
49   {(CHAR16)0x03FF,  1},       // Greek. 0x0370-0x03FF
50   {(CHAR16)0x04FF,  1},       // Cyrillic. 0x0400-0x04FF
51   {(CHAR16)0x052F,  0},       // Unassigned. As Armenian in ver3.0. 0x0500-0x052F
52   {(CHAR16)0x058F,  1},       // Armenian. 0x0530-0x058F
53   {(CHAR16)0x05FF,  1},       // Hebrew. 0x0590-0x05FF
54   {(CHAR16)0x06FF,  1},       // Arabic. 0x0600-0x06FF
55   {(CHAR16)0x08FF,  0},       // Unassigned. 0x0700-0x08FF
56   {(CHAR16)0x097F,  1},       // Devanagari. 0x0900-0x097F
57   {(CHAR16)0x09FF,  1},       // Bengali. 0x0980-0x09FF
58   {(CHAR16)0x0A7F,  1},       // Gurmukhi. 0x0A00-0x0A7F
59   {(CHAR16)0x0AFF,  1},       // Gujarati. 0x0A80-0x0AFF
60   {(CHAR16)0x0B7F,  1},       // Oriya. 0x0B00-0x0B7F
61   {(CHAR16)0x0BFF,  1},       // Tamil. (See page 7-92). 0x0B80-0x0BFF
62   {(CHAR16)0x0C7F,  1},       // Telugu. 0x0C00-0x0C7F
63   {(CHAR16)0x0CFF,  1},       // Kannada. (See page 7-100). 0x0C80-0x0CFF
64   {(CHAR16)0x0D7F,  1},       // Malayalam (See page 7-104). 0x0D00-0x0D7F
65   {(CHAR16)0x0DFF,  0},       // Unassigned. 0x0D80-0x0DFF
66   {(CHAR16)0x0E7F,  1},       // Thai. 0x0E00-0x0E7F
67   {(CHAR16)0x0EFF,  1},       // Lao. 0x0E80-0x0EFF
68   {(CHAR16)0x0FBF,  1},       // Tibetan. 0x0F00-0x0FBF
69   {(CHAR16)0x109F,  0},       // Unassigned. 0x0FC0-0x109F
70   {(CHAR16)0x10FF,  1},       // Georgian. 0x10A0-0x10FF
71   {(CHAR16)0x11FF,  1},       // Hangul Jamo. 0x1100-0x11FF
72   {(CHAR16)0x1DFF,  0},       // Unassigned. 0x1200-0x1DFF
73   {(CHAR16)0x1EFF,  1},       // Latin extended additional. 0x1E00-0x1EFF
74   {(CHAR16)0x1FFF,  1},       // Greek extended. 0x1F00-0x1FFF
75   *
76   */
77 
78   //
79   // Symbol area
80   //
81   {(CHAR16)0x2FFF,  1},
82   /*
83    * Merge the blocks and replace them with the above entry as they fall to
84    * the same category and they are all narrow glyph. This will reduce search
85    * time and table size. The merge will omit the reserved code.
86    *
87    * Remove the above item if below is un-commented.
88    *
89   {(CHAR16)0x206F,  1},       // General punctuation. (See page7-154). 0x200-0x206F
90   {(CHAR16)0x209F,  1},       // Superscripts and subscripts. 0x2070-0x209F
91   {(CHAR16)0x20CF,  1},       // Currency symbols. 0x20A0-0x20CF
92   {(CHAR16)0x20FF,  1},       // Combining diacritical marks for symbols. 0x20D0-0x20FF
93   {(CHAR16)0x214F,  1},       // Letterlike sympbols. 0x2100-0x214F
94   {(CHAR16)0x218F,  1},       // Number forms. 0x2150-0x218F
95   {(CHAR16)0x21FF,  1},       // Arrows. 0x2190-0x21FF
96   {(CHAR16)0x22FF,  1},       // Mathematical operators. 0x2200-0x22FF
97   {(CHAR16)0x23FF,  1},       // Miscellaneous technical. 0x2300-0x23FF
98   {(CHAR16)0x243F,  1},       // Control pictures. 0x2400-0x243F
99   {(CHAR16)0x245F,  1},       // Optical character recognition. 0x2440-0x245F
100   {(CHAR16)0x24FF,  1},       // Enclosed alphanumerics. 0x2460-0x24FF
101   {(CHAR16)0x257F,  1},       // Box drawing. 0x2500-0x257F
102   {(CHAR16)0x259F,  1},       // Block elements. 0x2580-0x259F
103   {(CHAR16)0x25FF,  1},       // Geometric shapes. 0x25A0-0x25FF
104   {(CHAR16)0x26FF,  1},       // Miscellaneous symbols. 0x2600-0x26FF
105   {(CHAR16)0x27BF,  1},       // Dingbats. 0x2700-0x27BF
106   {(CHAR16)0x2FFF,  0},       // Reserved. 0x27C0-0x2FFF
107   *
108   */
109 
110   //
111   // CJK phonetics and symbol area
112   //
113   {(CHAR16)0x33FF,  2},
114   /*
115    * Merge the blocks and replace them with the above entry as they fall to
116    * the same category and they are all wide glyph. This will reduce search
117    * time and table size. The merge will omit the reserved code.
118    *
119    * Remove the above item if below is un-commented.
120    *
121   {(CHAR16)0x303F,  2},       // CJK symbols and punctuation. 0x3000-0x303F
122   {(CHAR16)0x309F,  2},       // Hiragana. 0x3040-0x309F
123   {(CHAR16)0x30FF,  2},       // Katakana. 0x30A0-0x30FF
124   {(CHAR16)0x312F,  2},       // Bopomofo. 0x3100-0x312F
125   {(CHAR16)0x318F,  2},       // Hangul compatibility jamo. 0x3130-0x318F
126   {(CHAR16)0x319F,  2},       // Kanbun. 0x3190-0x319F
127   {(CHAR16)0x31FF,  0},       // Reserved. As Bopomofo extended in ver3.0. 0x31A0-0x31FF
128   {(CHAR16)0x32FF,  2},       // Enclosed CJK letters and months. 0x3200-0x32FF
129   {(CHAR16)0x33FF,  2},       // CJK compatibility. 0x3300-0x33FF
130   *
131   */
132 
133   //
134   // CJK ideograph area
135   //
136   {(CHAR16)0x9FFF,  2},
137   /*
138    * Merge the blocks and replace them with the above entry as they fall to
139    * the same category and they are all wide glyph. This will reduce search
140    * time and table size. The merge will omit the reserved code.
141    *
142    * Remove the above item if below is un-commented.
143    *
144   {(CHAR16)0x4DFF,  0},       // Reserved. 0x3400-0x4DBF as CJK unified ideographs
145                       // extension A in ver3.0. 0x3400-0x4DFF
146   {(CHAR16)0x9FFF,  2},       // CJK unified ideographs. 0x4E00-0x9FFF
147   *
148   */
149 
150   //
151   // Reserved
152   //
153   {(CHAR16)0xABFF,  0},       // Reserved. 0xA000-0xA490 as Yi syllables. 0xA490-0xA4D0
154   // as Yi radicals in ver3.0. 0xA000-0xABFF
155   //
156   // Hangul syllables
157   //
158   {(CHAR16)0xD7FF,  2},
159   /*
160    * Merge the blocks and replace them with the above entry as they fall to
161    * the same category and they are all wide glyph. This will reduce search
162    * time and table size. The merge will omit the reserved code.
163    *
164    * Remove the above item if below is un-commented.
165    *
166   {(CHAR16)0xD7A3,  2},       // Hangul syllables. 0xAC00-0xD7A3
167   {(CHAR16)0xD7FF,  0},       // Reserved. 0xD7A3-0xD7FF
168   *
169   */
170 
171   //
172   // Surrogates area
173   //
174   {(CHAR16)0xDFFF,  0},       // Surrogates, not used now. 0xD800-0xDFFF
175 
176   //
177   // Private use area
178   //
179   {(CHAR16)0xF8FF,  0},       // Private use area. 0xE000-0xF8FF
180 
181   //
182   // Compatibility area and specials
183   //
184   {(CHAR16)0xFAFF,  2},       // CJK compatibility ideographs. 0xF900-0xFAFF
185   {(CHAR16)0xFB4F,  1},       // Alphabetic presentation forms. 0xFB00-0xFB4F
186   {(CHAR16)0xFDFF,  1},       // Arabic presentation forms-A. 0xFB50-0xFDFF
187   {(CHAR16)0xFE1F,  0},       // Reserved. As variation selectors in ver3.0. 0xFE00-0xFE1F
188   {(CHAR16)0xFE2F,  1},       // Combining half marks. 0xFE20-0xFE2F
189   {(CHAR16)0xFE4F,  2},       // CJK compatibility forms. 0xFE30-0xFE4F
190   {(CHAR16)0xFE6F,  1},       // Small Form Variants. 0xFE50-0xFE6F
191   {(CHAR16)0xFEFF,  1},       // Arabic presentation forms-B. 0xFE70-0xFEFF
192   {(CHAR16)0xFFEF,  1},       // Half width and full width forms. 0xFF00-0xFFEF
193   {(CHAR16)0xFFFF,  0},       // Speicials. 0xFFF0-0xFFFF
194 };
195 
196 /**
197   This function computes and returns the width of the Unicode character
198   specified by UnicodeChar.
199 
200   @param  UnicodeChar   A Unicode character.
201 
202   @retval 0             The width if UnicodeChar could not be determined.
203   @retval 1             UnicodeChar is a narrow glyph.
204   @retval 2             UnicodeChar is a wide glyph.
205 
206 **/
207 UINTN
208 EFIAPI
GlueGetGlyphWidth(IN CHAR16 UnicodeChar)209 GlueGetGlyphWidth (
210   IN CHAR16  UnicodeChar
211   )
212 {
213   UINTN               Index;
214   UINTN               Low;
215   UINTN               High;
216   UNICODE_WIDTH_ENTRY *Item;
217 
218   Item  = NULL;
219   Low   = 0;
220   High  = (sizeof (mUnicodeWidthTable)) / (sizeof (UNICODE_WIDTH_ENTRY)) - 1;
221   while (Low <= High) {
222     Index = (Low + High) >> 1;
223     Item  = &(mUnicodeWidthTable[Index]);
224     if (Index == 0) {
225       if (UnicodeChar <= Item->WChar) {
226         break;
227       }
228 
229       return 0;
230     }
231 
232     if (UnicodeChar > Item->WChar) {
233       Low = Index + 1;
234     } else if (UnicodeChar <= mUnicodeWidthTable[Index - 1].WChar) {
235       High = Index - 1;
236     } else {
237       //
238       // Index - 1 < UnicodeChar <= Index. Found
239       //
240       break;
241     }
242   }
243 
244   if (Low <= High) {
245     return Item->Width;
246   }
247 
248   return 0;
249 }
250 
251 /**
252   This function computes and returns the display length of
253   the Null-terminated Unicode string specified by String.
254   If String is NULL, then 0 is returned.
255   If any of the widths of the Unicode characters in String
256   can not be determined, then 0 is returned.
257 
258   @param  String      A pointer to a Null-terminated Unicode string.
259 
260   @return The display length of the Null-terminated Unicode string specified by String.
261 
262 **/
263 UINTN
264 EFIAPI
UnicodeStringDisplayLength(IN CONST CHAR16 * String)265 UnicodeStringDisplayLength (
266   IN CONST CHAR16  *String
267   )
268 {
269   UINTN      Length;
270   UINTN      Width;
271 
272   if (String == NULL) {
273     return 0;
274   }
275 
276   Length = 0;
277   while (*String != 0) {
278     Width = GetGlyphWidth (*String);
279     if (Width == 0) {
280       return 0;
281     }
282 
283     Length += Width;
284     String++;
285   }
286 
287   return Length;
288 }
289