forked from robertknight/ocrs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.ts
More file actions
602 lines (525 loc) · 18.9 KB
/
content.ts
File metadata and controls
602 lines (525 loc) · 18.9 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import type { LineRecResult, RotatedRect, WordRecResult } from "./types";
type TextPosition = { textNode: Text; offset: number };
/**
* Return true if the point `(x, y)` is contained within `r`.
*
* The left/top edges are treated as "inside" the rect and the bottom/right
* edges as "outside". This ensures that for adjacent rects, a point will only
* lie within one rect.
*/
function rectContains(r: DOMRect, x: number, y: number) {
return x >= r.left && x < r.right && y >= r.top && y < r.bottom;
}
/**
* Return the text node and offset of the character at the point `(x, y)` in
* client coordinates.
*/
function textPositionFromPoint(
container: Element,
x: number,
y: number,
): TextPosition | null {
// TODO - Optimize this using `{Document, ShadowRoot}.elementsFromPoint` to
// filter the node list.
const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT);
let currentNode;
const range = new Range();
while ((currentNode = walker.nextNode())) {
const text = currentNode as Text;
const str = text.nodeValue!;
for (let i = 0; i < str.length; i++) {
range.setStart(text, i);
range.setEnd(text, i + 1);
const charRect = range.getBoundingClientRect();
if (rectContains(charRect, x, y)) {
return { textNode: text, offset: i };
}
}
}
return null;
}
/**
* Return the smallest axis-aligned rect that contains all corners of a
* rotated rect.
*/
function domRectFromRotatedRect(coords: RotatedRect): DOMRect {
const [x0, y0, x1, y1, x2, y2, x3, y3] = coords;
const left = Math.min(x0, x1, x2, x3);
const top = Math.min(y0, y1, y2, y3);
const right = Math.max(x0, x1, x2, x3);
const bottom = Math.max(y0, y1, y2, y3);
return new DOMRect(left, top, right - left, bottom - top);
}
/** Font used for transparent text layer content. */
const fixedFont = {
size: 16,
family: "sans-serif",
};
/**
* Create a line of selectable text in the transparent text layer.
*/
function createTextLine(line: LineRecResult): HTMLElement {
const lineEl = document.createElement("div");
lineEl.className = "text-line";
const { left, top, right, bottom } = domRectFromRotatedRect(line.coords);
Object.assign(lineEl.style, {
// Position transparent line above text
position: "absolute",
left: `${left}px`,
top: `${top}px`,
width: `${right - left}px`,
height: `${bottom - top}px`,
// Avoid line break if word elements don't quite fit. Also preserve spaces
// at the end of the line.
whiteSpace: "pre",
// Use a fixed font. This needs to match the font used when measuring the
// natural width of text.
fontSize: `${fixedFont.size}px`,
fontFamily: fixedFont.family,
// Make text transparent
color: "rgb(0 0 0 / 0)",
});
// Create canvas for measuring natural size of text.
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d")!;
context.font = `${fixedFont.size}px ${fixedFont.family}`;
const spaceWidth = context.measureText(" ").width;
// Add words to the line as inline-block elements. This allows us to create
// normal text selection behavior while adjusting the positioning and size
// of words to match the underlying pixels.
let prevWordRect: DOMRect | undefined;
let prevWordEl: HTMLElement | undefined;
for (const [wordIndex, word] of line.words.entries()) {
const wordRect = domRectFromRotatedRect(word.coords);
const leftMargin = prevWordRect
? wordRect.left - prevWordRect.right - spaceWidth
: wordRect.left - left;
// Create outer element for word. This sets the width and margin used for
// inline layout.
const wordEl = document.createElement("span");
Object.assign(wordEl.style, {
display: "inline-block",
marginLeft: leftMargin != null ? `${leftMargin}px` : undefined,
marginTop: `${wordRect.top - top}px`,
width: `${wordRect.width}px`,
height: `${wordRect.height}px`,
// Align top of word box with top of line.
verticalAlign: "top",
});
const metrics = context.measureText(word.text);
const xScale = wordRect.width / metrics.width;
const yScale = wordRect.height / fixedFont.size;
// Create inner element for word. This uses a transform to make the rendered
// size match the underlying text pixels. The transform doesn't affect
// layout. The inner and outer elements are separated so that the scale
// transform is not applied to the hit box for the outer element, as that
// would make the hit box's size `(width * xScale, height * yScale)`, which
// can interfere with selection of subsequent words.
const wordInner = document.createElement("span");
wordInner.textContent = word.text;
Object.assign(wordInner.style, {
display: "inline-block",
transformOrigin: "top left",
transform: `scale(${xScale}, ${yScale})`,
});
wordEl.append(wordInner);
lineEl.append(wordEl);
prevWordEl = wordEl;
prevWordRect = wordRect;
// Add space between words. We add this even after the last word in a line
// to ensure there is a space between the end of one line and the start of
// the next in a multi-line selection.
const spaceEl = document.createElement("span");
let spaceXScale = 1;
if (wordIndex < line.words.length - 1) {
const nextWordRect = domRectFromRotatedRect(
line.words[wordIndex + 1].coords,
);
const targetSpaceWidth = nextWordRect.left - wordRect.right;
spaceXScale = targetSpaceWidth / spaceWidth;
}
Object.assign(spaceEl.style, {
display: "inline-block",
// Align top of space with top of preceding word.
marginTop: `${wordRect.top - top}px`,
verticalAlign: "top",
// Scale the space to match the height of the word, and the width between
// the current and next words.
transformOrigin: "top left",
transform: `scale(${spaceXScale}, ${yScale})`,
});
spaceEl.textContent = " ";
lineEl.append(spaceEl);
}
return lineEl;
}
export type TextOverlay = {
/** A signal that fires when the overlay is removed. */
dismissed: AbortSignal;
/** Remove the overlay. */
remove(): void;
/**
* Set the recognized text for a line, or `null` if no text is found.
*
* This can be used by the OCR engine to supply recognition results before
* they have been requested as a result of eg. the user hovering a line,
* avoiding latency when recognition is needed later on.
*/
setLineText(lineIndex: number, recResult: LineRecResult | null): void;
};
/**
* Configuration for the text overlay.
*/
export type TextOverlayOptions = {
/** Coordinates of detected text lines. */
lineCoords: RotatedRect[];
/**
* Callback to run recognition on a line of text and return the recognition
* results, or `null` if no text was recognized.
*/
recognizeText(lineIndex: number): Promise<LineRecResult | null>;
/**
* A custom container for the overlay. This should be a positioned element,
* which the overlay will fill. If not specified, the overlay is sized to fill
* the viewport and placed either in `document.body` or the top layer element
* (eg. the fullscreen element) if there is one.
*/
container?: HTMLElement;
/**
* Whether the overlay is automatically dismissed when you click outside of
* it. Defaults to true.
*/
autoDismiss?: boolean;
};
/** The active overlay in the current document. */
let activeOverlay: TextOverlay | null = null;
/**
* Remove the active overlay in the current document.
*/
export function dismissTextOverlay() {
activeOverlay?.remove();
activeOverlay = null;
}
/**
* Return the container element into which the text overlay should be placed.
*
* By default this is the document body, but if there is a top layer [1] active,
* the overlay needs to be placed in that to be visible.
*
* [1] https://developer.mozilla.org/en-US/docs/Glossary/Top_layer
*/
function getOverlayParent() {
if (document.fullscreenElement) {
return document.fullscreenElement;
}
// Other ways of creating a top layer that are not yet handled:
// - `HTMLDialogElement.showModal()`
// - `HTMLElement.showPopover()`
return document.body;
}
/**
* Create an overlay which shows the location of OCR-ed text in the viewport
* and enables the user to select and copy text from it.
*
* Only one overlay is supported in the document at a time, and if there is
* already an existing active overlay when this function is called, it is
* dismissed.
*/
export function createTextOverlay({
recognizeText,
lineCoords,
container,
autoDismiss = true,
}: TextOverlayOptions): TextOverlay {
dismissTextOverlay();
// Pending recognition requests. These are processed in LIFO order as
// requests are triggered in response to user interactions (eg. hovering a
// text line) and so we want to give priority to the most recently hovered
// line.
const pendingRecRequests: Array<{
lineIndex: number;
resolve: (result: LineRecResult | null) => void;
}> = [];
let pendingRecTimer: number | undefined;
const flushPendingRequests = () => {
while (pendingRecRequests.length > 0) {
const req = pendingRecRequests.pop()!;
recognizeText(req.lineIndex).then((result) => req.resolve(result));
}
pendingRecTimer = undefined;
};
// Schedule recognition of a line. We buffer requests that happen close
// together and process them in LIFO order.
const scheduleRecognition = (lineIndex: number) => {
let resolve;
const recResult = new Promise<LineRecResult | null>((resolve_) => {
resolve = resolve_;
});
pendingRecRequests.push({ lineIndex, resolve: resolve! });
clearTimeout(pendingRecTimer);
// nb. Node typings for `setTimeout` are incorrectly being used here.
pendingRecTimer = setTimeout(
flushPendingRequests,
100,
) as unknown as number;
return recResult;
};
const canvasContainer = document.createElement("div");
Object.assign(canvasContainer.style, {
// Override default styles from the page.
all: "initial",
// Display overlay above other elements. If there is a top layer active,
// then we also need to ensure the element is added to that layer.
zIndex: 9999,
});
if (container) {
// Make the overlay fill the custom container.
Object.assign(canvasContainer.style, {
position: "absolute",
top: "0",
left: "0",
right: "0",
bottom: "0",
});
} else {
// Position the overlay so that it fills the viewport, but scrolls with
// the page contents. This allows the user to read parts of the page that
// were OCR-ed, without disrupting the selection in the part that has been.
//
// A known issue with this is that when the page is scrolled, text in the
// overlay will become mis-aligned with underlying pixels that belong to
// fixed-positioned elements.
Object.assign(canvasContainer.style, {
position: "absolute",
top: `${document.documentElement.scrollTop}px`,
left: `${document.documentElement.scrollLeft}px`,
width: `${window.innerWidth}px`,
height: `${window.innerHeight}px`,
});
}
// Use a shadow root to insulate children from page styles.
canvasContainer.attachShadow({ mode: "open" });
const overlayParent = container ?? getOverlayParent();
overlayParent.append(canvasContainer);
const canvas = document.createElement("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
Object.assign(canvas.style, {
position: "absolute",
top: "0",
left: "0",
right: "0",
bottom: "0",
});
canvasContainer.shadowRoot!.append(canvas);
// Draw text layer backdrop.
const ctx = canvas.getContext("2d")!;
ctx.fillStyle = "rgb(0 0 0 / .3)";
ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);
// Map of line index to:
// 1) Recognized text, if recognition is complete
// 2) A promise if recognition is in progress
// 3) `null` if recognition completed but no text was recognized
const textCache = new Map<
number,
LineRecResult | Promise<LineRecResult | null> | null
>();
const rotatedRectPath = (coords: RotatedRect) => {
const [x0, y0, x1, y1, x2, y2, x3, y3] = coords;
const path = new Path2D();
path.moveTo(x0, y0);
path.lineTo(x1, y1);
path.lineTo(x2, y2);
path.lineTo(x3, y3);
path.closePath();
return path;
};
// Cut out holes in the backdrop where there is detected text.
ctx.save();
ctx.globalCompositeOperation = "destination-out";
ctx.fillStyle = "white";
const linePaths = lineCoords.map(rotatedRectPath);
for (const path of linePaths) {
ctx.fill(path);
}
ctx.restore();
const lineIndexFromPoint = (clientX: number, clientY: number) => {
const canvasRect = canvas.getBoundingClientRect();
const canvasX = clientX - canvasRect.left;
const canvasY = clientY - canvasRect.top;
return linePaths.findIndex((lp) => ctx.isPointInPath(lp, canvasX, canvasY));
};
// Track the start and end coordinates of the current mouse drag operation.
const primaryButtonPressed = (e: MouseEvent) => e.buttons & 1;
let dragStartAt: { x: number; y: number } | null = null;
let dragEndAt: { x: number; y: number } | null = null;
canvasContainer.onmousedown = (e) => {
if (!dragStartAt) {
dragStartAt = { x: e.x, y: e.y };
}
};
canvasContainer.onmousemove = (e) => {
if (primaryButtonPressed(e)) {
dragEndAt = { x: e.x, y: e.y };
}
};
canvasContainer.onmouseup = (e) => {
dragEndAt = null;
dragStartAt = null;
};
canvasContainer.onmouseenter = (e) => {
if (primaryButtonPressed(e)) {
dragStartAt = { x: e.x, y: e.y };
} else {
dragStartAt = null;
}
};
/**
* Save recognition results for a line and, if not null, create the
* transparent text line allowing the user to select text.
*/
const initTextLine = (lineIndex: number, recResult: LineRecResult | null) => {
textCache.set(lineIndex, recResult);
if (!recResult) {
return;
}
const lineEl = createTextLine(recResult);
lineEl.setAttribute("data-line-index", lineIndex.toString());
// Insert line such that the DOM order is the same as the output order
// from the OCR lib, which produces lines in reading order. This makes
// text selection across lines and columns flow properly, provided that
// the OCR lib detected the reading order correctly.
const successor = Array.from(textLines.entries())
.sort((a, b) => a[0] - b[0])
.find(([entryLine, entryEl]) => entryLine >= lineIndex);
const successorNode = successor ? successor[1] : null;
textLayer.insertBefore(lineEl, successorNode);
textLines.set(lineIndex, lineEl);
// If a drag operation was in progress, update the selection once text
// recognition is completed, to include the newly recognized text.
if (dragStartAt && dragEndAt) {
const start = textPositionFromPoint(
textLayer,
dragStartAt.x,
dragStartAt.y,
);
const end = textPositionFromPoint(textLayer, dragEndAt.x, dragEndAt.y);
const selection = document.getSelection();
if (selection && start && end) {
selection.setBaseAndExtent(
start.textNode,
start.offset,
end.textNode,
end.offset,
);
}
}
};
// Perform on-demand recognition when the user hovers a line of text that has
// not yet been recognized.
const recognizeLine = async (
lineIndex: number,
): Promise<LineRecResult | null> => {
const cachedResult = textCache.get(lineIndex);
if (cachedResult !== undefined) {
return cachedResult;
}
const recPromise = scheduleRecognition(lineIndex);
textCache.set(lineIndex, recPromise);
const recResult = await recPromise;
initTextLine(lineIndex, recResult);
return recResult;
};
// Create the hidden text layer in which the user can select text.
const textLayer = document.createElement("div");
canvasContainer.shadowRoot!.append(textLayer);
const textLines = new Map<number, HTMLElement>();
let prevLine = -1;
canvas.onmousemove = async (e) => {
const currentLine = lineIndexFromPoint(e.clientX, e.clientY);
if (currentLine === prevLine) {
return;
}
prevLine = currentLine;
if (currentLine === -1) {
return;
}
// Recognize text for the current line, if just hovering, or all lines
// between the start and end point of the current drag operation if a mouse
// button is pressed.
const dragStartLine = dragStartAt
? lineIndexFromPoint(dragStartAt.x, dragStartAt.y)
: currentLine;
const startLine =
dragStartLine !== -1 ? Math.min(dragStartLine, currentLine) : currentLine;
const endLine =
dragStartLine !== -1 ? Math.max(dragStartLine, currentLine) : currentLine;
for (let lineIndex = startLine; lineIndex <= endLine; lineIndex++) {
if (!textCache.has(lineIndex)) {
// TODO: We currently recognize a single line at a time here, but
// recognition is more efficient if batches of similarly-sized lines
// are recognized at once.
recognizeLine(lineIndex);
}
}
};
// Signal used to remove global listeners etc. when overlay is removed.
const overlayRemoved = new AbortController();
overlayRemoved.signal.addEventListener("abort", () => {
canvasContainer.remove();
clearTimeout(pendingRecTimer);
});
const removeOverlay = () => overlayRemoved.abort();
const autoDismissOverlay = () => {
if (autoDismiss) {
removeOverlay();
}
};
// Dismiss overlay when user clicks on the backdrop, but not inside text or
// other UI elements in the overlay.
canvas.onclick = (e) => {
// Don't dismiss the overlay if the user started a drag action (eg. to
// select text), but happened to finish on the canvas instead of inside a
// text element.
if (dragStartAt) {
const dragDist = Math.sqrt(
(e.x - dragStartAt.x) ** 2 + (e.y - dragStartAt.y) ** 2,
);
if (dragDist >= 20) {
return;
}
}
// Don't dismiss the overlay if the user clicks inside a line that hasn't
// been recognized yet.
const lineIndex = lineIndexFromPoint(e.clientX, e.clientY);
if (lineIndex !== -1) {
return;
}
autoDismissOverlay();
};
// When the window is resized, the document layout will change and the OCR
// boxes will likely be incorrect, so just remove the overlay at that point.
window.addEventListener("resize", autoDismissOverlay, {
signal: overlayRemoved.signal,
});
document.addEventListener(
"keyup",
(e) => {
if (e.key === "Escape") {
autoDismissOverlay();
}
},
{ signal: overlayRemoved.signal },
);
activeOverlay = {
dismissed: overlayRemoved.signal,
setLineText: (lineIndex: number, recResult: LineRecResult) => {
if (textCache.has(lineIndex)) {
return;
}
initTextLine(lineIndex, recResult);
},
remove: removeOverlay,
};
return activeOverlay;
}