01: /*
02: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
03: *
04: * The program is provided "as is" without any warranty express or
05: * implied, including the warranty of non-infringement and the implied
06: * warranties of merchantibility and fitness for a particular purpose.
07: * IBM will not be liable for any damages suffered by you as a result
08: * of using the Program. In no event will IBM be liable for any
09: * special, indirect or consequential damages or lost profits even if
10: * IBM has been advised of the possibility of their occurrence. IBM
11: * will not be liable for any third party claims against you.
12: */
13: // Requires Java2
14: package com.ibm.richtext.textformat;
15:
16: import com.ibm.richtext.textlayout.attributes.AttributeMap;
17: import com.ibm.richtext.textlayout.attributes.TextAttribute;
18:
19: import com.ibm.richtext.textlayout.FontUtils;
20:
21: import java.util.Hashtable;
22: import java.awt.Font;
23:
24: final class FontResolver {
25:
26: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
27:
28: static {
29: // Even though it violates the Prime Directive I'll conditionalize
30: // this anyway, since it is just a 1.2 workaround which I greatly
31: // resent.
32: ///*JDK12IMPORTS
33: java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment()
34: .getAllFonts();
35: //JDK12IMPORTS*/
36: }
37:
38: private Hashtable styleMap;
39: private final AttributeMap fDefaultFontMap;
40:
41: public FontResolver(AttributeMap defaults) {
42:
43: styleMap = new Hashtable();
44: Hashtable tempMap = new Hashtable();
45: tempMap.put(TextAttribute.FAMILY, defaults
46: .get(TextAttribute.FAMILY));
47: tempMap.put(TextAttribute.WEIGHT, defaults
48: .get(TextAttribute.WEIGHT));
49: tempMap.put(TextAttribute.POSTURE, defaults
50: .get(TextAttribute.POSTURE));
51: tempMap.put(TextAttribute.SIZE, defaults
52: .get(TextAttribute.SIZE));
53: fDefaultFontMap = new AttributeMap(tempMap);
54: }
55:
56: /**
57: * Fetch result of resolve(style) from cache, if present.
58: */
59: public AttributeMap applyFont(AttributeMap style) {
60:
61: Object cachedMap = styleMap.get(style);
62:
63: if (cachedMap == null) {
64: AttributeMap resolvedMap = resolve(style);
65: styleMap.put(style, resolvedMap);
66: return resolvedMap;
67: } else {
68: return (AttributeMap) cachedMap;
69: }
70: }
71:
72: /**
73: * Return an AttributeMap containing a Font computed from the
74: * attributes in <tt>style</tt>.
75: */
76: public AttributeMap resolve(AttributeMap style) {
77:
78: if (style.get(TextAttribute.FONT) != null) {
79: return style;
80: }
81:
82: Font font = FontUtils.getFont(fDefaultFontMap
83: .addAttributes(style));
84:
85: return style.addAttribute(TextAttribute.FONT, font);
86: }
87: }
|