2010-06-10 18:43:12 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
2019-04-01 10:51:27 +00:00
|
|
|
* Copyright (C) 1992-2019 jean-pierre.charras
|
2021-07-16 20:13:26 +00:00
|
|
|
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2010-06-10 18:43:12 +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
|
|
|
|
*/
|
|
|
|
|
2016-01-16 00:57:52 +00:00
|
|
|
#include <algorithm> // std::max
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cerrno>
|
2016-01-16 00:57:52 +00:00
|
|
|
#include <cmath>
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2019-05-30 18:23:07 +00:00
|
|
|
#include <string>
|
2010-06-10 18:43:12 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2021-02-15 18:41:23 +00:00
|
|
|
#include <convert_to_biu.h>
|
2021-07-29 09:47:43 +00:00
|
|
|
#include <layer_ids.h>
|
2014-08-21 14:57:25 +00:00
|
|
|
|
2020-10-24 01:38:50 +00:00
|
|
|
#include <locale_io.h>
|
2015-07-27 19:48:17 +00:00
|
|
|
#include <potracelib.h>
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2016-01-16 00:57:52 +00:00
|
|
|
#include "bitmap2component.h"
|
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
if( bm != nullptr )
|
2011-10-26 09:53:42 +00:00
|
|
|
{
|
|
|
|
free( bm->map );
|
|
|
|
}
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
free( bm );
|
2010-06-15 18:27:41 +00:00
|
|
|
}
|
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 );
|
|
|
|
|
|
|
|
|
2019-05-30 18:23:07 +00:00
|
|
|
BITMAPCONV_INFO::BITMAPCONV_INFO( std::string& aData ):
|
|
|
|
m_Data( aData )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
m_Format = POSTSCRIPT_FMT;
|
|
|
|
m_PixmapWidth = 0;
|
|
|
|
m_PixmapHeight = 0;
|
|
|
|
m_ScaleX = 1.0;
|
|
|
|
m_ScaleY = 1.0;
|
2021-07-16 20:13:26 +00:00
|
|
|
m_Paths = nullptr;
|
2011-10-26 11:06:04 +00:00
|
|
|
m_CmpName = "LOGO";
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
int BITMAPCONV_INFO::ConvertBitmap( potrace_bitmap_t* aPotrace_bitmap, OUTPUT_FMT_ID aFormat,
|
|
|
|
int aDpi_X, int aDpi_Y, BMP2CMP_MOD_LAYER aModLayer )
|
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();
|
2019-07-24 11:42:30 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( !param )
|
|
|
|
{
|
2019-06-19 16:24:39 +00:00
|
|
|
char msg[256];
|
|
|
|
sprintf( msg, "Error allocating parameters: %s\n", strerror( errno ) );
|
|
|
|
m_errors += msg;
|
2010-06-10 18:43:12 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-07-24 11:42:30 +00:00
|
|
|
|
|
|
|
// For parameters: see http://potrace.sourceforge.net/potracelib.pdf
|
|
|
|
param->turdsize = 0; // area (in pixels) of largest path to be ignored.
|
|
|
|
// Potrace default is 2
|
|
|
|
param->opttolerance = 0.2; // curve optimization tolerance. Potrace default is 0.2
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
/* convert the bitmap to curves */
|
2010-06-15 18:27:41 +00:00
|
|
|
st = potrace_trace( param, aPotrace_bitmap );
|
2019-07-24 11:42:30 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( !st || st->status != POTRACE_STATUS_OK )
|
|
|
|
{
|
2015-01-18 11:49:32 +00:00
|
|
|
if( st )
|
|
|
|
{
|
|
|
|
potrace_state_free( st );
|
|
|
|
}
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2015-01-18 11:49:32 +00:00
|
|
|
potrace_param_free( param );
|
|
|
|
|
2019-06-19 16:24:39 +00:00
|
|
|
char msg[256];
|
|
|
|
sprintf( msg, "Error tracing bitmap: %s\n", strerror( errno ) );
|
|
|
|
m_errors += msg;
|
2010-06-10 18:43:12 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-30 18:23:07 +00:00
|
|
|
m_PixmapWidth = aPotrace_bitmap->w;
|
|
|
|
m_PixmapHeight = aPotrace_bitmap->h; // the bitmap size in pixels
|
|
|
|
m_Paths = st->plist;
|
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
|
|
|
{
|
2021-08-10 08:52:33 +00:00
|
|
|
case KICAD_WKS_LOGO:
|
|
|
|
m_Format = KICAD_WKS_LOGO;
|
2021-02-15 18:41:23 +00:00
|
|
|
m_ScaleX = PL_IU_PER_MM * 25.4 / aDpi_X; // the conversion scale from PPI to micron
|
|
|
|
m_ScaleY = PL_IU_PER_MM * 25.4 / aDpi_Y; // Y axis is top to bottom
|
2019-05-30 18:23:07 +00:00
|
|
|
createOutputData();
|
2013-06-18 17:27:47 +00:00
|
|
|
break;
|
|
|
|
|
2014-08-21 14:57:25 +00:00
|
|
|
case POSTSCRIPT_FMT:
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Format = POSTSCRIPT_FMT;
|
|
|
|
m_ScaleX = 1.0; // the conversion scale
|
|
|
|
m_ScaleY = m_ScaleX;
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2011-10-26 11:06:04 +00:00
|
|
|
// output vector data, e.g. as a rudimentary EPS file (mainly for tests)
|
2019-05-30 18:23:07 +00:00
|
|
|
createOutputData();
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
2014-08-21 14:57:25 +00:00
|
|
|
case EESCHEMA_FMT:
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Format = EESCHEMA_FMT;
|
2021-08-10 08:52:33 +00:00
|
|
|
m_ScaleX = SCH_IU_PER_MM * 25.4 / aDpi_X; // the conversion scale from PPI to eeschema iu
|
|
|
|
m_ScaleY = -SCH_IU_PER_MM * 25.4 / aDpi_Y; // Y axis is bottom to Top for components in libs
|
2019-05-30 18:23:07 +00:00
|
|
|
createOutputData();
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
2014-08-21 14:57:25 +00:00
|
|
|
case PCBNEW_KICAD_MOD:
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Format = PCBNEW_KICAD_MOD;
|
2021-02-15 18:41:23 +00:00
|
|
|
m_ScaleX = PCB_IU_PER_MM * 25.4 / aDpi_X; // the conversion scale from PPI to IU
|
|
|
|
m_ScaleY = PCB_IU_PER_MM * 25.4 / aDpi_Y; // Y axis is top to bottom in Footprint Editor
|
2019-05-30 18:23:07 +00:00
|
|
|
createOutputData( aModLayer );
|
2013-06-18 17:27:47 +00:00
|
|
|
break;
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-01 10:51:27 +00:00
|
|
|
|
2019-08-20 17:22:30 +00:00
|
|
|
const char* BITMAPCONV_INFO::getBoardLayerName( BMP2CMP_MOD_LAYER aChoice )
|
2014-11-12 16:59:12 +00:00
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
const char* layerName = "F.SilkS";
|
2014-11-12 16:59:12 +00:00
|
|
|
|
|
|
|
switch( aChoice )
|
|
|
|
{
|
|
|
|
case MOD_LYR_FSOLDERMASK:
|
|
|
|
layerName = "F.Mask";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOD_LYR_ECO1:
|
|
|
|
layerName = "Eco1.User";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOD_LYR_ECO2:
|
|
|
|
layerName = "Eco2.User";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOD_LYR_FSILKS:
|
|
|
|
default: // case MOD_LYR_FSILKS only unless there is a bug
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return layerName;
|
|
|
|
}
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2019-04-01 10:51:27 +00:00
|
|
|
|
2019-05-30 18:23:07 +00:00
|
|
|
void BITMAPCONV_INFO::outputDataHeader( const char * aBrdLayerName )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2021-08-10 08:52:33 +00:00
|
|
|
double Ypos = ( m_PixmapHeight / 2 * m_ScaleY ); // fields Y position in mm
|
|
|
|
double fieldSize; // fields text size in mm
|
2019-05-30 18:23:07 +00:00
|
|
|
char strbuf[1024];
|
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:
|
|
|
|
/* output vector data, e.g. as a rudimentary EPS file */
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += "%%!PS-Adobe-3.0 EPSF-3.0\n";
|
|
|
|
sprintf( strbuf, "%%%%BoundingBox: 0 0 %d %d\n", m_PixmapWidth, m_PixmapHeight );
|
|
|
|
m_Data += strbuf;
|
|
|
|
m_Data += "gsave\n";
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
2013-06-18 17:27:47 +00:00
|
|
|
case PCBNEW_KICAD_MOD:
|
|
|
|
// fields text size = 1.5 mm
|
|
|
|
// fields text thickness = 1.5 / 5 = 0.3mm
|
2021-08-10 08:52:33 +00:00
|
|
|
sprintf( strbuf, "(footprint \"%s\" (version 20210606) (generator bitmap2component) "
|
|
|
|
"(layer \"F.Cu\")\n (at 0 0)\n", m_CmpName.c_str() );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2021-02-21 12:21:12 +00:00
|
|
|
sprintf( strbuf, "(attr board_only exclude_from_pos_files exclude_from_bom)\n");
|
|
|
|
m_Data += strbuf;
|
2019-06-19 16:24:39 +00:00
|
|
|
sprintf( strbuf, " (fp_text reference \"G***\" (at 0 0) (layer %s)\n"
|
2021-07-16 20:13:26 +00:00
|
|
|
" (effects (font (thickness 0.3)))\n )\n", aBrdLayerName );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
|
|
|
sprintf( strbuf, " (fp_text value \"%s\" (at 0.75 0) (layer %s) hide\n"
|
2021-07-16 20:13:26 +00:00
|
|
|
" (effects (font (thickness 0.3)))\n )\n", m_CmpName.c_str(), aBrdLayerName );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2013-06-18 17:27:47 +00:00
|
|
|
break;
|
|
|
|
|
2021-08-10 08:52:33 +00:00
|
|
|
case KICAD_WKS_LOGO:
|
|
|
|
m_Data += "(kicad_wks (version 20210606) (generator bitmap2component)\n";
|
|
|
|
m_Data += " (polygon (pos 0 0 rbcorner) (rotate 0) (linewidth 0.01)\n";
|
2013-06-18 17:27:47 +00:00
|
|
|
break;
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
case EESCHEMA_FMT:
|
2021-08-10 08:52:33 +00:00
|
|
|
fieldSize = 1.27; // fields text size in mm (= 50 mils)
|
|
|
|
Ypos /= SCH_IU_PER_MM;
|
|
|
|
Ypos += fieldSize / 2;
|
|
|
|
// sprintf( strbuf, "# pixmap size w = %d, h = %d\n#\n", m_PixmapWidth, m_PixmapHeight );
|
|
|
|
sprintf( strbuf, "(kicad_symbol_lib (version 20210619) (generator bitmap2component)\n"
|
|
|
|
" (symbol \"%s\" (pin_names (offset 1.016)) (in_bom yes) (on_board yes)\n",
|
|
|
|
m_CmpName.c_str() );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2021-08-10 08:52:33 +00:00
|
|
|
|
|
|
|
sprintf( strbuf, " (property \"Reference\" \"#G\" (id 0) (at 0 %g 0)\n"
|
|
|
|
" (effects (font (size %g %g)) hide)\n )\n",
|
|
|
|
-Ypos, fieldSize, fieldSize );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2021-08-10 08:52:33 +00:00
|
|
|
|
|
|
|
sprintf( strbuf, " (property \"Value\" \"%s\" (id 1) (at 0 %g 0)\n"
|
|
|
|
" (effects (font (size %g %g)) hide)\n )\n",
|
|
|
|
m_CmpName.c_str(), Ypos, fieldSize, fieldSize );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2021-08-10 08:52:33 +00:00
|
|
|
sprintf( strbuf, " (property \"Footprint\" \"\" (id 2) (at 0 0 0)\n"
|
|
|
|
" (effects (font (size %g %g)) hide)\n )\n",
|
|
|
|
fieldSize, fieldSize );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2021-08-10 08:52:33 +00:00
|
|
|
|
|
|
|
sprintf( strbuf, " (property \"Datasheet\" \"\" (id 3) (at 0 0 0)\n"
|
|
|
|
" (effects (font (size %g %g)) hide)\n )\n",
|
|
|
|
fieldSize, fieldSize );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2021-08-10 08:52:33 +00:00
|
|
|
|
|
|
|
sprintf( strbuf, " (symbol \"%s_0_0\"\n", m_CmpName.c_str() );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-30 18:23:07 +00:00
|
|
|
void BITMAPCONV_INFO::outputDataEnd()
|
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:
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += "grestore\n";
|
|
|
|
m_Data += "%%EOF\n";
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
2013-06-18 17:27:47 +00:00
|
|
|
case PCBNEW_KICAD_MOD:
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += ")\n";
|
2013-06-18 17:27:47 +00:00
|
|
|
break;
|
|
|
|
|
2021-08-10 08:52:33 +00:00
|
|
|
case KICAD_WKS_LOGO:
|
|
|
|
m_Data += " )\n)\n";
|
2013-06-18 17:27:47 +00:00
|
|
|
break;
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
case EESCHEMA_FMT:
|
2021-08-10 08:52:33 +00:00
|
|
|
m_Data += " )\n"; // end symbol_0_0
|
|
|
|
m_Data += " )\n"; // end symbol
|
|
|
|
m_Data += ")\n"; // end lib
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 10:51:27 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
void BITMAPCONV_INFO::outputOnePolygon( SHAPE_LINE_CHAIN& aPolygon, const char* aBrdLayerName )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2019-04-01 10:51:27 +00:00
|
|
|
// write one polygon to output file.
|
|
|
|
// coordinates are expected in target unit.
|
2015-07-27 19:48:17 +00:00
|
|
|
int ii, jj;
|
|
|
|
VECTOR2I currpoint;
|
2019-05-30 18:23:07 +00:00
|
|
|
char strbuf[1024];
|
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
|
|
|
|
2015-07-27 19:48:17 +00:00
|
|
|
const VECTOR2I startpoint = aPolygon.CPoint( 0 );
|
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:
|
2013-06-18 17:27:47 +00:00
|
|
|
offsetY = (int)( m_PixmapHeight * m_ScaleY );
|
2021-07-16 20:13:26 +00:00
|
|
|
sprintf( strbuf, "newpath\n%d %d moveto\n", startpoint.x, offsetY - startpoint.y );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2013-06-18 17:27:47 +00:00
|
|
|
jj = 0;
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2015-07-27 19:48:17 +00:00
|
|
|
for( ii = 1; ii < aPolygon.PointCount(); ii++ )
|
2011-10-26 09:53:42 +00:00
|
|
|
{
|
2015-07-27 19:48:17 +00:00
|
|
|
currpoint = aPolygon.CPoint( ii );
|
2021-07-16 20:13:26 +00:00
|
|
|
sprintf( strbuf, " %d %d lineto", currpoint.x, offsetY - currpoint.y );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2013-06-18 17:27:47 +00:00
|
|
|
|
|
|
|
if( jj++ > 6 )
|
|
|
|
{
|
|
|
|
jj = 0;
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += "\n";
|
2013-06-18 17:27:47 +00:00
|
|
|
}
|
2011-10-26 09:53:42 +00:00
|
|
|
}
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += "\nclosepath fill\n";
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
|
2013-06-18 17:27:47 +00:00
|
|
|
case PCBNEW_KICAD_MOD:
|
|
|
|
{
|
2019-07-24 11:42:30 +00:00
|
|
|
double width = 0.0; // outline thickness in mm: no thickness
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += " (fp_poly (pts";
|
2013-06-18 17:27:47 +00:00
|
|
|
|
|
|
|
jj = 0;
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2015-07-27 19:48:17 +00:00
|
|
|
for( ii = 0; ii < aPolygon.PointCount(); ii++ )
|
2013-06-18 17:27:47 +00:00
|
|
|
{
|
2015-07-27 19:48:17 +00:00
|
|
|
currpoint = aPolygon.CPoint( ii );
|
2019-05-30 18:23:07 +00:00
|
|
|
sprintf( strbuf, " (xy %f %f)",
|
2021-07-16 20:13:26 +00:00
|
|
|
( currpoint.x - offsetX ) / PCB_IU_PER_MM,
|
|
|
|
( currpoint.y - offsetY ) / PCB_IU_PER_MM );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2013-06-18 17:27:47 +00:00
|
|
|
|
|
|
|
if( jj++ > 6 )
|
|
|
|
{
|
|
|
|
jj = 0;
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += "\n ";
|
2013-06-18 17:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-01 10:51:27 +00:00
|
|
|
// No need to close polygon
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += " )";
|
|
|
|
sprintf( strbuf, "(layer %s) (width %f)\n )\n", aBrdLayerName, width );
|
|
|
|
m_Data += strbuf;
|
2013-06-18 17:27:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-08-10 08:52:33 +00:00
|
|
|
case KICAD_WKS_LOGO:
|
|
|
|
m_Data += " (pts";
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2013-06-18 17:27:47 +00:00
|
|
|
// Internal units = micron, file unit = mm
|
|
|
|
jj = 0;
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2015-07-27 19:48:17 +00:00
|
|
|
for( ii = 0; ii < aPolygon.PointCount(); ii++ )
|
2013-06-18 17:27:47 +00:00
|
|
|
{
|
2015-07-27 19:48:17 +00:00
|
|
|
currpoint = aPolygon.CPoint( ii );
|
2019-05-30 18:23:07 +00:00
|
|
|
sprintf( strbuf, " (xy %.3f %.3f)",
|
2021-07-16 20:13:26 +00:00
|
|
|
( currpoint.x - offsetX ) / PL_IU_PER_MM,
|
|
|
|
( currpoint.y - offsetY ) / PL_IU_PER_MM );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2013-06-18 17:27:47 +00:00
|
|
|
|
|
|
|
if( jj++ > 4 )
|
|
|
|
{
|
|
|
|
jj = 0;
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += "\n ";
|
2013-06-18 17:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2013-06-18 17:27:47 +00:00
|
|
|
// Close polygon
|
2019-05-30 18:23:07 +00:00
|
|
|
sprintf( strbuf, " (xy %.3f %.3f) )\n",
|
2021-02-15 18:41:23 +00:00
|
|
|
( startpoint.x - offsetX ) / PL_IU_PER_MM,
|
|
|
|
( startpoint.y - offsetY ) / PL_IU_PER_MM );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2013-06-18 17:27:47 +00:00
|
|
|
break;
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
case EESCHEMA_FMT:
|
2021-08-10 08:52:33 +00:00
|
|
|
// The polygon outline thickness is fixed here to 0.01 ( 0.0 is the default thickness)
|
|
|
|
#define SCH_LINE_THICKNESS_MM 0.01
|
|
|
|
//sprintf( strbuf, "P %d 0 0 %d", (int) aPolygon.PointCount() + 1, EE_LINE_THICKNESS );
|
|
|
|
m_Data += " (polyline\n (pts\n";
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2015-07-27 19:48:17 +00:00
|
|
|
for( ii = 0; ii < aPolygon.PointCount(); ii++ )
|
2011-10-26 09:53:42 +00:00
|
|
|
{
|
2015-07-27 19:48:17 +00:00
|
|
|
currpoint = aPolygon.CPoint( ii );
|
2021-08-10 08:52:33 +00:00
|
|
|
sprintf( strbuf, " (xy %f %f)\n",
|
|
|
|
( currpoint.x - offsetX ) / SCH_IU_PER_MM,
|
|
|
|
( currpoint.y - offsetY ) / SCH_IU_PER_MM );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2011-10-26 09:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close polygon
|
2021-08-10 08:52:33 +00:00
|
|
|
sprintf( strbuf, " (xy %f %f)\n",
|
|
|
|
( startpoint.x - offsetX ) / SCH_IU_PER_MM,
|
|
|
|
( startpoint.y - offsetY ) / SCH_IU_PER_MM );
|
2019-05-30 18:23:07 +00:00
|
|
|
m_Data += strbuf;
|
2021-08-10 08:52:33 +00:00
|
|
|
m_Data += " )\n"; // end pts
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2021-08-10 08:52:33 +00:00
|
|
|
sprintf( strbuf, " (stroke (width %g)) (fill (type outline))\n",
|
|
|
|
SCH_LINE_THICKNESS_MM );
|
|
|
|
m_Data += strbuf;
|
|
|
|
m_Data += " )\n"; // end polyline
|
2010-06-10 18:43:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-30 18:23:07 +00:00
|
|
|
void BITMAPCONV_INFO::createOutputData( BMP2CMP_MOD_LAYER aModLayer )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
std::vector <potrace_dpoint_t> cornersBuffer;
|
|
|
|
|
2015-07-27 19:48:17 +00:00
|
|
|
// polyset_areas is a set of polygon to draw
|
|
|
|
SHAPE_POLY_SET polyset_areas;
|
2011-10-26 09:53:42 +00:00
|
|
|
|
2015-07-27 19:48:17 +00:00
|
|
|
// polyset_holes is the set of holes inside polyset_areas outlines
|
|
|
|
SHAPE_POLY_SET polyset_holes;
|
2011-10-26 09:53:42 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
potrace_dpoint_t( *c )[3];
|
2013-06-18 17:27:47 +00:00
|
|
|
|
2015-01-23 11:24:32 +00:00
|
|
|
LOCALE_IO toggle; // Temporary switch the locale to standard C to r/w floats
|
2013-06-18 17:27:47 +00:00
|
|
|
|
2014-11-12 16:59:12 +00:00
|
|
|
// The layer name has meaning only for .kicad_mod files.
|
|
|
|
// For these files the header creates 2 invisible texts: value and ref
|
2021-06-09 19:32:58 +00:00
|
|
|
// (needed but not useful) on silk screen layer
|
2019-08-20 17:22:30 +00:00
|
|
|
outputDataHeader( getBoardLayerName( MOD_LYR_FSILKS ) );
|
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
|
2019-05-30 18:23:07 +00:00
|
|
|
|
2019-02-01 16:36:32 +00:00
|
|
|
if(!m_Paths)
|
2019-06-19 16:24:39 +00:00
|
|
|
{
|
2022-01-22 09:23:55 +00:00
|
|
|
m_errors += "No shape in black and white image to convert: no outline created\n";
|
2019-06-19 16:24:39 +00:00
|
|
|
}
|
2019-05-30 18:23:07 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
while( paths != nullptr )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
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];
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
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
|
2015-07-27 19:48:17 +00:00
|
|
|
polyset_areas.NewOutline();
|
2011-10-26 09:53:42 +00:00
|
|
|
for( unsigned int i = 0; i < cornersBuffer.size(); i++ )
|
|
|
|
{
|
2015-07-27 19:48:17 +00:00
|
|
|
polyset_areas.Append( int( cornersBuffer[i].x * m_ScaleX ),
|
|
|
|
int( cornersBuffer[i].y * m_ScaleY ) );
|
2011-10-26 09:53:42 +00:00
|
|
|
}
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-10-26 09:53:42 +00:00
|
|
|
// Add current hole in polyset_holes
|
2015-07-27 19:48:17 +00:00
|
|
|
polyset_holes.NewOutline();
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2011-10-26 09:53:42 +00:00
|
|
|
for( unsigned int i = 0; i < cornersBuffer.size(); i++ )
|
|
|
|
{
|
2015-07-27 19:48:17 +00:00
|
|
|
polyset_holes.Append( int( cornersBuffer[i].x * m_ScaleX ),
|
|
|
|
int( cornersBuffer[i].y * m_ScaleY ) );
|
2011-10-26 09:53:42 +00:00
|
|
|
}
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
2015-07-27 19:48:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
cornersBuffer.clear();
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
// at the end of a group of a positive path and its negative children, fill.
|
|
|
|
if( paths->next == nullptr || paths->next->sign == '+' )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2019-04-01 10:51:27 +00:00
|
|
|
polyset_areas.Simplify( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
|
|
|
|
polyset_holes.Simplify( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
|
|
|
|
polyset_areas.BooleanSubtract( polyset_holes, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
|
|
|
|
|
|
|
|
// Ensure there are no self intersecting polygons
|
2022-08-26 23:41:04 +00:00
|
|
|
if( polyset_areas.NormalizeAreaOutlines() )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2022-08-26 23:41:04 +00:00
|
|
|
// Convert polygon with holes to a unique polygon
|
|
|
|
polyset_areas.Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
|
|
|
|
|
|
|
|
// Output current resulting polygon(s)
|
|
|
|
for( int ii = 0; ii < polyset_areas.OutlineCount(); ii++ )
|
|
|
|
{
|
|
|
|
SHAPE_LINE_CHAIN& poly = polyset_areas.Outline( ii );
|
|
|
|
outputOnePolygon( poly, getBoardLayerName( aModLayer ));
|
|
|
|
}
|
|
|
|
|
|
|
|
polyset_areas.RemoveAllContours();
|
|
|
|
polyset_holes.RemoveAllContours();
|
|
|
|
main_outline = true;
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
paths = paths->next;
|
|
|
|
}
|
|
|
|
|
2019-05-30 18:23:07 +00:00
|
|
|
outputDataEnd();
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 20:21:25 +00:00
|
|
|
// a helper function to calculate a square value
|
|
|
|
inline double square( double x )
|
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
return x * x;
|
2015-12-15 20:21:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2015-12-15 20:21:25 +00:00
|
|
|
// a helper function to calculate a cube value
|
|
|
|
inline double cube( double x )
|
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
return x * x * x;
|
2015-12-15 20:21:25 +00:00
|
|
|
}
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
|
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. */
|
2015-12-15 20:21:25 +00:00
|
|
|
dd0 = square( p1.x - 2 * p2.x + p3.x ) + square( p1.y - 2 * p2.y + p3.y );
|
|
|
|
dd1 = square( p2.x - 2 * p3.x + p4.x ) + square( p2.y - 2 * p3.y + p4.y );
|
|
|
|
dd = 6 * sqrt( std::max( dd0, dd1 ) );
|
2010-06-10 18:43:12 +00:00
|
|
|
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;
|
2015-12-15 20:21:25 +00:00
|
|
|
intermediate_point.x = p1.x * cube( 1 - t ) +
|
|
|
|
3* p2.x* square( 1 - t ) * t +
|
|
|
|
3 * p3.x * (1 - t) * square( t ) +
|
|
|
|
p4.x* cube( t );
|
|
|
|
|
|
|
|
intermediate_point.y = p1.y * cube( 1 - t ) +
|
|
|
|
3* p2.y* square( 1 - t ) * t +
|
|
|
|
3 * p3.y * (1 - t) * square( t ) + p4.y* cube( t );
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
aCornersBuffer.push_back( intermediate_point );
|
|
|
|
}
|
|
|
|
|
|
|
|
aCornersBuffer.push_back( p4 );
|
|
|
|
}
|