1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.report.pentaho.styles;
24 
25 import org.jfree.layouting.input.style.values.CSSNumericType;
26 import org.jfree.layouting.input.style.values.CSSNumericValue;
27 
28 /**
29  * A helper class that sums up various CSS-length. The various unit types are
30  * kept separate until the final result is computed to minimize the computation
31  * inaccuracy.
32  *
33  * @author Thomas Morgner
34  * @since 15.03.2007
35  */
36 public class LengthCalculator
37 {
38     // and centimeter (x10)
39 
40     private double millimeter;
41     // and pica (x12) and inch(x72). Px is assumed to be in 96dpi.
42     private double point;
43     private double pixel;
44 
LengthCalculator()45     public LengthCalculator()
46     {
47     }
48 
add(final CSSNumericValue value)49     public void add(final CSSNumericValue value)
50     {
51         if (value == null)
52         {
53             return;
54         }
55 
56         final CSSNumericType numericType = value.getType();
57         if (numericType == CSSNumericType.CM)
58         {
59             millimeter += value.getValue() * 10;
60         }
61         else if (numericType == CSSNumericType.MM)
62         {
63             millimeter += value.getValue();
64         }
65         else if (numericType == CSSNumericType.PT)
66         {
67             point += value.getValue();
68         }
69         else if (numericType == CSSNumericType.PC)
70         {
71             point += 12 * value.getValue();
72         }
73         else if (numericType == CSSNumericType.INCH)
74         {
75             point += 72 * value.getValue();
76         }
77         else if (numericType == CSSNumericType.PX)
78         {
79             pixel += value.getValue();
80         }
81         // LOGGER.debug ("Adding " + value  + " [mm: " + millimeter + "] [pt: " + point + "] px: [" + pixel + "]");
82     }
83 
getResult()84     public CSSNumericValue getResult()
85     {
86         if (pixel == 0 && point == 0)
87         {
88             return CSSNumericValue.createValue(CSSNumericType.MM, millimeter);
89         }
90         if (pixel == 0 && millimeter == 0)
91         {
92             return CSSNumericValue.createValue(CSSNumericType.PT, point);
93         }
94         if (point == 0 && millimeter == 0)
95         {
96             return CSSNumericValue.createValue(CSSNumericType.PX, pixel);
97         }
98         // else convert it.
99 
100         double result = point;
101         result += (millimeter * 10 * 72 / 254);
102         result += pixel * 72 / 96;
103 
104         return CSSNumericValue.createValue(CSSNumericType.PT, result);
105     }
106 }
107