Minor bug fixes in font loading and rendering.

This commit is contained in:
Martín Lucas Golini
2022-05-02 01:24:32 -03:00
parent ed52656988
commit e6c4842a60
9 changed files with 96 additions and 18 deletions

View File

@@ -10,11 +10,11 @@ bool Font::isEmojiCodePoint( const Uint32& codePoint ) {
const Uint32 rangeMax = 131069;
const Uint32 rangeMin2 = 126980;
const Uint32 rangeMax2 = 127569;
const Uint32 rangeMin3 = 8987;
const Uint32 rangeMin3 = 8986;
const Uint32 rangeMax3 = 12953;
return codePoint > 8987 && ( ( rangeMin <= codePoint && codePoint <= rangeMax ) ||
( rangeMin2 <= codePoint && codePoint <= rangeMax2 ) ||
( rangeMin3 <= codePoint && codePoint <= rangeMax3 ) );
return codePoint >= 8986 && ( ( rangeMin <= codePoint && codePoint <= rangeMax ) ||
( rangeMin2 <= codePoint && codePoint <= rangeMax2 ) ||
( rangeMin3 <= codePoint && codePoint <= rangeMax3 ) );
}
bool Font::containsEmojiCodePoint( const String& string ) {

View File

@@ -177,6 +177,10 @@ bool FontBMFont::isMonospace() const {
return mIsMonospace;
}
bool FontBMFont::isScalable() const {
return false;
}
const FontBMFont::Info& FontBMFont::getInfo() const {
return mInfo;
}

View File

@@ -126,6 +126,7 @@ bool FontTrueType::loadFromFile( const std::string& filename ) {
}
mFace = face;
mIsMonospace = FT_IS_FIXED_WIDTH( static_cast<FT_Face>( mFace ) );
mIsColorEmojiFont = checkIsColorEmojiFont( static_cast<FT_Face>( mFace ) );
mIsEmojiFont = FT_Get_Char_Index( static_cast<FT_Face>( mFace ), 0x1F600 ) != 0;
@@ -202,6 +203,7 @@ bool FontTrueType::loadFromMemory( const void* data, std::size_t sizeInBytes, bo
}
mFace = face;
mIsMonospace = FT_IS_FIXED_WIDTH( static_cast<FT_Face>( mFace ) );
mIsColorEmojiFont = checkIsColorEmojiFont( static_cast<FT_Face>( mFace ) );
mIsEmojiFont = FT_Get_Char_Index( static_cast<FT_Face>( mFace ), 0x1F600 ) != 0;
@@ -286,6 +288,7 @@ bool FontTrueType::loadFromStream( IOStream& stream ) {
}
mFace = face;
mIsMonospace = FT_IS_FIXED_WIDTH( static_cast<FT_Face>( mFace ) );
mIsColorEmojiFont = checkIsColorEmojiFont( static_cast<FT_Face>( mFace ) );
mIsEmojiFont = FT_Get_Char_Index( static_cast<FT_Face>( mFace ), 0x1F600 ) != 0;
FT_Stroker stroker = nullptr;
@@ -1015,8 +1018,9 @@ bool FontTrueType::setCurrentSize( unsigned int characterSize ) const {
} else {
return false;
}
} else if ( characterSize != currentSize && face->size->metrics.x_ppem > 0 ) {
return setCurrentSize( it->second );
} else if ( characterSize != currentSize &&
( result = FT_Set_Pixel_Sizes( face, 0, it->second ) ) == FT_Err_Ok ) {
return true;
}
}
}
@@ -1041,6 +1045,10 @@ void FontTrueType::setIsEmojiFont( bool isEmojiFont ) {
mIsEmojiFont = isEmojiFont;
}
void FontTrueType::setForceIsMonospace( bool isMonospace ) {
mIsMonospace = isMonospace;
}
void FontTrueType::setIsColorEmojiFont( bool isColorEmojiFont ) {
mIsColorEmojiFont = isColorEmojiFont;
}
@@ -1050,7 +1058,11 @@ bool FontTrueType::isColorEmojiFont() const {
}
bool FontTrueType::isMonospace() const {
return FT_IS_FIXED_WIDTH( static_cast<FT_Face>( mFace ) );
return mIsMonospace;
}
bool FontTrueType::isScalable() const {
return FT_IS_SCALABLE( static_cast<FT_Face>( mFace ) );
}
bool FontTrueType::isEmojiFont() const {

View File

@@ -120,7 +120,12 @@ Text::Text( const String& string, Font* font, unsigned int characterSize ) :
mFontShadowColor( Color( 0, 0, 0, 255 ) ),
mAlign( 0 ),
mFontHeight( mFont->getFontHeight( mRealFontSize ) ),
mTabWidth( 4 ) {}
mTabWidth( 4 ) {
if ( !mFont->isScalable() ) {
mFontSize = mFontHeight;
mRealFontSize = mFontHeight;
}
}
Text::Text( Font* font, unsigned int characterSize ) :
mFont( font ),
@@ -139,7 +144,12 @@ Text::Text( Font* font, unsigned int characterSize ) :
mFontShadowColor( Color( 0, 0, 0, 255 ) ),
mAlign( 0 ),
mFontHeight( mFont->getFontHeight( mRealFontSize ) ),
mTabWidth( 4 ) {}
mTabWidth( 4 ) {
if ( !mFont->isScalable() ) {
mFontSize = mFontHeight;
mRealFontSize = mFontHeight;
}
}
void Text::create( Font* font, const String& text, Color FontColor, Color FontShadowColor,
Uint32 characterSize ) {
@@ -179,7 +189,10 @@ void Text::setFont( Font* font ) {
mRealFontSize = PixelDensity::dpToPxI( mFontSize );
mFontHeight = mFont->getFontHeight( mRealFontSize );
if ( !mFont->isScalable() ) {
mFontSize = mFontHeight;
mRealFontSize = mFontHeight;
}
mGeometryNeedUpdate = true;
mCachedWidthNeedUpdate = true;
}
@@ -191,6 +204,10 @@ void Text::setFontSize( unsigned int size ) {
mRealFontSize = PixelDensity::dpToPxI( mFontSize );
mFontHeight = mFont->getFontHeight( mRealFontSize );
if ( !mFont->isScalable() ) {
mFontSize = mFontHeight;
mRealFontSize = mFontHeight;
}
mGeometryNeedUpdate = true;
mCachedWidthNeedUpdate = true;

View File

@@ -184,7 +184,7 @@ void App::openFontDialog( std::string& fontPath, bool loadingMonoFont ) {
if ( FileSystem::isRelativePath( absoluteFontPath ) )
absoluteFontPath = mResPath + fontPath;
UIFileDialog* dialog =
UIFileDialog::New( UIFileDialog::DefaultFlags, "*.ttf; *.otf; *.wolff; *.otb",
UIFileDialog::New( UIFileDialog::DefaultFlags, "*.ttf; *.otf; *.wolff; *.otb; *.bdf",
FileSystem::fileRemoveFileName( absoluteFontPath ) );
ModelIndex index = dialog->getMultiView()->getListView()->findRowWithText(
FileSystem::fileNameFromPath( fontPath ), true, true );
@@ -211,11 +211,36 @@ void App::openFontDialog( std::string& fontPath, bool loadingMonoFont ) {
FileSystem::fileRemoveExtension( FileSystem::fileNameFromPath( fontPath ) );
FontTrueType* fontMono = loadFont( fontName, fontPath );
if ( fontMono ) {
mFontMono = fontMono;
mFontMono->setBoldAdvanceSameAsRegular( true );
if ( mEditorSplitter ) {
mEditorSplitter->forEachEditor(
[&]( UICodeEditor* editor ) { editor->setFont( mFontMono ); } );
auto loadMonoFont = [&]( FontTrueType* fontMono ) {
mFontMono = fontMono;
mFontMono->setBoldAdvanceSameAsRegular( true );
mFontMono->setForceIsMonospace( true );
if ( mEditorSplitter ) {
mEditorSplitter->forEachEditor(
[&]( UICodeEditor* editor ) { editor->setFont( mFontMono ); } );
}
};
if ( !fontMono->isMonospace() ) {
auto* msgBox = UIMessageBox::New(
UIMessageBox::YES_NO,
i18n(
"confirm_loading_none_monospace_font",
"The editor only supports monospaced fonts and the selected font isn't "
"flagged as monospace.\nDo you want to load it anyways?" )
.unescape() );
msgBox->addEventListener(
Event::MsgBoxConfirmClick,
[&, loadMonoFont, fontMono]( const Event* ) { loadMonoFont( fontMono ); } );
msgBox->addEventListener( Event::MsgBoxCancelClick, [fontMono]( const Event* ) {
FontManager::instance()->remove( fontMono );
} );
msgBox->addEventListener( Event::OnClose,
[&]( const Event* ) { msgBox = nullptr; } );
msgBox->setTitle( i18n( "confirm_loading_font", "Font loading confirmation" ) );
msgBox->center();
msgBox->showWhenReady();
} else {
loadMonoFont( fontMono );
}
}
}
@@ -2643,6 +2668,8 @@ void App::init( std::string file, const Float& pidelDensity, const std::string&
mWindow = engine->createWindow( winSettings, contextSettings );
if ( mWindow->isOpen() ) {
Log::info( "Window creation took: %.2fms", globalClock.getElapsedTime().asMilliseconds() );
if ( mConfig.window.position != Vector2i( -1, -1 ) &&
mConfig.window.displayIndex < displayManager->getDisplayCount() )
mWindow->setPosition( mConfig.window.position.x, mConfig.window.position.y );
@@ -2678,9 +2705,10 @@ void App::init( std::string file, const Float& pidelDensity, const std::string&
mFont = loadFont( "sans-serif", mConfig.ui.serifFont, "assets/fonts/NotoSans-Regular.ttf" );
mFontMono =
loadFont( "monospace", mConfig.ui.monospaceFont, "assets/fonts/DejaVuSansMono.ttf" );
if ( mFontMono )
if ( mFontMono ) {
mFontMono->setBoldAdvanceSameAsRegular( true );
mFontMono->setForceIsMonospace( true );
}
loadFont( "NotoEmoji-Regular", "assets/fonts/NotoEmoji-Regular.ttf" );
#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN
@@ -2992,6 +3020,8 @@ void App::init( std::string file, const Float& pidelDensity, const std::string&
mInitColorScheme );
mEditorSplitter->setHideTabBarOnSingleTab( mConfig.editor.hideTabBarOnSingleTab );
Log::info( "Base UI took: %.2fms", globalClock.getElapsedTime().asMilliseconds() );
#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN
mFileWatcher = new efsw::FileWatcher();
mFileSystemListener = new FileSystemListener( mEditorSplitter, mFileSystemModel );
@@ -3021,6 +3051,8 @@ void App::init( std::string file, const Float& pidelDensity, const std::string&
mConsole = eeNew( Console, ( mFontMono, true, true, 1024 * 1000, 0, mWindow ) );
Log::info( "Complete UI took: %.2fms", globalClock.getElapsedTime().asMilliseconds() );
#if EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN
if ( file == "./this.program" )
file = "";
@@ -3028,6 +3060,9 @@ void App::init( std::string file, const Float& pidelDensity, const std::string&
initProjectTreeView( file );
Log::info( "Init ProjectTreeView took: %.2fms",
globalClock.getElapsedTime().asMilliseconds() );
#if EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN
if ( file.empty() )
downloadFileWeb( "https://raw.githubusercontent.com/SpartanJ/eepp/develop/README.md" );