diff --git a/swGraphicAPI/Assets/TextAsset/Text.cpp b/swGraphicAPI/Assets/TextAsset/Text.cpp index 804754f1..8815b921 100644 --- a/swGraphicAPI/Assets/TextAsset/Text.cpp +++ b/swGraphicAPI/Assets/TextAsset/Text.cpp @@ -47,6 +47,8 @@ std::vector< Position2d > TextArranger::ArrangeText( const std::wstring& text, letters.insert( letters.end(), line.begin(), line.end() ); translate = Position2d( this->Bounds.Left, translate.y - this->Interline * layout.NewLineSize() ); + // @todo Should check bottom edge of the text, not advance position. + // It should take into account that letters have different sizes and stick out in different way. if( this->CutOutOfBounds && translate.y < this->Bounds.Bottom ) break; } @@ -140,14 +142,14 @@ void TextArranger::ApplyAlignement( const FontLayout& layout, std { float offset = remainingSpace / 2.0f; for( auto& letter : letters ) - letter.x += offset; + letter.x = MoveToPixelGrid( offset + letter.x ); break; } case TextAlignment::Right: { for( auto& letter : letters ) - letter.x += remainingSpace; + letter.x = MoveToPixelGrid( remainingSpace + letter.x ); break; } @@ -174,7 +176,7 @@ void TextArranger::ApplyAlignement( const FontLayout& layout, std if( IsWhitespace( text[ i ] ) ) curOffset += offsetPerSpace; - letters[ i ].x += curOffset; + letters[ i ].x = MoveToPixelGrid( curOffset + letters[ i ].x ); } break; } @@ -204,6 +206,15 @@ float TextArranger::TextWidth( const std::vector< Position2d >& le // ================================ // +float TextArranger::MoveToPixelGrid( float value ) const +{ + if( this->SnapToPixel ) + return std::round( value ); + return value; +} + +// ================================ // + bool TextArranger::IsWhitespace ( wchar_t character ) { if( character == L' ' || character == L'\n' || character == L'\r' ) diff --git a/swGraphicAPI/Assets/TextAsset/Text.h b/swGraphicAPI/Assets/TextAsset/Text.h index f0501aca..96443d10 100644 --- a/swGraphicAPI/Assets/TextAsset/Text.h +++ b/swGraphicAPI/Assets/TextAsset/Text.h @@ -46,6 +46,7 @@ class TextArranger bool WrapText; /**Text crossing lower Bound won't be generated.*/ bool CutOutOfBounds; + bool SnapToPixel; //< Avoids letters blurring if they are not aligned to pixel grid. Rect2d Bounds; public: @@ -57,6 +58,7 @@ class TextArranger , UseKerning( true ) , WrapText( false ) , CutOutOfBounds( false ) + , SnapToPixel( true ) , Bounds( { 0.0f, 0.0f, 0.0f, 0.0f } ) {} @@ -69,6 +71,7 @@ class TextArranger void ApplyAlignement( const FontLayout& layout, std::vector< Position2d >& letters, std::wstring_view text ) const; float TextWidth( const std::vector< Position2d >& letters, const std::wstring& text, const FontLayout& layout ) const; + float MoveToPixelGrid( float value ) const; static bool IsWhitespace( wchar_t character ); static bool IsNewline( wchar_t character );