25#include <wx/filename.h>
30#include <wx/imagpng.h>
36 wxFileName fn = wxFileName::CreateTempFileName( aPrefix );
38 return fn.GetFullPath();
75 std::string aggregate = aBuffer;
80 size_t streamPos = aBuffer.find(
"stream\n", pos );
82 if( streamPos == std::string::npos )
85 size_t endPos = aBuffer.find(
"endstream", streamPos );
87 if( endPos == std::string::npos )
90 size_t dataStart = streamPos + 7;
91 size_t dataLen = ( endPos > dataStart ) ? ( endPos - dataStart ) : 0;
98 && ( aBuffer[dataStart + dataLen - 1] ==
'\n' || aBuffer[dataStart + dataLen - 1] ==
'\r' ) )
103 const unsigned char* data =
reinterpret_cast<const unsigned char*
>( aBuffer.data() + dataStart );
105 zs.next_in =
const_cast<Bytef*
>( data );
106 zs.avail_in =
static_cast<uInt
>( dataLen );
108 if( inflateInit( &zs ) == Z_OK )
112 out.resize( std::max( dataLen * 8 + 256,
size_t( 4096 ) ) );
117 zs.next_out =
reinterpret_cast<Bytef*
>( out.data() ) + zs.total_out;
118 zs.avail_out =
static_cast<uInt
>( out.size() - zs.total_out );
119 ret = inflate( &zs, Z_FINISH );
121 if( ret == Z_STREAM_END || ret != Z_BUF_ERROR )
124 out.resize( out.size() * 2 );
127 if( ret == Z_STREAM_END )
129 out.resize( zs.total_out );
140 aBuffer.swap( aggregate );
145 wxFFile file( aPdfPath,
"rb" );
147 if( !file.IsOpened() )
150 wxFileOffset len = file.Length();
154 aOutBuffer.resize(
static_cast<size_t>( len ) );
155 file.Read( aOutBuffer.data(), len );
163 if( aNeedle.empty() )
171 size_t p = aHaystack.find( aNeedle, pos );
172 if( p == std::string::npos )
182 long& aOutDarkPixels )
186 wxString rasterBase = wxFileName::CreateTempFileName( wxT(
"kicad_pdf_raster" ) );
187 wxString cmd = wxString::Format( wxT(
"pdftoppm -r %d -singlefile -png \"%s\" \"%s\"" ),
188 aDpi, aPdfPath, rasterBase );
189 int ret = wxExecute( cmd, wxEXEC_SYNC );
194 wxString pngPath = rasterBase + wxT(
".png" );
196 if( !wxFileExists( pngPath ) )
199 if( !wxImage::FindHandler( wxBITMAP_TYPE_PNG ) )
200 wxImage::AddHandler(
new wxPNGHandler );
202 wxImage img( pngPath );
206 int w = img.GetWidth();
207 int h = img.GetHeight();
210 for(
int y = 0; y < h; ++y )
212 for(
int x = 0; x < w; ++x )
214 unsigned char r = img.GetRed( x, y );
215 unsigned char g = img.GetGreen( x, y );
216 unsigned char b = img.GetBlue( x, y );
218 if( r < aNearWhiteThresh || g < aNearWhiteThresh || b < aNearWhiteThresh )
223 aOutDarkPixels = dark;
226 wxRemoveFile( pngPath );
233 if( !wxGetEnv( aEnvVar, &keepEnv ) || keepEnv.IsEmpty() )
234 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