FileImporter.cs 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. namespace SketchAssistant
  10. {
  11. public class FileImporter
  12. {
  13. /// <summary>
  14. /// scale factor for coordinates of svg file
  15. /// </summary>
  16. double scale;
  17. /// <summary>
  18. /// line pointer for the current svg document
  19. /// </summary>
  20. int i;
  21. /// <summary>
  22. /// array containing all characters interpreted as whitespaces which seperate words/tokens in the input file
  23. /// </summary>
  24. readonly char[] whitespaces = new char[] { ' ' , ',' };
  25. /// <summary>
  26. /// number of points to create along the outline of an ellipse, divisible by 4
  27. /// </summary>
  28. readonly int samplingRateEllipse = 12;
  29. /// <summary>
  30. /// number of points to create on a bezier curve, including start and end point (even numbr will result in "flat" bezier curves, uneven number in "pointed" ones
  31. /// </summary>
  32. readonly int samplingRateBezier = 101;
  33. public FileImporter()
  34. {
  35. }
  36. /// <summary>
  37. /// parses a drawing consisting of line objects, given as a file in the application specific .isad format
  38. /// </summary>
  39. /// <param name="fileName">the path of the input file</param>
  40. /// <returns>the width and height of the left canvas and the parsed picture as a list of lines</returns>
  41. public (int, int, List<Line>) ParseISADInputFile(String fileName)
  42. {
  43. return ParseISADInput(System.IO.File.ReadAllLines(fileName));
  44. }
  45. /// <summary>
  46. /// parses a drawing consisting of line objects, given as the content of a .isad file, seperated into lines
  47. /// </summary>
  48. /// <param name="allLines">an array holding all lines of the input file</param>
  49. /// <returns>the width and height of the left canvas and the parsed picture as a list of lines</returns>
  50. private (int, int, List<Line>) ParseISADInput(String[] allLines)
  51. {
  52. if (allLines.Length == 0)
  53. {
  54. throw new FileImporterException("file is empty", "", -1);
  55. }
  56. if (!"drawing".Equals(allLines[0]))
  57. {
  58. throw new FileImporterException("file is not an interactive sketch assistant drawing", ".isad files have to start with the 'drawing' token", 1);
  59. }
  60. if (!"enddrawing".Equals(allLines[allLines.Length - 1]))
  61. {
  62. throw new FileImporterException("unterminated drawing definition", ".isad files have to end with the 'enddrawing' token", allLines.Length);
  63. }
  64. (int, int) dimensions = ParseISADHeader(allLines);
  65. List<Line> picture = ParseISADBody(allLines, dimensions.Item1, dimensions.Item2);
  66. return (dimensions.Item1, dimensions.Item2, picture);
  67. }
  68. /// <summary>
  69. /// parses the first two lines of an input file in .isad format
  70. /// </summary>
  71. /// <param name="allLines">the input file as an array of lines</param>
  72. /// <returns>the width and height of the left canvas</returns>
  73. private (int, int) ParseISADHeader(String[] allLines)
  74. {
  75. int width;
  76. int height;
  77. if (!(allLines.Length > 1) || !Regex.Match(allLines[1], @"^\d+x?\d+$", RegexOptions.None).Success)
  78. {
  79. throw new FileImporterException("invalid or missing canvas size definition", "format: [width]x[heigth]", 2);
  80. }
  81. String[] size = allLines[1].Split('x');
  82. width = Convert.ToInt32(size[0]);
  83. height = Convert.ToInt32(size[1]);
  84. return (width, height);
  85. }
  86. /// <summary>
  87. /// parses all line entries of an input file in .isad format
  88. /// </summary>
  89. /// <param name="allLines">the input file as an array of lines</param>
  90. /// <returns>the parsed picture as a list of lines</returns>
  91. private List<Line> ParseISADBody(String[] allLines, int width, int height)
  92. {
  93. String lineStartString = "line";
  94. String lineEndString = "endline";
  95. List<Line> drawing = new List<Line>();
  96. //number of the line currently being parsed, enumeration starting at 0, body starts at the third line, therefore lin number 2
  97. int i = 2;
  98. //parse 'line' token and complete line definition
  99. int lineStartPointer = i;
  100. //holds the line number of the next expected beginning of a line definition, or of the enddrawing token
  101. while (lineStartString.Equals(allLines[i]))
  102. {
  103. //start parsing next line
  104. i++;
  105. List<Point> newLine = new List<Point>();
  106. while (!lineEndString.Equals(allLines[i]))
  107. {
  108. if (i == allLines.Length)
  109. {
  110. throw new FileImporterException("unterminated line definition", null, (i + 1));
  111. }
  112. //parse single point definition
  113. if (!Regex.Match(allLines[i], @"^\d+;\d+$", RegexOptions.None).Success)
  114. {
  115. throw new FileImporterException("invalid Point definition: wrong format", "format: [xCoordinate];[yCoordinate]", (i + 1) );
  116. }
  117. String[] coordinates = allLines[i].Split(';');
  118. //no errors possible, convertability to int already checked above
  119. int xCoordinate = Convert.ToInt32(coordinates[0]);
  120. int yCoordinate = Convert.ToInt32(coordinates[1]);
  121. if (xCoordinate < 0 || yCoordinate < 0 || xCoordinate > width - 1 || yCoordinate > height - 1)
  122. {
  123. throw new FileImporterException("invalid Point definition: point out of bounds", null, (i + 1) );
  124. }
  125. newLine.Add(new Point(xCoordinate, yCoordinate));
  126. //start parsing next line
  127. i++;
  128. }
  129. //"parse" 'endline' token, syntax already checked at the beginning, and start parsing next line
  130. i++;
  131. //add line to drawing
  132. drawing.Add(new Line(newLine));
  133. //update lineStartPointer to the presumable start of the next line
  134. lineStartPointer = i;
  135. }
  136. //check if end of body is reached after there are no more line definitions
  137. if(i != allLines.Length - 1)
  138. {
  139. throw new FileImporterException("missing or invalid line definition token", "line definitions start with the 'line' token", (i + 1));
  140. }
  141. //return parsed picture
  142. return drawing;
  143. }
  144. /// <summary>
  145. /// connection point for testing use only: calls ParseISADInput(String[] allLines) and directly passes the given argument (effectively bypassing the File Input functionality)
  146. /// </summary>
  147. /// <param name="allLines">an array holding all lines of the input file</param>
  148. /// <returns>the width and height of the left canvas and the parsed picture as a list of lines</returns>
  149. public (int, int, List<Line>) ParseISADInputForTesting(String[] allLines)
  150. {
  151. return ParseISADInput(allLines);
  152. }
  153. /// <summary>
  154. /// parses a svg drawing, given as a .svg file
  155. /// </summary>
  156. /// <param name="fileName">the path of the input file</param>
  157. /// <returns>the width and height of the left canvas and the parsed picture as a list of lines</returns>
  158. public (int, int, List<Line>) ParseSVGInputFile(String fileName, int windowWidth, int windowHeight)
  159. {
  160. return ParseSVGInput(System.IO.File.ReadAllLines(fileName), windowWidth, windowHeight);
  161. }
  162. /// <summary>
  163. /// parses a svg drawing, given as the content of a .svg file, seperated into lines
  164. /// </summary>
  165. /// <param name="allLines">an array holding all lines of the input file</param>
  166. /// <returns>the width and height of the left canvas and the parsed picture as a list of lines</returns>
  167. private (int, int, List<Line>) ParseSVGInput(String[] allLines, double windowWidth, double windowHeight)
  168. {
  169. i = 0; //reset line pointer
  170. if (allLines.Length == 0) //check for empty file
  171. {
  172. throw new FileImporterException("file is empty", "", -1);
  173. }
  174. (int, int) sizedef = ParseSVGHeader(allLines); //parse svg file header and get internal coordinate range
  175. i++;
  176. int width; //width of the resulting picture in pixels
  177. int height; //height of the resulting picture in pixels
  178. if (windowWidth / windowHeight > sizedef.Item1 / sizedef.Item2) //height dominant, width has to be smaller than drawing window to preserve xy-scale
  179. {
  180. scale = windowHeight / sizedef.Item2;
  181. Console.WriteLine("scale: (heights) " + windowHeight + "/" + sizedef.Item2);
  182. Console.WriteLine("widths: " + windowWidth + "/" + sizedef.Item1);
  183. height = (int)Math.Round(windowHeight);
  184. width = (int) Math.Round(scale * sizedef.Item1);
  185. Console.WriteLine(width + "x" + height + " (" + scale + ")");
  186. }
  187. else //width dominant, height has to be smaller than drawing window to preserve xy-scale
  188. {
  189. scale = windowWidth / sizedef.Item1;
  190. Console.WriteLine("scale: (widths) " + windowWidth + "/" + sizedef.Item1);
  191. Console.WriteLine("heights: " + windowHeight + "/" + sizedef.Item2);
  192. width = (int)Math.Round(windowWidth);
  193. height = (int)Math.Round(scale * sizedef.Item2);
  194. Console.WriteLine(width + "x" + height + " (" + scale + ")");
  195. }
  196. for(int j=0; j < allLines.Length; j++)
  197. {
  198. allLines[j] = allLines[j].Trim(whitespaces);
  199. }
  200. List<Line> picture = ParseSVGBody(allLines); //parse whole svg drawing into list of lines
  201. return (width, height, picture);
  202. }
  203. /// <summary>
  204. /// parses the svg file header and returns the internal coordinate range of this drawing, and iterates i to point to the start of svg element definitions
  205. /// </summary>
  206. /// <param name="allLines">an array holding all lines of the input file</param>
  207. /// <returns>the internal coordinate range of this drawing</returns>
  208. private (int, int) ParseSVGHeader(String[] allLines)
  209. {
  210. while (!allLines[i].StartsWith("<svg")) //skip non-relevant metadata at start of svg file
  211. {
  212. i++;
  213. }
  214. String[] currentLine = allLines[i].Split(' ');
  215. int width= -1;
  216. int height= -1;
  217. for(int j= 0; j < currentLine.Length; j++)
  218. {
  219. if (currentLine[j].StartsWith("width"))
  220. {
  221. width = Convert.ToInt32(ParseSingleSVGAttribute(currentLine[j]));
  222. }
  223. else if (currentLine[j].StartsWith("height"))
  224. {
  225. height = Convert.ToInt32(ParseSingleSVGAttribute(currentLine[j]));
  226. }
  227. }
  228. if(width == -1)
  229. {
  230. throw new FileImporterException("missing width definition in SVG header", "the header should contain the \"width=...\" attribute", i+1);
  231. }
  232. if (height == -1)
  233. {
  234. throw new FileImporterException("missing height definition in SVG header", "the header should contain the \"height=...\" attribute", i + 1);
  235. }
  236. return (width, height);
  237. }
  238. /// <summary>
  239. /// parses all relevant svg element definitions and skips the ones not representable by the sketch assistant
  240. /// </summary>
  241. /// <param name="allLines">an array holding all lines of the input file</param>
  242. /// <returns>the parsed picture as a list of lines</returns>
  243. private List<Line> ParseSVGBody(String[] allLines)
  244. {
  245. List<Line> picture = new List<Line>();
  246. while (!allLines[i].StartsWith("</svg"))
  247. {
  248. List<Line> element = ParseSingleSVGElement(allLines);
  249. if (element != null)
  250. {
  251. picture.AddRange(element);
  252. }
  253. i++;
  254. }
  255. return picture;
  256. }
  257. /// <summary>
  258. /// parses one toplevel svg element
  259. /// </summary>
  260. /// <param name="allLines">an array holding all lines of the input file</param>
  261. /// <returns>the parsed Element as a list of lines</returns>
  262. private List<Line> ParseSingleSVGElement(string[] allLines)
  263. {
  264. String[] currentElement = GetCurrentElement(allLines);
  265. if (currentElement[currentElement.Length - 1].EndsWith("/>")) //single line element
  266. {
  267. return ParseSingleLineSVGElement(currentElement);
  268. }
  269. else //element containing sub-elements
  270. {
  271. return ParseMultiLineSVGElement(currentElement, allLines);
  272. }
  273. }
  274. /// <summary>
  275. /// parses a single toplevel svg element only taking one line
  276. /// </summary>
  277. /// <param name="allLines">an array holding all lines of the input file</param>
  278. /// <returns>the parsed element as a Line object, or null if the element is not supported</returns>
  279. private List<Line> ParseSingleLineSVGElement(string[] currentElement)
  280. {
  281. List<Point> points= null;
  282. List<Line> element = null;
  283. switch (currentElement[0])
  284. {
  285. case "<rect":
  286. points = parseRect(currentElement);
  287. break;
  288. case "<circle":
  289. points = parseCircle(currentElement);
  290. break;
  291. case "<ellipse":
  292. points = parseEllipse(currentElement);
  293. break;
  294. case "<line":
  295. points = parseLine(currentElement);
  296. break;
  297. case "<polyline":
  298. points = parsePolyline(currentElement);
  299. break;
  300. case "<polygon":
  301. points = parsePolygon(currentElement);
  302. break;
  303. case "<path":
  304. element = parsePath(currentElement);
  305. break;
  306. default: //unsupported svg element
  307. Console.WriteLine("unsupported element: " + currentElement[0] + currentElement[0].Length);
  308. return null;
  309. }
  310. if (element == null)
  311. {
  312. element = new List<Line>();
  313. element.Add(new Line(points));
  314. }
  315. return element;
  316. }
  317. /// <summary>
  318. /// parses a rectangle definition into a List of Points representing a single line around the rectangle (in clockwise direction)
  319. /// </summary>
  320. /// <param name="currentElement">the definition of the element as whitespace seperated String[]</param>
  321. /// <returns>the parsed element as a List of Points</returns>
  322. private List<Point> parseRect(string[] currentElement)
  323. {
  324. double x = 0;
  325. double y = 0;
  326. double w = 0;
  327. double h = 0;
  328. double rx = 0;
  329. double ry = 0;
  330. for(int j= 0; j < currentElement.Length; j++)
  331. {
  332. if (currentElement[j].StartsWith("x="))
  333. {
  334. x = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  335. }
  336. else if (currentElement[j].StartsWith("y="))
  337. {
  338. y = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  339. }
  340. else if (currentElement[j].StartsWith("width="))
  341. {
  342. w = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  343. }
  344. else if (currentElement[j].StartsWith("height="))
  345. {
  346. h = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  347. }
  348. else if (currentElement[j].StartsWith("rx="))
  349. {
  350. rx = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  351. }
  352. else if (currentElement[j].StartsWith("ry="))
  353. {
  354. ry = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  355. }
  356. }
  357. List<Point> rect = new List<Point>();
  358. rect.Add(ScaleAndCreatePoint(x, y));
  359. rect.Add(ScaleAndCreatePoint(x + w, y));
  360. rect.Add(ScaleAndCreatePoint(x + w, y + h));
  361. rect.Add(ScaleAndCreatePoint(x, y + h));
  362. rect.Add(ScaleAndCreatePoint(x, y));
  363. Console.WriteLine("parsed point: " + x + ";" + y);
  364. return rect;
  365. }
  366. /// <summary>
  367. /// parses a circle definition into a List of Points
  368. /// </summary>
  369. /// <param name="currentElement">the definition of the element as whitespace seperated String[]</param>
  370. /// <returns>the parsed element as a List of Points</returns>
  371. private List<Point> parseCircle(string[] currentElement)
  372. {
  373. double x = 0;
  374. double y = 0;
  375. double r = 0;
  376. for (int j = 0; j < currentElement.Length; j++)
  377. {
  378. if (currentElement[j].StartsWith("cx="))
  379. {
  380. x = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  381. }
  382. else if (currentElement[j].StartsWith("cy="))
  383. {
  384. y = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  385. }
  386. else if (currentElement[j].StartsWith("r="))
  387. {
  388. r = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  389. }
  390. }
  391. return SampleEllipse(x, y, r, r);
  392. }
  393. /// <summary>
  394. /// parses a ellipse definition into a List of Points
  395. /// </summary>
  396. /// <param name="currentElement">the definition of the element as whitespace seperated String[]</param>
  397. /// <returns>the parsed element as a List of Points</returns>
  398. private List<Point> parseEllipse(string[] currentElement)
  399. {
  400. double x = 0;
  401. double y = 0;
  402. double rx = 0;
  403. double ry = 0;
  404. for (int j = 0; j < currentElement.Length; j++)
  405. {
  406. if (currentElement[j].StartsWith("cx="))
  407. {
  408. x = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  409. }
  410. else if (currentElement[j].StartsWith("cy="))
  411. {
  412. y = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  413. }
  414. else if (currentElement[j].StartsWith("rx="))
  415. {
  416. rx = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  417. }
  418. else if (currentElement[j].StartsWith("ry="))
  419. {
  420. ry = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  421. }
  422. }
  423. return SampleEllipse(x, y, rx, ry);
  424. }
  425. /// <summary>
  426. /// parses a line definition into a List of two Points
  427. /// </summary>
  428. /// <param name="currentElement">the definition of the element as whitespace seperated String[]</param>
  429. /// <returns>the parsed element as a List of Points</returns>
  430. private List<Point> parseLine(string[] currentElement)
  431. {
  432. double x1 = 0;
  433. double y1 = 0;
  434. double x2 = 0;
  435. double y2 = 0;
  436. for (int j = 0; j < currentElement.Length; j++)
  437. {
  438. if (currentElement[j].StartsWith("x1="))
  439. {
  440. x1 = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  441. }
  442. else if (currentElement[j].StartsWith("y1="))
  443. {
  444. y1 = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  445. }
  446. else if (currentElement[j].StartsWith("x2="))
  447. {
  448. x2 = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  449. }
  450. else if (currentElement[j].StartsWith("y2="))
  451. {
  452. y2 = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
  453. }
  454. }
  455. List<Point> line = new List<Point>();
  456. line.Add(ScaleAndCreatePoint(x1, y1));
  457. line.Add(ScaleAndCreatePoint(x2, y2));
  458. return line;
  459. }
  460. /// <summary>
  461. /// parses a polyline definition into a List of Points
  462. /// </summary>
  463. /// <param name="currentElement">the definition of the element as whitespace seperated String[]</param>
  464. /// <returns>the parsed element as a List of Points</returns>
  465. private List<Point> parsePolyline(string[] currentElement)
  466. {
  467. String[] points = null;
  468. for (int j = 0; j < currentElement.Length; j++)
  469. {
  470. if (currentElement[j].StartsWith("points="))
  471. {
  472. List<String> pointDefs = new List<string>();
  473. pointDefs.Add(currentElement[j].Substring(8)); //parse first point coordinates by removing 'points="'
  474. j++;
  475. while (!currentElement[j].EndsWith("\""))
  476. {
  477. pointDefs.Add(currentElement[j]); //parse intermediate point coordinates
  478. j++;
  479. }
  480. pointDefs.Add(currentElement[j].Trim('"')); //parse last point coordinates by removing '"'
  481. points = pointDefs.ToArray();
  482. }
  483. }
  484. List<Point> polyline = new List<Point>();
  485. for (int k = 0; k < points.Length - 1; k += 2)
  486. {
  487. polyline.Add(ScaleAndCreatePoint(Convert.ToDouble(points[k], CultureInfo.InvariantCulture), Convert.ToDouble(points[k + 1], CultureInfo.InvariantCulture)));
  488. }
  489. return polyline;
  490. }
  491. /// <summary>
  492. /// parses a polygon definition into a List of Points
  493. /// </summary>
  494. /// <param name="currentElement">the definition of the element as whitespace seperated String[]</param>
  495. /// <returns>the parsed element as a List of Points</returns>
  496. private List<Point> parsePolygon(string[] currentElement)
  497. {
  498. String[] points = null;
  499. for (int j = 0; j < currentElement.Length; j++)
  500. {
  501. if (currentElement[j].StartsWith("points="))
  502. {
  503. List<String> pointDefs = new List<string>();
  504. pointDefs.Add(currentElement[j].Substring(8)); //parse first point coordinates by removing 'points="'
  505. j++;
  506. while (!currentElement[j].EndsWith("\""))
  507. {
  508. pointDefs.Add(currentElement[j]); //parse intermediate point coordinates
  509. j++;
  510. }
  511. pointDefs.Add(currentElement[j].Trim('"')); //parse last point coordinates by removing '"'
  512. points = pointDefs.ToArray();
  513. }
  514. }
  515. List<Point> polygon = new List<Point>();
  516. for (int k = 0; k < points.Length - 1; k+=2)
  517. {
  518. polygon.Add(ScaleAndCreatePoint(Convert.ToDouble(points[k], CultureInfo.InvariantCulture), Convert.ToDouble(points[k+1], CultureInfo.InvariantCulture)));
  519. Console.WriteLine("parsed point: " + points[k] + ";" + points[k + 1]);
  520. }
  521. polygon.Add(ScaleAndCreatePoint(Convert.ToDouble(points[0], CultureInfo.InvariantCulture), Convert.ToDouble(points[1], CultureInfo.InvariantCulture))); //close polygon
  522. Console.WriteLine("parsed point: " + points[0] + ";" + points[1]);
  523. return polygon;
  524. }
  525. /// <summary>
  526. /// parses a path definition into a List of Points
  527. /// </summary>
  528. /// <param name="currentElement">the definition of the element as whitespace seperated String[]</param>
  529. /// <returns>the parsed element as a List of Points</returns>
  530. private List<Line> parsePath(string[] currentElement)
  531. {
  532. List<String> pathElements = new List<string>();
  533. for (int j = 0; j < currentElement.Length; j++)
  534. {
  535. if (currentElement[j].StartsWith("d="))
  536. {
  537. pathElements.Add(currentElement[j].Substring(3)); //parse first path element by removing 'd="'
  538. j++;
  539. while (!currentElement[j].EndsWith("\""))
  540. {
  541. pathElements.Add(currentElement[j]); //parse intermediate path element
  542. j++;
  543. }
  544. pathElements.Add(currentElement[j].Trim('"')); //parse last path element by removing '"'
  545. }
  546. }
  547. normalizePathDeclaration(pathElements); //expand path data to always explicitly have the command descriptor in front of the appropriate number of arguments and to seperate command descriptors, coordinates and other tokens always into seperate list elements (equivalent to seperation with spaces in the input file, but svg allows also for comma as a seperator, and for omitting seperators where possible without losing information (refer to svg grammer) to reduce file size
  548. foreach(String s in pathElements)
  549. {
  550. Console.WriteLine(s);
  551. }
  552. List<Line> element = new List<Line>();
  553. List<Point> currentLine = new List<Point>();
  554. double lastBezierControlPointX= 0;
  555. double lastBezierControlPointY= 0;
  556. double lastPositionX;
  557. double lastPositionY;
  558. double initialPositionX= -1;
  559. double initialPositionY= -1;
  560. bool newSubpath = true;
  561. //assume that svg is well formatted with spaces between each token, no emitted characters and only "[char] (appropriateNumber*[coordinate])" segments
  562. //pathElements = PreparePathElements(pathElements); //split pathElement list objects until every object is atomar (single character or single number (coordinate))
  563. //int k = 0; //index of active element in pathElements is always 0
  564. (List<Point>, double, double) valuesArc; //list of points, new values for: lastPositionX, lastPositionY
  565. (List<Point>, double, double, double, double) valuesBezierCurve; //list of points, new values for: lastPositionX, lastPositionY, lastBezierControlPointX, lastBezierControlPointY
  566. (Point, double, double) valuesSinglePoint = parse_M_L(pathElements); //new point, new values for: lastPositionX, lastPositionY
  567. currentLine = new List<Point>();
  568. currentLine.Add(valuesSinglePoint.Item1);
  569. lastPositionX = valuesSinglePoint.Item2;
  570. lastPositionY = valuesSinglePoint.Item3;
  571. String currentToken;
  572. while (!(pathElements.Count == 0)){
  573. if (newSubpath)
  574. {
  575. initialPositionX = lastPositionX; //update buffers for coordinates of first point of active subpath
  576. initialPositionY = lastPositionY;
  577. newSubpath = false;
  578. }
  579. currentToken = pathElements.First();
  580. if (currentToken.Equals("M"))
  581. {
  582. element.Add(new Line(currentLine)); //save current line
  583. valuesSinglePoint = parse_M_L(pathElements);
  584. currentLine = new List<Point>(); //create new empty line
  585. currentLine.Add(valuesSinglePoint.Item1); //add point to new line
  586. lastPositionX = valuesSinglePoint.Item2; //save last point coordinates
  587. lastPositionY = valuesSinglePoint.Item3; //save last point coordinates
  588. }
  589. else if (currentToken.Equals("m"))
  590. {
  591. element.Add(new Line(currentLine)); //save current line
  592. valuesSinglePoint = parse_m_l(pathElements, lastPositionX, lastPositionY);
  593. currentLine = new List<Point>(); //create new empty line
  594. currentLine.Add(valuesSinglePoint.Item1); //add point to new line
  595. lastPositionX = valuesSinglePoint.Item2; //save last point coordinates
  596. lastPositionY = valuesSinglePoint.Item3; //save last point coordinates
  597. }
  598. else if (currentToken.Equals("Z") || currentToken.Equals("z"))
  599. {
  600. valuesSinglePoint = parse_Z(pathElements, initialPositionX, initialPositionY); //method call only used for uniform program structure... only real effect of method is to consume one token
  601. newSubpath = true;
  602. currentLine.Add(valuesSinglePoint.Item1); //add point to old line
  603. element.Add(new Line(currentLine)); //save current line
  604. currentLine = new List<Point>(); //create new empty line
  605. currentLine.Add(valuesSinglePoint.Item1); //add point to new line
  606. lastPositionX = valuesSinglePoint.Item2; //save last point coordinates
  607. lastPositionY = valuesSinglePoint.Item3; //save last point coordinates
  608. }
  609. else if (currentToken.Equals("L"))
  610. {
  611. valuesSinglePoint = parse_M_L(pathElements);
  612. currentLine.Add(valuesSinglePoint.Item1); //add point to new line
  613. lastPositionX = valuesSinglePoint.Item2; //save last point coordinates
  614. lastPositionY = valuesSinglePoint.Item3; //save last point coordinates
  615. }
  616. else if (currentToken.Equals("l"))
  617. {
  618. valuesSinglePoint = parse_m_l(pathElements, lastPositionX, lastPositionY);
  619. currentLine.Add(valuesSinglePoint.Item1); //add point to new line
  620. lastPositionX = valuesSinglePoint.Item2; //save last point coordinates
  621. lastPositionY = valuesSinglePoint.Item3; //save last point coordinates
  622. }
  623. else if (currentToken.Equals("H"))
  624. {
  625. valuesSinglePoint = parse_H(pathElements, lastPositionY);
  626. currentLine.Add(valuesSinglePoint.Item1); //add point to new line
  627. lastPositionX = valuesSinglePoint.Item2; //save last point coordinates
  628. lastPositionY = valuesSinglePoint.Item3; //save last point coordinates
  629. }
  630. else if (currentToken.Equals("h"))
  631. {
  632. valuesSinglePoint = parse_h(pathElements, lastPositionX, lastPositionY);
  633. currentLine.Add(valuesSinglePoint.Item1); //add point to new line
  634. lastPositionX = valuesSinglePoint.Item2; //save last point coordinates
  635. lastPositionY = valuesSinglePoint.Item3; //save last point coordinates
  636. }
  637. else if (currentToken.Equals("V"))
  638. {
  639. valuesSinglePoint = parse_V(pathElements, lastPositionX);
  640. currentLine.Add(valuesSinglePoint.Item1); //add point to new line
  641. lastPositionX = valuesSinglePoint.Item2; //save last point coordinates
  642. lastPositionY = valuesSinglePoint.Item3; //save last point coordinates
  643. }
  644. else if (currentToken.Equals("v"))
  645. {
  646. valuesSinglePoint = parse_v(pathElements, lastPositionX, lastPositionY);
  647. currentLine.Add(valuesSinglePoint.Item1); //add point to new line
  648. lastPositionX = valuesSinglePoint.Item2; //save last point coordinates
  649. lastPositionY = valuesSinglePoint.Item3; //save last point coordinates
  650. }
  651. else if (currentToken.Equals("C"))
  652. {
  653. valuesBezierCurve = parse_C(pathElements, lastPositionX, lastPositionY);
  654. currentLine.AddRange(valuesBezierCurve.Item1); //add point to new line
  655. lastPositionX = valuesBezierCurve.Item2; //save last point coordinates
  656. lastPositionY = valuesBezierCurve.Item3; //save last point coordinates
  657. lastBezierControlPointX = valuesBezierCurve.Item4; //save last bezier control point coordinates
  658. lastBezierControlPointY = valuesBezierCurve.Item5; //save last bezier control point coordinates
  659. }
  660. else if (currentToken.Equals("c"))
  661. {
  662. valuesBezierCurve = parse_C(pathElements, lastPositionX, lastPositionY);
  663. currentLine.AddRange(valuesBezierCurve.Item1); //add point to new line
  664. lastPositionX = valuesBezierCurve.Item2; //save last point coordinates
  665. lastPositionY = valuesBezierCurve.Item3; //save last point coordinates
  666. lastBezierControlPointX = valuesBezierCurve.Item4; //save last bezier control point coordinates
  667. lastBezierControlPointY = valuesBezierCurve.Item5; //save last bezier control point coordinates
  668. }
  669. else if (currentToken.Equals("S"))
  670. {
  671. valuesBezierCurve = parse_S(pathElements, lastPositionX, lastPositionY, lastBezierControlPointX, lastBezierControlPointY);
  672. currentLine.AddRange(valuesBezierCurve.Item1); //add point to new line
  673. lastPositionX = valuesBezierCurve.Item2; //save last point coordinates
  674. lastPositionY = valuesBezierCurve.Item3; //save last point coordinates
  675. lastBezierControlPointX = valuesBezierCurve.Item4; //save last bezier control point coordinates
  676. lastBezierControlPointY = valuesBezierCurve.Item5; //save last bezier control point coordinates
  677. }
  678. else if (currentToken.Equals("s"))
  679. {
  680. valuesBezierCurve = parse_s(pathElements, lastPositionX, lastPositionY, lastBezierControlPointX, lastBezierControlPointY);
  681. currentLine.AddRange(valuesBezierCurve.Item1); //add point to new line
  682. lastPositionX = valuesBezierCurve.Item2; //save last point coordinates
  683. lastPositionY = valuesBezierCurve.Item3; //save last point coordinates
  684. lastBezierControlPointX = valuesBezierCurve.Item4; //save last bezier control point coordinates
  685. lastBezierControlPointY = valuesBezierCurve.Item5; //save last bezier control point coordinates
  686. }
  687. else if (currentToken.Equals("Q"))
  688. {
  689. valuesBezierCurve = parse_Q(pathElements, lastPositionX, lastPositionY);
  690. currentLine.AddRange(valuesBezierCurve.Item1); //add point to new line
  691. lastPositionX = valuesBezierCurve.Item2; //save last point coordinates
  692. lastPositionY = valuesBezierCurve.Item3; //save last point coordinates
  693. lastBezierControlPointX = valuesBezierCurve.Item4; //save last bezier control point coordinates
  694. lastBezierControlPointY = valuesBezierCurve.Item5; //save last bezier control point coordinates
  695. }
  696. else if (currentToken.Equals("q"))
  697. {
  698. valuesBezierCurve = parse_q(pathElements, lastPositionX, lastPositionY);
  699. currentLine.AddRange(valuesBezierCurve.Item1); //add point to new line
  700. lastPositionX = valuesBezierCurve.Item2; //save last point coordinates
  701. lastPositionY = valuesBezierCurve.Item3; //save last point coordinates
  702. lastBezierControlPointX = valuesBezierCurve.Item4; //save last bezier control point coordinates
  703. lastBezierControlPointY = valuesBezierCurve.Item5; //save last bezier control point coordinates
  704. }
  705. else if (currentToken.Equals("T"))
  706. {
  707. valuesBezierCurve = parse_T(pathElements, lastPositionX, lastPositionY, lastBezierControlPointX, lastBezierControlPointY);
  708. currentLine.AddRange(valuesBezierCurve.Item1); //add point to new line
  709. lastPositionX = valuesBezierCurve.Item2; //save last point coordinates
  710. lastPositionY = valuesBezierCurve.Item3; //save last point coordinates
  711. lastBezierControlPointX = valuesBezierCurve.Item4; //save last bezier control point coordinates
  712. lastBezierControlPointY = valuesBezierCurve.Item5; //save last bezier control point coordinates
  713. }
  714. else if (currentToken.Equals("t"))
  715. {
  716. valuesBezierCurve = parse_t(pathElements, lastPositionX, lastPositionY, lastBezierControlPointX, lastBezierControlPointY);
  717. currentLine.AddRange(valuesBezierCurve.Item1); //add point to new line
  718. lastPositionX = valuesBezierCurve.Item2; //save last point coordinates
  719. lastPositionY = valuesBezierCurve.Item3; //save last point coordinates
  720. lastBezierControlPointX = valuesBezierCurve.Item4; //save last bezier control point coordinates
  721. lastBezierControlPointY = valuesBezierCurve.Item5; //save last bezier control point coordinates
  722. }
  723. else if (currentToken.Equals("A"))
  724. {
  725. valuesArc = parse_A(pathElements, lastPositionX, lastPositionY);
  726. currentLine.AddRange(valuesArc.Item1); //add points to new line
  727. lastPositionX = valuesArc.Item2; //save last point coordinates
  728. lastPositionY = valuesArc.Item3; //save last point coordinates
  729. }
  730. else if (currentToken.Equals("a"))
  731. {
  732. valuesArc = parse_a(pathElements, lastPositionX, lastPositionY);
  733. currentLine.AddRange(valuesArc.Item1); //add points to new line
  734. lastPositionX = valuesArc.Item2; //save last point coordinates
  735. lastPositionY = valuesArc.Item3; //save last point coordinates
  736. }
  737. else
  738. {
  739. throw new FileImporterException("invalid path argument or path data formatting: read argument " + pathElements.First(), "valid path arguments are: {M,Z,L,H,V,C,S,Q,T,A} in upper and lower case", i + 1);
  740. }
  741. }
  742. if (currentLine.Count > 1)
  743. {
  744. element.Add(new Line(currentLine)); //save current line
  745. }
  746. return element;
  747. }
  748. private void normalizePathDeclaration(List<string> pathElements)
  749. {
  750. Char lastCommand = 'M';
  751. int argumentCounter = 0;
  752. for( int j= 0; j < pathElements.Count; j++)
  753. {
  754. String currentElement = pathElements.ElementAt(j);
  755. if (currentElement.Length != 1)
  756. {
  757. if (((currentElement.First() >= 'A' && currentElement.First() <= 'Z') || (currentElement.First() >= 'a' && currentElement.First() <= 'z')) && currentElement.First() != 'e') //seperate a single command descriptor / letter
  758. {
  759. pathElements.RemoveAt(j);
  760. pathElements.Insert(j, currentElement.First() + ""); //insert letter as seperate element
  761. pathElements.Insert(j + 1, currentElement.Substring(1)); //insert rest of String at next position so it will be processed again
  762. lastCommand = currentElement.First();
  763. argumentCounter = 0;
  764. }
  765. else if ((currentElement.First() >= '0' && currentElement.First() <= '9') || currentElement.First() == '-' || currentElement.First() == '+' || currentElement.First() != 'e') //seperate a single coordinate / number
  766. {
  767. bool decimalPointEncountered = false; //reuse the decimalPointEncountered flag for control flow first...
  768. switch (lastCommand){ //ceck for reaching of next command with omitted command descriptor
  769. case 'M':
  770. if (argumentCounter >= 2) decimalPointEncountered = true;
  771. break;
  772. case 'm':
  773. if (argumentCounter >= 2) decimalPointEncountered = true;
  774. break;
  775. case 'L':
  776. if (argumentCounter >= 2) decimalPointEncountered = true;
  777. break;
  778. case 'l':
  779. if (argumentCounter >= 2) decimalPointEncountered = true;
  780. break;
  781. case 'V':
  782. if (argumentCounter >= 1) decimalPointEncountered = true;
  783. break;
  784. case 'v':
  785. if (argumentCounter >= 1) decimalPointEncountered = true;
  786. break;
  787. case 'H':
  788. if (argumentCounter >= 1) decimalPointEncountered = true;
  789. break;
  790. case 'h':
  791. if (argumentCounter >= 1) decimalPointEncountered = true;
  792. break;
  793. case 'C':
  794. if (argumentCounter >= 6) decimalPointEncountered = true;
  795. break;
  796. case 'c':
  797. if (argumentCounter >= 6) decimalPointEncountered = true;
  798. break;
  799. case 'S':
  800. if (argumentCounter >= 4) decimalPointEncountered = true;
  801. break;
  802. case 's':
  803. if (argumentCounter >= 4) decimalPointEncountered = true;
  804. break;
  805. case 'Q':
  806. if (argumentCounter >= 4) decimalPointEncountered = true;
  807. break;
  808. case 'q':
  809. if (argumentCounter >= 4) decimalPointEncountered = true;
  810. break;
  811. case 'T':
  812. if (argumentCounter >= 2) decimalPointEncountered = true;
  813. break;
  814. case 't':
  815. if (argumentCounter >= 2) decimalPointEncountered = true;
  816. break;
  817. case 'A':
  818. if (argumentCounter >= 7) decimalPointEncountered = true;
  819. break;
  820. case 'a':
  821. if (argumentCounter >= 7) decimalPointEncountered = true;
  822. break;
  823. }
  824. if (decimalPointEncountered)
  825. {
  826. pathElements.Insert(j, lastCommand + ""); //repeat command descriptor
  827. j++; //skip command descriptor (was put into active position in the list
  828. argumentCounter = 0; //reset argument counter
  829. }
  830. decimalPointEncountered = false;
  831. for (int k = 1; k < currentElement.Length; k++)
  832. {
  833. if (!decimalPointEncountered && currentElement.ElementAt(k) == '.') //allow up to one decimal point in numbers
  834. {
  835. decimalPointEncountered = true;
  836. }
  837. else if (!((currentElement.ElementAt(k) >= '0' && currentElement.ElementAt(k) <= '9') || currentElement.First() == '-' || currentElement.First() == '+' || currentElement.First() != 'e'))
  838. {
  839. pathElements.RemoveAt(j);
  840. pathElements.Insert(j, currentElement.Substring(0, k - 1)); //insert number as seperate element
  841. pathElements.Insert(j + 1, currentElement.Substring(k)); //insert rest of String at next position so it will be processed again
  842. break;
  843. }
  844. }
  845. argumentCounter++;
  846. }
  847. else //parse non-space seperators and skip other unsupported characters (the only other valid ones per svg standard would be weird tokens looking like format descriptors (e.g. '#xC'), these are unsopported and will likely cause an error or other inconsitencies during parsing)
  848. {
  849. for (int k = 1; k < currentElement.Length; k++)
  850. {
  851. if (((currentElement.ElementAt(k) >= '0' && currentElement.ElementAt(k) <= '9')) || currentElement.ElementAt(k) == '-' || currentElement.ElementAt(k) == '+' || (currentElement.ElementAt(k) >= 'A' && currentElement.ElementAt(k) <= 'Z') || (currentElement.ElementAt(k) >= 'a' && currentElement.ElementAt(k) <= 'z'))
  852. {
  853. pathElements.RemoveAt(j);
  854. pathElements.Insert(j + 1, currentElement.Substring(k)); //insert rest of String at next position so it will be processed again
  855. break;
  856. }
  857. }
  858. }
  859. }
  860. else
  861. {
  862. if ((currentElement.First() >= 'A' && currentElement.First() <= 'Z') || (currentElement.First() >= 'a' && currentElement.First() <= 'z')) //update lastCommand buffer when reading single letter
  863. {
  864. lastCommand = currentElement.First();
  865. argumentCounter = 0;
  866. }
  867. else if(!(currentElement.First() >= '0' && currentElement.First() <= '9')) //not a number
  868. {
  869. pathElements.RemoveAt(j); //remove element
  870. j--; //decrement index pointer so next element will not be skipped (indices of all folowing elements just decreased by 1)
  871. }
  872. else //a single digit number
  873. {
  874. bool repeatCommandDescriptor = false; //reuse the decimalPointEncountered flag for control flow first...
  875. switch (lastCommand)
  876. { //ceck for reaching of next command with omitted command descriptor
  877. case 'M':
  878. if (argumentCounter >= 2) repeatCommandDescriptor = true;
  879. break;
  880. case 'm':
  881. if (argumentCounter >= 2) repeatCommandDescriptor = true;
  882. break;
  883. case 'L':
  884. if (argumentCounter >= 2) repeatCommandDescriptor = true;
  885. break;
  886. case 'l':
  887. if (argumentCounter >= 2) repeatCommandDescriptor = true;
  888. break;
  889. case 'V':
  890. if (argumentCounter >= 1) repeatCommandDescriptor = true;
  891. break;
  892. case 'v':
  893. if (argumentCounter >= 1) repeatCommandDescriptor = true;
  894. break;
  895. case 'H':
  896. if (argumentCounter >= 1) repeatCommandDescriptor = true;
  897. break;
  898. case 'h':
  899. if (argumentCounter >= 1) repeatCommandDescriptor = true;
  900. break;
  901. case 'C':
  902. if (argumentCounter >= 6) repeatCommandDescriptor = true;
  903. break;
  904. case 'c':
  905. if (argumentCounter >= 6) repeatCommandDescriptor = true;
  906. break;
  907. case 'S':
  908. if (argumentCounter >= 4) repeatCommandDescriptor = true;
  909. break;
  910. case 's':
  911. if (argumentCounter >= 4) repeatCommandDescriptor = true;
  912. break;
  913. case 'Q':
  914. if (argumentCounter >= 4) repeatCommandDescriptor = true;
  915. break;
  916. case 'q':
  917. if (argumentCounter >= 4) repeatCommandDescriptor = true;
  918. break;
  919. case 'T':
  920. if (argumentCounter >= 2) repeatCommandDescriptor = true;
  921. break;
  922. case 't':
  923. if (argumentCounter >= 2) repeatCommandDescriptor = true;
  924. break;
  925. case 'A':
  926. if (argumentCounter >= 7) repeatCommandDescriptor = true;
  927. break;
  928. case 'a':
  929. if (argumentCounter >= 7) repeatCommandDescriptor = true;
  930. break;
  931. }
  932. if (repeatCommandDescriptor)
  933. {
  934. pathElements.Insert(j, lastCommand + ""); //repeat command descriptor
  935. j++; //skip command descriptor (was put into active position in the list
  936. argumentCounter = 0; //reset argument counter
  937. }
  938. argumentCounter++;
  939. }
  940. }
  941. }
  942. }
  943. private (Point, double, double) parse_Z(List<string> pathElements, double initialPositionX, double initialPositionY)
  944. {
  945. pathElements.RemoveAt(0); //remove element descriptor token
  946. return (ScaleAndCreatePoint(initialPositionX, initialPositionY), initialPositionX, initialPositionY);
  947. }
  948. /// <summary>
  949. /// parses a "moveto", "close loop" or "lineto" path element with absolute coordinates
  950. /// </summary>
  951. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  952. /// <returns>the point at the end of the move, close loop or line action and its exact, unscaled coordinates</returns>
  953. private (Point, double, double) parse_M_L(List<string> pathElements)
  954. {
  955. pathElements.RemoveAt(0); //remove element descriptor token
  956. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse x coordinate
  957. pathElements.RemoveAt(0); //remove x coordinate token
  958. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse y coordinate
  959. pathElements.RemoveAt(0); //remove y coordinate token
  960. return (ScaleAndCreatePoint(x, y), x, y);
  961. }
  962. /// <summary>
  963. /// parses a "moveto", "close loop" or "lineto" path element with relative coordinates
  964. /// </summary>
  965. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  966. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  967. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  968. /// <returns>the point at the end of the move, close loop or line action and its exact, unscaled coordinates</returns>
  969. private (Point, double, double) parse_m_l(List<string> pathElements, double lastPositionX, double lastPositionY)
  970. {
  971. pathElements.RemoveAt(0); //remove element descriptor token
  972. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse relative x coordinate
  973. pathElements.RemoveAt(0); //remove x coordinate token
  974. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse relative y coordinate
  975. pathElements.RemoveAt(0); //remove y coordinate token
  976. x = lastPositionX + x; //compute absolute x coordinate
  977. y = lastPositionY + y; //compute absolute y coordinate
  978. return (ScaleAndCreatePoint(x, y), x, y);
  979. }
  980. /// <summary>
  981. /// parses a "horizontal lineto" path element with absolute coordinates
  982. /// </summary>
  983. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  984. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  985. /// <returns>the point at the end of the horizontal line action and its exact, unscaled coordinates</returns>
  986. private (Point, double, double) parse_H(List<string> pathElements, double lastPositionY)
  987. {
  988. pathElements.RemoveAt(0); //remove element descriptor token
  989. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse x coordinate
  990. pathElements.RemoveAt(0); //remove x coordinate token
  991. return (ScaleAndCreatePoint(x, lastPositionY), x, lastPositionY);
  992. }
  993. /// <summary>
  994. /// parses a "horizontal lineto" path element with relative coordinates
  995. /// </summary>
  996. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  997. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  998. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  999. /// <returns>the point at the end of the horizontal line action and its exact, unscaled coordinates</returns>
  1000. private (Point, double, double) parse_h(List<string> pathElements, double lastPositionX, double lastPositionY)
  1001. {
  1002. pathElements.RemoveAt(0); //remove element descriptor token
  1003. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse relative x coordinate
  1004. pathElements.RemoveAt(0); //remove x coordinate token
  1005. x = lastPositionX + x; //compute absolute x coordinate
  1006. return (ScaleAndCreatePoint(x, lastPositionY), x, lastPositionY);
  1007. }
  1008. /// <summary>
  1009. /// parses a "vertical lineto" path element with absolute coordinates
  1010. /// </summary>
  1011. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1012. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1013. /// <returns>the point at the end of the vertical line action and its exact, unscaled coordinates</returns>
  1014. private (Point, double, double) parse_V(List<string> pathElements, double lastPositionX)
  1015. {
  1016. pathElements.RemoveAt(0); //remove element descriptor token
  1017. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse y coordinate
  1018. pathElements.RemoveAt(0); //remove y coordinate token
  1019. return (ScaleAndCreatePoint(lastPositionX, y), lastPositionX, y);
  1020. }
  1021. /// <summary>
  1022. /// parses a "vertical lineto" path element with relative coordinates
  1023. /// </summary>
  1024. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1025. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1026. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  1027. /// <returns>the point at the end of the vertical line action and its exact, unscaled coordinates</returns>
  1028. private (Point, double, double) parse_v(List<string> pathElements, double lastPositionX, double lastPositionY)
  1029. {
  1030. pathElements.RemoveAt(0); //remove element descriptor token
  1031. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse relative y coordinate
  1032. pathElements.RemoveAt(0); //remove y coordinate token
  1033. y = lastPositionY + y; //compute absolute y coordinate
  1034. return (ScaleAndCreatePoint(lastPositionX, y), lastPositionX, y);
  1035. }
  1036. /// <summary>
  1037. /// parses a "cubic bezier curve" path element with absolute coordinates
  1038. /// </summary>
  1039. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1040. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1041. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  1042. /// <returns>a List of Points containing all sampled points on the bezier curve, aswell as the unscaled x and y coordinates of the last point of the curve and of the second bezier control point</returns>
  1043. private (List<Point>, double, double, double, double) parse_C(List<string> pathElements, double lastPositionX, double lastPositionY)
  1044. {
  1045. Console.WriteLine("parsing C: ");
  1046. for(int k= 0; k < 7; k++)
  1047. {
  1048. Console.WriteLine(pathElements.ElementAt(k));
  1049. }
  1050. pathElements.RemoveAt(0); //remove element descriptor token
  1051. double x1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse first control point x coordinate
  1052. pathElements.RemoveAt(0); //remove x coordinate token
  1053. double y1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse first control point y coordinate
  1054. pathElements.RemoveAt(0); //remove y coordinate token
  1055. double x2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse second control point x coordinate
  1056. pathElements.RemoveAt(0); //remove x coordinate token
  1057. double y2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse second control point y coordinate
  1058. pathElements.RemoveAt(0); //remove y coordinate token
  1059. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point x coordinate
  1060. pathElements.RemoveAt(0); //remove x coordinate token
  1061. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point y coordinate
  1062. pathElements.RemoveAt(0); //remove y coordinate token
  1063. return (SampleCubicBezier(lastPositionX, lastPositionY, x1, y1, x2, y2, x, y), x, y, x2, y2);
  1064. }
  1065. /// <summary>
  1066. /// parses a "cubic bezier curve" path element with relative coordinates
  1067. /// </summary>
  1068. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1069. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1070. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  1071. /// <returns>a List of Points containing all sampled points on the bezier curve, aswell as the unscaled x and y coordinates of the last point of the curve and of the second bezier control point</returns>
  1072. private (List<Point>, double, double, double, double) parse_c(List<string> pathElements, double lastPositionX, double lastPositionY)
  1073. {
  1074. pathElements.RemoveAt(0); //remove element descriptor token
  1075. double x1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse first control point x coordinate
  1076. pathElements.RemoveAt(0); //remove x coordinate token
  1077. double y1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse first control point y coordinate
  1078. pathElements.RemoveAt(0); //remove y coordinate token
  1079. double x2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse second control point x coordinate
  1080. pathElements.RemoveAt(0); //remove x coordinate token
  1081. double y2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse second control point y coordinate
  1082. pathElements.RemoveAt(0); //remove y coordinate token
  1083. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point x coordinate
  1084. pathElements.RemoveAt(0); //remove x coordinate token
  1085. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point y coordinate
  1086. pathElements.RemoveAt(0); //remove y coordinate token
  1087. x1 = lastPositionX + x1; //compute absolute x coordinate
  1088. y1 = lastPositionY + y1; //compute absolute y coordinate
  1089. x2 = lastPositionX + x2; //compute absolute x coordinate
  1090. y2 = lastPositionY + y2; //compute absolute y coordinate
  1091. x = lastPositionX + x; //compute absolute x coordinate
  1092. y = lastPositionY + y; //compute absolute y coordinate
  1093. return (SampleCubicBezier(lastPositionX, lastPositionY, x1, y1, x2, y2, x, y), x, y, x2, y2);
  1094. }
  1095. /// <summary>
  1096. /// parses a "cubic bezier curve shorthand" path element with absolute coordinates
  1097. /// </summary>
  1098. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1099. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1100. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  1101. /// <param name="lastBezierControlPointX">absolute x coordinate of the last bezier control point of the previous bezier curve</param>
  1102. /// <param name="lastBezierControlPointY">absolute y coordinate of the last bezier control point of the previous bezier curve</param>
  1103. /// <returns>a List of Points containing all sampled points on the bezier curve, aswell as the unscaled x and y coordinates of the last point of the curve and of the second bezier control point</returns>
  1104. private (List<Point>, double, double, double, double) parse_S(List<string> pathElements, double lastPositionX, double lastPositionY, double lastBezierControlPointX, double lastBezierControlPointY)
  1105. {
  1106. pathElements.RemoveAt(0); //remove element descriptor token
  1107. double x2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse second control point x coordinate
  1108. pathElements.RemoveAt(0); //remove x coordinate token
  1109. double y2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse second control point y coordinate
  1110. pathElements.RemoveAt(0); //remove y coordinate token
  1111. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point x coordinate
  1112. pathElements.RemoveAt(0); //remove x coordinate token
  1113. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point y coordinate
  1114. pathElements.RemoveAt(0); //remove y coordinate token
  1115. double x1 = lastPositionX + (lastPositionX - lastBezierControlPointX); //mirror last bezier control point at bezier start point to get first new bezier control point
  1116. double y1 = lastPositionY + (lastPositionY - lastBezierControlPointY); //mirror last bezier control point at bezier start point to get first new bezier control point
  1117. return (SampleCubicBezier(lastPositionX, lastPositionY, x1, y1, x2, y2, x, y), x, y, x2, y2);
  1118. }
  1119. /// <summary>
  1120. /// parses a "cubic bezier curve shorthand" path element with relative coordinates
  1121. /// </summary>
  1122. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1123. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1124. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  1125. /// <param name="lastBezierControlPointX">absolute x coordinate of the last bezier control point of the previous bezier curve</param>
  1126. /// <param name="lastBezierControlPointY">absolute y coordinate of the last bezier control point of the previous bezier curve</param>
  1127. /// <returns>a List of Points containing all sampled points on the bezier curve, aswell as the unscaled x and y coordinates of the last point of the curve and of the second bezier control point</returns>
  1128. private (List<Point>, double, double, double, double) parse_s(List<string> pathElements, double lastPositionX, double lastPositionY, double lastBezierControlPointX, double lastBezierControlPointY)
  1129. {
  1130. pathElements.RemoveAt(0); //remove element descriptor token
  1131. double x2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse second control point x coordinate
  1132. pathElements.RemoveAt(0); //remove x coordinate token
  1133. double y2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse second control point y coordinate
  1134. pathElements.RemoveAt(0); //remove y coordinate token
  1135. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point x coordinate
  1136. pathElements.RemoveAt(0); //remove x coordinate token
  1137. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point y coordinate
  1138. pathElements.RemoveAt(0); //remove y coordinate token
  1139. double x1 = lastPositionX + (lastPositionX - lastBezierControlPointX); //mirror last bezier control point at bezier start point to get first new bezier control point
  1140. double y1 = lastPositionY + (lastPositionY - lastBezierControlPointY); //mirror last bezier control point at bezier start point to get first new bezier control point
  1141. x2 = lastPositionX + x2; //compute absolute x coordinate
  1142. y2 = lastPositionY + y2; //compute absolute y coordinate
  1143. x = lastPositionX + x; //compute absolute x coordinate
  1144. y = lastPositionY + y; //compute absolute y coordinate
  1145. return (SampleCubicBezier(lastPositionX, lastPositionY, x1, y1, x2, y2, x, y), x, y, x2, y2);
  1146. }
  1147. /// <summary>
  1148. /// parses a "quadratic bezier curve" path element with absolute coordinates
  1149. /// </summary>
  1150. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1151. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1152. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  1153. /// <returns>a List of Points containing all sampled points on the bezier curve, aswell as the unscaled x and y coordinates of the last point of the curve and of the bezier control point</returns>
  1154. private (List<Point>, double, double, double, double) parse_Q(List<string> pathElements, double lastPositionX, double lastPositionY)
  1155. {
  1156. pathElements.RemoveAt(0); //remove element descriptor token
  1157. double x1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse control point x coordinate
  1158. pathElements.RemoveAt(0); //remove x coordinate token
  1159. double y1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse control point y coordinate
  1160. pathElements.RemoveAt(0); //remove y coordinate token
  1161. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point x coordinate
  1162. pathElements.RemoveAt(0); //remove x coordinate token
  1163. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point y coordinate
  1164. pathElements.RemoveAt(0); //remove y coordinate token
  1165. return (SampleQuadraticBezier(lastPositionX, lastPositionY, x1, y1, x, y), x, y, x1, y1);
  1166. }
  1167. /// <summary>
  1168. /// parses a "quadratic bezier curve" path element with relative coordinates
  1169. /// </summary>
  1170. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1171. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1172. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  1173. /// <returns>a List of Points containing all sampled points on the bezier curve, aswell as the unscaled x and y coordinates of the last point of the curve and of the bezier control point</returns>
  1174. private (List<Point>, double, double, double, double) parse_q(List<string> pathElements, double lastPositionX, double lastPositionY)
  1175. {
  1176. pathElements.RemoveAt(0); //remove element descriptor token
  1177. double x1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse control point x coordinate
  1178. pathElements.RemoveAt(0); //remove x coordinate token
  1179. double y1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse control point y coordinate
  1180. pathElements.RemoveAt(0); //remove y coordinate token
  1181. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point x coordinate
  1182. pathElements.RemoveAt(0); //remove x coordinate token
  1183. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point y coordinate
  1184. pathElements.RemoveAt(0); //remove y coordinate token
  1185. x1 = lastPositionX + x1; //compute absolute x coordinate
  1186. y1 = lastPositionY + y1; //compute absolute y coordinate
  1187. x = lastPositionX + x; //compute absolute x coordinate
  1188. y = lastPositionY + y; //compute absolute y coordinate
  1189. return (SampleQuadraticBezier(lastPositionX, lastPositionY, x1, y1, x, y), x, y, x1, y1);
  1190. }
  1191. /// <summary>
  1192. /// parses a "quadratic bezier curve shorthand" path element with absolute coordinates
  1193. /// </summary>
  1194. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1195. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1196. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  1197. /// <param name="lastBezierControlPointX">absolute x coordinate of the last bezier control point of the previous bezier curve</param>
  1198. /// <param name="lastBezierControlPointY">absolute y coordinate of the last bezier control point of the previous bezier curve</param>
  1199. /// <returns>a List of Points containing all sampled points on the bezier curve, aswell as the unscaled x and y coordinates of the last point of the curve and of the bezier control point</returns>
  1200. private (List<Point>, double, double, double, double) parse_T(List<string> pathElements, double lastPositionX, double lastPositionY, double lastBezierControlPointX, double lastBezierControlPointY)
  1201. {
  1202. pathElements.RemoveAt(0); //remove element descriptor token
  1203. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point x coordinate
  1204. pathElements.RemoveAt(0); //remove x coordinate token
  1205. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point y coordinate
  1206. pathElements.RemoveAt(0); //remove y coordinate token
  1207. double x1 = lastPositionX + (lastPositionX - lastBezierControlPointX); //mirror last bezier control point at bezier start point to get first new bezier control point
  1208. double y1 = lastPositionY + (lastPositionY - lastBezierControlPointY); //mirror last bezier control point at bezier start point to get first new bezier control point
  1209. return (SampleQuadraticBezier(lastPositionX, lastPositionY, x1, y1, x, y), x, y, x1, y1);
  1210. }
  1211. /// <summary>
  1212. /// parses a "quadratic bezier curve shorthand" path element with relative coordinates
  1213. /// </summary>
  1214. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1215. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1216. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  1217. /// <param name="lastBezierControlPointX">absolute x coordinate of the last bezier control point of the previous bezier curve</param>
  1218. /// <param name="lastBezierControlPointY">absolute y coordinate of the last bezier control point of the previous bezier curve</param>
  1219. /// <returns>a List of Points containing all sampled points on the bezier curve, aswell as the unscaled x and y coordinates of the last point of the curve and of the bezier control point</returns>
  1220. private (List<Point>, double, double, double, double) parse_t(List<string> pathElements, double lastPositionX, double lastPositionY, double lastBezierControlPointX, double lastBezierControlPointY)
  1221. {
  1222. pathElements.RemoveAt(0); //remove element descriptor token
  1223. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point x coordinate
  1224. pathElements.RemoveAt(0); //remove x coordinate token
  1225. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point y coordinate
  1226. pathElements.RemoveAt(0); //remove y coordinate token
  1227. x = lastPositionX + x; //compute absolute x coordinate
  1228. y = lastPositionY + y; //compute absolute y coordinate
  1229. double x1 = lastPositionX + (lastPositionX - lastBezierControlPointX); //mirror last bezier control point at bezier start point to get first new bezier control point
  1230. double y1 = lastPositionY + (lastPositionY - lastBezierControlPointY); //mirror last bezier control point at bezier start point to get first new bezier control point
  1231. return (SampleQuadraticBezier(lastPositionX, lastPositionY, x1, y1, x, y), x, y, x1, y1);
  1232. }
  1233. /// <summary>
  1234. /// parses a "elliptical arc" path element with absolute coordinates
  1235. /// </summary>
  1236. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1237. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1238. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  1239. /// <returns>a List of Points containing all sampled points on the elliptic arc, aswell as the unscaled x and y coordinates of the last point of the arc<returns>
  1240. private (List<Point>, double, double) parse_A(List<string> pathElements, double lastPositionX, double lastPositionY)
  1241. {
  1242. pathElements.RemoveAt(0); //remove element descriptor token
  1243. double rx = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse x radius
  1244. pathElements.RemoveAt(0); //remove x radius token
  1245. double ry = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse y radius
  1246. pathElements.RemoveAt(0); //remove y radius token
  1247. double thetha = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse rotation
  1248. pathElements.RemoveAt(0); //remove rotation token
  1249. bool largeArcFlag = Convert.ToInt16(pathElements.First()) == 1 ? true : false; //parse large arc flag
  1250. pathElements.RemoveAt(0); //remove large arc flag token
  1251. bool sweepFlag = Convert.ToInt16(pathElements.First()) == 1 ? true : false; //parse sweep flag
  1252. pathElements.RemoveAt(0); //remove sweep flag token
  1253. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point x coordinate
  1254. pathElements.RemoveAt(0); //remove x coordinate token
  1255. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point y coordinate
  1256. pathElements.RemoveAt(0); //remove y coordinate token
  1257. x = x - lastPositionX; //compute relative x coordinate
  1258. y = y - lastPositionY; //compute relative y coordinate
  1259. return (sampleArc(lastPositionX, lastPositionY, rx, ry, x, y, thetha, largeArcFlag, sweepFlag), x, y);
  1260. }
  1261. /// <summary>
  1262. /// parses a "elliptical arc" path element with relative coordinates
  1263. /// </summary>
  1264. /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
  1265. /// <param name="lastPositionX">absolute x coordinate of the last active point</param>
  1266. /// <param name="lastPositionY">absolute y coordinate of the last active point</param>
  1267. /// <returns>a List of Points containing all sampled points on the elliptic arc, aswell as the unscaled x and y coordinates of the last point of the arc</returns>
  1268. private (List<Point>, double, double) parse_a(List<string> pathElements, double lastPositionX, double lastPositionY)
  1269. {
  1270. pathElements.RemoveAt(0); //remove element descriptor token
  1271. double rx = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse x radius
  1272. pathElements.RemoveAt(0); //remove x radius token
  1273. double ry = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse y radius
  1274. pathElements.RemoveAt(0); //remove y radius token
  1275. double thetha = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse rotation
  1276. pathElements.RemoveAt(0); //remove rotation token
  1277. bool largeArcFlag = Convert.ToInt16(pathElements.First()) == 1 ? true : false; //parse large arc flag
  1278. pathElements.RemoveAt(0); //remove large arc flag token
  1279. bool sweepFlag = Convert.ToInt16(pathElements.First()) == 1 ? true : false; //parse sweep flag
  1280. pathElements.RemoveAt(0); //remove sweep flag token
  1281. double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point x coordinate
  1282. pathElements.RemoveAt(0); //remove x coordinate token
  1283. double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture); //parse target point y coordinate
  1284. pathElements.RemoveAt(0); //remove y coordinate token
  1285. return (sampleArc(lastPositionX, lastPositionY, rx, ry, x, y, thetha, largeArcFlag, sweepFlag), x, y);
  1286. }
  1287. /// <summary>
  1288. /// samles an arc of an ellipse into a list of points
  1289. /// </summary>
  1290. /// <param name="lastPositionX">x coordinate of last point</param>
  1291. /// <param name="lastPositionY">y coordinate of last point</param>
  1292. /// <param name="rx">x radius of the ellipse</param>
  1293. /// <param name="ry">y radius of the ellipse</param>
  1294. /// <param name="nextPositionXRelative">x coordinate of next point</param>
  1295. /// <param name="nextPositionYRelative">y coordinate of next point</param>
  1296. /// <param name="thetha">rotation of the ellipse around the x axis</param>
  1297. /// <param name="largeArcFlag">flag determining if the large or the small arc is to be drawn</param>
  1298. /// <param name="sweepFlag">flag determining in which direction the arc is to be drawn (false = ccw, true = cw)</param>
  1299. /// <returns></returns>
  1300. private List<Point> sampleArc(double lastPositionX, double lastPositionY, double rx, double ry, double nextPositionXRelative, double nextPositionYRelative, double thetha, bool largeArcFlag, bool sweepFlag)
  1301. {
  1302. double cos = Math.Cos(thetha / 180 * Math.PI);
  1303. double sin = Math.Sin(thetha / 180 * Math.PI);
  1304. double targetXTransformed = cos * nextPositionXRelative - sin * nextPositionYRelative; //rotate target point counterclockwise around the start point by [thetha] degrees, thereby practically rotating an intermediate coordinate system, which has its origin in the start point, clockwise by the same amount
  1305. double targetYTransformed = sin * nextPositionXRelative + cos * nextPositionYRelative;
  1306. (double[], double[]) values = sampleEllipticArcBiasedNoRotation(rx, ry, nextPositionXRelative, nextPositionYRelative, largeArcFlag, sweepFlag);
  1307. List<Point> result = new List<Point>();
  1308. for (int j = 0; j < values.Item1.Length; j++)
  1309. {
  1310. double xCoordinateRelative = cos * values.Item1[j] + sin * values.Item2[j]; //rotate backwards so intermediate coordinate system and "real" coordinate system have the same rotation again
  1311. double yCoordinateRelative = cos * values.Item2[j] - sin * values.Item1[j];
  1312. double xCoordinateAbsolute = lastPositionX + xCoordinateRelative; //translate relative to absolute coordinates (intermediate coordinate system is now again aligned with the "real" one (the virtual pane on which all vectorgraphic elements are placed) (note that this "real" coordinate system is still not the same as the one actually representing pixels for drawing, as it still has to be scaled appropriately (done inside the ScaleAndCreatePoint method)))
  1313. double yCoordinateAbsolute = lastPositionY + yCoordinateRelative;
  1314. result.Add(ScaleAndCreatePoint(xCoordinateAbsolute, yCoordinateAbsolute));
  1315. }
  1316. result.Add(ScaleAndCreatePoint(lastPositionX + nextPositionXRelative, lastPositionY + nextPositionYRelative)); //add end point
  1317. return result;
  1318. }
  1319. /// <summary>
  1320. /// samples an elliptical arc with given radii through coordinate origin and endpoint with specified properties
  1321. /// </summary>
  1322. /// <param name="rx">x radius</param>
  1323. /// <param name="ry">y radius</param>
  1324. /// <param name="nextPositionXRelative">x coordinate of next point</param>
  1325. /// <param name="nextPositionYRelative">y coordinate of next point</param>
  1326. /// <param name="largeArcFlag">flag determining if the large or the small arc is to be drawn</param>
  1327. /// <param name="sweepFlag">flag determining in which direction the arc is to be drawn (false = ccw, true = cw)</param>
  1328. /// <returns></returns>
  1329. private (double[], double[]) sampleEllipticArcBiasedNoRotation(double rx, double ry, double nextPositionXRelative, double nextPositionYRelative, bool largeArcFlag, bool sweepFlag)
  1330. {
  1331. double xStretchFactor = rx / ry; //get rx to ry ratio
  1332. (double[], double[]) values = sampleCircleArcBiasedNoRotation(ry, nextPositionXRelative, nextPositionYRelative, largeArcFlag, sweepFlag); //get a circular arc with radius ry
  1333. for (int j = 0; j < values.Item1.Length; j++)
  1334. {
  1335. values.Item1[j] = values.Item1[j] * xStretchFactor; //correct x coordinates to get an elliptical arc from a circular one
  1336. }
  1337. return values;
  1338. }
  1339. /// <summary>
  1340. /// samples a circular arc with given radius through coordinate origin and endpoint with specified properties
  1341. /// </summary>
  1342. /// <param name="r">radius</param>
  1343. /// <param name="nextPositionXRelative">x coordinate of next point</param>
  1344. /// <param name="nextPositionYRelative">y coordinate of next point</param>
  1345. /// <param name="largeArcFlag">flag determining if the large or the small arc is to be drawn</param>
  1346. /// <param name="sweepFlag">flag determining in which direction the arc is to be drawn (false = ccw, true = cw)</param>
  1347. /// <returns></returns>
  1348. private (double[], double[]) sampleCircleArcBiasedNoRotation(double r, double nextPositionXRelative, double nextPositionYRelative, bool largeArcFlag, bool sweepFlag)
  1349. {
  1350. // code adapted from https://stackoverflow.com/a/36211852
  1351. double radsq = r * r;
  1352. double q = Math.Sqrt(((nextPositionXRelative) * (nextPositionXRelative)) + ((nextPositionYRelative) * (nextPositionYRelative))); //Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
  1353. double x3 = (nextPositionXRelative) / 2; //(x1 + x2) / 2;
  1354. double y3 = (nextPositionYRelative) / 2; //(y1 + y2) / 2;
  1355. Console.WriteLine("radsq: " + radsq);
  1356. Console.WriteLine("q: " + q);
  1357. Console.WriteLine("x3: " + x3);
  1358. Console.WriteLine("y3: " + y3);
  1359. bool xPlusFlag; //flags needed to select center point "left" of the line between origin and the endpoint (will be used to select correct one ("left" or "right" one) later together with flags passed as arguments
  1360. bool yPlusFlag;
  1361. if (nextPositionXRelative > 0)
  1362. {
  1363. yPlusFlag = true; //left point lies above line
  1364. }
  1365. else
  1366. {
  1367. yPlusFlag = false; //left point lies below line
  1368. }
  1369. if (nextPositionYRelative > 0)
  1370. {
  1371. xPlusFlag = false; //left point lies left of line
  1372. }
  1373. else
  1374. {
  1375. xPlusFlag = true; //left point lies right of line
  1376. }
  1377. if(sweepFlag != largeArcFlag) //need "right" center point, not "left" one (refer to svg specification, sweepFlag means going around the circle in "clockwise" direction, largeArcFlag means tracing the larger of the two possible arcs in the selected direction)
  1378. {
  1379. xPlusFlag = !xPlusFlag;
  1380. yPlusFlag = !yPlusFlag;
  1381. }
  1382. double xC; // coordinates of center point of circle
  1383. double yC;
  1384. if(xPlusFlag) xC = x3 + Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((nextPositionYRelative) / q); //x3 + Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((y1 - y2) / q);
  1385. else xC = x3 - Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((nextPositionYRelative) / q);
  1386. if (yPlusFlag) yC = y3 + Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((nextPositionXRelative) / q); //y3 + Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((x2-x1) / q);
  1387. else yC = y3 - Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((nextPositionXRelative) / q);
  1388. Console.WriteLine("radsq - ((q / 2) * (q / 2))): " + (radsq - ((q / 2) * (q / 2))));
  1389. Console.WriteLine("Math.Sqrt(radsq - ((q / 2) * (q / 2))): " + Math.Sqrt(radsq - ((q / 2) * (q / 2))));
  1390. Console.WriteLine("computed center of circle (relative): point1= (0,0), point2= (" + nextPositionXRelative + "," + nextPositionYRelative + "), center= (" + xC + "," + yC + ")");
  1391. (double[], double[]) values = sampleCircleArcBiasedAroundCenter(-xC, -yC, nextPositionXRelative - xC, nextPositionYRelative - yC, r, sweepFlag);
  1392. for (int j = 0; j < values.Item1.Length; j++)
  1393. {
  1394. values.Item1[j] = values.Item1[j] + xC; //correct center point coordinate bias
  1395. values.Item2[j] = values.Item2[j] + yC;
  1396. }
  1397. return values;
  1398. }
  1399. /// <summary>
  1400. /// samples a circular arc with given radius around the center from the startpoint to the endpoint in the specified direction
  1401. /// </summary>
  1402. /// <param name="xStartPoint">x coordinate of the start point</param>
  1403. /// <param name="yStartPoint">y coordinate of the start point</param>
  1404. /// <param name="xFinalPoint">x coordinate of the final point</param>
  1405. /// <param name="yFinalPoint">y coordinate of the final point</param>
  1406. /// <param name="r">radius</param>
  1407. /// <param name="clockwise">direction</param>
  1408. /// <returns></returns>
  1409. private (double[], double[]) sampleCircleArcBiasedAroundCenter(double xStartPoint, double yStartPoint, double xFinalPoint, double yFinalPoint, double r, bool clockwise)
  1410. {
  1411. double phiEnd = Math.Atan2(yFinalPoint, xFinalPoint); // angles between points and origin and the positive x Axis
  1412. double phiStart = Math.Atan2(yStartPoint, xStartPoint);
  1413. double angle = ((double)2 * Math.PI) / (double)samplingRateEllipse; //compute angle increment (equal to the one used for ellipses)
  1414. int numberOfPoints = (int) Math.Ceiling(Math.Abs(phiEnd - phiStart) / angle); //compute number of points to sample
  1415. double[] xValues = new double[numberOfPoints];
  1416. double[] yValues = new double[numberOfPoints];
  1417. double phiCurrent = phiStart;
  1418. for (int j = 0; j < numberOfPoints-1; j++) //compute offset values of points for one quadrant
  1419. {
  1420. if (clockwise) phiCurrent -= angle; //get new angle
  1421. else phiCurrent += angle;
  1422. yValues[j] = Math.Sin(phiCurrent) * r; //angles are relative to positive x Axis!
  1423. xValues[j] = Math.Cos(phiCurrent) * r;
  1424. }
  1425. xValues[numberOfPoints - 1] = xFinalPoint; //(last segment always has an angle of less than or exactly 'angle')
  1426. yValues[numberOfPoints - 1] = yFinalPoint;
  1427. return (xValues, yValues);
  1428. }
  1429. /// <summary>
  1430. /// samples a cubic bezier curve with a static number of steps (samplingRateBezier)
  1431. /// </summary>
  1432. /// <param name="lastPositionX">x coordinate of last point</param>
  1433. /// <param name="lastPositionY">y coordinate of last point</param>
  1434. /// <param name="controlPoint1X">x coordinate of control point 1</param>
  1435. /// <param name="controlPoint1Y">y coordinate of control point 1</param>
  1436. /// <param name="controlPoint2X">x coordinate of control point 2</param>
  1437. /// <param name="controlPoint2Y">y coordinate of control point 2</param>
  1438. /// <param name="nextPositionX">x coordinate of next point</param>
  1439. /// <param name="nextPositionY">y coordinate of next point</param>
  1440. /// <returns>a List of Points containing all sampled points</returns>
  1441. private List<Point> SampleCubicBezier(double lastPositionX, double lastPositionY, double controlPoint1X, double controlPoint1Y, double controlPoint2X, double controlPoint2Y, double nextPositionX, double nextPositionY)
  1442. {
  1443. (double[], double[]) line1 = CreateDiscreteLine(lastPositionX, lastPositionY, controlPoint1X, controlPoint1Y);
  1444. (double[], double[]) line2 = CreateDiscreteLine(controlPoint1X, controlPoint1Y, controlPoint2X, controlPoint2Y);
  1445. (double[], double[]) line3 = CreateDiscreteLine(controlPoint2X, controlPoint2Y, nextPositionX, nextPositionY);
  1446. (double[], double[]) quadraticBezier1 = computeBezierStep(line1.Item1, line1.Item2, line2.Item1, line2.Item2);
  1447. (double[], double[]) quadraticBezier2 = computeBezierStep(line2.Item1, line2.Item2, line3.Item1, line3.Item2);
  1448. (double[], double[]) values = computeBezierStep(quadraticBezier1.Item1, quadraticBezier1.Item2, quadraticBezier2.Item1, quadraticBezier2.Item2);
  1449. List<Point> result = new List<Point>();
  1450. for (int j = 0; j < samplingRateBezier; j++)
  1451. {
  1452. result.Add(ScaleAndCreatePoint(values.Item1[j], values.Item2[j]));
  1453. }
  1454. return result;
  1455. }
  1456. /// <summary>
  1457. /// samples a quadratic bezier curve with a static number of steps (samplingRateBezier)
  1458. /// </summary>
  1459. /// <param name="lastPositionX">x coordinate of last point</param>
  1460. /// <param name="lastPositionY">y coordinate of last point</param>
  1461. /// <param name="controlPointX">x coordinate of control point</param>
  1462. /// <param name="controlPointY">y coordinate of control point</param>
  1463. /// <param name="nextPositionX">x coordinate of next point</param>
  1464. /// <param name="nextPositionY">y coordinate of next point</param>
  1465. /// <returns>a List of Points containing all sampled points</returns>
  1466. private List<Point> SampleQuadraticBezier(double lastPositionX, double lastPositionY, double controlPointX, double controlPointY, double nextPositionX, double nextPositionY)
  1467. {
  1468. (double[], double[]) line1 = CreateDiscreteLine(lastPositionX, lastPositionY, controlPointX, controlPointY);
  1469. (double[], double[]) line2 = CreateDiscreteLine(controlPointX, controlPointY, nextPositionX, nextPositionY);
  1470. (double[], double[]) values = computeBezierStep(line1.Item1, line1.Item2, line2.Item1, line2.Item2);
  1471. List<Point> result = new List<Point>();
  1472. for (int j = 0; j < samplingRateBezier; j++)
  1473. {
  1474. result.Add(ScaleAndCreatePoint(values.Item1[j], values.Item2[j]));
  1475. }
  1476. return result;
  1477. }
  1478. /// <summary>
  1479. /// create a discrete line with [samplingRateBezier] points (including start and end point) between two points
  1480. /// </summary>
  1481. /// <param name="point1X">coordinate of point 1</param>
  1482. /// <param name="point1Y">y coordinate of point 1</param>
  1483. /// <param name="point2X">x coordinate of point 2</param>
  1484. /// <param name="point2Y">y coordinate of point 2</param>
  1485. /// <returns>the discrete line as arrays of x and y coordinates</returns>
  1486. private (double[], double[]) CreateDiscreteLine(double point1X, double point1Y, double point2X, double point2Y)
  1487. {
  1488. double[] resultX = new double[samplingRateBezier];
  1489. double[] resultY = new double[samplingRateBezier];
  1490. for (int j = 0; j < samplingRateBezier; j++)
  1491. {
  1492. (double, double) pointResult = LinearInterpolationForBezier(point1X, point1Y, point2X, point2Y, j);
  1493. resultX[j] = pointResult.Item1;
  1494. resultY[j] = pointResult.Item2;
  1495. }
  1496. return (resultX, resultY);
  1497. }
  1498. /// <summary>
  1499. /// computes the discrete bezier curve between two given dicrete lines/curves
  1500. /// </summary>
  1501. /// <param name="line1X">x coordinates of all points in line 1</param>
  1502. /// <param name="line1Y">y coordinates of all points in line 1</param>
  1503. /// <param name="line2X">x coordinates of all points in line 2</param>
  1504. /// <param name="line2Y">y coordinates of all points in line 2</param>
  1505. /// <returns>the discrete bezier curve</returns>
  1506. private (double[], double[]) computeBezierStep(double[] line1X, double[] line1Y, double[] line2X, double[] line2Y)
  1507. {
  1508. double[] resultX = new double[samplingRateBezier];
  1509. double[] resultY = new double[samplingRateBezier];
  1510. for (int j = 0; j < samplingRateBezier; j++)
  1511. {
  1512. (double, double) pointResult = LinearInterpolationForBezier(line1X[j], line1Y[j], line2X[j], line2Y[j], j);
  1513. resultX[j] = pointResult.Item1;
  1514. resultY[j] = pointResult.Item2;
  1515. }
  1516. return (resultX, resultY);
  1517. }
  1518. /// <summary>
  1519. /// creates the linearly interpolated point at j/(samplingRateBezier - 1) between point 1 and point 2
  1520. /// </summary>
  1521. /// <param name="point1X">x coordinate of point 1</param>
  1522. /// <param name="point1Y">y coordinate of point 1</param>
  1523. /// <param name="point2X">x coordinate of point 2</param>
  1524. /// <param name="point2Y">y coordinate of point 2</param>
  1525. /// <param name="j">number of point to be interpolated, at a total number of [samplingRateBezier] points</param>
  1526. /// <returns>the linearly interpolated point</returns>
  1527. private (double, double) LinearInterpolationForBezier(double point1X, double point1Y, double point2X, double point2Y, int j)
  1528. {
  1529. double factor = ((double)1 / (double)(samplingRateBezier - 1)) * (double)j; //factor for linear interpolation
  1530. double x = point1X + ((point2X - point1X) * factor);
  1531. double y = point1Y + ((point2Y - point1Y) * factor);
  1532. return (x, y);
  1533. }
  1534. /// <summary>
  1535. /// parses a hierarchical svg element and all its sub-elements
  1536. /// </summary>
  1537. /// <param name="currentElement">the definition of the top level element as whitespace seperated String[]</param>
  1538. /// <param name="allLines">an array holding all lines of the input file</param>
  1539. /// <returns>the parsed element as a Line object, or null if the element is not supported</returns>
  1540. private List<Line> ParseMultiLineSVGElement(string[] currentElement, string[] allLines)
  1541. {
  1542. throw new NotImplementedException();
  1543. }
  1544. /// <summary>
  1545. /// removes the name of the attribute aswell as the '="' at the beginning and the '"' or '">' at the end of an attribute definition
  1546. /// </summary>
  1547. /// <param name="definition">the definition from the svg file</param>
  1548. /// <returns>the value of the attribute, as String (the part of definition contained between '"'s)</returns>
  1549. private String ParseSingleSVGAttribute(String definition)
  1550. {
  1551. return definition.Split('"')[1];
  1552. }
  1553. /// <summary>
  1554. /// fetches a single svg element definition that may extend ovr several lines of the input file, iterates i to point to the last line of the element definition
  1555. /// </summary>
  1556. /// <param name="allLines">an array holding all lines of the input file</param>
  1557. /// <returns>the definition of the current svg element, as String[] split by whitespaces</returns>
  1558. private String[] GetCurrentElement(String[] allLines)
  1559. {
  1560. List<String> currentElementTemp = allLines[i].Split(whitespaces).ToList();
  1561. while (!currentElementTemp.Last().EndsWith(">"))
  1562. {
  1563. i++;
  1564. currentElementTemp.AddRange(allLines[i].Split(whitespaces).ToList());
  1565. }
  1566. return currentElementTemp.ToArray();
  1567. }
  1568. /// <summary>
  1569. /// applies the scale factor to the coordinates and creates a new Point
  1570. /// </summary>
  1571. /// <param name="x">unscaled x coordinate</param>
  1572. /// <param name="y">unscaled y coordinate</param>
  1573. /// <returns>new Point with scaled coordinates</returns>
  1574. private Point ScaleAndCreatePoint(double x, double y)
  1575. {
  1576. return new Point((int)Math.Round(x * scale), (int)Math.Round(y * scale));
  1577. }
  1578. /// <summary>
  1579. /// creates a representation of an ellipse as a List of Points by sampling the outline of the ellipse
  1580. /// </summary>
  1581. /// <param name="x">x coordinate of the center of the ellipse</param>
  1582. /// <param name="y">y coordinate of the center of the ellipse</param>
  1583. /// <param name="rx">x radius of the ellipse</param>
  1584. /// <param name="ry">y radius of the ellipse</param>
  1585. /// <returns>the parsed element as a List of Points</returns>
  1586. private List<Point> SampleEllipse(double x, double y, double rx, double ry)
  1587. {
  1588. List<Point> ellipse = new List<Point>();
  1589. double angle = ((double)2 * Math.PI) / (double)samplingRateEllipse;
  1590. double yScale = ry / rx;
  1591. Console.WriteLine("parsing ellipse: " + x + ";" + y + "(" + rx + "x" + ry + ")" + " " + yScale + ":" + angle);
  1592. double[] xValues = new double[samplingRateEllipse / 4];
  1593. double[] yValues = new double[samplingRateEllipse / 4];
  1594. for (int j = 0; j < samplingRateEllipse / 4; j++) //compute offset values of points for one quadrant
  1595. {
  1596. xValues[j] = Math.Sin((double)j * angle) * rx;
  1597. yValues[j] = Math.Cos((double)j * angle) * rx;
  1598. Console.WriteLine("parsed ellipse value: " + xValues[j] + ";" + yValues[j]);
  1599. }
  1600. for (int j = 0; j < samplingRateEllipse / 4; j++) //create actual points for first quadrant
  1601. {
  1602. int xCoord = Convert.ToInt32(Math.Round(x + xValues[j]));
  1603. int yCoord = Convert.ToInt32(Math.Round(y - yValues[j] * yScale));
  1604. ellipse.Add(ScaleAndCreatePoint(xCoord, yCoord));
  1605. Console.WriteLine("parsed ellipse point: " + xCoord + ";" + yCoord + " pointCount: " + (samplingRateEllipse / 4));
  1606. }
  1607. for (int j = 0; j < samplingRateEllipse / 4; j++) //create actual points for second quadrant
  1608. {
  1609. int xCoord = Convert.ToInt32(Math.Round(x + yValues[j]));
  1610. int yCoord = Convert.ToInt32(Math.Round(y + xValues[j] * yScale));
  1611. ellipse.Add(ScaleAndCreatePoint(xCoord, yCoord));
  1612. Console.WriteLine("parsed ellipse point: " + xCoord + ";" + yCoord + " pointCount: " + (samplingRateEllipse / 4));
  1613. }
  1614. for (int j = 0; j < samplingRateEllipse / 4; j++) //create actual points for third quadrant
  1615. {
  1616. int xCoord = Convert.ToInt32(Math.Round(x - xValues[j]));
  1617. int yCoord = Convert.ToInt32(Math.Round(y + yValues[j] * yScale));
  1618. ellipse.Add(ScaleAndCreatePoint(xCoord, yCoord));
  1619. Console.WriteLine("parsed ellipse point: " + xCoord + ";" + yCoord + " pointCount: " + (samplingRateEllipse / 4));
  1620. }
  1621. for (int j = 0; j < samplingRateEllipse / 4; j++) //create actual points for fourth quadrant
  1622. {
  1623. int xCoord = Convert.ToInt32(Math.Round(x - yValues[j]));
  1624. int yCoord = Convert.ToInt32(Math.Round(y - xValues[j] * yScale));
  1625. ellipse.Add(ScaleAndCreatePoint(xCoord, yCoord));
  1626. Console.WriteLine("parsed ellipse point: " + xCoord + ";" + yCoord + " pointCount: " + (samplingRateEllipse / 4));
  1627. }
  1628. ellipse.Add(ScaleAndCreatePoint(Convert.ToInt32(Math.Round(x + 0)), Convert.ToInt32(Math.Round(y - rx * yScale)))); //close ellipse
  1629. return ellipse;
  1630. }
  1631. }
  1632. }