TTFLoader.js 4.2 KB

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