2012-05-03 19:08:14 +00:00
|
|
|
/**
|
2018-01-28 18:12:26 +00:00
|
|
|
* @file PDF_plotter.cpp
|
2020-11-10 13:50:16 +00:00
|
|
|
* @brief KiCad: specialized plotter for PDF files format
|
2012-05-03 19:08:14 +00:00
|
|
|
*/
|
|
|
|
|
2012-06-08 09:56:42 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1992-2012 Lorenzo Marcantonio, l.marcantonio@logossrl.com
|
2023-01-21 19:11:43 +00:00
|
|
|
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-06-08 09:56:42 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2020-11-06 10:32:14 +00:00
|
|
|
#include <algorithm>
|
2023-04-17 15:39:34 +00:00
|
|
|
#include <cstdio> // snprintf
|
2021-03-20 15:35:37 +00:00
|
|
|
|
|
|
|
#include <wx/filename.h>
|
2012-05-05 04:55:36 +00:00
|
|
|
#include <wx/mstream.h>
|
2021-03-20 15:35:37 +00:00
|
|
|
#include <wx/zstream.h>
|
2023-07-07 16:43:41 +00:00
|
|
|
#include <wx/wfstream.h>
|
|
|
|
#include <wx/datstrm.h>
|
2021-03-20 15:35:37 +00:00
|
|
|
|
2020-11-06 10:32:14 +00:00
|
|
|
#include <advanced_config.h>
|
2022-08-27 18:14:57 +00:00
|
|
|
#include <eda_text.h> // for IsGotoPageHref
|
2023-01-19 23:25:07 +00:00
|
|
|
#include <font/font.h>
|
2023-04-19 02:44:04 +00:00
|
|
|
#include <core/ignore.h>
|
2021-03-20 15:35:37 +00:00
|
|
|
#include <macros.h>
|
|
|
|
#include <trigo.h>
|
2022-09-05 22:28:44 +00:00
|
|
|
#include <string_utils.h>
|
2020-07-11 09:37:22 +00:00
|
|
|
|
2021-08-18 20:38:14 +00:00
|
|
|
#include <plotters/plotters_pslike.h>
|
2020-09-23 18:58:13 +00:00
|
|
|
|
2022-10-17 13:19:39 +00:00
|
|
|
|
2020-07-26 13:57:07 +00:00
|
|
|
std::string PDF_PLOTTER::encodeStringForPlotter( const wxString& aText )
|
|
|
|
{
|
2020-11-10 13:50:16 +00:00
|
|
|
// returns a string compatible with PDF string convention from a unicode string.
|
|
|
|
// if the initial text is only ASCII7, return the text between ( and ) for a good readability
|
|
|
|
// if the initial text is no ASCII7, return the text between < and >
|
|
|
|
// and encoded using 16 bits hexa (4 digits) by wide char (unicode 16)
|
2020-07-26 13:57:07 +00:00
|
|
|
std::string result;
|
|
|
|
|
|
|
|
// Is aText only ASCII7 ?
|
|
|
|
bool is_ascii7 = true;
|
|
|
|
|
|
|
|
for( size_t ii = 0; ii < aText.Len(); ii++ )
|
|
|
|
{
|
|
|
|
if( aText[ii] >= 0x7F )
|
|
|
|
{
|
|
|
|
is_ascii7 = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( is_ascii7 )
|
|
|
|
{
|
|
|
|
result = '(';
|
|
|
|
|
|
|
|
for( unsigned ii = 0; ii < aText.Len(); ii++ )
|
|
|
|
{
|
|
|
|
unsigned int code = aText[ii];
|
|
|
|
|
|
|
|
// These characters must be escaped
|
|
|
|
switch( code )
|
|
|
|
{
|
2020-10-08 15:37:18 +00:00
|
|
|
case '(':
|
|
|
|
case ')':
|
2020-07-26 13:57:07 +00:00
|
|
|
case '\\':
|
|
|
|
result += '\\';
|
|
|
|
KI_FALLTHROUGH;
|
|
|
|
|
|
|
|
default:
|
|
|
|
result += code;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result += ')';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = "<FEFF";
|
|
|
|
|
2023-04-17 15:39:34 +00:00
|
|
|
|
2020-07-26 13:57:07 +00:00
|
|
|
for( size_t ii = 0; ii < aText.Len(); ii++ )
|
|
|
|
{
|
|
|
|
unsigned int code = aText[ii];
|
|
|
|
char buffer[16];
|
2023-04-17 15:39:34 +00:00
|
|
|
std::snprintf( buffer, sizeof( buffer ), "%4.4X", code );
|
2020-07-26 13:57:07 +00:00
|
|
|
result += buffer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
result += '>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-13 18:54:33 +00:00
|
|
|
bool PDF_PLOTTER::OpenFile( const wxString& aFullFilename )
|
|
|
|
{
|
2020-11-16 00:04:55 +00:00
|
|
|
m_filename = aFullFilename;
|
2012-10-13 18:54:33 +00:00
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
wxASSERT( !m_outputFile );
|
2012-10-13 18:54:33 +00:00
|
|
|
|
|
|
|
// Open the PDF file in binary mode
|
2020-11-16 00:04:55 +00:00
|
|
|
m_outputFile = wxFopen( m_filename, wxT( "wb" ) );
|
2012-10-13 18:54:33 +00:00
|
|
|
|
2021-04-22 21:20:34 +00:00
|
|
|
if( m_outputFile == nullptr )
|
2012-10-13 18:54:33 +00:00
|
|
|
return false ;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2021-12-29 19:02:50 +00:00
|
|
|
void PDF_PLOTTER::SetViewport( const VECTOR2I& aOffset, double aIusPerDecimil,
|
2021-06-07 18:31:53 +00:00
|
|
|
double aScale, bool aMirror )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2020-11-16 00:04:55 +00:00
|
|
|
m_plotMirror = aMirror;
|
|
|
|
m_plotOffset = aOffset;
|
|
|
|
m_plotScale = aScale;
|
2012-08-29 20:13:47 +00:00
|
|
|
m_IUsPerDecimil = aIusPerDecimil;
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2020-11-10 13:50:16 +00:00
|
|
|
// The CTM is set to 1 user unit per decimal
|
2020-11-16 00:04:55 +00:00
|
|
|
m_iuPerDeviceUnit = 1.0 / aIusPerDecimil;
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2020-11-10 13:50:16 +00:00
|
|
|
/* The paper size in this engine is handled page by page
|
2012-05-03 19:08:14 +00:00
|
|
|
Look in the StartPage function */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-14 12:25:00 +00:00
|
|
|
void PDF_PLOTTER::SetCurrentLineWidth( int aWidth, void* aData )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( m_workFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2020-05-13 16:44:21 +00:00
|
|
|
if( aWidth == DO_NOT_SET_LINE_WIDTH )
|
|
|
|
return;
|
|
|
|
else if( aWidth == USE_DEFAULT_LINE_WIDTH )
|
|
|
|
aWidth = m_renderSettings->GetDefaultPenWidth();
|
2020-05-27 01:17:16 +00:00
|
|
|
|
|
|
|
if( aWidth == 0 )
|
2020-04-14 12:25:00 +00:00
|
|
|
aWidth = 1;
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2020-05-13 16:44:21 +00:00
|
|
|
wxASSERT_MSG( aWidth > 0, "Plotter called to set negative pen width" );
|
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
if( aWidth != m_currentPenWidth )
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "%g w\n", userToDeviceSize( aWidth ) );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
m_currentPenWidth = aWidth;
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-08 10:35:52 +00:00
|
|
|
void PDF_PLOTTER::emitSetRGBColor( double r, double g, double b, double a )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( m_workFile );
|
2022-04-08 10:35:52 +00:00
|
|
|
|
|
|
|
// PDF treats all colors as opaque, so the best we can do with alpha is generate an
|
|
|
|
// appropriate blended color assuming white paper.
|
|
|
|
if( a < 1.0 )
|
|
|
|
{
|
|
|
|
r = ( r * a ) + ( 1 - a );
|
|
|
|
g = ( g * a ) + ( 1 - a );
|
|
|
|
b = ( b * a ) + ( 1 - a );
|
|
|
|
}
|
|
|
|
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "%g %g %g rg %g %g %g RG\n", r, g, b, r, g, b );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2022-06-28 18:06:17 +00:00
|
|
|
void PDF_PLOTTER::SetDash( int aLineWidth, PLOT_DASH_TYPE aLineStyle )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( m_workFile );
|
2022-06-28 18:06:17 +00:00
|
|
|
|
|
|
|
switch( aLineStyle )
|
2017-11-13 03:53:27 +00:00
|
|
|
{
|
2019-12-28 00:55:11 +00:00
|
|
|
case PLOT_DASH_TYPE::DASH:
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "[%d %d] 0 d\n",
|
2022-06-28 18:06:17 +00:00
|
|
|
(int) GetDashMarkLenIU( aLineWidth ), (int) GetDashGapLenIU( aLineWidth ) );
|
2017-11-13 03:53:27 +00:00
|
|
|
break;
|
2022-06-28 18:06:17 +00:00
|
|
|
|
2019-12-28 00:55:11 +00:00
|
|
|
case PLOT_DASH_TYPE::DOT:
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "[%d %d] 0 d\n",
|
2022-06-28 18:06:17 +00:00
|
|
|
(int) GetDotMarkLenIU( aLineWidth ), (int) GetDashGapLenIU( aLineWidth ) );
|
2017-11-13 03:53:27 +00:00
|
|
|
break;
|
2022-06-28 18:06:17 +00:00
|
|
|
|
2019-12-28 00:55:11 +00:00
|
|
|
case PLOT_DASH_TYPE::DASHDOT:
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "[%d %d %d %d] 0 d\n",
|
2022-06-28 18:06:17 +00:00
|
|
|
(int) GetDashMarkLenIU( aLineWidth ), (int) GetDashGapLenIU( aLineWidth ),
|
|
|
|
(int) GetDotMarkLenIU( aLineWidth ), (int) GetDashGapLenIU( aLineWidth ) );
|
2017-11-13 03:53:27 +00:00
|
|
|
break;
|
2022-06-28 18:06:17 +00:00
|
|
|
|
2021-07-22 23:05:01 +00:00
|
|
|
case PLOT_DASH_TYPE::DASHDOTDOT:
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "[%d %d %d %d %d %d] 0 d\n",
|
2022-06-28 18:06:17 +00:00
|
|
|
(int) GetDashMarkLenIU( aLineWidth ), (int) GetDashGapLenIU( aLineWidth ),
|
|
|
|
(int) GetDotMarkLenIU( aLineWidth ), (int) GetDashGapLenIU( aLineWidth ),
|
|
|
|
(int) GetDotMarkLenIU( aLineWidth ), (int) GetDashGapLenIU( aLineWidth ) );
|
2021-07-22 23:05:01 +00:00
|
|
|
break;
|
2022-06-28 18:06:17 +00:00
|
|
|
|
2017-11-13 03:53:27 +00:00
|
|
|
default:
|
2022-05-15 12:59:23 +00:00
|
|
|
fputs( "[] 0 d\n", m_workFile );
|
2017-11-13 03:53:27 +00:00
|
|
|
}
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-29 19:02:50 +00:00
|
|
|
void PDF_PLOTTER::Rect( const VECTOR2I& p1, const VECTOR2I& p2, FILL_T fill, int width )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( m_workFile );
|
2023-10-12 02:59:26 +00:00
|
|
|
|
2023-10-12 05:40:37 +00:00
|
|
|
if( fill == FILL_T::NO_FILL && width <= 0 )
|
|
|
|
return;
|
|
|
|
|
2023-10-12 02:59:26 +00:00
|
|
|
SetCurrentLineWidth( width );
|
|
|
|
|
|
|
|
VECTOR2I size = p2 - p1;
|
|
|
|
|
|
|
|
if( size.x == 0 && size.y == 0 )
|
|
|
|
{
|
|
|
|
// Can't draw zero-sized rectangles
|
|
|
|
MoveTo( VECTOR2I( p1.x, p1.y ) );
|
|
|
|
FinishTo( VECTOR2I( p1.x, p1.y ) );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( std::min( std::abs( size.x ), std::abs( size.y ) ) < width )
|
|
|
|
{
|
|
|
|
// Too thick stroked rectangles are buggy, draw as polygon
|
|
|
|
std::vector<VECTOR2I> cornerList;
|
|
|
|
|
|
|
|
cornerList.emplace_back( p1.x, p1.y );
|
|
|
|
cornerList.emplace_back( p2.x, p1.y );
|
|
|
|
cornerList.emplace_back( p2.x, p2.y );
|
|
|
|
cornerList.emplace_back( p1.x, p2.y );
|
|
|
|
cornerList.emplace_back( p1.x, p1.y );
|
|
|
|
|
|
|
|
PlotPoly( cornerList, fill, width, nullptr );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-20 00:17:44 +00:00
|
|
|
VECTOR2D p1_dev = userToDeviceCoordinates( p1 );
|
|
|
|
VECTOR2D p2_dev = userToDeviceCoordinates( p2 );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2023-10-12 05:40:37 +00:00
|
|
|
char paintOp;
|
|
|
|
|
|
|
|
if( fill == FILL_T::NO_FILL )
|
|
|
|
paintOp = 'S';
|
|
|
|
else
|
|
|
|
paintOp = width > 0 ? 'B' : 'f';
|
|
|
|
|
2023-10-12 02:59:26 +00:00
|
|
|
fprintf( m_workFile, "%g %g %g %g re %c\n", p1_dev.x, p1_dev.y, p2_dev.x - p1_dev.x,
|
2023-10-12 05:40:37 +00:00
|
|
|
p2_dev.y - p1_dev.y, paintOp );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-29 19:02:50 +00:00
|
|
|
void PDF_PLOTTER::Circle( const VECTOR2I& pos, int diametre, FILL_T aFill, int width )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( m_workFile );
|
2023-10-12 05:40:37 +00:00
|
|
|
|
|
|
|
if( aFill == FILL_T::NO_FILL && width <= 0 )
|
|
|
|
return;
|
|
|
|
|
2022-01-20 00:17:44 +00:00
|
|
|
VECTOR2D pos_dev = userToDeviceCoordinates( pos );
|
|
|
|
double radius = userToDeviceSize( diametre / 2.0 );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
/* OK. Here's a trick. PDF doesn't support circles or circular angles, that's
|
|
|
|
a fact. You'll have to do with cubic beziers. These *can't* represent
|
|
|
|
circular arcs (NURBS can, beziers don't). But there is a widely known
|
2015-03-18 19:50:42 +00:00
|
|
|
approximation which is really good
|
|
|
|
*/
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
SetCurrentLineWidth( width );
|
2018-03-14 01:01:01 +00:00
|
|
|
|
|
|
|
// If diameter is less than width, switch to filled mode
|
2021-07-18 23:08:54 +00:00
|
|
|
if( aFill == FILL_T::NO_FILL && diametre < width )
|
2018-03-14 01:01:01 +00:00
|
|
|
{
|
2021-07-18 23:08:54 +00:00
|
|
|
aFill = FILL_T::FILLED_SHAPE;
|
2018-03-14 01:01:01 +00:00
|
|
|
SetCurrentLineWidth( 0 );
|
|
|
|
|
|
|
|
radius = userToDeviceSize( ( diametre / 2.0 ) + ( width / 2.0 ) );
|
|
|
|
}
|
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
double magic = radius * 0.551784; // You don't want to know where this come from
|
|
|
|
|
|
|
|
// This is the convex hull for the bezier approximated circle
|
2022-10-17 13:19:39 +00:00
|
|
|
fprintf( m_workFile,
|
|
|
|
"%g %g m "
|
|
|
|
"%g %g %g %g %g %g c "
|
|
|
|
"%g %g %g %g %g %g c "
|
|
|
|
"%g %g %g %g %g %g c "
|
|
|
|
"%g %g %g %g %g %g c %c\n",
|
2012-05-03 19:08:14 +00:00
|
|
|
pos_dev.x - radius, pos_dev.y,
|
|
|
|
|
|
|
|
pos_dev.x - radius, pos_dev.y + magic,
|
|
|
|
pos_dev.x - magic, pos_dev.y + radius,
|
|
|
|
pos_dev.x, pos_dev.y + radius,
|
|
|
|
|
|
|
|
pos_dev.x + magic, pos_dev.y + radius,
|
|
|
|
pos_dev.x + radius, pos_dev.y + magic,
|
|
|
|
pos_dev.x + radius, pos_dev.y,
|
|
|
|
|
|
|
|
pos_dev.x + radius, pos_dev.y - magic,
|
|
|
|
pos_dev.x + magic, pos_dev.y - radius,
|
|
|
|
pos_dev.x, pos_dev.y - radius,
|
|
|
|
|
|
|
|
pos_dev.x - magic, pos_dev.y - radius,
|
|
|
|
pos_dev.x - radius, pos_dev.y - magic,
|
|
|
|
pos_dev.x - radius, pos_dev.y,
|
|
|
|
|
2021-07-18 23:08:54 +00:00
|
|
|
aFill == FILL_T::NO_FILL ? 's' : 'b' );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-06 15:39:24 +00:00
|
|
|
void PDF_PLOTTER::Arc( const VECTOR2D& aCenter, const EDA_ANGLE& aStartAngle,
|
2023-08-22 13:06:53 +00:00
|
|
|
const EDA_ANGLE& aAngle, double aRadius, FILL_T aFill, int aWidth )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( m_workFile );
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2022-01-16 01:06:25 +00:00
|
|
|
if( aRadius <= 0 )
|
2018-03-14 01:01:01 +00:00
|
|
|
{
|
2022-01-16 01:06:25 +00:00
|
|
|
Circle( aCenter, aWidth, FILL_T::FILLED_SHAPE, 0 );
|
2012-05-03 19:08:14 +00:00
|
|
|
return;
|
2018-03-14 01:01:01 +00:00
|
|
|
}
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2022-01-16 01:06:25 +00:00
|
|
|
/*
|
|
|
|
* Arcs are not so easily approximated by beziers (in the general case), so we approximate
|
|
|
|
* them in the old way
|
|
|
|
*/
|
2023-08-22 13:06:53 +00:00
|
|
|
EDA_ANGLE startAngle = -aStartAngle;
|
|
|
|
EDA_ANGLE endAngle = startAngle - aAngle;
|
2022-01-16 01:06:25 +00:00
|
|
|
VECTOR2I start;
|
|
|
|
VECTOR2I end;
|
|
|
|
const EDA_ANGLE delta( 5, DEGREES_T ); // increment to draw circles
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2023-08-22 13:06:53 +00:00
|
|
|
if( startAngle > endAngle )
|
|
|
|
std::swap( startAngle, endAngle );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2022-01-16 01:06:25 +00:00
|
|
|
SetCurrentLineWidth( aWidth );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
// Usual trig arc plotting routine...
|
2022-01-26 19:15:17 +00:00
|
|
|
start.x = aCenter.x + KiROUND( aRadius * (-startAngle).Cos() );
|
|
|
|
start.y = aCenter.y + KiROUND( aRadius * (-startAngle).Sin() );
|
2022-01-20 00:17:44 +00:00
|
|
|
VECTOR2D pos_dev = userToDeviceCoordinates( start );
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "%g %g m ", pos_dev.x, pos_dev.y );
|
2021-06-07 18:31:53 +00:00
|
|
|
|
2022-01-16 01:06:25 +00:00
|
|
|
for( EDA_ANGLE ii = startAngle + delta; ii < endAngle; ii += delta )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-01-26 19:15:17 +00:00
|
|
|
end.x = aCenter.x + KiROUND( aRadius * (-ii).Cos() );
|
|
|
|
end.y = aCenter.y + KiROUND( aRadius * (-ii).Sin() );
|
2012-05-03 19:08:14 +00:00
|
|
|
pos_dev = userToDeviceCoordinates( end );
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "%g %g l ", pos_dev.x, pos_dev.y );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 19:15:17 +00:00
|
|
|
end.x = aCenter.x + KiROUND( aRadius * (-endAngle).Cos() );
|
|
|
|
end.y = aCenter.y + KiROUND( aRadius * (-endAngle).Sin() );
|
2012-05-03 19:08:14 +00:00
|
|
|
pos_dev = userToDeviceCoordinates( end );
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "%g %g l ", pos_dev.x, pos_dev.y );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
// The arc is drawn... if not filled we stroke it, otherwise we finish
|
|
|
|
// closing the pie at the center
|
2022-01-16 01:06:25 +00:00
|
|
|
if( aFill == FILL_T::NO_FILL )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
fputs( "S\n", m_workFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-16 01:06:25 +00:00
|
|
|
pos_dev = userToDeviceCoordinates( aCenter );
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "%g %g l b\n", pos_dev.x, pos_dev.y );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-29 19:02:50 +00:00
|
|
|
void PDF_PLOTTER::PlotPoly( const std::vector<VECTOR2I>& aCornerList, FILL_T aFill, int aWidth,
|
2021-07-18 23:08:54 +00:00
|
|
|
void* aData )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( m_workFile );
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
if( aCornerList.size() <= 1 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
SetCurrentLineWidth( aWidth );
|
|
|
|
|
2022-01-20 00:17:44 +00:00
|
|
|
VECTOR2D pos = userToDeviceCoordinates( aCornerList[0] );
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "%g %g m\n", pos.x, pos.y );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
|
|
|
|
{
|
|
|
|
pos = userToDeviceCoordinates( aCornerList[ii] );
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "%g %g l\n", pos.x, pos.y );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 10:56:47 +00:00
|
|
|
// Close path and stroke and/or fill
|
|
|
|
if( aFill == FILL_T::NO_FILL )
|
|
|
|
fputs( "S\n", m_workFile );
|
|
|
|
else if( aWidth == 0 )
|
|
|
|
fputs( "f\n", m_workFile );
|
|
|
|
else
|
|
|
|
fputs( "b\n", m_workFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-29 19:02:50 +00:00
|
|
|
void PDF_PLOTTER::PenTo( const VECTOR2I& pos, char plume )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( m_workFile );
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
if( plume == 'Z' )
|
|
|
|
{
|
2020-11-16 00:04:55 +00:00
|
|
|
if( m_penState != 'Z' )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
fputs( "S\n", m_workFile );
|
2020-11-16 00:04:55 +00:00
|
|
|
m_penState = 'Z';
|
|
|
|
m_penLastpos.x = -1;
|
|
|
|
m_penLastpos.y = -1;
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
if( m_penState != plume || pos != m_penLastpos )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-01-20 00:17:44 +00:00
|
|
|
VECTOR2D pos_dev = userToDeviceCoordinates( pos );
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "%g %g %c\n",
|
2012-05-03 19:08:14 +00:00
|
|
|
pos_dev.x, pos_dev.y,
|
|
|
|
( plume=='D' ) ? 'l' : 'm' );
|
|
|
|
}
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
m_penState = plume;
|
|
|
|
m_penLastpos = pos;
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2021-12-29 19:02:50 +00:00
|
|
|
void PDF_PLOTTER::PlotImage( const wxImage& aImage, const VECTOR2I& aPos, double aScaleFactor )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( m_workFile );
|
2021-12-29 19:02:50 +00:00
|
|
|
VECTOR2I pix_size( aImage.GetWidth(), aImage.GetHeight() );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
// Requested size (in IUs)
|
2022-01-20 00:17:44 +00:00
|
|
|
VECTOR2D drawsize( aScaleFactor * pix_size.x, aScaleFactor * pix_size.y );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
// calculate the bitmap start position
|
2021-12-29 19:02:50 +00:00
|
|
|
VECTOR2I start( aPos.x - drawsize.x / 2, aPos.y + drawsize.y / 2 );
|
2022-01-20 00:17:44 +00:00
|
|
|
VECTOR2D dev_start = userToDeviceCoordinates( start );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2023-07-07 16:43:41 +00:00
|
|
|
// Deduplicate images
|
2023-07-12 18:09:46 +00:00
|
|
|
auto findHandleForImage = [&]( const wxImage& aCurrImage ) -> int
|
2023-07-07 16:43:41 +00:00
|
|
|
{
|
|
|
|
for( const auto& [imgHandle, image] : m_imageHandles )
|
|
|
|
{
|
2023-07-12 18:09:46 +00:00
|
|
|
if( image.IsSameAs( aCurrImage ) )
|
2023-07-07 16:43:41 +00:00
|
|
|
return imgHandle;
|
|
|
|
|
2023-07-12 18:09:46 +00:00
|
|
|
if( image.GetWidth() != aCurrImage.GetWidth() )
|
2023-07-07 16:43:41 +00:00
|
|
|
continue;
|
|
|
|
|
2023-07-12 18:09:46 +00:00
|
|
|
if( image.GetHeight() != aCurrImage.GetHeight() )
|
2023-07-07 16:43:41 +00:00
|
|
|
continue;
|
|
|
|
|
2023-07-12 18:09:46 +00:00
|
|
|
if( image.GetType() != aCurrImage.GetType() )
|
2023-07-07 16:43:41 +00:00
|
|
|
continue;
|
|
|
|
|
2023-07-12 18:09:46 +00:00
|
|
|
if( image.HasAlpha() != aCurrImage.HasAlpha() )
|
2023-07-07 16:43:41 +00:00
|
|
|
continue;
|
|
|
|
|
2023-07-12 18:09:46 +00:00
|
|
|
if( image.HasMask() != aCurrImage.HasMask() || image.GetMaskRed() != aCurrImage.GetMaskRed()
|
|
|
|
|| image.GetMaskGreen() != aCurrImage.GetMaskGreen()
|
|
|
|
|| image.GetMaskBlue() != aCurrImage.GetMaskBlue() )
|
2023-07-07 16:43:41 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int pixCount = image.GetWidth() * image.GetHeight();
|
2023-07-12 18:09:46 +00:00
|
|
|
|
|
|
|
if( memcmp( image.GetData(), aCurrImage.GetData(), pixCount * 3 ) != 0 )
|
2023-07-07 16:43:41 +00:00
|
|
|
continue;
|
|
|
|
|
2023-07-12 18:09:46 +00:00
|
|
|
if( image.HasAlpha() && memcmp( image.GetAlpha(), aCurrImage.GetAlpha(), pixCount ) != 0 )
|
2023-07-07 16:43:41 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
return imgHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
int imgHandle = findHandleForImage( aImage );
|
|
|
|
|
|
|
|
if( imgHandle == -1 )
|
|
|
|
{
|
|
|
|
imgHandle = allocPdfObject();
|
|
|
|
m_imageHandles.emplace( imgHandle, aImage );
|
|
|
|
}
|
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
/* PDF has an uhm... simplified coordinate system handling. There is
|
|
|
|
*one* operator to do everything (the PS concat equivalent). At least
|
|
|
|
they kept the matrix stack to save restore environments. Also images
|
|
|
|
are always emitted at the origin with a size of 1x1 user units.
|
|
|
|
What we need to do is:
|
2019-08-20 17:22:30 +00:00
|
|
|
1) save the CTM end establish the new one
|
2012-05-03 19:08:14 +00:00
|
|
|
2) plot the image
|
|
|
|
3) restore the CTM
|
|
|
|
4) profit
|
|
|
|
*/
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile, "q %g 0 0 %g %g %g cm\n", // Step 1
|
2021-06-07 18:31:53 +00:00
|
|
|
userToDeviceSize( drawsize.x ),
|
|
|
|
userToDeviceSize( drawsize.y ),
|
|
|
|
dev_start.x, dev_start.y );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2023-07-07 16:43:41 +00:00
|
|
|
fprintf( m_workFile, "/Im%d Do\n", imgHandle );
|
|
|
|
fputs( "Q\n", m_workFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int PDF_PLOTTER::allocPdfObject()
|
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
m_xrefTable.push_back( 0 );
|
|
|
|
return m_xrefTable.size() - 1;
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int PDF_PLOTTER::startPdfObject(int handle)
|
|
|
|
{
|
2020-11-16 00:04:55 +00:00
|
|
|
wxASSERT( m_outputFile );
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( !m_workFile );
|
2017-04-21 12:16:40 +00:00
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
if( handle < 0)
|
|
|
|
handle = allocPdfObject();
|
|
|
|
|
2022-05-15 12:59:23 +00:00
|
|
|
m_xrefTable[handle] = ftell( m_outputFile );
|
2020-11-16 00:04:55 +00:00
|
|
|
fprintf( m_outputFile, "%d 0 obj\n", handle );
|
2012-05-03 19:08:14 +00:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PDF_PLOTTER::closePdfObject()
|
|
|
|
{
|
2020-11-16 00:04:55 +00:00
|
|
|
wxASSERT( m_outputFile );
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( !m_workFile );
|
2020-11-16 00:04:55 +00:00
|
|
|
fputs( "endobj\n", m_outputFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-10 13:50:16 +00:00
|
|
|
int PDF_PLOTTER::startPdfStream( int handle )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2020-11-16 00:04:55 +00:00
|
|
|
wxASSERT( m_outputFile );
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( !m_workFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
handle = startPdfObject( handle );
|
|
|
|
|
|
|
|
// This is guaranteed to be handle+1 but needs to be allocated since
|
|
|
|
// you could allocate more object during stream preparation
|
2022-05-15 12:59:23 +00:00
|
|
|
m_streamLengthHandle = allocPdfObject();
|
2020-11-06 10:32:14 +00:00
|
|
|
|
|
|
|
if( ADVANCED_CFG::GetCfg().m_DebugPDFWriter )
|
|
|
|
{
|
2020-11-16 00:04:55 +00:00
|
|
|
fprintf( m_outputFile,
|
2020-11-06 10:32:14 +00:00
|
|
|
"<< /Length %d 0 R >>\n" // Length is deferred
|
|
|
|
"stream\n", handle + 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-16 00:04:55 +00:00
|
|
|
fprintf( m_outputFile,
|
2020-11-06 10:32:14 +00:00
|
|
|
"<< /Length %d 0 R /Filter /FlateDecode >>\n" // Length is deferred
|
|
|
|
"stream\n", handle + 1 );
|
|
|
|
}
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
// Open a temporary file to accumulate the stream
|
2022-05-15 12:59:23 +00:00
|
|
|
m_workFilename = wxFileName::CreateTempFileName( "" );
|
|
|
|
m_workFile = wxFopen( m_workFilename, wxT( "w+b" ) );
|
|
|
|
wxASSERT( m_workFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PDF_PLOTTER::closePdfStream()
|
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( m_workFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2022-05-15 12:59:23 +00:00
|
|
|
long stream_len = ftell( m_workFile );
|
2015-04-02 11:18:19 +00:00
|
|
|
|
|
|
|
if( stream_len < 0 )
|
|
|
|
{
|
|
|
|
wxASSERT( false );
|
|
|
|
return;
|
|
|
|
}
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2012-05-09 00:26:15 +00:00
|
|
|
// Rewind the file, read in the page stream and DEFLATE it
|
2022-05-15 12:59:23 +00:00
|
|
|
fseek( m_workFile, 0, SEEK_SET );
|
2012-05-03 19:08:14 +00:00
|
|
|
unsigned char *inbuf = new unsigned char[stream_len];
|
|
|
|
|
2022-05-15 12:59:23 +00:00
|
|
|
int rc = fread( inbuf, 1, stream_len, m_workFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
wxASSERT( rc == stream_len );
|
2021-10-05 16:57:25 +00:00
|
|
|
ignore_unused( rc );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2012-05-09 00:26:15 +00:00
|
|
|
// We are done with the temporary file, junk it
|
2022-05-15 12:59:23 +00:00
|
|
|
fclose( m_workFile );
|
|
|
|
m_workFile = nullptr;
|
|
|
|
::wxRemoveFile( m_workFilename );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2020-11-06 10:32:14 +00:00
|
|
|
unsigned out_count;
|
2012-05-05 04:55:36 +00:00
|
|
|
|
2020-11-06 10:32:14 +00:00
|
|
|
if( ADVANCED_CFG::GetCfg().m_DebugPDFWriter )
|
2012-05-05 04:55:36 +00:00
|
|
|
{
|
2020-11-06 10:32:14 +00:00
|
|
|
out_count = stream_len;
|
2020-11-16 00:04:55 +00:00
|
|
|
fwrite( inbuf, out_count, 1, m_outputFile );
|
2020-11-06 10:32:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// NULL means memos owns the memory, but provide a hint on optimum size needed.
|
2021-07-15 19:26:35 +00:00
|
|
|
wxMemoryOutputStream memos( nullptr, std::max( 2000l, stream_len ) ) ;
|
2012-05-05 04:55:36 +00:00
|
|
|
|
2020-11-06 10:32:14 +00:00
|
|
|
{
|
|
|
|
/* Somewhat standard parameters to compress in DEFLATE. The PDF spec is
|
|
|
|
* misleading, it says it wants a DEFLATE stream but it really want a ZLIB
|
|
|
|
* stream! (a DEFLATE stream would be generated with -15 instead of 15)
|
|
|
|
* rc = deflateInit2( &zstrm, Z_BEST_COMPRESSION, Z_DEFLATED, 15,
|
|
|
|
* 8, Z_DEFAULT_STRATEGY );
|
|
|
|
*/
|
2012-05-05 04:55:36 +00:00
|
|
|
|
2020-11-06 10:32:14 +00:00
|
|
|
wxZlibOutputStream zos( memos, wxZ_BEST_COMPRESSION, wxZLIB_ZLIB );
|
2012-05-05 04:55:36 +00:00
|
|
|
|
2020-11-06 10:32:14 +00:00
|
|
|
zos.Write( inbuf, stream_len );
|
|
|
|
} // flush the zip stream using zos destructor
|
2012-05-05 04:55:36 +00:00
|
|
|
|
2020-11-06 10:32:14 +00:00
|
|
|
wxStreamBuffer* sb = memos.GetOutputStreamBuffer();
|
2012-05-05 04:55:36 +00:00
|
|
|
|
2020-11-06 10:32:14 +00:00
|
|
|
out_count = sb->Tell();
|
2020-11-16 00:04:55 +00:00
|
|
|
fwrite( sb->GetBufferStart(), 1, out_count, m_outputFile );
|
2020-11-06 10:32:14 +00:00
|
|
|
}
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2020-11-10 13:50:16 +00:00
|
|
|
delete[] inbuf;
|
2022-09-25 01:45:48 +00:00
|
|
|
fputs( "\nendstream\n", m_outputFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
closePdfObject();
|
|
|
|
|
|
|
|
// Writing the deferred length as an indirect object
|
2022-05-15 12:59:23 +00:00
|
|
|
startPdfObject( m_streamLengthHandle );
|
2020-11-16 00:04:55 +00:00
|
|
|
fprintf( m_outputFile, "%u\n", out_count );
|
2012-05-03 19:08:14 +00:00
|
|
|
closePdfObject();
|
|
|
|
}
|
|
|
|
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2022-09-25 02:52:09 +00:00
|
|
|
void PDF_PLOTTER::StartPage( const wxString& aPageNumber, const wxString& aPageName )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2020-11-16 00:04:55 +00:00
|
|
|
wxASSERT( m_outputFile );
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( !m_workFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2022-08-27 18:14:57 +00:00
|
|
|
m_pageNumbers.push_back( aPageNumber );
|
2022-09-25 02:52:09 +00:00
|
|
|
m_pageName = aPageName;
|
2022-08-27 18:14:57 +00:00
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
// Compute the paper size in IUs
|
2020-11-16 00:04:55 +00:00
|
|
|
m_paperSize = m_pageInfo.GetSizeMils();
|
|
|
|
m_paperSize.x *= 10.0 / m_iuPerDeviceUnit;
|
|
|
|
m_paperSize.y *= 10.0 / m_iuPerDeviceUnit;
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
// Open the content stream; the page object will go later
|
2022-05-15 12:59:23 +00:00
|
|
|
m_pageStreamHandle = startPdfStream();
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
/* Now, until ClosePage *everything* must be wrote in workFile, to be
|
|
|
|
compressed later in closePdfStream */
|
|
|
|
|
|
|
|
// Default graphic settings (coordinate system, default color and line style)
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_workFile,
|
2012-05-03 19:08:14 +00:00
|
|
|
"%g 0 0 %g 0 0 cm 1 J 1 j 0 0 0 rg 0 0 0 RG %g w\n",
|
|
|
|
0.0072 * plotScaleAdjX, 0.0072 * plotScaleAdjY,
|
2020-04-14 12:25:00 +00:00
|
|
|
userToDeviceSize( m_renderSettings->GetDefaultPenWidth() ) );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2023-07-07 16:43:41 +00:00
|
|
|
void WriteImageStream( const wxImage& aImage, wxDataOutputStream& aOut, wxColor bg, bool colorMode )
|
|
|
|
{
|
|
|
|
int w = aImage.GetWidth();
|
|
|
|
int h = aImage.GetHeight();
|
|
|
|
|
|
|
|
for( int y = 0; y < h; y++ )
|
|
|
|
{
|
|
|
|
for( int x = 0; x < w; x++ )
|
|
|
|
{
|
|
|
|
unsigned char r = aImage.GetRed( x, y ) & 0xFF;
|
|
|
|
unsigned char g = aImage.GetGreen( x, y ) & 0xFF;
|
|
|
|
unsigned char b = aImage.GetBlue( x, y ) & 0xFF;
|
|
|
|
|
|
|
|
if( aImage.HasMask() )
|
|
|
|
{
|
|
|
|
if( r == aImage.GetMaskRed() && g == aImage.GetMaskGreen()
|
|
|
|
&& b == aImage.GetMaskBlue() )
|
|
|
|
{
|
|
|
|
r = bg.Red();
|
|
|
|
g = bg.Green();
|
|
|
|
b = bg.Blue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( colorMode )
|
|
|
|
{
|
|
|
|
aOut.Write8( r );
|
|
|
|
aOut.Write8( g );
|
|
|
|
aOut.Write8( b );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Greyscale conversion (CIE 1931)
|
|
|
|
unsigned char grey = KiROUND( r * 0.2126 + g * 0.7152 + b * 0.0722 );
|
2023-07-12 18:09:46 +00:00
|
|
|
|
2023-07-07 16:43:41 +00:00
|
|
|
aOut.Write8( grey );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteImageSMaskStream( const wxImage& aImage, wxDataOutputStream& aOut )
|
|
|
|
{
|
|
|
|
int w = aImage.GetWidth();
|
|
|
|
int h = aImage.GetHeight();
|
|
|
|
|
|
|
|
if( aImage.HasMask() )
|
|
|
|
{
|
|
|
|
for( int y = 0; y < h; y++ )
|
|
|
|
{
|
|
|
|
for( int x = 0; x < w; x++ )
|
|
|
|
{
|
|
|
|
unsigned char a = 255;
|
|
|
|
unsigned char r = aImage.GetRed( x, y );
|
|
|
|
unsigned char g = aImage.GetGreen( x, y );
|
|
|
|
unsigned char b = aImage.GetBlue( x, y );
|
|
|
|
|
|
|
|
if( r == aImage.GetMaskRed() && g == aImage.GetMaskGreen()
|
|
|
|
&& b == aImage.GetMaskBlue() )
|
|
|
|
{
|
|
|
|
a = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
aOut.Write8( a );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( aImage.HasAlpha() )
|
|
|
|
{
|
|
|
|
int size = w * h;
|
|
|
|
aOut.Write8( aImage.GetAlpha(), size );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
void PDF_PLOTTER::ClosePage()
|
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
wxASSERT( m_workFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
// Close the page stream (and compress it)
|
|
|
|
closePdfStream();
|
|
|
|
|
2022-08-27 18:14:57 +00:00
|
|
|
// Page size is in 1/72 of inch (default user space units). Works like the bbox in postscript
|
|
|
|
// but there is no need for swapping the sizes, since PDF doesn't require a portrait page.
|
|
|
|
// We use the MediaBox but PDF has lots of other less-used boxes that could be used.
|
2022-05-15 19:35:00 +00:00
|
|
|
const double PTsPERMIL = 0.072;
|
|
|
|
VECTOR2D psPaperSize = VECTOR2D( m_pageInfo.GetSizeMils() ) * PTsPERMIL;
|
|
|
|
|
2022-09-05 22:28:44 +00:00
|
|
|
auto iuToPdfUserSpace =
|
|
|
|
[&]( const VECTOR2I& aCoord ) -> VECTOR2D
|
|
|
|
{
|
|
|
|
VECTOR2D retval = VECTOR2D( aCoord ) * PTsPERMIL / ( m_IUsPerDecimil * 10 );
|
|
|
|
// PDF y=0 is at bottom of page, invert coordinate
|
|
|
|
retval.y = psPaperSize.y - retval.y;
|
|
|
|
return retval;
|
|
|
|
};
|
2022-05-15 19:35:00 +00:00
|
|
|
|
|
|
|
// Handle annotations (at the moment only "link" type objects)
|
|
|
|
std::vector<int> hyperlinkHandles;
|
|
|
|
|
2022-08-27 18:14:57 +00:00
|
|
|
// Allocate all hyperlink objects for the page and calculate their position in user space
|
|
|
|
// coordinates
|
2022-07-03 15:19:55 +00:00
|
|
|
for( const std::pair<BOX2I, wxString>& linkPair : m_hyperlinksInPage )
|
2022-06-23 22:29:48 +00:00
|
|
|
{
|
|
|
|
const BOX2I& box = linkPair.first;
|
|
|
|
const wxString& url = linkPair.second;
|
|
|
|
|
|
|
|
VECTOR2D bottomLeft = iuToPdfUserSpace( box.GetPosition() );
|
|
|
|
VECTOR2D topRight = iuToPdfUserSpace( box.GetEnd() );
|
|
|
|
|
|
|
|
BOX2D userSpaceBox;
|
|
|
|
userSpaceBox.SetOrigin( bottomLeft );
|
|
|
|
userSpaceBox.SetEnd( topRight );
|
|
|
|
|
2022-07-03 15:19:55 +00:00
|
|
|
hyperlinkHandles.push_back( allocPdfObject() );
|
2022-06-23 22:29:48 +00:00
|
|
|
|
2022-07-03 15:19:55 +00:00
|
|
|
m_hyperlinkHandles.insert( { hyperlinkHandles.back(), { userSpaceBox, url } } );
|
2022-06-23 22:29:48 +00:00
|
|
|
}
|
2022-07-03 15:19:55 +00:00
|
|
|
|
2022-08-27 22:28:31 +00:00
|
|
|
for( const std::pair<BOX2I, std::vector<wxString>>& menuPair : m_hyperlinkMenusInPage )
|
|
|
|
{
|
|
|
|
const BOX2I& box = menuPair.first;
|
|
|
|
const std::vector<wxString>& urls = menuPair.second;
|
|
|
|
|
|
|
|
VECTOR2D bottomLeft = iuToPdfUserSpace( box.GetPosition() );
|
|
|
|
VECTOR2D topRight = iuToPdfUserSpace( box.GetEnd() );
|
|
|
|
|
|
|
|
BOX2D userSpaceBox;
|
|
|
|
userSpaceBox.SetOrigin( bottomLeft );
|
|
|
|
userSpaceBox.SetEnd( topRight );
|
|
|
|
|
|
|
|
hyperlinkHandles.push_back( allocPdfObject() );
|
|
|
|
|
|
|
|
m_hyperlinkMenuHandles.insert( { hyperlinkHandles.back(), { userSpaceBox, urls } } );
|
|
|
|
}
|
|
|
|
|
2022-05-15 19:35:00 +00:00
|
|
|
int hyperLinkArrayHandle = -1;
|
|
|
|
|
|
|
|
// If we have added any annotation links, create an array containing all the objects
|
|
|
|
if( hyperlinkHandles.size() > 0 )
|
|
|
|
{
|
|
|
|
hyperLinkArrayHandle = startPdfObject();
|
|
|
|
bool isFirst = true;
|
|
|
|
|
2022-08-27 18:14:57 +00:00
|
|
|
fputs( "[", m_outputFile );
|
2022-05-15 19:35:00 +00:00
|
|
|
|
2022-08-27 18:14:57 +00:00
|
|
|
for( int handle : hyperlinkHandles )
|
2022-05-15 19:35:00 +00:00
|
|
|
{
|
2022-08-27 18:14:57 +00:00
|
|
|
if( isFirst )
|
|
|
|
isFirst = false;
|
|
|
|
else
|
|
|
|
fprintf( m_outputFile, " " );
|
|
|
|
|
|
|
|
fprintf( m_outputFile, "%d 0 R", handle );
|
2022-05-15 19:35:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fputs( "]\n", m_outputFile );
|
|
|
|
closePdfObject();
|
|
|
|
}
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2022-05-15 19:35:00 +00:00
|
|
|
// Emit the page object and put it in the page list for later
|
2022-09-25 01:45:48 +00:00
|
|
|
int pageHandle = startPdfObject();
|
|
|
|
m_pageHandles.push_back( pageHandle );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
fprintf( m_outputFile,
|
2012-05-03 19:08:14 +00:00
|
|
|
"<<\n"
|
|
|
|
"/Type /Page\n"
|
|
|
|
"/Parent %d 0 R\n"
|
|
|
|
"/Resources <<\n"
|
|
|
|
" /ProcSet [/PDF /Text /ImageC /ImageB]\n"
|
2023-07-07 16:43:41 +00:00
|
|
|
" /Font %d 0 R\n"
|
|
|
|
" /XObject %d 0 R >>\n"
|
2022-05-15 19:35:00 +00:00
|
|
|
"/MediaBox [0 0 %g %g]\n"
|
|
|
|
"/Contents %d 0 R\n",
|
2022-05-15 12:59:23 +00:00
|
|
|
m_pageTreeHandle,
|
|
|
|
m_fontResDictHandle,
|
2023-07-07 16:43:41 +00:00
|
|
|
m_imgResDictHandle,
|
2022-05-15 19:35:00 +00:00
|
|
|
psPaperSize.x,
|
|
|
|
psPaperSize.y,
|
2022-05-15 12:59:23 +00:00
|
|
|
m_pageStreamHandle );
|
2022-05-15 19:35:00 +00:00
|
|
|
|
|
|
|
if( hyperlinkHandles.size() > 0 )
|
|
|
|
fprintf( m_outputFile, "/Annots %d 0 R", hyperLinkArrayHandle );
|
|
|
|
|
|
|
|
fputs( ">>\n", m_outputFile );
|
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
closePdfObject();
|
|
|
|
|
|
|
|
// Mark the page stream as idle
|
2022-05-15 12:59:23 +00:00
|
|
|
m_pageStreamHandle = 0;
|
2022-06-23 22:29:48 +00:00
|
|
|
|
2022-09-25 02:52:09 +00:00
|
|
|
wxString pageOutlineName = wxEmptyString;
|
2022-10-17 13:19:39 +00:00
|
|
|
|
2022-09-25 02:52:09 +00:00
|
|
|
if( m_pageName.IsEmpty() )
|
|
|
|
{
|
|
|
|
pageOutlineName = wxString::Format( _( "Page %s" ), m_pageNumbers.back() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pageOutlineName = wxString::Format( _( "%s (Page %s)" ), m_pageName, m_pageNumbers.back() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-25 03:36:03 +00:00
|
|
|
int actionHandle = emitGoToAction( pageHandle );
|
|
|
|
OUTLINE_NODE* pageOutlineNode =
|
|
|
|
addOutlineNode( m_outlineRoot.get(), actionHandle, pageOutlineName );
|
2022-09-25 01:45:48 +00:00
|
|
|
|
|
|
|
// let's reorg the symbol bookmarks under a page handle
|
2022-09-25 02:35:20 +00:00
|
|
|
// let's reorg the symbol bookmarks under a page handle
|
|
|
|
for( const auto& [groupName, groupVector] : m_bookmarksInPage )
|
2022-09-25 01:45:48 +00:00
|
|
|
{
|
2022-09-25 03:36:03 +00:00
|
|
|
OUTLINE_NODE* groupOutlineNode = addOutlineNode( pageOutlineNode, actionHandle, groupName );
|
2022-09-25 01:45:48 +00:00
|
|
|
|
2022-09-25 02:35:20 +00:00
|
|
|
for( const std::pair<BOX2I, wxString>& bookmarkPair : groupVector )
|
|
|
|
{
|
|
|
|
const BOX2I& box = bookmarkPair.first;
|
|
|
|
const wxString& ref = bookmarkPair.second;
|
2022-09-25 01:45:48 +00:00
|
|
|
|
2022-09-25 02:35:20 +00:00
|
|
|
VECTOR2I bottomLeft = iuToPdfUserSpace( box.GetPosition() );
|
|
|
|
VECTOR2I topRight = iuToPdfUserSpace( box.GetEnd() );
|
2022-09-25 01:45:48 +00:00
|
|
|
|
2022-09-25 12:45:08 +00:00
|
|
|
actionHandle = emitGoToAction( pageHandle, bottomLeft, topRight );
|
2022-09-25 01:45:48 +00:00
|
|
|
|
2022-09-25 12:45:08 +00:00
|
|
|
addOutlineNode( groupOutlineNode, actionHandle, ref );
|
2022-09-25 02:35:20 +00:00
|
|
|
}
|
2022-09-25 01:45:48 +00:00
|
|
|
|
2022-09-25 02:35:20 +00:00
|
|
|
std::sort( groupOutlineNode->children.begin(), groupOutlineNode->children.end(),
|
|
|
|
[]( const OUTLINE_NODE* a, const OUTLINE_NODE* b ) -> bool
|
|
|
|
{
|
|
|
|
return a->title < b->title;
|
|
|
|
} );
|
|
|
|
}
|
2022-09-25 01:45:48 +00:00
|
|
|
|
2022-07-03 15:19:55 +00:00
|
|
|
// Clean up
|
|
|
|
m_hyperlinksInPage.clear();
|
2022-08-27 22:28:31 +00:00
|
|
|
m_hyperlinkMenusInPage.clear();
|
2022-09-25 02:35:20 +00:00
|
|
|
m_bookmarksInPage.clear();
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2022-09-25 02:52:09 +00:00
|
|
|
bool PDF_PLOTTER::StartPlot( const wxString& aPageNumber )
|
|
|
|
{
|
|
|
|
return StartPlot( aPageNumber, wxEmptyString );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-17 13:19:39 +00:00
|
|
|
bool PDF_PLOTTER::StartPlot( const wxString& aPageNumber, const wxString& aPageName )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-10-17 13:19:39 +00:00
|
|
|
wxASSERT( m_outputFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
// First things first: the customary null object
|
2022-05-15 12:59:23 +00:00
|
|
|
m_xrefTable.clear();
|
|
|
|
m_xrefTable.push_back( 0 );
|
2022-07-03 15:19:55 +00:00
|
|
|
m_hyperlinksInPage.clear();
|
2022-08-27 22:28:31 +00:00
|
|
|
m_hyperlinkMenusInPage.clear();
|
2022-07-03 15:19:55 +00:00
|
|
|
m_hyperlinkHandles.clear();
|
2022-08-27 22:28:31 +00:00
|
|
|
m_hyperlinkMenuHandles.clear();
|
2022-09-25 02:35:20 +00:00
|
|
|
m_bookmarksInPage.clear();
|
2022-09-25 01:45:48 +00:00
|
|
|
m_totalOutlineNodes = 0;
|
|
|
|
|
|
|
|
m_outlineRoot = std::make_unique<OUTLINE_NODE>();
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
/* The header (that's easy!). The second line is binary junk required
|
|
|
|
to make the file binary from the beginning (the important thing is
|
|
|
|
that they must have the bit 7 set) */
|
2022-09-25 01:45:48 +00:00
|
|
|
fputs("%PDF-1.5\n%\200\201\202\203\n", m_outputFile);
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2021-08-17 17:44:11 +00:00
|
|
|
/* Allocate an entry for the page tree root, it will go in every page parent entry */
|
2022-05-15 12:59:23 +00:00
|
|
|
m_pageTreeHandle = allocPdfObject();
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
/* In the same way, the font resource dictionary is used by every page
|
|
|
|
(it *could* be inherited via the Pages tree */
|
2022-05-15 12:59:23 +00:00
|
|
|
m_fontResDictHandle = allocPdfObject();
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2023-07-07 16:43:41 +00:00
|
|
|
m_imgResDictHandle = allocPdfObject();
|
|
|
|
|
2023-07-04 05:57:26 +00:00
|
|
|
m_jsNamesHandle = allocPdfObject();
|
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
/* Now, the PDF is read from the end, (more or less)... so we start
|
|
|
|
with the page stream for page 1. Other more important stuff is written
|
|
|
|
at the end */
|
2022-10-17 13:19:39 +00:00
|
|
|
StartPage( aPageNumber, aPageName );
|
2012-05-03 19:08:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-25 01:45:48 +00:00
|
|
|
int PDF_PLOTTER::emitGoToAction( int aPageHandle, const VECTOR2I& aBottomLeft,
|
|
|
|
const VECTOR2I& aTopRight )
|
|
|
|
{
|
|
|
|
int actionHandle = allocPdfObject();
|
|
|
|
startPdfObject( actionHandle );
|
|
|
|
|
|
|
|
fprintf( m_outputFile,
|
|
|
|
"<</S /GoTo /D [%d 0 R /FitR %d %d %d %d]\n"
|
|
|
|
">>\n",
|
|
|
|
aPageHandle, aBottomLeft.x, aBottomLeft.y, aTopRight.x, aTopRight.y );
|
|
|
|
|
|
|
|
closePdfObject();
|
|
|
|
|
|
|
|
return actionHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-25 03:36:03 +00:00
|
|
|
int PDF_PLOTTER::emitGoToAction( int aPageHandle )
|
|
|
|
{
|
|
|
|
int actionHandle = allocPdfObject();
|
|
|
|
startPdfObject( actionHandle );
|
|
|
|
|
|
|
|
fprintf( m_outputFile,
|
|
|
|
"<</S /GoTo /D [%d 0 R /Fit]\n"
|
|
|
|
">>\n",
|
|
|
|
aPageHandle );
|
|
|
|
|
|
|
|
closePdfObject();
|
|
|
|
|
|
|
|
return actionHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-25 01:45:48 +00:00
|
|
|
void PDF_PLOTTER::emitOutlineNode( OUTLINE_NODE* node, int parentHandle, int nextNode,
|
|
|
|
int prevNode )
|
|
|
|
{
|
|
|
|
int nodeHandle = node->entryHandle;
|
|
|
|
int prevHandle = -1;
|
|
|
|
int nextHandle = -1;
|
2022-10-17 13:19:39 +00:00
|
|
|
|
2022-09-25 01:45:48 +00:00
|
|
|
for( std::vector<OUTLINE_NODE*>::iterator it = node->children.begin();
|
|
|
|
it != node->children.end(); it++ )
|
|
|
|
{
|
|
|
|
if( it >= node->children.end() - 1 )
|
|
|
|
{
|
|
|
|
nextHandle = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextHandle = ( *( it + 1 ) )->entryHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
emitOutlineNode( *it, nodeHandle, nextHandle, prevHandle );
|
|
|
|
|
|
|
|
prevHandle = ( *it )->entryHandle;
|
|
|
|
}
|
|
|
|
|
2022-10-17 13:19:39 +00:00
|
|
|
// -1 for parentHandle is the outline root itself which is handed elsewhere.
|
|
|
|
if( parentHandle != -1 )
|
2022-09-25 01:45:48 +00:00
|
|
|
{
|
|
|
|
startPdfObject( nodeHandle );
|
|
|
|
|
|
|
|
fprintf( m_outputFile,
|
2023-07-04 05:57:26 +00:00
|
|
|
"<<\n"
|
|
|
|
"/Title %s\n"
|
|
|
|
"/Parent %d 0 R\n",
|
2022-09-25 01:45:48 +00:00
|
|
|
encodeStringForPlotter(node->title ).c_str(),
|
|
|
|
parentHandle);
|
|
|
|
|
|
|
|
if( nextNode > 0 )
|
|
|
|
{
|
2023-07-04 05:57:26 +00:00
|
|
|
fprintf( m_outputFile, "/Next %d 0 R\n", nextNode );
|
2022-09-25 01:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( prevNode > 0 )
|
|
|
|
{
|
2023-07-04 05:57:26 +00:00
|
|
|
fprintf( m_outputFile, "/Prev %d 0 R\n", prevNode );
|
2022-09-25 01:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( node->children.size() > 0 )
|
|
|
|
{
|
2023-07-04 05:57:26 +00:00
|
|
|
fprintf( m_outputFile, "/Count %zd\n", -1 * node->children.size() );
|
|
|
|
fprintf( m_outputFile, "/First %d 0 R\n", node->children.front()->entryHandle );
|
|
|
|
fprintf( m_outputFile, "/Last %d 0 R\n", node->children.back()->entryHandle );
|
2022-09-25 01:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( node->actionHandle != -1 )
|
|
|
|
{
|
2023-07-04 05:57:26 +00:00
|
|
|
fprintf( m_outputFile, "/A %d 0 R\n", node->actionHandle );
|
2022-09-25 01:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fputs( ">>\n", m_outputFile );
|
|
|
|
closePdfObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PDF_PLOTTER::OUTLINE_NODE* PDF_PLOTTER::addOutlineNode( OUTLINE_NODE* aParent, int aActionHandle,
|
|
|
|
const wxString& aTitle )
|
|
|
|
{
|
|
|
|
OUTLINE_NODE *node = aParent->AddChild( aActionHandle, aTitle, allocPdfObject() );
|
|
|
|
m_totalOutlineNodes++;
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int PDF_PLOTTER::emitOutline()
|
|
|
|
{
|
|
|
|
if( m_outlineRoot->children.size() > 0 )
|
|
|
|
{
|
|
|
|
// declare the outline object
|
|
|
|
m_outlineRoot->entryHandle = allocPdfObject();
|
|
|
|
|
|
|
|
emitOutlineNode( m_outlineRoot.get(), -1, -1, -1 );
|
|
|
|
|
|
|
|
startPdfObject( m_outlineRoot->entryHandle );
|
|
|
|
|
|
|
|
fprintf( m_outputFile,
|
|
|
|
"<< /Type /Outlines\n"
|
|
|
|
" /Count %d\n"
|
|
|
|
" /First %d 0 R\n"
|
|
|
|
" /Last %d 0 R\n"
|
|
|
|
">>\n",
|
|
|
|
m_totalOutlineNodes,
|
|
|
|
m_outlineRoot->children.front()->entryHandle,
|
|
|
|
m_outlineRoot->children.back()->entryHandle
|
|
|
|
);
|
|
|
|
|
|
|
|
closePdfObject();
|
|
|
|
|
|
|
|
return m_outlineRoot->entryHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-10-17 13:19:39 +00:00
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
bool PDF_PLOTTER::EndPlot()
|
|
|
|
{
|
2020-11-16 00:04:55 +00:00
|
|
|
wxASSERT( m_outputFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
// Close the current page (often the only one)
|
|
|
|
ClosePage();
|
|
|
|
|
|
|
|
/* We need to declare the resources we're using (fonts in particular)
|
|
|
|
The useful standard one is the Helvetica family. Adding external fonts
|
|
|
|
is *very* involved! */
|
|
|
|
struct {
|
|
|
|
const char *psname;
|
|
|
|
const char *rsname;
|
|
|
|
int font_handle;
|
|
|
|
} fontdefs[4] = {
|
|
|
|
{ "/Helvetica", "/KicadFont", 0 },
|
|
|
|
{ "/Helvetica-Oblique", "/KicadFontI", 0 },
|
|
|
|
{ "/Helvetica-Bold", "/KicadFontB", 0 },
|
|
|
|
{ "/Helvetica-BoldOblique", "/KicadFontBI", 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Declare the font resources. Since they're builtin fonts, no descriptors (yay!)
|
2019-08-20 17:22:30 +00:00
|
|
|
We'll need metrics anyway to do any alignment (these are in the shared with
|
2012-05-03 19:08:14 +00:00
|
|
|
the postscript engine) */
|
|
|
|
for( int i = 0; i < 4; i++ )
|
|
|
|
{
|
|
|
|
fontdefs[i].font_handle = startPdfObject();
|
2020-11-16 00:04:55 +00:00
|
|
|
fprintf( m_outputFile,
|
2012-05-03 19:08:14 +00:00
|
|
|
"<< /BaseFont %s\n"
|
|
|
|
" /Type /Font\n"
|
|
|
|
" /Subtype /Type1\n"
|
|
|
|
/* Adobe is so Mac-based that the nearest thing to Latin1 is
|
|
|
|
the Windows ANSI encoding! */
|
|
|
|
" /Encoding /WinAnsiEncoding\n"
|
|
|
|
">>\n",
|
|
|
|
fontdefs[i].psname );
|
|
|
|
closePdfObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Named font dictionary (was allocated, now we emit it)
|
2022-05-15 12:59:23 +00:00
|
|
|
startPdfObject( m_fontResDictHandle );
|
2020-11-16 00:04:55 +00:00
|
|
|
fputs( "<<\n", m_outputFile );
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
for( int i = 0; i < 4; i++ )
|
|
|
|
{
|
2020-11-16 00:04:55 +00:00
|
|
|
fprintf( m_outputFile, " %s %d 0 R\n",
|
|
|
|
fontdefs[i].rsname, fontdefs[i].font_handle );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
fputs( ">>\n", m_outputFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
closePdfObject();
|
|
|
|
|
2023-07-07 16:43:41 +00:00
|
|
|
// Named image dictionary (was allocated, now we emit it)
|
|
|
|
startPdfObject( m_imgResDictHandle );
|
|
|
|
fputs( "<<\n", m_outputFile );
|
|
|
|
|
|
|
|
for( const auto& [imgHandle, image] : m_imageHandles )
|
|
|
|
{
|
|
|
|
fprintf( m_outputFile, " /Im%d %d 0 R\n", imgHandle, imgHandle );
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs( ">>\n", m_outputFile );
|
|
|
|
closePdfObject();
|
|
|
|
|
|
|
|
// Emit images with optional SMask for transparency
|
|
|
|
for( const auto& [imgHandle, image] : m_imageHandles )
|
|
|
|
{
|
|
|
|
// Init wxFFile so wxFFileOutputStream won't close file in dtor.
|
|
|
|
wxFFile outputFFile( m_outputFile );
|
|
|
|
|
|
|
|
// Image
|
|
|
|
startPdfObject( imgHandle );
|
|
|
|
int imgLenHandle = allocPdfObject();
|
|
|
|
int smaskHandle = ( image.HasAlpha() || image.HasMask() ) ? allocPdfObject() : -1;
|
|
|
|
|
|
|
|
fprintf( m_outputFile,
|
|
|
|
"<<\n"
|
|
|
|
"/Type /XObject\n"
|
|
|
|
"/Subtype /Image\n"
|
|
|
|
"/BitsPerComponent 8\n"
|
|
|
|
"/ColorSpace %s\n"
|
|
|
|
"/Width %d\n"
|
|
|
|
"/Height %d\n"
|
|
|
|
"/Filter /FlateDecode\n"
|
|
|
|
"/Length %d 0 R\n", // Length is deferred
|
|
|
|
m_colorMode ? "/DeviceRGB" : "/DeviceGray", image.GetWidth(), image.GetHeight(),
|
|
|
|
imgLenHandle );
|
|
|
|
|
|
|
|
if( smaskHandle != -1 )
|
|
|
|
fprintf( m_outputFile, "/SMask %d 0 R\n", smaskHandle );
|
|
|
|
|
|
|
|
fputs( ">>\n", m_outputFile );
|
|
|
|
fputs( "stream\n", m_outputFile );
|
|
|
|
|
|
|
|
long imgStreamStart = ftell( m_outputFile );
|
|
|
|
|
|
|
|
{
|
|
|
|
wxFFileOutputStream ffos( outputFFile );
|
|
|
|
wxZlibOutputStream zos( ffos, wxZ_BEST_COMPRESSION, wxZLIB_ZLIB );
|
|
|
|
wxDataOutputStream dos( zos );
|
|
|
|
|
|
|
|
WriteImageStream( image, dos, m_renderSettings->GetBackgroundColor().ToColour(),
|
|
|
|
m_colorMode );
|
|
|
|
}
|
|
|
|
|
|
|
|
long imgStreamSize = ftell( m_outputFile ) - imgStreamStart;
|
|
|
|
|
|
|
|
fputs( "\nendstream\n", m_outputFile );
|
|
|
|
closePdfObject();
|
|
|
|
|
|
|
|
startPdfObject( imgLenHandle );
|
|
|
|
fprintf( m_outputFile, "%ld\n", imgStreamSize );
|
|
|
|
closePdfObject();
|
|
|
|
|
|
|
|
if( smaskHandle != -1 )
|
|
|
|
{
|
|
|
|
// SMask
|
|
|
|
startPdfObject( smaskHandle );
|
|
|
|
int smaskLenHandle = allocPdfObject();
|
|
|
|
|
|
|
|
fprintf( m_outputFile,
|
|
|
|
"<<\n"
|
|
|
|
"/Type /XObject\n"
|
|
|
|
"/Subtype /Image\n"
|
|
|
|
"/BitsPerComponent 8\n"
|
|
|
|
"/ColorSpace /DeviceGray\n"
|
|
|
|
"/Width %d\n"
|
|
|
|
"/Height %d\n"
|
|
|
|
"/Length %d 0 R\n"
|
|
|
|
"/Filter /FlateDecode\n"
|
|
|
|
">>\n", // Length is deferred
|
|
|
|
image.GetWidth(), image.GetHeight(), smaskLenHandle );
|
|
|
|
|
|
|
|
fputs( "stream\n", m_outputFile );
|
|
|
|
|
|
|
|
long smaskStreamStart = ftell( m_outputFile );
|
|
|
|
|
|
|
|
{
|
|
|
|
wxFFileOutputStream ffos( outputFFile );
|
|
|
|
wxZlibOutputStream zos( ffos, wxZ_BEST_COMPRESSION, wxZLIB_ZLIB );
|
|
|
|
wxDataOutputStream dos( zos );
|
|
|
|
|
|
|
|
WriteImageSMaskStream( image, dos );
|
|
|
|
}
|
|
|
|
|
|
|
|
long smaskStreamSize = ftell( m_outputFile ) - smaskStreamStart;
|
|
|
|
|
|
|
|
fputs( "\nendstream\n", m_outputFile );
|
|
|
|
closePdfObject();
|
|
|
|
|
|
|
|
startPdfObject( smaskLenHandle );
|
|
|
|
fprintf( m_outputFile, "%u\n", (unsigned) smaskStreamSize );
|
|
|
|
closePdfObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
outputFFile.Detach(); // Don't close it
|
|
|
|
}
|
|
|
|
|
2022-08-27 22:28:31 +00:00
|
|
|
for( const auto& [ linkHandle, linkPair ] : m_hyperlinkHandles )
|
2022-06-23 22:29:48 +00:00
|
|
|
{
|
2022-08-27 22:28:31 +00:00
|
|
|
const BOX2D& box = linkPair.first;
|
|
|
|
const wxString& url = linkPair.second;
|
2022-06-23 22:29:48 +00:00
|
|
|
|
2022-08-27 22:28:31 +00:00
|
|
|
startPdfObject( linkHandle );
|
2022-06-23 22:29:48 +00:00
|
|
|
|
|
|
|
fprintf( m_outputFile,
|
2023-07-04 05:57:26 +00:00
|
|
|
"<<\n"
|
|
|
|
"/Type /Annot\n"
|
|
|
|
"/Subtype /Link\n"
|
|
|
|
"/Rect [%g %g %g %g]\n"
|
|
|
|
"/Border [16 16 0]\n",
|
2022-07-03 15:19:55 +00:00
|
|
|
box.GetLeft(), box.GetBottom(), box.GetRight(), box.GetTop() );
|
|
|
|
|
2022-08-27 18:14:57 +00:00
|
|
|
wxString pageNumber;
|
|
|
|
bool pageFound = false;
|
2022-07-03 15:19:55 +00:00
|
|
|
|
2022-08-27 18:14:57 +00:00
|
|
|
if( EDA_TEXT::IsGotoPageHref( url, &pageNumber ) )
|
2022-07-03 15:19:55 +00:00
|
|
|
{
|
2022-08-27 18:14:57 +00:00
|
|
|
for( size_t ii = 0; ii < m_pageNumbers.size(); ++ii )
|
2022-07-03 15:19:55 +00:00
|
|
|
{
|
2022-08-27 18:14:57 +00:00
|
|
|
if( m_pageNumbers[ii] == pageNumber )
|
|
|
|
{
|
|
|
|
fprintf( m_outputFile,
|
2023-07-04 05:57:26 +00:00
|
|
|
"/Dest [%d 0 R /FitB]\n"
|
2022-08-27 18:14:57 +00:00
|
|
|
">>\n",
|
|
|
|
m_pageHandles[ii] );
|
|
|
|
|
|
|
|
pageFound = true;
|
|
|
|
break;
|
|
|
|
}
|
2022-07-03 15:19:55 +00:00
|
|
|
}
|
2022-08-27 18:14:57 +00:00
|
|
|
|
|
|
|
if( !pageFound )
|
2022-07-03 15:19:55 +00:00
|
|
|
{
|
|
|
|
// destination page is not being plotted, assign the NOP action to the link
|
2023-07-04 05:57:26 +00:00
|
|
|
fprintf( m_outputFile, "/A << /Type /Action /S /NOP >>\n"
|
|
|
|
">>\n" );
|
2022-07-03 15:19:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( m_outputFile,
|
2023-07-04 05:57:26 +00:00
|
|
|
"/A << /Type /Action /S /URI /URI %s >>\n"
|
2022-07-03 15:19:55 +00:00
|
|
|
">>\n",
|
|
|
|
encodeStringForPlotter( url ).c_str() );
|
|
|
|
}
|
|
|
|
|
2022-06-23 22:29:48 +00:00
|
|
|
closePdfObject();
|
|
|
|
}
|
|
|
|
|
2022-08-27 22:28:31 +00:00
|
|
|
for( const auto& [ menuHandle, menuPair ] : m_hyperlinkMenuHandles )
|
|
|
|
{
|
|
|
|
const BOX2D& box = menuPair.first;
|
|
|
|
const std::vector<wxString>& urls = menuPair.second;
|
2023-07-04 05:57:26 +00:00
|
|
|
wxString js = wxT( "ShM([\n" );
|
2022-08-27 22:28:31 +00:00
|
|
|
|
|
|
|
for( const wxString& url : urls )
|
|
|
|
{
|
2022-09-05 22:28:44 +00:00
|
|
|
if( url.StartsWith( "!" ) )
|
|
|
|
{
|
2022-09-06 10:18:09 +00:00
|
|
|
wxString property = url.AfterFirst( '!' );
|
|
|
|
|
|
|
|
if( property.Find( "http:" ) >= 0 )
|
|
|
|
{
|
|
|
|
wxString href = property.substr( property.Find( "http:" ) );
|
|
|
|
|
2023-07-04 05:57:26 +00:00
|
|
|
js += wxString::Format( wxT( "[\"%s\", \"%s\"],\n" ),
|
2022-09-06 10:18:09 +00:00
|
|
|
EscapeString( property, CTX_JS_STR ),
|
|
|
|
EscapeString( href, CTX_JS_STR ) );
|
|
|
|
}
|
|
|
|
else if( property.Find( "https:" ) >= 0 )
|
|
|
|
{
|
|
|
|
wxString href = property.substr( property.Find( "https:" ) );
|
|
|
|
|
2023-07-04 05:57:26 +00:00
|
|
|
js += wxString::Format( wxT( "[\"%s\", \"%s\"],\n" ),
|
2022-09-06 10:18:09 +00:00
|
|
|
EscapeString( property, CTX_JS_STR ),
|
|
|
|
EscapeString( href, CTX_JS_STR ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-04 05:57:26 +00:00
|
|
|
js += wxString::Format( wxT( "[\"%s\"],\n" ),
|
2022-09-06 10:18:09 +00:00
|
|
|
EscapeString( property, CTX_JS_STR ) );
|
|
|
|
}
|
2022-09-05 22:28:44 +00:00
|
|
|
}
|
2022-09-06 10:18:09 +00:00
|
|
|
else if( url.StartsWith( "#" ) )
|
2022-08-27 22:28:31 +00:00
|
|
|
{
|
2022-09-06 10:18:09 +00:00
|
|
|
wxString pageNumber = url.AfterFirst( '#' );
|
|
|
|
|
2022-08-27 22:28:31 +00:00
|
|
|
for( size_t ii = 0; ii < m_pageNumbers.size(); ++ii )
|
|
|
|
{
|
|
|
|
if( m_pageNumbers[ii] == pageNumber )
|
2022-09-06 10:18:09 +00:00
|
|
|
{
|
|
|
|
wxString menuText = wxString::Format( _( "Show Page %s" ), pageNumber );
|
|
|
|
|
2023-07-04 05:57:26 +00:00
|
|
|
js += wxString::Format( wxT( "[\"%s\", \"#%d\"],\n" ),
|
2022-09-06 10:18:09 +00:00
|
|
|
EscapeString( menuText, CTX_JS_STR ),
|
|
|
|
static_cast<int>( ii ) );
|
|
|
|
break;
|
|
|
|
}
|
2022-08-27 22:28:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-06 10:18:09 +00:00
|
|
|
else if( url.StartsWith( "http:" ) || url.StartsWith( "https:" ) )
|
2022-09-05 22:28:44 +00:00
|
|
|
{
|
2022-09-06 10:18:09 +00:00
|
|
|
wxString menuText = wxString::Format( _( "Open %s" ), url );
|
2022-09-05 22:28:44 +00:00
|
|
|
|
2023-07-04 05:57:26 +00:00
|
|
|
js += wxString::Format( wxT( "[\"%s\", \"%s\"],\n" ),
|
2022-09-05 22:28:44 +00:00
|
|
|
EscapeString( menuText, CTX_JS_STR ),
|
2022-09-06 10:18:09 +00:00
|
|
|
EscapeString( url, CTX_JS_STR ) );
|
2022-09-05 22:28:44 +00:00
|
|
|
}
|
2022-08-27 22:28:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 05:57:26 +00:00
|
|
|
js += wxT( "]);" );
|
2022-08-27 22:28:31 +00:00
|
|
|
|
|
|
|
startPdfObject( menuHandle );
|
|
|
|
|
|
|
|
fprintf( m_outputFile,
|
2023-07-04 05:57:26 +00:00
|
|
|
"<<\n"
|
|
|
|
"/Type /Annot\n"
|
|
|
|
"/Subtype /Link\n"
|
|
|
|
"/Rect [%g %g %g %g]\n"
|
|
|
|
"/Border [16 16 0]\n",
|
2022-08-27 22:28:31 +00:00
|
|
|
box.GetLeft(), box.GetBottom(), box.GetRight(), box.GetTop() );
|
|
|
|
|
|
|
|
fprintf( m_outputFile,
|
2023-07-04 05:57:26 +00:00
|
|
|
"/A << /Type /Action /S /JavaScript /JS %s >>\n"
|
|
|
|
">>\n",
|
|
|
|
encodeStringForPlotter( js ).c_str() );
|
|
|
|
|
|
|
|
closePdfObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
startPdfObject( m_jsNamesHandle );
|
|
|
|
|
|
|
|
wxString js = R"JS(
|
|
|
|
function ShM(aEntries) {
|
|
|
|
var aParams = [];
|
|
|
|
for (var i in aEntries) {
|
|
|
|
aParams.push({
|
|
|
|
cName: aEntries[i][0],
|
|
|
|
cReturn: aEntries[i][1]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var cChoice = app.popUpMenuEx.apply(app, aParams);
|
|
|
|
if (cChoice != null && cChoice.substring(0, 1) == '#') this.pageNum = parseInt(cChoice.slice(1));
|
|
|
|
else if (cChoice != null && cChoice.substring(0, 4) == 'http') app.launchURL(cChoice);
|
|
|
|
}
|
|
|
|
)JS";
|
|
|
|
|
|
|
|
fprintf( m_outputFile,
|
|
|
|
"<< /JavaScript\n"
|
|
|
|
" << /Names\n"
|
|
|
|
" [ (JSInit) << /Type /Action /S /JavaScript /JS %s >> ]\n"
|
|
|
|
" >>\n"
|
2022-08-27 22:28:31 +00:00
|
|
|
">>\n",
|
2023-07-04 05:57:26 +00:00
|
|
|
encodeStringForPlotter( js ).c_str() );
|
2022-08-27 22:28:31 +00:00
|
|
|
|
|
|
|
closePdfObject();
|
|
|
|
}
|
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
/* The page tree: it's a B-tree but luckily we only have few pages!
|
|
|
|
So we use just an array... The handle was allocated at the beginning,
|
|
|
|
now we instantiate the corresponding object */
|
2022-05-15 12:59:23 +00:00
|
|
|
startPdfObject( m_pageTreeHandle );
|
2012-05-03 19:08:14 +00:00
|
|
|
fputs( "<<\n"
|
|
|
|
"/Type /Pages\n"
|
2020-11-16 00:04:55 +00:00
|
|
|
"/Kids [\n", m_outputFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2022-05-15 12:59:23 +00:00
|
|
|
for( unsigned i = 0; i < m_pageHandles.size(); i++ )
|
|
|
|
fprintf( m_outputFile, "%d 0 R\n", m_pageHandles[i] );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
fprintf( m_outputFile,
|
2012-05-03 19:08:14 +00:00
|
|
|
"]\n"
|
|
|
|
"/Count %ld\n"
|
2022-10-17 13:19:39 +00:00
|
|
|
">>\n", (long) m_pageHandles.size() );
|
2012-05-03 19:08:14 +00:00
|
|
|
closePdfObject();
|
|
|
|
|
2022-09-25 01:45:48 +00:00
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
// The info dictionary
|
|
|
|
int infoDictHandle = startPdfObject();
|
|
|
|
char date_buf[250];
|
2021-07-15 19:26:35 +00:00
|
|
|
time_t ltime = time( nullptr );
|
2021-06-07 18:31:53 +00:00
|
|
|
strftime( date_buf, 250, "D:%Y%m%d%H%M%S", localtime( <ime ) );
|
2017-01-20 21:00:20 +00:00
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
if( m_title.IsEmpty() )
|
2017-01-20 21:00:20 +00:00
|
|
|
{
|
2021-06-09 19:32:58 +00:00
|
|
|
// Windows uses '\' and other platforms use '/' as separator
|
2022-10-17 13:19:39 +00:00
|
|
|
m_title = m_filename.AfterLast( '\\' );
|
|
|
|
m_title = m_title.AfterLast( '/' );
|
2017-01-20 21:00:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
fprintf( m_outputFile,
|
2012-05-03 19:08:14 +00:00
|
|
|
"<<\n"
|
2020-07-26 13:57:07 +00:00
|
|
|
"/Producer (KiCad PDF)\n"
|
2012-05-03 19:08:14 +00:00
|
|
|
"/CreationDate (%s)\n"
|
2020-07-26 13:57:07 +00:00
|
|
|
"/Creator %s\n"
|
2021-04-06 09:28:56 +00:00
|
|
|
"/Title %s\n",
|
2012-05-03 19:08:14 +00:00
|
|
|
date_buf,
|
2020-11-16 00:04:55 +00:00
|
|
|
encodeStringForPlotter( m_creator ).c_str(),
|
|
|
|
encodeStringForPlotter( m_title ).c_str() );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
fputs( ">>\n", m_outputFile );
|
2012-05-03 19:08:14 +00:00
|
|
|
closePdfObject();
|
|
|
|
|
2022-09-25 01:45:48 +00:00
|
|
|
// Let's dump in the outline
|
|
|
|
int outlineHandle = emitOutline();
|
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
// The catalog, at last
|
|
|
|
int catalogHandle = startPdfObject();
|
2022-10-17 13:19:39 +00:00
|
|
|
|
2022-09-25 01:45:48 +00:00
|
|
|
if( outlineHandle > 0 )
|
|
|
|
{
|
|
|
|
fprintf( m_outputFile,
|
|
|
|
"<<\n"
|
|
|
|
"/Type /Catalog\n"
|
|
|
|
"/Pages %d 0 R\n"
|
|
|
|
"/Version /1.5\n"
|
|
|
|
"/PageMode /UseOutlines\n"
|
|
|
|
"/Outlines %d 0 R\n"
|
2023-07-04 05:57:26 +00:00
|
|
|
"/Names %d 0 R\n"
|
2022-09-25 01:45:48 +00:00
|
|
|
"/PageLayout /SinglePage\n"
|
|
|
|
">>\n",
|
|
|
|
m_pageTreeHandle,
|
2023-07-04 05:57:26 +00:00
|
|
|
outlineHandle,
|
|
|
|
m_jsNamesHandle );
|
2022-09-25 01:45:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( m_outputFile,
|
|
|
|
"<<\n"
|
|
|
|
"/Type /Catalog\n"
|
|
|
|
"/Pages %d 0 R\n"
|
|
|
|
"/Version /1.5\n"
|
|
|
|
"/PageMode /UseNone\n"
|
|
|
|
"/PageLayout /SinglePage\n"
|
|
|
|
">>\n",
|
|
|
|
m_pageTreeHandle );
|
|
|
|
}
|
2022-10-17 13:19:39 +00:00
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
closePdfObject();
|
|
|
|
|
|
|
|
/* Emit the xref table (format is crucial to the byte, each entry must
|
|
|
|
be 20 bytes long, and object zero must be done in that way). Also
|
|
|
|
the offset must be kept along for the trailer */
|
2020-11-16 00:04:55 +00:00
|
|
|
long xref_start = ftell( m_outputFile );
|
|
|
|
fprintf( m_outputFile,
|
2012-05-03 19:08:14 +00:00
|
|
|
"xref\n"
|
|
|
|
"0 %ld\n"
|
2022-05-15 12:59:23 +00:00
|
|
|
"0000000000 65535 f \n", (long) m_xrefTable.size() );
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2022-05-15 12:59:23 +00:00
|
|
|
for( unsigned i = 1; i < m_xrefTable.size(); i++ )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2022-05-15 12:59:23 +00:00
|
|
|
fprintf( m_outputFile, "%010ld 00000 n \n", m_xrefTable[i] );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Done the xref, go for the trailer
|
2020-11-16 00:04:55 +00:00
|
|
|
fprintf( m_outputFile,
|
2012-05-03 19:08:14 +00:00
|
|
|
"trailer\n"
|
|
|
|
"<< /Size %lu /Root %d 0 R /Info %d 0 R >>\n"
|
|
|
|
"startxref\n"
|
|
|
|
"%ld\n" // The offset we saved before
|
2014-06-20 08:55:30 +00:00
|
|
|
"%%%%EOF\n",
|
2022-05-15 12:59:23 +00:00
|
|
|
(unsigned long) m_xrefTable.size(), catalogHandle, infoDictHandle, xref_start );
|
2012-05-03 19:08:14 +00:00
|
|
|
|
2020-11-16 00:04:55 +00:00
|
|
|
fclose( m_outputFile );
|
2021-07-15 19:26:35 +00:00
|
|
|
m_outputFile = nullptr;
|
2012-05-03 19:08:14 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-10 13:50:16 +00:00
|
|
|
|
2023-08-06 19:20:53 +00:00
|
|
|
void PDF_PLOTTER::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,
|
|
|
|
KIFONT::FONT* aFont,
|
|
|
|
const KIFONT::METRICS& aFontMetrics,
|
|
|
|
void* aData )
|
2012-05-03 19:08:14 +00:00
|
|
|
{
|
2015-03-18 19:50:42 +00:00
|
|
|
// PDF files do not like 0 sized texts which create broken files.
|
|
|
|
if( aSize.x == 0 || aSize.y == 0 )
|
|
|
|
return;
|
|
|
|
|
2020-05-18 10:34:30 +00:00
|
|
|
// Render phantom text (which will be searchable) behind the stroke font. This won't
|
|
|
|
// be pixel-accurate, but it doesn't matter for searching.
|
|
|
|
int render_mode = 3; // invisible
|
2017-08-15 13:52:46 +00:00
|
|
|
|
2023-01-19 23:25:07 +00:00
|
|
|
VECTOR2I pos( aPos );
|
2020-05-18 12:37:05 +00:00
|
|
|
const char *fontname = aItalic ? ( aBold ? "/KicadFontBI" : "/KicadFontI" )
|
|
|
|
: ( aBold ? "/KicadFontB" : "/KicadFont" );
|
2017-08-15 13:52:46 +00:00
|
|
|
|
2020-11-10 13:50:16 +00:00
|
|
|
// Compute the copious transformation parameters of the Current Transform Matrix
|
2017-08-15 13:52:46 +00:00
|
|
|
double ctm_a, ctm_b, ctm_c, ctm_d, ctm_e, ctm_f;
|
|
|
|
double wideningFactor, heightFactor;
|
|
|
|
|
2023-02-24 08:44:25 +00:00
|
|
|
VECTOR2I t_size( std::abs( aSize.x ), std::abs( aSize.y ) );
|
|
|
|
|
|
|
|
computeTextParameters( aPos, aText, aOrient, t_size, m_plotMirror, aH_justify,
|
2021-12-28 22:13:54 +00:00
|
|
|
aV_justify, aWidth, aItalic, aBold, &wideningFactor, &ctm_a,
|
|
|
|
&ctm_b, &ctm_c, &ctm_d, &ctm_e, &ctm_f, &heightFactor );
|
2017-08-15 13:52:46 +00:00
|
|
|
|
|
|
|
SetColor( aColor );
|
|
|
|
SetCurrentLineWidth( aWidth, aData );
|
|
|
|
|
2023-01-19 23:25:07 +00:00
|
|
|
wxStringTokenizer str_tok( aText, " ", wxTOKEN_RET_DELIMS );
|
2017-08-15 13:52:46 +00:00
|
|
|
|
2023-01-21 19:11:43 +00:00
|
|
|
// If aFont is not specilied (== nullptr), use the default kicad stroke font
|
|
|
|
if( !aFont )
|
|
|
|
aFont = KIFONT::FONT::GetFont();
|
|
|
|
|
2023-08-06 19:20:53 +00:00
|
|
|
VECTOR2I full_box( aFont->StringBoundaryLimits( aText, t_size, aWidth, aBold, aItalic,
|
|
|
|
aFontMetrics ) );
|
2023-01-19 23:25:07 +00:00
|
|
|
VECTOR2I box_x( full_box.x, 0 );
|
|
|
|
VECTOR2I box_y( 0, full_box.y );
|
2017-08-15 13:52:46 +00:00
|
|
|
|
2023-01-19 23:25:07 +00:00
|
|
|
RotatePoint( box_x, aOrient );
|
|
|
|
RotatePoint( box_y, aOrient );
|
|
|
|
|
|
|
|
if( aH_justify == GR_TEXT_H_ALIGN_CENTER )
|
|
|
|
pos -= box_x / 2;
|
|
|
|
else if( aH_justify == GR_TEXT_H_ALIGN_RIGHT )
|
|
|
|
pos -= box_x;
|
|
|
|
|
|
|
|
if( aV_justify == GR_TEXT_V_ALIGN_CENTER )
|
|
|
|
pos += box_y / 2;
|
|
|
|
else if( aV_justify == GR_TEXT_V_ALIGN_TOP )
|
|
|
|
pos += box_y;
|
|
|
|
|
|
|
|
while( str_tok.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
wxString word = str_tok.GetNextToken();
|
|
|
|
|
2023-02-24 08:44:25 +00:00
|
|
|
computeTextParameters( pos, word, aOrient, t_size, m_plotMirror, GR_TEXT_H_ALIGN_LEFT,
|
2023-01-19 23:25:07 +00:00
|
|
|
GR_TEXT_V_ALIGN_BOTTOM, aWidth, aItalic, aBold, &wideningFactor, &ctm_a,
|
|
|
|
&ctm_b, &ctm_c, &ctm_d, &ctm_e, &ctm_f, &heightFactor );
|
|
|
|
|
|
|
|
// Extract the changed width and rotate by the orientation to get the offset for the
|
|
|
|
// next word
|
2023-08-06 19:20:53 +00:00
|
|
|
VECTOR2I bbox( aFont->StringBoundaryLimits( word, t_size, aWidth,
|
|
|
|
aBold, aItalic, aFontMetrics ).x, 0 );
|
2023-01-19 23:25:07 +00:00
|
|
|
RotatePoint( bbox, aOrient );
|
|
|
|
pos += bbox;
|
|
|
|
|
|
|
|
// Don't try to output a blank string
|
|
|
|
if( word.Trim( false ).Trim( true ).empty() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We use the full CTM instead of the text matrix because the same
|
|
|
|
coordinate system will be used for the overlining. Also the %f
|
|
|
|
for the trig part of the matrix to avoid %g going in exponential
|
|
|
|
format (which is not supported) */
|
|
|
|
fprintf( m_workFile, "q %f %f %f %f %g %g cm BT %s %g Tf %d Tr %g Tz ",
|
|
|
|
ctm_a, ctm_b, ctm_c, ctm_d, ctm_e, ctm_f,
|
|
|
|
fontname, heightFactor, render_mode, wideningFactor * 100 );
|
|
|
|
|
|
|
|
std::string txt_pdf = encodeStringForPlotter( word );
|
|
|
|
fprintf( m_workFile, "%s Tj ET\n", txt_pdf.c_str() );
|
|
|
|
// Restore the CTM
|
|
|
|
fputs( "Q\n", m_workFile );
|
|
|
|
}
|
2017-08-15 13:52:46 +00:00
|
|
|
|
2012-05-03 19:08:14 +00:00
|
|
|
// Plot the stroked text (if requested)
|
2021-12-29 17:31:40 +00:00
|
|
|
PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, aWidth, aItalic,
|
2023-08-06 19:20:53 +00:00
|
|
|
aBold, aMultilineAllowed, aFont, aFontMetrics );
|
2012-05-03 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 19:35:00 +00:00
|
|
|
|
2023-08-06 19:20:53 +00:00
|
|
|
void PDF_PLOTTER::PlotText( const VECTOR2I& aPos,
|
|
|
|
const COLOR4D& aColor,
|
|
|
|
const wxString& aText,
|
|
|
|
const TEXT_ATTRIBUTES& aAttributes,
|
|
|
|
KIFONT::FONT* aFont,
|
|
|
|
const KIFONT::METRICS& aFontMetrics,
|
|
|
|
void* aData )
|
2023-02-24 08:44:25 +00:00
|
|
|
{
|
|
|
|
VECTOR2I size = aAttributes.m_Size;
|
|
|
|
|
|
|
|
// PDF files do not like 0 sized texts which create broken files.
|
|
|
|
if( size.x == 0 || size.y == 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( aAttributes.m_Mirrored )
|
|
|
|
size.x = -size.x;
|
|
|
|
|
2023-08-06 19:20:53 +00:00
|
|
|
PDF_PLOTTER::Text( aPos, aColor, aText, aAttributes.m_Angle, size, aAttributes.m_Halign,
|
|
|
|
aAttributes.m_Valign, aAttributes.m_StrokeWidth, aAttributes.m_Italic,
|
|
|
|
aAttributes.m_Bold, aAttributes.m_Multiline, aFont, aFontMetrics, aData );
|
2023-02-24 08:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-03 15:19:55 +00:00
|
|
|
void PDF_PLOTTER::HyperlinkBox( const BOX2I& aBox, const wxString& aDestinationURL )
|
2022-05-15 19:35:00 +00:00
|
|
|
{
|
2022-07-03 15:19:55 +00:00
|
|
|
m_hyperlinksInPage.push_back( std::make_pair( aBox, aDestinationURL ) );
|
2022-05-15 19:35:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 22:28:31 +00:00
|
|
|
|
|
|
|
void PDF_PLOTTER::HyperlinkMenu( const BOX2I& aBox, const std::vector<wxString>& aDestURLs )
|
|
|
|
{
|
|
|
|
m_hyperlinkMenusInPage.push_back( std::make_pair( aBox, aDestURLs ) );
|
2022-09-25 01:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-17 13:19:39 +00:00
|
|
|
void PDF_PLOTTER::Bookmark( const BOX2I& aLocation, const wxString& aSymbolReference,
|
|
|
|
const wxString &aGroupName )
|
2022-09-25 01:45:48 +00:00
|
|
|
{
|
2022-09-25 02:35:20 +00:00
|
|
|
|
|
|
|
m_bookmarksInPage[aGroupName].push_back( std::make_pair( aLocation, aSymbolReference ) );
|
2022-09-25 01:45:48 +00:00
|
|
|
}
|