C++控制台打印wchar_t及PS1记忆卡日文字体适配问题
Hey there! Great job working through the encoding hoops with your PS1 memory card reader—handling Shift-JIS to UTF-8 and full-to-half width conversions can be tricky, so props for getting that sorted. Let’s tackle your font and portability questions:
Consolas-Style Monospace Fonts with Full Japanese Character Support
You’re right that many default console fonts miss niche characters like U+FF70 (half-width katakana). Here are some excellent Consolas-like monospace fonts that cover all Japanese characters, including those special half-width glyphs:
- Noto Sans Mono CJK JP: Google’s open-source Noto family includes this monospace variant. It’s clean, modern, and supports every Japanese character you’ll encounter (including U+FF70). The style is very close to Consolas, making it perfect for console output.
- Source Code Pro JP: Adobe’s Source Code Pro is a favorite among programmers, and the JP extension adds full Japanese support. It’s a crisp, readable monospace font that matches Consolas’s professional feel.
- Ricty Diminished: Built specifically for developers, this font combines Inconsolata (a Consolas alternative) with a Japanese font. It’s optimized for mixed code and Japanese text, and handles half-width katakana flawlessly.
- IBM Plex Mono JP: IBM’s Plex series includes a monospace variant with complete Japanese support. It’s slightly more rounded than Consolas but maintains that clean, functional monospace look.
Packaging Fonts for a Portable Console App
Windows console apps rely on system-installed fonts by default, but you can bundle a font with your program and load it temporarily at runtime to ensure consistent display across machines. Here’s how to do it:
Temporary Font Registration (Windows Native)
This method loads the font into the system temporarily (only for your app’s process) so you can use it in the console, then cleans up when your app exits. No permanent system changes are made.
Here’s a code snippet to implement this:
#include <windows.h> #include <cstdio> int main() { // Path to your bundled font file (same directory as your EXE) const wchar_t* fontFilePath = L"./NotoSansMonoCJKjp-Regular.ttf"; // Register the font temporarily (private to this process) if (!AddFontResourceExW(fontFilePath, FR_PRIVATE, nullptr)) { printf("Failed to load font\n"); return 1; } // Get the console output handle and configure the font HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_FONT_INFOEX fontInfo = {0}; fontInfo.cbSize = sizeof(CONSOLE_FONT_INFOEX); // Set the font to our bundled one (match the font's internal name) wcscpy_s(fontInfo.FaceName, L"Noto Sans Mono CJK JP"); fontInfo.dwFontSize.Y = 16; // Adjust height to your preference fontInfo.FontWeight = FW_NORMAL; fontInfo.FontFamily = FF_DONTCARE; if (!SetCurrentConsoleFontEx(consoleHandle, FALSE, &fontInfo)) { printf("Failed to set console font\n"); // Clean up before exiting RemoveFontResourceExW(fontFilePath, FR_PRIVATE, nullptr); return 1; } // Your existing memory card reading/display logic goes here // Don't forget to set console output to UTF-8 for proper rendering: SetConsoleOutputCP(CP_UTF8); // Clean up: unregister the font when done RemoveFontResourceExW(fontFilePath, FR_PRIVATE, nullptr); return 0; }
Key Notes for Portability:
- Font Licensing: Make sure the font you choose allows redistribution. All the fonts listed above use open licenses (OFL or Apache) that let you bundle them with your app.
- Testing: Windows 10/11 have better console font support than older versions. Test your app on target systems to ensure the font loads and displays correctly.
- Alternative for More Control: If you want to avoid relying on the Windows console’s font system, you could use a library like SDL or FTGL to create a custom window that renders text directly from your bundled font. This adds more complexity but gives you full control over rendering.
One quick extra tip: When printing UTF-8 text with cout, make sure to set the console output code page to UTF-8 with SetConsoleOutputCP(CP_UTF8)—this will prevent garbled text even if the system’s default code page isn’t UTF-8.
内容的提问来源于stack exchange,提问作者Jaska




