Beispiel #1
0
        internal static CharacterWidth[] For(int count)
        {
            var widths = new CharacterWidth[count];
            for (var i = 0; i < widths.Length; i++)
            {
                widths[i] = new CharacterWidth();
            }

            return widths;
        }
Beispiel #2
0
        internal static CharacterWidth[] For(int count)
        {
            var widths = new CharacterWidth[count];

            for (var i = 0; i < widths.Length; i++)
            {
                widths[i] = new CharacterWidth();
            }

            return(widths);
        }
Beispiel #3
0
 public static string CharacterImageURL(int characterId, CharacterWidth width)
 {
     return(BASE_URL + $"/Character/{characterId}_{((int)width).ToString()}.jpg");
 }
Beispiel #4
0
 public static string CharacterImageURL(int characterId, CharacterWidth width)
 {
     return($"{BASE_URL}/characters/{characterId}/portrait?size={(int)width}");
 }
Beispiel #5
0
        public AsciiArt(string text, FIGletFont font = null, CharacterWidth width = CharacterWidth.Fitted)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (text.Contains(Environment.NewLine))
            {
                throw new ArgumentException($"{nameof(text)} can not contain multi line");
            }

            Text           = text;
            Font           = font ?? FIGletFont.Default;
            CharacterWidth = width;
            Result         = new string[Font.Height];

            if (Text.Length == 0)
            {
                return;
            }

            switch (width)
            {
            case CharacterWidth.Full:
            {
                for (int currentLine = 0; currentLine < Height; currentLine++)
                {
                    StringBuilder lineBuilder = new StringBuilder( );
                    foreach (char currentChar in Text)
                    {
                        lineBuilder.Append(Font.GetCharacter(currentChar, currentLine));
                        lineBuilder.Append(' ');
                    }

                    Result [currentLine] = lineBuilder.ToString( );
                }

                break;
            }

            case CharacterWidth.Fitted:
            {
                for (int currentLine = 0; currentLine < Height; currentLine++)
                {
                    StringBuilder lineBuilder = new StringBuilder( );
                    foreach (char currentChar in Text)
                    {
                        lineBuilder.Append(Font.GetCharacter(currentChar, currentLine));
                    }

                    Result [currentLine] = lineBuilder.ToString( );
                }

                break;
            }

            case CharacterWidth.Smush:
            {
                for (int currentLine = 0; currentLine < Height; currentLine++)
                {
                    StringBuilder lineBuilder = new StringBuilder( );
                    lineBuilder.Append(Font.GetCharacter(Text [0], currentLine));
                    char lastChar = Text [0];
                    for (int currentCharIndex = 1; currentCharIndex < Text.Length; currentCharIndex++)
                    {
                        char   currentChar          = Text [currentCharIndex];
                        string currentCharacterLine = Font.GetCharacter(currentChar, currentLine);
                        if (lastChar != ' ' &&
                            currentChar != ' ')
                        {
                            if (lineBuilder [lineBuilder.Length - 1] == ' ')
                            {
                                lineBuilder [lineBuilder.Length - 1] = currentCharacterLine [0];
                            }
                            lineBuilder.Append(currentCharacterLine.Substring(1));
                        }
                        else
                        {
                            lineBuilder.Append(currentCharacterLine);
                        }
                        lastChar = currentChar;
                    }

                    Result [currentLine] = lineBuilder.ToString( );
                }

                break;
            }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Constructs a text display.
        /// </summary>
        /// <param name="width">Width in characters</param>
        /// <param name="height">Height in characters</param>
        /// <param name="fontFile">Font texture</param>
        /// <param name="convertFont">Enable support for masked fonts</param>
        /// <param name="paletteFile">Palette texture</param>
        public TextDisplay(uint width, uint height, string fontFile = "font.png", bool convertFont = true, string paletteFile = "palette.png")
        {
            if (string.IsNullOrEmpty(fontFile))
            {
                throw new ArgumentNullException("fontFile");
            }

            if (string.IsNullOrEmpty(paletteFile))
            {
                throw new ArgumentNullException("paletteFile");
            }

            Width  = width;
            Height = height;

            _data        = new Image(width, height, Color.Black);
            _dataTexture = new Texture(_data);

            var fontImage = new Image(Path.Combine(DataFolder, fontFile));

            CharacterWidth  = fontImage.Size.X / 16;
            CharacterHeight = fontImage.Size.Y / 16;

            if (convertFont)
            {
                var fontWidth  = fontImage.Size.X;
                var fontHeight = fontImage.Size.Y;
                var fontPixels = fontImage.Pixels;

                fontImage.Dispose();

                // use top left pixel of space as mask color
                var maskX = (32 % 16) * CharacterWidth;
                var maskY = (32 / 16) * CharacterHeight * (fontWidth * 4);
                var maskI = maskX + maskY;
                var maskR = fontPixels[maskI + 0];
                var maskG = fontPixels[maskI + 1];
                var maskB = fontPixels[maskI + 2];
                var maskA = fontPixels[maskI + 3];

                for (int i = 0; i < fontPixels.Length; i += 4)
                {
                    var r = fontPixels[i + 0];
                    var g = fontPixels[i + 1];
                    var b = fontPixels[i + 2];
                    var a = fontPixels[i + 3];

                    if (r == maskR && g == maskG && b == maskB && a == maskA)
                    {
                        // mask color, set to transparent
                        fontPixels[i + 3] = 0;
                    }
                    else
                    {
                        // set alpha channel to average of rgb
                        var level = (r + b + g) / 3f;
                        var alpha = a / 256f;

                        fontPixels[i + 3] = (byte)(level * alpha);
                    }
                }

                fontImage = new Image(fontWidth, fontHeight, fontPixels);
                //fontImage.SaveToFile("result.png");
            }

            _fontTexture = new Texture(fontImage);

            _palette        = new Image(Path.Combine(DataFolder, paletteFile));
            _paletteTexture = new Texture(_palette);

            _display    = new VertexArray(PrimitiveType.Quads, 4);
            _display[0] = new Vertex(new Vector2f(0, 0), new Vector2f(0, 0));
            _display[1] = new Vertex(new Vector2f(width * CharacterWidth, 0), new Vector2f(1, 0));
            _display[2] = new Vertex(new Vector2f(width * CharacterWidth, height * CharacterHeight), new Vector2f(1, 1));
            _display[3] = new Vertex(new Vector2f(0, height * CharacterHeight), new Vector2f(0, 1));

            var displayVertexSource   = File.ReadAllText(Path.Combine(DataFolder, "texter.vert"));
            var displayFragmentSource = File.ReadAllText(Path.Combine(DataFolder, "texter.frag"))
                                        .Replace("#W#", CharacterWidth.ToString("D"))
                                        .Replace("#H#", CharacterHeight.ToString("D"));

            _renderer = Shader.FromString(displayVertexSource, displayFragmentSource);
            _renderer.SetParameter("data", _dataTexture);
            _renderer.SetParameter("dataSize", width, height);
            _renderer.SetParameter("font", _fontTexture);
            _renderer.SetParameter("palette", _paletteTexture);
        }