1 29 30 package nextapp.echo2.app.componentxml.propertypeer; 31 32 import java.util.Collections ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 import org.w3c.dom.Element ; 37 38 import nextapp.echo2.app.Extent; 39 import nextapp.echo2.app.Font; 40 import nextapp.echo2.app.componentxml.InvalidPropertyException; 41 import nextapp.echo2.app.componentxml.PropertyXmlPeer; 42 import nextapp.echo2.app.util.DomUtil; 43 44 48 public class FontPeer 49 implements PropertyXmlPeer { 50 51 private static final Map TYPEFACE_TEXT_TO_CONSTANT; 52 static { 53 Map constantMap = new HashMap (); 54 constantMap.put("Helvetica", Font.HELVETICA); 55 constantMap.put("Arial", Font.ARIAL); 56 constantMap.put("Verdana", Font.VERDANA); 57 constantMap.put("Times", Font.TIMES); 58 constantMap.put("Times Roman", Font.TIMES_ROMAN); 59 constantMap.put("Times New Roman", Font.TIMES_NEW_ROMAN); 60 constantMap.put("Courier", Font.COURIER); 61 constantMap.put("Courier New", Font.COURIER_NEW); 62 TYPEFACE_TEXT_TO_CONSTANT = Collections.unmodifiableMap(constantMap); 63 } 64 65 private Font.Typeface getSimpleTypeface(String name) { 66 if (TYPEFACE_TEXT_TO_CONSTANT.containsKey(name)) { 67 return (Font.Typeface) TYPEFACE_TEXT_TO_CONSTANT.get(name); 68 } else { 69 return new Font.Typeface(name); 70 } 71 } 72 73 77 public Object getValue(ClassLoader classLoader, Class objectClass, Element propertyElement) 78 throws InvalidPropertyException { 79 Element fontElement = DomUtil.getChildElementByTagName(propertyElement, "font"); 80 Extent size = null; 81 if (fontElement.hasAttribute("size")) { 82 String sizeString = fontElement.getAttribute("size"); 83 size = ExtentPeer.toExtent(sizeString); 84 } 85 int style = 0; 86 if ("true".equals(fontElement.getAttribute("bold"))) { 87 style |= Font.BOLD; 88 } 89 if ("true".equals(fontElement.getAttribute("italic"))) { 90 style |= Font.ITALIC; 91 } 92 if ("true".equals(fontElement.getAttribute("underline"))) { 93 style |= Font.UNDERLINE; 94 } 95 if ("true".equals(fontElement.getAttribute("overline"))) { 96 style |= Font.OVERLINE; 97 } 98 if ("true".equals(fontElement.getAttribute("line-through"))) { 99 style |= Font.LINE_THROUGH; 100 } 101 Element [] typefaces = DomUtil.getChildElementsByTagName(fontElement, "typeface"); 102 Font.Typeface typeface; 103 if (typefaces.length == 0) { 104 if (fontElement.hasAttribute("typeface")) { 105 typeface = getSimpleTypeface(fontElement.getAttribute("typeface")); 106 } else { 107 typeface = null; 108 } 109 } else if (typefaces.length == 1) { 110 typeface = getSimpleTypeface(typefaces[0].getAttribute("name")); 111 } else { 112 typeface = new Font.Typeface(typefaces[typefaces.length - 1].getAttribute("name")); 113 for (int i = typefaces.length - 2; i >= 0; --i) { 114 typeface = new Font.Typeface(typefaces[i].getAttribute("name"), typeface); 115 } 116 } 117 return new Font(typeface, style, size); 118 } 119 } 120 | Popular Tags |