Contents | < Flow Layout | Graphics >

Inline Text Formatting

Inline elements allow XSL-FO developers to specify attributes for individual pieces of inline content (text and images), instead of the whole block.

In the example bellow, a fragment of text is filled with red, and it's font weight is set to bold:

<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="LetterPage" page-width="6in" 
                                  page-height="3in">
            <fo:region-body region-name="PageBody" margin="0.7in" 
                               background-color="rgb(245,245,245)"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="LetterPage">
        <fo:flow flow-name="PageBody">
            <fo:block font="12pt Arial">
                Some 
                <fo:inline font-weight="bold" color="red">inline text</fo:inline>  Œ 
                formatting.
            </fo:block>
        </fo:flow>
    </fo:page-sequence>
</fo:root>
For the complete source code for this code example see "Tutorial/Inline Formatting.fo" located under XML Documents Samples/Tutorial folder.

The result of rendering is displayed in following figure.

Figure 1

Things to notice:

Œ The fo:inline element wraps the fragment "inline text" and sets font-weight to bold. The text color is set to red using color attribute.

Any color can be described using either a standard color value (see Colors) or by using it's red, green and blue components. The following notations are equivalent:

<fo:inline color="red">Hello</fo:inline>
<fo:inline color="rgb(255,0,0)">Hello</fo:inline>

Subscripts and Superscripts

Inline elements also allow creation of sub-scripts of super-scripts:

<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="LetterPage" page-width="6in" 
                                  page-height="3in">
            <fo:region-body region-name="PageBody" margin="0.7in" 
                               background-color="rgb(245,245,245)"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="LetterPage">
        <fo:flow flow-name="PageBody" font="12pt Arial">
            <fo:block>
                Normal text 
                <fo:inline baseline-shift="sub" >sub-script</fo:inline>Œ 
                normal text 
                <fo:inline baseline-shift="super" >super-script</fo:inline>
                normal text.
            </fo:block>
            <fo:block>
                Normal text 
                <fo:inline baseline-shift="-50%" >-50%</fo:inline>Ž
                normal text 
                <fo:inline baseline-shift="50%" >+50%</fo:inline>
                normal text.
            </fo:block>
            <fo:block>
                Normal text 
                <fo:inline baseline-shift="-5pt" >-5pt</fo:inline>
                normal text 
                <fo:inline baseline-shift="5pt" >5pt</fo:inline>
                normal text.
            </fo:block>
        </fo:flow>
    </fo:page-sequence>
</fo:root>
For the complete source code for this code example see "Tutorial/Subscripts and Superscripts.fo" located under XML Documents Samples/Tutorial folder.

The rendering result is displayed in the next figure.

Figure 2

The property that controls the alignment of an inline element vertically within it's parent line is baseline-shift. As you can see in this example, the text can be shifted vertically using either "sub" Œ or "super"  which will use font metrics to determine the subscript or superscript positions. You can also use a percentual Ž or absolute  value.

 

Contents | < Flow Layout | Graphics >