2
0

TTFLoader.js 4.3 KB

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