From 2e39f07884d7574fdc4c2b28701c46eeb0565b2f Mon Sep 17 00:00:00 2001 From: Alexis Deveria Date: Tue, 22 Sep 2009 14:35:05 +0000 Subject: [PATCH] Fixed issue 166: Fit to content now crops correctly and deals with stroke-width. Also fixed zoom bug #2 on issue 208 git-svn-id: http://svg-edit.googlecode.com/svn/trunk@676 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/svgcanvas.js | 72 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 1132e573..385ef8d0 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -2872,16 +2872,26 @@ function BatchCommand(text) { var res = canvas.getResolution(); var w = res.w, h = res.h; - if(!x) { canvas.clearSelection(); // Get bounding box - var bbox = svgzoom.getBBox(); + var bbox = canvas.getStrokedBBox(); if(bbox) { - x = bbox.x + bbox.width; - y = bbox.y + bbox.height; + canvas.addToSelection(canvas.getVisibleElements()); + $.each(selectedElements, function(i, item) { + var sel_bb = item.getBBox(); + recalculateDimensions(item, { + x: sel_bb.x - bbox.x, + y: sel_bb.y - bbox.y, + width: sel_bb.width, + height: sel_bb.height + }); + }); + canvas.clearSelection(); + x = bbox.width; + y = bbox.height; } else { alert('No content to fit to'); return; @@ -2926,17 +2936,8 @@ function BatchCommand(text) { switch (val) { case 'selection': if(!selectedElements[0]) return; - var bb = selectedBBoxes[0]; - $.each(selectedBBoxes, function(i, sel) { - if(!sel || !i) return; - - // FIXME: Calculations still need some work... - bb.x = Math.min(bb.x, sel.x); - bb.y = Math.min(bb.y, sel.y); - bb.width = Math.max(sel.width + sel.x, bb.width + bb.x) - bb.x; - bb.height = Math.max(sel.height + sel.y, bb.height + bb.y) - bb.y; - }); - + var sel_elems = $.map(selectedElements, function(n){ if(n) return n; }); + var bb = canvas.getStrokedBBox(sel_elems); w_zoom = Math.round((editor_w / bb.width)*100 * spacer)/100; h_zoom = Math.round((editor_h / bb.height)*100 * spacer)/100; var zoomlevel = Math.min(w_zoom,h_zoom); @@ -2953,7 +2954,7 @@ function BatchCommand(text) { return {'zoom': zoomlevel, 'bbox': {width:res.w, height:res.h ,x:0, y:0}}; case 'content': - var bb = svgzoom.getBBox(); + var bb = canvas.getStrokedBBox(); w_zoom = Math.round((editor_w / bb.width)*100 * spacer)/100; h_zoom = Math.round((editor_h / bb.height)*100 * spacer)/100; var zoomlevel = Math.min(w_zoom,h_zoom); @@ -3663,6 +3664,45 @@ function BatchCommand(text) { } }; + this.getStrokedBBox = function(elems) { + // TODO: Get correct BBoxes for rotated elements + if(!elems) elems = canvas.getVisibleElements(); + var full_bb = elems[0].getBBox(); + var max_x = full_bb.x + full_bb.width; + var max_y = full_bb.y + full_bb.height; + var min_x = full_bb.x; + var min_y = full_bb.y; + var getOffset = function(elem) { + var sw = round(elem.getAttribute("stroke-width")); + var offset = 0; + if (elem.getAttribute("stroke") != "none" && !isNaN(sw)) { + offset += sw/2; + } + return offset; + } + + $.each(elems, function(i, elem) { + var cur_bb = elem.getBBox(); + var offset = getOffset(elem); + min_x = Math.min(min_x, cur_bb.x - offset); + min_y = Math.min(min_y, cur_bb.y - offset); + }); + + full_bb.x = min_x; + full_bb.y = min_y; + + $.each(elems, function(i, elem) { + var cur_bb = elem.getBBox(); + var offset = getOffset(elem); + max_x = Math.max(max_x, cur_bb.x + cur_bb.width + offset); + max_y = Math.max(max_y, cur_bb.y + cur_bb.height + offset); + }); + + full_bb.width = max_x - min_x; + full_bb.height = max_y - min_y; + return full_bb; + } + this.getVisibleElements = function(includeBBox) { var nodes = current_layer.childNodes; var i = nodes.length;