mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-26 10:08:06 +02:00
[Annotation] Add a div containing the text of a FreeText annotation (bug 1780375)
An annotation doesn't have to be in the text flow, hence it's likely a bad idea to insert its text in the text layer. But the text must be visible from a screen reader point of view so it must somewhere in the DOM. So with this patch, the text from a FreeText annotation is extracted and added in a div in its HTML counterpart, and with the patch #15237 the text should be visible and positioned relatively to the text flow.
This commit is contained in:
parent
159f853e06
commit
31155740c3
7 changed files with 178 additions and 20 deletions
|
@ -941,6 +941,57 @@ class Annotation {
|
|||
return null;
|
||||
}
|
||||
|
||||
get hasTextContent() {
|
||||
return false;
|
||||
}
|
||||
|
||||
async extractTextContent(evaluator, task, viewBox) {
|
||||
if (!this.appearance) {
|
||||
return;
|
||||
}
|
||||
|
||||
const resources = await this.loadResources(
|
||||
["ExtGState", "Font", "Properties", "XObject"],
|
||||
this.appearance
|
||||
);
|
||||
|
||||
const text = [];
|
||||
const buffer = [];
|
||||
const sink = {
|
||||
desiredSize: Math.Infinity,
|
||||
ready: true,
|
||||
|
||||
enqueue(chunk, size) {
|
||||
for (const item of chunk.items) {
|
||||
buffer.push(item.str);
|
||||
if (item.hasEOL) {
|
||||
text.push(buffer.join(""));
|
||||
buffer.length = 0;
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
await evaluator.getTextContent({
|
||||
stream: this.appearance,
|
||||
task,
|
||||
resources,
|
||||
includeMarkedContent: true,
|
||||
combineTextItems: true,
|
||||
sink,
|
||||
viewBox,
|
||||
});
|
||||
this.reset();
|
||||
|
||||
if (buffer.length) {
|
||||
text.push(buffer.join(""));
|
||||
}
|
||||
|
||||
if (text.length > 0) {
|
||||
this.data.textContent = text;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get field data for usage in JS sandbox.
|
||||
*
|
||||
|
@ -3250,6 +3301,10 @@ class FreeTextAnnotation extends MarkupAnnotation {
|
|||
this.data.annotationType = AnnotationType.FREETEXT;
|
||||
}
|
||||
|
||||
get hasTextContent() {
|
||||
return !!this.appearance;
|
||||
}
|
||||
|
||||
static createNewDict(annotation, xref, { apRef, ap }) {
|
||||
const { color, fontSize, rect, rotation, user, value } = annotation;
|
||||
const freetext = new Dict(xref);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue