TTFLoader.js 4.5 KB

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