at.spardat.xma.page.Scaler Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
/*
* @(#) $Id: Scaler.java 2089 2007-11-28 13:56:13Z s3460 $
*
*
*
*
*/
package at.spardat.xma.page;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
/**
* Implements scaling methods which can be used to scale pixel values the same amount as the
* current system font differs from the standard font (on Windows: small fonts 96 DPI).
* If the system font is set to 125% (big fonts 120 DPI), the scaling funtions multiply their
* input by 1.25.
* The methods convertXToCurrent() and convertYToCurrent() do the scaling.
* The methods convertXToStandard() and convertYToStandard() do the inverse operation.
* Ther are different methods for X and Y directions because there may be different scaling
* factors in the future.
*
* @author s2877
*/
public class Scaler {
static final String scaleText = "The quick brown fox jumped over the lazy dog.";
static Scaler instance;
//current scaling factors (aprox.: current base / small base)
double factorX = 1.0;
double factorY = 1.0;
//base values in case of small font
//double smallBaseX = 4.9382716049382722; //Sans Serif
double smallBaseX = 5.050505050505051; //Tahoma 8pt 96dpi
double smallBaseY = 12.0;
// standard DPI used as baseline for scalings
double standardDPI = 96;
/**
* constructs a scaler
*/
Scaler(Composite comp) {
PageClient page = null;
for(Control c = comp;page==null&c!=null;c=c.getParent()) {
page = (PageClient) c.getData();
}
if(page!=null) {
String alg = page.getComponent().getSession().getRuntimeProperty("scalerAlgorithm");
if("scalebyLabel".equals(alg)) {
scalebyLabel(comp);
} else {
scalebyDPI(comp);
}
} else {
scalebyDPI(comp);
}
}
private void scalebyLabel(Composite comp) {
Label label = new Label(comp,SWT.NONE);
label.setText(scaleText);
Point p = label.computeSize(SWT.DEFAULT,SWT.DEFAULT);
double fx = (p.x-1)/44.55;
double fy = p.y-1;
//rem: divide first to overcome rounding errors in case of fx==SmallBaseX and fy==SmallBaseY
factorX = (fx/smallBaseX); //+0.08;
factorY = (fy/smallBaseY); //+0.08;
label.dispose();
}
private void scalebyDPI(Composite comp) {
Point p = comp.getDisplay().getDPI();
factorX = p.x/standardDPI;
factorY = p.y/standardDPI;
}
// scaling accessors
/**
* Returns the scaling factor in X-direction.
*/
public double getFactorX() {
return factorX;
}
/**
* Returns the scaling factor in Y-direction.
*/
public double getFactorY() {
return factorY;
}
/**
* Determine if the scaling-factors are different from 1.
* @return true if the convert-Methods will change values
*/
public boolean isScalingNecessary() {
return factorX!=1.0 || factorY!=1.0;
}
// scaling core methods
/**
* scales the given value in X-direction.
*/
public int convertXToCurrent(int x) {
if (x<0) {
return -(int)(-x*factorX+0.5);
}
return (int)(x*factorX+0.5);
}
/**
* scales the given value in Y-direction.
*/
public int convertYToCurrent(int y) {
if (y<0) {
return -(int)(-y*factorY+0.5);
}
return (int)(y*factorY+0.5);
}
/**
* converts back the given scaled value
*/
public int convertXToStandard(int x) {
if (x<0) {
return -(int)(-x/factorX+0.5);
}
return (int)(x/factorX+0.5);
}
/**
* converts back the given scaled value
*/
public int convertYToStandard(int y) {
if (y<0) {
return -(int)(-y/factorY+0.5);
}
return (int)(y/factorY+0.5);
}
/**
* Get an instance of the scaler.
* @param comp one Composite or Shell of the application.
* @return the scaler
*/
public static Scaler getInstance(Composite comp) {
if(instance==null) {
instance = new Scaler(comp);
}
return instance;
}
}