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
|
|
|
*
|
|
|
|
* Outline font class
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2024-01-12 02:26:42 +00:00
|
|
|
#include <advanced_config.h>
|
2022-01-01 01:21:03 +00:00
|
|
|
#include <font/outline_decomposer.h>
|
|
|
|
#include <bezier_curves.h>
|
|
|
|
|
|
|
|
using namespace KIFONT;
|
|
|
|
|
|
|
|
OUTLINE_DECOMPOSER::OUTLINE_DECOMPOSER( FT_Outline& aOutline ) :
|
2022-01-13 15:25:02 +00:00
|
|
|
m_outline( aOutline ),
|
|
|
|
m_contours( nullptr )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VECTOR2D toVector2D( const FT_Vector* aFreeTypeVector )
|
|
|
|
{
|
2024-01-12 17:04:05 +00:00
|
|
|
return VECTOR2D( (double) aFreeTypeVector->x * GLYPH_SIZE_SCALER,
|
|
|
|
(double) aFreeTypeVector->y * GLYPH_SIZE_SCALER );
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OUTLINE_DECOMPOSER::newContour()
|
|
|
|
{
|
|
|
|
CONTOUR contour;
|
2022-07-31 16:35:37 +00:00
|
|
|
contour.m_Orientation = FT_Outline_Get_Orientation( &m_outline );
|
2022-01-01 01:21:03 +00:00
|
|
|
m_contours->push_back( contour );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OUTLINE_DECOMPOSER::addContourPoint( const VECTOR2D& p )
|
|
|
|
{
|
|
|
|
// don't add repeated points
|
2022-07-31 16:35:37 +00:00
|
|
|
if( m_contours->back().m_Points.empty() || m_contours->back().m_Points.back() != p )
|
|
|
|
m_contours->back().m_Points.push_back( p );
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OUTLINE_DECOMPOSER::moveTo( const FT_Vector* aEndPoint, void* aCallbackData )
|
|
|
|
{
|
|
|
|
OUTLINE_DECOMPOSER* decomposer = static_cast<OUTLINE_DECOMPOSER*>( aCallbackData );
|
|
|
|
|
2022-01-15 12:51:09 +00:00
|
|
|
decomposer->m_lastEndPoint = toVector2D( aEndPoint );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
decomposer->newContour();
|
|
|
|
decomposer->addContourPoint( decomposer->m_lastEndPoint );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OUTLINE_DECOMPOSER::lineTo( const FT_Vector* aEndPoint, void* aCallbackData )
|
|
|
|
{
|
|
|
|
OUTLINE_DECOMPOSER* decomposer = static_cast<OUTLINE_DECOMPOSER*>( aCallbackData );
|
|
|
|
|
2022-01-15 12:51:09 +00:00
|
|
|
decomposer->m_lastEndPoint = toVector2D( aEndPoint );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
decomposer->addContourPoint( decomposer->m_lastEndPoint );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OUTLINE_DECOMPOSER::quadraticTo( const FT_Vector* aControlPoint, const FT_Vector* aEndPoint,
|
|
|
|
void* aCallbackData )
|
|
|
|
{
|
|
|
|
return cubicTo( aControlPoint, nullptr, aEndPoint, aCallbackData );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OUTLINE_DECOMPOSER::cubicTo( const FT_Vector* aFirstControlPoint,
|
|
|
|
const FT_Vector* aSecondControlPoint, const FT_Vector* aEndPoint,
|
|
|
|
void* aCallbackData )
|
|
|
|
{
|
|
|
|
OUTLINE_DECOMPOSER* decomposer = static_cast<OUTLINE_DECOMPOSER*>( aCallbackData );
|
|
|
|
|
2024-02-05 15:30:44 +00:00
|
|
|
std::vector<VECTOR2D> bezier;
|
2022-01-01 01:21:03 +00:00
|
|
|
bezier.push_back( decomposer->m_lastEndPoint );
|
|
|
|
bezier.push_back( toVector2D( aFirstControlPoint ) );
|
|
|
|
|
|
|
|
if( aSecondControlPoint )
|
|
|
|
{
|
|
|
|
// aSecondControlPoint == nullptr for quadratic Beziers
|
|
|
|
bezier.push_back( toVector2D( aSecondControlPoint ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
bezier.push_back( toVector2D( aEndPoint ) );
|
|
|
|
|
2024-02-05 15:30:44 +00:00
|
|
|
std::vector<VECTOR2D> result;
|
2024-06-18 04:47:06 +00:00
|
|
|
BEZIER_POLY converter( bezier );
|
|
|
|
converter.GetPoly( result, ADVANCED_CFG::GetCfg().m_FontErrorSize );
|
2024-02-05 15:30:44 +00:00
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
for( const VECTOR2D& p : result )
|
|
|
|
decomposer->addContourPoint( p );
|
|
|
|
|
2022-01-15 12:51:09 +00:00
|
|
|
decomposer->m_lastEndPoint = toVector2D( aEndPoint );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-05 15:33:03 +00:00
|
|
|
bool OUTLINE_DECOMPOSER::OutlineToSegments( std::vector<CONTOUR>* aContours )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
|
|
|
m_contours = aContours;
|
|
|
|
|
|
|
|
FT_Outline_Funcs callbacks;
|
|
|
|
|
|
|
|
callbacks.move_to = moveTo;
|
|
|
|
callbacks.line_to = lineTo;
|
|
|
|
callbacks.conic_to = quadraticTo;
|
|
|
|
callbacks.cubic_to = cubicTo;
|
|
|
|
callbacks.shift = 0;
|
|
|
|
callbacks.delta = 0;
|
|
|
|
|
|
|
|
FT_Error e = FT_Outline_Decompose( &m_outline, &callbacks, this );
|
|
|
|
|
|
|
|
if( e )
|
2024-02-05 15:33:03 +00:00
|
|
|
return false;
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
for( CONTOUR& c : *m_contours )
|
2022-07-31 16:35:37 +00:00
|
|
|
c.m_Winding = winding( c.m_Points );
|
2024-02-05 15:33:03 +00:00
|
|
|
|
|
|
|
return true;
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-05 15:30:44 +00:00
|
|
|
int OUTLINE_DECOMPOSER::winding( const std::vector<VECTOR2D>& aContour ) const
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
|
|
|
// -1 == counterclockwise, 1 == clockwise
|
|
|
|
|
|
|
|
const int cw = 1;
|
|
|
|
const int ccw = -1;
|
|
|
|
|
|
|
|
if( aContour.size() < 2 )
|
|
|
|
{
|
|
|
|
// zero or one points, so not a clockwise contour - in fact not a contour at all
|
|
|
|
//
|
|
|
|
// It could also be argued that a contour needs 3 extremum points at a minimum to be
|
|
|
|
// considered a proper contour (ie. a glyph (subpart) outline, or a hole)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-25 21:12:47 +00:00
|
|
|
double sum = 0.0;
|
|
|
|
size_t len = aContour.size();
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2023-01-25 21:12:47 +00:00
|
|
|
for( size_t i = 0; i < len - 1; i++ )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2023-01-25 21:12:47 +00:00
|
|
|
VECTOR2D p1 = aContour[ i ];
|
|
|
|
VECTOR2D p2 = aContour[ i + 1 ];
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2023-01-25 21:12:47 +00:00
|
|
|
sum += ( ( p2.x - p1.x ) * ( p2.y + p1.y ) );
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 21:12:47 +00:00
|
|
|
sum += ( ( aContour[0].x - aContour[len - 1].x ) * ( aContour[0].y + aContour[len - 1].y ) );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2023-01-25 21:12:47 +00:00
|
|
|
if( sum > 0.0 )
|
2022-01-01 01:21:03 +00:00
|
|
|
return cw;
|
2023-01-25 21:12:47 +00:00
|
|
|
if( sum < 0.0 )
|
2022-01-01 01:21:03 +00:00
|
|
|
return ccw;
|
2023-01-25 21:12:47 +00:00
|
|
|
|
|
|
|
return 0;
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|