Jump to content

How to map/create a fontName ?


blaguman

Recommended Posts

Hello everybody !

I know this problem is frequent, but I have a question concerning what says Teodor about fonts :
In case of a font not available on every system, he says to "make sure you rely on some TTF files that you ship with your application and install them on-the-fly using the Font.createFont() method"
I indeed have ttf files, and my reports print correctly the text in these fonts, but do the measure with a default font, with different metrics, 'cause the system does not know the font.

How can I create a font on-the-fly ?

I created the font with the createFont method, and I have a Font object, but what do I have to do with it ? I tried to build a <FontKey, Font> map, and pass it to the exporter through the JRExporterParameter.FONT_MAP element, but the types do not match (a PdfFont is required, not a Font). So what's the difference between JRExporterParameter.FONT_MAP and JRPdfExporterParameter.FONT_MAP, by the way ?

I'd really like my application to work soon, so your my last chance... :)

If someone could give me a way to deal with that, it would be very welcome !

 

Regards,

Morgan

Link to comment
Share on other sites

  • Replies 8
  • Created
  • Last Reply

Top Posters In This Topic

Hi,

 

Since JR 3.1.3, when we introduced support for font extensions, working with fonts is much more easy.

You no longer have to call API methods to install fonts on the fly, or pass font mapping parameters to the exporter.

All you have to do is to put those TTF files in a JAR, together with an XML file which maps them to family names that you can later use with the fontName attribute inside report templates.

 

You can take a look at the /demo/fonts folder in the JR project distribution and you'll find the content of a font extension that provides two open source font families: DejaVu Sans and DejaVu Serif. Many of the samples in the JR distro use these font families directly from this font extension, just because that folder is part of the classpath.

You can package up your TTF files in a similar way and in a JAR that you just need to drop in the classpath. This is exactly what iR does, which contains a font extension JAR in its classpath called jasperreports-dejavu-fonts.jar.

Look at the content of the font.xml and jasperreports_extension.properties files present in-there and you'll immediately see what you need to do in order to make your TTF files available to JR on any machine.

 

I hope this helps.
Teodor

 

Link to comment
Share on other sites

Thank you again, Teodor, I think it's starting to work as I want !

Just a question again.. I have several languages supported in my application, and I'd like to have only one template for each document. In the fontName. Is it possible to specify, in the xml file, which font should be used for each locale (also using iTextAsian fonts) ?

Thank you very much...

Regards,

Morgan

Link to comment
Share on other sites

OK, I finally found how to deal with the font for each locale, but I still cannot use the iTextAsian fonts in the xml file.

I wrote something like this, which works :

...

<property name="name" value="TigreFont"/>
<property name="normal" value="ARIALNB.TTF"/>

...

 

And I would like to use a japanese font, for example, and write :

...

<property name="name" value="TigreFont"/>
<property name="normal" value="HeiseiKakuGo-W5"/>

...

But the HeiseiKakuGo font is not recognized. Do I have to prefix/suffix it with something so Jasper can understand it's a built-in font included in the iTextAsian jar ?

Thank you :)

 

Regards,

Morgan

Link to comment
Share on other sites

And what if I put the content of the iTextAsian archive in my jar ? How can I use these cmap files ? I'm sure there's an easy solution to this problem, but I'm still stucked... If a good soul is reading this post... :)

Regards,

Morgan



Post Edited by blaguman at 06/25/2009 08:49
Link to comment
Share on other sites

  • 2 weeks later...

Hi,

 

Yes, the solution is easy. You should simply not use this iTextAsian.jar file.

Also, in answering to your previous question about locales, if you have looked into the /demo/fonts folder as I told you, you would have seen that inside the fonts.xml file there are some locale lines commented out. They are there just to show you that you could define font families that apply to certain locales.

This way you could define two font families, having the same name, but pointing to different TTF files and applying to different locale. This way, inside the report template you only use one font with the fontName attribute, but at runtime, depending on the report locale, there are different TTF files that get used.

 

Why not using the iTextAsian.jar file? Because this iText specific file would be useful in case you would only use iText and it would allow you to generate PDF files directly, withouth the need to actually have the Asian fonts on your machine. You would rely on those fonts being available on the machine that views the resulting PDF. But with JR, we cannot use the iTextAsian.jar because we actually DO NEED the TTF files when the report is generated. They are required for the font metrics calculations that occur prior to the PDF being generated. So you see, when you use JR, you must HAVE the TTF files with you. You should pack them in a font extension as mentioned above.

In order to ease up addoption of font extensions, I have enhanced the /demo/samples/unicode sample to show how to generated text in Chinese. That sample now uses an open source Chinese font that is shipped as an extension inside the sample itself. You should get this sample from SVN, because it was not released yet with this enhancement.

http://jasperforge.org/scm/viewvc.php/trunk/jasperreports/demo/samples/unicode/?root=jasperreports

 

I hope this helps.

Teodor

 

Link to comment
Share on other sites

Hello Teodor.

 

Actually, you already answered to my questions on another topic :) But it is very nice of you to take time to answer this kind of problems.

Indeed, I understood I had to give JR the ttf files so it can have the metrics and then properly do the text measure. The only problem is I now have big pdf files, 'cause I guess the font are embedded. It's not a big issue, but I'm still wondering if there is a way to prevent Jasper from embedding the fonts, even if I know I would rely on the presence of these fonts on the client machine.

I noticed the 'pdfEmbedded' flag in the XML font extension process, but I don't see any difference between both true & false values at this point.

If you have the ultimate tip, I'd appreciate it :)

Thank you very much once again for everything you've done.

 

Regards,

Morgan

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...