1 16 17 package org.apache.poi.hssf.usermodel; 18 19 import java.util.*; 20 import java.awt.*; 21 import java.io.*; 22 23 32 class StaticFontMetrics 33 { 34 private static Properties fontMetricsProps; 35 private static Map fontDetailsMap = new HashMap(); 36 37 42 public static FontDetails getFontDetails(Font font) 43 { 44 if (fontMetricsProps == null) 45 { 46 InputStream metricsIn = null; 47 try 48 { 49 fontMetricsProps = new Properties(); 50 if (System.getProperty("font.metrics.filename") != null) 51 { 52 String filename = System.getProperty("font.metrics.filename"); 53 File file = new File(filename); 54 if (!file.exists()) 55 throw new FileNotFoundException("font_metrics.properties not found at path " + file.getAbsolutePath()); 56 metricsIn = new FileInputStream(file); 57 } 58 else 59 { 60 metricsIn = FontDetails.class.getResourceAsStream("/font_metrics.properties"); 61 if (metricsIn == null) 62 throw new FileNotFoundException("font_metrics.properties not found in classpath"); 63 } 64 fontMetricsProps.load(metricsIn); 65 } 66 catch ( IOException e ) 67 { 68 throw new RuntimeException ("Could not load font metrics: " + e.getMessage()); 69 } 70 finally 71 { 72 if (metricsIn != null) 73 { 74 try 75 { 76 metricsIn.close(); 77 } 78 catch ( IOException ignore ) { } 79 } 80 } 81 } 82 83 String fontName = font.getName(); 84 85 if (fontDetailsMap.get(fontName) == null) 86 { 87 FontDetails fontDetails = FontDetails.create(fontName, fontMetricsProps); 88 fontDetailsMap.put( fontName, fontDetails ); 89 return fontDetails; 90 } 91 else 92 { 93 return (FontDetails) fontDetailsMap.get(fontName); 94 } 95 96 } 97 } 98 | Popular Tags |