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