From de634c6f3e1324d3ad9b85ea23c6247ce633ecf4 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Mon, 5 Feb 2024 15:33:03 +0000 Subject: [PATCH] Generate tofu if we fail to decompose outline font glyph. --- common/font/outline_decomposer.cpp | 8 ++++---- common/font/outline_font.cpp | 31 +++++++++++++++++++++++++++++- include/font/outline_decomposer.h | 2 +- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/common/font/outline_decomposer.cpp b/common/font/outline_decomposer.cpp index 0c66d8e854..83c8fcc5b7 100644 --- a/common/font/outline_decomposer.cpp +++ b/common/font/outline_decomposer.cpp @@ -122,7 +122,7 @@ int OUTLINE_DECOMPOSER::cubicTo( const FT_Vector* aFirstControlPoint, } -void OUTLINE_DECOMPOSER::OutlineToSegments( std::vector* aContours ) +bool OUTLINE_DECOMPOSER::OutlineToSegments( std::vector* aContours ) { m_contours = aContours; @@ -138,12 +138,12 @@ void OUTLINE_DECOMPOSER::OutlineToSegments( std::vector* aContours ) FT_Error e = FT_Outline_Decompose( &m_outline, &callbacks, this ); if( e ) - { - // TODO: handle error != 0 - } + return false; for( CONTOUR& c : *m_contours ) c.m_Winding = winding( c.m_Points ); + + return true; } diff --git a/common/font/outline_font.cpp b/common/font/outline_font.cpp index e569701c89..263aa0686b 100644 --- a/common/font/outline_font.cpp +++ b/common/font/outline_font.cpp @@ -328,7 +328,36 @@ VECTOR2I OUTLINE_FONT::getTextAsGlyphsUnlocked( BOX2I* aBBox, std::vector contours; OUTLINE_DECOMPOSER decomposer( face->glyph->outline ); - decomposer.OutlineToSegments( &contours ); + + if( !decomposer.OutlineToSegments( &contours ) ) + { + double hb_advance = glyphPos[i].x_advance * GLYPH_SIZE_SCALER; + BOX2D tofuBox( { scaler * 0.03, 0.0 }, + { hb_advance - scaler * 0.02, scaler * 0.72 } ); + + contours.clear(); + + CONTOUR outline; + outline.m_Winding = 1; + outline.m_Orientation = FT_ORIENTATION_TRUETYPE; + outline.m_Points.push_back( tofuBox.GetPosition() ); + outline.m_Points.push_back( { tofuBox.GetSize().x, tofuBox.GetPosition().y } ); + outline.m_Points.push_back( tofuBox.GetSize() ); + outline.m_Points.push_back( { tofuBox.GetPosition().x, tofuBox.GetSize().y } ); + contours.push_back( outline ); + + CONTOUR hole; + tofuBox.Move( { scaler * 0.06, scaler * 0.06 } ); + tofuBox.SetSize( { tofuBox.GetWidth() - scaler * 0.06, + tofuBox.GetHeight() - scaler * 0.06 } ); + hole.m_Winding = 1; + hole.m_Orientation = FT_ORIENTATION_NONE; + hole.m_Points.push_back( tofuBox.GetPosition() ); + hole.m_Points.push_back( { tofuBox.GetSize().x, tofuBox.GetPosition().y } ); + hole.m_Points.push_back( tofuBox.GetSize() ); + hole.m_Points.push_back( { tofuBox.GetPosition().x, tofuBox.GetSize().y } ); + contours.push_back( hole ); + } std::unique_ptr glyph = std::make_unique(); std::vector holes; diff --git a/include/font/outline_decomposer.h b/include/font/outline_decomposer.h index 0b8f07ab0d..8838f7d911 100644 --- a/include/font/outline_decomposer.h +++ b/include/font/outline_decomposer.h @@ -54,7 +54,7 @@ class OUTLINE_DECOMPOSER public: OUTLINE_DECOMPOSER( FT_Outline& aOutline ); - void OutlineToSegments( std::vector* aContours ); + bool OutlineToSegments( std::vector* aContours ); private: void newContour();