BidiData.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.Tasks;
  20. using Topten.RichTextKit.Utils;
  21. namespace Topten.RichTextKit
  22. {
  23. /// <summary>
  24. /// Represents a unicode string and all associated attributes
  25. /// for each character required for the Bidi algorithm
  26. /// </summary>
  27. class BidiData
  28. {
  29. /// <summary>
  30. /// Construct a new empty BidiData
  31. /// </summary>
  32. public BidiData()
  33. {
  34. _types = new Buffer<Directionality>();
  35. _pairedBracketTypes= new Buffer<PairedBracketType>();
  36. _pairedBracketValues = new Buffer<int>();
  37. }
  38. List<int> _paragraphPositions = new List<int>();
  39. sbyte _paragraphEmbeddingLevel;
  40. public sbyte ParagraphEmbeddingLevel => _paragraphEmbeddingLevel;
  41. bool _hasBrackets;
  42. public bool HasBrackets => _hasBrackets;
  43. bool _hasEmbeddings;
  44. public bool HasEmbeddings => _hasEmbeddings;
  45. bool _hasIsolates;
  46. public bool HasIsolates => _hasIsolates;
  47. /// <summary>
  48. /// Initialize with an array of Unicode code points
  49. /// </summary>
  50. /// <param name="codePoints">The unicode code points to be processed</param>
  51. /// <param name="paragraphEmbeddingLevel">The paragraph embedding level</param>
  52. public void Init(Slice<int> codePoints, sbyte paragraphEmbeddingLevel)
  53. {
  54. // Set working buffer sizes
  55. _types.Length = codePoints.Length;
  56. _pairedBracketTypes.Length = codePoints.Length;
  57. _pairedBracketValues.Length = codePoints.Length;
  58. _paragraphPositions.Clear();
  59. _paragraphEmbeddingLevel = paragraphEmbeddingLevel;
  60. // Resolve the directionality, paired bracket type and paired bracket values for
  61. // all code points
  62. _hasBrackets = false;
  63. _hasEmbeddings = false;
  64. _hasIsolates = false;
  65. for (int i = 0; i < codePoints.Length; i++)
  66. {
  67. var bidiData = UnicodeClasses.BidiData(codePoints[i]);
  68. // Look up directionality
  69. var dir = (Directionality)(bidiData >> 24);
  70. _types[i] = dir;
  71. switch (dir)
  72. {
  73. case Directionality.LRE:
  74. case Directionality.LRO:
  75. case Directionality.RLE:
  76. case Directionality.RLO:
  77. case Directionality.PDF:
  78. _hasEmbeddings = true;
  79. break;
  80. case Directionality.LRI:
  81. case Directionality.RLI:
  82. case Directionality.FSI:
  83. case Directionality.PDI:
  84. _hasIsolates = true;
  85. break;
  86. }
  87. // Lookup paired bracket types
  88. var pbt = (PairedBracketType)((bidiData >> 16) & 0xFF);
  89. _pairedBracketTypes[i] = pbt;
  90. switch (pbt)
  91. {
  92. case PairedBracketType.o:
  93. _pairedBracketValues[i] = MapCanon((int)(bidiData & 0xFFFF));
  94. _hasBrackets = true;
  95. break;
  96. case PairedBracketType.c:
  97. _pairedBracketValues[i] = MapCanon(codePoints[i]);
  98. _hasBrackets = true;
  99. break;
  100. }
  101. /*
  102. if (_types[i] == RichTextKit.Directionality.B)
  103. {
  104. _types[i] = (Directionality)Directionality.WS;
  105. _paragraphPositions.Add(i);
  106. }
  107. */
  108. }
  109. // Create slices on work buffers
  110. Types = _types.AsSlice();
  111. PairedBracketTypes = _pairedBracketTypes.AsSlice();
  112. PairedBracketValues = _pairedBracketValues.AsSlice();
  113. }
  114. /// <summary>
  115. /// Map bracket types 0x3008 and 0x3009 to their canonical equivalents
  116. /// </summary>
  117. /// <param name="codePoint">The code point to be mapped</param>
  118. /// <returns>The mapped canonical code point, or the passed code point</returns>
  119. static int MapCanon(int codePoint)
  120. {
  121. if (codePoint == 0x3008)
  122. return 0x2329;
  123. if (codePoint == 0x3009)
  124. return 0x232A;
  125. else
  126. return codePoint;
  127. }
  128. /// <summary>
  129. /// Get the length of the data held by the BidiData
  130. /// </summary>
  131. public int Length => _types.Length;
  132. Buffer<Directionality> _types;
  133. Buffer<PairedBracketType> _pairedBracketTypes;
  134. Buffer<int> _pairedBracketValues;
  135. Buffer<Directionality> _savedTypes;
  136. Buffer<PairedBracketType> _savedPairedBracketTypes;
  137. /// <summary>
  138. /// Save the Types and PairedBracketTypes of this bididata
  139. /// </summary>
  140. /// <remarks>
  141. /// This is used when processing embedded style runs with
  142. /// directionality overrides. TextBlock saves the data,
  143. /// overrides the style runs to neutral, processes the bidi
  144. /// data for the entire paragraph and then restores this data
  145. /// before processing the embedded runs.
  146. /// </remarks>
  147. public void SaveTypes()
  148. {
  149. // Make sure we have a buffer
  150. if (_savedTypes == null)
  151. {
  152. _savedTypes = new Buffer<Directionality>();
  153. _savedPairedBracketTypes = new Buffer<PairedBracketType>();
  154. }
  155. // Capture the types data
  156. _savedTypes.Clear();
  157. _savedTypes.Add(_types.AsSlice());
  158. _savedPairedBracketTypes.Clear();
  159. _savedPairedBracketTypes.Add(_pairedBracketTypes.AsSlice());
  160. }
  161. /// <summary>
  162. /// Restore the data saved by SaveTypes
  163. /// </summary>
  164. public void RestoreTypes()
  165. {
  166. _types.Clear();
  167. _types.Add(_savedTypes.AsSlice());
  168. _pairedBracketTypes.Clear();
  169. _pairedBracketTypes.Add(_savedPairedBracketTypes.AsSlice());
  170. }
  171. /// <summary>
  172. /// The directionality of each code point
  173. /// </summary>
  174. public Slice<Directionality> Types;
  175. /// <summary>
  176. /// The paired bracket type for each code point
  177. /// </summary>
  178. public Slice<PairedBracketType> PairedBracketTypes;
  179. /// <summary>
  180. /// The paired bracket value for code code point
  181. /// </summary>
  182. /// <remarks>
  183. /// The paired bracket values are the code points
  184. /// of each character where the opening code point
  185. /// is replaced with the closing code point for easier
  186. /// matching. Also, bracket code points are mapped
  187. /// to their canonical equivalents
  188. /// </remarks>
  189. public Slice<int> PairedBracketValues;
  190. public Buffer<sbyte> _tempLevelBuffer;
  191. /// <summary>
  192. /// Gets a temporary level buffer. Used by TextBlock when
  193. /// resolving style runs with different directionality.
  194. /// </summary>
  195. /// <param name="length">Length of the required buffer</param>
  196. /// <returns>An uninitialized level buffer</returns>
  197. public Slice<sbyte> GetTempLevelBuffer(int length)
  198. {
  199. if (_tempLevelBuffer == null)
  200. _tempLevelBuffer = new Buffer<sbyte>();
  201. _tempLevelBuffer.Clear();
  202. return _tempLevelBuffer.Add(length, false);
  203. }
  204. }
  205. }