Package com.itextpdf.tool.xml.css
Class CssUtils
java.lang.Object
com.itextpdf.tool.xml.css.CssUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
private static final String
private static final String
private static final String
private static final String
private static final String
static final int
Default font size if none is set.private static CssUtils
private static final String
private static final String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfloat
calculateMarginTop
(float value, MarginMemory configuration) Calculates the margin top or spacingBefore based on the given value and the last margin bottom.float
calculateMarginTop
(String value, float largestFont, MarginMemory configuration) Calculates the margin top or spacingBefore based on the given value and the last margin bottom.float
checkMetricStyle
(Tag t, String style) Use only if value of style is a metric value (isMetricValue(String)
) or a numeric value in pixels (isNumericValue(String)
).
Checks if the style is present in the css of the tag, then parses it to pt.checkMetricStyle
(Map<String, String> css, String style) Use only if value of style is a metric value (isMetricValue(String)
) or a numeric value in pixels (isNumericValue(String)
).
Checks if the style is present in the css of the tag, then parses it to pt.int
Method used in preparation of splitting a string containing a numeric value with a metric unit (e.g.extractUrl
(String url) Parsesurl("file.jpg")
tofile.jpg
.static CssUtils
float
getLeftAndRightMargin
(Tag t, float pageWidth) Returns the sum of the left and right margin of a tag.boolean
isMetricValue
(String value) Checks whether a string contains an allowed metric unit in HTML/CSS; px, in, cm, mm, pc or pt.boolean
isNumericValue
(String value) Checks whether a string matches a numeric value (e.g.boolean
isRelativeValue
(String value) Checks whether a string contains an allowed value relative to previously set value.parseBorder
(String border) parseBorder
(String border, String borderKey) parseBoxValues
(String box, String pre, String post) Returns the top, bottom, left, right version for the given box.parseBoxValues
(String box, String pre, String post, String preKey) float
parsePxInCmMmPcToPt
(String length) Parses a length with an allowed metric unit (px, pt, in, cm, mm, pc, em or ex) or numeric value (e.g.float
parsePxInCmMmPcToPt
(String length, String defaultMetric) Parses a length with an allowed metric unit (px, pt, in, cm, mm, pc, em or ex) or numeric value (e.g.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.float
parseValueToPt
(String value, float baseValue) Convenience method for parsing a value to pt if a value can contain:
a numeric value in pixels (e.g.processBackground
(String background) Preparation before implementing the background style in iText.processFont
(String font) Preparation before implementing the font style in iText.processListStyle
(String listStyle) Preparation before implementing the list style in iText.String[]
Trims and Strips double spaces from the given string.Trims a string and removes surrounding " or '.float
validateTextHeight
(Map<String, String> css, float textHeight) Validates a given textHeight based on the content of a tag against the css styles "min-height" and "max-height" of the tag if present.
-
Field Details
-
COLOR
- See Also:
-
STYLE
- See Also:
-
WIDTH
- See Also:
-
BORDER2
- See Also:
-
_0_LEFT_1
- See Also:
-
_0_RIGHT_1
- See Also:
-
_0_BOTTOM_1
- See Also:
-
_0_TOP_1
- See Also:
-
instance
-
DEFAULT_FONT_SIZE_PT
public static final int DEFAULT_FONT_SIZE_PTDefault font size if none is set.- See Also:
-
borderwidth
-
borderstyle
-
backgroundPositions
-
-
Constructor Details
-
CssUtils
private CssUtils()
-
-
Method Details
-
getInstance
- Returns:
- Singleton instance of CssUtils.
-
parseBoxValues
Returns the top, bottom, left, right version for the given box. the keys will be the pre value concatenated with either top, bottom, right or left and the post value. Note: Does not work when double spaces are in the boxes value. (Tip: UsestripDoubleSpacesAndTrim(String)
)- Parameters:
box
- the value to parsepre
- the pre key partpost
- the post key part- Returns:
- a map with the parsed properties
-
parseBoxValues
-
parseBorder
- Parameters:
border
- the border property- Returns:
- a map of the border property parsed to each property (width, style, color).
-
parseBorder
-
stripDoubleSpacesAndTrim
Trims and Strips double spaces from the given string.- Parameters:
str
- the string to strip- Returns:
- the string without double spaces
-
stripDoubleSpacesTrimAndToLowerCase
-
processBackground
Preparation before implementing the background style in iText. Splits the given background style and its attributes into background-color, background-image, background-repeat, background-attachment, background-position and css styles.- Parameters:
background
- the string containing the font style value.- Returns:
- a map with the values of font parsed into each css property.
-
processListStyle
Preparation before implementing the list style in iText. Splits the given list style and its attributes into list-style-type, list-style-position and list-style-image.- Parameters:
listStyle
- the string containing the list style value.- Returns:
- a map with the values of the parsed list style into each css property.
-
processFont
Preparation before implementing the font style in iText. Splits the given font style and its attributes into font-size, line-height, font-weight, font-style, font-variant and font-family css styles.- Parameters:
font
- the string containing the font style value.- Returns:
- a map with the values of the parsed font into each css property.
-
checkMetricStyle
Use only if value of style is a metric value (isMetricValue(String)
) or a numeric value in pixels (isNumericValue(String)
).
Checks if the style is present in the css of the tag, then parses it to pt. and returns the parsed value.- Parameters:
t
- the tag which needs to be checked.style
- the style which needs to be checked.- Returns:
- float the parsed value of the style or 0f if the value was invalid.
-
checkMetricStyle
Use only if value of style is a metric value (isMetricValue(String)
) or a numeric value in pixels (isNumericValue(String)
).
Checks if the style is present in the css of the tag, then parses it to pt. and returns the parsed value.- Parameters:
css
- the map of css styles which needs to be checked.style
- the style which needs to be checked.- Returns:
- float the parsed value of the style or 0f if the value was invalid.
-
isMetricValue
Checks whether a string contains an allowed metric unit in HTML/CSS; px, in, cm, mm, pc or pt.- Parameters:
value
- the string that needs to be checked.- Returns:
- boolean true if value contains an allowed metric value.
-
isRelativeValue
Checks whether a string contains an allowed value relative to previously set value.- Parameters:
value
- the string that needs to be checked.- Returns:
- boolean true if value contains an allowed metric value.
-
isNumericValue
Checks whether a string matches a numeric value (e.g. 123, 1.23, .123). All these metric values are allowed in HTML/CSS.- Parameters:
value
- the string that needs to be checked.- Returns:
- boolean true if value contains an allowed metric value.
-
parseValueToPt
Convenience method for parsing a value to pt if a value can contain:
- 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 string containing the value to be parsed.baseValue
- float needed for the calculation of the relative value.- Returns:
- parsedValue float containing the parsed value in pt.
-
parseRelativeValue
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.
-
parsePxInCmMmPcToPt
Parses a length with an allowed metric unit (px, pt, in, cm, mm, pc, em or ex) 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
-
parsePxInCmMmPcToPt
Parses a length with an allowed metric unit (px, pt, in, cm, mm, pc, em or ex) or numeric value (e.g. 123, 1.23, .123) to pt.
A numeric value is considered to be in px as is default in HTML/CSS.- Parameters:
length
- the string containing the length.- Returns:
- float the parsed length in pt.
-
determinePositionBetweenValueAndUnit
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.
-
getLeftAndRightMargin
Returns the sum of the left and right margin of a tag.- Parameters:
t
- the tag of which the total horizontal margin is needed.pageWidth
- the page width- Returns:
- float the total horizontal margin.
-
extractUrl
Parsesurl("file.jpg")
tofile.jpg
.- Parameters:
url
- the url attribute to parse- Returns:
- the parsed url. Or original url if not wrappend in url()
-
validateTextHeight
Validates a given textHeight based on the content of a tag against the css styles "min-height" and "max-height" of the tag if present.- Parameters:
css
- the styles of a tagtextHeight
- the current textHeight based on the content of a tag- Returns:
- the final text height of an element.
-
calculateMarginTop
Calculates the margin top or spacingBefore based on the given value and the last margin bottom.
In HTML the margin-bottom of a tag overlaps with the margin-top of a following tag. This method simulates this behavior by subtracting the margin-top value of the given tag from the margin-bottom of the previous tag. The remaining value is returned or if the margin-bottom value is the largest, 0 is returned- Parameters:
value
- the margin-top value of the given tag.largestFont
- used if a relative value was given to calculate margin.configuration
- XmlWorkerConfig containing the last margin bottom.- Returns:
- an offset
-
calculateMarginTop
Calculates the margin top or spacingBefore based on the given value and the last margin bottom.
In HTML the margin-bottom of a tag overlaps with the margin-top of a following tag. This method simulates this behavior by subtracting the margin-top value of the given tag from the margin-bottom of the previous tag. The remaining value is returned or if the margin-bottom value is the largest, 0 is returned- Parameters:
value
- float containing the margin-top value.configuration
- XmlWorkerConfig containing the last margin bottom.- Returns:
- an offset
-
trimAndRemoveQuoutes
Trims a string and removes surrounding " or '.- Parameters:
s
- the string- Returns:
- trimmed and unquoted string
-
splitComplexCssStyle
-