factory.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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. var _xhtml = require("./xhtml.js");
  35. class XFAFactory {
  36. constructor(data) {
  37. try {
  38. this.root = new _parser.XFAParser().parse(XFAFactory._createDocument(data));
  39. const binder = new _bind.Binder(this.root);
  40. this.form = binder.bind();
  41. this.dataHandler = new _data.DataHandler(this.root, binder.getData());
  42. this.form[_xfa_object.$globalData].template = this.form;
  43. } catch (e) {
  44. (0, _util.warn)(`XFA - an error occurred during parsing and binding: ${e}`);
  45. }
  46. }
  47. isValid() {
  48. return this.root && this.form;
  49. }
  50. _createPagesHelper() {
  51. const iterator = this.form[_xfa_object.$toPages]();
  52. return new Promise((resolve, reject) => {
  53. const nextIteration = () => {
  54. try {
  55. const value = iterator.next();
  56. if (value.done) {
  57. resolve(value.value);
  58. } else {
  59. setTimeout(nextIteration, 0);
  60. }
  61. } catch (e) {
  62. reject(e);
  63. }
  64. };
  65. setTimeout(nextIteration, 0);
  66. });
  67. }
  68. async _createPages() {
  69. try {
  70. this.pages = await this._createPagesHelper();
  71. this.dims = this.pages.children.map(c => {
  72. const {
  73. width,
  74. height
  75. } = c.attributes.style;
  76. return [0, 0, parseInt(width), parseInt(height)];
  77. });
  78. } catch (e) {
  79. (0, _util.warn)(`XFA - an error occurred during layout: ${e}`);
  80. }
  81. }
  82. getBoundingBox(pageIndex) {
  83. return this.dims[pageIndex];
  84. }
  85. async getNumPages() {
  86. if (!this.pages) {
  87. await this._createPages();
  88. }
  89. return this.dims.length;
  90. }
  91. setImages(images) {
  92. this.form[_xfa_object.$globalData].images = images;
  93. }
  94. setFonts(fonts) {
  95. this.form[_xfa_object.$globalData].fontFinder = new _fonts.FontFinder(fonts);
  96. const missingFonts = [];
  97. for (let typeface of this.form[_xfa_object.$globalData].usedTypefaces) {
  98. typeface = (0, _utils.stripQuotes)(typeface);
  99. const font = this.form[_xfa_object.$globalData].fontFinder.find(typeface);
  100. if (!font) {
  101. missingFonts.push(typeface);
  102. }
  103. }
  104. if (missingFonts.length > 0) {
  105. return missingFonts;
  106. }
  107. return null;
  108. }
  109. appendFonts(fonts, reallyMissingFonts) {
  110. this.form[_xfa_object.$globalData].fontFinder.add(fonts, reallyMissingFonts);
  111. }
  112. async getPages() {
  113. if (!this.pages) {
  114. await this._createPages();
  115. }
  116. const pages = this.pages;
  117. this.pages = null;
  118. return pages;
  119. }
  120. serializeData(storage) {
  121. return this.dataHandler.serialize(storage);
  122. }
  123. static _createDocument(data) {
  124. if (!data["/xdp:xdp"]) {
  125. return data["xdp:xdp"];
  126. }
  127. return Object.values(data).join("");
  128. }
  129. static getRichTextAsHtml(rc) {
  130. if (!rc || typeof rc !== "string") {
  131. return null;
  132. }
  133. try {
  134. let root = new _parser.XFAParser(_xhtml.XhtmlNamespace, true).parse(rc);
  135. if (!["body", "xhtml"].includes(root[_xfa_object.$nodeName])) {
  136. const newRoot = _xhtml.XhtmlNamespace.body({});
  137. newRoot[_xfa_object.$appendChild](root);
  138. root = newRoot;
  139. }
  140. const result = root[_xfa_object.$toHTML]();
  141. if (!result.success) {
  142. return null;
  143. }
  144. const {
  145. html
  146. } = result;
  147. const {
  148. attributes
  149. } = html;
  150. if (attributes) {
  151. if (attributes.class) {
  152. attributes.class = attributes.class.filter(attr => !attr.startsWith("xfa"));
  153. }
  154. attributes.dir = "auto";
  155. }
  156. return {
  157. html,
  158. str: root[_xfa_object.$text]()
  159. };
  160. } catch (e) {
  161. (0, _util.warn)(`XFA - an error occurred during parsing of rich text: ${e}`);
  162. }
  163. return null;
  164. }
  165. }
  166. exports.XFAFactory = XFAFactory;