]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/textformat/FontResolver.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / textformat / FontResolver.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2004.  All Rights Reserved.\r
3  *\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
12  */\r
13 // Requires Java2\r
14 package com.ibm.richtext.textformat;\r
15 \r
16 import com.ibm.richtext.textlayout.attributes.AttributeMap;\r
17 import com.ibm.richtext.textlayout.attributes.TextAttribute;\r
18 \r
19 import com.ibm.richtext.textlayout.FontUtils;\r
20 \r
21 import java.util.Hashtable;\r
22 import java.awt.Font;\r
23 \r
24 final class FontResolver {\r
25 \r
26     static final String COPYRIGHT =\r
27                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
28 \r
29     static {\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
32 // resent.\r
33 ///*JDK12IMPORTS\r
34         java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();\r
35 //JDK12IMPORTS*/\r
36     }\r
37 \r
38     private Hashtable styleMap;\r
39     private final AttributeMap fDefaultFontMap;\r
40 \r
41     public FontResolver(AttributeMap defaults) {\r
42 \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
50     }\r
51 \r
52     /**\r
53      * Fetch result of resolve(style) from cache, if present.\r
54      */\r
55     public AttributeMap applyFont(AttributeMap style) {\r
56 \r
57         Object cachedMap = styleMap.get(style);\r
58 \r
59         if (cachedMap == null) {\r
60             AttributeMap resolvedMap = resolve(style);\r
61             styleMap.put(style, resolvedMap);\r
62             return resolvedMap;\r
63         }\r
64         else {\r
65             return (AttributeMap) cachedMap;\r
66         }\r
67     }\r
68 \r
69     /**\r
70      * Return an AttributeMap containing a Font computed from the\r
71      * attributes in <tt>style</tt>.\r
72      */\r
73     public AttributeMap resolve(AttributeMap style) {\r
74 \r
75         if (style.get(TextAttribute.FONT) != null) {\r
76             return style;\r
77         }\r
78 \r
79         Font font = FontUtils.getFont(fDefaultFontMap.addAttributes(style));\r
80 \r
81         return style.addAttribute(TextAttribute.FONT, font);\r
82     }\r
83 }\r