Embedding custom fonts

45 replies [Last post]
Eric
User offline. Last seen 1 year 23 weeks ago. Offline
Joined: 23 Jun 2009

You can embed a custom font in your app. This feature's been in for a little while, but wasn't documented. So I thought it'd be worth bringing it to your attention.

Replies

secure411dotorg
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 13 Oct 2010

Please provide an example showing a complete build.settings file for Android that includes a custom font. Or point to a reference that explains all the elements of the build settings.

Here was my wild guess trying to adapt the iphone version shown in the ansca example for embedding font

settings =
{
androidPermissions =
{
"android.permission.INTERNET"
},

android =
{
plist =
{
UIAppFonts =
{
"VeraMono.ttf"
}
},
},
}

Eric
User offline. Last seen 1 year 23 weeks ago. Offline
Joined: 23 Jun 2009

You don't need an entry on Android. I updated the docs to reflect that.

jonbeebe
User offline. Last seen 1 week 6 days ago. Offline
Joined: 26 Jul 2010

That explains why I was going through the docs yesterday, noticed it and thought... whoah I didn't know you could do that!

XenonBL's picture
XenonBL
User is online Online
Joined: 24 Sep 2010

Can anyone verify that this works on an actual device? Following the instructions here I can see and use my custom font in the Corona Sim and the Xcode Sim, but when I build for an actual device the font is absent. (ie - buttons that use the font for text labels look correct on both sims, but on the iPhone itself the buttons are blank.)

tokyodan's picture
tokyodan
User offline. Last seen 6 days 19 hours ago. Offline
Joined: 24 Jun 2009

THis works for me. In simulaator AND on my iPhone 3GS. Here is my build.settings file:

settings = {
iphone = {
plist = {
UIApplicationExitsOnSuspend = true,
UIAppFonts = {
"SFDigitalReadout-Heavy.ttf"
}
},
},
}

And in my project folder along with all source (*.lua), graphics, and sound files is:

SFDigitalReadout-Heavy.ttf

IgnacioIturra
User offline. Last seen 26 weeks 3 days ago. Offline
Joined: 31 Oct 2010

Yes I can also confirm it works. Though you'll need iOS 3.2. It's worth noting if you're planning to support the original iphone (can't install 3.2.), you'd be better off with bitmap text.

XenonBL's picture
XenonBL
User is online Online
Joined: 24 Sep 2010

Thanks for the confirmation, tokyodan and Ignaciolturra.

It turns out the font I was trying to use had the extention ".TTF" as opposed to ".ttf", which is how I had it in my build.settings. I guess OSX (and the simulators) are smart enough to handle that, but iOS isn't.

Also, Ignaciol, thanks for the heads up about the compatibility issue with original iPhone. I'm on the fence about supporting it since, currently, my game runs at about half speed on it vs. 3GS and 4. I wonder if there's a way to detect which phone is being used and fallback to a system font if it's an original iPhone.

XenonBL's picture
XenonBL
User is online Online
Joined: 24 Sep 2010

My custom font is now working on the devices, but there's another problem. Text using the custom font is shifted up a few pixels. It looks particularly bad when using the font for button label text since the font creeps over the top edge of my button graphic.

I Googled around and it seems this is a problem for people trying to use custom fonts on iPhones after iOS 3, when Apple changed the UPM for fonts from 1000 to 2048. I don't understand the problem, or what the solution might be, all I know is the font is aligned correctly in the Corona simulator and OSX, but is misaligned vertically on my iPhone. Anyone else notice their custom fonts misaligned on an actual device? Is this a bug for Ansca?

joemauke's picture
joemauke
User offline. Last seen 7 weeks 4 days ago. Offline
Joined: 14 Oct 2010

is there something Im missing? I have done everything pretty much to the letter what was written here. Was the font supposed to be in a certain folder?

IgnacioIturra
User offline. Last seen 26 weeks 3 days ago. Offline
Joined: 31 Oct 2010

Yes, you have to copy it to your project's root folder as well.

joemauke's picture
joemauke
User offline. Last seen 7 weeks 4 days ago. Offline
Joined: 14 Oct 2010

yes, did that as well as all the other directions to the letter

IgnacioIturra
User offline. Last seen 26 weeks 3 days ago. Offline
Joined: 31 Oct 2010

The only other thing I can think of is that you have the name of the font wrong. It's not necessarily the name of the file, but how it shows in other programs. Try using the font in something else and see what it's called (Also try different fonts to try and narrow down the issue).

joemauke's picture
joemauke
User offline. Last seen 7 weeks 4 days ago. Offline
Joined: 14 Oct 2010

I used FONTBOOK in osx to find the font name...although I used the sample code on showing native fonts on the device, and my font wasnt in there...no matter what the name was exactly. even the harrowprint exmple font provided didnt show, and my ipad has the latest iOS ver.

snarla's picture
snarla
User offline. Last seen 16 weeks 22 hours ago. Offline
Alumni
Joined: 25 May 2010

Hi, following up here in case someone else has a similar problem.

Another thing to check in your build.settings file is that the UIAppFonts entry needs to be a list, even if it only has one entry. In other words, it should look like this:

UIAppFonts = {
"SFDigitalReadout-Heavy.ttf"
}

This will not work:

UIAppFonts = "SFDigitalReadout-Heavy.ttf"

joemauke's picture
joemauke
User offline. Last seen 7 weeks 4 days ago. Offline
Joined: 14 Oct 2010

Ok, so now the font finally shows up...thanks for the heads up on how to get it right in the build file, but it would appear that I am having the same issue as XenonBL where the text is fine on Corona Sim but on the device its shifted up. Is this a bug for ANSCA? I would hate to have to go through and change all this around when its just a bug to be fixed.

nicksantan
User offline. Last seen 11 weeks 1 day ago. Offline
Joined: 7 Jul 2010

Dumb question: where can I find the build.settings file? Or do I create on in my project directory?

alanb
User offline. Last seen 50 weeks 1 day ago. Offline
Joined: 19 Jan 2011

@Nick: you create the build.settings file yourself in the root project directory.

Shaunathan
User offline. Last seen 44 weeks 6 days ago. Offline
Joined: 18 Jan 2011

dumber question, when I use a ttf font the font size is fixed, it doesn't matter how big I make the font in display.newText, it will stay the same size. Why is it doing that? It IS the correct font, but the size is unchangable.

DavidBFox's picture
DavidBFox
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 10 Oct 2010

Shaunathan, I had no trouble changing font size when using a custom font on a button. Want to paste in your code? Maybe there's a problem there.

rhalferty
User offline. Last seen 5 weeks 3 days ago. Offline
Joined: 24 Jan 2011

Does this work with the Windows Simulator? I am having trouble trying to get a custom font on my windows machine working. Of course this is Android only.

Shaunathan
User offline. Last seen 44 weeks 6 days ago. Offline
Joined: 18 Jan 2011

turns out it's the font, "linesquare rounded extended" on dafont.com it's a free font, so you can try it yourself. Perhaps it's a problem because it's a .ttf. Does Corona only work with .otf?

UPDATE:

Different .ttf worked fine. Maybe it has trouble with pixelized Flash compatible fonts?

DavidBFox's picture
DavidBFox
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 10 Oct 2010

Shanathan, I was able to get that linesquare rounded extended font to work in the simulator. Haven't tried it on an iPhone.

Three things to check.

1. Make sure you have a copy of the font in your library/fonts folder

2. Make sure you have a build.settings file at the same level as your main.lua file. It needs to have this code in it:

1
2
3
4
5
6
7
8
9
        iphone =
        {
                plist=
                {
                        UIAppFonts = {
                                "linesquare_rounded_extended.ttf",
                        },
                },
        },

Here the file name has to exactly match the actual font file.

3. When calling the font in your code, make sure you use the actual name of the font, as seen in FontBook and other apps. In this case, it's "Linesquare Rounded Extended" (case is important). For example,

1
2
3
4
local debugText = display.newText("Test message", 24, 0, "Linesquare Rounded Extended", 44)
debugText:setTextColor(255, 70, 70)
debugText.y = 24
debugText.x = 54

Urme
User offline. Last seen 27 weeks 4 days ago. Offline
Joined: 7 Feb 2011

I've tried all possible solutions but I can't get my custom font working on the device, it works fine in the simulator but on the device no text will display at all :(

DavidBFox's picture
DavidBFox
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 10 Oct 2010

Urme, I've had mixed results with fonts. Some worked for me and some didn't. See if you can get any custom fonts to work, like the Harrowprint example described here.

If you can get that one to work, then you could try your own. But I've found that at least 2/3 of the fonts I've tried didn't work, maybe because they're not TrueType, or some other issue. You could then zip up your project, including the font, upload it to your website, and link to it here so others can take a look.

nhakimsat20101
User offline. Last seen 49 weeks 4 days ago. Offline
Joined: 6 Feb 2011

Hi guyz

I have been reading this comments since morning up and down if i am missing something or not but i am yet not able to get the custom fonts on my device !!

Made changes in the build.setting file with the fonts in the root directory. which made the application work in Xcode sim but when the same thing was transferred to the Ipad the font is invisible

Dont know exactly how it would work.

If you guyz can upload a small example with the custom font which works just fine would be if a great help

Update...

I used the example provided above in my windows simulator downloading the ttf font and installed it in my fonts directory and also kept it in my root file used the exact same code provided above to test it

but the fonts wont load.

later i used a few other fonts which didnt had space in middle of their name and they were working just fine
which gives me a conclusion there is some issue with spaced name of the fonts which gives a conflict can you please confirm this back or if i am wrong can you provide solution.

Thanks and Regards
Atheros

DavidBFox's picture
DavidBFox
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 10 Oct 2010

Atheros, I had trouble with certain fonts while others worked fine. Never figured out why. So best place to start is with a font that we know works and see if you can get that to work on your device. Then try replacing with other fonts. Start by downloading the Harrowprint font that's linked from this page

http://developer.anscamobile.com/content/display-objects#Using_a_custom_font

Once you can get that one working, you at least know you have the settings correct.

I'm guessing that only Truetype fonts work... I've asked about this but haven't gotten a confirmation.

nhakimsat20101
User offline. Last seen 49 weeks 4 days ago. Offline
Joined: 6 Feb 2011

Hey David,

local text = display.newText( "Hello Harrowprint font!", 0, 0, "Harrowprint", 50 )

I saw the "Harrowprint" does not have a space in the middle i am sure this will work fine i tried many fonts i wish there was a a option to attach the file i would have attached the example so that ansca people can figure out the actual reason behind it

Regards
Atheros

DavidBFox's picture
DavidBFox
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 10 Oct 2010

Atheros, I also recall wondering if there might be an issue with fonts with a space in their name. I suggest you report this as a bug. There you can attach a zip file of your project (including the font) so the Ansca staff can try and duplicate the problem.

Click on "Report a Bug" in the upper right corner of this page, or go here:

http://developer.anscamobile.com/content/bug-submission

p120ph37
User offline. Last seen 42 weeks 1 day ago. Offline
Joined: 1 Feb 2011

An alternative (with both advantages and disadvantages) to compiling a TTS font into your project may be to use a pre-rendered bitmap font.

You can read about and download my implementation in this thread:
https://developer.anscamobile.com/forum/2011/02/05/bitmap-font

The advantages of a bitmap font are:
- You can have fonts which include baked-in effects like drop-shadow and gradient fill.
- You can use custom fonts without worrying about the tricky TTF embedding.
- You can save space by including only those letters that you will need.

The disadvantages of a bitmap font are:
- You cannot display text at arbitrary sizes
- You cannot display text in arbitrary colors
You can work around these two issues by creating multiple font files in different colors and sizes or by creating a large font file and scaling the text down.

rxmarccall's picture
rxmarccall
User offline. Last seen 4 days 2 hours ago. Offline
Joined: 18 Jan 2011

I am a windows user and am building for Android, it is my understanding then that for android, you do not need to add the font in your build settings correct? I still cant seem to get it to work in my game. here is my code:

1
local mytime = display.newText("0",  0, 0, "Harrowprint", 35)

and i have the font installed and also a copy in my root folder

g.harrison's picture
g.harrison
User offline. Last seen 5 hours 3 min ago. Offline
Joined: 23 Feb 2011

Any news or official docs on how to embed fonts and sample code?
Am using Windows and trying to build for Android.

rxmarccall's picture
rxmarccall
User offline. Last seen 4 days 2 hours ago. Offline
Joined: 18 Jan 2011

I finally got mine to work, but like others have said, it seems that only fonts that are single word named will work. All you need to do is install the font in your windows/fonts folder. then also make a copy to put in the root of your project folder, and then in your code you would replace Harrowpoint with whatever your font is named. (use the name that is shown in microsoft word, not the fonts file name)

should do the trick

g.harrison's picture
g.harrison
User offline. Last seen 5 hours 3 min ago. Offline
Joined: 23 Feb 2011

I am getting nowhere trying to get font embedded using Windows build for Android.
My font is ravie.ttf (single name) and is in the root directory.
It works fine in the simulator but not when built. I am using the trail version (377) of corona might that be an issue.
Any help greatly appreciated

Ansca?

3cordDesign's picture
3cordDesign
User offline. Last seen 3 weeks 4 days ago. Offline
Joined: 20 Oct 2010

Any updates on helping this out? I'm stuck like g.harrison. Tried everything... or so it seems. I even re-encoded it with fontLab. Still no luck.

p120ph37
User offline. Last seen 42 weeks 1 day ago. Offline
Joined: 1 Feb 2011

3cord, as I mentioned above, if you are desperate for a custom font and can't get the TTF fonts to embed, you can try using my BMF library (free) or TextCandy to display a bitmap font.

You can generate bitmap fonts from TTF fonts using BMFont (free, Windows) or GlyphDesigner (better program, paid, Mac).

3cordDesign's picture
3cordDesign
User offline. Last seen 3 weeks 4 days ago. Offline
Joined: 20 Oct 2010

Thanks for the idea p120.

I actually just now got it working. It was an error in my build settings. I had a comma and a curly brace in the wrong spot. So it works on my device now.

If anyone wants to use my build settings here they are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
settings = {
        orientation = {
                default = "landscapeRight",
                supported = { "landscapeLeft", "landscapeRight"}
        },
        iphone = {
                plist = {
                        CFBundleIconFile = "Icon.png",
                        CFBundleIconFiles = {
                                "Icon.png" , 
                                "Icon@2x.png" , 
                                "Icon-72.png" , 
                                "Icon-Small-50.png" , 
                                "Icon-Small.png" , 
                                "Icon-Small@2x.png"
                        },
                        UIStatusBarHidden = true,
                        UIPrerenderedIcon = true,
                        UIApplicationExitsOnSuspend = true,
                        UIAppFonts = {"YourFontHere.ttf"}
                },
        }
}

Like I said, once I got this under control it worked perfectly. Even fixed an error I had with one of my icons.

brainofsteel
User offline. Last seen 5 days 4 hours ago. Offline
Joined: 26 Jan 2011

Another quick tip (This is probably a no-brainer to those with a little more experience):
I needed a custom font on an Android app, so I downloaded the font, installed it on Windows, and copied the .ttf file to the app root directory. It still wasn't working, even after I had read and reread the above thread to make sure I was doing it right.
Turns out I just had to completely close and reopen the simulator. Once I did that it worked perfectly.

phulesgame
User offline. Last seen 40 weeks 2 days ago. Offline
Joined: 25 Feb 2011

brainsofsteel answer is the one that got it working for me ..... All the others are good, but no one else mentioned shutting the SIM and restarting.....

henning
User offline. Last seen 44 weeks 3 days ago. Offline
Joined: 12 Jul 2011

Hi,

who wants to embed the whole Unicode font file? In practice I need in my app only some symbols out of this font! How can I limit the character set that is embeded without having to create a complete new ttf file?

Regards,
Devarai

qmnjb007
User offline. Last seen 13 weeks 23 hours ago. Offline
Joined: 17 Feb 2011

i have this same problem as well... did you ever find a solution??

lhvio89's picture
lhvio89
User offline. Last seen 8 weeks 3 days ago. Offline
Joined: 12 Mar 2011

Hello,

I'm running into issues embedding custom fonts here as well. I followed the sample that is in the documentation (with Harrowprint, which I downloaded from the link provided in the sample code), and the custom font just won't display. I've copied the code to the letter, and made sure every file is in its proper place before running.

Are custom fonts just not working right now?

Thanks!
Logan

me7
User offline. Last seen 2 weeks 6 days ago. Offline
Joined: 10 May 2011

^I wonder the same as above. Anything changed on r704?

My font is being found (checked that names match), but is not loaded by the simulator. In Android it's not loaded (nor is it in the list of fonts).

I tried a bunch of "tricks":
1. Putting font in root dir (obvious)
2. Installing font in Windows (for simulator to see it, it still fails to load it though it is in fonts list)
3. Change font extension from OTF to TTF (including removing it)
4. Testing both font name and font names match, and also tried with filename(both with and without extension on both Android and simulator
5. Simplifying font name with FontForge (actually, it was simple already but I changed to an 8 letter lower case name)

Probably irrelevant, but in a pure Android version of the app, the font is loadable, but I guess Corona uses different font loading code.

Any more ideas? Or shall I submit as bug report? I would normally just select a working font but I'm on a short deadline and client specifically wants this font.

matias.kiviniemi
User offline. Last seen 2 days 14 hours ago. Offline
Joined: 19 Mar 2011

Any new insights, tricks or tips on this topic? I've hit a wall to get my custom font to load on Android. It works on simulator (windows) and I've tried all mentioned variations, i.e. simplifying names, referencing file name vs. font name, ttf vs. otf, rebooting but no luck.

me7
User offline. Last seen 2 weeks 6 days ago. Offline
Joined: 10 May 2011

I just gave up and embedded custom fonts for static strings into images and used system defaults for most dynamic strings, and had the font exported to BMF (bitmap font) for some dynamic strings where the difference in style was too distinct to be ignored.

I think, it's just something in the font file that causes loading in Corona to silently fail for that font. If it's possible, one can simply try replacing the font. However, my client had decided that that particular font should be used and I couldn't explain why it wouldn't work in Corona as opposed to Java Android. Tough luck. :-)

matias.kiviniemi
User offline. Last seen 2 days 14 hours ago. Offline
Joined: 19 Mar 2011

Result! \o/

I did an experiment that maybe it wants the actual path and tried using the pathToFile-version form resoure-dir. This helped nothing, but using aLogCat I could see Corona gave an error about not finding the font. So I concluded Corona tries to load the font but fails silently (like people suggested).

Next attempt convert/resave the font file if that would help. FontForge that was recommended seemed like a major PITA in Windows so I tried Type Light and a couple of web tools. What gave results was
http://onlinefontconverter.com/

When I converted from .otf to .ttf, the tool changed the filename from the original, presumably to match the font metadata. Using this new file and name (without extension!) worked! Either that tool changes file format, or it's the fact of file name matching internal metadata.

So to be clear
1) Upload "OldName.otf" to site
2) Save "NewName.ttf" to project root
3) Use "NewName" as the font name in Corona

And all this is ANDROID!

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.