TTFLoader.js 4.7 KB

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