TTFLoader.js 4.5 KB

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