KiCad PCB EDA Suite
common_plotPDF_functions.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 <pgm_base.h>
32 #include <trigo.h>
33 #include <wxstruct.h>
34 #include <base_struct.h>
35 #include <common.h>
36 #include <plot_common.h>
37 #include <macros.h>
38 #include <kicad_string.h>
39 #include <wx/zstream.h>
40 #include <wx/mstream.h>
41 
42 
43 /*
44  * Open or create the plot file aFullFilename
45  * return true if success, false if the file cannot be created/opened
46  *
47  * Opens the PDF file in binary mode
48  */
49 bool PDF_PLOTTER::OpenFile( const wxString& aFullFilename )
50 {
51  filename = aFullFilename;
52 
53  wxASSERT( !outputFile );
54 
55  // Open the PDF file in binary mode
56  outputFile = wxFopen( filename, wxT( "wb" ) );
57 
58  if( outputFile == NULL )
59  return false ;
60 
61  return true;
62 }
63 
64 void PDF_PLOTTER::SetPageSettings( const PAGE_INFO& aPageSettings )
65 {
66  pageInfo = aPageSettings;
67 }
68 
69 void PDF_PLOTTER::SetViewport( const wxPoint& aOffset, double aIusPerDecimil,
70  double aScale, bool aMirror )
71 {
72  m_plotMirror = aMirror;
73  plotOffset = aOffset;
74  plotScale = aScale;
75  m_IUsPerDecimil = aIusPerDecimil;
76 
77  // The CTM is set to 1 user unit per decimil
78  iuPerDeviceUnit = 1.0 / aIusPerDecimil;
79 
80  SetDefaultLineWidth( 100 / iuPerDeviceUnit ); // arbitrary default
81 
82  /* The paper size in this engined is handled page by page
83  Look in the StartPage function */
84 }
85 
86 
95 void PDF_PLOTTER::SetCurrentLineWidth( int width, void* aData )
96 {
97  wxASSERT( workFile );
98  int pen_width;
99 
100  if( width > 0 )
101  pen_width = width;
102  else if( width == 0 )
103  pen_width = 1;
104  else
105  pen_width = defaultPenWidth;
106 
107  if( pen_width != currentPenWidth )
108  fprintf( workFile, "%g w\n",
109  userToDeviceSize( pen_width ) );
110 
111  currentPenWidth = pen_width;
112 }
113 
114 
124 void PDF_PLOTTER::emitSetRGBColor( double r, double g, double b )
125 {
126  wxASSERT( workFile );
127  fprintf( workFile, "%g %g %g rg %g %g %g RG\n",
128  r, g, b, r, g, b );
129 }
130 
134 void PDF_PLOTTER::SetDash( bool dashed )
135 {
136  wxASSERT( workFile );
137  if( dashed )
138  fprintf( workFile, "[%d %d] 0 d\n",
139  (int) GetDashMarkLenIU(), (int) GetDashGapLenIU() );
140  else
141  fputs( "[] 0 d\n", workFile );
142 }
143 
144 
148 void PDF_PLOTTER::Rect( const wxPoint& p1, const wxPoint& p2, FILL_T fill, int width )
149 {
150  wxASSERT( workFile );
151  DPOINT p1_dev = userToDeviceCoordinates( p1 );
152  DPOINT p2_dev = userToDeviceCoordinates( p2 );
153 
154  SetCurrentLineWidth( width );
155  fprintf( workFile, "%g %g %g %g re %c\n", p1_dev.x, p1_dev.y,
156  p2_dev.x - p1_dev.x, p2_dev.y - p1_dev.y,
157  fill == NO_FILL ? 'S' : 'B' );
158 }
159 
160 
164 void PDF_PLOTTER::Circle( const wxPoint& pos, int diametre, FILL_T aFill, int width )
165 {
166  wxASSERT( workFile );
167  DPOINT pos_dev = userToDeviceCoordinates( pos );
168  double radius = userToDeviceSize( diametre / 2.0 );
169 
170  /* OK. Here's a trick. PDF doesn't support circles or circular angles, that's
171  a fact. You'll have to do with cubic beziers. These *can't* represent
172  circular arcs (NURBS can, beziers don't). But there is a widely known
173  approximation which is really good
174  */
175 
176  SetCurrentLineWidth( width );
177  double magic = radius * 0.551784; // You don't want to know where this come from
178 
179  // This is the convex hull for the bezier approximated circle
180  fprintf( workFile, "%g %g m "
181  "%g %g %g %g %g %g c "
182  "%g %g %g %g %g %g c "
183  "%g %g %g %g %g %g c "
184  "%g %g %g %g %g %g c %c\n",
185  pos_dev.x - radius, pos_dev.y,
186 
187  pos_dev.x - radius, pos_dev.y + magic,
188  pos_dev.x - magic, pos_dev.y + radius,
189  pos_dev.x, pos_dev.y + radius,
190 
191  pos_dev.x + magic, pos_dev.y + radius,
192  pos_dev.x + radius, pos_dev.y + magic,
193  pos_dev.x + radius, pos_dev.y,
194 
195  pos_dev.x + radius, pos_dev.y - magic,
196  pos_dev.x + magic, pos_dev.y - radius,
197  pos_dev.x, pos_dev.y - radius,
198 
199  pos_dev.x - magic, pos_dev.y - radius,
200  pos_dev.x - radius, pos_dev.y - magic,
201  pos_dev.x - radius, pos_dev.y,
202 
203  aFill == NO_FILL ? 's' : 'b' );
204 }
205 
206 
211 void PDF_PLOTTER::Arc( const wxPoint& centre, double StAngle, double EndAngle, int radius,
212  FILL_T fill, int width )
213 {
214  wxASSERT( workFile );
215  if( radius <= 0 )
216  return;
217 
218  /* Arcs are not so easily approximated by beziers (in the general case),
219  so we approximate them in the old way */
220  wxPoint start, end;
221  const int delta = 50; // increment (in 0.1 degrees) to draw circles
222 
223  if( StAngle > EndAngle )
224  std::swap( StAngle, EndAngle );
225 
226  SetCurrentLineWidth( width );
227 
228  // Usual trig arc plotting routine...
229  start.x = centre.x + KiROUND( cosdecideg( radius, -StAngle ) );
230  start.y = centre.y + KiROUND( sindecideg( radius, -StAngle ) );
231  DPOINT pos_dev = userToDeviceCoordinates( start );
232  fprintf( workFile, "%g %g m ", pos_dev.x, pos_dev.y );
233  for( int ii = StAngle + delta; ii < EndAngle; ii += delta )
234  {
235  end.x = centre.x + KiROUND( cosdecideg( radius, -ii ) );
236  end.y = centre.y + KiROUND( sindecideg( radius, -ii ) );
237  pos_dev = userToDeviceCoordinates( end );
238  fprintf( workFile, "%g %g l ", pos_dev.x, pos_dev.y );
239  }
240 
241  end.x = centre.x + KiROUND( cosdecideg( radius, -EndAngle ) );
242  end.y = centre.y + KiROUND( sindecideg( radius, -EndAngle ) );
243  pos_dev = userToDeviceCoordinates( end );
244  fprintf( workFile, "%g %g l ", pos_dev.x, pos_dev.y );
245 
246  // The arc is drawn... if not filled we stroke it, otherwise we finish
247  // closing the pie at the center
248  if( fill == NO_FILL )
249  {
250  fputs( "S\n", workFile );
251  }
252  else
253  {
254  pos_dev = userToDeviceCoordinates( centre );
255  fprintf( workFile, "%g %g l b\n", pos_dev.x, pos_dev.y );
256  }
257 }
258 
259 
263 void PDF_PLOTTER::PlotPoly( const std::vector< wxPoint >& aCornerList,
264  FILL_T aFill, int aWidth, void * aData )
265 {
266  wxASSERT( workFile );
267  if( aCornerList.size() <= 1 )
268  return;
269 
270  SetCurrentLineWidth( aWidth );
271 
272  DPOINT pos = userToDeviceCoordinates( aCornerList[0] );
273  fprintf( workFile, "%g %g m\n", pos.x, pos.y );
274 
275  for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
276  {
277  pos = userToDeviceCoordinates( aCornerList[ii] );
278  fprintf( workFile, "%g %g l\n", pos.x, pos.y );
279  }
280 
281  // Close path and stroke(/fill)
282  fprintf( workFile, "%c\n", aFill == NO_FILL ? 'S' : 'b' );
283 }
284 
285 
286 void PDF_PLOTTER::PenTo( const wxPoint& pos, char plume )
287 {
288  wxASSERT( workFile );
289  if( plume == 'Z' )
290  {
291  if( penState != 'Z' )
292  {
293  fputs( "S\n", workFile );
294  penState = 'Z';
295  penLastpos.x = -1;
296  penLastpos.y = -1;
297  }
298  return;
299  }
300 
301  if( penState != plume || pos != penLastpos )
302  {
303  DPOINT pos_dev = userToDeviceCoordinates( pos );
304  fprintf( workFile, "%g %g %c\n",
305  pos_dev.x, pos_dev.y,
306  ( plume=='D' ) ? 'l' : 'm' );
307  }
308  penState = plume;
309  penLastpos = pos;
310 }
311 
315 void PDF_PLOTTER::PlotImage( const wxImage & aImage, const wxPoint& aPos,
316  double aScaleFactor )
317 {
318  wxASSERT( workFile );
319  wxSize pix_size( aImage.GetWidth(), aImage.GetHeight() );
320 
321  // Requested size (in IUs)
322  DPOINT drawsize( aScaleFactor * pix_size.x,
323  aScaleFactor * pix_size.y );
324 
325  // calculate the bitmap start position
326  wxPoint start( aPos.x - drawsize.x / 2,
327  aPos.y + drawsize.y / 2);
328 
329  DPOINT dev_start = userToDeviceCoordinates( start );
330 
331  /* PDF has an uhm... simplified coordinate system handling. There is
332  *one* operator to do everything (the PS concat equivalent). At least
333  they kept the matrix stack to save restore environments. Also images
334  are always emitted at the origin with a size of 1x1 user units.
335  What we need to do is:
336  1) save the CTM end estabilish the new one
337  2) plot the image
338  3) restore the CTM
339  4) profit
340  */
341  fprintf( workFile, "q %g 0 0 %g %g %g cm\n", // Step 1
342  userToDeviceSize( drawsize.x ),
343  userToDeviceSize( drawsize.y ),
344  dev_start.x, dev_start.y );
345 
346  /* An inline image is a cross between a dictionary and a stream.
347  A real ugly construct (compared with the elegance of the PDF
348  format). Also it accepts some 'abbreviations', which is stupid
349  since the content stream is usually compressed anyway... */
350  fprintf( workFile,
351  "BI\n"
352  " /BPC 8\n"
353  " /CS %s\n"
354  " /W %d\n"
355  " /H %d\n"
356  "ID\n", colorMode ? "/RGB" : "/G", pix_size.x, pix_size.y );
357 
358  /* Here comes the stream (in binary!). I *could* have hex or ascii84
359  encoded it, but who cares? I'll go through zlib anyway */
360  for( int y = 0; y < pix_size.y; y++ )
361  {
362  for( int x = 0; x < pix_size.x; x++ )
363  {
364  unsigned char r = aImage.GetRed( x, y ) & 0xFF;
365  unsigned char g = aImage.GetGreen( x, y ) & 0xFF;
366  unsigned char b = aImage.GetBlue( x, y ) & 0xFF;
367  // As usual these days, stdio buffering has to suffeeeeerrrr
368  if( colorMode )
369  {
370  putc( r, workFile );
371  putc( g, workFile );
372  putc( b, workFile );
373  }
374  else
375  {
376  // Grayscale conversion
377  putc( (r + g + b) / 3, workFile );
378  }
379  }
380  }
381 
382  fputs( "EI Q\n", workFile ); // Finish step 2 and do step 3
383 }
384 
385 
392 {
393  xrefTable.push_back( 0 );
394  return xrefTable.size() - 1;
395 }
396 
397 
403 {
404  wxASSERT( outputFile );
405  wxASSERT( !workFile );
406 
407  if( handle < 0)
408  handle = allocPdfObject();
409 
410  xrefTable[handle] = ftell( outputFile );
411  fprintf( outputFile, "%d 0 obj\n", handle );
412  return handle;
413 }
414 
415 
420 {
421  wxASSERT( outputFile );
422  wxASSERT( !workFile );
423  fputs( "endobj\n", outputFile );
424 }
425 
426 
434 {
435  wxASSERT( outputFile );
436  wxASSERT( !workFile );
437  handle = startPdfObject( handle );
438 
439  // This is guaranteed to be handle+1 but needs to be allocated since
440  // you could allocate more object during stream preparation
442  fprintf( outputFile,
443  "<< /Length %d 0 R /Filter /FlateDecode >>\n" // Length is deferred
444  "stream\n", handle + 1 );
445 
446  // Open a temporary file to accumulate the stream
447  workFilename = filename + wxT(".tmp");
448  workFile = wxFopen( workFilename, wxT( "w+b" ));
449  wxASSERT( workFile );
450  return handle;
451 }
452 
453 
458 {
459  wxASSERT( workFile );
460 
461  long stream_len = ftell( workFile );
462 
463  if( stream_len < 0 )
464  {
465  wxASSERT( false );
466  return;
467  }
468 
469  // Rewind the file, read in the page stream and DEFLATE it
470  fseek( workFile, 0, SEEK_SET );
471  unsigned char *inbuf = new unsigned char[stream_len];
472 
473  int rc = fread( inbuf, 1, stream_len, workFile );
474  wxASSERT( rc == stream_len );
475  (void) rc;
476 
477  // We are done with the temporary file, junk it
478  fclose( workFile );
479  workFile = 0;
480  ::wxRemoveFile( workFilename );
481 
482  // NULL means memos owns the memory, but provide a hint on optimum size needed.
483  wxMemoryOutputStream memos( NULL, std::max( 2000l, stream_len ) ) ;
484 
485  {
486  /* Somewhat standard parameters to compress in DEFLATE. The PDF spec is
487  * misleading, it says it wants a DEFLATE stream but it really want a ZLIB
488  * stream! (a DEFLATE stream would be generated with -15 instead of 15)
489  * rc = deflateInit2( &zstrm, Z_BEST_COMPRESSION, Z_DEFLATED, 15,
490  * 8, Z_DEFAULT_STRATEGY );
491  */
492 
493  wxZlibOutputStream zos( memos, wxZ_BEST_COMPRESSION, wxZLIB_ZLIB );
494 
495  zos.Write( inbuf, stream_len );
496 
497  delete[] inbuf;
498 
499  } // flush the zip stream using zos destructor
500 
501  wxStreamBuffer* sb = memos.GetOutputStreamBuffer();
502 
503  unsigned out_count = sb->Tell();
504 
505  fwrite( sb->GetBufferStart(), 1, out_count, outputFile );
506 
507  fputs( "endstream\n", outputFile );
508  closePdfObject();
509 
510  // Writing the deferred length as an indirect object
512  fprintf( outputFile, "%u\n", out_count );
513  closePdfObject();
514 }
515 
520 {
521  wxASSERT( outputFile );
522  wxASSERT( !workFile );
523 
524  // Compute the paper size in IUs
526  paperSize.x *= 10.0 / iuPerDeviceUnit;
527  paperSize.y *= 10.0 / iuPerDeviceUnit;
528 
529  // Open the content stream; the page object will go later
531 
532  /* Now, until ClosePage *everything* must be wrote in workFile, to be
533  compressed later in closePdfStream */
534 
535  // Default graphic settings (coordinate system, default color and line style)
536  fprintf( workFile,
537  "%g 0 0 %g 0 0 cm 1 J 1 j 0 0 0 rg 0 0 0 RG %g w\n",
538  0.0072 * plotScaleAdjX, 0.0072 * plotScaleAdjY,
540 }
541 
546 {
547  wxASSERT( workFile );
548 
549  // Close the page stream (and compress it)
550  closePdfStream();
551 
552  // Emit the page object and put it in the page list for later
553  pageHandles.push_back( startPdfObject() );
554 
555  /* Page size is in 1/72 of inch (default user space units)
556  Works like the bbox in postscript but there is no need for
557  swapping the sizes, since PDF doesn't require a portrait page.
558  We use the MediaBox but PDF has lots of other less used boxes
559  to use */
560 
561  const double BIGPTsPERMIL = 0.072;
562  wxSize psPaperSize = pageInfo.GetSizeMils();
563 
564  fprintf( outputFile,
565  "<<\n"
566  "/Type /Page\n"
567  "/Parent %d 0 R\n"
568  "/Resources <<\n"
569  " /ProcSet [/PDF /Text /ImageC /ImageB]\n"
570  " /Font %d 0 R >>\n"
571  "/MediaBox [0 0 %d %d]\n"
572  "/Contents %d 0 R\n"
573  ">>\n",
576  int( ceil( psPaperSize.x * BIGPTsPERMIL ) ),
577  int( ceil( psPaperSize.y * BIGPTsPERMIL ) ),
579  closePdfObject();
580 
581  // Mark the page stream as idle
582  pageStreamHandle = 0;
583 }
584 
591 {
592  wxASSERT( outputFile );
593 
594  // First things first: the customary null object
595  xrefTable.clear();
596  xrefTable.push_back( 0 );
597 
598  /* The header (that's easy!). The second line is binary junk required
599  to make the file binary from the beginning (the important thing is
600  that they must have the bit 7 set) */
601  fputs( "%PDF-1.5\n%\200\201\202\203\n", outputFile );
602 
603  /* Allocate an entry for the page tree root, it will go in every page
604  parent entry */
606 
607  /* In the same way, the font resource dictionary is used by every page
608  (it *could* be inherited via the Pages tree */
610 
611  /* Now, the PDF is read from the end, (more or less)... so we start
612  with the page stream for page 1. Other more important stuff is written
613  at the end */
614  StartPage();
615  return true;
616 }
617 
618 
620 {
621  wxASSERT( outputFile );
622 
623  // Close the current page (often the only one)
624  ClosePage();
625 
626  /* We need to declare the resources we're using (fonts in particular)
627  The useful standard one is the Helvetica family. Adding external fonts
628  is *very* involved! */
629  struct {
630  const char *psname;
631  const char *rsname;
632  int font_handle;
633  } fontdefs[4] = {
634  { "/Helvetica", "/KicadFont", 0 },
635  { "/Helvetica-Oblique", "/KicadFontI", 0 },
636  { "/Helvetica-Bold", "/KicadFontB", 0 },
637  { "/Helvetica-BoldOblique", "/KicadFontBI", 0 }
638  };
639 
640  /* Declare the font resources. Since they're builtin fonts, no descriptors (yay!)
641  We'll need metrics anyway to do any aligment (these are in the shared with
642  the postscript engine) */
643  for( int i = 0; i < 4; i++ )
644  {
645  fontdefs[i].font_handle = startPdfObject();
646  fprintf( outputFile,
647  "<< /BaseFont %s\n"
648  " /Type /Font\n"
649  " /Subtype /Type1\n"
650 
651  /* Adobe is so Mac-based that the nearest thing to Latin1 is
652  the Windows ANSI encoding! */
653  " /Encoding /WinAnsiEncoding\n"
654  ">>\n",
655  fontdefs[i].psname );
656  closePdfObject();
657  }
658 
659  // Named font dictionary (was allocated, now we emit it)
661  fputs( "<<\n", outputFile );
662  for( int i = 0; i < 4; i++ )
663  {
664  fprintf( outputFile, " %s %d 0 R\n",
665  fontdefs[i].rsname, fontdefs[i].font_handle );
666  }
667  fputs( ">>\n", outputFile );
668  closePdfObject();
669 
670  /* The page tree: it's a B-tree but luckily we only have few pages!
671  So we use just an array... The handle was allocated at the beginning,
672  now we instantiate the corresponding object */
674  fputs( "<<\n"
675  "/Type /Pages\n"
676  "/Kids [\n", outputFile );
677 
678  for( unsigned i = 0; i < pageHandles.size(); i++ )
679  fprintf( outputFile, "%d 0 R\n", pageHandles[i] );
680 
681  fprintf( outputFile,
682  "]\n"
683  "/Count %ld\n"
684  ">>\n", (long) pageHandles.size() );
685  closePdfObject();
686 
687 
688  // The info dictionary
689  int infoDictHandle = startPdfObject();
690  char date_buf[250];
691  time_t ltime = time( NULL );
692  strftime( date_buf, 250, "D:%Y%m%d%H%M%S",
693  localtime( &ltime ) );
694 
695  if( title.IsEmpty() )
696  {
697  // Windows uses '\' and other platforms ue '/' as sepatator
698  title = filename.AfterLast('\\');
699  title = title.AfterLast('/');
700  }
701 
702  fprintf( outputFile,
703  "<<\n"
704  "/Producer (KiCAD PDF)\n"
705  "/CreationDate (%s)\n"
706  "/Creator (%s)\n"
707  "/Title (%s)\n"
708  "/Trapped false\n",
709  date_buf,
710  TO_UTF8( creator ),
711  TO_UTF8( title ) );
712 
713  fputs( ">>\n", outputFile );
714  closePdfObject();
715 
716  // The catalog, at last
717  int catalogHandle = startPdfObject();
718  fprintf( outputFile,
719  "<<\n"
720  "/Type /Catalog\n"
721  "/Pages %d 0 R\n"
722  "/Version /1.5\n"
723  "/PageMode /UseNone\n"
724  "/PageLayout /SinglePage\n"
725  ">>\n", pageTreeHandle );
726  closePdfObject();
727 
728  /* Emit the xref table (format is crucial to the byte, each entry must
729  be 20 bytes long, and object zero must be done in that way). Also
730  the offset must be kept along for the trailer */
731  long xref_start = ftell( outputFile );
732  fprintf( outputFile,
733  "xref\n"
734  "0 %ld\n"
735  "0000000000 65535 f \n", (long) xrefTable.size() );
736  for( unsigned i = 1; i < xrefTable.size(); i++ )
737  {
738  fprintf( outputFile, "%010ld 00000 n \n", xrefTable[i] );
739  }
740 
741  // Done the xref, go for the trailer
742  fprintf( outputFile,
743  "trailer\n"
744  "<< /Size %lu /Root %d 0 R /Info %d 0 R >>\n"
745  "startxref\n"
746  "%ld\n" // The offset we saved before
747  "%%%%EOF\n",
748  (unsigned long) xrefTable.size(), catalogHandle, infoDictHandle, xref_start );
749 
750  fclose( outputFile );
751  outputFile = NULL;
752 
753  return true;
754 }
755 
756 void PDF_PLOTTER::Text( const wxPoint& aPos,
757  const COLOR4D aColor,
758  const wxString& aText,
759  double aOrient,
760  const wxSize& aSize,
761  enum EDA_TEXT_HJUSTIFY_T aH_justify,
762  enum EDA_TEXT_VJUSTIFY_T aV_justify,
763  int aWidth,
764  bool aItalic,
765  bool aBold,
766  bool aMultilineAllowed,
767  void* aData )
768 {
769  // PDF files do not like 0 sized texts which create broken files.
770  if( aSize.x == 0 || aSize.y == 0 )
771  return;
772 
773  // Fix me: see how to use PDF text mode for multiline texts
774  if( aMultilineAllowed && !aText.Contains( wxT( "\n" ) ) )
775  aMultilineAllowed = false; // the text has only one line.
776 
777  // Emit native PDF text (if requested)
778  if( m_textMode != PLOTTEXTMODE_STROKE && !aMultilineAllowed )
779  {
780  const char *fontname = aItalic ? (aBold ? "/KicadFontBI" : "/KicadFontI")
781  : (aBold ? "/KicadFontB" : "/KicadFont");
782 
783  // Compute the copious tranformation parameters
784  double ctm_a, ctm_b, ctm_c, ctm_d, ctm_e, ctm_f;
785  double wideningFactor, heightFactor;
786  computeTextParameters( aPos, aText, aOrient, aSize, aH_justify,
787  aV_justify, aWidth, aItalic, aBold,
788  &wideningFactor, &ctm_a, &ctm_b, &ctm_c,
789  &ctm_d, &ctm_e, &ctm_f, &heightFactor );
790 
791  SetColor( aColor );
792  SetCurrentLineWidth( aWidth, aData );
793 
794  /* We use the full CTM instead of the text matrix because the same
795  coordinate system will be used for the overlining. Also the %f
796  for the trig part of the matrix to avoid %g going in exponential
797  format (which is not supported)
798  Rendermode 0 shows the text, rendermode 3 is invisible */
799  fprintf( workFile, "q %f %f %f %f %g %g cm BT %s %g Tf %d Tr %g Tz ",
800  ctm_a, ctm_b, ctm_c, ctm_d, ctm_e, ctm_f,
801  fontname, heightFactor,
802  (m_textMode == PLOTTEXTMODE_NATIVE) ? 0 : 3,
803  wideningFactor * 100 );
804 
805  // The text must be escaped correctly
807  fputs( " Tj ET\n", workFile );
808 
809  /* We are still in text coordinates, plot the overbars (if we're
810  * not doing phantom text) */
812  {
813  std::vector<int> pos_pairs;
814  postscriptOverlinePositions( aText, aSize.x, aItalic, aBold, &pos_pairs );
815  int overbar_y = KiROUND( aSize.y * 1.1 );
816  for( unsigned i = 0; i < pos_pairs.size(); i += 2)
817  {
818  /* This is a nontrivial situation: we are *not* in the user
819  coordinate system, so the userToDeviceCoordinates function
820  can't be used! Strange as it may seem, the userToDeviceSize
821  is the right function to use here... */
822  DPOINT dev_from = userToDeviceSize( wxSize( pos_pairs[i], overbar_y ) );
823  DPOINT dev_to = userToDeviceSize( wxSize( pos_pairs[i + 1], overbar_y ) );
824  fprintf( workFile, "%g %g m %g %g l ",
825  dev_from.x, dev_from.y, dev_to.x, dev_to.y );
826  }
827  }
828 
829  // Stroke and restore the CTM
830  fputs( "S Q\n", workFile );
831  }
832 
833  // Plot the stroked text (if requested)
834  if( m_textMode != PLOTTEXTMODE_NATIVE || aMultilineAllowed )
835  {
836  PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify,
837  aWidth, aItalic, aBold, aMultilineAllowed );
838  }
839 }
840 
void closePdfStream()
Finish the current PDF stream (writes the deferred length, too)
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:47
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
virtual bool EndPlot() override
virtual void StartPage()
Starts a new page in the PDF document.
int pageTreeHandle
Definition: plot_common.h:867
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: plot_common.h:869
bool colorMode
Definition: plot_common.h:534
char penState
Current pen state: 'U', 'D' or 'Z' (see PenTo)
Definition: plot_common.h:539
double m_IUsPerDecimil
Definition: plot_common.h:516
wxPoint plotOffset
Plot offset (in IUs)
Definition: plot_common.h:522
wxPoint penLastpos
Last pen positions; set to -1,-1 when the pen is at rest.
Definition: plot_common.h:541
static const int delta[8][2]
Definition: solve.cpp:112
PAGE_INFO pageInfo
Definition: plot_common.h:545
virtual void SetDash(bool dashed) override
PDF supports dashed lines.
This file contains miscellaneous commonly used macros and functions.
const wxSize & GetSizeMils() const
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: drawtxt.cpp:229
wxString workFilename
Handle to the deferred stream length.
Definition: plot_common.h:872
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.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
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: plot_common.h:871
bool m_plotMirror
X axis orientation (SVG) and plot mirrored (only for PS, PDF HPGL and SVG)
Definition: plot_common.h:526
virtual void SetDefaultLineWidth(int width) override
Set the default line width.
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.
Base window classes and related definitions.
double plotScaleAdjY
Definition: plot_common.h:723
int startPdfStream(int handle=-1)
Starts a PDF stream (for the page).
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
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
Set the plot offset and scaling for the current plot.
virtual void SetColor(COLOR4D color) override
The SetColor implementation is split with the subclasses: The PSLIKE computes the rgb values...
double plotScale
Plot scale - chosen by the user (even implicitly with 'fit in a4')
Definition: plot_common.h:510
Common plot library Plot settings, and plotting engines (Postscript, Gerber, HPGL and DXF) ...
FILE * workFile
Definition: plot_common.h:873
virtual DPOINT userToDeviceSize(const wxSize &size)
Modifies size according to the plotter scale factors (wxSize version, returns a DPOINT) ...
void closePdfObject()
Close the current PDF object.
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:54
wxString title
Definition: plot_common.h:544
virtual DPOINT userToDeviceCoordinates(const wxPoint &aCoordinate)
Modifies coordinates according to the orientation, scale factor, and offsets trace.
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: plot_common.h:868
double cosdecideg(double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:311
double sindecideg(double r, double a)
Circle generation utility: computes r * sin(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:302
void fputsPostscriptString(FILE *fout, const wxString &txt)
Write on a stream a string escaped for postscript/PDF.
virtual void emitSetRGBColor(double r, double g, double b) override
PDF supports colors fully.
see class PGM_BASE
std::vector< long > xrefTable
Temporary file to costruct the stream before zipping.
Definition: plot_common.h:874
int currentPenWidth
Definition: plot_common.h:537
#define max(a, b)
Definition: auxiliary.h:86
int defaultPenWidth
true to generate a negative image (PS mode mainly)
Definition: plot_common.h:536
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: plot_common.h:723
FILE * outputFile
true if the Y axis is top to bottom (SVG)
Definition: plot_common.h:531
double GetDashGapLenIU() const
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:56
void postscriptOverlinePositions(const wxString &aText, int aXSize, bool aItalic, bool aBold, std::vector< int > *pos_pairs)
Computes the x coordinates for the overlining in a string of text.
virtual bool OpenFile(const wxString &aFullFilename) override
Open or create the plot file aFullFilename.
Basic classes for most KiCad items.
virtual void SetPageSettings(const PAGE_INFO &aPageSettings) override
PDF can have multiple pages, so SetPageSettings can be called with the outputFile open (but not insid...
double GetDashMarkLenIU() const
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: plot_common.h:519
wxString filename
Definition: plot_common.h:543
PlotTextMode m_textMode
How to draw text.
Definition: plot_common.h:726
int allocPdfObject()
Allocate a new handle in the table of the PDF object.
void computeTextParameters(const wxPoint &aPos, const wxString &aText, int aOrient, const wxSize &aSize, 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 ...
int pageStreamHandle
Handles to the page objects.
Definition: plot_common.h:870
wxString creator
Definition: plot_common.h:542
wxSize paperSize
Paper size in IU - not in mils.
Definition: plot_common.h:547
virtual void SetCurrentLineWidth(int width, void *aData=NULL) override
Pen width setting for PDF.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
virtual void PlotImage(const wxImage &aImage, const wxPoint &aPos, double aScaleFactor) override
PDF images are handles as inline, not XObject streams...