12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Windows;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace SketchAssistantWPF
- {
- public class FileImporter
- {
-
-
-
- double scale;
-
-
-
- int i;
-
-
-
- readonly char[] whitespaces = new char[] { ' ' , ',' };
-
-
-
- readonly int samplingRateEllipse = 12;
-
-
-
- readonly int samplingRateBezier = 101;
-
-
-
-
-
- public Tuple<int, int, List<InternalLine>> ParseISADInputFile(String fileName)
- {
- return ParseISADInput(System.IO.File.ReadAllLines(fileName));
- }
-
-
-
-
-
- private Tuple<int, int, List<InternalLine>> ParseISADInput(String[] allLines)
- {
- if (allLines.Length == 0)
- {
- throw new FileImporterException("file is empty", "", -1);
- }
- if (!"drawing".Equals(allLines[0]))
- {
- throw new FileImporterException("file is not an interactive sketch assistant drawing", ".isad files have to start with the 'drawing' token", 1);
- }
- if (!"enddrawing".Equals(allLines[allLines.Length - 1]))
- {
- throw new FileImporterException("unterminated drawing definition", ".isad files have to end with the 'enddrawing' token", allLines.Length);
- }
- Tuple<int, int> dimensions = ParseISADHeader(allLines);
- List<InternalLine> picture = ParseISADBody(allLines, dimensions.Item1, dimensions.Item2);
-
- return new Tuple<int, int, List<InternalLine>>(dimensions.Item1, dimensions.Item2, picture);
- }
-
-
-
-
-
- private Tuple<int, int> ParseISADHeader(String[] allLines)
- {
- int width;
- int height;
- if (!(allLines.Length > 1) || !Regex.Match(allLines[1], @"^\d+x?\d+$", RegexOptions.None).Success)
- {
- throw new FileImporterException("invalid or missing canvas size definition", "format: [width]x[heigth]", 2);
- }
- String[] size = allLines[1].Split('x');
- width = Convert.ToInt32(size[0]);
- height = Convert.ToInt32(size[1]);
- return new Tuple<int, int>(width, height);
- }
-
-
-
-
-
- private List<InternalLine> ParseISADBody(String[] allLines, int width, int height)
- {
- String lineStartString = "line";
- String lineEndString = "endline";
- List<InternalLine> drawing = new List<InternalLine>();
-
- int i = 2;
-
- int lineStartPointer = i;
-
- while (lineStartString.Equals(allLines[i]))
- {
-
- i++;
- List<Point> newLine = new List<Point>();
- while (!lineEndString.Equals(allLines[i]))
- {
- if (i == allLines.Length)
- {
- throw new FileImporterException("unterminated line definition", null, (i + 1));
- }
-
- if (!Regex.Match(allLines[i], @"^\d+;\d+$", RegexOptions.None).Success)
- {
- throw new FileImporterException("invalid Point definition: wrong format", "format: [xCoordinate];[yCoordinate]", (i + 1));
- }
- String[] coordinates = allLines[i].Split(';');
-
- int xCoordinate = Convert.ToInt32(coordinates[0]);
- int yCoordinate = Convert.ToInt32(coordinates[1]);
- if (xCoordinate < 0 || yCoordinate < 0 || xCoordinate > width - 1 || yCoordinate > height - 1)
- {
- throw new FileImporterException("invalid Point definition: point out of bounds", null, (i + 1));
- }
- newLine.Add(new Point(xCoordinate, yCoordinate));
-
- i++;
- }
-
- i++;
-
- drawing.Add(new InternalLine(newLine));
-
- lineStartPointer = i;
- }
-
- if (i != allLines.Length - 1)
- {
- throw new FileImporterException("missing or invalid line definition token", "line definitions start with the 'line' token", (i + 1));
- }
-
- return drawing;
- }
-
-
-
-
-
- public Tuple<int, int, List<InternalLine>> ParseISADInputForTesting(String[] allLines)
- {
- return ParseISADInput(allLines);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Tuple<int, int, List<InternalLine>> ParseSVGInputFile(String fileName, int windowWidth, int windowHeight)
- {
- return ParseSVGInput(System.IO.File.ReadAllLines(fileName), windowWidth, windowHeight);
- }
-
-
-
-
-
- private Tuple<int, int, List<InternalLine>> ParseSVGInput(String[] allLines, double windowWidth, double windowHeight)
- {
- i = 0;
- if (allLines.Length == 0)
- {
- throw new FileImporterException("file is empty", "", -1);
- }
- var sizedef = ParseSVGHeader(allLines);
- i++;
- int width;
- int height;
- if(windowWidth != 0 && windowHeight != 0)
- {
- if (windowWidth / windowHeight > sizedef.Item1 / sizedef.Item2)
- {
- scale = windowHeight / sizedef.Item2;
- height = (int)Math.Round(windowHeight);
- width = (int) Math.Round(scale * sizedef.Item1);
- }
- else
- {
- scale = windowWidth / sizedef.Item1;
- width = (int)Math.Round(windowWidth);
- height = (int)Math.Round(scale * sizedef.Item2);
- }
- }
- else
- {
- scale = 1;
- width = sizedef.Item1;
- height = sizedef.Item2;
- }
- for (int j=0; j < allLines.Length; j++)
- {
- allLines[j] = allLines[j].Trim(whitespaces);
- }
- List<InternalLine> picture = ParseSVGBody(allLines);
- return new Tuple<int, int, List<InternalLine>>(width, height, picture);
- }
-
-
-
-
-
- private Tuple<int, int> ParseSVGHeader(String[] allLines)
- {
- while (!allLines[i].StartsWith("<svg"))
- {
- i++;
- }
- String[] currentLine = allLines[i].Split(' ');
- int width= -1;
- int height= -1;
- for(int j= 0; j < currentLine.Length; j++)
- {
- if (currentLine[j].StartsWith("width"))
- {
- width = Convert.ToInt32(ParseSingleSVGAttribute(currentLine[j]));
- }
- else if (currentLine[j].StartsWith("height"))
- {
- height = Convert.ToInt32(ParseSingleSVGAttribute(currentLine[j]));
- }
- }
- if(width == -1)
- {
- throw new FileImporterException("missing width definition in SVG header", "the header should contain the \"width=...\" attribute", i+1);
- }
- if (height == -1)
- {
- throw new FileImporterException("missing height definition in SVG header", "the header should contain the \"height=...\" attribute", i + 1);
- }
- return new Tuple<int, int>(width, height);
- }
-
-
-
-
-
- private List<InternalLine> ParseSVGBody(String[] allLines)
- {
- List<InternalLine> picture = new List<InternalLine>();
- while (!allLines[i].StartsWith("</svg"))
- {
- List<InternalLine> element = ParseSingleSVGElement(allLines);
- if (element != null)
- {
- picture.AddRange(element);
- }
- i++;
- if (i > allLines.Length - 1) throw new FileImporterException("unterminated input file: missing </svg> tag", "the file must not contain empty lines", i + 1);
- }
- return picture;
- }
-
-
-
-
-
- private List<InternalLine> ParseSingleSVGElement(string[] allLines)
- {
- String[] currentElement = GetCurrentElement(allLines);
- return ParseSingleLineSVGElement(currentElement);
- }
-
-
-
-
-
- private List<InternalLine> ParseSingleLineSVGElement(string[] currentElement)
- {
- List<Point> points= null;
- List<InternalLine> element = null;
- switch (currentElement[0])
- {
- case "<rect":
- points = ParseRect(currentElement);
- break;
- case "<circle":
- points = ParseCircle(currentElement);
- break;
- case "<ellipse":
- points = ParseEllipse(currentElement);
- break;
- case "<line":
- points = ParseLine(currentElement);
- break;
- case "<polyline":
- points = ParsePolyline(currentElement);
- break;
- case "<polygon":
- points = ParsePolygon(currentElement);
- break;
- case "<path":
- element = ParsePath(currentElement);
- break;
- default:
- return null;
- }
- if (element == null)
- {
- element = new List<InternalLine>();
- element.Add(new InternalLine(points));
- }
- return element;
- }
-
-
-
-
-
- private List<Point> ParseRect(string[] currentElement)
- {
- double x = 0;
- double y = 0;
- double w = 0;
- double h = 0;
- double rx = 0;
- double ry = 0;
- for(int j= 0; j < currentElement.Length; j++)
- {
- if (currentElement[j].StartsWith("x="))
- {
- x = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("y="))
- {
- y = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("width="))
- {
- w = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("height="))
- {
- h = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("rx="))
- {
- rx = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("ry="))
- {
- ry = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- }
- List<Point> rect = new List<Point>();
- rect.Add(ScaleAndCreatePoint(x, y));
- rect.Add(ScaleAndCreatePoint(x + w, y));
- rect.Add(ScaleAndCreatePoint(x + w, y + h));
- rect.Add(ScaleAndCreatePoint(x, y + h));
- rect.Add(ScaleAndCreatePoint(x, y));
- return rect;
- }
-
-
-
-
-
- private List<Point> ParseCircle(string[] currentElement)
- {
- double x = 0;
- double y = 0;
- double r = 0;
- for (int j = 0; j < currentElement.Length; j++)
- {
- if (currentElement[j].StartsWith("cx="))
- {
- x = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("cy="))
- {
- y = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("r="))
- {
- r = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- }
- return SampleEllipse(x, y, r, r);
- }
-
-
-
-
-
- private List<Point> ParseEllipse(string[] currentElement)
- {
- double x = 0;
- double y = 0;
- double rx = 0;
- double ry = 0;
- for (int j = 0; j < currentElement.Length; j++)
- {
- if (currentElement[j].StartsWith("cx="))
- {
- x = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("cy="))
- {
- y = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("rx="))
- {
- rx = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("ry="))
- {
- ry = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- }
- return SampleEllipse(x, y, rx, ry);
- }
-
-
-
-
-
- private List<Point> ParseLine(string[] currentElement)
- {
- double x1 = 0;
- double y1 = 0;
- double x2 = 0;
- double y2 = 0;
- for (int j = 0; j < currentElement.Length; j++)
- {
- if (currentElement[j].StartsWith("x1="))
- {
- x1 = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("y1="))
- {
- y1 = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("x2="))
- {
- x2 = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- else if (currentElement[j].StartsWith("y2="))
- {
- y2 = Convert.ToDouble(ParseSingleSVGAttribute(currentElement[j]), CultureInfo.InvariantCulture);
- }
- }
- List<Point> line = new List<Point>();
- line.Add(ScaleAndCreatePoint(x1, y1));
- line.Add(ScaleAndCreatePoint(x2, y2));
- return line;
- }
-
-
-
-
-
- private List<Point> ParsePolyline(string[] currentElement)
- {
- String[] points = null;
- for (int j = 0; j < currentElement.Length; j++)
- {
- if (currentElement[j].StartsWith("points="))
- {
- List<String> pointDefs = new List<string>();
- pointDefs.Add(currentElement[j].Substring(8));
- j++;
- while (!currentElement[j].EndsWith("\""))
- {
- pointDefs.Add(currentElement[j]);
- j++;
- }
- pointDefs.Add(currentElement[j].Trim('"'));
- points = pointDefs.ToArray();
- }
- }
- List<Point> polyline = new List<Point>();
- for (int k = 0; k < points.Length - 1; k += 2)
- {
- polyline.Add(ScaleAndCreatePoint(Convert.ToDouble(points[k], CultureInfo.InvariantCulture), Convert.ToDouble(points[k + 1], CultureInfo.InvariantCulture)));
- }
- return polyline;
- }
-
-
-
-
-
- private List<Point> ParsePolygon(string[] currentElement)
- {
- String[] points = null;
- for (int j = 0; j < currentElement.Length; j++)
- {
- if (currentElement[j].StartsWith("points="))
- {
- List<String> pointDefs = new List<string>();
- pointDefs.Add(currentElement[j].Substring(8));
- j++;
- while (!currentElement[j].EndsWith("\""))
- {
- pointDefs.Add(currentElement[j]);
- j++;
- }
- pointDefs.Add(currentElement[j].Trim('"'));
- points = pointDefs.ToArray();
- }
- }
- List<Point> polygon = new List<Point>();
- for (int k = 0; k < points.Length - 1; k+=2)
- {
- polygon.Add(ScaleAndCreatePoint(Convert.ToDouble(points[k], CultureInfo.InvariantCulture), Convert.ToDouble(points[k+1], CultureInfo.InvariantCulture)));
- }
- polygon.Add(ScaleAndCreatePoint(Convert.ToDouble(points[0], CultureInfo.InvariantCulture), Convert.ToDouble(points[1], CultureInfo.InvariantCulture)));
- return polygon;
- }
-
-
-
-
-
- private List<InternalLine> ParsePath(string[] currentElement)
- {
- List<String> pathElements = new List<string>();
- for (int j = 0; j < currentElement.Length; j++)
- {
- if (currentElement[j].StartsWith("d="))
- {
- pathElements.Add(currentElement[j].Substring(3));
- j++;
- while (!currentElement[j].EndsWith("\""))
- {
- pathElements.Add(currentElement[j]);
- j++;
- }
- pathElements.Add(currentElement[j].Trim('"'));
- }
- }
- NormalizePathDeclaration(pathElements);
- List<InternalLine> element = new List<InternalLine>();
- List<Point> currentLine = new List<Point>();
- double lastBezierControlPointX= 0;
- double lastBezierControlPointY= 0;
- double lastPositionX;
- double lastPositionY;
- double initialPositionX= -1;
- double initialPositionY= -1;
- bool newSubpath = true;
- Tuple<List<Point>, double, double> valuesArc;
- Tuple<List<Point>, double, double, double, double> valuesBezierCurve;
- var valuesSinglePoint = Parse_M_L(pathElements);
- currentLine = new List<Point>();
- currentLine.Add(valuesSinglePoint.Item1);
- lastPositionX = valuesSinglePoint.Item2;
- lastPositionY = valuesSinglePoint.Item3;
- String currentToken;
- while (!(pathElements.Count == 0)){
- if (newSubpath)
- {
- initialPositionX = lastPositionX;
- initialPositionY = lastPositionY;
- newSubpath = false;
- }
- currentToken = pathElements.First();
- if (currentToken.Equals("M"))
- {
- element.Add(new InternalLine(currentLine));
- valuesSinglePoint = Parse_M_L(pathElements);
- currentLine = new List<Point>();
- currentLine.Add(valuesSinglePoint.Item1);
- lastPositionX = valuesSinglePoint.Item2;
- lastPositionY = valuesSinglePoint.Item3;
- }
- else if (currentToken.Equals("m"))
- {
- element.Add(new InternalLine(currentLine));
- valuesSinglePoint = Parse_m_l(pathElements, lastPositionX, lastPositionY);
- currentLine = new List<Point>();
- currentLine.Add(valuesSinglePoint.Item1);
- lastPositionX = valuesSinglePoint.Item2;
- lastPositionY = valuesSinglePoint.Item3;
- }
- else if (currentToken.Equals("Z") || currentToken.Equals("z"))
- {
- valuesSinglePoint = Parse_Z(pathElements, initialPositionX, initialPositionY);
- newSubpath = true;
- currentLine.Add(valuesSinglePoint.Item1);
- element.Add(new InternalLine(currentLine));
- currentLine = new List<Point>();
- currentLine.Add(valuesSinglePoint.Item1);
- lastPositionX = valuesSinglePoint.Item2;
- lastPositionY = valuesSinglePoint.Item3;
- }
- else if (currentToken.Equals("L"))
- {
- valuesSinglePoint = Parse_M_L(pathElements);
- currentLine.Add(valuesSinglePoint.Item1);
- lastPositionX = valuesSinglePoint.Item2;
- lastPositionY = valuesSinglePoint.Item3;
- }
- else if (currentToken.Equals("l"))
- {
- valuesSinglePoint = Parse_m_l(pathElements, lastPositionX, lastPositionY);
- currentLine.Add(valuesSinglePoint.Item1);
- lastPositionX = valuesSinglePoint.Item2;
- lastPositionY = valuesSinglePoint.Item3;
- }
- else if (currentToken.Equals("H"))
- {
- valuesSinglePoint = Parse_H(pathElements, lastPositionY);
- currentLine.Add(valuesSinglePoint.Item1);
- lastPositionX = valuesSinglePoint.Item2;
- lastPositionY = valuesSinglePoint.Item3;
- }
- else if (currentToken.Equals("h"))
- {
- valuesSinglePoint = Parse_h(pathElements, lastPositionX, lastPositionY);
- currentLine.Add(valuesSinglePoint.Item1);
- lastPositionX = valuesSinglePoint.Item2;
- lastPositionY = valuesSinglePoint.Item3;
- }
- else if (currentToken.Equals("V"))
- {
- valuesSinglePoint = Parse_V(pathElements, lastPositionX);
- currentLine.Add(valuesSinglePoint.Item1);
- lastPositionX = valuesSinglePoint.Item2;
- lastPositionY = valuesSinglePoint.Item3;
- }
- else if (currentToken.Equals("v"))
- {
- valuesSinglePoint = Parse_v(pathElements, lastPositionX, lastPositionY);
- currentLine.Add(valuesSinglePoint.Item1);
- lastPositionX = valuesSinglePoint.Item2;
- lastPositionY = valuesSinglePoint.Item3;
- }
- else if (currentToken.Equals("C"))
- {
- valuesBezierCurve = Parse_C(pathElements, lastPositionX, lastPositionY);
- currentLine.AddRange(valuesBezierCurve.Item1);
- lastPositionX = valuesBezierCurve.Item2;
- lastPositionY = valuesBezierCurve.Item3;
- lastBezierControlPointX = valuesBezierCurve.Item4;
- lastBezierControlPointY = valuesBezierCurve.Item5;
- }
- else if (currentToken.Equals("c"))
- {
- valuesBezierCurve = Parse_C(pathElements, lastPositionX, lastPositionY);
- currentLine.AddRange(valuesBezierCurve.Item1);
- lastPositionX = valuesBezierCurve.Item2;
- lastPositionY = valuesBezierCurve.Item3;
- lastBezierControlPointX = valuesBezierCurve.Item4;
- lastBezierControlPointY = valuesBezierCurve.Item5;
- }
- else if (currentToken.Equals("S"))
- {
- valuesBezierCurve = Parse_S(pathElements, lastPositionX, lastPositionY, lastBezierControlPointX, lastBezierControlPointY);
- currentLine.AddRange(valuesBezierCurve.Item1);
- lastPositionX = valuesBezierCurve.Item2;
- lastPositionY = valuesBezierCurve.Item3;
- lastBezierControlPointX = valuesBezierCurve.Item4;
- lastBezierControlPointY = valuesBezierCurve.Item5;
- }
- else if (currentToken.Equals("s"))
- {
- valuesBezierCurve = Parse_s(pathElements, lastPositionX, lastPositionY, lastBezierControlPointX, lastBezierControlPointY);
- currentLine.AddRange(valuesBezierCurve.Item1);
- lastPositionX = valuesBezierCurve.Item2;
- lastPositionY = valuesBezierCurve.Item3;
- lastBezierControlPointX = valuesBezierCurve.Item4;
- lastBezierControlPointY = valuesBezierCurve.Item5;
- }
- else if (currentToken.Equals("Q"))
- {
- valuesBezierCurve = Parse_Q(pathElements, lastPositionX, lastPositionY);
- currentLine.AddRange(valuesBezierCurve.Item1);
- lastPositionX = valuesBezierCurve.Item2;
- lastPositionY = valuesBezierCurve.Item3;
- lastBezierControlPointX = valuesBezierCurve.Item4;
- lastBezierControlPointY = valuesBezierCurve.Item5;
- }
- else if (currentToken.Equals("q"))
- {
- valuesBezierCurve = Parse_q(pathElements, lastPositionX, lastPositionY);
- currentLine.AddRange(valuesBezierCurve.Item1);
- lastPositionX = valuesBezierCurve.Item2;
- lastPositionY = valuesBezierCurve.Item3;
- lastBezierControlPointX = valuesBezierCurve.Item4;
- lastBezierControlPointY = valuesBezierCurve.Item5;
- }
- else if (currentToken.Equals("T"))
- {
- valuesBezierCurve = Parse_T(pathElements, lastPositionX, lastPositionY, lastBezierControlPointX, lastBezierControlPointY);
- currentLine.AddRange(valuesBezierCurve.Item1);
- lastPositionX = valuesBezierCurve.Item2;
- lastPositionY = valuesBezierCurve.Item3;
- lastBezierControlPointX = valuesBezierCurve.Item4;
- lastBezierControlPointY = valuesBezierCurve.Item5;
- }
- else if (currentToken.Equals("t"))
- {
- valuesBezierCurve = Parse_t(pathElements, lastPositionX, lastPositionY, lastBezierControlPointX, lastBezierControlPointY);
- currentLine.AddRange(valuesBezierCurve.Item1);
- lastPositionX = valuesBezierCurve.Item2;
- lastPositionY = valuesBezierCurve.Item3;
- lastBezierControlPointX = valuesBezierCurve.Item4;
- lastBezierControlPointY = valuesBezierCurve.Item5;
- }
- else if (currentToken.Equals("A"))
- {
- valuesArc = Parse_A(pathElements, lastPositionX, lastPositionY);
- currentLine.AddRange(valuesArc.Item1);
- lastPositionX = valuesArc.Item2;
- lastPositionY = valuesArc.Item3;
- }
- else if (currentToken.Equals("a"))
- {
- valuesArc = Parse_a(pathElements, lastPositionX, lastPositionY);
- currentLine.AddRange(valuesArc.Item1);
- lastPositionX = valuesArc.Item2;
- lastPositionY = valuesArc.Item3;
- }
- else
- {
- 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);
- }
- }
- if (currentLine.Count > 1)
- {
- element.Add(new InternalLine(currentLine));
- }
- return element;
- }
-
-
-
-
- private void NormalizePathDeclaration(List<string> pathElements)
- {
- Char lastCommand = 'M';
- int argumentCounter = 0;
- for( int j= 0; j < pathElements.Count; j++)
- {
- String currentElement = pathElements.ElementAt(j);
- if (currentElement.Length != 1)
- {
- if (((currentElement.First() >= 'A' && currentElement.First() <= 'Z') || (currentElement.First() >= 'a' && currentElement.First() <= 'z')) && currentElement.First() != 'e')
- {
- pathElements.RemoveAt(j);
- pathElements.Insert(j, currentElement.First() + "");
- pathElements.Insert(j + 1, currentElement.Substring(1));
- lastCommand = currentElement.First();
- argumentCounter = 0;
- }
- else if ((currentElement.First() >= '0' && currentElement.First() <= '9') || currentElement.First() == '-' || currentElement.First() == '+' || currentElement.First() != 'e')
- {
- bool repeatCommandDescriptor = false;
- switch (lastCommand){
- case 'M':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 'm':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 'L':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 'l':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 'V':
- if (argumentCounter >= 1) repeatCommandDescriptor = true;
- break;
- case 'v':
- if (argumentCounter >= 1) repeatCommandDescriptor = true;
- break;
- case 'H':
- if (argumentCounter >= 1) repeatCommandDescriptor = true;
- break;
- case 'h':
- if (argumentCounter >= 1) repeatCommandDescriptor = true;
- break;
- case 'C':
- if (argumentCounter >= 6) repeatCommandDescriptor = true;
- break;
- case 'c':
- if (argumentCounter >= 6) repeatCommandDescriptor = true;
- break;
- case 'S':
- if (argumentCounter >= 4) repeatCommandDescriptor = true;
- break;
- case 's':
- if (argumentCounter >= 4) repeatCommandDescriptor = true;
- break;
- case 'Q':
- if (argumentCounter >= 4) repeatCommandDescriptor = true;
- break;
- case 'q':
- if (argumentCounter >= 4) repeatCommandDescriptor = true;
- break;
- case 'T':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 't':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 'A':
- if (argumentCounter >= 7) repeatCommandDescriptor = true;
- break;
- case 'a':
- if (argumentCounter >= 7) repeatCommandDescriptor = true;
- break;
- }
- if (repeatCommandDescriptor)
- {
- pathElements.Insert(j, lastCommand + "");
- j++;
- argumentCounter = 0;
- }
- bool decimalPointEncountered = false;
- for (int k = 1; k < currentElement.Length; k++)
- {
- if (!decimalPointEncountered && currentElement.ElementAt(k) == '.')
- {
- decimalPointEncountered = true;
- }
- else if (!((currentElement.ElementAt(k) >= '0' && currentElement.ElementAt(k) <= '9') || currentElement.First() == '-' || currentElement.First() == '+' || currentElement.First() != 'e'))
- {
- pathElements.RemoveAt(j);
- pathElements.Insert(j, currentElement.Substring(0, k - 1));
- pathElements.Insert(j + 1, currentElement.Substring(k));
- break;
- }
- }
- argumentCounter++;
- }
- else
- {
- for (int k = 1; k < currentElement.Length; k++)
- {
- 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'))
- {
- pathElements.RemoveAt(j);
- pathElements.Insert(j + 1, currentElement.Substring(k));
- break;
- }
- }
- }
- }
- else
- {
- if ((currentElement.First() >= 'A' && currentElement.First() <= 'Z') || (currentElement.First() >= 'a' && currentElement.First() <= 'z'))
- {
- lastCommand = currentElement.First();
- argumentCounter = 0;
- }
- else if(!(currentElement.First() >= '0' && currentElement.First() <= '9'))
- {
- pathElements.RemoveAt(j);
- j--;
- }
- else
- {
- bool repeatCommandDescriptor = false;
- switch (lastCommand)
- {
- case 'M':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 'm':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 'L':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 'l':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 'V':
- if (argumentCounter >= 1) repeatCommandDescriptor = true;
- break;
- case 'v':
- if (argumentCounter >= 1) repeatCommandDescriptor = true;
- break;
- case 'H':
- if (argumentCounter >= 1) repeatCommandDescriptor = true;
- break;
- case 'h':
- if (argumentCounter >= 1) repeatCommandDescriptor = true;
- break;
- case 'C':
- if (argumentCounter >= 6) repeatCommandDescriptor = true;
- break;
- case 'c':
- if (argumentCounter >= 6) repeatCommandDescriptor = true;
- break;
- case 'S':
- if (argumentCounter >= 4) repeatCommandDescriptor = true;
- break;
- case 's':
- if (argumentCounter >= 4) repeatCommandDescriptor = true;
- break;
- case 'Q':
- if (argumentCounter >= 4) repeatCommandDescriptor = true;
- break;
- case 'q':
- if (argumentCounter >= 4) repeatCommandDescriptor = true;
- break;
- case 'T':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 't':
- if (argumentCounter >= 2) repeatCommandDescriptor = true;
- break;
- case 'A':
- if (argumentCounter >= 7) repeatCommandDescriptor = true;
- break;
- case 'a':
- if (argumentCounter >= 7) repeatCommandDescriptor = true;
- break;
- }
- if (repeatCommandDescriptor)
- {
- pathElements.Insert(j, lastCommand + "");
- j++;
- argumentCounter = 0;
- }
- argumentCounter++;
- }
- }
- }
- }
-
-
-
-
-
-
-
- private Tuple<Point, double, double> Parse_Z(List<string> pathElements, double initialPositionX, double initialPositionY)
- {
- pathElements.RemoveAt(0);
- return new Tuple<Point, double, double>(ScaleAndCreatePoint(initialPositionX, initialPositionY), initialPositionX, initialPositionY);
- }
-
-
-
-
-
- private Tuple<Point, double, double> Parse_M_L(List<string> pathElements)
- {
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- return new Tuple<Point, double, double>(ScaleAndCreatePoint(x, y), x, y);
- }
-
-
-
-
-
-
-
- private Tuple<Point, double, double> Parse_m_l(List<string> pathElements, double lastPositionX, double lastPositionY)
- {
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- x = lastPositionX + x;
- y = lastPositionY + y;
- return new Tuple<Point, double, double>(ScaleAndCreatePoint(x, y), x, y);
- }
-
-
-
-
-
-
- private Tuple<Point, double, double> Parse_H(List<string> pathElements, double lastPositionY)
- {
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- return new Tuple<Point, double, double>(ScaleAndCreatePoint(x, lastPositionY), x, lastPositionY);
- }
-
-
-
-
-
-
-
- private Tuple<Point, double, double> Parse_h(List<string> pathElements, double lastPositionX, double lastPositionY)
- {
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- x = lastPositionX + x;
- return new Tuple<Point, double, double>(ScaleAndCreatePoint(x, lastPositionY), x, lastPositionY);
- }
-
-
-
-
-
-
- private Tuple<Point, double, double> Parse_V(List<string> pathElements, double lastPositionX)
- {
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- return new Tuple<Point, double, double>(ScaleAndCreatePoint(lastPositionX, y), lastPositionX, y);
- }
-
-
-
-
-
-
-
- private Tuple<Point, double, double> Parse_v(List<string> pathElements, double lastPositionX, double lastPositionY)
- {
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- y = lastPositionY + y;
- return new Tuple<Point, double, double>(ScaleAndCreatePoint(lastPositionX, y), lastPositionX, y);
- }
-
-
-
-
-
-
-
- private Tuple<List<Point>, double, double, double, double> Parse_C(List<string> pathElements, double lastPositionX, double lastPositionY)
- {
- pathElements.RemoveAt(0);
- double x1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double x2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- return new Tuple<List<Point>, double, double, double, double>(SampleCubicBezier(lastPositionX, lastPositionY, x1, y1, x2, y2, x, y), x, y, x2, y2);
- }
-
-
-
-
-
-
-
- private Tuple<List<Point>, double, double, double, double> Parse_c(List<string> pathElements, double lastPositionX, double lastPositionY)
- {
- pathElements.RemoveAt(0);
- double x1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double x2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- x1 = lastPositionX + x1;
- y1 = lastPositionY + y1;
- x2 = lastPositionX + x2;
- y2 = lastPositionY + y2;
- x = lastPositionX + x;
- y = lastPositionY + y;
- return new Tuple<List<Point>, double, double, double, double>(SampleCubicBezier(lastPositionX, lastPositionY, x1, y1, x2, y2, x, y), x, y, x2, y2);
- }
-
-
-
-
-
-
-
-
-
- private Tuple<List<Point>, double, double, double, double> Parse_S(List<string> pathElements, double lastPositionX, double lastPositionY, double lastBezierControlPointX, double lastBezierControlPointY)
- {
- pathElements.RemoveAt(0);
- double x2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double x1 = lastPositionX + (lastPositionX - lastBezierControlPointX);
- double y1 = lastPositionY + (lastPositionY - lastBezierControlPointY);
- return new Tuple<List<Point>, double, double, double, double>(SampleCubicBezier(lastPositionX, lastPositionY, x1, y1, x2, y2, x, y), x, y, x2, y2);
- }
-
-
-
-
-
-
-
-
-
- private Tuple<List<Point>, double, double, double, double> Parse_s(List<string> pathElements, double lastPositionX, double lastPositionY, double lastBezierControlPointX, double lastBezierControlPointY)
- {
- pathElements.RemoveAt(0);
- double x2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y2 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double x1 = lastPositionX + (lastPositionX - lastBezierControlPointX);
- double y1 = lastPositionY + (lastPositionY - lastBezierControlPointY);
- x2 = lastPositionX + x2;
- y2 = lastPositionY + y2;
- x = lastPositionX + x;
- y = lastPositionY + y;
- return new Tuple<List<Point>, double, double, double, double>(SampleCubicBezier(lastPositionX, lastPositionY, x1, y1, x2, y2, x, y), x, y, x2, y2);
- }
-
-
-
-
-
-
-
- private Tuple<List<Point>, double, double, double, double> Parse_Q(List<string> pathElements, double lastPositionX, double lastPositionY)
- {
- pathElements.RemoveAt(0);
- double x1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- return new Tuple<List<Point>, double, double, double, double>(SampleQuadraticBezier(lastPositionX, lastPositionY, x1, y1, x, y), x, y, x1, y1);
- }
-
-
-
-
-
-
-
- private Tuple<List<Point>, double, double, double, double> Parse_q(List<string> pathElements, double lastPositionX, double lastPositionY)
- {
- pathElements.RemoveAt(0);
- double x1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y1 = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- x1 = lastPositionX + x1;
- y1 = lastPositionY + y1;
- x = lastPositionX + x;
- y = lastPositionY + y;
- return new Tuple<List<Point>, double, double, double, double>(SampleQuadraticBezier(lastPositionX, lastPositionY, x1, y1, x, y), x, y, x1, y1);
- }
-
-
-
-
-
-
-
-
-
- private Tuple<List<Point>, double, double, double, double> Parse_T(List<string> pathElements, double lastPositionX, double lastPositionY, double lastBezierControlPointX, double lastBezierControlPointY)
- {
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double x1 = lastPositionX + (lastPositionX - lastBezierControlPointX);
- double y1 = lastPositionY + (lastPositionY - lastBezierControlPointY);
- return new Tuple<List<Point>, double, double, double, double>(SampleQuadraticBezier(lastPositionX, lastPositionY, x1, y1, x, y), x, y, x1, y1);
- }
-
-
-
-
-
-
-
-
-
- private Tuple<List<Point>, double, double, double, double> Parse_t(List<string> pathElements, double lastPositionX, double lastPositionY, double lastBezierControlPointX, double lastBezierControlPointY)
- {
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- x = lastPositionX + x;
- y = lastPositionY + y;
- double x1 = lastPositionX + (lastPositionX - lastBezierControlPointX);
- double y1 = lastPositionY + (lastPositionY - lastBezierControlPointY);
- return new Tuple<List<Point>, double, double, double, double>(SampleQuadraticBezier(lastPositionX, lastPositionY, x1, y1, x, y), x, y, x1, y1);
- }
-
-
-
-
-
-
-
- private Tuple<List<Point>, double, double> Parse_A(List<string> pathElements, double lastPositionX, double lastPositionY)
- {
- pathElements.RemoveAt(0);
- double rx = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double ry = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double thetha = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- bool largeArcFlag = Convert.ToInt16(pathElements.First()) == 1 ? true : false;
- pathElements.RemoveAt(0);
- bool sweepFlag = Convert.ToInt16(pathElements.First()) == 1 ? true : false;
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- x = x - lastPositionX;
- y = y - lastPositionY;
- return new Tuple<List<Point>, double, double>(SampleArc(lastPositionX, lastPositionY, rx, ry, x, y, thetha, largeArcFlag, sweepFlag), x, y);
- }
-
-
-
-
-
-
-
- private Tuple<List<Point>, double, double> Parse_a(List<string> pathElements, double lastPositionX, double lastPositionY)
- {
- pathElements.RemoveAt(0);
- double rx = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double ry = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double thetha = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- bool largeArcFlag = Convert.ToInt16(pathElements.First()) == 1 ? true : false;
- pathElements.RemoveAt(0);
- bool sweepFlag = Convert.ToInt16(pathElements.First()) == 1 ? true : false;
- pathElements.RemoveAt(0);
- double x = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- double y = Convert.ToDouble(pathElements.First(), CultureInfo.InvariantCulture);
- pathElements.RemoveAt(0);
- return new Tuple<List<Point>, double, double>(SampleArc(lastPositionX, lastPositionY, rx, ry, x, y, thetha, largeArcFlag, sweepFlag), x, y);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- private List<Point> SampleArc(double lastPositionX, double lastPositionY, double rx, double ry, double nextPositionXRelative, double nextPositionYRelative, double thetha, bool largeArcFlag, bool sweepFlag)
- {
- double cos = Math.Cos(thetha / 180 * Math.PI);
- double sin = Math.Sin(thetha / 180 * Math.PI);
- double targetXTransformed = cos * nextPositionXRelative - sin * nextPositionYRelative;
- double targetYTransformed = sin * nextPositionXRelative + cos * nextPositionYRelative;
- var values = SampleEllipticArcBiasedNoRotation(rx, ry, targetXTransformed, targetYTransformed, largeArcFlag, sweepFlag);
- List<Point> result = new List<Point>();
- for (int j = 0; j < values.Item1.Length; j++)
- {
- double xCoordinateRelative = cos * values.Item1[j] + sin * values.Item2[j];
- double yCoordinateRelative = cos * values.Item2[j] - sin * values.Item1[j];
- double xCoordinateAbsolute = lastPositionX + xCoordinateRelative;
- double yCoordinateAbsolute = lastPositionY + yCoordinateRelative;
- result.Add(ScaleAndCreatePoint(xCoordinateAbsolute, yCoordinateAbsolute));
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
-
- private Tuple<double[], double[]> SampleEllipticArcBiasedNoRotation(double rx, double ry, double targetXTransformed, double targetYTransformed, bool largeArcFlag, bool sweepFlag)
- {
- double xStretchFactor = rx / ry;
- var values = SampleCircleArcBiasedNoRotation(ry, targetXTransformed / xStretchFactor, targetYTransformed, largeArcFlag, sweepFlag);
- for (int j = 0; j < values.Item1.Length; j++)
- {
- values.Item1[j] = values.Item1[j] * xStretchFactor;
- }
- return values;
- }
-
-
-
-
-
-
-
-
-
- private Tuple<double[], double[]> SampleCircleArcBiasedNoRotation(double r, double nextPositionXRelative, double nextPositionYRelative, bool largeArcFlag, bool sweepFlag)
- {
-
- double radsq = r * r;
- double q = Math.Sqrt(((nextPositionXRelative) * (nextPositionXRelative)) + ((nextPositionYRelative) * (nextPositionYRelative)));
- double x3 = (nextPositionXRelative) / 2;
- double y3 = (nextPositionYRelative) / 2;
- bool xPlusFlag;
- bool yPlusFlag;
- if (nextPositionXRelative > 0)
- {
- yPlusFlag = true;
- }
- else
- {
- yPlusFlag = false;
- }
- if (nextPositionYRelative > 0)
- {
- xPlusFlag = false;
- }
- else
- {
- xPlusFlag = true;
- }
- if(sweepFlag != largeArcFlag)
- {
- xPlusFlag = !xPlusFlag;
- yPlusFlag = !yPlusFlag;
- }
- double xC;
- double yC;
- if(xPlusFlag) xC = x3 + Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((nextPositionYRelative) / q);
- else xC = x3 - Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((nextPositionYRelative) / q);
- if (yPlusFlag) yC = y3 + Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((nextPositionXRelative) / q);
- else yC = y3 - Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((nextPositionXRelative) / q);
- var values = SampleCircleArcBiasedAroundCenter(-xC, -yC, nextPositionXRelative - xC, nextPositionYRelative - yC, r, largeArcFlag, sweepFlag);
- for (int j = 0; j < values.Item1.Length; j++)
- {
- values.Item1[j] = values.Item1[j] + xC;
- values.Item2[j] = values.Item2[j] + yC;
- }
- return values;
- }
-
-
-
-
-
-
-
-
-
-
- private Tuple<double[], double[]> SampleCircleArcBiasedAroundCenter(double xStartPoint, double yStartPoint, double xFinalPoint, double yFinalPoint, double r, bool largeArcFlag, bool clockwise)
- {
- double phiEnd = Math.Atan2(yFinalPoint, xFinalPoint);
- double phiStart = Math.Atan2(yStartPoint, xStartPoint);
- double angle = ((double)2 * Math.PI) / (double)samplingRateEllipse;
- double angleDifference = Math.Abs(phiStart - phiEnd);
- if (angleDifference > 2 * Math.PI || angleDifference < 0) throw new Exception("angleDifference out of range: " + angleDifference);
- if (largeArcFlag)
- {
- if (angleDifference < Math.PI) angleDifference = ((double)2 * Math.PI) - angleDifference;
- }
- else
- {
- if(angleDifference > Math.PI) angleDifference = ((double)2 * Math.PI) - angleDifference;
- }
- int numberOfPoints = (int) Math.Ceiling(angleDifference / angle);
- double[] xValues = new double[numberOfPoints];
- double[] yValues = new double[numberOfPoints];
- double phiCurrent = phiStart;
- for (int j = 0; j < numberOfPoints-1; j++)
- {
- if (clockwise) phiCurrent -= angle;
- else phiCurrent += angle;
- yValues[j] = Math.Sin(phiCurrent) * r;
- xValues[j] = Math.Cos(phiCurrent) * r;
- }
- xValues[numberOfPoints - 1] = xFinalPoint;
- yValues[numberOfPoints - 1] = yFinalPoint;
- return new Tuple<double[], double[]>(xValues, yValues);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- private List<Point> SampleCubicBezier(double lastPositionX, double lastPositionY, double controlPoint1X, double controlPoint1Y, double controlPoint2X, double controlPoint2Y, double nextPositionX, double nextPositionY)
- {
- var line1 = CreateDiscreteLine(lastPositionX, lastPositionY, controlPoint1X, controlPoint1Y);
- var line2 = CreateDiscreteLine(controlPoint1X, controlPoint1Y, controlPoint2X, controlPoint2Y);
- var line3 = CreateDiscreteLine(controlPoint2X, controlPoint2Y, nextPositionX, nextPositionY);
- var quadraticBezier1 = ComputeBezierStep(line1.Item1, line1.Item2, line2.Item1, line2.Item2);
- var quadraticBezier2 = ComputeBezierStep(line2.Item1, line2.Item2, line3.Item1, line3.Item2);
- var values = ComputeBezierStep(quadraticBezier1.Item1, quadraticBezier1.Item2, quadraticBezier2.Item1, quadraticBezier2.Item2);
- List<Point> result = new List<Point>();
- for (int j = 0; j < samplingRateBezier; j++)
- {
- result.Add(ScaleAndCreatePoint(values.Item1[j], values.Item2[j]));
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
-
- private List<Point> SampleQuadraticBezier(double lastPositionX, double lastPositionY, double controlPointX, double controlPointY, double nextPositionX, double nextPositionY)
- {
- var line1 = CreateDiscreteLine(lastPositionX, lastPositionY, controlPointX, controlPointY);
- var line2 = CreateDiscreteLine(controlPointX, controlPointY, nextPositionX, nextPositionY);
- var values = ComputeBezierStep(line1.Item1, line1.Item2, line2.Item1, line2.Item2);
- List<Point> result = new List<Point>();
- for (int j = 0; j < samplingRateBezier; j++)
- {
- result.Add(ScaleAndCreatePoint(values.Item1[j], values.Item2[j]));
- }
- return result;
- }
-
-
-
-
-
-
-
-
- private Tuple<double[], double[]> CreateDiscreteLine(double point1X, double point1Y, double point2X, double point2Y)
- {
- double[] resultX = new double[samplingRateBezier];
- double[] resultY = new double[samplingRateBezier];
- for (int j = 0; j < samplingRateBezier; j++)
- {
- var pointResult = LinearInterpolationForBezier(point1X, point1Y, point2X, point2Y, j);
- resultX[j] = pointResult.Item1;
- resultY[j] = pointResult.Item2;
- }
- return new Tuple<double[], double[]>(resultX, resultY);
- }
-
-
-
-
-
-
-
-
- private Tuple<double[], double[]> ComputeBezierStep(double[] line1X, double[] line1Y, double[] line2X, double[] line2Y)
- {
- double[] resultX = new double[samplingRateBezier];
- double[] resultY = new double[samplingRateBezier];
- for (int j = 0; j < samplingRateBezier; j++)
- {
- var pointResult = LinearInterpolationForBezier(line1X[j], line1Y[j], line2X[j], line2Y[j], j);
- resultX[j] = pointResult.Item1;
- resultY[j] = pointResult.Item2;
- }
- return new Tuple<double[], double[]>(resultX, resultY);
- }
-
-
-
-
-
-
-
-
-
- private Tuple<double, double> LinearInterpolationForBezier(double point1X, double point1Y, double point2X, double point2Y, int j)
- {
- double factor = ((double)1 / (double)(samplingRateBezier - 1)) * (double)j;
- double x = point1X + ((point2X - point1X) * factor);
- double y = point1Y + ((point2Y - point1Y) * factor);
- return new Tuple<double, double>(x, y);
- }
-
-
-
-
-
-
- private List<InternalLine> ParseMultiLineSVGElement(string[] currentElement, string[] allLines)
- {
- throw new NotImplementedException();
- }
-
-
-
-
-
- private String ParseSingleSVGAttribute(String definition)
- {
- return definition.Split('"')[1];
- }
-
-
-
-
-
- private String[] GetCurrentElement(String[] allLines)
- {
- List<String> currentElementTemp = allLines[i].Split(whitespaces).ToList();
- while (!currentElementTemp.Last().EndsWith(">"))
- {
- i++;
- currentElementTemp.AddRange(allLines[i].Split(whitespaces).ToList());
- }
- return currentElementTemp.ToArray();
- }
-
-
-
-
-
-
- private Point ScaleAndCreatePoint(double x, double y)
- {
- return new Point((int)Math.Round(x * scale), (int)Math.Round(y * scale));
- }
-
-
-
-
-
-
-
-
- private List<Point> SampleEllipse(double x, double y, double rx, double ry)
- {
- List<Point> ellipse = new List<Point>();
- double angle = ((double)2 * Math.PI) / (double)samplingRateEllipse;
- double yScale = ry / rx;
- double[] xValues = new double[samplingRateEllipse / 4];
- double[] yValues = new double[samplingRateEllipse / 4];
- for (int j = 0; j < samplingRateEllipse / 4; j++)
- {
- xValues[j] = Math.Sin((double)j * angle) * rx;
- yValues[j] = Math.Cos((double)j * angle) * rx;
- }
- for (int j = 0; j < samplingRateEllipse / 4; j++)
- {
- int xCoord = Convert.ToInt32(Math.Round(x + xValues[j]));
- int yCoord = Convert.ToInt32(Math.Round(y - yValues[j] * yScale));
- ellipse.Add(ScaleAndCreatePoint(xCoord, yCoord));
- }
- for (int j = 0; j < samplingRateEllipse / 4; j++)
- {
- int xCoord = Convert.ToInt32(Math.Round(x + yValues[j]));
- int yCoord = Convert.ToInt32(Math.Round(y + xValues[j] * yScale));
- ellipse.Add(ScaleAndCreatePoint(xCoord, yCoord));
- }
- for (int j = 0; j < samplingRateEllipse / 4; j++)
- {
- int xCoord = Convert.ToInt32(Math.Round(x - xValues[j]));
- int yCoord = Convert.ToInt32(Math.Round(y + yValues[j] * yScale));
- ellipse.Add(ScaleAndCreatePoint(xCoord, yCoord));
- }
- for (int j = 0; j < samplingRateEllipse / 4; j++)
- {
- int xCoord = Convert.ToInt32(Math.Round(x - yValues[j]));
- int yCoord = Convert.ToInt32(Math.Round(y - xValues[j] * yScale));
- ellipse.Add(ScaleAndCreatePoint(xCoord, yCoord));
- }
- ellipse.Add(ScaleAndCreatePoint(Convert.ToInt32(Math.Round(x + 0)), Convert.ToInt32(Math.Round(y - rx * yScale))));
- return ellipse;
- }
- }
- }
|