| 1 | // Copyright (C) 2010 by Ben Cressey, Sylvain Beucler, Chris Spiegel. |
| 2 | // |
| 3 | // This file is part of Gargoyle. |
| 4 | // |
| 5 | // Gargoyle is free software; you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation; either version 2 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // Gargoyle is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with Gargoyle; if not, write to the Free Software |
| 17 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | |
| 19 | #include <memory> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <fontconfig/fontconfig.h> |
| 24 | |
| 25 | #include "format.h" |
| 26 | #include "optional.hpp" |
| 27 | |
| 28 | #include "font.h" |
| 29 | #include "glk.h" |
| 30 | #include "garglk.h" |
| 31 | |
| 32 | static FcConfig *cfg; |
| 33 | |
| 34 | static nonstd::optional<std::string> findfont(const std::string &fontname) |
| 35 | { |
| 36 | FcChar8 *strval = nullptr; |
| 37 | |
| 38 | auto p = garglk::unique(FcNameParse(reinterpret_cast<const FcChar8 *>(fontname.c_str())), FcPatternDestroy); |
| 39 | if (p == nullptr) { |
| 40 | return nonstd::nullopt; |
| 41 | } |
| 42 | |
| 43 | auto os = garglk::unique(FcObjectSetBuild(FC_FILE, static_cast<char *>(nullptr)), FcObjectSetDestroy); |
| 44 | if (os == nullptr) { |
| 45 | return nonstd::nullopt; |
| 46 | } |
| 47 | |
| 48 | auto fs = garglk::unique(FcFontList(cfg, p.get(), os.get()), FcFontSetDestroy); |
| 49 | if (fs->nfont == 0) { |
| 50 | return nonstd::nullopt; |
| 51 | } |
| 52 | |
| 53 | if (FcPatternGetString(fs->fonts[0], FC_FILE, 0, &strval) == FcResultTypeMismatch || strval == nullptr) { |
| 54 | return nonstd::nullopt; |
| 55 | } |
| 56 | |
| 57 | return reinterpret_cast<char *>(strval); |
| 58 | } |
| 59 | |
| 60 | static nonstd::optional<std::string> find_font_by_styles(const std::string &basefont, const std::vector<std::string> &styles, const std::vector<int> &weights, const std::vector<std::string> &slants) |
| 61 | { |
| 62 | // Prefer normal width fonts, but if there aren't any, allow whatever fontconfig finds. |
| 63 | std::vector<std::string> widths = {":width=100", ""}; |
| 64 | |
| 65 | for (const auto &width : widths) { |
| 66 | for (const auto &style : styles) { |
| 67 | for (const auto &weight : weights) { |
| 68 | for (const auto &slant : slants) { |
| 69 | auto fontname = Format("{}:style={}:weight={}:{}{}", basefont, style, weight, slant, width); |
| 70 | |
| 71 | auto fontpath = findfont(fontname); |
| 72 | if (fontpath.has_value()) { |
| 73 | return fontpath; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return nonstd::nullopt; |
| 81 | } |
| 82 | |
| 83 | bool garglk::fontreplace(const std::string &font, FontType type) |
| 84 | { |
| 85 | if (cfg == nullptr || font.empty()) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | // Although there are 4 "main" types of font (Regular, Italic, Bold, Bold |
| 90 | // Italic), there are actually a whole lot more possibilities, and, |
| 91 | // unfortunately, some fonts are inconsistent in what they report about |
| 92 | // their attributes. |
| 93 | // |
| 94 | // It's possible to ask fontconfig for a particular style (e.g. "Regular"), |
| 95 | // but that's not enough. The font Noto Serif, for example, has a font that |
| 96 | // lists its style as both "Condensed ExtraLight" and "Regular". If |
| 97 | // "style=Regular" were used to find regular fonts, this font would show up, |
| 98 | // which is not desirable. So in addition to the style, the weight (an |
| 99 | // integer) must also be requested. |
| 100 | // |
| 101 | // Going the other direction, Adobe Garamond has a font with the style |
| 102 | // "Regular Expert", the expert character set being totally unusable in |
| 103 | // Gargoyle. However, its integral weight is still regular. So asking just |
| 104 | // for a regular weight isn't sufficient: the style needs to be taken into |
| 105 | // account as well. |
| 106 | // |
| 107 | // In short, for each style, try various combinations of style name, weight, |
| 108 | // and slant in hopes of getting the desired font. These are ordered from |
| 109 | // highest to lowest priority, so "Italic regular italic" will be preferred |
| 110 | // over "Medium Oblique book oblique", for example. |
| 111 | std::vector<std::string> regular_styles = {"Regular", "Book", "Medium", "Roman"}; |
| 112 | std::vector<std::string> italic_styles = {"Italic", "Regular Italic", "Medium Italic", "Book Italic", "Oblique", "Regular Oblique", "Medium Oblique", "Book Oblique"}; |
| 113 | std::vector<std::string> bold_styles = {"Bold", "Extrabold", "Semibold", "Black"}; |
| 114 | std::vector<std::string> bold_italic_styles = {"Bold Italic", "Extrabold Italic", "Semibold Italic", "Black Italic", "Bold Oblique", "Extrabold Oblique", "Semibold Oblique", "Black Oblique"}; |
| 115 | |
| 116 | // Fontconfig could/should accept "medium" (for example), and this |
| 117 | // generally works, except that in some rare cases I've seen it |
| 118 | // return weights that are not medium (100); explicitly listing the |
| 119 | // weight value doesn't have this problem. |
| 120 | |
| 121 | std::vector<int> regular_weights = {FC_WEIGHT_REGULAR, FC_WEIGHT_BOOK, FC_WEIGHT_MEDIUM}; |
| 122 | std::vector<int> bold_weights = {FC_WEIGHT_BOLD, FC_WEIGHT_EXTRABOLD, FC_WEIGHT_DEMIBOLD, FC_WEIGHT_BLACK}; |
| 123 | |
| 124 | std::vector<std::string> roman_slants = {"roman"}; |
| 125 | std::vector<std::string> italic_slants = {"italic", "oblique"}; |
| 126 | |
| 127 | // regular or roman |
| 128 | auto sysfont = find_font_by_styles(font, regular_styles, regular_weights, roman_slants); |
| 129 | |
| 130 | // A regular font is required to exist, so bail if it doesn't (even |
| 131 | // if bold, italic, and/or bold italic are available). Without this, |
| 132 | // it'd be possible to get a setup where, for example, the bold font |
| 133 | // is one family and all other fonts are the fallback. Bold and |
| 134 | // italic variations can be created out of a regular font, so it's |
| 135 | // OK if those don't exist. |
| 136 | if (!sysfont.has_value()) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | FontFiller filler(type); |
| 141 | |
| 142 | filler.add(FontFiller::Style::Regular, sysfont); |
| 143 | |
| 144 | sysfont = find_font_by_styles(font, bold_styles, bold_weights, roman_slants); |
| 145 | filler.add(FontFiller::Style::Bold, sysfont); |
| 146 | |
| 147 | sysfont = find_font_by_styles(font, italic_styles, regular_weights, italic_slants); |
| 148 | filler.add(FontFiller::Style::Italic, sysfont); |
| 149 | |
| 150 | sysfont = find_font_by_styles(font, bold_italic_styles, bold_weights, italic_slants); |
| 151 | filler.add(FontFiller::Style::BoldItalic, sysfont); |
| 152 | |
| 153 | return filler.fill(); |
| 154 | } |
| 155 | |
| 156 | void fontload() |
| 157 | { |
| 158 | cfg = FcInitLoadConfigAndFonts(); |
| 159 | } |
| 160 | |
| 161 | void fontunload() |
| 162 | { |
| 163 | if (cfg != nullptr) { |
| 164 | FcConfigDestroy(cfg); |
| 165 | } |
| 166 | } |