TTFLoader.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. console.warn( "THREE.TTFLoader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  3. * Requires opentype.js to be included in the project.
  4. * Loads TTF files and converts them into typeface JSON that can be used directly
  5. * to create THREE.Font objects.
  6. */
  7. THREE.TTFLoader = function ( manager ) {
  8. THREE.Loader.call( this, manager );
  9. this.reversed = false;
  10. };
  11. THREE.TTFLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
  12. constructor: THREE.TTFLoader,
  13. load: function ( url, onLoad, onProgress, onError ) {
  14. var scope = this;
  15. var loader = new THREE.FileLoader( this.manager );
  16. loader.setPath( this.path );
  17. loader.setResponseType( 'arraybuffer' );
  18. loader.setRequestHeader( this.requestHeader );
  19. loader.setWithCredentials( this.withCredentials );
  20. loader.load( url, function ( buffer ) {
  21. try {
  22. onLoad( scope.parse( buffer ) );
  23. } catch ( e ) {
  24. if ( onError ) {
  25. onError( e );
  26. } else {
  27. console.error( e );
  28. }
  29. scope.manager.itemError( url );
  30. }
  31. }, onProgress, onError );
  32. },
  33. parse: function ( arraybuffer ) {
  34. function convert( font, reversed ) {
  35. var round = Math.round;
  36. var glyphs = {};
  37. var scale = ( 100000 ) / ( ( font.unitsPerEm || 2048 ) * 72 );
  38. var glyphIndexMap = font.encoding.cmap.glyphIndexMap;
  39. var unicodes = Object.keys( glyphIndexMap );
  40. for ( var i = 0; i < unicodes.length; i ++ ) {
  41. var unicode = unicodes[ i ];
  42. var glyph = font.glyphs.glyphs[ glyphIndexMap[ unicode ] ];
  43. if ( unicode !== undefined ) {
  44. var token = {
  45. ha: round( glyph.advanceWidth * scale ),
  46. x_min: round( glyph.xMin * scale ),
  47. x_max: round( glyph.xMax * scale ),
  48. o: ''
  49. };
  50. if ( reversed ) {
  51. glyph.path.commands = reverseCommands( glyph.path.commands );
  52. }
  53. glyph.path.commands.forEach( function ( command ) {
  54. if ( command.type.toLowerCase() === 'c' ) {
  55. command.type = 'b';
  56. }
  57. token.o += command.type.toLowerCase() + ' ';
  58. if ( command.x !== undefined && command.y !== undefined ) {
  59. token.o += round( command.x * scale ) + ' ' + round( command.y * scale ) + ' ';
  60. }
  61. if ( command.x1 !== undefined && command.y1 !== undefined ) {
  62. token.o += round( command.x1 * scale ) + ' ' + round( command.y1 * scale ) + ' ';
  63. }
  64. if ( command.x2 !== undefined && command.y2 !== undefined ) {
  65. token.o += round( command.x2 * scale ) + ' ' + round( command.y2 * scale ) + ' ';
  66. }
  67. } );
  68. glyphs[ String.fromCodePoint( glyph.unicode ) ] = token;
  69. }
  70. }
  71. return {
  72. glyphs: glyphs,
  73. familyName: font.getEnglishName( 'fullName' ),
  74. ascender: round( font.ascender * scale ),
  75. descender: round( font.descender * scale ),
  76. underlinePosition: font.tables.post.underlinePosition,
  77. underlineThickness: font.tables.post.underlineThickness,
  78. boundingBox: {
  79. xMin: font.tables.head.xMin,
  80. xMax: font.tables.head.xMax,
  81. yMin: font.tables.head.yMin,
  82. yMax: font.tables.head.yMax
  83. },
  84. resolution: 1000,
  85. original_font_information: font.tables.name
  86. };
  87. }
  88. function reverseCommands( commands ) {
  89. var paths = [];
  90. var path;
  91. commands.forEach( function ( c ) {
  92. if ( c.type.toLowerCase() === 'm' ) {
  93. path = [ c ];
  94. paths.push( path );
  95. } else if ( c.type.toLowerCase() !== 'z' ) {
  96. path.push( c );
  97. }
  98. } );
  99. var reversed = [];
  100. paths.forEach( function ( p ) {
  101. var result = {
  102. type: 'm',
  103. x: p[ p.length - 1 ].x,
  104. y: p[ p.length - 1 ].y
  105. };
  106. reversed.push( result );
  107. for ( var i = p.length - 1; i > 0; i -- ) {
  108. var command = p[ i ];
  109. var result = { type: command.type };
  110. if ( command.x2 !== undefined && command.y2 !== undefined ) {
  111. result.x1 = command.x2;
  112. result.y1 = command.y2;
  113. result.x2 = command.x1;
  114. result.y2 = command.y1;
  115. } else if ( command.x1 !== undefined && command.y1 !== undefined ) {
  116. result.x1 = command.x1;
  117. result.y1 = command.y1;
  118. }
  119. result.x = p[ i - 1 ].x;
  120. result.y = p[ i - 1 ].y;
  121. reversed.push( result );
  122. }
  123. } );
  124. return reversed;
  125. }
  126. if ( typeof opentype === 'undefined' ) {
  127. console.warn( 'THREE.TTFLoader: The loader requires opentype.js. Make sure it\'s included before using the loader.' );
  128. return null;
  129. }
  130. return convert( opentype.parse( arraybuffer ), this.reversed ); // eslint-disable-line no-undef
  131. }
  132. } );