All files / lib/basic Size.ts

88.81% Statements 127/143
92.59% Branches 50/54
79.16% Functions 19/24
88.81% Lines 127/143

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604                              1x 1x 1x 1x                                 1x 1x 1x   1x                                                                                                                               1x 8465x 8465x 8465x 8465x 5800x 5800x 5800x 8465x 389x 389x 2665x 2276x 2276x 2276x 1717x 2276x 6x 559x 369x 553x 184x 184x 184x 2276x 8465x 8465x 8465x                     1x     1x 8468x 8468x 8468x 8468x                                                         1x 376x 376x 376x 372x 371x 374x   376x           1x 8x 8x         1x 5x 5x 5x   1x 1x   1x 1x                                                                 1x 194x 194x 194x                                                                   1x 2x 2x 2x                                                                 1x 832x 832x 832x                                                                 1x 758x 758x 758x                                                               1x         1x 59x 59x               1x 1666x 1666x 1666x             1x 554x 554x                                                                                                                             1x                                             1x 216x 216x                                               1x 1x 1x                           1x     1x 1x 1x 1x 1x   144x 144x 3x 3x 144x 1x 1x 1x                     1x   1x 628x 628x 628x 628x 628x     1x             1x 1167x 1167x   1x         1x 1167x 1167x   1x       1x   1x  
/*
 * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
 * http://paperjs.org/
 *
 * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
 * http://juerglehni.com/ & https://puckey.studio/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * All rights reserved.
 */
 
// TODO: remove eslint-disable comment and deal with errors over time
/* eslint-disable */
 
import { ref } from '~/globals';
import { Base } from '~/straps';
import { Formatter } from '~/util/Formatter';
import { Numerical } from '~/util/Numerical';
 
/**
 * @name Size
 *
 * @class The Size object is used to describe the size or dimensions of
 * something, through its {@link #width} and {@link #height} properties.
 *
 * @classexample
 * // Create a size that is 10pt wide and 5pt high,
 * // and use it to define a rectangle:
 * var size = new Size(10, 5);
 * console.log(size.width); // 10
 * console.log(size.height); // 5
 * var rect = new Rectangle(new Point(20, 15), size);
 * console.log(rect); // { x: 20, y: 15, width: 10, height: 5 }
 */
export const Size = Base.extend(
  /** @lends Size# */ {
    _class: 'Size',
    // Tell Base.read that the Point constructor supports reading with index
    _readIndex: true,
 
    /**
     * Creates a Size object with the given width and height values.
     *
     * @name Size#initialize
     * @param {Number} width the width
     * @param {Number} height the height
     *
     * @example
     * // Create a size that is 10pt wide and 5pt high
     * var size = new Size(10, 5);
     * console.log(size.width); // 10
     * console.log(size.height); // 5
     */
    /**
     * Creates a Size object using the numbers in the given array as
     * dimensions.
     *
     * @name Size#initialize
     * @param {Array} array
     *
     * @example
     * // Creating a size of width: 320, height: 240 using an array of numbers:
     * var array = [320, 240];
     * var size = new Size(array);
     * console.log(size.width); // 320
     * console.log(size.height); // 240
     */
    /**
     * Creates a Size object using the properties in the given object.
     *
     * @name Size#initialize
     * @param {Object} object
     *
     * @example
     * // Creating a size of width: 10, height: 20 using an object literal:
     *
     * var size = new Size({
     *     width: 10,
     *     height: 20
     * });
     * console.log(size.width); // 10
     * console.log(size.height); // 20
     */
    /**
     * Creates a Size object using the coordinates of the given Size object.
     *
     * @name Size#initialize
     * @param {Size} size
     */
    /**
     * Creates a Size object using the {@link Point#x} and {@link Point#y}
     * values of the given Point object.
     *
     * @name Size#initialize
     * @param {Point} point
     *
     * @example
     * var point = new Point(50, 50);
     * var size = new Size(point);
     * console.log(size.width); // 50
     * console.log(size.height); // 50
     */
    initialize: function Size(arg0, arg1) {
      var type = typeof arg0,
        reading = this.__read,
        read = 0;
      if (type === 'number') {
        var hasHeight = typeof arg1 === 'number';
        this._set(arg0, hasHeight ? arg1 : arg0);
        if (reading) read = hasHeight ? 2 : 1;
      } else if (type === 'undefined' || arg0 === null) {
        this._set(0, 0);
        if (reading) read = arg0 === null ? 1 : 0;
      } else {
        var obj = type === 'string' ? arg0.split(/[\s,]+/) || [] : arg0;
        read = 1;
        if (Array.isArray(obj)) {
          this._set(+obj[0], +(obj.length > 1 ? obj[1] : obj[0]));
        } else if ('width' in obj) {
          this._set(obj.width || 0, obj.height || 0);
        } else if ('x' in obj) {
          this._set(obj.x || 0, obj.y || 0);
        } else {
          this._set(0, 0);
          read = 0;
        }
      }
      if (reading) this.__read = read;
      return this;
    },
 
    /**
     * Sets the size to the passed values. Note that any sequence of parameters
     * that is supported by the various {@link Size()} constructors also work
     * for calls of `set()`.
     *
     * @function
     * @param {...*} values
     * @return {Size}
     */
    set: '#initialize',
 
    // See Point#_set() for an explanation of #_set():
    _set: function (width, height) {
      this.width = width;
      this.height = height;
      return this;
    },
 
    /**
     * The width of the size
     *
     * @name Size#width
     * @type Number
     */
 
    /**
     * The height of the size
     *
     * @name Size#height
     * @type Number
     */
 
    /**
     * Checks whether the width and height of the size are equal to those of the
     * supplied size.
     *
     * @param {Size} size the size to compare to
     * @return {Boolean}
     *
     * @example
     * var size = new Size(5, 10);
     * console.log(size == new Size(5, 10)); // true
     * console.log(size == new Size(1, 1)); // false
     * console.log(size != new Size(1, 1)); // true
     */
    equals: function (size) {
      return (
        size === this ||
        (size &&
          ((this.width === size.width && this.height === size.height) ||
            (Array.isArray(size) && this.width === size[0] && this.height === size[1]))) ||
        false
      );
    },
 
    /**
     * Returns a copy of the size.
     * @return {Size}
     */
    clone: function () {
      return new Size(this.width, this.height);
    },
 
    /**
     * @return {String} a string representation of the size
     */
    toString: function () {
      var f = Formatter.instance;
      return '{ width: ' + f.number(this.width) + ', height: ' + f.number(this.height) + ' }';
    },
 
    _serialize: function (options) {
      var f = options.formatter;
      // See Point#_serialize()
      return [f.number(this.width), f.number(this.height)];
    },
 
    /**
     * Returns the addition of the supplied value to the width and height of the
     * size as a new size. The object itself is not modified!
     *
     * @name Size#add
     * @function
     * @operator
     * @param {Number} number the number to add
     * @return {Size} the addition of the size and the value as a new size
     *
     * @example
     * var size = new Size(5, 10);
     * var result = size + 20;
     * console.log(result); // {width: 25, height: 30}
     */
    /**
     * Returns the addition of the width and height of the supplied size to the
     * size as a new size. The object itself is not modified!
     *
     * @name Size#add
     * @function
     * @operator
     * @param {Size} size the size to add
     * @return {Size} the addition of the two sizes as a new size
     *
     * @example
     * var size1 = new Size(5, 10);
     * var size2 = new Size(10, 20);
     * var result = size1 + size2;
     * console.log(result); // {width: 15, height: 30}
     */
    add: function (/* size */) {
      var size = Size.read(arguments);
      return new Size(this.width + size.width, this.height + size.height);
    },
 
    /**
     * Returns the subtraction of the supplied value from the width and height
     * of the size as a new size. The object itself is not modified!
     * The object itself is not modified!
     *
     * @name Size#subtract
     * @function
     * @operator
     * @param {Number} number the number to subtract
     * @return {Size} the subtraction of the size and the value as a new size
     *
     * @example
     * var size = new Size(10, 20);
     * var result = size - 5;
     * console.log(result); // {width: 5, height: 15}
     */
    /**
     * Returns the subtraction of the width and height of the supplied size from
     * the size as a new size. The object itself is not modified!
     *
     * @name Size#subtract
     * @function
     * @operator
     * @param {Size} size the size to subtract
     * @return {Size} the subtraction of the two sizes as a new size
     *
     * @example
     * var firstSize = new Size(10, 20);
     * var secondSize = new Size(5, 5);
     * var result = firstSize - secondSize;
     * console.log(result); // {width: 5, height: 15}
     */
    subtract: function (/* size */) {
      var size = Size.read(arguments);
      return new Size(this.width - size.width, this.height - size.height);
    },
 
    /**
     * Returns the multiplication of the supplied value with the width and
     * height of the size as a new size. The object itself is not modified!
     *
     * @name Size#multiply
     * @function
     * @operator
     * @param {Number} number the number to multiply by
     * @return {Size} the multiplication of the size and the value as a new size
     *
     * @example
     * var size = new Size(10, 20);
     * var result = size * 2;
     * console.log(result); // {width: 20, height: 40}
     */
    /**
     * Returns the multiplication of the width and height of the supplied size
     * with the size as a new size. The object itself is not modified!
     *
     * @name Size#multiply
     * @function
     * @operator
     * @param {Size} size the size to multiply by
     * @return {Size} the multiplication of the two sizes as a new size
     *
     * @example
     * var firstSize = new Size(5, 10);
     * var secondSize = new Size(4, 2);
     * var result = firstSize * secondSize;
     * console.log(result); // {width: 20, height: 20}
     */
    multiply: function (/* size */) {
      var size = Size.read(arguments);
      return new Size(this.width * size.width, this.height * size.height);
    },
 
    /**
     * Returns the division of the supplied value by the width and height of the
     * size as a new size. The object itself is not modified!
     *
     * @name Size#divide
     * @function
     * @operator
     * @param {Number} number the number to divide by
     * @return {Size} the division of the size and the value as a new size
     *
     * @example
     * var size = new Size(10, 20);
     * var result = size / 2;
     * console.log(result); // {width: 5, height: 10}
     */
    /**
     * Returns the division of the width and height of the supplied size by the
     * size as a new size. The object itself is not modified!
     *
     * @name Size#divide
     * @function
     * @operator
     * @param {Size} size the size to divide by
     * @return {Size} the division of the two sizes as a new size
     *
     * @example
     * var firstSize = new Size(8, 10);
     * var secondSize = new Size(2, 5);
     * var result = firstSize / secondSize;
     * console.log(result); // {width: 4, height: 2}
     */
    divide: function (/* size */) {
      var size = Size.read(arguments);
      return new Size(this.width / size.width, this.height / size.height);
    },
 
    /**
     * The modulo operator returns the integer remainders of dividing the size
     * by the supplied value as a new size.
     *
     * @name Size#modulo
     * @function
     * @operator
     * @param {Number} value
     * @return {Size} the integer remainders of dividing the size by the value
     * as a new size
     *
     * @example
     * var size = new Size(12, 6);
     * console.log(size % 5); // {width: 2, height: 1}
     */
    /**
     * The modulo operator returns the integer remainders of dividing the size
     * by the supplied size as a new size.
     *
     * @name Size#modulo
     * @function
     * @operator
     * @param {Size} size
     * @return {Size} the integer remainders of dividing the sizes by each
     * other as a new size
     *
     * @example
     * var size = new Size(12, 6);
     * console.log(size % new Size(5, 2)); // {width: 2, height: 0}
     */
    modulo: function (/* size */) {
      var size = Size.read(arguments);
      return new Size(this.width % size.width, this.height % size.height);
    },
 
    negate: function () {
      return new Size(-this.width, -this.height);
    },
 
    /**
     * {@grouptitle Tests}
     * Checks if this size has both the width and height set to 0.
     *
     * @return {Boolean} {@true if both width and height are 0}
     */
    isZero: function () {
      var isZero = Numerical.isZero;
      return isZero(this.width) && isZero(this.height);
    },
 
    /**
     * Checks if the width or the height of the size are NaN.
     *
     * @return {Boolean} {@true if the width or height of the size are NaN}
     */
    isNaN: function () {
      return isNaN(this.width) || isNaN(this.height);
    },
 
    /**
     * {@grouptitle Math Functions}
     *
     * Returns a new size with rounded {@link #width} and {@link #height}
     * values. The object itself is not modified!
     *
     * @name Size#round
     * @function
     * @return {Size}
     *
     * @example
     * var size = new Size(10.2, 10.9);
     * var roundSize = size.round();
     * console.log(roundSize); // {x: 10, y: 11}
     */
 
    /**
     * Returns a new size with the nearest greater non-fractional values to the
     * specified {@link #width} and {@link #height} values. The object itself is
     * not modified!
     *
     * @name Size#ceil
     * @function
     * @return {Size}
     *
     * @example
     * var size = new Size(10.2, 10.9);
     * var ceilSize = size.ceil();
     * console.log(ceilSize); // {x: 11, y: 11}
     */
 
    /**
     * Returns a new size with the nearest smaller non-fractional values to the
     * specified {@link #width} and {@link #height} values. The object itself is
     * not modified!
     *
     * @name Size#floor
     * @function
     * @return {Size}
     *
     * @example
     * var size = new Size(10.2, 10.9);
     * var floorSize = size.floor();
     * console.log(floorSize); // {x: 10, y: 10}
     */
 
    /**
     * Returns a new size with the absolute values of the specified
     * {@link #width} and {@link #height} values. The object itself is not
     * modified!
     *
     * @name Size#abs
     * @function
     * @return {Size}
     *
     * @example
     * var size = new Size(-5, 10);
     * var absSize = size.abs();
     * console.log(absSize); // {x: 5, y: 10}
     */
 
    statics: /** @lends Size */ {
      /**
       * Returns a new size object with the smallest {@link #width} and
       * {@link #height} of the supplied sizes.
       *
       * @static
       * @param {Size} size1
       * @param {Size} size2
       * @return {Size} the newly created size object
       *
       * @example
       * var size1 = new Size(10, 100);
       * var size2 = new Size(200, 5);
       * var minSize = Size.min(size1, size2);
       * console.log(minSize); // {width: 10, height: 5}
       *
       * @example
       * // Find the minimum of multiple sizes:
       * var size1 = new Size(60, 100);
       * var size2 = new Size(200, 5);
       * var size3 = new Size(250, 35);
       * [size1, size2, size3].reduce(Size.min) // {width: 60, height: 5}
       */
      min: function (size1, size2) {
        return new Size(Math.min(size1.width, size2.width), Math.min(size1.height, size2.height));
      },
 
      /**
       * Returns a new size object with the largest {@link #width} and
       * {@link #height} of the supplied sizes.
       *
       * @static
       * @param {Size} size1
       * @param {Size} size2
       * @return {Size} the newly created size object
       *
       * @example
       * var size1 = new Size(10, 100);
       * var size2 = new Size(200, 5);
       * var maxSize = Size.max(size1, size2);
       * console.log(maxSize); // {width: 200, height: 100}
       *
       * @example
       * // Find the maximum of multiple sizes:
       * var size1 = new Size(60, 100);
       * var size2 = new Size(200, 5);
       * var size3 = new Size(250, 35);
       * [size1, size2, size3].reduce(Size.max) // {width: 250, height: 100}
       */
      max: function (size1, size2) {
        return new Size(Math.max(size1.width, size2.width), Math.max(size1.height, size2.height));
      },
 
      /**
       * Returns a size object with random {@link #width} and {@link #height}
       * values between `0` and `1`.
       *
       * @return {Size} the newly created size object
       * @static
       *
       * @example
       * var maxSize = new Size(100, 100);
       * var randomSize = Size.random();
       * var size = maxSize * randomSize;
       */
      random: function () {
        return new Size(Math.random(), Math.random());
      },
    },
  },
  Base.each(
    ['round', 'ceil', 'floor', 'abs'],
    function (key) {
      // Inject round, ceil, floor, abs:
      var op = Math[key];
      this[key] = function () {
        return new Size(op(this.width), op(this.height));
      };
    },
    {}
  )
);
 
/**
 * @name LinkedSize
 *
 * @class An internal version of Size that notifies its owner of each change
 * through setting itself again on the setter that corresponds to the getter
 * that produced this LinkedSize.
 *
 * @private
 */
export const LinkedSize = Size.extend({
  // Have LinkedSize appear as a normal Size in debugging
  initialize: function Size(width, height, owner, setter) {
    this._width = width;
    this._height = height;
    this._owner = owner;
    this._setter = setter;
  },
 
  // See Point#_set() for an explanation of #_set():
  _set: function (width, height, _dontNotify) {
    this._width = width;
    this._height = height;
    if (!_dontNotify) this._owner[this._setter](this);
    return this;
  },
 
  getWidth: function () {
    return this._width;
  },
 
  setWidth: function (width) {
    this._width = width;
    this._owner[this._setter](this);
  },
 
  getHeight: function () {
    return this._height;
  },
 
  setHeight: function (height) {
    this._height = height;
    this._owner[this._setter](this);
  },
});
 
ref.Size = Size;