public virtual Tuple <int, int> CalcEntryIndex(IRuleEntry entry) { var ruleIndex = GetIndexRule(); var item1 = -1; var item2 = -1; Func <double, double, double> rtOp = (d, i) => Math.Abs(d - i); var startDif = rtOp(ruleIndex[0], entry.StartValue); var endDif = rtOp(ruleIndex[0], entry.EndValue); for (var i = 1; i < ruleIndex.Count; i++) { if (item1 < 0) { //there is a shorter distance between this rule index than the previous if (startDif > rtOp(ruleIndex[i], entry.StartValue)) { startDif = rtOp(ruleIndex[i], entry.StartValue); } else { item1 = i - 1; } } if (item2 < 0) { if (endDif > rtOp(ruleIndex[i], entry.EndValue)) { endDif = rtOp(ruleIndex[i], entry.EndValue); } else { item2 = i - 1; } } if (item1 > 0 && item2 > 0) { break; } } return(new Tuple <int, int>(item1, item2)); }//end CalcEntryIndex
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); }
public virtual Tuple<int, int> CalcEntryIndex(IRuleEntry entry) { var ruleIndex = GetIndexRule(); var item1 = -1; var item2 = -1; Func<double, double, double> rtOp = (d, i) => Math.Abs(d - i); var startDif = rtOp(ruleIndex[0], entry.StartValue); var endDif = rtOp(ruleIndex[0], entry.EndValue); for (var i = 1; i < ruleIndex.Count; i++) { if (item1 < 0) { //there is a shorter distance between this rule index than the previous if (startDif > rtOp(ruleIndex[i], entry.StartValue)) startDif = rtOp(ruleIndex[i], entry.StartValue); else item1 = i - 1; } if (item2 < 0) { if (endDif > rtOp(ruleIndex[i], entry.EndValue)) endDif = rtOp(ruleIndex[i], entry.EndValue); else item2 = i - 1; } if (item1 > 0 && item2 > 0) break; } return new Tuple<int, int>(item1, item2); }