40 std::function<
const float&(
const float&,
const float& ) > comparator );
43 float aStep,
const float* aCurvePoints,
float aSegmentationThreshold,
44 std::vector< VECTOR2D >& aGeneratedPoints );
46 const VECTOR2D& aEnd,
float aOffset,
float aStep,
const float* aCurvePoints,
47 float aSegmentationThreshold, std::vector< VECTOR2D >& aGeneratedPoints );
66 FILE* fp = wxFopen( aFileName, wxT(
"rb" ) );
86 std::string str(
reinterpret_cast<char*
>( aMemBuffer.GetData() ), aMemBuffer.GetDataLen() );
87 wxCHECK( str.data()[aMemBuffer.GetDataLen()] ==
'\0',
false );
101 [](
unsigned int color )
106 for( NSVGshape* shape =
m_parsedImage->shapes; shape !=
nullptr; shape = shape->next )
108 if( !( shape->flags & NSVG_FLAGS_VISIBLE ) )
111 if( shape->stroke.type == NSVG_PAINT_NONE && shape->fill.type == NSVG_PAINT_NONE )
114 double lineWidth = shape->stroke.type != NSVG_PAINT_NONE ? shape->strokeWidth : -1;
115 bool filled = shape->fill.type != NSVG_PAINT_NONE && alpha( shape->fill.color ) > 0;
119 if( shape->fill.type == NSVG_PAINT_COLOR )
121 unsigned int icolor = shape->fill.color;
123 fillColor.
r = std::clamp( ( icolor >> 0 ) & 0xFF, 0u, 255u ) / 255.0;
124 fillColor.
g = std::clamp( ( icolor >> 8 ) & 0xFF, 0u, 255u ) / 255.0;
125 fillColor.
b = std::clamp( ( icolor >> 16 ) & 0xFF, 0u, 255u ) / 255.0;
126 fillColor.
a = std::clamp( ( icolor >> 24 ) & 0xFF, 0u, 255u ) / 255.0;
135 if( shape->stroke.type == NSVG_PAINT_COLOR )
137 unsigned int icolor = shape->stroke.color;
139 strokeColor.
r = std::clamp( ( icolor >> 0 ) & 0xFF, 0u, 255u ) / 255.0;
140 strokeColor.
g = std::clamp( ( icolor >> 8 ) & 0xFF, 0u, 255u ) / 255.0;
141 strokeColor.
b = std::clamp( ( icolor >> 16 ) & 0xFF, 0u, 255u ) / 255.0;
142 strokeColor.
a = std::clamp( ( icolor >> 24 ) & 0xFF, 0u, 255u ) / 255.0;
151 if( shape->strokeDashCount > 0 )
153 float* dashArray = shape->strokeDashArray;
158 const float dashThreshold = shape->strokeWidth * 1.9f;
160 for(
int i = 0; i < shape->strokeDashCount; i += 2 )
162 if( dashArray[i] < dashThreshold )
168 if( dotCount > 0 && dashCount == 0 )
170 else if( dotCount == 0 && dashCount > 0 )
172 else if( dotCount == 1 && dashCount == 1 )
174 else if( dotCount == 2 && dashCount == 1 )
182 switch( shape->fillRule )
193 if( filled && !
path->closed )
199 const bool closed =
true;
212 const bool closed =
path->closed || filled;
231 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before checking height" ) );
243 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before checking width" ) );
257 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before getting bbox" ) );
261 for( NSVGshape* shape =
m_parsedImage->shapes; shape !=
nullptr; shape = shape->next )
264 float( &bounds )[4] = shape->bounds;
266 shapeBbox.
SetOrigin( bounds[0], bounds[1] );
267 shapeBbox.
SetEnd( bounds[2], bounds[3] );
269 bbox.
Merge( shapeBbox );
277 std::vector<VECTOR2D>& aGeneratedPoints )
283 if( aGeneratedPoints.size() == 0 || aGeneratedPoints.back() != start )
284 aGeneratedPoints.push_back( start );
287 aGeneratedPoints.push_back(
end );
292 std::vector<VECTOR2D>& aGeneratedPoints )
294 const int pointsPerSegment = 4;
295 const int curveSpecificPointsPerSegment = 3;
296 const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
297 const float* currentPoints = aPoints;
298 int remainingPoints = aNumPoints;
300 while( remainingPoints >= pointsPerSegment )
303 currentPoints += curveSpecificCoordinatesPerSegment;
304 remainingPoints -= curveSpecificPointsPerSegment;
313 bool drewPolygon =
false;
318 std::vector<VECTOR2D> collectedPathPoints;
323 if( collectedPathPoints.size() > 2 )
325 DrawPolygon( collectedPathPoints, aStroke, aFilled, aFillColor );
343 const int pointsPerSegment = 4;
344 const int curveSpecificPointsPerSegment = 3;
345 const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
346 const float* currentCoords = aCoords;
347 int remainingPoints = aNumPoints;
349 while( remainingPoints >= pointsPerSegment )
359 currentCoords += curveSpecificCoordinatesPerSegment;
360 remainingPoints -= curveSpecificPointsPerSegment;
376 unsigned int numLineStartPoints = aPoints.size() - 1;
378 for(
unsigned int pointIndex = 0; pointIndex < numLineStartPoints; ++pointIndex )
385 return VECTOR2D( aPointCoordinates[0], aPointCoordinates[1] );
391 const int coordinatesPerPoint = 2;
393 auto firstCubicPoint =
getPoint( aPoints );
394 auto secondCubicPoint =
getPoint( aPoints + 1 * coordinatesPerPoint );
395 auto thirdCubicPoint =
getPoint( aPoints + 2 * coordinatesPerPoint );
396 auto fourthCubicPoint =
getPoint( aPoints + 3 * coordinatesPerPoint );
398 auto firstQuadraticPoint =
getPointInLine( firstCubicPoint, secondCubicPoint, aStep );
399 auto secondQuadraticPoint =
getPointInLine( secondCubicPoint, thirdCubicPoint, aStep );
400 auto thirdQuadraticPoint =
getPointInLine( thirdCubicPoint, fourthCubicPoint, aStep );
402 auto firstLinearPoint =
getPointInLine( firstQuadraticPoint, secondQuadraticPoint, aStep );
403 auto secondLinearPoint =
getPointInLine( secondQuadraticPoint, thirdQuadraticPoint, aStep );
405 return getPointInLine( firstLinearPoint, secondLinearPoint, aStep );
412 return aLineStart + ( aLineEnd - aLineStart ) * aDistance;
418 using comparatorFunction =
const float&(*)(
const float&,
const float& );
420 auto minimumComparator =
static_cast< comparatorFunction
>( &std::min );
421 auto maximumComparator =
static_cast< comparatorFunction
>( &std::max );
425 VECTOR2D boundingBoxDimensions = maximum - minimum;
427 return 0.001 * std::max( boundingBoxDimensions.
x, boundingBoxDimensions.
y );
432 std::function<
const float&(
const float&,
const float& ) > comparator )
434 float x = aCurvePoints[0];
435 float y = aCurvePoints[1];
437 for(
int pointIndex = 1; pointIndex < 3; ++pointIndex )
439 x = comparator( x, aCurvePoints[ 2 * pointIndex ] );
440 y = comparator( y, aCurvePoints[ 2 * pointIndex + 1 ] );
448 float aStep,
const float* aCurvePoints,
449 float aSegmentationThreshold,
450 std::vector< VECTOR2D >& aGeneratedPoints )
455 if( distanceToPreviousSegment > aSegmentationThreshold )
458 aSegmentationThreshold, aGeneratedPoints );
464 const VECTOR2D& aEnd,
float aOffset,
float aStep,
465 const float* aCurvePoints,
float aSegmentationThreshold,
466 std::vector< VECTOR2D >& aGeneratedPoints )
468 float newStep = aStep / 2.f;
469 float offsetAfterMiddle = aOffset + aStep;
471 segmentBezierCurve( aStart, aMiddle, aOffset, newStep, aCurvePoints, aSegmentationThreshold,
474 aGeneratedPoints.push_back( aMiddle );
477 aSegmentationThreshold, aGeneratedPoints );
484 auto lineDirection = aLineEnd - aLineStart;
486 auto lineStartToPoint = aPoint - aLineStart;
488 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)
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
static const COLOR4D BLACK
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