Skip to content

Commit 18e2166

Browse files
committed
- this is the next major iteration of ART!
- SVG/VML based instead of Canvas/VML - supports groups, dynamic transformations, dom operations with SVG/VML real elements via ART methods - real svg paths from any drawing application, including Illustrator, automatically converted to VML - and much more!
1 parent bf30d5c commit 18e2166

File tree

11 files changed

+871
-629
lines changed

11 files changed

+871
-629
lines changed

Fonts/MgOpen.Moderna.Bold.js renamed to Fonts/Moderna.Bold.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Fonts/MgOpen.Moderna.js renamed to Fonts/Moderna.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/ART.Canvas.js

Lines changed: 0 additions & 167 deletions
This file was deleted.

Source/ART.Font.js

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,73 +7,74 @@ description: Fonts for ART, implements code from [Cufón](http://cufon.shoqolate
77
88
authors: [Simo Kinnunen](http://twitter.com/sorccu), ART adaptation by [Valerio Proietti](http://mad4milk.net/)
99
10-
provides: [ART:text, ART.Font]
10+
provides: ART.Font
1111
12-
requires: [ART.Canvas, ART.VML]
12+
requires: ART.Shape
1313
1414
...
1515
*/
1616

17-
// stub
18-
19-
ART.Font = function(font){
20-
return font;
21-
};
22-
2317
(function(){
2418

2519
var fonts = {};
2620

27-
ART.defineFont = function(name, font){
28-
fonts[name] = new ART.Font(font);
29-
};
30-
31-
ART.lookupFont = function(name){
32-
return fonts[name];
33-
};
34-
3521
ART.registerFont = function(font){
36-
var face = font.face, name = face['font-family'].toLowerCase().split(' ');
37-
if (face['font-weight'] > 400) name.push('bold');
38-
if (face['font-stretch'] == 'oblique') name.push('italic');
39-
ART.defineFont(name.join('-'), font);
22+
var face = font.face, name = face['font-family'];
23+
if (!fonts[name]) fonts[name] = {};
24+
var currentFont = fonts[name];
25+
var isBold = (face['font-weight'] > 400), isItalic = (face['font-stretch'] == 'oblique');
26+
if (isBold && isItalic) currentFont.boldItalic = font;
27+
else if (isBold) currentFont.bold = font;
28+
else if (isItalic) currentFont.italic = font;
29+
else currentFont.normal = font;
30+
return this;
4031
};
4132

42-
var path = function(path, s, self){
43-
var X = 0, Y = 0;
33+
var VMLToSVG = function(path, s, x, y){
34+
var end = '';
4435
var regexp = /([mrvxe])([^a-z]*)/g, match;
4536
while ((match = regexp.exec(path))){
4637
var c = match[2].split(',');
4738
switch (match[1]){
48-
case 'v':
49-
self.bezierTo({x: X + s * ~~c[0], y: Y + s * ~~c[1]}, {x: X + s * ~~c[2], y: Y + s * ~~c[3]}, {x: X += s * ~~c[4], y: Y += s * ~~c[5]});
50-
break;
51-
case 'r': self.lineTo({x: X += s * ~~c[0], y: Y += s * ~~c[1]}); break;
52-
case 'm': self.moveTo({x: X = s * ~~c[0], y: Y = s * ~~c[1]}); break;
53-
case 'x': self.join(); break;
54-
case 'e': return;
39+
case 'v': end += 'c ' + (s * c[0]) + ',' + (s * c[1]) + ',' + (s * c[2]) + ',' + (s * c[3]) + ',' + (s * c[4]) + ',' + (s * c[5]); break;
40+
case 'r': end += 'l ' + (s * c[0]) + ',' + (s * c[1]); break;
41+
case 'm': end += 'M ' + (x + (s * c[0])) + ',' + (y + (s * c[1])); break;
42+
case 'x': end += 'z'; break;
5543
}
5644
}
45+
46+
return end;
5747
};
5848

59-
ART.implement({'text': function(font, size, text){
60-
if (typeof font == 'string') font = fonts[font];
61-
if (!font) return new Error('The specified font has not been found.');
62-
63-
this.save();
64-
size = size / font.face['units-per-em'];
65-
// Temporary "relative" fix shifting the whole layer by the pointer, since the pointer is lost with path. Should not matter since it's later restored.
66-
this.shift({x: this.pointer.x, y: this.pointer.y + Math.round(size * font.face.ascent)});
67-
var width = 0;
68-
for (var i = 0, l = text.length; i < l; ++i){
69-
var glyph = font.glyphs[text.charAt(i)] || font.glyphs[' '];
70-
if (glyph.d) path('m' + glyph.d + 'x', size, this);
71-
var w = size * (glyph.w || font.w);
72-
this.shift({x: w, y: 0});
73-
width += w;
49+
ART.Font = new Class({
50+
51+
Extends: ART.Shape,
52+
53+
initialize: function(font, variant, text, size){
54+
if (typeof font == 'string') font = fonts[font][(variant || 'normal').camelCase()];
55+
if (!font) throw new Error('The specified font has not been found.');
56+
this.font = font;
57+
58+
this.parent();
59+
if (text != null && size != null) this.draw(text, size);
60+
},
61+
62+
draw: function(text, size){
63+
var font = this.font;
64+
size = size / font.face['units-per-em'];
65+
66+
var width = 0, height = size * font.face.ascent, path = '';
67+
68+
for (var i = 0, l = text.length; i < l; ++i){
69+
var glyph = font.glyphs[text.charAt(i)] || font.glyphs[' '];
70+
var w = size * (glyph.w || font.w);
71+
if (glyph.d) path += VMLToSVG('m' + glyph.d + 'x', size, width, height);
72+
width += w;
73+
}
74+
75+
return this.parent(path);
7476
}
75-
this.restore();
76-
return {x: width, y: size * (font.face.ascent - font.face.descent)};
77-
}});
77+
78+
});
7879

7980
})();

Source/ART.Path.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
---
3+
4+
name: ART.Path
5+
6+
description: Class to generate a valid SVG path using method calls.
7+
8+
authors: [Valerio Proietti](http://mad4milk.net)
9+
10+
provides: ART.Path
11+
12+
requires: ART
13+
14+
...
15+
*/
16+
17+
ART.Path = new Class({
18+
19+
initialize: function(){
20+
this.clear();
21+
},
22+
23+
clear: function(){
24+
this.path = [];
25+
this.x = 0;
26+
this.y = 0;
27+
return this;
28+
},
29+
30+
move: function(x, y){
31+
this.path.push('m', x, y);
32+
return this;
33+
},
34+
35+
line: function(x, y){
36+
this.x = x; this.y = y;
37+
this.path.push('l', x, y);
38+
return this;
39+
},
40+
41+
close: function(){
42+
this.path.push('z');
43+
return this;
44+
},
45+
46+
bezier: function(c1x, c1y, c2x, c2y, ex, ey){
47+
this.path.push('c', c1x, c1y, c2x, c2y, ex, ey);
48+
return this;
49+
},
50+
51+
arcLeft: function(x, y){
52+
return this.bezier(0, y * Math.kappa, x - (x * Math.kappa), y, x, y);
53+
},
54+
55+
arcRight: function(x, y){
56+
return this.bezier(x * Math.kappa, 0, x, y - (y * Math.kappa), x, y);
57+
}
58+
59+
});
60+
61+
ART.Path.prototype.toString = function(){
62+
return this.path.join(' ');
63+
};

0 commit comments

Comments
 (0)