/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict";
// This is a UA widget. It runs in per-origin UA widget scope, // to be loaded by UAWidgetsChild.sys.mjs.
// TODO Bug 1770438 - The <img> element does not currently let child elements be // sized relative to the size of the containing <img> element. It would be better // to teach the <img> element how to do this. For the prototype, do the more expensive // operation of getting the bounding client rect, and handle the positioning manually. const imgRect = this.element.getBoundingClientRect();
div.style.width = imgRect.width + "px";
div.style.height = imgRect.height + "px";
canvas.style.width = imgRect.width + "px";
canvas.style.height = imgRect.height + "px";
// The ctx is only available when redrawing the canvas. This is operation is only // done when necessary, as it can be expensive. /** @type {null | CanvasRenderingContext2D} */
let ctx = null;
if ( // The canvas hasn't been drawn to yet. this.lastCanvasStyleWidth === null || // Only redraw when the image has grown 25% larger. This percentage was chosen // as it visually seemed to work well, with the canvas never appearing blurry // when manually testing it.
imgRect.width > this.lastCanvasStyleWidth * 1.25
) { const dpr = this.window.devicePixelRatio;
canvas.width = imgRect.width * dpr;
canvas.height = imgRect.height * dpr; this.lastCanvasStyleWidth = imgRect.width;
for (const span of spans) {
let spanRect = this.spanRects.get(span); if (!spanRect) { // This only needs to happen once.
spanRect = span.getBoundingClientRect(); this.spanRects.set(span, spanRect);
}
const points = span.dataset.points.split(",").map(p => Number(p)); // Use the points in the string, e.g. // "0.0275349,0.14537,0.0275349,0.244662,0.176966,0.244565,0.176966,0.145273" // 0 1 2 3 4 5 6 7 // ^ bottomleft ^ topleft ^ topright ^ bottomright
let [
bottomLeftX,
bottomLeftY,
topLeftX,
topLeftY,
topRightX,
topRightY,
bottomRightX,
bottomRightY,
] = points;
if (ctx) { // This composite operation will cut out the quads. The color is arbitrary.
ctx.globalCompositeOperation = "destination-out";
ctx.fillStyle = "#ffffff";
ctx.fill();
// Creating a round line will grow the selection slightly, and round the corners.
ctx.lineWidth = 10;
ctx.lineJoin = "round";
ctx.strokeStyle = "#ffffff";
ctx.stroke();
}
}
lazilyInitialize() { if (this.isInitialized) { return;
} this.isInitialized = true;
const parser = newthis.window.DOMParser();
let parserDoc = parser.parseFromString(
`<div class="textrecognition" xmlns="http://www.w3.org/1999/xhtml" role="none">
<link rel="stylesheet" href="chrome://global/skin/media/textrecognition.css" />
<canvas />
<!-- The spans will be reattached here -->
</div>`, "application/xml"
); if ( this.shadowRoot.children.length !== 1 || this.shadowRoot.firstChild.tagName !== "DIV"
) { thrownew Error( "Expected the shadowRoot to have a single div as the root element."
);
}
const spansDiv = this.shadowRoot.firstChild; // Example layout of spansDiv: // <div> // <span data-points="0.0275349,0.14537,0.0275349,0.244662,0.176966,0.244565,0.176966,0.145273"> // Text that has been recognized // </span> // ... // </div>
spansDiv.remove();
this.shadowRoot.importNodeAndAppendChildAt( this.shadowRoot,
parserDoc.documentElement, true/* deep */
);
this.shadowRoot.importNodeAndAppendChildAt( this.shadowRoot.firstChild,
spansDiv, true/* deep */
);
}
};
/** * A three dimensional vector. * * @typedef {[number, number, number]} Vec3
*/
/** * @param {Matrix3} a * @param {Matrix3} b * @returns {Matrix3}
*/ function multiplyMat3(a, b) {
let out = []; for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) {
let sum = 0; for (let k = 0; k < 3; k++) {
sum += a[3 * i + k] * b[3 * k + j];
}
out[3 * i + j] = sum;
}
} return out;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.