private static void WriteAsciiInColorStyled(string trailer, StyledString target, StyleSheet styleSheet)
        {
            var annotator = new TextAnnotator(styleSheet);
            var annotationMap =
                annotator.GetAnnotationMap(target
                    .AbstractValue); // Should eventually be target.AsStyledString() everywhere...?

            PopulateColorGeometry(annotationMap, target);

            MapToScreen(target, trailer);
        }
Exemple #2
0
        public StyledString ToAscii(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (Encoding.UTF8.GetByteCount(value) != value.Length)
            {
                throw new ArgumentException("String contains non-ascii characters");
            }

            var stringBuilder = new StringBuilder();

            var stringWidth            = GetStringWidth(font, value);
            var characterGeometry      = new char[font.Height + 1, stringWidth];
            var characterIndexGeometry = new int[font.Height + 1, stringWidth];
            var colorGeometry          = new Color[font.Height + 1, stringWidth];

            for (var line = 1; line <= font.Height; line++)
            {
                var runningWidthTotal = 0;

                for (var c = 0; c < value.Length; c++)
                {
                    var character = value[c];
                    var fragment  = GetCharacter(font, character, line);

                    stringBuilder.Append(fragment);
                    CalculateCharacterGeometries(fragment, c, runningWidthTotal, line, characterGeometry,
                                                 characterIndexGeometry);

                    runningWidthTotal += fragment.Length;
                }

                stringBuilder.AppendLine();
            }

            var styledString = new StyledString(value, stringBuilder.ToString());

            styledString.CharacterGeometry      = characterGeometry;
            styledString.CharacterIndexGeometry = characterIndexGeometry;
            styledString.ColorGeometry          = colorGeometry;

            return(styledString);
        }
        private static void MapToScreen(StyledString styledString, string trailer)
        {
            var oldSystemColor = System.Console.ForegroundColor;
            var rowLength = styledString.CharacterGeometry.GetLength(0);
            var columnLength = styledString.CharacterGeometry.GetLength(1);
            for (var row = 0; row < rowLength; row++)
            for (var column = 0; column < columnLength; column++)
            {
                System.Console.ForegroundColor = colorManager.GetConsoleColor(styledString.ColorGeometry[row, column]);

                if (row == rowLength - 1 && column == columnLength - 1)
                    System.Console.Write(styledString.CharacterGeometry[row, column] + trailer);
                else if (column == columnLength - 1)
                    System.Console.Write(styledString.CharacterGeometry[row, column] + "\r\n");
                else
                    System.Console.Write(styledString.CharacterGeometry[row, column]);
            }

            System.Console.ForegroundColor = oldSystemColor;
        }
        private static void PopulateColorGeometry(IEnumerable<KeyValuePair<string, Color>> annotationMap,
            StyledString target)
        {
            var abstractCharCount = 0;
            foreach (var fragment in annotationMap)
                for (var i = 0; i < fragment.Key.Length; i++)
                {
                    // This will run O(n^2) times...but with DP, could be O(n).
                    // Just need to keep a third array that keeps track of each abstract char's width, so you never iterate past that.
                    // This third array would be one-dimensional.

                    var rowLength = target.CharacterIndexGeometry.GetLength(0);
                    var columnLength = target.CharacterIndexGeometry.GetLength(1);
                    for (var row = 0; row < rowLength; row++)
                    for (var column = 0; column < columnLength; column++)
                        if (target.CharacterIndexGeometry[row, column] == abstractCharCount)
                            target.ColorGeometry[row, column] = fragment.Value;

                    abstractCharCount++;
                }
        }
 public static void WriteLineStyled(StyledString value, StyleSheet styleSheet)
 {
     WriteAsciiInColorStyled(WRITELINE_TRAILER, value, styleSheet);
 }