You can try to use net.sf.jasperreports.default.font.name
property.
Or you can use the default style
.
However, defining default styles or fonts in JasperReports is not mandatory. If no font is defined for a given element, the engine looks either for the inherited font attributes, or, if no attributes are found on this way, it looks for the net.sf.jasperreports.default.font.name
property in the
/src/default.jasperreports.properties
file. Its value defines the name of the font family to be used when font properties are not explicitly defined for a text element or inherited from its parent. The main default font properties and their values defined in the
/src/default.jasperreports.properties
file are:
-
net.sf.jasperreports.default.font.name=SansSerif
- the default font name. -
net.sf.jasperreports.default.font.size=10
- the default font size. -
net.sf.jasperreports.default.pdf.font.name=Helvetica
- the default PDF font. -
net.sf.jasperreports.default.pdf.encoding=Cp1252
- the default PDF character encoding. -
net.sf.jasperreports.default.pdf.embedded=false
- by default PDF fonts are not embedded
The Fonts Sample
This sample shows some practical examples of using fonts and font attributes in order to get a particular text appearance.
Because this sample uses a font extension based on the Gentium open source font files, and logical JVM font names also, it is strongly recommended to consult first the Font Extensions section below, and then to compile and run the sample.
In the example below, a series of font attributes are defined for the static text element:
One can say that this text will use a monospaced character set, 12 pts sized, underlined and oblique, and when exporting to PDF format, the equivalent fonts will be Courier-Oblique.The quick brown fox jumps over the lazy dog.
Running the Sample
Running the sample requires the Apache Ant library. Make sure that
ant
is already installed on your system (version 1.5 or later). In a command prompt/terminal window set the current folder to
demo/samples/fonts
within the JasperReports source project and run the > ant test view
command. It will generate all supported document types containing the sample report in the
demo/samples/fonts/build/reports
directory. A font extension sample xml file named fonts.xml
will be also generated in the same directory. It contains all font families available in the already installed font extensions. Then the report will open in the JasperReports internal viewer.
![](http1://img.it610.com/image/product/e18817209e7e476aa76ffa679de6c394.png)
/demo/samples/unicode |
Formerly used font definitions relied on font files available on the machine. In this case, when defining how a piece of text should look like, one had to take care about the following possible issues:
- The needed font library might not be available to the JVM at runtime because the font file is not installed on the system.
- When a font library is not available, the local JVM will replace it with some default fonts, and this could lead to various side effects, such as totally different text appearance or truncated pieces of text.
Therefore, this is not the best way to control fonts in JasperReports. A much better one is due to the extension points support, available in JasperReports. Font files can be provided as library extensions. In a very simple manner, making a font extension consists in putting various True Type Font files in a JAR file together with a properties file describing the content of the JAR, and an XML file defining relationships between fonts and locales.
Font Extensions Step By Step
Let's take a look into the
/demo/fonts
directory. It contains the DejaVu font extension available as default font for all samples shipped with the JasperReports project distribution package. As known from extensions support, any JasperReports extension provides a
jasperreports_extension.properties
file in its root package, required by the JasperReports extension mechanism. This file is used to describe the content of the extension JAR file and consists in the following lines: net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.dejavu=net/sf/jasperreports/fonts/fonts.xml
The SimpleFontExtensionsRegistryFactory class represents an implementation of the ExtensionsRegistryFactory interface, used to instantiate a font extension registry.
The extension registry obtained from the factory is able to return a list of actual extension point implementations based on extension point class type.
The second line provides the path to the XML file describing the actual font extension. The XML file in this case is named
fonts.xml
. The main unit in the
fonts.xml
file is the
element. A font family is an object instance which extends the FontFamily public interface. This is the point where font extensions can be connected with the JasperReports engine. Font families described in the
fonts.xml
file consist in up to 4 font faces: normal, bold, italic and bolditalic. A font face is described by the FontFace interface. In order to completely describe a font family extension, one have to indicate the mapping between the font faces and font files, the
pdfEncoding
and pdfEmbedded
attributes, equivalent font face names available for dedicated exporters, such as the HTML exporter) and a list of supported locales, because font files usually support only certain languages. The
fonts.xml
file includes 3 different font families:
DejaVu Sans
DejaVu Serif
DejaVu Sans Mono
SansSerif
Serif
Monospaced
Let's take a look at the DejaVu Sans family. All the font family settings already discussed can be found in the example below:
Thenet/sf/jasperreports/fonts/dejavu/DejaVuSans.ttf net/sf/jasperreports/fonts/dejavu/DejaVuSans-Bold.ttf net/sf/jasperreports/fonts/dejavu/DejaVuSans-Oblique.ttf net/sf/jasperreports/fonts/dejavu/DejaVuSans-BoldOblique.ttf Identity-H true 'DejaVu Sans', Arial, Helvetica, sans-serif 'DejaVu Sans', Arial, Helvetica, sans-serif
name
attribute and the
font face represent required elements in a font family definition, while all the others are optional. The name of the font family will be used as the
fontName
attribute of the text element or style in the report template. The fontName
together with the isBold
and isItalic
attributes of the text field or style in the report help to locate and load the appropriate font face from the family. If a particular font face is not present or declared in the family, then the normal font face will be used instead. In the example above we can see the mappings for the bold, italic and bolditalic font styles are also present. The
tag instructs the HTML exporters to correlate this font family with other HTML supported font families, such as Arial, Helvetica, sans-serif. The
contains a list of supported locales. This block being commented, the engine will try to apply this font family for any locale, without taking into account if the font file really provides support for that locale. If a particular locale is not supported, errors might occur at runtime and characters might be misrepresented. However, if a given font family needs to be represented for locales supported by different font files, one can define separate font families in the XML file, having the same name but with differing
tag. This feature is very useful when the same report has to be run in both Japanese and Chinese, because there is no TTF file that simultaneously supports these two languages. The
and
are used to specify the PDF encoding attribute and the PDF embedding flag, and people are strongly encouraged to use them instead of deprecated pdfEncoding
and pdfEmbedded
attributes available in the JRXML
tag. Now, let's take a look at logical JVM fonts mappings:
Here the DejaVu font families are added to the font families list available for HTML at export time.'DejaVu Sans', Arial, Helvetica, sans-serif 'DejaVu Sans', Arial, Helvetica, sans-serif 'DejaVu Serif', 'Times New Roman', Times, serif 'DejaVu Serif', 'Times New Roman', Times, serif 'DejaVu Sans Mono', 'Courier New', Courier, monospace 'DejaVu Sans Mono', 'Courier New', Courier, monospace
Once you have the TTF files, the
jasperreports_extension.properties
and fonts.xml
files, you can pack them together in a JAR file, and then put the JAR in your application's classpath, in order to make the new fonts available to your reports, wherever the application might run. For more details about deploying fonts as extensions, you can take a look at the Fonts sample provided with the JasperReports project distribution package, which adds one more font extension for another open source font called Gentium. Running the sample using the
> ant clean javac compile fontsXml
command will generate in the demo/samples/fonts/build/reports
a font extension xml file named fonts.xml
. This file contains all font families available in the already installed font extensions.
http://jasperreports.sourceforge.net/sample.reference/fonts/index.html