RenderX

Chapter 3 of the XSL-FO Tutorial

3. Font and Text Attributes

Let us now enrich the text with character-level formatting. Several properties control font styles — family, size, color, weight, etc. Let's look at some examples:

<fo:block font-family="Times" font-size="14pt">
  Hello, world!
</fo:block>

Font family is Times, and font size is 14 points.

<fo:block font-family="Times" font-size="14pt" font-style="italic">
  <fo:inline color="red">H</fo:inline>ello,
  <fo:inline font-weight="bold">world!</fo:inline>
</fo:block>

Same as above, plus:

  • the whole text is italicized (font-style="italic");

  • the first letter of the first word is written in red (color="red");

  • the second word is written in bold font (font-weight="bold").

Note a new formatting object — <fo:inline>. It corresponds to <SPAN> in HTML, and ascribes formatting to chunks of text within a block.

Font properties are inheritable. It means that, once defined for a formatting object, they apply to all formatting objects inside it. That's why the first inline sequence affects only the color of the font, leaving its family, size, and slant unmodified. Inheritable properties can be put almost everywhere on the formatting objects tree; as a rule, you specify default font for a document by applying these properties to <fo:flow>, <fo:page-sequence> or even <fo:root>.

To reduce typing, you can use a shorthand notation for setting font attributes as a group. For example, the above example can be rewritten as follows:

<fo:block font="italic 14pt Times">
  <fo:inline color="red">H</fo:inline>ello,
  <fo:inline font-weight="bold">world!</fo:inline>
</fo:block>

The font property has the following syntax:

[<style, weight, and/or variant>] <size>[/<line height>] <family>

It sets all mentioned attributes to specified values, and resets all other font-related attributes to their default values, overriding inherited values. Be careful when using this feature: font="14pt Times" is not equivalent to a conjunction of font-size="14pt" & font-family="Times"!

Let's now build a full XSL FO example with font attributes introduced above, and some new ones:

<?xml version="1.0" encoding="iso-8859-1"?>

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="my-page">
      <fo:region-body margin="1in"/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="my-page">
     <fo:flow flow-name="xsl-region-body" font="12pt Times"❶>
       <fo:block font="italic 24pt Helvetica">
         <fo:inline color="red">F</fo:inline>ont
         <fo:inline color="red">A</fo:inline>ttributes
       </fo:block>
       <fo:block>❷
         The inherited font for this block is 12pt Times.
       </fo:block>
       <fo:block>
         Font attributes:
         <fo:inline color="red">colored</fo:inline>,
         <fo:inline font-weight="bold">bold</fo:inline>,
         <fo:inline font-style="italic">italic</fo:inline>,
         <fo:inline font-size="75%">small</fo:inline>,
         <fo:inline font-size="133%">large</fo:inline>.
       </fo:block>
      <fo:block>
         Text attributes:❸
         <fo:inline text-decoration="underline">underlined</fo:inline>,
         <fo:inline letter-spacing="3pt"> expanded </fo:inline>,
         <fo:inline word-spacing="6pt">
           text with extra spacing between words
         </fo:inline>,
         <fo:inline text-transform="uppercase">all capitals</fo:inline>,
         <fo:inline text-transform="capitalize">capitalized</fo:inline>,
         text with <fo:inline baseline-shift="sub"
         font-size="smaller">subscripts</fo:inline>
         and <fo:inline baseline-shift="super"
         font-size="smaller">superscripts</fo:inline>.
      </fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>

A common font for the whole flow is specified.

This block inherits font attributes from the flow.

In this block, I introduce several other text-level properties:

text decoration

underline/overline/strikethrough;

letter and word spacing

a positive value expands text, a negative value condenses it;

text transformations

upper/lower case, capitalize;

shifted text

subscripts and superscripts.

Download the complete XSL-FO Tutorial as PDF