Font Mapping with iText problem

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
harrisx
Posts: 4
Joined: Fri Mar 30, 2007 5:36 pm

Font Mapping with iText problem

Post by harrisx » Fri Jul 06, 2007 6:17 pm

Hi,

I'm trying to create charts with a custom font given as ttf-file and then put them in an itext pdf-document using a DefaultFontMapper.
The code looks like this:

Code: Select all

imports ....

public class ITextFontTest {

	public static void main(String[] args) {
		
		Document document = new Document(PageSize.A4.rotate());
		try {
			PdfWriter writer = PdfWriter.getInstance(document,
					new FileOutputStream("HelloWorld.pdf"));

			document.open();
			
			BaseFont bf = BaseFont.createFont("MyFont.ttf",
			BaseFont.CP1252, BaseFont.EMBEDDED);
			BaseFont bf = BaseFont.createFont("MyFont.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			
			Font font = new Font(bf, 10);
			document.add(new Paragraph("MyFont.ttf Test ", font));

			// Font Mapping 
			DefaultFontMapper mapper = new DefaultFontMapper();
			// font file is in the curtrent directory 
			mapper.insertDirectory(".");
			

			DefaultFontMapper.BaseFontParameters pp =
				mapper.getBaseFontParameters("My Font");
			System.out.println("My Font found ");
			if (pp!=null) {

				pp.encoding = BaseFont.IDENTITY_H;
				System.out.println("Fontname: "+ pp.fontName);
				System.out.println("embedded: + "+ pp.embedded);
				
			} else {
				System.out.println("My Font not found! ");
			}

			// Adding the Chart to PDF
			PdfContentByte cb = writer.getDirectContent();
			PdfTemplate tp = cb.createTemplate(750, 350);
			Graphics2D g2d = tp.createGraphics(750, 350, mapper);
			Rectangle2D r2d = new Rectangle2D.Double(0, 0, 750, 350);

			JFreeChart chart = getChart();

			chart.draw(g2d, r2d);
			g2d.dispose();
			cb.addTemplate(tp, document.getPageSize().width() / 2 - 750 / 2, document.bottom() + 10);

		} catch (DocumentException de) {
			System.err.println(de.getMessage());
		} catch (IOException ioe) {
			System.err.println(ioe.getMessage());
		}

		document.close();

	}

	private static JFreeChart getChart() {

	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for (int i = 0; i < 10; i++) {
			dataset.setValue(Math.random(), "first value", "probe " + i);
			dataset.setValue(Math.random(), "second value", "probe " + i);
		}

		final StackedBarRenderer myRenderer = new StackedBarRenderer();
		final CategoryPlot myPlot = new CategoryPlot();
		myPlot.setDataset(dataset);
		myPlot.setRenderer(myRenderer);
		myRenderer.setSeriesPaint(0, java.awt.Color.lightGray);
		myRenderer.setSeriesPaint(1, java.awt.Color.red);
		myPlot.setDomainAxis(new CategoryAxis());
		myPlot.setRangeAxis(new NumberAxis("NumberAxis"));
		myPlot.setOrientation(PlotOrientation.VERTICAL);
		
		java.awt.Font font = new java.awt.Font("My Font", java.awt.Font.PLAIN, 12);
		final JFreeChart chart = new JFreeChart("Test Chart with MyFont.ttf legends ", font, myPlot,true);

		chart.setBackgroundPaint(Color.white);
		chart.setBorderPaint(Color.black);

		return chart;
	}
}
On my Computer (Windows) I get a PDF with the font embedded - as expected. Under Linux and even on other Windows systems the pdf written contains also the embedded font for the iText paragraph, but unfortunately the mapping for JFreeChart doesn't seem to work - all fonts in the chart are Helvetica (can be seen in document properties-fonts in Acrobat Reader).

I don't know if this is a JFreeChart problem but perhaps someone has at least a hint or can show me a direction where to look further ...

Thanks ahead!

Harris

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Jul 09, 2007 12:55 pm

Not sure. Definitely worth asking on the iText mailing list. Fonts usually manage to confuse me, but when I get home I'll have another read of the relevant chapter in Bruno Lowagie's iText book and see if I can figure it out...
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked