Skip to content

Commit fbe8a55

Browse files
committed
Refactor Format literal to Formatter class that keeps precision stored.
1 parent 8da8f11 commit fbe8a55

13 files changed

Lines changed: 116 additions & 107 deletions

File tree

src/basic/Matrix.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ var Matrix = this.Matrix = Base.extend(/** @lends Matrix# */{
120120
* @return {String} A string representation of this transform.
121121
*/
122122
toString: function() {
123-
var format = Format.number;
124-
return '[[' + [format(this._a), format(this._b),
125-
format(this._tx)].join(', ') + '], ['
126-
+ [format(this._c), format(this._d),
127-
format(this._ty)].join(', ') + ']]';
123+
var f = Formatter.instance;
124+
return '[[' + [f.number(this._a), f.number(this._b),
125+
f.number(this._tx)].join(', ') + '], ['
126+
+ [f.number(this._c), f.number(this._d),
127+
f.number(this._ty)].join(', ') + ']]';
128128
},
129129

130130
/**

src/basic/Point.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,16 @@ var Point = this.Point = Base.extend(/** @lends Point# */{
224224
* @return {String} A string representation of the point.
225225
*/
226226
toString: function() {
227-
var format = Format.number;
228-
return '{ x: ' + format(this.x) + ', y: ' + format(this.y) + ' }';
227+
var f = Formatter.instance;
228+
return '{ x: ' + f.number(this.x) + ', y: ' + f.number(this.y) + ' }';
229229
},
230230

231231
_serialize: function(options) {
232-
var format = Format.number,
233-
precision = options.precision;
234-
// For speed reasons, we directly call Format.number() here with
235-
// precision, instead of converting array through Base.serialize() which
236-
// makes a copy.
237-
return [format(this.x, precision),
238-
format(this.y, precision)];
232+
var f = options.formatter;
233+
// For speed reasons, we directly call formatter.number() here, instead
234+
// of converting array through Base.serialize() which makes a copy.
235+
return [f.number(this.x),
236+
f.number(this.y)];
239237
},
240238

241239
/**

src/basic/Rectangle.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,21 @@ var Rectangle = this.Rectangle = Base.extend(/** @lends Rectangle# */{
210210
* @return {String} A string representation of this rectangle.
211211
*/
212212
toString: function() {
213-
var format = Format.number;
214-
return '{ x: ' + format(this.x)
215-
+ ', y: ' + format(this.y)
216-
+ ', width: ' + format(this.width)
217-
+ ', height: ' + format(this.height)
213+
var f = Formatter.instance;
214+
return '{ x: ' + f.number(this.x)
215+
+ ', y: ' + f.number(this.y)
216+
+ ', width: ' + f.number(this.width)
217+
+ ', height: ' + f.number(this.height)
218218
+ ' }';
219219
},
220220

221221
_serialize: function(options) {
222-
var format = Format.number,
223-
precision = options.precision;
222+
var f = options.formatter;
224223
// See Point#_serialize()
225-
return [format(this.x, precision),
226-
format(this.y, precision),
227-
format(this.width, precision),
228-
format(this.height, precision)];
224+
return [f.number(this.x),
225+
f.number(this.y),
226+
f.number(this.width),
227+
f.number(this.height)];
229228
},
230229

231230
/**
@@ -846,6 +845,8 @@ var LinkedRectangle = Rectangle.extend({
846845
};
847846
}, /** @lends Rectangle# */{
848847
/**
848+
* {@grouptitle Item Bounds}
849+
*
849850
* Specifies whether an item's bounds are selected and will also
850851
* mark the item as selected.
851852
*

src/basic/Size.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,16 @@ var Size = this.Size = Base.extend(/** @lends Size# */{
171171
* @return {String} A string representation of the size.
172172
*/
173173
toString: function() {
174-
var format = Format.number;
175-
return '{ width: ' + format(this.width)
176-
+ ', height: ' + format(this.height) + ' }';
174+
var f = Formatter.instance;
175+
return '{ width: ' + f.number(this.width)
176+
+ ', height: ' + f.number(this.height) + ' }';
177177
},
178178

179179
_serialize: function(options) {
180-
var format = Format.number,
181-
precision = options.precision;
180+
var f = options.formatter;
182181
// See Point#_serialize()
183-
return [format(this.width, precision),
184-
format(this.height, precision)];
182+
return [f.number(this.width),
183+
f.number(this.height)];
185184
},
186185

187186
/**

src/core/Base.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ this.Base = Base.inject(/** @lends Base# */{
4444
if (!/^_/.test(key)) {
4545
var type = typeof value;
4646
this.push(key + ': ' + (type === 'number'
47-
? Format.number(value)
47+
? Formatter.instance.number(value)
4848
: type === 'string' ? "'" + value + "'" : value));
4949
}
5050
}, []).join(', ') + ' }';
@@ -270,9 +270,11 @@ this.Base = Base.inject(/** @lends Base# */{
270270
*/
271271
serialize: function(obj, options, compact, dictionary) {
272272
options = options || {};
273+
273274
var root = !dictionary,
274275
res;
275276
if (root) {
277+
options.formatter = new Formatter(options.precision);
276278
// Create a simple dictionary object that handles all the
277279
// storing and retrieving of dictionary definitions and
278280
// references, e.g. for symbols and gradients. Items that want
@@ -325,7 +327,7 @@ this.Base = Base.inject(/** @lends Base# */{
325327
res[i] = Base.serialize(obj[i], options, compact,
326328
dictionary);
327329
} else if (typeof obj === 'number') {
328-
res = Format.number(obj, options.precision);
330+
res = options.formatter.number(obj, options.precision);
329331
} else {
330332
res = obj;
331333
}

src/paper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ var paper = new function() {
107107
/*#*/ include('tool/Tool.js');
108108
/*#*/ } // options.browser
109109

110-
/*#*/ include('util/Format.js');
110+
/*#*/ include('util/Formatter.js');
111111
/*#*/ include('util/CanvasProvider.js');
112112
/*#*/ include('util/Numerical.js');
113113
/*#*/ include('util/BlendMode.js');

src/path/CurveLocation.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,18 @@ var CurveLocation = this.CurveLocation = Base.extend(/** @lends CurveLocation# *
223223
*/
224224
toString: function() {
225225
var parts = [],
226-
point = this.getPoint();
226+
point = this.getPoint(),
227+
f = Formatter.instance;
227228
if (point)
228229
parts.push('point: ' + point);
229230
var index = this.getIndex();
230231
if (index != null)
231232
parts.push('index: ' + index);
232233
var parameter = this.getParameter();
233234
if (parameter != null)
234-
parts.push('parameter: ' + Format.number(parameter));
235+
parts.push('parameter: ' + f.number(parameter));
235236
if (this._distance != null)
236-
parts.push('distance: ' + Format.number(this._distance));
237+
parts.push('distance: ' + f.number(this._distance));
237238
return '{ ' + parts.join(', ') + ' }';
238239
}
239240
});

src/path/Path.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
206206
getPathData: function(/* precision */) {
207207
var segments = this._segments,
208208
style = this._style,
209-
format = Format.point,
210209
precision = arguments[0],
210+
f = Formatter.instance,
211211
parts = [];
212212

213213
// TODO: Add support for H/V and/or relative commands, where appropriate
@@ -220,21 +220,21 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
220220
if (handle1.isZero() && handle2.isZero()) {
221221
if (!skipLine) {
222222
// L = absolute lineto: moving to a point with drawing
223-
parts.push('L' + format(point2, precision));
223+
parts.push('L' + f.point(point2, precision));
224224
}
225225
} else {
226226
// c = relative curveto: handle1, handle2 + end - start,
227227
// end - start
228228
var end = point2.subtract(point1);
229-
parts.push('c' + format(handle1, precision)
230-
+ ' ' + format(end.add(handle2), precision)
231-
+ ' ' + format(end, precision));
229+
parts.push('c' + f.point(handle1, precision)
230+
+ ' ' + f.point(end.add(handle2), precision)
231+
+ ' ' + f.point(end, precision));
232232
}
233233
}
234234

235235
if (segments.length === 0)
236236
return '';
237-
parts.push('M' + format(segments[0]._point));
237+
parts.push('M' + f.point(segments[0]._point));
238238
for (i = 0, l = segments.length - 1; i < l; i++)
239239
addCurve(segments[i], segments[i + 1], false);
240240
// We only need to draw the connecting curve if it is not a line, and if

src/style/Color.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -688,15 +688,15 @@ var Color = this.Color = Base.extend(new function() {
688688
var properties = types[this._type],
689689
parts = [],
690690
isGradient = this._type === 'gradient',
691-
format = Format.number;
691+
f = Formatter.instance;
692692
for (var i = 0, l = properties.length; i < l; i++) {
693693
var value = this._components[i];
694694
if (value != null)
695695
parts.push(properties[i] + ': '
696-
+ (isGradient ? value : format(value)));
696+
+ (isGradient ? value : f.number(value)));
697697
}
698698
if (this._alpha != null)
699-
parts.push('alpha: ' + format(this._alpha));
699+
parts.push('alpha: ' + f.number(this._alpha));
700700
return '{ ' + parts.join(', ') + ' }';
701701
},
702702

src/svg/SvgExport.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
* Paper.js DOM to a SVG DOM.
1616
*/
1717
new function() {
18-
// Shortcut to Format.number
19-
var format = Format.number,
18+
var formatter = Formatter.instance,
2019
namespaces = {
2120
href: 'http://www.w3.org/1999/xlink'
2221
};
@@ -26,7 +25,7 @@ new function() {
2625
var val = attrs[key],
2726
namespace = namespaces[key];
2827
if (typeof val === 'number')
29-
val = format(val);
28+
val = formatter.number(val);
3029
if (namespace) {
3130
node.setAttributeNS(namespace, key, val);
3231
} else {
@@ -72,11 +71,11 @@ new function() {
7271
angle = decomposed.rotation,
7372
scale = decomposed.scaling;
7473
if (trans && !trans.isZero())
75-
parts.push('translate(' + Format.point(trans) + ')');
74+
parts.push('translate(' + formatter.point(trans) + ')');
7675
if (!Numerical.isZero(scale.x - 1) || !Numerical.isZero(scale.y - 1))
77-
parts.push('scale(' + Format.point(scale) +')');
76+
parts.push('scale(' + formatter.point(scale) +')');
7877
if (angle)
79-
parts.push('rotate(' + format(angle) + ')');
78+
parts.push('rotate(' + formatter.number(angle) + ')');
8079
attrs.transform = parts.join(' ');
8180
} else {
8281
attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')';
@@ -223,7 +222,7 @@ new function() {
223222
case 'polygon':
224223
var parts = [];
225224
for(i = 0, l = segments.length; i < l; i++)
226-
parts.push(Format.point(segments[i]._point));
225+
parts.push(formatter.point(segments[i]._point));
227226
attrs = {
228227
points: parts.join(' ')
229228
};
@@ -296,8 +295,8 @@ new function() {
296295
break;
297296
}
298297
if (angle) {
299-
attrs.transform = 'rotate(' + format(angle) + ','
300-
+ Format.point(center) + ')';
298+
attrs.transform = 'rotate(' + formatter.number(angle) + ','
299+
+ formatter.point(center) + ')';
301300
// Tell applyStyle() that to transform the gradient the other way
302301
item._gradientMatrix = new Matrix().rotate(-angle, center);
303302
}
@@ -320,16 +319,16 @@ new function() {
320319
bounds = definition.getBounds();
321320
if (!symbolNode) {
322321
symbolNode = createElement('symbol', {
323-
viewBox: Format.rectangle(bounds)
322+
viewBox: formatter.rectangle(bounds)
324323
});
325324
symbolNode.appendChild(exportSvg(definition));
326325
setDefinition(symbol, symbolNode);
327326
}
328327
attrs.href = '#' + symbolNode.id;
329328
attrs.x += bounds.x;
330329
attrs.y += bounds.y;
331-
attrs.width = format(bounds.width);
332-
attrs.height = format(bounds.height);
330+
attrs.width = formatter.number(bounds.width);
331+
attrs.height = formatter.number(bounds.height);
333332
return createElement('use', attrs);
334333
}
335334

@@ -431,7 +430,7 @@ new function() {
431430
: entry.type === 'array'
432431
? value.join(',')
433432
: entry.type === 'number'
434-
? format(value)
433+
? formatter.number(value)
435434
: value;
436435
}
437436
});

0 commit comments

Comments
 (0)