1 16 17 package org.apache.poi.hssf.usermodel; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 import java.util.StringTokenizer ; 23 24 29 public class FontDetails 30 { 31 private String fontName; 32 private int height; 33 private Map charWidths = new HashMap (); 34 35 41 public FontDetails( String fontName, int height ) 42 { 43 this.fontName = fontName; 44 this.height = height; 45 } 46 47 public String getFontName() 48 { 49 return fontName; 50 } 51 52 public int getHeight() 53 { 54 return height; 55 } 56 57 public void addChar( char c, int width ) 58 { 59 charWidths.put(new Character (c), new Integer (width)); 60 } 61 62 67 public int getCharWidth( char c ) 68 { 69 Integer widthInteger = (Integer )(charWidths.get(new Character (c))); 70 if (widthInteger == null && c != 'W') 71 return getCharWidth('W'); 72 else 73 return widthInteger.intValue(); 74 } 75 76 public void addChars( char[] characters, int[] widths ) 77 { 78 for ( int i = 0; i < characters.length; i++ ) 79 { 80 charWidths.put( new Character (characters[i]), new Integer (widths[i])); 81 } 82 } 83 84 92 public static FontDetails create( String fontName, Properties fontMetricsProps ) 93 { 94 String heightStr = fontMetricsProps.getProperty( "font." + fontName + ".height"); 95 String widthsStr = fontMetricsProps.getProperty( "font." + fontName + ".widths"); 96 String charactersStr = fontMetricsProps.getProperty( "font." + fontName + ".characters"); 97 int height = Integer.parseInt(heightStr); 98 FontDetails d = new FontDetails(fontName, height); 99 String [] charactersStrArray = split(charactersStr, ",", -1); 100 String [] widthsStrArray = split(widthsStr, ",", -1); 101 if (charactersStrArray.length != widthsStrArray.length) 102 throw new RuntimeException ("Number of characters does not number of widths for font " + fontName); 103 for ( int i = 0; i < widthsStrArray.length; i++ ) 104 { 105 if (charactersStrArray[i].length() != 0) 106 d.addChar(charactersStrArray[i].charAt(0), Integer.parseInt(widthsStrArray[i])); 107 } 108 return d; 109 } 110 111 117 public int getStringWidth(String str) 118 { 119 int width = 0; 120 for (int i = 0; i < str.length(); i++) 121 { 122 width += getCharWidth(str.charAt(i)); 123 } 124 return width; 125 } 126 127 131 private static String [] split(String text, String separator, int max) 132 { 133 StringTokenizer tok = new StringTokenizer (text, separator); 134 int listSize = tok.countTokens(); 135 if(max != -1 && listSize > max) 136 listSize = max; 137 String list[] = new String [listSize]; 138 for(int i = 0; tok.hasMoreTokens(); i++) 139 { 140 if(max != -1 && i == listSize - 1) 141 { 142 StringBuffer buf = new StringBuffer ((text.length() * (listSize - i)) / listSize); 143 while(tok.hasMoreTokens()) 144 { 145 buf.append(tok.nextToken()); 146 if(tok.hasMoreTokens()) 147 buf.append(separator); 148 } 149 list[i] = buf.toString().trim(); 150 break; 151 } 152 list[i] = tok.nextToken().trim(); 153 } 154 155 return list; 156 } 157 158 159 } 160 | Popular Tags |