2022-01-01 01:21:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 Ola Rinta-Koski <gitlab@rinta-koski.net>
|
2024-01-12 17:04:05 +00:00
|
|
|
* Copyright (C) 2021-2024 Kicad Developers, see AUTHORS.txt for contributors.
|
2022-01-01 01:21:03 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits>
|
2022-03-07 14:01:37 +00:00
|
|
|
#include <harfbuzz/hb.h>
|
2022-01-01 01:21:03 +00:00
|
|
|
#include <harfbuzz/hb-ft.h>
|
|
|
|
#include <bezier_curves.h>
|
|
|
|
#include <geometry/shape_poly_set.h>
|
2023-01-25 21:10:31 +00:00
|
|
|
#include <font/fontconfig.h>
|
2022-01-01 01:21:03 +00:00
|
|
|
#include <font/outline_font.h>
|
|
|
|
#include FT_GLYPH_H
|
|
|
|
#include FT_BBOX_H
|
|
|
|
#include <trigo.h>
|
2023-09-18 23:52:27 +00:00
|
|
|
#include <core/utf8.h>
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
using namespace KIFONT;
|
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
FT_Library OUTLINE_FONT::m_freeType = nullptr;
|
2023-02-17 12:24:22 +00:00
|
|
|
std::mutex OUTLINE_FONT::m_freeTypeMutex;
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
OUTLINE_FONT::OUTLINE_FONT() :
|
2022-02-05 16:36:02 +00:00
|
|
|
m_face(NULL),
|
2023-01-25 21:10:31 +00:00
|
|
|
m_faceSize( 16 ),
|
|
|
|
m_fakeBold( false ),
|
|
|
|
m_fakeItal( false )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2023-02-17 12:24:22 +00:00
|
|
|
std::lock_guard<std::mutex> guard( m_freeTypeMutex );
|
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
if( !m_freeType )
|
|
|
|
FT_Init_FreeType( &m_freeType );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OUTLINE_FONT* OUTLINE_FONT::LoadFont( const wxString& aFontName, bool aBold, bool aItalic )
|
|
|
|
{
|
2023-02-14 01:04:55 +00:00
|
|
|
std::unique_ptr<OUTLINE_FONT> font = std::make_unique<OUTLINE_FONT>();
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
wxString fontFile;
|
2023-03-13 16:35:28 +00:00
|
|
|
int faceIndex;
|
2023-01-25 21:10:31 +00:00
|
|
|
using fc = fontconfig::FONTCONFIG;
|
|
|
|
|
2023-03-13 16:35:28 +00:00
|
|
|
fc::FF_RESULT retval = Fontconfig()->FindFont( aFontName, fontFile, faceIndex, aBold, aItalic );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2023-02-14 01:04:55 +00:00
|
|
|
if( retval == fc::FF_RESULT::FF_ERROR )
|
|
|
|
return nullptr;
|
|
|
|
|
2023-01-26 00:44:35 +00:00
|
|
|
if( retval == fc::FF_RESULT::FF_MISSING_BOLD || retval == fc::FF_RESULT::FF_MISSING_BOLD_ITAL )
|
2023-01-25 21:10:31 +00:00
|
|
|
font->SetFakeBold();
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2023-01-26 00:44:35 +00:00
|
|
|
if( retval == fc::FF_RESULT::FF_MISSING_ITAL || retval == fc::FF_RESULT::FF_MISSING_BOLD_ITAL )
|
2023-01-25 21:10:31 +00:00
|
|
|
font->SetFakeItal();
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2023-03-13 16:35:28 +00:00
|
|
|
if( font->loadFace( fontFile, faceIndex ) != 0 )
|
2023-02-14 01:04:55 +00:00
|
|
|
return nullptr;
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2022-01-09 00:44:52 +00:00
|
|
|
font->m_fontName = aFontName; // Keep asked-for name, even if we substituted.
|
|
|
|
font->m_fontFileName = fontFile;
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2023-02-14 01:04:55 +00:00
|
|
|
return font.release();
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-13 16:35:28 +00:00
|
|
|
FT_Error OUTLINE_FONT::loadFace( const wxString& aFontFileName, int aFaceIndex )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2023-02-17 12:24:22 +00:00
|
|
|
std::lock_guard<std::mutex> guard( m_freeTypeMutex );
|
|
|
|
|
2023-03-13 16:35:28 +00:00
|
|
|
FT_Error e = FT_New_Face( m_freeType, aFontFileName.mb_str( wxConvUTF8 ), aFaceIndex, &m_face );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
if( !e )
|
|
|
|
{
|
|
|
|
FT_Select_Charmap( m_face, FT_Encoding::FT_ENCODING_UNICODE );
|
2022-01-15 12:51:09 +00:00
|
|
|
// params:
|
|
|
|
// m_face = handle to face object
|
|
|
|
// 0 = char width in 1/64th of points ( 0 = same as char height )
|
|
|
|
// faceSize() = char height in 1/64th of points
|
|
|
|
// GLYPH_RESOLUTION = horizontal device resolution (288dpi, 4x default)
|
|
|
|
// 0 = vertical device resolution ( 0 = same as horizontal )
|
|
|
|
FT_Set_Char_Size( m_face, 0, faceSize(), GLYPH_RESOLUTION, 0 );
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the distance (interline) between 2 lines of text (for multiline texts). This is
|
|
|
|
* the distance between baselines, not the space between line bounding boxes.
|
|
|
|
*/
|
2023-08-06 19:20:53 +00:00
|
|
|
double OUTLINE_FONT::GetInterline( double aGlyphHeight, const METRICS& aFontMetrics ) const
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2023-08-06 19:20:53 +00:00
|
|
|
double glyphToFontHeight = 1.0;
|
2022-01-04 23:00:00 +00:00
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
if( GetFace()->units_per_EM )
|
2023-08-06 19:20:53 +00:00
|
|
|
glyphToFontHeight = GetFace()->height / GetFace()->units_per_EM;
|
2022-01-15 12:51:09 +00:00
|
|
|
|
2023-08-06 19:20:53 +00:00
|
|
|
return aFontMetrics.GetInterline( aGlyphHeight * glyphToFontHeight );
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool contourIsFilled( const CONTOUR& c )
|
|
|
|
{
|
2022-07-31 16:35:37 +00:00
|
|
|
switch( c.m_Orientation )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2022-07-31 16:35:37 +00:00
|
|
|
case FT_ORIENTATION_TRUETYPE: return c.m_Winding == 1;
|
|
|
|
case FT_ORIENTATION_POSTSCRIPT: return c.m_Winding == -1;
|
2022-01-01 01:21:03 +00:00
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool contourIsHole( const CONTOUR& c )
|
|
|
|
{
|
|
|
|
return !contourIsFilled( c );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOX2I OUTLINE_FONT::getBoundingBox( const std::vector<std::unique_ptr<GLYPH>>& aGlyphs ) const
|
|
|
|
{
|
|
|
|
int minX = INT_MAX;
|
|
|
|
int minY = INT_MAX;
|
|
|
|
int maxX = INT_MIN;
|
|
|
|
int maxY = INT_MIN;
|
|
|
|
|
|
|
|
for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aGlyphs )
|
|
|
|
{
|
|
|
|
BOX2D bbox = glyph->BoundingBox();
|
|
|
|
bbox.Normalize();
|
|
|
|
|
|
|
|
if( minX > bbox.GetX() )
|
|
|
|
minX = bbox.GetX();
|
|
|
|
|
|
|
|
if( minY > bbox.GetY() )
|
|
|
|
minY = bbox.GetY();
|
|
|
|
|
|
|
|
if( maxX < bbox.GetRight() )
|
|
|
|
maxX = bbox.GetRight();
|
|
|
|
|
|
|
|
if( maxY < bbox.GetBottom() )
|
|
|
|
maxY = bbox.GetBottom();
|
|
|
|
}
|
|
|
|
|
|
|
|
BOX2I ret;
|
|
|
|
ret.SetOrigin( minX, minY );
|
|
|
|
ret.SetEnd( maxX, maxY );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
void OUTLINE_FONT::GetLinesAsGlyphs( std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
|
2022-01-25 22:33:37 +00:00
|
|
|
const wxString& aText, const VECTOR2I& aPosition,
|
2023-08-06 19:20:53 +00:00
|
|
|
const TEXT_ATTRIBUTES& aAttrs,
|
|
|
|
const METRICS& aFontMetrics ) const
|
2022-01-07 17:42:43 +00:00
|
|
|
{
|
|
|
|
wxArrayString strings;
|
|
|
|
std::vector<VECTOR2I> positions;
|
|
|
|
std::vector<VECTOR2I> extents;
|
|
|
|
TEXT_STYLE_FLAGS textStyle = 0;
|
|
|
|
|
|
|
|
if( aAttrs.m_Italic )
|
2022-01-01 01:21:03 +00:00
|
|
|
textStyle |= TEXT_STYLE::ITALIC;
|
|
|
|
|
2023-08-06 19:20:53 +00:00
|
|
|
getLinePositions( aText, aPosition, strings, positions, extents, aAttrs, aFontMetrics );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
for( size_t i = 0; i < strings.GetCount(); i++ )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2023-05-17 20:30:31 +00:00
|
|
|
(void) drawMarkup( nullptr, aGlyphs, strings.Item( i ), positions[i], aAttrs.m_Size,
|
2023-08-06 19:20:53 +00:00
|
|
|
aAttrs.m_Angle, aAttrs.m_Mirrored, aPosition, textStyle, aFontMetrics );
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
VECTOR2I OUTLINE_FONT::GetTextAsGlyphs( BOX2I* aBBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
|
2022-01-25 22:33:37 +00:00
|
|
|
const wxString& aText, const VECTOR2I& aSize,
|
2022-01-04 23:00:00 +00:00
|
|
|
const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
|
|
|
|
bool aMirror, const VECTOR2I& aOrigin,
|
2022-01-01 01:21:03 +00:00
|
|
|
TEXT_STYLE_FLAGS aTextStyle ) const
|
2022-04-20 11:00:19 +00:00
|
|
|
{
|
|
|
|
// HarfBuzz needs further processing to split tab-delimited text into text runs.
|
|
|
|
|
|
|
|
constexpr double TAB_WIDTH = 4 * 0.6;
|
|
|
|
|
|
|
|
VECTOR2I position = aPosition;
|
|
|
|
wxString textRun;
|
|
|
|
|
|
|
|
if( aBBox )
|
|
|
|
{
|
|
|
|
aBBox->SetOrigin( aPosition );
|
|
|
|
aBBox->SetEnd( aPosition );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( wxUniChar c : aText )
|
|
|
|
{
|
|
|
|
// Handle tabs as locked to the nearest 4th column (in space-widths).
|
|
|
|
if( c == '\t' )
|
|
|
|
{
|
|
|
|
if( !textRun.IsEmpty() )
|
|
|
|
{
|
|
|
|
position = getTextAsGlyphs( aBBox, aGlyphs, textRun, aSize, position, aAngle,
|
|
|
|
aMirror, aOrigin, aTextStyle );
|
|
|
|
textRun.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
int tabWidth = KiROUND( aSize.x * TAB_WIDTH );
|
|
|
|
int currentIntrusion = ( position.x - aOrigin.x ) % tabWidth;
|
|
|
|
|
|
|
|
position.x += tabWidth - currentIntrusion;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textRun += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !textRun.IsEmpty() )
|
|
|
|
{
|
|
|
|
position = getTextAsGlyphs( aBBox, aGlyphs, textRun, aSize, position, aAngle, aMirror,
|
|
|
|
aOrigin, aTextStyle );
|
|
|
|
}
|
|
|
|
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VECTOR2I OUTLINE_FONT::getTextAsGlyphs( BOX2I* aBBox, 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
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2023-02-17 14:39:52 +00:00
|
|
|
std::lock_guard<std::mutex> guard( m_freeTypeMutex );
|
2023-02-23 22:58:37 +00:00
|
|
|
|
|
|
|
return getTextAsGlyphsUnlocked( aBBox, aGlyphs, aText, aSize, aPosition, aAngle, aMirror,
|
2023-04-17 14:15:29 +00:00
|
|
|
aOrigin, aTextStyle );
|
2023-02-23 22:58:37 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 17:04:05 +00:00
|
|
|
|
|
|
|
struct GLYPH_CACHE_KEY {
|
|
|
|
FT_Face face;
|
|
|
|
hb_codepoint_t codepoint;
|
|
|
|
bool fakeItalic;
|
|
|
|
bool fakeBold;
|
|
|
|
bool mirror;
|
|
|
|
EDA_ANGLE angle;
|
|
|
|
|
|
|
|
bool operator==(const GLYPH_CACHE_KEY& rhs ) const
|
|
|
|
{
|
|
|
|
return face == rhs.face && codepoint == rhs.codepoint
|
|
|
|
&& fakeItalic == rhs.fakeItalic && fakeBold == rhs.fakeBold
|
|
|
|
&& mirror == rhs.mirror && angle == rhs.angle;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template <>
|
|
|
|
struct hash<GLYPH_CACHE_KEY>
|
|
|
|
{
|
|
|
|
std::size_t operator()( const GLYPH_CACHE_KEY& k ) const
|
|
|
|
{
|
|
|
|
return hash<const void*>()( k.face ) ^ hash<unsigned>()( k.codepoint )
|
|
|
|
^ hash<int>()( k.fakeItalic ) ^ hash<int>()( k.fakeBold )
|
|
|
|
^ hash<int>()( k.mirror ) ^ hash<int>()( k.angle.AsTenthsOfADegree() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-17 14:15:29 +00:00
|
|
|
VECTOR2I OUTLINE_FONT::getTextAsGlyphsUnlocked( BOX2I* aBBox,
|
|
|
|
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
|
2023-02-23 22:58:37 +00:00
|
|
|
{
|
|
|
|
VECTOR2D glyphSize = aSize;
|
2022-01-15 12:51:09 +00:00
|
|
|
FT_Face face = m_face;
|
|
|
|
double scaler = faceSize();
|
|
|
|
|
|
|
|
if( IsSubscript( aTextStyle ) || IsSuperscript( aTextStyle ) )
|
|
|
|
{
|
|
|
|
scaler = subscriptSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// set glyph resolution so that FT_Load_Glyph() results are good enough for decomposing
|
|
|
|
FT_Set_Char_Size( face, 0, scaler, GLYPH_RESOLUTION, 0 );
|
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
hb_buffer_t* buf = hb_buffer_create();
|
2023-01-07 13:12:02 +00:00
|
|
|
hb_buffer_add_utf8( buf, UTF8( aText ).c_str(), -1, 0, -1 );
|
2022-04-25 15:33:43 +00:00
|
|
|
hb_buffer_guess_segment_properties( buf ); // guess direction, script, and language based on
|
|
|
|
// contents
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2022-01-15 12:51:09 +00:00
|
|
|
hb_font_t* referencedFont = hb_ft_font_create_referenced( face );
|
2022-01-01 01:21:03 +00:00
|
|
|
hb_ft_font_set_funcs( referencedFont );
|
|
|
|
hb_shape( referencedFont, buf, nullptr, 0 );
|
|
|
|
|
2023-11-18 04:59:43 +00:00
|
|
|
unsigned int glyphCount;
|
|
|
|
hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( buf, &glyphCount );
|
|
|
|
hb_glyph_position_t* glyphPos = hb_buffer_get_glyph_positions( buf, &glyphCount );
|
|
|
|
|
2022-01-15 12:51:09 +00:00
|
|
|
VECTOR2D scaleFactor( glyphSize.x / faceSize(), -glyphSize.y / faceSize() );
|
|
|
|
scaleFactor = scaleFactor * m_outlineFontSizeCompensation;
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
VECTOR2I cursor( 0, 0 );
|
|
|
|
|
2023-05-26 22:03:14 +00:00
|
|
|
if( aGlyphs )
|
|
|
|
aGlyphs->reserve( glyphCount );
|
|
|
|
|
2024-01-12 17:04:05 +00:00
|
|
|
// GLYPH_DATA is a collection of all outlines in the glyph; for example the 'o' glyph
|
|
|
|
// generally contains 2 contours, one for the glyph outline and one for the hole
|
|
|
|
static std::unordered_map<GLYPH_CACHE_KEY, GLYPH_DATA> s_glyphCache;
|
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
for( unsigned int i = 0; i < glyphCount; i++ )
|
|
|
|
{
|
2022-04-23 13:19:50 +00:00
|
|
|
// Don't process glyphs that were already included in a previous cluster
|
|
|
|
if( i > 0 && glyphInfo[i].cluster == glyphInfo[i-1].cluster )
|
|
|
|
continue;
|
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
if( aGlyphs )
|
|
|
|
{
|
2024-01-12 17:04:05 +00:00
|
|
|
GLYPH_CACHE_KEY key = { face, glyphInfo[i].codepoint, m_fakeItal, m_fakeBold,
|
|
|
|
aMirror, aAngle };
|
|
|
|
GLYPH_DATA& glyphData = s_glyphCache[ key ];
|
2023-01-25 21:10:31 +00:00
|
|
|
|
2024-01-12 17:04:05 +00:00
|
|
|
if( glyphData.m_Contours.empty() )
|
|
|
|
{
|
|
|
|
if( m_fakeItal )
|
|
|
|
{
|
|
|
|
FT_Matrix matrix;
|
|
|
|
// Create a 12 degree slant
|
|
|
|
const float angle = (float)( -M_PI * 12.0f ) / 180.0f;
|
|
|
|
matrix.xx = (FT_Fixed) ( cos( angle ) * 0x10000L );
|
|
|
|
matrix.xy = (FT_Fixed) ( -sin( angle ) * 0x10000L );
|
|
|
|
matrix.yx = (FT_Fixed) ( 0 * 0x10000L ); // Don't rotate in the y direction
|
|
|
|
matrix.yy = (FT_Fixed) ( 1 * 0x10000L );
|
|
|
|
|
|
|
|
FT_Set_Transform( face, &matrix, nullptr );
|
|
|
|
}
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2024-01-12 17:04:05 +00:00
|
|
|
FT_Load_Glyph( face, glyphInfo[i].codepoint, FT_LOAD_NO_BITMAP );
|
2023-01-25 21:10:31 +00:00
|
|
|
|
2024-01-12 17:04:05 +00:00
|
|
|
if( m_fakeBold )
|
|
|
|
FT_Outline_Embolden( &face->glyph->outline, 1 << 6 );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2024-01-12 17:04:05 +00:00
|
|
|
OUTLINE_DECOMPOSER decomposer( face->glyph->outline );
|
2024-02-05 15:33:03 +00:00
|
|
|
|
2024-01-12 17:04:05 +00:00
|
|
|
if( !decomposer.OutlineToSegments( &glyphData.m_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 } );
|
|
|
|
|
|
|
|
glyphData.m_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 } );
|
|
|
|
glyphData.m_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 } );
|
|
|
|
glyphData.m_Contours.push_back( hole );
|
|
|
|
}
|
2024-02-05 15:33:03 +00:00
|
|
|
}
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
std::unique_ptr<OUTLINE_GLYPH> glyph = std::make_unique<OUTLINE_GLYPH>();
|
|
|
|
std::vector<SHAPE_LINE_CHAIN> holes;
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2024-01-12 17:04:05 +00:00
|
|
|
for( CONTOUR& c : glyphData.m_Contours )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2024-02-05 15:30:44 +00:00
|
|
|
std::vector<VECTOR2D> points = c.m_Points;
|
|
|
|
SHAPE_LINE_CHAIN shape;
|
2022-01-04 23:00:00 +00:00
|
|
|
|
2023-05-26 22:03:14 +00:00
|
|
|
shape.ReservePoints( points.size() );
|
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
for( const VECTOR2D& v : points )
|
|
|
|
{
|
|
|
|
VECTOR2D pt( v + cursor );
|
2022-01-04 23:00:00 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
if( IsSubscript( aTextStyle ) )
|
2022-01-15 12:51:09 +00:00
|
|
|
pt.y += m_subscriptVerticalOffset * scaler;
|
2022-01-07 17:42:43 +00:00
|
|
|
else if( IsSuperscript( aTextStyle ) )
|
2022-01-15 12:51:09 +00:00
|
|
|
pt.y += m_superscriptVerticalOffset * scaler;
|
2022-01-04 23:00:00 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
pt *= scaleFactor;
|
|
|
|
pt += aPosition;
|
2022-01-04 23:00:00 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
if( aMirror )
|
|
|
|
pt.x = aOrigin.x - ( pt.x - aOrigin.x );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
if( !aAngle.IsZero() )
|
|
|
|
RotatePoint( pt, aOrigin, aAngle );
|
2022-01-04 23:00:00 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
shape.Append( pt.x, pt.y );
|
|
|
|
}
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
shape.SetClosed( true );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
if( contourIsHole( c ) )
|
|
|
|
holes.push_back( std::move( shape ) );
|
|
|
|
else
|
|
|
|
glyph->AddOutline( std::move( shape ) );
|
|
|
|
}
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
for( SHAPE_LINE_CHAIN& hole : holes )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2022-01-07 17:42:43 +00:00
|
|
|
if( hole.PointCount() )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2022-01-07 17:42:43 +00:00
|
|
|
for( int ii = 0; ii < glyph->OutlineCount(); ++ii )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2022-01-07 17:42:43 +00:00
|
|
|
if( glyph->Outline( ii ).PointInside( hole.GetPoint( 0 ) ) )
|
|
|
|
{
|
|
|
|
glyph->AddHole( std::move( hole ), ii );
|
|
|
|
break;
|
|
|
|
}
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-12 17:04:05 +00:00
|
|
|
if( glyphData.m_TriangulationData.empty() )
|
|
|
|
{
|
|
|
|
glyph->CacheTriangulation( false, false );
|
|
|
|
glyphData.m_TriangulationData = glyph->GetTriangulationData();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glyph->CacheTriangulation( glyphData.m_TriangulationData );
|
|
|
|
}
|
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
aGlyphs->push_back( std::move( glyph ) );
|
|
|
|
}
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2022-01-15 12:51:09 +00:00
|
|
|
hb_glyph_position_t& pos = glyphPos[i];
|
|
|
|
cursor.x += ( pos.x_advance * GLYPH_SIZE_SCALER );
|
|
|
|
cursor.y += ( pos.y_advance * GLYPH_SIZE_SCALER );
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 00:24:08 +00:00
|
|
|
int ascender = abs( face->size->metrics.ascender * GLYPH_SIZE_SCALER );
|
|
|
|
int descender = abs( face->size->metrics.descender * GLYPH_SIZE_SCALER );
|
|
|
|
VECTOR2I extents( cursor.x * scaleFactor.x, ( ascender + descender ) * abs( scaleFactor.y ) );
|
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
hb_buffer_destroy( buf );
|
2022-09-13 15:55:00 +00:00
|
|
|
hb_font_destroy( referencedFont );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
VECTOR2I cursorDisplacement( cursor.x * scaleFactor.x, -cursor.y * scaleFactor.y );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
if( aBBox )
|
2022-04-20 11:00:19 +00:00
|
|
|
aBBox->Merge( aPosition + extents );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
return VECTOR2I( aPosition.x + cursorDisplacement.x, aPosition.y + cursorDisplacement.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef OUTLINEFONT_RENDER_AS_PIXELS
|
|
|
|
#ifdef OUTLINEFONT_RENDER_AS_PIXELS
|
|
|
|
/*
|
|
|
|
* WIP: eeschema (and PDF output?) should use pixel rendering instead of linear segmentation
|
|
|
|
*/
|
2022-01-25 22:33:37 +00:00
|
|
|
void OUTLINE_FONT::RenderToOpenGLCanvas( KIGFX::OPENGL_GAL& aGal, const wxString& aString,
|
2022-01-04 23:00:00 +00:00
|
|
|
const VECTOR2D& aGlyphSize, const VECTOR2I& aPosition,
|
2022-01-01 01:21:03 +00:00
|
|
|
const EDA_ANGLE& aOrientation, bool aIsMirrored ) const
|
|
|
|
{
|
|
|
|
hb_buffer_t* buf = hb_buffer_create();
|
2023-01-07 13:12:02 +00:00
|
|
|
hb_buffer_add_utf8( buf, UTF8( aString ).c_str(), -1, 0, -1 );
|
2022-01-15 12:51:09 +00:00
|
|
|
hb_buffer_guess_segment_properties( buf ); // guess direction, script, and language based on contents
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
unsigned int glyphCount;
|
|
|
|
hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( buf, &glyphCount );
|
|
|
|
hb_glyph_position_t* glyphPos = hb_buffer_get_glyph_positions( buf, &glyphCount );
|
2023-04-17 14:15:29 +00:00
|
|
|
|
2023-02-17 14:39:52 +00:00
|
|
|
std::lock_guard<std::mutex> guard( m_freeTypeMutex );
|
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
hb_font_t* referencedFont = hb_ft_font_create_referenced( m_face );
|
|
|
|
|
|
|
|
hb_ft_font_set_funcs( referencedFont );
|
|
|
|
hb_shape( referencedFont, buf, nullptr, 0 );
|
|
|
|
|
|
|
|
const double mirror_factor = ( aIsMirrored ? 1 : -1 );
|
|
|
|
const double x_scaleFactor = mirror_factor * aGlyphSize.x / mScaler;
|
|
|
|
const double y_scaleFactor = aGlyphSize.y / mScaler;
|
|
|
|
|
|
|
|
hb_position_t cursor_x = 0;
|
|
|
|
hb_position_t cursor_y = 0;
|
|
|
|
|
|
|
|
for( unsigned int i = 0; i < glyphCount; i++ )
|
|
|
|
{
|
|
|
|
hb_glyph_position_t& pos = glyphPos[i];
|
|
|
|
int codepoint = glyphInfo[i].codepoint;
|
|
|
|
|
|
|
|
FT_Error e = FT_Load_Glyph( m_face, codepoint, FT_LOAD_DEFAULT );
|
|
|
|
// TODO handle FT_Load_Glyph error
|
|
|
|
|
|
|
|
FT_Glyph glyph;
|
|
|
|
e = FT_Get_Glyph( m_face->glyph, &glyph );
|
|
|
|
// TODO handle FT_Get_Glyph error
|
|
|
|
|
|
|
|
wxPoint pt( aPosition );
|
|
|
|
pt.x += ( cursor_x >> 6 ) * x_scaleFactor;
|
|
|
|
pt.y += ( cursor_y >> 6 ) * y_scaleFactor;
|
|
|
|
|
|
|
|
cursor_x += pos.x_advance;
|
|
|
|
cursor_y += pos.y_advance;
|
|
|
|
}
|
|
|
|
|
|
|
|
hb_buffer_destroy( buf );
|
|
|
|
}
|
|
|
|
#endif //OUTLINEFONT_RENDER_AS_PIXELS
|