C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
hello.c
Go to the documentation of this file.
1#include <inttypes.h>
2#include <locale.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <string.h>
6#include <wchar.h>
7
8typedef struct {
9 union {
10 uint32_t u32;
11 wchar_t u16[2];
12 uint8_t u8[4];
13 char c[4];
14 };
15 uint8_t backstop;
16 uint8_t width; // 5 - 5 (8 bits of EGC column width)
17} GCluster;
18
19int main() {
20 setlocale(LC_ALL, "");
21 GCluster g0, g1, g2, g3;
22
23 g0.u32 = L'┤';
24 g1.u32 = L'├';
25 g2.u32 = 0x2524;
26 g3.u32 = 0x251c;
27
28 printf("%08x %08x %08x %08x\n", g0.u32, g1.u32, g2.u32, g3.u32);
29 // 00002524 0000251c 00002524 0000251c
30 printf("%s %s %s %s\n", g0.c, g1.c, g2.c, g3.c);
31 // $% ␜% $% ␜%
32
33 // Here, we are assigning the wchar_t values directly to the u16 array
34 wchar_t c0 = L'┤';
35 wchar_t c1 = L'├';
36 g2.u16[0] = c0;
37 g3.u16[0] = c1;
38 printf("%08x %08x %08x %08x\n", g0.u32, g1.u32, g2.u32, g3.u32);
39 // 00002524 0000251c 00002524 0000251c
40 //
41 // We can probably use these values with Notcurses. They are not UTF-8
42 // encoded, but they are valid Unicode codepoints. The documentation seems
43 // to indicate that this encoding will work with ncplane_putwegc().
44 //
45 // We did that and then used ncplane_at_yx_cell() to fetch the values from
46 // the plane. We got 0xa494e2 and 0x9c94e2. Those are the correct multibyte
47 // UTF-8 encodings for our characters. So we need to use those values
48 // instead of the wchar_t values with the non-wide-character functions of
49 // Notcurses.
50 //
51 // We used strcpy to copy the wchar_t values into the char arrays. Low and
52 // behold, we got the correct multibyte UTF-8 encodings in the char arrays.
53 strcpy(g2.c, "┤"); // -> 0xa494e2
54 strcpy(g3.c, "├"); // -> 0x9c94e2
55 printf("%08x %08x %08x %08x\n", g0.u32, g1.u32, g2.u32, g3.u32);
56 printf("%s %s %s %s\n", g0.c, g1.c, g2.c, g3.c);
57 // $% ␜% ┤ ├
58 g0.u32 = L'┤';
59 strcpy(g2.c, "┤"); // -> 0xa494e2
60 printf("%x %x %s\n", g0.u32, g2.u32, g2.c);
61 // 2524 a494e2 ┤
62 g1.u32 = L'├';
63 strcpy(g3.c, "├"); // -> 0x9c94e2
64 printf("%x %x %s\n", g1.u32, g3.u32, g3.c);
65 // 251c 9c94e2 ├
66 // 00002524 0000251c 00a494e2 009c94e2
67 //
68 // Wee doggies! Would you look at that? Just like magic, we have the correct
69 // UTF-8 encoded values in the char arrays. Can use these with Notcurses?
70 // Well, we can use the char arrays with Notcurses, but we cannot use the
71 // wchar_t values. The wchar_t values are not UTF-8 encoded. They are just
72 // Unicode codepoints. We don't really care, so long as we have a way to get
73 // the results we want, either from Unicode codepoints or the character
74 // glyphs. I wonder if iconv can convert the wchar_t values to UTF-8 encoded
75 // char arrays. We can try that next.
76 //
77 printf("\n\nFrom Ode to a Mouse\n");
78 printf("\nBut Mousie, thou art no thy-lane,\n");
79 printf("In proving foresight may be vain:\n");
80 printf("The best laid schemes o’ Mice an’ Men\n");
81 printf(" Gang aft agley,\n");
82 printf("An’ lea’e us nought but grief an’ pain,\n");
83 printf(" For promis’d joy!\n\n");
84 printf(" by Robert Burns\n\n");
85
86 return 0;
87}
int main(int argc, char **argv)
Capture the current terminal settings for later restoration.
Definition amort.c:30
uint8_t backstop
Definition ui_backend.h:293
char c[4]
Definition ui_backend.h:291
uint8_t u8[4]
Definition ui_backend.h:290
uint32_t u32
Definition ui_backend.h:288
uint8_t width
Definition ui_backend.h:294
wchar_t u16[2]
Definition ui_backend.h:289