diff --git a/include/eepp/graphics/fonttruetype.hpp b/include/eepp/graphics/fonttruetype.hpp index bb18b05d8..81d64cbdf 100644 --- a/include/eepp/graphics/fonttruetype.hpp +++ b/include/eepp/graphics/fonttruetype.hpp @@ -85,7 +85,7 @@ class EE_API FontTrueType : public Font { const Uint32& getFontInternalId() const; - bool getEnableFallbackFont() const; + bool isFallbackFontEnabled() const; void setEnableFallbackFont( bool enableFallbackFont ); diff --git a/include/eepp/ui/doc/textdocument.hpp b/include/eepp/ui/doc/textdocument.hpp index 32125fefd..ba8fc71e4 100644 --- a/include/eepp/ui/doc/textdocument.hpp +++ b/include/eepp/ui/doc/textdocument.hpp @@ -555,6 +555,10 @@ class EE_API TextDocument { String getWordInPosition(); + bool mightBeBinary() const; + + void setMightBeBinary( bool mightBeBinary ); + protected: friend class UndoStack; @@ -581,6 +585,7 @@ class EE_API TextDocument { bool mDirtyOnFileSystem{ false }; bool mSaving{ false }; bool mDeleteOnClose{ false }; + bool mMightBeBinary{ false }; std::vector> mAutoCloseBracketsPairs; Uint32 mIndentWidth{ 4 }; IndentType mIndentType{ IndentType::IndentTabs }; diff --git a/src/eepp/graphics/fonttruetype.cpp b/src/eepp/graphics/fonttruetype.cpp index 8318295dd..72ac1b636 100644 --- a/src/eepp/graphics/fonttruetype.cpp +++ b/src/eepp/graphics/fonttruetype.cpp @@ -1112,7 +1112,7 @@ void FontTrueType::setEnableDynamicMonospace( bool enableDynamicMonospace ) { mEnableDynamicMonospace = enableDynamicMonospace; } -bool FontTrueType::getEnableFallbackFont() const { +bool FontTrueType::isFallbackFontEnabled() const { return mEnableFallbackFont; } diff --git a/src/eepp/ui/doc/textdocument.cpp b/src/eepp/ui/doc/textdocument.cpp index b15721e83..0d74c54ad 100644 --- a/src/eepp/ui/doc/textdocument.cpp +++ b/src/eepp/ui/doc/textdocument.cpp @@ -63,6 +63,10 @@ bool TextDocument::isUntitledEmpty() const { } void TextDocument::reset() { + mMightBeBinary = false; + mIsBOM = false; + mDirtyOnFileSystem = false; + mSaving = false; mFilePath = mDefaultFileName; mFileURI = URI( "file://" + mFilePath ); mFileRealPath = FileInfo(); @@ -153,6 +157,8 @@ TextDocument::LoadStatus TextDocument::loadFromStream( IOStream& file, std::stri } else if ( lastChar == '\r' ) { mLineEnding = LineEnding::CR; } + + mMightBeBinary = lineBuffer.find_first_of( '\0' ) != String::InvalidPos; } if ( mLineEnding == LineEnding::CRLF && lineBufferSize > 1 && @@ -1588,6 +1594,14 @@ String TextDocument::getWordInPosition() { return getWordInPosition( getSelection().start() ); } +bool TextDocument::mightBeBinary() const { + return mMightBeBinary; +} + +void TextDocument::setMightBeBinary( bool mightBeBinary ) { + mMightBeBinary = mightBeBinary; +} + void TextDocument::selectWord( bool withMulticursor ) { if ( !hasSelection() ) { setSelection( { nextWordBoundary( getSelection().start(), false ), diff --git a/src/eepp/ui/uicodeeditor.cpp b/src/eepp/ui/uicodeeditor.cpp index a69832dea..2ba8d12cd 100644 --- a/src/eepp/ui/uicodeeditor.cpp +++ b/src/eepp/ui/uicodeeditor.cpp @@ -2213,7 +2213,7 @@ void UICodeEditor::checkMatchingBrackets() { } Int64 UICodeEditor::getColFromXOffset( Int64 lineNumber, const Float& x ) const { - if ( x <= 0 || !mFont ) + if ( x <= 0 || !mFont || mDoc->isLoading() ) return 0; TextPosition pos = mDoc->sanitizePosition( TextPosition( lineNumber, 0 ) ); @@ -2696,8 +2696,17 @@ void UICodeEditor::drawLineText( const Int64& line, Vector2f position, const Flo Int64 curChar = 0; Int64 maxWidth = eeceil( mSize.getWidth() / getGlyphWidth() + 1 ); bool isMonospace = mFont->isMonospace(); + bool isFallbackFont = false; + bool isEmojiFallbackFont = false; Float lineOffset = getLineOffset(); size_t pos = 0; + if ( mDoc->mightBeBinary() && mFont->getType() == FontType::TTF ) { + FontTrueType* ttf = static_cast( mFont ); + isFallbackFont = ttf->isFallbackFontEnabled(); + isEmojiFallbackFont = ttf->isEmojiFallbackEnabled(); + ttf->setEnableFallbackFont( false ); + ttf->setEnableEmojiFallback( false ); + } for ( const auto& token : tokens ) { String text( pos < strLine.size() ? strLine.substr( pos, token.len ) : String() ); pos += token.len; @@ -2831,6 +2840,12 @@ void UICodeEditor::drawLineText( const Int64& line, Vector2f position, const Flo position.x += textWidth; curChar += characterWidth( text ); } + + if ( mDoc->mightBeBinary() && mFont->getType() == FontType::TTF ) { + FontTrueType* ttf = static_cast( mFont ); + ttf->setEnableFallbackFont( isFallbackFont ); + ttf->setEnableEmojiFallback( isEmojiFallbackFont ); + } } void UICodeEditor::drawTextRange( const TextRange& range, const std::pair& lineRange, @@ -3063,7 +3078,7 @@ static bool checkHexa( const std::string& hexStr ) { } void UICodeEditor::checkMouseOverColor( const Vector2i& position ) { - if ( !mColorPreview ) + if ( !mColorPreview || mDoc->isLoading() ) return; TextPosition pos( resolveScreenPosition( position.asFloat() ) ); const String& line = mDoc->line( pos.line() ).getText();