22#include <harfbuzz/hb.h>
23#include <harfbuzz/hb-ft.h>
30#include FT_SFNT_NAMES_H
31#include FT_TRUETYPE_TABLES_H
59 TT_OS2* os2 =
reinterpret_cast<TT_OS2*
>( FT_Get_Sfnt_Table(
m_face, FT_SFNT_OS2 ) );
66 if( os2->fsType & FT_FSTYPE_BITMAP_EMBEDDING_ONLY )
73 FT_UShort embeddingBits = os2->fsType & 0x000F;
76 if( embeddingBits == FT_FSTYPE_INSTALLABLE_EMBEDDING )
80 if( embeddingBits & FT_FSTYPE_EDITABLE_EMBEDDING )
84 if( embeddingBits & FT_FSTYPE_PREVIEW_AND_PRINT_EMBEDDING )
93 const std::vector<wxString>* aEmbeddedFiles,
94 bool aForDrawingSheet )
96 std::unique_ptr<OUTLINE_FONT> font = std::make_unique<OUTLINE_FONT>();
103 fc::FF_RESULT retval =
Fontconfig()->
FindFont( aFontName, fontFile, faceIndex, aBold, aItalic,
106 if( retval == fc::FF_RESULT::FF_ERROR )
109 if( retval == fc::FF_RESULT::FF_MISSING_BOLD || retval == fc::FF_RESULT::FF_MISSING_BOLD_ITAL )
112 if( retval == fc::FF_RESULT::FF_MISSING_ITAL || retval == fc::FF_RESULT::FF_MISSING_BOLD_ITAL )
115 if( font->loadFace( fontFile, faceIndex ) != 0 )
118 font->m_fontName = aFontName;
119 font->m_fontFileName = fontFile;
120 font->m_forDrawingSheet = aForDrawingSheet;
122 return font.release();
130 FT_Error e = FT_New_Face(
m_freeType, aFontFileName.mb_str( wxConvUTF8 ), aFaceIndex, &
m_face );
152 if( FT_Select_Charmap( aFace, FT_ENCODING_UNICODE ) == 0 )
157 static const FT_ULong probes[] = {
'A',
'a',
'0',
' ' };
159 for( FT_ULong codepoint : probes )
161 if( FT_Get_Char_Index( aFace, codepoint ) != 0 )
168 if( FT_Select_Charmap( aFace, FT_ENCODING_MS_SYMBOL ) == 0 )
172 FT_Select_Charmap( aFace, FT_ENCODING_UNICODE );
178 double glyphToFontHeight = 1.0;
183 glyphToFontHeight =
static_cast<double>(
GetFace()->height )
184 /
static_cast<double>(
GetFace()->units_per_EM );
186 return aFontMetrics.
GetInterline( aGlyphHeight * glyphToFontHeight );
194 case FT_ORIENTATION_TRUETYPE:
return c.
m_Winding == 1;
195 case FT_ORIENTATION_POSTSCRIPT:
return c.
m_Winding == -1;
196 default:
return false;
214 for(
const std::unique_ptr<KIFONT::GLYPH>& glyph : aGlyphs )
216 BOX2D bbox = glyph->BoundingBox();
219 if( minX > bbox.
GetX() )
222 if( minY > bbox.
GetY() )
240 const wxString& aText,
const VECTOR2I& aPosition,
242 const METRICS& aFontMetrics )
const
244 wxArrayString strings;
245 std::vector<VECTOR2I> positions;
246 std::vector<VECTOR2I> extents;
252 getLinePositions( aText, aPosition, strings, positions, extents, aAttrs, aFontMetrics );
254 for(
size_t i = 0; i < strings.GetCount(); i++ )
256 (void)
drawMarkup(
nullptr, aGlyphs, strings.Item( i ), positions[i], aAttrs.
m_Size,
263 const wxString& aText,
const VECTOR2I& aSize,
265 bool aMirror,
const VECTOR2I& aOrigin,
270 constexpr double TAB_WIDTH = 4 * 0.6;
278 aBBox->
SetEnd( aPosition );
281 for( wxUniChar c : aText )
286 if( !textRun.IsEmpty() )
288 position =
getTextAsGlyphs( aBBox, aGlyphs, textRun, aSize, position, aAngle,
289 aMirror, aOrigin, aTextStyle );
293 int tabWidth =
KiROUND( aSize.
x * TAB_WIDTH );
294 int currentIntrusion = ( position.
x - aOrigin.
x ) % tabWidth;
296 position.
x += tabWidth - currentIntrusion;
304 if( !textRun.IsEmpty() )
306 position =
getTextAsGlyphs( aBBox, aGlyphs, textRun, aSize, position, aAngle, aMirror,
307 aOrigin, aTextStyle );
315 const wxString& aText,
const VECTOR2I& aSize,
317 bool aMirror,
const VECTOR2I& aOrigin,
323 aOrigin, aTextStyle );
365 static std::unordered_map<HARFBUZZ_CACHE_KEY, HARFBUZZ_CACHE_ENTRY> s_harfbuzzCache;
367 std::string textUtf8 =
UTF8( aText );
374 hb_buffer_t* buf = hb_buffer_create();
375 hb_buffer_add_utf8( buf, textUtf8.c_str(), -1, 0, -1 );
376 hb_buffer_guess_segment_properties( buf );
379 hb_font_t* referencedFont = hb_ft_font_create_referenced( aFace );
381 hb_shape( referencedFont, buf,
nullptr, 0 );
383 unsigned int glyphCount;
384 hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( buf, &glyphCount );
385 hb_glyph_position_t* glyphPos = hb_buffer_get_glyph_positions( buf, &glyphCount );
387 entry.
m_GlyphInfo.assign( glyphInfo, glyphInfo + glyphCount );
391 hb_buffer_destroy( buf );
392 hb_font_destroy( referencedFont );
440 std::vector<CONTOUR>& aContours )
const
448 const float angle = (float) ( -
M_PI * 12.0f ) / 180.0f;
449 matrix.xx = (FT_Fixed) ( cos( angle ) * 0x10000L );
450 matrix.xy = (FT_Fixed) ( -sin( angle ) * 0x10000L );
452 matrix.yy = 0x10000L;
454 FT_Set_Transform(
m_face, &matrix,
nullptr );
460 if( FT_Load_Glyph(
m_face, aGlyphIndex, FT_LOAD_NO_BITMAP ) != 0 )
464 if(
m_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE )
468 FT_Outline_Embolden( &
m_face->glyph->outline, 1 << 6 );
483 std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
484 const wxString& aText,
const VECTOR2I& aSize,
486 bool aMirror,
const VECTOR2I& aOrigin,
502 unsigned int glyphCount =
static_cast<unsigned int>( hbShape.
m_GlyphInfo.size() );
503 const hb_glyph_info_t* glyphInfo = hbShape.
m_GlyphInfo.data();
512 aGlyphs->reserve( glyphCount );
516 static std::unordered_map<GLYPH_CACHE_KEY, GLYPH_DATA> s_glyphCache;
518 for(
unsigned int i = 0; i < glyphCount; i++ )
533 BOX2D tofuBox( { scaler * 0.03, 0.0 },
534 { hb_advance - scaler * 0.02, scaler * 0.72 } );
538 outline.m_Orientation = FT_ORIENTATION_TRUETYPE;
539 outline.m_Points.push_back( tofuBox.GetPosition() );
540 outline.m_Points.push_back( { tofuBox.GetSize().x, tofuBox.GetPosition().y } );
541 outline.m_Points.push_back( tofuBox.GetSize() );
542 outline.m_Points.push_back( { tofuBox.GetPosition().x, tofuBox.GetSize().y } );
543 glyphData.
m_Contours.push_back( std::move( outline ) );
546 tofuBox.Move( { scaler * 0.06, scaler * 0.06 } );
547 tofuBox.SetSize( { tofuBox.GetWidth() - scaler * 0.06,
548 tofuBox.GetHeight() - scaler * 0.06 } );
551 hole.
m_Points.push_back( tofuBox.GetPosition() );
552 hole.
m_Points.push_back( { tofuBox.GetSize().x, tofuBox.GetPosition().y } );
553 hole.
m_Points.push_back( tofuBox.GetSize() );
554 hole.
m_Points.push_back( { tofuBox.GetPosition().x, tofuBox.GetSize().y } );
555 glyphData.
m_Contours.push_back( std::move( hole ) );
559 std::unique_ptr<OUTLINE_GLYPH> glyph = std::make_unique<OUTLINE_GLYPH>();
560 std::vector<SHAPE_LINE_CHAIN> holes;
564 const std::vector<VECTOR2D>& points = c.
m_Points;
582 pt.
x = aOrigin.
x - ( pt.
x - aOrigin.
x );
593 holes.push_back( std::move( shape ) );
595 glyph->AddOutline( std::move( shape ) );
600 bool added_hole =
false;
602 if( hole.PointCount() )
604 for(
int ii = 0; ii < glyph->OutlineCount(); ++ii )
606 if( glyph->Outline( ii ).PointInside( hole.GetPoint( 0 ) ) )
608 glyph->AddHole( std::move( hole ), ii );
618 glyph->AddOutline( std::move( hole ) );
624 glyph->CacheTriangulation();
632 aGlyphs->push_back( std::move( glyph ) );
635 const hb_glyph_position_t& pos = glyphPos[i];
645 aBBox->
Merge( aPosition -
VECTOR2I( 0, ascender * abs( scaleFactor.
y ) ) );
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
constexpr void SetOrigin(const Vec &pos)
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
constexpr coord_type GetY() const
constexpr coord_type GetX() const
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
constexpr coord_type GetRight() const
constexpr void SetEnd(coord_type x, coord_type y)
constexpr coord_type GetBottom() const
void getLinePositions(const wxString &aText, const VECTOR2I &aPosition, wxArrayString &aTextLines, std::vector< VECTOR2I > &aPositions, std::vector< VECTOR2I > &aExtents, const TEXT_ATTRIBUTES &aAttrs, const METRICS &aFontMetrics) const
VECTOR2I drawMarkup(BOX2I *aBoundingBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle, const METRICS &aFontMetrics, std::optional< VECTOR2I > aMousePos=std::nullopt, wxString *aActiveUrl=nullptr) const
double GetInterline(double aFontHeight) const
bool OutlineToSegments(std::vector< CONTOUR > *aContours)
VECTOR2I getTextAsGlyphsUnlocked(BOX2I *aBoundingBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aSize, const VECTOR2I &aPosition, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle) const
static void SelectCharmap(FT_Face aFace)
Select the charmap used to map characters to glyphs for aFace.
static std::mutex m_freeTypeMutex
Mutex for freetype access, FT_Library and FT_Face are not thread safe.
double GetInterline(double aGlyphHeight, const METRICS &aFontMetrics) const override
Compute the distance (interline) between 2 lines of text (for multiline texts).
static FT_Library m_freeType
BOX2I getBoundingBox(const std::vector< std::unique_ptr< GLYPH > > &aGlyphs) const
FT_Error loadFace(const wxString &aFontFileName, int aFaceIndex)
static constexpr double m_superscriptVerticalOffset
VECTOR2I getTextAsGlyphs(BOX2I *aBoundingBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aSize, const VECTOR2I &aPosition, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle) const
bool LoadGlyphContours(unsigned int aGlyphIndex, std::vector< CONTOUR > &aContours) const
Load a single glyph into the shared FreeType slot and decompose its outline into line-chain contours.
VECTOR2I GetTextAsGlyphs(BOX2I *aBoundingBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aSize, const VECTOR2I &aPosition, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle) const override
Convert text string to an array of GLYPHs.
const FT_Face & GetFace() const
int faceSize(int aSize) const
void GetLinesAsGlyphs(std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aPosition, const TEXT_ATTRIBUTES &aAttrs, const METRICS &aFontMetrics) const
static OUTLINE_FONT * LoadFont(const wxString &aFontFileName, bool aBold, bool aItalic, const std::vector< wxString > *aEmbeddedFiles, bool aForDrawingSheet)
Load an outline font.
static constexpr double m_outlineFontSizeCompensation
EMBEDDING_PERMISSION GetEmbeddingPermission() const
int subscriptSize() const
static constexpr double m_subscriptVerticalOffset
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
void ReservePoints(size_t aSize)
Allocate a number of points all at once (for performance).
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
FF_RESULT FindFont(const wxString &aFontName, wxString &aFontFile, int &aFaceIndex, bool aBold, bool aItalic, const std::vector< wxString > *aEmbeddedFiles=nullptr)
Given a fully-qualified font name ("Times:Bold:Italic") find the closest matching font and return its...
unsigned int TEXT_STYLE_FLAGS
bool IsSuperscript(TEXT_STYLE_FLAGS aFlags)
bool IsSubscript(TEXT_STYLE_FLAGS aFlags)
FONTCONFIG * Fontconfig()
static constexpr std::size_t hash_val(const Types &... args)
constexpr int GLYPH_RESOLUTION
constexpr double GLYPH_SIZE_SCALER
static bool contourIsHole(const CONTOUR &c)
static bool contourIsFilled(const CONTOUR &c)
static const HARFBUZZ_CACHE_ENTRY & getHarfbuzzShape(FT_Face aFace, const wxString &aText, int aScaler)
bool operator==(const GLYPH_CACHE_KEY &rhs) const
std::vector< hb_glyph_info_t > m_GlyphInfo
std::vector< hb_glyph_position_t > m_GlyphPositions
bool operator==(const HARFBUZZ_CACHE_KEY &rhs) const
std::vector< VECTOR2D > m_Points
FT_Orientation m_Orientation
std::vector< CONTOUR > m_Contours
std::vector< std::unique_ptr< SHAPE_POLY_SET::TRIANGULATED_POLYGON > > m_TriangulationData
std::size_t operator()(const GLYPH_CACHE_KEY &k) const
std::size_t operator()(const HARFBUZZ_CACHE_KEY &k) const
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
VECTOR2< int32_t > VECTOR2I
VECTOR2< double > VECTOR2D