Coding in LiveCode
Convert fonts into a format that can be installed in iOS 7
This month’s LiveCode tutorial shows how to make an iOS 7 font converter
SKILL LEVEL
Taking things further
IT WILL TAKE
30 minutes
YOU’ll NEED
LiveCode
The LiveCode dev tool enables you to create apps that can be used to automate repetitious tasks and to do things that you may not even know how to do. The iOS Font Maker is a good example: it converts a TrueType or OpenType font to the format required to use it on an iPad or iPhone running iOS 7.
Let’s build a version from scratch, creating an XML-formatted file that contains custom names, the font file’s data encoded as base64, and a couple of unique numbers generated with a Unix ‘shell’ call. These will be merged into placeholders in the XML template before being saved as a ‘.mobileconfig’ file. It sounds complex but it’s relatively straightforward, and it demonstrates some of LiveCode’s deeper powers.
Start by making a new mainstack and dragging in two Text Entry fields and two Rectangle Buttons. Name the fields ‘ID’ and ‘Organisation’ and name the buttons ‘Choose’ and ‘Generate’. Drag in a Scrolling Field and name it ‘template’. Go to thehelpful.com/ livecode/iosfonts/template.zip, download and unzip the file, open in TextEdit, then copy and paste its contents – using the Run tool – into the ‘template’ field. Switch to the Edit tool and untick its Visible checkbox in the Inspector palette.
Select the ‘Choose’ button and click the Code button in the Toolbar. Outside of the ‘mouseUp’ handler, type global gFontData, gUUID1, gUUID2, gTheFile to set up global variables. These named variables, used to hold bits of information, will be remembered permanently and are available ‘globally’ to other scripts you write.
Inside the ‘mouseUp’ handler, we're first going to deal with selecting the font file and then reading its data. The answer file instruction in our first line opens a standard file selection dialog. The next line will check to see if the user cancelled this; the ‘it’ variable stores the path to the selected file, so if
Create your own custom software to automate complex and difficult or repetitious tasks
‘it’ is empty then exit this mouseUp handler. Next, we store ‘it’ in gTheFile; the ‘it’ variable will be used to hold the data read in the next step. Those three lines of code look like this: answer file "Pick a font" if it is empty then exit mouseUp put it into gTheFile
Now we open the file. It isn’t like double-clicking the document; this marks it as being open for reading/ writing by our software. After that, the file is read until the end of the file (EOF). That done, the file is closed. The file data read is stored in the ‘it’ variable, ready for use. Base64 is typically used to prepare data for MIME email use, but is also how fonts have to be encoded for use in iOS 7. We’ll use LiveCode’s ‘base64Encode’ function to put the encoded data into gFontData, a pre-made global variable. This is the code for all that: open file gTheFile read from file gTheFile until EOF close file gTheFile put base64Encode(it) into gFontData
Two UUID codes – universally unique identifiers – are needed. These 128-bit numbers are guaranteed (with a large margin of probability) to be unique every time. The Unix ‘uuidgen’ shell command generates a UUID – LiveCode can run shell commands, so there’s no need for OS X’s Terminal. Type this: put the shell of uuidgen into gUUID1 put the shell of uuidgen into gUUID2
Click Apply. This script will store three different chunks of data in three global variables. The next step is to make a button that merges these and the two Text Entry fields into the template and exports the result to disk. Keith Martin