FileImporter.cs 102 KB

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