Move shortFloat() into svgedit.units. Add test file for svgedit.units.

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1966 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2011-02-02 17:28:43 +00:00
parent fe537b05a8
commit 99931f7d5b
7 changed files with 121 additions and 65 deletions

View File

@ -36,13 +36,23 @@ var removedElements = {};
* An interface that all command objects must implement.
*
* interface svgedit.history.HistoryCommand {
* void apply();
* void unapply();
* void apply(svgedit.history.HistoryEventHandler);
* void unapply(svgedit.history.HistoryEventHandler);
* Element[] elements();
* String getText();
*
* static String type();
* }
*
* Interface: svgedit.history.HistoryEventHandler
* An interface for objects that will handle history events.
*
* interface svgedit.history.HistoryEventHandler {
* void handleHistoryEvent(eventType, command);
* }
*
* eventType is a string conforming to one of the HistoryEvent types.
* command is an object fulfilling the HistoryCommand interface.
*/
// Class: svgedit.history.MoveElementCommand
@ -443,18 +453,6 @@ svgedit.history.BatchCommand.prototype.isEmpty = function() {
};
/**
* Interface: svgedit.history.HistoryEventHandler
* An interface for objects that will handle history events.
*
* interface svgedit.history.HistoryEventHandler {
* void handleHistoryEvent(eventType, command);
* }
*
* eventType is a string conforming to one of the HistoryEvent types (see above).
* command is an object fulfilling the HistoryCommand interface (see above).
*/
// Class: svgedit.history.UndoManager
// Parameters:
// historyEventHandler - an object that conforms to the HistoryEventHandler interface

View File

@ -1535,8 +1535,8 @@
}
if(unit) {
x = svgCanvas.convertUnit(x);
y = svgCanvas.convertUnit(y);
x = svgedit.units.convertUnit(x);
y = svgedit.units.convertUnit(y);
}
$('#selected_x').val(x || 0);
@ -1560,8 +1560,8 @@
if(point) {
var seg_type = $('#seg_type');
if(unit) {
point.x = svgCanvas.convertUnit(point.x);
point.y = svgCanvas.convertUnit(point.y);
point.x = svgedit.units.convertUnit(point.x);
point.y = svgedit.units.convertUnit(point.y);
}
$('#path_node_x').val(point.x);
$('#path_node_y').val(point.y);
@ -1622,7 +1622,7 @@
var attrVal = elem.getAttribute(item);
if(curConfig.baseUnit !== 'px' && elem[item]) {
var bv = elem[item].baseVal.value;
attrVal = svgCanvas.convertUnit(bv);
attrVal = svgedit.units.convertUnit(bv);
}
$('#' + el_name + '_' + item).val(attrVal || 0);
@ -2734,8 +2734,8 @@
// update resolution option with actual resolution
var res = svgCanvas.getResolution();
if(curConfig.baseUnit !== "px") {
res.w = svgCanvas.convertUnit(res.w) + curConfig.baseUnit;
res.h = svgCanvas.convertUnit(res.h) + curConfig.baseUnit;
res.w = svgedit.units.convertUnit(res.w) + curConfig.baseUnit;
res.h = svgedit.units.convertUnit(res.h) + curConfig.baseUnit;
}
$('#canvas_width').val(res.w);

View File

@ -385,7 +385,8 @@ svgedit.units.init({
getBaseUnit: function() { return curConfig.baseUnit; },
getElement: getElem,
getHeight: function() { return svgcontent.getAttribute("height")/current_zoom; },
getWidth: function() { return svgcontent.getAttribute("width")/current_zoom; }
getWidth: function() { return svgcontent.getAttribute("width")/current_zoom; },
getRoundDigits: function() { return save_options.round_digits; }
});
// import from units.js
var convertToNum = canvas.convertToNum = svgedit.units.convertToNum;
@ -425,7 +426,7 @@ canvas.undoMgr = new svgedit.history.UndoManager({
call("changed", elems);
var cmdType = cmd.type();
var isApply = eventType == EventTypes.AFTER_APPLY;
var isApply = (eventType == EventTypes.AFTER_APPLY);
if (cmdType == MoveElementCommand.type()) {
var parent = isApply ? cmd.newParent : cmd.oldParent;
if (parent == svgcontent) {
@ -635,32 +636,6 @@ this.addExtension = function(name, ext_func) {
}
};
// Function: shortFloat
// Rounds a given value to a float with number of digits defined in save_options
//
// Parameters:
// val - The value as a String, Number or Array of two numbers to be rounded
//
// Returns:
// If a string/number was given, returns a Float. If an array, return a string
// with comma-seperated floats
var shortFloat = function(val) {
var digits = save_options.round_digits;
if(!isNaN(val)) {
// Note that + converts to Number
return +((+val).toFixed(digits));
} else if($.isArray(val)) {
return shortFloat(val[0]) + ',' + shortFloat(val[1]);
} else {
return parseFloat(val).toFixed(digits) - 0;
}
}
this.convertUnit = function(val, unit) {
return shortFloat(svgedit.units.convertUnit(val, unit));
};
// This method rounds the incoming value to the nearest value based on the current_zoom
var round = this.round = function(val) {
return parseInt(val*current_zoom)/current_zoom;
@ -5826,9 +5801,9 @@ var pathActions = canvas.pathActions = function() {
var addToD = function(pnts, more, last) {
var str = '';
var more = more?' '+more.join(' '):'';
var last = last?' '+shortFloat(last):'';
var last = last?' '+svgedit.units.shortFloat(last):'';
$.each(pnts, function(i, pnt) {
pnts[i] = shortFloat(pnt);
pnts[i] = svgedit.units.shortFloat(pnt);
});
d += letter + pnts.join(' ') + more + last;
}
@ -6113,16 +6088,16 @@ this.svgToString = function(elem, indent) {
// if(curConfig.baseUnit !== "px") {
// var unit = curConfig.baseUnit;
// var unit_m = svgedit.units.getTypeMap()[unit];
// res.w = shortFloat(res.w / unit_m)
// res.h = shortFloat(res.h / unit_m)
// res.w = svgedit.units.shortFloat(res.w / unit_m)
// res.h = svgedit.units.shortFloat(res.h / unit_m)
// vb = ' viewBox="' + [0, 0, res.w, res.h].join(' ') + '"';
// res.w += unit;
// res.h += unit;
// }
if(unit !== "px") {
res.w = this.convertUnit(res.w, unit) + unit;
res.h = this.convertUnit(res.h, unit) + unit;
res.w = svgedit.units.convertUnit(res.w, unit) + unit;
res.h = svgedit.units.convertUnit(res.h, unit) + unit;
}
out.push(' width="' + res.w + '" height="' + res.h + '"' + vb + ' xmlns="'+svgns+'"');
@ -6174,9 +6149,9 @@ this.svgToString = function(elem, indent) {
out.push(" ");
if(attr.localName === 'd') attrVal = pathActions.convertPath(elem, true);
if(!isNaN(attrVal)) {
attrVal = shortFloat(attrVal);
attrVal = svgedit.units.shortFloat(attrVal);
} else if(unit_re.test(attrVal)) {
attrVal = shortFloat(attrVal) + unit;
attrVal = svgedit.units.shortFloat(attrVal) + unit;
}
// Embed images when saving
@ -9657,7 +9632,6 @@ this.getPrivateMethods = function() {
round: round,
runExtensions: runExtensions,
sanitizeSvg: sanitizeSvg,
shortFloat: shortFloat,
SVGEditTransformList: svgedit.transformlist.SVGTransformList,
toString: toString,
transformBox: svgedit.math.transformBox,

View File

@ -186,7 +186,7 @@ svgedit.utilities.convertToXMLReferences = function(input) {
return output;
};
// Function: text2xml
// Function: svgedit.utilities.text2xml
// Cross-browser compatible method of converting a string to an XML tree
// found this function here: http://groups.google.com/group/jquery-dev/browse_thread/thread/c6d11387c580a77f
svgedit.utilities.text2xml = function(sXML) {
@ -209,7 +209,7 @@ svgedit.utilities.text2xml = function(sXML) {
return out;
};
// Function: bboxToObj
// Function: svgedit.utilities.bboxToObj
// Converts a SVGRect into an object.
//
// Parameters:
@ -226,7 +226,7 @@ svgedit.utilities.bboxToObj = function(bbox) {
}
};
// Function: walkTree
// Function: svgedit.utilities.walkTree
// Walks the tree and executes the callback on each element in a top-down fashion
//
// Parameters:
@ -242,7 +242,7 @@ svgedit.utilities.walkTree = function(elem, cbFn){
}
};
// Function: walkTreePost
// Function: svgedit.utilities.walkTreePost
// Walks the tree and executes the callback on each element in a depth-first fashion
// TODO: FIXME: Shouldn't this be calling walkTreePost?
//

View File

@ -51,6 +51,7 @@ var typeMap_ = {px: 1};
* function getElement() - returns an element in the container given an id
* function getHeight() - returns the container's height
* function getWidth() - returns the container's width
* function getRoundDigits() - returns the number of digits number should be rounded to
*/
/**
@ -95,6 +96,26 @@ svgedit.units.getTypeMap = function() {
return typeMap_;
};
// Function: svgedit.units.shortFloat
// Rounds a given value to a float with number of digits defined in save_options
//
// Parameters:
// val - The value as a String, Number or Array of two numbers to be rounded
//
// Returns:
// If a string/number was given, returns a Float. If an array, return a string
// with comma-seperated floats
svgedit.units.shortFloat = function(val) {
var digits = elementContainer_.getRoundDigits();
if(!isNaN(val)) {
// Note that + converts to Number
return +((+val).toFixed(digits));
} else if($.isArray(val)) {
return svgedit.units.shortFloat(val[0]) + ',' + svgedit.units.shortFloat(val[1]);
}
return parseFloat(val).toFixed(digits) - 0;
};
// Function: svgedit.units.convertUnit
// Converts the number to given unit or baseUnit
svgedit.units.convertUnit = function(val, unit) {
@ -102,7 +123,7 @@ svgedit.units.convertUnit = function(val, unit) {
// baseVal.convertToSpecifiedUnits(unitNumMap[unit]);
// var val = baseVal.valueInSpecifiedUnits;
// baseVal.convertToSpecifiedUnits(1);
return val / typeMap_[unit];
return svgedit.unit.shortFloat(val / typeMap_[unit]);
};
// Function: svgedit.units.setUnitAttr
@ -249,7 +270,9 @@ svgedit.units.isValidUnit = function(attr, val) {
result = (elem == null);
} catch(e) {}
return result;
} else valid = true;
} else {
valid = true;
}
return valid;
};

View File

@ -12,6 +12,7 @@
<iframe src='history_test.html' width='100%' height='300'></iframe>
<iframe src='select_test.html' width='100%' height='300'></iframe>
<iframe src='draw_test.html' width='100%' height='300'></iframe>
<iframe src='units_test.html' width='100%' height='300'></iframe>
</body>
<script>
window.setTimeout(function() {
@ -20,6 +21,6 @@ window.setTimeout(function() {
var f = iframes[i];
f.style.height = (f.contentDocument.body.scrollHeight + 20) + 'px';
}
}, 1200);
}, 5000);
</script>
</html>

60
test/units_test.html Normal file
View File

@ -0,0 +1,60 @@
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='qunit/qunit.css' type='text/css'/>
<script type='text/javascript' src='../editor/jquery.js'></script>
<script type='text/javascript' src='../editor/units.js'></script>
<script type='text/javascript' src='qunit/qunit.js'></script>
<script type='text/javascript'>
$(function() {
// log function
QUnit.log = function(result, message) {
if (window.console && window.console.log) {
window.console.log(result +' :: '+ message);
}
};
function setUp() {
svgedit.units.init({
getBaseUnit: function() { return "cm"; },
getElement: function() { return null; },
getHeight: function() { return 600; },
getWidth: function() { return 800; },
getRoundDigits: function() { return 4; }
});
}
test('Test svgedit.units package', function() {
expect(2);
ok(svgedit.units);
equals(typeof svgedit.units, typeof {});
});
test('Test svgedit.units.shortFloat()', function() {
expect(7);
setUp();
ok(svgedit.units.shortFloat);
equals(typeof svgedit.units.shortFloat, typeof function(){});
var shortFloat = svgedit.units.shortFloat;
equals(shortFloat(0.00000001), 0);
equals(shortFloat(1), 1);
equals(shortFloat(3.45678), 3.4568);
equals(shortFloat(1.23443), 1.2344);
equals(shortFloat(1.23455), 1.2346);
});
});
</script>
</head>
<body>
<h1 id='qunit-header'>Unit Tests for units.js</h1>
<h2 id='qunit-banner'></h2>
<h2 id='qunit-userAgent'></h2>
<ol id='qunit-tests'>
</ol>
<div id='anchor' style='visibility:hidden'>
</div>
</body>
</html>