27#include <fmt/format.h>
34static constexpr int MAX_SIMPLE_FONT_CODES = 256;
36static std::string formatUnicodeHex( uint32_t aCodepoint )
38 if( aCodepoint <= 0xFFFF )
39 return fmt::format(
"{:04X}", aCodepoint );
41 if( aCodepoint <= 0x10FFFF )
43 uint32_t value = aCodepoint - 0x10000;
44 uint16_t high = 0xD800 + ( value >> 10 );
45 uint16_t low = 0xDC00 + ( value & 0x3FF );
46 return fmt::format(
"{:04X}{:04X}", high, low );
49 return std::string(
"003F" );
55 unsigned aSubsetIndex,
bool aBold,
bool aItalic,
56 double aStrokeWidthFactor,
double aAspectRatio ) :
60 m_cmapName( fmt::format(
"KiCadStrokeCMap{}", aSubsetIndex ) ),
61 m_widths( MAX_SIMPLE_FONT_CODES, 0.0 ),
103 return std::string();
105 fmt::memory_buffer buffer;
111 fmt::format_to( std::back_inserter( buffer ),
"{:.3f} w 1 J 1 j ", lw );
114 for(
const std::vector<VECTOR2D>& stroke : *aGlyph )
116 bool firstPoint =
true;
118 for(
const VECTOR2D& point : stroke )
120 double x = ( point.x + cfg.m_PDFStrokeFontXOffset ) * xEm;
121 double y = point.y * yEm;
124 y += cfg.m_PDFStrokeFontYOffset * yEm;
128 fmt::format_to( std::back_inserter( buffer ),
"{:.3f} {:.3f} m ", x, y );
133 fmt::format_to( std::back_inserter( buffer ),
"{:.3f} {:.3f} l ", x, y );
137 if( !stroke.empty() )
138 fmt::format_to( std::back_inserter( buffer ),
"S " );
141 return std::string( buffer.data(), buffer.size() );
178 glyph =
m_font->GetGlyph( glyphIndex );
179 bbox =
m_font->GetGlyphBoundingBox( glyphIndex );
191 data.
m_maxX = ( origin.
x + size.
x ) * xEm;
192 data.
m_maxY = ( origin.
y + size.
y ) * yEm;
197 double newMinY = -data.
m_maxY;
198 double newMaxY = -data.
m_minY;
216 data.
m_minX -= halfStroke;
217 data.
m_minY -= halfStroke;
218 data.
m_maxX += halfStroke;
219 data.
m_maxY += halfStroke;
224 if( kerningFactor <= 0.0 )
228 data.
m_width = size.
x * xEm * kerningFactor;
229 data.
m_stream = fmt::format(
"{:.3f} 0 {:.3f} {:.3f} {:.3f} {:.3f} d1 {}",
241 m_glyphs.push_back( std::move( data ) );
267 return static_cast<int>(
m_glyphs.size() );
288 fmt::memory_buffer buffer;
289 fmt::format_to( std::back_inserter( buffer ),
"[ {} ", first );
291 for(
int code = first; code <= last; ++code )
296 fmt::format_to( std::back_inserter( buffer ),
"/{} ", glyph->
m_name );
298 fmt::format_to( std::back_inserter( buffer ),
"/.notdef " );
301 fmt::format_to( std::back_inserter( buffer ),
"]" );
302 return std::string( buffer.data(), buffer.size() );
311 fmt::memory_buffer buffer;
312 fmt::format_to( std::back_inserter( buffer ),
"[" );
314 for(
int code = first; code <= last; ++code )
315 fmt::format_to( std::back_inserter( buffer ),
" {:g}",
m_widths[code] );
317 fmt::format_to( std::back_inserter( buffer ),
" ]" );
318 return std::string( buffer.data(), buffer.size() );
324 size_t mappingCount = 0;
328 if( glyph.m_code == 0 )
334 fmt::memory_buffer buffer;
336 fmt::format_to( std::back_inserter( buffer ),
"/CIDInit /ProcSet findresource begin\n" );
337 fmt::format_to( std::back_inserter( buffer ),
"12 dict begin\n" );
338 fmt::format_to( std::back_inserter( buffer ),
"begincmap\n" );
339 fmt::format_to( std::back_inserter( buffer ),
"/CIDSystemInfo << /Registry (KiCad) /Ordering (StrokeFont) /Supplement 0 >> def\n" );
340 fmt::format_to( std::back_inserter( buffer ),
"/CMapName /{} def\n",
m_cmapName );
341 fmt::format_to( std::back_inserter( buffer ),
"/CMapType 2 def\n" );
342 fmt::format_to( std::back_inserter( buffer ),
"1 begincodespacerange\n" );
343 fmt::format_to( std::back_inserter( buffer ),
"<00> <FF>\n" );
344 fmt::format_to( std::back_inserter( buffer ),
"endcodespacerange\n" );
346 fmt::format_to( std::back_inserter( buffer ),
"{} beginbfchar\n", mappingCount );
350 if( glyph.m_code == 0 )
353 fmt::format_to( std::back_inserter( buffer ),
"<{:02X}> <{}>\n", glyph.m_code,
354 formatUnicodeHex(
static_cast<uint32_t
>( glyph.m_unicode ) ) );
357 fmt::format_to( std::back_inserter( buffer ),
"endbfchar\n" );
358 fmt::format_to( std::back_inserter( buffer ),
"endcmap\n" );
359 fmt::format_to( std::back_inserter( buffer ),
"CMapName currentdict /CMap defineresource pop\n" );
360 fmt::format_to( std::back_inserter( buffer ),
"end\n" );
361 fmt::format_to( std::back_inserter( buffer ),
"end\n" );
363 return std::string( buffer.data(), buffer.size() );
369 int value =
static_cast<int>( aCode );
372 return static_cast<int>(
'?' ) -
' ';
374 int index = value -
' ';
375 int count =
static_cast<int>(
m_font ?
m_font->GetGlyphCount() : 0 );
378 return static_cast<int>(
'?' ) -
' ';
386 return fmt::format(
"g{:02X}", aCode );
394 if( glyph.m_code == aCode )
403 m_font(
KIFONT::STROKE_FONT::LoadFont( wxEmptyString ) ),
422 int aStrokeWidth,
int aFontWidth,
425 int strokeWidth =
std::abs( aStrokeWidth );
426 int fontWidth =
std::abs( aFontWidth );
427 int fontHeight = strokeWidth > 0 ?
std::abs( aFontHeight ) : 0;
428 return { aBold, aItalic, strokeWidth, fontWidth, fontHeight };
433 int aStrokeWidth,
int aFontWidth,
int aFontHeight,
bool aBold,
445 std::string currentBytes;
447 for( wxUniChar ch : aText )
460 if( subset != currentSubset )
462 if( !currentBytes.empty() && currentSubset )
463 aRuns->push_back( { currentSubset, currentBytes, aBold, aItalic } );
465 currentSubset = subset;
466 currentBytes.clear();
469 currentBytes.push_back(
static_cast<char>( code ) );
472 if( !currentBytes.empty() && currentSubset )
473 aRuns->push_back( { currentSubset, std::move( currentBytes ), aBold, aItalic } );
477 int aFontWidth,
int aFontHeight,
478 bool aBold,
bool aItalic )
480 int strokeWidth =
std::abs( aStrokeWidth );
481 int fontWidth =
std::abs( aFontWidth );
482 int fontHeight =
std::abs( aFontHeight );
486 if( strokeWidth > 0 && fontHeight > 0 )
488 widthFactor =
static_cast<double>( strokeWidth ) /
static_cast<double>( fontHeight );
494 if( widthFactor <= 0.0 )
504 widthFactor *= boldMul;
508 double aspectRatio = 1.0;
510 if( fontHeight > 0 && fontWidth > 0 )
511 aspectRatio =
static_cast<double>( fontWidth ) /
static_cast<double>( fontHeight );
513 STYLE_KEY key =
styleKey( aBold, aItalic, aStrokeWidth, aFontWidth, aFontHeight );
516 for(
const std::unique_ptr<PDF_STROKE_FONT_SUBSET>& subset :
group.subsets )
518 if( subset->Contains( aCode ) )
522 for(
const std::unique_ptr<PDF_STROKE_FONT_SUBSET>& subset :
group.subsets )
524 if( subset->IsFull() )
527 if( subset->EnsureGlyph( aCode ) >= 0 )
532 auto newSubset = std::make_unique<PDF_STROKE_FONT_SUBSET>(
m_font.get(),
m_unitsPerEm, subsetIndex, aBold, aItalic,
533 widthFactor, aspectRatio );
536 group.subsets.emplace_back( std::move( newSubset ) );
542 std::vector<PDF_STROKE_FONT_SUBSET*> out;
547 for(
const auto&
up :
group.subsets )
548 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
unsigned m_nextSubsetIndex
std::map< STYLE_KEY, STYLE_GROUP > m_styleGroups
void EncodeString(const wxString &aText, std::vector< PDF_STROKE_FONT_RUN > *aRuns, int aStrokeWidth, int aFontWidth, int aFontHeight, bool aBold=false, bool aItalic=false)
std::tuple< bool, bool, int, int, int > STYLE_KEY
PDF_STROKE_FONT_SUBSET * ensureSubsetForGlyph(wxUniChar aCode, int aStrokeWidth, int aFontWidth, int aFontHeight, bool aBold, bool aItalic)
std::vector< PDF_STROKE_FONT_SUBSET * > AllSubsets() const
static STYLE_KEY styleKey(bool aBold, bool aItalic, int aStrokeWidth, int aFontWidth, int aFontHeight)
PDF_STROKE_FONT_MANAGER()
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
double m_strokeWidthFactor
std::string BuildToUnicodeCMap() const
bool Contains(wxUniChar aCode) const
PDF_STROKE_FONT_SUBSET(const KIFONT::STROKE_FONT *aFont, double aUnitsPerEm, unsigned aSubsetIndex, bool aBold, bool aItalic, double aStrokeWidthFactor, double aAspectRatio)
std::vector< GLYPH > m_glyphs
const GLYPH * glyphForCode(int aCode) const
std::string buildGlyphStream(const KIFONT::STROKE_GLYPH *aGlyph) const
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.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
VECTOR2< double > VECTOR2D