2021-12-29 17:31:40 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 Ola Rinta-Koski
|
2023-04-17 14:15:29 +00:00
|
|
|
* Copyright (C) 2021-2023 Kicad Developers, see AUTHORS.txt for contributors.
|
2021-12-29 17:31:40 +00:00
|
|
|
*
|
|
|
|
* Font abstract base 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
|
|
|
|
*/
|
|
|
|
|
2023-03-05 17:25:36 +00:00
|
|
|
#include <list>
|
|
|
|
#include <mutex>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2023-05-26 12:45:25 +00:00
|
|
|
#include <macros.h>
|
2021-12-31 14:07:24 +00:00
|
|
|
#include <string_utils.h>
|
|
|
|
#include <gal/graphics_abstraction_layer.h>
|
|
|
|
#include <font/stroke_font.h>
|
2022-01-01 01:21:03 +00:00
|
|
|
#include <font/outline_font.h>
|
2021-12-31 14:07:24 +00:00
|
|
|
#include <trigo.h>
|
|
|
|
#include <markup_parser.h>
|
2021-12-29 17:31:40 +00:00
|
|
|
|
2023-05-26 12:45:25 +00:00
|
|
|
// The "official" name of the Kicad stroke font (always existing)
|
2022-01-13 13:31:00 +00:00
|
|
|
#include <font/kicad_font_name.h>
|
2023-05-26 12:45:25 +00:00
|
|
|
#include <wx/tokenzr.h>
|
2022-01-04 11:52:29 +00:00
|
|
|
|
|
|
|
// markup_parser.h includes pegtl.hpp which includes windows.h... which leaks #define DrawText
|
|
|
|
#undef DrawText
|
|
|
|
|
|
|
|
|
2021-12-29 17:31:40 +00:00
|
|
|
using namespace KIFONT;
|
|
|
|
|
2023-08-06 19:20:53 +00:00
|
|
|
METRICS g_defaultMetrics;
|
|
|
|
|
|
|
|
|
|
|
|
const METRICS& METRICS::Default()
|
|
|
|
{
|
|
|
|
return g_defaultMetrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-31 14:07:24 +00:00
|
|
|
FONT* FONT::s_defaultFont = nullptr;
|
|
|
|
|
|
|
|
std::map< std::tuple<wxString, bool, bool>, FONT*> FONT::s_fontMap;
|
2021-12-29 17:31:40 +00:00
|
|
|
|
2023-03-05 17:25:36 +00:00
|
|
|
class MARKUP_CACHE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct ENTRY
|
|
|
|
{
|
|
|
|
std::string source;
|
|
|
|
std::unique_ptr<MARKUP::NODE> root;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::pair<wxString, ENTRY> CACHE_ENTRY;
|
|
|
|
|
|
|
|
MARKUP_CACHE( size_t aMaxSize ) :
|
|
|
|
m_maxSize( aMaxSize )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ENTRY& Put( const CACHE_ENTRY::first_type& aQuery, ENTRY&& aResult )
|
|
|
|
{
|
|
|
|
auto it = m_cache.find( aQuery );
|
|
|
|
|
|
|
|
m_cacheMru.emplace_front( CACHE_ENTRY( aQuery, std::move( aResult ) ) );
|
|
|
|
|
|
|
|
if( it != m_cache.end() )
|
|
|
|
{
|
|
|
|
m_cacheMru.erase( it->second );
|
|
|
|
m_cache.erase( it );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_cache[aQuery] = m_cacheMru.begin();
|
|
|
|
|
|
|
|
if( m_cache.size() > m_maxSize )
|
|
|
|
{
|
|
|
|
auto last = m_cacheMru.end();
|
|
|
|
last--;
|
|
|
|
m_cache.erase( last->first );
|
|
|
|
m_cacheMru.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_cacheMru.begin()->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
ENTRY* Get( const CACHE_ENTRY::first_type& aQuery )
|
|
|
|
{
|
|
|
|
auto it = m_cache.find( aQuery );
|
|
|
|
|
|
|
|
if( it == m_cache.end() )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
m_cacheMru.splice( m_cacheMru.begin(), m_cacheMru, it->second );
|
|
|
|
|
|
|
|
return &m_cacheMru.begin()->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
m_cacheMru.clear();
|
|
|
|
m_cache.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t m_maxSize;
|
|
|
|
std::list<CACHE_ENTRY> m_cacheMru;
|
|
|
|
std::unordered_map<wxString, std::list<CACHE_ENTRY>::iterator> m_cache;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static MARKUP_CACHE s_markupCache( 1024 );
|
|
|
|
static std::mutex s_markupCacheMutex;
|
|
|
|
|
2021-12-29 17:31:40 +00:00
|
|
|
|
|
|
|
FONT::FONT()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-31 14:07:24 +00:00
|
|
|
FONT* FONT::getDefaultFont()
|
|
|
|
{
|
|
|
|
if( !s_defaultFont )
|
|
|
|
s_defaultFont = STROKE_FONT::LoadFont( wxEmptyString );
|
|
|
|
|
|
|
|
return s_defaultFont;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FONT* FONT::GetFont( const wxString& aFontName, bool aBold, bool aItalic )
|
|
|
|
{
|
2022-01-13 13:31:00 +00:00
|
|
|
if( aFontName.empty() || aFontName.StartsWith( KICAD_FONT_NAME ) )
|
2021-12-31 14:07:24 +00:00
|
|
|
return getDefaultFont();
|
|
|
|
|
|
|
|
std::tuple<wxString, bool, bool> key = { aFontName, aBold, aItalic };
|
|
|
|
|
2023-08-26 13:21:12 +00:00
|
|
|
FONT* font = nullptr;
|
|
|
|
|
|
|
|
if( s_fontMap.find( key ) != s_fontMap.end() )
|
|
|
|
font = s_fontMap[key];
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-01-04 00:44:59 +00:00
|
|
|
if( !font )
|
|
|
|
font = OUTLINE_FONT::LoadFont( aFontName, aBold, aItalic );
|
|
|
|
|
|
|
|
if( !font )
|
2021-12-31 14:07:24 +00:00
|
|
|
font = getDefaultFont();
|
|
|
|
|
2022-01-04 00:44:59 +00:00
|
|
|
s_fontMap[key] = font;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FONT::IsStroke( const wxString& aFontName )
|
|
|
|
{
|
2022-01-09 00:44:52 +00:00
|
|
|
// This would need a more complex implementation if we ever support more stroke fonts
|
|
|
|
// than the KiCad Font.
|
2022-01-13 13:31:00 +00:00
|
|
|
return aFontName == _( "Default Font" ) || aFontName == KICAD_FONT_NAME;
|
2021-12-31 14:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-25 22:33:37 +00:00
|
|
|
void FONT::getLinePositions( const wxString& aText, const VECTOR2I& aPosition,
|
2022-01-04 23:00:00 +00:00
|
|
|
wxArrayString& aTextLines, std::vector<VECTOR2I>& aPositions,
|
2023-08-06 19:20:53 +00:00
|
|
|
std::vector<VECTOR2I>& aExtents, const TEXT_ATTRIBUTES& aAttrs,
|
|
|
|
const METRICS& aFontMetrics ) const
|
2021-12-31 14:07:24 +00:00
|
|
|
{
|
2022-01-04 23:00:00 +00:00
|
|
|
wxStringSplit( aText, aTextLines, '\n' );
|
|
|
|
int lineCount = aTextLines.Count();
|
|
|
|
aPositions.reserve( lineCount );
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2023-08-06 19:20:53 +00:00
|
|
|
int interline = GetInterline( aAttrs.m_Size.y, aFontMetrics ) * aAttrs.m_LineSpacing;
|
2022-01-04 23:00:00 +00:00
|
|
|
int height = 0;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
for( int i = 0; i < lineCount; i++ )
|
2021-12-31 14:07:24 +00:00
|
|
|
{
|
2022-01-07 17:42:43 +00:00
|
|
|
VECTOR2I pos( aPosition.x, aPosition.y + i * interline );
|
|
|
|
VECTOR2I end = boundingBoxSingleLine( nullptr, aTextLines[i], pos, aAttrs.m_Size,
|
2023-08-06 19:20:53 +00:00
|
|
|
aAttrs.m_Italic, aFontMetrics );
|
2022-01-07 17:42:43 +00:00
|
|
|
VECTOR2I bBox( end - pos );
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
aExtents.push_back( bBox );
|
2021-12-31 14:07:24 +00:00
|
|
|
|
|
|
|
if( i == 0 )
|
2022-03-30 14:49:59 +00:00
|
|
|
height += ( aAttrs.m_Size.y * 1.17 ); // 1.17 is a fudge to match 6.0 positioning
|
2021-12-31 14:07:24 +00:00
|
|
|
else
|
|
|
|
height += interline;
|
|
|
|
}
|
|
|
|
|
2022-01-11 13:47:21 +00:00
|
|
|
VECTOR2I offset( 0, 0 );
|
2021-12-31 14:07:24 +00:00
|
|
|
offset.y += aAttrs.m_Size.y;
|
|
|
|
|
2023-05-17 20:30:31 +00:00
|
|
|
if( IsStroke() )
|
|
|
|
{
|
|
|
|
// Fudge factors to match 6.0 positioning
|
|
|
|
offset.x += aAttrs.m_StrokeWidth / 1.52;
|
|
|
|
offset.y -= aAttrs.m_StrokeWidth * 0.052;
|
|
|
|
}
|
|
|
|
|
2021-12-31 14:07:24 +00:00
|
|
|
switch( aAttrs.m_Valign )
|
|
|
|
{
|
|
|
|
case GR_TEXT_V_ALIGN_TOP: break;
|
|
|
|
case GR_TEXT_V_ALIGN_CENTER: offset.y -= height / 2; break;
|
|
|
|
case GR_TEXT_V_ALIGN_BOTTOM: offset.y -= height; break;
|
2024-02-16 12:54:28 +00:00
|
|
|
case GR_TEXT_V_ALIGN_INDETERMINATE:
|
|
|
|
wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
|
|
|
|
break;
|
2021-12-31 14:07:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
for( int i = 0; i < lineCount; i++ )
|
2021-12-31 14:07:24 +00:00
|
|
|
{
|
2022-01-07 17:42:43 +00:00
|
|
|
VECTOR2I lineSize = aExtents.at( i );
|
2022-01-11 13:47:21 +00:00
|
|
|
VECTOR2I lineOffset( offset );
|
2022-01-04 23:00:00 +00:00
|
|
|
|
2021-12-31 14:07:24 +00:00
|
|
|
lineOffset.y += i * interline;
|
|
|
|
|
|
|
|
switch( aAttrs.m_Halign )
|
|
|
|
{
|
2024-02-16 12:54:28 +00:00
|
|
|
case GR_TEXT_H_ALIGN_LEFT: break;
|
|
|
|
case GR_TEXT_H_ALIGN_CENTER: lineOffset.x = -lineSize.x / 2; break;
|
|
|
|
case GR_TEXT_H_ALIGN_RIGHT: lineOffset.x = -( lineSize.x + offset.x ); break;
|
|
|
|
case GR_TEXT_H_ALIGN_INDETERMINATE:
|
|
|
|
wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
|
|
|
|
break;
|
2021-12-31 14:07:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
aPositions.push_back( aPosition + lineOffset );
|
2021-12-31 14:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw a string.
|
|
|
|
*
|
|
|
|
* @param aGal
|
|
|
|
* @param aText is the text to be drawn.
|
2022-01-04 23:00:00 +00:00
|
|
|
* @param aPosition is the text object position in world coordinates.
|
|
|
|
* @param aCursor is the current text position (for multiple text blocks within a single text
|
|
|
|
* object, such as a run of superscript characters)
|
|
|
|
* @param aAttrs are the styling attributes of the text, including its rotation
|
2021-12-31 14:07:24 +00:00
|
|
|
*/
|
2022-01-25 22:33:37 +00:00
|
|
|
void FONT::Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
|
2023-08-06 19:20:53 +00:00
|
|
|
const VECTOR2I& aCursor, const TEXT_ATTRIBUTES& aAttrs,
|
|
|
|
const METRICS& aFontMetrics ) const
|
2021-12-31 14:07:24 +00:00
|
|
|
{
|
|
|
|
if( !aGal || aText.empty() )
|
2022-01-07 17:42:43 +00:00
|
|
|
return;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
VECTOR2I position( aPosition - aCursor );
|
2021-12-31 14:07:24 +00:00
|
|
|
|
|
|
|
// Split multiline strings into separate ones and draw them line by line
|
|
|
|
wxArrayString strings_list;
|
2022-01-04 23:00:00 +00:00
|
|
|
std::vector<VECTOR2I> positions;
|
2022-01-07 17:42:43 +00:00
|
|
|
std::vector<VECTOR2I> extents;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2023-08-06 19:20:53 +00:00
|
|
|
getLinePositions( aText, position, strings_list, positions, extents, aAttrs, aFontMetrics );
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
aGal->SetLineWidth( aAttrs.m_StrokeWidth );
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
for( size_t i = 0; i < strings_list.GetCount(); i++ )
|
|
|
|
{
|
2022-01-07 17:42:43 +00:00
|
|
|
drawSingleLineText( aGal, nullptr, strings_list[i], positions[i], aAttrs.m_Size,
|
2022-08-27 19:57:34 +00:00
|
|
|
aAttrs.m_Angle, aAttrs.m_Mirrored, aPosition, aAttrs.m_Italic,
|
2023-08-06 19:20:53 +00:00
|
|
|
aAttrs.m_Underlined, aFontMetrics );
|
2021-12-31 14:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return position of cursor for drawing next substring
|
|
|
|
*/
|
2022-01-07 17:42:43 +00:00
|
|
|
VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
|
2023-03-05 17:25:36 +00:00
|
|
|
const MARKUP::NODE* aNode, const VECTOR2I& aPosition,
|
2023-05-17 20:30:31 +00:00
|
|
|
const KIFONT::FONT* aFont, const VECTOR2I& aSize, const EDA_ANGLE& aAngle,
|
2023-08-06 19:20:53 +00:00
|
|
|
bool aMirror, const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle,
|
|
|
|
const METRICS& aFontMetrics )
|
2021-12-31 14:07:24 +00:00
|
|
|
{
|
2023-05-17 20:30:31 +00:00
|
|
|
VECTOR2I nextPosition = aPosition;
|
2023-04-17 14:15:29 +00:00
|
|
|
bool drawUnderline = false;
|
|
|
|
bool drawOverbar = false;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-03-30 13:25:34 +00:00
|
|
|
if( aNode )
|
|
|
|
{
|
2022-02-08 18:50:53 +00:00
|
|
|
TEXT_STYLE_FLAGS textStyle = aTextStyle;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-02-08 18:50:53 +00:00
|
|
|
if( !aNode->is_root() )
|
|
|
|
{
|
|
|
|
if( aNode->isSubscript() )
|
2023-04-17 14:15:29 +00:00
|
|
|
textStyle |= TEXT_STYLE::SUBSCRIPT;
|
2022-02-08 18:50:53 +00:00
|
|
|
else if( aNode->isSuperscript() )
|
2023-04-17 14:15:29 +00:00
|
|
|
textStyle |= TEXT_STYLE::SUPERSCRIPT;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-02-08 18:50:53 +00:00
|
|
|
if( aNode->isOverbar() )
|
2023-04-17 14:15:29 +00:00
|
|
|
drawOverbar = true;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-02-08 18:50:53 +00:00
|
|
|
if( aNode->has_content() )
|
|
|
|
{
|
2022-02-26 19:02:34 +00:00
|
|
|
BOX2I bbox;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-02-26 22:56:10 +00:00
|
|
|
nextPosition = aFont->GetTextAsGlyphs( &bbox, aGlyphs, aNode->asWxString(), aSize,
|
2023-05-12 19:37:27 +00:00
|
|
|
nextPosition, aAngle, aMirror, aOrigin,
|
2022-02-26 19:02:34 +00:00
|
|
|
textStyle );
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-02-08 18:50:53 +00:00
|
|
|
if( aBoundingBox )
|
|
|
|
aBoundingBox->Merge( bbox );
|
|
|
|
}
|
2021-12-31 14:07:24 +00:00
|
|
|
}
|
2023-04-17 14:15:29 +00:00
|
|
|
else if( aTextStyle & TEXT_STYLE::UNDERLINE )
|
|
|
|
{
|
|
|
|
drawUnderline = true;
|
|
|
|
}
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-02-08 18:50:53 +00:00
|
|
|
for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
|
|
|
|
{
|
2023-03-05 17:25:36 +00:00
|
|
|
nextPosition = drawMarkup( aBoundingBox, aGlyphs, child.get(), nextPosition, aFont,
|
2023-08-06 19:20:53 +00:00
|
|
|
aSize, aAngle, aMirror, aOrigin, textStyle, aFontMetrics );
|
2022-02-08 18:50:53 +00:00
|
|
|
}
|
2021-12-31 14:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 14:15:29 +00:00
|
|
|
if( drawUnderline )
|
|
|
|
{
|
|
|
|
// Shorten the bar a little so its rounded ends don't make it over-long
|
|
|
|
double barTrim = aSize.x * 0.1;
|
2023-08-06 19:20:53 +00:00
|
|
|
double barOffset = aFontMetrics.GetUnderlineVerticalPosition( aSize.y );
|
2023-04-17 14:15:29 +00:00
|
|
|
|
|
|
|
VECTOR2D barStart( aPosition.x + barTrim, aPosition.y - barOffset );
|
|
|
|
VECTOR2D barEnd( nextPosition.x - barTrim, nextPosition.y - barOffset );
|
|
|
|
|
|
|
|
if( aGlyphs )
|
|
|
|
{
|
|
|
|
STROKE_GLYPH barGlyph;
|
|
|
|
|
|
|
|
barGlyph.AddPoint( barStart );
|
|
|
|
barGlyph.AddPoint( barEnd );
|
|
|
|
barGlyph.Finalize();
|
|
|
|
|
|
|
|
aGlyphs->push_back( barGlyph.Transform( { 1.0, 1.0 }, { 0, 0 }, false, aAngle, aMirror,
|
|
|
|
aOrigin ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( drawOverbar )
|
|
|
|
{
|
|
|
|
// Shorten the bar a little so its rounded ends don't make it over-long
|
|
|
|
double barTrim = aSize.x * 0.1;
|
2023-08-06 19:20:53 +00:00
|
|
|
double barOffset = aFontMetrics.GetOverbarVerticalPosition( aSize.y );
|
2023-04-17 14:15:29 +00:00
|
|
|
|
|
|
|
VECTOR2D barStart( aPosition.x + barTrim, aPosition.y - barOffset );
|
|
|
|
VECTOR2D barEnd( nextPosition.x - barTrim, nextPosition.y - barOffset );
|
|
|
|
|
|
|
|
if( aGlyphs )
|
|
|
|
{
|
|
|
|
STROKE_GLYPH barGlyph;
|
|
|
|
|
|
|
|
barGlyph.AddPoint( barStart );
|
|
|
|
barGlyph.AddPoint( barEnd );
|
|
|
|
barGlyph.Finalize();
|
|
|
|
|
|
|
|
aGlyphs->push_back( barGlyph.Transform( { 1.0, 1.0 }, { 0, 0 }, false, aAngle, aMirror,
|
|
|
|
aOrigin ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 14:07:24 +00:00
|
|
|
return nextPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
VECTOR2I FONT::drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
|
2022-01-25 22:33:37 +00:00
|
|
|
const wxString& aText, const VECTOR2I& aPosition, const VECTOR2I& aSize,
|
2023-05-17 20:30:31 +00:00
|
|
|
const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
|
2023-08-06 19:20:53 +00:00
|
|
|
TEXT_STYLE_FLAGS aTextStyle, const METRICS& aFontMetrics ) const
|
2022-01-04 11:52:29 +00:00
|
|
|
{
|
2023-03-05 17:25:36 +00:00
|
|
|
std::lock_guard<std::mutex> lock( s_markupCacheMutex );
|
|
|
|
|
|
|
|
MARKUP_CACHE::ENTRY* markup = s_markupCache.Get( aText );
|
|
|
|
|
|
|
|
if( !markup || !markup->root )
|
|
|
|
{
|
|
|
|
MARKUP_CACHE::ENTRY& cached = s_markupCache.Put( aText, {} );
|
|
|
|
|
|
|
|
cached.source = TO_UTF8( aText );
|
|
|
|
MARKUP::MARKUP_PARSER markupParser( &cached.source );
|
|
|
|
cached.root = markupParser.Parse();
|
|
|
|
markup = &cached;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( markup && markup->root );
|
2022-01-04 11:52:29 +00:00
|
|
|
|
2023-05-17 20:30:31 +00:00
|
|
|
return ::drawMarkup( aBoundingBox, aGlyphs, markup->root.get(), aPosition, this, aSize, aAngle,
|
2023-08-06 19:20:53 +00:00
|
|
|
aMirror, aOrigin, aTextStyle, aFontMetrics );
|
2022-01-04 11:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-25 22:33:37 +00:00
|
|
|
void FONT::drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxString& aText,
|
2022-01-11 13:47:21 +00:00
|
|
|
const VECTOR2I& aPosition, const VECTOR2I& aSize,
|
2022-01-07 17:42:43 +00:00
|
|
|
const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
|
2023-08-06 19:20:53 +00:00
|
|
|
bool aItalic, bool aUnderline, const METRICS& aFontMetrics ) const
|
2021-12-31 14:07:24 +00:00
|
|
|
{
|
|
|
|
if( !aGal )
|
2022-01-07 17:42:43 +00:00
|
|
|
return;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-01-04 11:52:29 +00:00
|
|
|
TEXT_STYLE_FLAGS textStyle = 0;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
if( aItalic )
|
2021-12-31 14:07:24 +00:00
|
|
|
textStyle |= TEXT_STYLE::ITALIC;
|
|
|
|
|
2022-08-27 19:57:34 +00:00
|
|
|
if( aUnderline )
|
|
|
|
textStyle |= TEXT_STYLE::UNDERLINE;
|
|
|
|
|
2022-01-04 00:44:59 +00:00
|
|
|
std::vector<std::unique_ptr<GLYPH>> glyphs;
|
2022-01-07 17:42:43 +00:00
|
|
|
|
2023-05-17 20:30:31 +00:00
|
|
|
(void) drawMarkup( aBoundingBox, &glyphs, aText, aPosition, aSize, aAngle, aMirror, aOrigin,
|
2023-08-06 19:20:53 +00:00
|
|
|
textStyle, aFontMetrics );
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-09-14 16:07:47 +00:00
|
|
|
aGal->DrawGlyphs( glyphs );
|
2021-12-31 14:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-25 22:33:37 +00:00
|
|
|
VECTOR2I FONT::StringBoundaryLimits( const wxString& aText, const VECTOR2I& aSize, int aThickness,
|
2023-08-06 19:20:53 +00:00
|
|
|
bool aBold, bool aItalic, const METRICS& aFontMetrics ) const
|
2022-01-07 14:49:17 +00:00
|
|
|
{
|
|
|
|
// TODO do we need to parse every time - have we already parsed?
|
2022-01-07 17:42:43 +00:00
|
|
|
BOX2I boundingBox;
|
|
|
|
TEXT_STYLE_FLAGS textStyle = 0;
|
2022-01-07 14:49:17 +00:00
|
|
|
|
|
|
|
if( aBold )
|
|
|
|
textStyle |= TEXT_STYLE::BOLD;
|
|
|
|
|
|
|
|
if( aItalic )
|
|
|
|
textStyle |= TEXT_STYLE::ITALIC;
|
|
|
|
|
2023-05-17 20:30:31 +00:00
|
|
|
(void) drawMarkup( &boundingBox, nullptr, aText, VECTOR2I(), aSize, ANGLE_0, false, VECTOR2I(),
|
2023-08-06 19:20:53 +00:00
|
|
|
textStyle, aFontMetrics );
|
2022-01-07 14:49:17 +00:00
|
|
|
|
|
|
|
if( IsStroke() )
|
|
|
|
{
|
|
|
|
// Inflate by a bit more than thickness/2 to catch diacriticals, descenders, etc.
|
2023-05-28 17:17:24 +00:00
|
|
|
boundingBox.Inflate( KiROUND( aThickness * 1.5 ) );
|
2022-01-07 14:49:17 +00:00
|
|
|
}
|
|
|
|
else if( IsOutline() )
|
|
|
|
{
|
2022-11-27 15:26:41 +00:00
|
|
|
// Outline fonts have thickness built in, and *usually* stay within their ascent/descent
|
2022-01-07 14:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return boundingBox.GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-25 22:33:37 +00:00
|
|
|
VECTOR2I FONT::boundingBoxSingleLine( BOX2I* aBBox, const wxString& aText,
|
|
|
|
const VECTOR2I& aPosition, const VECTOR2I& aSize,
|
2023-08-06 19:20:53 +00:00
|
|
|
bool aItalic, const METRICS& aFontMetrics ) const
|
2021-12-31 14:07:24 +00:00
|
|
|
{
|
2022-01-04 11:52:29 +00:00
|
|
|
TEXT_STYLE_FLAGS textStyle = 0;
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
if( aItalic )
|
2021-12-31 14:07:24 +00:00
|
|
|
textStyle |= TEXT_STYLE::ITALIC;
|
|
|
|
|
2023-05-17 20:30:31 +00:00
|
|
|
VECTOR2I extents = drawMarkup( aBBox, nullptr, aText, aPosition, aSize, ANGLE_0, false,
|
2023-08-06 19:20:53 +00:00
|
|
|
VECTOR2I(), textStyle, aFontMetrics );
|
2021-12-31 14:07:24 +00:00
|
|
|
|
2022-01-07 17:42:43 +00:00
|
|
|
return extents;
|
2021-12-31 14:07:24 +00:00
|
|
|
}
|
2022-01-25 22:33:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Break marked-up text into "words". In this context, a "word" is EITHER a run of marked-up
|
|
|
|
* text (subscript, superscript or overbar), OR a run of non-marked-up text separated by spaces.
|
|
|
|
*/
|
|
|
|
void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords,
|
|
|
|
const std::unique_ptr<MARKUP::NODE>& aNode, const KIFONT::FONT* aFont,
|
|
|
|
const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle )
|
|
|
|
{
|
|
|
|
TEXT_STYLE_FLAGS textStyle = aTextStyle;
|
|
|
|
|
|
|
|
if( !aNode->is_root() )
|
|
|
|
{
|
|
|
|
wxChar escapeChar = 0;
|
|
|
|
|
|
|
|
if( aNode->isSubscript() )
|
|
|
|
{
|
|
|
|
escapeChar = '_';
|
|
|
|
textStyle = TEXT_STYLE::SUBSCRIPT;
|
|
|
|
}
|
|
|
|
else if( aNode->isSuperscript() )
|
|
|
|
{
|
|
|
|
escapeChar = '^';
|
|
|
|
textStyle = TEXT_STYLE::SUPERSCRIPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aNode->isOverbar() )
|
|
|
|
{
|
|
|
|
escapeChar = '~';
|
|
|
|
textStyle |= TEXT_STYLE::OVERBAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( escapeChar )
|
|
|
|
{
|
2022-02-26 19:02:34 +00:00
|
|
|
wxString word = wxString::Format( wxT( "%c{" ), escapeChar );
|
2022-01-25 22:33:37 +00:00
|
|
|
int width = 0;
|
|
|
|
|
|
|
|
if( aNode->has_content() )
|
|
|
|
{
|
2022-02-26 22:56:10 +00:00
|
|
|
VECTOR2I next = aFont->GetTextAsGlyphs( nullptr, nullptr, aNode->asWxString(),
|
2022-02-26 19:02:34 +00:00
|
|
|
aSize, { 0, 0 }, ANGLE_0, false, { 0, 0 },
|
|
|
|
textStyle );
|
2022-02-26 22:56:10 +00:00
|
|
|
word += aNode->asWxString();
|
2022-01-25 22:33:37 +00:00
|
|
|
width += next.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<wxString, int>> childWords;
|
|
|
|
|
|
|
|
for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
|
|
|
|
wordbreakMarkup( &childWords, child, aFont, aSize, textStyle );
|
|
|
|
|
|
|
|
for( const std::pair<wxString, int>& childWord : childWords )
|
|
|
|
{
|
|
|
|
word += childWord.first;
|
|
|
|
width += childWord.second;
|
|
|
|
}
|
|
|
|
|
2022-02-26 19:02:34 +00:00
|
|
|
word += wxT( "}" );
|
2022-01-25 22:33:37 +00:00
|
|
|
aWords->emplace_back( std::make_pair( word, width ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-05 21:31:29 +00:00
|
|
|
wxString textRun = aNode->asWxString();
|
|
|
|
wxStringTokenizer tokenizer( textRun, " ", wxTOKEN_RET_DELIMS );
|
|
|
|
std::vector<wxString> words;
|
2022-01-25 22:33:37 +00:00
|
|
|
|
2023-05-05 21:31:29 +00:00
|
|
|
while( tokenizer.HasMoreTokens() )
|
|
|
|
words.emplace_back( tokenizer.GetNextToken() );
|
2022-01-25 22:33:37 +00:00
|
|
|
|
2023-05-05 21:31:29 +00:00
|
|
|
for( const wxString& word : words )
|
2022-01-25 22:33:37 +00:00
|
|
|
{
|
2023-05-05 21:31:29 +00:00
|
|
|
wxString chars = word;
|
|
|
|
chars.Trim();
|
|
|
|
|
|
|
|
int w = aFont->GetTextAsGlyphs( nullptr, nullptr, chars, aSize, { 0, 0 },
|
2022-01-25 22:33:37 +00:00
|
|
|
ANGLE_0, false, { 0, 0 }, textStyle ).x;
|
|
|
|
|
2023-05-05 21:31:29 +00:00
|
|
|
aWords->emplace_back( std::make_pair( word, w ) );
|
2022-01-25 22:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
|
|
|
|
wordbreakMarkup( aWords, child, aFont, aSize, textStyle );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FONT::wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText,
|
|
|
|
const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const
|
|
|
|
{
|
2022-02-26 19:02:34 +00:00
|
|
|
MARKUP::MARKUP_PARSER markupParser( TO_UTF8( aText ) );
|
2022-01-25 22:33:37 +00:00
|
|
|
std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
|
|
|
|
|
|
|
|
::wordbreakMarkup( aWords, root, this, aSize, aTextStyle );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a highly simplified line-breaker. KiCad is an EDA tool, not a word processor.
|
|
|
|
*
|
|
|
|
* 1) It breaks only on spaces. If you type a word wider than the column width then you get
|
|
|
|
* overflow.
|
|
|
|
* 2) It treats runs of formatted text (superscript, subscript, overbar) as single words.
|
|
|
|
* 3) It does not perform justification.
|
|
|
|
*
|
|
|
|
* The results of the linebreaking are the addition of \n in the text. It is presumed that this
|
|
|
|
* function is called on m_shownText (or equivalent) rather than the original source text.
|
|
|
|
*/
|
|
|
|
void FONT::LinebreakText( wxString& aText, int aColumnWidth, const VECTOR2I& aSize, int aThickness,
|
|
|
|
bool aBold, bool aItalic ) const
|
|
|
|
{
|
|
|
|
TEXT_STYLE_FLAGS textStyle = 0;
|
|
|
|
|
|
|
|
if( aBold )
|
|
|
|
textStyle |= TEXT_STYLE::BOLD;
|
|
|
|
|
|
|
|
if( aItalic )
|
|
|
|
textStyle |= TEXT_STYLE::ITALIC;
|
|
|
|
|
2022-02-03 21:47:10 +00:00
|
|
|
int spaceWidth = GetTextAsGlyphs( nullptr, nullptr, wxS( " " ), aSize, VECTOR2I(), ANGLE_0,
|
|
|
|
false, VECTOR2I(), textStyle ).x;
|
2022-01-25 22:33:37 +00:00
|
|
|
|
|
|
|
wxArrayString textLines;
|
|
|
|
wxStringSplit( aText, textLines, '\n' );
|
|
|
|
|
|
|
|
aText = wxEmptyString;
|
|
|
|
|
|
|
|
for( size_t ii = 0; ii < textLines.Count(); ++ii )
|
|
|
|
{
|
2023-05-05 21:31:29 +00:00
|
|
|
std::vector<std::pair<wxString, int>> markup;
|
2022-01-25 22:33:37 +00:00
|
|
|
std::vector<std::pair<wxString, int>> words;
|
|
|
|
|
2023-05-05 21:31:29 +00:00
|
|
|
wordbreakMarkup( &markup, textLines[ii], aSize, textStyle );
|
2022-01-25 22:33:37 +00:00
|
|
|
|
2023-05-05 21:31:29 +00:00
|
|
|
for( const auto& [ run, runWidth ] : markup )
|
2022-01-25 22:33:37 +00:00
|
|
|
{
|
2023-05-05 21:31:29 +00:00
|
|
|
if( !words.empty() && !words.back().first.EndsWith( ' ' ) )
|
2022-01-25 22:33:37 +00:00
|
|
|
{
|
2023-05-05 21:31:29 +00:00
|
|
|
words.back().first += run;
|
|
|
|
words.back().second += runWidth;
|
2022-01-25 22:33:37 +00:00
|
|
|
}
|
2023-05-05 21:31:29 +00:00
|
|
|
else
|
2022-01-25 22:33:37 +00:00
|
|
|
{
|
2023-05-05 21:31:29 +00:00
|
|
|
words.emplace_back( std::make_pair( run, runWidth ) );
|
2022-01-30 14:31:11 +00:00
|
|
|
}
|
2023-05-05 21:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool buryMode = false;
|
|
|
|
int lineWidth = 0;
|
|
|
|
wxString pendingSpaces;
|
|
|
|
|
|
|
|
for( const auto& [ word, wordWidth ] : words )
|
|
|
|
{
|
|
|
|
int pendingSpaceWidth = (int) pendingSpaces.Length() * spaceWidth;
|
|
|
|
bool overflow = lineWidth + pendingSpaceWidth + wordWidth > aColumnWidth - aThickness;
|
|
|
|
|
|
|
|
if( overflow && pendingSpaces.Length() > 0 )
|
2022-01-30 14:31:11 +00:00
|
|
|
{
|
2022-03-30 13:25:34 +00:00
|
|
|
aText += '\n';
|
|
|
|
lineWidth = 0;
|
2023-05-05 21:31:29 +00:00
|
|
|
pendingSpaces = wxEmptyString;
|
|
|
|
pendingSpaceWidth = 0;
|
|
|
|
buryMode = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( word == wxS( " " ) )
|
|
|
|
{
|
|
|
|
pendingSpaces += word;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( buryMode )
|
|
|
|
{
|
|
|
|
buryMode = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aText += pendingSpaces;
|
|
|
|
lineWidth += pendingSpaceWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( word.EndsWith( ' ' ) )
|
|
|
|
{
|
|
|
|
aText += word.Left( word.Length() - 1 );
|
|
|
|
pendingSpaces = wxS( " " );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aText += word;
|
|
|
|
pendingSpaces = wxEmptyString;
|
|
|
|
}
|
|
|
|
|
|
|
|
lineWidth += wordWidth;
|
2022-03-30 13:25:34 +00:00
|
|
|
}
|
2022-01-25 22:33:37 +00:00
|
|
|
}
|
2022-01-30 14:34:57 +00:00
|
|
|
|
|
|
|
// Add the newlines back onto the string
|
|
|
|
if( ii != ( textLines.Count() - 1 ) )
|
|
|
|
aText += '\n';
|
2022-01-25 22:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|