/// <summary> /// Tries to find the referencable node (with a name) corresponding to a given coordinate. /// If no such node exists, null is returned. /// </summary> /// <param name="item"></param> /// <returns></returns> public static Tikz_Node GetReferenceableNode(Tikz_XYItem item, Tikz_Picture tpict) { if (item == null || tpict == null) { return(null); } if (item is Tikz_Node) { Tikz_Node n = item as Tikz_Node; if (n.name == null || n.name.Trim() == "") { return(null); } else { return(n); } } else if (item is Tikz_Coord || item is Tikz_Arc) { // find the next node for (int i = item.parent.Children.IndexOf(item) + 1; i < item.parent.Children.Count; i++) { if (item.parent.Children[i] is Tikz_Node) { Tikz_Node n = item.parent.Children[i] as Tikz_Node; // check if the node is really at the same position as the coordinate item if (n.coord == null) { if (n.name != null && n.name.Trim() != "") { return(n); } continue; } else { return(null); } } if (!(item.parent.Children[i] is Tikz_Something)) { break; } } // if we get here, nothing was found return(null); } else { return(null); //throw new NotImplementedException("MakeReferenceableNode not implemented for this type"); } }
private static string UniquefyName(string oldname, Tikz_Picture tp) { int postfix = 1; while (tp.nodelist.ContainsKey(oldname + "_" + postfix)) { postfix++; } return(oldname + "_" + postfix); }
/// <summary> /// Takes an XYItem (like (2,2) or a node) and tries to make it into a referenceable node /// (i.e, one with a name) /// /// Concretely, the routine does the following: /// - if item is a named node, return item. /// - if item is an unnamed node, give it a unique name and return item. /// - if item is a coordinate, see if there is a node at this coordinate /// (algorithm: see if next non-tikz_something item is a node) /// - if yes, start anew with item=this node /// - if no, add a named node at the specified coordinate /// </summary> /// <param name="item"></param> /// <returns></returns> protected Tikz_Node MakeReferenceableNode(Tikz_XYItem item) { Tikz_Picture tpict = overlay.ParseTree.GetTikzPicture(); if (item is Tikz_Node) { Tikz_Node n = item as Tikz_Node; if (n.name == "") { n.SetName(tpict.GetUniqueName()); n.UpdateText(); } return(n); } else if (item is Tikz_Coord) { // find the next node for (int i = item.parent.Children.IndexOf(item) + 1; i < item.parent.Children.Count; i++) { if (item.parent.Children[i] is Tikz_Node) { // check if the node is really at the same position as the coordinate item if ((item.parent.Children[i] as Tikz_Node).coord == null) { return(MakeReferenceableNode(item.parent.Children[i] as Tikz_Node)); } else { break; } } if (!(item.parent.Children[i] is Tikz_Something)) { break; } } // if we get here, nothing was found => add a new node Tikz_Something ws = new Tikz_Something(" "); Tikz_Node n = new Tikz_Node(); n.coord = null; item.parent.InsertChildAt(ws, item.parent.Children.IndexOf(item) + 1); item.parent.InsertChildAt(n, item.parent.Children.IndexOf(item) + 2); n.SetName(tpict.GetUniqueName()); n.UpdateText(); return(n); } else { throw new NotImplementedException("MakeReferenceableNode not implemented for this type"); } }
//create a new CurAddTo (even though their already might be one) //(needed for edge tool) protected virtual bool AddNewCurAddTo() { // find tikzpicture Tikz_Picture tpict = overlay.ParseTree.GetTikzPicture(); if (tpict == null) { return(false); } Parser.Tikz_Path tp = new Parser.Tikz_Path(); tp.starttag = @"\draw "; tp.endtag = ";"; if (overlay.EdgeStyle != "") { Parser.Tikz_Options topt = new Parser.Tikz_Options(); topt.starttag = "["; topt.endtag = "]"; Parser.Tikz_Option to = new Parser.Tikz_Option(); to.type = Parser.Tikz_OptionType.key; to.key = overlay.EdgeStyle; topt.AddOption(to); tp.AddChild(topt); tp.options = topt; } if (overlay.CurEditing != null) { overlay.CurEditing.tikzitem.AddChild(tp); overlay.CurEditing.tikzitem.AddChild(new Parser.Tikz_Something("\r\n")); } else { tpict.AddChild(tp); tpict.AddChild(new Parser.Tikz_Something("\r\n")); } curAddTo = tp; return(true); }
public void ParseTest4() { string code = @"%some text \begin{tikzpicture}[scale=2] \draw (1,2) node (v) {bla} -- (3,3); \end{tikzpicture} "; Tikz_ParseTree actual = TikzParser.Parse(code); Tikz_Picture tp = actual.GetTikzPicture(); Tikz_Node n = tp.GetNodeByName("v"); System.Windows.Point p; bool ret = n.GetAbsPos(out p); Assert.IsTrue(ret); Assert.AreEqual(2, p.X); Assert.AreEqual(4, p.Y); Assert.AreEqual("bla", n.label); }
protected virtual bool EnsureCurAddToExists(out bool created) { created = false; if (overlay.ParseTree == null) { return(false); } // find tikzpicture Parser.Tikz_Picture tpict = overlay.ParseTree.GetTikzPicture(); if (tpict == null) { if (overlay.AllowEditing) { // add a new tikzpicture Tikz_Picture tp = new Tikz_Picture(); tp.starttag = "\\begin{tikzpicture}"; tp.AddChild(new Tikz_Something("\r\n")); tp.endtag = "\\end{tikzpicture}"; //overlay.BeginUpdate(); overlay.ParseTree.AddChild(tp); tp.UpdateText(); //overlay.EndUpdate(); } else { return(false); } } if (curAddTo == null || !(curAddTo is Parser.Tikz_Path)) { created = AddNewCurAddTo(); } return(true); }
public void ParseTest2() { string code = @"\begin{tikzpicture} \draw (1,2) -- (3,3); \end{tikzpicture} "; Tikz_ParseTree actual = TikzParser.Parse(code); Tikz_Picture tp = actual.GetTikzPicture(); Assert.AreEqual(0, tp.StartPosition()); Tikz_Path tpa = tp.Children.Find(tpi => tpi is Tikz_Path) as Tikz_Path; Assert.AreEqual(21, tpa.StartPosition()); Tikz_Coord tc = tpa.Children.Find(tpi => tpi is Tikz_Coord) as Tikz_Coord; Assert.AreEqual(1, tc.uX.number); Assert.AreEqual(2, tc.uY.number); Assert.AreEqual(27, tc.StartPosition()); }
protected bool EnsureParseTreeExists() { // Try to create a new ParseTree if (overlay.ParseTree == null) { // TODO return(false); //TryCreateNew(this, out lret); if (overlay.AllowEditing) { // create a new parsetree Tikz_ParseTree t = new Tikz_ParseTree(); Tikz_Picture tp = new Tikz_Picture(); tp.starttag = "\\begin{tikzpicture}"; tp.AddChild(new Tikz_Something("\r\n")); tp.endtag = "\\end{tikzpicture}"; overlay.BeginUpdate(); // overlay.ParseTree = t; t.AddChild(tp); tp.UpdateText(); overlay.EndUpdate(); return(true); } else { GlobalUI.ShowMessageBox("Parse tree could not be created. Please correct all parser errors in the code and try again.", "Function not available", MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } } else { return(true); } }
/// <summary> /// Scans for duplicate node names in the parse tree and changes them to make them unique. /// Also updates references. /// </summary> /// <param name="tp"></param> public static void UniquefyNodeNames(Tikz_ParseTree tpt) { if (tpt == null) { return; } Dictionary <string, Tikz_Node> nodelist = new Dictionary <string, Tikz_Node>(); Tikz_Picture tp = tpt.GetTikzPicture(); if (tp == null) { return; } tpt.BeginModify(); // clear current node refs tp.nodelist.Clear(); // scan for nodes, rename and re-register noderefs ScanTree(tp, nodelist, tp); tpt.EndModify(); }
public void SetCorrectRaster(TikzParseItem tpi, bool IsParent = false) { if (ParseTree == null) { return; } if (tpi == null) { Tikz_Picture tp = ParseTree.GetTikzPicture(); if (tp != null) { TikzMatrix M; if (!tp.GetCurrentTransformAt(null, out M)) { Rasterizer.View.CoordinateTransform = new TikzMatrix(); // if the program gets here, the global coord. transformation could not be understood->ovelay should display nothing } else { Rasterizer.View.CoordinateTransform = M; } //rasterizer.RasterOrigin = M.Transform(new Point(0, 0)); //rasterizer.RasterScale = M.m[1, 1]; Rasterizer.View.IsCartesian = !UsePolarCoordinates; } } else if (tpi is Tikz_Scope) { Tikz_Scope ts = tpi as Tikz_Scope; TikzMatrix M; if (IsParent) { if (!ts.GetCurrentTransformAt(null, out M)) // todo { M = new TikzMatrix(); // broken coords-> take unity as backup } } else { //if (!ts.parent.GetCurrentTransformAt(ts, out M)) // M = new TikzMatrix(); if (!ts.GetRasterTransform(out M)) { M = new TikzMatrix(); } } Rasterizer.View.CoordinateTransform = M; //rasterizer.RasterOrigin = M.Transform(new Point(0, 0)); //rasterizer.RasterScale = M.m[1, 1]; Rasterizer.View.IsCartesian = !IsParent || !UsePolarCoordinates; } else if (tpi is Tikz_XYItem) { Tikz_XYItem t = tpi as Tikz_XYItem; Point offset; if (t.GetAbsPos(out offset, true)) { TikzMatrix M; if (!t.parent.GetCurrentTransformAt(t, out M)) //.CloneIt(); { M = new TikzMatrix(); } M.m[0, 2] = offset.X; M.m[1, 2] = offset.Y; //rasterizer.RasterScale = M.m[1, 1]; Rasterizer.View.CoordinateTransform = M; Rasterizer.View.IsCartesian = !(t.IsPolar()); } else { throw new Exception("In PdfOverlay: Encountered drawn item without valid coordinates"); } } else if (tpi is Tikz_Path) { Tikz_Path ts = tpi as Tikz_Path; TikzMatrix M; if (IsParent) { Point curPointAtEnd; if (!ts.GetCurrentTransformAt(null, out M)) // todo { M = new TikzMatrix(); // broken coords-> take unity as backup } if (ts.GetAbsOffset(out curPointAtEnd, null)) { M.m[0, 2] = curPointAtEnd.X; M.m[1, 2] = curPointAtEnd.Y; } } else { if (!ts.parent.GetCurrentTransformAt(ts, out M)) { M = new TikzMatrix(); } //if (!ts.GetRasterTransform(out M)) // M = new TikzMatrix(); } Rasterizer.View.CoordinateTransform = M; //rasterizer.RasterOrigin = M.Transform(new Point(0, 0)); //rasterizer.RasterScale = M.m[1, 1]; Rasterizer.View.IsCartesian = !IsParent || !UsePolarCoordinates; } else { Debug.WriteLine("Error in SetCorrectRaster: unsupported type");//Rasterizer.IsCartesian = true; // should not get here } }
public void AssignStyle(AssignStyleType type) { string cStyle = NodeStyle; Tikz_Picture tp = ParseTree.GetTikzPicture(); if (tp == null) { return; } if (View.Tool != OverlayToolType.move) { return; // context menu should actually only open with move tool,... but to be safe against later changes... } if (type == AssignStyleType.AssignNewStyle || type == AssignStyleType.ChangeToNewStyle) { if (Overlay.InputMessageBox.ShowInputDialog("New style...", "Please enter a unique style name", out cStyle) != MessageBoxResult.OK) { return; } cStyle = cStyle.Trim(); // check if style name is valid and unique if (ParseTree == null || cStyle == "") { return; } if (ParseTree.styles.ContainsKey(cStyle)) { GlobalUI.ShowMessageBox("Error: Style name '" + cStyle + "' already exists.", "Style exists", MessageBoxButton.OK, MessageBoxImage.Warning); return; } // add new style, immediately before \begin{tikzpicture}[...] BeginUpdate(); Tikz_Option to = new Tikz_Option(); to.type = Tikz_OptionType.style; to.key = cStyle; to.val = ""; to.text = "\\tikzstyle{" + cStyle + "}=[];"; int index = ParseTree.Children.IndexOf(tp); if (index >= 0) { ParseTree.InsertChildAt(to, index); to.RegisterNodeAndStyleRefs(); ParseTree.InsertChildAt(new Tikz_Something(Environment.NewLine), index + 1); } } else { if (cStyle.Trim() == "") { return; } BeginUpdate(); } // loop through selected items and set styles foreach (OverlayShape ols in selectionTool.SelItems) { // currently only node styles can be set if (ols.item is Tikz_Node) { Tikz_Node tn = ols.item as Tikz_Node; if (tn.options == "" || type == AssignStyleType.ChangeToCurrentNodeStyle || type == AssignStyleType.ChangeToNewStyle) { tn.options = "[" + cStyle + "]"; } else // just add option { string o = tn.options.Trim(); if (o.EndsWith("]")) { o = o.Substring(0, o.Length - 1); o = o + ", " + cStyle + "]"; } // otherwise, do nothing (error) tn.options = o; } tn.UpdateText(); } } EndUpdate(); // Make sure EndUpdate() is always called (..if Beginupdate() was)! }
static bool FillItem(TikzContainerParseItem item, CommonTree t, CommonTokenStream tokens) { int curToken = t.TokenStartIndex; if (item is Tikz_ParseTree) { curToken = 0; // for root, start at the beginning } if (t.Children == null) { return(false); } foreach (CommonTree childt in t.Children) { addSomething(item, tokens, curToken, childt.TokenStartIndex - 1); switch (childt.Type) { case simpletikzParser.IM_PICTURE: Tikz_Picture tp = new Tikz_Picture(); FillItem(tp, childt, tokens); item.AddChild(tp); break; case simpletikzParser.IM_STARTTAG: item.starttag = getTokensString(tokens, childt.TokenStartIndex, childt.TokenStopIndex); break; case simpletikzParser.IM_ENDTAG: item.endtag = getTokensString(tokens, childt.TokenStartIndex, childt.TokenStopIndex); break; case simpletikzParser.IM_PATH: Tikz_Path tpath = new Tikz_Path(); FillItem(tpath, childt, tokens); item.AddChild(tpath); break; case simpletikzParser.IM_SCOPE: Tikz_Scope tscope = new Tikz_Scope(); FillItem(tscope, childt, tokens); item.AddChild(tscope); break; case simpletikzParser.IM_COORD: Tikz_Coord tc = Tikz_Coord.FromCommonTree(childt, tokens); tc.text = getTokensString(tokens, childt); item.AddChild(tc); break; case simpletikzParser.IM_ARC: Tikz_Arc ta = Tikz_Arc.FromCommonTree(childt, tokens); ta.text = getTokensString(tokens, childt); item.AddChild(ta); break; case simpletikzParser.IM_NODE: Tikz_Node tn = Tikz_Node.FromCommonTree(childt, tokens); tn.text = getTokensString(tokens, childt); item.AddChild(tn); break; case simpletikzParser.IM_OPTION_KV: case simpletikzParser.IM_OPTION_STYLE: Tikz_Option topt = Tikz_Option.FromCommonTree(childt, tokens); if (topt == null) { break; } //topt.text = getTokensString(tokens, childt); String s = getTokensString(tokens, childt); topt.text = s; item.AddChild(topt); break; case simpletikzParser.IM_OPTIONS: //Tikz_Options to = Tikz_Options.FromCommonTree(childt); Tikz_Options to = new Tikz_Options(); FillItem(to, childt, tokens); item.AddChild(to); //to.text = getTokensString(tokens, childt); //item.AddChild(tn); if (item.options == null) { // determine whether option belongs to the item (e.g. \draw [this belongs to draw] blabla [thisnot]) // i.e., the scope of the options is the whole item's body // this is hacky if (item.Children.Count == 1 || (item.Children.Count == 2 && (item.Children[0] is Tikz_Something) && item.Children[0].ToString().Trim() == "")) { item.options = to; } } break; case simpletikzParser.IM_TIKZSET: Tikz_Options to2 = new Tikz_Options(); FillItem(to2, childt, tokens); item.AddChild(to2); break; case simpletikzParser.IM_STYLE: Tikz_Option topt2 = Tikz_Option.FromCommonTree(childt, tokens); //FillItem(to2, childt, tokens); topt2.text = getTokensString(tokens, childt); item.AddChild(topt2); break; case simpletikzParser.IM_CONTROLS: Tikz_Controls tcontrols = new Tikz_Controls(); FillItem(tcontrols, childt, tokens); item.AddChild(tcontrols); break; case simpletikzParser.IM_SIZE: Tikz_Size tsize = Tikz_Size.FromCommonTree(childt, tokens); tsize.text = getTokensString(tokens, childt); item.AddChild(tsize); //Tikz_Size tsize = new Tikz_Size(); //item.AddChild(tsize); break; //case simpletikzParser.ID: //case simpletikzParser.IM_STRING: //case simpletikzParser.COMMAND: //case simpletikzParser.T__57: // break; case simpletikzParser.IM_TIKZEDT_CMD: Tikz_EdtCommand cmd = new Tikz_EdtCommand(getTokensString(tokens, childt)); item.AddChild(cmd); break; case simpletikzParser.IM_DONTCARE: Tikz_Something st = new Tikz_Something(getTokensString(tokens, childt)); item.AddChild(st); break; default: // getting here is an error throw new Exception(" childt.Type not handled! " + childt.Type.ToString() + " (\"" + getTokensString(tokens, childt) + "\")"); //break; } curToken = childt.TokenStopIndex + 1; } if (t.TokenStartIndex >= 0) // rule out empty code { addSomething(item, tokens, curToken, t.TokenStopIndex); } return(true); }
/// <summary> /// Recursive function to scan the parsetree in a depth first manner. /// nodelist contains the current map from _old_ nodenames to nodes. it is used to update node references /// </summary> private static void ScanTree(TikzContainerParseItem tc, Dictionary <string, Tikz_Node> nodelist, Tikz_Picture tp) { foreach (var tpi in tc.Children) { if (tpi is Tikz_Node) { Tikz_Node tn = tpi as Tikz_Node; if (tn.name != null && tn.name.Trim() != "") { string name = CleanName(tn.name.Trim()); // remember the node with its old name (all coordinates referring to this name henceforth will be changed to the new name nodelist[name] = tn; if (tp.nodelist.ContainsKey(name)) { // we have to change the name tn.name = UniquefyName(name, tp); tn.UpdateText(); } tn.RegisterNodeAndStyleRefs(); } } else if (tpi is Tikz_Coord) { Tikz_Coord tco = tpi as Tikz_Coord; if (tco.type == Tikz_CoordType.Named) { string nameref = CleanName(tco.nameref); if (nodelist.ContainsKey(nameref)) { if (CleanName(nodelist[nameref].name) != nameref) { tco.nameref = CleanName(nodelist[nameref].name); tco.UpdateText(); } } } } else if (tpi is TikzContainerParseItem) { ScanTree(tpi as TikzContainerParseItem, nodelist, tp); } } }