From 619bf51a4d64febb4953b43bf44a65e04518651d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sat, 5 Feb 2022 01:17:51 -0300 Subject: [PATCH] FontTrueType: Use the closed character size available when the requested is not available. --- include/eepp/graphics/fonttruetype.hpp | 1 + src/eepp/graphics/fonttruetype.cpp | 39 ++++++++++++++++++++------ src/tools/codeeditor/codeeditor.cpp | 2 +- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/include/eepp/graphics/fonttruetype.hpp b/include/eepp/graphics/fonttruetype.hpp index eeaea143b..7bf4b7fe5 100644 --- a/include/eepp/graphics/fonttruetype.hpp +++ b/include/eepp/graphics/fonttruetype.hpp @@ -113,6 +113,7 @@ class EE_API FontTrueType : public Font { mutable std::vector mPixelBuffer; ///< Pixel buffer holding a glyph's pixels before being written to the texture bool mBoldAdvanceSameAsRegular; + mutable std::map mClosestCharacterSize; }; }} // namespace EE::Graphics diff --git a/src/eepp/graphics/fonttruetype.cpp b/src/eepp/graphics/fonttruetype.cpp index d61e7bcfd..6937dac06 100644 --- a/src/eepp/graphics/fonttruetype.cpp +++ b/src/eepp/graphics/fonttruetype.cpp @@ -323,8 +323,8 @@ GlyphDrawable* FontTrueType::getGlyphDrawable( Uint32 codePoint, unsigned int ch GlyphDrawable* region = GlyphDrawable::New( page.texture, glyph.textureRect, String::format( "%s_%d_%u", mFontName.c_str(), characterSize, codePoint ) ); - region->setGlyphOffset( {glyph.bounds.Left - outlineThickness, - characterSize + glyph.bounds.Top - outlineThickness} ); + region->setGlyphOffset( { glyph.bounds.Left - outlineThickness, + characterSize + glyph.bounds.Top - outlineThickness } ); drawables[key] = region; return region; } @@ -741,12 +741,35 @@ bool FontTrueType::setCurrentSize( unsigned int characterSize ) const { // In the case of bitmap fonts, resizing can // fail if the requested size is not available if ( !FT_IS_SCALABLE( face ) ) { - Log::warning( "Failed to set bitmap font size to %d", characterSize ); - Log::warning( "Available sizes are: " ); - std::string str; - for ( int i = 0; i < face->num_fixed_sizes; ++i ) - str += String::format( "%d ", face->available_sizes[i].height ); - Log::warning( str ); + auto it = mClosestCharacterSize.find( characterSize ); + + if ( it == mClosestCharacterSize.end() ) { + Log::warning( "Failed to set bitmap font size to %d", characterSize ); + Log::warning( "Available sizes are: " ); + std::string str; + if ( face->num_fixed_sizes > 0 ) { + unsigned int selectedHeight = face->available_sizes[0].height; + int curDistance = eeabs( characterSize - selectedHeight ); + for ( int i = 0; i < face->num_fixed_sizes; ++i ) { + str += String::format( "%d ", face->available_sizes[i].height ); + int tDistance = + eeabs( characterSize - face->available_sizes[i].height ); + if ( tDistance < curDistance ) { + curDistance = tDistance; + selectedHeight = face->available_sizes[i].height; + } + } + Log::warning( str ); + Log::warning( "Setting closest bitmap font size available: ", + selectedHeight ); + mClosestCharacterSize[characterSize] = selectedHeight; + return setCurrentSize( selectedHeight ); + } else { + return false; + } + } else { + return setCurrentSize( it->second ); + } } } diff --git a/src/tools/codeeditor/codeeditor.cpp b/src/tools/codeeditor/codeeditor.cpp index 99d1056b8..60e77b0d3 100644 --- a/src/tools/codeeditor/codeeditor.cpp +++ b/src/tools/codeeditor/codeeditor.cpp @@ -179,7 +179,7 @@ void App::openFontDialog( std::string& fontPath ) { std::string absoluteFontPath( fontPath ); if ( isRelativePath( absoluteFontPath ) ) absoluteFontPath = mResPath + fontPath; - UIFileDialog* dialog = UIFileDialog::New( UIFileDialog::DefaultFlags, "*.ttf; *.otf; *.wolff", + UIFileDialog* dialog = UIFileDialog::New( UIFileDialog::DefaultFlags, "*.ttf; *.otf; *.wolff; *.otb", FileSystem::fileRemoveFileName( absoluteFontPath ) ); ModelIndex index = dialog->getMultiView()->getListView()->findRowWithText( FileSystem::fileNameFromPath( fontPath ), true, true );