< Summary

Class:Imagini.Fonts.Renderers.GraphicsTextRenderer
Assembly:Imagini.Fonts
File(s):/home/razer/vscode-projects/project-grove/imagini/Imagini.Fonts/Renderers/GraphicsTextRenderer.cs
Covered lines:75
Uncovered lines:4
Coverable lines:79
Total lines:140
Line coverage:94.9% (75 of 79)
Covered branches:45
Total branches:50
Branch coverage:90% (45 of 50)

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor(...)2084.61%100%
Draw(...)26097.05%92.3%
Draw(...)10100%100%
Dispose()20100%50%
Measure(...)20096.15%90%

File(s)

/home/razer/vscode-projects/project-grove/imagini/Imagini.Fonts/Renderers/GraphicsTextRenderer.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Drawing;
 4using Imagini.Drawing;
 5using Imagini.ImageSharp;
 6
 7namespace Imagini.Fonts.Renderers
 8{
 9    public class GraphicsTextRenderer : Resource, ITextRenderer
 10    {
 9911        public SpriteFont Font { get; private set; }
 12        private Graphics _graphics;
 13
 614        private List<Texture> _pages = new List<Texture>();
 15
 616        public GraphicsTextRenderer(Graphics graphics, SpriteFont font,
 617            TextureScalingQuality quality = TextureScalingQuality.Linear)
 18        {
 619            Font = font;
 620            _graphics = graphics;
 3621            foreach(var page in font.Pages)
 22            {
 023                var texture = TextureFactory.FromImage(_graphics, quality,
 024                    page.Texture);
 1225                texture.BlendMode = BlendMode.AlphaBlend;
 1226                page.Texture.Dispose(); // dispose and unset the original image
 1227                page.Texture = null;
 1228                _pages.Add(texture);
 29            }
 630        }
 31
 32        public void Draw(string text, PointF position, Color color,
 33            FontDrawingOptions options = new FontDrawingOptions())
 34        {
 835            if (options.Equals(default(FontDrawingOptions)))
 436                options = FontDrawingOptions.Default;
 37
 838            if (options.Scale <= 0.0f)
 039                return;
 40
 4841            foreach (var tex in _pages)
 1642                tex.ColorMod = color;
 43
 844            var scale = options.Scale;
 845            var scaledSpacing = (int)(options.LetterSpacing * scale);
 46
 847            var pageIndex = 0;
 848            var page = Font.Pages[0];
 849            var x = (int)position.X;
 850            var y = (int)position.Y;
 851            var scaledFontSize = (int)(Font.Font.Size * scale);
 20852            foreach(var symbol in text)
 53            {
 9654                if (_pages.Count > 1)
 55                {
 4856                    if (symbol < page.Start || symbol > page.End)
 57                    {
 2258                        pageIndex = Font.GetPageIndex(symbol);
 2859                        if (pageIndex < 0) pageIndex = 0;
 2260                        page = Font.Pages[pageIndex];
 61                    }
 62                }
 9663                var srcRect = page.GetGlyph(symbol);
 9664                if (char.IsWhiteSpace(symbol))
 65                {
 866                    x += srcRect.IsEmpty ? scaledFontSize : (int)(srcRect.Width * scale);
 867                    x += scaledSpacing;
 868                    continue;
 69                }
 8870                if (srcRect.IsEmpty)
 71                {
 72                    // symbol not found, replace with a rectangle
 1273                    _graphics.DrawRect(new Rectangle(x, y, scaledFontSize, scaledFontSize));
 1274                    x += scaledFontSize + 1 + scaledSpacing;
 1275                    continue;
 76                }
 77
 7678                var scaledWidth = scale == 1.0f ? srcRect.Width : (int)(srcRect.Width * scale);
 7679                var scaledHeight = scale == 1.0f ? srcRect.Height: (int)(srcRect.Height * scale);
 7680                var dstRect = new Rectangle(x, y, scaledWidth, scaledHeight);
 7681                _graphics.Draw(_pages[pageIndex], srcRect, dstRect);
 7682                x += scaledWidth + scaledSpacing;
 83            }
 884        }
 85
 86        public void Draw(string text, PointF position, FontDrawingOptions options = default(FontDrawingOptions))
 87        {
 688            Draw(text, position, _graphics.GetDrawingColor(), options);
 689        }
 90
 91        public void Dispose()
 92        {
 693            if (IsDisposed) return;
 694            base.Destroy();
 695        }
 96
 97        public Size Measure(string text, FontDrawingOptions options = new FontDrawingOptions())
 98        {
 499            if (options.Equals(default(FontDrawingOptions)))
 2100                options = FontDrawingOptions.Default;
 4101            if (options.Scale <= 0.0f)
 0102                return Size.Empty;
 103
 4104            var scale = options.Scale;
 4105            var scaledSpacing = (int)(options.LetterSpacing * scale);
 106
 4107            var pageIndex = 0;
 4108            var page = Font.Pages[0];
 4109            int x = 0;
 4110            var scaledFontSize = (int)(Font.Font.Size * scale);
 104111            foreach(var symbol in text)
 112            {
 48113                if (_pages.Count > 1)
 114                {
 22115                    if (symbol < page.Start || symbol > page.End)
 116                    {
 12117                        pageIndex = Font.GetPageIndex(symbol);
 18118                        if (pageIndex < 0) pageIndex = 0;
 12119                        page = Font.Pages[pageIndex];
 120                    }
 121                }
 48122                var srcRect = page.GetGlyph(symbol);
 48123                if (char.IsWhiteSpace(symbol))
 124                {
 4125                    x += srcRect.IsEmpty ? scaledFontSize : (int)(srcRect.Width * scale);
 4126                    x += scaledSpacing;
 4127                    continue;
 128                }
 44129                if (srcRect.IsEmpty)
 130                {
 131                    // symbol not found, replace with a rectangle
 6132                    x += scaledFontSize + 1 + scaledSpacing;
 6133                    continue;
 134                }
 38135                x += (int)(srcRect.Width * scale) + scaledSpacing;
 136            }
 4137            return new Size(x, scaledFontSize);
 138        }
 139    }
 140}