Tuesday, September 27, 2011

OS X & Fonts

One thing that surprises people in OS X is that unlike Classic Mac OS, text drawing OS X can't always draw text in italic or bold.

There's been some discussion on this in the lists & the forums and the reasons seem to all boil down to OS X drawing routines work differently than Classic Mac OS and require the specific variation of a font be installed so it can be used. There may be more technical underpinnings but the upshot is if you want italic or bold, the font has to have that variant or it won't work.

You can see this effect for yourself in a tool like TextEdit.

* Create a document and make it rich text.
* Type a bunch of text then select it all and open the Font panel.
* Select a font like Lucida Grande and try making the text italic.

In 10.5.8 this font has no italic variant and so you cannot select the Italic style for the text. It's not even enabled.

As you might guess, this affects Real Studio as well since we use system level text rendering routines. But there are options. You can always use the "standard fonts" installed with OS X that have the variants you need. Or, if you need a custom font or need to guarantee that the font you use has the variants, you can set up an Application specific font quite easily. 

The following is from this great tutorial posted by by paulg on the forums: 

1. Download a custom font file from one of the many font sites on the internet. Many are available for free with no restrictions. For example here is a freebie I will use in this tutorial: http://spacefonts.8m.com/fonts/stargate.zip

2. In a new RS Desktop Application drag a StaticText Field to the window.

3. Change the TextFont property to "Stargate" or the exact Full Name of the font, not the filename. You can get the Full Name of the font by using the finder to Get Info on the font File and you will see the Full Name. It is Case Sensitive. Note that you will not see the custom font in the IDE as it will only be visible at runtime, instead the default system font will be used. You can get around this by installing the font on your system. But this defeats the test we are trying to run.

4. Change the text to something other then Untitled. I also increased the size to 16 in my example and used all Caps for this particular font.

5. Build the application.

6. Open the built app's contents folder by right-clicking and choosing "Show Package Contents".

7. Navigate to the "Resources" folder and create a new folder to hold your font(s). In my example, I created a folder called "AppFonts".

8. Copy your font file(s) to this new directory.

9. In the Contents folder of the package, open the info.plist file in your favorite editor. If you are using the Plist editor add a new Child and add the key ATSApplicationFontsPath. Plist editor will display it as "Application fonts resource path" and set the value to the name of the folder you created in step 7. In my example I called it "AppFonts" so that is what I put as the Value.

Run the app and you will see the StaticText in the font you specified. In 10.6 you may get a warning that your app wants to use a font that is not installed on the system and asks if you wish to continue.

Of course this could use some automation and there may be a security concern if people get a message that your app wants to use a font that is not installed. I have a few other Non-RB apps that use fonts in this manner and I do not get that warning so not sure what that is about, maybe code signing?

You can use more then one font in your app, just include them all in the folder you created in step 7 and refer to them by name.

No comments: