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 );
198 const bool closed =
true;
203 DrawPath(
path->pts,
path->npts, !closed, stroke,
false, COLOR4D::UNSPECIFIED );
211 const bool closed =
path->closed || filled;
230 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before checking height" ) );
242 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before checking width" ) );
256 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before getting bbox" ) );
260 for( NSVGshape* shape =
m_parsedImage->shapes; shape !=
nullptr; shape = shape->next )
263 float( &bounds )[4] = shape->bounds;
265 shapeBbox.
SetOrigin( bounds[0], bounds[1] );
266 shapeBbox.
SetEnd( bounds[2], bounds[3] );
268 bbox.
Merge( shapeBbox );
276 std::vector<VECTOR2D>& aGeneratedPoints )
282 if( aGeneratedPoints.size() == 0 || aGeneratedPoints.back() != start )
283 aGeneratedPoints.push_back( start );
286 aGeneratedPoints.push_back(
end );
291 std::vector<VECTOR2D>& aGeneratedPoints )
293 const int pointsPerSegment = 4;
294 const int curveSpecificPointsPerSegment = 3;
295 const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
296 const float* currentPoints = aPoints;
297 int remainingPoints = aNumPoints;
299 while( remainingPoints >= pointsPerSegment )
302 currentPoints += curveSpecificCoordinatesPerSegment;
303 remainingPoints -= curveSpecificPointsPerSegment;
312 bool drewPolygon =
false;
317 std::vector<VECTOR2D> collectedPathPoints;
322 if( collectedPathPoints.size() > 2 )
324 DrawPolygon( collectedPathPoints, aStroke, aFilled, aFillColor );
342 const int pointsPerSegment = 4;
343 const int curveSpecificPointsPerSegment = 3;
344 const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
345 const float* currentCoords = aCoords;
346 int remainingPoints = aNumPoints;
348 while( remainingPoints >= pointsPerSegment )
358 currentCoords += curveSpecificCoordinatesPerSegment;
359 remainingPoints -= curveSpecificPointsPerSegment;
375 unsigned int numLineStartPoints = aPoints.size() - 1;
377 for(
unsigned int pointIndex = 0; pointIndex < numLineStartPoints; ++pointIndex )
384 return VECTOR2D( aPointCoordinates[0], aPointCoordinates[1] );
390 const int coordinatesPerPoint = 2;
392 auto firstCubicPoint =
getPoint( aPoints );
393 auto secondCubicPoint =
getPoint( aPoints + 1 * coordinatesPerPoint );
394 auto thirdCubicPoint =
getPoint( aPoints + 2 * coordinatesPerPoint );
395 auto fourthCubicPoint =
getPoint( aPoints + 3 * coordinatesPerPoint );
397 auto firstQuadraticPoint =
getPointInLine( firstCubicPoint, secondCubicPoint, aStep );
398 auto secondQuadraticPoint =
getPointInLine( secondCubicPoint, thirdCubicPoint, aStep );
399 auto thirdQuadraticPoint =
getPointInLine( thirdCubicPoint, fourthCubicPoint, aStep );
401 auto firstLinearPoint =
getPointInLine( firstQuadraticPoint, secondQuadraticPoint, aStep );
402 auto secondLinearPoint =
getPointInLine( secondQuadraticPoint, thirdQuadraticPoint, aStep );
404 return getPointInLine( firstLinearPoint, secondLinearPoint, aStep );
411 return aLineStart + ( aLineEnd - aLineStart ) * aDistance;
417 using comparatorFunction =
const float&(*)(
const float&,
const float& );
419 auto minimumComparator =
static_cast< comparatorFunction
>( &std::min );
420 auto maximumComparator =
static_cast< comparatorFunction
>( &std::max );
424 VECTOR2D boundingBoxDimensions = maximum - minimum;
426 return 0.001 * std::max( boundingBoxDimensions.
x, boundingBoxDimensions.
y );
431 std::function<
const float&(
const float&,
const float& ) > comparator )
433 float x = aCurvePoints[0];
434 float y = aCurvePoints[1];
436 for(
int pointIndex = 1; pointIndex < 3; ++pointIndex )
438 x = comparator( x, aCurvePoints[ 2 * pointIndex ] );
439 y = comparator( y, aCurvePoints[ 2 * pointIndex + 1 ] );
447 float aStep,
const float* aCurvePoints,
448 float aSegmentationThreshold,
449 std::vector< VECTOR2D >& aGeneratedPoints )
454 if( distanceToPreviousSegment > aSegmentationThreshold )
457 aSegmentationThreshold, aGeneratedPoints );
463 const VECTOR2D& aEnd,
float aOffset,
float aStep,
464 const float* aCurvePoints,
float aSegmentationThreshold,
465 std::vector< VECTOR2D >& aGeneratedPoints )
467 float newStep = aStep / 2.f;
468 float offsetAfterMiddle = aOffset + aStep;
470 segmentBezierCurve( aStart, aMiddle, aOffset, newStep, aCurvePoints, aSegmentationThreshold,
473 aGeneratedPoints.push_back( aMiddle );
476 aSegmentationThreshold, aGeneratedPoints );
483 auto lineDirection = aLineEnd - aLineStart;
485 auto lineStartToPoint = aPoint - aLineStart;
487 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