33 using System ;
44 using System . Text ;
55 using System . Text . RegularExpressions ;
6+ using System . Collections . Generic ;
7+ using System . Linq ;
8+ using System . Drawing ;
9+
610 public class Figlet
711 {
812 private readonly FigletFont font ;
@@ -19,23 +23,69 @@ public Figlet(FigletFont font)
1923 this . font = font ;
2024 }
2125
22- public string ToAscii ( string value )
26+ public StyledString ToAscii ( string value )
2327 {
2428 if ( value == null ) { throw new ArgumentNullException ( nameof ( value ) ) ; }
2529
2630 StringBuilder stringBuilder = new StringBuilder ( ) ;
2731
28- for ( int i = 1 ; i <= font . Height ; i ++ )
32+ int stringWidth = GetStringWidth ( font , value ) ;
33+ char [ , ] characterGeometry = new char [ font . Height + 1 , stringWidth ] ;
34+ int [ , ] characterIndexGeometry = new int [ font . Height + 1 , stringWidth ] ;
35+ Color [ , ] colorGeometry = new Color [ font . Height + 1 , stringWidth ] ;
36+
37+ for ( int line = 1 ; line <= font . Height ; line ++ )
2938 {
30- foreach ( var character in value )
39+ int runningWidthTotal = 0 ;
40+
41+ for ( int c = 0 ; c < value . Length ; c ++ )
3142 {
32- stringBuilder . Append ( GetCharacter ( this . font , character , i ) ) ;
43+ char character = value [ c ] ;
44+ string fragment = GetCharacter ( this . font , character , line ) ;
45+
46+ stringBuilder . Append ( fragment ) ;
47+ CalculateCharacterGeometries ( fragment , c , runningWidthTotal , line , characterGeometry , characterIndexGeometry ) ;
48+
49+ runningWidthTotal += fragment . Length ;
3350 }
3451
3552 stringBuilder . AppendLine ( ) ;
3653 }
3754
38- return stringBuilder . ToString ( ) ;
55+ StyledString styledString = new StyledString ( value , stringBuilder . ToString ( ) ) ;
56+ styledString . CharacterGeometry = characterGeometry ;
57+ styledString . CharacterIndexGeometry = characterIndexGeometry ;
58+ styledString . ColorGeometry = colorGeometry ;
59+
60+ return styledString ;
61+ }
62+
63+ private static void CalculateCharacterGeometries ( string fragment , int characterIndex , int runningWidthTotal , int line , char [ , ] charGeometry , int [ , ] indexGeometry )
64+ {
65+ for ( int i = runningWidthTotal ; i < runningWidthTotal + fragment . Length ; i ++ )
66+ {
67+ charGeometry [ line , i ] = fragment [ i - runningWidthTotal ] ;
68+ indexGeometry [ line , i ] = characterIndex ;
69+ }
70+ }
71+
72+ private static int GetStringWidth ( FigletFont font , string value )
73+ {
74+ List < int > charWidths = new List < int > ( ) ;
75+ foreach ( var character in value )
76+ {
77+ int charWidth = 0 ;
78+ for ( int line = 1 ; line <= font . Height ; line ++ )
79+ {
80+ string figletCharacter = GetCharacter ( font , character , line ) ;
81+
82+ charWidth = figletCharacter . Length > charWidth ? figletCharacter . Length : charWidth ;
83+ }
84+
85+ charWidths . Add ( charWidth ) ;
86+ }
87+
88+ return charWidths . Sum ( ) ;
3989 }
4090
4191 private static string GetCharacter ( FigletFont font , char character , int line )
0 commit comments