Source: core/drawing/viewbox/viewboxViewboxGeometry.js

  1. /**
  2. * @fileoverview A class implementing geometry related
  3. * functions for a drawing view-box.
  4. */
  5. goog.provide('xrx.viewbox.ViewboxGeometry');
  6. goog.require('goog.array');
  7. goog.require('goog.object');
  8. goog.require('xrx.geometry.Box');
  9. goog.require('xrx.geometry.Point');
  10. goog.require('xrx.EventTarget');
  11. /**
  12. * A class implementing geometry related functions for a drawing view-box.
  13. * @constructor
  14. * @private
  15. */
  16. xrx.viewbox.ViewboxGeometry = function() {
  17. goog.base(this);
  18. this.testPoint_ = new Array(2);
  19. };
  20. goog.inherits(xrx.viewbox.ViewboxGeometry, xrx.EventTarget);
  21. /**
  22. * Returns the width of this view-box, optionally the transformed
  23. * width (respecting the current scale) and / or the visible width
  24. * (respecting the current rotation).
  25. * @param {boolean} opt_transformed Whether to return the transformed
  26. * width respecting the current scale.
  27. * @param {boolean} opt_visible Whether to return the visible width
  28. * respecting the current rotation.
  29. * @return {number} The width.
  30. * @private
  31. */
  32. xrx.viewbox.ViewboxGeometry.prototype.getWidth = function(opt_transformed,
  33. opt_visible) {
  34. if (opt_visible === true && this.isHorizontal()) {
  35. return this.getHeight(opt_transformed);
  36. } else {
  37. var image = this.getDrawing().getLayerBackground().getImage();
  38. var width = image.getWidth();
  39. return !opt_transformed ? width : width * this.ctm_.getScale();
  40. }
  41. };
  42. /**
  43. * Returns the height of this view-box, optionally the transformed
  44. * height (respecting the current scale) and / or the visible height
  45. * (respecting the current rotation).
  46. * @param {boolean} opt_transformed Whether to return the transformed
  47. * height respecting the current scale.
  48. * @param {boolean} opt_visible Whether to return the visible height
  49. * respecting the current rotation.
  50. * @return {number} The height.
  51. * @private
  52. */
  53. xrx.viewbox.ViewboxGeometry.prototype.getHeight = function(opt_transformed, opt_visible) {
  54. if (opt_visible === true && this.isHorizontal()) {
  55. return this.getWidth(opt_transformed);
  56. } else {
  57. var image = this.getDrawing().getLayerBackground().getImage();
  58. var height = image.getHeight();
  59. return !opt_transformed ? height : height * this.ctm_.getScale();
  60. }
  61. };
  62. /**
  63. * Returns the bounding-box for this view-box, respecting transformation.
  64. * @return {goog.math.Box} The bounding box, coordinates are the visibles
  65. * and are transformed.
  66. * @private
  67. */
  68. xrx.viewbox.ViewboxGeometry.prototype.getBox = function() {
  69. var box;
  70. var rotation = this.getRotation();
  71. var transformed = new Array(4);
  72. var image = this.getDrawing().getLayerBackground().getImage();
  73. var width = image.getWidth();
  74. var height = image.getHeight();
  75. var coords = [0, 0, width, height]
  76. this.ctm_.transform(coords, 0, transformed, 0, 4);
  77. if (rotation === 0) {
  78. box = [transformed[1], transformed[2], transformed[3], transformed[0]];
  79. } else if (rotation === 90) {
  80. box = [transformed[2], transformed[3], transformed[0], transformed[1]];
  81. } else if (rotation === 180) {
  82. box = [transformed[3], transformed[0], transformed[1], transformed[2]];
  83. } else {
  84. box = [transformed[0], transformed[1], transformed[2], transformed[3]];
  85. }
  86. return box;
  87. };
  88. /**
  89. * Whether a point is contained in this view-box.
  90. * @param {Array<number>} point The native point, without translation.
  91. * @private
  92. */
  93. xrx.viewbox.ViewboxGeometry.prototype.containsPoint = function(point) {
  94. var box = this.getBox();
  95. return xrx.geometry.Box.containsPoint(box, point);
  96. };
  97. /**
  98. * @private
  99. */
  100. xrx.viewbox.ViewboxGeometry.prototype.getCenterPoint_ = function(opt_transformed) {
  101. var image = this.getDrawing().getLayerBackground().getImage();
  102. var natural = [image.getWidth() / 2, image.getHeight() / 2];
  103. var transformed;
  104. if (opt_transformed !== true) {
  105. return natural;
  106. } else {
  107. transformed = new Array(2);
  108. this.ctm_.transform(natural, 0, transformed, 0, 1);
  109. return transformed;
  110. }
  111. };
  112. /**
  113. * @private
  114. */
  115. xrx.viewbox.ViewboxGeometry.prototype.getVisibleOrientation_ = function(orientation) {
  116. var orientations = ['NW', 'SW', 'SE', 'NE'];
  117. var index = goog.array.indexOf(orientations, orientation);
  118. var rotations = this.getRotation() / 90;
  119. return orientations[(index + rotations) % 4];
  120. };
  121. /**
  122. * @private
  123. */
  124. xrx.viewbox.ViewboxGeometry.prototype.FixPoints_ = {
  125. 'C': [0, 0],
  126. 'NE': [0, 0],
  127. 'SE': [0, 0],
  128. 'SW': [0, 0],
  129. 'NW': [0, 0]
  130. };
  131. /**
  132. * @private
  133. */
  134. xrx.viewbox.ViewboxGeometry.prototype.getFixPoints_ = function() {
  135. var image = this.getDrawing().getLayerBackground().getImage();
  136. var width = image.getWidth();
  137. var height = image.getHeight();
  138. this.FixPoints_.C = [width / 2, height / 2];
  139. this.FixPoints_.NE = [width, 0];
  140. this.FixPoints_.SE = [width, height];
  141. this.FixPoints_.SW = [0, height];
  142. this.FixPoints_.NW = [0, 0];
  143. return this.FixPoints_;
  144. };
  145. /**
  146. * @private
  147. */
  148. xrx.viewbox.ViewboxGeometry.prototype.getFixPoint_ = function(orientation,
  149. opt_transformed, opt_visible) {
  150. var fixPoints = this.getFixPoints_();
  151. var fixPoint = !opt_visible ? fixPoints[orientation] :
  152. fixPoints[this.getVisibleOrientation_(orientation)];
  153. if (opt_transformed === true) {
  154. var point = new Array(2);
  155. this.ctm_.transform(fixPoint, 0, point, 0, 1);
  156. return point;
  157. }
  158. return fixPoint;
  159. };
  160. /**
  161. * Disposes this view-box.++
  162. */
  163. xrx.viewbox.ViewboxGeometry.prototype.disposeInternal = function() {
  164. this.testPoint_ = null;
  165. goog.base(this, 'disposeInternal');
  166. };