32#include <wx/filename.h>
33#include <wx/mstream.h>
34#include <wx/zstream.h>
56 bool is_ascii7 =
true;
58 for(
size_t ii = 0; ii < aText.Len(); ii++ )
60 if( aText[ii] >= 0x7F )
71 for(
unsigned ii = 0; ii < aText.Len(); ii++ )
73 unsigned int code = aText[ii];
96 for(
size_t ii = 0; ii < aText.Len(); ii++ )
98 unsigned int code = aText[ii];
100 sprintf( buffer,
"%4.4X", code );
129 double aScale,
bool aMirror )
156 wxASSERT_MSG( aWidth > 0,
"Plotter called to set negative pen width" );
173 r = ( r * a ) + ( 1 - a );
174 g = ( g * a ) + ( 1 - a );
175 b = ( b * a ) + ( 1 - a );
178 fprintf(
m_workFile,
"%g %g %g rg %g %g %g RG\n", r, g, b, r, g, b );
205 fprintf(
m_workFile,
"[%d %d %d %d %d %d] 0 d\n",
224 fprintf(
m_workFile,
"%g %g %g %g re %c\n", p1_dev.
x, p1_dev.
y,
252 double magic = radius * 0.551784;
257 "%g %g %g %g %g %g c "
258 "%g %g %g %g %g %g c "
259 "%g %g %g %g %g %g c "
260 "%g %g %g %g %g %g c %c\n",
261 pos_dev.
x - radius, pos_dev.
y,
263 pos_dev.
x - radius, pos_dev.
y + magic,
264 pos_dev.
x - magic, pos_dev.
y + radius,
265 pos_dev.
x, pos_dev.
y + radius,
267 pos_dev.
x + magic, pos_dev.
y + radius,
268 pos_dev.
x + radius, pos_dev.
y + magic,
269 pos_dev.
x + radius, pos_dev.
y,
271 pos_dev.
x + radius, pos_dev.
y - magic,
272 pos_dev.
x + magic, pos_dev.
y - radius,
273 pos_dev.
x, pos_dev.
y - radius,
275 pos_dev.
x - magic, pos_dev.
y - radius,
276 pos_dev.
x - radius, pos_dev.
y - magic,
277 pos_dev.
x - radius, pos_dev.
y,
284 FILL_T aFill,
int aWidth,
int aMaxError )
292 EDA_ANGLE startAngle( aStart - aCenter );
301 if( startAngle > endAngle )
311 fprintf(
m_workFile,
"%g %g m ", pos_dev.
x, pos_dev.
y );
319 fprintf(
m_workFile,
"%g %g l ", pos_dev.
x, pos_dev.
y );
323 fprintf(
m_workFile,
"%g %g l ", pos_dev.
x, pos_dev.
y );
334 fprintf(
m_workFile,
"%g %g l b\n", pos_dev.
x, pos_dev.
y );
360 if( startAngle > endAngle )
362 std::swap( startAngle, endAngle );
363 std::swap( start, end );
369 start.
x = aCenter.
x +
KiROUND( aRadius * (-startAngle).Cos() );
370 start.
y = aCenter.
y +
KiROUND( aRadius * (-startAngle).Sin() );
372 fprintf(
m_workFile,
"%g %g m ", pos_dev.
x, pos_dev.
y );
376 end.
x = aCenter.
x +
KiROUND( aRadius * (-ii).Cos() );
377 end.
y = aCenter.
y +
KiROUND( aRadius * (-ii).Sin() );
379 fprintf(
m_workFile,
"%g %g l ", pos_dev.
x, pos_dev.
y );
382 end.
x = aCenter.
x +
KiROUND( aRadius * (-endAngle).Cos() );
383 end.
y = aCenter.
y +
KiROUND( aRadius * (-endAngle).Sin() );
385 fprintf(
m_workFile,
"%g %g l ", pos_dev.
x, pos_dev.
y );
396 fprintf(
m_workFile,
"%g %g l b\n", pos_dev.
x, pos_dev.
y );
406 if( aCornerList.size() <= 1 )
414 for(
unsigned ii = 1; ii < aCornerList.size(); ii++ )
423 else if( aWidth == 0 )
451 pos_dev.
x, pos_dev.
y,
452 ( plume==
'D' ) ?
'l' :
'm' );
463 VECTOR2I pix_size( aImage.GetWidth(), aImage.GetHeight() );
466 VECTOR2D drawsize( aScaleFactor * pix_size.
x, aScaleFactor * pix_size.
y );
469 VECTOR2I start( aPos.
x - drawsize.
x / 2, aPos.
y + drawsize.
y / 2 );
482 fprintf(
m_workFile,
"q %g 0 0 %g %g %g cm\n",
485 dev_start.
x, dev_start.
y );
497 "ID\n",
m_colorMode ?
"/RGB" :
"/G", pix_size.
x, pix_size.
y );
501 for(
int y = 0; y < pix_size.
y; y++ )
503 for(
int x = 0; x < pix_size.
x; x++ )
505 unsigned char r = aImage.GetRed( x, y ) & 0xFF;
506 unsigned char g = aImage.GetGreen( x, y ) & 0xFF;
507 unsigned char b = aImage.GetBlue( x, y ) & 0xFF;
510 if( aImage.HasAlpha() )
512 unsigned char alpha = aImage.GetAlpha( x, y ) & 0xFF;
516 float a = 1.0 - ( (float) alpha / 255.0 );
517 r = ( int )( r + ( a * 0xFF ) ) & 0xFF;
518 g = ( int )( g + ( a * 0xFF ) ) & 0xFF;
519 b = ( int )( b + ( a * 0xFF ) ) & 0xFF;
523 if( aImage.HasMask() )
525 if( r == aImage.GetMaskRed() && g == aImage.GetMaskGreen()
526 && b == aImage.GetMaskBlue() )
544 unsigned char grey =
KiROUND( r * 0.2126 + g * 0.7152 + b * 0.0722 );
596 "<< /Length %d 0 R >>\n"
597 "stream\n", handle + 1 );
602 "<< /Length %d 0 R /Filter /FlateDecode >>\n"
603 "stream\n", handle + 1 );
628 unsigned char *inbuf =
new unsigned char[stream_len];
630 int rc = fread( inbuf, 1, stream_len,
m_workFile );
631 wxASSERT( rc == stream_len );
643 out_count = stream_len;
649 wxMemoryOutputStream memos(
nullptr, std::max( 2000l, stream_len ) ) ;
659 wxZlibOutputStream zos( memos, wxZ_BEST_COMPRESSION, wxZLIB_ZLIB );
661 zos.Write( inbuf, stream_len );
664 wxStreamBuffer* sb = memos.GetOutputStreamBuffer();
666 out_count = sb->Tell();
667 fwrite( sb->GetBufferStart(), 1, out_count,
m_outputFile );
702 "%g 0 0 %g 0 0 cm 1 J 1 j 0 0 0 rg 0 0 0 RG %g w\n",
718 const double PTsPERMIL = 0.072;
721 auto iuToPdfUserSpace =
726 retval.
y = psPaperSize.
y - retval.
y;
731 std::vector<int> hyperlinkHandles;
737 const BOX2I& box = linkPair.first;
738 const wxString& url = linkPair.second;
745 userSpaceBox.
SetEnd( topRight );
754 const BOX2I& box = menuPair.first;
755 const std::vector<wxString>& urls = menuPair.second;
762 userSpaceBox.
SetEnd( topRight );
769 int hyperLinkArrayHandle = -1;
772 if( hyperlinkHandles.size() > 0 )
779 for(
int handle : hyperlinkHandles )
802 " /ProcSet [/PDF /Text /ImageC /ImageB]\n"
804 "/MediaBox [0 0 %g %g]\n"
805 "/Contents %d 0 R\n",
812 if( hyperlinkHandles.size() > 0 )
813 fprintf(
m_outputFile,
"/Annots %d 0 R", hyperLinkArrayHandle );
822 wxString pageOutlineName = wxEmptyString;
844 for(
const std::pair<BOX2I, wxString>& bookmarkPair : groupVector )
846 const BOX2I& box = bookmarkPair.first;
847 const wxString& ref = bookmarkPair.second;
852 actionHandle =
emitGoToAction( pageHandle, bottomLeft, topRight );
857 std::sort( groupOutlineNode->
children.begin(), groupOutlineNode->
children.end(),
860 return a->title < b->title;
873 return StartPlot( aPageNumber, wxEmptyString );
920 "<</S /GoTo /D [%d 0 R /FitR %d %d %d %d]\n"
922 aPageHandle, aBottomLeft.
x, aBottomLeft.
y, aTopRight.
x, aTopRight.
y );
936 "<</S /GoTo /D [%d 0 R /Fit]\n"
953 for( std::vector<OUTLINE_NODE*>::iterator it = node->
children.begin();
956 if( it >= node->
children.end() - 1 )
962 nextHandle = ( *( it + 1 ) )->entryHandle;
967 prevHandle = ( *it )->entryHandle;
971 if( parentHandle != -1 )
1010 const wxString& aTitle )
1031 "<< /Type /Outlines\n"
1065 {
"/Helvetica",
"/KicadFont", 0 },
1066 {
"/Helvetica-Oblique",
"/KicadFontI", 0 },
1067 {
"/Helvetica-Bold",
"/KicadFontB", 0 },
1068 {
"/Helvetica-BoldOblique",
"/KicadFontBI", 0 }
1074 for(
int i = 0; i < 4; i++ )
1080 " /Subtype /Type1\n"
1083 " /Encoding /WinAnsiEncoding\n"
1085 fontdefs[i].psname );
1093 for(
int i = 0; i < 4; i++ )
1096 fontdefs[i].rsname, fontdefs[i].font_handle );
1104 const BOX2D& box = linkPair.first;
1105 const wxString& url = linkPair.second;
1112 " /Rect [%g %g %g %g]\n"
1113 " /Border [16 16 0]\n",
1116 wxString pageNumber;
1117 bool pageFound =
false;
1126 " /Dest [%d 0 R /FitB]\n"
1139 " /A << /Type /Action /S /NOP >>\n"
1146 " /A << /Type /Action /S /URI /URI %s >>\n"
1156 const BOX2D& box = menuPair.first;
1157 const std::vector<wxString>& urls = menuPair.second;
1158 wxString js = wxT(
"var aParams = [ " );
1160 for(
const wxString& url : urls )
1162 if( url.StartsWith(
"!" ) )
1164 wxString
property = url.AfterFirst(
'!' );
1166 if( property.Find(
"http:" ) >= 0 )
1168 wxString href =
property.substr( property.Find(
"http:" ) );
1174 else if( property.Find(
"https:" ) >= 0 )
1176 wxString href =
property.substr( property.Find(
"https:" ) );
1188 else if( url.StartsWith(
"#" ) )
1190 wxString pageNumber = url.AfterFirst(
'#' );
1200 static_cast<int>( ii ) );
1205 else if( url.StartsWith(
"http:" ) || url.StartsWith(
"https:" ) )
1217 js += wxT(
"var cChoice = app.popUpMenuEx.apply\\( app, aParams \\); " );
1218 js += wxT(
"if\\( cChoice != null && cChoice.substring\\( 0, 1 \\) == '#' \\)"
1219 " this.pageNum = cChoice.slice\\( 1 \\); " );
1220 js += wxT(
"else if\\( cChoice != null && cChoice.substring\\( 0, 4 \\) == 'http' \\)"
1221 " app.launchURL\\( cChoice \\);" );
1228 " /Rect [%g %g %g %g]\n"
1229 " /Border [16 16 0]\n",
1233 " /A << /Type /Action /S /JavaScript /JS (%s) >>\n"
1235 js.ToStdString().c_str() );
1261 time_t ltime = time(
nullptr );
1262 strftime( date_buf, 250,
"D:%Y%m%d%H%M%S", localtime( <ime ) );
1273 "/Producer (KiCad PDF)\n"
1274 "/CreationDate (%s)\n"
1290 if( outlineHandle > 0 )
1297 "/PageMode /UseOutlines\n"
1298 "/Outlines %d 0 R\n"
1299 "/PageLayout /SinglePage\n"
1311 "/PageMode /UseNone\n"
1312 "/PageLayout /SinglePage\n"
1326 "0000000000 65535 f \n", (
long)
m_xrefTable.size() );
1328 for(
unsigned i = 1; i <
m_xrefTable.size(); i++ )
1336 "<< /Size %lu /Root %d 0 R /Info %d 0 R >>\n"
1340 (
unsigned long)
m_xrefTable.size(), catalogHandle, infoDictHandle, xref_start );
1351 const wxString& aText,
1359 bool aMultilineAllowed,
1364 if( aSize.
x == 0 || aSize.
y == 0 )
1372 const char *fontname = aItalic ? ( aBold ?
"/KicadFontBI" :
"/KicadFontI" )
1373 : ( aBold ?
"/KicadFontB" :
"/KicadFont" );
1376 double ctm_a, ctm_b, ctm_c, ctm_d, ctm_e, ctm_f;
1377 double wideningFactor, heightFactor;
1380 aV_justify, aWidth, aItalic, aBold, &wideningFactor, &ctm_a,
1381 &ctm_b, &ctm_c, &ctm_d, &ctm_e, &ctm_f, &heightFactor );
1386 wxStringTokenizer str_tok( aText,
" ", wxTOKEN_RET_DELIMS );
1409 while( str_tok.HasMoreTokens() )
1411 wxString word = str_tok.GetNextToken();
1415 &ctm_b, &ctm_c, &ctm_d, &ctm_e, &ctm_f, &heightFactor );
1424 if( word.Trim(
false ).Trim(
true ).empty() )
1431 fprintf(
m_workFile,
"q %f %f %f %f %g %g cm BT %s %g Tf %d Tr %g Tz ",
1432 ctm_a, ctm_b, ctm_c, ctm_d, ctm_e, ctm_f,
1433 fontname, heightFactor,
render_mode, wideningFactor * 100 );
1436 fprintf(
m_workFile,
"%s Tj ET\n", txt_pdf.c_str() );
1442 PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, aWidth, aItalic,
1443 aBold, aMultilineAllowed, aFont );
1460 const wxString &aGroupName )
1463 m_bookmarksInPage[aGroupName].push_back( std::make_pair( aLocation, aSymbolReference ) );
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
void SetOrigin(const Vec &pos)
const Vec & GetPosition() const
coord_type GetTop() const
coord_type GetRight() const
coord_type GetLeft() const
coord_type GetBottom() const
void SetEnd(coord_type x, coord_type y)
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
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)
VECTOR2I StringBoundaryLimits(const wxString &aText, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic) const
Compute the boundary limits of aText (the bounding box of all shapes).
A color representation with 4 components: red, green, blue, alpha.
int GetDefaultPenWidth() const
const VECTOR2I & GetSizeMils() const
std::vector< int > m_pageHandles
Handles to the page objects.
FILE * m_workFile
Temporary file to construct the stream before zipping.
virtual void StartPage(const wxString &aPageNumber, const wxString &aPageName=wxEmptyString)
Start a new page in the PDF document.
virtual void ClosePage()
Close the current page in the PDF document (and emit its compressed stream).
void emitOutlineNode(OUTLINE_NODE *aNode, int aParentHandle, int aNextNode, int aPrevNode)
Emits a outline item object and recurses into any children.
int emitOutline()
Starts emitting the outline object.
virtual bool EndPlot() override
virtual void PlotPoly(const std::vector< VECTOR2I > &aCornerList, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=nullptr) override
Polygon plotting for PDF.
virtual void SetCurrentLineWidth(int width, void *aData=nullptr) override
Pen width setting for PDF.
virtual void Text(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const EDA_ANGLE &aOrient, const VECTOR2I &aSize, enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, KIFONT::FONT *aFont=nullptr, void *aData=nullptr) override
Draw text with the plotter.
int m_streamLengthHandle
Handle to the deferred stream length.
void PlotImage(const wxImage &aImage, const VECTOR2I &aPos, double aScaleFactor) override
PDF images are handles as inline, not XObject streams...
void HyperlinkMenu(const BOX2I &aBox, const std::vector< wxString > &aDestURLs) override
Create a clickable hyperlink menu with a rectangular click area.
virtual bool OpenFile(const wxString &aFullFilename) override
Open or create the plot file aFullFilename.
int m_fontResDictHandle
Font resource dictionary.
virtual void emitSetRGBColor(double r, double g, double b, double a) override
PDF supports colors fully.
virtual void Arc(const VECTOR2I &aCenter, const VECTOR2I &aStart, const VECTOR2I &aEnd, FILL_T aFill, int aWidth, int aMaxError) override
The PDF engine can't directly plot arcs so we use polygonization.
int startPdfStream(int handle=-1)
Starts a PDF stream (for the page).
virtual void Rect(const VECTOR2I &p1, const VECTOR2I &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
Rectangles in PDF.
std::map< int, std::pair< BOX2D, wxString > > m_hyperlinkHandles
int m_pageTreeHandle
Handle to the root of the page tree object.
int emitGoToAction(int aPageHandle, const VECTOR2I &aBottomLeft, const VECTOR2I &aTopRight)
Emits an action object that instructs a goto coordinates on a page.
void closePdfStream()
Finish the current PDF stream (writes the deferred length, too)
void Bookmark(const BOX2I &aBox, const wxString &aName, const wxString &aGroupName=wxEmptyString) override
Create a bookmark to a symbol.
std::vector< long > m_xrefTable
The PDF xref offset table.
void HyperlinkBox(const BOX2I &aBox, const wxString &aDestinationURL) override
Create a clickable hyperlink with a rectangular click area.
virtual void Circle(const VECTOR2I &pos, int diametre, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
Circle drawing for PDF.
virtual void PenTo(const VECTOR2I &pos, char plume) override
Moveto/lineto primitive, moves the 'pen' to the specified direction.
std::map< wxString, std::vector< std::pair< BOX2I, wxString > > > m_bookmarksInPage
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...
int startPdfObject(int handle=-1)
Open a new PDF object and returns the handle if the parameter is -1.
OUTLINE_NODE * addOutlineNode(OUTLINE_NODE *aParent, int aActionHandle, const wxString &aTitle)
Adds a new outline node entry.
std::string encodeStringForPlotter(const wxString &aUnicode) override
convert a wxString unicode string to a char string compatible with the accepted string PDF format (co...
std::vector< wxString > m_pageNumbers
List of loaded hyperlinks in current page.
virtual void SetDash(int aLineWidth, PLOT_DASH_TYPE aLineStyle) override
PDF supports dashed lines.
int allocPdfObject()
Allocate a new handle in the table of the PDF object.
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...
std::map< int, std::pair< BOX2D, std::vector< wxString > > > m_hyperlinkMenuHandles
int m_pageStreamHandle
Handle of the page content object.
std::vector< std::pair< BOX2I, wxString > > m_hyperlinksInPage
std::unique_ptr< OUTLINE_NODE > m_outlineRoot
Root outline node.
std::vector< std::pair< BOX2I, std::vector< wxString > > > m_hyperlinkMenusInPage
Handles for all the hyperlink objects that will be deferred.
void closePdfObject()
Close the current PDF object.
int m_totalOutlineNodes
Total number of outline nodes.
double GetDotMarkLenIU(int aLineWidth) const
double GetDashGapLenIU(int aLineWidth) const
static const int USE_DEFAULT_LINE_WIDTH
virtual VECTOR2D userToDeviceCoordinates(const VECTOR2I &aCoordinate)
Modify coordinates according to the orientation, scale factor, and offsets trace.
virtual void Text(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const EDA_ANGLE &aOrient, const VECTOR2I &aSize, enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify, int aPenWidth, bool aItalic, bool aBold, bool aMultilineAllowed, KIFONT::FONT *aFont, void *aData=nullptr)
Draw text with the plotter.
virtual VECTOR2D userToDeviceSize(const VECTOR2I &size)
Modify size according to the plotter scale factors (VECTOR2I version, returns a VECTOR2D).
double m_plotScale
Plot scale - chosen by the user (even implicitly with 'fit in a4')
FILE * m_outputFile
Output file.
static const int DO_NOT_SET_LINE_WIDTH
RENDER_SETTINGS * m_renderSettings
double GetDashMarkLenIU(int aLineWidth) const
virtual void SetColor(const COLOR4D &color) override
The SetColor implementation is split with the subclasses: The PSLIKE computes the rgb values,...
double plotScaleAdjX
Fine user scale adjust ( = 1.0 if no correction)
void computeTextParameters(const VECTOR2I &aPos, const wxString &aText, const EDA_ANGLE &aOrient, const VECTOR2I &aSize, bool aMirror, enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify, int aWidth, bool aItalic, bool aBold, double *wideningFactor, double *ctm_a, double *ctm_b, double *ctm_c, double *ctm_d, double *ctm_e, double *ctm_f, double *heightFactor)
This is the core for postscript/PDF text alignment.
static constexpr EDA_ANGLE & ANGLE_360
static constexpr EDA_ANGLE & FULL_CIRCLE
static constexpr EDA_ANGLE & ANGLE_0
int GetArcToSegmentCount(int aRadius, int aErrorMax, const EDA_ANGLE &aArcAngle)
void ignore_unused(const T &)
This file contains miscellaneous commonly used macros and functions.
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Plotting engines similar to ps (PostScript, Gerber, svg)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
PLOT_DASH_TYPE
Dashed line types.
wxString title
Title of outline node.
std::vector< OUTLINE_NODE * > children
Ordered list of children.
int entryHandle
Allocated handle for this outline entry.
OUTLINE_NODE * AddChild(int aActionHandle, const wxString &aTitle, int aEntryHandle)
int actionHandle
Handle to action.
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
double EuclideanNorm(const VECTOR2I &vector)
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
VECTOR2< double > VECTOR2D