TTFLoader.js 4.0 KB

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