123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820 |
- var svgedit = svgedit || {};
- (function() {
- if (!svgedit.recalculate) {
- svgedit.recalculate = {};
- }
- var NS = svgedit.NS;
- var context_;
- svgedit.recalculate.init = function(editorContext) {
- context_ = editorContext;
- };
- svgedit.recalculate.updateClipPath = function(attr, tx, ty) {
- var path = getRefElem(attr).firstChild;
- var cp_xform = svgedit.transformlist.getTransformList(path);
- var newxlate = context_.getSVGRoot().createSVGTransform();
- newxlate.setTranslate(tx, ty);
- cp_xform.appendItem(newxlate);
-
- svgedit.recalculate.recalculateDimensions(path);
- };
- svgedit.recalculate.recalculateDimensions = function(selected) {
- if (selected == null) {return null;}
-
- if (selected.nodeName == "svg" && navigator.userAgent.indexOf("Firefox/20") >= 0) {
- return null;
- }
- var svgroot = context_.getSVGRoot();
- var tlist = svgedit.transformlist.getTransformList(selected);
- var k;
-
- if (tlist && tlist.numberOfItems > 0) {
- k = tlist.numberOfItems;
- while (k--) {
- var xform = tlist.getItem(k);
- if (xform.type === 0) {
- tlist.removeItem(k);
- }
-
- else if (xform.type === 1) {
- if (svgedit.math.isIdentity(xform.matrix)) {
- tlist.removeItem(k);
- }
- }
-
- else if (xform.type === 4) {
- if (xform.angle === 0) {
- tlist.removeItem(k);
- }
- }
- }
-
- if (tlist.numberOfItems === 1 &&
- svgedit.utilities.getRotationAngle(selected)) {return null;}
- }
-
- if (!tlist || tlist.numberOfItems == 0) {
-
- selected.setAttribute('transform', '');
- selected.removeAttribute('transform');
- return null;
- }
-
- if (tlist) {
- k = tlist.numberOfItems;
- var mxs = [];
- while (k--) {
- var xform = tlist.getItem(k);
- if (xform.type === 1) {
- mxs.push([xform.matrix, k]);
- } else if (mxs.length) {
- mxs = [];
- }
- }
- if (mxs.length === 2) {
- var m_new = svgroot.createSVGTransformFromMatrix(svgedit.math.matrixMultiply(mxs[1][0], mxs[0][0]));
- tlist.removeItem(mxs[0][1]);
- tlist.removeItem(mxs[1][1]);
- tlist.insertItemBefore(m_new, mxs[1][1]);
- }
-
- k = tlist.numberOfItems;
- if (k >= 2 && tlist.getItem(k-2).type === 1 && tlist.getItem(k-1).type === 2) {
- var mt = svgroot.createSVGTransform();
- var m = svgedit.math.matrixMultiply(
- tlist.getItem(k-2).matrix,
- tlist.getItem(k-1).matrix);
- mt.setMatrix(m);
- tlist.removeItem(k-2);
- tlist.removeItem(k-2);
- tlist.appendItem(mt);
- }
- }
-
- switch ( selected.tagName ) {
-
- case 'line':
- case 'polyline':
- case 'polygon':
- case 'path':
- break;
- default:
- if ((tlist.numberOfItems === 1 && tlist.getItem(0).type === 1) ||
- (tlist.numberOfItems === 2 && tlist.getItem(0).type === 1 && tlist.getItem(0).type === 4)) {
- return null;
- }
- }
-
- var gsvg = $(selected).data('gsvg');
-
- var batchCmd = new svgedit.history.BatchCommand('Transform');
-
- var changes = {}, initial = null, attrs = [];
- switch (selected.tagName) {
- case 'line':
- attrs = ['x1', 'y1', 'x2', 'y2'];
- break;
- case 'circle':
- attrs = ['cx', 'cy', 'r'];
- break;
- case 'ellipse':
- attrs = ['cx', 'cy', 'rx', 'ry'];
- break;
- case 'foreignObject':
- case 'rect':
- case 'image':
- attrs = ['width', 'height', 'x', 'y'];
- break;
- case 'use':
- case 'text':
- case 'tspan':
- attrs = ['x', 'y'];
- break;
- case 'polygon':
- case 'polyline':
- initial = {};
- initial.points = selected.getAttribute('points');
- var list = selected.points;
- var len = list.numberOfItems;
- changes.points = new Array(len);
- var i;
- for (i = 0; i < len; ++i) {
- var pt = list.getItem(i);
- changes.points[i] = {x:pt.x, y:pt.y};
- }
- break;
- case 'path':
- initial = {};
- initial.d = selected.getAttribute('d');
- changes.d = selected.getAttribute('d');
- break;
- }
- if (attrs.length) {
- changes = $(selected).attr(attrs);
- $.each(changes, function(attr, val) {
- changes[attr] = svgedit.units.convertToNum(attr, val);
- });
- } else if (gsvg) {
-
- changes = {
- x: $(gsvg).attr('x') || 0,
- y: $(gsvg).attr('y') || 0
- };
- }
-
-
- if (initial == null) {
- initial = $.extend(true, {}, changes);
- $.each(initial, function(attr, val) {
- initial[attr] = svgedit.units.convertToNum(attr, val);
- });
- }
-
- initial.transform = context_.getStartTransform() || '';
-
- if ((selected.tagName == 'g' && !gsvg) || selected.tagName == 'a') {
- var box = svgedit.utilities.getBBox(selected),
- oldcenter = {x: box.x+box.width/2, y: box.y+box.height/2},
- newcenter = svgedit.math.transformPoint(box.x+box.width/2,
- box.y+box.height/2,
- svgedit.math.transformListToTransform(tlist).matrix),
- m = svgroot.createSVGMatrix();
-
- var gangle = svgedit.utilities.getRotationAngle(selected);
- if (gangle) {
- var a = gangle * Math.PI / 180;
- if ( Math.abs(a) > (1.0e-10) ) {
- var s = Math.sin(a)/(1 - Math.cos(a));
- } else {
-
- var s = 2/a;
- }
- var i;
- for (i = 0; i < tlist.numberOfItems; ++i) {
- var xform = tlist.getItem(i);
- if (xform.type == 4) {
-
- var rm = xform.matrix;
- oldcenter.y = (s*rm.e + rm.f)/2;
- oldcenter.x = (rm.e - s*rm.f)/2;
- tlist.removeItem(i);
- break;
- }
- }
- }
- var tx = 0, ty = 0,
- operation = 0,
- N = tlist.numberOfItems;
- if (N) {
- var first_m = tlist.getItem(0).matrix;
- }
-
- if (N >= 3 && tlist.getItem(N-2).type == 3 &&
- tlist.getItem(N-3).type == 2 && tlist.getItem(N-1).type == 2)
- {
- operation = 3;
-
-
-
- var tm = tlist.getItem(N-3).matrix,
- sm = tlist.getItem(N-2).matrix,
- tmn = tlist.getItem(N-1).matrix;
-
- var children = selected.childNodes;
- var c = children.length;
- while (c--) {
- var child = children.item(c);
- tx = 0;
- ty = 0;
- if (child.nodeType == 1) {
- var childTlist = svgedit.transformlist.getTransformList(child);
-
- if (!childTlist) {continue;}
- var m = svgedit.math.transformListToTransform(childTlist).matrix;
-
-
- var angle = svgedit.utilities.getRotationAngle(child);
- var oldStartTransform = context_.getStartTransform();
- var childxforms = [];
- context_.setStartTransform(child.getAttribute('transform'));
- if (angle || svgedit.math.hasMatrixTransform(childTlist)) {
- var e2t = svgroot.createSVGTransform();
- e2t.setMatrix(svgedit.math.matrixMultiply(tm, sm, tmn, m));
- childTlist.clear();
- childTlist.appendItem(e2t);
- childxforms.push(e2t);
- }
-
- else {
-
-
-
-
-
-
-
-
- var t2n = svgedit.math.matrixMultiply(m.inverse(), tmn, m);
-
- var t2 = svgroot.createSVGMatrix();
- t2.e = -t2n.e;
- t2.f = -t2n.f;
-
-
-
- var s2 = svgedit.math.matrixMultiply(t2.inverse(), m.inverse(), tm, sm, tmn, m, t2n.inverse());
- var translateOrigin = svgroot.createSVGTransform(),
- scale = svgroot.createSVGTransform(),
- translateBack = svgroot.createSVGTransform();
- translateOrigin.setTranslate(t2n.e, t2n.f);
- scale.setScale(s2.a, s2.d);
- translateBack.setTranslate(t2.e, t2.f);
- childTlist.appendItem(translateBack);
- childTlist.appendItem(scale);
- childTlist.appendItem(translateOrigin);
- childxforms.push(translateBack);
- childxforms.push(scale);
- childxforms.push(translateOrigin);
- }
- batchCmd.addSubCommand( svgedit.recalculate.recalculateDimensions(child) );
-
-
-
- context_.setStartTransform(oldStartTransform);
- }
- }
-
- tlist.removeItem(N-1);
- tlist.removeItem(N-2);
- tlist.removeItem(N-3);
- } else if (N >= 3 && tlist.getItem(N-1).type == 1) {
- operation = 3;
- m = svgedit.math.transformListToTransform(tlist).matrix;
- var e2t = svgroot.createSVGTransform();
- e2t.setMatrix(m);
- tlist.clear();
- tlist.appendItem(e2t);
- }
-
-
-
- else if ( (N == 1 || (N > 1 && tlist.getItem(1).type != 3)) &&
- tlist.getItem(0).type == 2)
- {
- operation = 2;
- var T_M = svgedit.math.transformListToTransform(tlist).matrix;
- tlist.removeItem(0);
- var M_inv = svgedit.math.transformListToTransform(tlist).matrix.inverse();
- var M2 = svgedit.math.matrixMultiply( M_inv, T_M );
-
- tx = M2.e;
- ty = M2.f;
- if (tx != 0 || ty != 0) {
-
- var children = selected.childNodes;
- var c = children.length;
-
- var clipPaths_done = [];
-
- while (c--) {
- var child = children.item(c);
- if (child.nodeType == 1) {
-
-
- if (child.getAttribute('clip-path')) {
-
- var attr = child.getAttribute('clip-path');
- if (clipPaths_done.indexOf(attr) === -1) {
- svgedit.recalculate.updateClipPath(attr, tx, ty);
- clipPaths_done.push(attr);
- }
- }
- var oldStartTransform = context_.getStartTransform();
- context_.setStartTransform(child.getAttribute('transform'));
-
- var childTlist = svgedit.transformlist.getTransformList(child);
-
- if (childTlist) {
- var newxlate = svgroot.createSVGTransform();
- newxlate.setTranslate(tx, ty);
- if (childTlist.numberOfItems) {
- childTlist.insertItemBefore(newxlate, 0);
- } else {
- childTlist.appendItem(newxlate);
- }
- batchCmd.addSubCommand(svgedit.recalculate.recalculateDimensions(child));
-
-
-
- var uses = selected.getElementsByTagNameNS(NS.SVG, 'use');
- var href = '#' + child.id;
- var u = uses.length;
- while (u--) {
- var useElem = uses.item(u);
- if (href == svgedit.utilities.getHref(useElem)) {
- var usexlate = svgroot.createSVGTransform();
- usexlate.setTranslate(-tx,-ty);
- svgedit.transformlist.getTransformList(useElem).insertItemBefore(usexlate, 0);
- batchCmd.addSubCommand( svgedit.recalculate.recalculateDimensions(useElem) );
- }
- }
- context_.setStartTransform(oldStartTransform);
- }
- }
- }
-
- clipPaths_done = [];
- context_.setStartTransform(oldStartTransform);
- }
- }
-
-
- else if (N == 1 && tlist.getItem(0).type == 1 && !gangle) {
- operation = 1;
- var m = tlist.getItem(0).matrix,
- children = selected.childNodes,
- c = children.length;
- while (c--) {
- var child = children.item(c);
- if (child.nodeType == 1) {
- var oldStartTransform = context_.getStartTransform();
- context_.setStartTransform(child.getAttribute('transform'));
- var childTlist = svgedit.transformlist.getTransformList(child);
-
- if (!childTlist) {continue;}
-
- var em = svgedit.math.matrixMultiply(m, svgedit.math.transformListToTransform(childTlist).matrix);
- var e2m = svgroot.createSVGTransform();
- e2m.setMatrix(em);
- childTlist.clear();
- childTlist.appendItem(e2m, 0);
-
- batchCmd.addSubCommand( svgedit.recalculate.recalculateDimensions(child) );
- context_.setStartTransform(oldStartTransform);
-
-
-
- var sw = child.getAttribute('stroke-width');
- if (child.getAttribute('stroke') !== 'none' && !isNaN(sw)) {
- var avg = (Math.abs(em.a) + Math.abs(em.d)) / 2;
- child.setAttribute('stroke-width', sw * avg);
- }
- }
- }
- tlist.clear();
- }
-
- else {
- if (gangle) {
- var newRot = svgroot.createSVGTransform();
- newRot.setRotate(gangle, newcenter.x, newcenter.y);
- if (tlist.numberOfItems) {
- tlist.insertItemBefore(newRot, 0);
- } else {
- tlist.appendItem(newRot);
- }
- }
- if (tlist.numberOfItems == 0) {
- selected.removeAttribute('transform');
- }
- return null;
- }
-
-
- if (operation == 2) {
- if (gangle) {
- newcenter = {
- x: oldcenter.x + first_m.e,
- y: oldcenter.y + first_m.f
- };
-
- var newRot = svgroot.createSVGTransform();
- newRot.setRotate(gangle, newcenter.x, newcenter.y);
- if (tlist.numberOfItems) {
- tlist.insertItemBefore(newRot, 0);
- } else {
- tlist.appendItem(newRot);
- }
- }
- }
-
- else if (operation == 3) {
- var m = svgedit.math.transformListToTransform(tlist).matrix;
- var roldt = svgroot.createSVGTransform();
- roldt.setRotate(gangle, oldcenter.x, oldcenter.y);
- var rold = roldt.matrix;
- var rnew = svgroot.createSVGTransform();
- rnew.setRotate(gangle, newcenter.x, newcenter.y);
- var rnew_inv = rnew.matrix.inverse(),
- m_inv = m.inverse(),
- extrat = svgedit.math.matrixMultiply(m_inv, rnew_inv, rold, m);
- tx = extrat.e;
- ty = extrat.f;
- if (tx != 0 || ty != 0) {
-
-
- var children = selected.childNodes;
- var c = children.length;
- while (c--) {
- var child = children.item(c);
- if (child.nodeType == 1) {
- var oldStartTransform = context_.getStartTransform();
- context_.setStartTransform(child.getAttribute('transform'));
- var childTlist = svgedit.transformlist.getTransformList(child);
- var newxlate = svgroot.createSVGTransform();
- newxlate.setTranslate(tx, ty);
- if (childTlist.numberOfItems) {
- childTlist.insertItemBefore(newxlate, 0);
- } else {
- childTlist.appendItem(newxlate);
- }
- batchCmd.addSubCommand( svgedit.recalculate.recalculateDimensions(child) );
- context_.setStartTransform(oldStartTransform);
- }
- }
- }
-
- if (gangle) {
- if (tlist.numberOfItems) {
- tlist.insertItemBefore(rnew, 0);
- } else {
- tlist.appendItem(rnew);
- }
- }
- }
- }
-
- else {
-
- var box = svgedit.utilities.getBBox(selected);
-
-
-
-
-
- if (!box && selected.tagName != 'path') return null;
-
- var m = svgroot.createSVGMatrix(),
-
- angle = svgedit.utilities.getRotationAngle(selected);
- if (angle) {
- var oldcenter = {x: box.x+box.width/2, y: box.y+box.height/2},
- newcenter = svgedit.math.transformPoint(box.x+box.width/2, box.y+box.height/2,
- svgedit.math.transformListToTransform(tlist).matrix);
-
- var a = angle * Math.PI / 180;
- if ( Math.abs(a) > (1.0e-10) ) {
- var s = Math.sin(a)/(1 - Math.cos(a));
- } else {
-
- var s = 2/a;
- }
- for (var i = 0; i < tlist.numberOfItems; ++i) {
- var xform = tlist.getItem(i);
- if (xform.type == 4) {
-
- var rm = xform.matrix;
- oldcenter.y = (s*rm.e + rm.f)/2;
- oldcenter.x = (rm.e - s*rm.f)/2;
- tlist.removeItem(i);
- break;
- }
- }
- }
-
-
- var operation = 0;
- var N = tlist.numberOfItems;
-
-
-
-
- if (!svgedit.browser.isWebkit()) {
- var fill = selected.getAttribute('fill');
- if (fill && fill.indexOf('url(') === 0) {
- var paint = getRefElem(fill);
- var type = 'pattern';
- if (paint.tagName !== type) type = 'gradient';
- var attrVal = paint.getAttribute(type + 'Units');
- if (attrVal === 'userSpaceOnUse') {
-
- m = svgedit.math.transformListToTransform(tlist).matrix;
- var gtlist = svgedit.transformlist.getTransformList(paint);
- var gmatrix = svgedit.math.transformListToTransform(gtlist).matrix;
- m = svgedit.math.matrixMultiply(m, gmatrix);
- var m_str = 'matrix(' + [m.a, m.b, m.c, m.d, m.e, m.f].join(',') + ')';
- paint.setAttribute(type + 'Transform', m_str);
- }
- }
- }
-
-
-
-
- if (N >= 3 && tlist.getItem(N-2).type == 3 &&
- tlist.getItem(N-3).type == 2 && tlist.getItem(N-1).type == 2)
-
-
-
-
- {
- operation = 3;
- m = svgedit.math.transformListToTransform(tlist, N-3, N-1).matrix;
- tlist.removeItem(N-1);
- tlist.removeItem(N-2);
- tlist.removeItem(N-3);
- }
-
- else if (N == 4 && tlist.getItem(N-1).type == 1) {
- operation = 3;
- m = svgedit.math.transformListToTransform(tlist).matrix;
- var e2t = svgroot.createSVGTransform();
- e2t.setMatrix(m);
- tlist.clear();
- tlist.appendItem(e2t);
-
- m = svgroot.createSVGMatrix();
- }
-
-
-
- else if ( (N == 1 || (N > 1 && tlist.getItem(1).type != 3)) &&
- tlist.getItem(0).type == 2)
- {
- operation = 2;
- var oldxlate = tlist.getItem(0).matrix,
- meq = svgedit.math.transformListToTransform(tlist,1).matrix,
- meq_inv = meq.inverse();
- m = svgedit.math.matrixMultiply( meq_inv, oldxlate, meq );
- tlist.removeItem(0);
- }
-
-
- else if (N == 1 && tlist.getItem(0).type == 1 && !angle) {
-
- m = svgedit.math.transformListToTransform(tlist).matrix;
- switch (selected.tagName) {
- case 'line':
- changes = $(selected).attr(['x1', 'y1', 'x2', 'y2']);
- case 'polyline':
- case 'polygon':
- changes.points = selected.getAttribute('points');
- if (changes.points) {
- var list = selected.points;
- var len = list.numberOfItems;
- changes.points = new Array(len);
- for (var i = 0; i < len; ++i) {
- var pt = list.getItem(i);
- changes.points[i] = {x:pt.x, y:pt.y};
- }
- }
- case 'path':
- changes.d = selected.getAttribute('d');
- operation = 1;
- tlist.clear();
- break;
- default:
- break;
- }
- }
-
-
- else {
- operation = 4;
- if (angle) {
- var newRot = svgroot.createSVGTransform();
- newRot.setRotate(angle, newcenter.x, newcenter.y);
-
- if (tlist.numberOfItems) {
- tlist.insertItemBefore(newRot, 0);
- } else {
- tlist.appendItem(newRot);
- }
- }
- if (tlist.numberOfItems == 0) {
- selected.removeAttribute('transform');
- }
- return null;
- }
-
-
- if (operation == 1 || operation == 2 || operation == 3) {
- svgedit.coords.remapElement(selected, changes, m);
- }
-
-
- if (operation == 2) {
- if (angle) {
- if (!svgedit.math.hasMatrixTransform(tlist)) {
- newcenter = {
- x: oldcenter.x + m.e,
- y: oldcenter.y + m.f
- };
- }
- var newRot = svgroot.createSVGTransform();
- newRot.setRotate(angle, newcenter.x, newcenter.y);
- if (tlist.numberOfItems) {
- tlist.insertItemBefore(newRot, 0);
- } else {
- tlist.appendItem(newRot);
- }
- }
-
-
-
- if (selected.tagName == 'text') {
- var children = selected.childNodes;
- var c = children.length;
- while (c--) {
- var child = children.item(c);
- if (child.tagName == 'tspan') {
- var tspanChanges = {
- x: $(child).attr('x') || 0,
- y: $(child).attr('y') || 0
- };
- svgedit.coords.remapElement(child, tspanChanges, m);
- }
- }
- }
- }
-
-
-
-
- else if (operation == 3 && angle) {
- var m = svgedit.math.transformListToTransform(tlist).matrix;
- var roldt = svgroot.createSVGTransform();
- roldt.setRotate(angle, oldcenter.x, oldcenter.y);
- var rold = roldt.matrix;
- var rnew = svgroot.createSVGTransform();
- rnew.setRotate(angle, newcenter.x, newcenter.y);
- var rnew_inv = rnew.matrix.inverse();
- var m_inv = m.inverse();
- var extrat = svgedit.math.matrixMultiply(m_inv, rnew_inv, rold, m);
-
- svgedit.coords.remapElement(selected, changes, extrat);
- if (angle) {
- if (tlist.numberOfItems) {
- tlist.insertItemBefore(rnew, 0);
- } else {
- tlist.appendItem(rnew);
- }
- }
- }
- }
-
- if (tlist.numberOfItems == 0) {
- selected.removeAttribute('transform');
- }
- batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(selected, initial));
- return batchCmd;
- };
- })();
|