xfa_layer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.XfaLayer = void 0;
  27. class XfaLayer {
  28. static setAttributes(html, attrs) {
  29. for (const [key, value] of Object.entries(attrs)) {
  30. if (value === null || value === undefined) {
  31. continue;
  32. }
  33. if (key !== "style") {
  34. html.setAttribute(key, value);
  35. } else {
  36. Object.assign(html.style, value);
  37. }
  38. }
  39. }
  40. static render(parameters) {
  41. const root = parameters.xfa;
  42. const rootHtml = document.createElement(root.name);
  43. if (root.attributes) {
  44. XfaLayer.setAttributes(rootHtml, root.attributes);
  45. }
  46. const stack = [[root, -1, rootHtml]];
  47. const rootDiv = parameters.div;
  48. rootDiv.appendChild(rootHtml);
  49. const coeffs = parameters.viewport.transform.join(",");
  50. rootDiv.style.transform = `matrix(${coeffs})`;
  51. rootDiv.setAttribute("class", "xfaLayer xfaFont");
  52. while (stack.length > 0) {
  53. const [parent, i, html] = stack[stack.length - 1];
  54. if (i + 1 === parent.children.length) {
  55. stack.pop();
  56. continue;
  57. }
  58. const child = parent.children[++stack[stack.length - 1][1]];
  59. if (child === null) {
  60. continue;
  61. }
  62. const {
  63. name
  64. } = child;
  65. if (name === "#text") {
  66. html.appendChild(document.createTextNode(child.value));
  67. continue;
  68. }
  69. const childHtml = document.createElement(name);
  70. html.appendChild(childHtml);
  71. if (child.attributes) {
  72. XfaLayer.setAttributes(childHtml, child.attributes);
  73. }
  74. if (child.children && child.children.length > 0) {
  75. stack.push([child, -1, childHtml]);
  76. } else if (child.value) {
  77. childHtml.appendChild(document.createTextNode(child.value));
  78. }
  79. }
  80. }
  81. static update(parameters) {
  82. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  83. parameters.div.style.transform = transform;
  84. parameters.div.hidden = false;
  85. }
  86. }
  87. exports.XfaLayer = XfaLayer;