diff options
author | Jeremias Maerki <jeremias@apache.org> | 2006-05-31 21:17:18 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2006-05-31 21:17:18 +0000 |
commit | e189115a925441c612694e3820bdc24bede5aa0d (patch) | |
tree | 8fd2c3feb4032c948eac1958f914cb89a61e9440 /src/java/org/apache/fop | |
parent | d067157a185e8e1ddc77cca58cc37d251bf1effc (diff) | |
download | xmlgraphics-fop-e189115a925441c612694e3820bdc24bede5aa0d.tar.gz xmlgraphics-fop-e189115a925441c612694e3820bdc24bede5aa0d.zip |
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.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@410672 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop')
5 files changed, 66 insertions, 8 deletions
diff --git a/src/java/org/apache/fop/fonts/FontMetrics.java b/src/java/org/apache/fop/fonts/FontMetrics.java index d7ec7975a..3ca228125 100644 --- a/src/java/org/apache/fop/fonts/FontMetrics.java +++ b/src/java/org/apache/fop/fonts/FontMetrics.java @@ -41,8 +41,16 @@ public interface FontMetrics { /** + * 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 */ diff --git a/src/java/org/apache/fop/fonts/LazyFont.java b/src/java/org/apache/fop/fonts/LazyFont.java index c08216ab0..19bb4ffde 100644 --- a/src/java/org/apache/fop/fonts/LazyFont.java +++ b/src/java/org/apache/fop/fonts/LazyFont.java @@ -177,6 +177,14 @@ public class LazyFont extends Typeface implements FontDescriptor { } /** + * @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) { diff --git a/src/java/org/apache/fop/fonts/Typeface.java b/src/java/org/apache/fop/fonts/Typeface.java index f39a80d40..34ed96215 100644 --- a/src/java/org/apache/fop/fonts/Typeface.java +++ b/src/java/org/apache/fop/fonts/Typeface.java @@ -54,5 +54,10 @@ public abstract class Typeface implements FontMetrics { return false; } + /** @see org.apache.fop.fonts.FontMetrics#getMaxAscent(int) */ + public int getMaxAscent(int size) { + return getAscender(size); + } + } diff --git a/src/java/org/apache/fop/render/java2d/FontMetricsMapper.java b/src/java/org/apache/fop/render/java2d/FontMetricsMapper.java index e49ee8967..6890300e9 100644 --- a/src/java/org/apache/fop/render/java2d/FontMetricsMapper.java +++ b/src/java/org/apache/fop/render/java2d/FontMetricsMapper.java @@ -86,6 +86,13 @@ public class FontMetricsMapper extends Typeface implements FontMetrics { } /** + * @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) { diff --git a/src/java/org/apache/fop/render/java2d/Java2DFontMetrics.java b/src/java/org/apache/fop/render/java2d/Java2DFontMetrics.java index e459781f9..0ddc44bbf 100644 --- a/src/java/org/apache/fop/render/java2d/Java2DFontMetrics.java +++ b/src/java/org/apache/fop/render/java2d/Java2DFontMetrics.java @@ -23,7 +23,10 @@ import java.awt.Font; 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 @@ public class Java2DFontMetrics { */ 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 @@ public class Java2DFontMetrics { } /** + * 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 @@ public class Java2DFontMetrics { 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 @@ public class Java2DFontMetrics { */ 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 @@ public class Java2DFontMetrics { 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; |