annotation_layer.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.AnnotationLayer = void 0;
  27. var _display_utils = require("./display_utils.js");
  28. var _util = require("../shared/util.js");
  29. class AnnotationElementFactory {
  30. static create(parameters) {
  31. const subtype = parameters.data.annotationType;
  32. switch (subtype) {
  33. case _util.AnnotationType.LINK:
  34. return new LinkAnnotationElement(parameters);
  35. case _util.AnnotationType.TEXT:
  36. return new TextAnnotationElement(parameters);
  37. case _util.AnnotationType.WIDGET:
  38. const fieldType = parameters.data.fieldType;
  39. switch (fieldType) {
  40. case "Tx":
  41. return new TextWidgetAnnotationElement(parameters);
  42. case "Btn":
  43. if (parameters.data.radioButton) {
  44. return new RadioButtonWidgetAnnotationElement(parameters);
  45. } else if (parameters.data.checkBox) {
  46. return new CheckboxWidgetAnnotationElement(parameters);
  47. }
  48. return new PushButtonWidgetAnnotationElement(parameters);
  49. case "Ch":
  50. return new ChoiceWidgetAnnotationElement(parameters);
  51. }
  52. return new WidgetAnnotationElement(parameters);
  53. case _util.AnnotationType.POPUP:
  54. return new PopupAnnotationElement(parameters);
  55. case _util.AnnotationType.FREETEXT:
  56. return new FreeTextAnnotationElement(parameters);
  57. case _util.AnnotationType.LINE:
  58. return new LineAnnotationElement(parameters);
  59. case _util.AnnotationType.SQUARE:
  60. return new SquareAnnotationElement(parameters);
  61. case _util.AnnotationType.CIRCLE:
  62. return new CircleAnnotationElement(parameters);
  63. case _util.AnnotationType.POLYLINE:
  64. return new PolylineAnnotationElement(parameters);
  65. case _util.AnnotationType.CARET:
  66. return new CaretAnnotationElement(parameters);
  67. case _util.AnnotationType.INK:
  68. return new InkAnnotationElement(parameters);
  69. case _util.AnnotationType.POLYGON:
  70. return new PolygonAnnotationElement(parameters);
  71. case _util.AnnotationType.HIGHLIGHT:
  72. return new HighlightAnnotationElement(parameters);
  73. case _util.AnnotationType.UNDERLINE:
  74. return new UnderlineAnnotationElement(parameters);
  75. case _util.AnnotationType.SQUIGGLY:
  76. return new SquigglyAnnotationElement(parameters);
  77. case _util.AnnotationType.STRIKEOUT:
  78. return new StrikeOutAnnotationElement(parameters);
  79. case _util.AnnotationType.STAMP:
  80. return new StampAnnotationElement(parameters);
  81. case _util.AnnotationType.FILEATTACHMENT:
  82. return new FileAttachmentAnnotationElement(parameters);
  83. default:
  84. return new AnnotationElement(parameters);
  85. }
  86. }
  87. }
  88. class AnnotationElement {
  89. constructor(parameters, isRenderable = false, ignoreBorder = false) {
  90. this.isRenderable = isRenderable;
  91. this.data = parameters.data;
  92. this.layer = parameters.layer;
  93. this.page = parameters.page;
  94. this.viewport = parameters.viewport;
  95. this.linkService = parameters.linkService;
  96. this.downloadManager = parameters.downloadManager;
  97. this.imageResourcesPath = parameters.imageResourcesPath;
  98. this.renderInteractiveForms = parameters.renderInteractiveForms;
  99. this.svgFactory = parameters.svgFactory;
  100. if (isRenderable) {
  101. this.container = this._createContainer(ignoreBorder);
  102. }
  103. }
  104. _createContainer(ignoreBorder = false) {
  105. const data = this.data,
  106. page = this.page,
  107. viewport = this.viewport;
  108. const container = document.createElement("section");
  109. let width = data.rect[2] - data.rect[0];
  110. let height = data.rect[3] - data.rect[1];
  111. container.setAttribute("data-annotation-id", data.id);
  112. const rect = _util.Util.normalizeRect([data.rect[0], page.view[3] - data.rect[1] + page.view[1], data.rect[2], page.view[3] - data.rect[3] + page.view[1]]);
  113. container.style.transform = `matrix(${viewport.transform.join(",")})`;
  114. container.style.transformOrigin = `-${rect[0]}px -${rect[1]}px`;
  115. if (!ignoreBorder && data.borderStyle.width > 0) {
  116. container.style.borderWidth = `${data.borderStyle.width}px`;
  117. if (data.borderStyle.style !== _util.AnnotationBorderStyleType.UNDERLINE) {
  118. width = width - 2 * data.borderStyle.width;
  119. height = height - 2 * data.borderStyle.width;
  120. }
  121. const horizontalRadius = data.borderStyle.horizontalCornerRadius;
  122. const verticalRadius = data.borderStyle.verticalCornerRadius;
  123. if (horizontalRadius > 0 || verticalRadius > 0) {
  124. const radius = `${horizontalRadius}px / ${verticalRadius}px`;
  125. container.style.borderRadius = radius;
  126. }
  127. switch (data.borderStyle.style) {
  128. case _util.AnnotationBorderStyleType.SOLID:
  129. container.style.borderStyle = "solid";
  130. break;
  131. case _util.AnnotationBorderStyleType.DASHED:
  132. container.style.borderStyle = "dashed";
  133. break;
  134. case _util.AnnotationBorderStyleType.BEVELED:
  135. (0, _util.warn)("Unimplemented border style: beveled");
  136. break;
  137. case _util.AnnotationBorderStyleType.INSET:
  138. (0, _util.warn)("Unimplemented border style: inset");
  139. break;
  140. case _util.AnnotationBorderStyleType.UNDERLINE:
  141. container.style.borderBottomStyle = "solid";
  142. break;
  143. default:
  144. break;
  145. }
  146. if (data.color) {
  147. container.style.borderColor = _util.Util.makeCssRgb(data.color[0] | 0, data.color[1] | 0, data.color[2] | 0);
  148. } else {
  149. container.style.borderWidth = 0;
  150. }
  151. }
  152. container.style.left = `${rect[0]}px`;
  153. container.style.top = `${rect[1]}px`;
  154. container.style.width = `${width}px`;
  155. container.style.height = `${height}px`;
  156. return container;
  157. }
  158. _createPopup(container, trigger, data) {
  159. if (!trigger) {
  160. trigger = document.createElement("div");
  161. trigger.style.height = container.style.height;
  162. trigger.style.width = container.style.width;
  163. container.appendChild(trigger);
  164. }
  165. const popupElement = new PopupElement({
  166. container,
  167. trigger,
  168. color: data.color,
  169. title: data.title,
  170. modificationDate: data.modificationDate,
  171. contents: data.contents,
  172. hideWrapper: true
  173. });
  174. const popup = popupElement.render();
  175. popup.style.left = container.style.width;
  176. container.appendChild(popup);
  177. }
  178. render() {
  179. (0, _util.unreachable)("Abstract method `AnnotationElement.render` called");
  180. }
  181. }
  182. class LinkAnnotationElement extends AnnotationElement {
  183. constructor(parameters) {
  184. const isRenderable = !!(parameters.data.url || parameters.data.dest || parameters.data.action);
  185. super(parameters, isRenderable);
  186. }
  187. render() {
  188. this.container.className = "linkAnnotation";
  189. const {
  190. data,
  191. linkService
  192. } = this;
  193. const link = document.createElement("a");
  194. if (data.url) {
  195. (0, _display_utils.addLinkAttributes)(link, {
  196. url: data.url,
  197. target: data.newWindow ? _display_utils.LinkTarget.BLANK : linkService.externalLinkTarget,
  198. rel: linkService.externalLinkRel,
  199. enabled: linkService.externalLinkEnabled
  200. });
  201. } else if (data.action) {
  202. this._bindNamedAction(link, data.action);
  203. } else {
  204. this._bindLink(link, data.dest);
  205. }
  206. this.container.appendChild(link);
  207. return this.container;
  208. }
  209. _bindLink(link, destination) {
  210. link.href = this.linkService.getDestinationHash(destination);
  211. link.onclick = () => {
  212. if (destination) {
  213. this.linkService.navigateTo(destination);
  214. }
  215. return false;
  216. };
  217. if (destination) {
  218. link.className = "internalLink";
  219. }
  220. }
  221. _bindNamedAction(link, action) {
  222. link.href = this.linkService.getAnchorUrl("");
  223. link.onclick = () => {
  224. this.linkService.executeNamedAction(action);
  225. return false;
  226. };
  227. link.className = "internalLink";
  228. }
  229. }
  230. class TextAnnotationElement extends AnnotationElement {
  231. constructor(parameters) {
  232. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  233. super(parameters, isRenderable);
  234. }
  235. render() {
  236. this.container.className = "textAnnotation";
  237. const image = document.createElement("img");
  238. image.style.height = this.container.style.height;
  239. image.style.width = this.container.style.width;
  240. image.src = this.imageResourcesPath + "annotation-" + this.data.name.toLowerCase() + ".svg";
  241. image.alt = "[{{type}} Annotation]";
  242. image.dataset.l10nId = "text_annotation_type";
  243. image.dataset.l10nArgs = JSON.stringify({
  244. type: this.data.name
  245. });
  246. if (!this.data.hasPopup) {
  247. this._createPopup(this.container, image, this.data);
  248. }
  249. this.container.appendChild(image);
  250. return this.container;
  251. }
  252. }
  253. class WidgetAnnotationElement extends AnnotationElement {
  254. render() {
  255. return this.container;
  256. }
  257. }
  258. class TextWidgetAnnotationElement extends WidgetAnnotationElement {
  259. constructor(parameters) {
  260. const isRenderable = parameters.renderInteractiveForms || !parameters.data.hasAppearance && !!parameters.data.fieldValue;
  261. super(parameters, isRenderable);
  262. }
  263. render() {
  264. const TEXT_ALIGNMENT = ["left", "center", "right"];
  265. this.container.className = "textWidgetAnnotation";
  266. let element = null;
  267. if (this.renderInteractiveForms) {
  268. if (this.data.multiLine) {
  269. element = document.createElement("textarea");
  270. element.textContent = this.data.fieldValue;
  271. } else {
  272. element = document.createElement("input");
  273. element.type = "text";
  274. element.setAttribute("value", this.data.fieldValue);
  275. }
  276. element.disabled = this.data.readOnly;
  277. if (this.data.maxLen !== null) {
  278. element.maxLength = this.data.maxLen;
  279. }
  280. if (this.data.comb) {
  281. const fieldWidth = this.data.rect[2] - this.data.rect[0];
  282. const combWidth = fieldWidth / this.data.maxLen;
  283. element.classList.add("comb");
  284. element.style.letterSpacing = `calc(${combWidth}px - 1ch)`;
  285. }
  286. } else {
  287. element = document.createElement("div");
  288. element.textContent = this.data.fieldValue;
  289. element.style.verticalAlign = "middle";
  290. element.style.display = "table-cell";
  291. let font = null;
  292. if (this.data.fontRefName && this.page.commonObjs.has(this.data.fontRefName)) {
  293. font = this.page.commonObjs.get(this.data.fontRefName);
  294. }
  295. this._setTextStyle(element, font);
  296. }
  297. if (this.data.textAlignment !== null) {
  298. element.style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
  299. }
  300. this.container.appendChild(element);
  301. return this.container;
  302. }
  303. _setTextStyle(element, font) {
  304. const style = element.style;
  305. style.fontSize = `${this.data.fontSize}px`;
  306. style.direction = this.data.fontDirection < 0 ? "rtl" : "ltr";
  307. if (!font) {
  308. return;
  309. }
  310. let bold = "normal";
  311. if (font.black) {
  312. bold = "900";
  313. } else if (font.bold) {
  314. bold = "bold";
  315. }
  316. style.fontWeight = bold;
  317. style.fontStyle = font.italic ? "italic" : "normal";
  318. const fontFamily = font.loadedName ? `"${font.loadedName}", ` : "";
  319. const fallbackName = font.fallbackName || "Helvetica, sans-serif";
  320. style.fontFamily = fontFamily + fallbackName;
  321. }
  322. }
  323. class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
  324. constructor(parameters) {
  325. super(parameters, parameters.renderInteractiveForms);
  326. }
  327. render() {
  328. this.container.className = "buttonWidgetAnnotation checkBox";
  329. const element = document.createElement("input");
  330. element.disabled = this.data.readOnly;
  331. element.type = "checkbox";
  332. if (this.data.fieldValue && this.data.fieldValue !== "Off") {
  333. element.setAttribute("checked", true);
  334. }
  335. this.container.appendChild(element);
  336. return this.container;
  337. }
  338. }
  339. class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
  340. constructor(parameters) {
  341. super(parameters, parameters.renderInteractiveForms);
  342. }
  343. render() {
  344. this.container.className = "buttonWidgetAnnotation radioButton";
  345. const element = document.createElement("input");
  346. element.disabled = this.data.readOnly;
  347. element.type = "radio";
  348. element.name = this.data.fieldName;
  349. if (this.data.fieldValue === this.data.buttonValue) {
  350. element.setAttribute("checked", true);
  351. }
  352. this.container.appendChild(element);
  353. return this.container;
  354. }
  355. }
  356. class PushButtonWidgetAnnotationElement extends LinkAnnotationElement {
  357. render() {
  358. const container = super.render();
  359. container.className = "buttonWidgetAnnotation pushButton";
  360. return container;
  361. }
  362. }
  363. class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
  364. constructor(parameters) {
  365. super(parameters, parameters.renderInteractiveForms);
  366. }
  367. render() {
  368. this.container.className = "choiceWidgetAnnotation";
  369. const selectElement = document.createElement("select");
  370. selectElement.disabled = this.data.readOnly;
  371. if (!this.data.combo) {
  372. selectElement.size = this.data.options.length;
  373. if (this.data.multiSelect) {
  374. selectElement.multiple = true;
  375. }
  376. }
  377. for (const option of this.data.options) {
  378. const optionElement = document.createElement("option");
  379. optionElement.textContent = option.displayValue;
  380. optionElement.value = option.exportValue;
  381. if (this.data.fieldValue.includes(option.displayValue)) {
  382. optionElement.setAttribute("selected", true);
  383. }
  384. selectElement.appendChild(optionElement);
  385. }
  386. this.container.appendChild(selectElement);
  387. return this.container;
  388. }
  389. }
  390. class PopupAnnotationElement extends AnnotationElement {
  391. constructor(parameters) {
  392. const isRenderable = !!(parameters.data.title || parameters.data.contents);
  393. super(parameters, isRenderable);
  394. }
  395. render() {
  396. const IGNORE_TYPES = ["Line", "Square", "Circle", "PolyLine", "Polygon", "Ink"];
  397. this.container.className = "popupAnnotation";
  398. if (IGNORE_TYPES.includes(this.data.parentType)) {
  399. return this.container;
  400. }
  401. const selector = `[data-annotation-id="${this.data.parentId}"]`;
  402. const parentElement = this.layer.querySelector(selector);
  403. if (!parentElement) {
  404. return this.container;
  405. }
  406. const popup = new PopupElement({
  407. container: this.container,
  408. trigger: parentElement,
  409. color: this.data.color,
  410. title: this.data.title,
  411. modificationDate: this.data.modificationDate,
  412. contents: this.data.contents
  413. });
  414. const parentLeft = parseFloat(parentElement.style.left);
  415. const parentWidth = parseFloat(parentElement.style.width);
  416. this.container.style.transformOrigin = `-${parentLeft + parentWidth}px -${parentElement.style.top}`;
  417. this.container.style.left = `${parentLeft + parentWidth}px`;
  418. this.container.appendChild(popup.render());
  419. return this.container;
  420. }
  421. }
  422. class PopupElement {
  423. constructor(parameters) {
  424. this.container = parameters.container;
  425. this.trigger = parameters.trigger;
  426. this.color = parameters.color;
  427. this.title = parameters.title;
  428. this.modificationDate = parameters.modificationDate;
  429. this.contents = parameters.contents;
  430. this.hideWrapper = parameters.hideWrapper || false;
  431. this.pinned = false;
  432. }
  433. render() {
  434. const BACKGROUND_ENLIGHT = 0.7;
  435. const wrapper = document.createElement("div");
  436. wrapper.className = "popupWrapper";
  437. this.hideElement = this.hideWrapper ? wrapper : this.container;
  438. this.hideElement.setAttribute("hidden", true);
  439. const popup = document.createElement("div");
  440. popup.className = "popup";
  441. const color = this.color;
  442. if (color) {
  443. const r = BACKGROUND_ENLIGHT * (255 - color[0]) + color[0];
  444. const g = BACKGROUND_ENLIGHT * (255 - color[1]) + color[1];
  445. const b = BACKGROUND_ENLIGHT * (255 - color[2]) + color[2];
  446. popup.style.backgroundColor = _util.Util.makeCssRgb(r | 0, g | 0, b | 0);
  447. }
  448. const title = document.createElement("h1");
  449. title.textContent = this.title;
  450. popup.appendChild(title);
  451. const dateObject = _display_utils.PDFDateString.toDateObject(this.modificationDate);
  452. if (dateObject) {
  453. const modificationDate = document.createElement("span");
  454. modificationDate.textContent = "{{date}}, {{time}}";
  455. modificationDate.dataset.l10nId = "annotation_date_string";
  456. modificationDate.dataset.l10nArgs = JSON.stringify({
  457. date: dateObject.toLocaleDateString(),
  458. time: dateObject.toLocaleTimeString()
  459. });
  460. popup.appendChild(modificationDate);
  461. }
  462. const contents = this._formatContents(this.contents);
  463. popup.appendChild(contents);
  464. this.trigger.addEventListener("click", this._toggle.bind(this));
  465. this.trigger.addEventListener("mouseover", this._show.bind(this, false));
  466. this.trigger.addEventListener("mouseout", this._hide.bind(this, false));
  467. popup.addEventListener("click", this._hide.bind(this, true));
  468. wrapper.appendChild(popup);
  469. return wrapper;
  470. }
  471. _formatContents(contents) {
  472. const p = document.createElement("p");
  473. const lines = contents.split(/(?:\r\n?|\n)/);
  474. for (let i = 0, ii = lines.length; i < ii; ++i) {
  475. const line = lines[i];
  476. p.appendChild(document.createTextNode(line));
  477. if (i < ii - 1) {
  478. p.appendChild(document.createElement("br"));
  479. }
  480. }
  481. return p;
  482. }
  483. _toggle() {
  484. if (this.pinned) {
  485. this._hide(true);
  486. } else {
  487. this._show(true);
  488. }
  489. }
  490. _show(pin = false) {
  491. if (pin) {
  492. this.pinned = true;
  493. }
  494. if (this.hideElement.hasAttribute("hidden")) {
  495. this.hideElement.removeAttribute("hidden");
  496. this.container.style.zIndex += 1;
  497. }
  498. }
  499. _hide(unpin = true) {
  500. if (unpin) {
  501. this.pinned = false;
  502. }
  503. if (!this.hideElement.hasAttribute("hidden") && !this.pinned) {
  504. this.hideElement.setAttribute("hidden", true);
  505. this.container.style.zIndex -= 1;
  506. }
  507. }
  508. }
  509. class FreeTextAnnotationElement extends AnnotationElement {
  510. constructor(parameters) {
  511. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  512. super(parameters, isRenderable, true);
  513. }
  514. render() {
  515. this.container.className = "freeTextAnnotation";
  516. if (!this.data.hasPopup) {
  517. this._createPopup(this.container, null, this.data);
  518. }
  519. return this.container;
  520. }
  521. }
  522. class LineAnnotationElement extends AnnotationElement {
  523. constructor(parameters) {
  524. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  525. super(parameters, isRenderable, true);
  526. }
  527. render() {
  528. this.container.className = "lineAnnotation";
  529. const data = this.data;
  530. const width = data.rect[2] - data.rect[0];
  531. const height = data.rect[3] - data.rect[1];
  532. const svg = this.svgFactory.create(width, height);
  533. const line = this.svgFactory.createElement("svg:line");
  534. line.setAttribute("x1", data.rect[2] - data.lineCoordinates[0]);
  535. line.setAttribute("y1", data.rect[3] - data.lineCoordinates[1]);
  536. line.setAttribute("x2", data.rect[2] - data.lineCoordinates[2]);
  537. line.setAttribute("y2", data.rect[3] - data.lineCoordinates[3]);
  538. line.setAttribute("stroke-width", data.borderStyle.width || 1);
  539. line.setAttribute("stroke", "transparent");
  540. svg.appendChild(line);
  541. this.container.append(svg);
  542. this._createPopup(this.container, line, data);
  543. return this.container;
  544. }
  545. }
  546. class SquareAnnotationElement extends AnnotationElement {
  547. constructor(parameters) {
  548. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  549. super(parameters, isRenderable, true);
  550. }
  551. render() {
  552. this.container.className = "squareAnnotation";
  553. const data = this.data;
  554. const width = data.rect[2] - data.rect[0];
  555. const height = data.rect[3] - data.rect[1];
  556. const svg = this.svgFactory.create(width, height);
  557. const borderWidth = data.borderStyle.width;
  558. const square = this.svgFactory.createElement("svg:rect");
  559. square.setAttribute("x", borderWidth / 2);
  560. square.setAttribute("y", borderWidth / 2);
  561. square.setAttribute("width", width - borderWidth);
  562. square.setAttribute("height", height - borderWidth);
  563. square.setAttribute("stroke-width", borderWidth || 1);
  564. square.setAttribute("stroke", "transparent");
  565. square.setAttribute("fill", "none");
  566. svg.appendChild(square);
  567. this.container.append(svg);
  568. this._createPopup(this.container, square, data);
  569. return this.container;
  570. }
  571. }
  572. class CircleAnnotationElement extends AnnotationElement {
  573. constructor(parameters) {
  574. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  575. super(parameters, isRenderable, true);
  576. }
  577. render() {
  578. this.container.className = "circleAnnotation";
  579. const data = this.data;
  580. const width = data.rect[2] - data.rect[0];
  581. const height = data.rect[3] - data.rect[1];
  582. const svg = this.svgFactory.create(width, height);
  583. const borderWidth = data.borderStyle.width;
  584. const circle = this.svgFactory.createElement("svg:ellipse");
  585. circle.setAttribute("cx", width / 2);
  586. circle.setAttribute("cy", height / 2);
  587. circle.setAttribute("rx", width / 2 - borderWidth / 2);
  588. circle.setAttribute("ry", height / 2 - borderWidth / 2);
  589. circle.setAttribute("stroke-width", borderWidth || 1);
  590. circle.setAttribute("stroke", "transparent");
  591. circle.setAttribute("fill", "none");
  592. svg.appendChild(circle);
  593. this.container.append(svg);
  594. this._createPopup(this.container, circle, data);
  595. return this.container;
  596. }
  597. }
  598. class PolylineAnnotationElement extends AnnotationElement {
  599. constructor(parameters) {
  600. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  601. super(parameters, isRenderable, true);
  602. this.containerClassName = "polylineAnnotation";
  603. this.svgElementName = "svg:polyline";
  604. }
  605. render() {
  606. this.container.className = this.containerClassName;
  607. const data = this.data;
  608. const width = data.rect[2] - data.rect[0];
  609. const height = data.rect[3] - data.rect[1];
  610. const svg = this.svgFactory.create(width, height);
  611. let points = [];
  612. for (const coordinate of data.vertices) {
  613. const x = coordinate.x - data.rect[0];
  614. const y = data.rect[3] - coordinate.y;
  615. points.push(x + "," + y);
  616. }
  617. points = points.join(" ");
  618. const polyline = this.svgFactory.createElement(this.svgElementName);
  619. polyline.setAttribute("points", points);
  620. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  621. polyline.setAttribute("stroke", "transparent");
  622. polyline.setAttribute("fill", "none");
  623. svg.appendChild(polyline);
  624. this.container.append(svg);
  625. this._createPopup(this.container, polyline, data);
  626. return this.container;
  627. }
  628. }
  629. class PolygonAnnotationElement extends PolylineAnnotationElement {
  630. constructor(parameters) {
  631. super(parameters);
  632. this.containerClassName = "polygonAnnotation";
  633. this.svgElementName = "svg:polygon";
  634. }
  635. }
  636. class CaretAnnotationElement extends AnnotationElement {
  637. constructor(parameters) {
  638. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  639. super(parameters, isRenderable, true);
  640. }
  641. render() {
  642. this.container.className = "caretAnnotation";
  643. if (!this.data.hasPopup) {
  644. this._createPopup(this.container, null, this.data);
  645. }
  646. return this.container;
  647. }
  648. }
  649. class InkAnnotationElement extends AnnotationElement {
  650. constructor(parameters) {
  651. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  652. super(parameters, isRenderable, true);
  653. this.containerClassName = "inkAnnotation";
  654. this.svgElementName = "svg:polyline";
  655. }
  656. render() {
  657. this.container.className = this.containerClassName;
  658. const data = this.data;
  659. const width = data.rect[2] - data.rect[0];
  660. const height = data.rect[3] - data.rect[1];
  661. const svg = this.svgFactory.create(width, height);
  662. for (const inkList of data.inkLists) {
  663. let points = [];
  664. for (const coordinate of inkList) {
  665. const x = coordinate.x - data.rect[0];
  666. const y = data.rect[3] - coordinate.y;
  667. points.push(`${x},${y}`);
  668. }
  669. points = points.join(" ");
  670. const polyline = this.svgFactory.createElement(this.svgElementName);
  671. polyline.setAttribute("points", points);
  672. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  673. polyline.setAttribute("stroke", "transparent");
  674. polyline.setAttribute("fill", "none");
  675. this._createPopup(this.container, polyline, data);
  676. svg.appendChild(polyline);
  677. }
  678. this.container.append(svg);
  679. return this.container;
  680. }
  681. }
  682. class HighlightAnnotationElement extends AnnotationElement {
  683. constructor(parameters) {
  684. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  685. super(parameters, isRenderable, true);
  686. }
  687. render() {
  688. this.container.className = "highlightAnnotation";
  689. if (!this.data.hasPopup) {
  690. this._createPopup(this.container, null, this.data);
  691. }
  692. return this.container;
  693. }
  694. }
  695. class UnderlineAnnotationElement extends AnnotationElement {
  696. constructor(parameters) {
  697. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  698. super(parameters, isRenderable, true);
  699. }
  700. render() {
  701. this.container.className = "underlineAnnotation";
  702. if (!this.data.hasPopup) {
  703. this._createPopup(this.container, null, this.data);
  704. }
  705. return this.container;
  706. }
  707. }
  708. class SquigglyAnnotationElement extends AnnotationElement {
  709. constructor(parameters) {
  710. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  711. super(parameters, isRenderable, true);
  712. }
  713. render() {
  714. this.container.className = "squigglyAnnotation";
  715. if (!this.data.hasPopup) {
  716. this._createPopup(this.container, null, this.data);
  717. }
  718. return this.container;
  719. }
  720. }
  721. class StrikeOutAnnotationElement extends AnnotationElement {
  722. constructor(parameters) {
  723. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  724. super(parameters, isRenderable, true);
  725. }
  726. render() {
  727. this.container.className = "strikeoutAnnotation";
  728. if (!this.data.hasPopup) {
  729. this._createPopup(this.container, null, this.data);
  730. }
  731. return this.container;
  732. }
  733. }
  734. class StampAnnotationElement extends AnnotationElement {
  735. constructor(parameters) {
  736. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  737. super(parameters, isRenderable, true);
  738. }
  739. render() {
  740. this.container.className = "stampAnnotation";
  741. if (!this.data.hasPopup) {
  742. this._createPopup(this.container, null, this.data);
  743. }
  744. return this.container;
  745. }
  746. }
  747. class FileAttachmentAnnotationElement extends AnnotationElement {
  748. constructor(parameters) {
  749. super(parameters, true);
  750. const {
  751. filename,
  752. content
  753. } = this.data.file;
  754. this.filename = (0, _display_utils.getFilenameFromUrl)(filename);
  755. this.content = content;
  756. if (this.linkService.eventBus) {
  757. this.linkService.eventBus.dispatch("fileattachmentannotation", {
  758. source: this,
  759. id: (0, _util.stringToPDFString)(filename),
  760. filename,
  761. content
  762. });
  763. }
  764. }
  765. render() {
  766. this.container.className = "fileAttachmentAnnotation";
  767. const trigger = document.createElement("div");
  768. trigger.style.height = this.container.style.height;
  769. trigger.style.width = this.container.style.width;
  770. trigger.addEventListener("dblclick", this._download.bind(this));
  771. if (!this.data.hasPopup && (this.data.title || this.data.contents)) {
  772. this._createPopup(this.container, trigger, this.data);
  773. }
  774. this.container.appendChild(trigger);
  775. return this.container;
  776. }
  777. _download() {
  778. if (!this.downloadManager) {
  779. (0, _util.warn)("Download cannot be started due to unavailable download manager");
  780. return;
  781. }
  782. this.downloadManager.downloadData(this.content, this.filename, "");
  783. }
  784. }
  785. class AnnotationLayer {
  786. static render(parameters) {
  787. const sortedAnnotations = [],
  788. popupAnnotations = [];
  789. for (const data of parameters.annotations) {
  790. if (!data) {
  791. continue;
  792. }
  793. if (data.annotationType === _util.AnnotationType.POPUP) {
  794. popupAnnotations.push(data);
  795. continue;
  796. }
  797. sortedAnnotations.push(data);
  798. }
  799. if (popupAnnotations.length) {
  800. sortedAnnotations.push(...popupAnnotations);
  801. }
  802. for (const data of sortedAnnotations) {
  803. const element = AnnotationElementFactory.create({
  804. data,
  805. layer: parameters.div,
  806. page: parameters.page,
  807. viewport: parameters.viewport,
  808. linkService: parameters.linkService,
  809. downloadManager: parameters.downloadManager,
  810. imageResourcesPath: parameters.imageResourcesPath || "",
  811. renderInteractiveForms: parameters.renderInteractiveForms || false,
  812. svgFactory: new _display_utils.DOMSVGFactory()
  813. });
  814. if (element.isRenderable) {
  815. parameters.div.appendChild(element.render());
  816. }
  817. }
  818. }
  819. static update(parameters) {
  820. for (const data of parameters.annotations) {
  821. const element = parameters.div.querySelector(`[data-annotation-id="${data.id}"]`);
  822. if (element) {
  823. element.style.transform = `matrix(${parameters.viewport.transform.join(",")})`;
  824. }
  825. }
  826. parameters.div.removeAttribute("hidden");
  827. }
  828. }
  829. exports.AnnotationLayer = AnnotationLayer;