This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathART.Font.js
89 lines (70 loc) · 2.49 KB
/
ART.Font.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
---
name: ART.Font
description: "Fonts for ART, implements code from [Cufón](http://cufon.shoqolate.com/)"
authors: ["[Simo Kinnunen](http://twitter.com/sorccu)", "[Valerio Proietti](http://mad4milk.net/)"]
provides: ART.Font
requires: ART.Shape
...
*/
(function(){
var fonts = {};
ART.registerFont = function(font){
var face = font.face,
family = face['font-family'],
weight = (face['font-weight'] > 400 ? 'bold' : 'normal'),
style = (face['font-stretch'] == 'oblique' ? 'italic' : 'normal');
fonts[weight + style + name] = font;
return this;
};
var VMLToSVG = function(path, s, x, y){
var end = '';
var regexp = /([mrvxe])([^a-z]*)/g, match;
while ((match = regexp.exec(path))){
var c = match[2].split(',');
switch (match[1]){
case 'v': end += 'c ' + (s * c[0]) + ',' + (s * c[1]) + ',' + (s * c[2]) + ',' + (s * c[3]) + ',' + (s * c[4]) + ',' + (s * c[5]); break;
case 'r': end += 'l ' + (s * c[0]) + ',' + (s * c[1]); break;
case 'm': end += 'M ' + (x + (s * c[0])) + ',' + (y + (s * c[1])); break;
case 'x': end += 'z'; break;
}
}
return end;
};
var parseFontString = function(font){
var regexp = /^\s*((?:(?:normal|bold|italic)\s+)*)(?:(\d+(?:\.\d+)?)[ptexm\%]*(?:\s*\/.*?)?\s+)?\s*\"?([^\"]*)/i,
match = regexp.exec(font);
return {
fontFamily: match[3],
fontSize: match[2],
fontStyle: (/italic/.exec(match[1]) || ''),
fontWeight: (/bold/.exec(match[1]) || '')
};
};
ART.Font = new Class({
Extends: ART.Shape,
initialize: function(text, font){
this.parent();
if (text != null && font != null) this.draw(text, font);
},
draw: function(text, font){
if (typeof font == 'string') font = parseFontString(font);
var family = font.fontFamily || font['font-family'],
weight = font.fontWeight || font['font-weight'] || 'normal',
style = font.fontStyle || font['font-style'] || 'normal',
size = parseFloat(font.fontSize || font['font-size'] || font.size);
font = font.glyphs ? font : fonts[weight + style + name];
if (!font) throw new Error('The specified font has not been found.');
size = size / font.face['units-per-em'];
var width = 0, height = size * font.face.ascent, path = '';
for (var i = 0, l = text.length; i < l; ++i){
var glyph = font.glyphs[text.charAt(i)] || font.glyphs[' '];
var w = size * (glyph.w || font.w);
if (glyph.d) path += VMLToSVG('m' + glyph.d + 'x', size, width, height);
width += w;
}
height -= size * font.face.descent;
return this.parent(path, width, height);
}
});
})();