1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

Guess alt text even when showing the dialog is disabled

This commit is contained in:
Calixte Denizet 2024-08-02 10:27:21 +02:00
parent b80e552760
commit d2c519e57a
3 changed files with 59 additions and 33 deletions

View file

@ -133,9 +133,52 @@ class StampEditor extends AnnotationEditor {
) {
this._editToolbar.hide();
this._uiManager.editAltText(this, /* firstTime = */ true);
} else {
this.div.focus();
return;
}
if (
!this._uiManager.useNewAltTextWhenAddingImage &&
this._uiManager.useNewAltTextFlow &&
this.#bitmap
) {
// The alt-text dialog isn't opened but we still want to guess the alt
// text.
this.mlGuessAltText();
}
this.div.focus();
}
async mlGuessAltText(imageData = null, updateAltTextData = true) {
if (this.hasAltTextData()) {
return null;
}
const { mlManager } = this._uiManager;
if (!mlManager || !(await mlManager.isEnabledFor("altText"))) {
return null;
}
const { data, width, height } =
imageData ||
this.copyCanvas(null, /* createImageData = */ true).imageData;
const response = await mlManager.guess({
name: "altText",
request: {
data,
width,
height,
channels: data.length / (width * height),
},
});
if (!response || response.error || !response.output) {
return null;
}
const altText = response.output;
await this.setGuessedAltText(altText);
if (updateAltTextData && !this.hasAltTextData()) {
this.altTextData = { alt: altText, decorative: false };
}
return altText;
}
#getBitmap() {
@ -370,6 +413,13 @@ class StampEditor extends AnnotationEditor {
}
copyCanvas(maxDimension, createImageData = false) {
if (!maxDimension) {
// TODO: get this value from Firefox
// (https://bugzilla.mozilla.org/show_bug.cgi?id=1908184)
// It's the maximum dimension that the AI can handle.
maxDimension = 224;
}
const { width: bitmapWidth, height: bitmapHeight } = this.#bitmap;
const canvas = document.createElement("canvas");

View file

@ -856,18 +856,6 @@ class AnnotationEditorUIManager {
}
}
hasMLManager() {
return !!this.#mlManager;
}
async mlGuess(data) {
return this.#mlManager?.guess(data) || null;
}
async isMLEnabledFor(name) {
return !!(await this.#mlManager?.isEnabledFor(name));
}
get mlManager() {
return this.#mlManager;
}