This is not really a bug as very small font sizes are in many places rounded
to zero but in any case I fixed iText to accept it. The fix is in the CVS.

----- Original Message ----- 
From: "Greg Steckman" <[EMAIL PROTECTED]>
To: <itext-questions@lists.sourceforge.net>
Sent: Thursday, August 18, 2005 9:37 PM
Subject: [iText-questions] Font rendering problem with Graphics2D and large
scale factors


> I'm rendering to a Graphics2D and the font output is not correct. I
> believe it is because I am using a large scale factor and small values
> for font size, positions, etc. Attached is the example from the web site
> (the one that draws a pear) with the modification that the scale has
> been set to 2834.646, and the position and size values correspondingly
> reduced. The attached PDF created by this code has the correct rendering
> of the pear, but the string "Pear" printed above it has the letters
> written on top of each other.
>
> I'm using the scale 2834.646 because 1 unit corresponds to 1 meter in
> output space, and is convenient for drawing things to scale.
>
> Is this a bug with iText, a limit in the PDF format's precision, or
> something else?
>
> Thanks,
>
> Greg
>
>
>
>
> public static void exportPDF(final File file){
>         final double scale=2834.646;
>         System.out.println("Using the java.awt.Graphics2D-object");
>
>         // step 1: creation of a document-object
>         Document document = new Document();
>
>         try{
>
>             // step 2: creation of the writer
>             PdfWriter writer = PdfWriter.getInstance(document, new
> FileOutputStream(file));
>
>             // step 3: we open the document
>             document.open();
>
>             // step 4: we grab the ContentByte and do some stuff with it
>
>             // we create a fontMapper and read all the fonts in the font
> directory
>             DefaultFontMapper mapper = new DefaultFontMapper();
>             FontFactory.registerDirectories();
>             mapper.insertDirectory("c:\\windows\\fonts");
>             // we create a template and a Graphics2D object that
> corresponds with it
>             int w = 150;
>             int h = 150;
>             PdfContentByte cb = writer.getDirectContent();
>             PdfTemplate tp = cb.createTemplate(w, h);
>             Graphics2D g2 = tp.createGraphics(w, h, mapper);
>             g2.scale(scale, scale);
>             tp.setWidth(w);
>             tp.setHeight(h);
>             double ew = w / 2/scale;
>             double eh = h / 2/scale;
>             Ellipse2D.Double circle, oval, leaf, stem;
>             Area circ, ov, leaf1, leaf2, st1, st2;
>             circle = new Ellipse2D.Double();
>             oval = new Ellipse2D.Double();
>             leaf = new Ellipse2D.Double();
>             stem = new Ellipse2D.Double();
>             circ = new Area(circle);
>             ov = new Area(oval);
>             leaf1 = new Area(leaf);
>             leaf2 = new Area(leaf);
>             st1 = new Area(stem);
>             st2 = new Area(stem);
>             g2.setColor(Color.green);
>
>             // Creates the first leaf by filling the intersection of two
> Area objects created from an ellipse.
>             leaf.setFrame(ew - 16/scale, eh - 29/scale, 15.0/scale,
> 15.0/scale);
>             leaf1 = new Area(leaf);
>             leaf.setFrame(ew - 14/scale, eh - 47/scale, 30.0/scale,
> 30.0/scale);
>             leaf2 = new Area(leaf);
>             leaf1.intersect(leaf2);
>             g2.fill(leaf1);
>
>             // Creates the second leaf.
>             leaf.setFrame(ew + 1/scale, eh - 29/scale, 15.0/scale,
> 15.0/scale);
>             leaf1 = new Area(leaf);
>             leaf2.intersect(leaf1);
>             g2.fill(leaf2);
>
>             g2.setColor(Color.black);
>
>             // Creates the stem by filling the Area resulting from the
> subtraction of two Area objects created from an
>             // ellipse.
>             stem.setFrame(ew, eh - 42/scale, 40.0/scale, 40.0/scale);
>             st1 = new Area(stem);
>             stem.setFrame(ew + 3/scale, eh - 47/scale, 50.0/scale,
> 50.0/scale);
>             st2 = new Area(stem);
>             st1.subtract(st2);
>             g2.fill(st1);
>
>             g2.setColor(Color.yellow);
>
>             // Creates the pear itself by filling the Area resulting
> from the union of two Area objects created by two
>             // different ellipses.
>             circle.setFrame(ew - 25/scale, eh, 50.0/scale, 50.0/scale);
>             oval.setFrame(ew - 19/scale, eh - 20/scale, 40.0/scale,
> 70.0/scale);
>             circ = new Area(circle);
>             ov = new Area(oval);
>             circ.add(ov);
>             g2.fill(circ);
>
>             g2.setColor(Color.black);
>             //java.awt.Font thisFont = new java.awt.Font("Arial",
> java.awt.Font.PLAIN, 18);
>             java.awt.Font
> thisFont=g2.getFont().deriveFont((float)(18/scale));
>             g2.setFont(thisFont);
>             String pear = "Pear";
>             FontMetrics metrics = g2.getFontMetrics();
>             int width = metrics.stringWidth(pear);
>             g2.drawString(pear, (float)((w/scale - width) / 2),
> (float)(20/scale));
>             g2.dispose();
>             cb.addTemplate(tp, 50, 400);
>
>         }catch(DocumentException de){
>             System.err.println(de.getMessage());
>         }catch(IOException ioe){
>             System.err.println(ioe.getMessage());
>         }
>
>         // step 5: we close the document
>         document.close();
>     }
>



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to