Font.js 3.1 KB
Newer Older
R
Rich Harris 已提交
1 2 3
import { ShapeUtils } from '../ShapeUtils';
import { ShapePath } from './Path';

4 5 6 7 8
/**
 * @author zz85 / http://www.lab4games.net/zz85/blog
 * @author mrdoob / http://mrdoob.com/
 */

M
Mr.doob 已提交
9
function Font( data ) {
10 11 12 13 14

	this.data = data;

};

R
Rich Harris 已提交
15
Object.assign( Font.prototype, {
16

17 18
	isFont: true,

M
Mr.doob 已提交
19
	generateShapes: function ( text, size, divisions ) {
20 21 22 23 24 25 26 27 28 29 30

		function createPaths( text ) {

			var chars = String( text ).split( '' );
			var scale = size / data.resolution;
			var offset = 0;

			var paths = [];

			for ( var i = 0; i < chars.length; i ++ ) {

M
Mr.doob 已提交
31
				var ret = createPath( chars[ i ], scale, offset );
32 33 34 35 36 37 38 39 40 41
				offset += ret.offset;

				paths.push( ret.path );

			}

			return paths;

		}

M
Mr.doob 已提交
42
		function createPath( c, scale, offset ) {
43

M
Mr.doob 已提交
44
			var glyph = data.glyphs[ c ] || data.glyphs[ '?' ];
45 46 47

			if ( ! glyph ) return;

R
Rich Harris 已提交
48
			var path = new ShapePath();
49

R
Rich Harris 已提交
50
			var pts = [], b2 = ShapeUtils.b2, b3 = ShapeUtils.b3;
M
Mr.doob 已提交
51
			var x, y, cpx, cpy, cpx0, cpy0, cpx1, cpy1, cpx2, cpy2, laste;
52

M
Mr.doob 已提交
53
			if ( glyph.o ) {
54

M
Mr.doob 已提交
55
				var outline = glyph._cachedOutline || ( glyph._cachedOutline = glyph.o.split( ' ' ) );
56

M
Mr.doob 已提交
57
				for ( var i = 0, l = outline.length; i < l; ) {
58

M
Mr.doob 已提交
59
					var action = outline[ i ++ ];
60 61 62

					switch ( action ) {

M
Mr.doob 已提交
63
						case 'm': // moveTo
64

M
Mr.doob 已提交
65 66
							x = outline[ i ++ ] * scale + offset;
							y = outline[ i ++ ] * scale;
67

M
Mr.doob 已提交
68
							path.moveTo( x, y );
69

M
Mr.doob 已提交
70
							break;
71

M
Mr.doob 已提交
72
						case 'l': // lineTo
73

M
Mr.doob 已提交
74 75
							x = outline[ i ++ ] * scale + offset;
							y = outline[ i ++ ] * scale;
76

M
Mr.doob 已提交
77
							path.lineTo( x, y );
78

M
Mr.doob 已提交
79
							break;
80

M
Mr.doob 已提交
81
						case 'q': // quadraticCurveTo
82

M
Mr.doob 已提交
83 84 85 86
							cpx  = outline[ i ++ ] * scale + offset;
							cpy  = outline[ i ++ ] * scale;
							cpx1 = outline[ i ++ ] * scale + offset;
							cpy1 = outline[ i ++ ] * scale;
87

M
Mr.doob 已提交
88
							path.quadraticCurveTo( cpx1, cpy1, cpx, cpy );
89

M
Mr.doob 已提交
90
							laste = pts[ pts.length - 1 ];
91

M
Mr.doob 已提交
92
							if ( laste ) {
93

M
Mr.doob 已提交
94 95
								cpx0 = laste.x;
								cpy0 = laste.y;
96

M
Mr.doob 已提交
97
								for ( var i2 = 1; i2 <= divisions; i2 ++ ) {
98

M
Mr.doob 已提交
99 100 101
									var t = i2 / divisions;
									b2( t, cpx0, cpx1, cpx );
									b2( t, cpy0, cpy1, cpy );
102

M
Mr.doob 已提交
103
								}
104

M
Mr.doob 已提交
105
							}
106

M
Mr.doob 已提交
107
							break;
108

M
Mr.doob 已提交
109
						case 'b': // bezierCurveTo
110

M
Mr.doob 已提交
111 112 113 114 115 116
							cpx  = outline[ i ++ ] * scale + offset;
							cpy  = outline[ i ++ ] * scale;
							cpx1 = outline[ i ++ ] * scale + offset;
							cpy1 = outline[ i ++ ] * scale;
							cpx2 = outline[ i ++ ] * scale + offset;
							cpy2 = outline[ i ++ ] * scale;
117

M
Mr.doob 已提交
118
							path.bezierCurveTo( cpx1, cpy1, cpx2, cpy2, cpx, cpy );
119

M
Mr.doob 已提交
120
							laste = pts[ pts.length - 1 ];
121

M
Mr.doob 已提交
122
							if ( laste ) {
123

M
Mr.doob 已提交
124 125
								cpx0 = laste.x;
								cpy0 = laste.y;
126

M
Mr.doob 已提交
127
								for ( var i2 = 1; i2 <= divisions; i2 ++ ) {
128

M
Mr.doob 已提交
129 130 131
									var t = i2 / divisions;
									b3( t, cpx0, cpx1, cpx2, cpx );
									b3( t, cpy0, cpy1, cpy2, cpy );
132

M
Mr.doob 已提交
133
								}
134 135 136

							}

M
Mr.doob 已提交
137
							break;
138 139 140 141 142 143 144 145 146 147 148 149 150 151

					}

				}

			}

			return { offset: glyph.ha * scale, path: path };

		}

		//

		if ( size === undefined ) size = 100;
M
Mr.doob 已提交
152
		if ( divisions === undefined ) divisions = 4;
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

		var data = this.data;

		var paths = createPaths( text );
		var shapes = [];

		for ( var p = 0, pl = paths.length; p < pl; p ++ ) {

			Array.prototype.push.apply( shapes, paths[ p ].toShapes() );

		}

		return shapes;

	}

169
} );
R
Rich Harris 已提交
170 171


172
export { Font };