UNPKG

5.49 kBtext/x-cView Raw
1#include <windows.h>
2#include <locale.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string>
6
7#include "TestUtil.cc"
8
9#define COUNT_OF(array) (sizeof(array) / sizeof((array)[0]))
10
11// See https://en.wikipedia.org/wiki/List_of_CJK_fonts
12const wchar_t kMSGothic[] = { 0xff2d, 0xff33, 0x0020, 0x30b4, 0x30b7, 0x30c3, 0x30af, 0 }; // Japanese
13const wchar_t kNSimSun[] = { 0x65b0, 0x5b8b, 0x4f53, 0 }; // Simplified Chinese
14const wchar_t kMingLight[] = { 0x7d30, 0x660e, 0x9ad4, 0 }; // Traditional Chinese
15const wchar_t kGulimChe[] = { 0xad74, 0xb9bc, 0xccb4, 0 }; // Korean
16
17int main() {
18 setlocale(LC_ALL, "");
19 wchar_t *cmdline = GetCommandLineW();
20 int argc = 0;
21 wchar_t **argv = CommandLineToArgvW(cmdline, &argc);
22 const HANDLE conout = openConout();
23
24 if (argc == 1) {
25 cprintf(L"Usage:\n");
26 cprintf(L" SetFont <index>\n");
27 cprintf(L" SetFont options\n");
28 cprintf(L"\n");
29 cprintf(L"Options for SetCurrentConsoleFontEx:\n");
30 cprintf(L" -idx INDEX\n");
31 cprintf(L" -w WIDTH\n");
32 cprintf(L" -h HEIGHT\n");
33 cprintf(L" -family (0xNN|NN)\n");
34 cprintf(L" -weight (normal|bold|NNN)\n");
35 cprintf(L" -face FACENAME\n");
36 cprintf(L" -face-{gothic|simsun|minglight|gulimche) [JP,CN-sim,CN-tra,KR]\n");
37 cprintf(L" -tt\n");
38 cprintf(L" -vec\n");
39 cprintf(L" -vp\n");
40 cprintf(L" -dev\n");
41 cprintf(L" -roman\n");
42 cprintf(L" -swiss\n");
43 cprintf(L" -modern\n");
44 cprintf(L" -script\n");
45 cprintf(L" -decorative\n");
46 return 0;
47 }
48
49 if (isdigit(argv[1][0])) {
50 int index = _wtoi(argv[1]);
51 HMODULE kernel32 = LoadLibraryW(L"kernel32.dll");
52 FARPROC proc = GetProcAddress(kernel32, "SetConsoleFont");
53 if (proc == NULL) {
54 cprintf(L"Couldn't get address of SetConsoleFont\n");
55 } else {
56 BOOL ret = reinterpret_cast<BOOL WINAPI(*)(HANDLE, DWORD)>(proc)(
57 conout, index);
58 cprintf(L"SetFont returned %d\n", ret);
59 }
60 return 0;
61 }
62
63 CONSOLE_FONT_INFOEX fontex = {0};
64 fontex.cbSize = sizeof(fontex);
65
66 for (int i = 1; i < argc; ++i) {
67 std::wstring arg = argv[i];
68 if (i + 1 < argc) {
69 std::wstring next = argv[i + 1];
70 if (arg == L"-idx") {
71 fontex.nFont = _wtoi(next.c_str());
72 ++i; continue;
73 } else if (arg == L"-w") {
74 fontex.dwFontSize.X = _wtoi(next.c_str());
75 ++i; continue;
76 } else if (arg == L"-h") {
77 fontex.dwFontSize.Y = _wtoi(next.c_str());
78 ++i; continue;
79 } else if (arg == L"-weight") {
80 if (next == L"normal") {
81 fontex.FontWeight = 400;
82 } else if (next == L"bold") {
83 fontex.FontWeight = 700;
84 } else {
85 fontex.FontWeight = _wtoi(next.c_str());
86 }
87 ++i; continue;
88 } else if (arg == L"-face") {
89 wcsncpy(fontex.FaceName, next.c_str(), COUNT_OF(fontex.FaceName));
90 ++i; continue;
91 } else if (arg == L"-family") {
92 fontex.FontFamily = strtol(narrowString(next).c_str(), nullptr, 0);
93 ++i; continue;
94 }
95 }
96 if (arg == L"-tt") {
97 fontex.FontFamily |= TMPF_TRUETYPE;
98 } else if (arg == L"-vec") {
99 fontex.FontFamily |= TMPF_VECTOR;
100 } else if (arg == L"-vp") {
101 // Setting the TMPF_FIXED_PITCH bit actually indicates variable
102 // pitch.
103 fontex.FontFamily |= TMPF_FIXED_PITCH;
104 } else if (arg == L"-dev") {
105 fontex.FontFamily |= TMPF_DEVICE;
106 } else if (arg == L"-roman") {
107 fontex.FontFamily = (fontex.FontFamily & ~0xF0) | FF_ROMAN;
108 } else if (arg == L"-swiss") {
109 fontex.FontFamily = (fontex.FontFamily & ~0xF0) | FF_SWISS;
110 } else if (arg == L"-modern") {
111 fontex.FontFamily = (fontex.FontFamily & ~0xF0) | FF_MODERN;
112 } else if (arg == L"-script") {
113 fontex.FontFamily = (fontex.FontFamily & ~0xF0) | FF_SCRIPT;
114 } else if (arg == L"-decorative") {
115 fontex.FontFamily = (fontex.FontFamily & ~0xF0) | FF_DECORATIVE;
116 } else if (arg == L"-face-gothic") {
117 wcsncpy(fontex.FaceName, kMSGothic, COUNT_OF(fontex.FaceName));
118 } else if (arg == L"-face-simsun") {
119 wcsncpy(fontex.FaceName, kNSimSun, COUNT_OF(fontex.FaceName));
120 } else if (arg == L"-face-minglight") {
121 wcsncpy(fontex.FaceName, kMingLight, COUNT_OF(fontex.FaceName));
122 } else if (arg == L"-face-gulimche") {
123 wcsncpy(fontex.FaceName, kGulimChe, COUNT_OF(fontex.FaceName));
124 } else {
125 cprintf(L"Unrecognized argument: %ls\n", arg.c_str());
126 exit(1);
127 }
128 }
129
130 cprintf(L"Setting to: nFont=%u dwFontSize=(%d,%d) "
131 L"FontFamily=0x%x FontWeight=%u "
132 L"FaceName=\"%ls\"\n",
133 static_cast<unsigned>(fontex.nFont),
134 fontex.dwFontSize.X, fontex.dwFontSize.Y,
135 fontex.FontFamily, fontex.FontWeight,
136 fontex.FaceName);
137
138 BOOL ret = SetCurrentConsoleFontEx(
139 conout,
140 FALSE,
141 &fontex);
142 cprintf(L"SetCurrentConsoleFontEx returned %d\n", ret);
143
144 return 0;
145}