]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/Dimensions.java
Implement ImageDescriptor.getImageData(int zoom)
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / Dimensions.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  * 10.10.2006
14  */
15 package org.simantics.utils.ui.gfx;
16
17 import org.simantics.utils.ObjectUtils;
18
19
20 /**
21  * Dimensions
22  * @author Toni Kalajainen
23  */
24 public final class Dimensions {
25
26     public final static Double INCH = 25.4;
27     
28     /** Pixel size, may be null if unknown */
29     public final PixelDimension pixelDimension;
30     
31     /** Physical size, may be null if unknown */
32     public final PhysicalDimension physicaldimension;
33     
34     public Dimensions(PixelDimension pixelDimension, PhysicalDimension physicalDimension)
35     {
36         this.physicaldimension = physicalDimension;
37         this.pixelDimension = pixelDimension;
38     }
39     
40     @Override
41     public int hashCode() {
42         int code = 0;
43         if (pixelDimension!=null) code^= pixelDimension.hashCode();
44         if (physicaldimension!=null) code^= physicaldimension.hashCode();
45         return code;
46     }
47     
48     @Override
49     public boolean equals(Object obj) {
50         if (!(obj instanceof Dimensions)) return false;
51         Dimensions other = (Dimensions) obj;
52         return ObjectUtils.objectEquals(other.physicaldimension, physicaldimension) &&
53                ObjectUtils.objectEquals(other.pixelDimension, pixelDimension);
54     }
55     
56     
57     
58     public PhysicalDimension getPhysicaldimension() {
59         return physicaldimension;
60     }
61
62     public PixelDimension getPixelDimension() {
63         return pixelDimension;
64     }
65
66     /**
67      * Calculate horizontal Dots per inch.
68      * @return horizontal DPI or null if value is not available
69      */
70     public Double getHorizontalDPI()
71     {
72         if (pixelDimension==null || physicaldimension==null) return null;
73         if (physicaldimension.width==0) return null;
74         return (((double) pixelDimension.width)/physicaldimension.width)*INCH;
75     }
76     
77     /**
78      * Calculate vertical Dots per inch.
79      * @return Vertical DPI or null if value is not available
80      */
81     public Double getVerticalDPI()    
82     {
83         if (pixelDimension==null || physicaldimension==null) return null;
84         if (physicaldimension.height==0) return null;
85         return (((double) pixelDimension.height)/physicaldimension.height)*INCH;
86     }
87     
88     @Override
89     public String toString() {
90         return "Dimensions [" + physicaldimension + ", " + pixelDimension + "]";
91     }
92     
93     /**
94      * returns aspect ratio (width / height)
95      * @return returns aspect ratio (width / height)
96      */
97     public double getAspectRatio()
98     {
99         if (physicaldimension!=null)
100             return physicaldimension.getAspectRatio();
101         if (pixelDimension!=null)
102             return pixelDimension.getAspectRatio();
103         return 1;
104     }
105     
106     
107 }