All files / lib/text PointText.ts

60.37% Statements 32/53
77.77% Branches 7/9
80% Functions 4/5
60.37% Lines 32/53

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                              1x 1x 1x                     1x 1x 1x                                                                   1x 11x 11x               1x     7x 7x 7x   1x 2x 2x 2x   1x                                               1x 13x 13x 13x 13x 13x 13x 13x   13x     13x 13x 13x 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 { LinkedPoint, Point } from '~/basic/Point';
import { Rectangle } from '~/basic/Rectangle';
import { TextItem } from './TextItem';
 
/**
 * @name PointText
 *
 * @class A PointText item represents a piece of typography in your Paper.js
 * project which starts from a certain point and extends by the amount of
 * characters contained in it.
 *
 * @extends TextItem
 */
export const PointText = TextItem.extend(
  /** @lends PointText# */ {
    _class: 'PointText',
 
    /**
     * Creates a point text item
     *
     * @name PointText#initialize
     * @param {Point} point the position where the text will start
     * @return {PointText} the newly created point text
     *
     * @example {@paperscript}
     * var text = new PointText(new Point(200, 50));
     * text.justification = 'center';
     * text.fillColor = 'black';
     * text.content = 'The contents of the point text';
     */
    /**
     * Creates a point text item from the properties described by an object
     * literal.
     *
     * @name PointText#initialize
     * @param {Object} object an object containing properties describing the
     *     path's attributes
     * @return {PointText} the newly created point text
     *
     * @example {@paperscript}
     * var text = new PointText({
     *     point: [50, 50],
     *     content: 'The contents of the point text',
     *     fillColor: 'black',
     *     fontFamily: 'Courier New',
     *     fontWeight: 'bold',
     *     fontSize: 25
     * });
     */
    initialize: function PointText() {
      TextItem.apply(this, arguments);
    },
 
    /**
     * The PointText's anchor point
     *
     * @bean
     * @type Point
     */
    getPoint: function () {
      // Se Item#getPosition for an explanation why we create new LinkedPoint
      // objects each time.
      var point = this._matrix.getTranslation();
      return new LinkedPoint(point.x, point.y, this, 'setPoint');
    },
 
    setPoint: function (/* point */) {
      var point = Point.read(arguments);
      this.translate(point.subtract(this._matrix.getTranslation()));
    },
 
    _draw: function (ctx, param, viewMatrix) {
      if (!this._content) return;
      this._setStyles(ctx, param, viewMatrix);
      var lines = this._lines,
        style = this._style,
        hasFill = style.hasFill(),
        hasStroke = style.hasStroke(),
        leading = style.getLeading(),
        shadowColor = ctx.shadowColor;
      ctx.font = style.getFontStyle();
      ctx.textAlign = style.getJustification();
      for (var i = 0, l = lines.length; i < l; i++) {
        // See Path._draw() for explanation about ctx.shadowColor
        ctx.shadowColor = shadowColor;
        var line = lines[i];
        if (hasFill) {
          ctx.fillText(line, 0, 0);
          ctx.shadowColor = 'rgba(0,0,0,0)';
        }
        if (hasStroke) ctx.strokeText(line, 0, 0);
        ctx.translate(0, leading);
      }
    },
 
    _getBounds: function (matrix, options) {
      var style = this._style,
        lines = this._lines,
        numLines = lines.length,
        justification = style.getJustification(),
        leading = style.getLeading(),
        width = this.getView().getTextWidth(style.getFontStyle(), lines),
        x = 0;
      // Adjust for different justifications.
      if (justification !== 'left') x -= width / (justification === 'center' ? 2 : 1);
      // Until we don't have baseline measuring, assume 1 / 4 leading as a
      // rough guess:
      var rect = new Rectangle(x, numLines ? -0.75 * leading : 0, width, numLines * leading);
      return matrix ? matrix._transformBounds(rect, rect) : rect;
    },
  }
);