30#include <wx/imagpng.h>
68 std::string aggregate = aBuffer;
73 size_t streamPos = aBuffer.find(
"stream\n", pos );
75 if( streamPos == std::string::npos )
78 size_t endPos = aBuffer.find(
"endstream", streamPos );
80 if( endPos == std::string::npos )
83 size_t dataStart = streamPos + 7;
84 size_t dataLen = ( endPos > dataStart ) ? ( endPos - dataStart ) : 0;
91 && ( aBuffer[dataStart + dataLen - 1] ==
'\n' || aBuffer[dataStart + dataLen - 1] ==
'\r' ) )
96 const unsigned char* data =
reinterpret_cast<const unsigned char*
>( aBuffer.data() + dataStart );
98 zs.next_in =
const_cast<Bytef*
>( data );
99 zs.avail_in =
static_cast<uInt
>( dataLen );
101 if( inflateInit( &zs ) == Z_OK )
105 out.resize( std::max( dataLen * 8 + 256,
size_t( 4096 ) ) );
110 zs.next_out =
reinterpret_cast<Bytef*
>( out.data() ) + zs.total_out;
111 zs.avail_out =
static_cast<uInt
>( out.size() - zs.total_out );
112 ret = inflate( &zs, Z_FINISH );
114 if( ret != Z_BUF_ERROR )
119 if( zs.avail_out > 0 )
122 out.resize( out.size() * 2 );
125 if( ret == Z_STREAM_END )
127 out.resize( zs.total_out );
138 aBuffer.swap( aggregate );
143 wxFFile file( aPdfPath,
"rb" );
145 if( !file.IsOpened() )
148 wxFileOffset len = file.Length();
152 aOutBuffer.resize(
static_cast<size_t>( len ) );
153 file.Read( aOutBuffer.data(), len );
161 if( aNeedle.empty() )
169 size_t p = aHaystack.find( aNeedle, pos );
170 if( p == std::string::npos )
180 long& aOutDarkPixels )
185 wxString rasterBase = rasterDir.
ChildPathStr(
"raster" );
186 wxString cmd = wxString::Format( wxT(
"pdftoppm -r %d -singlefile -png \"%s\" \"%s\"" ),
187 aDpi, aPdfPath, rasterBase );
188 int ret = wxExecute( cmd, wxEXEC_SYNC );
193 wxString pngPath = rasterBase + wxT(
".png" );
195 if( !wxFileExists( pngPath ) )
198 if( !wxImage::FindHandler( wxBITMAP_TYPE_PNG ) )
199 wxImage::AddHandler(
new wxPNGHandler );
201 wxImage img( pngPath );
205 int w = img.GetWidth();
206 int h = img.GetHeight();
209 for(
int y = 0; y < h; ++y )
211 for(
int x = 0; x < w; ++x )
213 unsigned char r = img.GetRed( x, y );
214 unsigned char g = img.GetGreen( x, y );
215 unsigned char b = img.GetBlue( x, y );
217 if( r < aNearWhiteThresh || g < aNearWhiteThresh || b < aNearWhiteThresh )
222 aOutDarkPixels = dark;
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.
wxString ChildPathStr(const wxString &aName) const
Get the path to a direct child of the temporary directory as a wxString, without creating the child.
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)
TEXT_ATTRIBUTES BuildTextAttributes(int aSizeIu, int aStrokeWidth, bool aBold, bool aItalic)
Build a commonly used set of text attributes for plotting text in tests.
VECTOR2< int32_t > VECTOR2I