@@ -7,14 +7,12 @@ namespace dev.klebersilva.tools.bitmapfontcreator
77{ 
88	internal  static class  BitmapFontCreator 
99	{ 
10- 		public  static void  TryCreateFont ( ExecutionData  data ,  bool  warnBeforeOverwrite ) 
10+ 		private  const  char  IgnoreCharacter  =  ' ' ; 
11+ 
12+ 		public  static bool  TryCreateFont ( ExecutionData  data ,  bool  warnBeforeOverwrite ,  out  string  error ) 
1113		{ 
12- 			var  error  =  CheckForErrors ( data ) ; 
13- 			if  ( ! string . IsNullOrEmpty ( error ) ) 
14- 			{ 
15- 				Debug . LogError ( error ) ; 
16- 				return ; 
17- 			} 
14+ 			error  =  CheckForErrors ( data ) ; 
15+ 			if  ( ! string . IsNullOrEmpty ( error ) )  return  false ; 
1816
1917			var  path  =  AssetDatabase . GetAssetPath ( data . Texture ) ; 
2018			var  baseName  =  Path . GetFileNameWithoutExtension ( path ) ; 
@@ -25,16 +23,18 @@ public static void TryCreateFont(ExecutionData data, bool warnBeforeOverwrite)
2523			if  ( warnBeforeOverwrite  &&  ! ( AssetDatabase . GUIDFromAssetPath ( materialPath )  ==  null  &&  AssetDatabase . GUIDFromAssetPath ( fontPath )  ==  null ) ) 
2624			{ 
2725				if  ( ! EditorUtility . DisplayDialog ( "Warning" ,  "Asset already exists. Overwrite? (It will keep the references)" ,  "Yes" ,  "No" ) ) 
28- 					return ; 
26+ 					return   false ; 
2927			} 
3028
3129			var  material  =  CreateMaterial ( baseName ,  data . Texture ) ; 
32- 			var  font  =  CreateFontAsset ( baseName ,  material ,  data ) ; 
30+ 			var  font  =  CreateFontAsset ( baseName ,  material ,  data ,  out  error ) ; 
31+ 			if  ( ! string . IsNullOrEmpty ( error ) )  return  false ; 
3332
3433			AssetDatabase . CreateAsset ( material ,  materialPath ) ; 
3534			CreateOrReplaceAsset ( font ,  fontPath ) ; 
3635
3736			AssetDatabase . Refresh ( ) ; 
37+ 			return  true ; 
3838		} 
3939
4040		private  static string  CheckForErrors ( ExecutionData  data ) 
@@ -45,8 +45,8 @@ private static string CheckForErrors(ExecutionData data)
4545			if  ( data . Texture  ==  null )  return  "Texture cannot be null" ; 
4646			if  ( ! data . Texture . isReadable )  return  "Texture must be readable. Set Read/Write Enabled to true inside Texture Properties" ; 
4747
48- 			if  ( data . Characters . Length  !=  data . Cols  *  data . Rows ) 
49- 				return  $ "Characters length ({ data . Characters . Length } ) must be equal to Cols ({ data . Cols } ) * Rows ({ data . Rows } )"; 
48+ 			if  ( data . ValidCharactersCount  !=  data . Cols  *  data . Rows ) 
49+ 				return  $ "Characters length ({ data . ValidCharactersCount } ) must be equal to Cols ({ data . Cols } ) * Rows ({ data . Rows } )"; 
5050
5151			return  null ; 
5252		} 
@@ -60,11 +60,20 @@ private static Material CreateMaterial(string baseName, Texture2D texture)
6060			} ; 
6161		} 
6262
63- 		private  static Font  CreateFontAsset ( string  baseName ,  Material  material ,  ExecutionData  data ) 
63+ 		private  static Font  CreateFontAsset ( string  baseName ,  Material  material ,  ExecutionData  data ,   out   string   error ) 
6464		{ 
65+ 			error  =  null ; 
6566			var  map  =  new  Dictionary < char ,  CharacterProps > ( ) ; 
66- 			foreach  ( var  e  in  data . CustomCharacterProps ) 
67+ 			for  ( var  i  =  0 ;  i  <  data . CustomCharacterProps . Count ;  i ++ ) 
68+ 			{ 
69+ 				var  e  =  data . CustomCharacterProps [ i ] ; 
70+ 				if  ( string . IsNullOrEmpty ( e . Character ) ) 
71+ 				{ 
72+ 					error  =  $ "Character for Custom Character Properties at position { i  +  1 }  is empty"; 
73+ 					return  null ; 
74+ 				} 
6775				map . Add ( e . Character [ 0 ] ,  e ) ; 
76+ 			} 
6877
6978			return  new  Font ( baseName ) 
7079			{ 
@@ -83,16 +92,15 @@ private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<c
8392			int  xMin ,  xMax ,  advance ; 
8493			int  largestAdvance  =  0 ; 
8594
86- 			// horizontal 
8795			for  ( var  row  =  0 ;  row  <  data . Rows ;  row ++ ) 
8896			{ 
8997				for  ( var  col  =  0 ;  col  <  data . Cols ;  col ++ ) 
9098				{ 
9199					var  i  =  data . Orientation  ==  Orientation . Horizontal 
92100						?  ( row  *  data . Cols )  +  col 
93101						:  ( col  *  data . Rows )  +  row ; 
94- 					var  ch  =  data . Characters [ i ] ; 
95- 					if  ( ch  ==  ' '   ||   ch   ==   ' \r '   ||   ch   ==   ' \n ' )  continue ; 
102+ 					var  ch  =  data . ValidCharacters [ i ] ; 
103+ 					if  ( ch  ==  IgnoreCharacter )  continue ; 
96104
97105					GetCharacterBounds ( 
98106						tex :  data . Texture , 
@@ -174,5 +182,67 @@ private static void CreateOrReplaceAsset<T>(T asset, string path) where T : Obje
174182				AssetDatabase . SaveAssets ( ) ; 
175183			} 
176184		} 
185+ 
186+ 		public  static Vector2Int  GuessRowsAndCols ( Texture2D  tex ) 
187+ 		{ 
188+ 			var  rows  =  0 ; 
189+ 			var  cols  =  0 ; 
190+ 
191+ 			uint  state  =  0 ;    // 0 = looking for not transparent, 1 = looking for transparent 
192+ 			bool  foundNonTransparentPixel ; 
193+ 
194+ 			for  ( var  x  =  0 ;  x  <  tex . width ;  x ++ ) 
195+ 			{ 
196+ 				foundNonTransparentPixel  =  false ; 
197+ 				for  ( var  y  =  0 ;  y  <  tex . height ;  y ++ ) 
198+ 				{ 
199+ 					if  ( tex . GetPixel ( x ,  y ) . a  ==  0 )  continue ; 
200+ 					foundNonTransparentPixel  =  true ; 
201+ 					break ; 
202+ 				} 
203+ 
204+ 				if  ( state  ==  0 ) 
205+ 				{ 
206+ 					if  ( foundNonTransparentPixel ) 
207+ 					{ 
208+ 						state  =  1 ; 
209+ 						cols ++ ; 
210+ 					} 
211+ 				} 
212+ 				else 
213+ 				{ 
214+ 					if  ( ! foundNonTransparentPixel ) 
215+ 						state  =  0 ; 
216+ 				} 
217+ 			} 
218+ 
219+ 			state  =  0 ; 
220+ 			for  ( var  y  =  0 ;  y  <  tex . height ;  y ++ ) 
221+ 			{ 
222+ 				foundNonTransparentPixel  =  false ; 
223+ 				for  ( var  x  =  0 ;  x  <  tex . width ;  x ++ ) 
224+ 				{ 
225+ 					if  ( tex . GetPixel ( x ,  y ) . a  ==  0 )  continue ; 
226+ 					foundNonTransparentPixel  =  true ; 
227+ 					break ; 
228+ 				} 
229+ 
230+ 				if  ( state  ==  0 ) 
231+ 				{ 
232+ 					if  ( foundNonTransparentPixel ) 
233+ 					{ 
234+ 						state  =  1 ; 
235+ 						rows ++ ; 
236+ 					} 
237+ 				} 
238+ 				else 
239+ 				{ 
240+ 					if  ( ! foundNonTransparentPixel ) 
241+ 						state  =  0 ; 
242+ 				} 
243+ 			} 
244+ 
245+ 			return  new ( rows ,  cols ) ; 
246+ 		} 
177247	} 
178248} 
0 commit comments