Преглед изворни кода

Merge pull request #17058 from troy351/dev

TTFLoader: Fixed traverse logic
Mr.doob пре 6 година
родитељ
комит
37285df967
3 измењених фајлова са 27 додато и 11 уклоњено
  1. 10 5
      examples/js/loaders/TTFLoader.js
  2. 10 5
      examples/jsm/loaders/TTFLoader.js
  3. 7 1
      src/extras/core/Font.js

+ 10 - 5
examples/js/loaders/TTFLoader.js

@@ -1,6 +1,7 @@
 /**
  * @author gero3 / https://github.com/gero3
  * @author tentone / https://github.com/tentone
+ * @author troy351 / https://github.com/troy351
  *
  * Requires opentype.js to be included in the project.
  * Loads TTF files and converts them into typeface JSON that can be used directly
@@ -48,12 +49,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 +103,7 @@ THREE.TTFLoader.prototype = {
 
 					} );
 
-					glyphs[ String.fromCharCode( glyph.unicode ) ] = token;
+					glyphs[ String.fromCodePoint( glyph.unicode ) ] = token;
 
 				}
 
@@ -106,7 +111,7 @@ THREE.TTFLoader.prototype = {
 
 			return {
 				glyphs: glyphs,
-				familyName: font.familyName,
+				familyName: font.getEnglishName( 'fullName' ),
 				ascender: round( font.ascender * scale ),
 				descender: round( font.descender * scale ),
 				underlinePosition: font.tables.post.underlinePosition,

+ 10 - 5
examples/jsm/loaders/TTFLoader.js

@@ -1,6 +1,7 @@
 /**
  * @author gero3 / https://github.com/gero3
  * @author tentone / https://github.com/tentone
+ * @author troy351 / https://github.com/troy351
  *
  * Requires opentype.js to be included in the project.
  * Loads TTF files and converts them into typeface JSON that can be used directly
@@ -54,11 +55,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 +108,7 @@ TTFLoader.prototype = {
 
 					} );
 
-					glyphs[ String.fromCharCode( glyph.unicode ) ] = token;
+					glyphs[ String.fromCodePoint( unicode ) ] = token;
 
 				}
 
@@ -111,7 +116,7 @@ TTFLoader.prototype = {
 
 			return {
 				glyphs: glyphs,
-				familyName: font.familyName,
+				familyName: font.getEnglishName( 'fullName' ),
 				ascender: round( font.ascender * scale ),
 				descender: round( font.descender * scale ),
 				underlinePosition: font.tables.post.underlinePosition,

+ 7 - 1
src/extras/core/Font.js

@@ -74,7 +74,13 @@ function createPath( char, scale, offsetX, offsetY, data ) {
 
 	var glyph = data.glyphs[ char ] || data.glyphs[ '?' ];
 
-	if ( ! glyph ) return;
+	if ( ! glyph ) {
+
+		console.error( 'THREE.Font: character "' + char + '" does not exists in font family ' + data.familyName + '.' );
+
+		return;
+
+	}
 
 	var path = new ShapePath();