Results 1 to 6 of 6

Thread: Using a Font not installed on the Computer

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Using a Font not installed on the Computer

    Hi!!

    It may have happen to you a time where u needed to use a font that is not (or normally is not) installed in the target computer. You can install it, but installing fonts slows down the system and may need restart. So I went looking (well kleinma may have helped a little) and found this link http://www.bobpowell.net/embedfonts.htm. But as this codes deals with unmanaged memory (and some times can occur some errors), I changed the code to create a temporary file. In my code I used a very uncommon font TI83Pluspc, is the font used by the Graphic Calculator TI-83 Plus, so where is says "\TI-83PL.TTF" you should change by the name of your font file.

    First you need to add the font file to the project as you would an image, a sound, etc. Then use this code:
    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     Dim FontCollection As New System.Drawing.Text.PrivateFontCollection()
    3.     'Filestream that will create the temporary font file, uses IO.Path.GetTempPath so the file is created in a Temporary folder
    4.     Dim FileStream As New IO.FileStream(IO.Path.GetTempPath & "\TI-83PL.TTF", IO.FileMode.Create, IO.FileAccess.Write)
    5.     'Creates the File, you can use My.Resources.TI_83PL because its a byte array
    6.     FileStream.Write(My.Resources.TI_83PL, 0, My.Resources.TI_83PL.Length)
    7.     FileStream.Close()
    8.     'Adds the Font to the font collection
    9.     FontCollection.AddFontFile(IO.Path.GetTempPath & "\TI-83PL.TTF")
    10.  
    11.     Dim f As System.Drawing.Font
    12.     'We will need the FontFamily because of the font styles, because some fonts dont include all styles
    13.     Dim ff As FontFamily
    14.     'Loop through the Families
    15.     For Each ff In FontCollection.Families
    16.         'If the regular style is available then uses it, you can use some other style just change this "FontStyle.Regular"
    17.         'For the style you want. You can also change its size
    18.         If ff.IsStyleAvailable(FontStyle.Regular) Then f = New Font(ff, 8.25, FontStyle.Regular, GraphicsUnit.Point)
    19.     Next
    20.     'Assign the font to the controls you want to use it in
    21.     Textbox1.Font = f
    22. End Sub
    And to keep the temporary file from staying on the computer, occupying space you should use this:
    vb Code:
    1. Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    2.     'As IO.Path.GetTempPath is readonly you don't need to have the path assign to a variable, you can just use this
    3.     IO.File.Delete(IO.Path.GetTempPath & "\TI-83PL.TTF")
    4. End Sub

    I'm not sure if it will work with all Font types. But with TrueType works perfectly.
    Last edited by Lasering; Nov 13th, 2007 at 06:14 PM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    New Member
    Join Date
    Feb 2008
    Posts
    3

    Re: Using a Font not installed on the Computer

    I use the same code but i always face a Sharing violation when trying to delete the font file from the temp folder.

    Have you ever run into this issue ?

    Thanks

  3. #3
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Using a Font not installed on the Computer

    When I build the installer for the application, if it's using a font that's probably not going to be on the target system (the end user) I have the installer install the font to the user's font collection, this way my app knows it's there whether it's a common font or not.

    But if there's not an installer for an application, this method seems perfect for the on-the-fly font verification
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4
    New Member
    Join Date
    Feb 2008
    Posts
    3

    Re: Using a Font not installed on the Computer

    Quote Originally Posted by JuggaloBrotha
    When I build the installer for the application, if it's using a font that's probably not going to be on the target system (the end user) I have the installer install the font to the user's font collection, this way my app knows it's there whether it's a common font or not.

    But if there's not an installer for an application, this method seems perfect for the on-the-fly font verification

    Thanks but the idea for me is to have a small single Executable file as a tool with no install. So with the font embedded in my Resources in can create the font on the fly and I want to delete it at the end. But it seems somehow the PrivateFontCollection is having some handle on the .ttf file and even disposing and Garbage collection doesn't close this handle
    Thanks

  5. #5

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: Using a Font not installed on the Computer

    Well benprin i've runned into that error but I can't understand why it gives it. I also noticed that if u restart the program (or windows can't remember), and try to delete manualy you can. Its like you can put it there and use but then u can take it out because somehow windows says you are still using it.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  6. #6
    New Member
    Join Date
    Feb 2008
    Posts
    3

    Re: Using a Font not installed on the Computer

    Quote Originally Posted by Lasering
    Well benprin i've runned into that error but I can't understand why it gives it. I also noticed that if u restart the program (or windows can't remember), and try to delete manualy you can. Its like you can put it there and use but then u can take it out because somehow windows says you are still using it.
    Thanks Lasering, yeah it seems the PrivateFontCollection gets an handle on the file and is not able to close it before the end of the application . I tried to delete even after my form is disposed but even then the lock on the file is still present, even if no control references it anymore in application...
    I would say it's a bug of the PrivateFontCollection not releasing correctly its assets... but may be there's a reason for that...well anyway thanks for the confirmation !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width