Class CssDimensionParsingUtils


  • public final class CssDimensionParsingUtils
    extends java.lang.Object
    Utilities class for CSS dimension parsing operations.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static org.slf4j.Logger logger  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int determinePositionBetweenValueAndUnit​(java.lang.String string)
      Method used in preparation of splitting a string containing a numeric value with a metric unit (e.g.
      private static boolean isDigit​(char ch)  
      private static boolean isExponentNotation​(java.lang.String s, int index)  
      static float parseAbsoluteFontSize​(java.lang.String fontSizeValue)
      Parses the absolute font size.
      static float parseAbsoluteFontSize​(java.lang.String fontSizeValue, java.lang.String defaultMetric)
      Parses the absolute font size.
      static float parseAbsoluteLength​(java.lang.String length)
      Parses the absolute length.
      static float parseAbsoluteLength​(java.lang.String length, java.lang.String defaultMetric)
      Parses a length with an allowed metric unit (px, pt, in, cm, mm, pc, q) or numeric value (e.g.
      static float parseAngle​(java.lang.String angle)
      Parses a angle with an allowed metric unit (deg, grad, rad) or numeric value (e.g.
      static float parseAngle​(java.lang.String angle, java.lang.String defaultMetric)
      Parses an angle with an allowed metric unit (deg, grad, rad) or numeric value (e.g.
      static int[] parseAspectRatio​(java.lang.String str)
      Parses an aspect ratio into an array with two integers.
      static TransparentColor parseColor​(java.lang.String colorValue)
      Parses either RGBA or CMYK color.
      static java.lang.Double parseDouble​(java.lang.String str)
      Parses a double without throwing an exception if something goes wrong.
      static java.lang.Float parseFlex​(java.lang.String value)
      Parses a flex value "xfr" to x.
      static java.lang.Float parseFloat​(java.lang.String str)
      Parses a float without throwing an exception if something goes wrong.
      static java.lang.Integer parseInteger​(java.lang.String str)
      Parses an integer without throwing an exception if something goes wrong.
      static float parseLength​(java.lang.String length, float percentBaseValue, float defaultValue, float fontSize, float rootFontSize)
      Parse length attributes.
      static UnitValue parseLengthValueToPt​(java.lang.String value, float emValue, float remValue)
      Convenience method for parsing a value to pt.
      static float parseRelativeFontSize​(java.lang.String relativeFontSizeValue, float baseValue)
      Parses the relative font size.
      static float parseRelativeValue​(java.lang.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.
      static float parseResolution​(java.lang.String resolutionStr)
      Parses the resolution.
      static float[] parseRgbaColor​(java.lang.String colorValue)
      Parses the RGBA color.
      static UnitValue[] parseSpecificCornerBorderRadius​(java.lang.String specificBorderRadius, float emValue, float remValue)
      Parses the border radius of specific corner.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • logger

        private static final org.slf4j.Logger logger
    • Constructor Detail

    • Method Detail

      • parseInteger

        public static java.lang.Integer parseInteger​(java.lang.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 java.lang.Float parseFloat​(java.lang.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 java.lang.Double parseDouble​(java.lang.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​(java.lang.String angle,
                                       java.lang.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​(java.lang.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​(java.lang.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​(java.lang.String length,
                                                java.lang.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​(java.lang.String length)
        Parses the absolute length.
        Parameters:
        length - the length as a string
        Returns:
        the length as a float
      • parseRelativeValue

        public static float parseRelativeValue​(java.lang.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​(java.lang.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 java.lang.Float parseFlex​(java.lang.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​(java.lang.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​(java.lang.String fontSizeValue,
                                                  java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.String resolutionStr)
        Parses the resolution.
        Parameters:
        resolutionStr - the resolution as a string
        Returns:
        a value in dpi
      • parseColor

        public static TransparentColor parseColor​(java.lang.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​(java.lang.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​(java.lang.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.
      • isDigit

        private static boolean isDigit​(char ch)
      • isExponentNotation

        private static boolean isExponentNotation​(java.lang.String s,
                                                  int index)