TextObjectBase.PreviewExt.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using FastReport.Utils;
  5. namespace FastReport
  6. {
  7. partial class TextObjectBase : ISearchable
  8. {
  9. #region ISearchable Members
  10. /// <inheritdoc/>
  11. public CharacterRange[] SearchText(string text, bool matchCase, bool wholeWord)
  12. {
  13. List<CharacterRange> ranges = new List<CharacterRange>();
  14. string nonWordChars = " `-=[];',./~!@#$%^&*()+{}:\"<>?\\|\r\n\t";
  15. int startIndex = 0;
  16. while (startIndex < Text.Length)
  17. {
  18. int i = Text.IndexOf(text, startIndex, matchCase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
  19. if (i >= 0)
  20. {
  21. bool skip = false;
  22. if (wholeWord)
  23. {
  24. if (i > 0 && nonWordChars.IndexOf(Text[i - 1]) == -1)
  25. skip = true;
  26. if (i + text.Length < Text.Length && nonWordChars.IndexOf(Text[i + text.Length]) == -1)
  27. skip = true;
  28. }
  29. if (!skip)
  30. ranges.Add(new CharacterRange(i, text.Length));
  31. startIndex = i + text.Length;
  32. }
  33. else
  34. break;
  35. }
  36. if (ranges.Count > 0)
  37. return new CharacterRange[] { ranges[0] };
  38. return null;
  39. }
  40. /// <inheritdoc/>
  41. public virtual void DrawSearchHighlight(FRPaintEventArgs e, CharacterRange range)
  42. {
  43. RectangleF rangeRect = new RectangleF(AbsLeft * e.ScaleX, AbsTop * e.ScaleY,
  44. Width * e.ScaleX, Height * e.ScaleY);
  45. using (Brush brush = new SolidBrush(Color.FromArgb(128, SystemColors.Highlight)))
  46. {
  47. e.Graphics.FillRectangle(brush, rangeRect);
  48. }
  49. }
  50. #endregion
  51. }
  52. }