<commit_before>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace CocosSharp
{

    public enum GlyphCollection 
    {
        Dynamic,
        NEHE,
        Ascii,
        Custom
    };

    // font attributes
    internal struct CCFontDefinition
    {

        public string FontName;
        public int FontSize;
        public CCTextAlignment Alignment;
        public CCVerticalTextAlignment LineAlignment;
        public CCSize Dimensions;
        public CCColor3B FontFillColor;
        public byte FontAlpha;
        public CCLabelLineBreak LineBreak;
        public bool isShouldAntialias;
    };



    internal struct LetterInfo
    {
        public CCFontLetterDefinition Definition;

        public CCPoint Position;
        public CCSize  ContentSize;
        public int   AtlasIndex;
    };

    [Flags]
    public enum CCLabelFormatFlags {
        Unknown               = 0x0001,
        SpriteFont            = 0x0002,
        BitmapFont            = 0x0004,
        CharacterMap          = 0x0008,
        SystemFont            = 0x0010,
    }

    public enum  CCLabelLineBreak {
        None              = 0,
        Character         = 1,
        Word              = 2,
    }

    public sealed class CCLabelFormat : IDisposable
    {

        CCLabelFormatFlags formatFlags = CCLabelFormatFlags.Unknown;


        public CCLabelFormat ()
        {
            LineBreaking = CCLabelLineBreak.Word;
        }

        public CCLabelFormat (CCLabelFormat format)
        {
            if (format == null)
                throw new ArgumentNullException ("format");

            Alignment = format.Alignment;
            LineAlignment = format.LineAlignment;
            FormatFlags = format.FormatFlags;
        }

        public CCLabelFormat(CCLabelFormatFlags options) : this()
        {
            formatFlags = options;
        }

        ~CCLabelFormat ()
        {
            Dispose (false);
        }


        public CCTextAlignment Alignment {
            get; set;
        }

        public CCVerticalTextAlignment LineAlignment { get; set; }

        public CCLabelLineBreak LineBreaking { get; set; }

        public object Clone()
        {
            return new CCLabelFormat (this);
        }

        public void Dispose ()
        {
            Dispose (true);
            System.GC.SuppressFinalize (this);
        }

        void Dispose (bool disposing)
        {
        }

        public static CCLabelFormat BitMapFont
        {
            get {
                return new CCLabelFormat () { FormatFlags = CCLabelFormatFlags.BitmapFont };
            }
        }

        public static CCLabelFormat SystemFont 
        {
            get {
                return new CCLabelFormat () { FormatFlags = CCLabelFormatFlags.SystemFont };
            }
        }

        public static CCLabelFormat SpriteFont 
        {
            get {
                return new CCLabelFormat () { FormatFlags = CCLabelFormatFlags.SpriteFont };
            }
        }


        public CCLabelFormatFlags FormatFlags {
            get {               
                return formatFlags;
            }

            set {
                formatFlags = value;
            }
        }

    }


    [Flags]
    public enum CCLabelType 
    {
        SpriteFont,
        BitMapFont,
        CharacterMap,
        SystemFont
    };

    public partial class CCLabel : CCNode, ICCTextContainer
    {

        const int defaultSpriteBatchCapacity = 29;

        internal static Dictionary<string, CCBMFontConfiguration> fontConfigurations = new Dictionary<string, CCBMFontConfiguration>();

        protected CCLabelLineBreak lineBreak;
        protected CCTextAlignment horzAlignment = CCTextAlignment.Center;
        protected CCVerticalTextAlignment vertAlignment = CCVerticalTextAlignment.Top;
        internal CCBMFontConfiguration FontConfiguration { get; set; }
        protected string fntConfigFile;
        protected string labelText;

        protected CCPoint ImageOffset { get; set; }
        protected CCSize labelDimensions;
        protected bool IsDirty { get; set; }
        public CCTextureAtlas TextureAtlas { get ; private set; }
        public CCRawList<CCSprite> Descendants { get; private set; }

        protected bool isColorModifiedByOpacity = false;

        public CCBlendFunc BlendFunc { get; set; }

        public CCLabelType LabelType { get; protected internal set; }
        private CCFontAtlas fontAtlas;
        private List<LetterInfo> lettersInfo = new List<LetterInfo>();

        //! used for optimization
        CCSprite reusedLetter;
        CCRect reusedRect;

        // System font
        bool systemFontDirty;
        string systemFont;
        float systemFontSize;

        float lineHeight = 0;
        float additionalKerning = 0;

        CCLabelFormat labelFormat;

        // Static properties

        public static float DefaultTexelToContentSizeRatio
        {
            set { DefaultTexelToContentSizeRatios = new CCSize(value, value); }
        }

        public static CCSize DefaultTexelToContentSizeRatios { get; set; }


        // Instance properties

        public float LineHeight 
        { 
            get { return lineHeight; } 
            set
            {
                if (LabelType == CCLabelType.SystemFont)
                    CCLog.Log("Not supported system font!");
                
                if (value != lineHeight)
                {
                    lineHeight = value;
                    IsDirty = true;
                }
            }
        }

        public float AdditionalKerning 
        { 
            get { return additionalKerning; } 
            set
            {
                if (LabelType == CCLabelType.SystemFont)
                    CCLog.Log("Not supported system font!");

                if (value != additionalKerning)
                {
                    additionalKerning = value;
                    IsDirty = true;
                }
            }
        }

        public CCLabelFormat LabelFormat
        {
            get { return labelFormat; }
            set
            {
                if (!labelFormat.Equals(value))
                {
                    // TODO: Check label format flags need to be checked so they can not be
                    // changed after being set.
                    labelFormat = value;
                    IsDirty = true;
                }
            }
        }

        public override CCPoint AnchorPoint
        {
            get { return base.AnchorPoint; }
            set
            {
                if (!AnchorPoint.Equals(value))
                {
                    base.AnchorPoint = value;
                    IsDirty = true;
                }
            }
        }

        public override float Scale
        {
            set
            {
                if (!value.Equals(base.ScaleX) || !value.Equals(base.ScaleY)) 
                {
                    base.Scale = value;
                    IsDirty = true;
                }
            }
        }

        public override float ScaleX
        {
            get { return base.ScaleX; }
            set
            {
                if (!value.Equals(base.ScaleX)) 
                {
                    base.ScaleX = value;
                    IsDirty = true;
                }
            }
        }

        public override float ScaleY
        {
            get { return base.ScaleY; }
            set
            {
                if (!value.Equals(base.ScaleY)) 
                {
                    base.ScaleY = value;
                    IsDirty = true;
                }
            }
        }

        public CCTextAlignment HorizontalAlignment
        {
            get { return labelFormat.Alignment; }
            set
            {
                if (labelFormat.Alignment != value)
                {
                    labelFormat.Alignment = value;
                    IsDirty = true;
                }
            }
        }

        public CCVerticalTextAlignment VerticalAlignment
        {
            get { return labelFormat.LineAlignment; }
            set
            {
                if (labelFormat.LineAlignment != value)
                {
                    labelFormat.LineAlignment = value;
                    IsDirty = true;
                }
            }
        }

        public override CCPoint Position
        {
            get { return base.Position; }
            set
            {
                if (base.Position != value)
                {
                    base.Position = value;
                    IsDirty = true;
                }
            }
        }

        public override float PositionX
        {
            get { return base.PositionX; }
            set
            {
                if (base.PositionX != value)
                {
                    base.PositionX = value;
                    IsDirty = true;
                }
            }
        }

        public override float PositionY
        {
            get { return base.PositionY; }
            set
            {
                if (base.PositionY != value)
                {
                    base.PositionY = value;
                    IsDirty = true;
                }
            }
        }

        public override CCSize ContentSize
        {
            get 
            {
                if (IsDirty || systemFontDirty)
                    UpdateContent();
                
                return base.ContentSize; 
            }
            set
            {
                if (base.ContentSize != value)
                {
                    base.ContentSize = value;
                    IsDirty = true;
                }
            }
        }

        public CCSize Dimensions
        {
            get { return labelDimensions; }
            set
            {
                if (labelDimensions != value)
                {
                    labelDimensions = value;
                    IsDirty = true;
                }
            }
        }

        public CCLabelLineBreak LineBreak
        {
            get { return lineBreak; }
            set
            {
                lineBreak = value;
                IsDirty = true;
            }
        }

        public bool IsAntialiased
        {
            get { return Texture.IsAntialiased; }
            set { Texture.IsAntialiased = value; }
        }

        public virtual CCTexture2D Texture
        {
            get { return TextureAtlas.Texture; }
            set
            {
                TextureAtlas.Texture = value;
                UpdateBlendFunc();
            }
        }

        void UpdateBlendFunc()
        {
            if (!TextureAtlas.Texture.HasPremultipliedAlpha)
            {
                BlendFunc = CCBlendFunc.NonPremultiplied;
            }
        }

        public virtual string Text
        {
            get { return labelText; }
            set
            {
                if (labelText != value)
                {
                    labelText = value;
                    IsDirty = true;
                }
            }
        }

        public string SystemFont
        {
            get { return systemFont; }
            set
            {
                if (systemFont != value)
                {
                    systemFont = value;
                    systemFontDirty = true;

                }
            }
        }

        public float SystemFontSize
        {
            get { return systemFontSize; }
            set
            {
                if (systemFontSize != value)
                {
                    systemFontSize = value;
                    systemFontDirty = true;

                }
            }
        }

        public static void PurgeCachedData()
        {
            if (fontConfigurations != null)
            {
                fontConfigurations.Clear();
            }
        }

        #region Constructors

        static CCLabel()
        {
            DefaultTexelToContentSizeRatios = CCSize.One;
        }

        public CCLabel() : this("", "")
        {
        }

        public CCLabel(string str, string fntFile)
            : this(str, fntFile, 0.0f)
        {
        }

        public CCLabel(string str, string fntFile, float size)
            : this(str, fntFile, size, CCTextAlignment.Left)
        {
        }

        public CCLabel(string str, string fntFile, float size, CCTextAlignment alignment)
            : this(str, fntFile, size, alignment, CCPoint.Zero)
        {
        }

        public CCLabel(string str, string fntFile, float size, CCTextAlignment alignment, CCPoint imageOffset) 
            : this(str, fntFile, size, alignment, imageOffset, null)
        {
        }

        public CCLabel(string str, string fntFile, float size, CCTextAlignment alignment, CCPoint imageOffset, CCTexture2D texture)
            : this(str, fntFile, size, alignment, CCVerticalTextAlignment.Top, imageOffset, null)
        {
        }

        public CCLabel(string str, string fntFile, float size, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, 
            CCPoint imageOffset, CCTexture2D texture)
            : this(str, fntFile, size, CCSize.Zero,
                new CCLabelFormat() { Alignment = hAlignment, LineAlignment = vAlignment}, 
                imageOffset, texture)
        {
        }

        public CCLabel(string str, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, 
            CCPoint imageOffset, CCTexture2D texture)
            : this (str, fntFile, dimensions, 
                new CCLabelFormat() { FormatFlags = CCLabelFormatFlags.BitmapFont, Alignment = hAlignment, LineAlignment = vAlignment}, 
                imageOffset, texture)<commit_msg>Add more overriding constructors to CCLabel.  Some examples were not displaying correctly because of modified constructors.<commit_after>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace CocosSharp
{

    public enum GlyphCollection 
    {
        Dynamic,
        NEHE,
        Ascii,
        Custom
    };

    // font attributes
    internal struct CCFontDefinition
    {

        public string FontName;
        public int FontSize;
        public CCTextAlignment Alignment;
        public CCVerticalTextAlignment LineAlignment;
        public CCSize Dimensions;
        public CCColor3B FontFillColor;
        public byte FontAlpha;
        public CCLabelLineBreak LineBreak;
        public bool isShouldAntialias;
    };



    internal struct LetterInfo
    {
        public CCFontLetterDefinition Definition;

        public CCPoint Position;
        public CCSize  ContentSize;
        public int   AtlasIndex;
    };

    [Flags]
    public enum CCLabelFormatFlags {
        Unknown               = 0x0001,
        SpriteFont            = 0x0002,
        BitmapFont            = 0x0004,
        CharacterMap          = 0x0008,
        SystemFont            = 0x0010,
    }

    public enum  CCLabelLineBreak {
        None              = 0,
        Character         = 1,
        Word              = 2,
    }

    public sealed class CCLabelFormat : IDisposable
    {

        CCLabelFormatFlags formatFlags = CCLabelFormatFlags.Unknown;


        public CCLabelFormat ()
        {
            LineBreaking = CCLabelLineBreak.Word;
        }

        public CCLabelFormat (CCLabelFormat format)
        {
            if (format == null)
                throw new ArgumentNullException ("format");

            Alignment = format.Alignment;
            LineAlignment = format.LineAlignment;
            FormatFlags = format.FormatFlags;
        }

        public CCLabelFormat(CCLabelFormatFlags options) : this()
        {
            formatFlags = options;
        }

        ~CCLabelFormat ()
        {
            Dispose (false);
        }


        public CCTextAlignment Alignment {
            get; set;
        }

        public CCVerticalTextAlignment LineAlignment { get; set; }

        public CCLabelLineBreak LineBreaking { get; set; }

        public object Clone()
        {
            return new CCLabelFormat (this);
        }

        public void Dispose ()
        {
            Dispose (true);
            System.GC.SuppressFinalize (this);
        }

        void Dispose (bool disposing)
        {
        }

        public static CCLabelFormat BitMapFont
        {
            get {
                return new CCLabelFormat () { FormatFlags = CCLabelFormatFlags.BitmapFont };
            }
        }

        public static CCLabelFormat SystemFont 
        {
            get {
                return new CCLabelFormat () { FormatFlags = CCLabelFormatFlags.SystemFont };
            }
        }

        public static CCLabelFormat SpriteFont 
        {
            get {
                return new CCLabelFormat () { FormatFlags = CCLabelFormatFlags.SpriteFont };
            }
        }


        public CCLabelFormatFlags FormatFlags {
            get {               
                return formatFlags;
            }

            set {
                formatFlags = value;
            }
        }

    }


    [Flags]
    public enum CCLabelType 
    {
        SpriteFont,
        BitMapFont,
        CharacterMap,
        SystemFont
    };

    public partial class CCLabel : CCNode, ICCTextContainer
    {

        const int defaultSpriteBatchCapacity = 29;

        internal static Dictionary<string, CCBMFontConfiguration> fontConfigurations = new Dictionary<string, CCBMFontConfiguration>();

        protected CCLabelLineBreak lineBreak;
        protected CCTextAlignment horzAlignment = CCTextAlignment.Center;
        protected CCVerticalTextAlignment vertAlignment = CCVerticalTextAlignment.Top;
        internal CCBMFontConfiguration FontConfiguration { get; set; }
        protected string fntConfigFile;
        protected string labelText;

        protected CCPoint ImageOffset { get; set; }
        protected CCSize labelDimensions;
        protected bool IsDirty { get; set; }
        public CCTextureAtlas TextureAtlas { get ; private set; }
        public CCRawList<CCSprite> Descendants { get; private set; }

        protected bool isColorModifiedByOpacity = false;

        public CCBlendFunc BlendFunc { get; set; }

        public CCLabelType LabelType { get; protected internal set; }
        private CCFontAtlas fontAtlas;
        private List<LetterInfo> lettersInfo = new List<LetterInfo>();

        //! used for optimization
        CCSprite reusedLetter;
        CCRect reusedRect;

        // System font
        bool systemFontDirty;
        string systemFont;
        float systemFontSize;

        float lineHeight = 0;
        float additionalKerning = 0;

        CCLabelFormat labelFormat;

        // Static properties

        public static float DefaultTexelToContentSizeRatio
        {
            set { DefaultTexelToContentSizeRatios = new CCSize(value, value); }
        }

        public static CCSize DefaultTexelToContentSizeRatios { get; set; }


        // Instance properties

        public float LineHeight 
        { 
            get { return lineHeight; } 
            set
            {
                if (LabelType == CCLabelType.SystemFont)
                    CCLog.Log("Not supported system font!");
                
                if (value != lineHeight)
                {
                    lineHeight = value;
                    IsDirty = true;
                }
            }
        }

        public float AdditionalKerning 
        { 
            get { return additionalKerning; } 
            set
            {
                if (LabelType == CCLabelType.SystemFont)
                    CCLog.Log("Not supported system font!");

                if (value != additionalKerning)
                {
                    additionalKerning = value;
                    IsDirty = true;
                }
            }
        }

        public CCLabelFormat LabelFormat
        {
            get { return labelFormat; }
            set
            {
                if (!labelFormat.Equals(value))
                {
                    // TODO: Check label format flags need to be checked so they can not be
                    // changed after being set.
                    labelFormat = value;
                    IsDirty = true;
                }
            }
        }

        public override CCPoint AnchorPoint
        {
            get { return base.AnchorPoint; }
            set
            {
                if (!AnchorPoint.Equals(value))
                {
                    base.AnchorPoint = value;
                    IsDirty = true;
                }
            }
        }

        public override float Scale
        {
            set
            {
                if (!value.Equals(base.ScaleX) || !value.Equals(base.ScaleY)) 
                {
                    base.Scale = value;
                    IsDirty = true;
                }
            }
        }

        public override float ScaleX
        {
            get { return base.ScaleX; }
            set
            {
                if (!value.Equals(base.ScaleX)) 
                {
                    base.ScaleX = value;
                    IsDirty = true;
                }
            }
        }

        public override float ScaleY
        {
            get { return base.ScaleY; }
            set
            {
                if (!value.Equals(base.ScaleY)) 
                {
                    base.ScaleY = value;
                    IsDirty = true;
                }
            }
        }

        public CCTextAlignment HorizontalAlignment
        {
            get { return labelFormat.Alignment; }
            set
            {
                if (labelFormat.Alignment != value)
                {
                    labelFormat.Alignment = value;
                    IsDirty = true;
                }
            }
        }

        public CCVerticalTextAlignment VerticalAlignment
        {
            get { return labelFormat.LineAlignment; }
            set
            {
                if (labelFormat.LineAlignment != value)
                {
                    labelFormat.LineAlignment = value;
                    IsDirty = true;
                }
            }
        }

        public override CCPoint Position
        {
            get { return base.Position; }
            set
            {
                if (base.Position != value)
                {
                    base.Position = value;
                    IsDirty = true;
                }
            }
        }

        public override float PositionX
        {
            get { return base.PositionX; }
            set
            {
                if (base.PositionX != value)
                {
                    base.PositionX = value;
                    IsDirty = true;
                }
            }
        }

        public override float PositionY
        {
            get { return base.PositionY; }
            set
            {
                if (base.PositionY != value)
                {
                    base.PositionY = value;
                    IsDirty = true;
                }
            }
        }

        public override CCSize ContentSize
        {
            get 
            {
                if (IsDirty || systemFontDirty)
                    UpdateContent();
                
                return base.ContentSize; 
            }
            set
            {
                if (base.ContentSize != value)
                {
                    base.ContentSize = value;
                    IsDirty = true;
                }
            }
        }

        public CCSize Dimensions
        {
            get { return labelDimensions; }
            set
            {
                if (labelDimensions != value)
                {
                    labelDimensions = value;
                    IsDirty = true;
                }
            }
        }

        public CCLabelLineBreak LineBreak
        {
            get { return lineBreak; }
            set
            {
                lineBreak = value;
                IsDirty = true;
            }
        }

        public bool IsAntialiased
        {
            get { return Texture.IsAntialiased; }
            set { Texture.IsAntialiased = value; }
        }

        public virtual CCTexture2D Texture
        {
            get { return TextureAtlas.Texture; }
            set
            {
                TextureAtlas.Texture = value;
                UpdateBlendFunc();
            }
        }

        void UpdateBlendFunc()
        {
            if (!TextureAtlas.Texture.HasPremultipliedAlpha)
            {
                BlendFunc = CCBlendFunc.NonPremultiplied;
            }
        }

        public virtual string Text
        {
            get { return labelText; }
            set
            {
                if (labelText != value)
                {
                    labelText = value;
                    IsDirty = true;
                }
            }
        }

        public string SystemFont
        {
            get { return systemFont; }
            set
            {
                if (systemFont != value)
                {
                    systemFont = value;
                    systemFontDirty = true;

                }
            }
        }

        public float SystemFontSize
        {
            get { return systemFontSize; }
            set
            {
                if (systemFontSize != value)
                {
                    systemFontSize = value;
                    systemFontDirty = true;

                }
            }
        }

        public static void PurgeCachedData()
        {
            if (fontConfigurations != null)
            {
                fontConfigurations.Clear();
            }
        }

        #region Constructors

        static CCLabel()
        {
            DefaultTexelToContentSizeRatios = CCSize.One;
        }

        public CCLabel() : this("", "")
        {
        }

        public CCLabel(string str, string fntFile)
            : this(str, fntFile, 0.0f)
        {
        }

        public CCLabel(string str, string fntFile, float size)
            : this(str, fntFile, size, CCTextAlignment.Left)
        {
        }

        public CCLabel(string str, string fntFile, float size, CCTextAlignment alignment)
            : this(str, fntFile, size, alignment, CCPoint.Zero)
        {
        }

        public CCLabel(string str, string fntFile, float size, CCTextAlignment alignment, CCPoint imageOffset) 
            : this(str, fntFile, size, alignment, imageOffset, null)
        {
        }

        public CCLabel(string str, string fntFile, float size, CCTextAlignment alignment, CCPoint imageOffset, CCTexture2D texture)
            : this(str, fntFile, size, alignment, CCVerticalTextAlignment.Top, imageOffset, null)
        {
        }

        public CCLabel(string str, string fntFile, float size, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, 
            CCPoint imageOffset, CCTexture2D texture)
            : this(str, fntFile, size, CCSize.Zero,
                new CCLabelFormat() { Alignment = hAlignment, LineAlignment = vAlignment}, 
                imageOffset, texture)
        {
        }

        public CCLabel(string str, string fntFile, CCSize dimensions)
            : this (str, fntFile, dimensions, 
                new CCLabelFormat(), CCPoint.Zero, null)
        {   }

        public CCLabel(string str, string fntFile, CCSize dimensions, CCTextAlignment hAlignment)
            : this (str, fntFile, dimensions, 
                new CCLabelFormat() { Alignment = hAlignment}, 
                CCPoint.Zero, null)
        {   }

        public CCLabel(string str, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignement)
            : this (str, fntFile, dimensions, hAlignment, vAlignement, CCPoint.Zero, null)
        {   }

        public CCLabel(string str, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, 
            CCPoint imageOffset, CCTexture2D texture)
            : this (str, fntFile, dimensions, 
                new CCLabelFormat() { Alignment = hAlignment, LineAlignment = vAlignment}, 
                imageOffset, texture)
{'fraction_non_alphanumeric': 0.06339967237112683, 'fraction_numerical': 0.002718622564567286, 'mean_word_length': 1.1819011406844107, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2236918', 'n_tokens_mistral': 7341, 'n_tokens_neox': 6672, 'n_words': 1657}