From 03f738225f8acd578788e809659a3a15bac2a10a Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 9 Aug 2007 14:27:06 +0000 Subject: [PATCH] If the Escher layer is asked to draw text with an invalid font, throw a much more useful error. Plus test git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@564219 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hssf/usermodel/FontDetails.java | 8 ++++++++ .../poi/hssf/usermodel/TestEscherGraphics2d.java | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/java/org/apache/poi/hssf/usermodel/FontDetails.java b/src/java/org/apache/poi/hssf/usermodel/FontDetails.java index 2faaef5ab..6a8172821 100644 --- a/src/java/org/apache/poi/hssf/usermodel/FontDetails.java +++ b/src/java/org/apache/poi/hssf/usermodel/FontDetails.java @@ -95,6 +95,14 @@ public class FontDetails String heightStr = fontMetricsProps.getProperty( "font." + fontName + ".height"); String widthsStr = fontMetricsProps.getProperty( "font." + fontName + ".widths"); String charactersStr = fontMetricsProps.getProperty( "font." + fontName + ".characters"); + + // Ensure that this is a font we know about + if(heightStr == null || widthsStr == null || charactersStr == null) { + // We don't know all we need to about this font + // Since we don't know its sizes, we can't work with it + throw new IllegalArgumentException("The supplied FontMetrics doesn't know about the font '" + fontName + "', so we can't use it. Please add it to your font metrics file (see StaticFontMetrics.getFontDetails"); + } + int height = Integer.parseInt(heightStr); FontDetails d = new FontDetails(fontName, height); String[] charactersStrArray = split(charactersStr, ",", -1); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestEscherGraphics2d.java b/src/testcases/org/apache/poi/hssf/usermodel/TestEscherGraphics2d.java index 1ed9f8fab..ddbe4b0a8 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestEscherGraphics2d.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestEscherGraphics2d.java @@ -50,6 +50,20 @@ public class TestEscherGraphics2d extends TestCase graphics.drawString("This is a test", 10, 10); HSSFTextbox t = (HSSFTextbox) escherGroup.getChildren().get(0); assertEquals("This is a test", t.getString().getString().toString()); + + // Check that with a valid font, it's still ok + Font font = new Font("Forte", Font.PLAIN, 12); + graphics.setFont(font); + graphics.drawString("This is another test", 10, 10); + + // But with an invalid font, we get an exception + font = new Font("IamAmadeUPfont", Font.PLAIN, 22); + graphics.setFont(font); + try { + graphics.drawString("This is another test", 10, 10); + fail(); + } catch(IllegalArgumentException e) { + } } public void testFillRect() throws Exception