2 * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
\r
4 * The program is provided "as is" without any warranty express or
\r
5 * implied, including the warranty of non-infringement and the implied
\r
6 * warranties of merchantibility and fitness for a particular purpose.
\r
7 * IBM will not be liable for any damages suffered by you as a result
\r
8 * of using the Program. In no event will IBM be liable for any
\r
9 * special, indirect or consequential damages or lost profits even if
\r
10 * IBM has been advised of the possibility of their occurrence. IBM
\r
11 * will not be liable for any third party claims against you.
\r
14 package com.ibm.richtext.textformat;
\r
16 import com.ibm.richtext.textlayout.attributes.AttributeMap;
\r
17 import com.ibm.richtext.textlayout.attributes.TextAttribute;
\r
19 import com.ibm.richtext.textlayout.FontUtils;
\r
21 import java.util.Hashtable;
\r
22 import java.awt.Font;
\r
24 final class FontResolver {
\r
26 static final String COPYRIGHT =
\r
27 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
30 // Even though it violates the Prime Directive I'll conditionalize
\r
31 // this anyway, since it is just a 1.2 workaround which I greatly
\r
34 java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
\r
38 private Hashtable styleMap;
\r
39 private final AttributeMap fDefaultFontMap;
\r
41 public FontResolver(AttributeMap defaults) {
\r
43 styleMap = new Hashtable();
\r
44 Hashtable tempMap = new Hashtable();
\r
45 tempMap.put(TextAttribute.FAMILY, defaults.get(TextAttribute.FAMILY));
\r
46 tempMap.put(TextAttribute.WEIGHT, defaults.get(TextAttribute.WEIGHT));
\r
47 tempMap.put(TextAttribute.POSTURE, defaults.get(TextAttribute.POSTURE));
\r
48 tempMap.put(TextAttribute.SIZE, defaults.get(TextAttribute.SIZE));
\r
49 fDefaultFontMap = new AttributeMap(tempMap);
\r
53 * Fetch result of resolve(style) from cache, if present.
\r
55 public AttributeMap applyFont(AttributeMap style) {
\r
57 Object cachedMap = styleMap.get(style);
\r
59 if (cachedMap == null) {
\r
60 AttributeMap resolvedMap = resolve(style);
\r
61 styleMap.put(style, resolvedMap);
\r
65 return (AttributeMap) cachedMap;
\r
70 * Return an AttributeMap containing a Font computed from the
\r
71 * attributes in <tt>style</tt>.
\r
73 public AttributeMap resolve(AttributeMap style) {
\r
75 if (style.get(TextAttribute.FONT) != null) {
\r
79 Font font = FontUtils.getFont(fDefaultFontMap.addAttributes(style));
\r
81 return style.addAttribute(TextAttribute.FONT, font);
\r