Sfoglia il codice sorgente

TTFLoader: using glyphIndexMap to traverse

troy351 6 anni fa
parent
commit
82faeb2e3c
2 ha cambiato i file con 16 aggiunte e 8 eliminazioni
  1. 8 4
      examples/js/loaders/TTFLoader.js
  2. 8 4
      examples/jsm/loaders/TTFLoader.js

+ 8 - 4
examples/js/loaders/TTFLoader.js

@@ -48,12 +48,16 @@ THREE.TTFLoader.prototype = {
 
 			var glyphs = {};
 			var scale = ( 100000 ) / ( ( font.unitsPerEm || 2048 ) * 72 );
+			
+			var glyphIndexMap = font.encoding.cmap.glyphIndexMap;
+			var unicodes = Object.keys( glyphIndexMap );
 
-			for ( var i = 0; i < font.glyphs.length; i ++ ) {
+			for ( var i = 0; i < unicodes.length; i ++ ) {
 
-				var glyph = font.glyphs.glyphs[ i ];
+				var unicode = unicodes[ i ];
+				var glyph = font.glyphs.glyphs[ glyphIndexMap[ unicode ] ];
 
-				if ( glyph.unicode !== undefined ) {
+				if ( unicode !== undefined ) {
 
 					var token = {
 						ha: round( glyph.advanceWidth * scale ),
@@ -98,7 +102,7 @@ THREE.TTFLoader.prototype = {
 
 					} );
 
-					glyphs[ String.fromCharCode( glyph.unicode ) ] = token;
+					glyphs[ String.fromCodePoint( glyph.unicode ) ] = token;
 
 				}
 

+ 8 - 4
examples/jsm/loaders/TTFLoader.js

@@ -54,11 +54,15 @@ TTFLoader.prototype = {
 			var glyphs = {};
 			var scale = ( 100000 ) / ( ( font.unitsPerEm || 2048 ) * 72 );
 
-			for ( var i = 0; i < font.glyphs.length; i ++ ) {
+			var glyphIndexMap = font.encoding.cmap.glyphIndexMap;
+			var unicodes = Object.keys( glyphIndexMap );
 
-				var glyph = font.glyphs.glyphs[ i ];
+			for ( var i = 0; i < unicodes.length; i ++ ) {
 
-				if ( glyph.unicode !== undefined ) {
+				var unicode = unicodes[ i ];
+				var glyph = font.glyphs.glyphs[ glyphIndexMap[ unicode ] ];
+
+				if ( unicode !== undefined ) {
 
 					var token = {
 						ha: round( glyph.advanceWidth * scale ),
@@ -103,7 +107,7 @@ TTFLoader.prototype = {
 
 					} );
 
-					glyphs[ String.fromCharCode( glyph.unicode ) ] = token;
+					glyphs[ String.fromCodePoint( unicode ) ] = token;
 
 				}