FontCollection

We found 10 examples in language CSharp for this search. You will see 35 fragments of code.
        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void Ctor_Default()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                Assert.Empty(fontCollection.Families);
            }
        }

        [ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_NullFileName_ThrowsArgumentNullException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                AssertExtensions.Throws<ArgumentNullException>("filename", "path", () => fontCollection.AddFontFile(null));
            }
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_InvalidPath_ThrowsArgumentException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                AssertExtensions.Throws<ArgumentException>("path", null, () => fontCollection.AddFontFile(string.Empty));
            }
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_NoSuchFilePath_ThrowsFileNotFoundException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                Assert.Throws<FileNotFoundException>(() => fontCollection.AddFontFile("fileName"));
            }
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_Directory_ThrowsFileNotFoundException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                AssertExtensions.Throws<FileNotFoundException, ExternalException>(() => fontCollection.AddFontFile(AppContext.BaseDirectory));
            }
        }

        [ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_Disposed_ThrowsArgumentException()
        {
            var fontCollection = new PrivateFontCollection();
            fontCollection.Dispose();

            AssertExtensions.Throws<ArgumentException>(null, () => fontCollection.AddFontFile("fileName"));
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void Ctor_Default()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                Assert.Empty(fontCollection.Families);
            }
        }

        [ActiveIssue(20884, TestPlatforms.AnyUnix)]
        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_NullFileName_ThrowsArgumentNullException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                AssertExtensions.Throws<ArgumentNullException>("path", () => fontCollection.AddFontFile(null));
            }
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_InvalidPath_ThrowsArgumentException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                AssertExtensions.Throws<ArgumentException>("path", null, () => fontCollection.AddFontFile(string.Empty));
            }
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_NoSuchFilePath_ThrowsFileNotFoundException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                Assert.Throws<FileNotFoundException>(() => fontCollection.AddFontFile("fileName"));
            }
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_Directory_ThrowsFileNotFoundException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                AssertExtensions.Throws<FileNotFoundException, ExternalException>(() => fontCollection.AddFontFile(AppContext.BaseDirectory));
            }
        }

        [ActiveIssue(20884, TestPlatforms.AnyUnix)]
        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_Disposed_ThrowsArgumentException()
        {
            var fontCollection = new PrivateFontCollection();
            fontCollection.Dispose();

            AssertExtensions.Throws<ArgumentException>(null, () => fontCollection.AddFontFile("fileName"));
        }

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Drawing.Text;

namespace F5074.DevExpressWinforms.Report
{
    public partial class Report01 : DevExpress.XtraReports.UI.XtraReport
    {
        public Report01()
        {
            InitializeComponent();
            //customFontStyle.Font = new Font(FontCollection.Families[0], 20F, FontStyle.Regular, GraphicsUnit.Point);
            //xrLabel1.Font = new Font(FontCollection.Families[0], 20F, FontStyle.Regular, GraphicsUnit.Point);
        }

        static PrivateFontCollection fontCollection;
        public static FontCollection FontCollection
        {
            get
            {
                if (fontCollection == null)
                {
                    fontCollection = new PrivateFontCollection();
                    //fontCollection.AddFontFile("free3of9.ttf");
                }
                return fontCollection;
            }
        }

    }
}

        [ConditionalFact(Helpers.GdiplusIsAvailable)]
        public void Ctor_Default()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                Assert.Empty(fontCollection.Families);
            }
        }

        [ActiveIssue(20884, TestPlatforms.AnyUnix)]
        [ConditionalFact(Helpers.GdiplusIsAvailable)]
        public void AddFontFile_NullFileName_ThrowsArgumentNullException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                AssertExtensions.Throws<ArgumentNullException>("path", () => fontCollection.AddFontFile(null));
            }
        }

        [ConditionalFact(Helpers.GdiplusIsAvailable)]
        public void AddFontFile_InvalidPath_ThrowsArgumentException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                AssertExtensions.Throws<ArgumentException>("path", null, () => fontCollection.AddFontFile(string.Empty));
            }
        }

        [ConditionalFact(Helpers.GdiplusIsAvailable)]
        public void AddFontFile_NoSuchFilePath_ThrowsFileNotFoundException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                Assert.Throws<FileNotFoundException>(() => fontCollection.AddFontFile("fileName"));
            }
        }

        [ActiveIssue(20884, TestPlatforms.AnyUnix)]
        [ConditionalFact(Helpers.GdiplusIsAvailable)]
        public void AddFontFile_Disposed_ThrowsArgumentException()
        {
            var fontCollection = new PrivateFontCollection();
            fontCollection.Dispose();

            AssertExtensions.Throws<ArgumentException>(null, () => fontCollection.AddFontFile("fileName"));
        }

        [ConditionalFact(Helpers.GdiplusIsAvailable)]
        public void AddMemoryFont_ZeroMemory_ThrowsArgumentException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                AssertExtensions.Throws<ArgumentException>(null, () => fontCollection.AddMemoryFont(IntPtr.Zero, 100));
            }
        }

        [ConditionalFact(Helpers.GdiPlusIsAvailableNotRedhat73)]
        public void Ctor_Default()
        {
            using (var fontCollection = new InstalledFontCollection())
            {
                Assert.NotEmpty(fontCollection.Families);
            }
        }

        [ConditionalFact(Helpers.GdiPlusIsAvailableNotRedhat73)]
        public void Families_GetWhenDisposed_ReturnsNonEmpty()
        {
            var fontCollection = new InstalledFontCollection();
            fontCollection.Dispose();

            Assert.NotEmpty(fontCollection.Families);
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void Dispose_MultipleTimes_Nop()
        {
            var fontCollection = new InstalledFontCollection();
            fontCollection.Dispose();
            fontCollection.Dispose();
        }


        /// <include file='doc\FontCollection.uex' path='docs/doc[@for="FontCollection.Dispose"]/*' />
        /// <devdoc>
        ///    Disposes of this <see cref='System.Drawing.Text.FontCollection'/>
        /// </devdoc>
        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void Ctor_Default()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                Assert.Empty(fontCollection.Families);
            }
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_AbsolutePath_Success()
        {
            // GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
            if (PlatformDetection.IsWindows7)
            {
                return;
            }

            using (var fontCollection = new PrivateFontCollection())
            {
                fontCollection.AddFontFile(Helpers.GetTestBitmapPath("empty.file"));
                fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.otf"));

                FontFamily fontFamily = Assert.Single(fontCollection.Families);
                Assert.Equal("Code New Roman", fontFamily.Name);
            }
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_RelativePath_Success()
        {
            // GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
            if (PlatformDetection.IsWindows7)
            {
                return;
            }

            using (var fontCollection = new PrivateFontCollection())
            {
                string relativePath = Path.Combine("fonts", "CodeNewRoman.ttf");
                fontCollection.AddFontFile(relativePath);

                FontFamily fontFamily = Assert.Single(fontCollection.Families);
                Assert.Equal("Code New Roman", fontFamily.Name);
            }
        }

        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_SamePathMultipleTimes_FamiliesContainsOnlyOneFont()
        {
            // GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
            if (PlatformDetection.IsWindows7)
            {
                return;
            }

            using (var fontCollection = new PrivateFontCollection())
            {
                fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.ttf"));
                fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.ttf"));

                FontFamily fontFamily = Assert.Single(fontCollection.Families);
                Assert.Equal("Code New Roman", fontFamily.Name);
            }
        }

        [ActiveIssue(20884, TestPlatforms.AnyUnix)]
        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_SameNameMultipleTimes_FamiliesContainsFirstFontOnly()
        {
            // GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
            if (PlatformDetection.IsWindows7)
            {
                return;
            }

            using (var fontCollection = new PrivateFontCollection())
            {
                fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.ttf"));
                fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.otf"));

                // Verify that the first file is used by checking that it contains metadata
                // associated with CodeNewRoman.ttf.
                const int FrenchLCID = 1036;
                FontFamily fontFamily = Assert.Single(fontCollection.Families);
                Assert.Equal("Code New Roman", fontFamily.Name);
                Assert.Equal("Bonjour", fontFamily.GetName(FrenchLCID));
            }
        }

        [ActiveIssue(20884, TestPlatforms.AnyUnix)]
        [ConditionalFact(Helpers.IsDrawingSupported)]
        public void AddFontFile_NullFileName_ThrowsArgumentNullException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                AssertExtensions.Throws<ArgumentNullException>("path", () => fontCollection.AddFontFile(null));
            }
        }

        public Font ResolveFont(string familyName, float emSize, FontStyle fontStyle, GraphicsUnit unit)
        {
            Font fontTester = new Font(familyName, emSize, fontStyle, unit);
            if (fontTester.Name == familyName || !TryResolve(ref familyName, ref fontStyle) )
            {
                return fontTester;
            }
            fontTester.Dispose();

            FontFamily fontFamily = GetFontFamily(familyName);
            return new Font(fontFamily, emSize, fontStyle, unit);
        }

        private static bool TryResolve(ref string familyName, ref FontStyle fontStyle)
        {
            if (familyName == "Segoe UI Light")
            {
                familyName = OPEN_SANS_LIGHT;
                if( fontStyle != FontStyle.Bold) fontStyle = FontStyle.Regular;
                return true;
            }

            if (familyName == "Segoe UI")
            {
                if (fontStyle == FontStyle.Bold)
                {
                    familyName = OPEN_SANS_BOLD;
                    return true;
                }

                familyName = OPEN_SANS_REGULAR;
                return true;
            }

            return false;
        }

        private FontFamily GetFontFamily(string familyName)
        {
            lock (fontCollection)
            {
                foreach (FontFamily fontFamily in fontCollection.Families)
                    if (fontFamily.Name == familyName) return fontFamily;

                string resourceName = GetType().Namespace + ".Resources." + familyName.Replace(' ', '_') + ".ttf";

                Stream fontStream = null;
                IntPtr data = IntPtr.Zero;
                try
                {
                    fontStream = GetType().Assembly.GetManifestResourceStream(resourceName);
                    int bytes = (int)fontStream.Length;
                    data = Marshal.AllocCoTaskMem(bytes);
                    byte[] fontdata = new byte[bytes];
                    fontStream.Read(fontdata, 0, bytes);
                    Marshal.Copy(fontdata, 0, data, bytes);
                    fontCollection.AddMemoryFont(data, bytes);
                    return fontCollection.Families[fontCollection.Families.Length - 1];
                }
                finally
                {
                    if (fontStream != null) fontStream.Dispose();
                    if (data != IntPtr.Zero) Marshal.FreeCoTaskMem(data);
                }
            }
        }

		public static IFontCollection AddFont(this IFontCollection fontCollection, string filename, string? alias = null)
		{
			_ = filename ?? throw new ArgumentNullException(nameof(filename));
			if (string.IsNullOrWhiteSpace(filename))
				throw new ArgumentException("Filename was not a valid file name.", nameof(filename));

			fontCollection.Add(new FontDescriptor(filename, alias, null));
			return fontCollection;
		}

		public static IFontCollection AddEmbeddedResourceFont(this IFontCollection fontCollection, Assembly assembly, string filename, string? alias = null)
		{
			_ = assembly ?? throw new ArgumentNullException(nameof(assembly));
			_ = filename ?? throw new ArgumentNullException(nameof(filename));
			if (string.IsNullOrWhiteSpace(filename))
				throw new ArgumentException("Filename was not a valid file name.", nameof(filename));

			fontCollection.Add(new FontDescriptor(filename, alias, assembly));
			return fontCollection;
		}


        /// <include file='doc\FontCollection.uex' path='docs/doc[@for="FontCollection.Dispose"]/*' />
        /// <devdoc>
        ///    Disposes of this <see cref='System.Drawing.Text.FontCollection'/>
        /// </devdoc>
        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

System.DrawingCore.Text.FontCollection : IDisposable

Methods :

public Void Dispose()
public FontFamily[] get_Families()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()

Other methods