Installing/Embedded Fonts

kimosavi

Active member
Joined
Apr 9, 2009
Messages
34
Location
Chicago, IL
Programming Experience
3-5
Hi. Wanted to start a new thread as a discussion on what will be the best way to install or embedd the fonts used on your project to the client machine.

The solution must be capable of working in XP/Vista.

Some of the topics that I found involves the following:

1. Embedding the fonts into the Assemby and calling them from memory.
(using Private Font Collections and installing them via AddFontMemResourceEx)

Works in XP (sometimes) but not in Vista

2. Embedding the fonts into the Assemby and calling them from memory.
(using Private Font Collections and installing them via TTEmbedFont and TTLoadEmbeddedFont)

HAVE NOT TRIED THIS, Seems too damn complicated for my knowledge, and most code samples are in C.


3. Embedding the fonts into the Assemby and calling them from a File
(using Private Font Collections and installing them via AddFontResourceEx)

Works in XP (sometimes) but not in Vista

4. Installing the fonts into the Windows\Fonts Folder @Runtime
@XP will have no problem, Vista will deny access)

5. Installing the fonts into the Windows\Fonts Folder @Installation
@XP will have no problem, Have not tried on Vista, but assuming the right permissions should work)


The purpose of this discussion is to find a solution that might work for everyone and post the code here for people to use it.

Your contributions will be highly appreciated.

if you need to see some code of the steps I have taken let me know.
 
I am in no way an expert in this area but some time back I managed to piece together a way of using an embedded .TTF font file embedded as a program resource (Resources/Other/Add Existing File...). You read it as a byte stream into a temporary file. Then you add the temporary file to a PrivateFontCollection.

Here's an example in a function. For the first argument you can use My.Resources.[resource name].

VB.NET:
Private Function ReadFontResource(ByVal ResourceFile As Byte(), _
                                       ByVal FontSize As Integer, ByVal Style As Drawing.FontStyle) As Font

        'Read the Resource Font File using a Filestream
    If Not IO.File.Exists("TempFile") Then
        Using fs As New System.IO.FileStream("TempFile", IO.FileMode.Create)
            fs.Write(ResourceFile, 0, ResourceFile.Length)
        End Using
    End If

        'Use the resulting File as a font
        Using pfc As New System.Drawing.Text.PrivateFontCollection
            pfc.AddFontFile("TempFile")
            Return New Font(pfc.Families(0), FontSize, Style)
        End Using

    End Function

I'm not sure if this is essentially different from what you have already tried but I tried it briefly under both XP and Vista and it seemed to work OK.

[Edit: I added a test for "TempFile" already existing in the above example. Apparently the file isn't quite as temporary as I thought!]

all the best, Vic
 
Last edited:
Back
Top