mirror of https://github.com/apache/poi.git
Further enhancements to Font Metrics support, wrt fonts with bold/italic varients, such as dialog (plus tests)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@564263 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
323820ffde
commit
9384c339df
|
@ -82,6 +82,16 @@ public class FontDetails
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static String buildFontHeightProperty(String fontName) {
|
||||||
|
return "font." + fontName + ".height";
|
||||||
|
}
|
||||||
|
protected static String buildFontWidthsProperty(String fontName) {
|
||||||
|
return "font." + fontName + ".widths";
|
||||||
|
}
|
||||||
|
protected static String buildFontCharactersProperty(String fontName) {
|
||||||
|
return "font." + fontName + ".characters";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an instance of <code>FontDetails</code> by loading them from the
|
* Create an instance of <code>FontDetails</code> by loading them from the
|
||||||
* provided property object.
|
* provided property object.
|
||||||
|
@ -92,9 +102,9 @@ public class FontDetails
|
||||||
*/
|
*/
|
||||||
public static FontDetails create( String fontName, Properties fontMetricsProps )
|
public static FontDetails create( String fontName, Properties fontMetricsProps )
|
||||||
{
|
{
|
||||||
String heightStr = fontMetricsProps.getProperty( "font." + fontName + ".height");
|
String heightStr = fontMetricsProps.getProperty( buildFontHeightProperty(fontName) );
|
||||||
String widthsStr = fontMetricsProps.getProperty( "font." + fontName + ".widths");
|
String widthsStr = fontMetricsProps.getProperty( buildFontWidthsProperty(fontName) );
|
||||||
String charactersStr = fontMetricsProps.getProperty( "font." + fontName + ".characters");
|
String charactersStr = fontMetricsProps.getProperty( buildFontCharactersProperty(fontName) );
|
||||||
|
|
||||||
// Ensure that this is a font we know about
|
// Ensure that this is a font we know about
|
||||||
if(heightStr == null || widthsStr == null || charactersStr == null) {
|
if(heightStr == null || widthsStr == null || charactersStr == null) {
|
||||||
|
|
|
@ -32,7 +32,9 @@ import java.io.*;
|
||||||
*/
|
*/
|
||||||
class StaticFontMetrics
|
class StaticFontMetrics
|
||||||
{
|
{
|
||||||
|
/** The font metrics property file we're using */
|
||||||
private static Properties fontMetricsProps;
|
private static Properties fontMetricsProps;
|
||||||
|
/** Our cache of font details we've already looked up */
|
||||||
private static Map fontDetailsMap = new HashMap();
|
private static Map fontDetailsMap = new HashMap();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,6 +44,8 @@ class StaticFontMetrics
|
||||||
*/
|
*/
|
||||||
public static FontDetails getFontDetails(Font font)
|
public static FontDetails getFontDetails(Font font)
|
||||||
{
|
{
|
||||||
|
// If we haven't already identified out font metrics file,
|
||||||
|
// figure out which one to use and load it
|
||||||
if (fontMetricsProps == null)
|
if (fontMetricsProps == null)
|
||||||
{
|
{
|
||||||
InputStream metricsIn = null;
|
InputStream metricsIn = null;
|
||||||
|
@ -81,16 +85,31 @@ class StaticFontMetrics
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Grab the base name of the font they've asked about
|
||||||
String fontName = font.getName();
|
String fontName = font.getName();
|
||||||
|
|
||||||
if (fontDetailsMap.get(fontName) == null)
|
// Some fonts support plain/bold/italic/bolditalic variants
|
||||||
{
|
// Others have different font instances for bold etc
|
||||||
|
// (eg font.dialog.plain.* vs font.Californian FB Bold.*)
|
||||||
|
String fontStyle = "";
|
||||||
|
if(font.isPlain()) fontStyle += "plain";
|
||||||
|
if(font.isBold()) fontStyle += "bold";
|
||||||
|
if(font.isItalic()) fontStyle += "italic";
|
||||||
|
|
||||||
|
// Do we have a definition for this font with just the name?
|
||||||
|
// If not, check with the font style added
|
||||||
|
if(fontMetricsProps.get(FontDetails.buildFontHeightProperty(fontName)) == null &&
|
||||||
|
fontMetricsProps.get(FontDetails.buildFontHeightProperty(fontName+"."+fontStyle)) != null) {
|
||||||
|
// Need to add on the style to the font name
|
||||||
|
fontName += "." + fontStyle;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the details on this font
|
||||||
|
if (fontDetailsMap.get(fontName) == null) {
|
||||||
FontDetails fontDetails = FontDetails.create(fontName, fontMetricsProps);
|
FontDetails fontDetails = FontDetails.create(fontName, fontMetricsProps);
|
||||||
fontDetailsMap.put( fontName, fontDetails );
|
fontDetailsMap.put( fontName, fontDetails );
|
||||||
return fontDetails;
|
return fontDetails;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return (FontDetails) fontDetailsMap.get(fontName);
|
return (FontDetails) fontDetailsMap.get(fontName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,6 +56,15 @@ public class TestEscherGraphics2d extends TestCase
|
||||||
graphics.setFont(font);
|
graphics.setFont(font);
|
||||||
graphics.drawString("This is another test", 10, 10);
|
graphics.drawString("This is another test", 10, 10);
|
||||||
|
|
||||||
|
// And test with ones that need the style appending
|
||||||
|
font = new Font("dialog", Font.PLAIN, 12);
|
||||||
|
graphics.setFont(font);
|
||||||
|
graphics.drawString("This is another test", 10, 10);
|
||||||
|
|
||||||
|
font = new Font("dialog", Font.BOLD, 12);
|
||||||
|
graphics.setFont(font);
|
||||||
|
graphics.drawString("This is another test", 10, 10);
|
||||||
|
|
||||||
// But with an invalid font, we get an exception
|
// But with an invalid font, we get an exception
|
||||||
font = new Font("IamAmadeUPfont", Font.PLAIN, 22);
|
font = new Font("IamAmadeUPfont", Font.PLAIN, 22);
|
||||||
graphics.setFont(font);
|
graphics.setFont(font);
|
||||||
|
|
Loading…
Reference in New Issue