Space Handling
XSL-FO defines various attributes for managing whitespace in FO. These allow you to control how linefeeds and whitespace are output.
Linefeeds and carriage returns
A linefeed is a character with ASCII code 10, or Unicode code point U+000A. This is different to a carriage return which has ASCII code 13. Ibex acts on linefeeds, not on carriage returns. Carriage returns are ignored during PDF creation.
Default treatment of linefeeds and spaces
By default linefeeds and whitespace preceding and following linefeeds are removed during formatting.
Figure 11-1 shows FO which has linefeeds at the end of each line. The resulting output shown in Figure 11-2 has neither linefeeds nor spaces around the text. This is the default treatment for text in XSL-FO.
<fo:block margin='2cm'>To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
</fo:block>
Using linefeeds to break text
The linefeed-treatment attribute is used to specify the treatment of linefeeds in text. This defaults to "ignore" causing linefeeds to be ignored. We can retain the linefeeds by setting the linefeed-treatment attribute to "preserve". Figure 11-3 shows our example with this attribute added. Figure 11-4 shows the output from this FO.
<fo:block linefeed-treatment="preserve">To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
</fo:fo:block>
We do not recommend using the linefeed-treatment attribute. It is too prone to breaking when the file is edited with different tools which might insert carriage returns instead of linefeeds or might reformat lines. It is better to use empty fo:block elements to mark line endings like this:
<fo:block>To be, or not to be: that is the question:<fo:block/>
Whether 'tis nobler in the mind to suffer<fo:block/>
The slings and arrows of outrageous fortune,<fo:block/>
Or to take arms against a sea of troubles,<fo:block/>
</fo:fo:block>
Retaining spaces
The white-space-treatment and white-space-collapse attributes are used to control the handling of spaces.
If we want to put some formatted code in our document, Figure 11-6 shows FO for this.
<fo:block linefeed-treatment="preserve">
private void swap_byte( ref byte x, ref byte y ) {
byte t = x;
x = y;
y = t;
}
</fo:block>
Setting linefeed-treatment = "preserve" we get the output show in Figure 11-7. We have preserved the linefeeds but all formatting spaces have gone.
The white-space-collapse attribute controls whether Ibex compresses adjacent white space characters into a single space. By default any number of adjacent spaces are compressed into a single space.
The white-space-treatment attribute controls whether Ibex ignores spaces adjacent to linefeeds. Setting white-space-treatment = "preserve" makes Ibex retain white space which appears adjacent to linefeeds.
If we set white-space-treatment to "preserve", and white-space-collapse to "false" we will retain the white spaces around the linefeeds. The FO for this is shown in Figure 11-8, and the formatted output is shown in Figure 11-9.
<fo:block
linefeed-treatment="preserve"
white-space-treatment="preserve"
white-space-collapse="false">
private void swap_byte( ref byte x, ref byte y ) {
byte t = x;
x = y;
y = t;
}
</fo:block>
Non-breaking spaces
Unicode defines the code point U+00A0 called NO-BREAK SPACE. This can be used to insert a space between words without allowing a line break to occur between the words. Ibex treats two words separated by a U+00A0 as a single word.
The non-breaking space can be inserted into XML using the entity. The example in Figure 11-10 shows a block used in a table header. It contains the three words "Score per 100". The default formatting is shown in Figure 11-11. If we want to move the word "per" to the next line to keep it with the "100", we replace the space between "per" and "100" with a non-breaking space. This will prevent Ibex breaking the line between the "per" and "100" words.
<fo:block-container width="2.8cm">
<fo:block border="1pt solid black"
padding="3pt" text-align="center">
Score per 100
</fo:block>
</fo:block-container>
<fo:block-container width="2.8cm">
<fo:block border="1pt solid black"
padding="3pt" text-align="center">
Score per 100
</fo:fo:block>
</fo:fo:block-container>
Figure 11-13 shows the FO with a non-breaking space and Figure 11-14 shows the resulting output.