FontLoader.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ( function () {
  2. class FontLoader extends THREE.Loader {
  3. constructor( manager ) {
  4. super( manager );
  5. }
  6. load( url, onLoad, onProgress, onError ) {
  7. const scope = this;
  8. const loader = new THREE.FileLoader( this.manager );
  9. loader.setPath( this.path );
  10. loader.setRequestHeader( this.requestHeader );
  11. loader.setWithCredentials( scope.withCredentials );
  12. loader.load( url, function ( text ) {
  13. let json;
  14. try {
  15. json = JSON.parse( text );
  16. } catch ( e ) {
  17. console.warn( 'THREE.FontLoader: typeface.js support is being deprecated. Use typeface.json instead.' );
  18. json = JSON.parse( text.substring( 65, text.length - 2 ) );
  19. }
  20. const font = scope.parse( json );
  21. if ( onLoad ) onLoad( font );
  22. }, onProgress, onError );
  23. }
  24. parse( json ) {
  25. return new Font( json );
  26. }
  27. } //
  28. class Font {
  29. constructor( data ) {
  30. this.type = 'Font';
  31. this.data = data;
  32. }
  33. generateShapes( text, size = 100 ) {
  34. const shapes = [];
  35. const paths = createPaths( text, size, this.data );
  36. for ( let p = 0, pl = paths.length; p < pl; p ++ ) {
  37. Array.prototype.push.apply( shapes, paths[ p ].toShapes() );
  38. }
  39. return shapes;
  40. }
  41. }
  42. function createPaths( text, size, data ) {
  43. const chars = Array.from( text );
  44. const scale = size / data.resolution;
  45. const line_height = ( data.boundingBox.yMax - data.boundingBox.yMin + data.underlineThickness ) * scale;
  46. const paths = [];
  47. let offsetX = 0,
  48. offsetY = 0;
  49. for ( let i = 0; i < chars.length; i ++ ) {
  50. const char = chars[ i ];
  51. if ( char === '\n' ) {
  52. offsetX = 0;
  53. offsetY -= line_height;
  54. } else {
  55. const ret = createPath( char, scale, offsetX, offsetY, data );
  56. offsetX += ret.offsetX;
  57. paths.push( ret.path );
  58. }
  59. }
  60. return paths;
  61. }
  62. function createPath( char, scale, offsetX, offsetY, data ) {
  63. const glyph = data.glyphs[ char ] || data.glyphs[ '?' ];
  64. if ( ! glyph ) {
  65. console.error( 'THREE.Font: character "' + char + '" does not exists in font family ' + data.familyName + '.' );
  66. return;
  67. }
  68. const path = new THREE.ShapePath();
  69. let x, y, cpx, cpy, cpx1, cpy1, cpx2, cpy2;
  70. if ( glyph.o ) {
  71. const outline = glyph._cachedOutline || ( glyph._cachedOutline = glyph.o.split( ' ' ) );
  72. for ( let i = 0, l = outline.length; i < l; ) {
  73. const action = outline[ i ++ ];
  74. switch ( action ) {
  75. case 'm':
  76. // moveTo
  77. x = outline[ i ++ ] * scale + offsetX;
  78. y = outline[ i ++ ] * scale + offsetY;
  79. path.moveTo( x, y );
  80. break;
  81. case 'l':
  82. // lineTo
  83. x = outline[ i ++ ] * scale + offsetX;
  84. y = outline[ i ++ ] * scale + offsetY;
  85. path.lineTo( x, y );
  86. break;
  87. case 'q':
  88. // quadraticCurveTo
  89. cpx = outline[ i ++ ] * scale + offsetX;
  90. cpy = outline[ i ++ ] * scale + offsetY;
  91. cpx1 = outline[ i ++ ] * scale + offsetX;
  92. cpy1 = outline[ i ++ ] * scale + offsetY;
  93. path.quadraticCurveTo( cpx1, cpy1, cpx, cpy );
  94. break;
  95. case 'b':
  96. // bezierCurveTo
  97. cpx = outline[ i ++ ] * scale + offsetX;
  98. cpy = outline[ i ++ ] * scale + offsetY;
  99. cpx1 = outline[ i ++ ] * scale + offsetX;
  100. cpy1 = outline[ i ++ ] * scale + offsetY;
  101. cpx2 = outline[ i ++ ] * scale + offsetX;
  102. cpy2 = outline[ i ++ ] * scale + offsetY;
  103. path.bezierCurveTo( cpx1, cpy1, cpx2, cpy2, cpx, cpy );
  104. break;
  105. }
  106. }
  107. }
  108. return {
  109. offsetX: glyph.ha * scale,
  110. path: path
  111. };
  112. }
  113. Font.prototype.isFont = true;
  114. THREE.Font = Font;
  115. THREE.FontLoader = FontLoader;
  116. } )();