TTFLoader.js 4.4 KB

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