TTFLoader.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import {
  2. FileLoader,
  3. Loader
  4. } from 'three';
  5. import opentype from '../libs/opentype.module.js';
  6. /**
  7. * A loader for the TTF format.
  8. *
  9. * Loads TTF files and converts them into typeface JSON that can be used directly
  10. * to create THREE.Font objects.
  11. *
  12. * ```js
  13. * const loader = new TTFLoader();
  14. * const json = await loader.loadAsync( 'fonts/ttf/kenpixel.ttf' );
  15. * const font = new Font( json );
  16. * ```
  17. *
  18. * @augments Loader
  19. */
  20. class TTFLoader extends Loader {
  21. /**
  22. * Constructs a new TTF loader.
  23. *
  24. * @param {LoadingManager} [manager] - The loading manager.
  25. */
  26. constructor( manager ) {
  27. super( manager );
  28. /**
  29. * Whether the TTF commands should be reversed or not.
  30. *
  31. * @type {boolean}
  32. * @default false
  33. */
  34. this.reversed = false;
  35. }
  36. /**
  37. * Starts loading from the given URL and passes the loaded TTF asset
  38. * to the `onLoad()` callback.
  39. *
  40. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  41. * @param {function(Object)} onLoad - Executed when the loading process has been finished.
  42. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  43. * @param {onErrorCallback} onError - Executed when errors occur.
  44. */
  45. load( url, onLoad, onProgress, onError ) {
  46. const scope = this;
  47. const loader = new FileLoader( this.manager );
  48. loader.setPath( this.path );
  49. loader.setResponseType( 'arraybuffer' );
  50. loader.setRequestHeader( this.requestHeader );
  51. loader.setWithCredentials( this.withCredentials );
  52. loader.load( url, function ( buffer ) {
  53. try {
  54. onLoad( scope.parse( buffer ) );
  55. } catch ( e ) {
  56. if ( onError ) {
  57. onError( e );
  58. } else {
  59. console.error( e );
  60. }
  61. scope.manager.itemError( url );
  62. }
  63. }, onProgress, onError );
  64. }
  65. /**
  66. * Parses the given TTF data and returns a JSON for creating a font.
  67. *
  68. * @param {ArrayBuffer} arraybuffer - The raw TTF data as an array buffer.
  69. * @return {Object} The result JSON.
  70. */
  71. parse( arraybuffer ) {
  72. function convert( font, reversed ) {
  73. const round = Math.round;
  74. const glyphs = {};
  75. const scale = ( 100000 ) / ( ( font.unitsPerEm || 2048 ) * 72 );
  76. const glyphIndexMap = font.encoding.cmap.glyphIndexMap;
  77. const unicodes = Object.keys( glyphIndexMap );
  78. for ( let i = 0; i < unicodes.length; i ++ ) {
  79. const unicode = unicodes[ i ];
  80. const glyph = font.glyphs.glyphs[ glyphIndexMap[ unicode ] ];
  81. if ( unicode !== undefined ) {
  82. const token = {
  83. ha: round( glyph.advanceWidth * scale ),
  84. x_min: round( glyph.xMin * scale ),
  85. x_max: round( glyph.xMax * scale ),
  86. o: ''
  87. };
  88. if ( reversed ) {
  89. glyph.path.commands = reverseCommands( glyph.path.commands );
  90. }
  91. glyph.path.commands.forEach( function ( command ) {
  92. if ( command.type.toLowerCase() === 'c' ) {
  93. command.type = 'b';
  94. }
  95. token.o += command.type.toLowerCase() + ' ';
  96. if ( command.x !== undefined && command.y !== undefined ) {
  97. token.o += round( command.x * scale ) + ' ' + round( command.y * scale ) + ' ';
  98. }
  99. if ( command.x1 !== undefined && command.y1 !== undefined ) {
  100. token.o += round( command.x1 * scale ) + ' ' + round( command.y1 * scale ) + ' ';
  101. }
  102. if ( command.x2 !== undefined && command.y2 !== undefined ) {
  103. token.o += round( command.x2 * scale ) + ' ' + round( command.y2 * scale ) + ' ';
  104. }
  105. } );
  106. glyphs[ String.fromCodePoint( glyph.unicode ) ] = token;
  107. }
  108. }
  109. return {
  110. glyphs: glyphs,
  111. familyName: font.getEnglishName( 'fullName' ),
  112. ascender: round( font.ascender * scale ),
  113. descender: round( font.descender * scale ),
  114. underlinePosition: font.tables.post.underlinePosition,
  115. underlineThickness: font.tables.post.underlineThickness,
  116. boundingBox: {
  117. xMin: font.tables.head.xMin,
  118. xMax: font.tables.head.xMax,
  119. yMin: font.tables.head.yMin,
  120. yMax: font.tables.head.yMax
  121. },
  122. resolution: 1000,
  123. original_font_information: font.tables.name
  124. };
  125. }
  126. function reverseCommands( commands ) {
  127. const paths = [];
  128. let path;
  129. commands.forEach( function ( c ) {
  130. if ( c.type.toLowerCase() === 'm' ) {
  131. path = [ c ];
  132. paths.push( path );
  133. } else if ( c.type.toLowerCase() !== 'z' ) {
  134. path.push( c );
  135. }
  136. } );
  137. const reversed = [];
  138. paths.forEach( function ( p ) {
  139. const result = {
  140. type: 'm',
  141. x: p[ p.length - 1 ].x,
  142. y: p[ p.length - 1 ].y
  143. };
  144. reversed.push( result );
  145. for ( let i = p.length - 1; i > 0; i -- ) {
  146. const command = p[ i ];
  147. const result = { type: command.type };
  148. if ( command.x2 !== undefined && command.y2 !== undefined ) {
  149. result.x1 = command.x2;
  150. result.y1 = command.y2;
  151. result.x2 = command.x1;
  152. result.y2 = command.y1;
  153. } else if ( command.x1 !== undefined && command.y1 !== undefined ) {
  154. result.x1 = command.x1;
  155. result.y1 = command.y1;
  156. }
  157. result.x = p[ i - 1 ].x;
  158. result.y = p[ i - 1 ].y;
  159. reversed.push( result );
  160. }
  161. } );
  162. return reversed;
  163. }
  164. return convert( opentype.parse( arraybuffer ), this.reversed );
  165. }
  166. }
  167. export { TTFLoader };
粤ICP备19079148号