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

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 std::string & 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
 
std::string m_messages
 

Detailed Description

Definition at line 38 of file svg_import_plugin.h.

Member Function Documentation

◆ DrawCubicBezierCurve()

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

Definition at line 142 of file svg_import_plugin.cpp.

144 {
145  auto start = getBezierPoint( aPoints, 0.0f );
146  auto end = getBezierPoint( aPoints, 1.0f );
147  auto segmentationThreshold = calculateBezierSegmentationThreshold( aPoints );
148 
149  aGeneratedPoints.push_back( start );
150  segmentBezierCurve( start, end, 0.0f, 0.5f, aPoints, segmentationThreshold, aGeneratedPoints );
151  aGeneratedPoints.push_back( end );
152 }
static float calculateBezierSegmentationThreshold(const float *aCurvePoints)
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)

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 124 of file svg_import_plugin.cpp.

126 {
127  const int pointsPerSegment = 4;
128  const int curveSpecificPointsPerSegment = 3;
129  const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
130  const float* currentPoints = aPoints;
131  int remainingPoints = aNumPoints;
132 
133  while( remainingPoints >= pointsPerSegment )
134  {
135  DrawCubicBezierCurve( currentPoints, aGeneratedPoints );
136  currentPoints += curveSpecificCoordinatesPerSegment;
137  remainingPoints -= curveSpecificPointsPerSegment;
138  }
139 }
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 161 of file svg_import_plugin.cpp.

162 {
163  unsigned int numLineStartPoints = aPoints.size() - 1;
164 
165  for( unsigned int pointIndex = 0; pointIndex < numLineStartPoints; ++pointIndex )
166  m_importer->AddLine( aPoints[ pointIndex ], aPoints[ pointIndex + 1 ], aWidth );
167 }
GRAPHICS_IMPORTER * m_importer
Importer used to create objects representing the imported shapes.
virtual void AddLine(const VECTOR2D &aOrigin, const VECTOR2D &aEnd, double aWidth)=0
Create an object representing a line segment.

References GRAPHICS_IMPORTER::AddLine(), and GRAPHICS_IMPORT_PLUGIN::m_importer.

Referenced by DrawPath().

◆ DrawPath()

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

Definition at line 110 of file svg_import_plugin.cpp.

111 {
112  std::vector< VECTOR2D > collectedPathPoints;
113 
114  if( aNumPoints > 0 )
115  DrawCubicBezierPath( aPoints, aNumPoints, collectedPathPoints );
116 
117  if( aFilled && aClosedPath )
118  DrawPolygon( collectedPathPoints, aLineWidth );
119  else
120  DrawLineSegments( collectedPathPoints, aLineWidth );
121 }
void DrawPolygon(const std::vector< VECTOR2D > &aPoints, double aWidth)
void DrawCubicBezierPath(const float *aPoints, int aNumPoints, std::vector< VECTOR2D > &aGeneratedPoints)
void DrawLineSegments(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 155 of file svg_import_plugin.cpp.

156 {
157  m_importer->AddPolygon( aPoints, aWidth );
158 }
virtual void AddPolygon(const std::vector< VECTOR2D > &aVertices, double aWidth)=0
GRAPHICS_IMPORTER * m_importer
Importer used to create objects representing the imported shapes.

References GRAPHICS_IMPORTER::AddPolygon(), and GRAPHICS_IMPORT_PLUGIN::m_importer.

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 46 of file svg_import_plugin.h.

47  {
48  static std::vector<std::string> exts = { "svg" };
49  return exts;
50  }

◆ 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 86 of file svg_import_plugin.cpp.

87 {
88  if( !m_parsedImage )
89  {
90  wxASSERT_MSG(false, "Image must have been loaded before checking height");
91  return 0.0;
92  }
93 
94  return m_parsedImage->height;
95 }
struct NSVGimage * m_parsedImage

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 98 of file svg_import_plugin.cpp.

99 {
100  if( !m_parsedImage )
101  {
102  wxASSERT_MSG(false, "Image must have been loaded before checking width");
103  return 0.0;
104  }
105 
106  return m_parsedImage->width;
107 }
struct NSVGimage * m_parsedImage

References m_parsedImage.

◆ GetMessages()

const std::string& 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 55 of file svg_import_plugin.h.

56  {
57  return m_messages;
58  }
std::string m_messages

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 41 of file svg_import_plugin.h.

42  {
43  return "Scalable Vector Graphics";
44  }

◆ 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 68 of file graphics_import_plugin.h.

69  {
70  wxString ret;
71  bool first = true;
72 
73  for( const auto& extension : GetFileExtensions() )
74  {
75  if( first )
76  first = false;
77  else
78  ret += ";";
79 
80  ret += "*." + formatWildcardExt( extension );
81  }
82 
83  return ret;
84  }
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 72 of file svg_import_plugin.cpp.

73 {
74  for( NSVGshape* shape = m_parsedImage->shapes; shape != NULL; shape = shape->next )
75  {
76  double lineWidth = shape->strokeWidth;
77 
78  for( NSVGpath* path = shape->paths; path != NULL; path = path->next )
79  DrawPath( path->pts, path->npts, path->closed, shape->fill.type == NSVG_PAINT_COLOR, lineWidth );
80  }
81 
82  return true;
83 }
void DrawPath(const float *aPoints, int aNumPoints, bool aClosedPath, bool aFilled, double aLineWidth)
#define NULL
struct NSVGimage * m_parsedImage

References DrawPath(), m_parsedImage, and NULL.

◆ 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 54 of file svg_import_plugin.cpp.

55 {
56  wxCHECK( m_importer, false );
57 
58  // wxFopen takes care of unicode filenames across platforms
59  FILE* fp = wxFopen( aFileName, "rt" );
60 
61  if( fp == nullptr )
62  return false;
63 
64  // nsvgParseFromFile will close the file after reading
65  m_parsedImage = nsvgParseFromFile( fp, "mm", 96 );
66 
67  wxCHECK( m_parsedImage, false );
68 
69  return true;
70 }
struct NSVGimage * m_parsedImage
GRAPHICS_IMPORTER * m_importer
Importer used to create objects representing the imported shapes.

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 47 of file graphics_import_plugin.h.

48  {
49  m_importer = aImporter;
50  }
GRAPHICS_IMPORTER * m_importer
Importer used to create objects representing the imported shapes.

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 114 of file graphics_import_plugin.h.

114 {}

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 125 of file graphics_import_plugin.h.

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

◆ m_messages

std::string SVG_IMPORT_PLUGIN::m_messages
private

Definition at line 80 of file svg_import_plugin.h.

Referenced by GetMessages().

◆ m_parsedImage

struct NSVGimage* SVG_IMPORT_PLUGIN::m_parsedImage
private

Definition at line 78 of file svg_import_plugin.h.

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


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