mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-28 23:28:16 +02:00
Support rich content in markup annotation
- use the xfa parser but in the xhtml namespace.
This commit is contained in:
parent
0e7614df7f
commit
cf8dc750d6
14 changed files with 188 additions and 39 deletions
|
@ -20,6 +20,7 @@ import {
|
|||
$content,
|
||||
$extra,
|
||||
$getChildren,
|
||||
$getParent,
|
||||
$globalData,
|
||||
$nodeName,
|
||||
$onText,
|
||||
|
@ -38,6 +39,7 @@ import {
|
|||
import { getMeasurement, HTMLResult, stripQuotes } from "./utils.js";
|
||||
|
||||
const XHTML_NS_ID = NamespaceIds.xhtml.id;
|
||||
const $richText = Symbol();
|
||||
|
||||
const VALID_STYLES = new Set([
|
||||
"color",
|
||||
|
@ -109,6 +111,7 @@ const StyleMapping = new Map([
|
|||
|
||||
const spacesRegExp = /\s+/g;
|
||||
const crlfRegExp = /[\r\n]+/g;
|
||||
const crlfForRichTextRegExp = /\r\n?/g;
|
||||
|
||||
function mapStyle(styleStr, node) {
|
||||
const style = Object.create(null);
|
||||
|
@ -185,6 +188,7 @@ const NoWhites = new Set(["body", "html"]);
|
|||
class XhtmlObject extends XmlObject {
|
||||
constructor(attributes, name) {
|
||||
super(XHTML_NS_ID, name);
|
||||
this[$richText] = false;
|
||||
this.style = attributes.style || "";
|
||||
}
|
||||
|
||||
|
@ -197,11 +201,16 @@ class XhtmlObject extends XmlObject {
|
|||
return !NoWhites.has(this[$nodeName]);
|
||||
}
|
||||
|
||||
[$onText](str) {
|
||||
str = str.replace(crlfRegExp, "");
|
||||
if (!this.style.includes("xfa-spacerun:yes")) {
|
||||
str = str.replace(spacesRegExp, " ");
|
||||
[$onText](str, richText = false) {
|
||||
if (!richText) {
|
||||
str = str.replace(crlfRegExp, "");
|
||||
if (!this.style.includes("xfa-spacerun:yes")) {
|
||||
str = str.replace(spacesRegExp, " ");
|
||||
}
|
||||
} else {
|
||||
this[$richText] = true;
|
||||
}
|
||||
|
||||
if (str) {
|
||||
this[$content] += str;
|
||||
}
|
||||
|
@ -311,6 +320,15 @@ class XhtmlObject extends XmlObject {
|
|||
return HTMLResult.EMPTY;
|
||||
}
|
||||
|
||||
let value;
|
||||
if (this[$richText]) {
|
||||
value = this[$content]
|
||||
? this[$content].replace(crlfForRichTextRegExp, "\n")
|
||||
: undefined;
|
||||
} else {
|
||||
value = this[$content] || undefined;
|
||||
}
|
||||
|
||||
return HTMLResult.success({
|
||||
name: this[$nodeName],
|
||||
attributes: {
|
||||
|
@ -318,7 +336,7 @@ class XhtmlObject extends XmlObject {
|
|||
style: mapStyle(this.style, this),
|
||||
},
|
||||
children,
|
||||
value: this[$content] || "",
|
||||
value,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -457,6 +475,10 @@ class P extends XhtmlObject {
|
|||
}
|
||||
|
||||
[$text]() {
|
||||
const siblings = this[$getParent]()[$getChildren]();
|
||||
if (siblings[siblings.length - 1] === this) {
|
||||
return super[$text]();
|
||||
}
|
||||
return super[$text]() + "\n";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue