39 std::function<
const float&(
const float&,
const float& ) > comparator );
42 float aStep,
const float* aCurvePoints,
float aSegmentationThreshold,
43 std::vector< VECTOR2D >& aGeneratedPoints );
45 const VECTOR2D& aEnd,
float aOffset,
float aStep,
const float* aCurvePoints,
46 float aSegmentationThreshold, std::vector< VECTOR2D >& aGeneratedPoints );
65 FILE* fp = wxFopen( aFileName, wxT(
"rb" ) );
85 std::string str(
reinterpret_cast<char*
>( aMemBuffer.GetData() ), aMemBuffer.GetDataLen() );
86 wxCHECK( str.data()[aMemBuffer.GetDataLen()] ==
'\0',
false );
100 [](
unsigned int color )
105 for( NSVGshape* shape =
m_parsedImage->shapes; shape !=
nullptr; shape = shape->next )
107 if( !( shape->flags & NSVG_FLAGS_VISIBLE ) )
110 if( shape->stroke.type == NSVG_PAINT_NONE && shape->fill.type == NSVG_PAINT_NONE )
113 double lineWidth = shape->stroke.type != NSVG_PAINT_NONE ? shape->strokeWidth : -1;
114 bool filled = shape->fill.type != NSVG_PAINT_NONE && alpha( shape->fill.color ) > 0;
116 COLOR4D fillColor = COLOR4D::UNSPECIFIED;
118 if( shape->fill.type == NSVG_PAINT_COLOR )
120 unsigned int icolor = shape->fill.color;
122 fillColor.
r = std::clamp( ( icolor >> 0 ) & 0xFF, 0u, 255u ) / 255.0;
123 fillColor.
g = std::clamp( ( icolor >> 8 ) & 0xFF, 0u, 255u ) / 255.0;
124 fillColor.
b = std::clamp( ( icolor >> 16 ) & 0xFF, 0u, 255u ) / 255.0;
125 fillColor.
a = std::clamp( ( icolor >> 24 ) & 0xFF, 0u, 255u ) / 255.0;
128 if( fillColor == COLOR4D::BLACK )
129 fillColor = COLOR4D::UNSPECIFIED;
132 COLOR4D strokeColor = COLOR4D::UNSPECIFIED;
134 if( shape->stroke.type == NSVG_PAINT_COLOR )
136 unsigned int icolor = shape->stroke.color;
138 strokeColor.
r = std::clamp( ( icolor >> 0 ) & 0xFF, 0u, 255u ) / 255.0;
139 strokeColor.
g = std::clamp( ( icolor >> 8 ) & 0xFF, 0u, 255u ) / 255.0;
140 strokeColor.
b = std::clamp( ( icolor >> 16 ) & 0xFF, 0u, 255u ) / 255.0;
141 strokeColor.
a = std::clamp( ( icolor >> 24 ) & 0xFF, 0u, 255u ) / 255.0;
144 if( strokeColor == COLOR4D::BLACK )
145 strokeColor = COLOR4D::UNSPECIFIED;
150 if( shape->strokeDashCount > 0 )
152 float* dashArray = shape->strokeDashArray;
157 const float dashThreshold = shape->strokeWidth * 1.9f;
159 for(
int i = 0; i < shape->strokeDashCount; i += 2 )
161 if( dashArray[i] < dashThreshold )
167 if( dotCount > 0 && dashCount == 0 )
168 dashType = LINE_STYLE::DOT;
169 else if( dotCount == 0 && dashCount > 0 )
170 dashType = LINE_STYLE::DASH;
171 else if( dotCount == 1 && dashCount == 1 )
172 dashType = LINE_STYLE::DASHDOT;
173 else if( dotCount == 2 && dashCount == 1 )
174 dashType = LINE_STYLE::DASHDOTDOT;
181 switch( shape->fillRule )
192 if( filled && !
path->closed )
197 static IMPORTED_STROKE noStroke( -1, LINE_STYLE::SOLID, COLOR4D::UNSPECIFIED );
199 DrawPath(
path->pts,
path->npts,
false, stroke,
false, COLOR4D::UNSPECIFIED );
207 const bool closed =
path->closed || filled;
226 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before checking height" ) );
238 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before checking width" ) );
252 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before getting bbox" ) );
256 for( NSVGshape* shape =
m_parsedImage->shapes; shape !=
nullptr; shape = shape->next )
259 float( &bounds )[4] = shape->bounds;
261 shapeBbox.
SetOrigin( bounds[0], bounds[1] );
262 shapeBbox.
SetEnd( bounds[2], bounds[3] );
264 bbox.
Merge( shapeBbox );
272 std::vector<VECTOR2D>& aGeneratedPoints )
278 if( aGeneratedPoints.size() == 0 || aGeneratedPoints.back() != start )
279 aGeneratedPoints.push_back( start );
282 aGeneratedPoints.push_back(
end );
287 std::vector<VECTOR2D>& aGeneratedPoints )
289 const int pointsPerSegment = 4;
290 const int curveSpecificPointsPerSegment = 3;
291 const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
292 const float* currentPoints = aPoints;
293 int remainingPoints = aNumPoints;
295 while( remainingPoints >= pointsPerSegment )
298 currentPoints += curveSpecificCoordinatesPerSegment;
299 remainingPoints -= curveSpecificPointsPerSegment;
308 bool drewPolygon =
false;
313 std::vector<VECTOR2D> collectedPathPoints;
318 if( collectedPathPoints.size() > 2 )
320 DrawPolygon( collectedPathPoints, aStroke, aFilled, aFillColor );
338 const int pointsPerSegment = 4;
339 const int curveSpecificPointsPerSegment = 3;
340 const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
341 const float* currentCoords = aCoords;
342 int remainingPoints = aNumPoints;
344 while( remainingPoints >= pointsPerSegment )
354 currentCoords += curveSpecificCoordinatesPerSegment;
355 remainingPoints -= curveSpecificPointsPerSegment;
371 unsigned int numLineStartPoints = aPoints.size() - 1;
373 for(
unsigned int pointIndex = 0; pointIndex < numLineStartPoints; ++pointIndex )
380 return VECTOR2D( aPointCoordinates[0], aPointCoordinates[1] );
386 const int coordinatesPerPoint = 2;
388 auto firstCubicPoint =
getPoint( aPoints );
389 auto secondCubicPoint =
getPoint( aPoints + 1 * coordinatesPerPoint );
390 auto thirdCubicPoint =
getPoint( aPoints + 2 * coordinatesPerPoint );
391 auto fourthCubicPoint =
getPoint( aPoints + 3 * coordinatesPerPoint );
393 auto firstQuadraticPoint =
getPointInLine( firstCubicPoint, secondCubicPoint, aStep );
394 auto secondQuadraticPoint =
getPointInLine( secondCubicPoint, thirdCubicPoint, aStep );
395 auto thirdQuadraticPoint =
getPointInLine( thirdCubicPoint, fourthCubicPoint, aStep );
397 auto firstLinearPoint =
getPointInLine( firstQuadraticPoint, secondQuadraticPoint, aStep );
398 auto secondLinearPoint =
getPointInLine( secondQuadraticPoint, thirdQuadraticPoint, aStep );
400 return getPointInLine( firstLinearPoint, secondLinearPoint, aStep );
407 return aLineStart + ( aLineEnd - aLineStart ) * aDistance;
413 using comparatorFunction =
const float&(*)(
const float&,
const float& );
415 auto minimumComparator =
static_cast< comparatorFunction
>( &std::min );
416 auto maximumComparator =
static_cast< comparatorFunction
>( &std::max );
420 VECTOR2D boundingBoxDimensions = maximum - minimum;
422 return 0.001 * std::max( boundingBoxDimensions.
x, boundingBoxDimensions.
y );
427 std::function<
const float&(
const float&,
const float& ) > comparator )
429 float x = aCurvePoints[0];
430 float y = aCurvePoints[1];
432 for(
int pointIndex = 1; pointIndex < 3; ++pointIndex )
434 x = comparator( x, aCurvePoints[ 2 * pointIndex ] );
435 y = comparator( y, aCurvePoints[ 2 * pointIndex + 1 ] );
443 float aStep,
const float* aCurvePoints,
444 float aSegmentationThreshold,
445 std::vector< VECTOR2D >& aGeneratedPoints )
450 if( distanceToPreviousSegment > aSegmentationThreshold )
453 aSegmentationThreshold, aGeneratedPoints );
459 const VECTOR2D& aEnd,
float aOffset,
float aStep,
460 const float* aCurvePoints,
float aSegmentationThreshold,
461 std::vector< VECTOR2D >& aGeneratedPoints )
463 float newStep = aStep / 2.f;
464 float offsetAfterMiddle = aOffset + aStep;
466 segmentBezierCurve( aStart, aMiddle, aOffset, newStep, aCurvePoints, aSegmentationThreshold,
469 aGeneratedPoints.push_back( aMiddle );
472 aSegmentationThreshold, aGeneratedPoints );
479 auto lineDirection = aLineEnd - aLineStart;
481 auto lineStartToPoint = aPoint - aLineStart;
483 auto distance = lineNormal.Dot( lineStartToPoint );
constexpr void SetOrigin(const Vec &pos)
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
constexpr void SetEnd(coord_type x, coord_type y)
void AddSpline(const VECTOR2D &aStart, const VECTOR2D &aBezierControl1, const VECTOR2D &aBezierControl2, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke) override
Create an object representing an arc.
void AddLine(const VECTOR2D &aStart, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke) override
Create an object representing a line segment.
void PostprocessNestedPolygons()
void ImportTo(GRAPHICS_IMPORTER &aImporter)
void AddPolygon(const std::vector< VECTOR2D > &aVertices, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor=COLOR4D::UNSPECIFIED) override
Create an object representing a polygon.
virtual void NewShape(POLY_FILL_RULE aFillRule=PF_NONZERO)
GRAPHICS_IMPORTER * m_importer
Importer used to create objects representing the imported shapes.
A clone of IMPORTED_STROKE, but with floating-point width.
A color representation with 4 components: red, green, blue, alpha.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
void DrawPolygon(const std::vector< VECTOR2D > &aPoints, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor)
virtual BOX2D GetImageBBox() const override
Return image bounding box from original imported file.
GRAPHICS_IMPORTER_BUFFER m_internalImporter
struct NSVGimage * m_parsedImage
void DrawLineSegments(const std::vector< VECTOR2D > &aPoints, const IMPORTED_STROKE &aStroke)
bool Import() override
Actually imports the file.
virtual double GetImageWidth() const override
Return image width from original imported file.
bool LoadFromMemory(const wxMemoryBuffer &aMemBuffer) override
Set memory buffer with content for import.
bool Load(const wxString &aFileName) override
Load file for import.
virtual double GetImageHeight() const override
Return image height from original imported file.
void DrawSplinePath(const float *aPoints, int aNumPoints, const IMPORTED_STROKE &aStroke)
Draw a path made up of cubic Bezier curves, adding them as real bezier curves.
void DrawPath(const float *aPoints, int aNumPoints, bool aClosedPath, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor)
void ReportMsg(const wxString &aMessage) override
constexpr VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
LINE_STYLE
Dashed line types.
static void segmentBezierCurve(const VECTOR2D &aStart, const VECTOR2D &aEnd, float aOffset, float aStep, const float *aCurvePoints, float aSegmentationThreshold, std::vector< VECTOR2D > &aGeneratedPoints)
static float distanceFromPointToLine(const VECTOR2D &aPoint, const VECTOR2D &aLineStart, const VECTOR2D &aLineEnd)
static VECTOR2D getPointInLine(const VECTOR2D &aLineStart, const VECTOR2D &aLineEnd, float aDistance)
static VECTOR2D getBezierPoint(const float *aCurvePoints, float aStep)
static VECTOR2D calculateBezierBoundingBoxExtremity(const float *aCurvePoints, std::function< const float &(const float &, const float &) > comparator)
static void GatherInterpolatedCubicBezierCurve(const float *aPoints, std::vector< VECTOR2D > &aGeneratedPoints)
static void createNewBezierCurveSegments(const VECTOR2D &aStart, const VECTOR2D &aMiddle, const VECTOR2D &aEnd, float aOffset, float aStep, const float *aCurvePoints, float aSegmentationThreshold, std::vector< VECTOR2D > &aGeneratedPoints)
static VECTOR2D getPoint(const float *aPointCoordinates)
static float calculateBezierSegmentationThreshold(const float *aCurvePoints)
static void GatherInterpolatedCubicBezierPath(const float *aPoints, int aNumPoints, std::vector< VECTOR2D > &aGeneratedPoints)
VECTOR2< double > VECTOR2D