FontLoader.js 3.1 KB

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