You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2006/05/31 23:17:19 UTC

svn commit: r410672 - in /xmlgraphics/fop/trunk/src: documentation/content/xdocs/trunk/ java/org/apache/fop/fonts/ java/org/apache/fop/render/java2d/ sandbox/org/apache/fop/render/pcl/

Author: jeremias
Date: Wed May 31 14:17:18 2006
New Revision: 410672

URL: http://svn.apache.org/viewvc?rev=410672&view=rev
Log:
Improved accuracy of font size selection. The font size is not rounded down to the next integer point value anymore. (Java2D renderers profit from that one, too)

PCL Renderer:
Found a use case for that Java2D ascent value (which I call MaxAscent). It is used for painting text as bitmaps and to make sure the image the text is painted on is big enough if the font ascends beyond the em box. For non-Java2D fonts, MaxAscent is the same as ascent.
Added a check that lets text painting fall back to bitmaps if there are characters that are not in the ISO-8859-1 encoding. This also enables Symbol and ZapfDingbats fonts.
A "text-rendering" setting allows to disable PCL text painting in case the mix of PCL and bitmap text painting should result in unwelcome output. Note: the bitmap rendering is relatively slow (many small bitmaps). In the end we might end up rendering using a BitmapRenderer and only wrapping the whole thing in PCL (much like the Windows PCL drivers do). This would be faster than creating many small bitmaps.

Modified:
    xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/configuration.xml
    xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/output.xml
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontMetrics.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/LazyFont.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/Typeface.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/FontMetricsMapper.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/Java2DFontMetrics.java
    xmlgraphics/fop/trunk/src/sandbox/org/apache/fop/render/pcl/PCLRenderer.java

Modified: xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/configuration.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/configuration.xml?rev=410672&r1=410671&r2=410672&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/configuration.xml (original)
+++ xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/configuration.xml Wed May 31 14:17:18 2006
@@ -253,10 +253,11 @@
         offered by Java.
       </p>
       <p>
-        Additionally, there's one setting that controls how borders are painted.
+        Additionally, there are certain settings that control who the renderer handles various elements.
       </p>
 <source><![CDATA[<renderer mime="application/vnd.hp-PCL">
   <rendering>quality</rendering>
+  <text-rendering>bitmap</text-rendering>
 </renderer>]]></source>
       <p>
         The default value for the "rendering" setting is "speed" which causes borders 
@@ -264,6 +265,12 @@
         dashed etc.) are available. If you want support for all border modes, set the
         value to "quality" as indicated above. This will cause the borders to be painted
         as bitmaps.
+      </p>
+      <p>
+        The default value for the "text-rendering" setting is "auto" which paints the
+        base fonts using PCL fonts. Non-base fonts are painted as bitmaps through Java2D.
+        If the mix of painting methods results in unwelcome output, you can set this
+        to "bitmap" which causes all text to be rendered as bitmaps.
       </p>
     </section>
   </section>

Modified: xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/output.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/output.xml?rev=410672&r1=410671&r2=410672&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/output.xml (original)
+++ xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/output.xml Wed May 31 14:17:18 2006
@@ -369,10 +369,11 @@
     <section id="pcl-configuration">
       <title>Configuration</title>
       <p>
-        The PCL renderer configuration currently allows one setting:
+        The PCL renderer configuration currently allows the following settings:
       </p>
 <source><![CDATA[<renderer mime="application/vnd.hp-PCL">
   <rendering>quality</rendering>
+  <text-rendering>bitmap</text-rendering>
 </renderer>]]></source>
       <p>
         The default value for the "rendering" setting is "speed" which causes borders 
@@ -380,6 +381,12 @@
         dashed etc.) are available. If you want support for all border modes, set the
         value to "quality" as indicated above. This will cause the borders to be painted
         as bitmaps.
+      </p>
+      <p>
+        The default value for the "text-rendering" setting is "auto" which paints the
+        base fonts using PCL fonts. Non-base fonts are painted as bitmaps through Java2D.
+        If the mix of painting methods results in unwelcome output, you can set this
+        to "bitmap" which causes all text to be rendered as bitmaps.
       </p>
       <p>
         You can control the output resolution for the PCL using the "target resolution" 

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontMetrics.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontMetrics.java?rev=410672&r1=410671&r2=410672&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontMetrics.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontMetrics.java Wed May 31 14:17:18 2006
@@ -41,8 +41,16 @@
     
 
     /**
+     * Returns the maximum ascent of the font described by this
+     * FontMetrics object. Note: This is not the same as getAscender().
+     * @param size font size
+     * @return ascent in milliponts
+     */
+    int getMaxAscent(int size);
+    
+    /**
      * Returns the ascent of the font described by this
-     * FontMetrics object.
+     * FontMetrics object. It returns the nominal ascent within the em box.
      * @param size font size
      * @return ascent in milliponts
      */

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/LazyFont.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/LazyFont.java?rev=410672&r1=410671&r2=410672&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/LazyFont.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/LazyFont.java Wed May 31 14:17:18 2006
@@ -177,6 +177,14 @@
     }
 
     /**
+     * @see org.apache.fop.fonts.FontMetrics#getMaxAscent(int)
+     */
+    public int getMaxAscent(int size) {
+        load(true);
+        return realFont.getMaxAscent(size);
+    }
+
+    /**
      * @see org.apache.fop.fonts.FontMetrics#getAscender(int)
      */
     public int getAscender(int size) {

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/Typeface.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/Typeface.java?rev=410672&r1=410671&r2=410672&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/Typeface.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/Typeface.java Wed May 31 14:17:18 2006
@@ -54,5 +54,10 @@
         return false;
     }
 
+    /** @see org.apache.fop.fonts.FontMetrics#getMaxAscent(int) */
+    public int getMaxAscent(int size) {
+        return getAscender(size);
+    }
+    
 }
 

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/FontMetricsMapper.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/FontMetricsMapper.java?rev=410672&r1=410671&r2=410672&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/FontMetricsMapper.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/FontMetricsMapper.java Wed May 31 14:17:18 2006
@@ -86,6 +86,13 @@
     }
     
     /**
+     * @see org.apache.fop.fonts.FontMetrics#getMaxAscent(int)
+     */
+    public int getMaxAscent(int size) {
+        return metric.getMaxAscent(family, style, size);
+    }
+
+    /**
      * @see org.apache.fop.fonts.FontMetrics#getAscender(int)
      */
     public int getAscender(int size) {

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/Java2DFontMetrics.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/Java2DFontMetrics.java?rev=410672&r1=410671&r2=410672&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/Java2DFontMetrics.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/Java2DFontMetrics.java Wed May 31 14:17:18 2006
@@ -23,7 +23,10 @@
 import java.awt.Graphics2D;
 import java.awt.geom.Rectangle2D;
 import java.awt.FontMetrics;
+import java.awt.font.LineMetrics;
+import java.awt.font.TextAttribute;
 import java.awt.font.TextLayout;
+import java.util.Map;
 
 /**
  * This is a FontMetrics to be used  for AWT  rendering.
@@ -97,6 +100,9 @@
      */
     private FontMetrics fmt = null;
 
+    /** A LineMetrics to access high-resolution metrics information. */
+    private LineMetrics lineMetrics;
+    
     /**
      * Temp graphics object needed to get the font metrics
      */
@@ -112,6 +118,19 @@
     }
 
     /**
+     * Determines the font's maximum ascent of the Font described by the current
+     * FontMetrics object
+     * @param family font family (java name) to use
+     * @param style font style (java def.) to use
+     * @param size font size
+     * @return ascent in milliponts
+     */
+    public int getMaxAscent(String family, int style, int size) {
+        setFont(family, style, size);
+        return Math.round(lineMetrics.getAscent() * FONT_FACTOR);
+    }
+    
+    /**
      * Determines the font ascent of the Font described by this
      * FontMetrics object
      * @param family font family (java name) to use
@@ -233,6 +252,19 @@
         return width;
     }
 
+    private Font getBaseFont(String family, int style, float size) {
+        Map atts = new java.util.HashMap();
+        atts.put(TextAttribute.FAMILY, family);
+        if ((style & Font.BOLD) != 0) {
+            atts.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
+        }
+        if ((style & Font.ITALIC) != 0) {
+            atts.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
+        }
+        atts.put(TextAttribute.SIZE, new Float(size)); //size in pt
+        return new Font(atts);
+    }
+    
     /**
      * Checks whether the font  for which values are
      * requested is the one used immediately before or
@@ -244,21 +276,19 @@
      */
     private boolean setFont(String family, int style, int size) {
         boolean changed = false;
-        //TODO this seems bad. It rounds font sizes down to the next integer value (=pt)
-        int s = (int)(size / 1000f);
-        //int s = size;
+        float s = size / 1000f;
 
         if (f1 == null) {
-            f1 = new Font(family, style, s);
+            f1 = getBaseFont(family, style, s);
             fmt = graphics.getFontMetrics(f1);
             changed = true;
         } else {
             if ((this.style != style) || !this.family.equals(family)
                     || this.size != s) {
                 if (family.equals(this.family)) {
-                    f1 = f1.deriveFont(style, (float)s);
+                    f1 = f1.deriveFont(style, s);
                 } else {
-                    f1 = new Font(family, style, s);
+                    f1 = getBaseFont(family, style, s);
                 }
                 fmt = graphics.getFontMetrics(f1);
                 changed = true;
@@ -282,7 +312,7 @@
             descender = (int)Math.round((rect.getY() + rect.getHeight()) * -1000);
             
             //Alternative way to get metrics but the ascender is again wrong for our purposes
-            //lineMetrics = f1.getLineMetrics("", graphics.getFontRenderContext());
+            lineMetrics = f1.getLineMetrics("", graphics.getFontRenderContext());
         }
         // save the family and style for later comparison
         this.family = family;

Modified: xmlgraphics/fop/trunk/src/sandbox/org/apache/fop/render/pcl/PCLRenderer.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/sandbox/org/apache/fop/render/pcl/PCLRenderer.java?rev=410672&r1=410671&r2=410672&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/sandbox/org/apache/fop/render/pcl/PCLRenderer.java (original)
+++ xmlgraphics/fop/trunk/src/sandbox/org/apache/fop/render/pcl/PCLRenderer.java Wed May 31 14:17:18 2006
@@ -128,6 +128,12 @@
     private boolean qualityBeforeSpeed = false;
     
     /**
+     * Controls whether all text should be painted as text. This is a fallback setting in case
+     * the mixture of native and bitmapped text does not provide the necessary quality.
+     */
+    private boolean allTextAsBitmaps = false;
+    
+    /**
      * Create the PCL renderer
      */
     public PCLRenderer() {
@@ -148,6 +154,16 @@
                     "Valid values for 'rendering' are 'quality' and 'speed'. Value found: " 
                         + rendering);
         }
+        String textRendering = cfg.getChild("text-rendering").getValue(null);
+        if ("bitmap".equalsIgnoreCase(textRendering)) {
+            this.allTextAsBitmaps = true;
+        } else if ("auto".equalsIgnoreCase(textRendering)) {
+            this.allTextAsBitmaps = false;
+        } else if (textRendering != null) {
+            throw new ConfigurationException(
+                    "Valid values for 'text-rendering' are 'auto' and 'bitmap'. Value found: " 
+                        + textRendering);
+        }
     }
 
     /**
@@ -202,10 +218,17 @@
      * Sets the current font (NOTE: Hard-coded font mappings ATM!)
      * @param name the font name (internal F* names for now)
      * @param size the font size
+     * @param text the text to be rendered (used to determine if there are non-printable chars)
      * @return true if the font can be mapped to PCL
      * @throws IOException if an I/O problem occurs
      */
-    public boolean setFont(String name, float size) throws IOException {
+    public boolean setFont(String name, float size, String text) throws IOException {
+        byte[] encoded = text.getBytes("ISO-8859-1");
+        for (int i = 0, c = encoded.length; i < c; i++) {
+            if (encoded[i] == 0x3F && text.charAt(i) != '?') {
+                return false;
+            }
+        }
         int fontcode = 0;
         if (name.length() > 1 && name.charAt(0) == 'F') {
             try {
@@ -214,6 +237,7 @@
                 log.error(e);
             }
         }
+        //Note "(ON" selects ISO 8859-1 symbol set as used by PCLGenerator
         String formattedSize = gen.formatDouble2(size / 1000);
         switch (fontcode) {
         case 1:     // F1 = Helvetica
@@ -288,17 +312,19 @@
             break;
         case 13:    // F13 = Symbol
 
-            gen.writeCommand("(19M");
-            gen.writeCommand("(s1p" + formattedSize + "v0s0b16686T");
+            return false;
+            //gen.writeCommand("(19M");
+            //gen.writeCommand("(s1p" + formattedSize + "v0s0b16686T");
             // ECMA Latin 1 Symbol Set in Times Roman???
             // gen.writeCommand("(9U");
             // gen.writeCommand("(s1p" + formattedSize + "v0s0b25093T");
-            break;
+            //break;
         case 14:    // F14 = Zapf Dingbats
 
-            gen.writeCommand("(14L");
-            gen.writeCommand("(s1p" + formattedSize + "v0s0b45101T");
-            break;
+            return false;
+            //gen.writeCommand("(14L");
+            //gen.writeCommand("(s1p" + formattedSize + "v0s0b45101T");
+            //break;
         default:
             //gen.writeCommand("(0N");
             //gen.writeCommand("(s" + formattedSize + "V");
@@ -554,7 +580,9 @@
 
         try {
             final Color col = (Color)text.getTrait(Trait.COLOR);
-            boolean pclFont = setFont(fontname, fontsize);
+            boolean pclFont = allTextAsBitmaps 
+                    ? false
+                    : setFont(fontname, fontsize, text.getText()); 
             if (pclFont) {
                 //this.currentFill = col;
                 if (col != null) {
@@ -577,11 +605,15 @@
                 
                 //for cursive fonts, so the text isn't clipped
                 int extraWidth = font.getFontSize() / 3;
+                final FontMetricsMapper mapper = (FontMetricsMapper)fontInfo.getMetricsFor(
+                        font.getFontName());
+                int maxAscent = mapper.getMaxAscent(font.getFontSize()) / 1000;
+                final int additionalBPD = maxAscent - baseline;
                 
                 Graphics2DAdapter g2a = getGraphics2DAdapter();
                 final Rectangle paintRect = new Rectangle(
-                        rx, currentBPPosition + text.getOffset(),
-                        text.getIPD() + extraWidth, text.getBPD());
+                        rx, currentBPPosition + text.getOffset() - additionalBPD,
+                        text.getIPD() + extraWidth, text.getBPD() + additionalBPD);
                 RendererContext rc = createRendererContext(paintRect.x, paintRect.y, 
                         paintRect.width, paintRect.height, null);
                 Map atts = new java.util.HashMap();
@@ -592,10 +624,8 @@
                 Graphics2DImagePainter painter = new Graphics2DImagePainter() {
 
                     public void paint(Graphics2D g2d, Rectangle2D area) {
-                        FontMetricsMapper mapper = (FontMetricsMapper)fontInfo.getMetricsFor(
-                                font.getFontName());
                         g2d.setFont(mapper.getFont(font.getFontSize()));
-                        g2d.translate(0, baseline);
+                        g2d.translate(0, baseline + additionalBPD);
                         g2d.scale(1000, 1000);
                         g2d.setColor(col);
                         Java2DRenderer.renderText(text, g2d, font);



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org


Re: svn commit: r410672 - in /xmlgraphics/fop/trunk/src: documentation/content/xdocs/trunk/ java/org/apache/fop/fonts/ java/org/apache/fop/render/java2d/ sandbox/org/apache/fop/render/pcl/

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
I'd like to add some comments here:
I'm a bit disappointed by PCL. While it is quite feature-rich, it has
some important gaps. Examples are restrictions for handling transparent
images at least in PCL5 (PCL5c seems to be a little better but is also
quite a bit more complicated). Another example is the lack of
sophisticated clipping (non-rect clipping areas) in HP GL/2. It's
practically impossible to implement a full native Graphics2D
implementation for HP GL/2. PCL contains support for TrueType fonts or
downloading bitmap fonts, but implementing these is quite some work and
not even the Windows PCL printer drivers seem to really do this. All
examples I've generated so far made extensive use of bitmap painting
much like what I resorted to in many areas now. And that despite setting
on the printer driver which would suggest otherwise. At some point you
really only paint bitmaps so you could just as well use the
BitmapRenderer and wrap the bitmap in a PCL wrapper to handle painting
on a desktop printer including tray selection. It would most likely be
faster than the mix of bitmaps and native ops. Ok, it depends on the
document. It is certainly possible to invest more time in a more
sophisticated implementation (I've taken a few shortcuts along the way),
but I doubt they are worth the effort.

On 31.05.2006 23:17:19 jeremias wrote:
> Author: jeremias
> Date: Wed May 31 14:17:18 2006
> New Revision: 410672
> 
> URL: http://svn.apache.org/viewvc?rev=410672&view=rev
> Log:
> Improved accuracy of font size selection. The font size is not rounded down to the next integer point value anymore. (Java2D renderers profit from that one, too)
> 
> PCL Renderer:
> Found a use case for that Java2D ascent value (which I call MaxAscent). It is used for painting text as bitmaps and to make sure the image the text is painted on is big enough if the font ascends beyond the em box. For non-Java2D fonts, MaxAscent is the same as ascent.
> Added a check that lets text painting fall back to bitmaps if there are characters that are not in the ISO-8859-1 encoding. This also enables Symbol and ZapfDingbats fonts.
> A "text-rendering" setting allows to disable PCL text painting in case the mix of PCL and bitmap text painting should result in unwelcome output. Note: the bitmap rendering is relatively slow (many small bitmaps). In the end we might end up rendering using a BitmapRenderer and only wrapping the whole thing in PCL (much like the Windows PCL drivers do). This would be faster than creating many small bitmaps.

<snip/>


Jeremias Maerki