protected override IMAGE32 Evaluate() { if (FontFamily == null) { return(null); } if (Size <= 0) { return(null); } var font = new SixLabors.Fonts.Font(FontFamily, Size); var context = new SixLabors.Fonts.RendererOptions(font); var characters = "0123456789ABCDEFGHIJKLM"; var padding = new Size(this.Padding, this.Padding); var cellSize = context.GetGlypthMaxBounds(characters.ToCharArray()).OuterRound().Size; cellSize += padding * 2; var image = ImageFactory.CreateImage <Rgba32>(cellSize * 16); foreach (var c in characters.ToArray()) { _DrawGlyph(image, cellSize, padding, context, c); } return(image); }
public void DrawTextCoreOld() { using (var image = new Image <Rgba32>(800, 800)) { Fonts.Font font = Fonts.SystemFonts.CreateFont("Arial", 12); image.Mutate( x => DrawTextOldVersion( x, new TextGraphicsOptions { GraphicsOptions = { Antialias = true }, TextOptions = { WrapTextWidth = 780 } }, this.TextToRender, font, null, Processing.Pens.Solid(Color.HotPink, 10), new PointF(10, 10))); } IImageProcessingContext DrawTextOldVersion( IImageProcessingContext source, TextGraphicsOptions options, string text, SixLabors.Fonts.Font font, IBrush brush, IPen pen, PointF location) { var style = new SixLabors.Fonts.RendererOptions(font, options.TextOptions.DpiX, options.TextOptions.DpiY, location) { ApplyKerning = options.TextOptions.ApplyKerning, TabWidth = options.TextOptions.TabWidth, WrappingWidth = options.TextOptions.WrapTextWidth, HorizontalAlignment = options.TextOptions.HorizontalAlignment, VerticalAlignment = options.TextOptions.VerticalAlignment }; IPathCollection glyphs = TextBuilder.GenerateGlyphs(text, style); var pathOptions = new ShapeGraphicsOptions() { GraphicsOptions = options.GraphicsOptions }; if (brush != null) { source.Fill(pathOptions, brush, glyphs); } if (pen != null) { source.Draw(pathOptions, pen, glyphs); } return(source); } }
// https://docs.microsoft.com/es-es/typography/opentype/spec/ttch01 public static void TestGlyph() { var family = SixLabors.Fonts.SystemFonts.Find("arial"); var font = new SixLabors.Fonts.Font(family, 50); var roptions = new SixLabors.Fonts.RendererOptions(font, 96); var size1 = SixLabors.Fonts.TextMeasurer.Measure("f", roptions); var size2 = SixLabors.Fonts.TextMeasurer.Measure("g", roptions); }
public static void DrawGlypth(this Image <Rgba32> target, SixLabors.Fonts.RendererOptions options, int x, int y, char character) { var origin = new PointF(x, y); // origin += new SizeF(0.5f, 0.5f); var path = options .GetCharacterGlypth(character) .Translate(origin); target.Mutate(dc => dc.Fill(Rgba32.White, path)); }
// https://github.com/SixLabors/Fonts/issues/57 public static RectangleF GetGlypthMaxBounds(this SixLabors.Fonts.RendererOptions options, params char[] characters) { var rect = new SixLabors.Primitives.RectangleF(float.PositiveInfinity, float.PositiveInfinity, float.NegativeInfinity, float.NegativeInfinity); foreach (var c in characters) { var r = options.GetCharacterGlypth(c).Bounds; rect = rect.IsInitialized() ? rect.UnionWith(r) : r; } return(rect); }
public void DrawTextCoreOld() { using (var image = new Image <Rgba32>(800, 800)) { var font = SixLabors.Fonts.SystemFonts.CreateFont("Arial", 12); image.Mutate(x => DrawTextOldVersion(x, new TextGraphicsOptions(true) { WrapTextWidth = 780 }, TextToRender, font, Processing.Brushes.Solid(Rgba32.HotPink), null, new SixLabors.Primitives.PointF(10, 10))); } IImageProcessingContext DrawTextOldVersion( IImageProcessingContext source, TextGraphicsOptions options, string text, SixLabors.Fonts.Font font, IBrush brush, IPen pen, SixLabors.Primitives.PointF location) { float dpiX = 72; float dpiY = 72; var style = new SixLabors.Fonts.RendererOptions(font, dpiX, dpiY, location) { ApplyKerning = options.ApplyKerning, TabWidth = options.TabWidth, WrappingWidth = options.WrapTextWidth, HorizontalAlignment = options.HorizontalAlignment, VerticalAlignment = options.VerticalAlignment }; Shapes.IPathCollection glyphs = Shapes.TextBuilder.GenerateGlyphs(text, style); var pathOptions = (GraphicsOptions)options; if (brush != null) { source.Fill(pathOptions, brush, glyphs); } if (pen != null) { source.Draw(pathOptions, pen, glyphs); } return(source); } }
public SizeF MeasureString(String text, Font font, int width, StringFormat format) { SixLabors.Fonts.FontFamily family = SixLabors.Fonts.SystemFonts.Find(font.Name); //font.Bold //font.Italic //font.Underline //font.Strikeout SixLabors.Fonts.Font fo = new SixLabors.Fonts.Font(family, font.Size); SixLabors.Fonts.RendererOptions ro = new SixLabors.Fonts.RendererOptions(fo); SixLabors.Primitives.SizeF result = SixLabors.Fonts.TextMeasurer.Measure(text, ro); return(new System.Drawing.SizeF(result.Width, result.Height)); }
public static Image <Rgba32> CreateGlyphImage(this SixLabors.Fonts.Font font, Char character, Rgba32 foreground) { var options = TextGraphicsOptions.Default; var text = character.ToString(); var roptions = new SixLabors.Fonts.RendererOptions(font, 96); var size = SixLabors.Fonts.TextMeasurer.Measure(text, roptions); int w = (int)Math.Ceiling(size.Width); int h = (int)Math.Ceiling(size.Height); var img = new Image <Rgba32>(w, h); img.Mutate(dc => dc.DrawText(options, text, font, foreground, System.Numerics.Vector2.Zero)); return(img); }
public static IMAGE32 RenderText(this SixLabors.Fonts.FontFamily ffamily, string text, float fsize, float padding, COLOR color, TextGraphicsOptions options) { // http://sixlabors.com/2017/04/08/watermarks/ if (string.IsNullOrEmpty(text)) { return(null); } var font = new SixLabors.Fonts.Font(ffamily, fsize); var roptions = new SixLabors.Fonts.RendererOptions(font, 96); var size = SixLabors.Fonts.TextMeasurer.Measure(text, roptions); var w = (int)Math.Ceiling(size.Width + padding * 2); var h = (int)Math.Ceiling(size.Height + padding * 2); var img = new IMAGE32(w, h); img.Mutate(dc => dc.DrawText(options, text, font, color, new System.Numerics.Vector2(padding, padding))); return(img); }
private static void _DrawGlyph(Image <Rgba32> dstImage, Size cellSize, Size padding, SixLabors.Fonts.RendererOptions context, char c) { var x = ((int)c) % 16; var y = ((int)c) / 16; x *= cellSize.Width; y *= cellSize.Height; x += padding.Width; y += padding.Height; var glyph = context.GetCharacterGlypth(c); var bounds = glyph.Bounds; dstImage.DrawGlypth(context, x, y, c); }
public static IPathCollection GetCharacterGlypth(this SixLabors.Fonts.RendererOptions options, char character) { return(TextBuilder.GenerateGlyphs(string.Empty + character, options)); }