annotation_layer.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. element.name = this.data.fieldName;
  278. if (this.data.maxLen !== null) {
  279. element.maxLength = this.data.maxLen;
  280. }
  281. if (this.data.comb) {
  282. const fieldWidth = this.data.rect[2] - this.data.rect[0];
  283. const combWidth = fieldWidth / this.data.maxLen;
  284. element.classList.add("comb");
  285. element.style.letterSpacing = `calc(${combWidth}px - 1ch)`;
  286. }
  287. } else {
  288. element = document.createElement("div");
  289. element.textContent = this.data.fieldValue;
  290. element.style.verticalAlign = "middle";
  291. element.style.display = "table-cell";
  292. let font = null;
  293. if (this.data.fontRefName && this.page.commonObjs.has(this.data.fontRefName)) {
  294. font = this.page.commonObjs.get(this.data.fontRefName);
  295. }
  296. this._setTextStyle(element, font);
  297. }
  298. if (this.data.textAlignment !== null) {
  299. element.style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
  300. }
  301. this.container.appendChild(element);
  302. return this.container;
  303. }
  304. _setTextStyle(element, font) {
  305. const style = element.style;
  306. style.fontSize = `${this.data.fontSize}px`;
  307. style.direction = this.data.fontDirection < 0 ? "rtl" : "ltr";
  308. if (!font) {
  309. return;
  310. }
  311. let bold = "normal";
  312. if (font.black) {
  313. bold = "900";
  314. } else if (font.bold) {
  315. bold = "bold";
  316. }
  317. style.fontWeight = bold;
  318. style.fontStyle = font.italic ? "italic" : "normal";
  319. const fontFamily = font.loadedName ? `"${font.loadedName}", ` : "";
  320. const fallbackName = font.fallbackName || "Helvetica, sans-serif";
  321. style.fontFamily = fontFamily + fallbackName;
  322. }
  323. }
  324. class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
  325. constructor(parameters) {
  326. super(parameters, parameters.renderInteractiveForms);
  327. }
  328. render() {
  329. this.container.className = "buttonWidgetAnnotation checkBox";
  330. const element = document.createElement("input");
  331. element.disabled = this.data.readOnly;
  332. element.type = "checkbox";
  333. element.name = this.data.fieldName;
  334. if (this.data.fieldValue && this.data.fieldValue !== "Off") {
  335. element.setAttribute("checked", true);
  336. }
  337. this.container.appendChild(element);
  338. return this.container;
  339. }
  340. }
  341. class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
  342. constructor(parameters) {
  343. super(parameters, parameters.renderInteractiveForms);
  344. }
  345. render() {
  346. this.container.className = "buttonWidgetAnnotation radioButton";
  347. const element = document.createElement("input");
  348. element.disabled = this.data.readOnly;
  349. element.type = "radio";
  350. element.name = this.data.fieldName;
  351. if (this.data.fieldValue === this.data.buttonValue) {
  352. element.setAttribute("checked", true);
  353. }
  354. this.container.appendChild(element);
  355. return this.container;
  356. }
  357. }
  358. class PushButtonWidgetAnnotationElement extends LinkAnnotationElement {
  359. render() {
  360. const container = super.render();
  361. container.className = "buttonWidgetAnnotation pushButton";
  362. return container;
  363. }
  364. }
  365. class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
  366. constructor(parameters) {
  367. super(parameters, parameters.renderInteractiveForms);
  368. }
  369. render() {
  370. this.container.className = "choiceWidgetAnnotation";
  371. const selectElement = document.createElement("select");
  372. selectElement.disabled = this.data.readOnly;
  373. selectElement.name = this.data.fieldName;
  374. if (!this.data.combo) {
  375. selectElement.size = this.data.options.length;
  376. if (this.data.multiSelect) {
  377. selectElement.multiple = true;
  378. }
  379. }
  380. for (const option of this.data.options) {
  381. const optionElement = document.createElement("option");
  382. optionElement.textContent = option.displayValue;
  383. optionElement.value = option.exportValue;
  384. if (this.data.fieldValue.includes(option.displayValue)) {
  385. optionElement.setAttribute("selected", true);
  386. }
  387. selectElement.appendChild(optionElement);
  388. }
  389. this.container.appendChild(selectElement);
  390. return this.container;
  391. }
  392. }
  393. class PopupAnnotationElement extends AnnotationElement {
  394. constructor(parameters) {
  395. const isRenderable = !!(parameters.data.title || parameters.data.contents);
  396. super(parameters, isRenderable);
  397. }
  398. render() {
  399. const IGNORE_TYPES = ["Line", "Square", "Circle", "PolyLine", "Polygon", "Ink"];
  400. this.container.className = "popupAnnotation";
  401. if (IGNORE_TYPES.includes(this.data.parentType)) {
  402. return this.container;
  403. }
  404. const selector = `[data-annotation-id="${this.data.parentId}"]`;
  405. const parentElement = this.layer.querySelector(selector);
  406. if (!parentElement) {
  407. return this.container;
  408. }
  409. const popup = new PopupElement({
  410. container: this.container,
  411. trigger: parentElement,
  412. color: this.data.color,
  413. title: this.data.title,
  414. modificationDate: this.data.modificationDate,
  415. contents: this.data.contents
  416. });
  417. const parentLeft = parseFloat(parentElement.style.left);
  418. const parentWidth = parseFloat(parentElement.style.width);
  419. this.container.style.transformOrigin = `-${parentLeft + parentWidth}px -${parentElement.style.top}`;
  420. this.container.style.left = `${parentLeft + parentWidth}px`;
  421. this.container.appendChild(popup.render());
  422. return this.container;
  423. }
  424. }
  425. class PopupElement {
  426. constructor(parameters) {
  427. this.container = parameters.container;
  428. this.trigger = parameters.trigger;
  429. this.color = parameters.color;
  430. this.title = parameters.title;
  431. this.modificationDate = parameters.modificationDate;
  432. this.contents = parameters.contents;
  433. this.hideWrapper = parameters.hideWrapper || false;
  434. this.pinned = false;
  435. }
  436. render() {
  437. const BACKGROUND_ENLIGHT = 0.7;
  438. const wrapper = document.createElement("div");
  439. wrapper.className = "popupWrapper";
  440. this.hideElement = this.hideWrapper ? wrapper : this.container;
  441. this.hideElement.setAttribute("hidden", true);
  442. const popup = document.createElement("div");
  443. popup.className = "popup";
  444. const color = this.color;
  445. if (color) {
  446. const r = BACKGROUND_ENLIGHT * (255 - color[0]) + color[0];
  447. const g = BACKGROUND_ENLIGHT * (255 - color[1]) + color[1];
  448. const b = BACKGROUND_ENLIGHT * (255 - color[2]) + color[2];
  449. popup.style.backgroundColor = _util.Util.makeCssRgb(r | 0, g | 0, b | 0);
  450. }
  451. const title = document.createElement("h1");
  452. title.textContent = this.title;
  453. popup.appendChild(title);
  454. const dateObject = _display_utils.PDFDateString.toDateObject(this.modificationDate);
  455. if (dateObject) {
  456. const modificationDate = document.createElement("span");
  457. modificationDate.textContent = "{{date}}, {{time}}";
  458. modificationDate.dataset.l10nId = "annotation_date_string";
  459. modificationDate.dataset.l10nArgs = JSON.stringify({
  460. date: dateObject.toLocaleDateString(),
  461. time: dateObject.toLocaleTimeString()
  462. });
  463. popup.appendChild(modificationDate);
  464. }
  465. const contents = this._formatContents(this.contents);
  466. popup.appendChild(contents);
  467. this.trigger.addEventListener("click", this._toggle.bind(this));
  468. this.trigger.addEventListener("mouseover", this._show.bind(this, false));
  469. this.trigger.addEventListener("mouseout", this._hide.bind(this, false));
  470. popup.addEventListener("click", this._hide.bind(this, true));
  471. wrapper.appendChild(popup);
  472. return wrapper;
  473. }
  474. _formatContents(contents) {
  475. const p = document.createElement("p");
  476. const lines = contents.split(/(?:\r\n?|\n)/);
  477. for (let i = 0, ii = lines.length; i < ii; ++i) {
  478. const line = lines[i];
  479. p.appendChild(document.createTextNode(line));
  480. if (i < ii - 1) {
  481. p.appendChild(document.createElement("br"));
  482. }
  483. }
  484. return p;
  485. }
  486. _toggle() {
  487. if (this.pinned) {
  488. this._hide(true);
  489. } else {
  490. this._show(true);
  491. }
  492. }
  493. _show(pin = false) {
  494. if (pin) {
  495. this.pinned = true;
  496. }
  497. if (this.hideElement.hasAttribute("hidden")) {
  498. this.hideElement.removeAttribute("hidden");
  499. this.container.style.zIndex += 1;
  500. }
  501. }
  502. _hide(unpin = true) {
  503. if (unpin) {
  504. this.pinned = false;
  505. }
  506. if (!this.hideElement.hasAttribute("hidden") && !this.pinned) {
  507. this.hideElement.setAttribute("hidden", true);
  508. this.container.style.zIndex -= 1;
  509. }
  510. }
  511. }
  512. class FreeTextAnnotationElement extends AnnotationElement {
  513. constructor(parameters) {
  514. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  515. super(parameters, isRenderable, true);
  516. }
  517. render() {
  518. this.container.className = "freeTextAnnotation";
  519. if (!this.data.hasPopup) {
  520. this._createPopup(this.container, null, this.data);
  521. }
  522. return this.container;
  523. }
  524. }
  525. class LineAnnotationElement extends AnnotationElement {
  526. constructor(parameters) {
  527. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  528. super(parameters, isRenderable, true);
  529. }
  530. render() {
  531. this.container.className = "lineAnnotation";
  532. const data = this.data;
  533. const width = data.rect[2] - data.rect[0];
  534. const height = data.rect[3] - data.rect[1];
  535. const svg = this.svgFactory.create(width, height);
  536. const line = this.svgFactory.createElement("svg:line");
  537. line.setAttribute("x1", data.rect[2] - data.lineCoordinates[0]);
  538. line.setAttribute("y1", data.rect[3] - data.lineCoordinates[1]);
  539. line.setAttribute("x2", data.rect[2] - data.lineCoordinates[2]);
  540. line.setAttribute("y2", data.rect[3] - data.lineCoordinates[3]);
  541. line.setAttribute("stroke-width", data.borderStyle.width || 1);
  542. line.setAttribute("stroke", "transparent");
  543. svg.appendChild(line);
  544. this.container.append(svg);
  545. this._createPopup(this.container, line, data);
  546. return this.container;
  547. }
  548. }
  549. class SquareAnnotationElement extends AnnotationElement {
  550. constructor(parameters) {
  551. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  552. super(parameters, isRenderable, true);
  553. }
  554. render() {
  555. this.container.className = "squareAnnotation";
  556. const data = this.data;
  557. const width = data.rect[2] - data.rect[0];
  558. const height = data.rect[3] - data.rect[1];
  559. const svg = this.svgFactory.create(width, height);
  560. const borderWidth = data.borderStyle.width;
  561. const square = this.svgFactory.createElement("svg:rect");
  562. square.setAttribute("x", borderWidth / 2);
  563. square.setAttribute("y", borderWidth / 2);
  564. square.setAttribute("width", width - borderWidth);
  565. square.setAttribute("height", height - borderWidth);
  566. square.setAttribute("stroke-width", borderWidth || 1);
  567. square.setAttribute("stroke", "transparent");
  568. square.setAttribute("fill", "none");
  569. svg.appendChild(square);
  570. this.container.append(svg);
  571. this._createPopup(this.container, square, data);
  572. return this.container;
  573. }
  574. }
  575. class CircleAnnotationElement extends AnnotationElement {
  576. constructor(parameters) {
  577. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  578. super(parameters, isRenderable, true);
  579. }
  580. render() {
  581. this.container.className = "circleAnnotation";
  582. const data = this.data;
  583. const width = data.rect[2] - data.rect[0];
  584. const height = data.rect[3] - data.rect[1];
  585. const svg = this.svgFactory.create(width, height);
  586. const borderWidth = data.borderStyle.width;
  587. const circle = this.svgFactory.createElement("svg:ellipse");
  588. circle.setAttribute("cx", width / 2);
  589. circle.setAttribute("cy", height / 2);
  590. circle.setAttribute("rx", width / 2 - borderWidth / 2);
  591. circle.setAttribute("ry", height / 2 - borderWidth / 2);
  592. circle.setAttribute("stroke-width", borderWidth || 1);
  593. circle.setAttribute("stroke", "transparent");
  594. circle.setAttribute("fill", "none");
  595. svg.appendChild(circle);
  596. this.container.append(svg);
  597. this._createPopup(this.container, circle, data);
  598. return this.container;
  599. }
  600. }
  601. class PolylineAnnotationElement extends AnnotationElement {
  602. constructor(parameters) {
  603. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  604. super(parameters, isRenderable, true);
  605. this.containerClassName = "polylineAnnotation";
  606. this.svgElementName = "svg:polyline";
  607. }
  608. render() {
  609. this.container.className = this.containerClassName;
  610. const data = this.data;
  611. const width = data.rect[2] - data.rect[0];
  612. const height = data.rect[3] - data.rect[1];
  613. const svg = this.svgFactory.create(width, height);
  614. let points = [];
  615. for (const coordinate of data.vertices) {
  616. const x = coordinate.x - data.rect[0];
  617. const y = data.rect[3] - coordinate.y;
  618. points.push(x + "," + y);
  619. }
  620. points = points.join(" ");
  621. const polyline = this.svgFactory.createElement(this.svgElementName);
  622. polyline.setAttribute("points", points);
  623. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  624. polyline.setAttribute("stroke", "transparent");
  625. polyline.setAttribute("fill", "none");
  626. svg.appendChild(polyline);
  627. this.container.append(svg);
  628. this._createPopup(this.container, polyline, data);
  629. return this.container;
  630. }
  631. }
  632. class PolygonAnnotationElement extends PolylineAnnotationElement {
  633. constructor(parameters) {
  634. super(parameters);
  635. this.containerClassName = "polygonAnnotation";
  636. this.svgElementName = "svg:polygon";
  637. }
  638. }
  639. class CaretAnnotationElement extends AnnotationElement {
  640. constructor(parameters) {
  641. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  642. super(parameters, isRenderable, true);
  643. }
  644. render() {
  645. this.container.className = "caretAnnotation";
  646. if (!this.data.hasPopup) {
  647. this._createPopup(this.container, null, this.data);
  648. }
  649. return this.container;
  650. }
  651. }
  652. class InkAnnotationElement extends AnnotationElement {
  653. constructor(parameters) {
  654. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  655. super(parameters, isRenderable, true);
  656. this.containerClassName = "inkAnnotation";
  657. this.svgElementName = "svg:polyline";
  658. }
  659. render() {
  660. this.container.className = this.containerClassName;
  661. const data = this.data;
  662. const width = data.rect[2] - data.rect[0];
  663. const height = data.rect[3] - data.rect[1];
  664. const svg = this.svgFactory.create(width, height);
  665. for (const inkList of data.inkLists) {
  666. let points = [];
  667. for (const coordinate of inkList) {
  668. const x = coordinate.x - data.rect[0];
  669. const y = data.rect[3] - coordinate.y;
  670. points.push(`${x},${y}`);
  671. }
  672. points = points.join(" ");
  673. const polyline = this.svgFactory.createElement(this.svgElementName);
  674. polyline.setAttribute("points", points);
  675. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  676. polyline.setAttribute("stroke", "transparent");
  677. polyline.setAttribute("fill", "none");
  678. this._createPopup(this.container, polyline, data);
  679. svg.appendChild(polyline);
  680. }
  681. this.container.append(svg);
  682. return this.container;
  683. }
  684. }
  685. class HighlightAnnotationElement extends AnnotationElement {
  686. constructor(parameters) {
  687. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  688. super(parameters, isRenderable, true);
  689. }
  690. render() {
  691. this.container.className = "highlightAnnotation";
  692. if (!this.data.hasPopup) {
  693. this._createPopup(this.container, null, this.data);
  694. }
  695. return this.container;
  696. }
  697. }
  698. class UnderlineAnnotationElement extends AnnotationElement {
  699. constructor(parameters) {
  700. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  701. super(parameters, isRenderable, true);
  702. }
  703. render() {
  704. this.container.className = "underlineAnnotation";
  705. if (!this.data.hasPopup) {
  706. this._createPopup(this.container, null, this.data);
  707. }
  708. return this.container;
  709. }
  710. }
  711. class SquigglyAnnotationElement extends AnnotationElement {
  712. constructor(parameters) {
  713. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  714. super(parameters, isRenderable, true);
  715. }
  716. render() {
  717. this.container.className = "squigglyAnnotation";
  718. if (!this.data.hasPopup) {
  719. this._createPopup(this.container, null, this.data);
  720. }
  721. return this.container;
  722. }
  723. }
  724. class StrikeOutAnnotationElement extends AnnotationElement {
  725. constructor(parameters) {
  726. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  727. super(parameters, isRenderable, true);
  728. }
  729. render() {
  730. this.container.className = "strikeoutAnnotation";
  731. if (!this.data.hasPopup) {
  732. this._createPopup(this.container, null, this.data);
  733. }
  734. return this.container;
  735. }
  736. }
  737. class StampAnnotationElement extends AnnotationElement {
  738. constructor(parameters) {
  739. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  740. super(parameters, isRenderable, true);
  741. }
  742. render() {
  743. this.container.className = "stampAnnotation";
  744. if (!this.data.hasPopup) {
  745. this._createPopup(this.container, null, this.data);
  746. }
  747. return this.container;
  748. }
  749. }
  750. class FileAttachmentAnnotationElement extends AnnotationElement {
  751. constructor(parameters) {
  752. super(parameters, true);
  753. const {
  754. filename,
  755. content
  756. } = this.data.file;
  757. this.filename = (0, _display_utils.getFilenameFromUrl)(filename);
  758. this.content = content;
  759. if (this.linkService.eventBus) {
  760. this.linkService.eventBus.dispatch("fileattachmentannotation", {
  761. source: this,
  762. id: (0, _util.stringToPDFString)(filename),
  763. filename,
  764. content
  765. });
  766. }
  767. }
  768. render() {
  769. this.container.className = "fileAttachmentAnnotation";
  770. const trigger = document.createElement("div");
  771. trigger.style.height = this.container.style.height;
  772. trigger.style.width = this.container.style.width;
  773. trigger.addEventListener("dblclick", this._download.bind(this));
  774. if (!this.data.hasPopup && (this.data.title || this.data.contents)) {
  775. this._createPopup(this.container, trigger, this.data);
  776. }
  777. this.container.appendChild(trigger);
  778. return this.container;
  779. }
  780. _download() {
  781. if (!this.downloadManager) {
  782. (0, _util.warn)("Download cannot be started due to unavailable download manager");
  783. return;
  784. }
  785. this.downloadManager.downloadData(this.content, this.filename, "");
  786. }
  787. }
  788. class AnnotationLayer {
  789. static render(parameters) {
  790. const sortedAnnotations = [],
  791. popupAnnotations = [];
  792. for (const data of parameters.annotations) {
  793. if (!data) {
  794. continue;
  795. }
  796. if (data.annotationType === _util.AnnotationType.POPUP) {
  797. popupAnnotations.push(data);
  798. continue;
  799. }
  800. sortedAnnotations.push(data);
  801. }
  802. if (popupAnnotations.length) {
  803. sortedAnnotations.push(...popupAnnotations);
  804. }
  805. for (const data of sortedAnnotations) {
  806. const element = AnnotationElementFactory.create({
  807. data,
  808. layer: parameters.div,
  809. page: parameters.page,
  810. viewport: parameters.viewport,
  811. linkService: parameters.linkService,
  812. downloadManager: parameters.downloadManager,
  813. imageResourcesPath: parameters.imageResourcesPath || "",
  814. renderInteractiveForms: parameters.renderInteractiveForms || false,
  815. svgFactory: new _display_utils.DOMSVGFactory()
  816. });
  817. if (element.isRenderable) {
  818. parameters.div.appendChild(element.render());
  819. }
  820. }
  821. }
  822. static update(parameters) {
  823. for (const data of parameters.annotations) {
  824. const element = parameters.div.querySelector(`[data-annotation-id="${data.id}"]`);
  825. if (element) {
  826. element.style.transform = `matrix(${parameters.viewport.transform.join(",")})`;
  827. }
  828. }
  829. parameters.div.removeAttribute("hidden");
  830. }
  831. }
  832. exports.AnnotationLayer = AnnotationLayer;