FileImporter.cs 102 KB

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