bidi.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* Copyright 2012 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { warn } from "../shared/util.js";
  16. // Character types for symbols from 0000 to 00FF.
  17. // Source: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
  18. // prettier-ignore
  19. const baseTypes = [
  20. "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "S", "B", "S",
  21. "WS", "B", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN",
  22. "BN", "BN", "BN", "BN", "B", "B", "B", "S", "WS", "ON", "ON", "ET",
  23. "ET", "ET", "ON", "ON", "ON", "ON", "ON", "ES", "CS", "ES", "CS", "CS",
  24. "EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "CS", "ON",
  25. "ON", "ON", "ON", "ON", "ON", "L", "L", "L", "L", "L", "L", "L", "L",
  26. "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
  27. "L", "L", "L", "L", "ON", "ON", "ON", "ON", "ON", "ON", "L", "L", "L",
  28. "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
  29. "L", "L", "L", "L", "L", "L", "L", "L", "L", "ON", "ON", "ON", "ON",
  30. "BN", "BN", "BN", "BN", "BN", "BN", "B", "BN", "BN", "BN", "BN", "BN",
  31. "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN",
  32. "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "CS", "ON", "ET",
  33. "ET", "ET", "ET", "ON", "ON", "ON", "ON", "L", "ON", "ON", "BN", "ON",
  34. "ON", "ET", "ET", "EN", "EN", "ON", "L", "ON", "ON", "ON", "EN", "L",
  35. "ON", "ON", "ON", "ON", "ON", "L", "L", "L", "L", "L", "L", "L", "L",
  36. "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
  37. "L", "ON", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
  38. "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
  39. "L", "L", "L", "L", "L", "ON", "L", "L", "L", "L", "L", "L", "L", "L"
  40. ];
  41. // Character types for symbols from 0600 to 06FF.
  42. // Source: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
  43. // Note that 061D does not exist in the Unicode standard (see
  44. // http://unicode.org/charts/PDF/U0600.pdf), so we replace it with an
  45. // empty string and issue a warning if we encounter this character. The
  46. // empty string is required to properly index the items after it.
  47. // prettier-ignore
  48. const arabicTypes = [
  49. "AN", "AN", "AN", "AN", "AN", "AN", "ON", "ON", "AL", "ET", "ET", "AL",
  50. "CS", "AL", "ON", "ON", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM",
  51. "NSM", "NSM", "NSM", "NSM", "AL", "AL", "", "AL", "AL", "AL", "AL", "AL",
  52. "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
  53. "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
  54. "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
  55. "AL", "AL", "AL", "AL", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM",
  56. "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM",
  57. "NSM", "NSM", "NSM", "NSM", "AN", "AN", "AN", "AN", "AN", "AN", "AN",
  58. "AN", "AN", "AN", "ET", "AN", "AN", "AL", "AL", "AL", "NSM", "AL", "AL",
  59. "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
  60. "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
  61. "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
  62. "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
  63. "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
  64. "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
  65. "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
  66. "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
  67. "AL", "AL", "AL", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "AN",
  68. "ON", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "AL", "AL", "NSM", "NSM",
  69. "ON", "NSM", "NSM", "NSM", "NSM", "AL", "AL", "EN", "EN", "EN", "EN",
  70. "EN", "EN", "EN", "EN", "EN", "EN", "AL", "AL", "AL", "AL", "AL", "AL"
  71. ];
  72. function isOdd(i) {
  73. return (i & 1) !== 0;
  74. }
  75. function isEven(i) {
  76. return (i & 1) === 0;
  77. }
  78. function findUnequal(arr, start, value) {
  79. let j, jj;
  80. for (j = start, jj = arr.length; j < jj; ++j) {
  81. if (arr[j] !== value) {
  82. return j;
  83. }
  84. }
  85. return j;
  86. }
  87. function setValues(arr, start, end, value) {
  88. for (let j = start; j < end; ++j) {
  89. arr[j] = value;
  90. }
  91. }
  92. function reverseValues(arr, start, end) {
  93. for (let i = start, j = end - 1; i < j; ++i, --j) {
  94. const temp = arr[i];
  95. arr[i] = arr[j];
  96. arr[j] = temp;
  97. }
  98. }
  99. function createBidiText(str, isLTR, vertical = false) {
  100. let dir = "ltr";
  101. if (vertical) {
  102. dir = "ttb";
  103. } else if (!isLTR) {
  104. dir = "rtl";
  105. }
  106. return { str, dir };
  107. }
  108. // These are used in bidi(), which is called frequently. We re-use them on
  109. // each call to avoid unnecessary allocations.
  110. const chars = [];
  111. const types = [];
  112. function bidi(str, startLevel = -1, vertical = false) {
  113. let isLTR = true;
  114. const strLength = str.length;
  115. if (strLength === 0 || vertical) {
  116. return createBidiText(str, isLTR, vertical);
  117. }
  118. // Get types and fill arrays
  119. chars.length = strLength;
  120. types.length = strLength;
  121. let numBidi = 0;
  122. let i, ii;
  123. for (i = 0; i < strLength; ++i) {
  124. chars[i] = str.charAt(i);
  125. const charCode = str.charCodeAt(i);
  126. let charType = "L";
  127. if (charCode <= 0x00ff) {
  128. charType = baseTypes[charCode];
  129. } else if (0x0590 <= charCode && charCode <= 0x05f4) {
  130. charType = "R";
  131. } else if (0x0600 <= charCode && charCode <= 0x06ff) {
  132. charType = arabicTypes[charCode & 0xff];
  133. if (!charType) {
  134. warn("Bidi: invalid Unicode character " + charCode.toString(16));
  135. }
  136. } else if (0x0700 <= charCode && charCode <= 0x08ac) {
  137. charType = "AL";
  138. }
  139. if (charType === "R" || charType === "AL" || charType === "AN") {
  140. numBidi++;
  141. }
  142. types[i] = charType;
  143. }
  144. // Detect the bidi method
  145. // - If there are no rtl characters then no bidi needed
  146. // - If less than 30% chars are rtl then string is primarily ltr,
  147. // unless the string is very short.
  148. // - If more than 30% chars are rtl then string is primarily rtl
  149. if (numBidi === 0) {
  150. isLTR = true;
  151. return createBidiText(str, isLTR);
  152. }
  153. if (startLevel === -1) {
  154. if (numBidi / strLength < 0.3 && strLength > 4) {
  155. isLTR = true;
  156. startLevel = 0;
  157. } else {
  158. isLTR = false;
  159. startLevel = 1;
  160. }
  161. }
  162. const levels = [];
  163. for (i = 0; i < strLength; ++i) {
  164. levels[i] = startLevel;
  165. }
  166. /*
  167. X1-X10: skip most of this, since we are NOT doing the embeddings.
  168. */
  169. const e = isOdd(startLevel) ? "R" : "L";
  170. const sor = e;
  171. const eor = sor;
  172. /*
  173. W1. Examine each non-spacing mark (NSM) in the level run, and change the
  174. type of the NSM to the type of the previous character. If the NSM is at the
  175. start of the level run, it will get the type of sor.
  176. */
  177. let lastType = sor;
  178. for (i = 0; i < strLength; ++i) {
  179. if (types[i] === "NSM") {
  180. types[i] = lastType;
  181. } else {
  182. lastType = types[i];
  183. }
  184. }
  185. /*
  186. W2. Search backwards from each instance of a European number until the
  187. first strong type (R, L, AL, or sor) is found. If an AL is found, change
  188. the type of the European number to Arabic number.
  189. */
  190. lastType = sor;
  191. let t;
  192. for (i = 0; i < strLength; ++i) {
  193. t = types[i];
  194. if (t === "EN") {
  195. types[i] = lastType === "AL" ? "AN" : "EN";
  196. } else if (t === "R" || t === "L" || t === "AL") {
  197. lastType = t;
  198. }
  199. }
  200. /*
  201. W3. Change all ALs to R.
  202. */
  203. for (i = 0; i < strLength; ++i) {
  204. t = types[i];
  205. if (t === "AL") {
  206. types[i] = "R";
  207. }
  208. }
  209. /*
  210. W4. A single European separator between two European numbers changes to a
  211. European number. A single common separator between two numbers of the same
  212. type changes to that type:
  213. */
  214. for (i = 1; i < strLength - 1; ++i) {
  215. if (types[i] === "ES" && types[i - 1] === "EN" && types[i + 1] === "EN") {
  216. types[i] = "EN";
  217. }
  218. if (
  219. types[i] === "CS" &&
  220. (types[i - 1] === "EN" || types[i - 1] === "AN") &&
  221. types[i + 1] === types[i - 1]
  222. ) {
  223. types[i] = types[i - 1];
  224. }
  225. }
  226. /*
  227. W5. A sequence of European terminators adjacent to European numbers changes
  228. to all European numbers:
  229. */
  230. for (i = 0; i < strLength; ++i) {
  231. if (types[i] === "EN") {
  232. // do before
  233. for (let j = i - 1; j >= 0; --j) {
  234. if (types[j] !== "ET") {
  235. break;
  236. }
  237. types[j] = "EN";
  238. }
  239. // do after
  240. for (let j = i + 1; j < strLength; ++j) {
  241. if (types[j] !== "ET") {
  242. break;
  243. }
  244. types[j] = "EN";
  245. }
  246. }
  247. }
  248. /*
  249. W6. Otherwise, separators and terminators change to Other Neutral:
  250. */
  251. for (i = 0; i < strLength; ++i) {
  252. t = types[i];
  253. if (t === "WS" || t === "ES" || t === "ET" || t === "CS") {
  254. types[i] = "ON";
  255. }
  256. }
  257. /*
  258. W7. Search backwards from each instance of a European number until the
  259. first strong type (R, L, or sor) is found. If an L is found, then change
  260. the type of the European number to L.
  261. */
  262. lastType = sor;
  263. for (i = 0; i < strLength; ++i) {
  264. t = types[i];
  265. if (t === "EN") {
  266. types[i] = lastType === "L" ? "L" : "EN";
  267. } else if (t === "R" || t === "L") {
  268. lastType = t;
  269. }
  270. }
  271. /*
  272. N1. A sequence of neutrals takes the direction of the surrounding strong
  273. text if the text on both sides has the same direction. European and Arabic
  274. numbers are treated as though they were R. Start-of-level-run (sor) and
  275. end-of-level-run (eor) are used at level run boundaries.
  276. */
  277. for (i = 0; i < strLength; ++i) {
  278. if (types[i] === "ON") {
  279. const end = findUnequal(types, i + 1, "ON");
  280. let before = sor;
  281. if (i > 0) {
  282. before = types[i - 1];
  283. }
  284. let after = eor;
  285. if (end + 1 < strLength) {
  286. after = types[end + 1];
  287. }
  288. if (before !== "L") {
  289. before = "R";
  290. }
  291. if (after !== "L") {
  292. after = "R";
  293. }
  294. if (before === after) {
  295. setValues(types, i, end, before);
  296. }
  297. i = end - 1; // reset to end (-1 so next iteration is ok)
  298. }
  299. }
  300. /*
  301. N2. Any remaining neutrals take the embedding direction.
  302. */
  303. for (i = 0; i < strLength; ++i) {
  304. if (types[i] === "ON") {
  305. types[i] = e;
  306. }
  307. }
  308. /*
  309. I1. For all characters with an even (left-to-right) embedding direction,
  310. those of type R go up one level and those of type AN or EN go up two
  311. levels.
  312. I2. For all characters with an odd (right-to-left) embedding direction,
  313. those of type L, EN or AN go up one level.
  314. */
  315. for (i = 0; i < strLength; ++i) {
  316. t = types[i];
  317. if (isEven(levels[i])) {
  318. if (t === "R") {
  319. levels[i] += 1;
  320. } else if (t === "AN" || t === "EN") {
  321. levels[i] += 2;
  322. }
  323. } else {
  324. // isOdd
  325. if (t === "L" || t === "AN" || t === "EN") {
  326. levels[i] += 1;
  327. }
  328. }
  329. }
  330. /*
  331. L1. On each line, reset the embedding level of the following characters to
  332. the paragraph embedding level:
  333. segment separators,
  334. paragraph separators,
  335. any sequence of whitespace characters preceding a segment separator or
  336. paragraph separator, and any sequence of white space characters at the end
  337. of the line.
  338. */
  339. // don't bother as text is only single line
  340. /*
  341. L2. From the highest level found in the text to the lowest odd level on
  342. each line, reverse any contiguous sequence of characters that are at that
  343. level or higher.
  344. */
  345. // find highest level & lowest odd level
  346. let highestLevel = -1;
  347. let lowestOddLevel = 99;
  348. let level;
  349. for (i = 0, ii = levels.length; i < ii; ++i) {
  350. level = levels[i];
  351. if (highestLevel < level) {
  352. highestLevel = level;
  353. }
  354. if (lowestOddLevel > level && isOdd(level)) {
  355. lowestOddLevel = level;
  356. }
  357. }
  358. // now reverse between those limits
  359. for (level = highestLevel; level >= lowestOddLevel; --level) {
  360. // find segments to reverse
  361. let start = -1;
  362. for (i = 0, ii = levels.length; i < ii; ++i) {
  363. if (levels[i] < level) {
  364. if (start >= 0) {
  365. reverseValues(chars, start, i);
  366. start = -1;
  367. }
  368. } else if (start < 0) {
  369. start = i;
  370. }
  371. }
  372. if (start >= 0) {
  373. reverseValues(chars, start, levels.length);
  374. }
  375. }
  376. /*
  377. L3. Combining marks applied to a right-to-left base character will at this
  378. point precede their base character. If the rendering engine expects them to
  379. follow the base characters in the final display process, then the ordering
  380. of the marks and the base character must be reversed.
  381. */
  382. // don't bother for now
  383. /*
  384. L4. A character that possesses the mirrored property as specified by
  385. Section 4.7, Mirrored, must be depicted by a mirrored glyph if the resolved
  386. directionality of that character is R.
  387. */
  388. // don't mirror as characters are already mirrored in the pdf
  389. // Finally, return string
  390. for (i = 0, ii = chars.length; i < ii; ++i) {
  391. const ch = chars[i];
  392. if (ch === "<" || ch === ">") {
  393. chars[i] = "";
  394. }
  395. }
  396. return createBidiText(chars.join(""), isLTR);
  397. }
  398. export { bidi };