您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

createSVG.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. const fs = require("./fs_promises.js"),
  2. path = require("path"),
  3. wboPencilPoint = require("../client-data/tools/pencil/wbo_pencil_point.js").wboPencilPoint;
  4. function htmlspecialchars(str) {
  5. if (typeof str !== "string") return "";
  6. return str.replace(/[<>&"']/g, function (c) {
  7. switch (c) {
  8. case '<': return '&lt;';
  9. case '>': return '&gt;';
  10. case '&': return '&amp;';
  11. case '"': return '&quot;';
  12. case "'": return '&#39;'; }});
  13. }
  14. function renderPath(el, pathstring) {
  15. return '<path ' +
  16. (el.id ?
  17. ('id="' + htmlspecialchars(el.id) + '" ') : '') +
  18. 'stroke-width="' + (el.size | 0) + '" ' +
  19. (el.opacity ?
  20. ('opacity="' + parseFloat(el.opacity) + '" ') : '') +
  21. 'stroke="' + htmlspecialchars(el.color) + '" ' +
  22. 'd="' + pathstring + '" ' +
  23. '/>';
  24. }
  25. const Tools = {
  26. /**
  27. * @return {string}
  28. */
  29. "Text": function (el) {
  30. return '<text ' +
  31. 'id="' + htmlspecialchars(el.id || "t") + '" ' +
  32. 'x="' + (el.x | 0) + '" ' +
  33. 'y="' + (el.y | 0) + '" ' +
  34. 'font-size="' + (el.size | 0) + '" ' +
  35. 'fill="' + htmlspecialchars(el.color || "#000") + '" ' +
  36. '>' + htmlspecialchars(el.txt || "") + '</text>';
  37. },
  38. /**
  39. * @return {string}
  40. */
  41. "Pencil": function (el) {
  42. if (!el._children) return "";
  43. let pts = el._children.reduce(function (pts, point) {
  44. return wboPencilPoint(pts, point.x, point.y);
  45. }, []);
  46. const pathstring = pts.map(function (op) {
  47. return op.type + ' ' + op.values.join(' ')
  48. }).join(' ');
  49. return renderPath(el, pathstring);
  50. },
  51. /**
  52. * @return {string}
  53. */
  54. "Rectangle": function (el) {
  55. return '<rect ' +
  56. (el.id ?
  57. ('id="' + htmlspecialchars(el.id) + '" ') : '') +
  58. 'x="' + (el.x || 0) + '" ' +
  59. 'y="' + (el.y || 0) + '" ' +
  60. 'width="' + (el.x2 - el.x) + '" ' +
  61. 'height="' + (el.y2 - el.y) + '" ' +
  62. 'stroke="' + htmlspecialchars(el.color) + '" ' +
  63. 'stroke-width="' + (el.size | 0) + '" ' +
  64. '/>';
  65. },
  66. /**
  67. * @return {string}
  68. */
  69. "Ellipse": function (el) {
  70. const cx = Math.round((el.x2 + el.x) / 2);
  71. const cy = Math.round((el.y2 + el.y) / 2);
  72. const rx = Math.abs(el.x2 - el.x) / 2;
  73. const ry = Math.abs(el.y2 - el.y) / 2;
  74. const pathstring =
  75. "M" + (cx - rx) + " " + cy +
  76. "a" + rx + "," + ry + " 0 1,0 " + (rx * 2) + ",0" +
  77. "a" + rx + "," + ry + " 0 1,0 " + (rx * -2) + ",0";
  78. return renderPath(el, pathstring);
  79. },
  80. /**
  81. * @return {string}
  82. */
  83. "Straight line": function (el) {
  84. const pathstring = "M" + el.x + " " + el.y + "L" + el.x2 + " " + el.y2;
  85. return renderPath(el, pathstring);
  86. }
  87. };
  88. /**
  89. * Writes the given board as an svg to the given writeable stream
  90. * @param {Object[string, BoardElem]} obj
  91. * @param {WritableStream} writeable
  92. */
  93. async function toSVG(obj, writeable) {
  94. const margin = 400;
  95. const elems = Object.values(obj);
  96. const dim = elems.reduce(function (dim, elem) {
  97. return [
  98. Math.max(elem.x + margin | 0, dim[0]),
  99. Math.max(elem.y + margin | 0, dim[1]),
  100. ]
  101. }, [margin, margin]);
  102. writeable.write(
  103. '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" ' +
  104. 'width="' + dim[0] + '" height="' + dim[1] + '">' +
  105. '<defs><style type="text/css"><![CDATA[' +
  106. 'text {font-family:"Arial"}' +
  107. 'path {fill:none;stroke-linecap:round;stroke-linejoin:round;}' +
  108. 'rect {fill:none}' +
  109. ']]></style></defs>'
  110. );
  111. await Promise.all(elems.map(async function (elem) {
  112. await Promise.resolve(); // Do not block the event loop
  113. const renderFun = Tools[elem.tool];
  114. if (renderFun) writeable.write(renderFun(elem));
  115. else console.warn("Missing render function for tool", elem.tool);
  116. }));
  117. writeable.write('</svg>');
  118. }
  119. async function renderBoard(file, stream) {
  120. const data = await fs.promises.readFile(file);
  121. var board = JSON.parse(data);
  122. return toSVG(board, stream);
  123. }
  124. if (require.main === module) {
  125. const config = require("./configuration.js");
  126. const HISTORY_FILE = process.argv[2] || path.join(config.HISTORY_DIR, "board-anonymous.json");
  127. renderBoard(HISTORY_FILE, process.stdout)
  128. .catch(console.error.bind(console));
  129. } else {
  130. module.exports = { 'renderBoard': renderBoard };
  131. }