How to create a Privatefontcollection

Asked

Viewed 64 times

1

I have some fonts that are not standard Windows, so I’m creating a PrivateFontCollection but I’m having a hard time.

These sources are used by the entire program, so I thought I’d create a Classe Static with a method get, but how could add the sources to the PrivateFontCollection? I know we use the method AddFontFile so I thought I’d add the fonts right after the user login, but I’m not getting it.

Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using DevExpress.CodeParser;

namespace Onee.Classes.Font
{
    public static class Fonts
    {
        private static PrivateFontCollection Font_collection;

        Font_collection.AddFontFile("C:\\Users\\thomas\\Documents\\Workspace\\Onee\\Onee Beta 1.10.16.2\\Onee\\Resources\\C_Light.otf");
        Font_collection.AddFontFile("C:\\Users\\thomas\\Documents\\Workspace\\Onee\\Onee Beta 1.10.16.2\\Onee\\Resources\\C_Bold.otf");

        public static PrivateFontCollection Collection
        {
            get { return Font.Fonts.Font_collection; }
        }
    }   
}

Form:

        private void set_font()
        {
            label4.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);
            label_usuario.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);
            label3.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);
            label2.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);
            label5.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);
            label1.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);

            button_servicos.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
            button_comercial.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
            button_certificados.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
            button_equipamentos.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
            button_compras.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
            simpleButton1.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
            simpleButton2.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
            button_opsistema.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
        }

Error:

inserir a descrição da imagem aqui

I was able to solve it this way:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using DevExpress.CodeParser;

namespace Onee.Classes.Font
{
    public static class Fonts
    {
        private static PrivateFontCollection Font_collection = new PrivateFontCollection();

        public static void add_font()
        {
            Font_collection.AddFontFile("C:\\Users\\thomas\\Documents\\Workspace\\Onee\\Onee Beta 1.10.16.2\\Onee\\Resources\\C_Light.otf");
            Font_collection.AddFontFile("C:\\Users\\thomas\\Documents\\Workspace\\Onee\\Onee Beta 1.10.16.2\\Onee\\Resources\\C_Bold.otf");
        }

        public static PrivateFontCollection Collection
        {
            get { return Font.Fonts.Font_collection; }
        }
    }
}

That’s the right approach?

1 answer

1


Some simple improvements to implement:

  1. Use@ before a string to interpret it literally. This helps in cases when there are too many characters to escape. Ex:

    Font_collection.AddFontFile("C:\\Meu\\Diretorio\\Super\\Obscuro");
    

    Vira:

    Font_collection.AddFontFile(@"C:\Meu\Diretorio\Super\Obscuro");
    
  2. Whether to add sources (method add_font()) only once, consider using a static builder:

    static Fonts(){                    
        Font_collection = new PrivateFontCollection();
        Font_collection.AddFontFile(@"C:\Fonte\Um");
        Font_collection.AddFontFile(@"C:\Fonte\Dois");
    }
    
  3. You do not need to create a new instance of Font for each control! Create one for a desired style and use it in all the Controls you want to own this font.

Still, some other items of its implementation are questionable (absolute paths to reference a file/resource?), but are very comprehensive and outside the scope of this question.

  • Friend, could you direct me, or explain to me about these absolute paths? I made a revision in the code, directing the path to the application path, using Directory.GetCurrentDirectory()

  • 1

    @Thomaserichpimentel the problem of using an absolute file path is that it makes your program less portable. Imagine that you want to put your new program on another computer. Using the code posted by you it would only work if you own a C:driver, and in this driver there exists a users folder, and in this folder exist the user Thomas, so on... It is much easier to use a directory related to your application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.