Esempio n. 1
0
  /**
   * Convert a <code>DimensionValue</code> from one units to another, The conversion is between
   * absolute the units should be one of the absolute units(CM, IN, MM, PT, PC).
   *
   * @param dimension the numeric measure of the dimension.
   * @param appUnit the application unit of the dimension, if the dimension has not specified a
   *     unit, the the application unit will be applied to it. It must be one of the absolute unit.
   * @param targetUnits the desired unit.
   * @return <code>DimensionValue</code> in the target unit.
   */
  public static DimensionValue convertTo(
      DimensionValue dimension, String appUnit, String targetUnits) {
    String fromUnit = dimension.getUnits();
    if (DimensionValue.DEFAULT_UNIT.equalsIgnoreCase(fromUnit)) fromUnit = appUnit;

    return convertTo(dimension.getMeasure(), fromUnit, targetUnits);
  }
Esempio n. 2
0
  /**
   * Convert a measure from one units to another. The application units, target units and base size
   * units must be one of the absolute units(CM, IN, MM, PT, PC). The input dimension value must be
   * one of the following types:
   *
   * <ul>
   *   <li><code>String</code>. It must be a legal dimension value, measure part and units part such
   *       as '10 em', '+3.5pt', '10%' or only measure part, such as 10.12, 45, +4.
   *   <li><code>DimensionValue</code>
   *   <li><code>DimensionHandle</code>
   * </ul>
   *
   * @param value the input dimension value to be converted
   * @param appUnits the application units, used as the original units to convert from when units
   *     part of the input value is empty or null.It must be one of the absolute unit(CM, IN, MM,
   *     PT, PC).
   * @param targetUnits the desired units, it must be one of the absolute unit(CM, IN, MM, PT, PC).
   * @param baseSize the base size to convert value with relative units, such as em, ex and %
   * @param baseSizeUnits the units for the base size. It must be one of the absolute units(CM, IN,
   *     MM, PT, PC). By default it is <code>DesignChoiceConstants.UNITS_PT</code>
   * @param dpi int value that represents the pixel per inch
   * @return double value in the target unit.
   */
  public static double convertTo(
      Object value,
      String appUnits,
      String targetUnits,
      double baseSize,
      String baseSizeUnits,
      int dpi) {
    if (value == null) return 0.0;

    double measure = 0.0;
    String fromUnits = ""; // $NON-NLS-1$

    // get the measure and unit from the value

    if (value instanceof String) {
      try {
        DimensionValue parsedValue = DimensionValue.parse((String) value);
        // the value can not be null
        measure = parsedValue.getMeasure();
        fromUnits = parsedValue.getUnits();
      } catch (PropertyValueException e) {
        // TODO: support the font-size choices?
        throw new IllegalArgumentException(
            "Given string is not well-formatted dimension!"); //$NON-NLS-1$
      }
    } else if (value instanceof DimensionValue) {
      DimensionValue parsedValue = (DimensionValue) value;
      measure = parsedValue.getMeasure();
      fromUnits = parsedValue.getUnits();
    } else if (value instanceof DimensionHandle) {
      DimensionHandle dimensionHandle = (DimensionHandle) value;
      measure = dimensionHandle.getMeasure();
      fromUnits = dimensionHandle.getUnits();
      if (StringUtil.isBlank(fromUnits)) fromUnits = dimensionHandle.getDefaultUnit();
    }
    // not supported value format
    else
      throw new IllegalArgumentException(
          "Given dimension value is a not supported format!"); //$NON-NLS-1$

    // if units is null or empty, set it to application unit
    if (StringUtil.isBlank(fromUnits)) fromUnits = appUnits;

    // if baseSizeUnit is empty or null, set it to 'pt'
    if (StringUtil.isBlank(baseSizeUnits)) baseSizeUnits = DesignChoiceConstants.UNITS_PT;

    DimensionValue convertedValue = null;
    // do some prepare for the relative units
    if (DesignChoiceConstants.UNITS_EM.equals(fromUnits)) {
      convertedValue = DimensionUtil.convertTo(measure * baseSize, baseSizeUnits, targetUnits);
    } else if (DesignChoiceConstants.UNITS_EX.equals(fromUnits)) {
      convertedValue = DimensionUtil.convertTo(measure * baseSize / 3, baseSizeUnits, targetUnits);
    } else if (DesignChoiceConstants.UNITS_PERCENTAGE.equals(fromUnits)) {
      convertedValue =
          DimensionUtil.convertTo(measure * baseSize / 100, baseSizeUnits, targetUnits);
    } else if (DesignChoiceConstants.UNITS_PX.equals(fromUnits)) {
      convertedValue = convertTo(measure / dpi, DesignChoiceConstants.UNITS_IN, targetUnits);
    } else convertedValue = convertTo(measure, fromUnits, targetUnits);
    return convertedValue.getMeasure();
  }
Esempio n. 3
0
 /**
  * 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;
 }
Esempio n. 4
0
  /**
   * Convert a dimension from one units to another, the dimension like "12pt, 12cm" is composed of
   * two parts: "measure" and "units". The conversion is between absolute the units should be one of
   * the absolute units(CM, IN, MM, PT, PC).
   *
   * @param dimension a string representing a absolute dimension value like "12pt, 12pc...".
   * @param appUnit the application unit of the dimension, if the dimension has not specified a
   *     unit, the the application unit will be applied to it. It must be one of the absolute unit.
   * @param targetUnits the desired unit.
   * @return <code>DimensionValue</code> in the target unit.
   * @throws PropertyValueException if the dimension is not valid.
   */
  public static DimensionValue convertTo(String dimension, String appUnit, String targetUnits)
      throws PropertyValueException {

    DimensionValue dim = DimensionValue.parse(dimension);
    return convertTo(dim, appUnit, targetUnits);
  }