xref: /DragonStub/lib/boxdraw.c (revision 530d68ba191850edafc6da22cb2df55bec0c5fa5)
1*530d68baSNigel Croxon /*++
2*530d68baSNigel Croxon 
3*530d68baSNigel Croxon Copyright (c) 1998  Intel Corporation
4*530d68baSNigel Croxon 
5*530d68baSNigel Croxon Module Name:
6*530d68baSNigel Croxon 
7*530d68baSNigel Croxon     BoxDraw.c
8*530d68baSNigel Croxon 
9*530d68baSNigel Croxon Abstract:
10*530d68baSNigel Croxon     Lib functions to support Box Draw Unicode code pages.
11*530d68baSNigel Croxon 
12*530d68baSNigel Croxon 
13*530d68baSNigel Croxon 
14*530d68baSNigel Croxon Revision History
15*530d68baSNigel Croxon 
16*530d68baSNigel Croxon --*/
17*530d68baSNigel Croxon 
18*530d68baSNigel Croxon #include "lib.h"
19*530d68baSNigel Croxon 
20*530d68baSNigel Croxon typedef struct {
21*530d68baSNigel Croxon     CHAR16  Unicode;
22*530d68baSNigel Croxon     CHAR8   PcAnsi;
23*530d68baSNigel Croxon     CHAR8   Ascii;
24*530d68baSNigel Croxon } UNICODE_TO_CHAR;
25*530d68baSNigel Croxon 
26*530d68baSNigel Croxon 
27*530d68baSNigel Croxon //
28*530d68baSNigel Croxon // This list is used to define the valid extend chars.
29*530d68baSNigel Croxon // It also provides a mapping from Unicode to PCANSI or
30*530d68baSNigel Croxon // ASCII. The ASCII mapping we just made up.
31*530d68baSNigel Croxon //
32*530d68baSNigel Croxon //
33*530d68baSNigel Croxon 
34*530d68baSNigel Croxon STATIC UNICODE_TO_CHAR UnicodeToPcAnsiOrAscii[] = {
35*530d68baSNigel Croxon     { BOXDRAW_HORIZONTAL,                 0xc4, L'-'},
36*530d68baSNigel Croxon     { BOXDRAW_VERTICAL,                   0xb3, L'|'},
37*530d68baSNigel Croxon     { BOXDRAW_DOWN_RIGHT,                 0xda, L'/'},
38*530d68baSNigel Croxon     { BOXDRAW_DOWN_LEFT,                  0xbf, L'\\'},
39*530d68baSNigel Croxon     { BOXDRAW_UP_RIGHT,                   0xc0, L'\\'},
40*530d68baSNigel Croxon     { BOXDRAW_UP_LEFT,                    0xd9, L'/'},
41*530d68baSNigel Croxon     { BOXDRAW_VERTICAL_RIGHT,             0xc3, L'|'},
42*530d68baSNigel Croxon     { BOXDRAW_VERTICAL_LEFT,              0xb4, L'|'},
43*530d68baSNigel Croxon     { BOXDRAW_DOWN_HORIZONTAL,            0xc2, L'+'},
44*530d68baSNigel Croxon     { BOXDRAW_UP_HORIZONTAL,              0xc1, L'+'},
45*530d68baSNigel Croxon     { BOXDRAW_VERTICAL_HORIZONTAL,        0xc5, L'+'},
46*530d68baSNigel Croxon     { BOXDRAW_DOUBLE_HORIZONTAL,          0xcd, L'-'},
47*530d68baSNigel Croxon     { BOXDRAW_DOUBLE_VERTICAL,            0xba, L'|'},
48*530d68baSNigel Croxon     { BOXDRAW_DOWN_RIGHT_DOUBLE,          0xd5, L'/'},
49*530d68baSNigel Croxon     { BOXDRAW_DOWN_DOUBLE_RIGHT,          0xd6, L'/'},
50*530d68baSNigel Croxon     { BOXDRAW_DOUBLE_DOWN_RIGHT,          0xc9, L'/'},
51*530d68baSNigel Croxon     { BOXDRAW_DOWN_LEFT_DOUBLE,           0xb8, L'\\'},
52*530d68baSNigel Croxon     { BOXDRAW_DOWN_DOUBLE_LEFT,           0xb7, L'\\'},
53*530d68baSNigel Croxon     { BOXDRAW_DOUBLE_DOWN_LEFT,           0xbb, L'\\'},
54*530d68baSNigel Croxon     { BOXDRAW_UP_RIGHT_DOUBLE,            0xd4, L'\\'},
55*530d68baSNigel Croxon     { BOXDRAW_UP_DOUBLE_RIGHT,            0xd3, L'\\'},
56*530d68baSNigel Croxon     { BOXDRAW_DOUBLE_UP_RIGHT,            0xc8, L'\\'},
57*530d68baSNigel Croxon     { BOXDRAW_UP_LEFT_DOUBLE,             0xbe, L'/'},
58*530d68baSNigel Croxon     { BOXDRAW_UP_DOUBLE_LEFT,             0xbd, L'/'},
59*530d68baSNigel Croxon     { BOXDRAW_DOUBLE_UP_LEFT,             0xbc, L'/'},
60*530d68baSNigel Croxon     { BOXDRAW_VERTICAL_RIGHT_DOUBLE,      0xc6, L'|'},
61*530d68baSNigel Croxon     { BOXDRAW_VERTICAL_DOUBLE_RIGHT,      0xc7, L'|'},
62*530d68baSNigel Croxon     { BOXDRAW_DOUBLE_VERTICAL_RIGHT,      0xcc, L'|'},
63*530d68baSNigel Croxon     { BOXDRAW_VERTICAL_LEFT_DOUBLE,       0xb5, L'|'},
64*530d68baSNigel Croxon     { BOXDRAW_VERTICAL_DOUBLE_LEFT,       0xb6, L'|'},
65*530d68baSNigel Croxon     { BOXDRAW_DOUBLE_VERTICAL_LEFT,       0xb9, L'|'},
66*530d68baSNigel Croxon     { BOXDRAW_DOWN_HORIZONTAL_DOUBLE,     0xd1, L'+'},
67*530d68baSNigel Croxon     { BOXDRAW_DOWN_DOUBLE_HORIZONTAL,     0xd2, L'+'},
68*530d68baSNigel Croxon     { BOXDRAW_DOUBLE_DOWN_HORIZONTAL,     0xcb, L'+'},
69*530d68baSNigel Croxon     { BOXDRAW_UP_HORIZONTAL_DOUBLE,       0xcf, L'+'},
70*530d68baSNigel Croxon     { BOXDRAW_UP_DOUBLE_HORIZONTAL,       0xd0, L'+'},
71*530d68baSNigel Croxon     { BOXDRAW_DOUBLE_UP_HORIZONTAL,       0xca, L'+'},
72*530d68baSNigel Croxon     { BOXDRAW_VERTICAL_HORIZONTAL_DOUBLE, 0xd8, L'+'},
73*530d68baSNigel Croxon     { BOXDRAW_VERTICAL_DOUBLE_HORIZONTAL, 0xd7, L'+'},
74*530d68baSNigel Croxon     { BOXDRAW_DOUBLE_VERTICAL_HORIZONTAL, 0xce, L'+'},
75*530d68baSNigel Croxon 
76*530d68baSNigel Croxon     { BLOCKELEMENT_FULL_BLOCK,            0xdb, L'*'},
77*530d68baSNigel Croxon     { BLOCKELEMENT_LIGHT_SHADE,           0xb0, L'+'},
78*530d68baSNigel Croxon 
79*530d68baSNigel Croxon     { GEOMETRICSHAPE_UP_TRIANGLE,         0x1e, L'^'},
80*530d68baSNigel Croxon     { GEOMETRICSHAPE_RIGHT_TRIANGLE,      0x10, L'>'},
81*530d68baSNigel Croxon     { GEOMETRICSHAPE_DOWN_TRIANGLE,       0x1f, L'v'},
82*530d68baSNigel Croxon     { GEOMETRICSHAPE_LEFT_TRIANGLE,       0x11, L'<'},
83*530d68baSNigel Croxon 
84*530d68baSNigel Croxon     /* BugBug: Left Arrow is an ESC. We can not make it print
85*530d68baSNigel Croxon                 on a PCANSI terminal. If we can make left arrow
86*530d68baSNigel Croxon                 come out on PC ANSI we can add it back.
87*530d68baSNigel Croxon 
88*530d68baSNigel Croxon     { ARROW_LEFT,                         0x1b, L'<'},
89*530d68baSNigel Croxon     */
90*530d68baSNigel Croxon 
91*530d68baSNigel Croxon     { ARROW_UP,                           0x18, L'^'},
92*530d68baSNigel Croxon 
93*530d68baSNigel Croxon     /* BugBut: Took out left arrow so right has to go too.
94*530d68baSNigel Croxon        { ARROW_RIGHT,                        0x1a, L'>'},
95*530d68baSNigel Croxon     */
96*530d68baSNigel Croxon     { ARROW_DOWN,                         0x19, L'v'},
97*530d68baSNigel Croxon 
98*530d68baSNigel Croxon     { 0x0000, 0x00, L'\0' }
99*530d68baSNigel Croxon };
100*530d68baSNigel Croxon 
101*530d68baSNigel Croxon 
102*530d68baSNigel Croxon BOOLEAN
LibIsValidTextGraphics(IN CHAR16 Graphic,OUT CHAR8 * PcAnsi,OPTIONAL OUT CHAR8 * Ascii OPTIONAL)103*530d68baSNigel Croxon LibIsValidTextGraphics (
104*530d68baSNigel Croxon     IN  CHAR16  Graphic,
105*530d68baSNigel Croxon     OUT CHAR8   *PcAnsi,    OPTIONAL
106*530d68baSNigel Croxon     OUT CHAR8   *Ascii      OPTIONAL
107*530d68baSNigel Croxon     )
108*530d68baSNigel Croxon /*++
109*530d68baSNigel Croxon 
110*530d68baSNigel Croxon Routine Description:
111*530d68baSNigel Croxon 
112*530d68baSNigel Croxon     Detects if a Unicode char is for Box Drawing text graphics.
113*530d68baSNigel Croxon 
114*530d68baSNigel Croxon Arguments:
115*530d68baSNigel Croxon 
116*530d68baSNigel Croxon     Grphic  - Unicode char to test.
117*530d68baSNigel Croxon 
118*530d68baSNigel Croxon     PcAnsi  - Optional pointer to return PCANSI equivalent of Graphic.
119*530d68baSNigel Croxon 
120*530d68baSNigel Croxon     Asci    - Optional pointer to return Ascii equivalent of Graphic.
121*530d68baSNigel Croxon 
122*530d68baSNigel Croxon Returns:
123*530d68baSNigel Croxon 
124*530d68baSNigel Croxon     TRUE if Gpaphic is a supported Unicode Box Drawing character.
125*530d68baSNigel Croxon 
126*530d68baSNigel Croxon --*/{
127*530d68baSNigel Croxon     UNICODE_TO_CHAR     *Table;
128*530d68baSNigel Croxon 
129*530d68baSNigel Croxon     if ((((Graphic & 0xff00) != 0x2500) && ((Graphic & 0xff00) != 0x2100))) {
130*530d68baSNigel Croxon 
131*530d68baSNigel Croxon         //
132*530d68baSNigel Croxon         // Unicode drawing code charts are all in the 0x25xx range,
133*530d68baSNigel Croxon         //  arrows are 0x21xx
134*530d68baSNigel Croxon         //
135*530d68baSNigel Croxon         return FALSE;
136*530d68baSNigel Croxon     }
137*530d68baSNigel Croxon 
138*530d68baSNigel Croxon     for (Table = UnicodeToPcAnsiOrAscii; Table->Unicode != 0x0000; Table++) {
139*530d68baSNigel Croxon         if (Graphic == Table->Unicode) {
140*530d68baSNigel Croxon             if (PcAnsi) {
141*530d68baSNigel Croxon                 *PcAnsi = Table->PcAnsi;
142*530d68baSNigel Croxon             }
143*530d68baSNigel Croxon             if (Ascii) {
144*530d68baSNigel Croxon                 *Ascii = Table->Ascii;
145*530d68baSNigel Croxon             }
146*530d68baSNigel Croxon             return TRUE;
147*530d68baSNigel Croxon         }
148*530d68baSNigel Croxon     }
149*530d68baSNigel Croxon     return FALSE;
150*530d68baSNigel Croxon }
151*530d68baSNigel Croxon 
152*530d68baSNigel Croxon BOOLEAN
IsValidAscii(IN CHAR16 Ascii)153*530d68baSNigel Croxon IsValidAscii (
154*530d68baSNigel Croxon     IN  CHAR16  Ascii
155*530d68baSNigel Croxon     )
156*530d68baSNigel Croxon {
157*530d68baSNigel Croxon     if ((Ascii >= 0x20) && (Ascii <= 0x7f)) {
158*530d68baSNigel Croxon         return TRUE;
159*530d68baSNigel Croxon     }
160*530d68baSNigel Croxon     return FALSE;
161*530d68baSNigel Croxon }
162*530d68baSNigel Croxon 
163*530d68baSNigel Croxon BOOLEAN
IsValidEfiCntlChar(IN CHAR16 c)164*530d68baSNigel Croxon IsValidEfiCntlChar (
165*530d68baSNigel Croxon     IN  CHAR16  c
166*530d68baSNigel Croxon     )
167*530d68baSNigel Croxon {
168*530d68baSNigel Croxon     if (c == CHAR_NULL || c == CHAR_BACKSPACE || c == CHAR_LINEFEED || c == CHAR_CARRIAGE_RETURN) {
169*530d68baSNigel Croxon         return TRUE;
170*530d68baSNigel Croxon     }
171*530d68baSNigel Croxon     return FALSE;
172*530d68baSNigel Croxon }
173*530d68baSNigel Croxon 
174