KickJava   Java API By Example, From Geeks To Geeks.

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


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: FontSizePropertyMaker.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 LengthProperty.Maker handles the special treatment of
28  * relative font sizes described in 7.8.4.
29  */

30 public class FontSizePropertyMaker
31     extends LengthProperty.Maker implements Constants {
32
33     /** The default normal font size in mpt */
34     private static final int FONT_SIZE_NORMAL = 12000;
35     /** The factor to be applied when stepping font sizes upwards */
36     private static final double FONT_SIZE_GROWTH_FACTOR = 1.2;
37     
38     /**
39      * Create a length property which can handle relative font sizes
40      * @param propId the font size property id.
41      */

42     public FontSizePropertyMaker(int propId) {
43         super(propId);
44     }
45     
46     /**
47      * @see PropertyMaker#convertProperty
48      * Implements the parts of 7.8.4 relevant to relative font sizes
49      */

50     public Property convertProperty(Property p,
51                                     PropertyList propertyList,
52                                     FObj fo) throws PropertyException {
53         if (p.getEnum() == EN_LARGER || p.getEnum() == EN_SMALLER) {
54             // get the corresponding property from parent
55
Property pp = propertyList.getFromParent(this.getPropId());
56             int baseFontSize = computeClosestAbsoluteFontSize(pp.getLength().getValue());
57             if (p.getEnum() == EN_LARGER) {
58                 return new FixedLength((int)Math.round((baseFontSize * FONT_SIZE_GROWTH_FACTOR)));
59             } else {
60                 return new FixedLength((int)Math.round((baseFontSize / FONT_SIZE_GROWTH_FACTOR)));
61             }
62         }
63         return super.convertProperty(p, propertyList, fo);
64     }
65     
66     /**
67      * Calculates the nearest absolute font size to the given
68      * font size.
69      * @param baseFontSize the font size in mpt of the parent fo
70      * @return the closest absolute font size
71      */

72     private int computeClosestAbsoluteFontSize(int baseFontSize) {
73         double scale = FONT_SIZE_GROWTH_FACTOR;
74         int lastStepFontSize = FONT_SIZE_NORMAL;
75         if (baseFontSize < FONT_SIZE_NORMAL) {
76             // Need to shrink the font sizes = scale downwards
77
scale = 1 / FONT_SIZE_GROWTH_FACTOR;
78         }
79         // Calculate the inital next step font size
80
int nextStepFontSize = (int)Math.round(lastStepFontSize * scale);
81         while (scale < 1 && nextStepFontSize > baseFontSize
82                 || scale > 1 && nextStepFontSize < baseFontSize) {
83             // baseFontSize is still bigger (if we grow) or smaller (if we shrink)
84
// than the last caculated step
85
lastStepFontSize = nextStepFontSize;
86             nextStepFontSize = (int)Math.round(lastStepFontSize * scale);
87         }
88         // baseFontSize is between last and next step font size
89
// Return the step value closer to the baseFontSize
90
if (Math.abs(lastStepFontSize - baseFontSize)
91                 <= Math.abs(baseFontSize - nextStepFontSize)) {
92             return lastStepFontSize;
93         }
94         return nextStepFontSize;
95     }
96
97 }
98
Popular Tags