Skip to content

Commit

Permalink
[Editor] Guess alt text even when showing the dialog is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Aug 2, 2024
1 parent 501da85 commit 9a4b7fc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
50 changes: 48 additions & 2 deletions src/display/editor/stamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,54 @@ 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() {
if (this.hasAltTextData()) {
return;
}

const { mlManager } = this._uiManager;
if (!mlManager || !(await mlManager.isEnabledFor("altText"))) {
return;
}

// TODO: get this value from Firefox
// (https://bugzilla.mozilla.org/show_bug.cgi?id=1908184)
const AI_MAX_IMAGE_DIMENSION = 224;
const {
imageData: { width, height, data },
} = this.copyCanvas(AI_MAX_IMAGE_DIMENSION, /* createImageData = */ true);
const response = await mlManager.guess({
name: "altText",
request: {
data,
width,
height,
channels: data.length / (width * height),
},
});
if (!response || response.error || !response.output) {
return;
}
const altText = response.output;
await this.setGuessedAltText(altText);
if (!this.hasAltTextData()) {
this.altTextData = { alt: altText, decorative: false };
}
}

Expand Down
12 changes: 0 additions & 12 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion web/new_alt_text_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class NewAltTextManager {

// When calling #mlGuessAltText we don't wait for it, so we must take care
// that the alt text dialog can have been closed before the response is.
const response = await this.#uiManager.mlGuess({
const response = await this.#uiManager.mlManager.guess({
name: "altText",
request: {
data,
Expand Down

0 comments on commit 9a4b7fc

Please sign in to comment.