Пример #1
0
        /// <summary>
        ///     This method is responsible for converting a string of text into
        ///     TextureData. This is done by converting each character in the string
        ///     into an id that maps to the sprites stored in the FontChip. The ids
        ///     are based on ASCII values. The font class starts at ASCII
        ///     <paramref name="value" /> for an empty space and will scale up from
        ///     there based on how many sprites are in the FontChip. By default it
        ///     supports up to ASCII 128 but can support more as long as the sprites
        ///     exist.
        /// </summary>
        /// <param name="value">
        ///     The string to be converted into pixel data.
        /// </param>
        /// <param name="spriteChip"></param>
        /// <param name="fontMap"></param>
        /// <param name="textureData">
        ///     A reference to a <see cref="TextureData" /> class to store the pixel
        ///     data in.
        /// </param>
        /// <param name="stripEmptyLines">
        ///     Ignore empty lines in the supplied text. By default this is set to
        ///     false.
        /// </param>
        /// <param name="letterSpacing"></param>
        /// <param name="offset"></param>
        public void GenerateTextData(string value, SpriteChip spriteChip, int[] fontMap, TextureData textureData, bool stripEmptyLines = false, int letterSpacing = 0, int offset = 0)
        {
            // Strip out any tabs
            value = value.Replace("\t", "     ");

            var result = value.Split(new[] { "\n", "\r\n" },
                                     stripEmptyLines ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);

            Array.Reverse(result);

            var totalLines = result.Length;

            //spriteChip = this;

            var cWidth  = spriteChip.width;
            var cHeight = spriteChip.height;

            // index text
            var tWidth  = 0;
            var tHeight = totalLines;

            for (var i = 0; i < totalLines; i++)
            {
                tWidth = Math.Max(tWidth, result[i].Length);
            }

            var realWidth  = (cWidth + letterSpacing) * tWidth;
            var realHeight = cHeight * tHeight;

            if (textureData.width != realWidth || textureData.height != realHeight)
            {
                textureData.Resize(realWidth, realHeight);
            }

            textureData.Clear();

            // convert each line into a sprite id

            var charOffset = 32;

            var tmpData = new int[spriteChip.width * spriteChip.height];

            for (var i = 0; i < totalLines; i++)
            {
                var line       = result[i];
                var characters = line.Length;
                for (var j = 0; j < characters; j++)
                {
                    var character = line[j];
                    var spriteID  = Convert.ToInt32(character) - charOffset;

                    //Debug.Log("Char " + character + " " + spriteID);
                    spriteChip.ReadSpriteAt(fontMap[spriteID], tmpData);

                    textureData.MergePixels(j * (cWidth + letterSpacing), i * cHeight, cWidth, cHeight, tmpData);
                }
            }
        }
Пример #2
0
        /// <summary>
        ///     Use this method to get the raw pixel data of the font text that is generated.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="spriteChip"></param>
        /// <param name="fontName"></param>
        /// <param name="pixels"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="letterSpacing"></param>
        public void ConvertTextToPixelData(string value, SpriteChip spriteChip, string fontName, ref int[] pixels, out int width, out int height, int letterSpacing = 0)
        {
            if (fontName == "Default")
            {
                fontName = fonts.Keys.First();
            }

            var fontMap = fonts[fontName];

            GenerateTextData(value, spriteChip, fontMap, tmpTextureData, false, letterSpacing);
            tmpTextureData.CopyPixels(ref pixels);
            width  = tmpTextureData.width;
            height = tmpTextureData.height;
        }