1 19 20 25 26 package org.netbeans.modules.css.visual.model; 27 28 import java.awt.Font ; 29 import java.awt.GraphicsEnvironment ; 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.StringTokenizer ; 35 import javax.swing.DefaultComboBoxModel ; 36 import javax.swing.DefaultListModel ; 37 38 43 public class FontModel{ 44 45 Map fontFamilyNames = new HashMap (); 46 List fontFaceNames = new ArrayList (); 47 48 public FontModel(){ 49 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 50 Font [] fonts = ge.getAllFonts(); 51 52 for (int i=0; i<fonts.length; i++) { 54 String familyName = fonts[i].getFamily(); 56 String faceName = fonts[i].getName(); 57 58 List list = (List )fontFamilyNames.get(familyName); 60 if (list == null) { 61 list = new ArrayList (); 62 fontFamilyNames.put(familyName, list); 63 64 } 65 list.add(faceName); 66 fontFaceNames.add(faceName); 67 } 68 } 69 70 71 public DefaultListModel getFontFamilySetList(){ 72 return new FontFamilySetList(); 73 } 74 75 public DefaultListModel getFontList(){ 76 return new FontList(); 77 } 78 79 public DefaultListModel getWebFontList(){ 80 return new WebFontList(); 81 } 82 83 public DefaultListModel getFontFamilyList(){ 84 return new FontFamilyList(); 85 } 86 87 public DefaultListModel getFontSizeList(){ 88 return new FontSizeList(); 89 } 90 91 public DefaultComboBoxModel getFontSizeUnitList(){ 92 return new FontSizeUnitList(); 93 } 94 95 public DefaultComboBoxModel getFontStyleList(){ 96 return new FontStyleList(); 97 } 98 99 public DefaultComboBoxModel getFontSelectionList(){ 100 return new FontSelectionList(); 101 } 102 103 public DefaultComboBoxModel getFontWeightList(){ 104 return new FontWeightList(); 105 } 106 107 public DefaultComboBoxModel getFontVariantList(){ 108 return new FontVariantList(); 109 } 110 111 public FontSize getFontSize(String fontSizeStr){ 112 return new FontSize(fontSizeStr); 113 } 114 115 116 public class FontSize{ 117 FontSizeUnitList unitList = new FontSizeUnitList(); 118 String fontSizeUnit = null; 119 String fontSize = null; 120 public FontSize(String fontSizeStr){ 121 fontSize = fontSizeStr.trim(); 122 for(int i=0; i< unitList.getSize(); i++){ 123 String unit = (String )unitList.getElementAt(i); 124 if(fontSize.endsWith(unit)){ 125 fontSizeUnit = unit; 126 fontSize = fontSize.replaceAll(unit,""); 127 } 128 } 129 } 130 131 public String getUnit(){ 132 return fontSizeUnit; 133 } 134 135 public String getValue(){ 136 if(Utils.isInteger(fontSize)){ 137 return fontSize; 138 }else{ 139 return null; 140 } 141 } 142 } 143 144 145 public Font resolveFont(CssStyleData styleData, Font baseFont) { 146 String fontFamily = styleData.getProperty(CssProperties.FONT_FAMILY); 147 String fontSize = styleData.getProperty(CssProperties.FONT_SIZE); 148 String fontStyle = styleData.getProperty(CssProperties.FONT_STYLE); 149 String fontVariant = styleData.getProperty(CssProperties.FONT_VARIANT); 150 String fontWeight = styleData.getProperty(CssProperties.FONT_WEIGHT); 151 String name = resolveFontName(fontFamily, baseFont.getName()); 152 int style = resolveFontStyle(fontStyle,fontWeight, baseFont.getStyle()); 153 int size = resolveFontSize(fontSize, baseFont.getSize()); 154 return new Font (name, style, size); 155 } 156 157 private String resolveFontName(String fontFamily, String defName){ 158 String fontName = defName; 159 if(fontFamily != null){ 160 StringTokenizer st = new StringTokenizer (fontFamily.trim(),","); 161 while(st.hasMoreTokens()){ 162 String cssFamilyName = st.nextToken(); 163 cssFamilyName = cssFamilyName.replaceAll("'",""); 164 if (fontFamilyNames.containsKey(cssFamilyName)){ 166 List fontFaceList = (List ) fontFamilyNames.get(cssFamilyName); 167 fontName = (String ) fontFaceList.get(0); 168 }else if (fontFaceNames.contains(cssFamilyName)){ 169 fontName = cssFamilyName; 170 } 171 } 172 } 173 return fontName; 174 } 175 176 private int resolveFontStyle(String fontStyle, String fontWeight, int defStyle){ 177 int style = defStyle; 178 if((fontStyle != null) && (fontStyle.equals("italic") || fontStyle.equals("oblique"))){ 179 style = Font.ITALIC; 181 } 182 if((fontWeight != null) && fontWeight.equals(fontWeight)){ 183 style = style | Font.BOLD; 184 } 185 return style; 186 } 187 188 private int resolveFontSize(String fontSizeStr, int defSize){ 189 int size = defSize; 191 if(fontSizeStr != null){ 192 FontSize fontSize = new FontSize(fontSizeStr); 193 String fontSizeValue = fontSize.getValue(); 194 if(Utils.isInteger(fontSizeValue)){ 195 size = Utils.getInteger(fontSizeValue); 196 }else{ 197 if (fontSizeValue.equals("XX-small")) size = 4; if (fontSizeValue.equals("X-small")) size = 6; if (fontSizeValue.equals("small")) size = 8; if (fontSizeValue.equals("medium")) size = 12; if (fontSizeValue.equals("large")) size = 14; if (fontSizeValue.equals("X-large")) size = 16; if (fontSizeValue.equals("XX-large")) size = 20; } 205 206 if (size < 4) { 207 size = 4; 208 }else if( size > 72){ 209 size = 72; 210 } 211 } 212 return size; 213 } 214 215 216 public static class FontSelectionList extends DefaultComboBoxModel { 217 public FontSelectionList(){ 218 addElement(org.openide.util.NbBundle.getMessage(FontModel.class, "FONTS")); addElement(org.openide.util.NbBundle.getMessage(FontModel.class, "FONT_FAMILIES")); addElement(org.openide.util.NbBundle.getMessage(FontModel.class, "WEB_FONTS")); } 222 } 223 224 public static class FontList extends DefaultListModel { 225 public FontList(){ 226 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 227 String fontNames[] = ge.getAvailableFontFamilyNames(); 228 229 for (int i=0; i<fontNames.length; i++) { 231 addElement(fontNames[i]); 232 } 233 } 234 } 235 236 public static class WebFontList extends DefaultListModel { 237 public WebFontList(){ 238 addElement(CssStyleData.NOT_SET); 239 String [] webFontValues = CssProperties.getWebFontValues(); 240 for (int i=0; i<webFontValues.length; i++) { 241 addElement(webFontValues[i]); 242 } 243 } 244 } 245 246 public static class FontFamilyList extends DefaultListModel { 247 public FontFamilyList(){ 248 addElement(CssStyleData.NOT_SET); 249 String [] FontfamilyValues = CssProperties.getFontFamilyValues(); 250 for (int i=0; i<FontfamilyValues.length; i++) { 251 addElement(FontfamilyValues[i]); 252 } 253 } 254 } 255 256 public static class FontFamilySetList extends DefaultListModel { 257 public FontFamilySetList(){ 258 addElement(CssStyleData.NOT_SET); 259 String [] FontfamilySetValues = CssProperties.getFontFamilySetValues(); 260 for (int i=0; i<FontfamilySetValues.length; i++) { 261 addElement(FontfamilySetValues[i]); 262 } 263 } 264 } 265 266 public static class FontSizeList extends DefaultListModel { 267 public FontSizeList(){ 268 addElement(CssStyleData.NOT_SET); 269 String [] FontSizeValues = CssProperties.getFontSizeValues(); 270 for (int i=0; i<FontSizeValues.length; i++) { 271 addElement(FontSizeValues[i]); 272 } 273 } 274 } 275 276 public static class FontSizeUnitList extends DefaultComboBoxModel { 277 public FontSizeUnitList(){ 278 String [] unitValues = CssProperties.getCssLengthUnits(); 279 for(int i=0; i< unitValues.length; i++){ 280 addElement(unitValues[i]); 281 } 282 } 283 } 284 285 public static class FontStyleList extends DefaultComboBoxModel { 286 public FontStyleList(){ 287 String [] propValues = CssProperties.getCssPropertyValues(CssProperties.FONT_STYLE); 288 addElement(CssStyleData.NOT_SET); 289 for(int i=0; i< propValues.length; i++){ 290 addElement(propValues[i]); 291 } 292 } 293 } 294 295 public static class FontWeightList extends DefaultComboBoxModel { 296 public FontWeightList(){ 297 String [] propValues = CssProperties.getCssPropertyValues(CssProperties.FONT_WEIGHT); 298 addElement(CssStyleData.NOT_SET); 299 for(int i=0; i< propValues.length; i++){ 300 addElement(propValues[i]); 301 } 302 setSelectedItem("px"); 303 } 304 } 305 306 public static class FontVariantList extends DefaultComboBoxModel { 307 public FontVariantList(){ 308 String [] propValues = CssProperties.getCssPropertyValues(CssProperties.FONT_VARIANT); 309 addElement(CssStyleData.NOT_SET); 310 for(int i=0; i< propValues.length; i++){ 311 addElement(propValues[i]); 312 } 313 } 314 } 315 } 316 | Popular Tags |