FontLoader.js 3.2 KB

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