001package com.hfg.graphics.units; 002 003 004import com.hfg.exception.ProgrammingException; 005 006 007//------------------------------------------------------------------------------ 008/** 009 * Base class for unit-independent graphic length measurement. 010 * 011 * @author J. Alex Taylor, hairyfatguy.com 012 */ 013//------------------------------------------------------------------------------ 014// com.hfg XML/HTML Coding Library 015// 016// This library is free software; you can redistribute it and/or 017// modify it under the terms of the GNU Lesser General Public 018// License as published by the Free Software Foundation; either 019// version 2.1 of the License, or (at your option) any later version. 020// 021// This library is distributed in the hope that it will be useful, 022// but WITHOUT ANY WARRANTY; without even the implied warranty of 023// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 024// Lesser General Public License for more details. 025// 026// You should have received a copy of the GNU Lesser General Public 027// License along with this library; if not, write to the Free Software 028// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 029// 030// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 031// jataylor@hairyfatguy.com 032//------------------------------------------------------------------------------ 033 034public class GfxSizeImpl implements GfxSize 035{ 036 037 //########################################################################## 038 // PRIVATE FIELDS 039 //########################################################################## 040 041 private float mValue; 042 private GfxUnits mGfxUnits; 043 044 045 private static int sDPI = 96; 046 047 //########################################################################## 048 // CONSTRUCTORS 049 //########################################################################## 050 051 //-------------------------------------------------------------------------- 052 protected GfxSizeImpl(int inValue, GfxUnits inUnits) 053 { 054 mValue = inValue; 055 mGfxUnits = inUnits; 056 } 057 058 //-------------------------------------------------------------------------- 059 protected GfxSizeImpl(float inValue, GfxUnits inUnits) 060 { 061 mValue = inValue; 062 mGfxUnits = inUnits; 063 } 064 065 //########################################################################## 066 // PUBLIC METHODS 067 //########################################################################## 068 069 //-------------------------------------------------------------------------- 070 public static void setResolutionInDPI(int inValue) 071 { 072 sDPI = inValue; 073 } 074 075 //-------------------------------------------------------------------------- 076 public static int getResolutionInDPI() 077 { 078 return sDPI; 079 } 080 081 082 //-------------------------------------------------------------------------- 083 public float value() 084 { 085 return mValue; 086 } 087 088 //-------------------------------------------------------------------------- 089 public GfxUnits getUnits() 090 { 091 return mGfxUnits; 092 } 093 094 //-------------------------------------------------------------------------- 095 @Override 096 public String toString() 097 { 098 return value() + mGfxUnits.abbrev(); 099 } 100 101 //--------------------------------------------------------------------------- 102 public int toInt(GfxUnits inUnits) 103 { 104 return (int) to(inUnits); 105 } 106 107 //--------------------------------------------------------------------------- 108 public float to(GfxUnits inUnits) 109 { 110 float convertedValue = 0; 111 112 float points = toPoints(); 113 114 115 if (inUnits.equals(GfxUnits.points)) 116 { 117 convertedValue = points; 118 } 119 else if (inUnits.equals(GfxUnits.half_points)) 120 { 121 convertedValue = points * 2f; 122 } 123 else if (inUnits.equals(GfxUnits.dxa)) 124 { 125 convertedValue = points * 20f; // dxa = twentieth of a point 126 } 127 else if (inUnits.equals(GfxUnits.picas)) 128 { 129 convertedValue = points / 12f; // There are 12 points per pica 130 } 131 else if (inUnits.equals(GfxUnits.inches)) 132 { 133 convertedValue = points / 72f; // There are 72 points per inch 134 } 135 else if (inUnits.equals(GfxUnits.centimeters)) 136 { 137 convertedValue = points / 72f * 2.54f; // There are 72 points per inch and 2.54 cm per inch 138 } 139 else if (inUnits.equals(GfxUnits.millimeters)) 140 { 141 convertedValue = points / 72f * 25.4f; // There are 72 points per inch and 25.4 mm per inch 142 } 143 else if (inUnits.equals(GfxUnits.micrometers)) 144 { 145 convertedValue = points / 72f * 25.4f * 1000; // There are 72 points per inch and 25,400 micrometers per inch 146 } 147 else if (inUnits.equals(GfxUnits.pixels)) 148 { 149 convertedValue = points * sDPI / 72f; 150 } 151 else if (inUnits.equals(GfxUnits.emus)) // 914400 EMUs per inch 152 { 153 convertedValue = points * 914400f / 72f; 154 } 155 else 156 { 157 throw new ProgrammingException(inUnits + " is not a currently supported unit for conversion!"); 158 } 159 160 return convertedValue; 161 162 } 163 164 //-------------------------------------------------------------------------- 165 public void scale(float inScalingFactor) 166 { 167 mValue *= inScalingFactor; 168 } 169 170 //--------------------------------------------------------------------------- 171 public GfxSize add(GfxSize inValue) 172 { 173 GfxSize result; 174 if (inValue != null) 175 { 176 result = new Pixels(to(GfxUnits.pixels) + inValue.to(GfxUnits.pixels)); 177 } 178 else 179 { 180 result = this; 181 } 182 183 return result; 184 } 185 186 //--------------------------------------------------------------------------- 187 public GfxSize subtract(GfxSize inValue) 188 { 189 GfxSize result; 190 if (inValue != null) 191 { 192 result = new Pixels(to(GfxUnits.pixels) - inValue.to(GfxUnits.pixels)); 193 } 194 else 195 { 196 result = this; 197 } 198 199 return result; 200 } 201 202 //--------------------------------------------------------------------------- 203 private float toPoints() 204 { 205 float points = 0; 206 207 if (mGfxUnits.equals(GfxUnits.points)) 208 { 209 points = mValue; 210 } 211 else if (mGfxUnits.equals(GfxUnits.half_points)) 212 { 213 points = mValue / 2f; 214 } 215 else if (mGfxUnits.equals(GfxUnits.picas)) 216 { 217 points = mValue * 12f; // There are 12 points per pica 218 } 219 else if (mGfxUnits.equals(GfxUnits.dxa)) 220 { 221 points = mValue / 20f; // dxa = twentieth of a point 222 } 223 else if (mGfxUnits.equals(GfxUnits.inches)) 224 { 225 points = mValue * 72f; // There are 72 points per inch 226 } 227 else if (mGfxUnits.equals(GfxUnits.centimeters)) 228 { 229 points = mValue * 72f / 2.54f; // There are 72 points per inch and 2.54 cm per inch 230 } 231 else if (mGfxUnits.equals(GfxUnits.millimeters)) 232 { 233 points = mValue * 72f / 25.4f; // There are 72 points per inch and 25.4 mm per inch 234 } 235 else if (mGfxUnits.equals(GfxUnits.micrometers)) 236 { 237 points = mValue * 72f * 1000 / 25.4f; // There are 72 points per inch and 25400 micrometers per inch 238 } 239 else if (mGfxUnits.equals(GfxUnits.pixels)) 240 { 241 points = mValue * 72f / sDPI; 242 } 243 else if (mGfxUnits.equals(GfxUnits.emus)) 244 { 245 points = mValue * 72f / 914400f; 246 } 247 else 248 { 249 throw new ProgrammingException(mGfxUnits + " is not a currently supported unit for conversion!"); 250 } 251 252 return points; 253 } 254}