< Summary

Class:Imagini.Fonts.SpriteFont
Assembly:Imagini.Fonts
File(s):/home/razer/vscode-projects/project-grove/imagini/Imagini.Fonts/SpriteFont.cs
Covered lines:25
Uncovered lines:3
Coverable lines:28
Total lines:74
Line coverage:89.2% (25 of 28)
Covered branches:8
Total branches:10
Branch coverage:80% (8 of 10)

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor(...)4093.75%100%
GetPageIndex(...)2033.33%50%
Dispose()40100%75%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using SixLabors.Fonts;
 3using System.Linq;
 4using System;
 5
 6/// <summary>
 7/// Sprite font generation and drawing module.
 8/// </summary>
 9namespace Imagini.Fonts
 10{
 11    /// <summary>
 12    /// Represents a sprite font.
 13    /// </summary>
 14    public class SpriteFont : IDisposable
 15    {
 16        /// <summary>
 17        /// Returns the pages of this sprite font.
 18        /// </summary>
 7119        public IReadOnlyList<SpriteFontPage> Pages { get; private set; }
 20
 21        /// <summary>
 22        /// Gets the font for which this sprite font was generated.
 23        /// </summary>
 2224        public Font Font { get; private set; }
 25
 926        private List<(char start, char end)> _rangeLookup = new List<(char start, char end)>();
 27
 028        public SpriteFont(Font font, IEnumerable<char> symbols,
 929            int textureSize = 512, int padding = 1)
 30        {
 931            ISet<char> chars = Symbols.GetPrintable(symbols);
 932            var pages = new List<SpriteFontPage>();
 33            do
 34            {
 1635                var before = chars.Count;
 1636                var page = new SpriteFontPage(font, ref chars, textureSize, padding);
 1637                var after = chars.Count;
 1638                if (after == before)
 139                    throw new ImaginiException("Texture size is too small");
 1540                pages.Add(page);
 1541                _rangeLookup.Add((page.Start, page.End));
 1542            } while (chars.Any());
 843            Pages = pages;
 844            Font = font;
 845        }
 46
 47        /// <summary>
 48        /// Returns index of the page that can contain the specified glyph.
 49        /// Returns -1 if the glyph code point is out of range.
 50        /// </summary>
 51        public int GetPageIndex(char glyph)
 52        {
 053            return _rangeLookup.FindIndex(pair =>
 054                glyph >= pair.start &&
 8455                glyph <= pair.end);
 56        }
 57
 58        /// <summary>
 59        /// Indicates if this object is disposed.
 60        /// </summary>
 461        public bool IsDisposed { get; private set; }
 62        /// <summary>
 63        /// Disposes the object.
 64        /// </summary>
 65        public void Dispose()
 66        {
 267            if (IsDisposed) return;
 1068            foreach(var page in Pages)
 369                page.Texture.Dispose();
 270            Pages = null;
 271            IsDisposed = true;
 272        }
 73    }
 74}