KickJava   Java API By Example, From Geeks To Geeks.

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


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: FontShorthandProperty.java 429168 2006-08-06 18:23:59Z adelmelle $ */
19
20 package org.apache.fop.fo.properties;
21
22 import org.apache.fop.fo.Constants;
23 import org.apache.fop.fo.FObj;
24 import org.apache.fop.fo.PropertyList;
25 import org.apache.fop.fo.expr.PropertyException;
26
27 /**
28  * Property subclass for the font shorthand
29  */

30 public class FontShorthandProperty extends ListProperty {
31
32     /**
33      * Inner class for creating instances of FontShorthandProperty
34      */

35     public static class Maker extends PropertyMaker {
36
37         private static final int[] PROP_IDS = {
38             Constants.PR_FONT_SIZE, Constants.PR_FONT_FAMILY,
39             Constants.PR_LINE_HEIGHT, Constants.PR_FONT_STYLE,
40             Constants.PR_FONT_VARIANT, Constants.PR_FONT_WEIGHT
41         };
42         
43         /**
44          * @param propId ID of the property for which Maker should be created
45          */

46         public Maker(int propId) {
47             super(propId);
48         }
49         
50         /**
51          * @see PropertyMaker#make(PropertyList, String, FObj)
52          */

53         public Property make(PropertyList propertyList,
54                 String JavaDoc value, FObj fo) throws PropertyException {
55             
56             try {
57                 FontShorthandProperty newProp = new FontShorthandProperty();
58                 newProp.setSpecifiedValue(value);
59                 
60                 String JavaDoc specVal = value;
61                 Property prop = null;
62                 if ("inherit".equals(specVal)) {
63                     /* fill the list with the individual properties from the parent */
64                     for (int i = PROP_IDS.length; --i >= 0;) {
65                         prop = propertyList.getFromParent(PROP_IDS[i]);
66                         newProp.addProperty(prop, i);
67                     }
68                 } else {
69                     /* initialize list with nulls */
70                     for (int pos = PROP_IDS.length; --pos >= 0;) {
71                         newProp.addProperty(null, pos);
72                     }
73                     prop = checkEnumValues(specVal);
74                     if (prop == null) {
75                         /* not an enum:
76                          * value should consist at least of font-size and font-family
77                          * separated by a space
78                          * mind the possible spaces from quoted font-family names
79                          */

80                         int spaceIndex = value.indexOf(' ');
81                         int quoteIndex = (value.indexOf('\'') == -1)
82                             ? value.indexOf('\"') : value.indexOf('\'');
83                         if (spaceIndex == -1
84                                 || (quoteIndex != -1 && spaceIndex > quoteIndex)) {
85                             /* no spaces or first space appears after the first
86                              * single/double quote, so malformed value string
87                              */

88                             throw new PropertyException("Invalid property value: "
89                                     + "font=\"" + value + "\"");
90                         }
91                         PropertyMaker m = null;
92                         int fromIndex = spaceIndex + 1;
93                         int toIndex = specVal.length();
94                         /* at least one space that appears before the first
95                          * single/double quote, so extract the individual properties
96                          */

97                         boolean fontFamilyParsed = false;
98                         int commaIndex = value.indexOf(',');
99                         while (!fontFamilyParsed) {
100                             /* value contains a (list of) possibly quoted
101                              * font-family name(s)
102                              */

103                             if (commaIndex == -1) {
104                                 /* no list, just a single name
105                                  * (or first name in the list)
106                                  */

107                                 if (quoteIndex != -1) {
108                                     /* a single name, quoted
109                                      */

110                                     fromIndex = quoteIndex;
111                                 }
112                                 m = FObj.getPropertyMakerFor(PROP_IDS[1]);
113                                 prop = m.make(propertyList, specVal.substring(fromIndex), fo);
114                                 newProp.addProperty(prop, 1);
115                                 fontFamilyParsed = true;
116                             } else {
117                                 if (quoteIndex != -1 && quoteIndex < commaIndex) {
118                                     /* a quoted font-family name as first name
119                                      * in the comma-separated list
120                                      * fromIndex = index of the first quote
121                                      */

122                                     fromIndex = quoteIndex;
123                                     quoteIndex = -1;
124                                 } else {
125                                     fromIndex = value.lastIndexOf(' ', commaIndex) + 1;
126                                 }
127                                 commaIndex = -1;
128                             }
129                         }
130                         toIndex = fromIndex - 1;
131                         fromIndex = value.lastIndexOf(' ', toIndex - 1) + 1;
132                         value = specVal.substring(fromIndex, toIndex);
133                         int slashIndex = value.indexOf('/');
134                         String JavaDoc fontSize = value.substring(0,
135                                 (slashIndex == -1) ? value.length() : slashIndex);
136                         m = FObj.getPropertyMakerFor(PROP_IDS[0]);
137                         prop = m.make(propertyList, fontSize, fo);
138                         /* need to make sure subsequent call to LineHeightPropertyMaker.make()
139                          * doesn't generate the default font-size property...
140                          */

141                         propertyList.putExplicit(PROP_IDS[0], prop);
142                         newProp.addProperty(prop, 0);
143                         if (slashIndex != -1) {
144                             /* line-height */
145                             String JavaDoc lineHeight = value.substring(slashIndex + 1);
146                             m = FObj.getPropertyMakerFor(PROP_IDS[2]);
147                             prop = m.make(propertyList, lineHeight, fo);
148                             newProp.addProperty(prop, 2);
149                         }
150                         if (fromIndex != 0) {
151                             toIndex = fromIndex - 1;
152                             value = specVal.substring(0, toIndex);
153                             fromIndex = 0;
154                             spaceIndex = value.indexOf(' ');
155                             do {
156                                 toIndex = (spaceIndex == -1) ? value.length() : spaceIndex;
157                                 String JavaDoc val = value.substring(fromIndex, toIndex);
158                                 for (int i = 6; --i >= 3;) {
159                                     if (newProp.list.get(i) == null) {
160                                         /* not set */
161                                         m = FObj.getPropertyMakerFor(PROP_IDS[i]);
162                                         val = m.checkValueKeywords(val);
163                                         prop = m.checkEnumValues(val);
164                                         if (prop != null) {
165                                             newProp.addProperty(prop, i);
166                                         }
167                                     }
168                                 }
169                                 fromIndex = toIndex + 1;
170                                 spaceIndex = value.indexOf(' ', fromIndex);
171                             } while (toIndex != value.length());
172                         }
173                     } else {
174                         //TODO: implement enum values
175
log.warn("Enum values other than \"inherit\""
176                                 + " not yet supported for the font shorthand.");
177                         return null;
178                     }
179                 }
180                 if (newProp.list.get(0) == null || newProp.list.get(1) == null) {
181                     throw new PropertyException("Invalid property value: "
182                             + "font-size and font-family are required for the font shorthand"
183                             + "\nfont=\"" + value + "\"");
184                 }
185                 return newProp;
186            } catch (PropertyException pe) {
187                pe.setLocator(propertyList.getFObj().getLocator());
188                pe.setPropertyName(getName());
189                throw pe;
190            }
191         }
192     }
193     
194     private void addProperty(Property prop, int pos) {
195         while (list.size() < (pos + 1)) {
196             list.add(null);
197         }
198         list.set(pos, prop);
199     }
200 }
201
Popular Tags