123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971 |
- 'use strict';
- exports.type = 'perItem';
- exports.active = true;
- exports.description = 'optimizes path data: writes in shorter form, applies transformations';
- exports.params = {
- applyTransforms: true,
- applyTransformsStroked: true,
- makeArcs: {
- threshold: 2.5,
- tolerance: 0.5
- },
- straightCurves: true,
- lineShorthands: true,
- curveSmoothShorthands: true,
- floatPrecision: 3,
- transformPrecision: 5,
- removeUseless: true,
- collapseRepeated: true,
- utilizeAbsolute: true,
- leadingZero: true,
- negativeExtraSpace: true,
- noSpaceAfterFlags: true,
- forceAbsolutePath: false
- };
- var pathElems = require('./_collections.js').pathElems,
- path2js = require('./_path.js').path2js,
- js2path = require('./_path.js').js2path,
- applyTransforms = require('./_path.js').applyTransforms,
- cleanupOutData = require('../lib/svgo/tools').cleanupOutData,
- roundData,
- precision,
- error,
- arcThreshold,
- arcTolerance,
- hasMarkerMid,
- hasStrokeLinecap;
- exports.fn = function(item, params) {
- if (item.isElem(pathElems) && item.hasAttr('d')) {
- precision = params.floatPrecision;
- error = precision !== false ? +Math.pow(.1, precision).toFixed(precision) : 1e-2;
- roundData = precision > 0 && precision < 20 ? strongRound : round;
- if (params.makeArcs) {
- arcThreshold = params.makeArcs.threshold;
- arcTolerance = params.makeArcs.tolerance;
- }
- hasMarkerMid = item.hasAttr('marker-mid');
- var stroke = item.computedAttr('stroke'),
- strokeLinecap = item.computedAttr('stroke');
- hasStrokeLinecap = stroke && stroke != 'none' && strokeLinecap && strokeLinecap != 'butt';
- var data = path2js(item);
-
- if (data.length) {
- convertToRelative(data);
- if (params.applyTransforms) {
- data = applyTransforms(item, data, params);
- }
- data = filters(data, params);
- if (params.utilizeAbsolute) {
- data = convertToMixed(data, params);
- }
- js2path(item, data, params);
- }
- }
- };
- function convertToRelative(path) {
- var point = [0, 0],
- subpathPoint = [0, 0],
- baseItem;
- path.forEach(function(item, index) {
- var instruction = item.instruction,
- data = item.data;
-
- if (data) {
-
-
- if ('mcslqta'.indexOf(instruction) > -1) {
- point[0] += data[data.length - 2];
- point[1] += data[data.length - 1];
- if (instruction === 'm') {
- subpathPoint[0] = point[0];
- subpathPoint[1] = point[1];
- baseItem = item;
- }
- } else if (instruction === 'h') {
- point[0] += data[0];
- } else if (instruction === 'v') {
- point[1] += data[0];
- }
-
-
-
- if (instruction === 'M') {
- if (index > 0) instruction = 'm';
- data[0] -= point[0];
- data[1] -= point[1];
- subpathPoint[0] = point[0] += data[0];
- subpathPoint[1] = point[1] += data[1];
- baseItem = item;
- }
-
-
- else if ('LT'.indexOf(instruction) > -1) {
- instruction = instruction.toLowerCase();
-
-
- data[0] -= point[0];
- data[1] -= point[1];
- point[0] += data[0];
- point[1] += data[1];
-
- } else if (instruction === 'C') {
- instruction = 'c';
-
-
- data[0] -= point[0];
- data[1] -= point[1];
- data[2] -= point[0];
- data[3] -= point[1];
- data[4] -= point[0];
- data[5] -= point[1];
- point[0] += data[4];
- point[1] += data[5];
-
-
- } else if ('SQ'.indexOf(instruction) > -1) {
- instruction = instruction.toLowerCase();
-
-
- data[0] -= point[0];
- data[1] -= point[1];
- data[2] -= point[0];
- data[3] -= point[1];
- point[0] += data[2];
- point[1] += data[3];
-
- } else if (instruction === 'A') {
- instruction = 'a';
-
-
- data[5] -= point[0];
- data[6] -= point[1];
- point[0] += data[5];
- point[1] += data[6];
-
- } else if (instruction === 'H') {
- instruction = 'h';
- data[0] -= point[0];
- point[0] += data[0];
-
- } else if (instruction === 'V') {
- instruction = 'v';
- data[0] -= point[1];
- point[1] += data[0];
- }
- item.instruction = instruction;
- item.data = data;
-
- item.coords = point.slice(-2);
- }
-
- else if (instruction == 'z') {
- if (baseItem) {
- item.coords = baseItem.coords;
- }
- point[0] = subpathPoint[0];
- point[1] = subpathPoint[1];
- }
- item.base = index > 0 ? path[index - 1].coords : [0, 0];
- });
- return path;
- }
- function filters(path, params) {
- var stringify = data2Path.bind(null, params),
- relSubpoint = [0, 0],
- pathBase = [0, 0],
- prev = {};
- path = path.filter(function(item, index, path) {
- var instruction = item.instruction,
- data = item.data,
- next = path[index + 1];
- if (data) {
- var sdata = data,
- circle;
- if (instruction === 's') {
- sdata = [0, 0].concat(data);
- if ('cs'.indexOf(prev.instruction) > -1) {
- var pdata = prev.data,
- n = pdata.length;
-
- sdata[0] = pdata[n - 2] - pdata[n - 4];
- sdata[1] = pdata[n - 1] - pdata[n - 3];
- }
- }
-
- if (
- params.makeArcs &&
- (instruction == 'c' || instruction == 's') &&
- isConvex(sdata) &&
- (circle = findCircle(sdata))
- ) {
- var r = roundData([circle.radius])[0],
- angle = findArcAngle(sdata, circle),
- sweep = sdata[5] * sdata[0] - sdata[4] * sdata[1] > 0 ? 1 : 0,
- arc = {
- instruction: 'a',
- data: [r, r, 0, 0, sweep, sdata[4], sdata[5]],
- coords: item.coords.slice(),
- base: item.base
- },
- output = [arc],
-
- relCenter = [circle.center[0] - sdata[4], circle.center[1] - sdata[5]],
- relCircle = { center: relCenter, radius: circle.radius },
- arcCurves = [item],
- hasPrev = 0,
- suffix = '',
- nextLonghand;
- if (
- prev.instruction == 'c' && isConvex(prev.data) && isArcPrev(prev.data, circle) ||
- prev.instruction == 'a' && prev.sdata && isArcPrev(prev.sdata, circle)
- ) {
- arcCurves.unshift(prev);
- arc.base = prev.base;
- arc.data[5] = arc.coords[0] - arc.base[0];
- arc.data[6] = arc.coords[1] - arc.base[1];
- var prevData = prev.instruction == 'a' ? prev.sdata : prev.data;
- var prevAngle = findArcAngle(prevData,
- {
- center: [prevData[4] + circle.center[0], prevData[5] + circle.center[1]],
- radius: circle.radius
- }
- );
- angle += prevAngle;
- if (angle > Math.PI) arc.data[3] = 1;
- hasPrev = 1;
- }
-
- for (var j = index; (next = path[++j]) && ~'cs'.indexOf(next.instruction);) {
- var nextData = next.data;
- if (next.instruction == 's') {
- nextLonghand = makeLonghand({instruction: 's', data: next.data.slice() },
- path[j - 1].data);
- nextData = nextLonghand.data;
- nextLonghand.data = nextData.slice(0, 2);
- suffix = stringify([nextLonghand]);
- }
- if (isConvex(nextData) && isArc(nextData, relCircle)) {
- angle += findArcAngle(nextData, relCircle);
- if (angle - 2 * Math.PI > 1e-3) break;
- if (angle > Math.PI) arc.data[3] = 1;
- arcCurves.push(next);
- if (2 * Math.PI - angle > 1e-3) {
- arc.coords = next.coords;
- arc.data[5] = arc.coords[0] - arc.base[0];
- arc.data[6] = arc.coords[1] - arc.base[1];
- } else {
-
- arc.data[5] = 2 * (relCircle.center[0] - nextData[4]);
- arc.data[6] = 2 * (relCircle.center[1] - nextData[5]);
- arc.coords = [arc.base[0] + arc.data[5], arc.base[1] + arc.data[6]];
- arc = {
- instruction: 'a',
- data: [r, r, 0, 0, sweep,
- next.coords[0] - arc.coords[0], next.coords[1] - arc.coords[1]],
- coords: next.coords,
- base: arc.coords
- };
- output.push(arc);
- j++;
- break;
- }
- relCenter[0] -= nextData[4];
- relCenter[1] -= nextData[5];
- } else break;
- }
- if ((stringify(output) + suffix).length < stringify(arcCurves).length) {
- if (path[j] && path[j].instruction == 's') {
- makeLonghand(path[j], path[j - 1].data);
- }
- if (hasPrev) {
- var prevArc = output.shift();
- roundData(prevArc.data);
- relSubpoint[0] += prevArc.data[5] - prev.data[prev.data.length - 2];
- relSubpoint[1] += prevArc.data[6] - prev.data[prev.data.length - 1];
- prev.instruction = 'a';
- prev.data = prevArc.data;
- item.base = prev.coords = prevArc.coords;
- }
- arc = output.shift();
- if (arcCurves.length == 1) {
- item.sdata = sdata.slice();
- } else if (arcCurves.length - 1 - hasPrev > 0) {
-
- path.splice.apply(path, [index + 1, arcCurves.length - 1 - hasPrev].concat(output));
- }
- if (!arc) return false;
- instruction = 'a';
- data = arc.data;
- item.coords = arc.coords;
- }
- }
-
-
-
- if (precision !== false) {
- if ('mltqsc'.indexOf(instruction) > -1) {
- for (var i = data.length; i--;) {
- data[i] += item.base[i % 2] - relSubpoint[i % 2];
- }
- } else if (instruction == 'h') {
- data[0] += item.base[0] - relSubpoint[0];
- } else if (instruction == 'v') {
- data[0] += item.base[1] - relSubpoint[1];
- } else if (instruction == 'a') {
- data[5] += item.base[0] - relSubpoint[0];
- data[6] += item.base[1] - relSubpoint[1];
- }
- roundData(data);
- if (instruction == 'h') relSubpoint[0] += data[0];
- else if (instruction == 'v') relSubpoint[1] += data[0];
- else {
- relSubpoint[0] += data[data.length - 2];
- relSubpoint[1] += data[data.length - 1];
- }
- roundData(relSubpoint);
- if (instruction.toLowerCase() == 'm') {
- pathBase[0] = relSubpoint[0];
- pathBase[1] = relSubpoint[1];
- }
- }
-
- if (params.straightCurves) {
- if (
- instruction === 'c' &&
- isCurveStraightLine(data) ||
- instruction === 's' &&
- isCurveStraightLine(sdata)
- ) {
- if (next && next.instruction == 's')
- makeLonghand(next, data);
- instruction = 'l';
- data = data.slice(-2);
- }
- else if (
- instruction === 'q' &&
- isCurveStraightLine(data)
- ) {
- if (next && next.instruction == 't')
- makeLonghand(next, data);
- instruction = 'l';
- data = data.slice(-2);
- }
- else if (
- instruction === 't' &&
- prev.instruction !== 'q' &&
- prev.instruction !== 't'
- ) {
- instruction = 'l';
- data = data.slice(-2);
- }
- else if (
- instruction === 'a' &&
- (data[0] === 0 || data[1] === 0)
- ) {
- instruction = 'l';
- data = data.slice(-2);
- }
- }
-
-
-
- if (
- params.lineShorthands &&
- instruction === 'l'
- ) {
- if (data[1] === 0) {
- instruction = 'h';
- data.pop();
- } else if (data[0] === 0) {
- instruction = 'v';
- data.shift();
- }
- }
-
-
- if (
- params.collapseRepeated &&
- !hasMarkerMid &&
- ('mhv'.indexOf(instruction) > -1) &&
- prev.instruction &&
- instruction == prev.instruction.toLowerCase() &&
- (
- (instruction != 'h' && instruction != 'v') ||
- (prev.data[0] >= 0) == (item.data[0] >= 0)
- )) {
- prev.data[0] += data[0];
- if (instruction != 'h' && instruction != 'v') {
- prev.data[1] += data[1];
- }
- prev.coords = item.coords;
- path[index] = prev;
- return false;
- }
-
- if (params.curveSmoothShorthands && prev.instruction) {
-
- if (instruction === 'c') {
-
- if (
- prev.instruction === 'c' &&
- data[0] === -(prev.data[2] - prev.data[4]) &&
- data[1] === -(prev.data[3] - prev.data[5])
- ) {
- instruction = 's';
- data = data.slice(2);
- }
-
- else if (
- prev.instruction === 's' &&
- data[0] === -(prev.data[0] - prev.data[2]) &&
- data[1] === -(prev.data[1] - prev.data[3])
- ) {
- instruction = 's';
- data = data.slice(2);
- }
-
- else if (
- 'cs'.indexOf(prev.instruction) === -1 &&
- data[0] === 0 &&
- data[1] === 0
- ) {
- instruction = 's';
- data = data.slice(2);
- }
- }
-
- else if (instruction === 'q') {
-
- if (
- prev.instruction === 'q' &&
- data[0] === (prev.data[2] - prev.data[0]) &&
- data[1] === (prev.data[3] - prev.data[1])
- ) {
- instruction = 't';
- data = data.slice(2);
- }
-
- else if (
- prev.instruction === 't' &&
- data[2] === prev.data[0] &&
- data[3] === prev.data[1]
- ) {
- instruction = 't';
- data = data.slice(2);
- }
- }
- }
-
- if (params.removeUseless && !hasStrokeLinecap) {
-
- if (
- (
- 'lhvqtcs'.indexOf(instruction) > -1
- ) &&
- data.every(function(i) { return i === 0; })
- ) {
- path[index] = prev;
- return false;
- }
-
- if (
- instruction === 'a' &&
- data[5] === 0 &&
- data[6] === 0
- ) {
- path[index] = prev;
- return false;
- }
- }
- item.instruction = instruction;
- item.data = data;
- prev = item;
- } else {
-
- relSubpoint[0] = pathBase[0];
- relSubpoint[1] = pathBase[1];
- if (prev.instruction == 'z') return false;
- prev = item;
- }
- return true;
- });
- return path;
- }
- function convertToMixed(path, params) {
- var prev = path[0];
- path = path.filter(function(item, index) {
- if (index == 0) return true;
- if (!item.data) {
- prev = item;
- return true;
- }
- var instruction = item.instruction,
- data = item.data,
- adata = data && data.slice(0);
- if ('mltqsc'.indexOf(instruction) > -1) {
- for (var i = adata.length; i--;) {
- adata[i] += item.base[i % 2];
- }
- } else if (instruction == 'h') {
- adata[0] += item.base[0];
- } else if (instruction == 'v') {
- adata[0] += item.base[1];
- } else if (instruction == 'a') {
- adata[5] += item.base[0];
- adata[6] += item.base[1];
- }
- roundData(adata);
- var absoluteDataStr = cleanupOutData(adata, params),
- relativeDataStr = cleanupOutData(data, params);
-
-
-
-
- if (
- params.forceAbsolutePath || (
- absoluteDataStr.length < relativeDataStr.length &&
- !(
- params.negativeExtraSpace &&
- instruction == prev.instruction &&
- prev.instruction.charCodeAt(0) > 96 &&
- absoluteDataStr.length == relativeDataStr.length - 1 &&
- (data[0] < 0 || /^0\./.test(data[0]) && prev.data[prev.data.length - 1] % 1)
- ))
- ) {
- item.instruction = instruction.toUpperCase();
- item.data = adata;
- }
- prev = item;
- return true;
- });
- return path;
- }
- function isConvex(data) {
- var center = getIntersection([0, 0, data[2], data[3], data[0], data[1], data[4], data[5]]);
- return center &&
- (data[2] < center[0] == center[0] < 0) &&
- (data[3] < center[1] == center[1] < 0) &&
- (data[4] < center[0] == center[0] < data[0]) &&
- (data[5] < center[1] == center[1] < data[1]);
- }
- function getIntersection(coords) {
-
- var a1 = coords[1] - coords[3],
- b1 = coords[2] - coords[0],
- c1 = coords[0] * coords[3] - coords[2] * coords[1],
-
- a2 = coords[5] - coords[7],
- b2 = coords[6] - coords[4],
- c2 = coords[4] * coords[7] - coords[5] * coords[6],
- denom = (a1 * b2 - a2 * b1);
- if (!denom) return;
- var cross = [
- (b1 * c2 - b2 * c1) / denom,
- (a1 * c2 - a2 * c1) / -denom
- ];
- if (
- !isNaN(cross[0]) && !isNaN(cross[1]) &&
- isFinite(cross[0]) && isFinite(cross[1])
- ) {
- return cross;
- }
- }
- function strongRound(data) {
- for (var i = data.length; i-- > 0;) {
- if (data[i].toFixed(precision) != data[i]) {
- var rounded = +data[i].toFixed(precision - 1);
- data[i] = +Math.abs(rounded - data[i]).toFixed(precision + 1) >= error ?
- +data[i].toFixed(precision) :
- rounded;
- }
- }
- return data;
- }
- function round(data) {
- for (var i = data.length; i-- > 0;) {
- data[i] = Math.round(data[i]);
- }
- return data;
- }
- function isCurveStraightLine(data) {
-
- var i = data.length - 2,
- a = -data[i + 1],
- b = data[i],
- d = 1 / (a * a + b * b);
- if (i <= 1 || !isFinite(d)) return false;
-
- while ((i -= 2) >= 0) {
- if (Math.sqrt(Math.pow(a * data[i] + b * data[i + 1], 2) * d) > error)
- return false;
- }
- return true;
- }
- function makeLonghand(item, data) {
- switch (item.instruction) {
- case 's': item.instruction = 'c'; break;
- case 't': item.instruction = 'q'; break;
- }
- item.data.unshift(data[data.length - 2] - data[data.length - 4], data[data.length - 1] - data[data.length - 3]);
- return item;
- }
- function getDistance(point1, point2) {
- return Math.hypot(point1[0] - point2[0], point1[1] - point2[1]);
- }
- function getCubicBezierPoint(curve, t) {
- var sqrT = t * t,
- cubT = sqrT * t,
- mt = 1 - t,
- sqrMt = mt * mt;
- return [
- 3 * sqrMt * t * curve[0] + 3 * mt * sqrT * curve[2] + cubT * curve[4],
- 3 * sqrMt * t * curve[1] + 3 * mt * sqrT * curve[3] + cubT * curve[5]
- ];
- }
- function findCircle(curve) {
- var midPoint = getCubicBezierPoint(curve, 1/2),
- m1 = [midPoint[0] / 2, midPoint[1] / 2],
- m2 = [(midPoint[0] + curve[4]) / 2, (midPoint[1] + curve[5]) / 2],
- center = getIntersection([
- m1[0], m1[1],
- m1[0] + m1[1], m1[1] - m1[0],
- m2[0], m2[1],
- m2[0] + (m2[1] - midPoint[1]), m2[1] - (m2[0] - midPoint[0])
- ]),
- radius = center && getDistance([0, 0], center),
- tolerance = Math.min(arcThreshold * error, arcTolerance * radius / 100);
- if (center && radius < 1e15 &&
- [1/4, 3/4].every(function(point) {
- return Math.abs(getDistance(getCubicBezierPoint(curve, point), center) - radius) <= tolerance;
- }))
- return { center: center, radius: radius};
- }
- function isArc(curve, circle) {
- var tolerance = Math.min(arcThreshold * error, arcTolerance * circle.radius / 100);
- return [0, 1/4, 1/2, 3/4, 1].every(function(point) {
- return Math.abs(getDistance(getCubicBezierPoint(curve, point), circle.center) - circle.radius) <= tolerance;
- });
- }
- function isArcPrev(curve, circle) {
- return isArc(curve, {
- center: [circle.center[0] + curve[4], circle.center[1] + curve[5]],
- radius: circle.radius
- });
- }
- function findArcAngle(curve, relCircle) {
- var x1 = -relCircle.center[0],
- y1 = -relCircle.center[1],
- x2 = curve[4] - relCircle.center[0],
- y2 = curve[5] - relCircle.center[1];
- return Math.acos(
- (x1 * x2 + y1 * y2) /
- Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2))
- );
- }
- function data2Path(params, pathData) {
- return pathData.reduce(function(pathString, item) {
- var strData = '';
- if (item.data) {
- strData = cleanupOutData(roundData(item.data.slice()), params);
- }
- return pathString + item.instruction + strData;
- }, '');
- }
|