public TextCanvas ComposeArrow(Rule ruler, TextCanvas tc) { if (ruler == null) { throw new NoRuleSetException(); } var arrowIdx = ruler.CalcEntryIndex(this); Width = tc.Width; //find the line on the text canvas var ti = tc.Items.FirstOrDefault(t => t.Index == arrowIdx.Item1); if (ti == null) { return(tc); } //scope in on the ranges for the located line TextRange fromTr; TryGetTextRange(ti.Ranges, FromBlock.Id, out fromTr); TextRange toTr; TryGetTextRange(ti.Ranges, ToBlock.Id, out toTr); if (fromTr == null || toTr == null) { return(tc); } var isOuterBlock = !string.IsNullOrWhiteSpace(tc.Id); System.Console.WriteLine( $"({(isOuterBlock ? "Outer Block" : "InnerBlock")}) {fromTr.Id} -> {toTr.Id}"); var arrowText = new StringBuilder(); var arrowDirection = DetermineArrowDirection(fromTr.Id, toTr.Id, ti.Ranges); switch (arrowDirection) { case PrintLocation.Left: arrowText = GetLeftArrowStrBldr(fromTr, toTr, ti); break; case PrintLocation.Right: arrowText = GetRightArrowStrBldr(fromTr, toTr, ti); break; } tc.Items.First(t => t.Index == arrowIdx.Item1).Text = NfString.MergeString(arrowText.ToString(), new string(tc.Items.First(t => t.Index == arrowIdx.Item1).Text.ToArray())).ToCharArray().ToList(); return(tc); }
public static TextCanvas Merge(this TextCanvas entry1Tc, TextCanvas entry2Tc, Rule ruler) { //check for null args if (ruler == null || entry2Tc == null) { throw new DrawingException("A merge requires a source, a candidate and a ruler"); } //validate args have something to work with if (entry1Tc.IsEmptyItems() && entry2Tc.IsEmptyItems()) { return(new TextCanvas()); } if (entry2Tc.IsEmptyItems() && !entry1Tc.IsEmptyItems()) { return(entry1Tc); } if (entry1Tc.IsEmptyItems() && !entry2Tc.IsEmptyItems()) { return(entry2Tc); } //determine total edges for the whole var entriesMin = entry1Tc.MinIndex < entry2Tc.MinIndex ? entry1Tc.MinIndex : entry2Tc.MinIndex; var entriesMax = entry1Tc.MaxIndex > entry2Tc.MaxIndex ? entry1Tc.MaxIndex : entry2Tc.MaxIndex; Func <TextItem, bool> tti = ti1 => ti1 != null && !ti1.IsEmptyText(); var textCanvas = new TextCanvas { Ruler = ruler, Items = new List <TextItem>(), MaxIndex = entriesMax, MinIndex = entriesMin, Width = entry1Tc.Width > entry2Tc.Width ? entry1Tc.Width : entry2Tc.Width }; for (var i = entriesMin; i <= entriesMax; i++) { var entry1i = entry1Tc.Items.FirstOrDefault(item => item.Index == i); var entry2i = entry2Tc.Items.FirstOrDefault(item => item.Index == i); if (entry1i == null && entry2i == null) { continue; } //merge line if (tti(entry1i) && tti(entry2i)) { var newEntry = entry1i.Copy(); newEntry.Ranges.AddRange(entry2i.Ranges); newEntry.Text = NfString.MergeString(new string(entry1i.Text.ToArray()), new string(entry2i.Text.ToArray())) .ToCharArray().Where(c => Convert.ToInt32(c) >= 0x20) .ToList(); textCanvas.Items.Add(newEntry); continue; } if (tti(entry1i)) { textCanvas.Items.Add(entry1i.Copy()); continue; } textCanvas.Items.Add(entry2i.Copy()); } return(textCanvas); }