下面列出了org.w3c.dom.css.CSSPrimitiveValue#CSS_NUMBER 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static FloatValue toFloatValue( Object value )
{
if( value == null )
{
return null;
}
if( value instanceof Number )
{
return new FloatValue( CSSPrimitiveValue.CSS_NUMBER, ( ( Number ) value ).floatValue( ) );
}
else if( value instanceof DimensionType )
{
return toFloatValue( ( DimensionType ) value );
}
else
{
return toFloatValue( value.toString( ) );
}
}
public static FloatValue toFloatValue( DimensionType value )
{
if ( value.getValueType( ) == DimensionType.TYPE_DIMENSION )
{
Object obj = UnitMapping.get( value.getUnits( ) );
short unit;
if( obj != null )
{
unit = ( ( Short ) obj ).shortValue( );
}
else
{
unit = CSSPrimitiveValue.CSS_NUMBER;
}
return new FloatValue( unit, ( float ) value.getMeasure( ) );
}
return null;
}
/**
* Creates a color component from a lexical unit.
*/
protected Value createColorComponent( LexicalUnit lu ) throws DOMException
{
switch ( lu.getLexicalUnitType( ) )
{
case LexicalUnit.SAC_INTEGER :
return new FloatValue( CSSPrimitiveValue.CSS_NUMBER, lu
.getIntegerValue( ) );
case LexicalUnit.SAC_REAL :
return new FloatValue( CSSPrimitiveValue.CSS_NUMBER, lu
.getFloatValue( ) );
case LexicalUnit.SAC_PERCENTAGE :
return new FloatValue( CSSPrimitiveValue.CSS_PERCENTAGE, lu
.getFloatValue( ) );
}
throw createInvalidRGBComponentUnitDOMException( lu
.getLexicalUnitType( ) );
}
private int getSize( CSSValue value )
{
// Copied from
// org.eclipse.birt.report.engine.layout.pdf.util.PropertyUtil.getDimensionValueConsiderDpi()
if ( value != null && ( value instanceof FloatValue ) )
{
FloatValue fv = (FloatValue) value;
float v = fv.getFloatValue( );
switch ( fv.getPrimitiveType( ) )
{
case CSSPrimitiveValue.CSS_CM :
return (int) ( v * 72 / 2.54 );
case CSSPrimitiveValue.CSS_IN :
return (int) ( v * 72 );
case CSSPrimitiveValue.CSS_MM :
return (int) ( v * 7.2 / 2.54 );
case CSSPrimitiveValue.CSS_PC :
return (int) ( v * 12 );
case CSSPrimitiveValue.CSS_PX :
return (int) ( v / dpi * 72f );
case CSSPrimitiveValue.CSS_PT :
return (int) v;
case CSSPrimitiveValue.CSS_NUMBER :
return (int) ( v / 1000 );
}
}
return 0;
}
/**
* Get the border width from a style. It don't support '%'.
*
* @param style
* @param borderNum
* @return
*/
private int getBorderWidthValue( IStyle style, int borderNum )
{
if ( null == style )
{
return 0;
}
if ( IStyle.STYLE_BORDER_TOP_WIDTH != borderNum
&& IStyle.STYLE_BORDER_RIGHT_WIDTH != borderNum
&& IStyle.STYLE_BORDER_BOTTOM_WIDTH != borderNum
&& IStyle.STYLE_BORDER_LEFT_WIDTH != borderNum )
{
return 0;
}
CSSValue value = style.getProperty( borderNum );
if ( value != null && ( value instanceof FloatValue ) )
{
FloatValue fv = (FloatValue) value;
float v = fv.getFloatValue( );
switch ( fv.getPrimitiveType( ) )
{
case CSSPrimitiveValue.CSS_CM :
return (int) ( v * 72000 / 2.54 );
case CSSPrimitiveValue.CSS_IN :
return (int) ( v * 72000 );
case CSSPrimitiveValue.CSS_MM :
return (int) ( v * 7200 / 2.54 );
case CSSPrimitiveValue.CSS_PT :
return (int) ( v * 1000 );
case CSSPrimitiveValue.CSS_NUMBER :
return (int) v;
}
}
return 0;
}
/**
* Returns the dimension value in inches.
*/
public static double getDimensionValue( CSSValue value, int dpi )
{
if ( value != null && ( value instanceof FloatValue ) )
{
FloatValue fv = (FloatValue) value;
float v = fv.getFloatValue( );
switch ( fv.getPrimitiveType( ) )
{
case CSSPrimitiveValue.CSS_CM :
return ( v / 2.54 );
case CSSPrimitiveValue.CSS_IN :
return ( v );
case CSSPrimitiveValue.CSS_MM :
return ( v / 25.4 );
case CSSPrimitiveValue.CSS_PC :
return ( v * 12 * 1000 );
case CSSPrimitiveValue.CSS_PX :
return ( v / dpi );
case CSSPrimitiveValue.CSS_PT :
return ( v / 72 );
case CSSPrimitiveValue.CSS_NUMBER :
return v;
}
}
return 0;
}
private static int getDimensionValue( CSSValue value, int dpi,
int referenceLength )
{
if ( value != null && ( value instanceof FloatValue ) )
{
FloatValue fv = (FloatValue) value;
float v = fv.getFloatValue( );
switch ( fv.getPrimitiveType( ) )
{
case CSSPrimitiveValue.CSS_CM :
return (int) ( v * 72000 / 2.54 );
case CSSPrimitiveValue.CSS_IN :
return (int) ( v * 72000 );
case CSSPrimitiveValue.CSS_MM :
return (int) ( v * 7200 / 2.54 );
case CSSPrimitiveValue.CSS_PC :
return (int) ( v * 12 * 1000 );
case CSSPrimitiveValue.CSS_PX :
return (int) ( v / dpi * 72000f );
case CSSPrimitiveValue.CSS_PT :
return (int) ( v * 1000 );
case CSSPrimitiveValue.CSS_NUMBER :
return (int) v;
case CSSPrimitiveValue.CSS_PERCENTAGE :
return (int) ( referenceLength * v / 100.0 );
}
}
return 0;
}
public static FloatValue toFloatValue( String value )
{
try
{
Float fValue = new Float( value );
return new FloatValue( CSSPrimitiveValue.CSS_NUMBER, fValue.floatValue( ) );
}
catch( NumberFormatException ex )
{
}
return toFloatValue( DimensionType.parserUnit( value ) );
}
/**
* Returns the Value corresponding to the given system color.
*/
public static CSSValue getSystemColor(String ident) {
ident = ident.toLowerCase();
SystemColor sc = (SystemColor)factories.get(ident);
return new RGBColorValue
(new FloatValue(CSSPrimitiveValue.CSS_NUMBER, sc.getRed()),
new FloatValue(CSSPrimitiveValue.CSS_NUMBER, sc.getGreen()),
new FloatValue(CSSPrimitiveValue.CSS_NUMBER, sc.getBlue()));
}
/**
* Implements {@link ValueManager#createFloatValue(short,float)}.
*/
public Value createFloatValue(short type, float floatValue)
throws DOMException {
if (type == CSSPrimitiveValue.CSS_NUMBER) {
int i = (int) floatValue;
if (floatValue == i) {
switch (i) {
case 100:
return CSSValueConstants.NUMBER_100;
case 200:
return CSSValueConstants.NUMBER_200;
case 300:
return CSSValueConstants.NUMBER_300;
case 400:
return CSSValueConstants.NUMBER_400;
case 500:
return CSSValueConstants.NUMBER_500;
case 600:
return CSSValueConstants.NUMBER_600;
case 700:
return CSSValueConstants.NUMBER_700;
case 800:
return CSSValueConstants.NUMBER_800;
case 900:
return CSSValueConstants.NUMBER_900;
}
}
}
throw createInvalidFloatValueDOMException(floatValue);
}
public static int getDimensionValueConsiderDpi( CSSValue value,
IContent content )
{
if ( value != null && ( value instanceof FloatValue ) )
{
FloatValue fv = (FloatValue) value;
float v = fv.getFloatValue( );
switch ( fv.getPrimitiveType( ) )
{
case CSSPrimitiveValue.CSS_CM :
return (int) ( v * 72000 / 2.54 );
case CSSPrimitiveValue.CSS_IN :
return (int) ( v * 72000 );
case CSSPrimitiveValue.CSS_MM :
return (int) ( v * 7200 / 2.54 );
case CSSPrimitiveValue.CSS_PC :
return (int) ( v * 12 * 1000 );
case CSSPrimitiveValue.CSS_PX :
ReportDesignHandle designHandle = content
.getReportContent( ).getDesign( ).getReportDesign( );
int dpi = designHandle.getImageDPI( );
if ( dpi == 0 )
{
dpi = 96;
}
return (int) ( v / dpi * 72000f );
case CSSPrimitiveValue.CSS_PT :
return (int) ( v * 1000 );
case CSSPrimitiveValue.CSS_NUMBER :
return (int) v;
}
}
return 0;
}
/**
* Implements {@link
* ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
*/
public Value computeValue( CSSStylableElement elt, CSSEngine engine,
int idx, Value value )
{
if ( value.getCssValueType( ) == CSSValue.CSS_PRIMITIVE_VALUE )
{
switch ( value.getPrimitiveType( ) )
{
case CSSPrimitiveValue.CSS_NUMBER :
case CSSPrimitiveValue.CSS_PX :
case CSSPrimitiveValue.CSS_MM :
case CSSPrimitiveValue.CSS_CM :
case CSSPrimitiveValue.CSS_IN :
case CSSPrimitiveValue.CSS_PT :
case CSSPrimitiveValue.CSS_PC :
return value;
case CSSPrimitiveValue.CSS_EMS :
float v = value.getFloatValue( );
Value fontSize = (Value) elt.getComputedStyle( )
.getProperty( IStyle.STYLE_FONT_SIZE );
float fs = fontSize.getFloatValue( );
return new FloatValue( fontSize.getPrimitiveType( ), v * fs );
case CSSPrimitiveValue.CSS_EXS :
v = value.getFloatValue( );
fontSize = (Value) elt.getComputedStyle( )
.getProperty( IStyle.STYLE_FONT_SIZE );
fs = fontSize.getFloatValue( );
return new FloatValue( fontSize.getPrimitiveType( ), v
* fs * 0.5f );
}
}
return value;
}
/**
* Returns the CSS text associated with the given type/value pair.
*/
public static String getCssText( short unit, float value )
{
if ( unit < 0 || unit >= UNITS.length )
{
throw new DOMException( DOMException.SYNTAX_ERR, "" );
}
String s = FORMATTER.format( value );
return s + UNITS[unit - CSSPrimitiveValue.CSS_NUMBER];
}
protected int getDimensionValue( CSSValue value, int referenceLength )
{
if ( value != null && ( value instanceof FloatValue ) )
{
FloatValue fv = (FloatValue) value;
float v = fv.getFloatValue( );
switch ( fv.getPrimitiveType( ) )
{
case CSSPrimitiveValue.CSS_CM :
return (int) ( v * 72000 / 2.54 );
case CSSPrimitiveValue.CSS_IN :
return (int) ( v * 72000 );
case CSSPrimitiveValue.CSS_MM :
return (int) ( v * 7200 / 2.54 );
case CSSPrimitiveValue.CSS_PC :
return (int) ( v * 12 * 1000 );
case CSSPrimitiveValue.CSS_PX :
float dpi = getResolution( );
return (int) ( v / dpi * 72000f );
case CSSPrimitiveValue.CSS_PT :
return (int) ( v * 1000 );
case CSSPrimitiveValue.CSS_NUMBER :
return (int) v;
case CSSPrimitiveValue.CSS_PERCENTAGE :
return (int) ( referenceLength * v / 100.0 );
}
}
return 0;
}
protected int getDimensionValue( CSSValue value, int referenceLength )
{
if ( value != null && ( value instanceof FloatValue ) )
{
FloatValue fv = (FloatValue) value;
float v = fv.getFloatValue( );
switch ( fv.getPrimitiveType( ) )
{
case CSSPrimitiveValue.CSS_CM :
return (int) ( v * 72000 / 2.54 );
case CSSPrimitiveValue.CSS_IN :
return (int) ( v * 72000 );
case CSSPrimitiveValue.CSS_MM :
return (int) ( v * 7200 / 2.54 );
case CSSPrimitiveValue.CSS_PT :
return (int) ( v * 1000 );
case CSSPrimitiveValue.CSS_NUMBER :
return (int) v;
case CSSPrimitiveValue.CSS_PERCENTAGE :
return (int) ( referenceLength * v/100.0 );
}
}
return 0;
}
protected int getDimensionValue( CSSValue value, int referenceLength )
{
if ( value != null && ( value instanceof FloatValue ) )
{
FloatValue fv = (FloatValue) value;
float v = fv.getFloatValue( );
switch ( fv.getPrimitiveType( ) )
{
case CSSPrimitiveValue.CSS_CM :
return (int) ( v * 72000 / 2.54 );
case CSSPrimitiveValue.CSS_IN :
return (int) ( v * 72000 );
case CSSPrimitiveValue.CSS_MM :
return (int) ( v * 7200 / 2.54 );
case CSSPrimitiveValue.CSS_PT :
return (int) ( v * 1000 );
case CSSPrimitiveValue.CSS_NUMBER :
return (int) v;
case CSSPrimitiveValue.CSS_PERCENTAGE :
return (int) ( referenceLength * v / 100.0 );
}
}
return 0;
}
public Value getDefaultValue() {
return new FloatValue( CSSPrimitiveValue.CSS_NUMBER, defaultValue );
}
/**
* Implements {@link
* ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
*/
public Value computeValue(CSSStylableElement elt, CSSEngine engine,
int idx, Value value) {
CSSContext ctx = engine.getCSSContext();
float fs = ctx.getMediumFontSize();
// absolute size
if (value == CSSValueConstants.XX_SMALL_VALUE) {
return new FloatValue( CSSPrimitiveValue.CSS_PT,
fs / 1.2f / 1.2f / 1.2f);
}
if (value == CSSValueConstants.X_SMALL_VALUE) {
return new FloatValue( CSSPrimitiveValue.CSS_PT,
fs / 1.2f / 1.2f);
}
if (value == CSSValueConstants.SMALL_VALUE) {
return new FloatValue( CSSPrimitiveValue.CSS_PT, fs / 1.2f );
}
if (value == CSSValueConstants.MEDIUM_VALUE) {
return new FloatValue( CSSPrimitiveValue.CSS_PT, fs );
}
if (value == CSSValueConstants.LARGE_VALUE) {
return new FloatValue( CSSPrimitiveValue.CSS_PT, fs * 1.2f );
}
if (value == CSSValueConstants.X_LARGE_VALUE) {
return new FloatValue( CSSPrimitiveValue.CSS_PT,
fs * 1.2f * 1.2f);
}
if (value == CSSValueConstants.XX_LARGE_VALUE) {
return new FloatValue( CSSPrimitiveValue.CSS_PT,
fs * 1.2f * 1.2f * 1.2f);
}
float scale = 1.0f;
boolean doParentRelative = false;
// relative size
if (value == CSSValueConstants.SMALLER_VALUE) {
doParentRelative = true;
scale = 1.0f / 1.2f;
} else if (value == CSSValueConstants.LARGER_VALUE) {
doParentRelative = true;
scale = 1.2f;
} else if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
// relative length && percentage
switch (value.getPrimitiveType()) {
case CSSPrimitiveValue.CSS_EMS:
doParentRelative = true;
scale = value.getFloatValue();
break;
case CSSPrimitiveValue.CSS_EXS:
doParentRelative = true;
scale = value.getFloatValue() * 0.5f;
break;
case CSSPrimitiveValue.CSS_PERCENTAGE:
doParentRelative = true;
scale = value.getFloatValue() * 0.01f;
break;
}
}
if (doParentRelative) {
CSSStylableElement parent = (CSSStylableElement) elt.getParent();
if (parent != null) {
IStyle style = parent.getComputedStyle();
if (style != null) {
Value fontSize = (Value)style.getProperty(IStyle.STYLE_FONT_SIZE);
if (fontSize != null) {
fs = fontSize.getFloatValue();
return new FloatValue( fontSize.getPrimitiveType( ), fs * scale );
}
}
}
return new FloatValue( CSSPrimitiveValue.CSS_PT, fs * scale );
}
if(value.getPrimitiveType() == CSSPrimitiveValue.CSS_NUMBER)
{
return super.computeValue( elt, engine, idx, new FloatValue(
CSSPrimitiveValue.CSS_PT,
value.getFloatValue()));
}
return super.computeValue(elt, engine, idx, value);
}
/**
* Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
*/
public Value createValue( LexicalUnit lu, CSSEngine engine )
throws DOMException
{
switch ( lu.getLexicalUnitType( ) )
{
case LexicalUnit.SAC_EM :
return new FloatValue( CSSPrimitiveValue.CSS_EMS, lu
.getFloatValue( ) );
case LexicalUnit.SAC_EX :
return new FloatValue( CSSPrimitiveValue.CSS_EXS, lu
.getFloatValue( ) );
case LexicalUnit.SAC_PIXEL :
return new FloatValue( CSSPrimitiveValue.CSS_PX, lu
.getFloatValue( ) );
case LexicalUnit.SAC_CENTIMETER :
return new FloatValue( CSSPrimitiveValue.CSS_CM, lu
.getFloatValue( ) );
case LexicalUnit.SAC_MILLIMETER :
return new FloatValue( CSSPrimitiveValue.CSS_MM, lu
.getFloatValue( ) );
case LexicalUnit.SAC_INCH :
return new FloatValue( CSSPrimitiveValue.CSS_IN, lu
.getFloatValue( ) );
case LexicalUnit.SAC_POINT :
return new FloatValue( CSSPrimitiveValue.CSS_PT, lu
.getFloatValue( ) );
case LexicalUnit.SAC_PICA :
return new FloatValue( CSSPrimitiveValue.CSS_PC, lu
.getFloatValue( ) );
case LexicalUnit.SAC_INTEGER :
return new FloatValue( CSSPrimitiveValue.CSS_NUMBER, lu
.getIntegerValue( ) );
case LexicalUnit.SAC_REAL :
return new FloatValue( CSSPrimitiveValue.CSS_NUMBER, lu
.getFloatValue( ) );
case LexicalUnit.SAC_PERCENTAGE :
return new FloatValue( CSSPrimitiveValue.CSS_PERCENTAGE, lu
.getFloatValue( ) );
}
throw createInvalidLexicalUnitDOMException( lu.getLexicalUnitType( ) );
}
/**
* Converts the actual float value to the given unit type.
*/
public static float convertFloatValue( short unitType, FloatValue value )
{
switch ( unitType )
{
case CSSPrimitiveValue.CSS_NUMBER :
case CSSPrimitiveValue.CSS_PERCENTAGE :
case CSSPrimitiveValue.CSS_EMS :
case CSSPrimitiveValue.CSS_EXS :
case CSSPrimitiveValue.CSS_DIMENSION :
case CSSPrimitiveValue.CSS_PX :
if ( value.getPrimitiveType( ) == unitType )
{
return value.getFloatValue( );
}
break;
case CSSPrimitiveValue.CSS_CM :
return toCentimeters( unitType, value.getFloatValue( ) );
case CSSPrimitiveValue.CSS_MM :
return toMillimeters( unitType, value.getFloatValue( ) );
case CSSPrimitiveValue.CSS_IN :
return toInches( unitType, value.getFloatValue( ) );
case CSSPrimitiveValue.CSS_PT :
return toPoints( unitType, value.getFloatValue( ) );
case CSSPrimitiveValue.CSS_PC :
return toPicas( unitType, value.getFloatValue( ) );
case CSSPrimitiveValue.CSS_DEG :
return toDegrees( unitType, value.getFloatValue( ) );
case CSSPrimitiveValue.CSS_RAD :
return toRadians( unitType, value.getFloatValue( ) );
case CSSPrimitiveValue.CSS_GRAD :
return toGradians( unitType, value.getFloatValue( ) );
case CSSPrimitiveValue.CSS_MS :
return toMilliseconds( unitType, value.getFloatValue( ) );
case CSSPrimitiveValue.CSS_S :
return toSeconds( unitType, value.getFloatValue( ) );
case CSSPrimitiveValue.CSS_HZ :
return toHertz( unitType, value.getFloatValue( ) );
case CSSPrimitiveValue.CSS_KHZ :
return tokHertz( unitType, value.getFloatValue( ) );
}
throw new DOMException( DOMException.INVALID_ACCESS_ERR, "" );
}