All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.graphics.units.GfxSizeImpl Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.graphics.units;


import com.hfg.exception.ProgrammingException;


//------------------------------------------------------------------------------
/**
 * Base class for unit-independent graphic length measurement.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class GfxSizeImpl implements GfxSize
{

   //##########################################################################
   // PRIVATE FIELDS
   //##########################################################################

   private float    mValue;
   private GfxUnits mGfxUnits;


   private static int sDPI = 96;

   //##########################################################################
   // CONSTRUCTORS
   //##########################################################################

   //--------------------------------------------------------------------------
   protected GfxSizeImpl(int inValue, GfxUnits inUnits)
   {
      mValue    = inValue;
      mGfxUnits = inUnits;
   }

   //--------------------------------------------------------------------------
   protected GfxSizeImpl(float inValue, GfxUnits inUnits)
   {
      mValue    = inValue;
      mGfxUnits = inUnits;
   }

   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################

   //--------------------------------------------------------------------------
   public static void setResolutionInDPI(int inValue)
   {
      sDPI = inValue;
   }

   //--------------------------------------------------------------------------
   public static int getResolutionInDPI()
   {
      return sDPI;
   }


   //--------------------------------------------------------------------------
   public float value()
   {
      return mValue;
   }

   //--------------------------------------------------------------------------
   public GfxUnits getUnits()
   {
      return mGfxUnits;
   }

   //--------------------------------------------------------------------------
   @Override
   public String toString()
   {
      return value() + mGfxUnits.abbrev();
   }

   //---------------------------------------------------------------------------
   public int toInt(GfxUnits inUnits)
   {
      return (int) to(inUnits);
   }

   //---------------------------------------------------------------------------
   public float to(GfxUnits inUnits)
   {
      float convertedValue = 0;

      float points = toPoints();


      if (inUnits.equals(GfxUnits.points))
      {
         convertedValue = points;
      }
      else if (inUnits.equals(GfxUnits.half_points))
      {
         convertedValue = points * 2f;
      }
      else if (inUnits.equals(GfxUnits.dxa))
      {
         convertedValue = points * 20f; // dxa = twentieth of a point
      }
      else if (inUnits.equals(GfxUnits.picas))
      {
         convertedValue = points / 12f; // There are 12 points per pica
      }
      else if (inUnits.equals(GfxUnits.inches))
      {
         convertedValue = points / 72f; // There are 72 points per inch
      }
      else if (inUnits.equals(GfxUnits.centimeters))
      {
         convertedValue = points / 72f * 2.54f; // There are 72 points per inch and 2.54 cm per inch
      }
      else if (inUnits.equals(GfxUnits.millimeters))
      {
         convertedValue = points / 72f * 25.4f; // There are 72 points per inch and 25.4 mm per inch
      }
      else if (inUnits.equals(GfxUnits.micrometers))
      {
         convertedValue = points / 72f * 25.4f * 1000; // There are 72 points per inch and 25,400 micrometers per inch
      }
      else if (inUnits.equals(GfxUnits.pixels))
      {
         convertedValue = points * sDPI / 72f;
      }
      else if (inUnits.equals(GfxUnits.emus))  // 914400 EMUs per inch
      {
         convertedValue = points * 914400f / 72f;
      }
      else
      {
         throw new ProgrammingException(inUnits + " is not a currently supported unit for conversion!");
      }

      return convertedValue;

   }

   //--------------------------------------------------------------------------
   public void scale(float inScalingFactor)
   {
      mValue *= inScalingFactor;
   }

   //---------------------------------------------------------------------------
   public GfxSize add(GfxSize inValue)
   {
      GfxSize result;
      if (inValue != null)
      {
         result = new Pixels(to(GfxUnits.pixels) + inValue.to(GfxUnits.pixels));
      }
      else
      {
         result = this;
      }

      return result;
   }

   //---------------------------------------------------------------------------
   public GfxSize subtract(GfxSize inValue)
   {
      GfxSize result;
      if (inValue != null)
      {
         result = new Pixels(to(GfxUnits.pixels) - inValue.to(GfxUnits.pixels));
      }
      else
      {
         result = this;
      }

      return result;
   }

   //---------------------------------------------------------------------------
   private float toPoints()
   {
      float points = 0;

      if (mGfxUnits.equals(GfxUnits.points))
      {
         points = mValue;
      }
      else if (mGfxUnits.equals(GfxUnits.half_points))
      {
         points = mValue / 2f;
      }
      else if (mGfxUnits.equals(GfxUnits.picas))
      {
         points = mValue * 12f; // There are 12 points per pica
      }
      else if (mGfxUnits.equals(GfxUnits.dxa))
      {
         points = mValue / 20f; // dxa = twentieth of a point
      }
      else if (mGfxUnits.equals(GfxUnits.inches))
      {
         points = mValue * 72f; // There are 72 points per inch
      }
      else if (mGfxUnits.equals(GfxUnits.centimeters))
      {
         points = mValue * 72f / 2.54f; // There are 72 points per inch and 2.54 cm per inch
      }
      else if (mGfxUnits.equals(GfxUnits.millimeters))
      {
         points = mValue * 72f / 25.4f; // There are 72 points per inch and 25.4 mm per inch
      }
      else if (mGfxUnits.equals(GfxUnits.micrometers))
      {
         points = mValue * 72f * 1000 / 25.4f; // There are 72 points per inch and 25400 micrometers per inch
      }
      else if (mGfxUnits.equals(GfxUnits.pixels))
      {
         points = mValue * 72f / sDPI;
      }
      else if (mGfxUnits.equals(GfxUnits.emus))
      {
         points = mValue * 72f / 914400f;
      }
      else
      {
         throw new ProgrammingException(mGfxUnits + " is not a currently supported unit for conversion!");
      }

      return points;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy