2010-06-10 18:43:12 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1992-2010 jean-pierre.charras
|
|
|
|
* Copyright (C) 1992-2010 Kicad Developers, see change_log.txt for contributors.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2011-10-26 09:53:42 +00:00
|
|
|
#include <math.h>
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
// For some unknown reasons, polygon.hpp shoul be included first
|
|
|
|
#include "boost/polygon/polygon.hpp"
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "potracelib.h"
|
|
|
|
#include "auxiliary.h"
|
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
#ifndef max
|
|
|
|
#define max( a, b ) ( ( (a) > (b) ) ? (a) : (b) )
|
2011-10-26 09:53:42 +00:00
|
|
|
#endif
|
|
|
|
#ifndef min
|
2010-06-10 18:43:12 +00:00
|
|
|
#define min( a, b ) ( ( (a) < (b) ) ? (a) : (b) )
|
2011-10-26 09:53:42 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Define some types used here from boost::polygon
|
|
|
|
namespace bpl = boost::polygon; // bpl = boost polygon library
|
|
|
|
using namespace bpl::operators; // +, -, =, ...
|
|
|
|
|
|
|
|
typedef int coordinate_type;
|
|
|
|
|
|
|
|
typedef bpl::polygon_data<coordinate_type> KPolygon; // define a basic polygon
|
|
|
|
typedef std::vector<KPolygon> KPolygonSet; // define a set of polygons
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
typedef bpl::point_data<coordinate_type> KPolyPoint; // define a corner of a polygon
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
enum output_format {
|
|
|
|
POSTSCRIPT_FMT = 1,
|
|
|
|
PCBNEW_FMT,
|
|
|
|
EESCHEMA_FMT
|
|
|
|
};
|
2010-06-15 18:27:41 +00:00
|
|
|
/* free a potrace bitmap */
|
2011-10-26 09:53:42 +00:00
|
|
|
static void bm_free( potrace_bitmap_t* bm )
|
|
|
|
{
|
|
|
|
if( bm != NULL )
|
|
|
|
{
|
|
|
|
free( bm->map );
|
|
|
|
}
|
|
|
|
free( bm );
|
2010-06-15 18:27:41 +00:00
|
|
|
}
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
/* Helper class to handle useful info to convert a bitmap image to
|
2010-06-10 18:43:12 +00:00
|
|
|
* a polygonal object description
|
|
|
|
*/
|
|
|
|
class BITMAPCONV_INFO
|
|
|
|
{
|
|
|
|
public:
|
2011-10-26 11:06:04 +00:00
|
|
|
enum output_format m_Format; // File format
|
2010-06-10 18:43:12 +00:00
|
|
|
int m_PixmapWidth;
|
|
|
|
int m_PixmapHeight; // the bitmap size in pixels
|
|
|
|
double m_ScaleX;
|
|
|
|
double m_ScaleY; // the conversion scale
|
|
|
|
potrace_path_t* m_Paths; // the list of paths, from potrace (list of lines and bezier curves)
|
2011-10-26 11:06:04 +00:00
|
|
|
FILE* m_Outfile; // File to create
|
|
|
|
const char * m_CmpName; // The string used as cmp/footprint name
|
|
|
|
|
|
|
|
public:
|
|
|
|
BITMAPCONV_INFO();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function CreateOutputFile
|
|
|
|
* Creates the output file specified by m_Outfile,
|
|
|
|
* depending on file format given by m_Format
|
|
|
|
*/
|
|
|
|
void CreateOutputFile();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Function OuputFileHeader
|
|
|
|
* write to file the header depending on file format
|
|
|
|
*/
|
|
|
|
void OuputFileHeader();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function OuputFileEnd
|
|
|
|
* write to file the last strings depending on file format
|
|
|
|
*/
|
|
|
|
void OuputFileEnd();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function OuputOnePolygon
|
|
|
|
* write one polygon to output file.
|
|
|
|
* Polygon coordinates are expected scaled by the polugon extraction function
|
|
|
|
*/
|
|
|
|
void OuputOnePolygon( KPolygon & aPolygon );
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void BezierToPolyline( std::vector <potrace_dpoint_t>& aCornersBuffer,
|
|
|
|
potrace_dpoint_t p1,
|
|
|
|
potrace_dpoint_t p2,
|
|
|
|
potrace_dpoint_t p3,
|
|
|
|
potrace_dpoint_t p4 );
|
|
|
|
|
|
|
|
|
|
|
|
BITMAPCONV_INFO::BITMAPCONV_INFO()
|
|
|
|
{
|
|
|
|
m_Format = POSTSCRIPT_FMT;
|
|
|
|
m_PixmapWidth = 0;
|
|
|
|
m_PixmapHeight = 0;
|
|
|
|
m_ScaleX = 1.0;
|
|
|
|
m_ScaleY = 1.0;
|
|
|
|
m_Paths = NULL;
|
|
|
|
m_Outfile = NULL;
|
2011-10-26 11:06:04 +00:00
|
|
|
m_CmpName = "LOGO";
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
int bitmap2component( potrace_bitmap_t* aPotrace_bitmap, FILE* aOutfile, int aFormat )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2011-10-26 09:53:42 +00:00
|
|
|
potrace_param_t* param;
|
|
|
|
potrace_state_t* st;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
// set tracing parameters, starting from defaults
|
2010-06-10 18:43:12 +00:00
|
|
|
param = potrace_param_default();
|
|
|
|
if( !param )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Error allocating parameters: %s\n", strerror( errno ) );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
param->turdsize = 0;
|
|
|
|
|
|
|
|
/* convert the bitmap to curves */
|
2010-06-15 18:27:41 +00:00
|
|
|
st = potrace_trace( param, aPotrace_bitmap );
|
2010-06-10 18:43:12 +00:00
|
|
|
if( !st || st->status != POTRACE_STATUS_OK )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Error tracing bitmap: %s\n", strerror( errno ) );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BITMAPCONV_INFO info;
|
2010-06-15 18:27:41 +00:00
|
|
|
info.m_PixmapWidth = aPotrace_bitmap->w;
|
|
|
|
info.m_PixmapHeight = aPotrace_bitmap->h; // the bitmap size in pixels
|
2010-06-10 18:43:12 +00:00
|
|
|
info.m_Paths = st->plist;
|
2010-06-15 18:27:41 +00:00
|
|
|
info.m_Outfile = aOutfile;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2010-06-15 18:27:41 +00:00
|
|
|
switch( aFormat )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2010-06-15 18:27:41 +00:00
|
|
|
case 2:
|
2010-06-10 18:43:12 +00:00
|
|
|
info.m_Format = POSTSCRIPT_FMT;
|
|
|
|
info.m_ScaleX = info.m_ScaleY = 1.0; // the conversion scale
|
2011-10-26 11:06:04 +00:00
|
|
|
// output vector data, e.g. as a rudimentary EPS file (mainly for tests)
|
|
|
|
info.CreateOutputFile();
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
2010-06-15 18:27:41 +00:00
|
|
|
case 1:
|
2010-06-10 18:43:12 +00:00
|
|
|
info.m_Format = EESCHEMA_FMT;
|
2010-06-15 18:27:41 +00:00
|
|
|
info.m_ScaleX = 1000.0 / 300; // the conversion scale
|
2011-10-26 09:53:42 +00:00
|
|
|
info.m_ScaleY = -info.m_ScaleX; // Y axis is bottom to Top for components in libs
|
2011-10-26 11:06:04 +00:00
|
|
|
info.CreateOutputFile();
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
2010-06-15 18:27:41 +00:00
|
|
|
case 0:
|
2010-06-10 18:43:12 +00:00
|
|
|
info.m_Format = PCBNEW_FMT;
|
2011-10-26 09:53:42 +00:00
|
|
|
info.m_ScaleX = 10000.0 / 300; // the conversion scale
|
|
|
|
info.m_ScaleY = info.m_ScaleX; // Y axis is top to bottom in modedit
|
2011-10-26 11:06:04 +00:00
|
|
|
info.CreateOutputFile();
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-10-26 09:53:42 +00:00
|
|
|
break;
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-15 18:27:41 +00:00
|
|
|
bm_free( aPotrace_bitmap );
|
2010-06-10 18:43:12 +00:00
|
|
|
potrace_state_free( st );
|
|
|
|
potrace_param_free( param );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
void BITMAPCONV_INFO::OuputFileHeader()
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2011-10-26 11:06:04 +00:00
|
|
|
int Ypos = (int) ( m_PixmapHeight / 2 * m_ScaleY );
|
2010-06-10 18:43:12 +00:00
|
|
|
int fieldSize; // fields text size = 60 mils
|
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
switch( m_Format )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
case POSTSCRIPT_FMT:
|
|
|
|
/* output vector data, e.g. as a rudimentary EPS file */
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "%%!PS-Adobe-3.0 EPSF-3.0\n" );
|
|
|
|
fprintf( m_Outfile, "%%%%BoundingBox: 0 0 %d %d\n",
|
|
|
|
m_PixmapWidth, m_PixmapHeight );
|
|
|
|
fprintf( m_Outfile, "gsave\n" );
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PCBNEW_FMT:
|
|
|
|
#define FIELD_LAYER 21
|
|
|
|
fieldSize = 600; // fields text size = 60 mils
|
|
|
|
Ypos += fieldSize / 2;
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "PCBNEW-LibModule-V1\n" );
|
|
|
|
fprintf( m_Outfile, "$INDEX\n%s\n$EndINDEX\n", m_CmpName );
|
|
|
|
|
|
|
|
fprintf( m_Outfile, "#\n# %s\n", m_CmpName );
|
|
|
|
fprintf( m_Outfile, "# pixmap w = %d, h = %d\n#\n",
|
|
|
|
m_PixmapWidth, m_PixmapHeight );
|
|
|
|
fprintf( m_Outfile, "$MODULE %s\n", m_CmpName );
|
|
|
|
fprintf( m_Outfile, "Po 0 0 0 15 00000000 00000000 ~~\n" );
|
|
|
|
fprintf( m_Outfile, "Li %s\n", m_CmpName );
|
|
|
|
fprintf( m_Outfile, "T0 0 %d %d %d 0 %d N I %d \"G***\"\n",
|
2011-10-26 09:53:42 +00:00
|
|
|
Ypos, fieldSize, fieldSize, fieldSize / 5, FIELD_LAYER );
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "T1 0 %d %d %d 0 %d N I %d \"%s\"\n",
|
|
|
|
-Ypos, fieldSize, fieldSize, fieldSize / 5, FIELD_LAYER, m_CmpName );
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EESCHEMA_FMT:
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "EESchema-LIBRARY Version 2.3\n" );
|
|
|
|
fprintf( m_Outfile, "#\n# %s\n", m_CmpName );
|
|
|
|
fprintf( m_Outfile, "# pixmap size w = %d, h = %d\n#\n",
|
|
|
|
m_PixmapWidth, m_PixmapHeight );
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
// print reference and value
|
|
|
|
fieldSize = 60; // fields text size = 60 mils
|
|
|
|
Ypos += fieldSize / 2;
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "DEF %s G 0 40 Y Y 1 F N\n", m_CmpName );
|
|
|
|
fprintf( m_Outfile, "F0 \"#G\" 0 %d %d H I C CNN\n", Ypos, fieldSize );
|
|
|
|
fprintf( m_Outfile, "F1 \"%s\" 0 %d %d H I C CNN\n", m_CmpName, -Ypos, fieldSize );
|
|
|
|
fprintf( m_Outfile, "DRAW\n" );
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
void BITMAPCONV_INFO::OuputFileEnd()
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2011-10-26 11:06:04 +00:00
|
|
|
switch( m_Format )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
case POSTSCRIPT_FMT:
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "grestore\n" );
|
|
|
|
fprintf( m_Outfile, "%%EOF\n" );
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PCBNEW_FMT:
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "$EndMODULE %s\n", m_CmpName );
|
|
|
|
fprintf( m_Outfile, "$EndLIBRARY\n" );
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EESCHEMA_FMT:
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "ENDDRAW\n" );
|
|
|
|
fprintf( m_Outfile, "ENDDEF\n" );
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
/**
|
|
|
|
* Function OuputOnePolygon
|
|
|
|
* write one polygon to output file.
|
|
|
|
* Polygon coordinates are expected scaled by the polugon extraction function
|
|
|
|
*/
|
2011-10-26 11:06:04 +00:00
|
|
|
void BITMAPCONV_INFO::OuputOnePolygon( KPolygon & aPolygon )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
unsigned ii;
|
2011-10-26 09:53:42 +00:00
|
|
|
KPolyPoint currpoint;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
int offsetX = (int)( m_PixmapWidth / 2 * m_ScaleX );
|
|
|
|
int offsetY = (int)( m_PixmapHeight / 2 * m_ScaleY );
|
2011-10-26 09:53:42 +00:00
|
|
|
|
|
|
|
KPolyPoint startpoint = *aPolygon.begin();
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
switch( m_Format )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
case POSTSCRIPT_FMT:
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "%d %d moveto\n",
|
2011-10-26 09:53:42 +00:00
|
|
|
startpoint.x(), startpoint.y() );
|
|
|
|
for( ii = 1; ii < aPolygon.size(); ii++ )
|
|
|
|
{
|
|
|
|
currpoint = *(aPolygon.begin() + ii);
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "%d %d lineto\n",
|
2011-10-26 09:53:42 +00:00
|
|
|
currpoint.x(), currpoint.y() );
|
|
|
|
}
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "0 setgray fill\n" );
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PCBNEW_FMT:
|
|
|
|
{
|
|
|
|
#define SILKSCREEN_N_FRONT 21
|
|
|
|
int layer = SILKSCREEN_N_FRONT;
|
|
|
|
int width = 1;
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "DP %d %d %d %d %d %d %d\n",
|
2011-10-26 09:53:42 +00:00
|
|
|
0, 0, 0, 0,
|
2011-11-01 08:06:33 +00:00
|
|
|
(int) aPolygon.size() + 1, width, layer );
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
for( ii = 0; ii < aPolygon.size(); ii++ )
|
|
|
|
{
|
|
|
|
currpoint = *( aPolygon.begin() + ii );
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "Dl %d %d\n",
|
2011-10-26 09:53:42 +00:00
|
|
|
currpoint.x() - offsetX, currpoint.y() - offsetY );
|
|
|
|
}
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
// Close polygon
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, "Dl %d %d\n",
|
2011-10-26 09:53:42 +00:00
|
|
|
startpoint.x() - offsetX, startpoint.y() - offsetY );
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
2011-10-26 09:53:42 +00:00
|
|
|
break;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
case EESCHEMA_FMT:
|
2011-11-01 08:06:33 +00:00
|
|
|
fprintf( m_Outfile, "P %d 0 0 1", (int) aPolygon.size() + 1 );
|
2011-10-26 09:53:42 +00:00
|
|
|
for( ii = 0; ii < aPolygon.size(); ii++ )
|
|
|
|
{
|
|
|
|
currpoint = *(aPolygon.begin() + ii);
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, " %d %d",
|
2011-10-26 09:53:42 +00:00
|
|
|
currpoint.x() - offsetX, currpoint.y() - offsetY );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close polygon
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, " %d %d",
|
2011-10-26 09:53:42 +00:00
|
|
|
startpoint.x() - offsetX, startpoint.y() - offsetY );
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
fprintf( m_Outfile, " F\n" );
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
void BITMAPCONV_INFO::CreateOutputFile()
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2011-10-26 09:53:42 +00:00
|
|
|
KPolyPoint currpoint;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
std::vector <potrace_dpoint_t> cornersBuffer;
|
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
// This KPolygonSet polyset_areas is a complex polygon to draw
|
|
|
|
// and can be complex depending on holes inside this polygon
|
|
|
|
KPolygonSet polyset_areas;
|
|
|
|
|
|
|
|
// This KPolygonSet polyset_holes is the set of holes inside polyset_areas
|
|
|
|
KPolygonSet polyset_holes;
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
potrace_dpoint_t( *c )[3];
|
2011-10-26 11:06:04 +00:00
|
|
|
OuputFileHeader();
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
bool main_outline = true;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
/* draw each as a polygon with no hole.
|
|
|
|
* Bezier curves are approximated by a polyline
|
|
|
|
*/
|
2011-10-26 11:06:04 +00:00
|
|
|
potrace_path_t* paths = m_Paths; // the list of paths
|
2010-06-10 18:43:12 +00:00
|
|
|
while( paths != NULL )
|
|
|
|
{
|
2011-10-26 09:53:42 +00:00
|
|
|
int cnt = paths->curve.n;
|
|
|
|
int* tag = paths->curve.tag;
|
|
|
|
c = paths->curve.c;
|
|
|
|
potrace_dpoint_t startpoint = c[cnt - 1][2];
|
|
|
|
for( int i = 0; i < cnt; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
switch( tag[i] )
|
|
|
|
{
|
|
|
|
case POTRACE_CORNER:
|
|
|
|
cornersBuffer.push_back( c[i][1] );
|
|
|
|
cornersBuffer.push_back( c[i][2] );
|
|
|
|
startpoint = c[i][2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case POTRACE_CURVETO:
|
|
|
|
BezierToPolyline( cornersBuffer, startpoint, c[i][0], c[i][1], c[i][2] );
|
|
|
|
startpoint = c[i][2];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store current path
|
|
|
|
if( main_outline )
|
|
|
|
{
|
|
|
|
main_outline = false;
|
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
// build the current main polygon
|
|
|
|
std::vector<KPolyPoint> cornerslist; // a simple boost polygon
|
|
|
|
for( unsigned int i = 0; i < cornersBuffer.size(); i++ )
|
|
|
|
{
|
2011-10-26 11:06:04 +00:00
|
|
|
currpoint.x( (coordinate_type) (cornersBuffer[i].x * m_ScaleX) );
|
|
|
|
currpoint.y( (coordinate_type) (cornersBuffer[i].y * m_ScaleY) );
|
2011-10-26 09:53:42 +00:00
|
|
|
cornerslist.push_back( currpoint );
|
|
|
|
}
|
|
|
|
|
|
|
|
KPolygon poly;
|
|
|
|
bpl::set_points( poly, cornerslist.begin(), cornerslist.end() );
|
|
|
|
polyset_areas.push_back( poly );
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-10-26 09:53:42 +00:00
|
|
|
// Add current hole in polyset_holes
|
|
|
|
std::vector<KPolyPoint> cornerslist; // a simple boost polygon
|
|
|
|
for( unsigned int i = 0; i < cornersBuffer.size(); i++ )
|
|
|
|
{
|
2011-10-26 11:06:04 +00:00
|
|
|
currpoint.x( (coordinate_type) (cornersBuffer[i].x * m_ScaleX) );
|
|
|
|
currpoint.y( (coordinate_type) (cornersBuffer[i].y * m_ScaleY) );
|
2011-10-26 09:53:42 +00:00
|
|
|
cornerslist.push_back( currpoint );
|
|
|
|
}
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
KPolygon poly;
|
|
|
|
bpl::set_points( poly, cornerslist.begin(), cornerslist.end() );
|
|
|
|
polyset_holes.push_back( poly );
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
cornersBuffer.clear();
|
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
/* at the end of a group of a positive path and its negative children, fill.
|
|
|
|
*/
|
2010-06-10 18:43:12 +00:00
|
|
|
if( paths->next == NULL || paths->next->sign == '+' )
|
|
|
|
{
|
2011-10-26 09:53:42 +00:00
|
|
|
// Substract holes to main polygon:
|
|
|
|
polyset_areas -= polyset_holes;
|
|
|
|
|
|
|
|
// Output current resulting polygon(s)
|
|
|
|
for( unsigned ii = 0; ii < polyset_areas.size(); ii++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2011-10-26 09:53:42 +00:00
|
|
|
KPolygon& poly = polyset_areas[ii];
|
2011-10-26 11:06:04 +00:00
|
|
|
OuputOnePolygon(poly );
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
polyset_areas.clear();
|
|
|
|
polyset_holes.clear();
|
2010-06-10 18:43:12 +00:00
|
|
|
main_outline = true;
|
|
|
|
}
|
|
|
|
paths = paths->next;
|
|
|
|
}
|
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
OuputFileEnd();
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* render a Bezier curve. */
|
|
|
|
void BezierToPolyline( std::vector <potrace_dpoint_t>& aCornersBuffer,
|
|
|
|
potrace_dpoint_t p1,
|
|
|
|
potrace_dpoint_t p2,
|
|
|
|
potrace_dpoint_t p3,
|
|
|
|
potrace_dpoint_t p4 )
|
|
|
|
{
|
|
|
|
double dd0, dd1, dd, delta, e2, epsilon, t;
|
|
|
|
|
|
|
|
// p1 = starting point
|
|
|
|
|
|
|
|
/* we approximate the curve by small line segments. The interval
|
|
|
|
* size, epsilon, is determined on the fly so that the distance
|
|
|
|
* between the true curve and its approximation does not exceed the
|
|
|
|
* desired accuracy delta. */
|
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
delta = 0.25; /* desired accuracy, in pixels */
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
/* let dd = maximal value of 2nd derivative over curve - this must
|
|
|
|
* occur at an endpoint. */
|
|
|
|
dd0 = sq( p1.x - 2 * p2.x + p3.x ) + sq( p1.y - 2 * p2.y + p3.y );
|
|
|
|
dd1 = sq( p2.x - 2 * p3.x + p4.x ) + sq( p2.y - 2 * p3.y + p4.y );
|
|
|
|
dd = 6 * sqrt( max( dd0, dd1 ) );
|
|
|
|
e2 = 8 * delta <= dd ? 8 * delta / dd : 1;
|
|
|
|
epsilon = sqrt( e2 ); /* necessary interval size */
|
|
|
|
|
|
|
|
for( t = epsilon; t<1; t += epsilon )
|
|
|
|
{
|
|
|
|
potrace_dpoint_t intermediate_point;
|
|
|
|
intermediate_point.x = p1.x * cu( 1 - t ) +
|
|
|
|
3* p2.x* sq( 1 - t ) * t +
|
|
|
|
3 * p3.x * (1 - t) * sq( t ) +
|
2011-10-26 09:53:42 +00:00
|
|
|
p4.x* cu( t );
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
intermediate_point.y = p1.y * cu( 1 - t ) +
|
|
|
|
3* p2.y* sq( 1 - t ) * t +
|
|
|
|
3 * p3.y * (1 - t) * sq( t ) + p4.y* cu( t );
|
|
|
|
|
|
|
|
aCornersBuffer.push_back( intermediate_point );
|
|
|
|
}
|
|
|
|
|
|
|
|
aCornersBuffer.push_back( p4 );
|
|
|
|
}
|