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;
import com.hfg.units.Quantity;


//------------------------------------------------------------------------------
/**
 * 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, Cloneable
{

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

   private float    mValue;
   private GfxUnits mGfxUnits;

   private GfxContext mGfxContext = sDefaultGfxContext;

   private static GfxContext sDefaultGfxContext = new GfxContext(new ScreenResolution());

   //##########################################################################
   // 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 setDefaultGfxContext(GfxContext inValue)
   {
      sDefaultGfxContext = inValue;
   }


   //--------------------------------------------------------------------------
   public GfxSizeImpl setGfxContext(GfxContext inValue)
   {
      mGfxContext = inValue;
      return this;
   }


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

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

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

   //---------------------------------------------------------------------------
   @Override
   public GfxSize clone()
   {
      GfxSize cloneObj;
      try
      {
         cloneObj = (GfxSize) super.clone();
      }
      catch (CloneNotSupportedException e)
      {
         throw new ProgrammingException("Error during cloning!", e);
      }

      return cloneObj;
   }

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

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

      if (mGfxUnits.equals(inUnits))
      {
         // No need to convert
         convertedValue = mValue;
      }
      else
      {
         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 * mGfxContext.getResolution().convertTo(ResolutionUnit.DPI).floatValue() / 72f;
         }
         else if (inUnits.equals(GfxUnits.emus))  // 914400 EMUs per inch
         {
            convertedValue = points * 914400f / 72f;
         }
         else if (inUnits.equals(GfxUnits.dots))
         {
            // Convert points to inches
            float inches = points / 72f; // There are 72 points per inch
            // Convert inches to dots
            convertedValue = inches * mGfxContext.getResolution().convertTo(ResolutionUnit.DPI).floatValue();
         }
         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 = null;
      if (inValue != null)
      {
         result = GfxSize.allocate(value() + inValue.to(getUnits()), getUnits());
      }
      else
      {
         result = this;
      }

      return result;
   }

   //---------------------------------------------------------------------------
   public GfxSize subtract(GfxSize inValue)
   {
      GfxSize result;
      if (inValue != null)
      {
         result = GfxSize.allocate(value() - inValue.to(getUnits()), getUnits());
      }
      else
      {
         result = this;
      }

      return result;
   }

   //##########################################################################
   // PRIVATE METHODS
   //##########################################################################

   //---------------------------------------------------------------------------
   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 / mGfxContext.getResolution().convertTo(ResolutionUnit.DPI).floatValue();
      }
      else if (mGfxUnits.equals(GfxUnits.emus))
      {
         points = mValue * 72f / 914400f;
      }
      else if (mGfxUnits.equals(GfxUnits.dots))
      {
         // Convert dots to inches
         float inches = mValue / mGfxContext.getResolution().convertTo(ResolutionUnit.DPI).floatValue();
         // Convert inches to points
         points = inches * 72f; // There are 72 points per inch
      }
      else
      {
         throw new ProgrammingException(mGfxUnits + " is not a currently supported unit for conversion!");
      }

      return points;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy