KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > properties > CommonFont


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: CommonFont.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.fo.properties;
21
22 // FOP
23
import java.util.List JavaDoc;
24
25 import org.apache.fop.datatypes.Length;
26 import org.apache.fop.datatypes.Numeric;
27 import org.apache.fop.datatypes.PercentBaseContext;
28 import org.apache.fop.fo.Constants;
29 import org.apache.fop.fo.PropertyList;
30 import org.apache.fop.fo.expr.PropertyException;
31 import org.apache.fop.fonts.Font;
32 import org.apache.fop.fonts.FontInfo;
33 import org.apache.fop.fonts.FontMetrics;
34 import org.apache.fop.fonts.FontTriplet;
35
36 /**
37  * Collection of properties used in
38  */

39 public class CommonFont {
40
41     /**
42      * The "font-family" property.
43      */

44     private String JavaDoc[] fontFamily;
45
46     /**
47      * The "font-selection-strategy" property.
48      */

49     public int fontSelectionStrategy;
50
51     /**
52      * The "font-size" property.
53      */

54     public Length fontSize;
55
56     /**
57      * The "font-stretch" property.
58      */

59     public int fontStretch;
60
61     /**
62      * The "font-size-adjust" property.
63      */

64     public Numeric fontSizeAdjust;
65
66     /**
67      * The "font-style" property.
68      */

69     public int fontStyle;
70
71     /**
72      * The "font-variant" property.
73      */

74     public int fontVariant;
75
76     /**
77      * The "font-weight" property.
78      */

79     public int fontWeight;
80
81     private Font fontState;
82
83     /**
84      * Create a CommonFont object.
85      * @param pList The PropertyList to get properties from.
86      */

87     public CommonFont(PropertyList pList) throws PropertyException {
88         List JavaDoc lst = pList.get(Constants.PR_FONT_FAMILY).getList();
89         fontFamily = new String JavaDoc[lst.size()];
90         for (int i = 0, c = lst.size(); i < c; i++) {
91             fontFamily[i] = ((Property)lst.get(i)).getString();
92         }
93         if (fontFamily.length == 0) {
94             //Shouldn't happen, but we never know.
95
fontFamily = new String JavaDoc[] {"any"};
96         }
97         fontSelectionStrategy = pList.get(Constants.PR_FONT_SELECTION_STRATEGY).getEnum();
98         fontSize = pList.get(Constants.PR_FONT_SIZE).getLength();
99         fontStretch = pList.get(Constants.PR_FONT_STRETCH).getEnum();
100         fontSizeAdjust = pList.get(Constants.PR_FONT_SIZE_ADJUST).getNumeric();
101         fontStyle = pList.get(Constants.PR_FONT_STYLE).getEnum();
102         fontVariant = pList.get(Constants.PR_FONT_VARIANT).getEnum();
103         fontWeight = pList.get(Constants.PR_FONT_WEIGHT).getEnum();
104     }
105
106     /** @return the first font-family name in the list */
107     public String JavaDoc getFirstFontFamily() {
108         return this.fontFamily[0];
109     }
110     
111     /** @return the font-family names */
112     public String JavaDoc[] getFontFamily() {
113         return this.fontFamily;
114     }
115     
116     /**
117      * Overrides the font-family.
118      * @param value the new font-family
119      */

120     public void overrideFontFamily(String JavaDoc value) {
121         this.fontFamily = new String JavaDoc[] {value};
122         
123     }
124     
125     /**
126      * Create and return a Font object based on the properties.
127      *
128      * @param fontInfo
129      * @return a Font object.
130      */

131     public Font getFontState(FontInfo fontInfo, PercentBaseContext context) {
132         if (fontState == null) {
133             /**@todo this is ugly. need to improve. */
134
135             int font_weight = 400;
136             if (fontWeight == Constants.EN_BOLDER) {
137                 // +100 from inherited
138
} else if (fontWeight == Constants.EN_LIGHTER) {
139                 // -100 from inherited
140
} else {
141                 switch (fontWeight) {
142                 case Constants.EN_100: font_weight = 100; break;
143                 case Constants.EN_200: font_weight = 200; break;
144                 case Constants.EN_300: font_weight = 300; break;
145                 case Constants.EN_400: font_weight = 400; break;
146                 case Constants.EN_500: font_weight = 500; break;
147                 case Constants.EN_600: font_weight = 600; break;
148                 case Constants.EN_700: font_weight = 700; break;
149                 case Constants.EN_800: font_weight = 800; break;
150                 case Constants.EN_900: font_weight = 900; break;
151                 }
152             }
153
154             String JavaDoc style;
155             switch (fontStyle) {
156             case Constants.EN_ITALIC:
157                 style = "italic";
158                 break;
159             case Constants.EN_OBLIQUE:
160                 style = "oblique";
161                 break;
162             case Constants.EN_BACKSLANT:
163                 style = "backslant";
164                 break;
165             default:
166                 style = "normal";
167             }
168             // NOTE: this is incomplete. font-size may be specified with
169
// various kinds of keywords too
170
//int fontVariant = propertyList.get("font-variant").getEnum();
171
FontTriplet triplet = fontInfo.fontLookup(getFontFamily(), style,
172                                                font_weight);
173             fontState = fontInfo.getFontInstance(triplet, fontSize.getValue(context));
174         }
175         return fontState;
176     }
177
178 }
179
Popular Tags