/* LayoutUnitConversionHelper.java

	Purpose:
		
	Description:
		
	History:
		August 23, 5:53:16 PM     2010, Created by Ashish Dasnurkar

Copyright (C) 2010 Potix Corporation. All Rights Reserved.
 */
package org.zkoss.zss.model.impl.pdf;

/**
 * Internal Use Only. Utility methods for POI width/height to pdf user space unit conversion
 * @author ashish
 *
 */
/*package*/ class LayoutUnitConversionHelper {
	 /** convert pixel to twip (1/20 point) */
	 public static int pxToTwip(int px) {
	  return px * 72 * 20 / 96; //assume 96dpi
	 }

	 /** convert twip (1/20 point) to pixel */
	 public static int twipToPx(int twip) {
	  return twip * 96 / 72 / 20; //assume 96dpi
	 }

	 /** convert file 1/256 character width to pixel */
	 public static int fileChar256ToPx(int char256, int charWidth) {
	  final double w = (double) char256;
	  return (int) Math.floor(w * charWidth / 256 + 0.5);
	 }
	 
	 /** convert pixel to file 1/256 character width */
	 public static int pxToFileChar256(int px, int charWidth) {
	  final double w = (double) px;
	  return (int) Math.floor(w * 256 / charWidth + 0.5);
	 }
	 
	 /** convert pixels to pdf user space points. 58 as a pdfCharWidth 
	  * gives most accurate conversion */
	 public static double pxToPdfHeightPoints(int px) {
		 final double w = (double) px;
		 return (double) (w * 0.725); // 0.725 is get from experiments(by compare to excel printouts)
	 }
	 
	 public static double pxToPdfWidthPoints(int px) {
		 final double w = (double) px;
		 return (double) (w * 0.805); // 0.805 is get from experiments(by compare to excel printouts)
	 }

	 /** convert pixels to pdf user space points. 58 as a pdfCharWidth 
	  * gives most accurate conversion */
	 public static double twippxToPdfPoints(int px, int pdfCharWidth) {
		 final double w = (double) px;
		 return w * pdfCharWidth / 72;
	 }

	 /** converts from file 1/256 character width to pdf user space points */
	 public static double fileChar256ToPdfPoints(int char256, int charWidth, 
			 int pdfCharWidth) {
		 return pxToPdfWidthPoints(fileChar256ToPx(char256, charWidth));
	 }
	
	 /** converts from twip 1/20 of a point width to pdf user space points */
	 public static double twipsToPdfPoints(int twip, int pdfCharWidth) {
		 return twippxToPdfPoints(twipToPx(twip), pdfCharWidth);
	 }
	 
}
