From bd65fc543cd38ea48afc508d4f60c6bc6df5ef56 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Mon, 18 Jun 2018 23:50:41 +0200 Subject: [PATCH] CachingGlyphRenderer: use a 1 dictionary instead of 3 --- .../Text/Processors/DrawTextProcessor.cs | 71 +++++++++++-------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/src/ImageSharp.Drawing/Processing/Text/Processors/DrawTextProcessor.cs b/src/ImageSharp.Drawing/Processing/Text/Processors/DrawTextProcessor.cs index f86bd827f..9327f9449 100644 --- a/src/ImageSharp.Drawing/Processing/Text/Processors/DrawTextProcessor.cs +++ b/src/ImageSharp.Drawing/Processing/Text/Processors/DrawTextProcessor.cs @@ -167,19 +167,19 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors private PathBuilder builder; private Point currentRenderPosition = default; - private GlyphRendererParameters currentRenderingGlyph = default; + private GlyphRendererParameters currentGlyphRenderParams = default; private int offset = 0; private PointF currentPoint = default(PointF); - private HashSet renderedGlyphs = new HashSet(); - private Dictionary> glyphMap; - private Dictionary> glyphMapPen; + + private readonly Dictionary glyphData = new Dictionary(); + private bool renderOutline = false; private bool renderFill = false; private bool raterizationRequired = false; - public CachingGlyphRenderer(MemoryAllocator memoryManager, int size, IPen pen, bool renderFill) + public CachingGlyphRenderer(MemoryAllocator memoryAllocator, int size, IPen pen, bool renderFill) { - this.MemoryManager = memoryManager; + this.MemoryAllocator = memoryAllocator; this.Pen = pen; this.renderFill = renderFill; this.renderOutline = pen != null; @@ -187,14 +187,12 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors if (this.renderFill) { this.FillOperations = new List(size); - this.glyphMap = new Dictionary>(); } if (this.renderOutline) { this.offset = (int)MathF.Ceiling((pen.StrokeWidth * 2) + 2); this.OutlineOperations = new List(size); - this.glyphMapPen = new Dictionary>(); } this.builder = new PathBuilder(); @@ -204,7 +202,7 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors public List OutlineOperations { get; } - public MemoryAllocator MemoryManager { get; internal set; } + public MemoryAllocator MemoryAllocator { get; internal set; } public IPen Pen { get; internal set; } @@ -221,8 +219,8 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors // we have offset our rendering origion a little bit down to prevent edge cropping, move the draw origin up to compensate this.currentRenderPosition = new Point(this.currentRenderPosition.X - this.offset, this.currentRenderPosition.Y - this.offset); - this.currentRenderingGlyph = paramters; - if (this.renderedGlyphs.Contains(paramters)) + this.currentGlyphRenderParams = paramters; + if (this.glyphData.ContainsKey(paramters)) { // we have already drawn the glyph vectors skip trying again this.raterizationRequired = false; @@ -254,21 +252,12 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors public void Dispose() { - if (this.renderFill) + foreach (KeyValuePair kv in this.glyphData) { - foreach (KeyValuePair> m in this.glyphMap) - { - m.Value.Dispose(); - } + kv.Value.Dispose(); } - if (this.renderOutline) - { - foreach (KeyValuePair> m in this.glyphMapPen) - { - m.Value.Dispose(); - } - } + this.glyphData.Clear(); } public void EndFigure() @@ -278,13 +267,16 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors public void EndGlyph() { + GlyphRenderData renderData = default; + // has the glyoh been rendedered already???? if (this.raterizationRequired) { IPath path = this.builder.Build(); + if (this.renderFill) { - this.glyphMap[this.currentRenderingGlyph] = this.Render(path); + renderData.FillMap = this.Render(path); } if (this.renderOutline) @@ -298,10 +290,14 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors path = path.GenerateOutline(this.Pen.StrokeWidth, this.Pen.StrokePattern); } - this.glyphMapPen[this.currentRenderingGlyph] = this.Render(path); + renderData.OutlineMap = this.Render(path); } - this.renderedGlyphs.Add(this.currentRenderingGlyph); + this.glyphData[this.currentGlyphRenderParams] = renderData; + } + else + { + renderData = this.glyphData[this.currentGlyphRenderParams]; } if (this.renderFill) @@ -309,7 +305,7 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors this.FillOperations.Add(new DrawingOperation { Location = this.currentRenderPosition, - Map = this.glyphMap[this.currentRenderingGlyph] + Map = renderData.FillMap }); } @@ -318,7 +314,7 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors this.OutlineOperations.Add(new DrawingOperation { Location = this.currentRenderPosition, - Map = this.glyphMapPen[this.currentRenderingGlyph] + Map = renderData.OutlineMap }); } } @@ -341,10 +337,10 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors } // take the path inside the path builder, scan thing and generate a Buffer2d representing the glyph and cache it. - Buffer2D fullBuffer = this.MemoryManager.Allocate2D(size.Width + 1, size.Height + 1, true); + Buffer2D fullBuffer = this.MemoryAllocator.Allocate2D(size.Width + 1, size.Height + 1, true); - using (IBuffer bufferBacking = this.MemoryManager.Allocate(path.MaxIntersections)) - using (IBuffer rowIntersectionBuffer = this.MemoryManager.Allocate(size.Width)) + using (IBuffer bufferBacking = this.MemoryAllocator.Allocate(path.MaxIntersections)) + using (IBuffer rowIntersectionBuffer = this.MemoryAllocator.Allocate(size.Width)) { float subpixelFraction = 1f / subpixelCount; float subpixelFractionPoint = subpixelFraction / subpixelCount; @@ -457,6 +453,19 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors this.builder.AddBezier(this.currentPoint, secondControlPoint, point); this.currentPoint = point; } + + private struct GlyphRenderData : IDisposable + { + public Buffer2D FillMap; + + public Buffer2D OutlineMap; + + public void Dispose() + { + this.FillMap?.Dispose(); + this.OutlineMap?.Dispose(); + } + } } } } \ No newline at end of file