Skip to content

Commit

Permalink
Fixed an old UILinearLayout bug when using layout weights.
Browse files Browse the repository at this point in the history
Fixed a performance regression in line wrapping when a monospace font contains fallback glyphs from other fonts.
Fixes in code editor horizontal scroll.
  • Loading branch information
SpartanJ committed Feb 28, 2025
1 parent a91fde6 commit 8867c87
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 46 deletions.
10 changes: 5 additions & 5 deletions include/eepp/graphics/text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ class EE_API Text {

static Float getTextWidth( Font* font, const Uint32& fontSize, const String& string,
const Uint32& style, const Uint32& tabWidth = 4,
const Float& outlineThickness = 0.f );
const Float& outlineThickness = 0.f, Uint32 textDrawHints = 0 );

static Float getTextWidth( Font* font, const Uint32& fontSize, const String::View& string,
const Uint32& style, const Uint32& tabWidth = 4,
const Float& outlineThickness = 0.f );
const Float& outlineThickness = 0.f, Uint32 textDrawHints = 0 );

static Float getTextWidth( const String& string, const FontStyleConfig& config,
const Uint32& tabWidth = 4 );
const Uint32& tabWidth = 4, Uint32 textDrawHints = 0 );

static Float getTextWidth( const String::View& string, const FontStyleConfig& config,
const Uint32& tabWidth = 4 );
const Uint32& tabWidth = 4, Uint32 textDrawHints = 0 );

static Sizef draw( const String& string, const Vector2f& pos, Font* font, Float fontSize,
const Color& fontColor, Uint32 style = 0, Float outlineThickness = 0.f,
Expand Down Expand Up @@ -325,7 +325,7 @@ class EE_API Text {
template <typename StringType>
static Float getTextWidth( Font* font, const Uint32& fontSize, const StringType& string,
const Uint32& style, const Uint32& tabWidth = 4,
const Float& outlineThickness = 0.f );
const Float& outlineThickness = 0.f, Uint32 textDrawHints = 0 );

template <typename StringType>
static Sizef
Expand Down
6 changes: 4 additions & 2 deletions include/eepp/ui/doc/documentview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ class EE_API DocumentView {
static LineWrapInfo
computeLineBreaks( const String::View& string, const FontStyleConfig& fontStyle, Float maxWidth,
LineWrapMode mode, bool keepIndentation, Uint32 tabWidth = 4,
Float whiteSpaceWidth = 0.f /* 0 = should calculate it */ );
Float whiteSpaceWidth = 0.f /* 0 = should calculate it */,
bool allAscii = false );

static LineWrapInfo computeLineBreaks( const String& string, const FontStyleConfig& fontStyle,
Float maxWidth, LineWrapMode mode, bool keepIndentation,
Uint32 tabWidth = 4, Float whiteSpaceWidth = 0.f );
Uint32 tabWidth = 4, Float whiteSpaceWidth = 0.f,
bool allAscii = false );

static LineWrapInfo computeLineBreaks( const TextDocument& doc, size_t line,
const FontStyleConfig& fontStyle, Float maxWidth,
Expand Down
26 changes: 16 additions & 10 deletions src/eepp/graphics/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,26 +216,28 @@ Uint32 Text::stringToStyleFlag( const std::string& str ) {

Float Text::getTextWidth( Font* font, const Uint32& fontSize, const String& string,
const Uint32& style, const Uint32& tabWidth,
const Float& outlineThickness ) {
return getTextWidth<String>( font, fontSize, string, style, tabWidth, outlineThickness );
const Float& outlineThickness, Uint32 textDrawHints ) {
return getTextWidth<String>( font, fontSize, string, style, tabWidth, outlineThickness,
textDrawHints );
}

Float Text::getTextWidth( Font* font, const Uint32& fontSize, const String::View& string,
const Uint32& style, const Uint32& tabWidth,
const Float& outlineThickness ) {
return getTextWidth<String::View>( font, fontSize, string, style, tabWidth, outlineThickness );
const Float& outlineThickness, Uint32 textDrawHints ) {
return getTextWidth<String::View>( font, fontSize, string, style, tabWidth, outlineThickness,
textDrawHints );
}

Float Text::getTextWidth( const String& string, const FontStyleConfig& config,
const Uint32& tabWidth ) {
const Uint32& tabWidth, Uint32 textDrawHints ) {
return getTextWidth<String>( config.Font, config.CharacterSize, string, config.Style, tabWidth,
config.OutlineThickness );
config.OutlineThickness, textDrawHints );
}

Float Text::getTextWidth( const String::View& string, const FontStyleConfig& config,
const Uint32& tabWidth ) {
const Uint32& tabWidth, Uint32 textDrawHints ) {
return getTextWidth<String::View>( config.Font, config.CharacterSize, string, config.Style,
tabWidth, config.OutlineThickness );
tabWidth, config.OutlineThickness, textDrawHints );
}

Sizef Text::draw( const String& string, const Vector2f& pos, Font* font, Float fontSize,
Expand Down Expand Up @@ -1001,7 +1003,7 @@ void Text::findWordFromCharacterIndex( Int32 characterIndex, Int32& initCur, Int
template <typename StringType>
Float Text::getTextWidth( Font* font, const Uint32& fontSize, const StringType& string,
const Uint32& style, const Uint32& tabWidth,
const Float& outlineThickness ) {
const Float& outlineThickness, Uint32 textDrawHints ) {
if ( NULL == font || string.empty() )
return 0;
Float width = 0;
Expand All @@ -1010,10 +1012,14 @@ Float Text::getTextWidth( Font* font, const Uint32& fontSize, const StringType&
Uint32 prevChar = 0;
bool bold = ( style & Text::Bold ) != 0;
bool italic = ( style & Text::Italic ) != 0;
bool isMonospace = font && ( font->isMonospace() ||
( font->getType() == FontType::TTF &&
static_cast<FontTrueType*>( font )->isIdentifiedAsMonospace() &&
( textDrawHints & DrawHints::AllAscii ) != 0 ) );
Float hspace = static_cast<Float>(
font->getGlyph( L' ', fontSize, bold, italic, outlineThickness ).advance );

if ( font->isMonospace() ) {
if ( isMonospace ) {
size_t len = string.length();
Float width = 0;
Float maxWidth = 0;
Expand Down
21 changes: 14 additions & 7 deletions src/eepp/ui/doc/documentview.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <eepp/graphics/fonttruetype.hpp>
#include <eepp/graphics/text.hpp>
#include <eepp/system/log.hpp>
#include <eepp/system/luapattern.hpp>
Expand Down Expand Up @@ -54,7 +55,8 @@ Float DocumentView::computeOffsets( const String::View& string, const FontStyleC
static const String sepSpaces = " \t\n\v\f\r";
auto nonIndentPos = string.find_first_not_of( sepSpaces.data() );
if ( nonIndentPos != String::View::npos ) {
Float w = Text::getTextWidth( string.substr( 0, nonIndentPos ), fontStyle, tabWidth );
Float w = Text::getTextWidth( string.substr( 0, nonIndentPos ), fontStyle, tabWidth,
Text::DrawHints::AllAscii );
return maxWidth != 0.f ? ( w > maxWidth ? 0.f : w ) : w;
}
return 0.f;
Expand All @@ -64,7 +66,7 @@ DocumentView::LineWrapInfo DocumentView::computeLineBreaks( const String::View&
const FontStyleConfig& fontStyle,
Float maxWidth, LineWrapMode mode,
bool keepIndentation, Uint32 tabWidth,
Float whiteSpaceWidth ) {
Float whiteSpaceWidth, bool allAscii ) {
LineWrapInfo info;
info.wraps.push_back( 0 );
if ( string.empty() || nullptr == fontStyle.Font || mode == LineWrapMode::NoWrap )
Expand All @@ -86,7 +88,11 @@ DocumentView::LineWrapInfo DocumentView::computeLineBreaks( const String::View&

Float xoffset = 0.f;
Float lastWidth = 0.f;
bool isMonospace = fontStyle.Font->isMonospace();
bool isMonospace =
fontStyle.Font &&
( fontStyle.Font->isMonospace() ||
( fontStyle.Font->getType() == FontType::TTF &&
static_cast<FontTrueType*>( fontStyle.Font )->isIdentifiedAsMonospace() && allAscii ) );

size_t lastSpace = 0;
Uint32 prevChar = 0;
Expand Down Expand Up @@ -134,19 +140,20 @@ DocumentView::LineWrapInfo DocumentView::computeLineBreaks( const String& string
const FontStyleConfig& fontStyle,
Float maxWidth, LineWrapMode mode,
bool keepIndentation, Uint32 tabWidth,
Float whiteSpaceWidth ) {
Float whiteSpaceWidth, bool allAscii ) {
return computeLineBreaks( string.view(), fontStyle, maxWidth, mode, keepIndentation, tabWidth,
whiteSpaceWidth );
whiteSpaceWidth, allAscii );
}

DocumentView::LineWrapInfo DocumentView::computeLineBreaks( const TextDocument& doc, size_t line,
const FontStyleConfig& fontStyle,
Float maxWidth, LineWrapMode mode,
bool keepIndentation, Uint32 tabWidth,
Float whiteSpaceWidth ) {
const auto& text = doc.line( line ).getText();
const auto& docLine = doc.line( line );
const auto& text = docLine.getText();
return computeLineBreaks( text.view().substr( 0, text.size() - 1 ), fontStyle, maxWidth, mode,
keepIndentation, tabWidth, whiteSpaceWidth );
keepIndentation, tabWidth, whiteSpaceWidth, docLine.isAscii() );
}

DocumentView::DocumentView( std::shared_ptr<TextDocument> doc, FontStyleConfig fontStyle,
Expand Down
19 changes: 10 additions & 9 deletions src/eepp/ui/uicodeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void UICodeEditor::draw() {
mLoader->setVisible( false );
}

if ( mDoc->isLoading() )
if ( mDoc->isLoading() || mSize.getWidth() == 0 )
return;

if ( mDirtyEditor )
Expand Down Expand Up @@ -981,6 +981,8 @@ void UICodeEditor::setLineWrapMode( LineWrapMode mode ) {
mDocView.setLineWrapMode( mode );
if ( prevMode != mode ) {
scrollToCursor();

mLongestLineWidth = 0;
invalidateLongestLineWidth();
}
}
Expand Down Expand Up @@ -1901,15 +1903,15 @@ void UICodeEditor::drawCursor( const Vector2f& startScroll, const Float& lineHei
void UICodeEditor::onSizeChange() {
invalidateEditor( false );
invalidateLineWrapMaxWidth( false );
if ( mDocView.isWrapEnabled() )
if ( !mDocView.isWrapEnabled() )
invalidateLongestLineWidth();
UIWidget::onSizeChange();
}

void UICodeEditor::onPaddingChange() {
invalidateEditor( false );
invalidateLineWrapMaxWidth( false );
if ( mDocView.isWrapEnabled() )
if ( !mDocView.isWrapEnabled() )
invalidateLongestLineWidth();
UIWidget::onPaddingChange();
}
Expand Down Expand Up @@ -1947,10 +1949,10 @@ Float UICodeEditor::getLineWidth( const Int64& docLine ) {

if ( mDocView.isWrappedLine( docLine ) ) {
auto vline = mDocView.getVisibleLineInfo( docLine );
auto& line = mDoc->line( docLine ).getText();
const auto& line = mDoc->line( docLine ).getText();
Float width = 0;

if ( isNotMonospace() ) {
if ( !isMonospaceLine ) {
auto& line = mDoc->line( docLine );
auto found = mLinesWidthCache.find( docLine );
if ( found != mLinesWidthCache.end() && line.getHash() == found->second.first )
Expand All @@ -1966,14 +1968,14 @@ Float UICodeEditor::getLineWidth( const Int64& docLine ) {
width = eemax( width, curWidth );
}

if ( isNotMonospace() ) {
if ( !isMonospaceLine ) {
mLinesWidthCache[docLine] = { line.getHash(), width };
}

return width;
}

if ( isNotMonospace() ) {
if ( !isMonospaceLine ) {
auto& line = mDoc->line( docLine );
auto found = mLinesWidthCache.find( docLine );
if ( found != mLinesWidthCache.end() && line.getHash() == found->second.first )
Expand Down Expand Up @@ -2005,8 +2007,7 @@ void UICodeEditor::updateScrollBar() {
Float viewPortWidth = getViewportWidth();
mHScrollBar->setPageStep( viewPortWidth / mLongestLineWidth );
mHScrollBar->setClickStep( 0.2f );
bool showHScroll = mLongestLineWidth > viewPortWidth && !mDocView.isWrapEnabled() &&
!mLongestLineWidthDirty;
bool showHScroll = mLongestLineWidth > viewPortWidth && !mDocView.isWrapEnabled();
mHScrollBar->setEnabled( showHScroll );
mHScrollBar->setVisible( showHScroll );
}
Expand Down
19 changes: 6 additions & 13 deletions src/eepp/ui/uilinearlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,9 @@ void UILinearLayout::packVertical() {
Vector2f pos( mPaddingPx.Left, curY );

if ( widget->getLayoutWeight() != 0 ) {
Int32 totSize =
( getLayoutHeightPolicy() == SizePolicy::MatchParent )
? getPixelsSize().getHeight() - mPaddingPx.Top - mPaddingPx.Bottom
: getParent()->getPixelsSize().getHeight() - mLayoutMarginPx.Bottom -
mLayoutMarginPx.Top - mPaddingPx.Top - mPaddingPx.Bottom;
Float newSize =
eeceil( totSize - freeSize.getHeight() ) * widget->getLayoutWeight();
Float totSize = getPixelsSize().getHeight() - mPaddingPx.Top - mPaddingPx.Bottom;
Float newSize = eemax(
eeceil( totSize - freeSize.getHeight() ) * widget->getLayoutWeight(), 0.f );

widget->setPixelsSize( widget->getPixelsSize().getWidth(), newSize );
}
Expand Down Expand Up @@ -340,12 +336,9 @@ void UILinearLayout::packHorizontal() {
Vector2f pos( curX, mPaddingPx.Top );

if ( widget->getLayoutWeight() != 0 ) {
Int32 totSize =
( getLayoutWidthPolicy() == SizePolicy::MatchParent )
? getPixelsSize().getWidth() - mPaddingPx.Left - mPaddingPx.Right
: getParent()->getPixelsSize().getWidth() - mLayoutMarginPx.Right -
mLayoutMarginPx.Left - mPaddingPx.Left - mPaddingPx.Right;
Float newSize = eeceil( totSize - freeSize.getWidth() ) * widget->getLayoutWeight();
Float totSize = getPixelsSize().getWidth() - mPaddingPx.Left - mPaddingPx.Right;
Float newSize = eemax(
eeceil( totSize - freeSize.getWidth() ) * widget->getLayoutWeight(), 0.f );

widget->setPixelsSize( newSize, widget->getPixelsSize().getHeight() );
}
Expand Down

0 comments on commit 8867c87

Please sign in to comment.