27#include <wx/filename.h>
32#include <wx/imagpng.h>
38 wxFileName fn = wxFileName::CreateTempFileName( aPrefix );
40 return fn.GetFullPath();
77 std::string aggregate = aBuffer;
82 size_t streamPos = aBuffer.find(
"stream\n", pos );
84 if( streamPos == std::string::npos )
87 size_t endPos = aBuffer.find(
"endstream", streamPos );
89 if( endPos == std::string::npos )
92 size_t dataStart = streamPos + 7;
93 size_t dataLen = ( endPos > dataStart ) ? ( endPos - dataStart ) : 0;
97 const unsigned char* data =
reinterpret_cast<const unsigned char*
>( aBuffer.data() + dataStart );
99 zs.next_in =
const_cast<Bytef*
>( data );
100 zs.avail_in =
static_cast<uInt
>( dataLen );
102 if( inflateInit( &zs ) == Z_OK )
105 out.resize( dataLen * 4 + 64 );
106 zs.next_out =
reinterpret_cast<Bytef*
>( out.data() );
107 zs.avail_out =
static_cast<uInt
>( out.size() );
108 int ret = inflate( &zs, Z_FINISH );
110 if( ret == Z_STREAM_END )
112 out.resize( zs.total_out );
123 aBuffer.swap( aggregate );
128 wxFFile file( aPdfPath,
"rb" );
130 if( !file.IsOpened() )
133 wxFileOffset len = file.Length();
137 aOutBuffer.resize(
static_cast<size_t>( len ) );
138 file.Read( aOutBuffer.data(), len );
146 if( aNeedle.empty() )
154 size_t p = aHaystack.find( aNeedle, pos );
155 if( p == std::string::npos )
165 long& aOutDarkPixels )
169 wxString rasterBase = wxFileName::CreateTempFileName( wxT(
"kicad_pdf_raster" ) );
170 wxString cmd = wxString::Format( wxT(
"pdftoppm -r %d -singlefile -png \"%s\" \"%s\"" ),
171 aDpi, aPdfPath, rasterBase );
172 int ret = wxExecute( cmd, wxEXEC_SYNC );
177 wxString pngPath = rasterBase + wxT(
".png" );
179 if( !wxFileExists( pngPath ) )
182 if( !wxImage::FindHandler( wxBITMAP_TYPE_PNG ) )
183 wxImage::AddHandler(
new wxPNGHandler );
185 wxImage img( pngPath );
189 int w = img.GetWidth();
190 int h = img.GetHeight();
193 for(
int y = 0; y < h; ++y )
195 for(
int x = 0; x < w; ++x )
197 unsigned char r = img.GetRed( x, y );
198 unsigned char g = img.GetGreen( x, y );
199 unsigned char b = img.GetBlue( x, y );
201 if( r < aNearWhiteThresh || g < aNearWhiteThresh || b < aNearWhiteThresh )
206 aOutDarkPixels = dark;
209 wxRemoveFile( pngPath );
216 if( !wxGetEnv( aEnvVar, &keepEnv ) || keepEnv.IsEmpty() )
217 wxRemoveFile( aPath );
static STROKE_FONT * LoadFont(const wxString &aFontName)
Load a stroke font.
A color representation with 4 components: red, green, blue, alpha.
An abstract base class for deriving all objects that can be added to a VIEW.
KIGFX::COLOR4D m_background
KIGFX::COLOR4D GetColor(const KIGFX::VIEW_ITEM *, int) const override
Returns the color that should be used to draw the specific VIEW_ITEM on the specific layer using curr...
GR_TEXT_H_ALIGN_T m_Halign
GR_TEXT_V_ALIGN_T m_Valign
static constexpr EDA_ANGLE ANGLE_0
int CountOccurrences(const std::string &aHaystack, const std::string &aNeedle)
Count occurrences of a substring in a string (overlapping allowed).
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.
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.
static void append_decompressed_streams(std::string &aBuffer)
wxString MakeTempPdfPath(const wxString &aPrefix)
Make a temporary file path with .pdf extension using a given prefix.
TEXT_ATTRIBUTES BuildTextAttributes(int aSizeIu, int aStrokeWidth, bool aBold, bool aItalic)
Build a commonly used set of text attributes for plotting text in tests.
void MaybeRemoveFile(const wxString &aPath, const wxString &aEnvVar)
Remove a file unless the given environment variable is set (defaults to KICAD_KEEP_TEST_PDF).
VECTOR2< int32_t > VECTOR2I