protected override void BeforeImageApply() { base.BeforeImageApply(); // do everything at the image level as we are delegating the processing down to other processors var style = new RendererOptions(this.Font, this.Options.DpiX, this.Options.DpiY, this.Location) { ApplyKerning = this.Options.ApplyKerning, TabWidth = this.Options.TabWidth, WrappingWidth = this.Options.WrapTextWidth, HorizontalAlignment = this.Options.HorizontalAlignment, VerticalAlignment = this.Options.VerticalAlignment, FallbackFontFamilies = this.Options.FallbackFonts, ColorFontSupport = this.definition.Options.RenderColorFonts ? ColorFontSupport.MicrosoftColrFormat : ColorFontSupport.None, }; // (this.definition.Options.RenderColorFonts) //{ this.textRenderer = new ColorCachingGlyphRenderer(this.Configuration.MemoryAllocator, this.Text.Length, this.Pen, this.Brush != null); // } // else // { // this.textRenderer = new CachingGlyphRenderer(this.Configuration.MemoryAllocator, this.Text.Length, this.Pen, this.Brush != null); // } this.textRenderer.Options = (GraphicsOptions)this.Options; var renderer = new TextRenderer(this.textRenderer); renderer.RenderText(this.Text, style); }
public static void RenderTextProcessor(FontFamily fontFamily, string text, float pointSize = 12, IEnumerable <FontFamily> fallbackFonts = null) { var textOptions = new TextGraphicsOptionsCopy { ApplyKerning = true, DpiX = 96, DpiY = 96, RenderColorFonts = true, }; if (fallbackFonts != null) { textOptions.FallbackFonts.AddRange(fallbackFonts); } var font = new Font(fontFamily, pointSize); var renderOptions = new RendererOptions(font, textOptions.DpiX, textOptions.DpiY) { ApplyKerning = true, ColorFontSupport = ColorFontSupport.MicrosoftColrFormat, FallbackFontFamilies = textOptions.FallbackFonts?.ToArray() }; var textSize = TextMeasurer.Measure(text, renderOptions); using (var img = new Image <Rgba32>((int)Math.Ceiling(textSize.Width) + 20, (int)Math.Ceiling(textSize.Height) + 20)) { img.Mutate(x => x.Fill(Color.White).ApplyProcessor(new DrawTextProcessorCopy(textOptions, text, font, new SolidBrushCopy(Color.Black), null, new PointF(5, 5)))); string fullPath = CreatePath(font.Name, text + ".caching.png"); Directory.CreateDirectory(System.IO.Path.GetDirectoryName(fullPath)); img.Save(fullPath); } }
/// <summary> /// Initializes a new instance of the <see cref="DrawTextProcessorCopy"/> class. /// </summary> /// <param name="options">The options</param> /// <param name="text">The text we want to render</param> /// <param name="font">The font we want to render with</param> /// <param name="brush">The brush to source pixel colors from.</param> /// <param name="pen">The pen to outline text with.</param> /// <param name="location">The location on the image to start drawing the text from.</param> public DrawTextProcessorCopy(TextGraphicsOptionsCopy options, string text, Font font, SolidBrushCopy brush, IPen pen, PointF location) { // Guard.NotNull(text, nameof(text)); // Guard.NotNull(font, nameof(font)); if (brush is null && pen is null) { throw new ArgumentNullException($"Expected a {nameof(brush)} or {nameof(pen)}. Both were null"); } this.Options = options; this.Text = text; this.Font = font; this.Location = location; this.Brush = brush; this.Pen = pen; }
public static void RenderTextProcessorWithAlignment( FontFamily fontFamily, string text, float pointSize = 12, IEnumerable <FontFamily> fallbackFonts = null) { foreach (VerticalAlignment va in (VerticalAlignment[])Enum.GetValues(typeof(VerticalAlignment))) { if (va != VerticalAlignment.Center) { //continue; } foreach (HorizontalAlignment ha in (HorizontalAlignment[])Enum.GetValues(typeof(HorizontalAlignment))) { if (ha != HorizontalAlignment.Center) { // continue; } var textOptions = new TextGraphicsOptionsCopy { ApplyKerning = true, DpiX = 96, DpiY = 96, RenderColorFonts = true, VerticalAlignment = va, HorizontalAlignment = ha }; if (fallbackFonts != null) { textOptions.FallbackFonts.AddRange(fallbackFonts); } var font = new Font(fontFamily, pointSize); var renderOptions = new RendererOptions(font, textOptions.DpiX, textOptions.DpiY) { ApplyKerning = true, ColorFontSupport = ColorFontSupport.MicrosoftColrFormat, FallbackFontFamilies = textOptions.FallbackFonts?.ToArray(), VerticalAlignment = va, HorizontalAlignment = ha }; FontRectangle textSize = TextMeasurer.Measure(text, renderOptions); using var img = new Image <Rgba32>(((int)textSize.Width * 2) + 20, ((int)textSize.Height * 2) + 20); Size size = img.Size(); img.Mutate(x => x.Fill(Color.White).ApplyProcessor( new DrawTextProcessorCopy( textOptions, text, font, new SolidBrushCopy(Color.Black), null, new PointF(size.Width / 2F, size.Height / 2F)))); string h = ha.ToString().Replace(nameof(HorizontalAlignment), string.Empty).ToLower(); string v = va.ToString().Replace(nameof(VerticalAlignment), string.Empty).ToLower(); string fullPath = CreatePath(font.Name, text + "-" + h + "-" + v + ".png"); Directory.CreateDirectory(System.IO.Path.GetDirectoryName(fullPath)); img.Save(fullPath); } } }