VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtMilStdSvgPathParser.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2026 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
6 #pragma once
7 
8 // SVG path command reference
9 // ==========================
10 // Each command is a single letter followed by numeric parameters.
11 // UPPERCASE = absolute coordinates. lowercase = relative offset from the
12 // current pen position.
13 //
14 // M x,y MoveTo (absolute) - lift pen, move to (x,y). Starts a new
15 // subpath. Subsequent coordinate pairs without a new letter
16 // are treated as implicit LineTo commands.
17 //
18 // m dx,dy MoveTo (relative) - same but offset from current position.
19 //
20 // L x,y LineTo (absolute) - draw straight line from current point
21 // to (x,y). Multiple coordinate pairs chain as further LineTos.
22 //
23 // l dx,dy LineTo (relative) - same but offset from current position.
24 //
25 // C x1,y1 x2,y2 x,y
26 // CurveTo (absolute) - cubic Bezier curve. (x1,y1) and
27 // (x2,y2) are the two control points; (x,y) is the end point.
28 // Multiple sets of 6 values chain as further cubic segments.
29 //
30 // c dx1,dy1 dx2,dy2 dx,dy
31 // CurveTo (relative) - same but all three pairs are offsets
32 // from the current position.
33 //
34 // Z (or z) ClosePath - draw a straight line back to the start of the
35 // current subpath and close it. Case is ignored.
36 //
37 // All JSON d-strings use a 200x200 coordinate space centred at
38 // (100,100). parseSvgPath() outputs coordinates unchanged in that 200-unit
39 // space. Callers scale to the final render size using sc = size / 200.0 and
40 // the centre offset (p - 100.0) * sc.
41 //
42 // Cubic Bezier curves are sampled into straight-line polylines at parse time
43 // (controlled by the bezierSegs parameter) so the caller never has to handle
44 // curve commands at render time.
45 
47 #include <cmath>
48 #include <sstream>
49 #include <string>
50 #include <vector>
51 
52 namespace makVrv
53 {
54  // A single contiguous polyline produced by parseSvgPath().
55  // A new DtParsedPolyline begins on each M/m command and on each Z/z.
57  {
58  std::vector<DtPoint> points;
59  bool closed = false;
60  };
61 
70  inline std::vector<DtParsedPolyline> parseSvgPath(const std::string& d,
71  int bezierSegs = 24)
72  {
73  std::vector<DtParsedPolyline> result;
74  DtParsedPolyline current;
75 
76  // Sample a cubic Bezier (p0:p3, control pts p1,p2) into current polyline.
77  // Coordinates are in raw 200-space throughout.
78  auto sampleCubic = [&](DtPoint p0, DtPoint p1, DtPoint p2, DtPoint p3)
79  {
80  for (int i = 1; i <= bezierSegs; ++i)
81  {
82  double t = static_cast<double>(i) / bezierSegs;
83  double mt = 1.0 - t;
84  double x = mt*mt*mt*p0.x + 3.0*mt*mt*t*p1.x
85  + 3.0*mt*t*t*p2.x + t*t*t*p3.x;
86  double y = mt*mt*mt*p0.y + 3.0*mt*mt*t*p1.y
87  + 3.0*mt*t*t*p2.y + t*t*t*p3.y;
88  current.points.push_back({ x, y });
89  }
90  };
91 
92  // Replace commas with spaces so the stream tokenizer handles both
93  // "M45,150" and "M 45 150" identically.
94  std::string src = d;
95  for (char& c : src)
96  if (c == ',') c = ' ';
97 
98  std::istringstream ss(src);
99  char cmd = 0;
100  double cx = 0.0, cy = 0.0; // current pen position (200-space)
101  double spx = 0.0, spy = 0.0; // start of current subpath (for Z reset)
102 
103  auto readNum = [&](double& v) -> bool {
104  return static_cast<bool>(ss >> v);
105  };
106 
107  while (ss >> std::ws && !ss.eof())
108  {
109  // If next character is a letter it is a new command, otherwise
110  // it is an implicit repeat of the previous command.
111  char peek = static_cast<char>(ss.peek());
112  if (std::isalpha(static_cast<unsigned char>(peek)))
113  {
114  char newCmd;
115  ss >> newCmd;
116 
117  // A new M/m flushes the current polyline.
118  if ((newCmd == 'M' || newCmd == 'm') && !current.points.empty())
119  {
120  result.push_back(current);
121  current = DtParsedPolyline{};
122  }
123  cmd = newCmd;
124  }
125 
126  double x, y;
127  switch (cmd)
128  {
129  // -- Absolute MoveTo ----------------------------------------------
130  case 'M':
131  if (!readNum(x) || !readNum(y)) continue;
132  cx = x; cy = y;
133  spx = cx; spy = cy; // record subpath start
134  current.points.push_back({ cx, cy });
135  cmd = 'L'; // implicit LineTo for subsequent pairs
136  break;
137 
138  // -- Relative MoveTo ----------------------------------------------
139  case 'm':
140  if (!readNum(x) || !readNum(y)) continue;
141  cx += x; cy += y;
142  spx = cx; spy = cy; // record subpath start
143  current.points.push_back({ cx, cy });
144  cmd = 'l';
145  break;
146 
147  // -- Absolute LineTo ----------------------------------------------
148  case 'L':
149  if (!readNum(x) || !readNum(y)) continue;
150  cx = x; cy = y;
151  current.points.push_back({ cx, cy });
152  break;
153 
154  // -- Relative LineTo ----------------------------------------------
155  case 'l':
156  if (!readNum(x) || !readNum(y)) continue;
157  cx += x; cy += y;
158  current.points.push_back({ cx, cy });
159  break;
160 
161  // -- Absolute horizontal LineTo -----------------------------------
162  case 'H':
163  if (!readNum(x)) continue;
164  cx = x;
165  current.points.push_back({ cx, cy });
166  break;
167 
168  // -- Relative horizontal LineTo -----------------------------------
169  case 'h':
170  if (!readNum(x)) continue;
171  cx += x;
172  current.points.push_back({ cx, cy });
173  break;
174 
175  // -- Absolute vertical LineTo -------------------------------------
176  case 'V':
177  if (!readNum(y)) continue;
178  cy = y;
179  current.points.push_back({ cx, cy });
180  break;
181 
182  // -- Relative vertical LineTo -------------------------------------
183  case 'v':
184  if (!readNum(y)) continue;
185  cy += y;
186  current.points.push_back({ cx, cy });
187  break;
188 
189  // -- Absolute cubic Bezier ----------------------------------------
190  case 'C':
191  {
192  double x1, y1, x2, y2, ex, ey;
193  if (!readNum(x1) || !readNum(y1) ||
194  !readNum(x2) || !readNum(y2) ||
195  !readNum(ex) || !readNum(ey)) continue;
196  sampleCubic({ cx, cy }, { x1, y1 }, { x2, y2 }, { ex, ey });
197  cx = ex; cy = ey;
198  break;
199  }
200 
201  // -- Relative cubic Bezier ----------------------------------------
202  case 'c':
203  {
204  double dx1, dy1, dx2, dy2, dx, dy;
205  if (!readNum(dx1) || !readNum(dy1) ||
206  !readNum(dx2) || !readNum(dy2) ||
207  !readNum(dx) || !readNum(dy)) continue;
208  sampleCubic({ cx, cy },
209  { cx+dx1, cy+dy1 },
210  { cx+dx2, cy+dy2 },
211  { cx+dx, cy+dy });
212  cx += dx; cy += dy;
213  break;
214  }
215 
216  // -- ClosePath ----------------------------------------------------
217  case 'Z': case 'z':
218  current.closed = true;
219  result.push_back(current);
220  current = DtParsedPolyline{};
221  cx = spx; cy = spy; // SVG spec: pen resets to subpath start after Z
222  break;
223 
224  default:
225  ss.ignore();
226  break;
227  }
228  }
229 
230  if (!current.points.empty())
231  result.push_back(current);
232 
233  return result;
234  }
235 
236 } // namespace makVrv
Definition: DtMilStdTypes.h:205
double x
Definition: DtMilStdTypes.h:207
bool closed
True when the sub-path ended with Z/z.
Definition: DtMilStdSvgPathParser.h:59
std::vector< DtParsedPolyline > parseSvgPath(const std::string &d, int bezierSegs=24)
Parse a JSON SVG path d-string into a list of polylines.
Definition: DtMilStdSvgPathParser.h:70
Definition: DtMilStdSvgPathParser.h:56
double y
Definition: DtMilStdTypes.h:208
std::vector< DtPoint > points
Definition: DtMilStdSvgPathParser.h:58

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)