From df0f864b737c3b98f376bb49fca0740382f6736e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sat, 30 Sep 2023 21:54:07 -0300 Subject: [PATCH] More performance improvements. Expose String::View. --- include/eepp/core/string.hpp | 4 + include/eepp/graphics/text.hpp | 47 +- include/eepp/system/color.hpp | 5 +- include/eepp/ui/uicodeeditor.hpp | 9 + src/eepp/core/string.cpp | 7 +- src/eepp/graphics/text.cpp | 94 +++- src/eepp/system/color.cpp | 421 +++++++----------- src/eepp/ui/uicodeeditor.cpp | 54 ++- .../autocomplete/autocompleteplugin.cpp | 3 +- 9 files changed, 327 insertions(+), 317 deletions(-) diff --git a/include/eepp/core/string.hpp b/include/eepp/core/string.hpp index 12277494f..24309737e 100644 --- a/include/eepp/core/string.hpp +++ b/include/eepp/core/string.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include namespace EE { @@ -29,6 +30,7 @@ class EE_API String { typedef StringType::reverse_iterator ReverseIterator; //! Reverse Iterator type typedef StringType::const_reverse_iterator ConstReverseIterator; //! Constant iterator type typedef Uint32 HashType; + typedef std::basic_string_view View; static const std::size_t InvalidPos; ///< Represents an invalid position in the string @@ -874,6 +876,8 @@ class EE_API String { */ bool contains( const String& needle ); + String::View view() const; + private: friend EE_API bool operator==( const String& left, const String& right ); friend EE_API bool operator<( const String& left, const String& right ); diff --git a/include/eepp/graphics/text.hpp b/include/eepp/graphics/text.hpp index 0e33bf117..92d22bb95 100644 --- a/include/eepp/graphics/text.hpp +++ b/include/eepp/graphics/text.hpp @@ -27,6 +27,28 @@ class EE_API Text { const Uint32& style, const Uint32& tabWidth = 4, const Float& outlineThickness = 0.f ); + static Float getTextWidth( Font* font, const Uint32& fontSize, const String::View& string, + const Uint32& style, const Uint32& tabWidth = 4, + const Float& outlineThickness = 0.f ); + + static Sizef draw( const String& string, const Vector2f& pos, Font* font, Float fontSize, + const Color& fontColor, Uint32 style = 0, Float outlineThickness = 0.f, + const Color& outlineColor = Color::Black, + const Color& shadowColor = Color::Black, + const Vector2f& shadowOffset = { 1, 1 }, const Uint32& tabWidth = 4 ); + + static Sizef draw( const String& string, const Vector2f& pos, const FontStyleConfig& config, + const Uint32& tabWidth = 4 ); + + static Sizef draw( const String::View& string, const Vector2f& pos, Font* font, Float fontSize, + const Color& fontColor, Uint32 style = 0, Float outlineThickness = 0.f, + const Color& outlineColor = Color::Black, + const Color& shadowColor = Color::Black, + const Vector2f& shadowOffset = { 1, 1 }, const Uint32& tabWidth = 4 ); + + static Sizef draw( const String::View& string, const Vector2f& pos, + const FontStyleConfig& config, const Uint32& tabWidth = 4 ); + static Int32 findCharacterFromPos( const Vector2i& pos, bool returnNearest, Font* font, const Uint32& fontSize, const String& string, const Uint32& style, const Uint32& tabWidth = 4, @@ -44,15 +66,6 @@ class EE_API Text { static Text* New( Font* font, unsigned int characterSize = PixelDensity::dpToPx( 12 ) ); - static Sizef draw( const String& string, const Vector2f& pos, Font* font, Float fontSize, - const Color& fontColor, Uint32 style = 0, Float outlineThickness = 0.f, - const Color& outlineColor = Color::Black, - const Color& shadowColor = Color::Black, - const Vector2f& shadowOffset = { 1, 1 }, const Uint32& tabWidth = 4 ); - - static Sizef draw( const String& string, const Vector2f& pos, const FontStyleConfig& config, - const Uint32& tabWidth = 4 ); - Text(); Text( const String& string, Font* font, @@ -245,6 +258,22 @@ class EE_API Text { const Color& backgroundColor ); void onNewString(); + + template + static Float getTextWidth( Font* font, const Uint32& fontSize, const StringType& string, + const Uint32& style, const Uint32& tabWidth = 4, + const Float& outlineThickness = 0.f ); + + template + static Sizef draw( const StringType& string, const Vector2f& pos, Font* font, Float fontSize, + const Color& fontColor, Uint32 style = 0, Float outlineThickness = 0.f, + const Color& outlineColor = Color::Black, + const Color& shadowColor = Color::Black, + const Vector2f& shadowOffset = { 1, 1 }, const Uint32& tabWidth = 4 ); + + template + static Sizef draw( const StringType& string, const Vector2f& pos, const FontStyleConfig& config, + const Uint32& tabWidth = 4 ); }; }} // namespace EE::Graphics diff --git a/include/eepp/system/color.hpp b/include/eepp/system/color.hpp index 257188bce..7c299f8ad 100644 --- a/include/eepp/system/color.hpp +++ b/include/eepp/system/color.hpp @@ -2,6 +2,7 @@ #define EE_SYSTEMCCOLORS_H #include +#include #include #include #include @@ -366,8 +367,8 @@ class EE_API Color : public tColor { static const Color yellowgreen; private: - static std::map sColors; - static std::map sColorMap; + static UnorderedMap sColors; + static UnorderedMap sColorMap; static void initColorMap(); }; diff --git a/include/eepp/ui/uicodeeditor.hpp b/include/eepp/ui/uicodeeditor.hpp index e8cfed416..34c2fdccc 100644 --- a/include/eepp/ui/uicodeeditor.hpp +++ b/include/eepp/ui/uicodeeditor.hpp @@ -461,6 +461,10 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client { Float getTextWidth( const String& text ) const; + size_t characterWidth( const String::View& str ) const; + + Float getTextWidth( const String::View& text ) const; + Float getLineHeight() const; Float getCharacterSize() const; @@ -902,6 +906,11 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client { bool ignoreSelectionMatch ); void updateHighlightWordCache(); + + template size_t characterWidth( const StringType& str ) const; + + template Float getTextWidth( const StringType& text ) const; + }; }} // namespace EE::UI diff --git a/src/eepp/core/string.cpp b/src/eepp/core/string.cpp index 8ee7de999..c1c5f0959 100644 --- a/src/eepp/core/string.cpp +++ b/src/eepp/core/string.cpp @@ -276,7 +276,8 @@ String::HashType String::hash( const std::string& str ) { } String::HashType String::hash( const String& str ) { - return std::hash{}( str.mString ); + return String::hash( reinterpret_cast( str.mString.data() ), + str.size() * sizeof( String::StringBaseType ) ); } bool String::isCharacter( const int& value ) { @@ -785,6 +786,10 @@ bool String::contains( const String& needle ) { return String::contains( *this, needle ); } +String::View String::view() const { + return String::View( mString ); +} + void String::replace( std::string& target, const std::string& that, const std::string& with ) { std::size_t start_pos = target.find( that ); if ( start_pos == std::string::npos ) diff --git a/src/eepp/graphics/text.cpp b/src/eepp/graphics/text.cpp index d4a33ca2b..cacb937f7 100644 --- a/src/eepp/graphics/text.cpp +++ b/src/eepp/graphics/text.cpp @@ -70,6 +70,44 @@ Uint32 Text::stringToStyleFlag( const std::string& str ) { return flags; } +Float Text::getTextWidth( Font* font, const Uint32& fontSize, const String& string, + const Uint32& style, const Uint32& tabWidth, + const Float& outlineThickness ) { + return getTextWidth( font, fontSize, string, style, tabWidth, outlineThickness ); +} + +Float Text::getTextWidth( Font* font, const Uint32& fontSize, const String::View& string, + const Uint32& style, const Uint32& tabWidth, + const Float& outlineThickness ) { + return getTextWidth( font, fontSize, string, style, tabWidth, outlineThickness ); +} + +Sizef Text::draw( const String& string, const Vector2f& pos, Font* font, Float fontSize, + const Color& fontColor, Uint32 style, Float outlineThickness, + const Color& outlineColor, const Color& shadowColor, const Vector2f& shadowOffset, + const Uint32& tabWidth ) { + return draw( string, pos, font, fontSize, fontColor, style, outlineThickness, + outlineColor, shadowColor, shadowOffset, tabWidth ); +} + +Sizef Text::draw( const String& string, const Vector2f& pos, const FontStyleConfig& config, + const Uint32& tabWidth ) { + return draw( string, pos, config, tabWidth ); +} + +Sizef Text::draw( const String::View& string, const Vector2f& pos, Font* font, Float fontSize, + const Color& fontColor, Uint32 style, Float outlineThickness, + const Color& outlineColor, const Color& shadowColor, const Vector2f& shadowOffset, + const Uint32& tabWidth ) { + return draw( string, pos, font, fontSize, fontColor, style, outlineThickness, + outlineColor, shadowColor, shadowOffset, tabWidth ); +} + +Sizef Text::draw( const String::View& string, const Vector2f& pos, const FontStyleConfig& config, + const Uint32& tabWidth ) { + return draw( string, pos, config, tabWidth ); +} + Text* Text::New() { return eeNew( Text, () ); } @@ -82,7 +120,8 @@ Text* Text::New( Font* font, unsigned int characterSize ) { return eeNew( Text, ( font, characterSize ) ); } -Sizef Text::draw( const String& string, const Vector2f& pos, Font* font, Float fontSize, +template +Sizef Text::draw( const StringType& string, const Vector2f& pos, Font* font, Float fontSize, const Color& fontColor, Uint32 style, Float outlineThickness, const Color& outlineColor, const Color& shadowColor, const Vector2f& shadowOffset, const Uint32& tabWidth ) { @@ -105,20 +144,45 @@ Sizef Text::draw( const String& string, const Vector2f& pos, Font* font, Float f Float top = cpos.y + std::floor( fontSize + underlineOffset - ( underlineThickness / 2 ) + 0.5f ); Float bottom = top + std::floor( underlineThickness + 0.5f ); - Primitives p; - p.setColor( fontColor ); - p.drawRectangle( Rectf( pos.x, top, pos.x + width, bottom ) ); + if ( style & Text::Shadow ) { + BR->quadsSetTexCoord( 0, 0, 1, 1 ); + BR->quadsSetColor( shadowColor ); + BR->batchQuad( Rectf( pos.x + shadowOffset.x, top + shadowOffset.y, + pos.x + width + shadowOffset.x, bottom + +shadowOffset.y ) ); + } + if ( outlineThickness ) { + BR->quadsSetTexCoord( 0, 0, 1, 1 ); + BR->quadsSetColor( outlineColor ); + BR->batchQuad( Rectf( pos.x - outlineThickness, top - outlineThickness, + pos.x + width + outlineThickness, bottom + outlineThickness ) ); + } + BR->quadsSetTexCoord( 0, 0, 1, 1 ); + BR->quadsSetColor( fontColor ); + BR->batchQuad( Rectf( pos.x, top, pos.x + width, bottom ) ); }; auto drawStrikeThrough = [&]() { Rectf xBounds = font->getGlyph( L'x', fontSize, isBold, isItalic ).bounds; - Float strikeThroughOffset = xBounds.Top + xBounds.Bottom / 2.f; + Float strikeThroughOffset = xBounds.Top + xBounds.Bottom * 0.5f; Float underlineThickness = font->getUnderlineThickness( fontSize ); - Float top = cpos.y + strikeThroughOffset; + Float top = std::floor( cpos.y + fontSize + strikeThroughOffset - + ( underlineThickness / 2 ) + 0.5f ); Float bottom = top + std::floor( underlineThickness + 0.5f ); - Primitives p; - p.setColor( fontColor ); - p.drawRectangle( Rectf( pos.x, top, pos.x + width, bottom ) ); + if ( style & Text::Shadow ) { + BR->quadsSetTexCoord( 0, 0, 1, 1 ); + BR->quadsSetColor( shadowColor ); + BR->batchQuad( Rectf( pos.x + shadowOffset.x, top + shadowOffset.y, + pos.x + width + shadowOffset.x, bottom + +shadowOffset.y ) ); + } + if ( outlineThickness ) { + BR->quadsSetTexCoord( 0, 0, 1, 1 ); + BR->quadsSetColor( outlineColor ); + BR->batchQuad( Rectf( pos.x - outlineThickness, top - outlineThickness, + pos.x + width + outlineThickness, bottom + outlineThickness ) ); + } + BR->quadsSetTexCoord( 0, 0, 1, 1 ); + BR->quadsSetColor( fontColor ); + BR->batchQuad( Rectf( pos.x, top, pos.x + width, bottom ) ); }; for ( size_t i = 0; i < ssize; ++i ) { @@ -205,11 +269,12 @@ Sizef Text::draw( const String& string, const Vector2f& pos, Font* font, Float f return size; } -Sizef Text::draw( const String& string, const Vector2f& pos, const FontStyleConfig& config, +template +Sizef Text::draw( const StringType& string, const Vector2f& pos, const FontStyleConfig& config, const Uint32& tabWidth ) { - return draw( string, pos, config.Font, config.CharacterSize, config.FontColor, config.Style, - config.OutlineThickness, config.OutlineColor, config.ShadowColor, - config.ShadowOffset, tabWidth ); + return draw( string, pos, config.Font, config.CharacterSize, config.FontColor, + config.Style, config.OutlineThickness, config.OutlineColor, + config.ShadowColor, config.ShadowOffset, tabWidth ); } Text::Text() {} @@ -450,7 +515,8 @@ void Text::findWordFromCharacterIndex( Int32 characterIndex, Int32& initCur, Int } } -Float Text::getTextWidth( Font* font, const Uint32& fontSize, const String& string, +template +Float Text::getTextWidth( Font* font, const Uint32& fontSize, const StringType& string, const Uint32& style, const Uint32& tabWidth, const Float& outlineThickness ) { if ( NULL == font || string.empty() ) diff --git a/src/eepp/system/color.cpp b/src/eepp/system/color.cpp index 62d0493a7..f4f2f74ad 100644 --- a/src/eepp/system/color.cpp +++ b/src/eepp/system/color.cpp @@ -15,8 +15,8 @@ template inline T _round( T r ) { } // namespace -std::map Color::sColors; -std::map Color::sColorMap; +UnorderedMap Color::sColors; +UnorderedMap Color::sColorMap; // Keep the old defined colors const Color Color::Transparent = Color( 0x00000000 ); @@ -766,275 +766,154 @@ bool Color::validHexColorString( const std::string& hexColor ) { void Color::initColorMap() { if ( !sColorMap.empty() ) return; - sColorMap.insert( - std::pair( String::hash( "transparent" ), Color::Transparent ) ); - sColorMap.insert( - std::pair( String::hash( "aliceblue" ), Color::aliceblue ) ); - sColorMap.insert( - std::pair( String::hash( "antiquewhite" ), Color::antiquewhite ) ); - sColorMap.insert( std::pair( String::hash( "aqua" ), Color::aqua ) ); - sColorMap.insert( - std::pair( String::hash( "aquamarine" ), Color::aquamarine ) ); - sColorMap.insert( std::pair( String::hash( "azure" ), Color::azure ) ); - sColorMap.insert( std::pair( String::hash( "beige" ), Color::beige ) ); - sColorMap.insert( - std::pair( String::hash( "bisque" ), Color::bisque ) ); - sColorMap.insert( std::pair( String::hash( "black" ), Color::black ) ); - sColorMap.insert( std::pair( String::hash( "blanchedalmond" ), - Color::blanchedalmond ) ); - sColorMap.insert( std::pair( String::hash( "blue" ), Color::blue ) ); - sColorMap.insert( - std::pair( String::hash( "blueviolet" ), Color::blueviolet ) ); - sColorMap.insert( std::pair( String::hash( "brown" ), Color::brown ) ); - sColorMap.insert( - std::pair( String::hash( "burlywood" ), Color::burlywood ) ); - sColorMap.insert( - std::pair( String::hash( "cadetblue" ), Color::cadetblue ) ); - sColorMap.insert( - std::pair( String::hash( "chartreuse" ), Color::chartreuse ) ); - sColorMap.insert( - std::pair( String::hash( "chocolate" ), Color::chocolate ) ); - sColorMap.insert( std::pair( String::hash( "coral" ), Color::coral ) ); - sColorMap.insert( std::pair( String::hash( "cornflowerblue" ), - Color::cornflowerblue ) ); - sColorMap.insert( - std::pair( String::hash( "cornsilk" ), Color::cornsilk ) ); - sColorMap.insert( - std::pair( String::hash( "crimson" ), Color::crimson ) ); - sColorMap.insert( std::pair( String::hash( "cyan" ), Color::cyan ) ); - sColorMap.insert( - std::pair( String::hash( "darkblue" ), Color::darkblue ) ); - sColorMap.insert( - std::pair( String::hash( "darkcyan" ), Color::darkcyan ) ); - sColorMap.insert( std::pair( String::hash( "darkgoldenrod" ), - Color::darkgoldenrod ) ); - sColorMap.insert( - std::pair( String::hash( "darkgray" ), Color::darkgray ) ); - sColorMap.insert( - std::pair( String::hash( "darkgreen" ), Color::darkgreen ) ); - sColorMap.insert( - std::pair( String::hash( "darkgrey" ), Color::darkgrey ) ); - sColorMap.insert( - std::pair( String::hash( "darkkhaki" ), Color::darkkhaki ) ); - sColorMap.insert( - std::pair( String::hash( "darkmagenta" ), Color::darkmagenta ) ); - sColorMap.insert( std::pair( String::hash( "darkolivegreen" ), - Color::darkolivegreen ) ); - sColorMap.insert( - std::pair( String::hash( "darkorange" ), Color::darkorange ) ); - sColorMap.insert( - std::pair( String::hash( "darkorchid" ), Color::darkorchid ) ); - sColorMap.insert( - std::pair( String::hash( "darkred" ), Color::darkred ) ); - sColorMap.insert( - std::pair( String::hash( "darksalmon" ), Color::darksalmon ) ); - sColorMap.insert( - std::pair( String::hash( "darkseagreen" ), Color::darkseagreen ) ); - sColorMap.insert( std::pair( String::hash( "darkslateblue" ), - Color::darkslateblue ) ); - sColorMap.insert( std::pair( String::hash( "darkslategray" ), - Color::darkslategray ) ); - sColorMap.insert( std::pair( String::hash( "darkslategrey" ), - Color::darkslategrey ) ); - sColorMap.insert( std::pair( String::hash( "darkturquoise" ), - Color::darkturquoise ) ); - sColorMap.insert( - std::pair( String::hash( "darkviolet" ), Color::darkviolet ) ); - sColorMap.insert( - std::pair( String::hash( "deeppink" ), Color::deeppink ) ); - sColorMap.insert( - std::pair( String::hash( "deepskyblue" ), Color::deepskyblue ) ); - sColorMap.insert( - std::pair( String::hash( "dimgray" ), Color::dimgray ) ); - sColorMap.insert( - std::pair( String::hash( "dimgrey" ), Color::dimgrey ) ); - sColorMap.insert( - std::pair( String::hash( "dodgerblue" ), Color::dodgerblue ) ); - sColorMap.insert( - std::pair( String::hash( "firebrick" ), Color::firebrick ) ); - sColorMap.insert( - std::pair( String::hash( "floralwhite" ), Color::floralwhite ) ); - sColorMap.insert( - std::pair( String::hash( "forestgreen" ), Color::forestgreen ) ); - sColorMap.insert( - std::pair( String::hash( "fuchsia" ), Color::fuchsia ) ); - sColorMap.insert( - std::pair( String::hash( "gainsboro" ), Color::gainsboro ) ); - sColorMap.insert( - std::pair( String::hash( "ghostwhite" ), Color::ghostwhite ) ); - sColorMap.insert( std::pair( String::hash( "gold" ), Color::gold ) ); - sColorMap.insert( - std::pair( String::hash( "goldenrod" ), Color::goldenrod ) ); - sColorMap.insert( std::pair( String::hash( "gray" ), Color::gray ) ); - sColorMap.insert( std::pair( String::hash( "green" ), Color::green ) ); - sColorMap.insert( - std::pair( String::hash( "greenyellow" ), Color::greenyellow ) ); - sColorMap.insert( std::pair( String::hash( "grey" ), Color::grey ) ); - sColorMap.insert( - std::pair( String::hash( "honeydew" ), Color::honeydew ) ); - sColorMap.insert( - std::pair( String::hash( "hotpink" ), Color::hotpink ) ); - sColorMap.insert( - std::pair( String::hash( "indianred" ), Color::indianred ) ); - sColorMap.insert( - std::pair( String::hash( "indigo" ), Color::indigo ) ); - sColorMap.insert( std::pair( String::hash( "ivory" ), Color::ivory ) ); - sColorMap.insert( std::pair( String::hash( "khaki" ), Color::khaki ) ); - sColorMap.insert( - std::pair( String::hash( "lavender" ), Color::lavender ) ); - sColorMap.insert( std::pair( String::hash( "lavenderblush" ), - Color::lavenderblush ) ); - sColorMap.insert( - std::pair( String::hash( "lawngreen" ), Color::lawngreen ) ); - sColorMap.insert( - std::pair( String::hash( "lemonchiffon" ), Color::lemonchiffon ) ); - sColorMap.insert( - std::pair( String::hash( "lightblue" ), Color::lightblue ) ); - sColorMap.insert( - std::pair( String::hash( "lightcoral" ), Color::lightcoral ) ); - sColorMap.insert( - std::pair( String::hash( "lightcyan" ), Color::lightcyan ) ); - sColorMap.insert( std::pair( String::hash( "lightgoldenrodyellow" ), - Color::lightgoldenrodyellow ) ); - sColorMap.insert( - std::pair( String::hash( "lightgray" ), Color::lightgray ) ); - sColorMap.insert( - std::pair( String::hash( "lightgreen" ), Color::lightgreen ) ); - sColorMap.insert( - std::pair( String::hash( "lightgrey" ), Color::lightgrey ) ); - sColorMap.insert( - std::pair( String::hash( "lightpink" ), Color::lightpink ) ); - sColorMap.insert( - std::pair( String::hash( "lightsalmon" ), Color::lightsalmon ) ); - sColorMap.insert( std::pair( String::hash( "lightseagreen" ), - Color::lightseagreen ) ); - sColorMap.insert( - std::pair( String::hash( "lightskyblue" ), Color::lightskyblue ) ); - sColorMap.insert( std::pair( String::hash( "lightslategray" ), - Color::lightslategray ) ); - sColorMap.insert( std::pair( String::hash( "lightslategrey" ), - Color::lightslategrey ) ); - sColorMap.insert( std::pair( String::hash( "lightsteelblue" ), - Color::lightsteelblue ) ); - sColorMap.insert( - std::pair( String::hash( "lightyellow" ), Color::lightyellow ) ); - sColorMap.insert( std::pair( String::hash( "lime" ), Color::lime ) ); - sColorMap.insert( - std::pair( String::hash( "limegreen" ), Color::limegreen ) ); - sColorMap.insert( std::pair( String::hash( "linen" ), Color::linen ) ); - sColorMap.insert( - std::pair( String::hash( "magenta" ), Color::magenta ) ); - sColorMap.insert( - std::pair( String::hash( "maroon" ), Color::maroon ) ); - sColorMap.insert( std::pair( String::hash( "mediumaquamarine" ), - Color::mediumaquamarine ) ); - sColorMap.insert( - std::pair( String::hash( "mediumblue" ), Color::mediumblue ) ); - sColorMap.insert( - std::pair( String::hash( "mediumorchid" ), Color::mediumorchid ) ); - sColorMap.insert( - std::pair( String::hash( "mediumpurple" ), Color::mediumpurple ) ); - sColorMap.insert( std::pair( String::hash( "mediumseagreen" ), - Color::mediumseagreen ) ); - sColorMap.insert( std::pair( String::hash( "mediumslateblue" ), - Color::mediumslateblue ) ); - sColorMap.insert( std::pair( String::hash( "mediumspringgreen" ), - Color::mediumspringgreen ) ); - sColorMap.insert( std::pair( String::hash( "mediumturquoise" ), - Color::mediumturquoise ) ); - sColorMap.insert( std::pair( String::hash( "mediumvioletred" ), - Color::mediumvioletred ) ); - sColorMap.insert( - std::pair( String::hash( "midnightblue" ), Color::midnightblue ) ); - sColorMap.insert( - std::pair( String::hash( "mintcream" ), Color::mintcream ) ); - sColorMap.insert( - std::pair( String::hash( "mistyrose" ), Color::mistyrose ) ); - sColorMap.insert( - std::pair( String::hash( "moccasin" ), Color::moccasin ) ); - sColorMap.insert( - std::pair( String::hash( "navajowhite" ), Color::navajowhite ) ); - sColorMap.insert( std::pair( String::hash( "navy" ), Color::navy ) ); - sColorMap.insert( - std::pair( String::hash( "oldlace" ), Color::oldlace ) ); - sColorMap.insert( std::pair( String::hash( "olive" ), Color::olive ) ); - sColorMap.insert( - std::pair( String::hash( "olivedrab" ), Color::olivedrab ) ); - sColorMap.insert( - std::pair( String::hash( "orange" ), Color::orange ) ); - sColorMap.insert( - std::pair( String::hash( "orangered" ), Color::orangered ) ); - sColorMap.insert( - std::pair( String::hash( "orchid" ), Color::orchid ) ); - sColorMap.insert( std::pair( String::hash( "palegoldenrod" ), - Color::palegoldenrod ) ); - sColorMap.insert( - std::pair( String::hash( "palegreen" ), Color::palegreen ) ); - sColorMap.insert( std::pair( String::hash( "paleturquoise" ), - Color::paleturquoise ) ); - sColorMap.insert( std::pair( String::hash( "palevioletred" ), - Color::palevioletred ) ); - sColorMap.insert( - std::pair( String::hash( "papayawhip" ), Color::papayawhip ) ); - sColorMap.insert( - std::pair( String::hash( "peachpuff" ), Color::peachpuff ) ); - sColorMap.insert( std::pair( String::hash( "peru" ), Color::peru ) ); - sColorMap.insert( std::pair( String::hash( "pink" ), Color::pink ) ); - sColorMap.insert( std::pair( String::hash( "plum" ), Color::plum ) ); - sColorMap.insert( - std::pair( String::hash( "powderblue" ), Color::powderblue ) ); - sColorMap.insert( - std::pair( String::hash( "purple" ), Color::purple ) ); - sColorMap.insert( std::pair( String::hash( "red" ), Color::red ) ); - sColorMap.insert( - std::pair( String::hash( "rosybrown" ), Color::rosybrown ) ); - sColorMap.insert( - std::pair( String::hash( "royalblue" ), Color::royalblue ) ); - sColorMap.insert( - std::pair( String::hash( "saddlebrown" ), Color::saddlebrown ) ); - sColorMap.insert( - std::pair( String::hash( "salmon" ), Color::salmon ) ); - sColorMap.insert( - std::pair( String::hash( "sandybrown" ), Color::sandybrown ) ); - sColorMap.insert( - std::pair( String::hash( "seagreen" ), Color::seagreen ) ); - sColorMap.insert( - std::pair( String::hash( "seashell" ), Color::seashell ) ); - sColorMap.insert( - std::pair( String::hash( "sienna" ), Color::sienna ) ); - sColorMap.insert( - std::pair( String::hash( "silver" ), Color::silver ) ); - sColorMap.insert( - std::pair( String::hash( "skyblue" ), Color::skyblue ) ); - sColorMap.insert( - std::pair( String::hash( "slateblue" ), Color::slateblue ) ); - sColorMap.insert( - std::pair( String::hash( "slategray" ), Color::slategray ) ); - sColorMap.insert( - std::pair( String::hash( "slategrey" ), Color::slategrey ) ); - sColorMap.insert( std::pair( String::hash( "snow" ), Color::snow ) ); - sColorMap.insert( - std::pair( String::hash( "springgreen" ), Color::springgreen ) ); - sColorMap.insert( - std::pair( String::hash( "steelblue" ), Color::steelblue ) ); - sColorMap.insert( std::pair( String::hash( "tan" ), Color::tan ) ); - sColorMap.insert( std::pair( String::hash( "teal" ), Color::teal ) ); - sColorMap.insert( - std::pair( String::hash( "thistle" ), Color::thistle ) ); - sColorMap.insert( - std::pair( String::hash( "tomato" ), Color::tomato ) ); - sColorMap.insert( - std::pair( String::hash( "turquoise" ), Color::turquoise ) ); - sColorMap.insert( - std::pair( String::hash( "violet" ), Color::violet ) ); - sColorMap.insert( std::pair( String::hash( "wheat" ), Color::wheat ) ); - sColorMap.insert( std::pair( String::hash( "white" ), Color::white ) ); - sColorMap.insert( - std::pair( String::hash( "whitesmoke" ), Color::whitesmoke ) ); - sColorMap.insert( - std::pair( String::hash( "yellow" ), Color::yellow ) ); - sColorMap.insert( - std::pair( String::hash( "yellowgreen" ), Color::yellowgreen ) ); + sColorMap.insert( { String::hash( "transparent" ), Color::Transparent } ); + sColorMap.insert( { String::hash( "aliceblue" ), Color::aliceblue } ); + sColorMap.insert( { String::hash( "antiquewhite" ), Color::antiquewhite } ); + sColorMap.insert( { String::hash( "aqua" ), Color::aqua } ); + sColorMap.insert( { String::hash( "aquamarine" ), Color::aquamarine } ); + sColorMap.insert( { String::hash( "azure" ), Color::azure } ); + sColorMap.insert( { String::hash( "beige" ), Color::beige } ); + sColorMap.insert( { String::hash( "bisque" ), Color::bisque } ); + sColorMap.insert( { String::hash( "black" ), Color::black } ); + sColorMap.insert( { String::hash( "blanchedalmond" ), Color::blanchedalmond } ); + sColorMap.insert( { String::hash( "blue" ), Color::blue } ); + sColorMap.insert( { String::hash( "blueviolet" ), Color::blueviolet } ); + sColorMap.insert( { String::hash( "brown" ), Color::brown } ); + sColorMap.insert( { String::hash( "burlywood" ), Color::burlywood } ); + sColorMap.insert( { String::hash( "cadetblue" ), Color::cadetblue } ); + sColorMap.insert( { String::hash( "chartreuse" ), Color::chartreuse } ); + sColorMap.insert( { String::hash( "chocolate" ), Color::chocolate } ); + sColorMap.insert( { String::hash( "coral" ), Color::coral } ); + sColorMap.insert( { String::hash( "cornflowerblue" ), Color::cornflowerblue } ); + sColorMap.insert( { String::hash( "cornsilk" ), Color::cornsilk } ); + sColorMap.insert( { String::hash( "crimson" ), Color::crimson } ); + sColorMap.insert( { String::hash( "cyan" ), Color::cyan } ); + sColorMap.insert( { String::hash( "darkblue" ), Color::darkblue } ); + sColorMap.insert( { String::hash( "darkcyan" ), Color::darkcyan } ); + sColorMap.insert( { String::hash( "darkgoldenrod" ), Color::darkgoldenrod } ); + sColorMap.insert( { String::hash( "darkgray" ), Color::darkgray } ); + sColorMap.insert( { String::hash( "darkgreen" ), Color::darkgreen } ); + sColorMap.insert( { String::hash( "darkgrey" ), Color::darkgrey } ); + sColorMap.insert( { String::hash( "darkkhaki" ), Color::darkkhaki } ); + sColorMap.insert( { String::hash( "darkmagenta" ), Color::darkmagenta } ); + sColorMap.insert( { String::hash( "darkolivegreen" ), Color::darkolivegreen } ); + sColorMap.insert( { String::hash( "darkorange" ), Color::darkorange } ); + sColorMap.insert( { String::hash( "darkorchid" ), Color::darkorchid } ); + sColorMap.insert( { String::hash( "darkred" ), Color::darkred } ); + sColorMap.insert( { String::hash( "darksalmon" ), Color::darksalmon } ); + sColorMap.insert( { String::hash( "darkseagreen" ), Color::darkseagreen } ); + sColorMap.insert( { String::hash( "darkslateblue" ), Color::darkslateblue } ); + sColorMap.insert( { String::hash( "darkslategray" ), Color::darkslategray } ); + sColorMap.insert( { String::hash( "darkslategrey" ), Color::darkslategrey } ); + sColorMap.insert( { String::hash( "darkturquoise" ), Color::darkturquoise } ); + sColorMap.insert( { String::hash( "darkviolet" ), Color::darkviolet } ); + sColorMap.insert( { String::hash( "deeppink" ), Color::deeppink } ); + sColorMap.insert( { String::hash( "deepskyblue" ), Color::deepskyblue } ); + sColorMap.insert( { String::hash( "dimgray" ), Color::dimgray } ); + sColorMap.insert( { String::hash( "dimgrey" ), Color::dimgrey } ); + sColorMap.insert( { String::hash( "dodgerblue" ), Color::dodgerblue } ); + sColorMap.insert( { String::hash( "firebrick" ), Color::firebrick } ); + sColorMap.insert( { String::hash( "floralwhite" ), Color::floralwhite } ); + sColorMap.insert( { String::hash( "forestgreen" ), Color::forestgreen } ); + sColorMap.insert( { String::hash( "fuchsia" ), Color::fuchsia } ); + sColorMap.insert( { String::hash( "gainsboro" ), Color::gainsboro } ); + sColorMap.insert( { String::hash( "ghostwhite" ), Color::ghostwhite } ); + sColorMap.insert( { String::hash( "gold" ), Color::gold } ); + sColorMap.insert( { String::hash( "goldenrod" ), Color::goldenrod } ); + sColorMap.insert( { String::hash( "gray" ), Color::gray } ); + sColorMap.insert( { String::hash( "green" ), Color::green } ); + sColorMap.insert( { String::hash( "greenyellow" ), Color::greenyellow } ); + sColorMap.insert( { String::hash( "grey" ), Color::grey } ); + sColorMap.insert( { String::hash( "honeydew" ), Color::honeydew } ); + sColorMap.insert( { String::hash( "hotpink" ), Color::hotpink } ); + sColorMap.insert( { String::hash( "indianred" ), Color::indianred } ); + sColorMap.insert( { String::hash( "indigo" ), Color::indigo } ); + sColorMap.insert( { String::hash( "ivory" ), Color::ivory } ); + sColorMap.insert( { String::hash( "khaki" ), Color::khaki } ); + sColorMap.insert( { String::hash( "lavender" ), Color::lavender } ); + sColorMap.insert( { String::hash( "lavenderblush" ), Color::lavenderblush } ); + sColorMap.insert( { String::hash( "lawngreen" ), Color::lawngreen } ); + sColorMap.insert( { String::hash( "lemonchiffon" ), Color::lemonchiffon } ); + sColorMap.insert( { String::hash( "lightblue" ), Color::lightblue } ); + sColorMap.insert( { String::hash( "lightcoral" ), Color::lightcoral } ); + sColorMap.insert( { String::hash( "lightcyan" ), Color::lightcyan } ); + sColorMap.insert( { String::hash( "lightgoldenrodyellow" ), Color::lightgoldenrodyellow } ); + sColorMap.insert( { String::hash( "lightgray" ), Color::lightgray } ); + sColorMap.insert( { String::hash( "lightgreen" ), Color::lightgreen } ); + sColorMap.insert( { String::hash( "lightgrey" ), Color::lightgrey } ); + sColorMap.insert( { String::hash( "lightpink" ), Color::lightpink } ); + sColorMap.insert( { String::hash( "lightsalmon" ), Color::lightsalmon } ); + sColorMap.insert( { String::hash( "lightseagreen" ), Color::lightseagreen } ); + sColorMap.insert( { String::hash( "lightskyblue" ), Color::lightskyblue } ); + sColorMap.insert( { String::hash( "lightslategray" ), Color::lightslategray } ); + sColorMap.insert( { String::hash( "lightslategrey" ), Color::lightslategrey } ); + sColorMap.insert( { String::hash( "lightsteelblue" ), Color::lightsteelblue } ); + sColorMap.insert( { String::hash( "lightyellow" ), Color::lightyellow } ); + sColorMap.insert( { String::hash( "lime" ), Color::lime } ); + sColorMap.insert( { String::hash( "limegreen" ), Color::limegreen } ); + sColorMap.insert( { String::hash( "linen" ), Color::linen } ); + sColorMap.insert( { String::hash( "magenta" ), Color::magenta } ); + sColorMap.insert( { String::hash( "maroon" ), Color::maroon } ); + sColorMap.insert( { String::hash( "mediumaquamarine" ), Color::mediumaquamarine } ); + sColorMap.insert( { String::hash( "mediumblue" ), Color::mediumblue } ); + sColorMap.insert( { String::hash( "mediumorchid" ), Color::mediumorchid } ); + sColorMap.insert( { String::hash( "mediumpurple" ), Color::mediumpurple } ); + sColorMap.insert( { String::hash( "mediumseagreen" ), Color::mediumseagreen } ); + sColorMap.insert( { String::hash( "mediumslateblue" ), Color::mediumslateblue } ); + sColorMap.insert( { String::hash( "mediumspringgreen" ), Color::mediumspringgreen } ); + sColorMap.insert( { String::hash( "mediumturquoise" ), Color::mediumturquoise } ); + sColorMap.insert( { String::hash( "mediumvioletred" ), Color::mediumvioletred } ); + sColorMap.insert( { String::hash( "midnightblue" ), Color::midnightblue } ); + sColorMap.insert( { String::hash( "mintcream" ), Color::mintcream } ); + sColorMap.insert( { String::hash( "mistyrose" ), Color::mistyrose } ); + sColorMap.insert( { String::hash( "moccasin" ), Color::moccasin } ); + sColorMap.insert( { String::hash( "navajowhite" ), Color::navajowhite } ); + sColorMap.insert( { String::hash( "navy" ), Color::navy } ); + sColorMap.insert( { String::hash( "oldlace" ), Color::oldlace } ); + sColorMap.insert( { String::hash( "olive" ), Color::olive } ); + sColorMap.insert( { String::hash( "olivedrab" ), Color::olivedrab } ); + sColorMap.insert( { String::hash( "orange" ), Color::orange } ); + sColorMap.insert( { String::hash( "orangered" ), Color::orangered } ); + sColorMap.insert( { String::hash( "orchid" ), Color::orchid } ); + sColorMap.insert( { String::hash( "palegoldenrod" ), Color::palegoldenrod } ); + sColorMap.insert( { String::hash( "palegreen" ), Color::palegreen } ); + sColorMap.insert( { String::hash( "paleturquoise" ), Color::paleturquoise } ); + sColorMap.insert( { String::hash( "palevioletred" ), Color::palevioletred } ); + sColorMap.insert( { String::hash( "papayawhip" ), Color::papayawhip } ); + sColorMap.insert( { String::hash( "peachpuff" ), Color::peachpuff } ); + sColorMap.insert( { String::hash( "peru" ), Color::peru } ); + sColorMap.insert( { String::hash( "pink" ), Color::pink } ); + sColorMap.insert( { String::hash( "plum" ), Color::plum } ); + sColorMap.insert( { String::hash( "powderblue" ), Color::powderblue } ); + sColorMap.insert( { String::hash( "purple" ), Color::purple } ); + sColorMap.insert( { String::hash( "red" ), Color::red } ); + sColorMap.insert( { String::hash( "rosybrown" ), Color::rosybrown } ); + sColorMap.insert( { String::hash( "royalblue" ), Color::royalblue } ); + sColorMap.insert( { String::hash( "saddlebrown" ), Color::saddlebrown } ); + sColorMap.insert( { String::hash( "salmon" ), Color::salmon } ); + sColorMap.insert( { String::hash( "sandybrown" ), Color::sandybrown } ); + sColorMap.insert( { String::hash( "seagreen" ), Color::seagreen } ); + sColorMap.insert( { String::hash( "seashell" ), Color::seashell } ); + sColorMap.insert( { String::hash( "sienna" ), Color::sienna } ); + sColorMap.insert( { String::hash( "silver" ), Color::silver } ); + sColorMap.insert( { String::hash( "skyblue" ), Color::skyblue } ); + sColorMap.insert( { String::hash( "slateblue" ), Color::slateblue } ); + sColorMap.insert( { String::hash( "slategray" ), Color::slategray } ); + sColorMap.insert( { String::hash( "slategrey" ), Color::slategrey } ); + sColorMap.insert( { String::hash( "snow" ), Color::snow } ); + sColorMap.insert( { String::hash( "springgreen" ), Color::springgreen } ); + sColorMap.insert( { String::hash( "steelblue" ), Color::steelblue } ); + sColorMap.insert( { String::hash( "tan" ), Color::tan } ); + sColorMap.insert( { String::hash( "teal" ), Color::teal } ); + sColorMap.insert( { String::hash( "thistle" ), Color::thistle } ); + sColorMap.insert( { String::hash( "tomato" ), Color::tomato } ); + sColorMap.insert( { String::hash( "turquoise" ), Color::turquoise } ); + sColorMap.insert( { String::hash( "violet" ), Color::violet } ); + sColorMap.insert( { String::hash( "wheat" ), Color::wheat } ); + sColorMap.insert( { String::hash( "white" ), Color::white } ); + sColorMap.insert( { String::hash( "whitesmoke" ), Color::whitesmoke } ); + sColorMap.insert( { String::hash( "yellow" ), Color::yellow } ); + sColorMap.insert( { String::hash( "yellowgreen" ), Color::yellowgreen } ); } }} // namespace EE::System diff --git a/src/eepp/ui/uicodeeditor.cpp b/src/eepp/ui/uicodeeditor.cpp index 42810d8d4..2c1dbd9bb 100644 --- a/src/eepp/ui/uicodeeditor.cpp +++ b/src/eepp/ui/uicodeeditor.cpp @@ -1826,7 +1826,7 @@ Float UICodeEditor::getXOffsetCol( const TextPosition& position ) const { return x; } -size_t UICodeEditor::characterWidth( const String& str ) const { +template size_t UICodeEditor::characterWidth( const StringType& str ) const { Int64 cc = str.size(); Int64 count = 0; for ( Int64 i = 0; i < cc; i++ ) @@ -1834,7 +1834,23 @@ size_t UICodeEditor::characterWidth( const String& str ) const { return count; } -Float UICodeEditor::getTextWidth( const String& line ) const { +size_t UICodeEditor::characterWidth( const String& str ) const { + return characterWidth( str ); +} + +Float UICodeEditor::getTextWidth( const String& text ) const { + return getTextWidth( text ); +} + +size_t UICodeEditor::characterWidth( const String::View& str ) const { + return characterWidth( str ); +} + +Float UICodeEditor::getTextWidth( const String::View& text ) const { + return getTextWidth( text ); +} + +template Float UICodeEditor::getTextWidth( const StringType& line ) const { if ( mFont && !mFont->isMonospace() ) { return Text::getTextWidth( mFont, getCharacterSize(), line, mFontStyleConfig.Style, mTabWidth ); @@ -2858,23 +2874,23 @@ void UICodeEditor::drawLineText( const Int64& line, Vector2f position, const Flo ttf->setEnableEmojiFallback( false ); } - String buff; + String::View buff; Sizef size; FontStyleConfig fontStyle( mFontStyleConfig ); fontStyle.CharacterSize = fontSize; for ( const auto& token : tokens ) { - const String* text = &strLine; - if ( pos < strLine.size() && !( pos == 0 && text->size() == token.len ) ) { - buff = strLine.substr( pos, token.len ); - text = &buff; + String::View text = strLine.view(); + if ( pos < strLine.size() && !( pos == 0 && text.size() == token.len ) ) { + buff = strLine.view().substr( pos, token.len ); + text = buff; } pos += token.len; - Float textWidth = isMonospace ? getTextWidth( *text ) : 0; + Float textWidth = isMonospace ? getTextWidth( text ) : 0; if ( !isMonospace || ( position.x + textWidth >= mScreenPos.x && position.x <= mScreenPos.x + mSize.getWidth() ) ) { - Int64 curCharsWidth = text->size(); + Int64 curCharsWidth = text.size(); Int64 curPositionChar = eefloor( mScroll.x / getGlyphWidth() ); Float curMaxPositionChar = curPositionChar + maxWidth; const SyntaxColorScheme::Style& style = mColorScheme.getSyntaxStyle( token.type ); @@ -2885,10 +2901,10 @@ void UICodeEditor::drawLineText( const Int64& line, Vector2f position, const Flo mLinkPosition.start().line() == line ) { if ( mLinkPosition.start().column() >= curChar && mLinkPosition.end().column() <= curChar + curCharsWidth ) { - size_t linkPos = text->find( mLink ); + size_t linkPos = text.find( mLink.view() ); if ( linkPos != String::InvalidPos ) { - String beforeString( text->substr( 0, linkPos ) ); - String afterString( text->substr( linkPos + mLink.size() ) ); + String::View beforeString( text.substr( 0, linkPos ) ); + String::View afterString( text.substr( linkPos + mLink.size() ) ); Float offset = 0.f; Uint32 lineStyle = fontStyle.Style; @@ -2953,7 +2969,7 @@ void UICodeEditor::drawLineText( const Int64& line, Vector2f position, const Flo textWidth = offset; position.x += textWidth; - curChar += characterWidth( *text ); + curChar += characterWidth( text ); continue; } } @@ -2973,7 +2989,7 @@ void UICodeEditor::drawLineText( const Int64& line, Vector2f position, const Flo Int64 end = eemin( totalChars, minimumCharsToCoverScreen ); if ( curCharsWidth >= charsToVisible ) { size = Text::draw( - text->substr( start, end ), + text.substr( start, end ), { position.x + start * getGlyphWidth(), position.y + lineOffset }, fontStyle, mTabWidth ); if ( minimumCharsToCoverScreen == end ) @@ -2981,11 +2997,11 @@ void UICodeEditor::drawLineText( const Int64& line, Vector2f position, const Flo } } else { size = - Text::draw( text->substr( 0, eemin( curCharsWidth, maxWidth ) ), + Text::draw( text.substr( 0, eemin( curCharsWidth, maxWidth ) ), { position.x, position.y + lineOffset }, fontStyle, mTabWidth ); } } else { - size = Text::draw( *text, { position.x, position.y + lineOffset }, fontStyle, + size = Text::draw( text, { position.x, position.y + lineOffset }, fontStyle, mTabWidth ); } @@ -2996,7 +3012,7 @@ void UICodeEditor::drawLineText( const Int64& line, Vector2f position, const Flo } position.x += textWidth; - curChar += characterWidth( *text ); + curChar += characterWidth( text ); } if ( mDoc->mightBeBinary() && mFont->getType() == FontType::TTF ) { @@ -3087,7 +3103,7 @@ void UICodeEditor::drawColorPreview( const Vector2f& startScroll, const Float& l void UICodeEditor::drawWhitespaces( const std::pair& lineRange, const Vector2f& startScroll, const Float& lineHeight ) { - Float tabWidth = getTextWidth( "\t" ); + Float tabWidth = getTextWidth( String( "\t" ) ); Float glyphW = getGlyphWidth(); Color color( Color( mWhitespaceColor ).blendAlpha( mAlpha ) ); unsigned int fontSize = getCharacterSize(); @@ -3151,7 +3167,7 @@ void UICodeEditor::drawIndentationGuides( const std::pair& lineRange, p.setForceDraw( false ); Float w = eefloor( PixelDensity::dpToPx( 1 ) ); String idt( mDoc->getIndentString() ); - int spaceW = getTextWidth( " " ); + int spaceW = getTextWidth( String( " " ) ); p.setColor( Color( mWhitespaceColor ).blendAlpha( mAlpha ) ); int indentSize = mDoc->getIndentType() == TextDocument::IndentType::IndentTabs ? getTabWidth() diff --git a/src/tools/ecode/plugins/autocomplete/autocompleteplugin.cpp b/src/tools/ecode/plugins/autocomplete/autocompleteplugin.cpp index 3c4675b53..92f97658c 100644 --- a/src/tools/ecode/plugins/autocomplete/autocompleteplugin.cpp +++ b/src/tools/ecode/plugins/autocomplete/autocompleteplugin.cpp @@ -739,7 +739,8 @@ void AutoCompletePlugin::postDraw( UICodeEditor* editor, const Vector2f& startSc eemin( mSuggestionsStartIndex + mSuggestionsMaxVisible, suggestions.size() ); for ( size_t i = mSuggestionsStartIndex; i < maxIndex; i++ ) - largestString = eemax( largestString, editor->getTextWidth( suggestions[i].text ) ); + largestString = + eemax( largestString, editor->getTextWidth( String{ suggestions[i].text } ) ); Sizef bar( PixelDensity::dpToPxI( 6 ), eemax( PixelDensity::dpToPx( 8 ),