30 #include <SDL2/SDL_ttf.h> 35 #include "Utilities.h" 50 TTF_CloseFont(ttfFont);
66 TTF_SizeUTF8(fontPointer.get(), text.c_str(), &w, &h);
67 return std::make_tuple(w, h);
76 std::vector<std::filesystem::path> mFontPathList{};
79 std::stringstream strm(
"/usr/share/fonts:/usr/local/share/fonts");
80 std::string rootPathStr{};
81 while (getline(strm, rootPathStr,
':')) {
82 mFontPathList.emplace_back(rootPathStr);
100 template<
typename StringType>
101 std::optional<std::filesystem::path>
locateFont(
const std::filesystem::path &path, StringType fontName) {
102 for (
auto &p : std::filesystem::recursive_directory_iterator(path)) {
103 if (p.path().stem() == fontName && p.is_regular_file()) {
116 template<
typename StringType>
117 std::optional<std::filesystem::path>
getFontPath(StringType fontName) {
118 if (
auto found = mFontPathMap.find(fontName); found != mFontPathMap.end())
119 return found->second;
121 for (
auto const &rootPath : mFontPathList) {
122 auto fontPath = locateFont(rootPath, fontName);
124 mFontPathMap[fontName] = fontPath.value();
139 template<
typename StringType>
141 if (
auto found = mFontCache.find(
FontCacheKey{fontName, ptSize}); found != mFontCache.end()) {
142 return found->second;
145 if (
auto fontPath = getFontPath(fontName); fontPath) {
147 auto font = mFontCache.emplace(
FontCacheKey{fontName, ptSize}, fontPointer);
149 return font.first->second;
162 inline std::tuple<int, int, int, int, int> getGlyphMetrics(
FontPointer &font,
char glyph) {
163 int minx{}, maxx{}, miny{}, maxy{}, advance{};
165 TTF_GlyphMetrics(font.get(), glyph, &minx, &maxx, &miny, &maxy, &advance);
166 return std::make_tuple(minx, maxx, miny, maxy, advance);
193 fontMetrics.
fontHeight = TTF_FontHeight(font.get());
194 fontMetrics.fontAscent = TTF_FontAscent(font.get());
195 fontMetrics.fontDescent = TTF_FontDescent(font.get());
196 fontMetrics.fontLineSkip = TTF_FontLineSkip(font.get());
210 auto font = fontCache.
getFont(fontName, fontSize);
212 font = fontCache.
getFont(
"FreeSans", fontSize);
214 throw std::runtime_error(
StringCompositor(
"Neither font", fontName,
" nor default font 'FreeSans' found: ",
std::map< FontCacheKey, FontPointer > mFontCache
The font cache.
Definition: Font.h:159
std::optional< std::filesystem::path > getFontPath(StringType fontName)
Find a font name in the font name cache.
Definition: Font.h:117
void operator()(TTF_Font *ttfFont)
Destroy a TTF_Font pointer.
Definition: Font.h:49
std::string StringCompositor(Arg &&arg, Args &&... args)
Composite a pack of arguments that are streamable to a string.
Definition: Utilities.h:114
std::pair< std::string, int > FontCacheKey
Type for TTF cache key.
Definition: Font.h:55
std::shared_ptr< TTF_Font > FontPointer
Type for TTF smart pointer.
Definition: Font.h:54
std::map< FontCacheKey, FontPointer > FontCacheStore
Type for TTF cache store.
Definition: Font.h:56
int fontLineSkip
The size of a line advance for the font.
Definition: Font.h:174
auto fetchFont(FontCache &fontCache, const std::string &fontName, int fontSize)
Fetch a font.
Definition: Font.h:209
auto textSizeUTF8(FontPointer &fontPointer, const std::string &text)
Get the size of a UTF8 string.
Definition: Font.h:64
std::optional< std::filesystem::path > locateFont(const std::filesystem::path &path, StringType fontName)
Locate a font file.
Definition: Font.h:101
FontPointer getFont(StringType fontName, int ptSize)
Get a FontPointer to a named Font of a specific point size.
Definition: Font.h:140
auto getFontMetrics(FontPointer &font)
Get the font metrics of the current font.
Definition: Font.h:191
The size metrics that pertain to a particular font.
Definition: Font.h:173
Cache storage for requested fonts.
Definition: Font.h:74
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
std::map< std::string, std::filesystem::path > mFontPathMap
The font file path cache.
Definition: Font.h:157
A functor to destroy a TTF_Font.
Definition: Font.h:43
int fontHeight
The total height of the font (ascent - descent.
Definition: Font.h:174