/** * Merges two dimension values with the given dpi value. If these two dimension values are not in * the same unit, only dimension values in absolute units and pixels can be merged. The unit of * the merged result will be according to the first dimension value except its unit is pixel.If * one of them is null, the other value will be returned. * * @param dimension1 the first dimension value to merge * @param dimension2 the second dimension value to merge * @param dpi the dpi value * @return the merged dimension value, or null if these two dimension value cannot be merged or * both of them are null. */ public static DimensionValue mergeDimension( DimensionValue dimension1, DimensionValue dimension2, int dpi) { if (dimension1 == null || dimension2 == null) { if (dimension1 == null) { return dimension2; } return dimension1; } String unit = dimension1.getUnits(); String unit2 = dimension2.getUnits(); Double meature = null; if (unit.equalsIgnoreCase(unit2)) { meature = dimension1.getMeasure() + dimension2.getMeasure(); } else if (isAbsoluteUnit(unit)) { if (isAbsoluteUnit(unit2)) { meature = dimension1.getMeasure() + convertTo(dimension2, null, unit).getMeasure(); } else if (DesignChoiceConstants.UNITS_PX.equalsIgnoreCase(unit2)) { meature = dimension1.getMeasure() + convertTo(dimension2, null, unit, 0, validateDPI(dpi)); } } else if (DesignChoiceConstants.UNITS_PX.equalsIgnoreCase(unit) && isAbsoluteUnit(unit2)) { meature = convertTo(dimension1, null, unit2, 0, validateDPI(dpi)) + dimension2.getMeasure(); unit = unit2; } if (meature != null) return new DimensionValue(meature, unit); return null; }
/** * Return if the given unit is a relative unit or not. The following units defined in <code> * DesignChoiceConstants</code> are considered as relative: * * <ul> * <li>UNITS_EM * <li>UNITS_EX * <li>UNITS_PERCENTAGE * <li>UNITS_PX * </ul> * * @param unit a given unit. * @return <code>true</code> if the unit is a relative unit like em, ex, % and px. Return <code> * false</code> if the unit is not a relative unit.( it can be an absolute relative unit like * "mm", or even an unrecognized unit. ) */ public static final boolean isRelativeUnit(String unit) { return DesignChoiceConstants.UNITS_EM.equalsIgnoreCase(unit) || DesignChoiceConstants.UNITS_EX.equalsIgnoreCase(unit) || DesignChoiceConstants.UNITS_PERCENTAGE.equalsIgnoreCase(unit) || DesignChoiceConstants.UNITS_PX.equalsIgnoreCase(unit); }