KiCad PCB EDA Suite
Loading...
Searching...
No Matches
outline_font.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (C) 2021 Ola Rinta-Koski <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <limits>
22#include <harfbuzz/hb.h>
23#include <harfbuzz/hb-ft.h>
24#include <bezier_curves.h>
26#include <font/fontconfig.h>
27#include <font/outline_font.h>
28#include <ft2build.h>
29#include FT_FREETYPE_H
30#include FT_SFNT_NAMES_H
31#include FT_TRUETYPE_TABLES_H
32#include FT_GLYPH_H
33#include FT_BBOX_H
34#include <trigo.h>
35#include <core/utf8.h>
36
37using namespace KIFONT;
38
39
40FT_Library OUTLINE_FONT::m_freeType = nullptr;
42
44 m_face(NULL),
45 m_faceSize( 16 ),
46 m_fakeBold( false ),
47 m_fakeItal( false ),
48 m_forDrawingSheet( false )
49{
50 std::lock_guard<std::mutex> guard( m_freeTypeMutex );
51
52 if( !m_freeType )
53 FT_Init_FreeType( &m_freeType );
54}
55
56
58{
59 TT_OS2* os2 = reinterpret_cast<TT_OS2*>( FT_Get_Sfnt_Table( m_face, FT_SFNT_OS2 ) );
60
61 // If this table isn't present, we can't assume anything
62 if( !os2 )
64
65 // We don't support bitmap fonts, so this disables embedding
66 if( os2->fsType & FT_FSTYPE_BITMAP_EMBEDDING_ONLY )
68
69 // Per the OpenType spec, only bits 0-3 of fsType define the embedding license.
70 // Bits 8-9 (no-subsetting, bitmap-only) are independent modifiers and must be
71 // masked off before checking the embedding permission level.
72 // See: http://freetype.org/freetype2/docs/reference/ft2-information_retrieval.html
73 FT_UShort embeddingBits = os2->fsType & 0x000F;
74
75 // This allows the font to be exported from KiCad
76 if( embeddingBits == FT_FSTYPE_INSTALLABLE_EMBEDDING )
78
79 // This allows us to use the font in KiCad but not export
80 if( embeddingBits & FT_FSTYPE_EDITABLE_EMBEDDING )
82
83 // This is not actually supported by KiCad ATM(2024)
84 if( embeddingBits & FT_FSTYPE_PREVIEW_AND_PRINT_EMBEDDING )
86
87 // Anything else that is not explicitly enabled we treat as restricted.
89}
90
91
92OUTLINE_FONT* OUTLINE_FONT::LoadFont( const wxString& aFontName, bool aBold, bool aItalic,
93 const std::vector<wxString>* aEmbeddedFiles,
94 bool aForDrawingSheet )
95{
96 std::unique_ptr<OUTLINE_FONT> font = std::make_unique<OUTLINE_FONT>();
97
98 wxString fontFile;
99 int faceIndex;
100 using fc = fontconfig::FONTCONFIG;
101
102
103 fc::FF_RESULT retval = Fontconfig()->FindFont( aFontName, fontFile, faceIndex, aBold, aItalic,
104 aEmbeddedFiles );
105
106 if( retval == fc::FF_RESULT::FF_ERROR )
107 return nullptr;
108
109 if( retval == fc::FF_RESULT::FF_MISSING_BOLD || retval == fc::FF_RESULT::FF_MISSING_BOLD_ITAL )
110 font->SetFakeBold();
111
112 if( retval == fc::FF_RESULT::FF_MISSING_ITAL || retval == fc::FF_RESULT::FF_MISSING_BOLD_ITAL )
113 font->SetFakeItal();
114
115 if( font->loadFace( fontFile, faceIndex ) != 0 )
116 return nullptr;
117
118 font->m_fontName = aFontName; // Keep asked-for name, even if we substituted.
119 font->m_fontFileName = fontFile;
120 font->m_forDrawingSheet = aForDrawingSheet;
121
122 return font.release();
123}
124
125
126FT_Error OUTLINE_FONT::loadFace( const wxString& aFontFileName, int aFaceIndex )
127{
128 std::lock_guard<std::mutex> guard( m_freeTypeMutex );
129
130 FT_Error e = FT_New_Face( m_freeType, aFontFileName.mb_str( wxConvUTF8 ), aFaceIndex, &m_face );
131
132 if( !e )
133 {
135 // params:
136 // m_face = handle to face object
137 // 0 = char width in 1/64th of points ( 0 = same as char height )
138 // faceSize() = char height in 1/64th of points
139 // GLYPH_RESOLUTION = horizontal device resolution (1152dpi, 16x default)
140 // 0 = vertical device resolution ( 0 = same as horizontal )
141 FT_Set_Char_Size( m_face, 0, faceSize(), GLYPH_RESOLUTION, 0 );
142 }
143
144 return e;
145}
146
147
148void OUTLINE_FONT::SelectCharmap( FT_Face aFace )
149{
150 // A normal text font carries a Unicode charmap that maps the Basic Latin block directly.
151 // Keep it when present.
152 if( FT_Select_Charmap( aFace, FT_ENCODING_UNICODE ) == 0 )
153 {
154 // Some legacy "symbol" fonts expose a Unicode charmap that only mirrors their private-use
155 // (U+F000..U+F0FF) glyph layout, leaving ASCII unmapped. Probe a few common characters to
156 // tell a usable Unicode charmap apart from such a font.
157 static const FT_ULong probes[] = { 'A', 'a', '0', ' ' };
158
159 for( FT_ULong codepoint : probes )
160 {
161 if( FT_Get_Char_Index( aFace, codepoint ) != 0 )
162 return;
163 }
164 }
165
166 // No Unicode charmap can resolve ASCII. If the font carries a Microsoft Symbol charmap,
167 // select it so HarfBuzz applies its U+F000 offset remapping and the glyphs become reachable.
168 if( FT_Select_Charmap( aFace, FT_ENCODING_MS_SYMBOL ) == 0 )
169 return;
170
171 // Otherwise fall back to the Unicode charmap (e.g. a CJK-only font with no Basic Latin).
172 FT_Select_Charmap( aFace, FT_ENCODING_UNICODE );
173}
174
175
176double OUTLINE_FONT::GetInterline( double aGlyphHeight, const METRICS& aFontMetrics ) const
177{
178 double glyphToFontHeight = 1.0;
179
180 if( GetFace()->units_per_EM )
181 glyphToFontHeight = GetFace()->height / GetFace()->units_per_EM;
182
183 return aFontMetrics.GetInterline( aGlyphHeight * glyphToFontHeight );
184}
185
186
187static bool contourIsFilled( const CONTOUR& c )
188{
189 switch( c.m_Orientation )
190 {
191 case FT_ORIENTATION_TRUETYPE: return c.m_Winding == 1;
192 case FT_ORIENTATION_POSTSCRIPT: return c.m_Winding == -1;
193 default: return false;
194 }
195}
196
197
198static bool contourIsHole( const CONTOUR& c )
199{
200 return !contourIsFilled( c );
201}
202
203
204BOX2I OUTLINE_FONT::getBoundingBox( const std::vector<std::unique_ptr<GLYPH>>& aGlyphs ) const
205{
206 int minX = INT_MAX;
207 int minY = INT_MAX;
208 int maxX = INT_MIN;
209 int maxY = INT_MIN;
210
211 for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aGlyphs )
212 {
213 BOX2D bbox = glyph->BoundingBox();
214 bbox.Normalize();
215
216 if( minX > bbox.GetX() )
217 minX = bbox.GetX();
218
219 if( minY > bbox.GetY() )
220 minY = bbox.GetY();
221
222 if( maxX < bbox.GetRight() )
223 maxX = bbox.GetRight();
224
225 if( maxY < bbox.GetBottom() )
226 maxY = bbox.GetBottom();
227 }
228
229 BOX2I ret;
230 ret.SetOrigin( minX, minY );
231 ret.SetEnd( maxX, maxY );
232 return ret;
233}
234
235
236void OUTLINE_FONT::GetLinesAsGlyphs( std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
237 const wxString& aText, const VECTOR2I& aPosition,
238 const TEXT_ATTRIBUTES& aAttrs,
239 const METRICS& aFontMetrics ) const
240{
241 wxArrayString strings;
242 std::vector<VECTOR2I> positions;
243 std::vector<VECTOR2I> extents;
244 TEXT_STYLE_FLAGS textStyle = 0;
245
246 if( aAttrs.m_Italic )
247 textStyle |= TEXT_STYLE::ITALIC;
248
249 getLinePositions( aText, aPosition, strings, positions, extents, aAttrs, aFontMetrics );
250
251 for( size_t i = 0; i < strings.GetCount(); i++ )
252 {
253 (void) drawMarkup( nullptr, aGlyphs, strings.Item( i ), positions[i], aAttrs.m_Size,
254 aAttrs.m_Angle, aAttrs.m_Mirrored, aPosition, textStyle, aFontMetrics );
255 }
256}
257
258
259VECTOR2I OUTLINE_FONT::GetTextAsGlyphs( BOX2I* aBBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
260 const wxString& aText, const VECTOR2I& aSize,
261 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
262 bool aMirror, const VECTOR2I& aOrigin,
263 TEXT_STYLE_FLAGS aTextStyle ) const
264{
265 // HarfBuzz needs further processing to split tab-delimited text into text runs.
266
267 constexpr double TAB_WIDTH = 4 * 0.6;
268
269 VECTOR2I position = aPosition;
270 wxString textRun;
271
272 if( aBBox )
273 {
274 aBBox->SetOrigin( aPosition );
275 aBBox->SetEnd( aPosition );
276 }
277
278 for( wxUniChar c : aText )
279 {
280 // Handle tabs as locked to the nearest 4th column (in space-widths).
281 if( c == '\t' )
282 {
283 if( !textRun.IsEmpty() )
284 {
285 position = getTextAsGlyphs( aBBox, aGlyphs, textRun, aSize, position, aAngle,
286 aMirror, aOrigin, aTextStyle );
287 textRun.clear();
288 }
289
290 int tabWidth = KiROUND( aSize.x * TAB_WIDTH );
291 int currentIntrusion = ( position.x - aOrigin.x ) % tabWidth;
292
293 position.x += tabWidth - currentIntrusion;
294 }
295 else
296 {
297 textRun += c;
298 }
299 }
300
301 if( !textRun.IsEmpty() )
302 {
303 position = getTextAsGlyphs( aBBox, aGlyphs, textRun, aSize, position, aAngle, aMirror,
304 aOrigin, aTextStyle );
305 }
306
307 return position;
308}
309
310
311VECTOR2I OUTLINE_FONT::getTextAsGlyphs( BOX2I* aBBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
312 const wxString& aText, const VECTOR2I& aSize,
313 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
314 bool aMirror, const VECTOR2I& aOrigin,
315 TEXT_STYLE_FLAGS aTextStyle ) const
316{
317 std::lock_guard<std::mutex> guard( m_freeTypeMutex );
318
319 return getTextAsGlyphsUnlocked( aBBox, aGlyphs, aText, aSize, aPosition, aAngle, aMirror,
320 aOrigin, aTextStyle );
321}
322
323
325 FT_Face face;
326 std::string text;
328
329 bool operator==(const HARFBUZZ_CACHE_KEY& rhs ) const
330 {
331 return face == rhs.face
332 && scaler == rhs.scaler
333 && text == rhs.text;
334 }
335};
336
337
339{
340 std::vector<hb_glyph_info_t> m_GlyphInfo;
341 std::vector<hb_glyph_position_t> m_GlyphPositions;
342 bool m_Initialized = false;
343};
344
345
346namespace std
347{
348 template <>
350 {
351 std::size_t operator()( const HARFBUZZ_CACHE_KEY& k ) const
352 {
353 return hash_val( k.face, k.scaler, k.text );
354 }
355 };
356}
357
358
359static const HARFBUZZ_CACHE_ENTRY& getHarfbuzzShape( FT_Face aFace, const wxString& aText,
360 int aScaler )
361{
362 static std::unordered_map<HARFBUZZ_CACHE_KEY, HARFBUZZ_CACHE_ENTRY> s_harfbuzzCache;
363
364 std::string textUtf8 = UTF8( aText );
365 HARFBUZZ_CACHE_KEY key = { aFace, textUtf8, aScaler };
366
367 HARFBUZZ_CACHE_ENTRY& entry = s_harfbuzzCache[key];
368
369 if( !entry.m_Initialized )
370 {
371 hb_buffer_t* buf = hb_buffer_create();
372 hb_buffer_add_utf8( buf, textUtf8.c_str(), -1, 0, -1 );
373 hb_buffer_guess_segment_properties( buf ); // guess direction, script, and language based on
374 // contents
375
376 hb_font_t* referencedFont = hb_ft_font_create_referenced( aFace );
377
378 hb_shape( referencedFont, buf, nullptr, 0 );
379
380 unsigned int glyphCount;
381 hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( buf, &glyphCount );
382 hb_glyph_position_t* glyphPos = hb_buffer_get_glyph_positions( buf, &glyphCount );
383
384 entry.m_GlyphInfo.assign( glyphInfo, glyphInfo + glyphCount );
385 entry.m_GlyphPositions.assign( glyphPos, glyphPos + glyphCount );
386 entry.m_Initialized = true;
387
388 hb_buffer_destroy( buf );
389 hb_font_destroy( referencedFont );
390 }
391
392 return entry;
393}
394
395
397 FT_Face face;
398 hb_codepoint_t codepoint;
403 bool mirror;
406
407 bool operator==(const GLYPH_CACHE_KEY& rhs ) const
408 {
409 return face == rhs.face
410 && codepoint == rhs.codepoint
411 && scale == rhs.scale
413 && fakeItalic == rhs.fakeItalic
414 && fakeBold == rhs.fakeBold
415 && mirror == rhs.mirror
416 && supersub == rhs.supersub
417 && angle == rhs.angle;
418 }
419};
420
421
422namespace std
423{
424 template <>
425 struct hash<GLYPH_CACHE_KEY>
426 {
427 std::size_t operator()( const GLYPH_CACHE_KEY& k ) const
428 {
429 return hash_val( k.face, k.codepoint, k.scale.x, k.scale.y, k.forDrawingSheet,
431 }
432 };
433}
434
435
437 std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
438 const wxString& aText, const VECTOR2I& aSize,
439 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
440 bool aMirror, const VECTOR2I& aOrigin,
441 TEXT_STYLE_FLAGS aTextStyle ) const
442{
443 VECTOR2D glyphSize = aSize;
444 FT_Face face = m_face;
445 double scaler = faceSize();
446 bool supersub = IsSuperscript( aTextStyle ) || IsSubscript( aTextStyle );
447
448 if( supersub )
449 scaler = subscriptSize();
450
451 // set glyph resolution so that FT_Load_Glyph() results are good enough for decomposing
452 FT_Set_Char_Size( face, 0, scaler, GLYPH_RESOLUTION, 0 );
453
454 const HARFBUZZ_CACHE_ENTRY& hbShape = getHarfbuzzShape( face, aText, scaler );
455
456 unsigned int glyphCount = static_cast<unsigned int>( hbShape.m_GlyphInfo.size() );
457 const hb_glyph_info_t* glyphInfo = hbShape.m_GlyphInfo.data();
458 const hb_glyph_position_t* glyphPos = hbShape.m_GlyphPositions.data();
459
460 VECTOR2D scaleFactor( glyphSize.x / faceSize(), -glyphSize.y / faceSize() );
461 scaleFactor = scaleFactor * m_outlineFontSizeCompensation;
462
463 VECTOR2I cursor( 0, 0 );
464
465 if( aGlyphs )
466 aGlyphs->reserve( glyphCount );
467
468 // GLYPH_DATA is a collection of all outlines in the glyph; for example the 'o' glyph
469 // generally contains 2 contours, one for the glyph outline and one for the hole
470 static std::unordered_map<GLYPH_CACHE_KEY, GLYPH_DATA> s_glyphCache;
471
472 for( unsigned int i = 0; i < glyphCount; i++ )
473 {
474 if( aGlyphs )
475 {
476 GLYPH_CACHE_KEY key = { face, glyphInfo[i].codepoint, scaleFactor, m_forDrawingSheet,
477 m_fakeItal, m_fakeBold, aMirror, supersub, aAngle };
478 GLYPH_DATA& glyphData = s_glyphCache[ key ];
479
480 if( glyphData.m_Contours.empty() )
481 {
482 if( m_fakeItal )
483 {
484 FT_Matrix matrix;
485 // Create a 12 degree slant
486 const float angle = (float)( -M_PI * 12.0f ) / 180.0f;
487 matrix.xx = (FT_Fixed) ( cos( angle ) * 0x10000L );
488 matrix.xy = (FT_Fixed) ( -sin( angle ) * 0x10000L );
489 matrix.yx = (FT_Fixed) ( 0 * 0x10000L ); // Don't rotate in the y direction
490 matrix.yy = (FT_Fixed) ( 1 * 0x10000L );
491
492 FT_Set_Transform( face, &matrix, nullptr );
493 }
494
495 FT_Load_Glyph( face, glyphInfo[i].codepoint, FT_LOAD_NO_BITMAP );
496
497 if( m_fakeBold )
498 FT_Outline_Embolden( &face->glyph->outline, 1 << 6 );
499
500 OUTLINE_DECOMPOSER decomposer( face->glyph->outline );
501
502 if( !decomposer.OutlineToSegments( &glyphData.m_Contours ) )
503 {
504 double hb_advance = glyphPos[i].x_advance * GLYPH_SIZE_SCALER;
505 BOX2D tofuBox( { scaler * 0.03, 0.0 },
506 { hb_advance - scaler * 0.02, scaler * 0.72 } );
507
508 glyphData.m_Contours.clear();
509
510 CONTOUR outline;
511 outline.m_Winding = 1;
512 outline.m_Orientation = FT_ORIENTATION_TRUETYPE;
513 outline.m_Points.push_back( tofuBox.GetPosition() );
514 outline.m_Points.push_back( { tofuBox.GetSize().x, tofuBox.GetPosition().y } );
515 outline.m_Points.push_back( tofuBox.GetSize() );
516 outline.m_Points.push_back( { tofuBox.GetPosition().x, tofuBox.GetSize().y } );
517 glyphData.m_Contours.push_back( std::move( outline ) );
518
519 CONTOUR hole;
520 tofuBox.Move( { scaler * 0.06, scaler * 0.06 } );
521 tofuBox.SetSize( { tofuBox.GetWidth() - scaler * 0.06,
522 tofuBox.GetHeight() - scaler * 0.06 } );
523 hole.m_Winding = 1;
524 hole.m_Orientation = FT_ORIENTATION_NONE;
525 hole.m_Points.push_back( tofuBox.GetPosition() );
526 hole.m_Points.push_back( { tofuBox.GetSize().x, tofuBox.GetPosition().y } );
527 hole.m_Points.push_back( tofuBox.GetSize() );
528 hole.m_Points.push_back( { tofuBox.GetPosition().x, tofuBox.GetSize().y } );
529 glyphData.m_Contours.push_back( std::move( hole ) );
530 }
531 }
532
533 std::unique_ptr<OUTLINE_GLYPH> glyph = std::make_unique<OUTLINE_GLYPH>();
534 std::vector<SHAPE_LINE_CHAIN> holes;
535
536 for( CONTOUR& c : glyphData.m_Contours )
537 {
538 std::vector<VECTOR2D> points = c.m_Points;
539 SHAPE_LINE_CHAIN shape;
540
541 shape.ReservePoints( points.size() );
542
543 for( const VECTOR2D& v : points )
544 {
545 VECTOR2D pt( v + cursor );
546
547 if( IsSubscript( aTextStyle ) )
548 pt.y += m_subscriptVerticalOffset * scaler;
549 else if( IsSuperscript( aTextStyle ) )
550 pt.y += m_superscriptVerticalOffset * scaler;
551
552 pt *= scaleFactor;
553 pt += aPosition;
554
555 if( aMirror )
556 pt.x = aOrigin.x - ( pt.x - aOrigin.x );
557
558 if( !aAngle.IsZero() )
559 RotatePoint( pt, aOrigin, aAngle );
560
561 shape.Append( pt.x, pt.y );
562 }
563
564 shape.SetClosed( true );
565
566 if( contourIsHole( c ) )
567 holes.push_back( std::move( shape ) );
568 else
569 glyph->AddOutline( std::move( shape ) );
570 }
571
572 for( SHAPE_LINE_CHAIN& hole : holes )
573 {
574 bool added_hole = false;
575
576 if( hole.PointCount() )
577 {
578 for( int ii = 0; ii < glyph->OutlineCount(); ++ii )
579 {
580 if( glyph->Outline( ii ).PointInside( hole.GetPoint( 0 ) ) )
581 {
582 glyph->AddHole( std::move( hole ), ii );
583 added_hole = true;
584 break;
585 }
586 }
587
588 // Some lovely TTF fonts decided that winding didn't matter for outlines that
589 // don't have holes, so holes that don't fit in any outline are added as
590 // outlines.
591 if( !added_hole )
592 glyph->AddOutline( std::move( hole ) );
593 }
594 }
595
596 if( glyphData.m_TriangulationData.empty() )
597 {
598 glyph->CacheTriangulation();
599 glyphData.m_TriangulationData = glyph->GetTriangulationData();
600 }
601 else
602 {
603 glyph->CacheTriangulation( glyphData.m_TriangulationData );
604 }
605
606 aGlyphs->push_back( std::move( glyph ) );
607 }
608
609 const hb_glyph_position_t& pos = glyphPos[i];
610 cursor.x += ( pos.x_advance * GLYPH_SIZE_SCALER );
611 cursor.y += ( pos.y_advance * GLYPH_SIZE_SCALER );
612 }
613
614 int ascender = abs( face->size->metrics.ascender * GLYPH_SIZE_SCALER );
615 int descender = abs( face->size->metrics.descender * GLYPH_SIZE_SCALER );
616
617 if( aBBox )
618 {
619 aBBox->Merge( aPosition - VECTOR2I( 0, ascender * abs( scaleFactor.y ) ) );
620 aBBox->Merge( aPosition + VECTOR2I( cursor.x * scaleFactor.x, descender * abs( scaleFactor.y ) ) );
621 }
622
623 return VECTOR2I( aPosition.x + cursor.x * scaleFactor.x, aPosition.y - cursor.y * scaleFactor.y );
624}
625
626
627#undef OUTLINEFONT_RENDER_AS_PIXELS
628#ifdef OUTLINEFONT_RENDER_AS_PIXELS
629/*
630 * WIP: Eeschema (and PDF output?) should use pixel rendering instead of linear segmentation
631 */
632void OUTLINE_FONT::RenderToOpenGLCanvas( KIGFX::OPENGL_GAL& aGal, const wxString& aString,
633 const VECTOR2D& aGlyphSize, const VECTOR2I& aPosition,
634 const EDA_ANGLE& aOrientation, bool aIsMirrored ) const
635{
636 hb_buffer_t* buf = hb_buffer_create();
637 hb_buffer_add_utf8( buf, UTF8( aString ).c_str(), -1, 0, -1 );
638
639 // guess direction, script, and language based on contents
640 hb_buffer_guess_segment_properties( buf );
641
642 unsigned int glyphCount;
643 hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( buf, &glyphCount );
644 hb_glyph_position_t* glyphPos = hb_buffer_get_glyph_positions( buf, &glyphCount );
645
646 std::lock_guard<std::mutex> guard( m_freeTypeMutex );
647
648 hb_font_t* referencedFont = hb_ft_font_create_referenced( m_face );
649
650 hb_shape( referencedFont, buf, nullptr, 0 );
651
652 const double mirror_factor = ( aIsMirrored ? 1 : -1 );
653 const double x_scaleFactor = mirror_factor * aGlyphSize.x / mScaler;
654 const double y_scaleFactor = aGlyphSize.y / mScaler;
655
656 hb_position_t cursor_x = 0;
657 hb_position_t cursor_y = 0;
658
659 for( unsigned int i = 0; i < glyphCount; i++ )
660 {
661 const hb_glyph_position_t& pos = glyphPos[i];
662 int codepoint = glyphInfo[i].codepoint;
663
664 FT_Error e = FT_Load_Glyph( m_face, codepoint, FT_LOAD_DEFAULT );
665 // TODO handle FT_Load_Glyph error
666
667 FT_Glyph glyph;
668 e = FT_Get_Glyph( m_face->glyph, &glyph );
669 // TODO handle FT_Get_Glyph error
670
671 wxPoint pt( aPosition );
672 pt.x += ( cursor_x >> 6 ) * x_scaleFactor;
673 pt.y += ( cursor_y >> 6 ) * y_scaleFactor;
674
675 cursor_x += pos.x_advance;
676 cursor_y += pos.y_advance;
677 }
678
679 hb_buffer_destroy( buf );
680}
681
682#endif //OUTLINEFONT_RENDER_AS_PIXELS
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
BOX2< VECTOR2D > BOX2D
Definition box2.h:919
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:233
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition box2.h:142
constexpr coord_type GetY() const
Definition box2.h:204
constexpr coord_type GetX() const
Definition box2.h:203
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:654
constexpr coord_type GetRight() const
Definition box2.h:213
constexpr void SetEnd(coord_type x, coord_type y)
Definition box2.h:293
constexpr coord_type GetBottom() const
Definition box2.h:218
double AsDegrees() const
Definition eda_angle.h:116
bool IsZero() const
Definition eda_angle.h:136
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
Definition font.cpp:177
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
Definition font.cpp:388
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
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
OpenGL implementation of the Graphics Abstraction Layer.
Definition opengl_gal.h:70
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...
Definition utf8.h:67
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...
@ ITALIC
Definition font.h:44
unsigned int TEXT_STYLE_FLAGS
Definition font.h:61
bool IsSuperscript(TEXT_STYLE_FLAGS aFlags)
Definition font.h:76
bool IsSubscript(TEXT_STYLE_FLAGS aFlags)
Definition font.h:82
FONTCONFIG * Fontconfig()
static constexpr std::size_t hash_val(const Types &... args)
Definition hash.h:47
constexpr int GLYPH_RESOLUTION
constexpr double GLYPH_SIZE_SCALER
STL namespace.
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)
hb_codepoint_t codepoint
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 m_Initialized
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
#define M_PI
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.
Definition trigo.cpp:225
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682