2
0

html_utils.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.layoutClass = layoutClass;
  27. exports.measureToString = measureToString;
  28. exports.toStyle = toStyle;
  29. var _xfa_object = require("./xfa_object.js");
  30. var _util = require("../../shared/util.js");
  31. function measureToString(m) {
  32. if (typeof m === "string") {
  33. return "0px";
  34. }
  35. return Number.isInteger(m) ? `${m}px` : `${m.toFixed(2)}px`;
  36. }
  37. const converters = {
  38. anchorType(node, style) {
  39. if (!("transform" in style)) {
  40. style.transform = "";
  41. }
  42. switch (node.anchorType) {
  43. case "bottomCenter":
  44. style.transform += "translate(-50%, -100%)";
  45. break;
  46. case "bottomLeft":
  47. style.transform += "translate(0,-100%)";
  48. break;
  49. case "bottomRight":
  50. style.transform += "translate(-100%,-100%)";
  51. break;
  52. case "middleCenter":
  53. style.transform += "translate(-50%,-50%)";
  54. break;
  55. case "middleLeft":
  56. style.transform += "translate(0,-50%)";
  57. break;
  58. case "middleRight":
  59. style.transform += "translate(-100%,-50%)";
  60. break;
  61. case "topCenter":
  62. style.transform += "translate(-50%,0)";
  63. break;
  64. case "topRight":
  65. style.transform += "translate(-100%,0)";
  66. break;
  67. }
  68. },
  69. dimensions(node, style) {
  70. if (node.w) {
  71. style.width = measureToString(node.w);
  72. } else {
  73. style.width = "auto";
  74. if (node.maxW > 0) {
  75. style.maxWidth = measureToString(node.maxW);
  76. }
  77. style.minWidth = measureToString(node.minW);
  78. }
  79. if (node.h) {
  80. style.height = measureToString(node.h);
  81. } else {
  82. style.height = "auto";
  83. if (node.maxH > 0) {
  84. style.maxHeight = measureToString(node.maxH);
  85. }
  86. style.minHeight = measureToString(node.minH);
  87. }
  88. },
  89. position(node, style) {
  90. const parent = node[_xfa_object.$getParent]();
  91. if (parent && parent.layout && parent.layout !== "position") {
  92. return;
  93. }
  94. style.position = "absolute";
  95. style.left = measureToString(node.x);
  96. style.top = measureToString(node.y);
  97. },
  98. rotate(node, style) {
  99. if (node.rotate) {
  100. if (!("transform" in style)) {
  101. style.transform = "";
  102. }
  103. style.transform += `rotate(-${node.rotate}deg)`;
  104. style.transformOrigin = "top left";
  105. }
  106. },
  107. presence(node, style) {
  108. switch (node.presence) {
  109. case "invisible":
  110. style.visibility = "hidden";
  111. break;
  112. case "hidden":
  113. case "inactive":
  114. style.display = "none";
  115. break;
  116. }
  117. }
  118. };
  119. function layoutClass(node) {
  120. switch (node.layout) {
  121. case "position":
  122. return "xfaPosition";
  123. case "lr-tb":
  124. return "xfaLrTb";
  125. case "rl-row":
  126. return "xfaRlRow";
  127. case "rl-tb":
  128. return "xfaRlTb";
  129. case "row":
  130. return "xfaRow";
  131. case "table":
  132. return "xfaTable";
  133. case "tb":
  134. return "xfaTb";
  135. default:
  136. return "xfaPosition";
  137. }
  138. }
  139. function toStyle(node, ...names) {
  140. const style = Object.create(null);
  141. for (const name of names) {
  142. const value = node[name];
  143. if (value === null) {
  144. continue;
  145. }
  146. if (value instanceof _xfa_object.XFAObject) {
  147. const newStyle = value[_xfa_object.$toStyle]();
  148. if (newStyle) {
  149. Object.assign(style, newStyle);
  150. } else {
  151. (0, _util.warn)(`(DEBUG) - XFA - style for ${name} not implemented yet`);
  152. }
  153. continue;
  154. }
  155. if (converters.hasOwnProperty(name)) {
  156. converters[name](node, style);
  157. }
  158. }
  159. return style;
  160. }