CSS.java revision 4379
1759N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A * or visit www.oracle.com if you need additional information or have any 1759N/A * as a typesafe enumeration. The HTML View implementations use 1759N/A * CSS attributes to determine how they will render. This also defines 1759N/A * properties, such as font, are mapped to the intrinsic properties. 139N/A * <p>The following describes the CSS properties that are suppored by the 480N/A * <li>font-size (supports relative units) 480N/A * <li>background-color (with the exception of transparent) 480N/A * <li>background-repeat 480N/A * <li>background-position 1759N/A * <li>text-decoration (with the exception of blink and overline) 1759N/A * <li>vertical-align (only sup and super) 1759N/A * <li>text-align (justify is treated as center) 139N/A * <li>border-right-style 139N/A * <li>border-bottom-style 139N/A * <li>border-left-style 139N/A * <li>border-style (only supports inset, outset and none) 139N/A * <li>border-right-color 139N/A * <li>border-bottom-color 139N/A * <li>border-left-color 139N/A * <li>list-style-position 139N/A * The following are modeled, but currently not rendered. 139N/A * <li>background-attachment (background always treated as scroll) 139N/A * <li>border-top-width (this is used to indicate if a border should be used) 480N/A * <li>border-right-width 480N/A * <li>border-bottom-width 480N/A * <li>border-left-width 139N/A * <p><b>Note: for the time being we do not fully support relative units, 139N/A * unless noted, so that 0N/A * p { margin-top: 10% } will be treated as if no margin-top was specified. 0N/A * @author Timothy Prinzing 0N/A * @author Scott Violet 0N/A * Definitions to be used as a key on AttributeSet's 0N/A * that might hold CSS attributes. Since this is a 0N/A * closed set (i.e. defined exactly by the specification), 0N/A * it is final and cannot be extended. 0N/A * The string representation of the attribute. This 0N/A * should exactly match the string specified in the 0N/A * CSS specification. * Fetch the default value for the attribute. * If there is no default value (such as for * composite attributes), null will be returned. * Indicates if the attribute should be inherited * from the parent or not. new Attribute(
"background-attachment",
"scroll",
false);
new Attribute(
"background-color",
"transparent",
false);
new Attribute(
"background-image",
"none",
false);
new Attribute(
"background-repeat",
"repeat",
false);
new Attribute(
"border-bottom-style",
"none",
false);
new Attribute(
"border-bottom-width",
"medium",
false);
new Attribute(
"border-left-style",
"none",
false);
new Attribute(
"border-left-width",
"medium",
false);
new Attribute(
"border-right-style",
"none",
false);
new Attribute(
"border-right-width",
"medium",
false);
new Attribute(
"border-style",
"none",
false);
new Attribute(
"border-top-style",
"none",
false);
new Attribute(
"border-top-width",
"medium",
false);
new Attribute(
"border-width",
"medium",
false);
new Attribute(
"font-variant",
"normal",
true);
new Attribute(
"font-weight",
"normal",
true);
new Attribute(
"letter-spacing",
"normal",
true);
new Attribute(
"line-height",
"normal",
true);
new Attribute(
"list-style-image",
"none",
true);
new Attribute(
"list-style-position",
"outside",
true);
new Attribute(
"list-style-type",
"disc",
true);
* made up css attributes to describe orientation depended * margins. used for <dir>, <menu>, <ul> etc. see * 5088268 for more details new Attribute(
"text-decoration",
"none",
true);
new Attribute(
"text-transform",
"none",
true);
new Attribute(
"vertical-align",
"baseline",
false);
new Attribute(
"word-spacing",
"normal",
true);
new Attribute(
"white-space",
"normal",
true);
// All possible CSS attribute keys. static final class Value {
* The string representation of the attribute. This * should exactly match the string specified in the // setup the css conversion table for (
int i =
0; i < n; i++) {
* Sets the base font size. <code>sz</code> is a CSS value, and is * not necessarily the point size. Use getPointSize to determine the * point size corresponding to <code>sz</code>. * Sets the base font size from the passed in string. * Returns the base font size. * Parses the CSS property <code>key</code> with value * <code>value</code> placing the result in <code>att</code>. * Gets the internal CSS representation of <code>value</code> which is * a CSS value of the CSS attribute named <code>key</code>. The receiver * should not modify <code>value</code>, and the first <code>count</code> * Maps from a StyleConstants to a CSS Attribute. * Maps from a StyleConstants value to a CSS value. * Converts the passed in CSS value to a StyleConstants value. * <code>key</code> identifies the CSS attribute being mapped. * Returns the font for the values in the passed in AttributeSet. * It is assumed the keys will be CSS.Attribute keys. * <code>sc</code> is the StyleContext that will be messaged to get * the font once the size, name and style have been determined. * If the vertical alignment is set to either superscirpt or * subscript we reduce the font size by 2 points. // PENDING(prinz) this is a 1.1 based implementation, need to also * Takes a set of attributes and turn it into a color * specification. This might be used to specify things * like brighter, more hue, etc. * This will return null if there is no value for <code>key</code>. * @param key CSS.Attribute identifying where color is stored. * @param a the set of attributes * Returns the size of a font from the passed in string. * @param size CSS string describing font size * @param baseFontSize size to use for relative units. * Returns the length of the attribute in <code>a</code> with * Convert a set of HTML attributes to an equivalent * @param htmlAttrSet AttributeSet containing the HTML attributes. * @return AttributeSet containing the corresponding CSS attributes. * The AttributeSet will be empty if there are no mapping // translate border width into the cells, if it has non-zero value. // If table contains the BORDER attribute cells should have border width equals 1 * Navigator uses ALIGN for caption placement and IE uses VALIGN. // Some browsers accept <TABLE BORDER> and <TABLE BORDER=""> with the same semantics as BORDER=1 * The hashtable and the static initalization block below, * set up a mapping from well-known HTML attributes to * CSS attributes. For the most part, there is a 1-1 mapping * between the two. However in the case of certain HTML * attributes for example HTML.Attribute.VSPACE or * HTML.Attribute.HSPACE, end up mapping to two CSS.Attribute's. * Therefore, the value associated with each HTML.Attribute. * key ends up being an array of CSS.Attribute.* objects. * The hashtable and static initialization that follows sets * up a translation from StyleConstants (i.e. the <em>well known</em> * attributes) to the associated CSS attributes. /** Maps from HTML value to a CSS value. Used in internal mapping. */ /** Maps from CSS value (string) to internal value. */ // load the attribute map // initialize StyleConstants mapping * Return the set of all possible CSS attribute keys. * Translates a string to a <code>CSS.Attribute</code> object. * This will return <code>null</code> if there is no attribute * @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 * Translates a string to a <code>CSS.Value</code> object. * This will return <code>null</code> if there is no value * @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 * 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 // Relative URL, try from base * Converts a type Color to a hex string * in the format "#RRGGBB" * Convert a "#FFFFFF" hex string to a Color. * If the color specification is bad, an attempt * will be made to fix it up. * Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" * 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. // Find the next numeric char int[]
index =
new int[
1];
* 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 // Skip non-decimal chars * @return an array of all the strings in <code>value</code> * that are separated by whitespace. * Return the point size, given a size index. Legal HTML index sizes // HR needs special handling due to us treating it as a leaf. * HTML.Attribute.ALIGN needs special processing. * It can map to to 1 of many(3) possible CSS attributes * depending on the nature of the tag the attribute is * part off and depending on the value of the attribute. * The html size attribute has a mapping in the CSS world only * if it is par of a font or base font tag. * In the case of all remaining HTML.Attribute's they * map to 1 or more CCS.Attribute. * Given a CSS.Attribute object and its corresponding HTML.Attribute's * value, this method returns a CssValue object to associate with the * @param the CSS.Attribute * @param a String containing the value associated HTML.Attribtue. * Maps an HTML.Attribute object to its appropriate CSS.Attributes. * @return CSS.Attribute[] * Maps HTML.Attribute.ALIGN to either: * CSS.Attribute.TEXT_ALIGN * CSS.Attribute.VERTICAL_ALIGN * based on the tag associated with the attribute and the * value of the attribute. * @param AttributeSet containing HTML attributes. * @return CSS.Attribute mapping for HTML.Attribute.ALIGN. String htmlAttrValue = (String)htmlAttrSet.getAttribute(HTML.Attribute.ALIGN); CSS.Attribute cssAttr = CSS.Attribute.TEXT_ALIGN; if (htmlAttrValue != null && htmlAttrSet instanceof Element) { Element elem = (Element)htmlAttrSet; if (!elem.isLeaf() && tag.isBlock() && validTextAlignValue(htmlAttrValue)) { return CSS.Attribute.TEXT_ALIGN; } else if (isFloater(htmlAttrValue)) { return CSS.Attribute.FLOAT; } else if (elem.isLeaf()) { return CSS.Attribute.VERTICAL_ALIGN; * Fetches the tag associated with the HTML AttributeSet. * @param AttributeSet containing the HTML attributes. * Base class to CSS values in the attribute sets. This * is intended to act as a convertor to/from other attribute * The CSS parser uses the parseCssValue method to convert * a string to whatever format is appropriate a given key * (i.e. these convertors are stored in a map using the * CSS.Attribute as a key and the CssValue as the value). * The HTML to CSS conversion process first converts the * HTML.Attribute to a CSS.Attribute, and then calls * the parseHtmlValue method on the value of the HTML * attribute to produce the corresponding CSS value. * The StyleConstants to CSS conversion process first * converts the StyleConstants attribute to a * CSS.Attribute, and then calls the fromStyleConstants * method to convert the StyleConstants value to a * The CSS to StyleConstants conversion process first * converts the StyleConstants attribute to a * CSS.Attribute, and then calls the toStyleConstants * method to convert the CSS value to a StyleConstants * Convert a CSS value string to the internal format * (for fast processing) used in the attribute sets. * The fallback storage for any value that we don't * have a special binary format for is a String. * Convert an HTML attribute value to a CSS attribute * value. If there is no conversion, return null. * This is implemented to simply forward to the CSS * parsing by default (since some of the attribute * values are the same). If the attribute value * isn't recognized as a CSS value it is generally * Converts a <code>StyleConstants</code> attribute value to * a CSS attribute value. If there is no conversion, * returns <code>null</code>. By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @param value the value of a <code>StyleConstants</code> * attribute to be converted * @return the CSS value that represents the * <code>StyleConstants</code> value * Converts a CSS attribute value to a * <code>StyleConstants</code> * value. If there is no conversion, returns * By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @param v the view containing <code>AttributeSet</code> * @return the <code>StyleConstants</code> attribute value that * represents the CSS attribute value * Return the CSS format of the value * The value as a string... before conversion to a * By default CSS attributes are represented as simple * strings. They also have no conversion to/from * StyleConstants by default. This class represents the * value as a string (via the superclass), but * provides StyleConstants conversion support for the * CSS attributes that are held as strings. * Convert a CSS value string to the internal format * (for fast processing) used in the attribute sets. * This produces a StringValue, so that it can be * used to convert from CSS to StyleConstants values. * Converts a <code>StyleConstants</code> attribute value to * a CSS attribute value. If there is no conversion * returns <code>null</code>. * @param key the <code>StyleConstants</code> attribute * @param value the value of a <code>StyleConstants</code> * attribute to be converted * @return the CSS value that represents the * <code>StyleConstants</code> value * Converts a CSS attribute value to a * <code>StyleConstants</code> value. * If there is no conversion, returns <code>null</code>. * By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @return the <code>StyleConstants</code> attribute value that * represents the CSS attribute value // Used by ViewAttributeSet * Represents a value for the CSS.FONT_SIZE attribute. * The binary format of the value can be one of several * types. If the type is Float, * the value is specified in terms of point or * percentage, depending upon the ending of the * If the type is Integer, the value is specified * in terms of a size index. * Returns the size in points. This is ultimately * @param a the attribute set the value is being * requested from. We may need to walk up the * resolve hierarchy if it's relative. // it's an index, translate from size table // a is null, or no resolve parent. // relative sizes, larger | smaller (adjust from parent by // em, ex refer to parent sizes // lengths: pt, mm, cm, pc, in, px // em (font height 3em would be 3 times font height) // lengths are (+/-) followed by a number and two letter * relative sizes in the size attribute are relative * to the <basefont>'s size. * Converts a <code>StyleConstants</code> attribute value to * a CSS attribute value. If there is no conversion * returns <code>null</code>. By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @param value the value of a <code>StyleConstants</code> * attribute to be converted * @return the CSS value that represents the * <code>StyleConstants</code> value * Converts a CSS attribute value to a <code>StyleConstants</code> * value. If there is no conversion, returns <code>null</code>. * By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @return the <code>StyleConstants</code> attribute value that * represents the CSS attribute value * Returns the font family to use. * Converts a <code>StyleConstants</code> attribute value to * a CSS attribute value. If there is no conversion * returns <code>null</code>. By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @param value the value of a <code>StyleConstants</code> * attribute to be converted * @return the CSS value that represents the * <code>StyleConstants</code> value * Converts a CSS attribute value to a <code>StyleConstants</code> * value. If there is no conversion, returns <code>null</code>. * By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @return the <code>StyleConstants</code> attribute value that * represents the CSS attribute value // PENDING(prinz) add support for relative values * Converts a <code>StyleConstants</code> attribute value to * a CSS attribute value. If there is no conversion * returns <code>null</code>. By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @param value the value of a <code>StyleConstants</code> * attribute to be converted * @return the CSS value that represents the * <code>StyleConstants</code> value * Converts a CSS attribute value to a <code>StyleConstants</code> * value. If there is no conversion, returns <code>null</code>. * By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @return the <code>StyleConstants</code> attribute value that * represents the CSS attribute value * Returns the color to use. * Converts a <code>StyleConstants</code> attribute value to * a CSS attribute value. If there is no conversion * returns <code>null</code>. By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @param value the value of a <code>StyleConstants</code> * attribute to be converted * @return the CSS value that represents the * <code>StyleConstants</code> value * Converts a CSS attribute value to a <code>StyleConstants</code> * value. If there is no conversion, returns <code>null</code>. * By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @return the <code>StyleConstants</code> attribute value that * represents the CSS attribute value // CSS.Values are static, don't archive it. * if this length value may be negative. * Returns the length (span) to use. * Returns the length (span) to use. If the value represents * a percentage, it is scaled based on <code>currentValue</code>. * Returns true if the length represents a percentage of the // Not pixels, use LengthUnit // PENDING: currently, we only support absolute values and * Converts a <code>StyleConstants</code> attribute value to * a CSS attribute value. If there is no conversion, * returns <code>null</code>. By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @param value the value of a <code>StyleConstants</code> * attribute to be converted * @return the CSS value that represents the * <code>StyleConstants</code> value * Converts a CSS attribute value to a <code>StyleConstants</code> * value. If there is no conversion, returns <code>null</code>. * By default, there is no conversion. * @param key the <code>StyleConstants</code> attribute * @return the <code>StyleConstants</code> attribute value that * represents the CSS attribute value /** If true, span is a percentage value, and that to determine * the length another value needs to be passed in. */ /** Either the absolute value (percentage == false) or * BorderWidthValue is used to model BORDER_XXX_WIDTH and adds support /** Values used to represent border width. */ private static final float[]
values = {
1,
2,
4 };
* Handles uniquing of CSS values, like lists, and background image * Used for background images, to represent the position. // bitmask: bit 0, horizontal relative, bit 1 horizontal relative to // font size, 2 vertical relative to size, 3 vertical relative to // 'top left' and 'left top' both mean the same as '0% 0%'. // 'top', 'top center' and 'center top' mean the same as '50% 0%'. // 'right top' and 'top right' mean the same as '100% 0%'. // 'left', 'left center' and 'center left' mean the same as // 'center' and 'center center' mean the same as '50% 50%'. // 'right', 'right center' and 'center right' mean the same as // 'bottom left' and 'left bottom' mean the same as '0% 100%'. // 'bottom', 'bottom center' and 'center bottom' mean the same as // 'bottom right' and 'right bottom' mean the same as '100% 100%'. // bit 0 for vert, 1 hor, 2 for center // First, check for keywords else if ((
found &
2) ==
2) {
// no horiz, no vert, but center * Used for BackgroundImages. // PENDING: this base is wrong for linked style sheets. * Parses a length value, this is used internally, and never added * to an AttributeSet or returned to the developer. // Not sure about 1.3, determined by experiementation. // mapping according to the CSS2 spec // 0 - value indicates real value // 1 - % value, value relative to depends upon key. // 50% will have a value = .5 // 2 - add value to parent value. // 3 - em/ex relative to font size of element (except for // font-size, which is relative to parent). * Class used to parse font property. The font property is shorthand * for the other font properties. This expands the properties, placing * them in the attributeset. * Parses the shorthand font string <code>value</code>, placing the * result in <code>attr</code>. // [ <font-style> || <font-variant> || <font-weight> ]? <font-size> // [ / <line-height> ]? <font-family> // bitmask, 1 for style, 2 for variant, 3 for weight // Check for font-style font-variant font-weight // string at index should be the font-size // remainder of strings are font-family * Parses the background property into its intrinsic values. * Parses the shorthand font string <code>value</code>, placing the * result in <code>attr</code>. // bitmask: 0 for image, 1 repeat, 2 attachment, 3 position, // Currently, there is no good way to express this. css.addInternalCSSValue(attr, CSS.Attribute.BACKGROUND_COLOR, * Used to parser margin and padding. * <code>value</code>, placing the result in <code>attr</code>. * <code>names</code> give the 4 instrinsic property names. // Identifies all values. // 0 & 2 = strings[0], 1 & 3 = strings[1] // Part is non-parseable or occured more than once. // Unspecified parts get default values. // Dispatch collected values to individual properties. * Calculate the requirements needed to tile the requirements * given by the iterator that would be tiled. The calculation * takes into consideration margin and border spacing. for (
int i =
0; i < n; i++) {
// adjust for the spacing area * Calculate a tiled layout for the given iterator. * This should be done collapsing the neighboring * margins to be a total of the maximum of the two * neighboring margin areas as described in the CSS spec. * first pass, calculate the preferred sizes, adjustments needed because * of margin collapsing, and the flexibility to adjust the sizes. //max gain we can get adjusting elements with adjustmentWeight <= i //max loss we can get adjusting elements with adjustmentWeight <= i for (
int i =
0; i < n; i++) {
* Second pass, expand or contract by as much as possible to reach * the target span. This takes the margin collapsing into account * prior to adjusting the span. // determine the adjustment to be made // adjustmentsArray[] is sorted. I do not bother about for (
int i =
0; i < n; i++) {
// while rounding we could lose several pixels. // check for infinite loop // try to distribute roundError. one pixel per cell for (
int i =
0; i < n; i++) {
* An iterator to express the requirements to use when computing // Determine what values in valueConvertor need to be written out. // Should we throw an exception here? // Should we throw an exception here? // Reconstruct the hashtable. * we need StyleSheet for resolving lenght units. (see * we can not pass stylesheet for handling relative sizes. (do not * think changing public API is necessary) * CSS is not likely to be accessed from more then one thread. * Having local storage for StyleSheet for resolving relative /** Maps from CSS key to CssValue. */ /** Size used for relative units. */