From 22e83d3da847439f90df0658b65aa89f5c4be3dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Mon, 7 Aug 2023 18:24:21 -0300 Subject: [PATCH] Fix outline thickness. --- include/eepp/graphics/font.hpp | 2 +- include/eepp/graphics/fontbmfont.hpp | 2 +- include/eepp/graphics/fontsprite.hpp | 2 +- include/eepp/graphics/fonttruetype.hpp | 2 +- src/eepp/graphics/fontbmfont.cpp | 2 +- src/eepp/graphics/fontsprite.cpp | 2 +- src/eepp/graphics/fonttruetype.cpp | 18 +++++----- src/eepp/graphics/text.cpp | 46 ++++++++++++++++---------- 8 files changed, 44 insertions(+), 32 deletions(-) diff --git a/include/eepp/graphics/font.hpp b/include/eepp/graphics/font.hpp index 802efb5c9..5e2342d62 100644 --- a/include/eepp/graphics/font.hpp +++ b/include/eepp/graphics/font.hpp @@ -98,7 +98,7 @@ class EE_API Font { const Float& forzeSize = 0 ) const = 0; virtual Float getKerning( Uint32 first, Uint32 second, unsigned int characterSize, bool bold, - bool italic ) const = 0; + bool italic, Float outlineThickness = 0 ) const = 0; virtual Float getLineSpacing( unsigned int characterSize ) const = 0; diff --git a/include/eepp/graphics/fontbmfont.hpp b/include/eepp/graphics/fontbmfont.hpp index be8575255..403fbba8d 100644 --- a/include/eepp/graphics/fontbmfont.hpp +++ b/include/eepp/graphics/fontbmfont.hpp @@ -44,7 +44,7 @@ class EE_API FontBMFont : public Font { Float outlineThickness = 0, const Float& maxWidth = 0 ) const; Float getKerning( Uint32 first, Uint32 second, unsigned int characterSize, bool bold, - bool italic ) const; + bool italic, Float outlineThickness = 0 ) const; Float getLineSpacing( unsigned int characterSize ) const; diff --git a/include/eepp/graphics/fontsprite.hpp b/include/eepp/graphics/fontsprite.hpp index 3fe60a980..36f138ba5 100644 --- a/include/eepp/graphics/fontsprite.hpp +++ b/include/eepp/graphics/fontsprite.hpp @@ -48,7 +48,7 @@ class EE_API FontSprite : public Font { Float outlineThickness = 0, const Float& maxWidth = 0 ) const; Float getKerning( Uint32 first, Uint32 second, unsigned int characterSize, bool bold, - bool italic ) const; + bool italic, Float outlineThickness = 0 ) const; Float getLineSpacing( unsigned int characterSize ) const; diff --git a/include/eepp/graphics/fonttruetype.hpp b/include/eepp/graphics/fonttruetype.hpp index 2317f5bc0..56e93736e 100644 --- a/include/eepp/graphics/fonttruetype.hpp +++ b/include/eepp/graphics/fonttruetype.hpp @@ -43,7 +43,7 @@ class EE_API FontTrueType : public Font { Float outlineThickness = 0, const Float& maxWidth = 0 ) const; Float getKerning( Uint32 first, Uint32 second, unsigned int characterSize, bool bold, - bool italic ) const; + bool italic, Float outlineThickness = 0 ) const; Float getLineSpacing( unsigned int characterSize ) const; diff --git a/src/eepp/graphics/fontbmfont.cpp b/src/eepp/graphics/fontbmfont.cpp index ab1b521e5..e415a12e5 100644 --- a/src/eepp/graphics/fontbmfont.cpp +++ b/src/eepp/graphics/fontbmfont.cpp @@ -237,7 +237,7 @@ Glyph FontBMFont::loadGlyph( Uint32 codePoint, unsigned int characterSize, bool, return glyph; } -Float FontBMFont::getKerning( Uint32, Uint32, unsigned int, bool, bool ) const { +Float FontBMFont::getKerning( Uint32, Uint32, unsigned int, bool, bool, Float ) const { return 0; } diff --git a/src/eepp/graphics/fontsprite.cpp b/src/eepp/graphics/fontsprite.cpp index 77e822dff..050ab2ba2 100644 --- a/src/eepp/graphics/fontsprite.cpp +++ b/src/eepp/graphics/fontsprite.cpp @@ -215,7 +215,7 @@ Glyph FontSprite::loadGlyph( Uint32 codePoint, unsigned int characterSize ) cons return glyph; } -Float FontSprite::getKerning( Uint32, Uint32, unsigned int, bool, bool ) const { +Float FontSprite::getKerning( Uint32, Uint32, unsigned int, bool, bool, Float ) const { return 0; } diff --git a/src/eepp/graphics/fonttruetype.cpp b/src/eepp/graphics/fonttruetype.cpp index 8ce67c168..f94f0b52a 100644 --- a/src/eepp/graphics/fonttruetype.cpp +++ b/src/eepp/graphics/fonttruetype.cpp @@ -56,7 +56,7 @@ static inline Uint64 getIndexKey( Uint32 fontInternalId, Uint32 index, bool bold Float outlineThickness ) { return ( static_cast( reinterpret( fontInternalId ) ) << 48 ) | ( static_cast( - reinterpret( static_cast( outlineThickness ) * 1000 ) ) + reinterpret( static_cast( outlineThickness ) * 100 ) ) << 34 ) | ( static_cast( bold ) << 33 ) | ( static_cast( italics ) << 32 ) | index; @@ -310,7 +310,8 @@ const Glyph& FontTrueType::getGlyph( Uint32 codePoint, unsigned int characterSiz FontManager::instance()->getColorEmojiFont()->getType() == FontType::TTF ) { if ( isMonospace() && maxWidth == 0.f ) { - Glyph monospaceGlyph = getGlyph( ' ', characterSize, bold, outlineThickness ); + Glyph monospaceGlyph = + getGlyph( ' ', characterSize, bold, italic, outlineThickness ); maxWidth = monospaceGlyph.advance; } @@ -323,7 +324,8 @@ const Glyph& FontTrueType::getGlyph( Uint32 codePoint, unsigned int characterSiz FontManager::instance()->getEmojiFont()->getType() == FontType::TTF ) { if ( isMonospace() && maxWidth == 0.f ) { - Glyph monospaceGlyph = getGlyph( ' ', characterSize, bold, outlineThickness ); + Glyph monospaceGlyph = + getGlyph( ' ', characterSize, bold, italic, outlineThickness ); maxWidth = monospaceGlyph.advance; } @@ -514,7 +516,7 @@ GlyphDrawable* FontTrueType::getGlyphDrawable( Uint32 codePoint, unsigned int ch } Float FontTrueType::getKerning( Uint32 first, Uint32 second, unsigned int characterSize, bool bold, - bool italic ) const { + bool italic, Float outlineThickness ) const { // Special case where first or second is 0 (null character) if ( first == 0 || second == 0 || isMonospace() ) return 0.f; @@ -527,10 +529,10 @@ Float FontTrueType::getKerning( Uint32 first, Uint32 second, unsigned int charac FT_UInt index2 = getGlyphIndex( second ); // Retrieve position compensation deltas generated by FT_LOAD_FORCE_AUTOHINT flag - auto firstRsbDelta = - static_cast( getGlyph( first, characterSize, bold, italic ).rsbDelta ); - auto secondLsbDelta = - static_cast( getGlyph( second, characterSize, bold, italic ).lsbDelta ); + auto firstRsbDelta = static_cast( + getGlyph( first, characterSize, bold, italic, outlineThickness ).rsbDelta ); + auto secondLsbDelta = static_cast( + getGlyph( second, characterSize, bold, italic, outlineThickness ).lsbDelta ); // Get the kerning vector FT_Vector kerning; diff --git a/src/eepp/graphics/text.cpp b/src/eepp/graphics/text.cpp index 979d3805d..65ad3b28c 100644 --- a/src/eepp/graphics/text.cpp +++ b/src/eepp/graphics/text.cpp @@ -422,12 +422,13 @@ Float Text::getTextWidth( Font* font, const Uint32& fontSize, const String& stri Uint32 prevChar = 0; bool bold = ( style & Text::Bold ) != 0; bool italic = ( style & Text::Italic ) != 0; - Float hspace = static_cast( font->getGlyph( L' ', fontSize, bold, italic ).advance ); + Float hspace = static_cast( + font->getGlyph( L' ', fontSize, bold, italic, outlineThickness ).advance ); for ( std::size_t i = 0; i < string.size(); ++i ) { rune = string.at( i ); Glyph glyph = font->getGlyph( rune, fontSize, bold, italic, outlineThickness ); if ( rune != '\r' && rune != '\t' ) { - width += font->getKerning( prevChar, rune, fontSize, bold, italic ); + width += font->getKerning( prevChar, rune, fontSize, bold, italic, outlineThickness ); prevChar = rune; width += glyph.advance; } else if ( rune == '\t' ) { @@ -454,7 +455,8 @@ Vector2f Text::findCharacterPos( std::size_t index, Font* font, const Uint32& fo // Precompute the variables needed by the algorithm bool bold = ( style & Text::Bold ) != 0; bool italic = ( style & Italic ) != 0; - Float hspace = static_cast( font->getGlyph( L' ', fontSize, bold, italic ).advance ); + Float hspace = static_cast( + font->getGlyph( L' ', fontSize, bold, italic, outlineThickness ).advance ); Float vspace = static_cast( font->getLineSpacing( fontSize ) ); // Compute the position @@ -464,7 +466,8 @@ Vector2f Text::findCharacterPos( std::size_t index, Font* font, const Uint32& fo String::StringBaseType curChar = string[i]; // Apply the kerning offset - position.x += static_cast( font->getKerning( prevChar, curChar, fontSize, bold, italic ) ); + position.x += static_cast( + font->getKerning( prevChar, curChar, fontSize, bold, italic, outlineThickness ) ); prevChar = curChar; // Handle special characters @@ -485,7 +488,7 @@ Vector2f Text::findCharacterPos( std::size_t index, Font* font, const Uint32& fo // For regular characters, add the advance offset of the glyph position.x += static_cast( - font->getGlyph( curChar, fontSize, bold, outlineThickness ).advance ); + font->getGlyph( curChar, fontSize, bold, italic, outlineThickness ).advance ); } return position; @@ -509,16 +512,17 @@ Int32 Text::findCharacterFromPos( const Vector2i& pos, bool returnNearest, Font* bool italic = ( style & Italic ) != 0; Vector2f fpos( pos.asFloat() ); - Float hspace = static_cast( font->getGlyph( L' ', fontSize, bold, italic ).advance ); + Float hspace = static_cast( + font->getGlyph( L' ', fontSize, bold, italic, outlineThickness ).advance ); for ( std::size_t i = 0; i < tSize; ++i ) { rune = string[i]; - Glyph glyph = font->getGlyph( rune, fontSize, bold, outlineThickness ); + Glyph glyph = font->getGlyph( rune, fontSize, bold, italic, outlineThickness ); lWidth = width; if ( rune != '\r' && rune != '\t' ) { - width += font->getKerning( prevChar, rune, fontSize, bold, italic ); + width += font->getKerning( prevChar, rune, fontSize, bold, italic, outlineThickness ); prevChar = rune; width += glyph.advance; } else if ( rune == '\t' ) { @@ -579,14 +583,16 @@ void Text::getWidthInfo() { mLinesStartIndex.push_back( 0 ); - Float hspace = static_cast( mFont->getGlyph( L' ', mRealFontSize, bold, italic ).advance ); + Float hspace = static_cast( + mFont->getGlyph( L' ', mRealFontSize, bold, italic, mOutlineThickness ).advance ); for ( std::size_t i = 0; i < mString.size(); ++i ) { CharID = static_cast( mString.at( i ) ); - Glyph glyph = mFont->getGlyph( CharID, mRealFontSize, bold, mOutlineThickness ); + Glyph glyph = mFont->getGlyph( CharID, mRealFontSize, bold, italic, mOutlineThickness ); if ( CharID != '\r' && CharID != '\t' ) { - Width += mFont->getKerning( prevChar, CharID, mRealFontSize, bold, italic ); + Width += mFont->getKerning( prevChar, CharID, mRealFontSize, bold, italic, + mOutlineThickness ); prevChar = CharID; Width += glyph.advance; } @@ -635,10 +641,11 @@ void Text::wrapText( const Uint32& maxWidth ) { bool bold = ( mStyle & Bold ) != 0; bool italic = ( mStyle & Italic ) != 0; - Float hspace = static_cast( mFont->getGlyph( L' ', mRealFontSize, bold, italic ).advance ); + Float hspace = static_cast( + mFont->getGlyph( L' ', mRealFontSize, bold, italic, mOutlineThickness ).advance ); while ( *tChar ) { - Glyph pChar = mFont->getGlyph( *tChar, mRealFontSize, bold, mOutlineThickness ); + Glyph pChar = mFont->getGlyph( *tChar, mRealFontSize, bold, italic, mOutlineThickness ); Float fCharWidth = (Float)pChar.advance; @@ -651,7 +658,8 @@ void Text::wrapText( const Uint32& maxWidth ) { tWordWidth += fCharWidth; if ( *tChar != '\r' ) { - tWordWidth += mFont->getKerning( prevChar, *tChar, mRealFontSize, bold, italic ); + tWordWidth += mFont->getKerning( prevChar, *tChar, mRealFontSize, bold, italic, + mOutlineThickness ); prevChar = *tChar; } @@ -926,8 +934,8 @@ void Text::ensureGeometryUpdate() { } // Precompute the variables needed by the algorithm - Float hspace = static_cast( - mFont->getGlyph( L' ', mRealFontSize, bold, reqItalic ).advance ); + Float hspace = + static_cast( mFont->getGlyph( L' ', mRealFontSize, bold, reqItalic ).advance ); Float vspace = static_cast( mFont->getLineSpacing( mRealFontSize ) ); Float x = 0.f; Float y = static_cast( mRealFontSize ); @@ -957,7 +965,8 @@ void Text::ensureGeometryUpdate() { Uint32 curChar = mString[i]; // Apply the kerning offset - x += mFont->getKerning( prevChar, curChar, mRealFontSize, bold, reqItalic ); + x += mFont->getKerning( prevChar, curChar, mRealFontSize, bold, reqItalic, + mOutlineThickness ); prevChar = curChar; // If we're using the underlined style and there's a new line, draw a line @@ -1030,7 +1039,8 @@ void Text::ensureGeometryUpdate() { // Apply the outline if ( mOutlineThickness != 0 ) { - const Glyph& glyph = mFont->getGlyph( curChar, mRealFontSize, bold, mOutlineThickness ); + const Glyph& glyph = + mFont->getGlyph( curChar, mRealFontSize, bold, italic, mOutlineThickness ); Float left = glyph.bounds.Left; Float top = glyph.bounds.Top;