30#include <fmt/format.h>
37static constexpr int MAX_SIMPLE_FONT_CODES = 256;
45 bool aInvertY,
bool aBold )
50 fmt::memory_buffer buffer;
56 if( boldMul < 1.0 ) boldMul = 1.0;
63 double lw = aUnitsPerEm * factor;
64 fmt::format_to( std::back_inserter( buffer ),
"{:.3f} w 1 J 1 j ", lw );
67 for(
const std::vector<VECTOR2D>& stroke : *aGlyph )
69 bool firstPoint =
true;
71 for(
const VECTOR2D& point : stroke )
73 double x = ( point.x + cfg.m_PDFStrokeFontXOffset ) * aUnitsPerEm;
74 double y = point.y * aUnitsPerEm;
79 y += cfg.m_PDFStrokeFontYOffset * aUnitsPerEm;
84 fmt::format_to( std::back_inserter( buffer ),
"{:.3f} {:.3f} m ", x, y );
89 fmt::format_to( std::back_inserter( buffer ),
"{:.3f} {:.3f} l ", x, y );
94 fmt::format_to( std::back_inserter( buffer ),
"S " );
97 return std::string( buffer.data(), buffer.size() );
100static std::string formatUnicodeHex( uint32_t aCodepoint )
102 if( aCodepoint <= 0xFFFF )
103 return fmt::format(
"{:04X}", aCodepoint );
105 if( aCodepoint <= 0x10FFFF )
107 uint32_t value = aCodepoint - 0x10000;
108 uint16_t high = 0xD800 + ( value >> 10 );
109 uint16_t low = 0xDC00 + ( value & 0x3FF );
110 return fmt::format(
"{:04X}{:04X}", high, low );
113 return std::string(
"003F" );
119 unsigned aSubsetIndex,
bool aBold,
bool aItalic ) :
123 m_cmapName( fmt::format(
"KiCadStrokeCMap{}", aSubsetIndex ) ),
124 m_widths( MAX_SIMPLE_FONT_CODES, 0.0 ),
141 notdef.
m_name =
".notdef";
191 glyph =
m_font->GetGlyph( glyphIndex );
192 bbox =
m_font->GetGlyphBoundingBox( glyphIndex );
210 double newMinY = -data.
m_maxY;
211 double newMaxY = -data.
m_minY;
237 widthFactor *= boldMul;
240 if( widthFactor <= 0.0 )
244 data.
m_minX -= halfStroke;
245 data.
m_minY -= halfStroke;
246 data.
m_maxX += halfStroke;
247 data.
m_maxY += halfStroke;
252 if( kerningFactor <= 0.0 )
257 data.
m_stream = fmt::format(
"{:.3f} 0 {:.3f} {:.3f} {:.3f} {:.3f} d1 {}",
269 m_glyphs.push_back( std::move( data ) );
295 return static_cast<int>(
m_glyphs.size() );
316 fmt::memory_buffer buffer;
317 fmt::format_to( std::back_inserter( buffer ),
"[ {} ", first );
319 for(
int code = first; code <= last; ++code )
324 fmt::format_to( std::back_inserter( buffer ),
"/{} ", glyph->
m_name );
326 fmt::format_to( std::back_inserter( buffer ),
"/.notdef " );
329 fmt::format_to( std::back_inserter( buffer ),
"]" );
330 return std::string( buffer.data(), buffer.size() );
339 fmt::memory_buffer buffer;
340 fmt::format_to( std::back_inserter( buffer ),
"[" );
342 for(
int code = first; code <= last; ++code )
343 fmt::format_to( std::back_inserter( buffer ),
" {:g}",
m_widths[code] );
345 fmt::format_to( std::back_inserter( buffer ),
" ]" );
346 return std::string( buffer.data(), buffer.size() );
352 size_t mappingCount = 0;
356 if( glyph.m_code == 0 )
362 fmt::memory_buffer buffer;
364 fmt::format_to( std::back_inserter( buffer ),
"/CIDInit /ProcSet findresource begin\n" );
365 fmt::format_to( std::back_inserter( buffer ),
"12 dict begin\n" );
366 fmt::format_to( std::back_inserter( buffer ),
"begincmap\n" );
367 fmt::format_to( std::back_inserter( buffer ),
"/CIDSystemInfo << /Registry (KiCad) /Ordering (StrokeFont) /Supplement 0 >> def\n" );
368 fmt::format_to( std::back_inserter( buffer ),
"/CMapName /{} def\n",
m_cmapName );
369 fmt::format_to( std::back_inserter( buffer ),
"/CMapType 2 def\n" );
370 fmt::format_to( std::back_inserter( buffer ),
"1 begincodespacerange\n" );
371 fmt::format_to( std::back_inserter( buffer ),
"<00> <FF>\n" );
372 fmt::format_to( std::back_inserter( buffer ),
"endcodespacerange\n" );
374 fmt::format_to( std::back_inserter( buffer ),
"{} beginbfchar\n", mappingCount );
378 if( glyph.m_code == 0 )
381 fmt::format_to( std::back_inserter( buffer ),
"<{:02X}> <{}>\n", glyph.m_code,
382 formatUnicodeHex(
static_cast<uint32_t
>( glyph.m_unicode ) ) );
385 fmt::format_to( std::back_inserter( buffer ),
"endbfchar\n" );
386 fmt::format_to( std::back_inserter( buffer ),
"endcmap\n" );
387 fmt::format_to( std::back_inserter( buffer ),
"CMapName currentdict /CMap defineresource pop\n" );
388 fmt::format_to( std::back_inserter( buffer ),
"end\n" );
389 fmt::format_to( std::back_inserter( buffer ),
"end\n" );
391 return std::string( buffer.data(), buffer.size() );
397 int value =
static_cast<int>( aCode );
400 return static_cast<int>(
'?' ) -
' ';
402 int index = value -
' ';
403 int count =
static_cast<int>(
m_font ?
m_font->GetGlyphCount() : 0 );
406 return static_cast<int>(
'?' ) -
' ';
414 return fmt::format(
"g{:02X}", aCode );
422 if( glyph.m_code == aCode )
431 m_font(
KIFONT::STROKE_FONT::LoadFont( wxEmptyString ) ),
451 unsigned key =
styleKey( aBold, aItalic );
456 std::vector<PDF_STROKE_FONT_RUN>* aRuns,
457 bool aBold,
bool aItalic )
468 std::string currentBytes;
470 for( wxUniChar ch : aText )
482 if( subset != currentSubset )
484 if( !currentBytes.empty() && currentSubset )
485 aRuns->push_back( { currentSubset, currentBytes, aBold, aItalic } );
487 currentSubset = subset;
488 currentBytes.clear();
491 currentBytes.push_back(
static_cast<char>( code ) );
494 if( !currentBytes.empty() && currentSubset )
495 aRuns->push_back( { currentSubset, std::move( currentBytes ), aBold, aItalic } );
502 for(
const std::unique_ptr<PDF_STROKE_FONT_SUBSET>& subset :
group.subsets )
504 if( subset->Contains( aCode ) )
508 for(
const std::unique_ptr<PDF_STROKE_FONT_SUBSET>& subset :
group.subsets )
510 if( subset->IsFull() )
513 if( subset->EnsureGlyph( aCode ) >= 0 )
518 auto newSubset = std::make_unique<PDF_STROKE_FONT_SUBSET>(
m_font.get(),
m_unitsPerEm, subsetIndex, aBold, aItalic );
521 group.subsets.emplace_back( std::move( newSubset ) );
527 std::vector<PDF_STROKE_FONT_SUBSET*> out;
532 for(
const auto&
up :
group.subsets )
533 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