public Bitmap MakeTextTexture(Text t) { Font font; if (NewFont) { //outlined font looks smaller t.fontsize = Math.Max(t.fontsize, 9); t.fontsize *= 1.65f; font = new Font("Arial", t.fontsize, FontStyle.Bold); } else { font = new Font("Verdana", t.fontsize); } var parts = DecodeColors(t.text, t.color); float totalwidth = 0; float totalheight = 0; List<SizeF> sizes = new List<SizeF>(); using (Bitmap bmp = new Bitmap(1, 1)) { using (Graphics g = Graphics.FromImage(bmp)) { for (int i = 0; i < parts.Count; i++) { SizeF size = g.MeasureString(parts[i].text, font); if (size.Width == 0 || size.Height == 0) { continue; } if (NewFont) { size.Width *= 0.7f; } totalwidth += size.Width; totalheight = Math.Max(totalheight, size.Height); sizes.Add(size); } } } SizeF size2 = new SizeF(NextPowerOfTwo((uint)totalwidth), NextPowerOfTwo((uint)totalheight)); Bitmap bmp2 = new Bitmap((int)size2.Width, (int)size2.Height); using (Graphics g2 = Graphics.FromImage(bmp2)) { float currentwidth = 0; for (int i = 0; i < parts.Count; i++) { SizeF sizei = sizes[i]; if (sizei.Width == 0 || sizei.Height == 0) { continue; } if (NewFont) { StringFormat format = StringFormat.GenericTypographic; g2.FillRectangle(new SolidBrush(Color.FromArgb(textalpha, 0, 0, 0)), currentwidth, 0, sizei.Width, sizei.Height); g2.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; //g2.DrawString(parts[i].text, font, new SolidBrush(parts[i].color), currentwidth, 0); Rectangle rect = new Rectangle() { X = (int)currentwidth, Y = 0 }; using (GraphicsPath path = GetStringPath(parts[i].text, t.fontsize, rect, font, format)) { g2.SmoothingMode = SmoothingMode.AntiAlias; RectangleF off = rect; off.Offset(2, 2); using (GraphicsPath offPath = GetStringPath(parts[i].text, t.fontsize, off, font, format)) { Brush b = new SolidBrush(Color.FromArgb(100, 0, 0, 0)); g2.FillPath(b, offPath); b.Dispose(); } g2.FillPath(new SolidBrush(parts[i].color), path); g2.DrawPath(Pens.Black, path); } } else { g2.FillRectangle(new SolidBrush(Color.Black), currentwidth, 0, sizei.Width, sizei.Height); g2.DrawString(parts[i].text, font, new SolidBrush(parts[i].color), currentwidth, 0); } currentwidth += sizei.Width; } } return bmp2; }
public Bitmap MakeTextTexture(Text t) { Font font = ManicDiggerGameWindow.fn; t.fontsize = font.Size; var parts = DecodeColors(t.text, t.color); float totalwidth = 0; float totalheight = 0; List<SizeF> sizes = new List<SizeF>(); using (Bitmap bmp = new Bitmap(1, 1)) { for (int i = 0; i < parts.Count; i++) { SizeF size = MeasureTextSize(parts[i].text, font); if (size.Width == 0 || size.Height == 0) { continue; } totalwidth += size.Width; totalheight = Math.Max(totalheight, size.Height); sizes.Add(size); } } SizeF size2 = new SizeF(NextPowerOfTwo((uint)totalwidth), NextPowerOfTwo((uint)totalheight)); Bitmap bmp2 = new Bitmap((int)size2.Width, (int)size2.Height); using (Graphics g2 = Graphics.FromImage(bmp2)) { float currentwidth = 0; for (int i = 0; i < parts.Count; i++) { parts[i].text = parts[i].text; SizeF sizei = MeasureTextSize(parts[i].text, font); if (sizei.Width == 0 || sizei.Height == 0) { continue; } g2.SmoothingMode = SmoothingMode.HighQuality; g2.DrawString(parts[i].text, font, new SolidBrush(Color.Black), currentwidth + 1.3f, 1.3f); g2.DrawString(parts[i].text, font, new SolidBrush(parts[i].color.ToColor()), currentwidth, 0); currentwidth += sizei.Width; } } return bmp2; }