1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

Merge pull request #14074 from Snuffleupagus/issue-14046

[api-minor] Add basic support for RTL text-content in PopupAnnotations (issue 14046)
This commit is contained in:
Jonas Jenwald 2021-09-25 12:37:44 +02:00 committed by GitHub
commit b23b8d8a5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 124 additions and 57 deletions

View file

@ -47,6 +47,7 @@ import {
Name,
RefSet,
} from "./primitives.js";
import { bidi } from "./bidi.js";
import { Catalog } from "./catalog.js";
import { ColorSpace } from "./colorspace.js";
import { FileSpec } from "./file_spec.js";
@ -356,6 +357,7 @@ class Annotation {
constructor(params) {
const dict = params.dict;
this.setTitle(dict.get("T"));
this.setContents(dict.get("Contents"));
this.setModificationDate(dict.get("M"));
this.setFlags(dict.get("F"));
@ -374,7 +376,7 @@ class Annotation {
annotationFlags: this.flags,
borderStyle: this.borderStyle,
color: this.color,
contents: this.contents,
contentsObj: this._contents,
hasAppearance: !!this.appearance,
id: params.id,
modificationDate: this.modificationDate,
@ -500,17 +502,35 @@ class Annotation {
return this._isPrintable(this.flags);
}
/**
* @private
*/
_parseStringHelper(data) {
const str = typeof data === "string" ? stringToPDFString(data) : "";
const dir = str && bidi(str).dir === "rtl" ? "rtl" : "ltr";
return { str, dir };
}
/**
* Set the title.
*
* @param {string} title - The title of the annotation, used e.g. with
* PopupAnnotations.
*/
setTitle(title) {
this._title = this._parseStringHelper(title);
}
/**
* Set the contents.
*
* @public
* @memberof Annotation
* @param {string} contents - Text to display for the annotation or, if the
* type of annotation does not display text, a
* description of the annotation's contents
*/
setContents(contents) {
this.contents = stringToPDFString(contents || "");
this._contents = this._parseStringHelper(contents);
}
/**
@ -1014,10 +1034,11 @@ class MarkupAnnotation extends Annotation {
// the group attributes from the primary annotation.
const parent = dict.get("IRT");
this.data.title = stringToPDFString(parent.get("T") || "");
this.setTitle(parent.get("T"));
this.data.titleObj = this._title;
this.setContents(parent.get("Contents"));
this.data.contents = this.contents;
this.data.contentsObj = this._contents;
if (!parent.has("CreationDate")) {
this.data.creationDate = null;
@ -1043,7 +1064,7 @@ class MarkupAnnotation extends Annotation {
this.data.color = this.color;
}
} else {
this.data.title = stringToPDFString(dict.get("T") || "");
this.data.titleObj = this._title;
this.setCreationDate(dict.get("CreationDate"));
this.data.creationDate = this.creationDate;
@ -2405,8 +2426,11 @@ class PopupAnnotation extends Annotation {
}
}
this.data.title = stringToPDFString(parentItem.get("T") || "");
this.data.contents = stringToPDFString(parentItem.get("Contents") || "");
this.setTitle(parentItem.get("T"));
this.data.titleObj = this._title;
this.setContents(parentItem.get("Contents"));
this.data.contentsObj = this._contents;
}
}

View file

@ -120,7 +120,7 @@ function createBidiText(str, isLTR, vertical = false) {
const chars = [];
const types = [];
function bidi(str, startLevel, vertical) {
function bidi(str, startLevel = -1, vertical = false) {
let isLTR = true;
const strLength = str.length;
if (strLength === 0 || vertical) {