Defines an ASCII font using a matrix of boolean values.
Exemplo n.º 1
0
        /// <summary>
        /// Converts a font definition string to a raster font.
        /// </summary>
        /// <param name="source">The string to get font data from.</param>
        /// <returns></returns>
        public static RasterFont FromString(string source)
        {
            try
            {
                StringReader reader = new StringReader(source);
                string       head   = reader.ReadLine().Trim();
                var          m      = Regex.Match(head, @"(?<cx>\d+)x(?<cy>\d+)");
                RasterFont   font   = null;
                if (!m.Success)
                {
                    return(null);
                }
                else
                {
                    font = new RasterFont(Convert.ToInt32(m.Groups["cx"].Value), Convert.ToInt32(m.Groups["cy"].Value));
                }

                for (int i = 0; i < 256 && reader.Peek() != -1; i++)
                {
                    font.glyphs[i] = new bool[font.CharWidth][];
                    for (int j = 0; j < font.CharHeight; j++)
                    {
                        string line = reader.ReadLine();
                        font.glyphs[i][j] = line.Trim().ToCharArray().Select <char, bool>(c => c == '+').ToArray();
                    }
                }

                return(font);
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts a font definition string to a raster font.
        /// </summary>
        /// <param name="source">The string to get font data from.</param>
        /// <returns></returns>
        public static RasterFont FromString(string source)
        {
            try
            {
                StringReader reader = new StringReader(source);
                string head = reader.ReadLine().Trim();
                var m = Regex.Match(head, @"(?<cx>\d+)x(?<cy>\d+)");
                RasterFont font = null;
                if (!m.Success)
                {
                    return null;
                }
                else
                {
                    font = new RasterFont(Convert.ToInt32(m.Groups["cx"].Value), Convert.ToInt32(m.Groups["cy"].Value));
                }

                for (int i = 0; i < 256 && reader.Peek() != -1; i++)
                {
                    font.glyphs[i] = new bool[font.CharWidth][];
                    for (int j = 0; j < font.CharHeight; j++)
                    {
                        string line = reader.ReadLine();
                        font.glyphs[i][j] = line.Trim().ToCharArray().Select<char, bool>(c => c == '+').ToArray();
                    }
                }

                return font;
            }
            catch
            {
                return null;
            }
        }