Class CssDimensionParsingUtils

java.lang.Object
com.itextpdf.styledxmlparser.css.util.CssDimensionParsingUtils

public final class CssDimensionParsingUtils extends Object
Utilities class for CSS dimension parsing operations.
  • Method Details

    • parseInteger

      public static Integer parseInteger (String str)
      Parses an integer without throwing an exception if something goes wrong.
      Parameters:
      str - a string that might be an integer value
      Returns:
      the integer value, or null if something went wrong
    • parseFloat

      public static Float parseFloat (String str)
      Parses a float without throwing an exception if something goes wrong.
      Parameters:
      str - a string that might be a float value
      Returns:
      the float value, or null if something went wrong
    • parseDouble

      public static Double parseDouble (String str)
      Parses a double without throwing an exception if something goes wrong.
      Parameters:
      str - a string that might be a double value
      Returns:
      the double value, or null if something went wrong
    • parseAngle

      public static float parseAngle (String angle, String defaultMetric)
      Parses an angle with an allowed metric unit (deg, grad, rad) or numeric value (e.g. 123, 1.23, .123) to rad.
      Parameters:
      angle - String containing the angle to parse
      defaultMetric - default metric to use in case the input string does not specify a metric
      Returns:
      the angle in radians
    • parseAngle

      public static float parseAngle (String angle)
      Parses a angle with an allowed metric unit (deg, grad, rad) or numeric value (e.g. 123, 1.23, .123) to rad. Default metric is degrees
      Parameters:
      angle - String containing the angle to parse
      Returns:
      the angle in radians
    • parseAspectRatio

      public static int[] parseAspectRatio (String str)
      Parses an aspect ratio into an array with two integers.
      Parameters:
      str - a string that might contain two integer values
      Returns:
      the aspect ratio as an array of two integer values
    • parseAbsoluteLength

      public static float parseAbsoluteLength (String length, String defaultMetric)
      Parses a length with an allowed metric unit (px, pt, in, cm, mm, pc, q) or numeric value (e.g. 123, 1.23, .123) to pt.
      A numeric value (without px, pt, etc in the given length string) is considered to be in the default metric that was given.
      Parameters:
      length - the string containing the length
      defaultMetric - the string containing the metric if it is possible that the length string does not contain one. If null the length is considered to be in px as is default in HTML/CSS
      Returns:
      parsed value
    • parseAbsoluteLength

      public static float parseAbsoluteLength (String length)
      Parses the absolute length.
      Parameters:
      length - the length as a string
      Returns:
      the length as a float
    • parseRelativeValue

      public static float parseRelativeValue (String relativeValue, float baseValue)
      Parses an relative value based on the base value that was given, in the metric unit of the base value.
      (e.g. margin=10% should be based on the page width, so if an A4 is used, the margin = 0.10*595.0 = 59.5f)
      Parameters:
      relativeValue - in %, em or ex
      baseValue - the value the returned float is based on
      Returns:
      the parsed float in the metric unit of the base value
    • parseLengthValueToPt

      public static UnitValue parseLengthValueToPt (String value, float emValue, float remValue)
      Convenience method for parsing a value to pt. Possible values are:
      • a numeric value in pixels (e.g. 123, 1.23, .123),
      • a value with a metric unit (px, in, cm, mm, pc or pt) attached to it,
      • or a value with a relative value (%, em, ex).
      Parameters:
      value - the value
      emValue - the em value
      remValue - the root em value
      Returns:
      the unit value
    • parseFlex

      public static Float parseFlex (String value)
      Parses a flex value "xfr" to x.
      Parameters:
      value - String containing the flex value to parse
      Returns:
      the flex value as a float
    • parseLength

      public static float parseLength (String length, float percentBaseValue, float defaultValue, float fontSize, float rootFontSize)
      Parse length attributes.
      Parameters:
      length - String for parsing
      percentBaseValue - the value on which percent length is based on
      defaultValue - default value if length is not recognized
      fontSize - font size of the current element
      rootFontSize - root element font size
      Returns:
      absolute value in points
    • parseAbsoluteFontSize

      public static float parseAbsoluteFontSize (String fontSizeValue, String defaultMetric)
      Parses the absolute font size.

      A numeric value (without px, pt, etc in the given length string) is considered to be in the default metric that was given.

      Parameters:
      fontSizeValue - the font size value as a String
      defaultMetric - the string containing the metric if it is possible that the length string does not contain one. If null the length is considered to be in px as is default in HTML/CSS.
      Returns:
      the font size value as a float
    • parseAbsoluteFontSize

      public static float parseAbsoluteFontSize (String fontSizeValue)
      Parses the absolute font size.

      A numeric value (without px, pt, etc in the given length string) is considered to be in the px.

      Parameters:
      fontSizeValue - the font size value as a String
      Returns:
      the font size value as a float
    • parseRelativeFontSize

      public static float parseRelativeFontSize (String relativeFontSizeValue, float baseValue)
      Parses the relative font size.
      Parameters:
      relativeFontSizeValue - the relative font size value as a String
      baseValue - the base value
      Returns:
      the relative font size value as a float
    • parseSpecificCornerBorderRadius

      public static UnitValue[] parseSpecificCornerBorderRadius (String specificBorderRadius, float emValue, float remValue)
      Parses the border radius of specific corner.
      Parameters:
      specificBorderRadius - string that defines the border radius of specific corner
      emValue - the em value
      remValue - the root em value
      Returns:
      an array of UnitValues that define horizontal and vertical border radius values
    • parseResolution

      public static float parseResolution (String resolutionStr)
      Parses the resolution.
      Parameters:
      resolutionStr - the resolution as a string
      Returns:
      a value in dpi
    • parseColor

      public static TransparentColor parseColor (String colorValue)
      Parses either RGBA or CMYK color.
      Parameters:
      colorValue - the color value
      Returns:
      an RGBA or CMYK value expressed as an array with four float values
    • parseRgbaColor

      public static float[] parseRgbaColor (String colorValue)
      Parses the RGBA color.
      Parameters:
      colorValue - the color value
      Returns:
      an RGBA value expressed as an array with four float values
    • determinePositionBetweenValueAndUnit

      public static int determinePositionBetweenValueAndUnit (String string)
      Method used in preparation of splitting a string containing a numeric value with a metric unit (e.g. 18px, 9pt, 6cm, etc).

      Determines the position between digits and affiliated characters ('+','-','0-9' and '.') and all other characters.
      e.g. string "16px" will return 2, string "0.5em" will return 3 and string '-8.5mm' will return 4.
      Parameters:
      string - containing a numeric value with a metric unit
      Returns:
      int position between the numeric value and unit or 0 if string is null or string started with a non-numeric value.