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 )
210 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before checking height" ) );
222 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before checking width" ) );
236 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before getting bbox" ) );
240 for( NSVGshape* shape =
m_parsedImage->shapes; shape !=
nullptr; shape = shape->next )
243 float( &bounds )[4] = shape->bounds;
245 shapeBbox.
SetOrigin( bounds[0], bounds[1] );
246 shapeBbox.
SetEnd( bounds[2], bounds[3] );
248 bbox.
Merge( shapeBbox );
259 std::vector<VECTOR2D> collectedPathPoints;
264 if( aClosedPath && collectedPathPoints.size() > 2 )
265 DrawPolygon( collectedPathPoints, aStroke, aFilled, aFillColor );
272 std::vector<VECTOR2D>& aGeneratedPoints )
274 const int pointsPerSegment = 4;
275 const int curveSpecificPointsPerSegment = 3;
276 const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
277 const float* currentPoints = aPoints;
278 int remainingPoints = aNumPoints;
280 while( remainingPoints >= pointsPerSegment )
283 currentPoints += curveSpecificCoordinatesPerSegment;
284 remainingPoints -= curveSpecificPointsPerSegment;
290 std::vector<VECTOR2D>& aGeneratedPoints )
296 aGeneratedPoints.push_back( start );
297 segmentBezierCurve( start, end, 0.0f, 0.5f, aPoints, segmentationThreshold, aGeneratedPoints );
298 aGeneratedPoints.push_back( end );
313 unsigned int numLineStartPoints = aPoints.size() - 1;
315 for(
unsigned int pointIndex = 0; pointIndex < numLineStartPoints; ++pointIndex )
322 return VECTOR2D( aPointCoordinates[0], aPointCoordinates[1] );
328 const int coordinatesPerPoint = 2;
330 auto firstCubicPoint =
getPoint( aPoints );
331 auto secondCubicPoint =
getPoint( aPoints + 1 * coordinatesPerPoint );
332 auto thirdCubicPoint =
getPoint( aPoints + 2 * coordinatesPerPoint );
333 auto fourthCubicPoint =
getPoint( aPoints + 3 * coordinatesPerPoint );
335 auto firstQuadraticPoint =
getPointInLine( firstCubicPoint, secondCubicPoint, aStep );
336 auto secondQuadraticPoint =
getPointInLine( secondCubicPoint, thirdCubicPoint, aStep );
337 auto thirdQuadraticPoint =
getPointInLine( thirdCubicPoint, fourthCubicPoint, aStep );
339 auto firstLinearPoint =
getPointInLine( firstQuadraticPoint, secondQuadraticPoint, aStep );
340 auto secondLinearPoint =
getPointInLine( secondQuadraticPoint, thirdQuadraticPoint, aStep );
342 return getPointInLine( firstLinearPoint, secondLinearPoint, aStep );
349 return aLineStart + ( aLineEnd - aLineStart ) * aDistance;
355 using comparatorFunction =
const float&(*)(
const float&,
const float& );
357 auto minimumComparator =
static_cast< comparatorFunction
>( &std::min );
358 auto maximumComparator =
static_cast< comparatorFunction
>( &std::max );
362 VECTOR2D boundingBoxDimensions = maximum - minimum;
364 return 0.001 * std::max( boundingBoxDimensions.
x, boundingBoxDimensions.
y );
369 std::function<
const float&(
const float&,
const float& ) > comparator )
371 float x = aCurvePoints[0];
372 float y = aCurvePoints[1];
374 for(
int pointIndex = 1; pointIndex < 3; ++pointIndex )
376 x = comparator( x, aCurvePoints[ 2 * pointIndex ] );
377 y = comparator( y, aCurvePoints[ 2 * pointIndex + 1 ] );
385 float aStep,
const float* aCurvePoints,
386 float aSegmentationThreshold,
387 std::vector< VECTOR2D >& aGeneratedPoints )
392 if( distanceToPreviousSegment > aSegmentationThreshold )
395 aSegmentationThreshold, aGeneratedPoints );
401 const VECTOR2D& aEnd,
float aOffset,
float aStep,
402 const float* aCurvePoints,
float aSegmentationThreshold,
403 std::vector< VECTOR2D >& aGeneratedPoints )
405 float newStep = aStep / 2.f;
406 float offsetAfterMiddle = aOffset + aStep;
408 segmentBezierCurve( aStart, aMiddle, aOffset, newStep, aCurvePoints, aSegmentationThreshold,
411 aGeneratedPoints.push_back( aMiddle );
414 aSegmentationThreshold, aGeneratedPoints );
421 auto lineDirection = aLineEnd - aLineStart;
423 auto lineStartToPoint = aPoint - aLineStart;
425 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 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 DrawCubicBezierPath(const float *aPoints, int aNumPoints, std::vector< VECTOR2D > &aGeneratedPoints)
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.
void DrawCubicBezierCurve(const float *aPoints, std::vector< VECTOR2D > &aGeneratedPoints)
virtual double GetImageHeight() const override
Return image height from original imported file.
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 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)
VECTOR2< double > VECTOR2D