| 1 | // Copyright (C) 2010 by Ben Cressey. |
| 2 | // Copyright (C) 2021 by Chris Spiegel |
| 3 | // |
| 4 | // This file is part of Gargoyle. |
| 5 | // |
| 6 | // Gargoyle is free software; you can redistribute it and/or modify |
| 7 | // it under the terms of the GNU General Public License as published by |
| 8 | // the Free Software Foundation; either version 2 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // Gargoyle is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public License |
| 17 | // along with Gargoyle; if not, write to the Free Software |
| 18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | |
| 20 | #define NOMINMAX |
| 21 | #include <windows.h> |
| 22 | |
| 23 | #include <cstdio> |
| 24 | #include <cstdlib> |
| 25 | #include <string> |
| 26 | |
| 27 | #include "format.h" |
| 28 | #include "optional.hpp" |
| 29 | |
| 30 | #include "font.h" |
| 31 | #include "glk.h" |
| 32 | #include "garglk.h" |
| 33 | |
| 34 | #define FONT_SUBKEY ("Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts") |
| 35 | |
| 36 | // forward declaration |
| 37 | static bool find_font_file(const std::string &facename, std::string &filepath); |
| 38 | |
| 39 | static HDC hdc; |
| 40 | |
| 41 | static int CALLBACK font_cb(ENUMLOGFONTEX *lpelfe, NEWTEXTMETRICEX *, int, LPARAM filler_) |
| 42 | { |
| 43 | FontFiller *filler = reinterpret_cast<FontFiller *>(filler_); |
| 44 | std::string filepath; |
| 45 | std::string style = reinterpret_cast<char *>(lpelfe->elfStyle); |
| 46 | |
| 47 | if (!find_font_file(reinterpret_cast<char *>(lpelfe->elfFullName), filepath)) { |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | if (style == "Regular" || style == "Roman") { |
| 52 | filler->add(FontFiller::Style::Regular, filepath); |
| 53 | } else if (style == "Bold") { |
| 54 | filler->add(FontFiller::Style::Bold, filepath); |
| 55 | } else if (style == "Italic" || style == "Oblique") { |
| 56 | filler->add(FontFiller::Style::Italic, filepath); |
| 57 | } else if (style == "Bold Italic" || style == "Bold Oblique" || style == "BoldItalic" || style == "BoldOblique") { |
| 58 | filler->add(FontFiller::Style::BoldItalic, filepath); |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static std::string make_font_filepath(const std::string &filename) |
| 65 | { |
| 66 | // create the absolute path to the font file |
| 67 | if (filename.find(':') == std::string::npos && std::getenv("SYSTEMROOT") != nullptr) { |
| 68 | return Format("{}\\Fonts\\{}", std::getenv("SYSTEMROOT"), filename); |
| 69 | } else { |
| 70 | return filename; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static bool find_font_file_with_key(HKEY key, const char *subkey, const std::string &facename, std::string &filepath) |
| 75 | { |
| 76 | HKEY hkey; |
| 77 | DWORD size; |
| 78 | std::string face; |
| 79 | char filename[256]; |
| 80 | |
| 81 | // open the Fonts registry key |
| 82 | if (RegOpenKeyExA(key, subkey, 0, KEY_QUERY_VALUE, &hkey) != ERROR_SUCCESS) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | // check for a TrueType font |
| 87 | face = Format("{} (TrueType)", facename); |
| 88 | |
| 89 | size = sizeof(filename); |
| 90 | |
| 91 | if (RegQueryValueExA(hkey, face.c_str(), nullptr, nullptr, (PBYTE)filename, &size) == ERROR_SUCCESS) { |
| 92 | filepath = make_font_filepath(filename); |
| 93 | RegCloseKey(hkey); |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | // check for an OpenType font |
| 98 | face = Format("{} (OpenType)", facename); |
| 99 | |
| 100 | size = sizeof(filename); |
| 101 | |
| 102 | if (RegQueryValueExA(hkey, face.c_str(), nullptr, nullptr, (PBYTE)filename, &size) == ERROR_SUCCESS) { |
| 103 | filepath = make_font_filepath(filename); |
| 104 | RegCloseKey(hkey); |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | // TODO: support TrueType/OpenType collections (TTC/OTC) |
| 109 | |
| 110 | RegCloseKey(hkey); |
| 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | static bool find_font_file(const std::string &facename, std::string &filepath) |
| 116 | { |
| 117 | // First, try the per-user key |
| 118 | if (find_font_file_with_key(HKEY_CURRENT_USER, FONT_SUBKEY, facename, filepath)) { |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | // Nope. Try the system-wide key. |
| 123 | return find_font_file_with_key(HKEY_LOCAL_MACHINE, FONT_SUBKEY, facename, filepath); |
| 124 | } |
| 125 | |
| 126 | bool garglk::fontreplace(const std::string &font, FontType type) |
| 127 | { |
| 128 | if (font.empty()) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | LOGFONTA logfont; |
| 133 | |
| 134 | ZeroMemory(&logfont, sizeof logfont); |
| 135 | logfont.lfCharSet = DEFAULT_CHARSET; |
| 136 | logfont.lfPitchAndFamily = FF_DONTCARE; |
| 137 | |
| 138 | hdc = GetDC(0); |
| 139 | |
| 140 | FontFiller filler(type); |
| 141 | |
| 142 | std::snprintf(logfont.lfFaceName, LF_FACESIZE, "%s", font.c_str()); |
| 143 | EnumFontFamiliesExA(hdc, &logfont, reinterpret_cast<FONTENUMPROCA>(font_cb), reinterpret_cast<LPARAM>(&filler), 0); |
| 144 | |
| 145 | ReleaseDC(0, hdc); |
| 146 | |
| 147 | return filler.fill(); |
| 148 | } |
| 149 | |
| 150 | void fontload() |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | void fontunload() |
| 155 | { |
| 156 | } |