TTFLoader.js 4.3 KB

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