24 October 2010
Familiarity with Adobe AIR, HTML, JavaScript, and CSS is recommended.
All
Adobe AIR 2.5 introduces support for downloadable font resources or web fonts. Web fonts can be referenced using the @font-face CSS rule, which is supported by most modern browsers and enables web developers to link and display fonts that are not installed on the user's machine. These fonts are downloaded by the browser from a remote server when the content is viewed.
Typography is important for a number of digital print editions of newspapers, magazines, books. The user experience depends on the quality of the typography. Making sure your carefully selected fonts can be rendered on the user's display is the first and most important step towards a great user experience. Furthermore, web fonts are protected with services such as Typekit: users who browse a page that uses the @font-face rule don't have access to the font files. This allows you to use commercial fonts and thus differentiate your products from others.
Adobe AIR 2.5 supports web fonts for desktop applications built using ActionScript/Flex (using HTMLLoader) or HTML/JavaScript/CSS. If you build applications for mobile, then web fonts are supported through the StageWebView control.
AIR supports the following web font formats:
Embedded OpenType (EOT), Web Open Font Format (WOFF), and SVG Fonts are currently not supported.
Before going into details on how an AIR application might use web fonts, I'd like to provide a brief overview of the companion application for this article. I decided to build this application using HTML/JavaScript/CSS mainly because web fonts are an HTML-related feature (although you can use this feature in an ActionScript or Flex project as well).
The AIRWebFontDemo sample application is a simple book reader (see Figure 1). It displays a book (Alice in Wonderland by Lewis Caroll) and it lets you change the font size and family. You can navigate through the content using the scrollbar or the navigation buttons on the bottom. The application also has a fullscreen mode and remembers the last state. When the application is closed, information about the application's window size, scroll position, and font size and family are persisted on the disk and restored the next time the user opens the application.
As you can see it is a pretty simple application both from the point of view of the source code and features. Nevertheless, it touches on some of the features you'd probably want to have in a real book reader.
If you download the AIRWebFontDemo.zip file (this file is an Aptana AIR project), unzip the file, and import it into Aptana Studio using File > Import > General > Existing Projects into Workspace (selecting the folder where you unzipped the file) you will see a project with the structure shown in Figure 2.
Now that you have an overview of the structure of the application have a look how it was implemented.
The application user interface is defined in the AIRWebFontDemo.html file and its companion CSS file, style.css. Basically, what I did here is a liquid design with two fixed zones on the Y axis (the top and the bottom stripes) and a mobile one in between them. On these two zones I placed the UI controls. In the flexible one I placed an <iframe> . And this <iframe> is used for displaying the book itself.
If you take a look at the AIRWebFontDemo.html and style.css files you'll notice that I had to use a lot of DIVs and CSS in order to create this liquid design. After finishing the user interface for this application I came to this conclusion: when developing desktop applications with AIR and HTML I don't think using DIV tags instead of TABLE tags is recommended. When you build applications you have to position (pixel perfect) a lot of controls and using a mix of DIV and TABLE tags helps both in development speed and quality. Imagine the work needed for creating the user interface of a complex text editor using only DIV tags.
Next, all the scripting that brings to life the user interface defined in AIRWebFontDemo.html is in js/script.js file. These are the main features provided by this script:
Some of this logic is hooked using the onload events of the main HTML application and <iframe> content. For example once the onload event is thrown by the main application (and this means the applicatin was initialized, thus I can access the stage, for example), the initialize() function is executed and I use this to register the listeners. Then, when the onload event is thrown by the <iframe> (this means the book content was loaded by the <iframe> including the Web Font used) I execute the restorePosition() function, which in turns applies the values saved in the previous session.
In order to persist the information related to application state I chose to create a JavaScript object and then I serialize this object in a file in the application storage directory.
Finally, the last important part of this application is the books/alice.html file. This file stores the book content. It has some JavaScript code and CSS. There are two reasons for the presence of JavaScript:
<iframe> content (in the nonapplication sandbox) I had to use the Sandbox Bridge in order to push the style or scrolling changes from the main application to the page displayed by the <iframe> . Thus I have a number of JavaScript functions defined here and exposed through the Sandbox Bridge.The main idea behind web fonts is that the font is loaded by the browser at runtime from a remote server. Depending on who's the remote server you have different workflows to follow in order to use web fonts in Adobe AIR.
If you use Typekit services, for example, you'll have to:
<iframe> and set the documentRoot and sandboxRoot to reflect where the HTML file is located relative to appliction source folder and what is the domain name you set in your Typekit account. For example, these are values set in my application:<iframe id="myFrame" src="alice.html"
documentRoot="app:/books/"
sandboxRoot="http://corlan.org">
</iframe>
<head> section of the HTML file where you use the web fonts you have to add the following <script> tags (the URL for the first script is customized for each account; this means you have to retrieve the script from your Typekit account):<script type="text/javascript" src="http://use.typekit.com/opp1ueg.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
body tag and the tk-adobe-garamond-pro class. Then you have to press the Publish button in order to enable the settings.<style type="text/css">
body {
font-family: adobe-garamond-pro-1,adobe-garamond-pro-2, serif;
}
</style>
If you want to use a font from Google Font Directory, then the steps are:
<head> node. For example, for the Cantarell font the tag is: <link href='http://fonts.googleapis.com/css?family=Cantarell&subset=latin' rel='stylesheet' type='text/css'>
h1 { font-family: 'Cantarell', arial, serif; }
Lastly, you should be aware of the overhead that web fonts add to a project in terms of resources which have to be downloaded at runtime. Because web fonts are retrieved from a remote server, as you can observe if you run my application, it can take some time in order to download the font file. This means you should always take this in consideration when using a large number of web fonts.
The addition of web fonts support in Adobe AIR opens up the possibility to finetune and control the text presentation in your applications. This can improve a lot your projects when what are you building a digital version of a book, magazine, or newspaper. If you want to stay in touch with what the Adobe Type Team is doing you can follow their blog. Also refer to the Adobe Web Fonts FAQ.