/// <summary> /// Create an instance of /// <c>FontDetails</c> /// by loading them from the /// provided property object. /// </summary> /// <param name="fontName">the font name.</param> /// <param name="fontMetricsProps">the property object holding the details of this /// particular font.</param> /// <returns>a new FontDetails instance.</returns> public static FontDetails Create(String fontName, Properties fontMetricsProps) { String heightStr = fontMetricsProps[BuildFontHeightProperty(fontName)]; String widthsStr = fontMetricsProps[BuildFontWidthsProperty(fontName)]; String CharsStr = fontMetricsProps[BuildFontCharsProperty(fontName)]; // Ensure that this Is a font we know about if (heightStr == null || widthsStr == null || CharsStr == 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 ArgumentException("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 = int.Parse(heightStr, CultureInfo.InvariantCulture); FontDetails d = new FontDetails(fontName, height); String[] CharsStrArray = Split(CharsStr, ",", -1); String[] widthsStrArray = Split(widthsStr, ",", -1); if (CharsStrArray.Length != widthsStrArray.Length) throw new Exception("Number of Chars does not number of widths for font " + fontName); for (int i = 0; i < widthsStrArray.Length; i++) { if (CharsStrArray[i].Trim().Length != 0) d.AddChar(CharsStrArray[i].Trim()[0], int.Parse(widthsStrArray[i], CultureInfo.InvariantCulture)); } return d; }
/** * Retrieves the fake font details for a given font. * @param font the font to lookup. * @return the fake 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) { Stream metricsIn = null; try { fontMetricsProps = new Properties(); // Check to see if the font metric file was specified // as a system property String propFileName = null; try { propFileName = ConfigurationManager.AppSettings["font.metrics.filename"]; } catch (Exception) { } if (propFileName != null) { if (!File.Exists(propFileName)) { throw new FileNotFoundException("font_metrics.properties not found at path " + Path.GetFullPath(propFileName)); } metricsIn = new MemoryStream(Resource1.font_metrics); } else { // Use the built-in font metrics file off the classpath metricsIn = new MemoryStream(Resource1.font_metrics); if (metricsIn == null) { throw new FileNotFoundException("font_metrics.properties not found in classpath"); } } fontMetricsProps.Load(metricsIn); } catch (IOException e) { throw new Exception("Could not Load font metrics: " + e.Message); } finally { if (metricsIn != null) { try { metricsIn.Close(); } catch (IOException) { } } } } // Grab the base name of the font they've asked about String fontName = font.FontFamily.Name; // 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.Bold) { fontStyle += "bold"; } if (font.Italic) { fontStyle += "italic"; } // Do we have a definition for this font with just the name? // If not, Check with the font style Added if (fontMetricsProps[FontDetails.BuildFontHeightProperty(fontName)] == null && fontMetricsProps[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[fontName] == null) { FontDetails fontDetails = FontDetails.Create(fontName, fontMetricsProps); fontDetailsMap[fontName] = fontDetails; return(fontDetails); } else { return((FontDetails)fontDetailsMap[fontName]); } }