KickJava   Java API By Example, From Geeks To Geeks.

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


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: FontStretchPropertyMaker.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.fo.properties;
21 import org.apache.fop.fo.Constants;
22 import org.apache.fop.fo.FObj;
23 import org.apache.fop.fo.PropertyList;
24 import org.apache.fop.fo.expr.PropertyException;
25
26 /**
27  * This subclass of EnumProperty.Maker handles the special treatment of
28  * relative font stretch values described in 7.8.5.
29  */

30 public class FontStretchPropertyMaker
31     extends EnumProperty.Maker implements Constants {
32     
33     /* Ordered list of absolute font stretch properties so we can easily find the next /
34      * previous one */

35     private Property[] orderedFontStretchValues = null;
36         
37     /**
38      * Create an enum property which can handle relative font stretches
39      * @param propId the font size property id.
40      */

41     public FontStretchPropertyMaker(int propId) {
42         super(propId);
43     }
44     
45     /**
46      * @see PropertyMaker#convertProperty
47      * Implements the parts of 7.8.5 relevant to relative font stretches
48      */

49     public Property convertProperty(Property p,
50                                     PropertyList propertyList,
51                                     FObj fo) throws PropertyException {
52         // if it is a relative font stretch value get current parent value and step
53
// up or down accordingly
54
if (p.getEnum() == EN_NARROWER) {
55             return computeNextAbsoluteFontStretch(propertyList.getFromParent(this.getPropId()), -1);
56         } else if (p.getEnum() == EN_WIDER) {
57             return computeNextAbsoluteFontStretch(propertyList.getFromParent(this.getPropId()), 1);
58         }
59         return super.convertProperty(p, propertyList, fo);
60     }
61
62     /**
63      * Calculates the nearest absolute font stretch property to the given
64      * font stretch
65      * @param baseProperty the font stretch property as set on the parent fo
66      * @param direction should be -1 to get the next narrower value or +1 for the next wider value
67      */

68     private Property computeNextAbsoluteFontStretch(Property baseProperty, int direction) {
69         // Create the table entries the first time around
70
// @todo is this thread safe, do we need to worry about this here?
71
if (orderedFontStretchValues == null) {
72             orderedFontStretchValues = new Property[] {
73                 checkEnumValues("ultra-condensed"),
74                 checkEnumValues("extra-condensed"),
75                 checkEnumValues("condensed"),
76                 checkEnumValues("semi-condensed"),
77                 checkEnumValues("normal"),
78                 checkEnumValues("semi-expanded"),
79                 checkEnumValues("expanded"),
80                 checkEnumValues("extra-expanded"),
81                 checkEnumValues("ultra-expanded")
82             };
83         }
84         int baseValue = baseProperty.getEnum();
85         for (int i = 0; i < orderedFontStretchValues.length; i++) {
86             if (baseValue == orderedFontStretchValues[i].getEnum()) {
87                 // increment/decrement the index and make sure its within the array bounds
88
i = Math.min(Math.max(0, i + direction), orderedFontStretchValues.length - 1);
89                 return orderedFontStretchValues[i];
90             }
91         }
92         // return the normal value
93
return orderedFontStretchValues[4];
94     }
95
96 }
97
Popular Tags