26#include <fmt/format.h>
33static constexpr int MAX_SIMPLE_FONT_CODES = 256;
41 bool aInvertY,
bool aBold )
46 fmt::memory_buffer buffer;
52 if( boldMul < 1.0 ) boldMul = 1.0;
59 double lw = aUnitsPerEm * factor;
60 fmt::format_to( std::back_inserter( buffer ),
"{:.3f} w 1 J 1 j ", lw );
63 for(
const std::vector<VECTOR2D>& stroke : *aGlyph )
65 bool firstPoint =
true;
67 for(
const VECTOR2D& point : stroke )
69 double x = ( point.x + cfg.m_PDFStrokeFontXOffset ) * aUnitsPerEm;
70 double y = point.y * aUnitsPerEm;
75 y += cfg.m_PDFStrokeFontYOffset * aUnitsPerEm;
80 fmt::format_to( std::back_inserter( buffer ),
"{:.3f} {:.3f} m ", x, y );
85 fmt::format_to( std::back_inserter( buffer ),
"{:.3f} {:.3f} l ", x, y );
90 fmt::format_to( std::back_inserter( buffer ),
"S " );
93 return std::string( buffer.data(), buffer.size() );
96static std::string formatUnicodeHex( uint32_t aCodepoint )
98 if( aCodepoint <= 0xFFFF )
99 return fmt::format(
"{:04X}", aCodepoint );
101 if( aCodepoint <= 0x10FFFF )
103 uint32_t value = aCodepoint - 0x10000;
104 uint16_t high = 0xD800 + ( value >> 10 );
105 uint16_t low = 0xDC00 + ( value & 0x3FF );
106 return fmt::format(
"{:04X}{:04X}", high, low );
109 return std::string(
"003F" );
115 unsigned aSubsetIndex,
bool aBold,
bool aItalic ) :
119 m_cmapName( fmt::format(
"KiCadStrokeCMap{}", aSubsetIndex ) ),
120 m_widths( MAX_SIMPLE_FONT_CODES, 0.0 ),
137 notdef.
m_name =
".notdef";
187 glyph =
m_font->GetGlyph( glyphIndex );
188 bbox =
m_font->GetGlyphBoundingBox( glyphIndex );
206 double newMinY = -data.
m_maxY;
207 double newMaxY = -data.
m_minY;
233 widthFactor *= boldMul;
236 if( widthFactor <= 0.0 )
240 data.
m_minX -= halfStroke;
241 data.
m_minY -= halfStroke;
242 data.
m_maxX += halfStroke;
243 data.
m_maxY += halfStroke;
248 if( kerningFactor <= 0.0 )
253 data.
m_stream = fmt::format(
"{:.3f} 0 {:.3f} {:.3f} {:.3f} {:.3f} d1 {}",
265 m_glyphs.push_back( std::move( data ) );
291 return static_cast<int>(
m_glyphs.size() );
312 fmt::memory_buffer buffer;
313 fmt::format_to( std::back_inserter( buffer ),
"[ {} ", first );
315 for(
int code = first; code <= last; ++code )
320 fmt::format_to( std::back_inserter( buffer ),
"/{} ", glyph->
m_name );
322 fmt::format_to( std::back_inserter( buffer ),
"/.notdef " );
325 fmt::format_to( std::back_inserter( buffer ),
"]" );
326 return std::string( buffer.data(), buffer.size() );
335 fmt::memory_buffer buffer;
336 fmt::format_to( std::back_inserter( buffer ),
"[" );
338 for(
int code = first; code <= last; ++code )
339 fmt::format_to( std::back_inserter( buffer ),
" {:g}",
m_widths[code] );
341 fmt::format_to( std::back_inserter( buffer ),
" ]" );
342 return std::string( buffer.data(), buffer.size() );
348 size_t mappingCount = 0;
352 if( glyph.m_code == 0 )
358 fmt::memory_buffer buffer;
360 fmt::format_to( std::back_inserter( buffer ),
"/CIDInit /ProcSet findresource begin\n" );
361 fmt::format_to( std::back_inserter( buffer ),
"12 dict begin\n" );
362 fmt::format_to( std::back_inserter( buffer ),
"begincmap\n" );
363 fmt::format_to( std::back_inserter( buffer ),
"/CIDSystemInfo << /Registry (KiCad) /Ordering (StrokeFont) /Supplement 0 >> def\n" );
364 fmt::format_to( std::back_inserter( buffer ),
"/CMapName /{} def\n",
m_cmapName );
365 fmt::format_to( std::back_inserter( buffer ),
"/CMapType 2 def\n" );
366 fmt::format_to( std::back_inserter( buffer ),
"1 begincodespacerange\n" );
367 fmt::format_to( std::back_inserter( buffer ),
"<00> <FF>\n" );
368 fmt::format_to( std::back_inserter( buffer ),
"endcodespacerange\n" );
370 fmt::format_to( std::back_inserter( buffer ),
"{} beginbfchar\n", mappingCount );
374 if( glyph.m_code == 0 )
377 fmt::format_to( std::back_inserter( buffer ),
"<{:02X}> <{}>\n", glyph.m_code,
378 formatUnicodeHex(
static_cast<uint32_t
>( glyph.m_unicode ) ) );
381 fmt::format_to( std::back_inserter( buffer ),
"endbfchar\n" );
382 fmt::format_to( std::back_inserter( buffer ),
"endcmap\n" );
383 fmt::format_to( std::back_inserter( buffer ),
"CMapName currentdict /CMap defineresource pop\n" );
384 fmt::format_to( std::back_inserter( buffer ),
"end\n" );
385 fmt::format_to( std::back_inserter( buffer ),
"end\n" );
387 return std::string( buffer.data(), buffer.size() );
393 int value =
static_cast<int>( aCode );
396 return static_cast<int>(
'?' ) -
' ';
398 int index = value -
' ';
399 int count =
static_cast<int>(
m_font ?
m_font->GetGlyphCount() : 0 );
402 return static_cast<int>(
'?' ) -
' ';
410 return fmt::format(
"g{:02X}", aCode );
418 if( glyph.m_code == aCode )
427 m_font(
KIFONT::STROKE_FONT::LoadFont( wxEmptyString ) ),
447 unsigned key =
styleKey( aBold, aItalic );
452 std::vector<PDF_STROKE_FONT_RUN>* aRuns,
453 bool aBold,
bool aItalic )
464 std::string currentBytes;
466 for( wxUniChar ch : aText )
478 if( subset != currentSubset )
480 if( !currentBytes.empty() && currentSubset )
481 aRuns->push_back( { currentSubset, currentBytes, aBold, aItalic } );
483 currentSubset = subset;
484 currentBytes.clear();
487 currentBytes.push_back(
static_cast<char>( code ) );
490 if( !currentBytes.empty() && currentSubset )
491 aRuns->push_back( { currentSubset, std::move( currentBytes ), aBold, aItalic } );
498 for(
const std::unique_ptr<PDF_STROKE_FONT_SUBSET>& subset :
group.subsets )
500 if( subset->Contains( aCode ) )
504 for(
const std::unique_ptr<PDF_STROKE_FONT_SUBSET>& subset :
group.subsets )
506 if( subset->IsFull() )
509 if( subset->EnsureGlyph( aCode ) >= 0 )
514 auto newSubset = std::make_unique<PDF_STROKE_FONT_SUBSET>(
m_font.get(),
m_unitsPerEm, subsetIndex, aBold, aItalic );
517 group.subsets.emplace_back( std::move( newSubset ) );
523 std::vector<PDF_STROKE_FONT_SUBSET*> out;
528 for(
const auto&
up :
group.subsets )
529 out.push_back(
up.get() );
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
constexpr const Vec & GetOrigin() const
constexpr const SizeVec & GetSize() const
Implement a stroke font drawing.
static STROKE_FONT * LoadFont(const wxString &aFontName)
Load a stroke font.
std::unique_ptr< KIFONT::STROKE_FONT > m_font
std::map< unsigned, STYLE_GROUP > m_styleGroups
unsigned m_nextSubsetIndex
STYLE_GROUP & groupFor(bool aBold, bool aItalic)
PDF_STROKE_FONT_SUBSET * ensureSubsetForGlyph(wxUniChar aCode, bool aBold, bool aItalic)
std::vector< PDF_STROKE_FONT_SUBSET * > AllSubsets() const
void EncodeString(const wxString &aText, std::vector< PDF_STROKE_FONT_RUN > *aRuns, bool aBold=false, bool aItalic=false)
PDF_STROKE_FONT_MANAGER()
static unsigned styleKey(bool aBold, bool aItalic)
std::string BuildDifferencesArray() const
std::string m_resourceName
const KIFONT::STROKE_FONT * m_font
int EnsureGlyph(wxUniChar aCode)
int CodeForGlyph(wxUniChar aCode) const
std::string BuildWidthsArray() const
std::map< wxUniChar, int > m_unicodeToCode
std::string makeGlyphName(int aCode) const
std::vector< double > m_widths
std::string BuildToUnicodeCMap() const
bool Contains(wxUniChar aCode) const
std::vector< GLYPH > m_glyphs
const GLYPH * glyphForCode(int aCode) const
PDF_STROKE_FONT_SUBSET(const KIFONT::STROKE_FONT *aFont, double aUnitsPerEm, unsigned aSubsetIndex, bool aBold, bool aItalic)
int glyphIndexForUnicode(wxUniChar aCode) const
double m_PDFStrokeFontKerningFactor
Kerning (spacing) factor applied to glyph advance (width).
double m_PDFStrokeFontXOffset
Horizontal offset factor applied to stroke font glyph coordinates (in EM units) after to compensate m...
double m_PDFStrokeFontYOffset
Vertical offset factor applied to stroke font glyph coordinates (in EM units) after Y inversion to co...
double m_PDFStrokeFontBoldMultiplier
Multiplier applied to stroke width factor when rendering bold stroke font subsets.
double m_PDFStrokeFontWidthFactor
Stroke font line width factor relative to EM size for PDF stroke fonts.
VECTOR2< double > VECTOR2D