inout.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.insideOriented = exports.inside = exports.outsideOriented = exports.outside = void 0;
  4. const util_1 = require("./util");
  5. const outside = (portPosition, elemBBox, args) => outsideLayout(portPosition, elemBBox, false, args);
  6. exports.outside = outside;
  7. const outsideOriented = (portPosition, elemBBox, args) => outsideLayout(portPosition, elemBBox, true, args);
  8. exports.outsideOriented = outsideOriented;
  9. const inside = (portPosition, elemBBox, args) => insideLayout(portPosition, elemBBox, false, args);
  10. exports.inside = inside;
  11. const insideOriented = (portPosition, elemBBox, args) => insideLayout(portPosition, elemBBox, true, args);
  12. exports.insideOriented = insideOriented;
  13. function outsideLayout(portPosition, elemBBox, autoOrient, args) {
  14. const offset = args.offset != null ? args.offset : 15;
  15. const angle = elemBBox.getCenter().theta(portPosition);
  16. const bboxAngles = getBBoxAngles(elemBBox);
  17. let y;
  18. let tx;
  19. let ty;
  20. let textAnchor;
  21. let orientAngle = 0;
  22. if (angle < bboxAngles[1] || angle > bboxAngles[2]) {
  23. y = '.3em';
  24. tx = offset;
  25. ty = 0;
  26. textAnchor = 'start';
  27. }
  28. else if (angle < bboxAngles[0]) {
  29. y = '0';
  30. tx = 0;
  31. ty = -offset;
  32. if (autoOrient) {
  33. orientAngle = -90;
  34. textAnchor = 'start';
  35. }
  36. else {
  37. textAnchor = 'middle';
  38. }
  39. }
  40. else if (angle < bboxAngles[3]) {
  41. y = '.3em';
  42. tx = -offset;
  43. ty = 0;
  44. textAnchor = 'end';
  45. }
  46. else {
  47. y = '.6em';
  48. tx = 0;
  49. ty = offset;
  50. if (autoOrient) {
  51. orientAngle = 90;
  52. textAnchor = 'start';
  53. }
  54. else {
  55. textAnchor = 'middle';
  56. }
  57. }
  58. return (0, util_1.toResult)({
  59. position: {
  60. x: Math.round(tx),
  61. y: Math.round(ty),
  62. },
  63. angle: orientAngle,
  64. attrs: {
  65. '.': {
  66. y,
  67. 'text-anchor': textAnchor,
  68. },
  69. },
  70. }, args);
  71. }
  72. function insideLayout(portPosition, elemBBox, autoOrient, args) {
  73. const offset = args.offset != null ? args.offset : 15;
  74. const angle = elemBBox.getCenter().theta(portPosition);
  75. const bboxAngles = getBBoxAngles(elemBBox);
  76. let y;
  77. let tx;
  78. let ty;
  79. let textAnchor;
  80. let orientAngle = 0;
  81. if (angle < bboxAngles[1] || angle > bboxAngles[2]) {
  82. y = '.3em';
  83. tx = -offset;
  84. ty = 0;
  85. textAnchor = 'end';
  86. }
  87. else if (angle < bboxAngles[0]) {
  88. y = '.6em';
  89. tx = 0;
  90. ty = offset;
  91. if (autoOrient) {
  92. orientAngle = 90;
  93. textAnchor = 'start';
  94. }
  95. else {
  96. textAnchor = 'middle';
  97. }
  98. }
  99. else if (angle < bboxAngles[3]) {
  100. y = '.3em';
  101. tx = offset;
  102. ty = 0;
  103. textAnchor = 'start';
  104. }
  105. else {
  106. y = '0em';
  107. tx = 0;
  108. ty = -offset;
  109. if (autoOrient) {
  110. orientAngle = -90;
  111. textAnchor = 'start';
  112. }
  113. else {
  114. textAnchor = 'middle';
  115. }
  116. }
  117. return (0, util_1.toResult)({
  118. position: {
  119. x: Math.round(tx),
  120. y: Math.round(ty),
  121. },
  122. angle: orientAngle,
  123. attrs: {
  124. '.': {
  125. y,
  126. 'text-anchor': textAnchor,
  127. },
  128. },
  129. }, args);
  130. }
  131. function getBBoxAngles(elemBBox) {
  132. const center = elemBBox.getCenter();
  133. const tl = center.theta(elemBBox.getTopLeft());
  134. const bl = center.theta(elemBBox.getBottomLeft());
  135. const br = center.theta(elemBBox.getBottomRight());
  136. const tr = center.theta(elemBBox.getTopRight());
  137. return [tl, tr, br, bl];
  138. }
  139. //# sourceMappingURL=inout.js.map