/// <summary> /// Measures the map to return the destination width and height of the texture /// </summary> /// <param name="font">Font name</param> /// <param name="size">Font size</param> /// <param name="style">Font Style</param> /// <param name="width">Resulting width</param> /// <param name="height">Resulting height</param> public static void MeasureMap(string font, float size, FontMapStyles style, out int width, out int height) { using (var bmp = new Bitmap(100, 100)) using (var gra = System.Drawing.Graphics.FromImage(bmp)) using (var fmt = StringFormat.GenericDefault) using (var fnt = new Font(font, size, (FontStyle)style, GraphicsUnit.Pixel)) { string str = new string(ValidKeys); var s = gra.MeasureString( str, fnt, int.MaxValue, fmt); if (s.Width <= MAXTEXTURESIZE) { width = (int)s.Width + 1; height = (int)s.Height + 1; } else { width = MAXTEXTURESIZE; int a = (int)s.Width / MAXTEXTURESIZE; height = ((int)s.Height + 1) * (a + 1); } } }
/// <summary> /// Generates a new description /// </summary> /// <param name="font">Font name</param> /// <param name="size">Size</param> /// <param name="style">Style</param> /// <param name="textColor">Text color</param> /// <param name="shadowColor">Shadow color</param> /// <returns>Returns the new generated description</returns> public static TextDrawerDescription Generate(string font, int size, FontMapStyles style, Color textColor, Color shadowColor) { return(new TextDrawerDescription() { Name = string.Format("TextBox {0} {1} {2}", font, size, style), Font = font, FontSize = size, Style = style, TextColor = textColor, ShadowColor = shadowColor, }); }
/// <summary> /// Generates a new description /// </summary> /// <param name="font">Font name</param> /// <param name="size">Size</param> /// <param name="style">Style</param> /// <param name="textColor">Text color</param> /// <returns>Returns the new generated description</returns> public static TextDrawerDescription Generate(string font, int size, FontMapStyles style, Color textColor) { return(Generate(font, size, style, textColor, Color.Transparent)); }
/// <summary> /// Creates a font map of the specified font and size /// </summary> /// <param name="game">Game</param> /// <param name="font">Font name</param> /// <param name="size">Size</param> /// <param name="bold">Weight</param> /// <returns>Returns created font map</returns> public static FontMap Map(Game game, string font, float size, FontMapStyles style) { var fMap = gCache.Find(f => f.Font == font && f.Size == size && f.Style == style); if (fMap == null) { //Calc the destination texture width and height MeasureMap(font, size, style, out int width, out int height); //Calc the delta value for margins an new lines float delta = (float)Math.Sqrt(size) + (size / 40f); fMap = new FontMap() { Font = font, Size = size, Style = style, Delta = (int)delta, TextureWidth = width, TextureHeight = height, }; using (var bmp = new Bitmap(width, height)) using (var gra = System.Drawing.Graphics.FromImage(bmp)) using (var fmt = StringFormat.GenericDefault) using (var fnt = new Font(font, size, (FontStyle)style, GraphicsUnit.Pixel)) { gra.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; gra.FillRegion( Brushes.Transparent, new Region(new System.Drawing.RectangleF(0, 0, width, height))); float left = fMap.Delta; float top = 0f; for (int i = 0; i < ValidKeys.Length; i++) { char c = ValidKeys[i]; var s = gra.MeasureString( c.ToString(), fnt, int.MaxValue, fmt); if (c == ' ') { //White space s.Width = fnt.SizeInPoints; } if (left + (int)s.Width + fMap.Delta + fMap.Delta >= width) { //Next texture line left = fMap.Delta; top += (int)s.Height + fMap.Delta; } gra.DrawString( c.ToString(), fnt, Brushes.White, left, top, fmt); var chr = new FontMapChar() { X = left, Y = top, Width = s.Width, Height = s.Height + (fMap.Delta / 2), }; fMap.map.Add(c, chr); left += (int)s.Width + fMap.Delta + fMap.Delta; } //Generate the texture fMap.bitmapStream = new MemoryStream(); bmp.Save(fMap.bitmapStream, ImageFormat.Png); fMap.Texture = game.ResourceManager.CreateResource(fMap.bitmapStream, false); } //Add map to the font cache gCache.Add(fMap); } return(fMap); }