public Size MeasureString(string str, float size, SdfFont font) { float sz = 1.0f / ((float)font.FontSize / (float)size); var p = new Size(0, 0); foreach (var c in str) { var chr = font.GetChar(c).Clone(); chr.Width = (int)((float)chr.Width * sz); chr.Height = (int)((float)chr.Height * sz); chr.X = (int)((float)chr.X * sz); chr.Y = (int)((float)chr.Y * sz); chr.Xadvance = (int)((float)chr.Xadvance * sz); chr.Xoffset = (int)((float)chr.Xoffset * sz); chr.Yoffset = (int)((float)chr.Yoffset * sz); if (chr.Height > p.Height) { p.Height = chr.Height; } p.Width += chr.Xadvance; } return(p); }
protected override void BeforeRun() { Console.Clear(); Screen.SetMode(VbeScreen.ScreenSize.Size800X600, VbeScreen.Bpp.Bpp32); Screen.Clear(Colors.Blue); _deltaT = RTC.Second; var g = new Graphics(Canvas); g.Clear(Colors.White); Canvas.WriteToScreen(); terminus = new SdfFont(Terminus.Terminus_fnt, Image.FromBytes(Terminus.Terminus_ppm, "ppm")); g.DrawString(10, 10, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 14f, terminus, Colors.Black); Canvas.WriteToScreen(); g.DrawString(10, 25, "abcdefghijklmnopqrstuvwxyz", 14f, terminus, Colors.Black); Canvas.WriteToScreen(); g.DrawString(10, 44, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 30f, terminus, Colors.Black); Canvas.WriteToScreen(); g.DrawString(10, 74, "abcdefghijklmnopqrstuvwxyz", 30f, terminus, Colors.Black); Canvas.WriteToScreen(); }
private void Form1_Paint(object sender, PaintEventArgs e) { var canvas = new FormCanvas(Width, Height); var g = new Graphics(canvas); g.Clear(Colors.White); // var img = Image.FromBytes(File.ReadAllBytes("Myvar Logo Solid plain.ppm"), "ppm").ResizeImage(128, 128); // g.DrawImage(10, 10, img); var terminus = new SdfFont(File.ReadAllText("Terminus.fnt"), Image.FromBytes(File.ReadAllBytes("Terminus.ppm"), "ppm")); // g.DrawString(10, 10, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 14f, terminus, Colors.Black); // g.DrawString(10, 25, "abcdefghijklmnopqrstuvwxyz", 14f, terminus, Colors.Black); // g.DrawString(10, 44, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 30f, terminus, Colors.Black); // g.DrawString(10, 74, "abcdefghijklmnopqrstuvwxyz", 30f, terminus, Colors.Black); int x = 10; int y = 10; var s = "Hi"; e.Graphics.Clear(System.Drawing.Color.White); for (int i = 1; i < 158; i++) { g.DrawString(x, y, s, i, terminus, Colors.Black); var size = g.MeasureString(s, i, terminus); y += size.Height + 2; if (y + 50 > Height) { y = 10; x += size.Width; } e.Graphics.DrawImageUnscaled(canvas.Bitmap, 0, 0); e.Graphics.Flush(); Update(); } //g.DrawString(10, 100, "Hello World", 10f, terminus, Colors.Black); e.Graphics.Clear(System.Drawing.Color.White); e.Graphics.DrawImageUnscaled(canvas.Bitmap, 0, 0); }
public void DrawString(int x, int y, string str, float size, SdfFont font, Color txtcolor) { float width = 0.5f; float edge = 0.2f; float borderWidth = 0.0f; float borderEdge = 0.05f; // ScaleTransform(sz, sz); var atlas = font.AtlasImage; float sz = 1.0f / ((float)font.FontSize / (float)size); // if (size != font.FontSize) { atlas = atlas.ResizeImage((int)((float)atlas.Width * sz), (int)((float)atlas.Height * sz)); } foreach (var c in str) { var chr = font.GetChar(c).Clone(); chr.Width = (int)((float)chr.Width * sz); chr.Height = (int)((float)chr.Height * sz); chr.X = (int)((float)chr.X * sz); chr.Y = (int)((float)chr.Y * sz); chr.Xadvance = (int)((float)chr.Xadvance * sz); chr.Xoffset = (int)((float)chr.Xoffset * sz); chr.Yoffset = (int)((float)chr.Yoffset * sz); for (int x1 = chr.X; x1 < chr.X + chr.Width; x1++) { for (int y1 = chr.Y; y1 < chr.Y + chr.Height; y1++) { float distance = 1.0f - (atlas.GetPixel(x1, y1).R / 255f); float alpha = 1.0f - Smoothstep(width, width + edge, distance); float distance2 = 1.0f - (atlas.GetPixel(x1, y1).R / 255f); float outlinealpha = 1.0f - Smoothstep(borderWidth, borderWidth + borderEdge, distance2); float overallAlpha = alpha + (1.0f - alpha) * outlinealpha; var color = txtcolor; if (overallAlpha != 1.0f) { var gpx = _canvas.GetPixel(x + x1 - chr.X + chr.Xoffset, y + y1 - chr.Y + chr.Yoffset); color = Blend(txtcolor, gpx, overallAlpha); } color.A = 255; SetPixel(Offset.X + x + x1 - chr.X + chr.Xoffset, Offset.Y + y + y1 - chr.Y + chr.Yoffset, color); } } x += chr.Xadvance; } }