Exemplo n.º 1
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);
        }