Using fonts in app without installing them?

Hey all,
I want to use .ttf fonts in Windows in my Xojo app, without installing them in the system. How would I do this? Latest version of Xojo, desktop version. If I include the .ttf font files in my app directory, will I then be able to use them without installing them? Do I need to embed them into the Xojo app itself?

do you use MBS Plugin?
There is a function: folderitem.FontActivateMBS(OnlyLocal as boolean) as integer

For Mac and Windows.

see
https://www.monkeybreadsoftware.net/fonts-folderitem-method.shtml#1

What you want to do has to be carried out with calls to the Win32 Library API. The font file has to be present on the disk, then you can make it available to the system while your application is running with CreateScalableFontResource function, followed by AddFontResource.
https://msdn.microsoft.com/en-us/library/windows/desktop/dd183517(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/dd183326(v=vs.85).aspx
When your program ends, you must call RemoveFontResource
https://msdn.microsoft.com/en-us/library/windows/desktop/dd162922(v=vs.85).aspx

Or use the plugin Christian just talked about.

Otherwise, all common installer programs do install fonts for you.

Michel: I’m installing an app for Steam, so need to be able to use the fonts without a typical Windows installer. How would I do what you described in Xojo, that will work for Win XP through Windows 8?

See https://github.com/arbp/WFS

The enclosed method /UI Extras/Modules/UIExtraWFS/TemporarilyInstallFont should do that for you.

So I add this code by: Add Module, then add a Method to it? And how would I call it to temporarily install a font in the current running folder named arial.ttf for example?

Get WFS from the repository I posted the link to (download the zip to have everything). Inside, look for the TemporaryInstallFont method at the path I told you inside the project.
In your project, add that method to a module. Make sure to set it as Glocal.

Then do something like this :

dim f as folderitem = SpecialFolder.Desktop.child("calebasse.ttf") TemporarilyInstallFont(f,false) Label1.TextSize = 20 Label1.TextFont = "calebasse"

How exactly do I add it, so I can use it in the app? Step by step, if you could, please. I create a module, add the method to it in the app, and it tells me if I reference ‘TemporarilyInstallFont’ in the main window that loads and its code that it ‘does not exist’. I did make the module Global. Do I have to copy the whole code and plug it in there without changing it at all?

If I just copy this by itself to a module, adding a method:

#tag Method, Flags = &h1 Protected Sub TemporarilyInstallFont(fontFile as FolderItem, privateFont as Boolean = true) #if TargetWin32 Soft Declare Sub AddFontResourceExW Lib "Gdi32" ( filename as WString, flags as Integer, reserved as Integer ) Soft Declare Sub AddFontResourceA Lib "Gdi32" ( filename as CString ) Soft Declare Sub AddFontResourceW Lib "Gdi32" ( filename as WString ) Const FR_PRIVATE = &h10 if privateFont and System.IsFunctionAvailable( "AddFontResourceExW", "Gdi32" ) then AddFontResourceExW( fontFile.AbsolutePath, FR_PRIVATE, 0 ) else if System.IsFunctionAvailable( "AddFontResourceW", "Gdi32" ) then AddFontResourceW( fontFile.AbsolutePath ) else AddFontResourceA( fontFile.AbsolutePath ) end if end if #else #pragma unused fontFile #pragma unused privateFont #endif End Sub #tag EndMethod

I get a Syntax Error at ‘End Sub’.

[quote=161970:@Derek DiBenedetto]If I just copy this by itself to a module, adding a method:

#tag Method, Flags = &h1 Protected Sub TemporarilyInstallFont(fontFile as FolderItem, privateFont as Boolean = true) #if TargetWin32 Soft Declare Sub AddFontResourceExW Lib "Gdi32" ( filename as WString, flags as Integer, reserved as Integer ) Soft Declare Sub AddFontResourceA Lib "Gdi32" ( filename as CString ) Soft Declare Sub AddFontResourceW Lib "Gdi32" ( filename as WString ) Const FR_PRIVATE = &h10 if privateFont and System.IsFunctionAvailable( "AddFontResourceExW", "Gdi32" ) then AddFontResourceExW( fontFile.AbsolutePath, FR_PRIVATE, 0 ) else if System.IsFunctionAvailable( "AddFontResourceW", "Gdi32" ) then AddFontResourceW( fontFile.AbsolutePath ) else AddFontResourceA( fontFile.AbsolutePath ) end if end if #else #pragma unused fontFile #pragma unused privateFont #endif End Sub #tag EndMethod

I get a Syntax Error at ‘End Sub’.[/quote]

remove end sub

You could simply copy the method from WFS. Select it in the navigator, right click/Copy.
Then select the module in your project, right click/Paste. The method will be added to it.
Then select the method in your project module, right click it, and select “Inspect”
Make sure it is “Global”

If I do that, I get a ton of “does not exist” errors in my code where I try to use the command. I inspected it, and it is Global.

If I paste in the whole thing, then the ‘End Function’ lines give syntax errors…apparently this code is bugged, more than likely. Anyone have other suggestions for getting this done?

Here is another way I just tested to work, step by step :

  • Open the Windows Functionaity Suite project
  • Select the “Windows Functionality Suite” folder in the WFS project. Go Edit/Copy
  • Create a new project. Go Edit menu/Paste

This will put all the methods in your project. Do not worry about the size, only the methods used will be added to the compiled program.

  • Inside the folder, open Ui Extras/Modules/UIExtrasWFS/Methods
  • Select TemporaryInstallFont
  • Right click, select “TemporaryInstallFont”
  • In the inspector, change the scope from “Protected” to “Global”

Now we need to remove a bug in WFS

  • Open UI Extras/Classes/StatusBarWFS/Methods/Constructor/Owner as Window

  • Comment the first line ConstructorHelper( owner.Handle, owner.GrowIcon )

  • Place a label on the window

  • Add a button and double click on it.

  • In the action event, put the Xojo code I posted above for my font Calebasse

  • Change the code to point to the font you want to install, modify Label1.TextFont accordingly.

I have written that verifying step by step the instructions. Follow that and you should be fine.

[quote]Open the Windows Functionaity Suite project
Select the “Windows Functionality Suite” folder in the WFS project. Go Edit/Copy[/quote]

I tried selecting it (using Firefox) and it switches to a multiple folder view. There is no way to do Edit/Copy…they’re greyed out.

You have to download the project from github first…

As I told you, download the zip (on the right hand side), then when it is on your disk double click on it to open. Then drag the WFS folder onto your disk to extract.

Ok, I followed the directions, got it set up, specified the font for a TextField on a screen after loading it, so it types in that font:

Dim f as Folderitem f = SpecialFolder.Desktop.child("Anti-everything.ttf") TemporarilyInstallFont(f,false) TextField1.TextFont="Anti Everything" TextField1.TextSize=30

Font is located in same folder as .exe. It does not work; it types in some large basic skinny system font. Am I doing anything wrong? File name and font system name are correct. Another label and button caption doesn’t seem to work right either:

f = SpecialFolder.Desktop.child("CorleoneDue.ttf") TemporarilyInstallFont(f,false) f = SpecialFolder.Desktop.child("TCM_____.ttf") TemporarilyInstallFont(f,false) PushButton1.TextFont="Corleone Due" PushButton1.TextSize=28 PushButton1.Caption="OK" Label6.TextFont="Tw Cen MT" label6.Text="Choose your Influence Goal for this game:"

Both seem to use some skinny system font instead of the designated font.

Test system does not have the fonts installed and is Windows 7, fully updated.

[quote=162135:@Derek DiBenedetto]Ok, I followed the directions, got it set up, specified the font for a TextField on a screen after loading it, so it types in that font:

Dim f as Folderitem f = SpecialFolder.Desktop.child("Anti-everything.ttf") TemporarilyInstallFont(f,false) TextField1.TextFont="Anti Everything" TextField1.TextSize=30

Font is located in same folder as .exe. It does not work; it types in some large basic skinny system font. Am I doing anything wrong? File name and font system name are correct. Another label and button caption doesn’t seem to work right either:

f = SpecialFolder.Desktop.child("CorleoneDue.ttf") TemporarilyInstallFont(f,false) f = SpecialFolder.Desktop.child("TCM_____.ttf") TemporarilyInstallFont(f,false) PushButton1.TextFont="Corleone Due" PushButton1.TextSize=28 PushButton1.Caption="OK" Label6.TextFont="Tw Cen MT" label6.Text="Choose your Influence Goal for this game:"

Both seem to use some skinny system font instead of the designated font.[/quote]

Derek, from your code the font should be on the desktop, not next to the exe. That is probably the problem. Place the Anti-everything.ttf file on the desktop, and you should be fine.