Beispiel #1
0
        public static TextCanvas Concat(this TextCanvas block1Tc, TextCanvas block2Tc, Rule ruler)
        {
            if (ruler == null)
            {
                throw new NoRuleSetException();
            }

            if (block2Tc == null)
            {
                return(block1Tc);
            }

            var minIndex      = block1Tc.MinIndex < block2Tc.MinIndex ? block1Tc.MinIndex : block2Tc.MinIndex;
            var maxIndex      = block1Tc.MaxIndex > block2Tc.MaxIndex ? block1Tc.MaxIndex : block2Tc.MaxIndex;
            var blockBarCount = 0;

            Func <char[], bool> printBar =
                c =>
                new string(c).EndsWith($"{Config.GraphChars.Rail}{Config.GraphChars.BarRailIntersect}");

            var tc = new TextCanvas
            {
                MaxIndex = maxIndex,
                Ruler    = ruler,
                MinIndex = minIndex,
                Id       = block1Tc.Id + Config.GraphChars.IDSeparator + block2Tc.Id
            };

            for (var i = minIndex; i <= maxIndex; i++)
            {
                var ti1 = block1Tc.GetTextOrEmpty(i, block1Tc.Width);
                var ti2 = block2Tc.GetTextOrEmpty(i, block2Tc.Width);

                if (ti1.Index < 0 || ti2.Index < 0)
                {
                    continue;
                }

                var ntxt = new List <char>(ti1.Text);
                if (blockBarCount == 1 || block2Tc.MaxIndex >= i)
                {
                    ntxt.Add(Config.GraphChars.Bar);
                }
                else
                {
                    ntxt.Add((char)0x20);
                }

                if (printBar(ti1.Text.ToArray()))
                {
                    blockBarCount += 1;
                }

                var txtRng = new TextRange {
                    Id = block2Tc.Id, Length = ti2.Text.Count, StartIndex = ntxt.Count
                };
                txtRng.InnerRanges.AddRange(ti2.Ranges.Where(x => x.Id != block2Tc.Id));
                ntxt.AddRange(ti2.Text);

                var nti = new TextItem {
                    HashMarkValue = ti1.HashMarkValue, Index = i, Text = ntxt
                };
                nti.Ranges.AddRange(ti1.Ranges);
                nti.Ranges.Add(txtRng);
                tc.Items.Add(nti);

                if (ntxt.Count > tc.Width)
                {
                    tc.Width = ntxt.Count;
                }
            }
            return(tc);
        }
Beispiel #2
0
        public static TextCanvas Concat(this TextCanvas block1Tc, TextCanvas block2Tc, Rule ruler)
        {
            if(ruler == null)
                throw new NoRuleSetException();

            if (block2Tc == null)
            {
                return block1Tc;
            }

            var minIndex = block1Tc.MinIndex < block2Tc.MinIndex ? block1Tc.MinIndex : block2Tc.MinIndex;
            var maxIndex = block1Tc.MaxIndex > block2Tc.MaxIndex ? block1Tc.MaxIndex : block2Tc.MaxIndex;
            var blockBarCount = 0;

            Func<char[], bool> printBar =
                c =>
                    new String(c).EndsWith(string.Format("{0}{1}",
                        Config.GraphChars.Rail,
                        Config.GraphChars.BarRailIntersect));

            var tc = new TextCanvas() { MaxIndex = maxIndex, Ruler = ruler, MinIndex = minIndex, Id = block1Tc.Id + Config.GraphChars.IDSeparator + block2Tc.Id };
            for (var i = minIndex; i <= maxIndex; i++)
            {
                var ti1 = block1Tc.GetTextOrEmpty(i, block1Tc.Width);
                var ti2 = block2Tc.GetTextOrEmpty(i, block2Tc.Width);

                if (ti1.Index < 0 || ti2.Index < 0)
                    continue;

                var ntxt = new List<char>(ti1.Text);
                if (blockBarCount == 1 || block2Tc.MaxIndex >= i)
                    ntxt.Add(Config.GraphChars.Bar);
                else
                    ntxt.Add((char)0x20);

                if (printBar(ti1.Text.ToArray()))
                    blockBarCount += 1;

                var txtRng = new TextRange() {Id = block2Tc.Id, Length = ti2.Text.Count, StartIndex = ntxt.Count};
                ntxt.AddRange(ti2.Text);

                var nti = new TextItem() {HashMarkValue = ti1.HashMarkValue, Index = i, Text = ntxt};
                nti.Ranges.AddRange(ti1.Ranges);
                nti.Ranges.Add(txtRng);
                tc.Items.Add(nti);

                if (ntxt.Count > tc.Width)
                    tc.Width = ntxt.Count;
            }
            return tc;
        }
Beispiel #3
0
        public static TextCanvas ToTextCanvas(this IRuleEntry entry, Rule ruler)
        {
            if(ruler == null)
                throw new NoRuleSetException();

            var entryIndex = ruler.CalcEntryIndex(entry);
            var entryLines = entry.ToString().Split(Config.GraphChars.UNIX_NL_CHAR);

            var ruleIndex = ruler.GetIndexRule();

            //iteration is on this difference so there needs to be at least that many lines in the string
            if (entryIndex.Item2 - entryIndex.Item1 > entryLines.Length)
            {
                throw new DrawingException(
                    string.Format(
                                  "Entry is indexed at '{0}' to '{1}', a difference of '{2}' while the entry as a string is composed of '{3}' lines",
                        entryIndex.Item1,
                        entryIndex.Item2,
                        (entryIndex.Item2 - entryIndex.Item1),
                        entryLines.Length));
            }

            var textCanvas = new TextCanvas
                             {
                                 Ruler = ruler,
                                 Items = new List<TextItem>(),
                                 MinIndex = entryIndex.Item1,
                                 MaxIndex = entryIndex.Item2,
                                 StartValue = entry.StartValue,
                                 EndValue = entry.EndValue,
                                 Width =  entry.Width,
                                 Id = entry.Id
                             };

            //single line entry
            if (entryIndex.Item2 == entryIndex.Item1)
            {
                if (entryIndex.Item1 < 0 || entryIndex.Item1 >= ruleIndex.Count)
                    throw new DrawingException(
                        string.Format(
                            "The entry by ID '{0}' having the text value of '{1}' " +
                            "is set to a position that is beyond the current ruler's max.",
                            entry.Id, entry));

                textCanvas.Items.Add(new TextItem
                                     {
                                         HashMarkValue = ruleIndex[entryIndex.Item1],
                                         Index = entryIndex.Item1,
                                         Text = entry.ToString().ToCharArray().ToList()
                                     });
                return textCanvas;
            }

            //multiple line entry
            for (var i = entryIndex.Item1; i <= entryIndex.Item2; i++)
            {
                var hashMark = ruleIndex[i];
                var text = entryLines[(i - entryIndex.Item1)];

                if (string.IsNullOrEmpty(text))
                    continue;
                var nti = new TextItem
                          {
                              HashMarkValue = hashMark,
                              Index = i,
                              Text = text.ToCharArray().ToList()
                          };
                nti.Ranges.Add(new TextRange(){Id = entry.Id,Length = nti.Text.Count, StartIndex = 0});
                textCanvas.Items.Add(nti);
            }

            return textCanvas;
        }
Beispiel #4
0
 public TextItem Copy()
 {
     var ti = new TextItem { HashMarkValue = HashMarkValue, Index = Index };
     var txtChars = new char[Text.Count];
     Text.CopyTo(txtChars);
     ti.Text = txtChars.ToList();
     foreach (var tr in Ranges)
     {
         ti.Ranges.Add(new TextRange {Id = tr.Id, Length = tr.Length, StartIndex = tr.StartIndex});
     }
     return ti;
 }