aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/3rdparty/winpty/misc/SetFont.cc
blob: 9bcd4b4cc971dd9e0fc18c49edac85018832dbbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <windows.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>

#include "TestUtil.cc"

#define COUNT_OF(array) (sizeof(array) / sizeof((array)[0]))

// See https://en.wikipedia.org/wiki/List_of_CJK_fonts
const wchar_t kMSGothic[] = { 0xff2d, 0xff33, 0x0020, 0x30b4, 0x30b7, 0x30c3, 0x30af, 0 }; // Japanese
const wchar_t kNSimSun[] = { 0x65b0, 0x5b8b, 0x4f53, 0 }; // Simplified Chinese
const wchar_t kMingLight[] = { 0x7d30, 0x660e, 0x9ad4, 0 }; // Traditional Chinese
const wchar_t kGulimChe[] = { 0xad74, 0xb9bc, 0xccb4, 0 }; // Korean

int main() {
    setlocale(LC_ALL, "");
    wchar_t *cmdline = GetCommandLineW();
    int argc = 0;
    wchar_t **argv = CommandLineToArgvW(cmdline, &argc);
    const HANDLE conout = openConout();

    if (argc == 1) {
        cprintf(L"Usage:\n");
        cprintf(L"  SetFont <index>\n");
        cprintf(L"  SetFont options\n");
        cprintf(L"\n");
        cprintf(L"Options for SetCurrentConsoleFontEx:\n");
        cprintf(L"  -idx INDEX\n");
        cprintf(L"  -w WIDTH\n");
        cprintf(L"  -h HEIGHT\n");
        cprintf(L"  -family (0xNN|NN)\n");
        cprintf(L"  -weight (normal|bold|NNN)\n");
        cprintf(L"  -face FACENAME\n");
        cprintf(L"  -face-{gothic|simsun|minglight|gulimche) [JP,CN-sim,CN-tra,KR]\n");
        cprintf(L"  -tt\n");
        cprintf(L"  -vec\n");
        cprintf(L"  -vp\n");
        cprintf(L"  -dev\n");
        cprintf(L"  -roman\n");
        cprintf(L"  -swiss\n");
        cprintf(L"  -modern\n");
        cprintf(L"  -script\n");
        cprintf(L"  -decorative\n");
        return 0;
    }

    if (isdigit(argv[1][0])) {
        int index = _wtoi(argv[1]);
        HMODULE kernel32 = LoadLibraryW(L"kernel32.dll");
        FARPROC proc = GetProcAddress(kernel32, "SetConsoleFont");
        if (proc == NULL) {
            cprintf(L"Couldn't get address of SetConsoleFont\n");
        } else {
            BOOL ret = reinterpret_cast<BOOL WINAPI(*)(HANDLE, DWORD)>(proc)(
                    conout, index);
            cprintf(L"SetFont returned %d\n", ret);
        }
        return 0;
    }

    CONSOLE_FONT_INFOEX fontex = {0};
    fontex.cbSize = sizeof(fontex);

    for (int i = 1; i < argc; ++i) {
        std::wstring arg = argv[i];
        if (i + 1 < argc) {
            std::wstring next = argv[i + 1];
            if (arg == L"-idx") {
                fontex.nFont = _wtoi(next.c_str());
                ++i; continue;
            } else if (arg == L"-w") {
                fontex.dwFontSize.X = _wtoi(next.c_str());
                ++i; continue;
            } else if (arg == L"-h") {
                fontex.dwFontSize.Y = _wtoi(next.c_str());
                ++i; continue;
            } else if (arg == L"-weight") {
                if (next == L"normal") {
                    fontex.FontWeight = 400;
                } else if (next == L"bold") {
                    fontex.FontWeight = 700;
                } else {
                    fontex.FontWeight = _wtoi(next.c_str());
                }
                ++i; continue;
            } else if (arg == L"-face") {
                wcsncpy(fontex.FaceName, next.c_str(), COUNT_OF(fontex.FaceName));
                ++i; continue;
            } else if (arg == L"-family") {
                fontex.FontFamily = strtol(narrowString(next).c_str(), nullptr, 0);
                ++i; continue;
            }
        }
        if (arg == L"-tt") {
            fontex.FontFamily |= TMPF_TRUETYPE;
        } else if (arg == L"-vec") {
            fontex.FontFamily |= TMPF_VECTOR;
        } else if (arg == L"-vp") {
            // Setting the TMPF_FIXED_PITCH bit actually indicates variable
            // pitch.
            fontex.FontFamily |= TMPF_FIXED_PITCH;
        } else if (arg == L"-dev") {
            fontex.FontFamily |= TMPF_DEVICE;
        } else if (arg == L"-roman") {
            fontex.FontFamily = (fontex.FontFamily & ~0xF0) | FF_ROMAN;
        } else if (arg == L"-swiss") {
            fontex.FontFamily = (fontex.FontFamily & ~0xF0) | FF_SWISS;
        } else if (arg == L"-modern") {
            fontex.FontFamily = (fontex.FontFamily & ~0xF0) | FF_MODERN;
        } else if (arg == L"-script") {
            fontex.FontFamily = (fontex.FontFamily & ~0xF0) | FF_SCRIPT;
        } else if (arg == L"-decorative") {
            fontex.FontFamily = (fontex.FontFamily & ~0xF0) | FF_DECORATIVE;
        } else if (arg == L"-face-gothic") {
            wcsncpy(fontex.FaceName, kMSGothic, COUNT_OF(fontex.FaceName));
        } else if (arg == L"-face-simsun") {
            wcsncpy(fontex.FaceName, kNSimSun, COUNT_OF(fontex.FaceName));
        } else if (arg == L"-face-minglight") {
            wcsncpy(fontex.FaceName, kMingLight, COUNT_OF(fontex.FaceName));
        } else if (arg == L"-face-gulimche") {
            wcsncpy(fontex.FaceName, kGulimChe, COUNT_OF(fontex.FaceName));
        } else {
            cprintf(L"Unrecognized argument: %ls\n", arg.c_str());
            exit(1);
        }
    }

    cprintf(L"Setting to: nFont=%u dwFontSize=(%d,%d) "
        L"FontFamily=0x%x FontWeight=%u "
        L"FaceName=\"%ls\"\n",
        static_cast<unsigned>(fontex.nFont),
        fontex.dwFontSize.X, fontex.dwFontSize.Y,
        fontex.FontFamily, fontex.FontWeight,
        fontex.FaceName);

    BOOL ret = SetCurrentConsoleFontEx(
        conout,
        FALSE,
        &fontex);
    cprintf(L"SetCurrentConsoleFontEx returned %d\n", ret);

    return 0;
}