TTFLoader.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. class TTFLoader extends Loader {
  12. constructor( manager ) {
  13. super( manager );
  14. this.reversed = false;
  15. }
  16. load( url, onLoad, onProgress, onError ) {
  17. const scope = this;
  18. const loader = new FileLoader( this.manager );
  19. loader.setPath( this.path );
  20. loader.setResponseType( 'arraybuffer' );
  21. loader.setRequestHeader( this.requestHeader );
  22. loader.setWithCredentials( this.withCredentials );
  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( arraybuffer ) {
  37. function convert( font, reversed ) {
  38. const round = Math.round;
  39. const glyphs = {};
  40. const scale = ( 100000 ) / ( ( font.unitsPerEm || 2048 ) * 72 );
  41. const glyphIndexMap = font.encoding.cmap.glyphIndexMap;
  42. const unicodes = Object.keys( glyphIndexMap );
  43. for ( let i = 0; i < unicodes.length; i ++ ) {
  44. const unicode = unicodes[ i ];
  45. const glyph = font.glyphs.glyphs[ glyphIndexMap[ unicode ] ];
  46. if ( unicode !== undefined ) {
  47. const 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. const paths = [];
  93. let 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. const reversed = [];
  103. paths.forEach( function ( p ) {
  104. const 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 ( let i = p.length - 1; i > 0; i -- ) {
  111. const command = p[ i ];
  112. const 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 ); // eslint-disable-line no-undef
  134. }
  135. }
  136. export { TTFLoader };