private Bitmap blackBackgroundFont(Text_ t) { Font font = new Font("Verdana", t.fontsize, (FontStyle)t.GetFontStyle()); SizeF size; using (Bitmap bmp = new Bitmap(1, 1)) { using (Graphics g = Graphics.FromImage(bmp)) { size = g.MeasureString(t.text, font, new PointF(0, 0), new StringFormat(StringFormatFlags.MeasureTrailingSpaces)); } } SizeF size2 = new SizeF(NextPowerOfTwo((uint)size.Width), NextPowerOfTwo((uint)size.Height)); if (size2.Width == 0 || size2.Height == 0) { return(new Bitmap(1, 1)); } Bitmap bmp2 = new Bitmap((int)size2.Width, (int)size2.Height); using (Graphics g2 = Graphics.FromImage(bmp2)) { if (size.Width != 0 && size.Height != 0) { g2.FillRectangle(new SolidBrush(Color.Black), 0, 0, size.Width, size.Height); g2.DrawString(t.text, font, new SolidBrush(Color.FromArgb(t.color)), 0, 0); } } return(bmp2); }
private Bitmap simpleFont(Text_ t) { Font font; try { font = new Font(t.GetFontFamily(), t.GetFontSize(), (FontStyle)t.GetFontStyle()); } catch { throw new Exception(); } SizeF size = MeasureTextSize(t.GetText(), font); SizeF size2 = new SizeF(NextPowerOfTwo((uint)size.Width), NextPowerOfTwo((uint)size.Height)); if (size2.Width == 0 || size2.Height == 0) { return(new Bitmap(1, 1)); } Bitmap bmp2 = new Bitmap((int)size2.Width, (int)size2.Height); using (Graphics g2 = Graphics.FromImage(bmp2)) { if (size.Width != 0 && size.Height != 0) { g2.SmoothingMode = SmoothingMode.AntiAlias; g2.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; // Draw text g2.DrawString(t.GetText(), font, new SolidBrush(Color.FromArgb(t.GetColor())), 0, 0, StringFormat.GenericTypographic); } } return(bmp2); }
private Bitmap blackBackgroundFont(Text_ t) { Font font; try { font = new Font(t.GetFontFamily(), t.GetFontSize(), (FontStyle)t.GetFontStyle()); } catch { throw new Exception(); } SizeF size = MeasureTextSize(t.GetText(), font); SizeF size2 = new SizeF(NextPowerOfTwo((uint)size.Width), NextPowerOfTwo((uint)size.Height)); if (size2.Width == 0 || size2.Height == 0) { return(new Bitmap(1, 1)); } Bitmap bmp2 = new Bitmap((int)size2.Width, (int)size2.Height); using (Graphics g2 = Graphics.FromImage(bmp2)) { if (size.Width != 0 && size.Height != 0) { // Draw black background g2.FillRectangle(new SolidBrush(Color.Black), 0, 0, size.Width, size.Height); // Draw text g2.DrawString(t.GetText(), font, new SolidBrush(Color.FromArgb(t.GetColor())), 0, 0, StringFormat.GenericTypographic); } } return(bmp2); }
// TODO: Currently broken in mono (Graphics Path). private Bitmap defaultFont(Text_ t) { Font font; //outlined font looks smaller float oldfontsize = t.fontsize; t.fontsize = Math.Max(t.fontsize, 9); t.fontsize *= 1.65f; try { font = new Font(t.GetFontFamily(), t.fontsize, (FontStyle)t.GetFontStyle()); } catch { throw new Exception(); } SizeF size = MeasureTextSize(t.text, font); size.Width *= 0.7f; SizeF size2 = new SizeF(NextPowerOfTwo((uint)size.Width), NextPowerOfTwo((uint)size.Height)); if (size2.Width == 0 || size2.Height == 0) { return(new Bitmap(1, 1)); } Bitmap bmp2 = new Bitmap((int)size2.Width, (int)size2.Height); using (Graphics g2 = Graphics.FromImage(bmp2)) { if (size.Width != 0 && size.Height != 0) { StringFormat format = StringFormat.GenericTypographic; g2.FillRectangle(new SolidBrush(Color.FromArgb(textalpha, 0, 0, 0)), 0, 0, size.Width, size.Height); g2.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; Rectangle rect = new Rectangle() { X = 0, Y = 0 }; using (GraphicsPath path = GetStringPath(t.text, t.fontsize, rect, font, format)) { g2.SmoothingMode = SmoothingMode.AntiAlias; RectangleF off = rect; off.Offset(2, 2); using (GraphicsPath offPath = GetStringPath(t.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(Color.FromArgb(t.color)), path); g2.DrawPath(Pens.Black, path); } } } return(bmp2); }
/// <summary> /// Add text to layer /// </summary> /// <returns>The Text object</returns> public Text_ AddText(string text, PointF location, double angle = 0, double width = 1.5, double height = 1.5, double thickness = 0.3) { Text_ text_ = new Text_(); text_.Text = text; text_.Location = location; text_.Angle = angle; text_.Width = width; text_.Height = height; text_.Thickness = thickness; texts.Add(text_); return(text_); }
private Bitmap niceFont(Text_ t) { float fontsize = t.fontsize; Font font; fontsize = Math.Max(fontsize, 9); fontsize *= 1.1f; try { font = new Font(t.GetFontFamily(), fontsize, (FontStyle)t.GetFontStyle()); } catch { throw new Exception(); } SizeF size; using (Bitmap bmp = new Bitmap(1, 1)) { using (Graphics g = Graphics.FromImage(bmp)) { size = g.MeasureString(t.text, font, new PointF(0, 0), new StringFormat(StringFormatFlags.MeasureTrailingSpaces)); } } SizeF size2 = new SizeF(NextPowerOfTwo((uint)size.Width), NextPowerOfTwo((uint)size.Height)); if (size2.Width == 0 || size2.Height == 0) { return(new Bitmap(1, 1)); } Bitmap bmp2 = new Bitmap((int)size2.Width, (int)size2.Height); using (Graphics g2 = Graphics.FromImage(bmp2)) { if (size.Width != 0 && size.Height != 0) { g2.SmoothingMode = SmoothingMode.AntiAlias; g2.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; Matrix mx = new Matrix(1f, 0, 0, 1f, 1, 1); g2.Transform = mx; g2.DrawString(t.text, font, new SolidBrush(Color.FromArgb(128, Color.Black)), 0, 0); g2.ResetTransform(); g2.DrawString(t.text, font, new SolidBrush(Color.FromArgb(t.color)), 0, 0); } } return(bmp2); }
public static dynamic GetTSObject(Text[] dynArray) { if (dynArray is null) { return(null); } var list = new System.Collections.Generic.List <dynamic>(); foreach (var dynItem in dynArray) { list.Add(Text_.GetTSObject(dynItem)); } return(list.ToArray()); }
public static Text[] FromTSObject(dynamic[] tsArray) { if (tsArray is null) { return(null); } var list = new System.Collections.Generic.List <Text>(); foreach (var tsItem in tsArray) { list.Add(Text_.FromTSObject(tsItem)); } return(list.ToArray()); }
public virtual Bitmap MakeTextTexture(Text_ t) { switch (this.Font) { case FontType.Default: return this.defaultFont(t); case FontType.BlackBackground: return this.blackBackgroundFont(t); case FontType.Simple: return this.simpleFont(t); case FontType.Nice: return this.niceFont(t); default: return this.defaultFont(t); } }
TextTexture GetTextTexture(string text, FontCi font) { for (int i = 0; i < textTexturesCount; i++) { TextTexture t = textTextures[i]; if (t == null) { continue; } if (t.text == text && t.font.size == font.size && t.font.family == font.family && t.font.style == font.style) { return(t); } } TextTexture textTexture = new TextTexture(); Text_ text_ = new Text_(); text_.text = text; text_.font = font; text_.color = Game.ColorFromArgb(255, 255, 255, 255); BitmapCi textBitmap = textColorRenderer.CreateTextTexture(text_); int texture = p.LoadTextureFromBitmap(textBitmap); IntRef textWidth = new IntRef(); IntRef textHeight = new IntRef(); p.TextSize(text, font, textWidth, textHeight); textTexture.texture = texture; textTexture.texturewidth = p.FloatToInt(p.BitmapGetWidth(textBitmap)); textTexture.textureheight = p.FloatToInt(p.BitmapGetHeight(textBitmap)); textTexture.text = text; textTexture.font = font; textTexture.textwidth = textWidth.value; textTexture.textheight = textHeight.value; p.BitmapDelete(textBitmap); textTextures[textTexturesCount++] = textTexture; return(textTexture); }
private Bitmap niceFont(Text_ t) { float fontsize = t.fontsize; Font font; fontsize = Math.Max(fontsize, 9); fontsize *= 1.1f; try { font = new Font(t.GetFontFamily(), fontsize, (FontStyle)t.GetFontStyle()); } catch { throw new Exception(); } SizeF size = MeasureTextSize(t.text, font); SizeF size2 = new SizeF(NextPowerOfTwo((uint)size.Width), NextPowerOfTwo((uint)size.Height)); if (size2.Width == 0 || size2.Height == 0) { return(new Bitmap(1, 1)); } Bitmap bmp2 = new Bitmap((int)size2.Width, (int)size2.Height); using (Graphics g2 = Graphics.FromImage(bmp2)) { if (size.Width != 0 && size.Height != 0) { g2.SmoothingMode = SmoothingMode.AntiAlias; g2.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; #if DEBUG // Display measured text sizes g2.DrawRectangle(new Pen(Color.FromArgb(255, 0, 255, 0)), 0, 0, (int)size.Width, (int)size.Height); g2.DrawRectangle(new Pen(Color.FromArgb(255, 255, 255, 0)), 0, 0, (int)size2.Width - 1, (int)size2.Height - 1); #endif // Draw text shadow Matrix mx = new Matrix(1f, 0, 0, 1f, 1, 1); g2.Transform = mx; g2.DrawString(t.text, font, new SolidBrush(Color.FromArgb(128, Color.Black)), 0, 0); g2.ResetTransform(); // Draw text g2.DrawString(t.text, font, new SolidBrush(Color.FromArgb(t.color)), 0, 0); } } return(bmp2); }
public virtual Bitmap MakeTextTexture(Text_ t) { switch (this.Font) { case FontType.Default: return(this.defaultFont(t)); case FontType.BlackBackground: return(this.blackBackgroundFont(t)); case FontType.Simple: return(this.simpleFont(t)); case FontType.Nice: return(this.niceFont(t)); default: return(this.defaultFont(t)); } }
public abstract BitmapCi CreateTextTexture(Text_ t);
public void Draw2dText(string text, FontCi font, float x, float y, IntRef color, bool enabledepthtest) { if (text == null || platform.StringTrim(text) == "") { return; } if (color == null) { color = IntRef.Create(Game.ColorFromArgb(255, 255, 255, 255)); } Text_ t = new Text_(); t.text = text; t.color = color.value; t.fontsize = font.size; t.fontfamily = font.family; t.fontstyle = font.style; CachedTexture ct; if (GetCachedTextTexture(t) == null) { ct = MakeTextTexture(t); if (ct == null) { return; } for (int i = 0; i < cachedTextTexturesMax; i++) { if (cachedTextTextures[i] == null) { CachedTextTexture ct1 = new CachedTextTexture(); ct1.text = t; ct1.texture = ct; cachedTextTextures[i] = ct1; break; } } } ct = GetCachedTextTexture(t); ct.lastuseMilliseconds = platform.TimeMillisecondsFromStart(); platform.GLDisableAlphaTest(); Draw2dTexture(ct.textureId, x, y, ct.sizeX, ct.sizeY, null, 0, Game.ColorFromArgb(255, 255, 255, 255), enabledepthtest); platform.GLEnableAlphaTest(); DeleteUnusedCachedTextTextures(); }
CachedTexture MakeTextTexture(Text_ t) { CachedTexture ct = new CachedTexture(); BitmapCi bmp = textColorRenderer.CreateTextTexture(t); ct.sizeX = platform.BitmapGetWidth(bmp); ct.sizeY = platform.BitmapGetHeight(bmp); ct.textureId = platform.LoadTextureFromBitmap(bmp); platform.BitmapDelete(bmp); return ct; }
CachedTexture GetCachedTextTexture(Text_ t) { for (int i = 0; i < cachedTextTexturesMax; i++) { CachedTextTexture ct = cachedTextTextures[i]; if (ct == null) { continue; } if (ct.text.Equals_(t)) { return ct.texture; } } return null; }
// TODO: Currently broken in mono (Graphics Path). private Bitmap defaultFont(Text_ t) { Font font; //outlined font looks smaller float oldfontsize = t.fontsize; t.fontsize = Math.Max(t.fontsize, 9); t.fontsize *= 1.65f; try { font = new Font("Arial", t.fontsize, (FontStyle)t.GetFontStyle()); } catch { throw new Exception(); } SizeF size; using (Bitmap bmp = new Bitmap(1, 1)) { using (Graphics g = Graphics.FromImage(bmp)) { size = g.MeasureString(t.text, font, new PointF(0, 0), new StringFormat(StringFormatFlags.MeasureTrailingSpaces)); } } size.Width *= 0.7f; SizeF size2 = new SizeF(NextPowerOfTwo((uint)size.Width), NextPowerOfTwo((uint)size.Height)); if (size2.Width == 0 || size2.Height == 0) { return new Bitmap(1, 1); } Bitmap bmp2 = new Bitmap((int)size2.Width, (int)size2.Height); using (Graphics g2 = Graphics.FromImage(bmp2)) { if (size.Width != 0 && size.Height != 0) { StringFormat format = StringFormat.GenericTypographic; g2.FillRectangle(new SolidBrush(Color.FromArgb(textalpha, 0, 0, 0)), 0, 0, size.Width, size.Height); g2.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; Rectangle rect = new Rectangle() { X = 0, Y = 0 }; using (GraphicsPath path = GetStringPath(t.text, t.fontsize, rect, font, format)) { g2.SmoothingMode = SmoothingMode.AntiAlias; RectangleF off = rect; off.Offset(2, 2); using (GraphicsPath offPath = GetStringPath(t.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(Color.FromArgb(t.color)), path); g2.DrawPath(Pens.Black, path); } } } return bmp2; }
private Bitmap simpleFont(Text_ t) { float fontsize = t.fontsize; Font font; fontsize = Math.Max(t.fontsize, 9); fontsize *= 1.1f; try { font = new Font("Arial", fontsize, (FontStyle)t.GetFontStyle()); } catch { throw new Exception(); } SizeF size; using (Bitmap bmp = new Bitmap(1, 1)) { using (Graphics g = Graphics.FromImage(bmp)) { size = g.MeasureString(t.text, font, new PointF(0, 0), new StringFormat(StringFormatFlags.MeasureTrailingSpaces)); } } SizeF size2 = new SizeF(NextPowerOfTwo((uint)size.Width), NextPowerOfTwo((uint)size.Height)); if (size2.Width == 0 || size2.Height == 0) { return new Bitmap(1, 1); } Bitmap bmp2 = new Bitmap((int)size2.Width, (int)size2.Height); using (Graphics g2 = Graphics.FromImage(bmp2)) { if (size.Width != 0 && size.Height != 0) { g2.SmoothingMode = SmoothingMode.AntiAlias; g2.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g2.DrawString(t.text, font, new SolidBrush(Color.FromArgb(t.color)), 0, 0); } } return bmp2; }
/// <summary> /// Creates a bitmap from a given string /// </summary> /// <param name="t">The <see cref="Text_"/> object to create an image from</param> /// <returns>A <see cref="BitmapCi"/> containing the rendered text</returns> internal BitmapCi CreateTextTexture(Text_ t) { IntRef partsCount = new IntRef(); TextPart[] parts = DecodeColors(t.text, t.color, partsCount); float totalwidth = 0; float totalheight = 0; int[] sizesX = new int[partsCount.value]; int[] sizesY = new int[partsCount.value]; for (int i = 0; i < partsCount.value; i++) { IntRef outWidth = new IntRef(); IntRef outHeight = new IntRef(); platform.TextSize(parts[i].text, t.font, outWidth, outHeight); sizesX[i] = outWidth.value; sizesY[i] = outHeight.value; totalwidth += outWidth.value; totalheight = MathCi.MaxFloat(totalheight, outHeight.value); } int size2X = NextPowerOfTwo(platform.FloatToInt(totalwidth) + 1); int size2Y = NextPowerOfTwo(platform.FloatToInt(totalheight) + 1); BitmapCi bmp2 = platform.BitmapCreate(size2X, size2Y); int[] bmp2Pixels = new int[size2X * size2Y]; float currentwidth = 0; for (int i = 0; i < partsCount.value; i++) { int sizeiX = sizesX[i]; int sizeiY = sizesY[i]; if (sizeiX == 0 || sizeiY == 0) { continue; } Text_ partText = new Text_(); partText.text = parts[i].text; partText.color = parts[i].color; partText.font = t.font; BitmapCi partBmp = platform.CreateTextTexture(partText); int partWidth = platform.FloatToInt(platform.BitmapGetWidth(partBmp)); int partHeight = platform.FloatToInt(platform.BitmapGetHeight(partBmp)); int[] partBmpPixels = new int[partWidth * partHeight]; platform.BitmapGetPixelsArgb(partBmp, partBmpPixels); for (int x = 0; x < partWidth; x++) { for (int y = 0; y < partHeight; y++) { if (x + currentwidth >= size2X) { continue; } if (y >= size2Y) { continue; } int c = partBmpPixels[MapUtilCi.Index2d(x, y, partWidth)]; if (Game.ColorA(c) > 0) { bmp2Pixels[MapUtilCi.Index2d(platform.FloatToInt(currentwidth) + x, y, size2X)] = c; } } } currentwidth += sizeiX; } platform.BitmapSetPixelsArgb(bmp2, bmp2Pixels); return(bmp2); }
public override BitmapCi CreateTextTexture(Text_ t) { Bitmap bmp = textrenderer.MakeTextTexture(t); return new BitmapCiCs() { bmp = bmp }; }
internal bool Equals_(Text_ t) { return this.text == t.text && this.fontsize == t.fontsize && this.color == t.color && this.fontfamily == t.fontfamily && this.fontstyle == t.fontstyle; }
internal BitmapCi CreateTextTexture(Text_ t) { IntRef partsCount = new IntRef(); TextPart[] parts = DecodeColors(t.text, t.color, partsCount); float totalwidth = 0; float totalheight = 0; int[] sizesX = new int[partsCount.value]; int[] sizesY = new int[partsCount.value]; for (int i = 0; i < partsCount.value; i++) { IntRef outWidth = new IntRef(); IntRef outHeight = new IntRef(); platform.TextSize(parts[i].text, t.fontsize, outWidth, outHeight); sizesX[i] = outWidth.value; sizesY[i] = outHeight.value; totalwidth += outWidth.value; totalheight = MathCi.MaxFloat(totalheight, outHeight.value); } int size2X = NextPowerOfTwo(platform.FloatToInt(totalwidth) + 1); int size2Y = NextPowerOfTwo(platform.FloatToInt(totalheight) + 1); BitmapCi bmp2 = platform.BitmapCreate(size2X, size2Y); int[] bmp2Pixels = new int[size2X * size2Y]; float currentwidth = 0; for (int i = 0; i < partsCount.value; i++) { int sizeiX = sizesX[i]; int sizeiY = sizesY[i]; if (sizeiX == 0 || sizeiY == 0) { continue; } Text_ partText = new Text_(); partText.text = parts[i].text; partText.color = parts[i].color; partText.fontsize = t.fontsize; partText.fontstyle = t.fontstyle; partText.fontfamily = t.fontfamily; BitmapCi partBmp = platform.CreateTextTexture(partText); int partWidth = platform.FloatToInt(platform.BitmapGetWidth(partBmp)); int partHeight = platform.FloatToInt(platform.BitmapGetHeight(partBmp)); int[] partBmpPixels = new int[partWidth * partHeight]; platform.BitmapGetPixelsArgb(partBmp, partBmpPixels); for (int x = 0; x < partWidth; x++) { for (int y = 0; y < partHeight; y++) { if (x + currentwidth >= size2X) { continue; } if (y >= size2Y) { continue; } int c = partBmpPixels[MapUtilCi.Index2d(x, y, partWidth)]; if (Game.ColorA(c) > 0) { bmp2Pixels[MapUtilCi.Index2d(platform.FloatToInt(currentwidth) + x, y, size2X)] = c; } } } currentwidth += sizeiX; } platform.BitmapSetPixelsArgb(bmp2, bmp2Pixels); return bmp2; }
TextTexture GetTextTexture(string text, float fontSize) { for (int i = 0; i < textTexturesCount; i++) { TextTexture t = textTextures[i]; if (t == null) { continue; } if (t.text == text && t.size == fontSize) { return t; } } TextTexture textTexture = new TextTexture(); Text_ text_ = new Text_(); text_.text = text; text_.fontsize = fontSize; text_.fontfamily = "Arial"; text_.color = Game.ColorFromArgb(255, 255, 255, 255); BitmapCi textBitmap = textColorRenderer.CreateTextTexture(text_); int texture = p.LoadTextureFromBitmap(textBitmap); IntRef textWidth = new IntRef(); IntRef textHeight = new IntRef(); p.TextSize(text, fontSize, textWidth, textHeight); textTexture.texture = texture; textTexture.texturewidth = p.FloatToInt(p.BitmapGetWidth(textBitmap)); textTexture.textureheight = p.FloatToInt(p.BitmapGetHeight(textBitmap)); textTexture.text = text; textTexture.size = fontSize; textTexture.textwidth = textWidth.value; textTexture.textheight = textHeight.value; p.BitmapDelete(textBitmap); textTextures[textTexturesCount++] = textTexture; return textTexture; }
private Bitmap blackBackgroundFont(Text_ t) { Font font = new Font("Verdana", t.fontsize, (FontStyle)t.GetFontStyle()); SizeF size; using (Bitmap bmp = new Bitmap(1, 1)) { using (Graphics g = Graphics.FromImage(bmp)) { size = g.MeasureString(t.text, font, new PointF(0, 0), new StringFormat(StringFormatFlags.MeasureTrailingSpaces)); } } SizeF size2 = new SizeF(NextPowerOfTwo((uint)size.Width), NextPowerOfTwo((uint)size.Height)); if (size2.Width == 0 || size2.Height == 0) { return new Bitmap(1, 1); } Bitmap bmp2 = new Bitmap((int)size2.Width, (int)size2.Height); using (Graphics g2 = Graphics.FromImage(bmp2)) { if (size.Width != 0 && size.Height != 0) { g2.FillRectangle(new SolidBrush(Color.Black), 0, 0, size.Width, size.Height); g2.DrawString(t.text, font, new SolidBrush(Color.FromArgb(t.color)), 0, 0); } } return bmp2; }