Skip to content

Commit

Permalink
[Annotations] Add some aria-owns in the text layer to link to annotat…
Browse files Browse the repository at this point in the history
…ions (bug 1780375)

This patch doesn't structurally change the text layer: it just adds some aria-owns
attributes to some spans.
The aria-owns attribute expect to have an element id, hence it's why it adds back an
id on the element rendering an annotation, but this id is built in using crypto.randomUUID
to avoid any potential issues with the hash in the url.
The elements in the annotation layer are moved into the DOM in order to have them in the
same "order" as they visually are.
The overall goal is to help screen readers to present to the user the annotations as
they visually are and as they come in the text flow.
It is clearly not perfect, but it should improve readability for some people with visual
disabilities.
  • Loading branch information
calixteman committed Aug 12, 2022
1 parent cef2ac9 commit f316300
Show file tree
Hide file tree
Showing 23 changed files with 436 additions and 246 deletions.
45 changes: 40 additions & 5 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
warn,
} from "../shared/util.js";
import {
AnnotationPrefix,
DOMSVGFactory,
getFilenameFromUrl,
PDFDateString,
Expand Down Expand Up @@ -1901,7 +1902,8 @@ class PopupElement {
}
if (this.hideElement.hidden) {
this.hideElement.hidden = false;
this.container.style.zIndex += 1;
this.container.style.zIndex =
parseInt(this.container.style.zIndex) + 1000;
}
}

Expand All @@ -1918,7 +1920,8 @@ class PopupElement {
}
if (!this.hideElement.hidden && !this.pinned) {
this.hideElement.hidden = true;
this.container.style.zIndex -= 1;
this.container.style.zIndex =
parseInt(this.container.style.zIndex) - 1000;
}
}
}
Expand Down Expand Up @@ -2465,6 +2468,19 @@ class FileAttachmentAnnotationElement extends AnnotationElement {
*/

class AnnotationLayer {
static #appendElement(element, id, div, accessibilityManager) {
const contentElement = element.firstChild || element;
contentElement.id = `${AnnotationPrefix}${id}`;

div.append(element);
accessibilityManager?.moveElementInDOM(
div,
element,
contentElement,
/* isRemovable = */ false
);
}

/**
* Render a new annotation layer with all annotation elements.
*
Expand All @@ -2473,9 +2489,10 @@ class AnnotationLayer {
* @memberof AnnotationLayer
*/
static render(parameters) {
const { annotations, div, viewport } = parameters;
const { annotations, div, viewport, accessibilityManager } = parameters;

this.#setDimensions(div, viewport);
let zIndex = 0;

for (const data of annotations) {
if (data.annotationType !== AnnotationType.POPUP) {
Expand Down Expand Up @@ -2508,15 +2525,33 @@ class AnnotationLayer {
}
if (Array.isArray(rendered)) {
for (const renderedElement of rendered) {
div.append(renderedElement);
renderedElement.style.zIndex = zIndex++;
AnnotationLayer.#appendElement(
renderedElement,
data.id,
div,
accessibilityManager
);
}
} else {
// The accessibility manager will move the annotation in the DOM in
// order to match the visual ordering.
// But if an annotation is above an other one, then we must draw it
// after the other one whatever the order is in the DOM, hence the
// use of the z-index.
rendered.style.zIndex = zIndex++;

if (element instanceof PopupAnnotationElement) {
// Popup annotation elements should not be on top of other
// annotation elements to prevent interfering with mouse events.
div.prepend(rendered);
} else {
div.append(rendered);
AnnotationLayer.#appendElement(
rendered,
data.id,
div,
accessibilityManager
);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { BaseException, stringToBytes, Util, warn } from "../shared/util.js";

const SVG_NS = "http://www.w3.org/2000/svg";

const AnnotationPrefix = "pdfjs_internal_id_";

class PixelsPerInch {
static CSS = 96.0;

Expand Down Expand Up @@ -652,6 +654,7 @@ function getCurrentTransformInverse(ctx) {
}

export {
AnnotationPrefix,
binarySearchFirstItem,
deprecated,
DOMCanvasFactory,
Expand Down
Loading

0 comments on commit f316300

Please # to comment.