You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2009/06/25 19:10:25 UTC
svn commit: r788440 - in
/incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font:
PDSimpleFont.java PDTrueTypeFont.java PDType1Font.java
Author: lehmi
Date: Thu Jun 25 17:10:24 2009
New Revision: 788440
URL: http://svn.apache.org/viewvc?rev=788440&view=rev
Log:
PDFBOX-485: rotate the canvas instead of the font. Thanks to Volker Bier (volker dot bier at astrium dot eads dot net)
Modified:
incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDSimpleFont.java
incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java
incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java
Modified: incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDSimpleFont.java
URL: http://svn.apache.org/viewvc/incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDSimpleFont.java?rev=788440&r1=788439&r2=788440&view=diff
==============================================================================
--- incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDSimpleFont.java (original)
+++ incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDSimpleFont.java Thu Jun 25 17:10:24 2009
@@ -16,9 +16,12 @@
*/
package org.apache.pdfbox.pdmodel.font;
+import java.awt.Font;
import java.awt.Graphics;
+import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
-
+import java.awt.geom.Point2D;
+import java.awt.geom.NoninvertibleTransformException;
import java.io.IOException;
import java.util.HashMap;
@@ -67,7 +70,8 @@
/**
* {@inheritDoc}
*/
- public void drawString( String string, Graphics g, float fontSize, AffineTransform at, float x, float y ) throws IOException
+ public void drawString( String string, Graphics g, float fontSize,
+ AffineTransform at, float x, float y ) throws IOException
{
System.err.println( "Not yet implemented:" + getClass().getName() );
}
@@ -302,4 +306,51 @@
{
return getFontDescriptor().getFontBoundingBox();
}
+
+ /**
+ * This will draw a string on a canvas using the font.
+ *
+ * @param g2d The graphics to draw onto.
+ * @param at The transformation matrix with all infos for scaling and shearing of the font.
+ * @param awtFont The font to draw.
+ * @param fontSize The size of the font to draw.
+ * @param x The x coordinate to draw at.
+ * @param y The y coordinate to draw at.
+ * @param string The string to draw.
+ *
+ */
+ protected void writeFont(final Graphics2D g2d, final AffineTransform at, final Font awtFont,
+ final float fontSize, final float x, final float y, final String string)
+ {
+ // check if we have a rotation
+ if (!at.isIdentity())
+ {
+ try
+ {
+ AffineTransform atInv = at.createInverse();
+ // do only apply the size of the transform, rotation will be realized by rotating the graphics,
+ // otherwise the hp printers will not render the font
+ g2d.setFont(awtFont.deriveFont(fontSize));
+ // apply the inverse transformation to the graphics, which should be the same as applying the
+ // transformation itself to the text
+ g2d.transform(at);
+ // translate the coordinates
+ Point2D.Float newXy = new Point2D.Float(x,y);
+ atInv.transform(new Point2D.Float( x, y), newXy);
+ g2d.drawString( string, (float)newXy.getX(), (float)newXy.getY() );
+ // restore the original transformation
+ g2d.transform(atInv);
+ }
+ catch (NoninvertibleTransformException e)
+ {
+ System.err.println( "Error in "+getClass().getName()+".writeFont:"+e);
+ }
+ }
+ else
+ {
+ g2d.setFont( awtFont.deriveFont( at ).deriveFont( fontSize ) );
+ g2d.drawString( string, x, y );
+ }
+ }
+
}
Modified: incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java
URL: http://svn.apache.org/viewvc/incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java?rev=788440&r1=788439&r2=788440&view=diff
==============================================================================
--- incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java (original)
+++ incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java Thu Jun 25 17:10:24 2009
@@ -290,7 +290,8 @@
}
for( int i=0; i<widthValues.length; i++ )
{
- if(glyphToCCode[i]-firstChar < widths.size() && glyphToCCode[i]-firstChar >= 0 && widths.get( glyphToCCode[i]-firstChar) == zero )
+ if(glyphToCCode[i]-firstChar < widths.size() && glyphToCCode[i]-firstChar >= 0
+ && widths.get( glyphToCCode[i]-firstChar) == zero )
{
widths.set( glyphToCCode[i]-firstChar,
new Integer( (int)(widthValues[i]* 1000f)/header.getUnitsPerEm() ) );
@@ -315,7 +316,8 @@
/**
* {@inheritDoc}
*/
- public void drawString( String string, Graphics g, float fontSize, AffineTransform at, float x, float y ) throws IOException
+ public void drawString( String string, Graphics g, float fontSize,
+ AffineTransform at, float x, float y ) throws IOException
{
PDFontDescriptorDictionary fd = (PDFontDescriptorDictionary)getFontDescriptor();
if( awtFont == null )
@@ -325,49 +327,50 @@
{
try
{
- // create a font with the embedded data
- awtFont = Font.createFont( Font.TRUETYPE_FONT, ff2Stream.createInputStream() );
+ // create a font with the embedded data
+ awtFont = Font.createFont( Font.TRUETYPE_FONT, ff2Stream.createInputStream() );
}
catch( FontFormatException f )
{
- logger().info("Can't read the embedded font " + fd.getFontName() );
+ logger().info("Can't read the embedded font " + fd.getFontName() );
}
}
else
{
- // check if the font is part of our environment
- awtFont = FontManager.getAwtFont(fd.getFontName());
- if (awtFont == null)
- {
- logger().info("Can't find the specified font " + fd.getFontName() );
+ // check if the font is part of our environment
+ awtFont = FontManager.getAwtFont(fd.getFontName());
+ if (awtFont == null)
+ {
+ logger().info("Can't find the specified font " + fd.getFontName() );
// check if there is a font mapping for an external font file
TrueTypeFont ttf = getExternalFontFile2( fd );
- if( ttf != null )
+ if( ttf != null )
{
try
- {
- awtFont = Font.createFont( Font.TRUETYPE_FONT, ttf.getOriginalData() );
+ {
+ awtFont = Font.createFont( Font.TRUETYPE_FONT, ttf.getOriginalData() );
+ }
+ catch( FontFormatException f )
+ {
+ logger().info("Can't read the external fontfile " + fd.getFontName() );
}
- catch( FontFormatException f )
- {
- logger().info("Can't read the external fontfile " + fd.getFontName() );
- }
}
- }
+ }
}
- if (awtFont == null)
- {
- // we can't find anything, so we have to use the standard font
- awtFont = FontManager.getStandardFont();
- logger().info("Using font "+awtFont.getName()+ " instead");
+ if (awtFont == null)
+ {
+ // we can't find anything, so we have to use the standard font
+ awtFont = FontManager.getStandardFont();
+ logger().info("Using font "+awtFont.getName()+ " instead");
}
}
+
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
- g2d.setFont( awtFont.deriveFont( at ).deriveFont( fontSize ) );
- g2d.drawString( string, x, y );
+ writeFont(g2d, at, awtFont, fontSize, x, y, string);
}
+
/**
* Permit to load an external TTF Font program file
*
@@ -377,7 +380,7 @@
* @param fd The font descriptor currently used
*
* @return A PDStream with the Font File program, null if fd is null
- *
+ *grep -r
* @throws IOException If the font is not found
*/
private TrueTypeFont getExternalFontFile2(PDFontDescriptorDictionary fd)
Modified: incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java
URL: http://svn.apache.org/viewvc/incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java?rev=788440&r1=788439&r2=788440&view=diff
==============================================================================
--- incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java (original)
+++ incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java Thu Jun 25 17:10:24 2009
@@ -181,53 +181,63 @@
/**
* {@inheritDoc}
*/
- public void drawString( String string, Graphics g, float fontSize, AffineTransform at, float x, float y ) throws IOException
+ public void drawString( String string, Graphics g, float fontSize,
+ AffineTransform at, float x, float y ) throws IOException
{
if( awtFont == null )
{
String baseFont = getBaseFont();
- PDFontDescriptor fd = getFontDescriptor();
- if (fd != null && fd instanceof PDFontDescriptorDictionary){
- PDFontDescriptorDictionary fdDictionary = (PDFontDescriptorDictionary)fd;
- PDStream ffStream = fdDictionary.getFontFile();
- if( ffStream == null && fdDictionary.getFontFile3() != null)
- // TODO FontFile3-streams containing CIDFontType0C or OpenType fonts aren't yet supported
- logger().info("Embedded font-type is not supported " + fd.getFontName() );
- if( ffStream != null )
- {
- try {
- // create a font with the embedded data
- awtFont = Font.createFont( TYPE1_FONT, ffStream.createInputStream() );
- } catch (FontFormatException e) {
- logger().info("Can't read the embedded font " + fd.getFontName() );
- }
- }
- else {
- // check if the font is part of our environment
- awtFont = FontManager.getAwtFont(fd.getFontName());
- if (awtFont == null)
- logger().info("Can't find the specified font " + fd.getFontName() );
- }
- }
- else
- {
- // check if the font is part of our environment
- awtFont = FontManager.getAwtFont(baseFont);
- if (awtFont == null)
- logger().info("Can't find the specified basefont " + baseFont );
- }
- if (awtFont == null)
- {
- // we can't find anything, so we have to use the standard font
- awtFont = FontManager.getStandardFont();
- logger().info("Using font "+awtFont.getName()+ " instead");
- }
+ PDFontDescriptor fd = getFontDescriptor();
+ if (fd != null && fd instanceof PDFontDescriptorDictionary)
+ {
+ PDFontDescriptorDictionary fdDictionary = (PDFontDescriptorDictionary)fd;
+ PDStream ffStream = fdDictionary.getFontFile();
+ if( ffStream == null && fdDictionary.getFontFile3() != null)
+ {
+ // TODO FontFile3-streams containing CIDFontType0C or OpenType fonts aren't yet supported
+ logger().info("Embedded font-type is not supported " + fd.getFontName() );
+ }
+ if( ffStream != null )
+ {
+ try
+ {
+ // create a font with the embedded data
+ awtFont = Font.createFont( TYPE1_FONT, ffStream.createInputStream() );
+ }
+ catch (FontFormatException e)
+ {
+ logger().info("Can't read the embedded font " + fd.getFontName() );
+ }
+ }
+ else
+ {
+ // check if the font is part of our environment
+ awtFont = FontManager.getAwtFont(fd.getFontName());
+ if (awtFont == null)
+ {
+ logger().info("Can't find the specified font " + fd.getFontName() );
+ }
+ }
+ }
+ else
+ {
+ // check if the font is part of our environment
+ awtFont = FontManager.getAwtFont(baseFont);
+ if (awtFont == null)
+ {
+ logger().info("Can't find the specified basefont " + baseFont );
+ }
+ }
+ if (awtFont == null)
+ {
+ // we can't find anything, so we have to use the standard font
+ awtFont = FontManager.getStandardFont();
+ logger().info("Using font "+awtFont.getName()+ " instead");
+ }
}
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
- g2d.setFont( awtFont.deriveFont( at ).deriveFont( fontSize ) );
-
- g2d.drawString( string, x, y );
+ writeFont(g2d, at, awtFont, fontSize, x, y, string);
}
-
+
}