📄 css.java
字号:
htmlAttrToCssAttrMap.put(HTML.Attribute.BACKGROUND, new CSS.Attribute[]{CSS.Attribute.BACKGROUND_IMAGE}); htmlAttrToCssAttrMap.put(HTML.Attribute.BGCOLOR, new CSS.Attribute[]{CSS.Attribute.BACKGROUND_COLOR}); htmlAttrToCssAttrMap.put(HTML.Attribute.WIDTH, new CSS.Attribute[]{CSS.Attribute.WIDTH}); htmlAttrToCssAttrMap.put(HTML.Attribute.HEIGHT, new CSS.Attribute[]{CSS.Attribute.HEIGHT}); htmlAttrToCssAttrMap.put(HTML.Attribute.BORDER, new CSS.Attribute[]{CSS.Attribute.BORDER_TOP_WIDTH, CSS.Attribute.BORDER_RIGHT_WIDTH, CSS.Attribute.BORDER_BOTTOM_WIDTH, CSS.Attribute.BORDER_LEFT_WIDTH}); htmlAttrToCssAttrMap.put(HTML.Attribute.CELLPADDING, new CSS.Attribute[]{CSS.Attribute.PADDING}); htmlAttrToCssAttrMap.put(HTML.Attribute.CELLSPACING, new CSS.Attribute[]{CSS.Attribute.BORDER_SPACING}); htmlAttrToCssAttrMap.put(HTML.Attribute.MARGINWIDTH, new CSS.Attribute[]{CSS.Attribute.MARGIN_LEFT, CSS.Attribute.MARGIN_RIGHT}); htmlAttrToCssAttrMap.put(HTML.Attribute.MARGINHEIGHT, new CSS.Attribute[]{CSS.Attribute.MARGIN_TOP, CSS.Attribute.MARGIN_BOTTOM}); htmlAttrToCssAttrMap.put(HTML.Attribute.HSPACE, new CSS.Attribute[]{CSS.Attribute.PADDING_LEFT, CSS.Attribute.PADDING_RIGHT}); htmlAttrToCssAttrMap.put(HTML.Attribute.VSPACE, new CSS.Attribute[]{CSS.Attribute.PADDING_BOTTOM, CSS.Attribute.PADDING_TOP}); htmlAttrToCssAttrMap.put(HTML.Attribute.FACE, new CSS.Attribute[]{CSS.Attribute.FONT_FAMILY}); htmlAttrToCssAttrMap.put(HTML.Attribute.SIZE, new CSS.Attribute[]{CSS.Attribute.FONT_SIZE}); htmlAttrToCssAttrMap.put(HTML.Attribute.VALIGN, new CSS.Attribute[]{CSS.Attribute.VERTICAL_ALIGN}); htmlAttrToCssAttrMap.put(HTML.Attribute.ALIGN, new CSS.Attribute[]{CSS.Attribute.VERTICAL_ALIGN, CSS.Attribute.TEXT_ALIGN, CSS.Attribute.FLOAT}); htmlAttrToCssAttrMap.put(HTML.Attribute.TYPE, new CSS.Attribute[]{CSS.Attribute.LIST_STYLE_TYPE}); htmlAttrToCssAttrMap.put(HTML.Attribute.NOWRAP, new CSS.Attribute[]{CSS.Attribute.WHITE_SPACE}); // initialize StyleConstants mapping styleConstantToCssMap.put(StyleConstants.FontFamily, CSS.Attribute.FONT_FAMILY); styleConstantToCssMap.put(StyleConstants.FontSize, CSS.Attribute.FONT_SIZE); styleConstantToCssMap.put(StyleConstants.Bold, CSS.Attribute.FONT_WEIGHT); styleConstantToCssMap.put(StyleConstants.Italic, CSS.Attribute.FONT_STYLE); styleConstantToCssMap.put(StyleConstants.Underline, CSS.Attribute.TEXT_DECORATION); styleConstantToCssMap.put(StyleConstants.StrikeThrough, CSS.Attribute.TEXT_DECORATION); styleConstantToCssMap.put(StyleConstants.Superscript, CSS.Attribute.VERTICAL_ALIGN); styleConstantToCssMap.put(StyleConstants.Subscript, CSS.Attribute.VERTICAL_ALIGN); styleConstantToCssMap.put(StyleConstants.Foreground, CSS.Attribute.COLOR); styleConstantToCssMap.put(StyleConstants.Background, CSS.Attribute.BACKGROUND_COLOR); styleConstantToCssMap.put(StyleConstants.FirstLineIndent, CSS.Attribute.TEXT_INDENT); styleConstantToCssMap.put(StyleConstants.LeftIndent, CSS.Attribute.MARGIN_LEFT); styleConstantToCssMap.put(StyleConstants.RightIndent, CSS.Attribute.MARGIN_RIGHT); styleConstantToCssMap.put(StyleConstants.SpaceAbove, CSS.Attribute.MARGIN_TOP); styleConstantToCssMap.put(StyleConstants.SpaceBelow, CSS.Attribute.MARGIN_BOTTOM); styleConstantToCssMap.put(StyleConstants.Alignment, CSS.Attribute.TEXT_ALIGN); // HTML->CSS htmlValueToCssValueMap.put("disc", CSS.Value.DISC); htmlValueToCssValueMap.put("square", CSS.Value.SQUARE); htmlValueToCssValueMap.put("circle", CSS.Value.CIRCLE); htmlValueToCssValueMap.put("1", CSS.Value.DECIMAL); htmlValueToCssValueMap.put("a", CSS.Value.LOWER_ALPHA); htmlValueToCssValueMap.put("A", CSS.Value.UPPER_ALPHA); htmlValueToCssValueMap.put("i", CSS.Value.LOWER_ROMAN); htmlValueToCssValueMap.put("I", CSS.Value.UPPER_ROMAN); // CSS-> internal CSS cssValueToInternalValueMap.put("none", CSS.Value.NONE); cssValueToInternalValueMap.put("disc", CSS.Value.DISC); cssValueToInternalValueMap.put("square", CSS.Value.SQUARE); cssValueToInternalValueMap.put("circle", CSS.Value.CIRCLE); cssValueToInternalValueMap.put("decimal", CSS.Value.DECIMAL); cssValueToInternalValueMap.put("lower-roman", CSS.Value.LOWER_ROMAN); cssValueToInternalValueMap.put("upper-roman", CSS.Value.UPPER_ROMAN); cssValueToInternalValueMap.put("lower-alpha", CSS.Value.LOWER_ALPHA); cssValueToInternalValueMap.put("upper-alpha", CSS.Value.UPPER_ALPHA); cssValueToInternalValueMap.put("repeat", CSS.Value.BACKGROUND_REPEAT); cssValueToInternalValueMap.put("no-repeat", CSS.Value.BACKGROUND_NO_REPEAT); cssValueToInternalValueMap.put("repeat-x", CSS.Value.BACKGROUND_REPEAT_X); cssValueToInternalValueMap.put("repeat-y", CSS.Value.BACKGROUND_REPEAT_Y); cssValueToInternalValueMap.put("scroll", CSS.Value.BACKGROUND_SCROLL); cssValueToInternalValueMap.put("fixed", CSS.Value.BACKGROUND_FIXED); // Register all the CSS attribute keys for archival/unarchival Object[] keys = CSS.Attribute.allAttributes; try { for (int i = 0; i < keys.length; i++) { StyleContext.registerStaticAttributeKey(keys[i]); } } catch (Throwable e) { e.printStackTrace(); } // Register all the CSS Values for archival/unarchival keys = CSS.Value.allValues; try { for (int i = 0; i < keys.length; i++) { StyleContext.registerStaticAttributeKey(keys[i]); } } catch (Throwable e) { e.printStackTrace(); } } /** * Return the set of all possible CSS attribute keys. */ public static Attribute[] getAllAttributeKeys() { Attribute[] keys = new Attribute[Attribute.allAttributes.length]; System.arraycopy(Attribute.allAttributes, 0, keys, 0, Attribute.allAttributes.length); return keys; } /** * Translates a string to a <code>CSS.Attribute</code> object. * This will return <code>null</code> if there is no attribute * by the given name. * * @param name the name of the CSS attribute to fetch the * typesafe enumeration for * @return the <code>CSS.Attribute</code> object, * or <code>null</code> if the string * doesn't represent a valid attribute key */ public static final Attribute getAttribute(String name) { return (Attribute) attributeMap.get(name); } /** * Translates a string to a <code>CSS.Value</code> object. * This will return <code>null</code> if there is no value * by the given name. * * @param name the name of the CSS value to fetch the * typesafe enumeration for * @return the <code>CSS.Value</code> object, * or <code>null</code> if the string * doesn't represent a valid CSS value name; this does * not mean that it doesn't represent a valid CSS value */ static final Value getValue(String name) { return (Value) valueMap.get(name); } // // Conversion related methods/classes // /** * Returns a URL for the given CSS url string. If relative, * <code>base</code> is used as the parent. If a valid URL can not * be found, this will not throw a MalformedURLException, instead * null will be returned. */ static URL getURL(URL base, String cssString) { if (cssString == null) { return null; } if (cssString.startsWith("url(") && cssString.endsWith(")")) { cssString = cssString.substring(4, cssString.length() - 1); } // Absolute first try { URL url = new URL(cssString); if (url != null) { return url; } } catch (MalformedURLException mue) { } // Then relative if (base != null) { // Relative URL, try from base try { URL url = new URL(base, cssString); return url; } catch (MalformedURLException muee) { } } return null; } /** * Converts a type Color to a hex string * in the format "#RRGGBB" */ static String colorToHex(Color color) { String colorstr = new String("#"); // Red String str = Integer.toHexString(color.getRed()); if (str.length() > 2) str = str.substring(0, 2); else if (str.length() < 2) colorstr += "0" + str; else colorstr += str; // Green str = Integer.toHexString(color.getGreen()); if (str.length() > 2) str = str.substring(0, 2); else if (str.length() < 2) colorstr += "0" + str; else colorstr += str; // Blue str = Integer.toHexString(color.getBlue()); if (str.length() > 2) str = str.substring(0, 2); else if (str.length() < 2) colorstr += "0" + str; else colorstr += str; return colorstr; } /** * Convert a "#FFFFFF" hex string to a Color. * If the color specification is bad, an attempt * will be made to fix it up. */ static final Color hexToColor(String value) { String digits; int n = value.length(); if (value.startsWith("#")) { digits = value.substring(1, Math.min(value.length(), 7)); } else { digits = value; } String hstr = "0x" + digits; Color c; try { c = Color.decode(hstr); } catch (NumberFormatException nfe) { c = null; } return c; } /** * Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" * to a Color. */ static Color stringToColor(String str) { Color color = null; if (str.length() == 0) color = Color.black; else if (str.startsWith("rgb(")) { color = parseRGB(str); } else if (str.charAt(0) == '#') color = hexToColor(str); else if (str.equalsIgnoreCase("Black")) color = hexToColor("#000000"); else if(str.equalsIgnoreCase("Silver")) color = hexToColor("#C0C0C0"); else if(str.equalsIgnoreCase("Gray")) color = hexToColor("#808080"); else if(str.equalsIgnoreCase("White")) color = hexToColor("#FFFFFF"); else if(str.equalsIgnoreCase("Maroon")) color = hexToColor("#800000"); else if(str.equalsIgnoreCase("Red")) color = hexToColor("#FF0000"); else if(str.equalsIgnoreCase("Purple")) color = hexToColor("#800080"); else if(str.equalsIgnoreCase("Fuchsia")) color = hexToColor("#FF00FF"); else if(str.equalsIgnoreCase("Green")) color = hexToColor("#008000"); else if(str.equalsIgnoreCase("Lime")) color = hexToColor("#00FF00"); else if(str.equalsIgnoreCase("Olive")) color = hexToColor("#808000"); else if(str.equalsIgnoreCase("Yellow")) color = hexToColor("#FFFF00"); else if(str.equalsIgnoreCase("Navy")) color = hexToColor("#000080"); else if(str.equalsIgnoreCase("Blue")) color = hexToColor("#0000FF"); else if(str.equalsIgnoreCase("Teal")) color = hexToColor("#008080"); else if(str.equalsIgnoreCase("Aqua")) color = hexToColor("#00FFFF"); else color = hexToColor(str); // sometimes get specified without leading # return color; } /** * Parses a String in the format <code>rgb(r, g, b)</code> where * each of the Color components is either an integer, or a floating number * with a % after indicating a percentage value of 255. Values are * constrained to fit with 0-255. The resulting Color is returned. */ private static Color parseRGB(String string) { // Find the next numeric char int[] index = new int[1]; index[0] = 4; int red = getColorComponent(string, index); int green = getColorComponent(string, index); int blue = getColorComponent(string, index); return new Color(red, green, blue); } /** * Returns the next integer value from <code>string</code> starting * at <code>index[0]</code>. The value can either can an integer, or * a percentage (floating number ending with %), in which case it is * multiplied by 255. */ private static int getColorComponent(String string, int[] index) { int length = string.length(); char aChar; // Skip non-decimal chars while(index[0] < length && (aChar = string.charAt(index[0])) != '-' && !Character.isDigit(aChar) && aChar != '.') { index[0]++; } int start = index[0]; if (start < length && string.charAt(index[0]) == '-') { index[0]++; } while(index[0] < length && Character.isDigit(string.charAt(index[0]))) { index[0]++; } if (index[0] < length && string.charAt(index[0]) == '.') { // Decimal value index[0]++; while(index[0] < length && Character.isDigit(string.charAt(index[0]))) { index[0]++; } } if (start != index[0]) { try { float value = Float.parseFloat(string.substring (start, index[0])); if (index[0] < length && string.charAt(index[0]) == '%') { index[0]++; value = value * 255f / 100f; } return Math.min(255, Math.max(0, (int)value)); } catch (NumberFormatException nfe) { // Treat as 0 } } return 0; } static int getIndexOfSize(float pt, int[] sizeMap) { for (int i = 0; i < sizeMap.length; i ++ ) if (pt <= sizeMap[i]) return i + 1; return sizeMap.length; } static int getIndexOfSize(float pt, StyleSheet ss) { int[] sizeMap = (ss != null) ? ss.getSizeMap() : StyleSheet.sizeMapDefault; return getIndexOfSize(pt, sizeMap); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -