7 class FontLoader extends Loader {
9 constructor( manager ) {
15 load( url, onLoad, onProgress, onError ) {
19 const loader = new FileLoader( this.manager );
20 loader.setPath( this.path );
21 loader.setRequestHeader( this.requestHeader );
22 loader.setWithCredentials( this.withCredentials );
23 loader.load( url, function ( text ) {
25 const font = scope.parse( JSON.parse( text ) );
27 if ( onLoad ) onLoad( font );
29 }, onProgress, onError );
35 return new Font( json );
55 generateShapes( text, size = 100 ) {
58 const paths = createPaths( text, size, this.data );
60 for ( let p = 0, pl = paths.length; p < pl; p ++ ) {
62 shapes.push( ...paths[ p ].toShapes() );
72 function createPaths( text, size, data ) {
74 const chars = Array.from( text );
75 const scale = size / data.resolution;
76 const line_height = ( data.boundingBox.yMax - data.boundingBox.yMin + data.underlineThickness ) * scale;
80 let offsetX = 0, offsetY = 0;
82 for ( let i = 0; i < chars.length; i ++ ) {
84 const char = chars[ i ];
86 if ( char === '\n' ) {
89 offsetY -= line_height;
93 const ret = createPath( char, scale, offsetX, offsetY, data );
94 offsetX += ret.offsetX;
95 paths.push( ret.path );
105 function createPath( char, scale, offsetX, offsetY, data ) {
107 const glyph = data.glyphs[ char ] || data.glyphs[ '?' ];
111 console.error( 'THREE.Font: character "' + char + '" does not exists in font family ' + data.familyName + '.' );
117 const path = new ShapePath();
119 let x, y, cpx, cpy, cpx1, cpy1, cpx2, cpy2;
123 const outline = glyph._cachedOutline || ( glyph._cachedOutline = glyph.o.split( ' ' ) );
125 for ( let i = 0, l = outline.length; i < l; ) {
127 const action = outline[ i ++ ];
133 x = outline[ i ++ ] * scale + offsetX;
134 y = outline[ i ++ ] * scale + offsetY;
142 x = outline[ i ++ ] * scale + offsetX;
143 y = outline[ i ++ ] * scale + offsetY;
149 case 'q': // quadraticCurveTo
151 cpx = outline[ i ++ ] * scale + offsetX;
152 cpy = outline[ i ++ ] * scale + offsetY;
153 cpx1 = outline[ i ++ ] * scale + offsetX;
154 cpy1 = outline[ i ++ ] * scale + offsetY;
156 path.quadraticCurveTo( cpx1, cpy1, cpx, cpy );
160 case 'b': // bezierCurveTo
162 cpx = outline[ i ++ ] * scale + offsetX;
163 cpy = outline[ i ++ ] * scale + offsetY;
164 cpx1 = outline[ i ++ ] * scale + offsetX;
165 cpy1 = outline[ i ++ ] * scale + offsetY;
166 cpx2 = outline[ i ++ ] * scale + offsetX;
167 cpy2 = outline[ i ++ ] * scale + offsetY;
169 path.bezierCurveTo( cpx1, cpy1, cpx2, cpy2, cpx, cpy );
179 return { offsetX: glyph.ha * scale, path: path };
183 export { FontLoader, Font };