FileImporter.cs 107 KB

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