<commit_before>﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace SadConsole
{
    public class Font
    {
        public string Name { get; set; }

        public string FilePath { get; set; }

        public int CellHeight { get; set; }

        public int CellWidth { get; set; }

        public int CellPadding { get; set; }

        public bool IsDefault { get; set; }

        [IgnoreDataMember]
        public int Rows { get { return Image.Height / (CellHeight + CellPadding); } }

        [IgnoreDataMember]
        public Texture2D Image { get; private set; }

        [IgnoreDataMember]
        public Point[] CharacterIndexPoints;

        [IgnoreDataMember]
        public Rectangle[] CharacterIndexRects;

        #region Constructors
        public Font() { }

        public Font(string name, XElement fontXmlNode, GraphicsDevice device)
        {
            XAttribute xwidth = fontXmlNode.Attribute("width");
            XAttribute xheight = fontXmlNode.Attribute("height");
            string filename = fontXmlNode.Value;

            if (xwidth == null || xheight == null)
                throw new Exception("Width or Height attribute for font is missing");

            int width;
            int height;

            if (!int.TryParse(xwidth.Value, out width))
                throw new Exception("Width value is invalid: " + xwidth.Value);
            if (!int.TryParse(xheight.Value, out height))
                throw new Exception("Height value is invalid: " + xheight.Value);

            FilePath = filename;
            Name = name;
            CellWidth = width;
            CellHeight = height;

            Generate();
        }
        #endregion

        #region Methods
        /// <summary>
        /// After the font has been loaded, (with the FilePath, CellHeight, and CellWidth fields filled out) this method will create the actual texture.
        /// </summary>
        public void Generate()
        {
            System.IO.Stream fontStream = System.IO.File.OpenRead(FilePath);
            
            this.Image = Texture2D.FromStream(Engine.Device, fontStream);
            fontStream.Dispose();

            ConfigureRects();
        }

        public void ConfigureRects()
        {
            CharacterIndexRects = new Rectangle[Rows * Engine.FontColumns];
            CharacterIndexPoints = new Point[CharacterIndexRects.Length];

            for (int i = 0; i < CharacterIndexRects.Length; i++)
            {
                CharacterIndexPoints[i] = new Point(i % Engine.FontColumns, i / Engine.FontColumns);
                if (CellPadding != 0)
                    CharacterIndexRects[i] = new Rectangle((CharacterIndexPoints[i].X * CellWidth) + ((CharacterIndexPoints[i].X + 1) * CellPadding),
                                                           (CharacterIndexPoints[i].Y * CellHeight) + ((CharacterIndexPoints[i].Y + 1) * CellPadding), CellWidth, CellHeight);
                else
                    CharacterIndexRects[i] = new Rectangle(CharacterIndexPoints[i].X * CellWidth, CharacterIndexPoints[i].Y * CellHeight, CellWidth, CellHeight);
            }
        }

        private void GetImageMask()
        {
            Texture2D texture = new Texture2D(Engine.Device, Image.Width, Image.Height,
                                                false, SurfaceFormat.Color);
            Color[] newPixels = new Color[texture.Width * texture.Height];
            Color[] oldPixels = new Color[texture.Width * texture.Height];
            texture.GetData<Color>(newPixels);
            Image.GetData<Color>(oldPixels);
        }

        /// <summary>
        /// Resizes the graphics device manager to this font cell size.
        /// </summary>
        /// <param name="manager">Graphics device manager to resize.</param>
        /// <param name="width">The width in cell count.</param>
        /// <param name="height">The height in cell count.</param>
        /// <param name="additionalWidth">Additional pixel width to add to the resize.</param>
        /// <param name="additionalHeight">Additional pixel height to add to the resize.</param>
        public void ResizeGraphicsDeviceManager(GraphicsDeviceManager manager, int width, int height, int additionalWidth, int additionalHeight)
        {
            manager.PreferredBackBufferWidth = (CellWidth * width) + additionalWidth;
            manager.PreferredBackBufferHeight = (CellHeight * height) + additionalHeight;
            manager.ApplyChanges();

            Engine.WindowWidth = manager.PreferredBackBufferWidth;
            Engine.WindowHeight = manager.PreferredBackBufferHeight;
        }

        [OnDeserialized]
        private void AfterDeserialized(System.Runtime.Serialization.StreamingContext context)
        {
            Generate();
        }
        #endregion
    }
}
<commit_msg>Make texture loading nicer in Font.<commit_after>﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace SadConsole
{
    public class Font
    {
        public string Name { get; set; }

        public string FilePath { get; set; }

        public int CellHeight { get; set; }

        public int CellWidth { get; set; }

        public int CellPadding { get; set; }

        public bool IsDefault { get; set; }

        [IgnoreDataMember]
        public int Rows { get { return Image.Height / (CellHeight + CellPadding); } }

        [IgnoreDataMember]
        public Texture2D Image { get; private set; }

        [IgnoreDataMember]
        public Point[] CharacterIndexPoints;

        [IgnoreDataMember]
        public Rectangle[] CharacterIndexRects;

        #region Constructors
        public Font() { }

        public Font(string name, XElement fontXmlNode, GraphicsDevice device)
        {
            XAttribute xwidth = fontXmlNode.Attribute("width");
            XAttribute xheight = fontXmlNode.Attribute("height");
            string filename = fontXmlNode.Value;

            if (xwidth == null || xheight == null)
                throw new Exception("Width or Height attribute for font is missing");

            int width;
            int height;

            if (!int.TryParse(xwidth.Value, out width))
                throw new Exception("Width value is invalid: " + xwidth.Value);
            if (!int.TryParse(xheight.Value, out height))
                throw new Exception("Height value is invalid: " + xheight.Value);

            FilePath = filename;
            Name = name;
            CellWidth = width;
            CellHeight = height;

            Generate();
        }
        #endregion

        #region Methods
        /// <summary>
        /// After the font has been loaded, (with the FilePath, CellHeight, and CellWidth fields filled out) this method will create the actual texture.
        /// </summary>
        public void Generate()
        {
            using (System.IO.Stream fontStream = System.IO.File.OpenRead(FilePath))
            {
                Image = Texture2D.FromStream(Engine.Device, fontStream);
            }

            ConfigureRects();
        }

        public void ConfigureRects()
        {
            CharacterIndexRects = new Rectangle[Rows * Engine.FontColumns];
            CharacterIndexPoints = new Point[CharacterIndexRects.Length];

            for (int i = 0; i < CharacterIndexRects.Length; i++)
            {
                CharacterIndexPoints[i] = new Point(i % Engine.FontColumns, i / Engine.FontColumns);
                if (CellPadding != 0)
                    CharacterIndexRects[i] = new Rectangle((CharacterIndexPoints[i].X * CellWidth) + ((CharacterIndexPoints[i].X + 1) * CellPadding),
                                                           (CharacterIndexPoints[i].Y * CellHeight) + ((CharacterIndexPoints[i].Y + 1) * CellPadding), CellWidth, CellHeight);
                else
                    CharacterIndexRects[i] = new Rectangle(CharacterIndexPoints[i].X * CellWidth, CharacterIndexPoints[i].Y * CellHeight, CellWidth, CellHeight);
            }
        }

        private void GetImageMask()
        {
            Texture2D texture = new Texture2D(Engine.Device, Image.Width, Image.Height,
                                                false, SurfaceFormat.Color);
            Color[] newPixels = new Color[texture.Width * texture.Height];
            Color[] oldPixels = new Color[texture.Width * texture.Height];
            texture.GetData<Color>(newPixels);
            Image.GetData<Color>(oldPixels);
        }

        /// <summary>
        /// Resizes the graphics device manager to this font cell size.
        /// </summary>
        /// <param name="manager">Graphics device manager to resize.</param>
        /// <param name="width">The width in cell count.</param>
        /// <param name="height">The height in cell count.</param>
        /// <param name="additionalWidth">Additional pixel width to add to the resize.</param>
        /// <param name="additionalHeight">Additional pixel height to add to the resize.</param>
        public void ResizeGraphicsDeviceManager(GraphicsDeviceManager manager, int width, int height, int additionalWidth, int additionalHeight)
        {
            manager.PreferredBackBufferWidth = (CellWidth * width) + additionalWidth;
            manager.PreferredBackBufferHeight = (CellHeight * height) + additionalHeight;
            manager.ApplyChanges();

            Engine.WindowWidth = manager.PreferredBackBufferWidth;
            Engine.WindowHeight = manager.PreferredBackBufferHeight;
        }

        [OnDeserialized]
        private void AfterDeserialized(System.Runtime.Serialization.StreamingContext context)
        {
            Generate();
        }
        #endregion
    }
}

{'fraction_non_alphanumeric': 0.09638919793668453, 'fraction_numerical': 0.0016182866390209367, 'mean_word_length': 2.044334975369458, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3963799', 'n_tokens_mistral': 2472, 'n_tokens_neox': 2381, 'n_words': 663}