KiCad PCB EDA Suite
SVG_IMPORT_PLUGIN Class Reference

#include <svg_import_plugin.h>

Inheritance diagram for SVG_IMPORT_PLUGIN:
GRAPHICS_IMPORT_PLUGIN

Public Member Functions

 SVG_IMPORT_PLUGIN ()
 
const wxString GetName () const override
 Return the plugin name. More...
 
const std::vector< std::string > GetFileExtensions () const override
 Return a vector of the file extensions handled by this plugin. More...
 
const wxString & GetMessages () const override
 
bool Import () override
 Actually imports the file. More...
 
bool Load (const wxString &aFileName) override
 Load file for import. More...
 
virtual double GetImageHeight () const override
 Return image height from original imported file. More...
 
virtual double GetImageWidth () const override
 Return image width from original imported file. More...
 
virtual void SetImporter (GRAPHICS_IMPORTER *aImporter)
 Set the receiver of the imported shapes. More...
 
wxString GetWildcards () const
 Return a list of wildcards that contains the file extensions handled by this plugin, separated with a semi-colon. More...
 
virtual void SetLineWidthMM (double aLineWidth)
 

Protected Attributes

GRAPHICS_IMPORTERm_importer
 < Importer used to create objects representing the imported shapes. More...
 

Private Member Functions

void DrawPath (const float *aPoints, int aNumPoints, bool aClosedPath, bool aFilled, double aLineWidth)
 
void DrawCubicBezierPath (const float *aPoints, int aNumPoints, std::vector< VECTOR2D > &aGeneratedPoints)
 
void DrawCubicBezierCurve (const float *aPoints, std::vector< VECTOR2D > &aGeneratedPoints)
 
void DrawPolygon (const std::vector< VECTOR2D > &aPoints, double aWidth)
 
void DrawLineSegments (const std::vector< VECTOR2D > &aPoints, double aWidth)
 

Private Attributes

struct NSVGimage * m_parsedImage
 
wxString m_messages
 
GRAPHICS_IMPORTER_BUFFER m_internalImporter
 

Detailed Description

Definition at line 39 of file svg_import_plugin.h.

Constructor & Destructor Documentation

◆ SVG_IMPORT_PLUGIN()

SVG_IMPORT_PLUGIN::SVG_IMPORT_PLUGIN ( )
inline

Definition at line 42 of file svg_import_plugin.h.

42 :
44 m_parsedImage( nullptr )
45 {
46 }
Interface for vector graphics import plugins.
struct NSVGimage * m_parsedImage

Member Function Documentation

◆ DrawCubicBezierCurve()

void SVG_IMPORT_PLUGIN::DrawCubicBezierCurve ( const float *  aPoints,
std::vector< VECTOR2D > &  aGeneratedPoints 
)
private

Definition at line 160 of file svg_import_plugin.cpp.

162{
163 auto start = getBezierPoint( aPoints, 0.0f );
164 auto end = getBezierPoint( aPoints, 1.0f );
165 auto segmentationThreshold = calculateBezierSegmentationThreshold( aPoints );
166
167 aGeneratedPoints.push_back( start );
168 segmentBezierCurve( start, end, 0.0f, 0.5f, aPoints, segmentationThreshold, aGeneratedPoints );
169 aGeneratedPoints.push_back( end );
170}
static void segmentBezierCurve(const VECTOR2D &aStart, const VECTOR2D &aEnd, float aOffset, float aStep, const float *aCurvePoints, float aSegmentationThreshold, std::vector< VECTOR2D > &aGeneratedPoints)
static VECTOR2D getBezierPoint(const float *aCurvePoints, float aStep)
static float calculateBezierSegmentationThreshold(const float *aCurvePoints)

References calculateBezierSegmentationThreshold(), getBezierPoint(), and segmentBezierCurve().

Referenced by DrawCubicBezierPath().

◆ DrawCubicBezierPath()

void SVG_IMPORT_PLUGIN::DrawCubicBezierPath ( const float *  aPoints,
int  aNumPoints,
std::vector< VECTOR2D > &  aGeneratedPoints 
)
private

Definition at line 142 of file svg_import_plugin.cpp.

144{
145 const int pointsPerSegment = 4;
146 const int curveSpecificPointsPerSegment = 3;
147 const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
148 const float* currentPoints = aPoints;
149 int remainingPoints = aNumPoints;
150
151 while( remainingPoints >= pointsPerSegment )
152 {
153 DrawCubicBezierCurve( currentPoints, aGeneratedPoints );
154 currentPoints += curveSpecificCoordinatesPerSegment;
155 remainingPoints -= curveSpecificPointsPerSegment;
156 }
157}
void DrawCubicBezierCurve(const float *aPoints, std::vector< VECTOR2D > &aGeneratedPoints)

References DrawCubicBezierCurve().

Referenced by DrawPath().

◆ DrawLineSegments()

void SVG_IMPORT_PLUGIN::DrawLineSegments ( const std::vector< VECTOR2D > &  aPoints,
double  aWidth 
)
private

Definition at line 179 of file svg_import_plugin.cpp.

180{
181 unsigned int numLineStartPoints = aPoints.size() - 1;
182
183 for( unsigned int pointIndex = 0; pointIndex < numLineStartPoints; ++pointIndex )
184 m_internalImporter.AddLine( aPoints[ pointIndex ], aPoints[ pointIndex + 1 ], aWidth );
185}
void AddLine(const VECTOR2D &aStart, const VECTOR2D &aEnd, double aWidth) override
Create an object representing a line segment.
GRAPHICS_IMPORTER_BUFFER m_internalImporter

References GRAPHICS_IMPORTER_BUFFER::AddLine(), and m_internalImporter.

Referenced by DrawPath().

◆ DrawPath()

void SVG_IMPORT_PLUGIN::DrawPath ( const float *  aPoints,
int  aNumPoints,
bool  aClosedPath,
bool  aFilled,
double  aLineWidth 
)
private

Definition at line 127 of file svg_import_plugin.cpp.

129{
130 std::vector< VECTOR2D > collectedPathPoints;
131
132 if( aNumPoints > 0 )
133 DrawCubicBezierPath( aPoints, aNumPoints, collectedPathPoints );
134
135 if( aPoly && aFilled )
136 DrawPolygon( collectedPathPoints, aLineWidth );
137 else
138 DrawLineSegments( collectedPathPoints, aLineWidth );
139}
void DrawCubicBezierPath(const float *aPoints, int aNumPoints, std::vector< VECTOR2D > &aGeneratedPoints)
void DrawLineSegments(const std::vector< VECTOR2D > &aPoints, double aWidth)
void DrawPolygon(const std::vector< VECTOR2D > &aPoints, double aWidth)

References DrawCubicBezierPath(), DrawLineSegments(), and DrawPolygon().

Referenced by Import().

◆ DrawPolygon()

void SVG_IMPORT_PLUGIN::DrawPolygon ( const std::vector< VECTOR2D > &  aPoints,
double  aWidth 
)
private

Definition at line 173 of file svg_import_plugin.cpp.

174{
175 m_internalImporter.AddPolygon( aPoints, aWidth );
176}
void AddPolygon(const std::vector< VECTOR2D > &aVertices, double aWidth) override

References GRAPHICS_IMPORTER_BUFFER::AddPolygon(), and m_internalImporter.

Referenced by DrawPath().

◆ GetFileExtensions()

const std::vector< std::string > SVG_IMPORT_PLUGIN::GetFileExtensions ( ) const
inlineoverridevirtual

Return a vector of the file extensions handled by this plugin.

Implements GRAPHICS_IMPORT_PLUGIN.

Definition at line 53 of file svg_import_plugin.h.

54 {
55 static std::vector<std::string> exts = { "svg" };
56 return exts;
57 }

◆ GetImageHeight()

double SVG_IMPORT_PLUGIN::GetImageHeight ( ) const
overridevirtual

Return image height from original imported file.

Returns
Original Image height in mm.

Implements GRAPHICS_IMPORT_PLUGIN.

Definition at line 103 of file svg_import_plugin.cpp.

104{
105 if( !m_parsedImage )
106 {
107 wxASSERT_MSG( false, wxT( "Image must have been loaded before checking height" ) );
108 return 0.0;
109 }
110
111 return m_parsedImage->height;
112}

References m_parsedImage.

◆ GetImageWidth()

double SVG_IMPORT_PLUGIN::GetImageWidth ( ) const
overridevirtual

Return image width from original imported file.

Returns
Original Image width in mm.

Implements GRAPHICS_IMPORT_PLUGIN.

Definition at line 115 of file svg_import_plugin.cpp.

116{
117 if( !m_parsedImage )
118 {
119 wxASSERT_MSG( false, wxT( "Image must have been loaded before checking width" ) );
120 return 0.0;
121 }
122
123 return m_parsedImage->width;
124}

References m_parsedImage.

◆ GetMessages()

const wxString & SVG_IMPORT_PLUGIN::GetMessages ( ) const
inlineoverridevirtual
Returns
the list of messages in one string. Each message ends by '
'

Implements GRAPHICS_IMPORT_PLUGIN.

Definition at line 62 of file svg_import_plugin.h.

63 {
64 return m_messages;
65 }

References m_messages.

◆ GetName()

const wxString SVG_IMPORT_PLUGIN::GetName ( ) const
inlineoverridevirtual

Return the plugin name.

This string will be used as the description in the file dialog.

Implements GRAPHICS_IMPORT_PLUGIN.

Definition at line 48 of file svg_import_plugin.h.

49 {
50 return "Scalable Vector Graphics";
51 }

◆ GetWildcards()

wxString GRAPHICS_IMPORT_PLUGIN::GetWildcards ( ) const
inlineinherited

Return a list of wildcards that contains the file extensions handled by this plugin, separated with a semi-colon.

Definition at line 69 of file graphics_import_plugin.h.

70 {
71 wxString ret;
72 bool first = true;
73
74 for( const auto& extension : GetFileExtensions() )
75 {
76 if( first )
77 first = false;
78 else
79 ret += wxT( ";" );
80
81 ret += wxT( "*." ) + formatWildcardExt( extension );
82 }
83
84 return ret;
85 }
virtual const std::vector< std::string > GetFileExtensions() const =0
Return a vector of the file extensions handled by this plugin.
wxString formatWildcardExt(const wxString &aWildcard)
Format wildcard extension to support case sensitive file dialogs.

References formatWildcardExt(), and GRAPHICS_IMPORT_PLUGIN::GetFileExtensions().

◆ Import()

bool SVG_IMPORT_PLUGIN::Import ( )
overridevirtual

Actually imports the file.

It is necessary to have loaded the file beforehand.

Implements GRAPHICS_IMPORT_PLUGIN.

Definition at line 71 of file svg_import_plugin.cpp.

72{
73 for( NSVGshape* shape = m_parsedImage->shapes; shape != nullptr; shape = shape->next )
74 {
75 double lineWidth = shape->strokeWidth;
76
78
79 switch( shape->fillRule )
80 {
81 case NSVG_FILLRULE_NONZERO: rule = GRAPHICS_IMPORTER::PF_NONZERO; break;
82 case NSVG_FILLRULE_EVENODD: rule = GRAPHICS_IMPORTER::PF_EVEN_ODD; break;
83 default: break;
84 }
85
87
88 for( NSVGpath* path = shape->paths; path != nullptr; path = path->next )
89 {
90 DrawPath( path->pts, path->npts, path->closed || rule == GRAPHICS_IMPORTER::PF_EVEN_ODD,
91 shape->fill.type == NSVG_PAINT_COLOR, lineWidth );
92 }
93 }
94
96 wxCHECK( m_importer, false );
98
99 return true;
100}
void ImportTo(GRAPHICS_IMPORTER &aImporter)
virtual void NewShape(POLY_FILL_RULE aFillRule=PF_NONZERO)
GRAPHICS_IMPORTER * m_importer
< Importer used to create objects representing the imported shapes.
void DrawPath(const float *aPoints, int aNumPoints, bool aClosedPath, bool aFilled, double aLineWidth)

References DrawPath(), GRAPHICS_IMPORTER_BUFFER::ImportTo(), GRAPHICS_IMPORT_PLUGIN::m_importer, m_internalImporter, m_parsedImage, GRAPHICS_IMPORTER::NewShape(), path, GRAPHICS_IMPORTER::PF_EVEN_ODD, GRAPHICS_IMPORTER::PF_NONZERO, and GRAPHICS_IMPORTER_BUFFER::PostprocessNestedPolygons().

◆ Load()

bool SVG_IMPORT_PLUGIN::Load ( const wxString &  aFileName)
overridevirtual

Load file for import.

It is necessary to have the GRAPHICS_IMPORTER object set before.

Implements GRAPHICS_IMPORT_PLUGIN.

Definition at line 53 of file svg_import_plugin.cpp.

54{
55 wxCHECK( m_importer, false );
56
57 // wxFopen takes care of unicode filenames across platforms
58 FILE* fp = wxFopen( aFileName, wxT( "rt" ) );
59
60 if( fp == nullptr )
61 return false;
62
63 // nsvgParseFromFile will close the file after reading
64 m_parsedImage = nsvgParseFromFile( fp, "mm", 96 );
65
66 wxCHECK( m_parsedImage, false );
67
68 return true;
69}

References GRAPHICS_IMPORT_PLUGIN::m_importer, and m_parsedImage.

◆ SetImporter()

virtual void GRAPHICS_IMPORT_PLUGIN::SetImporter ( GRAPHICS_IMPORTER aImporter)
inlinevirtualinherited

Set the receiver of the imported shapes.

Reimplemented in DXF_IMPORT_PLUGIN.

Definition at line 48 of file graphics_import_plugin.h.

49 {
50 m_importer = aImporter;
51 }

References GRAPHICS_IMPORT_PLUGIN::m_importer.

Referenced by DXF_IMPORT_PLUGIN::SetImporter().

◆ SetLineWidthMM()

virtual void GRAPHICS_IMPORT_PLUGIN::SetLineWidthMM ( double  aLineWidth)
inlinevirtualinherited

Reimplemented in DXF_IMPORT_PLUGIN.

Definition at line 115 of file graphics_import_plugin.h.

115{}

Member Data Documentation

◆ m_importer

GRAPHICS_IMPORTER* GRAPHICS_IMPORT_PLUGIN::m_importer
protectedinherited

< Importer used to create objects representing the imported shapes.

Definition at line 126 of file graphics_import_plugin.h.

Referenced by DXF_IMPORT_PLUGIN::Import(), Import(), Load(), GRAPHICS_IMPORT_PLUGIN::SetImporter(), and DXF_IMPORT_PLUGIN::SetImporter().

◆ m_internalImporter

GRAPHICS_IMPORTER_BUFFER SVG_IMPORT_PLUGIN::m_internalImporter
private

Definition at line 90 of file svg_import_plugin.h.

Referenced by DrawLineSegments(), DrawPolygon(), and Import().

◆ m_messages

wxString SVG_IMPORT_PLUGIN::m_messages
private

Definition at line 87 of file svg_import_plugin.h.

Referenced by GetMessages().

◆ m_parsedImage

struct NSVGimage* SVG_IMPORT_PLUGIN::m_parsedImage
private

Definition at line 85 of file svg_import_plugin.h.

Referenced by GetImageHeight(), GetImageWidth(), Import(), and Load().


The documentation for this class was generated from the following files: