KiCad PCB EDA Suite
PDF_plotter.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 1992-2012 Lorenzo Marcantonio, l.marcantonio@logossrl.com
10  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, you may find one here:
24  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25  * or you may search the http://www.gnu.org website for the version 2 license,
26  * or you may write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 
30 #include <fctsys.h>
31 #include <trigo.h>
32 #include <eda_base_frame.h>
33 #include <base_struct.h>
34 #include <common.h>
35 #include <plotter.h>
36 #include <macros.h>
37 #include <wx/zstream.h>
38 #include <wx/mstream.h>
39 #include <math/util.h> // for KiROUND
40 
41 #include <algorithm>
42 
43 
44 /*
45  * Open or create the plot file aFullFilename
46  * return true if success, false if the file cannot be created/opened
47  *
48  * Opens the PDF file in binary mode
49  */
50 bool PDF_PLOTTER::OpenFile( const wxString& aFullFilename )
51 {
52  filename = aFullFilename;
53 
54  wxASSERT( !outputFile );
55 
56  // Open the PDF file in binary mode
57  outputFile = wxFopen( filename, wxT( "wb" ) );
58 
59  if( outputFile == NULL )
60  return false ;
61 
62  return true;
63 }
64 
65 
66 void PDF_PLOTTER::SetViewport( const wxPoint& aOffset, double aIusPerDecimil,
67  double aScale, bool aMirror )
68 {
69  m_plotMirror = aMirror;
70  plotOffset = aOffset;
71  plotScale = aScale;
72  m_IUsPerDecimil = aIusPerDecimil;
73 
74  // The CTM is set to 1 user unit per decimil
75  iuPerDeviceUnit = 1.0 / aIusPerDecimil;
76 
77  /* The paper size in this engined is handled page by page
78  Look in the StartPage function */
79 }
80 
81 
90 void PDF_PLOTTER::SetCurrentLineWidth( int aWidth, void* aData )
91 {
92  wxASSERT( workFile );
93 
94  if( aWidth == DO_NOT_SET_LINE_WIDTH )
95  return;
96  else if( aWidth == USE_DEFAULT_LINE_WIDTH )
98 
99  if( aWidth == 0 )
100  aWidth = 1;
101 
102  wxASSERT_MSG( aWidth > 0, "Plotter called to set negative pen width" );
103 
104  if( aWidth != currentPenWidth )
105  fprintf( workFile, "%g w\n", userToDeviceSize( aWidth ) );
106 
107  currentPenWidth = aWidth;
108 }
109 
110 
120 void PDF_PLOTTER::emitSetRGBColor( double r, double g, double b )
121 {
122  wxASSERT( workFile );
123  fprintf( workFile, "%g %g %g rg %g %g %g RG\n",
124  r, g, b, r, g, b );
125 }
126 
131 {
132  wxASSERT( workFile );
133  switch( dashed )
134  {
136  fprintf( workFile, "[%d %d] 0 d\n",
137  (int) GetDashMarkLenIU(), (int) GetDashGapLenIU() );
138  break;
139  case PLOT_DASH_TYPE::DOT:
140  fprintf( workFile, "[%d %d] 0 d\n",
141  (int) GetDotMarkLenIU(), (int) GetDashGapLenIU() );
142  break;
144  fprintf( workFile, "[%d %d %d %d] 0 d\n",
145  (int) GetDashMarkLenIU(), (int) GetDashGapLenIU(),
146  (int) GetDotMarkLenIU(), (int) GetDashGapLenIU() );
147  break;
148  default:
149  fputs( "[] 0 d\n", workFile );
150  }
151 }
152 
153 
157 void PDF_PLOTTER::Rect( const wxPoint& p1, const wxPoint& p2, FILL_T fill, int width )
158 {
159  wxASSERT( workFile );
160  DPOINT p1_dev = userToDeviceCoordinates( p1 );
161  DPOINT p2_dev = userToDeviceCoordinates( p2 );
162 
163  SetCurrentLineWidth( width );
164  fprintf( workFile, "%g %g %g %g re %c\n", p1_dev.x, p1_dev.y,
165  p2_dev.x - p1_dev.x, p2_dev.y - p1_dev.y,
166  fill == NO_FILL ? 'S' : 'B' );
167 }
168 
169 
173 void PDF_PLOTTER::Circle( const wxPoint& pos, int diametre, FILL_T aFill, int width )
174 {
175  wxASSERT( workFile );
176  DPOINT pos_dev = userToDeviceCoordinates( pos );
177  double radius = userToDeviceSize( diametre / 2.0 );
178 
179  /* OK. Here's a trick. PDF doesn't support circles or circular angles, that's
180  a fact. You'll have to do with cubic beziers. These *can't* represent
181  circular arcs (NURBS can, beziers don't). But there is a widely known
182  approximation which is really good
183  */
184 
185  SetCurrentLineWidth( width );
186 
187  // If diameter is less than width, switch to filled mode
188  if( aFill == NO_FILL && diametre < width )
189  {
190  aFill = FILLED_SHAPE;
191  SetCurrentLineWidth( 0 );
192 
193  radius = userToDeviceSize( ( diametre / 2.0 ) + ( width / 2.0 ) );
194  }
195 
196  double magic = radius * 0.551784; // You don't want to know where this come from
197 
198  // This is the convex hull for the bezier approximated circle
199  fprintf( workFile, "%g %g m "
200  "%g %g %g %g %g %g c "
201  "%g %g %g %g %g %g c "
202  "%g %g %g %g %g %g c "
203  "%g %g %g %g %g %g c %c\n",
204  pos_dev.x - radius, pos_dev.y,
205 
206  pos_dev.x - radius, pos_dev.y + magic,
207  pos_dev.x - magic, pos_dev.y + radius,
208  pos_dev.x, pos_dev.y + radius,
209 
210  pos_dev.x + magic, pos_dev.y + radius,
211  pos_dev.x + radius, pos_dev.y + magic,
212  pos_dev.x + radius, pos_dev.y,
213 
214  pos_dev.x + radius, pos_dev.y - magic,
215  pos_dev.x + magic, pos_dev.y - radius,
216  pos_dev.x, pos_dev.y - radius,
217 
218  pos_dev.x - magic, pos_dev.y - radius,
219  pos_dev.x - radius, pos_dev.y - magic,
220  pos_dev.x - radius, pos_dev.y,
221 
222  aFill == NO_FILL ? 's' : 'b' );
223 }
224 
225 
230 void PDF_PLOTTER::Arc( const wxPoint& centre, double StAngle, double EndAngle, int radius,
231  FILL_T fill, int width )
232 {
233  wxASSERT( workFile );
234  if( radius <= 0 )
235  {
236  Circle( centre, width, FILLED_SHAPE, 0 );
237  return;
238  }
239 
240  /* Arcs are not so easily approximated by beziers (in the general case),
241  so we approximate them in the old way */
242  wxPoint start, end;
243  const int delta = 50; // increment (in 0.1 degrees) to draw circles
244 
245  if( StAngle > EndAngle )
246  std::swap( StAngle, EndAngle );
247 
248  SetCurrentLineWidth( width );
249 
250  // Usual trig arc plotting routine...
251  start.x = centre.x + KiROUND( cosdecideg( radius, -StAngle ) );
252  start.y = centre.y + KiROUND( sindecideg( radius, -StAngle ) );
253  DPOINT pos_dev = userToDeviceCoordinates( start );
254  fprintf( workFile, "%g %g m ", pos_dev.x, pos_dev.y );
255  for( int ii = StAngle + delta; ii < EndAngle; ii += delta )
256  {
257  end.x = centre.x + KiROUND( cosdecideg( radius, -ii ) );
258  end.y = centre.y + KiROUND( sindecideg( radius, -ii ) );
259  pos_dev = userToDeviceCoordinates( end );
260  fprintf( workFile, "%g %g l ", pos_dev.x, pos_dev.y );
261  }
262 
263  end.x = centre.x + KiROUND( cosdecideg( radius, -EndAngle ) );
264  end.y = centre.y + KiROUND( sindecideg( radius, -EndAngle ) );
265  pos_dev = userToDeviceCoordinates( end );
266  fprintf( workFile, "%g %g l ", pos_dev.x, pos_dev.y );
267 
268  // The arc is drawn... if not filled we stroke it, otherwise we finish
269  // closing the pie at the center
270  if( fill == NO_FILL )
271  {
272  fputs( "S\n", workFile );
273  }
274  else
275  {
276  pos_dev = userToDeviceCoordinates( centre );
277  fprintf( workFile, "%g %g l b\n", pos_dev.x, pos_dev.y );
278  }
279 }
280 
281 
285 void PDF_PLOTTER::PlotPoly( const std::vector< wxPoint >& aCornerList,
286  FILL_T aFill, int aWidth, void * aData )
287 {
288  wxASSERT( workFile );
289  if( aCornerList.size() <= 1 )
290  return;
291 
292  SetCurrentLineWidth( aWidth );
293 
294  DPOINT pos = userToDeviceCoordinates( aCornerList[0] );
295  fprintf( workFile, "%g %g m\n", pos.x, pos.y );
296 
297  for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
298  {
299  pos = userToDeviceCoordinates( aCornerList[ii] );
300  fprintf( workFile, "%g %g l\n", pos.x, pos.y );
301  }
302 
303  // Close path and stroke(/fill)
304  fprintf( workFile, "%c\n", aFill == NO_FILL ? 'S' : 'b' );
305 }
306 
307 
308 void PDF_PLOTTER::PenTo( const wxPoint& pos, char plume )
309 {
310  wxASSERT( workFile );
311  if( plume == 'Z' )
312  {
313  if( penState != 'Z' )
314  {
315  fputs( "S\n", workFile );
316  penState = 'Z';
317  penLastpos.x = -1;
318  penLastpos.y = -1;
319  }
320  return;
321  }
322 
323  if( penState != plume || pos != penLastpos )
324  {
325  DPOINT pos_dev = userToDeviceCoordinates( pos );
326  fprintf( workFile, "%g %g %c\n",
327  pos_dev.x, pos_dev.y,
328  ( plume=='D' ) ? 'l' : 'm' );
329  }
330  penState = plume;
331  penLastpos = pos;
332 }
333 
337 void PDF_PLOTTER::PlotImage( const wxImage & aImage, const wxPoint& aPos,
338  double aScaleFactor )
339 {
340  wxASSERT( workFile );
341  wxSize pix_size( aImage.GetWidth(), aImage.GetHeight() );
342 
343  // Requested size (in IUs)
344  DPOINT drawsize( aScaleFactor * pix_size.x,
345  aScaleFactor * pix_size.y );
346 
347  // calculate the bitmap start position
348  wxPoint start( aPos.x - drawsize.x / 2,
349  aPos.y + drawsize.y / 2);
350 
351  DPOINT dev_start = userToDeviceCoordinates( start );
352 
353  /* PDF has an uhm... simplified coordinate system handling. There is
354  *one* operator to do everything (the PS concat equivalent). At least
355  they kept the matrix stack to save restore environments. Also images
356  are always emitted at the origin with a size of 1x1 user units.
357  What we need to do is:
358  1) save the CTM end establish the new one
359  2) plot the image
360  3) restore the CTM
361  4) profit
362  */
363  fprintf( workFile, "q %g 0 0 %g %g %g cm\n", // Step 1
364  userToDeviceSize( drawsize.x ),
365  userToDeviceSize( drawsize.y ),
366  dev_start.x, dev_start.y );
367 
368  /* An inline image is a cross between a dictionary and a stream.
369  A real ugly construct (compared with the elegance of the PDF
370  format). Also it accepts some 'abbreviations', which is stupid
371  since the content stream is usually compressed anyway... */
372  fprintf( workFile,
373  "BI\n"
374  " /BPC 8\n"
375  " /CS %s\n"
376  " /W %d\n"
377  " /H %d\n"
378  "ID\n", colorMode ? "/RGB" : "/G", pix_size.x, pix_size.y );
379 
380  /* Here comes the stream (in binary!). I *could* have hex or ascii84
381  encoded it, but who cares? I'll go through zlib anyway */
382  for( int y = 0; y < pix_size.y; y++ )
383  {
384  for( int x = 0; x < pix_size.x; x++ )
385  {
386  unsigned char r = aImage.GetRed( x, y ) & 0xFF;
387  unsigned char g = aImage.GetGreen( x, y ) & 0xFF;
388  unsigned char b = aImage.GetBlue( x, y ) & 0xFF;
389 
390  // PDF inline images don't support alpha, so premultiply against white background
391  if( aImage.HasAlpha() )
392  {
393  unsigned char alpha = aImage.GetAlpha( x, y ) & 0xFF;
394 
395  if( alpha < 0xFF )
396  {
397  float a = 1.0 - ( (float) alpha / 255.0 );
398  r = ( int )( r + ( a * 0xFF ) ) & 0xFF;
399  g = ( int )( g + ( a * 0xFF ) ) & 0xFF;
400  b = ( int )( b + ( a * 0xFF ) ) & 0xFF;
401  }
402  }
403 
404  if( aImage.HasMask() )
405  {
406  if( r == aImage.GetMaskRed() && g == aImage.GetMaskGreen() && b == aImage.GetMaskBlue() )
407  {
408  r = 0xFF;
409  g = 0xFF;
410  b = 0xFF;
411  }
412  }
413 
414  // As usual these days, stdio buffering has to suffeeeeerrrr
415  if( colorMode )
416  {
417  putc( r, workFile );
418  putc( g, workFile );
419  putc( b, workFile );
420  }
421  else
422  {
423  // Greyscale conversion (CIE 1931)
424  unsigned char grey = KiROUND( r * 0.2126 + g * 0.7152 + b * 0.0722 );
425  putc( grey, workFile );
426  }
427  }
428  }
429 
430  fputs( "EI Q\n", workFile ); // Finish step 2 and do step 3
431 }
432 
433 
440 {
441  xrefTable.push_back( 0 );
442  return xrefTable.size() - 1;
443 }
444 
445 
451 {
452  wxASSERT( outputFile );
453  wxASSERT( !workFile );
454 
455  if( handle < 0)
456  handle = allocPdfObject();
457 
458  xrefTable[handle] = ftell( outputFile );
459  fprintf( outputFile, "%d 0 obj\n", handle );
460  return handle;
461 }
462 
463 
468 {
469  wxASSERT( outputFile );
470  wxASSERT( !workFile );
471  fputs( "endobj\n", outputFile );
472 }
473 
474 
482 {
483  wxASSERT( outputFile );
484  wxASSERT( !workFile );
485  handle = startPdfObject( handle );
486 
487  // This is guaranteed to be handle+1 but needs to be allocated since
488  // you could allocate more object during stream preparation
490  fprintf( outputFile,
491  "<< /Length %d 0 R /Filter /FlateDecode >>\n" // Length is deferred
492  "stream\n", handle + 1 );
493 
494  // Open a temporary file to accumulate the stream
495  workFilename = filename + wxT(".tmp");
496  workFile = wxFopen( workFilename, wxT( "w+b" ));
497  wxASSERT( workFile );
498  return handle;
499 }
500 
501 
506 {
507  wxASSERT( workFile );
508 
509  long stream_len = ftell( workFile );
510 
511  if( stream_len < 0 )
512  {
513  wxASSERT( false );
514  return;
515  }
516 
517  // Rewind the file, read in the page stream and DEFLATE it
518  fseek( workFile, 0, SEEK_SET );
519  unsigned char *inbuf = new unsigned char[stream_len];
520 
521  int rc = fread( inbuf, 1, stream_len, workFile );
522  wxASSERT( rc == stream_len );
523  (void) rc;
524 
525  // We are done with the temporary file, junk it
526  fclose( workFile );
527  workFile = 0;
528  ::wxRemoveFile( workFilename );
529 
530  // NULL means memos owns the memory, but provide a hint on optimum size needed.
531  wxMemoryOutputStream memos( NULL, std::max( 2000l, stream_len ) ) ;
532 
533  {
534  /* Somewhat standard parameters to compress in DEFLATE. The PDF spec is
535  * misleading, it says it wants a DEFLATE stream but it really want a ZLIB
536  * stream! (a DEFLATE stream would be generated with -15 instead of 15)
537  * rc = deflateInit2( &zstrm, Z_BEST_COMPRESSION, Z_DEFLATED, 15,
538  * 8, Z_DEFAULT_STRATEGY );
539  */
540 
541  wxZlibOutputStream zos( memos, wxZ_BEST_COMPRESSION, wxZLIB_ZLIB );
542 
543  zos.Write( inbuf, stream_len );
544 
545  delete[] inbuf;
546 
547  } // flush the zip stream using zos destructor
548 
549  wxStreamBuffer* sb = memos.GetOutputStreamBuffer();
550 
551  unsigned out_count = sb->Tell();
552 
553  fwrite( sb->GetBufferStart(), 1, out_count, outputFile );
554 
555  fputs( "endstream\n", outputFile );
556  closePdfObject();
557 
558  // Writing the deferred length as an indirect object
560  fprintf( outputFile, "%u\n", out_count );
561  closePdfObject();
562 }
563 
568 {
569  wxASSERT( outputFile );
570  wxASSERT( !workFile );
571 
572  // Compute the paper size in IUs
574  paperSize.x *= 10.0 / iuPerDeviceUnit;
575  paperSize.y *= 10.0 / iuPerDeviceUnit;
576 
577  // Open the content stream; the page object will go later
579 
580  /* Now, until ClosePage *everything* must be wrote in workFile, to be
581  compressed later in closePdfStream */
582 
583  // Default graphic settings (coordinate system, default color and line style)
584  fprintf( workFile,
585  "%g 0 0 %g 0 0 cm 1 J 1 j 0 0 0 rg 0 0 0 RG %g w\n",
586  0.0072 * plotScaleAdjX, 0.0072 * plotScaleAdjY,
588 }
589 
594 {
595  wxASSERT( workFile );
596 
597  // Close the page stream (and compress it)
598  closePdfStream();
599 
600  // Emit the page object and put it in the page list for later
601  pageHandles.push_back( startPdfObject() );
602 
603  /* Page size is in 1/72 of inch (default user space units)
604  Works like the bbox in postscript but there is no need for
605  swapping the sizes, since PDF doesn't require a portrait page.
606  We use the MediaBox but PDF has lots of other less used boxes
607  to use */
608 
609  const double BIGPTsPERMIL = 0.072;
610  wxSize psPaperSize = pageInfo.GetSizeMils();
611 
612  fprintf( outputFile,
613  "<<\n"
614  "/Type /Page\n"
615  "/Parent %d 0 R\n"
616  "/Resources <<\n"
617  " /ProcSet [/PDF /Text /ImageC /ImageB]\n"
618  " /Font %d 0 R >>\n"
619  "/MediaBox [0 0 %d %d]\n"
620  "/Contents %d 0 R\n"
621  ">>\n",
624  int( ceil( psPaperSize.x * BIGPTsPERMIL ) ),
625  int( ceil( psPaperSize.y * BIGPTsPERMIL ) ),
627  closePdfObject();
628 
629  // Mark the page stream as idle
630  pageStreamHandle = 0;
631 }
632 
639 {
640  wxASSERT( outputFile );
641 
642  // First things first: the customary null object
643  xrefTable.clear();
644  xrefTable.push_back( 0 );
645 
646  /* The header (that's easy!). The second line is binary junk required
647  to make the file binary from the beginning (the important thing is
648  that they must have the bit 7 set) */
649  fputs( "%PDF-1.5\n%\200\201\202\203\n", outputFile );
650 
651  /* Allocate an entry for the page tree root, it will go in every page
652  parent entry */
654 
655  /* In the same way, the font resource dictionary is used by every page
656  (it *could* be inherited via the Pages tree */
658 
659  /* Now, the PDF is read from the end, (more or less)... so we start
660  with the page stream for page 1. Other more important stuff is written
661  at the end */
662  StartPage();
663  return true;
664 }
665 
666 
668 {
669  wxASSERT( outputFile );
670 
671  // Close the current page (often the only one)
672  ClosePage();
673 
674  /* We need to declare the resources we're using (fonts in particular)
675  The useful standard one is the Helvetica family. Adding external fonts
676  is *very* involved! */
677  struct {
678  const char *psname;
679  const char *rsname;
680  int font_handle;
681  } fontdefs[4] = {
682  { "/Helvetica", "/KicadFont", 0 },
683  { "/Helvetica-Oblique", "/KicadFontI", 0 },
684  { "/Helvetica-Bold", "/KicadFontB", 0 },
685  { "/Helvetica-BoldOblique", "/KicadFontBI", 0 }
686  };
687 
688  /* Declare the font resources. Since they're builtin fonts, no descriptors (yay!)
689  We'll need metrics anyway to do any alignment (these are in the shared with
690  the postscript engine) */
691  for( int i = 0; i < 4; i++ )
692  {
693  fontdefs[i].font_handle = startPdfObject();
694  fprintf( outputFile,
695  "<< /BaseFont %s\n"
696  " /Type /Font\n"
697  " /Subtype /Type1\n"
698 
699  /* Adobe is so Mac-based that the nearest thing to Latin1 is
700  the Windows ANSI encoding! */
701  " /Encoding /WinAnsiEncoding\n"
702  ">>\n",
703  fontdefs[i].psname );
704  closePdfObject();
705  }
706 
707  // Named font dictionary (was allocated, now we emit it)
709  fputs( "<<\n", outputFile );
710  for( int i = 0; i < 4; i++ )
711  {
712  fprintf( outputFile, " %s %d 0 R\n",
713  fontdefs[i].rsname, fontdefs[i].font_handle );
714  }
715  fputs( ">>\n", outputFile );
716  closePdfObject();
717 
718  /* The page tree: it's a B-tree but luckily we only have few pages!
719  So we use just an array... The handle was allocated at the beginning,
720  now we instantiate the corresponding object */
722  fputs( "<<\n"
723  "/Type /Pages\n"
724  "/Kids [\n", outputFile );
725 
726  for( unsigned i = 0; i < pageHandles.size(); i++ )
727  fprintf( outputFile, "%d 0 R\n", pageHandles[i] );
728 
729  fprintf( outputFile,
730  "]\n"
731  "/Count %ld\n"
732  ">>\n", (long) pageHandles.size() );
733  closePdfObject();
734 
735 
736  // The info dictionary
737  int infoDictHandle = startPdfObject();
738  char date_buf[250];
739  time_t ltime = time( NULL );
740  strftime( date_buf, 250, "D:%Y%m%d%H%M%S",
741  localtime( &ltime ) );
742 
743  if( title.IsEmpty() )
744  {
745  // Windows uses '\' and other platforms ue '/' as sepatator
746  title = filename.AfterLast('\\');
747  title = title.AfterLast('/');
748  }
749 
750  fprintf( outputFile,
751  "<<\n"
752  "/Producer (KiCAD PDF)\n"
753  "/CreationDate (%s)\n"
754  "/Creator (%s)\n"
755  "/Title (%s)\n"
756  "/Trapped False\n",
757  date_buf,
758  TO_UTF8( creator ),
759  TO_UTF8( title ) );
760 
761  fputs( ">>\n", outputFile );
762  closePdfObject();
763 
764  // The catalog, at last
765  int catalogHandle = startPdfObject();
766  fprintf( outputFile,
767  "<<\n"
768  "/Type /Catalog\n"
769  "/Pages %d 0 R\n"
770  "/Version /1.5\n"
771  "/PageMode /UseNone\n"
772  "/PageLayout /SinglePage\n"
773  ">>\n", pageTreeHandle );
774  closePdfObject();
775 
776  /* Emit the xref table (format is crucial to the byte, each entry must
777  be 20 bytes long, and object zero must be done in that way). Also
778  the offset must be kept along for the trailer */
779  long xref_start = ftell( outputFile );
780  fprintf( outputFile,
781  "xref\n"
782  "0 %ld\n"
783  "0000000000 65535 f \n", (long) xrefTable.size() );
784  for( unsigned i = 1; i < xrefTable.size(); i++ )
785  {
786  fprintf( outputFile, "%010ld 00000 n \n", xrefTable[i] );
787  }
788 
789  // Done the xref, go for the trailer
790  fprintf( outputFile,
791  "trailer\n"
792  "<< /Size %lu /Root %d 0 R /Info %d 0 R >>\n"
793  "startxref\n"
794  "%ld\n" // The offset we saved before
795  "%%%%EOF\n",
796  (unsigned long) xrefTable.size(), catalogHandle, infoDictHandle, xref_start );
797 
798  fclose( outputFile );
799  outputFile = NULL;
800 
801  return true;
802 }
803 
804 void PDF_PLOTTER::Text( const wxPoint& aPos,
805  const COLOR4D aColor,
806  const wxString& aText,
807  double aOrient,
808  const wxSize& aSize,
809  enum EDA_TEXT_HJUSTIFY_T aH_justify,
810  enum EDA_TEXT_VJUSTIFY_T aV_justify,
811  int aWidth,
812  bool aItalic,
813  bool aBold,
814  bool aMultilineAllowed,
815  void* aData )
816 {
817  // PDF files do not like 0 sized texts which create broken files.
818  if( aSize.x == 0 || aSize.y == 0 )
819  return;
820 
821  // Render phantom text (which will be searchable) behind the stroke font. This won't
822  // be pixel-accurate, but it doesn't matter for searching.
823  int render_mode = 3; // invisible
824 
825  const char *fontname = aItalic ? ( aBold ? "/KicadFontBI" : "/KicadFontI" )
826  : ( aBold ? "/KicadFontB" : "/KicadFont" );
827 
828  // Compute the copious transformation parameters of the Curent Transform Matrix
829  double ctm_a, ctm_b, ctm_c, ctm_d, ctm_e, ctm_f;
830  double wideningFactor, heightFactor;
831 
832  computeTextParameters( aPos, aText, aOrient, aSize, m_plotMirror, aH_justify,
833  aV_justify, aWidth, aItalic, aBold,
834  &wideningFactor, &ctm_a, &ctm_b, &ctm_c,
835  &ctm_d, &ctm_e, &ctm_f, &heightFactor );
836 
837  SetColor( aColor );
838  SetCurrentLineWidth( aWidth, aData );
839 
840  /* We use the full CTM instead of the text matrix because the same
841  coordinate system will be used for the overlining. Also the %f
842  for the trig part of the matrix to avoid %g going in exponential
843  format (which is not supported) */
844  fprintf( workFile, "q %f %f %f %f %g %g cm BT %s %g Tf %d Tr %g Tz ",
845  ctm_a, ctm_b, ctm_c, ctm_d, ctm_e, ctm_f,
846  fontname, heightFactor, render_mode, wideningFactor * 100 );
847 
848  // The text must be escaped correctly
850  fputs( " Tj ET\n", workFile );
851 
852  // Restore the CTM
853  fputs( "Q\n", workFile );
854 
855  // Plot the stroked text (if requested)
856  PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, aWidth,
857  aItalic, aBold, aMultilineAllowed );
858 }
859 
double GetDotMarkLenIU() const
Definition: plotter.cpp:140
void closePdfStream()
Finish the current PDF stream (writes the deferred length, too)
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:47
virtual bool EndPlot() override
virtual void StartPage()
Starts a new page in the PDF document.
int pageTreeHandle
Definition: plotter.h:926
virtual void PenTo(const wxPoint &pos, char plume) override
moveto/lineto primitive, moves the 'pen' to the specified direction
virtual void Text(const wxPoint &aPos, const COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, void *aData=NULL) override
Draws text with the plotter.
std::vector< int > pageHandles
Font resource dictionary.
Definition: plotter.h:928
virtual void SetDash(PLOT_DASH_TYPE dashed) override
PDF supports dashed lines.
bool colorMode
Definition: plotter.h:589
char penState
Definition: plotter.h:592
double m_IUsPerDecimil
Definition: plotter.h:571
wxPoint plotOffset
Plot offset (in IUs)
Definition: plotter.h:577
wxPoint penLastpos
Definition: plotter.h:593
PAGE_INFO pageInfo
Definition: plotter.h:598
This file contains miscellaneous commonly used macros and functions.
virtual void Text(const wxPoint &aPos, const COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, void *aData=NULL)
Draws text with the plotter.
Definition: gr_text.cpp:222
wxString workFilename
Handle to the deferred stream length.
Definition: plotter.h:931
virtual void PlotPoly(const std::vector< wxPoint > &aCornerList, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=NULL) override
Polygon plotting for PDF.
double GetDashGapLenIU() const
Definition: plotter.cpp:152
virtual void ClosePage()
Close the current page in the PDF document (and emit its compressed stream)
int streamLengthHandle
Handle of the page content object.
Definition: plotter.h:930
static const int USE_DEFAULT_LINE_WIDTH
Definition: plotter.h:119
bool m_plotMirror
X axis orientation (SVG) and plot mirrored (only for PS, PDF HPGL and SVG)
Definition: plotter.h:581
#define NULL
virtual void Arc(const wxPoint &centre, double StAngle, double EndAngle, int rayon, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
The PDF engine can't directly plot arcs, it uses the base emulation.
double plotScaleAdjY
Definition: plotter.h:781
int startPdfStream(int handle=-1)
Starts a PDF stream (for the page).
int startPdfObject(int handle=-1)
Open a new PDF object and returns the handle if the parameter is -1.
virtual void SetViewport(const wxPoint &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...
Definition: PDF_plotter.cpp:66
virtual void SetColor(COLOR4D color) override
The SetColor implementation is split with the subclasses: The PSLIKE computes the rgb values,...
Definition: PS_plotter.cpp:52
Base window classes and related definitions.
double plotScale
Plot scale - chosen by the user (even implicitly with 'fit in a4')
Definition: plotter.h:565
RENDER_SETTINGS * m_renderSettings
Definition: plotter.h:603
const wxSize & GetSizeMils() const
Definition: page_info.h:142
FILE * workFile
Definition: plotter.h:932
virtual DPOINT userToDeviceSize(const wxSize &size)
Modifies size according to the plotter scale factors (wxSize version, returns a DPOINT)
Definition: plotter.cpp:125
void closePdfObject()
Close the current PDF object.
PLOT_DASH_TYPE
Enum for choosing dashed line type.
Definition: plotter.h:87
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:54
wxString title
Definition: plotter.h:597
virtual DPOINT userToDeviceCoordinates(const wxPoint &aCoordinate)
Modifies coordinates according to the orientation, scale factor, and offsets trace.
Definition: plotter.cpp:94
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
Rectangles in PDF.
int fontResDictHandle
Handle to the root of the page tree object.
Definition: plotter.h:927
double cosdecideg(double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:408
double sindecideg(double r, double a)
Circle generation utility: computes r * sin(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:399
void fputsPostscriptString(FILE *fout, const wxString &txt)
Write on a stream a string escaped for postscript/PDF.
Definition: PS_plotter.cpp:306
virtual void emitSetRGBColor(double r, double g, double b) override
PDF supports colors fully.
std::vector< long > xrefTable
Temporary file to costruct the stream before zipping.
Definition: plotter.h:933
int currentPenWidth
Definition: plotter.h:591
double GetDashMarkLenIU() const
Definition: plotter.cpp:146
virtual void Circle(const wxPoint &pos, int diametre, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
Circle drawing for PDF.
double plotScaleAdjX
Fine user scale adjust ( = 1.0 if no correction)
Definition: plotter.h:781
FILE * outputFile
true if the Y axis is top to bottom (SVG)
Definition: plotter.h:586
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
#define TO_UTF8(wxstring)
The common library.
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:42
int GetDefaultPenWidth() const
virtual bool OpenFile(const wxString &aFullFilename) override
Open or create the plot file aFullFilename.
Definition: PDF_plotter.cpp:50
void computeTextParameters(const wxPoint &aPos, const wxString &aText, int aOrient, const wxSize &aSize, bool aMirror, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_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 It computes the transformation matrix to generate ...
Definition: PS_plotter.cpp:426
virtual bool StartPlot() override
The PDF engine supports multiple pages; the first one is opened 'for free' the following are to be cl...
double iuPerDeviceUnit
Device scale (from IUs to plotter device units - usually decimils)
Definition: plotter.h:574
static const int DO_NOT_SET_LINE_WIDTH
Definition: plotter.h:118
wxString filename
Definition: plotter.h:596
int allocPdfObject()
Allocate a new handle in the table of the PDF object.
int pageStreamHandle
Handles to the page objects.
Definition: plotter.h:929
wxString creator
Definition: plotter.h:595
wxSize paperSize
Definition: plotter.h:599
virtual void SetCurrentLineWidth(int width, void *aData=NULL) override
Pen width setting for PDF.
Definition: PDF_plotter.cpp:90
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99
virtual void PlotImage(const wxImage &aImage, const wxPoint &aPos, double aScaleFactor) override
PDF images are handles as inline, not XObject streams...