Пример #1
0
        /// <summary>
        /// 用一个纹理绘制指定范围内的所有可见字符
        /// </summary>
        /// <param name="ttfFullname"></param>
        /// <param name="fontHeight">此值越大,绘制文字的清晰度越高,但占用的纹理资源就越多。</param>
        /// <param name="firstChar"></param>
        /// <param name="lastChar"></param>
        /// <param name="maxTextureWidth">生成的纹理的最大宽度。</param>
        /// <returns></returns>
        public static IEnumerable <TTFTextureYeildingState> GetTTFTexture(
            string ttfFullname, int fontHeight, int maxTextureWidth, char firstChar, char lastChar)
        {
            FreeTypeLibrary library = new FreeTypeLibrary();

            FreeTypeFace face = new FreeTypeFace(library, ttfFullname);

            Dictionary <char, CharacterInfo> charInfoDict = null;
            int textureWidth = 0, textureHeight = 0;

            System.Drawing.Bitmap bigBitmap = null;

            foreach (var item in GetTextureBlueprint(face, fontHeight, maxTextureWidth, firstChar, lastChar))
            {
                charInfoDict  = item.dict;
                textureWidth  = item.textureWidth;
                textureHeight = item.textureHeight;

                yield return(item);
            }

            if (textureWidth == 0)
            {
                textureWidth = 1;
            }
            if (textureHeight == 0)
            {
                textureHeight = 1;
            }

            foreach (var item in GetBigBitmap(face, fontHeight, maxTextureWidth,
                                              firstChar, lastChar, charInfoDict, textureWidth, textureHeight))
            {
                bigBitmap = item.bigBitmap;

                yield return(item);
            }

            face.Dispose();
            library.Dispose();

            var result = new FontTexture()
            {
                TtfFullname  = ttfFullname,
                FontHeight   = fontHeight,
                FirstChar    = firstChar,
                LastChar     = lastChar,
                BigBitmap    = bigBitmap,
                CharInfoDict = charInfoDict,
            };

            FileInfo fileInfo = new FileInfo(ttfFullname);

            yield return(new TTFTextureYeildingState()
            {
                percent = 100,
                ttfTexture = result,
                message = string.Format("got font texture for {0}", fileInfo.Name),
            });
        }
Пример #2
0
        /// <summary>
        /// 用一个纹理绘制指定范围内的所有可见字符
        /// </summary>
        /// <param name="ttfFullname"></param>
        /// <param name="fontHeight">此值越大,绘制文字的清晰度越高,但占用的纹理资源就越多。</param>
        /// <param name="firstChar"></param>
        /// <param name="lastChar"></param>
        /// <param name="maxTextureWidth">生成的纹理的最大宽度。</param>
        /// <returns></returns>
        public static FontTexture GetTTFTexture(string ttfFullname, int fontHeight, int maxTextureWidth, char firstChar, char lastChar)
        {
            FreeTypeLibrary library = new FreeTypeLibrary();

            FreeTypeFace face = new FreeTypeFace(library, ttfFullname);

            Dictionary <char, CharacterInfo> charInfoDict;
            int textureWidth, textureHeight;

            GetTextureBlueprint(face, fontHeight, maxTextureWidth, firstChar, lastChar, out charInfoDict, out textureWidth, out textureHeight);

            if (textureWidth == 0)
            {
                textureWidth = 1;
            }
            if (textureHeight == 0)
            {
                textureHeight = 1;
            }

            System.Drawing.Bitmap bigBitmap = GetBigBitmap(face, fontHeight, maxTextureWidth, firstChar, lastChar, charInfoDict, textureWidth, textureHeight);

            face.Dispose();
            library.Dispose();

            var result = new FontTexture()
            {
                TtfFullname = ttfFullname, FontHeight = fontHeight, FirstChar = firstChar, LastChar = lastChar, BigBitmap = bigBitmap, CharInfoDict = charInfoDict,
            };

            return(result);
        }
Пример #3
0
        /// <summary>
        /// 根据已经生成的贴图和附带的Xml配置文件得到一个<see cref="FontTexture"/>。
        /// <para>此贴图和Xml文件</para>
        /// </summary>
        /// <param name="textureFullname"></param>
        /// <param name="xmlFullname"></param>
        /// <returns></returns>
        public static FontTexture GetTTFTexture(string textureFullname, string xmlFullname)
        {
            XElement    ttfTextureElement = XElement.Load(xmlFullname); //new XElement(xmlFullname);
            FontTexture result            = Parse(ttfTextureElement);

            System.Drawing.Bitmap bigBitmap = new System.Drawing.Bitmap(textureFullname);
            result.BigBitmap = bigBitmap;

            return(result);
        }
Пример #4
0
        public static XElement ToXElement(this FontTexture texture)
        {
            XElement result = new XElement(strTTFTexture,
                                           new XAttribute(strFontHeight, texture.FontHeight),
                                           new XAttribute(strFirstChar, (int)texture.FirstChar),
                                           new XAttribute(strLastChar, (int)texture.LastChar),
                                           texture.CharInfoDict.ToXElement());

            return(result);
        }
Пример #5
0
        private static FontTexture Parse(XElement xElement)
        {
            FontTexture result = new FontTexture();

            result.FontHeight   = int.Parse(xElement.Attribute(strFontHeight).Value);
            result.FirstChar    = (char)int.Parse(xElement.Attribute(strFirstChar).Value);
            result.LastChar     = (char)int.Parse(xElement.Attribute(strLastChar).Value);
            result.CharInfoDict = CharacterInfoDictHelper.Parse(
                xElement.Element(CharacterInfoDictHelper.strCharacterInfoDict));

            return(result);
        }