SSvgPaintEngine.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.SSvgPaintEngine = void 0;
  17. var lib_1 = require("@saga-web/base/lib");
  18. var SPath2D_1 = require("../SPath2D");
  19. var __1 = require("..");
  20. var SSvgPaintEngine = (function (_super) {
  21. __extends(SSvgPaintEngine, _super);
  22. function SSvgPaintEngine(w, h) {
  23. var _this = _super.call(this) || this;
  24. _this._builder = new lib_1.SStringBuilder();
  25. _this.version = "1.1";
  26. _this.width = 0;
  27. _this.height = 0;
  28. _this.width = w;
  29. _this.height = h;
  30. _this._builder.append("<?xml version='1.0' standalone='no'?>");
  31. _this._builder.append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"");
  32. _this._builder.append(" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">");
  33. _this._builder.append("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"");
  34. _this._builder.append("version=\"" + _this.version + "\" width=\"" + w + "\" height=\"" + h + "\">");
  35. return _this;
  36. }
  37. Object.defineProperty(SSvgPaintEngine.prototype, "type", {
  38. get: function () {
  39. return __1.SPaintEngineType.SVG;
  40. },
  41. enumerable: false,
  42. configurable: true
  43. });
  44. SSvgPaintEngine.prototype.toSvg = function () {
  45. return this._builder.toString("\n") + "</svg>\n";
  46. };
  47. SSvgPaintEngine.prototype.setClip = function (path) {
  48. };
  49. SSvgPaintEngine.prototype.clearRect = function (rect) { };
  50. SSvgPaintEngine.prototype.drawRect = function (rect) {
  51. this._builder.append("<rect x=\"" + rect.x + "\" y=\"" + rect.y + "\" width=\"" + rect.width + "\" height=\"" + rect.height + "\" " + this.getStyle(true, true, false) + " " + this.getSvgMatrix() + "/>");
  52. };
  53. SSvgPaintEngine.prototype.drawCircle = function (cx, cy, r) {
  54. this._builder.append("<circle cx=\"" + cx + "\" cy=\"" + cy + "\" r=\"" + r + "\" " + this.getStyle(true, true, false) + " " + this.getSvgMatrix() + "/>");
  55. };
  56. SSvgPaintEngine.prototype.drawEllipse = function (cx, cy, rx, ry) {
  57. this._builder.append("<ellipse cx=\"" + cx + "\" cy=\"" + cy + "\" rx=\"" + rx + "\" ry=\"" + ry + "\" " + this.getStyle(true, true, false) + " " + this.getSvgMatrix() + "/>");
  58. };
  59. SSvgPaintEngine.prototype.drawArc = function (x, y, width, height, startAngle, endAngle) {
  60. var path = SPath2D_1.SPath2D.arc(x, y, width, height, startAngle, endAngle);
  61. this._builder.append("<path d=\"" + path + "\" " + this.getStyle(false, true, false) + " " + this.getSvgMatrix() + "/>");
  62. };
  63. SSvgPaintEngine.prototype.drawChord = function (x, y, width, height, startAngle, endAngle) {
  64. var path = SPath2D_1.SPath2D.chord(x, y, width, height, startAngle, endAngle);
  65. this._builder.append("<path d=\"" + path + "\" " + this.getStyle(true, true, false) + " " + this.getSvgMatrix() + "/>");
  66. };
  67. SSvgPaintEngine.prototype.drawPie = function (x, y, width, height, startAngle, endAngle) {
  68. var path = SPath2D_1.SPath2D.pie(x, y, width, height, startAngle, endAngle);
  69. this._builder.append("<path d=\"" + path + "\" " + this.getStyle(true, true, false) + " " + this.getSvgMatrix() + "/>");
  70. };
  71. SSvgPaintEngine.prototype.drawLine = function (line) {
  72. this._builder.append("<line x1=\"" + line.x1 + "\" y1=\"" + line.y1 + "\" x2=\"" + line.x2 + "\" y2=\"" + line.y2 + "\" " + this.getStyle(false, true, false) + " " + this.getSvgMatrix() + "/>");
  73. };
  74. SSvgPaintEngine.prototype.drawPolyline = function (points) {
  75. this._builder.append("<polyline points=\"" + SSvgPaintEngine.pointsToStr(points) + "\" " + this.getStyle(false, true, false) + " " + this.getSvgMatrix() + "/>");
  76. };
  77. SSvgPaintEngine.prototype.drawPolygon = function (points) {
  78. this._builder.append("<polygon points=\"" + SSvgPaintEngine.pointsToStr(points) + "\" " + this.getStyle(true, true, false) + " " + this.getSvgMatrix() + "/>");
  79. };
  80. SSvgPaintEngine.prototype.drawPath = function (path) {
  81. this._builder.append("<path d=\"" + path.svgPath() + "\" " + this.getStyle(true, true, false) + " " + this.getSvgMatrix() + "/>");
  82. };
  83. SSvgPaintEngine.prototype.drawText = function (text, x, y, maxWidth) {
  84. this._builder.append("<text x=\"" + x + "\" y=\"" + y + "\" " + this.getStyle(true, false, true) + " " + this.getSvgMatrix() + ">" + text + "</text>>");
  85. };
  86. SSvgPaintEngine.prototype.drawImage = function (img, x, y, width, height) {
  87. };
  88. SSvgPaintEngine.prototype.textWidth = function (text) {
  89. return 0;
  90. };
  91. SSvgPaintEngine.prototype.changeFont = function (font) { };
  92. SSvgPaintEngine.prototype.getSvgMatrix = function () {
  93. return "transform=\"matrix(" + this.state.matrix.a + ", " + this.state.matrix.b + ", " + this.state.matrix.c + ", " + this.state.matrix.d + ", " + this.state.matrix.e + ", " + this.state.matrix.f + ")\"";
  94. };
  95. SSvgPaintEngine.pointsToStr = function (points) {
  96. var strBuilder = new lib_1.SStringBuilder();
  97. for (var _i = 0, points_1 = points; _i < points_1.length; _i++) {
  98. var p = points_1[_i];
  99. strBuilder.append(p.x + "," + p.y);
  100. }
  101. return strBuilder.toString(" ");
  102. };
  103. SSvgPaintEngine.prototype.getStyle = function (brush, pen, font) {
  104. if (brush === void 0) { brush = false; }
  105. if (pen === void 0) { pen = false; }
  106. if (font === void 0) { font = false; }
  107. var builder = new lib_1.SStringBuilder();
  108. builder.append('style="');
  109. if (pen) {
  110. builder.append("stroke:rgba(" + this.state.pen.color.red + ", " + this.state.pen.color.green + ", " + this.state.pen.color.blue + ", " + this.state.pen.color.alpha /
  111. 255.0 + ");");
  112. builder.append("stroke-width:" + this.state.pen.lineWidth + ";");
  113. }
  114. else {
  115. builder.append("stroke-width:0;");
  116. }
  117. if (brush) {
  118. builder.append("fill:rgba(" + this.state.brush.color.red + ", " + this.state.brush.color.green + ", " + this.state.brush.color.blue + ", " + this.state.brush.color
  119. .alpha / 255.0 + ");");
  120. }
  121. else {
  122. builder.append("fill:rgba(0,0,0,0);");
  123. }
  124. if (font) {
  125. builder.append("font-size:" + this.state.font.size + "px;");
  126. }
  127. builder.append('"');
  128. return builder.toString("");
  129. };
  130. return SSvgPaintEngine;
  131. }(__1.SPaintEngine));
  132. exports.SSvgPaintEngine = SSvgPaintEngine;