Skip to content

Commit

Permalink
Try to keep font atlas texture below 1024x1024 to keep support for Wi…
Browse files Browse the repository at this point in the history
…ndows GDI OpenGL renderer
  • Loading branch information
piernov committed May 18, 2022
1 parent 5ca519d commit c9416a3
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/openboardview/main_opengl.cpp
Expand Up @@ -311,6 +311,18 @@ int main(int argc, char **argv) {

if (!customFont.empty()) fontList.push_front(customFont);

// Try to keep atlas size below 1024x1024 pixels (max texture size on e.g. Windows GDI)
io.Fonts->TexDesiredWidth = 1024;
// Max size is 76px for a single font with current glyph range (192 glyphs) and default oversampling (h*3, v*1), could break in future ImGui updates
// Different fonts can also overflow, e.g. Arial and Helvetica work but Algerian does not
// Expected max g.font_size is around 50, limit set in BoardView.cpp for Preferences panel, user can manually set higher value in config file but could break
// Rough estimate with some margin (72 instead of 76), assuming small font is 2 times smaller, max size for larger font is
double maxLargeFontSizeSquared = 72.0 * 72.0 - g.font_size * g.font_size - (g.font_size / 2.0) * (g.font_size / 2.0);
if (maxLargeFontSizeSquared < 1.0) {
maxLargeFontSizeSquared = 1.0;
}
double largeFontScaleFactor = std::min(8.0, std::sqrt(maxLargeFontSizeSquared) / g.font_size); // Find max scale factor for large font, use at most 8 times larger font

for (const auto &name : fontList) {
app.obvconfig.WriteStr("fontName", name.c_str());
#ifdef _WIN32
Expand All @@ -321,17 +333,18 @@ int main(int argc, char **argv) {
io.Fonts->AddFontFromMemoryTTF(
const_cast<void *>(reinterpret_cast<const void *>(ttf.data())), ttf.size(), g.font_size, &font_cfg);
io.Fonts->AddFontFromMemoryTTF(
const_cast<void *>(reinterpret_cast<const void *>(ttf.data())), ttf.size(), g.font_size * 8, &font_cfg); // Larger font for resizeable (zoomed) text
const_cast<void *>(reinterpret_cast<const void *>(ttf.data())), ttf.size(), g.font_size * largeFontScaleFactor, &font_cfg); // Larger font for resizeable (zoomed) text
io.Fonts->AddFontFromMemoryTTF(
const_cast<void *>(reinterpret_cast<const void *>(ttf.data())), ttf.size(), g.font_size / 2, &font_cfg); // Smaller font for resizeable (zoomed) text
break;
}

#else
const std::string fontpath = get_font_path(name);
if (fontpath.empty()) continue; // Font not found
if (check_fileext(fontpath, ".ttf")) { // ImGui handles only TrueType fonts so exclude anything which has a different ext
io.Fonts->AddFontFromFileTTF(fontpath.c_str(), g.font_size);
io.Fonts->AddFontFromFileTTF(fontpath.c_str(), g.font_size * 8); // Larger font for resizeable (zoomed) text
io.Fonts->AddFontFromFileTTF(fontpath.c_str(), g.font_size * largeFontScaleFactor); // Larger font for resizeable (zoomed) text
io.Fonts->AddFontFromFileTTF(fontpath.c_str(), g.font_size / 2); // Smaller font for resizeable (zoomed) text
break;
}
Expand Down

0 comments on commit c9416a3

Please sign in to comment.