<commit_before>//     The contents of this file are subject to the Mozilla Public License
//     Version 1.1 (the "License"); you may not use this file except in
//     compliance with the License. You may obtain a copy of the License at
//     http://www.mozilla.org/MPL/
//
//     Software distributed under the License is distributed on an "AS IS"
//     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//     License for the specific language governing rights and limitations
//     under the License.
//
//     The Original Code is AgateLib.
//
//     The Initial Developer of the Original Code is <NAME>.
//     Portions created by <NAME> are Copyright (C) 2006.
//     All Rights Reserved.
//
//     Contributor(s): <NAME>
//
using System;
using System.Collections.Generic;
using Drawing = System.Drawing;
using System.Text;
using System.Xml;

using ERY.AgateLib.Geometry;

namespace ERY.AgateLib.ImplBase
{
    /// <summary>
    /// Provides a basic implementation for the use of non-system fonts provided
    /// as a bitmap.
    /// 
    /// To construct a bitmap font, call the appropriate static FontSurface method.
    /// </summary>
    public class BitmapFontImpl : FontSurfaceImpl
    {
        Surface mSurface;

        /// <summary>
        /// Stores source rectangles for all characters.
        /// </summary>
        Dictionary<char, RectangleF> mSrcRects = new Dictionary<char, RectangleF>();

        int mCharHeight;
        double mAverageCharWidth;

        /// <summary>
        /// Constructs a BitmapFontImpl, assuming the characters in the given file
        /// are all the same size, and are in their ASCII order.
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="characterSize"></param>
        public BitmapFontImpl(string filename, Size characterSize)
        {
            mSurface = new Surface(filename);
            mCharHeight = characterSize.Height;

            ExtractMonoSpaceAsciiFont(characterSize);
        }

        /// <summary>
        /// Constructs a BitmapFontImpl, taking the passed surface as the source for
        /// the characters.  The source rectangles for each character are passed in.
        /// </summary>
        /// <param name="surface">Surface which contains the image data for the font glyphs.</param>
        /// <param name="srcRects">An object implementing the IDictionary&lt;char, Rectangle&gt;
        /// interface, containing the source rectangles on the surface for each font glyph.</param>
        public BitmapFontImpl(Surface surface, IDictionary<char, RectangleF> srcRects)
        {
            float maxHeight = 0;

            foreach (KeyValuePair<char, RectangleF> kvp in srcRects)
            {
                mSrcRects.Add(kvp.Key, kvp.Value);

                if (kvp.Value.Height > maxHeight)
                    maxHeight = kvp.Value.Height;
            }

            mCharHeight = (int)Math.Ceiling(maxHeight);
            mSurface = surface;
        }

        /// <summary>
        /// Disposes of the object.
        /// </summary>
        public override void Dispose()
        {
            mSurface.Dispose();
        }

        private void ExtractMonoSpaceAsciiFont(Size characterSize)
        {
            int x = 0;
            int y = 0;
            char val = '\0';

            while (y + characterSize.Height <= mSurface.SurfaceHeight)
            {
                RectangleF src = new RectangleF(x, y, characterSize.Width, characterSize.Height);

                mSrcRects[val] = src;

                val++;
                x += characterSize.Width;

                if (x + characterSize.Width > mSurface.SurfaceWidth)
                {
                    y += characterSize.Height;
                    x = 0;
                }
            }

            CalcAverageCharWidth();
        }

        /// <summary>
        /// Saves the bitmap font to two files, an image file which contains the
        /// binary image data, and an XML file which contains all the glyph definitions.
        /// </summary>
        /// <param name="imageFilename"></param>
        /// <param name="xmlFileName"></param>
        public void Save(string imageFilename, string xmlFileName)
        {
            XmlDocument doc = new XmlDocument();

            Save(imageFilename, doc, doc);

            doc.Save(xmlFileName);
        }
        private void Save(string imageFilename, XmlNode node, XmlDocument doc)
        {
            mSurface.SaveTo(imageFilename);

            XmlNode root = doc.CreateElement("Font");

            foreach (char glyph in mSrcRects.Keys)
            {
                XmlNode current = doc.CreateElement("Glyph");

                XmlAttribute ch = doc.CreateAttribute("Char");
                ch.Value = glyph.ToString();

                XmlAttribute rect = doc.CreateAttribute("Source");
                rect.Value = mSrcRects[glyph].ToString();

                current.Attributes.Append(ch);
                current.Attributes.Append(rect);

                root.AppendChild(current);
            }

            node.AppendChild(root);
        }

        /// <summary>
        /// Creates a bitmap font by loading an OS font, and drawing it to 
        /// a bitmap to use as a Surface object.  You should only use this method
        /// if writing a driver.
        /// </summary>
        /// <seealso cref="FontSurface.BitmapFont(string, float, FontStyle)"/>
        /// <param name="fontFamily"></param><commit_msg>Changed XML serializer to write ascii value instead of the character to the file.<commit_after>//     The contents of this file are subject to the Mozilla Public License
//     Version 1.1 (the "License"); you may not use this file except in
//     compliance with the License. You may obtain a copy of the License at
//     http://www.mozilla.org/MPL/
//
//     Software distributed under the License is distributed on an "AS IS"
//     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//     License for the specific language governing rights and limitations
//     under the License.
//
//     The Original Code is AgateLib.
//
//     The Initial Developer of the Original Code is <NAME>.
//     Portions created by <NAME> are Copyright (C) 2006.
//     All Rights Reserved.
//
//     Contributor(s): <NAME>
//
using System;
using System.Collections.Generic;
using Drawing = System.Drawing;
using System.Text;
using System.Xml;

using ERY.AgateLib.Geometry;

namespace ERY.AgateLib.ImplBase
{
    /// <summary>
    /// Provides a basic implementation for the use of non-system fonts provided
    /// as a bitmap.
    /// 
    /// To construct a bitmap font, call the appropriate static FontSurface method.
    /// </summary>
    public class BitmapFontImpl : FontSurfaceImpl
    {
        Surface mSurface;

        /// <summary>
        /// Stores source rectangles for all characters.
        /// </summary>
        Dictionary<char, RectangleF> mSrcRects = new Dictionary<char, RectangleF>();

        int mCharHeight;
        double mAverageCharWidth;

        /// <summary>
        /// Constructs a BitmapFontImpl, assuming the characters in the given file
        /// are all the same size, and are in their ASCII order.
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="characterSize"></param>
        public BitmapFontImpl(string filename, Size characterSize)
        {
            mSurface = new Surface(filename);
            mCharHeight = characterSize.Height;

            ExtractMonoSpaceAsciiFont(characterSize);
        }

        /// <summary>
        /// Constructs a BitmapFontImpl, taking the passed surface as the source for
        /// the characters.  The source rectangles for each character are passed in.
        /// </summary>
        /// <param name="surface">Surface which contains the image data for the font glyphs.</param>
        /// <param name="srcRects">An object implementing the IDictionary&lt;char, Rectangle&gt;
        /// interface, containing the source rectangles on the surface for each font glyph.</param>
        public BitmapFontImpl(Surface surface, IDictionary<char, RectangleF> srcRects)
        {
            float maxHeight = 0;

            foreach (KeyValuePair<char, RectangleF> kvp in srcRects)
            {
                mSrcRects.Add(kvp.Key, kvp.Value);

                if (kvp.Value.Height > maxHeight)
                    maxHeight = kvp.Value.Height;
            }

            mCharHeight = (int)Math.Ceiling(maxHeight);
            mSurface = surface;
        }

        /// <summary>
        /// Disposes of the object.
        /// </summary>
        public override void Dispose()
        {
            mSurface.Dispose();
        }

        private void ExtractMonoSpaceAsciiFont(Size characterSize)
        {
            int x = 0;
            int y = 0;
            char val = '\0';

            while (y + characterSize.Height <= mSurface.SurfaceHeight)
            {
                RectangleF src = new RectangleF(x, y, characterSize.Width, characterSize.Height);

                mSrcRects[val] = src;

                val++;
                x += characterSize.Width;

                if (x + characterSize.Width > mSurface.SurfaceWidth)
                {
                    y += characterSize.Height;
                    x = 0;
                }
            }

            CalcAverageCharWidth();
        }

        /// <summary>
        /// Saves the bitmap font to two files, an image file which contains the
        /// binary image data, and an XML file which contains all the glyph definitions.
        /// </summary>
        /// <param name="imageFilename"></param>
        /// <param name="xmlFileName"></param>
        public void Save(string imageFilename, string xmlFileName)
        {
            XmlDocument doc = new XmlDocument();

            Save(imageFilename, doc, doc);

            doc.Save(xmlFileName);
        }
        private void Save(string imageFilename, XmlNode node, XmlDocument doc)
        {
            mSurface.SaveTo(imageFilename);

            XmlNode root = doc.CreateElement("Font");

            foreach (char glyph in mSrcRects.Keys)
            {
                XmlNode current = doc.CreateElement("Glyph");

                XmlAttribute ch = doc.CreateAttribute("Char");
                ch.Value = ((int)glyph).ToString();
                
                XmlAttribute rect = doc.CreateAttribute("Source");
                rect.Value = mSrcRects[glyph].ToString();

                current.Attributes.Append(ch);
                current.Attributes.Append(rect);

                root.AppendChild(current);
            }

            node.AppendChild(root);
        }

        /// <summary>
        /// Creates a bitmap font by loading an OS font, and drawing it to 
        /// a bitmap to use as a Surface object.  You should only use this method
        /// if writing a driver.
        /// </summary>
        /// <seealso cref="FontSurface.BitmapFont(string, float, FontStyle)"/>
        /// <param name="fontFamily"></param>
{'fraction_non_alphanumeric': 0.10183938986092418, 'fraction_numerical': 0.0019739793629430236, 'mean_word_length': 1.7816321437484401, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1759504', 'n_tokens_mistral': 2918, 'n_tokens_neox': 2731, 'n_words': 918}