@@ -163,6 +163,7 @@ class PathNanRemover : protected EmbeddedQueue<4>
163163 VertexSource *m_source;
164164 bool m_remove_nans;
165165 bool m_has_curves;
166+ bool valid_segment_exists;
166167
167168 public:
168169 /* has_curves should be true if the path contains bezier curve
@@ -172,7 +173,9 @@ class PathNanRemover : protected EmbeddedQueue<4>
172173 PathNanRemover (VertexSource &source, bool remove_nans, bool has_curves)
173174 : m_source(&source), m_remove_nans(remove_nans), m_has_curves(has_curves)
174175 {
175- // empty
176+ // ignore all close/end_poly commands until after the first valid
177+ // (nan-free) command is encountered
178+ valid_segment_exists = false ;
176179 }
177180
178181 inline void rewind (unsigned path_id)
@@ -202,8 +205,13 @@ class PathNanRemover : protected EmbeddedQueue<4>
202205 are found along the way, the queue is emptied, and
203206 the next curve segment is handled. */
204207 code = m_source->vertex (x, y);
208+ /* The vertices attached to STOP and CLOSEPOLY left are never
209+ used, so we leave them as-is even if NaN. However, CLOSEPOLY
210+ only makes sense if a valid MOVETO command has already been
211+ emitted. */
205212 if (code == agg::path_cmd_stop ||
206- code == (agg::path_cmd_end_poly | agg::path_flags_close)) {
213+ (code == (agg::path_cmd_end_poly | agg::path_flags_close) &&
214+ valid_segment_exists)) {
207215 return code;
208216 }
209217
@@ -224,6 +232,7 @@ class PathNanRemover : protected EmbeddedQueue<4>
224232 }
225233
226234 if (!has_nan) {
235+ valid_segment_exists = true ;
227236 break ;
228237 }
229238
@@ -251,21 +260,23 @@ class PathNanRemover : protected EmbeddedQueue<4>
251260 code = m_source->vertex (x, y);
252261
253262 if (code == agg::path_cmd_stop ||
254- code == (agg::path_cmd_end_poly | agg::path_flags_close)) {
263+ (code == (agg::path_cmd_end_poly | agg::path_flags_close) &&
264+ valid_segment_exists)) {
255265 return code;
256266 }
257267
258268 if (!(std::isfinite (*x) && std::isfinite (*y))) {
259269 do {
260270 code = m_source->vertex (x, y);
261271 if (code == agg::path_cmd_stop ||
262- code == (agg::path_cmd_end_poly | agg::path_flags_close)) {
272+ (code == (agg::path_cmd_end_poly | agg::path_flags_close) &&
273+ valid_segment_exists)) {
263274 return code;
264275 }
265276 } while (!(std::isfinite (*x) && std::isfinite (*y)));
266277 return agg::path_cmd_move_to;
267278 }
268-
279+ valid_segment_exists = true ;
269280 return code;
270281 }
271282 }
0 commit comments