Chapter 23. Fonts

Table of Contents

1. What is a font?
1.1. Runtime considerations
1.2. Bitmap fonts and outline fonts
1.3. Embedded Fonts and Client Fonts
1.4. Default Fonts
2. OpenLaszlo "pseudo-pixel" fonts
3. Syntax
3.1. The "font" attribute in opening tags
3.2. The <font> tag
3.3. Comparison to CSS
3.4. Importing fonts
3.5. Syntax for importing fonts
4. Using fonts
4.1. Font attributes
4.2. How a view chooses its font attributes
4.3. Fonts provided with the OpenLaszlo Server
5. DHTML Font design considerations
6. Performance considerations

This chapter describes how to use fonts in LZX applications.

1. What is a font?

A font is a set of glyphs (images) representing the characters from some particular character set in a particular size, style, and typeface. There are several different computer representations for fonts. The most widely known are Adobe Systems, Inc.'s Postscript font definitions and Apple's TrueType.

1.1. Runtime considerations

Since DHTML doesn't support compiled fonts, we have to rely on system fonts. This means fonts may not look exactly the same across runtimes because letter spacing and rendering/smoothing are often different from one implementation to the next.

Small type isn't smoothed in DHTML as it is in SWF. I'm not sure what to do here, except choose our fonts carefully and/or avoid small point sizes.

We make every effort to make font metrics in DHTML match those in swf. They may not match exactly but they're very close.

It is virtually impossible to achieve pixel-perfect font rendering across all runtimes. However, with minimal changes for alignment and font names, you can usually make SWF and DHTML versions of your application essentially indistinguishable with regard to appearance of text.

1.2. Bitmap fonts and outline fonts

The image of each character in a font may be encoded either as

  • a bitmap (a bitmap font, sometimes called a pixel font)

  • a higher-level description in terms of lines and areas (an outline font).

In general, bitmap fonts are intended to be displayed at a specific size and outline fonts can be displayed at a variety of sizes.

1.3. Embedded Fonts and Client Fonts

[Warning]
[SWF]

SWF only: The features described in this section only work in applications compiled to SWF. They do not work in applications compiled to other runtimes.

Only some runtimes support embedded fonts (just SWF for now). Because of this, if you're not compiling to SWF all fonts need to be on the end-user's system and <font> tag(s) don't work. Font attributes can take a list of names (optionally including compiled font names for swf.) The runtime goes through the comma-separated list and picks the first one it finds on the system. This allows for fallback fonts if one or more fonts can't be found by name.

OpenLaszlo applications can access fonts in either of two ways: by building them into the application itself (SWF only) or by using the fonts that are resident on the client machine (DHTML and SWF).

With embedded fonts, there is a reasonable guarantee that the text in that application will look the same, no matter where it is run. However, fonts are large, and including fonts in your application makes the application (download) larger, and the startup time slower. With client fonts, (also called native fonts or device fonts), applications are smaller and start faster, but there is no guarantee that the rendering of that font, or the font definitions themselves, will be the same from computer to computer. Embedded fonts provide higher fidelity.

1.3.1. Client fonts cannot be rotated or change opacity

In addition, because of limitations of the Flash Player, there are certain things you cannot do with client fonts. For example, client fonts cannot be rotated, nor can you vary their opacity. Here is a summary of the differences:

1.3.2. Summary of differences between client and embedded fonts

Client fonts

  • Smaller downloads and faster initialization

  • Some limitations (no rotation, etc)

  • No guarantee that application will look the same on all platforms

Embedded fonts

  • Larger downloads; slower startup

  • All text manipulations available

  • Designer control over application appearance

1.4. Default Fonts

By default, OpenLaszlo applications use the device fonts.

[Note] Note
The Adobe Flash API to measure text width is reporting an incorrect value: there's no way for us to know what the real width of the text is. As a result, you may see some clipping of text, as in the italicized fonts in the following code example.

Example 23.1. Using device fonts

<canvas width="100%"> 
  <window title="Fonts are fun!" width="400" height="400" resizable="true">
  <view width="500">
    <simplelayout axis="y" spacing="10"/>
    <view width="400" font="Arial" fontsize="14">
      <simplelayout axis="y" spacing="10"/>
      <text>Arial</text>
      <text>abcdefghijklmnopqrstuvwxyz1234567890!@#$%^*()-=+|</text>
      <text>ABCDEFGHIJKLMNOPQRSTUVWXYZ ,./ ;?;':"[]{}\~`</text>
      <text><b>Arial</b></text>
      <text><i>Arial</i></text>
      <text><b><i>Arial</i></b></text>
      <inputtext>inputtext here</inputtext>
      <inputtext multiline="true" height="100" width="100">hello there</inputtext>
      <inputtext>Arial</inputtext>
      <text>
        <b><i>BoldItalic</i> Bold </b><i>Italic</i>
        <u>Underline <b>Bold Underline</b></u></text>
    </view>
    
    <view width="400" font="Arial Narrow" fontsize="14">
      <simplelayout axis="y" spacing="10"/>
      <text>Arial Narrow</text>
      <text>abcdefghijklmnopqrstuvwxyz1234567890!@#$%^*()-=+|</text>
      <text>ABCDEFGHIJKLMNOPQRSTUVWXYZ ,./?;':"[]{}\~`</text>
      <text><b>Arial Narrow</b></text>
      <text><i>Arial Narrow</i></text>
      <text><u>Arial Narrow</u></text>
      <text><b><i>Arial Narrow</i></b></text>
      <inputtext>inputtext here</inputtext>
      <inputtext multiline="true" height="100" width="100">hello there</inputtext>
      <inputtext>Arial Narrow</inputtext>
      <text>
        <b><i>BoldItalic</i> Bold </b><i>Italic</i>
        <u>Underline <b>Bold Underline</b></u></text>
    </view>
    <view width="400" font="Times New Roman" fontsize="14">
      <simplelayout axis="y" spacing="10"/>
      <text>Times New Roman</text>
      <text>abcdefghijklmnopqrstuvwxyz1234567890!@#$%^*()-=+|</text>
      <text>ABCDEFGHIJKLMNOPQRSTUVWXYZ ,./?;':"[]{}\~`</text>
      <text><b>Times New Roman</b></text>
      <text><i>Times New Roman</i></text>
      <text><u>Times New Roman</u></text>
      <text><b><i>Times New Roman</i></b></text>
      <inputtext>inputtext here</inputtext>
      <inputtext multiline="true" height="100" width="100">hello there</inputtext>
      <inputtext>Times New Roman</inputtext>
      <text>
        <b><i>BoldItalic</i> Bold</b>
        <i>Italic</i><u>Underline <b>Bold Underline</b></u>
      </text>
    </view>
    </view>
    <scrollbar axis="y"/>
  </window>
</canvas>

You can override the default font by redefining the default font on the <canvas> . Note that you need to do this within the <canvas> itself—you cannot do it using an attribute tag.

Example 23.2. setting the default font on the canvas

<canvas width="100%" height="100" bgcolor="gray" font="default" fontsize="8">
     <font name="default" src="verity/verityplus11.ttf"/>
     <text>default Foobar the quick brown frotz jumped over the bad music</text> 
     <text font="default" fontsize="8">Verity Foobar the quick brown frotz jumped over the bad music</text> 
     <text font="Verdana">Verdana Foobar the quick brown frotz jumped over the bad music</text>
     <simplelayout/>
</canvas>

2. OpenLaszlo "pseudo-pixel" fonts

[Warning]
[SWF]

SWF only: The features described in this section only work in applications compiled to SWF. They do not work in applications compiled to other runtimes.

Because the underlying Flash runtime can only display outline fonts, OpenLaszlo applications today can only support outline fonts. This would seem to imply that fonts in a OpenLaszlo application can be displayed at any size, and strictly speaking this is true. However, the OpenLaszlo runtime library (the LFC) includes a number of "pseudo-pixel" fonts. These font files contain outlines that describe precise bitmaps that, when rendered and antialiased by Flash at a specific size, appear pixel-accurate for another size. Again, even though you specify the font size in LZX as one value, the characters, as they are rendered, will appear pixel accurate for another font size that is typically indicated by the font file name.

The following font files in $LPS_HOME/lps/fonts are designed to be displayed at font size 8 only.

  • lztahoe*

  • lzsans*

  • verity/verity*

  • Any font file whose name is of the form: face-size-style-width.ttf

[Note] Note

Embarrassing historical tidbit: the lzsans fonts have an '8' in their name but actually appear as size 9.

3. Syntax

There are two ways to identify which font your application, or any element of it, should use: the font attribute on <view> s and classes derived from views, and the <font>.

3.1. The "font" attribute in opening tags

The font attribute specifies font to be used for any <text> or <inputtext> elements that appear inside this view. Like all the font properties (fontstyle and fontsize too) these properties cascade down the view hierarchy until a new value is specified. When the font attributes are modified at runtime, using JavaScript, the font is changed for the view itself, not for any of its subviews.

For example,

Example 23.3. Using the font attribute

<canvas height="50" width="100%">
  <simplelayout/>
  <view font="helvetica">
    <text>Howdy!</text>
  </view>
  <view font="times">
    <text>Howdy back!</text>
  </view>
</canvas>

3.1.1. Specifying more than one font

You can supply more than one font name as argument to the font attribute. At runtime, the system will try the font list from left to right and use the first one it finds. This can come in handy if you have an application that you would like to compile to both SWF and DHTML.

The font attribute accepts a comma-separated list of fonts so DHTML can fall back to a system font when a compiled font isn't found by name. For example, say you have embedded the "helmet" font:

<font name="helmet" src="helmet.ttf"/>

Then the tag,

<text font="helmet,helvetica,sans-serif">

finds the compiled font 'helmet' when you compile to SWF and falls back to 'helvetica, sans-serif' when you compile to DHTML.

3.2. The <font> tag

[Warning]
[SWF]

SWF only: The features described in this section only work in applications compiled to SWF. They do not work in applications compiled to other runtimes.

The value of the font attribute is the name of a embedded font, that is declared with the <font> tag.

The behavior of font in LZX closely parallels HTML/CSS, but is not equivalent in all cases. In particular:

  • The value of the 'font' attribute is a comma-separated list of font names. These font names are used in priority order. The first font in the list that is available is used to render the text of the view.

  • Font names can include the names of fonts on the client runtime, such as (typically) "Helvetica" and "Times".

  • There are three "generic family names", which can be used as font names. These generic family names are "serif", "sans-serif", and "monospace".

  • The default font is "sans-serif". No font is embedded in the application by default.

  • There are no warnings for fonts that are named as the literal values of 'font' attributes but don't have embedded font definitions.

OpenLaszlo does not implement generic font family definitions, but it does not preclude them nor is it incompatible with them (as described below).

Example 23.4. Default font

<canvas height="40" width="100%">
     <text>Hello World</text>
</canvas>

This renders Hello World in a client font.

Example 23.5. Default font with non-default font size

<canvas height="100" width="100%">
   <text fontsize="24">Hello World</text>
</canvas>

This renders Hello World in a larger client font, with no jaggies.

Example 23.6. Equivalence of generic and definite names

<canvas height="120" width="100%">
   <simplelayout axis="y"/>
   <text font="sans-serif">Some text</text>
   <text font="Helvetica">Some text</text>
   <text font="Times">Some text</text>
   <text font="serif">Some text</text>
   <text font="Courier">Some text</text>
   <text font="monospace">Some text</text>
</canvas>

This displays three pairs of items. The items in each pair may be rendered in the same font, depending on the default fonts on the client.

Example 23.7. "nofont" attribute

<canvas height="100" width="100%">
     <simplelayout axis="y"/>
     <text font="nofont, nofont, sans-serif">Some text</text>
     <text font="nofont, sans-serif">Some text</text>
     <text font="sans-serif">Some text</text>
 </canvas>

This displays three lines in the same font.

3.3. Comparison to CSS

The OpenLaszlo font attribute is similar to the CSS 'font' and 'font-family' properties: here. The remaining differences are:

  • LZX specifies the font as an XML attribute. HTML+CSS specifies it is a CSS property.

  • An OpenLaszlo 'font' can only be a list of font names. This makes it more similar to the CSS 'font-family' than to the CSS 'font', which is a shorthand property that can also accept font size, style, variant, weight, and line-height values.

  • CSS defines the additional generic font families 'cursive' and 'fantasy' , as well as "system font" names 'caption', 'icon', 'menu', 'message-box', 'small-caption', and 'status-bar'. (Generic family name definitions, below, let the application or library developer define these system names.)

  • CSS allows multiword font names to be quoted, e.g. "Times New Roman" (where the quotes are part of the value). LZX does not allow this.

3.4. Importing fonts

In order to use any font other than the default font in an OpenLaszlo application, you must first declare it. The default font is described below.

OpenLaszlo documentation uses the verbs, importing, defining, and declaring, somewhat interchangeably when it comes to fonts. Each of these words implies that you are letting LZX know about a font you are going to use in your application. In LZX, fonts are referred to by name and there is an application-global namespace for fonts. Fonts can be defined directly under the <canvas> or <library> tags like:

Example 23.8. Declaring a font

<font name="myfont" src="myfont.ttf"/>

Each font that is imported has a name, an optional style, and a src file that contains the TTF. (The name is interpreted as the font face when the LZX application renders HTML text. See Chapter 21, Text Views.)

The style must be one of plain, bold, italic, or bold italic and the default style is plain. You must declare each style of a font that you intend to use. The style of a font is interpreted when LZX applications render HTML text. See Chapter 21, Text Views for more information.

The OpenLaszlo compiler will auto-include the plain, bold, italic, and bold italic styles of the default font, if it can detect that they will be needed. In general, it is safest to declare the bold, italic, and bold italic styles explicitly if you know you will need them. For fonts other than the default, you must explicitly import a file for each of the styles. For more details, see the section on the default font.

Each font must be imported from an external file and the value of the src attribute must be a pathname to that file (URLs are not currently supported). Relative pathnames are resolved relative to the including file. The OpenLaszlo Server also looks in $LPS_HOME/lps/fonts directory.

3.5. Syntax for importing fonts

There are two syntaxes for importing a font. The example below imports the plain and bold style of a font named myfont.

Example 23.9. Declaring fonts

<font name="myfont" src="myfont.ttf"      style="plain"/>
<font name="myfont" src="myfont-bold.ttf" style="bold" />

Alternatively, you can declare several styles at once like:

Example 23.10. Declaring multiple font styles (faces) at once

<font name="myfont">
  <face src="myfont.ttf"      style="plain"      />
  <face src="myfont-bold.ttf" style="bold"       />
  <face src="myfont-ital.ttf" style="italic"     />
  <face src="myfont-bita.ttf" style="bold italic"/>
</font>

Notes:

  • Redefining a font name is unsupported and application behavior is undefined if you do this.

  • There are no font aliases today. A single font can have only one name. If you need the same font to have two names, you must declare it twice (and it will be embedded twice).

4. Using fonts

4.1. Font attributes

The base LZX visual object, <view> , has three font attributes, font, fontstyle, and fontsize. The font attribute with value myfont indicates that this view should look for and use a font named myfont to render text. The fontsize attribute indicates the point size at which the font should be rendered and the fontstyle indicates the style of the font that should be used.

Table 23.1. Default values for font attributes

Attribute name Default value
font "default"
fontsize 8
fontstyle plain

As you will see below, these are actually the default values of the font attributes for the canvas. Because of the way that font attributes cascade, the values of these attributes on the canvas are essentially the defaults for the entire application. Below are some examples of using font attributes.

Example 23.11. Using a font

<canvas height="70" width="100%">
  <font name="helmet" src="helmetr.ttf"/>
  <text font="helmet" fontsize="20">Hello Helmet</text>
</canvas>

As you can see below, The HTML text rendering tags, <b> (for bold) and <i> (for italic) instruct the OpenLaszlo application to look for and use the bold, italic, or bold italic version of the current font.

Example 23.12. Using font styles and html text

<canvas height="70" width="100%">
  <font name="myfont">
    <face src="helmetr.ttf" style="plain"/>
    <face src="helmetb.ttf" style="bold"/>
    <face src="helmeti.ttf" style="italic"/>
    <face src="helmetbi.ttf" style="bold italic"/>
  </font>
  <text font="myfont" fontsize="20">
    plain 
    <b>bold</b>
    <i>italic</i>
    <b><i>bold italic</i></b>
  </text>
</canvas>

In the example below, we use the HTML text <font> tag to change fonts inside a single piece of text.

Example 23.13. Using multiple fonts in HTML text

<canvas height="70" width="100%">
  <font name="helmet" src="helmetr.ttf"/>
  <font name="arioso" src="ariosor.ttf"/>
  <text fontsize="20">
    <font face="helmet">helmet</font> 
    <font face="arioso">arioso</font>
  </text>
  </canvas>

In the example below, you can see that setting the fontsize can get you very big text.

Example 23.14. Using the fontsize attribute

<canvas height="100" width="100%">
  <font name="helmet" src="helmetr.ttf"/>
  <text resize="true" fontsize="100" font="helmet">BIG</text>
</canvas>

Many of the fonts look too small at the default fontsize of 8.

Example 23.15. Omitting the fontsize attribute

<canvas height="70" width="100%">
  <font name="helmet" src="helmetr.ttf"/>
  <text font="helmet">Helmet at default size(8) is very hard to read!</text>
</canvas>

You can specify the font for inputtext, too.

Example 23.16. Specifying the font for inputtext

<canvas fontsize="20" width="100%" height="70">
  <font name="helmet" src="helmetr.ttf"/>
  <inputtext bgcolor="red" font="helmet">Type your text here</inputtext>
</canvas>

Note that HTML text does not work in <inputtext> .

4.2. How a view chooses its font attributes

There are two basic rules that a view uses to choose its font attributes.

  • font attributes cascade

  • font attribute definitions in classes bind tightly (to children)

These are described in turn below.

4.2.1. Font attribute cascading

Cascading occurs when a view does not specify one of its font attributes. In this case, it will receive its parent's value for that font attribute. If the parent doesn't specify a font attribute, it keeps going up the view hierarchy to find a view that specifies a value for that font attribute, going all the way to the canvas, if no ancestors specify a value for the given font attribute. Each of the three font attributes cascade separately.

Example 23.17. Using cascaded font attributes

<canvas height="250" width="100%">
  <font name="conga" src="congar.ttf"/>
  <font name="conga" src="congab.ttf" style="bold"/>
  <view layout="axis: y; spacing: 2" font="conga">
    <text resize="true" fontsize="12" text="I will be in conga 12"/>
    <text resize="true" fontsize="22" text="I will be in conga 22"/>
    <text resize="true" fontsize="32" text="I will be in conga 32"/>
    <view layout="axis: y; spacing: 2" fontsize="32" fontstyle="bold">
      <text resize="true" text="I will be bold"/>
      <text resize="true" text="Me, too"/>
      <text resize="true" fontsize="8" font="default" text="I will be in the default font"/>
    </view>
  </view>
</canvas>

You can even specify font attributes on the canvas:

Example 23.18. Specifying a canvas font

<canvas font="tahoe" width="100%" height="70">
  <font name="tahoe" src="lztahoe8.ttf"/>
  <text text="hello"/>
</canvas>

4.2.2. Font attributes and classes

Specifying a font attribute on a class definition:

  1. causes instances of that class to use the class definition of that font attribute and

  2. the class definition font attribute cascades down to instance children in the class definition itself.

Class font attributes bind more tightly than cascaded font attributes.

4.2.3. Font attribute summary

The following is a set of rules that a view uses to choose its font:

  • Use the font attribute set on the instance. If not set, then

  • Use the font attribute set on this instance's baseclass (or any of its subclasses, going up the chain). If it is not set,

  • Use the cascaded font attribute.

You may want to use a font attribute on a class definition if you are going to closely tie the implementation (design, layout, positioning) of that class to that specific font. In general, you should only do this when absolutely necessary.

Example 23.19. Using font attributes in a class definition

<canvas height="100" width="100%">
  <font name="helmet" src="helmetr.ttf"/>
  <font name="tahoe" src="lztahoe8.ttf"/>
  <class name="myclass" fontsize="20" font="helmet">
    <attribute name="defaultplacement" value="'content'"/>
    <text bgcolor="red" text="Uses the class font"/>
    <view name="content"/>
  </class>
  
  <view layout="axis:y; spacing: 2" font="tahoe">
    <myclass/>
    <text text="use cascaded font"/>
  </view>
</canvas>

4.3. Fonts provided with the OpenLaszlo Server

The OpenLaszlo Server provides a number of fonts for your use in $LPS_HOME/lps/fonts. The pseudo-pixel fonts that come with the OpenLaszlo Server are described above. The OpenLaszlo default (pseudo-pixel) font, verity11 is there as is an outline version of the same glyphs in bitstream-1.10-vera.

5. DHTML Font design considerations

[Warning]
[DHTML]

DHTML only: The features described in this section only work in applications compiled to DHTML. They do not work in applications compiled to other runtimes.

There is a general tension in DHTML regarding who controls type size—the user or the application. w3c leans toward the end user having ultimate control, which means that you, as designer, have to be prepared for the fonts you have specified not being the ones that are displayed.

Small type isn't smoothed in DHTML as it is in SWF. It is hard to offer advice about what to do here, except choose your fonts carefully and/or avoid small point sizes. This is a browser and/or display setting, so there is nothing you can do about it. (For example., OS X allows you to set a minimum font size, below which smoothing will never happen; Safari lets you set a minimum font size for accessibility and smaller sizes will just be mapped up to that minimum.)

However, note that the Flash Player and some browsers do not handle font sizes equivalently. You may find that, relative to applications compiled to SWF, you need to step the size down for some DHTML OpenLaszlo applications where the text shows up too large.

6. Performance considerations

[Warning]
[SWF]

SWF only: The features described in this section only work in applications compiled to SWF. They do not work in applications compiled to other runtimes.

Embedded fonts (in applications compiled to SWF) increase the size of the application.

When deciding what embedded fonts to use, you may need to take into account how fonts affect the size, hence download time, of your application. Here are some heuristics to help you with file size/download calculus. -

  • The larger the font, the larger the application. The size of the font can be estimated by looking at the file size of the .ttf file.

  • Fonts with more complicated shape outlines will be larger.

  • Fonts with more character codes in them will be larger. One size optimization technique is to use a version of the font file that has only the characters in it that your application uses.