All files / lib/svg SvgExport.ts

91.34% Statements 327/358
80.9% Branches 89/110
82.35% Functions 14/17
91.34% Lines 327/358

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                              1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x           1x   36x   36x   28x 28x 28x             10x 10x 9x 9x 9x 10x 1x 1x 10x 10x 10x 28x     5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x     5x 28x 28x   36x   7x 7x 7x 7x 12x   12x 12x 12x 1x 1x 1x 1x 1x 1x 12x 11x 11x 12x 12x 7x 7x   36x                           36x 15x 15x 7x   7x 7x 11x 11x 11x   11x 15x 3x 2x 2x 2x 9x 9x 2x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 8x 8x 8x 11x 15x   36x 9x 9x 9x 9x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 9x 6x 2x 6x 4x 4x 4x 6x 9x 9x   36x               36x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   36x           2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x     6x 6x 6x 2x 2x 2x 2x   36x             36x 36x 36x 36x 36x 36x 36x 36x 36x 36x   36x 28x 28x 28x   28x   28x     448x 448x 448x 448x 448x 28x 420x 448x 248x       10x 10x 10x 248x 16x 248x 232x 232x 94x 138x 10x     2x 8x 128x 16x 112x   112x 232x 248x 28x   28x   28x   28x   28x 28x   36x 36x 4x     4x 4x   36x       4x   4x   4x   4x 4x   36x 15x 15x 15x         3x 3x     4x 3x       3x 3x 4x 4x   3x 3x 15x 15x   36x 28x 28x 28x     28x 28x 28x 28x 28x 28x 28x   36x 15x 15x 15x 15x   36x 36x 9x 9x 9x 36x   36x 36x 6x 6x 6x 6x 6x 6x 6x 6x   6x 5x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x     6x       6x 6x 6x 6x 6x 36x 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 { Base } from '~/straps';
import { Matrix } from '~/basic/Matrix';
import { Point } from '~/basic/Point';
import { Rectangle } from '~/basic/Rectangle';
import { Item } from '~/item/Item';
import { Project } from '~/item/Project';
import { Formatter } from '~/util/Formatter';
import { Numerical } from '~/util/Numerical';
import { UID } from '~/util/UID';
import { SvgElement } from './SvgElement';
import { SvgStyles } from './SvgStyles';
 
/**
 * A function scope holding all the functionality needed to convert a
 * Paper.js DOM to a SVG DOM.
 */
new (function () {
  // TODO: Consider moving formatter into options object, and pass it along.
  var formatter;
 
  function getTransform(matrix, coordinates, center) {
    // Use new Base() so we can use Base#set() on it.
    var attrs = new Base(),
      trans = matrix.getTranslation();
    if (coordinates) {
      // If the item supports x- and y- coordinates, we're taking out the
      // translation part of the matrix and move it to x, y attributes, to
      // produce more readable markup, and not have to use center points
      // in rotate(). To do so, SVG requires us to inverse transform the
      // translation point by the matrix itself, since they are provided
      // in local coordinates.
      var point;
      if (matrix.isInvertible()) {
        matrix = matrix._shiftless();
        point = matrix._inverseTransform(trans);
        trans = null;
      } else {
        point = new Point();
      }
      attrs[center ? 'cx' : 'x'] = point.x;
      attrs[center ? 'cy' : 'y'] = point.y;
    }
    if (!matrix.isIdentity()) {
      // See if we can decompose the matrix and can formulate it as a
      // simple translate/scale/rotate command sequence.
      var decomposed = matrix.decompose();
      if (decomposed) {
        var parts = [],
          angle = decomposed.rotation,
          scale = decomposed.scaling,
          skew = decomposed.skewing;
        if (trans && !trans.isZero()) parts.push('translate(' + formatter.point(trans) + ')');
        if (angle) parts.push('rotate(' + formatter.number(angle) + ')');
        if (!Numerical.isZero(scale.x - 1) || !Numerical.isZero(scale.y - 1))
          parts.push('scale(' + formatter.point(scale) + ')');
        if (skew.x) parts.push('skewX(' + formatter.number(skew.x) + ')');
        if (skew.y) parts.push('skewY(' + formatter.number(skew.y) + ')');
        attrs.transform = parts.join(' ');
      } else {
        attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')';
      }
    }
    return attrs;
  }
 
  function exportGroup(item, options) {
    // @ts-expect-error
    var attrs = getTransform(item._matrix),
      children = item._children;
    var node = SvgElement.create('g', attrs, formatter);
    for (var i = 0, l = children.length; i < l; i++) {
      var child = children[i];
      // @ts-expect-error
      var childNode = exportSVG(child, options);
      if (childNode) {
        if (child.isClipMask()) {
          var clip = SvgElement.create('clipPath');
          clip.appendChild(childNode);
          setDefinition(child, clip, 'clip');
          SvgElement.set(node, {
            'clip-path': 'url(#' + clip.id + ')',
          });
        } else {
          node.appendChild(childNode);
        }
      }
    }
    return node;
  }
 
  function exportRaster(item, options) {
    // @ts-expect-error
    var attrs = getTransform(item._matrix, true),
      size = item.getSize(),
      image = item.getImage();
    // Take into account that rasters are centered:
    attrs.x -= size.width / 2;
    attrs.y -= size.height / 2;
    attrs.width = size.width;
    attrs.height = size.height;
    attrs.href = (options.embedImages == false && image && image.src) || item.toDataURL();
    return SvgElement.create('image', attrs, formatter);
  }
 
  function exportPath(item, options) {
    var matchShapes = options.matchShapes;
    if (matchShapes) {
      var shape = item.toShape(false);
      // @ts-expect-error
      if (shape) return exportShape(shape, options);
    }
    var segments = item._segments,
      length = segments.length,
      type,
      // @ts-expect-error
      attrs = getTransform(item._matrix);
    if (matchShapes && length >= 2 && !item.hasHandles()) {
      if (length > 2) {
        type = item._closed ? 'polygon' : 'polyline';
        var parts = [];
        for (var i = 0; i < length; i++) {
          parts.push(formatter.point(segments[i]._point));
        }
        attrs.points = parts.join(' ');
      } else {
        type = 'line';
        var start = segments[0]._point,
          end = segments[1]._point;
        attrs.set({
          x1: start.x,
          y1: start.y,
          x2: end.x,
          y2: end.y,
        });
      }
    } else {
      type = 'path';
      attrs.d = item.getPathData(null, options.precision);
    }
    return SvgElement.create(type, attrs, formatter);
  }
 
  function exportShape(item) {
    var type = item._type,
      radius = item._radius,
      attrs = getTransform(item._matrix, true, type !== 'rectangle');
    if (type === 'rectangle') {
      type = 'rect'; // SVG
      var size = item._size,
        width = size.width,
        height = size.height;
      attrs.x -= width / 2;
      attrs.y -= height / 2;
      attrs.width = width;
      attrs.height = height;
      if (radius.isZero()) radius = null;
    }
    if (radius) {
      if (type === 'circle') {
        attrs.r = radius;
      } else {
        attrs.rx = radius.width;
        attrs.ry = radius.height;
      }
    }
    return SvgElement.create(type, attrs, formatter);
  }
 
  function exportCompoundPath(item, options) {
    // @ts-expect-error
    var attrs = getTransform(item._matrix);
    var data = item.getPathData(null, options.precision);
    if (data) attrs.d = data;
    return SvgElement.create('path', attrs, formatter);
  }
 
  function exportSymbolItem(item, options) {
    // @ts-expect-error
    var attrs = getTransform(item._matrix, true),
      definition = item._definition,
      node = getDefinition(definition, 'symbol'),
      definitionItem = definition._item,
      bounds = definitionItem.getStrokeBounds();
    if (!node) {
      node = SvgElement.create('symbol', {
        viewBox: formatter.rectangle(bounds),
      });
      // @ts-expect-error
      node.appendChild(exportSVG(definitionItem, options));
      setDefinition(definition, node, 'symbol');
    }
    attrs.href = '#' + node.id;
    attrs.x += bounds.x;
    attrs.y += bounds.y;
    attrs.width = bounds.width;
    attrs.height = bounds.height;
    attrs.overflow = 'visible';
    return SvgElement.create('use', attrs, formatter);
  }
 
  function exportGradient(color) {
    // NOTE: As long as the fillTransform attribute is not implemented,
    // we need to create a separate gradient object for each gradient,
    // even when they share the same gradient definition.
    // http://www.svgopen.org/2011/papers/20-Separating_gradients_from_geometry/
    // TODO: Implement gradient merging in SvgImport
    var gradientNode = getDefinition(color, 'color');
    if (!gradientNode) {
      var gradient = color.getGradient(),
        radial = gradient._radial,
        origin = color.getOrigin(),
        destination = color.getDestination(),
        attrs;
      if (radial) {
        attrs = {
          cx: origin.x,
          cy: origin.y,
          r: origin.getDistance(destination),
        };
        var highlight = color.getHighlight();
        if (highlight) {
          attrs.fx = highlight.x;
          attrs.fy = highlight.y;
        }
      } else {
        attrs = {
          x1: origin.x,
          y1: origin.y,
          x2: destination.x,
          y2: destination.y,
        };
      }
      attrs.gradientUnits = 'userSpaceOnUse';
      gradientNode = SvgElement.create((radial ? 'radial' : 'linear') + 'Gradient', attrs, formatter);
      var stops = gradient._stops;
      for (var i = 0, l = stops.length; i < l; i++) {
        var stop = stops[i],
          stopColor = stop._color,
          alpha = stopColor.getAlpha(),
          offset = stop._offset;
        attrs = {
          offset: offset == null ? i / (l - 1) : offset,
        };
        if (stopColor) attrs['stop-color'] = stopColor.toCSS(true);
        // See applyStyle for an explanation of why there are separated
        // opacity / color attributes.
        if (alpha < 1) attrs['stop-opacity'] = alpha;
        gradientNode.appendChild(SvgElement.create('stop', attrs, formatter));
      }
      setDefinition(color, gradientNode, 'color');
    }
    return 'url(#' + gradientNode.id + ')';
  }
 
  function exportText(item) {
    // @ts-expect-error
    var node = SvgElement.create('text', getTransform(item._matrix, true), formatter);
    node.textContent = item._content;
    return node;
  }
 
  var exporters = {
    Group: exportGroup,
    Layer: exportGroup,
    Raster: exportRaster,
    Path: exportPath,
    Shape: exportShape,
    CompoundPath: exportCompoundPath,
    SymbolItem: exportSymbolItem,
    PointText: exportText,
  };
 
  function applyStyle(item, node, options, isRoot) {
    var attrs: any = {},
      parent = !isRoot && item.getParent(),
      style = [];
 
    if (item._name != null) attrs.id = item._name;
 
    Base.each(SvgStyles, function (entry) {
      // Get a given style only if it differs from the value on the parent
      // (A layer or group which can have style values in SVG).
      var get = entry.get,
        type = entry.type,
        value = item[get]();
      if (
        entry.exportFilter
          ? entry.exportFilter(item, value)
          : options.reduceAttributes == false || !parent || !Base.equals(parent[get](), value)
      ) {
        if (type === 'color' && value != null) {
          // Support for css-style rgba() values is not in SVG 1.1, so
          // separate the alpha value of colors with alpha into the
          // separate fill- / stroke-opacity attribute:
          var alpha = value.getAlpha();
          if (alpha < 1) attrs[entry.attribute + '-opacity'] = alpha;
        }
        if (type === 'style') {
          style.push(entry.attribute + ': ' + value);
        } else {
          attrs[entry.attribute] =
            value == null
              ? 'none'
              : type === 'color'
                ? value.gradient
                  ? // true for noAlpha, see above
                    // @ts-expect-error
                    exportGradient(value, item)
                  : value.toCSS(true)
                : type === 'array'
                  ? value.join(',')
                  : type === 'lookup'
                    ? entry.toSVG[value]
                    : value;
        }
      }
    });
 
    if (style.length) attrs.style = style.join(';');
 
    if (attrs.opacity === 1) delete attrs.opacity;
 
    if (!item._visible) attrs.visibility = 'hidden';
 
    return SvgElement.set(node, attrs, formatter);
  }
 
  var definitions;
  function getDefinition(item, type) {
    if (!definitions) definitions = { ids: {}, svgs: {} };
    // Use #__id for items that don't have internal #_id properties (Color),
    // and give them ids from their own private id pool named 'svg'.
    return item && definitions.svgs[type + '-' + (item._id || item.__id || (item.__id = UID.get('svg')))];
  }
 
  function setDefinition(item, node, type) {
    // Make sure the definitions lookup is created before we use it.
    // This is required by 'clip', where getDefinition() is not called.
    // @ts-expect-error
    if (!definitions) getDefinition();
    // Have different id ranges per type
    var typeId = (definitions.ids[type] = (definitions.ids[type] || 0) + 1);
    // Give the svg node an id, and link to it from the item id.
    node.id = type + '-' + typeId;
    // See getDefinition() for an explanation of #__id:
    definitions.svgs[type + '-' + (item._id || item.__id)] = node;
  }
 
  function exportDefinitions(node, options) {
    var svg = node,
      defs = null;
    if (definitions) {
      // We can only use svg nodes as definition containers. Have the loop
      // produce one if it's a single item of another type (when calling
      // #exportSVG() on an item rather than a whole project)
      // jsdom in Node.js uses uppercase values for nodeName...
      svg = node.nodeName.toLowerCase() === 'svg' && node;
      for (var i in definitions.svgs) {
        // This code is inside the loop so we only create a container if
        // we actually have svgs.
        if (!defs) {
          if (!svg) {
            svg = SvgElement.create('svg');
            svg.appendChild(node);
          }
          defs = svg.insertBefore(SvgElement.create('defs'), svg.firstChild);
        }
        defs.appendChild(definitions.svgs[i]);
      }
      // Clear definitions at the end of export
      definitions = null;
    }
    return options.asString ? new globalThis.XMLSerializer().serializeToString(svg) : svg;
  }
 
  function exportSVG(item, options, isRoot) {
    var exporter = exporters[item._class],
      node = exporter && exporter(item, options);
    if (node) {
      // Support onExportItem callback, to provide mechanism to handle
      // special attributes (e.g. inkscape:transform-center)
      var onExport = options.onExport;
      if (onExport) node = onExport(item, node, options) || node;
      var data = JSON.stringify(item._data);
      if (data && data !== '{}' && data !== 'null') node.setAttribute('data-paper-data', data);
    }
    return node && applyStyle(item, node, options, isRoot);
  }
 
  function setOptions(options) {
    if (!options) options = {};
    formatter = new Formatter(options.precision);
    return options;
  }
 
  Item.inject({
    exportSVG: function (options) {
      options = setOptions(options);
      return exportDefinitions(exportSVG(this, options, true), options);
    },
  });
 
  Project.inject({
    exportSVG: function (options) {
      options = setOptions(options);
      var children = this._children,
        view = this.getView(),
        bounds = Base.pick(options.bounds, 'view'),
        mx = options.matrix || (bounds === 'view' && view._matrix),
        matrix = mx && Matrix.read([mx]),
        rect =
          bounds === 'view'
            ? new Rectangle([0, 0], view.getViewSize())
            : bounds === 'content'
              ? Item._getBounds(children, matrix, { stroke: true }).rect
              : Rectangle.read([bounds], 0, { readNull: true }),
        attrs: any = {
          version: '1.1',
          xmlns: SvgElement.svg,
          'xmlns:xlink': SvgElement.xlink,
        };
      if (rect) {
        attrs.width = rect.width;
        attrs.height = rect.height;
        if (rect.x || rect.x === 0 || rect.y || rect.y === 0) attrs.viewBox = formatter.rectangle(rect);
      }
      var node = SvgElement.create('svg', attrs, formatter),
        parent = node;
      // If the view has a transformation, wrap all layers in a group with
      // that transformation applied to.
      if (matrix && !matrix.isIdentity()) {
        // @ts-expect-error
        parent = node.appendChild(SvgElement.create('g', getTransform(matrix), formatter));
      }
      for (var i = 0, l = children.length; i < l; i++) {
        parent.appendChild(exportSVG(children[i], options, true));
      }
      return exportDefinitions(node, options);
    },
  });
})();