kerry
7 years ago
9 changed files with 400 additions and 45 deletions
@ -0,0 +1,284 @@
|
||||
package com.fr.third.v2.lowagie.text.html; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* @author kerry |
||||
* @date 2018/4/9 |
||||
*/ |
||||
public class CSSUtils { |
||||
|
||||
public class CSS{ |
||||
public final class Value { |
||||
private Value(){}; |
||||
public static final String THIN = "thin"; |
||||
public static final String MEDIUM = "medium"; |
||||
public static final String THICK = "thick"; |
||||
public static final String NONE = "none"; |
||||
public static final String HIDDEN = "hidden"; |
||||
public static final String DOTTED = "dotted"; |
||||
public static final String DASHED = "dashed"; |
||||
public static final String SOLID = "solid"; |
||||
public static final String DOUBLE = "double"; |
||||
public static final String GROOVE = "groove"; |
||||
public static final String RIDGE = "ridge"; |
||||
public static final String INSET = "inset"; |
||||
public static final String OUTSET = "outset"; |
||||
public static final String LEFT = "left"; |
||||
public static final String CENTER = "center"; |
||||
public static final String JUSTIFY = "justify"; |
||||
public static final String BOTTOM = "bottom"; |
||||
public static final String TOP = "top"; |
||||
public static final String RIGHT = "right"; |
||||
public static final String REPEAT = "repeat"; |
||||
public static final String NO_REPEAT = "no-repeat"; |
||||
public static final String REPEAT_X = "repeat-x"; |
||||
public static final String REPEAT_Y = "repeat-y"; |
||||
public static final String FIXED = "fixed"; |
||||
public static final String SCROLL = "scroll"; |
||||
public static final String DISC = "disc"; |
||||
public static final String SQUARE = "square"; |
||||
public static final String CIRCLE = "circle"; |
||||
public static final String DECIMAL = "decimal"; |
||||
public static final String LOWER_ROMAN = "lower-roman"; |
||||
public static final String UPPER_ROMAN = "upper-roman"; |
||||
public static final String LOWER_GREEK = "lower-greek"; |
||||
public static final String UPPER_GREEK = "upper-greek"; |
||||
public static final String LOWER_ALPHA = "lower-alpha"; |
||||
public static final String UPPER_ALPHA = "upper-alpha"; |
||||
public static final String LOWER_LATIN = "lower-latin"; |
||||
public static final String UPPER_LATIN = "upper-latin"; |
||||
public static final String INSIDE = "inside"; |
||||
public static final String OUTSIDE = "outside"; |
||||
public static final String INHERIT = "inherit"; |
||||
public static final String UNDERLINE = "underline"; |
||||
public static final String BOLD = "bold"; |
||||
public static final String ITALIC = "italic"; |
||||
public static final String OBLIQUE = "oblique"; |
||||
public static final String SUPER = "super"; |
||||
public static final String SUB = "sub"; |
||||
public static final String TEXT_TOP = "text-top"; |
||||
public static final String TEXT_BOTTOM = "text-bottom"; |
||||
public static final String LINE_THROUGH = "line-through"; |
||||
public static final String RELATIVE = "relative"; |
||||
public static final String HIDE = "hide"; |
||||
public static final String XX_SMALL = "xx-small"; |
||||
public static final String X_SMALL = "x-small"; |
||||
public static final String SMALL = "small"; |
||||
public static final String LARGE = "large"; |
||||
public static final String X_LARGE = "x-large"; |
||||
public static final String XX_LARGE = "xx-large"; |
||||
public static final String SMALLER = "smaller"; |
||||
public static final String LARGER = "larger"; |
||||
public static final String PX = "px"; |
||||
public static final String IN = "in"; |
||||
public static final String CM = "cm"; |
||||
public static final String MM = "mm"; |
||||
public static final String PT = "pt"; |
||||
public static final String PC = "pc"; |
||||
public static final String PERCENTAGE = "%"; |
||||
public static final String EM = "em"; |
||||
public static final String EX = "ex"; |
||||
public static final String ALWAYS = "always"; |
||||
public static final String AVOID = "avoid"; |
||||
public static final String ABSOLUTE = "absolute"; |
||||
public static final String AUTO = "auto"; |
||||
public static final String INLINE = "inline"; |
||||
public static final String BLOCK = "block"; |
||||
public static final String SEPARATE = "separate"; |
||||
public static final String COLLAPSE = "collapse"; |
||||
public static final String RTL = "rtl"; |
||||
public static final String LTR = "ltr"; |
||||
public static final String INLINE_BLOCK = "inline-block"; |
||||
public static final String INLINE_TABLE = "inline-table"; |
||||
public static final String LIST_ITEM = "list-item"; |
||||
public static final String RUN_IN = "run-in"; |
||||
public static final String TABLE = "table"; |
||||
public static final String TABLE_CAPTION = "table-caption"; |
||||
public static final String TABLE_CELL = "table-cell"; |
||||
public static final String TABLE_COLUMN_GROUP = "table-column-group"; |
||||
public static final String TABLE_COLUMN = "table-column"; |
||||
public static final String TABLE_FOOTER_GROUP = "table-footer-group"; |
||||
public static final String TABLE_HEADER_GROUP = "table-header-group"; |
||||
public static final String TABLE_ROW = "table-row"; |
||||
public static final String TABLE_ROW_GROUP = "table-row-group"; |
||||
} |
||||
|
||||
public final class Property { |
||||
|
||||
private Property() {}; |
||||
public static final String BACKGROUND = "background"; |
||||
public static final String BACKGROUND_IMAGE = "background-image"; |
||||
public static final String BACKGROUND_REPEAT = "background-repeat"; |
||||
public static final String BACKGROUND_ATTACHMENT = "background-attachment"; |
||||
public static final String BACKGROUND_POSITION = "background-position"; |
||||
public static final String BACKGROUND_COLOR = "background-color"; |
||||
public static final String LIST_STYLE = "list-style"; |
||||
public static final String LIST_STYLE_TYPE = "list-style-type"; |
||||
public static final String LIST_STYLE_POSITION = "list-style-position"; |
||||
public static final String LIST_STYLE_IMAGE = "list-style-image"; |
||||
public static final String MARGIN = "margin"; |
||||
public static final String TOP = "top"; |
||||
public static final String MARGIN_LEFT = "margin-left"; |
||||
public static final String MARGIN_RIGHT = "margin-right"; |
||||
public static final String MARGIN_TOP = "margin-top"; |
||||
public static final String MARGIN_BOTTOM = "margin-bottom"; |
||||
public static final String BORDER = "border"; |
||||
public static final String BORDER_LEFT = "border-left"; |
||||
public static final String BORDER_TOP = "border-top"; |
||||
public static final String BORDER_RIGHT = "border-right"; |
||||
public static final String BORDER_BOTTOM = "border-bottom"; |
||||
public static final String BORDER_WIDTH = "border-width"; |
||||
public static final String BORDER_STYLE = "border-style"; |
||||
public static final String BORDER_COLOR = "border-color"; |
||||
public static final String BORDER_COLLAPSE = "border-collapse"; |
||||
public static final String BORDER_SPACING = "border-spacing"; |
||||
public static final String BORDER_TOP_WIDTH = "border-top-width"; |
||||
public static final String BORDER_BOTTOM_WIDTH = "border-bottom-width"; |
||||
public static final String BORDER_LEFT_WIDTH = "border-left-width"; |
||||
public static final String BORDER_RIGHT_WIDTH = "border-right-width"; |
||||
public static final String BORDER_TOP_COLOR = "border-top-color"; |
||||
public static final String BORDER_BOTTOM_COLOR = "border-bottom-color"; |
||||
public static final String BORDER_LEFT_COLOR = "border-left-color"; |
||||
public static final String BORDER_RIGHT_COLOR = "border-right-color"; |
||||
public static final String BORDER_TOP_STYLE = "border-top-style"; |
||||
public static final String BORDER_BOTTOM_STYLE = "border-bottom-style"; |
||||
public static final String BORDER_LEFT_STYLE = "border-left-style"; |
||||
public static final String BORDER_RIGHT_STYLE = "border-right-style"; |
||||
public static final String PADDING = "padding"; |
||||
public static final String PADDING_TOP = "padding-top"; |
||||
public static final String PADDING_BOTTOM = "padding-bottom"; |
||||
public static final String PADDING_LEFT = "padding-left"; |
||||
public static final String PADDING_RIGHT = "padding-right"; |
||||
public static final String FONT = "font"; |
||||
public static final String FONT_WEIGHT = "font-weight"; |
||||
public static final String FONT_SIZE = "font-size"; |
||||
public static final String FONT_STYLE = "font-style"; |
||||
public static final String FONT_FAMILY = "font-family"; |
||||
public static final String TEXT_DECORATION = "text-decoration"; |
||||
public static final String COLOR = "color"; |
||||
public static final String TAB_INTERVAL = "tab-interval"; |
||||
public static final String XFA_TAB_COUNT = "xfa-tab-count"; |
||||
public static final String XFA_FONT_HORIZONTAL_SCALE = "xfa-font-horizontal-scale"; |
||||
public static final String XFA_FONT_VERTICAL_SCALE = "xfa-font-vertical-scale"; |
||||
public static final String BEFORE = "before"; |
||||
public static final String AFTER = "after"; |
||||
public static final String HEIGHT = "height"; |
||||
public static final String WIDTH = "width"; |
||||
public static final String LETTER_SPACING = "letter-spacing"; |
||||
public static final String VERTICAL_ALIGN = "vertical-align"; |
||||
public static final String LINE_HEIGHT = "line-height"; |
||||
public static final String TEXT_ALIGN = "text-align"; |
||||
public static final String TEXT_VALIGN = "text-valign"; |
||||
public static final String TEXT_INDENT = "text-indent"; |
||||
public static final String POSITION = "position"; |
||||
public static final String EMPTY_CELLS = "empty-cells"; |
||||
public static final String CELLPADDING = "cellpadding"; |
||||
//deprecated
|
||||
public static final String CELLPADDING_LEFT = "cellpadding-left"; |
||||
public static final String CELLPADDING_TOP = "cellpadding-top"; |
||||
public static final String CELLPADDING_RIGHT = "cellpadding-right"; |
||||
public static final String CELLPADDING_BOTTOM = "cellpadding-bottom"; |
||||
|
||||
public static final String CAPTION_SIDE = "caption-side"; |
||||
public static final String TAB_STOPS = "tab-stops"; |
||||
public static final String XFA_TAB_STOPS = "xfa-tab-stops"; |
||||
public static final String PAGE_BREAK_BEFORE = "page-break-before"; |
||||
public static final String PAGE_BREAK_INSIDE = "page-break-inside"; |
||||
public static final String PAGE_BREAK_AFTER = "page-break-after"; |
||||
public static final String REPEAT_HEADER = "repeat-header"; |
||||
public static final String REPEAT_FOOTER = "repeat-footer"; |
||||
public static final String LEFT = "left"; |
||||
public static final String DISPLAY = "display"; |
||||
public static final String MIN_WIDTH = "min-width"; |
||||
public static final String MAX_WIDTH = "max-width"; |
||||
public static final String MIN_HEIGHT = "min-height"; |
||||
public static final String MAX_HEIGHT = "max-height"; |
||||
public static final String RIGHT = "right"; |
||||
public static final String BOTTOM = "bottom"; |
||||
public static final String FLOAT = "float"; |
||||
public static final String DIRECTION = "direction"; |
||||
} |
||||
|
||||
} |
||||
|
||||
private static final Set<String> backgroundPositions = new HashSet<String>( |
||||
Arrays.asList(new String[] { CSS.Value.LEFT, CSS.Value.CENTER, CSS.Value.BOTTOM, CSS.Value.TOP, CSS.Value.RIGHT })); |
||||
|
||||
public static Map<String, String> processBackground(final String background) { |
||||
Map<String, String> rules = new HashMap<String, String>(); |
||||
String[] styles = splitComplexCssStyle(background); |
||||
for(String style : styles) { |
||||
if (style.contains("url(")) { |
||||
rules.put(CSS.Property.BACKGROUND_IMAGE, style); |
||||
} else if (style.equalsIgnoreCase(CSS.Value.REPEAT) |
||||
|| style.equalsIgnoreCase(CSS.Value.NO_REPEAT) |
||||
|| style.equalsIgnoreCase(CSS.Value.REPEAT_X) |
||||
|| style.equalsIgnoreCase(CSS.Value.REPEAT_Y)) { |
||||
rules.put(CSS.Property.BACKGROUND_REPEAT, style); |
||||
} else if (style.equalsIgnoreCase(CSS.Value.FIXED) || style.equalsIgnoreCase(CSS.Value.SCROLL)) { |
||||
rules.put(CSS.Property.BACKGROUND_ATTACHMENT, style); |
||||
} else if (backgroundPositions.contains(style)) { |
||||
if(rules.get(CSS.Property.BACKGROUND_POSITION) == null) { |
||||
rules.put(CSS.Property.BACKGROUND_POSITION, style); |
||||
} else { |
||||
style = style.concat(" "+rules.get(CSS.Property.BACKGROUND_POSITION)); |
||||
rules.put(CSS.Property.BACKGROUND_POSITION, style); |
||||
} |
||||
} else if (isNumericValue(style) || isMetricValue(style) || isRelativeValue(style)) { |
||||
if(rules.get(CSS.Property.BACKGROUND_POSITION) == null) { |
||||
rules.put(CSS.Property.BACKGROUND_POSITION, style); |
||||
} else { |
||||
style = style.concat(" "+rules.get(CSS.Property.BACKGROUND_POSITION)); |
||||
rules.put(CSS.Property.BACKGROUND_POSITION, style); |
||||
} |
||||
} else if(style.contains("rgb(") || style.contains("rgba(") || style.contains("#") || WebColors.NAMES.containsKey(style.toLowerCase())) { |
||||
rules.put(CSS.Property.BACKGROUND_COLOR, style); |
||||
} |
||||
} |
||||
return rules; |
||||
} |
||||
public static String[] splitComplexCssStyle(String s) { |
||||
s = s.replaceAll("\\s*,\\s*", ",") ; |
||||
return s.split("\\s"); |
||||
} |
||||
|
||||
|
||||
public static boolean isMetricValue(final String value) { |
||||
return value.contains(CSS.Value.PX) || value.contains(CSS.Value.IN) || value.contains(CSS.Value.CM) |
||||
|| value.contains(CSS.Value.MM) || value.contains(CSS.Value.PC) || value.contains(CSS.Value.PT); |
||||
|
||||
} |
||||
|
||||
public static boolean isRelativeValue(final String value) { |
||||
return value.contains(CSS.Value.PERCENTAGE) || value.contains(CSS.Value.EM) || value.contains(CSS.Value.EX); |
||||
|
||||
} |
||||
|
||||
public static boolean isNumericValue(final String value) { |
||||
return value.matches("^-?\\d\\d*\\.\\d*$") || value.matches("^-?\\d\\d*$") || value.matches("^-?\\.\\d\\d*$"); |
||||
|
||||
} |
||||
|
||||
|
||||
public static String extractUrl(final String url) { |
||||
String str = null; |
||||
if (url.startsWith("url")) { |
||||
String urlString = url.substring(3).trim().replace("(", "").replace(")", "").trim(); |
||||
if (urlString.startsWith("'") && urlString.endsWith("'")) { |
||||
str = urlString.substring(urlString.indexOf("'")+1, urlString.lastIndexOf("'")); |
||||
} else if ( urlString.startsWith("\"") && urlString.endsWith("\"") ) { |
||||
str = urlString.substring(urlString.indexOf('"')+1, urlString.lastIndexOf('"')); |
||||
} else { |
||||
str = urlString; |
||||
} |
||||
} else { |
||||
str = url; |
||||
} |
||||
return str; |
||||
} |
||||
} |
Loading…
Reference in new issue