/// <summary> /// Highlight text on the page /// </summary> /// <param name="pageIndex">Zero-based index of the page</param> /// <param name="charIndex">Zero-based char index on the page.</param> /// <param name="charsCount">The number of highlighted characters on the page or -1 for highlight text from charIndex to end of the page.</param> /// <param name="color">Highlight color</param> public void HighlightText(int pageIndex, int charIndex, int charsCount, Color color) { //normalize all user input if (pageIndex < 0) pageIndex = 0; if (pageIndex > Document.Pages.Count - 1) pageIndex = Document.Pages.Count - 1; int charsCnt = Document.Pages[pageIndex].Text.CountChars; if (charIndex < 0) charIndex = 0; if (charIndex > charsCnt - 1) charIndex = charsCnt - 1; if (charsCount < 0) charsCount = charsCnt - charIndex; if (charIndex + charsCount > charsCnt) charsCount = charsCnt - 1 - charIndex; if (charsCount <= 0) return; var newEntry = new HighlightInfo() { CharIndex = charIndex, CharsCount = charsCount, Color = color }; if (!_highlightedText.ContainsKey(pageIndex)) { if (color != Helpers.ColorEmpty) { _highlightedText.Add(pageIndex, new List<HighlightInfo>()); _highlightedText[pageIndex].Add(newEntry); } } else { var entries = _highlightedText[pageIndex]; //Analize exists entries and remove overlapped and trancate intersecting entries for (int i = entries.Count - 1; i >= 0; i--) { List<HighlightInfo> calcEntries; if (CalcIntersectEntries(entries[i], newEntry, out calcEntries)) { if (calcEntries.Count == 0) entries.RemoveAt(i); else for (int j = 0; j < calcEntries.Count; j++) if (j == 0) entries[i] = calcEntries[j]; else entries.Insert(i, calcEntries[j]); } } if (color != Helpers.ColorEmpty) entries.Add(newEntry); } InvalidateVisual(); OnHighlightedTextChanged(EventArgs.Empty); }
private bool CalcIntersectEntries(HighlightInfo existEntry, HighlightInfo addingEntry, out List<HighlightInfo> calcEntries) { calcEntries = new List<HighlightInfo>(); int eStart = existEntry.CharIndex; int eEnd = existEntry.CharIndex + existEntry.CharsCount - 1; int aStart = addingEntry.CharIndex; int aEnd = addingEntry.CharIndex + addingEntry.CharsCount - 1; if (eStart < aStart && eEnd >= aStart && eEnd <= aEnd) { calcEntries.Add(new HighlightInfo() { CharIndex = eStart, CharsCount = aStart - eStart, Color = existEntry.Color }); return true; } else if (eStart >= aStart && eStart <= aEnd && eEnd > aEnd) { calcEntries.Add(new HighlightInfo() { CharIndex = aEnd + 1, CharsCount = eEnd - aEnd, Color = existEntry.Color }); return true; } else if (eStart >= aStart && eEnd <= aEnd) return true; else if (eStart < aStart && eEnd > aEnd) { calcEntries.Add(new HighlightInfo() { CharIndex = eStart, CharsCount = aStart - eStart, Color = existEntry.Color }); calcEntries.Add(new HighlightInfo() { CharIndex = aEnd + 1, CharsCount = eEnd - aEnd, Color = existEntry.Color }); return true; } //no intersection return false; }
/// <summary> /// Highlight text on the page /// </summary> /// <param name="pageIndex">Zero-based index of the page</param> /// <param name="highlightInfo">Sets the options for highlighting text</param> public void HighlightText(int pageIndex, HighlightInfo highlightInfo) { HighlightText(pageIndex, highlightInfo.CharIndex, highlightInfo.CharsCount, highlightInfo.Color); }