Skip to content

Commit

Permalink
Snap to pixels option in arranger
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Nov 13, 2024
1 parent 5b5ec5e commit 734b0f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
17 changes: 14 additions & 3 deletions swGraphicAPI/Assets/TextAsset/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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' )
Expand Down
3 changes: 3 additions & 0 deletions swGraphicAPI/Assets/TextAsset/Text.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -57,6 +58,7 @@ class TextArranger
, UseKerning( true )
, WrapText( false )
, CutOutOfBounds( false )
, SnapToPixel( true )
, Bounds( { 0.0f, 0.0f, 0.0f, 0.0f } )
{}

Expand All @@ -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 );
Expand Down

0 comments on commit 734b0f4

Please sign in to comment.