layout.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.addHTML = addHTML;
  27. exports.flushHTML = flushHTML;
  28. exports.getAvailableSpace = getAvailableSpace;
  29. var _xfa_object = require("./xfa_object.js");
  30. var _html_utils = require("./html_utils.js");
  31. function flushHTML(node) {
  32. const attributes = node[_xfa_object.$extra].attributes;
  33. const html = {
  34. name: "div",
  35. attributes,
  36. children: node[_xfa_object.$extra].children
  37. };
  38. if (node[_xfa_object.$extra].failingNode) {
  39. const htmlFromFailing = node[_xfa_object.$extra].failingNode[_xfa_object.$flushHTML]();
  40. if (htmlFromFailing) {
  41. html.children.push(htmlFromFailing);
  42. }
  43. }
  44. if (html.children.length === 0) {
  45. return null;
  46. }
  47. node[_xfa_object.$extra].children = [];
  48. delete node[_xfa_object.$extra].line;
  49. return html;
  50. }
  51. function addHTML(node, html, bbox) {
  52. const extra = node[_xfa_object.$extra];
  53. const availableSpace = extra.availableSpace;
  54. switch (node.layout) {
  55. case "position":
  56. {
  57. const [x, y, w, h] = bbox;
  58. extra.width = Math.max(extra.width, x + w);
  59. extra.height = Math.max(extra.height, y + h);
  60. extra.children.push(html);
  61. break;
  62. }
  63. case "lr-tb":
  64. case "rl-tb":
  65. if (!extra.line || extra.attempt === 1) {
  66. extra.line = {
  67. name: "div",
  68. attributes: {
  69. class: node.layout === "lr-tb" ? "xfaLr" : "xfaRl"
  70. },
  71. children: []
  72. };
  73. extra.children.push(extra.line);
  74. }
  75. extra.line.children.push(html);
  76. if (extra.attempt === 0) {
  77. const [,, w, h] = bbox;
  78. extra.currentWidth += w;
  79. extra.height = Math.max(extra.height, extra.prevHeight + h);
  80. } else {
  81. const [,, w, h] = bbox;
  82. extra.width = Math.max(extra.width, extra.currentWidth);
  83. extra.currentWidth = w;
  84. extra.prevHeight = extra.height;
  85. extra.height += h;
  86. extra.attempt = 0;
  87. }
  88. break;
  89. case "rl-row":
  90. case "row":
  91. {
  92. extra.children.push(html);
  93. const [,, w, h] = bbox;
  94. extra.width += w;
  95. extra.height = Math.max(extra.height, h);
  96. const height = (0, _html_utils.measureToString)(extra.height);
  97. for (const child of extra.children) {
  98. if (child.attributes.class === "xfaWrapper") {
  99. child.children[child.children.length - 1].attributes.style.height = height;
  100. } else {
  101. child.attributes.style.height = height;
  102. }
  103. }
  104. break;
  105. }
  106. case "table":
  107. {
  108. const [,, w, h] = bbox;
  109. extra.width = Math.min(availableSpace.width, Math.max(extra.width, w));
  110. extra.height += h;
  111. extra.children.push(html);
  112. break;
  113. }
  114. case "tb":
  115. {
  116. const [,,, h] = bbox;
  117. extra.width = availableSpace.width;
  118. extra.height += h;
  119. extra.children.push(html);
  120. break;
  121. }
  122. }
  123. }
  124. function getAvailableSpace(node) {
  125. const availableSpace = node[_xfa_object.$extra].availableSpace;
  126. switch (node.layout) {
  127. case "lr-tb":
  128. case "rl-tb":
  129. switch (node[_xfa_object.$extra].attempt) {
  130. case 0:
  131. return {
  132. width: availableSpace.width - node[_xfa_object.$extra].currentWidth,
  133. height: availableSpace.height - node[_xfa_object.$extra].prevHeight
  134. };
  135. case 1:
  136. return {
  137. width: availableSpace.width,
  138. height: availableSpace.height - node[_xfa_object.$extra].height
  139. };
  140. default:
  141. return {
  142. width: Infinity,
  143. height: availableSpace.height - node[_xfa_object.$extra].prevHeight
  144. };
  145. }
  146. case "rl-row":
  147. case "row":
  148. const width = node[_xfa_object.$extra].columnWidths.slice(node[_xfa_object.$extra].currentColumn).reduce((a, x) => a + x);
  149. return {
  150. width,
  151. height: availableSpace.height
  152. };
  153. case "table":
  154. case "tb":
  155. return {
  156. width: availableSpace.width,
  157. height: availableSpace.height - node[_xfa_object.$extra].height
  158. };
  159. case "position":
  160. default:
  161. return availableSpace;
  162. }
  163. }