StyleRun.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // RichTextKit
  2. // Copyright © 2019-2020 Topten Software. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. // not use this product except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using Topten.RichTextKit.Utils;
  22. namespace Topten.RichTextKit
  23. {
  24. /// <summary>
  25. /// Represets a style run - a logical run of characters all with the same
  26. /// style.
  27. /// </summary>
  28. public class StyleRun : IRun
  29. {
  30. /// <summary>
  31. /// Get the code points of this run.
  32. /// </summary>
  33. public Slice<int> CodePoints => CodePointBuffer.SubSlice(Start, Length);
  34. /// <summary>
  35. /// Get the text of this style run
  36. /// </summary>
  37. /// <returns>A string</returns>
  38. public override string ToString()
  39. {
  40. return Utf32Utils.FromUtf32(CodePoints);
  41. }
  42. /// <summary>
  43. /// The index of the first code point in this run (relative to the text block
  44. /// as a whole).
  45. /// </summary>
  46. public int Start
  47. {
  48. get;
  49. internal set;
  50. }
  51. /// <summary>
  52. /// The number of code points this run.
  53. /// </summary>
  54. public int Length
  55. {
  56. get;
  57. internal set;
  58. }
  59. /// <summary>
  60. /// The index of the first code point after this run.
  61. /// </summary>
  62. public int End => Start + Length;
  63. /// <summary>
  64. /// The style attributes to be applied to text in this run.
  65. /// </summary>
  66. public IStyle Style
  67. {
  68. get;
  69. internal set;
  70. }
  71. int IRun.Offset => Start;
  72. int IRun.Length => Length;
  73. /// <summary>
  74. /// The global list of code points
  75. /// </summary>
  76. internal Buffer<int> CodePointBuffer;
  77. internal static ThreadLocal<ObjectPool<StyleRun>> Pool = new ThreadLocal<ObjectPool<StyleRun>>(() => new ObjectPool<StyleRun>()
  78. {
  79. Cleaner = (r) =>
  80. {
  81. r.CodePointBuffer = null;
  82. r.Style = null;
  83. }
  84. });
  85. }
  86. }