bidi.js
8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { lst } from "./misc.js"
// BIDI HELPERS
export function iterateBidiSections(order, from, to, f) {
if (!order) return f(from, to, "ltr", 0)
let found = false
for (let i = 0; i < order.length; ++i) {
let part = order[i]
if (part.from < to && part.to > from || from == to && part.to == from) {
f(Math.max(part.from, from), Math.min(part.to, to), part.level == 1 ? "rtl" : "ltr", i)
found = true
}
}
if (!found) f(from, to, "ltr")
}
export let bidiOther = null
export function getBidiPartAt(order, ch, sticky) {
let found
bidiOther = null
for (let i = 0; i < order.length; ++i) {
let cur = order[i]
if (cur.from < ch && cur.to > ch) return i
if (cur.to == ch) {
if (cur.from != cur.to && sticky == "before") found = i
else bidiOther = i
}
if (cur.from == ch) {
if (cur.from != cur.to && sticky != "before") found = i
else bidiOther = i
}
}
return found != null ? found : bidiOther
}
// Bidirectional ordering algorithm
// See http://unicode.org/reports/tr9/tr9-13.html for the algorithm
// that this (partially) implements.
// One-char codes used for character types:
// L (L): Left-to-Right
// R (R): Right-to-Left
// r (AL): Right-to-Left Arabic
// 1 (EN): European Number
// + (ES): European Number Separator
// % (ET): European Number Terminator
// n (AN): Arabic Number
// , (CS): Common Number Separator
// m (NSM): Non-Spacing Mark
// b (BN): Boundary Neutral
// s (B): Paragraph Separator
// t (S): Segment Separator
// w (WS): Whitespace
// N (ON): Other Neutrals
// Returns null if characters are ordered as they appear
// (left-to-right), or an array of sections ({from, to, level}
// objects) in the order in which they occur visually.
let bidiOrdering = (function() {
// Character types for codepoints 0 to 0xff
let lowTypes = "bbbbbbbbbtstwsbbbbbbbbbbbbbbssstwNN%%%NNNNNN,N,N1111111111NNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNbbbbbbsbbbbbbbbbbbbbbbbbbbbbbbbbb,N%%%%NNNNLNNNNN%%11NLNNN1LNNNNNLLLLLLLLLLLLLLLLLLLLLLLNLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLN"
// Character types for codepoints 0x600 to 0x6f9
let arabicTypes = "nnnnnnNNr%%r,rNNmmmmmmmmmmmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmmmmmmmmmmmmmmmnnnnnnnnnn%nnrrrmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmnNmmmmmmrrmmNmmmmrr1111111111"
function charType(code) {
if (code <= 0xf7) return lowTypes.charAt(code)
else if (0x590 <= code && code <= 0x5f4) return "R"
else if (0x600 <= code && code <= 0x6f9) return arabicTypes.charAt(code - 0x600)
else if (0x6ee <= code && code <= 0x8ac) return "r"
else if (0x2000 <= code && code <= 0x200b) return "w"
else if (code == 0x200c) return "b"
else return "L"
}
let bidiRE = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac]/
let isNeutral = /[stwN]/, isStrong = /[LRr]/, countsAsLeft = /[Lb1n]/, countsAsNum = /[1n]/
function BidiSpan(level, from, to) {
this.level = level
this.from = from; this.to = to
}
return function(str, direction) {
let outerType = direction == "ltr" ? "L" : "R"
if (str.length == 0 || direction == "ltr" && !bidiRE.test(str)) return false
let len = str.length, types = []
for (let i = 0; i < len; ++i)
types.push(charType(str.charCodeAt(i)))
// W1. Examine each non-spacing mark (NSM) in the level run, and
// change the type of the NSM to the type of the previous
// character. If the NSM is at the start of the level run, it will
// get the type of sor.
for (let i = 0, prev = outerType; i < len; ++i) {
let type = types[i]
if (type == "m") types[i] = prev
else prev = type
}
// W2. Search backwards from each instance of a European number
// until the first strong type (R, L, AL, or sor) is found. If an
// AL is found, change the type of the European number to Arabic
// number.
// W3. Change all ALs to R.
for (let i = 0, cur = outerType; i < len; ++i) {
let type = types[i]
if (type == "1" && cur == "r") types[i] = "n"
else if (isStrong.test(type)) { cur = type; if (type == "r") types[i] = "R" }
}
// W4. A single European separator between two European numbers
// changes to a European number. A single common separator between
// two numbers of the same type changes to that type.
for (let i = 1, prev = types[0]; i < len - 1; ++i) {
let type = types[i]
if (type == "+" && prev == "1" && types[i+1] == "1") types[i] = "1"
else if (type == "," && prev == types[i+1] &&
(prev == "1" || prev == "n")) types[i] = prev
prev = type
}
// W5. A sequence of European terminators adjacent to European
// numbers changes to all European numbers.
// W6. Otherwise, separators and terminators change to Other
// Neutral.
for (let i = 0; i < len; ++i) {
let type = types[i]
if (type == ",") types[i] = "N"
else if (type == "%") {
let end
for (end = i + 1; end < len && types[end] == "%"; ++end) {}
let replace = (i && types[i-1] == "!") || (end < len && types[end] == "1") ? "1" : "N"
for (let j = i; j < end; ++j) types[j] = replace
i = end - 1
}
}
// W7. Search backwards from each instance of a European number
// until the first strong type (R, L, or sor) is found. If an L is
// found, then change the type of the European number to L.
for (let i = 0, cur = outerType; i < len; ++i) {
let type = types[i]
if (cur == "L" && type == "1") types[i] = "L"
else if (isStrong.test(type)) cur = type
}
// N1. A sequence of neutrals takes the direction of the
// surrounding strong text if the text on both sides has the same
// direction. European and Arabic numbers act as if they were R in
// terms of their influence on neutrals. Start-of-level-run (sor)
// and end-of-level-run (eor) are used at level run boundaries.
// N2. Any remaining neutrals take the embedding direction.
for (let i = 0; i < len; ++i) {
if (isNeutral.test(types[i])) {
let end
for (end = i + 1; end < len && isNeutral.test(types[end]); ++end) {}
let before = (i ? types[i-1] : outerType) == "L"
let after = (end < len ? types[end] : outerType) == "L"
let replace = before == after ? (before ? "L" : "R") : outerType
for (let j = i; j < end; ++j) types[j] = replace
i = end - 1
}
}
// Here we depart from the documented algorithm, in order to avoid
// building up an actual levels array. Since there are only three
// levels (0, 1, 2) in an implementation that doesn't take
// explicit embedding into account, we can build up the order on
// the fly, without following the level-based algorithm.
let order = [], m
for (let i = 0; i < len;) {
if (countsAsLeft.test(types[i])) {
let start = i
for (++i; i < len && countsAsLeft.test(types[i]); ++i) {}
order.push(new BidiSpan(0, start, i))
} else {
let pos = i, at = order.length, isRTL = direction == "rtl" ? 1 : 0
for (++i; i < len && types[i] != "L"; ++i) {}
for (let j = pos; j < i;) {
if (countsAsNum.test(types[j])) {
if (pos < j) { order.splice(at, 0, new BidiSpan(1, pos, j)); at += isRTL }
let nstart = j
for (++j; j < i && countsAsNum.test(types[j]); ++j) {}
order.splice(at, 0, new BidiSpan(2, nstart, j))
at += isRTL
pos = j
} else ++j
}
if (pos < i) order.splice(at, 0, new BidiSpan(1, pos, i))
}
}
if (direction == "ltr") {
if (order[0].level == 1 && (m = str.match(/^\s+/))) {
order[0].from = m[0].length
order.unshift(new BidiSpan(0, 0, m[0].length))
}
if (lst(order).level == 1 && (m = str.match(/\s+$/))) {
lst(order).to -= m[0].length
order.push(new BidiSpan(0, len - m[0].length, len))
}
}
return direction == "rtl" ? order.reverse() : order
}
})()
// Get the bidi ordering for the given line (and cache it). Returns
// false for lines that are fully left-to-right, and an array of
// BidiSpan objects otherwise.
export function getOrder(line, direction) {
let order = line.order
if (order == null) order = line.order = bidiOrdering(line.text, direction)
return order
}