24#include <boost/test/unit_test.hpp>
26#include <wx/filename.h>
56 const std::string sampleUtf8 =
"ABCDEF Привет 日本語 漢字";
57 wxString sample = wxString::FromUTF8( sampleUtf8.c_str() );
58 wxString pdfPath =
getTempPdfPath(
"kicad_pdf_unicode_allstyles" );
68 auto emitStyle = [&](
bool bold,
bool italic,
int yoff )
75 strokeFont.get(), metrics );
78 emitStyle(
false,
false, 0 );
79 emitStyle(
true,
false, 8000 );
80 emitStyle(
false,
true, 16000 );
81 emitStyle(
true,
true, 24000 );
88 BOOST_CHECK( buffer.rfind(
"%PDF", 0 ) == 0 );
93 BOOST_CHECK_MESSAGE( cmapCount >= 4,
"Expected at least 4 CMaps (got " << cmapCount <<
")" );
95 auto requireAll = [&](
const char* codeHex,
const char* label ) {
98 <<
") expected in all 4 styles; found " << occurrences );
101 requireAll(
"041F",
"Cyrillic PE" );
102 requireAll(
"65E5",
"Kanji 日" );
103 requireAll(
"672C",
"Kanji 本" );
111 const std::string sampleUtf8 =
"ABCDEF Привет 日本語 漢字";
112 wxString sample = wxString::FromUTF8( sampleUtf8.c_str() );
140 strokeFont.get(), metrics );
147 auto contains = [&](
const char* needle ) {
return PdfContains( buffer2, needle ); };
150 BOOST_CHECK_MESSAGE( contains(
"0420" ) || contains(
"0440" ),
"Missing Cyrillic glyph mapping (0420/0440)" );
153 BOOST_CHECK_MESSAGE( contains(
"6F22" ) || contains(
"6漢" ),
"Expect Chinese Han character mapping (6F22 / 漢)" );
163 "Rasterized PDF appears blank or too sparse (" << darkPixels
164 <<
" dark pixels)" );
177 const std::string sampleUtf8 =
"ABCDEF Привет 日本語 漢字";
178 wxString sample = wxString::FromUTF8( sampleUtf8.c_str() );
193 strokeFont.get(), metrics );
196 auto contains = [&](
const char* needle ) {
return PdfContains( buffer3, needle ); };
205 const std::string sampleUtf8 =
"ABCDEF Привет 日本語 漢字";
206 wxString sample = wxString::FromUTF8( sampleUtf8.c_str() );
222 auto contains = [&](
const char* n ) {
return PdfContains( buffer4, n ); };
230 const std::string sampleUtf8 =
"ABCDEF Привет 日本語 漢字";
231 wxString sample = wxString::FromUTF8( sampleUtf8.c_str() );
232 wxString pdfPath =
getTempPdfPath(
"kicad_pdf_unicode_bolditalic" );
250 auto contains = [&](
const char* n ) {
return PdfContains( buffer5, n ); };
251 BOOST_CHECK_MESSAGE( contains(
"041F" ),
"Missing Cyrillic glyph mapping (bold-italic 041F)" );
252 BOOST_CHECK_MESSAGE( contains(
"65E5" ),
"Missing Japanese glyph mapping (bold-italic 65E5)" );
260 const std::string sampleUtf8 =
"MW";
261 wxString sample = wxString::FromUTF8( sampleUtf8.c_str() );
277 strokeFont.get(), metrics );
288 double unitsPerEm = 1000.0;
293 std::string::size_type pos = 0;
294 int checkedGlyphs = 0;
296 while( ( pos = buffer.find(
" d1 ", pos ) ) != std::string::npos )
299 std::string::size_type lineStart = buffer.rfind(
'\n', pos );
301 if( lineStart == std::string::npos )
306 std::string d1Line = buffer.substr( lineStart, pos + 3 - lineStart );
308 double width, wy, minX, minY, maxX, maxY;
310 if( sscanf( d1Line.c_str(),
"%lf %lf %lf %lf %lf %lf", &width, &wy, &minX, &minY,
311 &maxX, &maxY ) == 6 )
314 if( width == 0.0 && maxX == 0.0 )
324 "Glyph bbox minX (" << minX
325 <<
") suggests X offset or stroke padding is missing" );
329 "Glyph bbox maxX (" << maxX <<
") must exceed advance width ("
330 << width <<
") to prevent clipping" );
339 "Expected at least 2 non-notdef glyphs, found " << checkedGlyphs );
352 const std::string sampleUtf8 =
"Yg Test ñ";
353 wxString sample = wxString::FromUTF8( sampleUtf8.c_str() );
376 BOOST_CHECK( buffer6.rfind(
"%PDF", 0 ) == 0 );
381 "PDF should contain d1 operators for glyph bounding boxes" );
392 fontFile.RemoveLastDir();
393 fontFile.AppendDir( wxT(
"resources" ) );
394 fontFile.AppendDir( wxT(
"fonts" ) );
395 fontFile.SetFullName( wxT(
"NotoSans-Regular.ttf" ) );
396 wxString fontPath = fontFile.GetFullPath();
410 std::vector<wxString> embeddedFonts;
411 embeddedFonts.push_back( fontPath );
416 wxString sample = wxString::FromUTF8(
"Outline café" );
419 outlineFont, metrics );
423 wxFFile file( pdfPath,
"rb" );
425 wxFileOffset len = file.Length();
426 std::string buffer; buffer.resize( (
size_t) len ); file.Read( buffer.data(), len );
427 BOOST_CHECK( buffer.rfind(
"%PDF", 0 ) == 0 );
430 appendDecompressed();
433 "Expected CIDFontType2 descendant font" );
435 "Embedded outline font should include FontFile2 stream" );
437 "BaseFont should reference Noto Sans subset" );
439 "ToUnicode map should include Latin character with accent" );
441 "Outline font resource entry missing" );
446 wxString rasterBase = wxFileName::CreateTempFileName( wxT(
"kicad_pdf_raster") );
447 wxString cmd = wxString::Format( wxT(
"pdftoppm -r 72 -singlefile -png \"%s\" \"%s\""),
448 pdfPath, rasterBase );
450 int ret = wxExecute( cmd, wxEXEC_SYNC );
454 wxString pngPath = rasterBase + wxT(
".png");
456 if( wxFileExists( pngPath ) )
459 if( !wxImage::FindHandler( wxBITMAP_TYPE_PNG ) )
460 wxImage::AddHandler(
new wxPNGHandler );
462 wxImage img( pngPath );
463 BOOST_REQUIRE_MESSAGE( img.IsOk(),
"Failed to load rasterized PDF image" );
466 int w = img.GetWidth();
467 int h = img.GetHeight();
469 for(
int y = 0; y < h; ++y )
471 for(
int x = 0; x < w; ++x )
473 unsigned char r = img.GetRed( x, y );
474 unsigned char g = img.GetGreen( x, y );
475 unsigned char b = img.GetBlue( x, y );
478 if( r < 240 || g < 240 || b < 240 )
486 "Rasterized PDF appears blank or too sparse (" << darkPixels
487 <<
" dark pixels). Outline font may not be rendering correctly." );
490 wxRemoveFile( pngPath );
494 BOOST_TEST_MESSAGE(
"pdftoppm succeeded but PNG output missing; skipping raster validation" );
525 wxString textWithTab = wxT(
"Before\tAfter" );
526 wxString textWithoutTab = wxT(
"BeforeAfter" );
529 strokeFont.get(), metrics );
531 strokeFont.get(), metrics );
537 BOOST_CHECK( buffer.rfind(
"%PDF", 0 ) == 0 );
542 "PDF should contain 'A' from 'After'" );
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
FONT is an abstract base class for both outline and stroke fonts.
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
A color representation with 4 components: red, green, blue, alpha.
virtual bool EndPlot() override
virtual bool OpenFile(const wxString &aFullFilename) override
Open or create the plot file aFullFilename.
virtual bool StartPlot(const wxString &aPageNumber) override
The PDF engine supports multiple pages; the first one is opened 'for free' the following are to be cl...
virtual void SetViewport(const VECTOR2I &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
PDF can have multiple pages, so SetPageSettings can be called with the outputFile open (but not insid...
virtual void PlotText(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const TEXT_ATTRIBUTES &aAttributes, KIFONT::FONT *aFont, const KIFONT::METRICS &aFontMetrics, void *aData=nullptr) override
void SetRenderSettings(RENDER_SETTINGS *aSettings)
Minimal concrete render settings suitable for plotters in tests.
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_PDFStrokeFontWidthFactor
Stroke font line width factor relative to EM size for PDF stroke fonts.
std::string GetTestDataRootDir()
int CountOccurrences(const std::string &aHaystack, const std::string &aNeedle)
Count occurrences of a substring in a string (overlapping allowed).
bool PdfContains(const std::string &aBuffer, const char *aNeedle)
Convenience contains check.
bool ReadPdfWithDecompressedStreams(const wxString &aPdfPath, std::string &aOutBuffer)
Read a PDF file and append best-effort decompressed contents of any Flate streams to the returned buf...
std::unique_ptr< KIFONT::STROKE_FONT > LoadStrokeFontUnique()
Load the default stroke font and return a unique_ptr for RAII deletion.
TEXT_ATTRIBUTES BuildTextAttributes(int aSizeIu=3000, int aStrokeWidth=300, bool aBold=false, bool aItalic=false)
Build a commonly used set of text attributes for plotting text in tests.
bool RasterizePdfCountDark(const wxString &aPdfPath, int aDpi, int aNearWhiteThresh, long &aOutDarkPixels)
Rasterize a PDF page to PNG using pdftoppm if available and count non-near-white pixels.
wxString MakeTempPdfPath(const wxString &aPrefix)
Make a temporary file path with .pdf extension using a given prefix.
void MaybeRemoveFile(const wxString &aPath, const wxString &aEnvVar=wxT("KICAD_KEEP_TEST_PDF"))
Remove a file unless the given environment variable is set (defaults to KICAD_KEEP_TEST_PDF).
Plotting engines similar to ps (PostScript, Gerber, svg)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(PlotMultilingualAllStylesMappings)
static wxString getTempPdfPath(const wxString &name)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
VECTOR2< int32_t > VECTOR2I