It may be a limitation of the java.awt.Font.layoutGlyphVector() on surrogates.
Paulo ----- Original Message ----- From: "Justin Dolezy" <[email protected]> To: <[email protected]> Sent: Saturday, November 14, 2009 6:21 PM Subject: [iText-questions] Some unicode chars incorrect when painteddirectly (Double sharp and double flat musical symbols) > Hi, > > I'm creating PDF content by drawing direct to the Graphics2D surface (via > PdfTemplate.createGraphicsShapes). It's my understanding that no fonts are > (or even need to be) embedded, so what is draw to the Graphics2D is what > will appear on the PDF. > > However there are 2 unicode musical symbol chars (double flat: 𝄫 and > double sharp: 𝄪) which do not appear correctly - they actually appear as > Far Eastern characters (looks Chinese to my uneducated guess!). The > regular flat and sharp musical symbols are fine. > > I'm running this on Mac OS X - code pasted below so you can reproduce this > easily. Windows machines will need the Arial Unicode MS font installed. > > All the symbols appear fine if I put the StringPainter widget below in a > JFrame, but make them paint to the PDF and it goes wrong. > > I'm guessing this is an iText bug? Or am I doing something wrong here? > Seems pretty straightforward! > > Thanks for any tips or confirmation that it's a bug :) > Justin > > > --- EXAMPLE CODE DEMONSTRATING THE PROBLEM IN THE OUTPUT PDF: ---- > > package com.neckdiagrams.exporters; > > import java.awt.Dimension; > import java.awt.Font; > import java.awt.Graphics; > import java.awt.Graphics2D; > import java.io.FileOutputStream; > > import javax.swing.JComponent; > > import com.lowagie.text.Chunk; > import com.lowagie.text.Document; > import com.lowagie.text.DocumentException; > import com.lowagie.text.pdf.PdfContentByte; > import com.lowagie.text.pdf.PdfTemplate; > import com.lowagie.text.pdf.PdfWriter; > > public class PdfUnicodeTextImageTest > { > public static final String FLAT_SYMBOL = "\u266D"; > public static final String SHARP_SYMBOL = "\u266F"; > > public static final String DOUBLE_FLAT_SYMBOL = "\uD834\uDD2B"; > public static final String DOUBLE_SHARP_SYMBOL = "\uD834\uDD2A"; > > > > > > public static void main(String[] args) throws InterruptedException > { > new PdfUnicodeTextImageTest(); > Thread.sleep(2000); > System.exit(0); > } > > > > public PdfUnicodeTextImageTest() > { > try > { > final FileOutputStream fileOutputStream = new > FileOutputStream("/tmp/unicode_test.pdf"); > final Document document = new Document(); > final PdfWriter pdf = PdfWriter.getInstance(document, > fileOutputStream); > > > > try > { > document.open(); > document.setPageSize(new > com.lowagie.text.Rectangle(500,800)); > document.newPage(); > > > > printPage(document, pdf); > } > finally > { > document.close(); > fileOutputStream.flush(); > fileOutputStream.close(); > } > } > catch (Exception e) > { > e.printStackTrace(); > } > } > > private void printPage(Document document, PdfWriter pdf) throws > DocumentException > { > document.add(new Chunk(" ")); > final PdfContentByte contentbytes = pdf.getDirectContent(); > final PdfTemplate template = contentbytes.createTemplate(500, 800); > final Graphics2D graphics2d = template.createGraphicsShapes(500, > 800); > > StringPainter painter = new StringPainter(new Font("Arial Unicode > MS", Font.PLAIN, 16)); > painter.setBounds(50,50,200,100); > painter.paint(graphics2d); > > > > graphics2d.dispose(); > contentbytes.addTemplate(template, 0, 0); > } > > class StringPainter extends JComponent > { > private Font font; > > > > public StringPainter(Font font) > { > this.font = font; > } > > > > @Override > protected void paintComponent(Graphics g) > { > super.paintComponent(g); > > > > Graphics2D g2 = (Graphics2D) g; > > > > g2.setFont(font); > > > > g2.drawString("TEST: " + FLAT_SYMBOL + " " + > DOUBLE_FLAT_SYMBOL, 10, 50); > g2.drawString("TEST: " + SHARP_SYMBOL + " " + > DOUBLE_SHARP_SYMBOL, 10, 80); > } > } > } > > ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://www.1t3xt.com/docs/book.php Check the site with examples before you ask questions: http://www.1t3xt.info/examples/ You can also search the keywords list: http://1t3xt.info/tutorials/keywords/
