UNPKG

3.83 kBtext/x-cView Raw
1//
2// Test half-width vs full-width characters.
3//
4
5#include <windows.h>
6#include <assert.h>
7#include <stdlib.h>
8#include <stdio.h>
9
10#include "TestUtil.cc"
11
12static void writeChars(const wchar_t *text) {
13 wcslen(text);
14 const int len = wcslen(text);
15 DWORD actual = 0;
16 BOOL ret = WriteConsoleW(
17 GetStdHandle(STD_OUTPUT_HANDLE),
18 text, len, &actual, NULL);
19 trace("writeChars: ret=%d, actual=%lld", ret, (long long)actual);
20}
21
22static void dumpChars(int x, int y, int w, int h) {
23 BOOL ret;
24 const COORD bufSize = {w, h};
25 const COORD bufCoord = {0, 0};
26 const SMALL_RECT topLeft = {x, y, x + w - 1, y + h - 1};
27 CHAR_INFO mbcsData[w * h];
28 CHAR_INFO unicodeData[w * h];
29 SMALL_RECT readRegion;
30 readRegion = topLeft;
31 ret = ReadConsoleOutputW(GetStdHandle(STD_OUTPUT_HANDLE), unicodeData,
32 bufSize, bufCoord, &readRegion);
33 assert(ret);
34 readRegion = topLeft;
35 ret = ReadConsoleOutputA(GetStdHandle(STD_OUTPUT_HANDLE), mbcsData,
36 bufSize, bufCoord, &readRegion);
37 assert(ret);
38
39 printf("\n");
40 for (int i = 0; i < w * h; ++i) {
41 printf("(%02d,%02d) CHAR: %04x %4x -- %02x %4x\n",
42 x + i % w, y + i / w,
43 (unsigned short)unicodeData[i].Char.UnicodeChar,
44 (unsigned short)unicodeData[i].Attributes,
45 (unsigned char)mbcsData[i].Char.AsciiChar,
46 (unsigned short)mbcsData[i].Attributes);
47 }
48}
49
50int main(int argc, char *argv[]) {
51 system("cls");
52 setWindowPos(0, 0, 1, 1);
53 setBufferSize(80, 38);
54 setWindowPos(0, 0, 80, 38);
55
56 // Write text.
57 const wchar_t text1[] = {
58 0x3044, // U+3044 (HIRAGANA LETTER I)
59 0x2014, // U+2014 (EM DASH)
60 0x3044, // U+3044 (HIRAGANA LETTER I)
61 0xFF2D, // U+FF2D (FULLWIDTH LATIN CAPITAL LETTER M)
62 0x30FC, // U+30FC (KATAKANA-HIRAGANA PROLONGED SOUND MARK)
63 0x0031, // U+3031 (DIGIT ONE)
64 0x2014, // U+2014 (EM DASH)
65 0x0032, // U+0032 (DIGIT TWO)
66 0x005C, // U+005C (REVERSE SOLIDUS)
67 0x3044, // U+3044 (HIRAGANA LETTER I)
68 0
69 };
70 setCursorPos(0, 0);
71 writeChars(text1);
72
73 setCursorPos(78, 1);
74 writeChars(L"<>");
75
76 const wchar_t text2[] = {
77 0x0032, // U+3032 (DIGIT TWO)
78 0x3044, // U+3044 (HIRAGANA LETTER I)
79 0,
80 };
81 setCursorPos(78, 1);
82 writeChars(text2);
83
84 system("pause");
85
86 dumpChars(0, 0, 17, 1);
87 dumpChars(2, 0, 2, 1);
88 dumpChars(2, 0, 1, 1);
89 dumpChars(3, 0, 1, 1);
90 dumpChars(78, 1, 2, 1);
91 dumpChars(0, 2, 2, 1);
92
93 system("pause");
94 system("cls");
95
96 const wchar_t text3[] = {
97 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 1
98 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 2
99 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 3
100 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 4
101 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 5
102 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 6
103 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 7
104 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 8
105 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 9
106 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 10
107 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 11
108 0x30FC, 0x30FC, 0x30FC, 0xFF2D, // 12
109 L'\r', '\n',
110 L'\r', '\n',
111 0
112 };
113 writeChars(text3);
114 system("pause");
115 {
116 const COORD bufSize = {80, 2};
117 const COORD bufCoord = {0, 0};
118 SMALL_RECT readRegion = {0, 0, 79, 1};
119 CHAR_INFO unicodeData[160];
120 BOOL ret = ReadConsoleOutputW(GetStdHandle(STD_OUTPUT_HANDLE), unicodeData,
121 bufSize, bufCoord, &readRegion);
122 assert(ret);
123 for (int i = 0; i < 96; ++i) {
124 printf("%04x ", unicodeData[i].Char.UnicodeChar);
125 }
126 printf("\n");
127 }
128
129 return 0;
130}