PrivateFontCollection

We found 10 examples in language CSharp for this search. You will see 30 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"));
        }

        public static void RegiterFonts ( )
        {
            
            PrivateFontCollection privateFontCollection=new PrivateFontCollection();
            privateFontCollection.AddFontFile( "Fonts\\calibri.ttf" );
            foreach ( FontFamily font in privateFontCollection.Families )
                FontFamiliesCollection.Add( font.Name , font );
        }


    private FontFamily LoadFont(byte[] fontResource)
    {
        int dataLength = fontResource.Length;
        IntPtr fontPtr = Marshal.AllocCoTaskMem(dataLength);
        Marshal.Copy(fontResource, 0, fontPtr, dataLength);

        uint cFonts = 0;
        AddFontMemResourceEx(fontPtr, (uint)fontResource.Length, IntPtr.Zero, ref cFonts);
        privateFontCollection.AddMemoryFont(fontPtr, dataLength);

        return privateFontCollection.Families.Last();
    }

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

        [ConditionalFact(Helpers.GdiplusIsAvailable)]
        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.GdiplusIsAvailable)]
        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.GdiplusIsAvailable)]
        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.GdiplusIsAvailable)]
        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.GdiplusIsAvailable)]
        public void AddFontFile_NullFileName_ThrowsArgumentNullException()
        {
            using (var fontCollection = new PrivateFontCollection())
            {
                AssertExtensions.Throws<ArgumentNullException>("path", () => fontCollection.AddFontFile(null));
            }
        }


        /// <include file='doc\PrivateFontCollection.uex' path='docs/doc[@for="PrivateFontCollection.Dispose"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Cleans up Windows resources for this
        ///    <see cref='System.Drawing.Text.PrivateFontCollection'/> .
        ///    </para>
        /// </devdoc>
        protected override void Dispose(bool disposing) {
            if (nativeFontCollection != IntPtr.Zero) {
                try{
#if DEBUG
                    int status = 
#endif            
                    SafeNativeMethods.Gdip.GdipDeletePrivateFontCollection(out nativeFontCollection);
#if DEBUG
                    Debug.Assert(status == SafeNativeMethods.Gdip.Ok, "GDI+ returned an error status: " + status.ToString(CultureInfo.InvariantCulture));
#endif        
                }
                catch( Exception ex ){
                    if( ClientUtils.IsSecurityOrCriticalException( ex ) ) {
                        throw;
                    }

                    Debug.Fail( "Exception thrown during Dispose: " + ex.ToString() );
                }
                finally{
                    nativeFontCollection = IntPtr.Zero;
                }
            }

            base.Dispose(disposing);
        }

        /// <include file='doc\PrivateFontCollection.uex' path='docs/doc[@for="PrivateFontCollection.AddFontFile"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Adds a font from the specified file to
        ///       this <see cref='System.Drawing.Text.PrivateFontCollection'/>.
        ///    </para>
        /// </devdoc>
        public void AddFontFile (string filename) {
            IntSecurity.DemandReadFileIO(filename);
            int status = SafeNativeMethods.Gdip.GdipPrivateAddFontFile(new HandleRef(this, nativeFontCollection), filename);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
                
            // Register private font with GDI as well so pure GDI-based controls (TextBox, Button for instance) can access it.
            SafeNativeMethods.AddFontFile( filename );
        }

        // 









        public void AddMemoryFont (IntPtr memory, int length) {
            IntSecurity.ObjectFromWin32Handle.Demand();

            int status = SafeNativeMethods.Gdip.GdipPrivateAddMemoryFont(new HandleRef(this, nativeFontCollection), new HandleRef(null, memory), length);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
        }


                                                                                                                    // ---------------------------------------------------------------------------------
        public void SetupFonts()                                                                                    // Method used to load and set up all the fonts that will be used in the application
        {                                                                                                           // ---------------------------------------------------------------------------------
            try
            {
                Roboto.AddFontFile(AppDomain.CurrentDomain.BaseDirectory + @"Fonts\RobotoCondensed-Regular.ttf");   // Load the Roboto Condensed font into the PrivateFontCollection.
                Roboto.AddFontFile(AppDomain.CurrentDomain.BaseDirectory + @"Fonts\RobotoCondensed-Italic.ttf");    // Load the Roboto Condensed Italic font into the PrivateFontCollection.
                Roboto.AddFontFile(AppDomain.CurrentDomain.BaseDirectory + @"Fonts\RobotoCondensed-Bold.ttf");      // Load the Roboto Condensed Bold font into the PrivateFontCollection.
                Roboto.AddFontFile(AppDomain.CurrentDomain.BaseDirectory + @"Fonts\RobotoCondensed-BoldItalic.ttf");// Load the Roboto Condensed Bold Italic font into the PrivateFontCollection.

                Roboto.AddFontFile(AppDomain.CurrentDomain.BaseDirectory + @"Fonts\Roboto-Regular.ttf");            // Load the Roboto font into the PrivateFontCollection.
                Roboto.AddFontFile(AppDomain.CurrentDomain.BaseDirectory + @"Fonts\Roboto-Bold.ttf");               // Load the Roboto Bold font into the PrivateFontCollection.
                Roboto.AddFontFile(AppDomain.CurrentDomain.BaseDirectory + @"Fonts\Roboto-Light.ttf");              // Load the Roboto light font into the PrivateFontCollection.
                Roboto.AddFontFile(AppDomain.CurrentDomain.BaseDirectory + @"Fonts\Roboto-Thin.ttf");               // Load the Roboto thin font into the PrivateFontCollection.

                // Regular font styles

                RobotoRegular = new Font(Roboto.Families[0], 12);                                                   // Set the Roboto font to the Roboto font family as present in the PrivateFontCollection.
                RobotoCondensed = new Font(Roboto.Families[1], 12);                                                 // Set the Roboto condensed font to the Roboto condensed font family as present in the PrivateFontCollection.
                RobotoLight = new Font(Roboto.Families[2], 12);                                                     // Set the Roboto condensed font to the Roboto condensed font family as present in the PrivateFontCollection.
                RobotoThin = new Font(Roboto.Families[3], 12);                                                      // Set the Roboto label font identically to the Roboto Condensed font but of size 8.5pt.

                // Regular Large Font Styles

                RobotoRegularLarge = new Font(Roboto.Families[0], 24);                                              // Set the Roboto font to the Roboto font family as present in the PrivateFontCollection.
                RobotoCondensedLarge = new Font(Roboto.Families[1], 24);                                            // Set the Roboto condensed font to the Roboto condensed font family as present in the PrivateFontCollection.
                RobotoLightLarge = new Font(Roboto.Families[2], 24);                                                // Set the Roboto light large font to the Roboto condensed font family as present in the PrivateFontCollection.

                // Custom Fonts  
                RobotoLargeTextbox = new Font(Roboto.Families[2], 20F);
                RobotoLargeButton = new Font(Roboto.Families[0], 27F);
                RobotoMidTextbox = new Font(Roboto.Families[0], 21.75F);                                              
                RobotoTopTitle = new Font(Roboto.Families[2], 48);
                RobotoTitleLabel = new Font(Roboto.Families[3], 44);
                RobotoTitleLabelSmall = new Font(Roboto.Families[3], 36);

                RobotoSmallTextboxLabel = new Font(Roboto.Families[2], 20.25F);                            
                RobotoSmallTextbox = new Font(Roboto.Families[2], 16F);                                                
                RobotoRegularText = new Font(Roboto.Families[2], 20.25F);

                // TinyUI
                RobotoTopTitleTinyUI = new Font(Roboto.Families[2], 20.25F);
                RobotoSpecialTextTinyUI = new Font(Roboto.Families[2], 15.75F);
                RobotoNumericUpDownTinyUI = new Font(Roboto.Families[1], 9.75F);
            }
            catch (Exception)
            {
                MessageBox.Show("The font files could not be found or loaded. This is bad, a very dire situation indeed.");
                // Inform the user that the font could not be found or loaded.
                RobotoRegular = new Font("Microsoft Sans Serif", 8);                                                 // Load Microsoft Sans Serif in place of the Roboto Regular font.
                RobotoCondensed = new Font("Microsoft Sans Serif", 11);                                               // Load Microsoft Sans Serif in place of the Roboto Condensed font.
                RobotoLabel = new Font("Microsoft Sans Serif", 8);                                                    // Load Microsoft Sans Serif in place of the Roboto Label font.
            }
        }

        /*
        public void SetupMainWindow(MainForm xMainWindow)
        {
            xMainWindow.lbl_ActionBar_BottomRight.Font = RobotoCondensed;                                                    // Set the font of the ActionBar/Toolstrip bottom right label to the Roboto Condensed font.
        }
        */

        public void SetupMainWindow(MainFormSmall xMainWindow)
        {
            xMainWindow.lbl_ActionBar_BottomRight.Font = RobotoCondensed;                                                    // Set the font of the ActionBar/Toolstrip bottom right label to the Roboto Condensed font.
        }


    //static XPrivateFontCollection()
    //{
    //  // HACK: Use one global PrivateFontCollection in GDI+
    //  // TODO: Make a list of it
    //  if (global != null)
    //    throw new InvalidOperationException("Because of limitations in GDI+ you can only have one instance of XPrivateFontCollection in your application.");
    //  global = this;
    //}
    //internal static XPrivateFontCollection global;

    /// <summary>
    /// Disposes all fonts from the collection.
    /// </summary>
    public void Dispose()
    {
#if GDI
      //privateFonts.Clear();
      this.privateFontCollection.Dispose();
      this.privateFontCollection = new PrivateFontCollection();
#endif
      s_global = null;
      //GC.SuppressFinalize(this);
    }

    /// <summary>
    /// Sets a new global font collection and returns the previous one, or null if no previous one exists.
    /// </summary>
    public static XPrivateFontCollection SetGlobalFontCollection(XPrivateFontCollection fontCollection)
    {
      if (fontCollection==null)
        throw new ArgumentNullException("fontCollection");

      XPrivateFontCollection old = s_global;
      s_global = fontCollection;
      return old;
    }


        /// <include file='doc\PrivateFontCollection.uex' path='docs/doc[@for="PrivateFontCollection.Dispose"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Cleans up Windows resources for this
        ///    <see cref='System.Drawing.Text.PrivateFontCollection'/> .
        ///    </para>
        /// </devdoc>
        protected override void Dispose(bool disposing) {
            if (nativeFontCollection != IntPtr.Zero) {
                try{
#if DEBUG
                    int status = 
#endif            
                    SafeNativeMethods.Gdip.GdipDeletePrivateFontCollection(out nativeFontCollection);
#if DEBUG
                    Debug.Assert(status == SafeNativeMethods.Gdip.Ok, "GDI+ returned an error status: " + status.ToString(CultureInfo.InvariantCulture));
#endif
                    if (gdiFonts != null) {
                        foreach (string fontFile in gdiFonts) {
                            SafeNativeMethods.RemoveFontFile(fontFile);
                        }
                        gdiFonts.Clear();
                        gdiFonts = null;
                    }
                }
                catch ( Exception ex ){
                    if( ClientUtils.IsSecurityOrCriticalException( ex ) ) {
                        throw;
                    }

                    Debug.Fail( "Exception thrown during Dispose: " + ex.ToString() );
                }
                finally{
                    nativeFontCollection = IntPtr.Zero;
                }
            }

            base.Dispose(disposing);
        }

        /// <include file='doc\PrivateFontCollection.uex' path='docs/doc[@for="PrivateFontCollection.AddFontFile"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Adds a font from the specified file to
        ///       this <see cref='System.Drawing.Text.PrivateFontCollection'/>.
        ///    </para>
        /// </devdoc>
        public void AddFontFile (string filename) {
            IntSecurity.DemandReadFileIO(filename);
            int status = SafeNativeMethods.Gdip.GdipPrivateAddFontFile(new HandleRef(this, nativeFontCollection), filename);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);

            // Register private font with GDI as well so pure GDI-based controls (TextBox, Button for instance) can access it.
            if (SafeNativeMethods.AddFontFile(filename) != 0) {
                if (gdiFonts != null) {
                    gdiFonts.Add(filename);
                }
            }
        }

        // 









        public void AddMemoryFont (IntPtr memory, int length) {
            IntSecurity.ObjectFromWin32Handle.Demand();

            int status = SafeNativeMethods.Gdip.GdipPrivateAddMemoryFont(new HandleRef(this, nativeFontCollection), new HandleRef(null, memory), length);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
        }


        /// <include file='doc\PrivateFontCollection.uex' path='docs/doc[@for="PrivateFontCollection.Dispose"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Cleans up Windows resources for this
        ///    <see cref='System.Drawing.Text.PrivateFontCollection'/> .
        ///    </para>
        /// </devdoc>
        protected override void Dispose(bool disposing) {
            if (nativeFontCollection != IntPtr.Zero) {
                try{
#if DEBUG
                    int status = 
#endif            
                    SafeNativeMethods.Gdip.GdipDeletePrivateFontCollection(out nativeFontCollection);
#if DEBUG
                    Debug.Assert(status == SafeNativeMethods.Gdip.Ok, "GDI+ returned an error status: " + status.ToString(CultureInfo.InvariantCulture));
#endif        
                }
                catch( Exception ex ){
                    if( ClientUtils.IsSecurityOrCriticalException( ex ) ) {
                        throw;
                    }

                    Debug.Fail( "Exception thrown during Dispose: " + ex.ToString() );
                }
                finally{
                    nativeFontCollection = IntPtr.Zero;
                }
            }

            base.Dispose(disposing);
        }

        /// <include file='doc\PrivateFontCollection.uex' path='docs/doc[@for="PrivateFontCollection.AddFontFile"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Adds a font from the specified file to
        ///       this <see cref='System.Drawing.Text.PrivateFontCollection'/>.
        ///    </para>
        /// </devdoc>
        public void AddFontFile (string filename) {
            IntSecurity.DemandReadFileIO(filename);
            int status = SafeNativeMethods.Gdip.GdipPrivateAddFontFile(new HandleRef(this, nativeFontCollection), filename);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
                
            // Register private font with GDI as well so pure GDI-based controls (TextBox, Button for instance) can access it.
            SafeNativeMethods.AddFontFile( filename );
        }

        // 









        public void AddMemoryFont (IntPtr memory, int length) {
            IntSecurity.ObjectFromWin32Handle.Demand();

            int status = SafeNativeMethods.Gdip.GdipPrivateAddMemoryFont(new HandleRef(this, nativeFontCollection), new HandleRef(null, memory), length);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
        }


        /// <include file='doc\PrivateFontCollection.uex' path='docs/doc[@for="PrivateFontCollection.Dispose"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Cleans up Windows resources for this
        ///    <see cref='System.Drawing.Text.PrivateFontCollection'/> .
        ///    </para>
        /// </devdoc>
        protected override void Dispose(bool disposing) {
            if (nativeFontCollection != IntPtr.Zero) {
                try{
#if DEBUG
                    int status = 
#endif            
                    SafeNativeMethods.Gdip.GdipDeletePrivateFontCollection(out nativeFontCollection);
#if DEBUG
                    Debug.Assert(status == SafeNativeMethods.Gdip.Ok, "GDI+ returned an error status: " + status.ToString(CultureInfo.InvariantCulture));
#endif        
                }
                catch( Exception ex ){
                    if( ClientUtils.IsSecurityOrCriticalException( ex ) ) {
                        throw;
                    }

                    Debug.Fail( "Exception thrown during Dispose: " + ex.ToString() );
                }
                finally{
                    nativeFontCollection = IntPtr.Zero;
                }
            }

            base.Dispose(disposing);
        }

        /// <include file='doc\PrivateFontCollection.uex' path='docs/doc[@for="PrivateFontCollection.AddFontFile"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Adds a font from the specified file to
        ///       this <see cref='System.Drawing.Text.PrivateFontCollection'/>.
        ///    </para>
        /// </devdoc>
        public void AddFontFile (string filename) {
            IntSecurity.DemandReadFileIO(filename);
            int status = SafeNativeMethods.Gdip.GdipPrivateAddFontFile(new HandleRef(this, nativeFontCollection), filename);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
                
            // Register private font with GDI as well so pure GDI-based controls (TextBox, Button for instance) can access it.
            SafeNativeMethods.AddFontFile( filename );
        }

        // 









        public void AddMemoryFont (IntPtr memory, int length) {
            IntSecurity.ObjectFromWin32Handle.Demand();

            int status = SafeNativeMethods.Gdip.GdipPrivateAddMemoryFont(new HandleRef(this, nativeFontCollection), new HandleRef(null, memory), length);

            if (status != SafeNativeMethods.Gdip.Ok)
                throw SafeNativeMethods.Gdip.StatusException(status);
        }

System.Drawing.Text.PrivateFontCollection : IDisposable

Constructors :

public PrivateFontCollection()

Methods :

public Void AddFontFile(String filename = )
public Void AddMemoryFont(IntPtr memory = , Int32 length = )
public FontFamily[] get_Families()
public Void Dispose()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()