8.17. Text element #

8.17.1. Text element attributes

Two variants are accepted.

<Line>
    <field value="expression..." ... />
</Line>

and

<Line>
    <literal ... >Literal text</literal>
</Line>

Neither field nor literal have child nodes, only attributes or XML values.

The two variants are interchangeable, see Text element value below.

8.17.1. Text element attributes #

Note that Expressions in attribute settings below depend on the parent node context. Some may only use constant expressions or query column references from Independent queries. Child nodes of <Output> nodes in <Report> context may also use report query column references.

8.17.1.1. Value #

The value of the text element. It's accepted in two ways: in the value attribute or as the XML value for the field and literal nodes.

<Line>
    <field value="'This text'" />
    <field>This text</field>
    <literal value="'This text'" />
    <literal>This text</literal>
</Line>

The value is parsed as an expression from the value attribute. See Expressions.

On the other hand, the value is taken as a literal string from the XML value in the second and fourth examples above.

8.17.1.2. Delayed (precalculated) value #

This setting indicates whether the value is "delayed" or "precalculated", i.e. the value for the last row in the data set is used for every row. It is equivalent to referencing an Expression variable with precalculate="yes" and without resetonbreak.

<field delayed="yes" />
<field precalculate="yes" />

Default is false.

This setting is only applicable for line elements in the Output node node in <Report> context.

Note that in OpenCReports, an expression may mix references to precalculated variables with non-precalculated variables and query field references. The result will use the precalculated value only for the precalculated variables. Other references will use values derived from the current row in the data set. This is an extension over RLIB.

8.17.1.3. Format string #

The format string is one way to format the field value to be displayed in the generated output.

The format string is expected to be a string so quoting around it is needed.

See Formatting data for details.

8.17.1.3.1. Format attribute examples #

Format a value as a string, first as is, second with adding a surrounding text:

<field value="query1.field1" format="'%s'" >

<field value="query1.field1"
       format="'Look, there is a %s there!'" >

Here's the same with the "new syle" formatting flag:

<field value="query1.field1" format="'!&%s'" />

<field value="query1.field1"
       format="'Look, there is a !&%s there!'" >

Also, with the 2nd generation new style formatting flag:

<field value="query1.field1"
       format="'!&{%s}'" ... />

Here's an example to truncate a string to 6 characters using format string flags in legacy and both new style formatting flags:

<field value="query1.field1"
       format="'%6.6s'" ... />

<field value="query1.field1"
       format="'!&%6.6s'" ... />

<field value="query1.field1"
       format="'!&{%6.6s}'" ... />

Format a numeric value with three decimal places, first as is, second with adding a surrounding text:

<field value="query1.field1"
       format="'%.3d'" >

<field value="query1.field1"
       format="'You have %.3d apples.'" >

Convert a number from a string and the value with two decimal places:

<field value="val(query1.field1)"
       format="'%.2d'" />

Here are the same examples using the "new style" formatting flags:

<field value="query1.field1"
       format="'!#%.3d'" >

<field value="query1.field1"
       format="'You have !#%.3d apples.'" >

<field value="val(query1.field1)"
       format="'!#%.2d'" />

Here are the same examples using the 2nd generation new style formatting flags:

<field value="query1.field1"
       format="'!#{%.3d}'" >

<field value="query1.field1"
       format="'You have !#{%.3d} apples.'" >

<field value="val(query1.field1)"
       format="'!#%.2d'" />

Format a numeric value with monetary details either using the first or the second generation format strings:

<field value="query1.field1"
       format="'!$%=*#150n'" />

<field value="query1.field1"
       format="'!${%=*#150n}'" />

Format a datetime value, first with the preferred datetime format for the locale, then only the year, month and day using the YYYY-MM-DD format:

<field value="query1.field1"
       format="'!@%c'" />

<field value="query1.field1"
       format="'!@%Y-%m-%d'" ... />

Here's the same using the second generation format strings:

<field value="query1.field1"
       format="'!@{%c}'" ... />

<field value="query1.field1"
       format="'!@{%Y-%m-%d}'" ... />

8.17.1.4. Width #

The field width.

<field value="3" format="'%.2d'" width="6" />

Default is unset, i.e. the field width is implicitly set to the rendered width (in points) of the field's formatted value as text, using the font name and font size of the field.

When the field is the last one in the <Line>, then its width will be the remaining page or column width.

If set, the width's unit depends on the Size unit attribute.

When the Size unit attribute is set to rlib, the field width is measured in number of characters.

The character width is queried from the font set by Line font name and Line font size, Report font name and Report font size, or Part font name and Part font size in decreasing order of precedence.

Text element font name and Text element font size do not influence the field width calculation.

This allows using uniquely set font name and size for individual text elements, including proportional fonts, using the RLIB compatible field width settings.

When using different font names or font sizes for different text elements in the same <Line>, the text elements are aligned vertically so all text elements are rendered on the same typographic baseline.

8.17.1.5. Alignment #

It specifies the alignment for the text element. It works in accordance with the Text element width, i.e. the field's formatted value (as text) is aligned inside the specified field width.

<field value="3" format="'%.2d'"
       width="6" align="'center'" />

The alignment values may be left, right, center or justified.

Justified alignment is like left alignment, but for Multi-line (memo) fields, all lines but the last are justified. This is best used with the default word wrapping. In this case, the spacing between words is enlarged. justified and left behave the same for single-line fields.

Default is left.

OpenCReports decoupled the data from displaying it. For example, with the PDF output, the text element is not truncated to the element width. Instead, pixel perfect alignment is used together with masking the parts of the value with a bounding box. This is an improvement over RLIB where data was truncated (in every output formats) because it was designed for using only monospace fonts.

8.17.1.6. Text color #

This color is used to render text. It's accepted with both American and British spelling.

<Line color="'blue'">
<Line colour="'blue'">

Default is what's set for Line node, otherwise black. See Color specification.

8.17.1.7. Background color #

This color is used to render the background rectangle under the text. It's accepted with both American and British spelling.

<Line bgcolor="'blue'">
<Line bgcolour="'blue'">

Default is what's set for Line node, otherwise white. See Color specification.

8.17.1.8. Font name #

The font name attribute specifies the font for the text element's scope. It may be specified in two forms, the first one is the preferred name, the second is for RLIB compatibility:

<Part font_name="'Arial'">
<Part fontName="'Arial'">

If both forms are specified, font_name is used.

Default font name is what's set (in decreasing priority) in Line font name, Report font name or Part font name. If none of them is set, it's Courier.

8.17.1.9. Font size #

The font size attribute specifies the font size for the text element's scope. It may be specified in two forms, the first one is the preferred name, the second is for RLIB compatibility:

<Part font_size="10">
<Part fontSize="10">

If both forms are specified, font_size is used.

Default font name is what's set (in decreasing priority) in Line font size, Report font size or Part font size. If none of them is set, it's 12.

8.17.1.10. Bold font #

Whether the text element uses bold font. It overrides the Line bold attribute for this text element.

<field bold="yes" />

Default is what's set for Line bold attribute. false if both are unset.

8.17.1.11. Italic font #

Whether the text element uses italic font. It overrides the Line italic attribute for this text element. It is accepted in two forms:

<field italic="yes" />
<field italics="yes" />

Default is what's set for Line italic attribute. false if both are unset.

8.17.1.13. Multi-line (memo) field #

This attribute allows breaking long text fields to multiple lines according to the Text element width.

<field value="'This is a long text...'"
       width="12" memo="yes" />

Default is false, i.e. the field is rendered on a single line.

Every line of the text element (regardless if it's a memo field or not) is aligned according to Text element alignment.

8.17.1.14. Multi-line field wrapping #

This attribute allows wrapping multi-line text at characters as opposed to words.

<field value="'This is a long text...'"
       width="12" memo="yes" memo_wrap_chars="yes" />

Default is false, i.e. the field is wrapped at word boundaries.

In OpenCReports, character wrapping adds hyphenation. This is an improvement over RLIB.

8.17.1.15. Multi-line field row limit #

This attribute allows limiting multi-line text with a maximum row number.

<field value="'This is a long text...'"
       width="12" memo="yes" memo_max_lines="20" />

Default is no limit.

8.17.1.16. Translation #

This attribute allows the text element to be translated to a specified language or locale. See Locale.

<field value="'This is a field'"
       translate="yes" />

The expression result for translate must be numeric (boolean).

For translations to work, the translation settings and the language (locale) must be correctly set up.

OpenCReports will attempt to translate both the format string (if specified) and the text element's value. For example, if the format string has a translated variant in the translations, then this formatted result will be translated:

<field value="q.apples"
       format="'You have %d apples.'"
       translate="yes" />

Default is no.

An alternative way is to use the translation functions directly in the field expression. See translate() and translate2(). When using them, the translate="yes" attribute is not needed.

8.17.1.17. Column number #

This attribute is accepted for RLIB compatibility, but it's unused.

<field value="'This is a long text...'"
       col="3" />