-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
235 lines (206 loc) · 6.47 KB
/
index.html
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta charset="utf-8">
<title>BiDi Demo</title>
<script src="bidi.js"></script>
<style>
#text-input,
#colorized-input {
font-size: 3rem;
}
#text-input {
border: dashed black 1px;
}
#colorized-input {
margin-top: 5px;
text-align: center;
}
#dir-button {
align-self: flex-start;
height: 50px;
margin: 5px;
flex-grow: 1;
}
#input-container {
flex-grow: 30;
overflow-wrap: anywhere;
}
#input-container>* {
margin-bottom: 10px;
}
#description {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div id="description">
<div><span style="color: blue;">Blue</span>: Left-To-Right</div>
<div><span style="color: red;">Red</span>: Right-To-Left</div>
<div><span style="color: gray;">Gray</span>: Neutral</div>
<div><span style="color: green;">Green</span>: Weak</div>
<div><span style="color: #d4d400;">Yellow</span>: Directional Formatting</div>
</div>
<div style="display: flex;">
<div id="input-container">
<div id="text-input" contenteditable="" spellcheck="false"></div>
<!-- <textarea id="text-input" cols="50"></textarea> -->
<details open="true">
<summary>Insert BiDi Characters</summary>
<div id="bidi-char-buttons">
<button title="Left-To-Right Mark" data-value="‎">LRM</button>
<button title="Right-To-Left Mark" data-value="‏">RLM</button>
<button title="Arabic Letter Mark" data-value="؜">ALM</button>
<button title="Left-To-Right Embedding" data-value="‪">LRE</button>
<button title="Right-To-Left Embedding" data-value="‫">RLE</button>
<button title="Left-To-Right Override" data-value="‭">LRO</button>
<button title="Right-To-Left Override" data-value="‮">RLO</button>
<button title="Pop Directional Formatting" data-value="‬">PDF</button>
<button title="Left-To-Right Isolate" data-value="⁦">LRI</button>
<button title="Right-To-Left Isolate" data-value="⁧">RLI</button>
<button title="First Strong Isolate" data-value="⁨">FSI</button>
<button title="Pop Directional Isolate" data-value="⁩">PDI</button>
</div>
</details>
<div id="colorized-input"></div>
</div>
<button id="dir-button">Switch Direction</button>
</div>
<canvas id="canvas" width="1000" height="800"></canvas>
<script>
const bidi = bidi_js();
const input = document.getElementById("text-input");
const colorizedInput = document.getElementById("colorized-input");
const inputContainer = document.getElementById("input-container");
const dirButton = document.getElementById("dir-button");
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
setCanvasDimensions(document.body.clientWidth, document.body.clientHeight);
ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
window.addEventListener('resize', () => {
// Reset transformation matrix
ctx.setTransform(1, 0, 0, 1, 0, 0);
setCanvasDimensions(document.body.clientWidth, document.body.clientHeight);
ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
draw();
});
input.addEventListener("input", draw);
dirButton.addEventListener("click", e => {
inputContainer.dir = inputContainer.dir == "rtl" ? "ltr" : "rtl";
// recalculate embedding levels
colorizeLevels();
});
const bidiButtons = document.querySelectorAll("#bidi-char-buttons > button");
for (const button of bidiButtons) {
button.addEventListener("click", e => {
input.focus();
document.execCommand("insertText", false, e.target.dataset.value);
draw();
});
}
function draw() {
const text = input.textContent;
colorizeLevels();
const rectWidth = 25;
const rectHeight = 35;
const offsetX = 10;
let offsetY = 25;
const gapX = 10;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = 'red';
ctx.fillStyle = 'black';
let j = 0;
for (let i = 0; i < text.length; i++) {
if ((offsetX + j * (rectWidth + gapX) + rectWidth) * window.devicePixelRatio >= canvas.width) {
offsetY += 70;
j = 0;
}
const bidiType = bidi.getBidiCharTypeName(text[i]);
ctx.strokeStyle = colorForChar(bidiType);
ctx.strokeRect(offsetX + j * (rectWidth + gapX), offsetY, rectWidth, rectHeight);
ctx.fillText(text[i], offsetX + j * (rectWidth + gapX) + rectWidth / 2 - 5, offsetY - 10);
let num = (i + 1).toString();
let numWidth = ctx.measureText(num).width;
ctx.fillText(num, offsetX + j * (rectWidth + gapX) + rectWidth / 2 - numWidth / 2, offsetY +
rectHeight / 2);
let bidiTypeWidth = ctx.measureText(bidiType).width;
ctx.fillText(bidiType, offsetX + j * (rectWidth + gapX) + rectWidth / 2 - bidiTypeWidth / 2, offsetY +
rectHeight - 5);
j++;
}
}
const bidiColors = {
// Left-To-Right characters
'L': 'blue',
// Right-To-Left characters
'R': 'red',
'AL': 'red',
// Weak characters
'EN': 'green',
'ES': 'green',
'ET': 'green',
'AN': 'green',
'CS': 'green',
'NSM': 'green',
'BN': 'green',
// Neutral characters
'B': 'gray',
'S': 'gray',
'WS': 'gray',
'ON': 'gray',
// Directional formatting characters
'LRE': 'yellow',
'LRO': 'yellow',
'RLE': 'yellow',
'RLO': 'yellow',
'PDF': 'yellow',
'LRI': 'yellow',
'RLI': 'yellow',
'FSI': 'yellow',
'PDI': 'yellow',
};
function colorizeLevels() {
const text = input.textContent;
colorizedInput.innerHTML = '';
var embeddingLevels = bidi.getEmbeddingLevels(
text,
inputContainer.dir,
)
const {
levels,
paragraphs
} = embeddingLevels;
let i = 0;
while (i < text.length) {
let currentLevel = levels[i];
const dir = levelDir(currentLevel);
let j = i + 1;
while (j < text.length && dir === levelDir(levels[j])) {
j++;
}
let node = document.createElement("span");
node.style.backgroundColor = bidiColors[dir];
node.style.border = "solid 1px black";
let levelText = text.slice(i, j);
node.textContent = levelText;
colorizedInput.appendChild(node);
i = j;
}
}
function levelDir(level) {
return level % 2 ? 'R' : 'L';
}
function colorForChar(bidiType) {
let color = bidiColors[bidiType];
if (!color) color = 'black';
return color;
}
function setCanvasDimensions(width, height) {
canvas.width = width - canvas.offsetLeft;
canvas.height = height - canvas.offsetTop;
}
</script>
</body>
</html>