factory.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.XFAFactory = void 0;
  27. var _xfa_object = require("./xfa_object.js");
  28. var _bind = require("./bind.js");
  29. var _data = require("./data.js");
  30. var _fonts = require("./fonts.js");
  31. var _utils = require("./utils.js");
  32. var _util = require("../../shared/util.js");
  33. var _parser = require("./parser.js");
  34. class XFAFactory {
  35. constructor(data) {
  36. try {
  37. this.root = new _parser.XFAParser().parse(XFAFactory._createDocument(data));
  38. const binder = new _bind.Binder(this.root);
  39. this.form = binder.bind();
  40. this.dataHandler = new _data.DataHandler(this.root, binder.getData());
  41. this.form[_xfa_object.$globalData].template = this.form;
  42. } catch (e) {
  43. (0, _util.warn)(`XFA - an error occurred during parsing and binding: ${e}`);
  44. }
  45. }
  46. isValid() {
  47. return this.root && this.form;
  48. }
  49. _createPages() {
  50. try {
  51. this.pages = this.form[_xfa_object.$toHTML]();
  52. this.dims = this.pages.children.map(c => {
  53. const {
  54. width,
  55. height
  56. } = c.attributes.style;
  57. return [0, 0, parseInt(width), parseInt(height)];
  58. });
  59. } catch (e) {
  60. (0, _util.warn)(`XFA - an error occurred during layout: ${e}`);
  61. }
  62. }
  63. getBoundingBox(pageIndex) {
  64. return this.dims[pageIndex];
  65. }
  66. get numberPages() {
  67. if (!this.pages) {
  68. this._createPages();
  69. }
  70. return this.dims.length;
  71. }
  72. setImages(images) {
  73. this.form[_xfa_object.$globalData].images = images;
  74. }
  75. setFonts(fonts) {
  76. this.form[_xfa_object.$globalData].fontFinder = new _fonts.FontFinder(fonts);
  77. const missingFonts = [];
  78. for (let typeface of this.form[_xfa_object.$globalData].usedTypefaces) {
  79. typeface = (0, _utils.stripQuotes)(typeface);
  80. const font = this.form[_xfa_object.$globalData].fontFinder.find(typeface);
  81. if (!font) {
  82. missingFonts.push(typeface);
  83. }
  84. }
  85. if (missingFonts.length > 0) {
  86. return missingFonts;
  87. }
  88. return null;
  89. }
  90. appendFonts(fonts, reallyMissingFonts) {
  91. this.form[_xfa_object.$globalData].fontFinder.add(fonts, reallyMissingFonts);
  92. }
  93. getPages() {
  94. if (!this.pages) {
  95. this._createPages();
  96. }
  97. const pages = this.pages;
  98. this.pages = null;
  99. return pages;
  100. }
  101. serializeData(storage) {
  102. return this.dataHandler.serialize(storage);
  103. }
  104. static _createDocument(data) {
  105. if (!data["/xdp:xdp"]) {
  106. return data["xdp:xdp"];
  107. }
  108. return Object.values(data).join("");
  109. }
  110. }
  111. exports.XFAFactory = XFAFactory;