Determining the Correct String for UIFont.FromName

We use a lot of different fonts throughout the different apps on our platform, and often it can be a little bit of a chore to figure out the correct string to feed to UIFont.FromName() in order to properly instantiate a font from the files. One approached I've used in the past is to throw some lines in my app's startup code like this:

foreach (var family in UIFont.FamilyNames)
	foreach (var fontName in UIFont.FontNamesForFamilyName(family))
		Console.WriteLine(fontName);

Then I could quickly skim the output, pick out the name I was looking for, and move on with my life. This works, but it's a bit tedious and requires manual work every time. Neither of those really sits well with me.

I recently came across LCDF Typetools which is a nice little set of tools, including otfinfo. You can get these tools in a variety of ways. I went with Homebrew, which is nice and simple:

brew install lcdf-typetools

Here's what the output of otainfo looks like:

+ otfinfo -i SourceSansPro-Light.ttf
Family:              Source Sans Pro Light
Subfamily:           Regular
Full name:           Source Sans Pro Light
PostScript name:     SourceSansPro-Light
Preferred family:    Source Sans Pro
Preferred subfamily: Light
Version:             Version 2.010;PS Version 2.0;hotconv 1.0.78;makeotf.lib2.5.61930
Unique ID:           2.010;ADBE;SourceSansPro-Light;ADOBE
Designer:            Paul D. Hunt
Manufacturer:        Adobe Systems Incorporated
Vendor URL:          http://www.adobe.com/type
Trademark:           Source is a trademark of Adobe Systems Incorporated in the United States and/or other countries.
Copyright:           Copyright 2010, 2012, 2014 Adobe Systems Incorporated (http://www.adobe.com/), with Reserved Font Name 'Source'.
License URL:         http://scripts.sil.org/OFL
License Description: This Font Software is licensed under the SIL Open Font License, Version 1.1.

This license is available with a FAQ at: http://scripts.sil.org/OFL. This Font Software is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the SIL Open Font License for the specific language, permissions and limitations governing your use of this Font Software.
Vendor ID:           ADBE

In this case we're looking specifically for PostScript name. It wouldn't be difficult to parse this output to get it, but thankfully they have us covered and have a flag for just returning that name:

+ otfinfo -p SourceSansPro-Light.ttf
SourceSansPro-Light

Perfect! No extra parsing needed. As I've documented before, I use F#/FAKE for all of my build scripting, so naturally I needed to try hooking this up into that as well:


Target "font-test" (fun () ->
    ExecProcessAndReturnMessages (fun p ->
        p.FileName <- "otfinfo"
        p.Arguments <- "-p SourceSansPro-Light.ttf"
    ) (TimeSpan.FromSeconds 30.)
    |> fun response ->
        match response.ExitCode with
        | 0 -> 
            let postscriptName = response.Messages.[0]

            printfn "PostScript Name: %s" postscriptName
        | 1 -> failwith "Error processing font file"
)
comments powered by Disqus
Navigation