PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/addons/web/static/lib/pdfjs/src/core/bidi.js

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