jdk/src/share/classes/javax/swing/text/html/CSS.java
author lana
Tue, 12 Aug 2008 17:59:58 -0700
changeset 1299 027d966d5658
parent 715 f16baef3a20e
parent 1287 a04aca99c77a
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.HeadlessException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.ImageIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.SizeRequirements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Defines a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <a href="http://www.w3.org/TR/REC-CSS1">CSS attributes</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * as a typesafe enumeration.  The HTML View implementations use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * CSS attributes to determine how they will render. This also defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * methods to map between CSS/HTML/StyleConstants. Any shorthand
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * properties, such as font, are mapped to the intrinsic properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>The following describes the CSS properties that are suppored by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * rendering engine:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <ul><li>font-family
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   <li>font-style
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   <li>font-size (supports relative units)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   <li>font-weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   <li>font
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   <li>color
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   <li>background-color (with the exception of transparent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   <li>background-image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   <li>background-repeat
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   <li>background-position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   <li>background
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   <li>background-repeat
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   <li>text-decoration (with the exception of blink and overline)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   <li>vertical-align (only sup and super)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   <li>text-align (justify is treated as center)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *   <li>margin-top
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   <li>margin-right
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   <li>margin-bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   <li>margin-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *   <li>margin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *   <li>padding-top
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   <li>padding-right
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *   <li>padding-bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   <li>padding-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   <li>border-style (only supports inset, outset and none)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   <li>list-style-type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   <li>list-style-position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * The following are modeled, but currently not rendered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <ul><li>font-variant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *   <li>background-attachment (background always treated as scroll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *   <li>word-spacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *   <li>letter-spacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *   <li>text-indent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   <li>text-transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   <li>line-height
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   <li>border-top-width (this is used to indicate if a border should be used)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   <li>border-right-width
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   <li>border-bottom-width
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   <li>border-left-width
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   <li>border-width
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   <li>border-top
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   <li>border-right
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *   <li>border-bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *   <li>border-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   <li>border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   <li>width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *   <li>height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *   <li>float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *   <li>clear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *   <li>display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *   <li>white-space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *   <li>list-style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <p><b>Note: for the time being we do not fully support relative units,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * unless noted, so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * p { margin-top: 10% } will be treated as if no margin-top was specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @author  Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @see StyleSheet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
public class CSS implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Definitions to be used as a key on AttributeSet's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * that might hold CSS attributes.  Since this is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * closed set (i.e. defined exactly by the specification),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * it is final and cannot be extended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static final class Attribute {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        private Attribute(String name, String defaultValue, boolean inherited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            this.defaultValue = defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            this.inherited = inherited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * The string representation of the attribute.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * should exactly match the string specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * CSS specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * Fetch the default value for the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * If there is no default value (such as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * composite attributes), null will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        public String getDefaultValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * Indicates if the attribute should be inherited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * from the parent or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public boolean isInherited() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return inherited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        private String defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        private boolean inherited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        public static final Attribute BACKGROUND =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            new Attribute("background", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        public static final Attribute BACKGROUND_ATTACHMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            new Attribute("background-attachment", "scroll", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        public static final Attribute BACKGROUND_COLOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            new Attribute("background-color", "transparent", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        public static final Attribute BACKGROUND_IMAGE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            new Attribute("background-image", "none", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        public static final Attribute BACKGROUND_POSITION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            new Attribute("background-position", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        public static final Attribute BACKGROUND_REPEAT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            new Attribute("background-repeat", "repeat", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        public static final Attribute BORDER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            new Attribute("border", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        public static final Attribute BORDER_BOTTOM =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            new Attribute("border-bottom", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        public static final Attribute BORDER_BOTTOM_COLOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            new Attribute("border-bottom-color", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        public static final Attribute BORDER_BOTTOM_STYLE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            new Attribute("border-bottom-style", "none", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        public static final Attribute BORDER_BOTTOM_WIDTH =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            new Attribute("border-bottom-width", "medium", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        public static final Attribute BORDER_COLOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            new Attribute("border-color", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        public static final Attribute BORDER_LEFT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            new Attribute("border-left", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        public static final Attribute BORDER_LEFT_COLOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            new Attribute("border-left-color", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        public static final Attribute BORDER_LEFT_STYLE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            new Attribute("border-left-style", "none", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        public static final Attribute BORDER_LEFT_WIDTH =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            new Attribute("border-left-width", "medium", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        public static final Attribute BORDER_RIGHT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            new Attribute("border-right", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        public static final Attribute BORDER_RIGHT_COLOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            new Attribute("border-right-color", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        public static final Attribute BORDER_RIGHT_STYLE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            new Attribute("border-right-style", "none", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        public static final Attribute BORDER_RIGHT_WIDTH =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            new Attribute("border-right-width", "medium", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        public static final Attribute BORDER_STYLE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            new Attribute("border-style", "none", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        public static final Attribute BORDER_TOP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            new Attribute("border-top", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        public static final Attribute BORDER_TOP_COLOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            new Attribute("border-top-color", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        public static final Attribute BORDER_TOP_STYLE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            new Attribute("border-top-style", "none", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public static final Attribute BORDER_TOP_WIDTH =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            new Attribute("border-top-width", "medium", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        public static final Attribute BORDER_WIDTH =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            new Attribute("border-width", "medium", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        public static final Attribute CLEAR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            new Attribute("clear", "none", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        public static final Attribute COLOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            new Attribute("color", "black", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        public static final Attribute DISPLAY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            new Attribute("display", "block", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        public static final Attribute FLOAT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            new Attribute("float", "none", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        public static final Attribute FONT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            new Attribute("font", null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        public static final Attribute FONT_FAMILY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            new Attribute("font-family", null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        public static final Attribute FONT_SIZE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            new Attribute("font-size", "medium", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        public static final Attribute FONT_STYLE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            new Attribute("font-style", "normal", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        public static final Attribute FONT_VARIANT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            new Attribute("font-variant", "normal", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        public static final Attribute FONT_WEIGHT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            new Attribute("font-weight", "normal", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        public static final Attribute HEIGHT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            new Attribute("height", "auto", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        public static final Attribute LETTER_SPACING =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            new Attribute("letter-spacing", "normal", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        public static final Attribute LINE_HEIGHT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            new Attribute("line-height", "normal", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        public static final Attribute LIST_STYLE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            new Attribute("list-style", null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        public static final Attribute LIST_STYLE_IMAGE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            new Attribute("list-style-image", "none", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        public static final Attribute LIST_STYLE_POSITION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            new Attribute("list-style-position", "outside", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        public static final Attribute LIST_STYLE_TYPE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            new Attribute("list-style-type", "disc", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        public static final Attribute MARGIN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            new Attribute("margin", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        public static final Attribute MARGIN_BOTTOM =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            new Attribute("margin-bottom", "0", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        public static final Attribute MARGIN_LEFT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            new Attribute("margin-left", "0", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        public static final Attribute MARGIN_RIGHT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            new Attribute("margin-right", "0", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         * made up css attributes to describe orientation depended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         * margins. used for <dir>, <menu>, <ul> etc. see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * 5088268 for more details
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        static final Attribute MARGIN_LEFT_LTR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            new Attribute("margin-left-ltr",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                          Integer.toString(Integer.MIN_VALUE), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        static final Attribute MARGIN_LEFT_RTL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            new Attribute("margin-left-rtl",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                          Integer.toString(Integer.MIN_VALUE), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        static final Attribute MARGIN_RIGHT_LTR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            new Attribute("margin-right-ltr",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                          Integer.toString(Integer.MIN_VALUE), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        static final Attribute MARGIN_RIGHT_RTL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            new Attribute("margin-right-rtl",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                          Integer.toString(Integer.MIN_VALUE), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        public static final Attribute MARGIN_TOP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            new Attribute("margin-top", "0", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        public static final Attribute PADDING =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            new Attribute("padding", null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        public static final Attribute PADDING_BOTTOM =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            new Attribute("padding-bottom", "0", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        public static final Attribute PADDING_LEFT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            new Attribute("padding-left", "0", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        public static final Attribute PADDING_RIGHT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            new Attribute("padding-right", "0", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        public static final Attribute PADDING_TOP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            new Attribute("padding-top", "0", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        public static final Attribute TEXT_ALIGN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            new Attribute("text-align", null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        public static final Attribute TEXT_DECORATION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            new Attribute("text-decoration", "none", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        public static final Attribute TEXT_INDENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            new Attribute("text-indent", "0", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        public static final Attribute TEXT_TRANSFORM =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            new Attribute("text-transform", "none", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        public static final Attribute VERTICAL_ALIGN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            new Attribute("vertical-align", "baseline", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        public static final Attribute WORD_SPACING =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            new Attribute("word-spacing", "normal", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        public static final Attribute WHITE_SPACE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            new Attribute("white-space", "normal", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        public static final Attribute WIDTH =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            new Attribute("width", "auto", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        /*public*/ static final Attribute BORDER_SPACING =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            new Attribute("border-spacing", "0", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        /*public*/ static final Attribute CAPTION_SIDE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            new Attribute("caption-side", "left", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // All possible CSS attribute keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        static final Attribute[] allAttributes = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            BACKGROUND, BACKGROUND_ATTACHMENT, BACKGROUND_COLOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            BACKGROUND_IMAGE, BACKGROUND_POSITION, BACKGROUND_REPEAT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            BORDER, BORDER_BOTTOM, BORDER_BOTTOM_WIDTH, BORDER_COLOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            BORDER_LEFT, BORDER_LEFT_WIDTH, BORDER_RIGHT, BORDER_RIGHT_WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            BORDER_STYLE, BORDER_TOP, BORDER_TOP_WIDTH, BORDER_WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            BORDER_TOP_STYLE, BORDER_RIGHT_STYLE, BORDER_BOTTOM_STYLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            BORDER_LEFT_STYLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            BORDER_TOP_COLOR, BORDER_RIGHT_COLOR, BORDER_BOTTOM_COLOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            BORDER_LEFT_COLOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            CLEAR, COLOR, DISPLAY, FLOAT, FONT, FONT_FAMILY, FONT_SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            FONT_STYLE, FONT_VARIANT, FONT_WEIGHT, HEIGHT, LETTER_SPACING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            LINE_HEIGHT, LIST_STYLE, LIST_STYLE_IMAGE, LIST_STYLE_POSITION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            LIST_STYLE_TYPE, MARGIN, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            MARGIN_TOP, PADDING, PADDING_BOTTOM, PADDING_LEFT, PADDING_RIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            PADDING_TOP, TEXT_ALIGN, TEXT_DECORATION, TEXT_INDENT, TEXT_TRANSFORM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            VERTICAL_ALIGN, WORD_SPACING, WHITE_SPACE, WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            BORDER_SPACING, CAPTION_SIDE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            MARGIN_LEFT_LTR, MARGIN_LEFT_RTL, MARGIN_RIGHT_LTR, MARGIN_RIGHT_RTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        private static final Attribute[] ALL_MARGINS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                { MARGIN_TOP, MARGIN_RIGHT, MARGIN_BOTTOM, MARGIN_LEFT };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        private static final Attribute[] ALL_PADDING =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                { PADDING_TOP, PADDING_RIGHT, PADDING_BOTTOM, PADDING_LEFT };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        private static final Attribute[] ALL_BORDER_WIDTHS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                { BORDER_TOP_WIDTH, BORDER_RIGHT_WIDTH, BORDER_BOTTOM_WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                  BORDER_LEFT_WIDTH };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        private static final Attribute[] ALL_BORDER_STYLES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                { BORDER_TOP_STYLE, BORDER_RIGHT_STYLE, BORDER_BOTTOM_STYLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                  BORDER_LEFT_STYLE };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        private static final Attribute[] ALL_BORDER_COLORS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                { BORDER_TOP_COLOR, BORDER_RIGHT_COLOR, BORDER_BOTTOM_COLOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                  BORDER_LEFT_COLOR };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    static final class Value {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        private Value(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
         * The string representation of the attribute.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         * should exactly match the string specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * CSS specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        static final Value INHERITED = new Value("inherited");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        static final Value NONE = new Value("none");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        static final Value HIDDEN = new Value("hidden");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        static final Value DOTTED = new Value("dotted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        static final Value DASHED = new Value("dashed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        static final Value SOLID = new Value("solid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        static final Value DOUBLE = new Value("double");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        static final Value GROOVE = new Value("groove");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        static final Value RIDGE = new Value("ridge");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        static final Value INSET = new Value("inset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        static final Value OUTSET = new Value("outset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        // Lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        static final Value DISC = new Value("disc");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        static final Value CIRCLE = new Value("circle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        static final Value SQUARE = new Value("square");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        static final Value DECIMAL = new Value("decimal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        static final Value LOWER_ROMAN = new Value("lower-roman");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        static final Value UPPER_ROMAN = new Value("upper-roman");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        static final Value LOWER_ALPHA = new Value("lower-alpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        static final Value UPPER_ALPHA = new Value("upper-alpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        // background-repeat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        static final Value BACKGROUND_NO_REPEAT = new Value("no-repeat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        static final Value BACKGROUND_REPEAT = new Value("repeat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        static final Value BACKGROUND_REPEAT_X = new Value("repeat-x");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        static final Value BACKGROUND_REPEAT_Y = new Value("repeat-y");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        // background-attachment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        static final Value BACKGROUND_SCROLL = new Value("scroll");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        static final Value BACKGROUND_FIXED = new Value("fixed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        static final Value[] allValues = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            INHERITED, NONE, DOTTED, DASHED, SOLID, DOUBLE, GROOVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            RIDGE, INSET, OUTSET, DISC, CIRCLE, SQUARE, DECIMAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            LOWER_ROMAN, UPPER_ROMAN, LOWER_ALPHA, UPPER_ALPHA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            BACKGROUND_NO_REPEAT, BACKGROUND_REPEAT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            BACKGROUND_REPEAT_X, BACKGROUND_REPEAT_Y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            BACKGROUND_FIXED, BACKGROUND_FIXED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public CSS() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        baseFontSize = baseFontSizeIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // setup the css conversion table
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   471
        valueConvertor = new Hashtable<Object, Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        valueConvertor.put(CSS.Attribute.FONT_SIZE, new FontSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        valueConvertor.put(CSS.Attribute.FONT_FAMILY, new FontFamily());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        valueConvertor.put(CSS.Attribute.FONT_WEIGHT, new FontWeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        Object bs = new BorderStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        valueConvertor.put(CSS.Attribute.BORDER_TOP_STYLE, bs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        valueConvertor.put(CSS.Attribute.BORDER_RIGHT_STYLE, bs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        valueConvertor.put(CSS.Attribute.BORDER_BOTTOM_STYLE, bs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        valueConvertor.put(CSS.Attribute.BORDER_LEFT_STYLE, bs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        Object cv = new ColorValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        valueConvertor.put(CSS.Attribute.COLOR, cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        valueConvertor.put(CSS.Attribute.BACKGROUND_COLOR, cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        valueConvertor.put(CSS.Attribute.BORDER_TOP_COLOR, cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        valueConvertor.put(CSS.Attribute.BORDER_RIGHT_COLOR, cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        valueConvertor.put(CSS.Attribute.BORDER_BOTTOM_COLOR, cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        valueConvertor.put(CSS.Attribute.BORDER_LEFT_COLOR, cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        Object lv = new LengthValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        valueConvertor.put(CSS.Attribute.MARGIN_TOP, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        valueConvertor.put(CSS.Attribute.MARGIN_BOTTOM, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        valueConvertor.put(CSS.Attribute.MARGIN_LEFT, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        valueConvertor.put(CSS.Attribute.MARGIN_LEFT_LTR, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        valueConvertor.put(CSS.Attribute.MARGIN_LEFT_RTL, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        valueConvertor.put(CSS.Attribute.MARGIN_RIGHT, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        valueConvertor.put(CSS.Attribute.MARGIN_RIGHT_LTR, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        valueConvertor.put(CSS.Attribute.MARGIN_RIGHT_RTL, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        valueConvertor.put(CSS.Attribute.PADDING_TOP, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        valueConvertor.put(CSS.Attribute.PADDING_BOTTOM, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        valueConvertor.put(CSS.Attribute.PADDING_LEFT, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        valueConvertor.put(CSS.Attribute.PADDING_RIGHT, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        Object bv = new BorderWidthValue(null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        valueConvertor.put(CSS.Attribute.BORDER_TOP_WIDTH, bv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        valueConvertor.put(CSS.Attribute.BORDER_BOTTOM_WIDTH, bv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        valueConvertor.put(CSS.Attribute.BORDER_LEFT_WIDTH, bv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        valueConvertor.put(CSS.Attribute.BORDER_RIGHT_WIDTH, bv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        Object nlv = new LengthValue(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        valueConvertor.put(CSS.Attribute.TEXT_INDENT, nlv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        valueConvertor.put(CSS.Attribute.WIDTH, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        valueConvertor.put(CSS.Attribute.HEIGHT, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        valueConvertor.put(CSS.Attribute.BORDER_SPACING, lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        Object sv = new StringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        valueConvertor.put(CSS.Attribute.FONT_STYLE, sv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        valueConvertor.put(CSS.Attribute.TEXT_DECORATION, sv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        valueConvertor.put(CSS.Attribute.TEXT_ALIGN, sv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        valueConvertor.put(CSS.Attribute.VERTICAL_ALIGN, sv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        Object valueMapper = new CssValueMapper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        valueConvertor.put(CSS.Attribute.LIST_STYLE_TYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                           valueMapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        valueConvertor.put(CSS.Attribute.BACKGROUND_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                           new BackgroundImage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        valueConvertor.put(CSS.Attribute.BACKGROUND_POSITION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                           new BackgroundPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        valueConvertor.put(CSS.Attribute.BACKGROUND_REPEAT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                           valueMapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        valueConvertor.put(CSS.Attribute.BACKGROUND_ATTACHMENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                           valueMapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        Object generic = new CssValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        int n = CSS.Attribute.allAttributes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            CSS.Attribute key = CSS.Attribute.allAttributes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            if (valueConvertor.get(key) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                valueConvertor.put(key, generic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Sets the base font size. <code>sz</code> is a CSS value, and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * not necessarily the point size. Use getPointSize to determine the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * point size corresponding to <code>sz</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    void setBaseFontSize(int sz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (sz < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
          baseFontSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        else if (sz > 7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
          baseFontSize = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
          baseFontSize = sz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Sets the base font size from the passed in string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    void setBaseFontSize(String size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        int relSize, absSize, diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (size != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (size.startsWith("+")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                relSize = Integer.valueOf(size.substring(1)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                setBaseFontSize(baseFontSize + relSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            } else if (size.startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                relSize = -Integer.valueOf(size.substring(1)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                setBaseFontSize(baseFontSize + relSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                setBaseFontSize(Integer.valueOf(size).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * Returns the base font size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    int getBaseFontSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return baseFontSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Parses the CSS property <code>key</code> with value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <code>value</code> placing the result in <code>att</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    void addInternalCSSValue(MutableAttributeSet attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                             CSS.Attribute key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if (key == CSS.Attribute.FONT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            ShorthandFontParser.parseShorthandFont(this, value, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        else if (key == CSS.Attribute.BACKGROUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            ShorthandBackgroundParser.parseShorthandBackground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                               (this, value, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        else if (key == CSS.Attribute.MARGIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            ShorthandMarginParser.parseShorthandMargin(this, value, attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                           CSS.Attribute.ALL_MARGINS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        else if (key == CSS.Attribute.PADDING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            ShorthandMarginParser.parseShorthandMargin(this, value, attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                                           CSS.Attribute.ALL_PADDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        else if (key == CSS.Attribute.BORDER_WIDTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            ShorthandMarginParser.parseShorthandMargin(this, value, attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                           CSS.Attribute.ALL_BORDER_WIDTHS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        else if (key == CSS.Attribute.BORDER_COLOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            ShorthandMarginParser.parseShorthandMargin(this, value, attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                            CSS.Attribute.ALL_BORDER_COLORS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        else if (key == CSS.Attribute.BORDER_STYLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            ShorthandMarginParser.parseShorthandMargin(this, value, attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                            CSS.Attribute.ALL_BORDER_STYLES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        else if ((key == CSS.Attribute.BORDER) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                   (key == CSS.Attribute.BORDER_TOP) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                   (key == CSS.Attribute.BORDER_RIGHT) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                   (key == CSS.Attribute.BORDER_BOTTOM) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                   (key == CSS.Attribute.BORDER_LEFT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            ShorthandBorderParser.parseShorthandBorder(attr, key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            Object iValue = getInternalCSSValue(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            if (iValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                attr.addAttribute(key, iValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Gets the internal CSS representation of <code>value</code> which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * a CSS value of the CSS attribute named <code>key</code>. The receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * should not modify <code>value</code>, and the first <code>count</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * strings are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    Object getInternalCSSValue(CSS.Attribute key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        CssValue conv = (CssValue) valueConvertor.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        Object r = conv.parseCssValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return r != null ? r : conv.parseCssValue(key.getDefaultValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * Maps from a StyleConstants to a CSS Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    Attribute styleConstantsKeyToCSSKey(StyleConstants sc) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   640
        return styleConstantToCssMap.get(sc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * Maps from a StyleConstants value to a CSS value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    Object styleConstantsValueToCSSValue(StyleConstants sc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                         Object styleValue) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   648
        Attribute cssKey = styleConstantsKeyToCSSKey(sc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        if (cssKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            CssValue conv = (CssValue)valueConvertor.get(cssKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            return conv.fromStyleConstants(sc, styleValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Converts the passed in CSS value to a StyleConstants value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * <code>key</code> identifies the CSS attribute being mapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    Object cssValueToStyleConstantsValue(StyleConstants key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if (value instanceof CssValue) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   662
            return ((CssValue)value).toStyleConstants(key, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Returns the font for the values in the passed in AttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * It is assumed the keys will be CSS.Attribute keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * <code>sc</code> is the StyleContext that will be messaged to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * the font once the size, name and style have been determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    Font getFont(StyleContext sc, AttributeSet a, int defaultSize, StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        ss = getStyleSheet(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        int size = getFontSize(a, defaultSize, ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         * If the vertical alignment is set to either superscirpt or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         * subscript we reduce the font size by 2 points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        StringValue vAlignV = (StringValue)a.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                              (CSS.Attribute.VERTICAL_ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        if ((vAlignV != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            String vAlign = vAlignV.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            if ((vAlign.indexOf("sup") >= 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                (vAlign.indexOf("sub") >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                size -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        FontFamily familyValue = (FontFamily)a.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                                            (CSS.Attribute.FONT_FAMILY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        String family = (familyValue != null) ? familyValue.getValue() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                                  Font.SANS_SERIF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        int style = Font.PLAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        FontWeight weightValue = (FontWeight) a.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                                  (CSS.Attribute.FONT_WEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        if ((weightValue != null) && (weightValue.getValue() > 400)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            style |= Font.BOLD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        Object fs = a.getAttribute(CSS.Attribute.FONT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if ((fs != null) && (fs.toString().indexOf("italic") >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            style |= Font.ITALIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        if (family.equalsIgnoreCase("monospace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            family = Font.MONOSPACED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        Font f = sc.getFont(family, style, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        if (f == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            || (f.getFamily().equals(Font.DIALOG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                && ! family.equalsIgnoreCase(Font.DIALOG))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            family = Font.SANS_SERIF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            f = sc.getFont(family, style, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    static int getFontSize(AttributeSet attr, int defaultSize, StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        // PENDING(prinz) this is a 1.1 based implementation, need to also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        // have a 1.2 version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        FontSize sizeValue = (FontSize)attr.getAttribute(CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                                                         FONT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        return (sizeValue != null) ? sizeValue.getValue(attr, ss)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                                   : defaultSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * Takes a set of attributes and turn it into a color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * specification.  This might be used to specify things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * like brighter, more hue, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * This will return null if there is no value for <code>key</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @param key CSS.Attribute identifying where color is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @param a the set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @return the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    Color getColor(AttributeSet a, CSS.Attribute key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        ColorValue cv = (ColorValue) a.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (cv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            return cv.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Returns the size of a font from the passed in string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @param size CSS string describing font size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @param baseFontSize size to use for relative units.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    float getPointSize(String size, StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        int relSize, absSize, diff, index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        ss = getStyleSheet(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if (size != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            if (size.startsWith("+")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                relSize = Integer.valueOf(size.substring(1)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                return getPointSize(baseFontSize + relSize, ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            } else if (size.startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                relSize = -Integer.valueOf(size.substring(1)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                return getPointSize(baseFontSize + relSize, ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                absSize = Integer.valueOf(size).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                return getPointSize(absSize, ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * Returns the length of the attribute in <code>a</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * key <code>key</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    float getLength(AttributeSet a, CSS.Attribute key, StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        ss = getStyleSheet(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        LengthValue lv = (LengthValue) a.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        boolean isW3CLengthUnits = (ss == null) ? false : ss.isW3CLengthUnits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        float len = (lv != null) ? lv.getValue(isW3CLengthUnits) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * Convert a set of HTML attributes to an equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * set of CSS attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   786
     * @param htmlAttrSet AttributeSet containing the HTML attributes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @return AttributeSet containing the corresponding CSS attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *        The AttributeSet will be empty if there are no mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *        CSS attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        MutableAttributeSet cssAttrSet = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        Element elem = (Element)htmlAttrSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        HTML.Tag tag = getHTMLTag(htmlAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        if ((tag == HTML.Tag.TD) || (tag == HTML.Tag.TH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            // translate border width into the cells, if it has non-zero value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            AttributeSet tableAttr = elem.getParentElement().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                                     getParentElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            int borderWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                borderWidth = Integer.parseInt(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    (String) tableAttr.getAttribute(HTML.Attribute.BORDER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                borderWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            if (borderWidth > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                translateAttribute(HTML.Attribute.BORDER, tableAttr, cssAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            String pad = (String)tableAttr.getAttribute(HTML.Attribute.CELLPADDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            if (pad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                LengthValue v =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    (LengthValue)getInternalCSSValue(CSS.Attribute.PADDING_TOP, pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                v.span = (v.span < 0) ? 0 : v.span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                cssAttrSet.addAttribute(CSS.Attribute.PADDING_TOP, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                cssAttrSet.addAttribute(CSS.Attribute.PADDING_BOTTOM, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                cssAttrSet.addAttribute(CSS.Attribute.PADDING_LEFT, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                cssAttrSet.addAttribute(CSS.Attribute.PADDING_RIGHT, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (elem.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            translateEmbeddedAttributes(htmlAttrSet, cssAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            translateAttributes(tag, htmlAttrSet, cssAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        if (tag == HTML.Tag.CAPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
             * Navigator uses ALIGN for caption placement and IE uses VALIGN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            Object v = htmlAttrSet.getAttribute(HTML.Attribute.ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            if ((v != null) && (v.equals("top") || v.equals("bottom"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                cssAttrSet.addAttribute(CSS.Attribute.CAPTION_SIDE, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                cssAttrSet.removeAttribute(CSS.Attribute.TEXT_ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                v = htmlAttrSet.getAttribute(HTML.Attribute.VALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                    cssAttrSet.addAttribute(CSS.Attribute.CAPTION_SIDE, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        return cssAttrSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   843
    private static final Hashtable<String, Attribute> attributeMap = new Hashtable<String, Attribute>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   844
    private static final Hashtable<String, Value> valueMap = new Hashtable<String, Value>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * The hashtable and the static initalization block below,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * set up a mapping from well-known HTML attributes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * CSS attributes.  For the most part, there is a 1-1 mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * between the two.  However in the case of certain HTML
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * attributes for example HTML.Attribute.VSPACE or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * HTML.Attribute.HSPACE, end up mapping to two CSS.Attribute's.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Therefore, the value associated with each HTML.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * key ends up being an array of CSS.Attribute.* objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   856
    private static final Hashtable<HTML.Attribute, CSS.Attribute[]> htmlAttrToCssAttrMap = new Hashtable<HTML.Attribute, CSS.Attribute[]>(20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * The hashtable and static initialization that follows sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * up a translation from StyleConstants (i.e. the <em>well known</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * attributes) to the associated CSS attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   863
    private static final Hashtable<Object, Attribute> styleConstantToCssMap = new Hashtable<Object, Attribute>(17);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    /** Maps from HTML value to a CSS value. Used in internal mapping. */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   865
    private static final Hashtable<String, CSS.Value> htmlValueToCssValueMap = new Hashtable<String, CSS.Value>(8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    /** Maps from CSS value (string) to internal value. */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   867
    private static final Hashtable<String, CSS.Value> cssValueToInternalValueMap = new Hashtable<String, CSS.Value>(13);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        // load the attribute map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        for (int i = 0; i < Attribute.allAttributes.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            attributeMap.put(Attribute.allAttributes[i].toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                             Attribute.allAttributes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        // load the value map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        for (int i = 0; i < Value.allValues.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            valueMap.put(Value.allValues[i].toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                             Value.allValues[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        htmlAttrToCssAttrMap.put(HTML.Attribute.COLOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                 new CSS.Attribute[]{CSS.Attribute.COLOR});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        htmlAttrToCssAttrMap.put(HTML.Attribute.TEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                                 new CSS.Attribute[]{CSS.Attribute.COLOR});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        htmlAttrToCssAttrMap.put(HTML.Attribute.CLEAR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                                 new CSS.Attribute[]{CSS.Attribute.CLEAR});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        htmlAttrToCssAttrMap.put(HTML.Attribute.BACKGROUND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                                 new CSS.Attribute[]{CSS.Attribute.BACKGROUND_IMAGE});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        htmlAttrToCssAttrMap.put(HTML.Attribute.BGCOLOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                                 new CSS.Attribute[]{CSS.Attribute.BACKGROUND_COLOR});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        htmlAttrToCssAttrMap.put(HTML.Attribute.WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                                 new CSS.Attribute[]{CSS.Attribute.WIDTH});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        htmlAttrToCssAttrMap.put(HTML.Attribute.HEIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                                 new CSS.Attribute[]{CSS.Attribute.HEIGHT});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        htmlAttrToCssAttrMap.put(HTML.Attribute.BORDER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                                 new CSS.Attribute[]{CSS.Attribute.BORDER_TOP_WIDTH, CSS.Attribute.BORDER_RIGHT_WIDTH, CSS.Attribute.BORDER_BOTTOM_WIDTH, CSS.Attribute.BORDER_LEFT_WIDTH});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        htmlAttrToCssAttrMap.put(HTML.Attribute.CELLPADDING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                                 new CSS.Attribute[]{CSS.Attribute.PADDING});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        htmlAttrToCssAttrMap.put(HTML.Attribute.CELLSPACING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                                 new CSS.Attribute[]{CSS.Attribute.BORDER_SPACING});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        htmlAttrToCssAttrMap.put(HTML.Attribute.MARGINWIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                                 new CSS.Attribute[]{CSS.Attribute.MARGIN_LEFT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                                                     CSS.Attribute.MARGIN_RIGHT});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        htmlAttrToCssAttrMap.put(HTML.Attribute.MARGINHEIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                                 new CSS.Attribute[]{CSS.Attribute.MARGIN_TOP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                                                     CSS.Attribute.MARGIN_BOTTOM});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        htmlAttrToCssAttrMap.put(HTML.Attribute.HSPACE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                                 new CSS.Attribute[]{CSS.Attribute.PADDING_LEFT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                                                     CSS.Attribute.PADDING_RIGHT});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        htmlAttrToCssAttrMap.put(HTML.Attribute.VSPACE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                                 new CSS.Attribute[]{CSS.Attribute.PADDING_BOTTOM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                                                     CSS.Attribute.PADDING_TOP});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        htmlAttrToCssAttrMap.put(HTML.Attribute.FACE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                                 new CSS.Attribute[]{CSS.Attribute.FONT_FAMILY});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        htmlAttrToCssAttrMap.put(HTML.Attribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                                 new CSS.Attribute[]{CSS.Attribute.FONT_SIZE});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        htmlAttrToCssAttrMap.put(HTML.Attribute.VALIGN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                                 new CSS.Attribute[]{CSS.Attribute.VERTICAL_ALIGN});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        htmlAttrToCssAttrMap.put(HTML.Attribute.ALIGN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                                 new CSS.Attribute[]{CSS.Attribute.VERTICAL_ALIGN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                                                     CSS.Attribute.TEXT_ALIGN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                                                     CSS.Attribute.FLOAT});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        htmlAttrToCssAttrMap.put(HTML.Attribute.TYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                                 new CSS.Attribute[]{CSS.Attribute.LIST_STYLE_TYPE});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        htmlAttrToCssAttrMap.put(HTML.Attribute.NOWRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                                 new CSS.Attribute[]{CSS.Attribute.WHITE_SPACE});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        // initialize StyleConstants mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        styleConstantToCssMap.put(StyleConstants.FontFamily,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                                  CSS.Attribute.FONT_FAMILY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        styleConstantToCssMap.put(StyleConstants.FontSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                                  CSS.Attribute.FONT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        styleConstantToCssMap.put(StyleConstants.Bold,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                                  CSS.Attribute.FONT_WEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        styleConstantToCssMap.put(StyleConstants.Italic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                                  CSS.Attribute.FONT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        styleConstantToCssMap.put(StyleConstants.Underline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                                  CSS.Attribute.TEXT_DECORATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        styleConstantToCssMap.put(StyleConstants.StrikeThrough,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                                  CSS.Attribute.TEXT_DECORATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        styleConstantToCssMap.put(StyleConstants.Superscript,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                                  CSS.Attribute.VERTICAL_ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        styleConstantToCssMap.put(StyleConstants.Subscript,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                                  CSS.Attribute.VERTICAL_ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        styleConstantToCssMap.put(StyleConstants.Foreground,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                                  CSS.Attribute.COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        styleConstantToCssMap.put(StyleConstants.Background,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                                  CSS.Attribute.BACKGROUND_COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        styleConstantToCssMap.put(StyleConstants.FirstLineIndent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                                  CSS.Attribute.TEXT_INDENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        styleConstantToCssMap.put(StyleConstants.LeftIndent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                                  CSS.Attribute.MARGIN_LEFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        styleConstantToCssMap.put(StyleConstants.RightIndent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                                  CSS.Attribute.MARGIN_RIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        styleConstantToCssMap.put(StyleConstants.SpaceAbove,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                                  CSS.Attribute.MARGIN_TOP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        styleConstantToCssMap.put(StyleConstants.SpaceBelow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                                  CSS.Attribute.MARGIN_BOTTOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        styleConstantToCssMap.put(StyleConstants.Alignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                                  CSS.Attribute.TEXT_ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        // HTML->CSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        htmlValueToCssValueMap.put("disc", CSS.Value.DISC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        htmlValueToCssValueMap.put("square", CSS.Value.SQUARE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        htmlValueToCssValueMap.put("circle", CSS.Value.CIRCLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        htmlValueToCssValueMap.put("1", CSS.Value.DECIMAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        htmlValueToCssValueMap.put("a", CSS.Value.LOWER_ALPHA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        htmlValueToCssValueMap.put("A", CSS.Value.UPPER_ALPHA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        htmlValueToCssValueMap.put("i", CSS.Value.LOWER_ROMAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        htmlValueToCssValueMap.put("I", CSS.Value.UPPER_ROMAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        // CSS-> internal CSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        cssValueToInternalValueMap.put("none", CSS.Value.NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        cssValueToInternalValueMap.put("disc", CSS.Value.DISC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        cssValueToInternalValueMap.put("square", CSS.Value.SQUARE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        cssValueToInternalValueMap.put("circle", CSS.Value.CIRCLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        cssValueToInternalValueMap.put("decimal", CSS.Value.DECIMAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        cssValueToInternalValueMap.put("lower-roman", CSS.Value.LOWER_ROMAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        cssValueToInternalValueMap.put("upper-roman", CSS.Value.UPPER_ROMAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        cssValueToInternalValueMap.put("lower-alpha", CSS.Value.LOWER_ALPHA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        cssValueToInternalValueMap.put("upper-alpha", CSS.Value.UPPER_ALPHA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        cssValueToInternalValueMap.put("repeat", CSS.Value.BACKGROUND_REPEAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        cssValueToInternalValueMap.put("no-repeat",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                                       CSS.Value.BACKGROUND_NO_REPEAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        cssValueToInternalValueMap.put("repeat-x",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                                       CSS.Value.BACKGROUND_REPEAT_X);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        cssValueToInternalValueMap.put("repeat-y",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                                       CSS.Value.BACKGROUND_REPEAT_Y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        cssValueToInternalValueMap.put("scroll",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                                       CSS.Value.BACKGROUND_SCROLL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        cssValueToInternalValueMap.put("fixed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                                       CSS.Value.BACKGROUND_FIXED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        // Register all the CSS attribute keys for archival/unarchival
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        Object[] keys = CSS.Attribute.allAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        try {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   997
            for (Object key : keys) {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   998
                StyleContext.registerStaticAttributeKey(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        // Register all the CSS Values for archival/unarchival
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        keys = CSS.Value.allValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        try {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1007
            for (Object key : keys) {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1008
                StyleContext.registerStaticAttributeKey(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * Return the set of all possible CSS attribute keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    public static Attribute[] getAllAttributeKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        Attribute[] keys = new Attribute[Attribute.allAttributes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        System.arraycopy(Attribute.allAttributes, 0, keys, 0, Attribute.allAttributes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * Translates a string to a <code>CSS.Attribute</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * This will return <code>null</code> if there is no attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * by the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @param name the name of the CSS attribute to fetch the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *  typesafe enumeration for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @return the <code>CSS.Attribute</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *  or <code>null</code> if the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *  doesn't represent a valid attribute key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    public static final Attribute getAttribute(String name) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1036
        return attributeMap.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * Translates a string to a <code>CSS.Value</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * This will return <code>null</code> if there is no value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * by the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @param name the name of the CSS value to fetch the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     *  typesafe enumeration for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @return the <code>CSS.Value</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *  or <code>null</code> if the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *  doesn't represent a valid CSS value name; this does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *  not mean that it doesn't represent a valid CSS value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    static final Value getValue(String name) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1052
        return valueMap.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    // Conversion related methods/classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * Returns a URL for the given CSS url string. If relative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * <code>base</code> is used as the parent. If a valid URL can not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * be found, this will not throw a MalformedURLException, instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * null will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    static URL getURL(URL base, String cssString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        if (cssString == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        if (cssString.startsWith("url(") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            cssString.endsWith(")")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            cssString = cssString.substring(4, cssString.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        // Absolute first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            URL url = new URL(cssString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        } catch (MalformedURLException mue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        // Then relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        if (base != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            // Relative URL, try from base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                URL url = new URL(base, cssString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            catch (MalformedURLException muee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * Converts a type Color to a hex string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * in the format "#RRGGBB"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    static String colorToHex(Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1101
      String colorstr = "#";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
      // Red
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
      String str = Integer.toHexString(color.getRed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
      if (str.length() > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        str = str.substring(0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
      else if (str.length() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        colorstr += "0" + str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        colorstr += str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
      // Green
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
      str = Integer.toHexString(color.getGreen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
      if (str.length() > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        str = str.substring(0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
      else if (str.length() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        colorstr += "0" + str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        colorstr += str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
      // Blue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
      str = Integer.toHexString(color.getBlue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
      if (str.length() > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        str = str.substring(0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
      else if (str.length() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        colorstr += "0" + str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        colorstr += str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
      return colorstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
      * Convert a "#FFFFFF" hex string to a Color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
      * If the color specification is bad, an attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
      * will be made to fix it up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    static final Color hexToColor(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        String digits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        int n = value.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        if (value.startsWith("#")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            digits = value.substring(1, Math.min(value.length(), 7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            digits = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        String hstr = "0x" + digits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        Color c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            c = Color.decode(hstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
         return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * to a Color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    static Color stringToColor(String str) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1161
      Color color;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
      if (str == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
      if (str.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        color = Color.black;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
      else if (str.startsWith("rgb(")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
          color = parseRGB(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
      else if (str.charAt(0) == '#')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        color = hexToColor(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
      else if (str.equalsIgnoreCase("Black"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        color = hexToColor("#000000");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
      else if(str.equalsIgnoreCase("Silver"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        color = hexToColor("#C0C0C0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
      else if(str.equalsIgnoreCase("Gray"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        color = hexToColor("#808080");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
      else if(str.equalsIgnoreCase("White"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        color = hexToColor("#FFFFFF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
      else if(str.equalsIgnoreCase("Maroon"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        color = hexToColor("#800000");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
      else if(str.equalsIgnoreCase("Red"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        color = hexToColor("#FF0000");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
      else if(str.equalsIgnoreCase("Purple"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        color = hexToColor("#800080");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
      else if(str.equalsIgnoreCase("Fuchsia"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        color = hexToColor("#FF00FF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
      else if(str.equalsIgnoreCase("Green"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        color = hexToColor("#008000");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
      else if(str.equalsIgnoreCase("Lime"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        color = hexToColor("#00FF00");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
      else if(str.equalsIgnoreCase("Olive"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        color = hexToColor("#808000");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
      else if(str.equalsIgnoreCase("Yellow"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        color = hexToColor("#FFFF00");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
      else if(str.equalsIgnoreCase("Navy"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        color = hexToColor("#000080");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
      else if(str.equalsIgnoreCase("Blue"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        color = hexToColor("#0000FF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
      else if(str.equalsIgnoreCase("Teal"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        color = hexToColor("#008080");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
      else if(str.equalsIgnoreCase("Aqua"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        color = hexToColor("#00FFFF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
      else if(str.equalsIgnoreCase("Orange"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        color = hexToColor("#FF8000");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
          color = hexToColor(str); // sometimes get specified without leading #
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
      return color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * Parses a String in the format <code>rgb(r, g, b)</code> where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * each of the Color components is either an integer, or a floating number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * with a % after indicating a percentage value of 255. Values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * constrained to fit with 0-255. The resulting Color is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    private static Color parseRGB(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        // Find the next numeric char
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        int[] index = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        index[0] = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        int red = getColorComponent(string, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        int green = getColorComponent(string, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        int blue = getColorComponent(string, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        return new Color(red, green, blue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * Returns the next integer value from <code>string</code> starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * at <code>index[0]</code>. The value can either can an integer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * a percentage (floating number ending with %), in which case it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * multiplied by 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    private static int getColorComponent(String string, int[] index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        int length = string.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        char aChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        // Skip non-decimal chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        while(index[0] < length && (aChar = string.charAt(index[0])) != '-' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
              !Character.isDigit(aChar) && aChar != '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            index[0]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        int start = index[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        if (start < length && string.charAt(index[0]) == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            index[0]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        while(index[0] < length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                         Character.isDigit(string.charAt(index[0]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            index[0]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        if (index[0] < length && string.charAt(index[0]) == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            // Decimal value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            index[0]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            while(index[0] < length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                  Character.isDigit(string.charAt(index[0]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                index[0]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        if (start != index[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                float value = Float.parseFloat(string.substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                                               (start, index[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                if (index[0] < length && string.charAt(index[0]) == '%') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                    index[0]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                    value = value * 255f / 100f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                return Math.min(255, Math.max(0, (int)value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                // Treat as 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    static int getIndexOfSize(float pt, int[] sizeMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        for (int i = 0; i < sizeMap.length; i ++ )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                if (pt <= sizeMap[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                        return i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        return sizeMap.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    static int getIndexOfSize(float pt, StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        int[] sizeMap = (ss != null) ? ss.getSizeMap() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            StyleSheet.sizeMapDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        return getIndexOfSize(pt, sizeMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * @return an array of all the strings in <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *         that are separated by whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    static String[] parseStrings(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        int         current, last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        int         length = (value == null) ? 0 : value.length();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1301
        Vector<String> temp = new Vector<String>(4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        current = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        while (current < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            // Skip ws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            while (current < length && Character.isWhitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                   (value.charAt(current))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                current++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            last = current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            while (current < length && !Character.isWhitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                   (value.charAt(current))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                current++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            if (last != current) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                temp.addElement(value.substring(last, current));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            current++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        String[] retValue = new String[temp.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        temp.copyInto(retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * Return the point size, given a size index. Legal HTML index sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * are 1-7.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    float getPointSize(int index, StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        ss = getStyleSheet(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        int[] sizeMap = (ss != null) ? ss.getSizeMap() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            StyleSheet.sizeMapDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        --index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        if (index < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
          return sizeMap[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        else if (index > sizeMap.length - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
          return sizeMap[sizeMap.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
          return sizeMap[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    private void translateEmbeddedAttributes(AttributeSet htmlAttrSet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                                             MutableAttributeSet cssAttrSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        Enumeration keys = htmlAttrSet.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        if (htmlAttrSet.getAttribute(StyleConstants.NameAttribute) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            HTML.Tag.HR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            // HR needs special handling due to us treating it as a leaf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            translateAttributes(HTML.Tag.HR, htmlAttrSet, cssAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            Object key = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            if (key instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                HTML.Tag tag = (HTML.Tag)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                Object o = htmlAttrSet.getAttribute(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                if (o != null && o instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                    translateAttributes(tag, (AttributeSet)o, cssAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            } else if (key instanceof CSS.Attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                cssAttrSet.addAttribute(key, htmlAttrSet.getAttribute(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    private void translateAttributes(HTML.Tag tag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                                            AttributeSet htmlAttrSet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                                            MutableAttributeSet cssAttrSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        Enumeration names = htmlAttrSet.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            if (name instanceof HTML.Attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                HTML.Attribute key = (HTML.Attribute)name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                 * HTML.Attribute.ALIGN needs special processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                 * It can map to to 1 of many(3) possible CSS attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                 * depending on the nature of the tag the attribute is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                 * part off and depending on the value of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                if (key == HTML.Attribute.ALIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                    String htmlAttrValue = (String)htmlAttrSet.getAttribute(HTML.Attribute.ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                    if (htmlAttrValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                        CSS.Attribute cssAttr = getCssAlignAttribute(tag, htmlAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                        if (cssAttr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                            Object o = getCssValue(cssAttr, htmlAttrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                            if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                                cssAttrSet.addAttribute(cssAttr, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                     * The html size attribute has a mapping in the CSS world only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                     * if it is par of a font or base font tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                    if (key == HTML.Attribute.SIZE && !isHTMLFontTag(tag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                    translateAttribute(key, htmlAttrSet, cssAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            } else if (name instanceof CSS.Attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                cssAttrSet.addAttribute(name, htmlAttrSet.getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    private void translateAttribute(HTML.Attribute key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                                           AttributeSet htmlAttrSet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                                           MutableAttributeSet cssAttrSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
         * In the case of all remaining HTML.Attribute's they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
         * map to 1 or more CCS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        CSS.Attribute[] cssAttrList = getCssAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        String htmlAttrValue = (String)htmlAttrSet.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        if (cssAttrList == null || htmlAttrValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1425
        for (Attribute cssAttr : cssAttrList) {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1426
            Object o = getCssValue(cssAttr, htmlAttrValue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            if (o != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1428
                cssAttrSet.addAttribute(cssAttr , o);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * Given a CSS.Attribute object and its corresponding HTML.Attribute's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * value, this method returns a CssValue object to associate with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * CSS attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * @param the CSS.Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * @param a String containing the value associated HTML.Attribtue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    Object getCssValue(CSS.Attribute cssAttr, String htmlAttrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        CssValue value = (CssValue)valueConvertor.get(cssAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        Object o = value.parseHtmlValue(htmlAttrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * Maps an HTML.Attribute object to its appropriate CSS.Attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * @param HTML.Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * @return CSS.Attribute[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    private CSS.Attribute[] getCssAttribute(HTML.Attribute hAttr) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  1454
        return htmlAttrToCssAttrMap.get(hAttr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * Maps HTML.Attribute.ALIGN to either:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     *     CSS.Attribute.TEXT_ALIGN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     *     CSS.Attribute.FLOAT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     *     CSS.Attribute.VERTICAL_ALIGN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * based on the tag associated with the attribute and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     * value of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * @param AttributeSet containing HTML attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * @return CSS.Attribute mapping for HTML.Attribute.ALIGN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    private CSS.Attribute getCssAlignAttribute(HTML.Tag tag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                                                   AttributeSet htmlAttrSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        return CSS.Attribute.TEXT_ALIGN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        String htmlAttrValue = (String)htmlAttrSet.getAttribute(HTML.Attribute.ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        CSS.Attribute cssAttr = CSS.Attribute.TEXT_ALIGN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        if (htmlAttrValue != null && htmlAttrSet instanceof Element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            Element elem = (Element)htmlAttrSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            if (!elem.isLeaf() && tag.isBlock() && validTextAlignValue(htmlAttrValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                return CSS.Attribute.TEXT_ALIGN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            } else if (isFloater(htmlAttrValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                return CSS.Attribute.FLOAT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            } else if (elem.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                return CSS.Attribute.VERTICAL_ALIGN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * Fetches the tag associated with the HTML AttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * @param  AttributeSet containing the HTML attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * @return HTML.Tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    private HTML.Tag getHTMLTag(AttributeSet htmlAttrSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        Object o = htmlAttrSet.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        if (o instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            HTML.Tag tag = (HTML.Tag) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            return tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    private boolean isHTMLFontTag(HTML.Tag tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        return (tag != null && ((tag == HTML.Tag.FONT) || (tag == HTML.Tag.BASEFONT)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    private boolean isFloater(String alignValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        return (alignValue.equals("left") || alignValue.equals("right"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    private boolean validTextAlignValue(String alignValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        return (isFloater(alignValue) || alignValue.equals("center"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * Base class to CSS values in the attribute sets.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * is intended to act as a convertor to/from other attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * The CSS parser uses the parseCssValue method to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * a string to whatever format is appropriate a given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * (i.e. these convertors are stored in a map using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * CSS.Attribute as a key and the CssValue as the value).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * The HTML to CSS conversion process first converts the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * HTML.Attribute to a CSS.Attribute, and then calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * the parseHtmlValue method on the value of the HTML
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * attribute to produce the corresponding CSS value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * The StyleConstants to CSS conversion process first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * converts the StyleConstants attribute to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * CSS.Attribute, and then calls the fromStyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * method to convert the StyleConstants value to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * CSS value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * The CSS to StyleConstants conversion process first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * converts the StyleConstants attribute to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * CSS.Attribute, and then calls the toStyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * method to convert the CSS value to a StyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    static class CssValue implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
         * Convert a CSS value string to the internal format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
         * (for fast processing) used in the attribute sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
         * The fallback storage for any value that we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
         * have a special binary format for is a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
         * Convert an HTML attribute value to a CSS attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
         * value.  If there is no conversion, return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
         * This is implemented to simply forward to the CSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
         * parsing by default (since some of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
         * values are the same).  If the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
         * isn't recognized as a CSS value it is generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
         * returned as null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        Object parseHtmlValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            return parseCssValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
         * Converts a <code>StyleConstants</code> attribute value to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
         * a CSS attribute value.  If there is no conversion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
         * returns <code>null</code>.  By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
         * @param value the value of a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
         *   attribute to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
         * @return the CSS value that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
         *   <code>StyleConstants</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        Object fromStyleConstants(StyleConstants key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
         * Converts a CSS attribute value to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
         * <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
         * value.  If there is no conversion, returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
         * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
         * By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
         * @param v the view containing <code>AttributeSet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
         * @return the <code>StyleConstants</code> attribute value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
         *   represents the CSS attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        Object toStyleConstants(StyleConstants key, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
         * Return the CSS format of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            return svalue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
         * The value as a string... before conversion to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
         * binary format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        String svalue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * By default CSS attributes are represented as simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * strings.  They also have no conversion to/from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * StyleConstants by default. This class represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * value as a string (via the superclass), but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * provides StyleConstants conversion support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * CSS attributes that are held as strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    static class StringValue extends CssValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
         * Convert a CSS value string to the internal format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
         * (for fast processing) used in the attribute sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
         * This produces a StringValue, so that it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
         * used to convert from CSS to StyleConstants values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            StringValue sv = new StringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            sv.svalue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            return sv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
         * Converts a <code>StyleConstants</code> attribute value to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
         * a CSS attribute value.  If there is no conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
         * returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
         * @param value the value of a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
         *   attribute to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
         * @return the CSS value that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
         *   <code>StyleConstants</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        Object fromStyleConstants(StyleConstants key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            if (key == StyleConstants.Italic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                if (value.equals(Boolean.TRUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                    return parseCssValue("italic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                return parseCssValue("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            } else if (key == StyleConstants.Underline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                if (value.equals(Boolean.TRUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                    return parseCssValue("underline");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                return parseCssValue("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            } else if (key == StyleConstants.Alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                int align = ((Integer)value).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                String ta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                switch(align) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                case StyleConstants.ALIGN_LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                    ta = "left";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                case StyleConstants.ALIGN_RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                    ta = "right";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                case StyleConstants.ALIGN_CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                    ta = "center";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                case StyleConstants.ALIGN_JUSTIFIED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                    ta = "justify";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                    ta = "left";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                return parseCssValue(ta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            } else if (key == StyleConstants.StrikeThrough) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                if (value.equals(Boolean.TRUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                    return parseCssValue("line-through");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                return parseCssValue("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            } else if (key == StyleConstants.Superscript) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                if (value.equals(Boolean.TRUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                    return parseCssValue("super");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                return parseCssValue("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            } else if (key == StyleConstants.Subscript) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                if (value.equals(Boolean.TRUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                    return parseCssValue("sub");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                return parseCssValue("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
         * Converts a CSS attribute value to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
         * <code>StyleConstants</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
         * If there is no conversion, returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
         * By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
         * @return the <code>StyleConstants</code> attribute value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
         *   represents the CSS attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        Object toStyleConstants(StyleConstants key, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            if (key == StyleConstants.Italic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                if (svalue.indexOf("italic") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                    return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            } else if (key == StyleConstants.Underline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                if (svalue.indexOf("underline") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                    return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            } else if (key == StyleConstants.Alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                if (svalue.equals("right")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                    return new Integer(StyleConstants.ALIGN_RIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                } else if (svalue.equals("center")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                    return new Integer(StyleConstants.ALIGN_CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                } else if  (svalue.equals("justify")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                    return new Integer(StyleConstants.ALIGN_JUSTIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                return new Integer(StyleConstants.ALIGN_LEFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            } else if (key == StyleConstants.StrikeThrough) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                if (svalue.indexOf("line-through") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                    return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            } else if (key == StyleConstants.Superscript) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                if (svalue.indexOf("super") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                    return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            } else if (key == StyleConstants.Subscript) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                if (svalue.indexOf("sub") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                    return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        // Used by ViewAttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        boolean isItalic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            return (svalue.indexOf("italic") != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        boolean isStrike() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            return (svalue.indexOf("line-through") != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        boolean isUnderline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            return (svalue.indexOf("underline") != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        boolean isSub() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            return (svalue.indexOf("sub") != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        boolean isSup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            return (svalue.indexOf("sup") != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * Represents a value for the CSS.FONT_SIZE attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * The binary format of the value can be one of several
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * types.  If the type is Float,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * the value is specified in terms of point or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * percentage, depending upon the ending of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * associated string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * If the type is Integer, the value is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * in terms of a size index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
    class FontSize extends CssValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
         * Returns the size in points.  This is ultimately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
         * what we need for the purpose of creating/fetching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
         * a Font object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
         * @param a the attribute set the value is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
         *  requested from.  We may need to walk up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
         *  resolve hierarchy if it's relative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        int getValue(AttributeSet a, StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            ss = getStyleSheet(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            if (index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                // it's an index, translate from size table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                return Math.round(getPointSize((int) value, ss));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            else if (lu == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                return Math.round(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                if (lu.type == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                    boolean isW3CLengthUnits = (ss == null) ? false : ss.isW3CLengthUnits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                    return Math.round(lu.getValue(isW3CLengthUnits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                    AttributeSet resolveParent = a.getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                    if (resolveParent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                        int pValue = StyleConstants.getFontSize(resolveParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                        float retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                        if (lu.type == 1 || lu.type == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                            retValue = lu.value * (float)pValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                            retValue = lu.value + (float)pValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                        return Math.round(retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                // a is null, or no resolve parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                return 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            FontSize fs = new FontSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            fs.svalue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                if (value.equals("xx-small")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                    fs.value = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                    fs.index = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                } else if (value.equals("x-small")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                    fs.value = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                    fs.index = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                } else if (value.equals("small")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                    fs.value = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                    fs.index = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                } else if (value.equals("medium")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                    fs.value = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                    fs.index = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                } else if (value.equals("large")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                    fs.value = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                    fs.index = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                } else if (value.equals("x-large")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                    fs.value = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                    fs.index = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                } else if (value.equals("xx-large")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                    fs.value = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                    fs.index = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                    fs.lu = new LengthUnit(value, (short)1, 1f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                // relative sizes, larger | smaller (adjust from parent by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                // 1.5 pixels)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                // em, ex refer to parent sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                // lengths: pt, mm, cm, pc, in, px
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                //          em (font height 3em would be 3 times font height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                //          ex (height of X)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                // lengths are (+/-) followed by a number and two letter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                // unit identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                fs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            return fs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        Object parseHtmlValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            if ((value == null) || (value.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            FontSize fs = new FontSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
            fs.svalue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                 * relative sizes in the size attribute are relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                 * to the <basefont>'s size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                int baseFontSize = getBaseFontSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                if (value.charAt(0) == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                    int relSize = Integer.valueOf(value.substring(1)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                    fs.value = baseFontSize + relSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                    fs.index = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                } else if (value.charAt(0) == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                    int relSize = -Integer.valueOf(value.substring(1)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                    fs.value = baseFontSize + relSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                    fs.index = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                    fs.value = Integer.parseInt(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                    if (fs.value > 7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                        fs.value = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                    } else if (fs.value < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                        fs.value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                    fs.index = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                fs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            return fs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
         * Converts a <code>StyleConstants</code> attribute value to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
         * a CSS attribute value.  If there is no conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
         * returns <code>null</code>.  By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
         * @param value the value of a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
         *   attribute to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
         * @return the CSS value that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
         *   <code>StyleConstants</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        Object fromStyleConstants(StyleConstants key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            if (value instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                FontSize fs = new FontSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                fs.value = getIndexOfSize(((Number)value).floatValue(), StyleSheet.sizeMapDefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                fs.svalue = Integer.toString((int)fs.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                fs.index = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                return fs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            return parseCssValue(value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
         * Converts a CSS attribute value to a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
         * value.  If there is no conversion, returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
         * By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
         * @return the <code>StyleConstants</code> attribute value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
         *   represents the CSS attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        Object toStyleConstants(StyleConstants key, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                return Integer.valueOf(getValue(v.getAttributes(), null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            return Integer.valueOf(getValue(null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
        float value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        boolean index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        LengthUnit lu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    static class FontFamily extends CssValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
         * Returns the font family to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        String getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
            return family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            int cIndex = value.indexOf(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            FontFamily ff = new FontFamily();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            ff.svalue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            ff.family = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            if (cIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                setFontName(ff, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                int lastIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                int length = value.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                cIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                    // skip ws.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                    while (cIndex < length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                           Character.isWhitespace(value.charAt(cIndex)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                        cIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                    // Find next ','
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                    lastIndex = cIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                    cIndex = value.indexOf(',', cIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                    if (cIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                        cIndex = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                    if (lastIndex < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                        if (lastIndex != cIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                            int lastCharIndex = cIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                            if (cIndex > 0 && value.charAt(cIndex - 1) == ' '){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                                lastCharIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                            setFontName(ff, value.substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                                        (lastIndex, lastCharIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                            done = (ff.family != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                        cIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            if (ff.family == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                ff.family = Font.SANS_SERIF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            return ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        private void setFontName(FontFamily ff, String fontName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            ff.family = fontName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        Object parseHtmlValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            // TBD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            return parseCssValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
         * Converts a <code>StyleConstants</code> attribute value to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
         * a CSS attribute value.  If there is no conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
         * returns <code>null</code>.  By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
         * @param value the value of a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
         *   attribute to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
         * @return the CSS value that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
         *   <code>StyleConstants</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        Object fromStyleConstants(StyleConstants key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            return parseCssValue(value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
         * Converts a CSS attribute value to a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
         * value.  If there is no conversion, returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
         * By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
         * @return the <code>StyleConstants</code> attribute value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
         *   represents the CSS attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        Object toStyleConstants(StyleConstants key, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
            return family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        String family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    static class FontWeight extends CssValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            return weight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
            FontWeight fw = new FontWeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            fw.svalue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            if (value.equals("bold")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                fw.weight = 700;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
            } else if (value.equals("normal")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                fw.weight = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                // PENDING(prinz) add support for relative values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                    fw.weight = Integer.parseInt(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                    fw = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            return fw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
         * Converts a <code>StyleConstants</code> attribute value to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
         * a CSS attribute value.  If there is no conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
         * returns <code>null</code>.  By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
         * @param value the value of a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
         *   attribute to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
         * @return the CSS value that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
         *   <code>StyleConstants</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        Object fromStyleConstants(StyleConstants key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            if (value.equals(Boolean.TRUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                return parseCssValue("bold");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            return parseCssValue("normal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
         * Converts a CSS attribute value to a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
         * value.  If there is no conversion, returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
         * By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
         * @return the <code>StyleConstants</code> attribute value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
         *   represents the CSS attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        Object toStyleConstants(StyleConstants key, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
            return (weight > 500) ? Boolean.TRUE : Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        boolean isBold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
            return (weight > 500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
        int weight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
    static class ColorValue extends CssValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
         * Returns the color to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        Color getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            Color c = stringToColor(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                ColorValue cv = new ColorValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                cv.svalue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                cv.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                return cv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        Object parseHtmlValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            return parseCssValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
         * Converts a <code>StyleConstants</code> attribute value to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
         * a CSS attribute value.  If there is no conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
         * returns <code>null</code>.  By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
         * @param value the value of a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
         *   attribute to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
         * @return the CSS value that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
         *   <code>StyleConstants</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        Object fromStyleConstants(StyleConstants key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            ColorValue colorValue = new ColorValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            colorValue.c = (Color)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
            colorValue.svalue = colorToHex(colorValue.c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            return colorValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
         * Converts a CSS attribute value to a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
         * value.  If there is no conversion, returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
         * By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
         * @return the <code>StyleConstants</code> attribute value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
         *   represents the CSS attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        Object toStyleConstants(StyleConstants key, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        Color c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
    static class BorderStyle extends CssValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        CSS.Value getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            CSS.Value cssv = CSS.getValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            if (cssv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                if ((cssv == CSS.Value.INSET) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                    (cssv == CSS.Value.OUTSET) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                    (cssv == CSS.Value.NONE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                    (cssv == CSS.Value.DOTTED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                    (cssv == CSS.Value.DASHED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                    (cssv == CSS.Value.SOLID) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                    (cssv == CSS.Value.DOUBLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                    (cssv == CSS.Value.GROOVE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                    (cssv == CSS.Value.RIDGE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                    BorderStyle bs = new BorderStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                    bs.svalue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                    bs.style = cssv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                    return bs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                     throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                s.writeObject(style.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
            s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
            Object value = s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                style = CSS.getValue((String)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        // CSS.Values are static, don't archive it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        transient private CSS.Value style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
    static class LengthValue extends CssValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
         * if this length value may be negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
        boolean mayBeNegative;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        LengthValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
            this(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        LengthValue(boolean mayBeNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            this.mayBeNegative = mayBeNegative;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
         * Returns the length (span) to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        float getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            return getValue(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
        float getValue(boolean isW3CLengthUnits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            return getValue(0, isW3CLengthUnits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
         * Returns the length (span) to use. If the value represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
         * a percentage, it is scaled based on <code>currentValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
        float getValue(float currentValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            return getValue(currentValue, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        float getValue(float currentValue, boolean isW3CLengthUnits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            if (percentage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                return span * currentValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            return LengthUnit.getValue(span, units, isW3CLengthUnits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
         * Returns true if the length represents a percentage of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
         * containing box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        boolean isPercentage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
            return percentage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            LengthValue lv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                // Assume pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                float absolute = Float.valueOf(value).floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                lv = new LengthValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                lv.span = absolute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                // Not pixels, use LengthUnit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
                LengthUnit lu = new LengthUnit(value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                                               LengthUnit.UNINITALIZED_LENGTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                                               0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                // PENDING: currently, we only support absolute values and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                // percentages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                switch (lu.type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                    // Absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                    lv = new LengthValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                    lv.span =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                        (mayBeNegative) ? lu.value : Math.max(0, lu.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                    lv.units = lu.units;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                    // %
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                    lv = new LengthValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                    lv.span = Math.max(0, Math.min(1, lu.value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                    lv.percentage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
            lv.svalue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            return lv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
        Object parseHtmlValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            if (value.equals(HTML.NULL_ATTRIBUTE_VALUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                value = "1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
            return parseCssValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
         * Converts a <code>StyleConstants</code> attribute value to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
         * a CSS attribute value.  If there is no conversion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
         * returns <code>null</code>.  By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
         * @param value the value of a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
         *   attribute to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
         * @return the CSS value that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
         *   <code>StyleConstants</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
        Object fromStyleConstants(StyleConstants key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
            LengthValue v = new LengthValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            v.svalue = value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
            v.span = ((Float)value).floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
         * Converts a CSS attribute value to a <code>StyleConstants</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
         * value.  If there is no conversion, returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
         * By default, there is no conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
         * @param key the <code>StyleConstants</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
         * @return the <code>StyleConstants</code> attribute value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
         *   represents the CSS attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
        Object toStyleConstants(StyleConstants key, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            return new Float(getValue(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        /** If true, span is a percentage value, and that to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
         * the length another value needs to be passed in. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        boolean percentage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        /** Either the absolute value (percentage == false) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
         * a percentage value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        float span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        String units = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
     * BorderWidthValue is used to model BORDER_XXX_WIDTH and adds support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
     * for the thin/medium/thick values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
    static class BorderWidthValue extends LengthValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
        BorderWidthValue(String svalue, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            this.svalue = svalue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            span = values[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
            percentage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                if (value.equals("thick")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                    return new BorderWidthValue(value, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                else if (value.equals("medium")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                    return new BorderWidthValue(value, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                else if (value.equals("thin")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                    return new BorderWidthValue(value, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
            // Assume its a length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            return super.parseCssValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        Object parseHtmlValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
            if (value == HTML.NULL_ATTRIBUTE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                return parseCssValue("medium");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
            return parseCssValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        /** Values used to represent border width. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        private static final float[] values = { 1, 2, 4 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * Handles uniquing of CSS values, like lists, and background image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * repeating.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
    static class CssValueMapper extends CssValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            Object retValue = cssValueToInternalValueMap.get(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            if (retValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                retValue = cssValueToInternalValueMap.get(value.toLowerCase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        Object parseHtmlValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            Object retValue = htmlValueToCssValueMap.get(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            if (retValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                retValue = htmlValueToCssValueMap.get(value.toLowerCase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     * Used for background images, to represent the position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
    static class BackgroundPosition extends CssValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        float horizontalPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
        float verticalPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        // bitmask: bit 0, horizontal relative, bit 1 horizontal relative to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
        // font size, 2 vertical relative to size, 3 vertical relative to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        // font size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
        short relative;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
            // 'top left' and 'left top' both mean the same as '0% 0%'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
            // 'top', 'top center' and 'center top' mean the same as '50% 0%'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
            // 'right top' and 'top right' mean the same as '100% 0%'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
            // 'left', 'left center' and 'center left' mean the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
            //        '0% 50%'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            // 'center' and 'center center' mean the same as '50% 50%'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
            // 'right', 'right center' and 'center right' mean the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
            //        '100% 50%'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
            // 'bottom left' and 'left bottom' mean the same as '0% 100%'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
            // 'bottom', 'bottom center' and 'center bottom' mean the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
            //        '50% 100%'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
            // 'bottom right' and 'right bottom' mean the same as '100% 100%'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
            String[]  strings = CSS.parseStrings(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
            int count = strings.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
            BackgroundPosition bp = new BackgroundPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
            bp.relative = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
            bp.svalue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
            if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
                // bit 0 for vert, 1 hor, 2 for center
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                short found = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                while (index < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
                    // First, check for keywords
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
                    String string = strings[index++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
                    if (string.equals("center")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
                        found |= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                        if ((found & 1) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
                            if (string.equals("top")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
                                found |= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
                            else if (string.equals("bottom")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
                                found |= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
                                bp.verticalPosition = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
                        if ((found & 2) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
                            if (string.equals("left")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                                found |= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
                                bp.horizontalPosition = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                            else if (string.equals("right")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                                found |= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                                bp.horizontalPosition = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                if (found != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                    if ((found & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                        if ((found & 2) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                            // vert and no horiz.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                            bp.horizontalPosition = .5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                    else if ((found & 2) == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
                        // horiz and no vert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                        bp.verticalPosition = .5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                        // no horiz, no vert, but center
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                        bp.horizontalPosition = bp.verticalPosition = .5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                    // Assume lengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                    LengthUnit lu = new LengthUnit(strings[0], (short)0, 0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                    if (lu.type == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                        bp.horizontalPosition = lu.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
                        bp.relative = (short)(1 ^ bp.relative);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
                    else if (lu.type == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
                        bp.horizontalPosition = lu.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                    else if (lu.type == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
                        bp.horizontalPosition = lu.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
                        bp.relative = (short)((1 ^ bp.relative) | 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                    if (count > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
                        lu = new LengthUnit(strings[1], (short)0, 0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                        if (lu.type == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                            bp.verticalPosition = lu.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
                            bp.relative = (short)(4 ^ bp.relative);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
                        else if (lu.type == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                            bp.verticalPosition = lu.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                        else if (lu.type == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                            bp.verticalPosition = lu.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
                            bp.relative = (short)((4 ^ bp.relative) | 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                        bp.verticalPosition = .5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            return bp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
        boolean isHorizontalPositionRelativeToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
            return ((relative & 1) == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
        boolean isHorizontalPositionRelativeToFontSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
            return ((relative & 2) == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
        float getHorizontalPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
            return horizontalPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
        boolean isVerticalPositionRelativeToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
            return ((relative & 4) == 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
        boolean isVerticalPositionRelativeToFontSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
            return ((relative & 8) == 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
        float getVerticalPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
            return verticalPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
     * Used for BackgroundImages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
    static class BackgroundImage extends CssValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
        private boolean    loadedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        private ImageIcon  image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
        Object parseCssValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
            BackgroundImage retValue = new BackgroundImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
            retValue.svalue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
        Object parseHtmlValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
            return parseCssValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        // PENDING: this base is wrong for linked style sheets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
        ImageIcon getImage(URL base) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
            if (!loadedImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                    if (!loadedImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                        URL url = CSS.getURL(base, svalue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                        loadedImage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                        if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                            image = new ImageIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                            Image tmpImg = Toolkit.getDefaultToolkit().createImage(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                            if (tmpImg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                                image.setImage(tmpImg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
            return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
     * Parses a length value, this is used internally, and never added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
     * to an AttributeSet or returned to the developer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
    static class LengthUnit implements Serializable {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2601
        static Hashtable<String, Float> lengthMapping = new Hashtable<String, Float>(6);
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2602
        static Hashtable<String, Float> w3cLengthMapping = new Hashtable<String, Float>(6);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
            lengthMapping.put("pt", new Float(1f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            // Not sure about 1.3, determined by experiementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
            lengthMapping.put("px", new Float(1.3f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
            lengthMapping.put("mm", new Float(2.83464f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            lengthMapping.put("cm", new Float(28.3464f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
            lengthMapping.put("pc", new Float(12f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            lengthMapping.put("in", new Float(72f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
            int res = 72;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                res = Toolkit.getDefaultToolkit().getScreenResolution();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
            } catch (HeadlessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
            // mapping according to the CSS2 spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
            w3cLengthMapping.put("pt", new Float(res/72f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
            w3cLengthMapping.put("px", new Float(1f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
            w3cLengthMapping.put("mm", new Float(res/25.4f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
            w3cLengthMapping.put("cm", new Float(res/2.54f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
            w3cLengthMapping.put("pc", new Float(res/6f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
            w3cLengthMapping.put("in", new Float(res));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
        LengthUnit(String value, short defaultType, float defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
            parse(value, defaultType, defaultValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
        void parse(String value, short defaultType, float defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            type = defaultType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            this.value = defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
            int length = value.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            if (length > 0 && value.charAt(length - 1) == '%') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
                    this.value = Float.valueOf(value.substring(0, length - 1)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
                                               floatValue() / 100.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                    type = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                catch (NumberFormatException nfe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
            if (length >= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                units = value.substring(length - 2, length);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2644
                Float scale = lengthMapping.get(units);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                if (scale != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                        this.value = Float.valueOf(value.substring(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
                               length - 2)).floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                        type = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                    catch (NumberFormatException nfe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                else if (units.equals("em") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                         units.equals("ex")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                        this.value = Float.valueOf(value.substring(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                                      length - 2)).floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                        type = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
                    catch (NumberFormatException nfe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                else if (value.equals("larger")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
                    this.value = 2f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
                    type = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
                else if (value.equals("smaller")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                    this.value = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
                    type = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
                    // treat like points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
                        this.value = Float.valueOf(value).floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
                        type = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
                    } catch (NumberFormatException nfe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
            else if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
                // treat like points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
                    this.value = Float.valueOf(value).floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
                    type = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                } catch (NumberFormatException nfe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        float getValue(boolean w3cLengthUnits) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2688
            Hashtable<String, Float> mapping = (w3cLengthUnits) ? w3cLengthMapping : lengthMapping;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
            float scale = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
            if (units != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2691
                Float scaleFloat = mapping.get(units);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                if (scaleFloat != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
                    scale = scaleFloat.floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
            return this.value * scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
        static float getValue(float value, String units, Boolean w3cLengthUnits) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2701
            Hashtable<String, Float> mapping = (w3cLengthUnits) ? w3cLengthMapping : lengthMapping;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
            float scale = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
            if (units != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  2704
                Float scaleFloat = mapping.get(units);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
                if (scaleFloat != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
                    scale = scaleFloat.floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
            return value * scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
            return type + " " + value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
        // 0 - value indicates real value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
        // 1 - % value, value relative to depends upon key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
        //     50% will have a value = .5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
        // 2 - add value to parent value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
        // 3 - em/ex relative to font size of element (except for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
        //     font-size, which is relative to parent).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
        short type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
        float value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
        String units = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
        static final short UNINITALIZED_LENGTH = (short)10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
     * Class used to parse font property. The font property is shorthand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
     * for the other font properties. This expands the properties, placing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
     * them in the attributeset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
    static class ShorthandFontParser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
         * Parses the shorthand font string <code>value</code>, placing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
         * result in <code>attr</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
        static void parseShorthandFont(CSS css, String value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
                                       MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
            // font is of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
            // [ <font-style> || <font-variant> || <font-weight> ]? <font-size>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
            //   [ / <line-height> ]? <font-family>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
            String[]   strings = CSS.parseStrings(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
            int        count = strings.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
            int        index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
            // bitmask, 1 for style, 2 for variant, 3 for weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
            short      found = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
            int        maxC = Math.min(3, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
            // Check for font-style font-variant font-weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
            while (index < maxC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
                if ((found & 1) == 0 && isFontStyle(strings[index])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
                    css.addInternalCSSValue(attr, CSS.Attribute.FONT_STYLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                                            strings[index++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                    found |= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
                else if ((found & 2) == 0 && isFontVariant(strings[index])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
                    css.addInternalCSSValue(attr, CSS.Attribute.FONT_VARIANT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
                                            strings[index++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
                    found |= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                else if ((found & 4) == 0 && isFontWeight(strings[index])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
                    css.addInternalCSSValue(attr, CSS.Attribute.FONT_WEIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                                            strings[index++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                    found |= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                else if (strings[index].equals("normal")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
            if ((found & 1) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
                css.addInternalCSSValue(attr, CSS.Attribute.FONT_STYLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
                                        "normal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
            if ((found & 2) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
                css.addInternalCSSValue(attr, CSS.Attribute.FONT_VARIANT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                                        "normal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
            if ((found & 4) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
                css.addInternalCSSValue(attr, CSS.Attribute.FONT_WEIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
                                        "normal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
            // string at index should be the font-size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
            if (index < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
                String fontSize = strings[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
                int slashIndex = fontSize.indexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                if (slashIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                    fontSize = fontSize.substring(0, slashIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                    strings[index] = strings[index].substring(slashIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
                css.addInternalCSSValue(attr, CSS.Attribute.FONT_SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
                                        fontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                css.addInternalCSSValue(attr, CSS.Attribute.FONT_SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                                        "medium");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
            // Check for line height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
            if (index < count && strings[index].startsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                String lineHeight = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                if (strings[index].equals("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                    if (++index < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                        lineHeight = strings[index++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
                    lineHeight = strings[index++].substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                // line height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                if (lineHeight != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
                    css.addInternalCSSValue(attr, CSS.Attribute.LINE_HEIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
                                            lineHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                    css.addInternalCSSValue(attr, CSS.Attribute.LINE_HEIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
                                            "normal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
                css.addInternalCSSValue(attr, CSS.Attribute.LINE_HEIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
                                        "normal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
            // remainder of strings are font-family
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
            if (index < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                String family = strings[index++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
                while (index < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
                    family += " " + strings[index++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
                css.addInternalCSSValue(attr, CSS.Attribute.FONT_FAMILY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
                                        family);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
                css.addInternalCSSValue(attr, CSS.Attribute.FONT_FAMILY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
                                        Font.SANS_SERIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
        private static boolean isFontStyle(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
            return (string.equals("italic") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
                    string.equals("oblique"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
        private static boolean isFontVariant(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
            return (string.equals("small-caps"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
        private static boolean isFontWeight(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
            if (string.equals("bold") || string.equals("bolder") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
                string.equals("italic") || string.equals("lighter")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
            // test for 100-900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
            return (string.length() == 3 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                    string.charAt(0) >= '1' && string.charAt(0) <= '9' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                    string.charAt(1) == '0' && string.charAt(2) == '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
     * Parses the background property into its intrinsic values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
    static class ShorthandBackgroundParser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
         * Parses the shorthand font string <code>value</code>, placing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
         * result in <code>attr</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
        static void parseShorthandBackground(CSS css, String value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
                                             MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
            String[] strings = parseStrings(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
            int count = strings.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
            int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
            // bitmask: 0 for image, 1 repeat, 2 attachment, 3 position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            //          4 color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
            short found = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
            while (index < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
                String string = strings[index++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
                if ((found & 1) == 0 && isImage(string)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                    css.addInternalCSSValue(attr, CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
                                            BACKGROUND_IMAGE, string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
                    found |= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
                else if ((found & 2) == 0 && isRepeat(string)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
                    css.addInternalCSSValue(attr, CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                                            BACKGROUND_REPEAT, string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
                    found |= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
                else if ((found & 4) == 0 && isAttachment(string)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
                    css.addInternalCSSValue(attr, CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
                                            BACKGROUND_ATTACHMENT, string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
                    found |= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
                else if ((found & 8) == 0 && isPosition(string)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
                    if (index < count && isPosition(strings[index])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
                        css.addInternalCSSValue(attr, CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
                                                BACKGROUND_POSITION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                                                string + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                                                strings[index++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                        css.addInternalCSSValue(attr, CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                                                BACKGROUND_POSITION, string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                    found |= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
                else if ((found & 16) == 0 && isColor(string)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                    css.addInternalCSSValue(attr, CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
                                            BACKGROUND_COLOR, string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                    found |= 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
            if ((found & 1) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                css.addInternalCSSValue(attr, CSS.Attribute.BACKGROUND_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                                        null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
            if ((found & 2) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                css.addInternalCSSValue(attr, CSS.Attribute.BACKGROUND_REPEAT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                                        "repeat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
            if ((found & 4) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                css.addInternalCSSValue(attr, CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                                        BACKGROUND_ATTACHMENT, "scroll");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
            if ((found & 8) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
                css.addInternalCSSValue(attr, CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
                                        BACKGROUND_POSITION, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
            // Currently, there is no good way to express this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
            if ((found & 16) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
                css.addInternalCSSValue(attr, CSS.Attribute.BACKGROUND_COLOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
                                        null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
        static boolean isImage(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
            return (string.startsWith("url(") && string.endsWith(")"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
        static boolean isRepeat(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
            return (string.equals("repeat-x") || string.equals("repeat-y") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
                    string.equals("repeat") || string.equals("no-repeat"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
        static boolean isAttachment(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
            return (string.equals("fixed") || string.equals("scroll"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
        static boolean isPosition(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
            return (string.equals("top") || string.equals("bottom") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
                    string.equals("left") || string.equals("right") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
                    string.equals("center") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
                    (string.length() > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
                     Character.isDigit(string.charAt(0))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
        static boolean isColor(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
            return (CSS.stringToColor(string) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
     * Used to parser margin and padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
    static class ShorthandMarginParser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
         * Parses the shorthand margin/padding/border string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
         * <code>value</code>, placing the result in <code>attr</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
         * <code>names</code> give the 4 instrinsic property names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        static void parseShorthandMargin(CSS css, String value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
                                         MutableAttributeSet attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
                                         CSS.Attribute[] names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
            String[] strings = parseStrings(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
            int count = strings.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
            int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
                // empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
                // Identifies all values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
                for (int counter = 0; counter < 4; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
                    css.addInternalCSSValue(attr, names[counter], strings[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
                // 0 & 2 = strings[0], 1 & 3 = strings[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
                css.addInternalCSSValue(attr, names[0], strings[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
                css.addInternalCSSValue(attr, names[2], strings[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
                css.addInternalCSSValue(attr, names[1], strings[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
                css.addInternalCSSValue(attr, names[3], strings[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
                css.addInternalCSSValue(attr, names[0], strings[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
                css.addInternalCSSValue(attr, names[1], strings[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                css.addInternalCSSValue(attr, names[2], strings[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                css.addInternalCSSValue(attr, names[3], strings[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
                for (int counter = 0; counter < 4; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
                    css.addInternalCSSValue(attr, names[counter],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
                                            strings[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
    static class ShorthandBorderParser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
        static Attribute[] keys = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
            Attribute.BORDER_TOP, Attribute.BORDER_RIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
            Attribute.BORDER_BOTTOM, Attribute.BORDER_LEFT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
        static void parseShorthandBorder(MutableAttributeSet attributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
                                            CSS.Attribute key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
            Object[] parts = new Object[CSSBorder.PARSERS.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
            String[] strings = parseStrings(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
            for (String s : strings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
                boolean valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
                for (int i = 0; i < parts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
                    Object v = CSSBorder.PARSERS[i].parseCssValue(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
                    if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
                        if (parts[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
                            parts[i] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
                            valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
                if (!valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
                    // Part is non-parseable or occured more than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
            // Unspecified parts get default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
            for (int i = 0; i < parts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
                if (parts[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
                    parts[i] = CSSBorder.DEFAULTS[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
            // Dispatch collected values to individual properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
            for (int i = 0; i < keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
                if ((key == Attribute.BORDER) || (key == keys[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
                    for (int k = 0; k < parts.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
                        attributes.addAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
                                        CSSBorder.ATTRIBUTES[k][i], parts[k]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
     * Calculate the requirements needed to tile the requirements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
     * given by the iterator that would be tiled.  The calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
     * takes into consideration margin and border spacing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
    static SizeRequirements calculateTiledRequirements(LayoutIterator iter, SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
        long minimum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
        long maximum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
        long preferred = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
        int lastMargin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
        int totalSpacing = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
        int n = iter.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
            iter.setIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
            int margin0 = lastMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
            int margin1 = (int) iter.getLeadingCollapseSpan();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  3091
            totalSpacing += Math.max(margin0, margin1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
            preferred += (int) iter.getPreferredSpan(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
            minimum += iter.getMinimumSpan(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
            maximum += iter.getMaximumSpan(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
            lastMargin = (int) iter.getTrailingCollapseSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        totalSpacing += lastMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
        totalSpacing += 2 * iter.getBorderWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
        // adjust for the spacing area
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
        minimum += totalSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
        preferred += totalSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
        maximum += totalSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
        // set return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
            r = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
        r.minimum = (minimum > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
        r.preferred = (preferred > Integer.MAX_VALUE) ? Integer.MAX_VALUE :(int) preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
        r.maximum = (maximum > Integer.MAX_VALUE) ? Integer.MAX_VALUE :(int) maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
     * Calculate a tiled layout for the given iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
     * This should be done collapsing the neighboring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
     * margins to be a total of the maximum of the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
     * neighboring margin areas as described in the CSS spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
    static void calculateTiledLayout(LayoutIterator iter, int targetSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
         * first pass, calculate the preferred sizes, adjustments needed because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
         * of margin collapsing, and the flexibility to adjust the sizes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
        long preferred = 0;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  3129
        long currentPreferred;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
        int lastMargin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
        int totalSpacing = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
        int n = iter.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
        int adjustmentWeightsCount = LayoutIterator.WorstAdjustmentWeight + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
        //max gain we can get adjusting elements with adjustmentWeight <= i
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
        long gain[] = new long[adjustmentWeightsCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
        //max loss we can get adjusting elements with adjustmentWeight <= i
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
        long loss[] = new long[adjustmentWeightsCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
        for (int i = 0; i < adjustmentWeightsCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
            gain[i] = loss[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
            iter.setIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
            int margin0 = lastMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
            int margin1 = (int) iter.getLeadingCollapseSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
            iter.setOffset(Math.max(margin0, margin1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
            totalSpacing += iter.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
            currentPreferred = (long)iter.getPreferredSpan(targetSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
            iter.setSpan((int) currentPreferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
            preferred += currentPreferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
            gain[iter.getAdjustmentWeight()] +=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
                (long)iter.getMaximumSpan(targetSpan) - currentPreferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
            loss[iter.getAdjustmentWeight()] +=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
                currentPreferred - (long)iter.getMinimumSpan(targetSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
            lastMargin = (int) iter.getTrailingCollapseSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
        totalSpacing += lastMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
        totalSpacing += 2 * iter.getBorderWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
        for (int i = 1; i < adjustmentWeightsCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
            gain[i] += gain[i - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
            loss[i] += loss[i - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
         * Second pass, expand or contract by as much as possible to reach
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
         * the target span.  This takes the margin collapsing into account
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
         * prior to adjusting the span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
        // determine the adjustment to be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
        int allocated = targetSpan - totalSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
        long desiredAdjustment = allocated - preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
        long adjustmentsArray[] = (desiredAdjustment > 0) ? gain : loss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
        desiredAdjustment = Math.abs(desiredAdjustment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
        int adjustmentLevel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
        for (;adjustmentLevel <= LayoutIterator.WorstAdjustmentWeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
             adjustmentLevel++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
            // adjustmentsArray[] is sorted. I do not bother about
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
            // binary search though
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
            if (adjustmentsArray[adjustmentLevel] >= desiredAdjustment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
        float adjustmentFactor = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
        if (adjustmentLevel <= LayoutIterator.WorstAdjustmentWeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
            desiredAdjustment -= (adjustmentLevel > 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
                adjustmentsArray[adjustmentLevel - 1] : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
            if (desiredAdjustment != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
                float maximumAdjustment =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
                    adjustmentsArray[adjustmentLevel] -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
                    ((adjustmentLevel > 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
                     adjustmentsArray[adjustmentLevel - 1] : 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
                     );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
                adjustmentFactor = desiredAdjustment / maximumAdjustment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
        // make the adjustments
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  3201
        int totalOffset = (int)iter.getBorderWidth();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
            iter.setIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
            iter.setOffset( iter.getOffset() + totalOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
            if (iter.getAdjustmentWeight() < adjustmentLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
                iter.setSpan((int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
                             ((allocated > preferred) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
                              Math.floor(iter.getMaximumSpan(targetSpan)) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
                              Math.ceil(iter.getMinimumSpan(targetSpan))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
                              )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
                             );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
            } else if (iter.getAdjustmentWeight() == adjustmentLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
                int availableSpan = (allocated > preferred) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
                    (int) iter.getMaximumSpan(targetSpan) - iter.getSpan() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
                    iter.getSpan() - (int) iter.getMinimumSpan(targetSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
                int adj = (int)Math.floor(adjustmentFactor * availableSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
                iter.setSpan(iter.getSpan() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
                             ((allocated > preferred) ? adj : -adj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
            totalOffset = (int) Math.min((long) iter.getOffset() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
                                         (long) iter.getSpan(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
                                         Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
        // while rounding we could lose several pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
        int roundError = targetSpan - totalOffset -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
            (int)iter.getTrailingCollapseSpan() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
            (int)iter.getBorderWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
        int adj = (roundError > 0) ? 1 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
        roundError *= adj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
        boolean canAdjust = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
        while (roundError > 0 && canAdjust) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
            // check for infinite loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
            canAdjust = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
            int offsetAdjust = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
            // try to distribute roundError. one pixel per cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
                iter.setIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
                iter.setOffset(iter.getOffset() + offsetAdjust);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
                int curSpan = iter.getSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
                if (roundError > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
                    int boundGap = (adj > 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
                        (int)Math.floor(iter.getMaximumSpan(targetSpan)) - curSpan :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
                        curSpan - (int)Math.ceil(iter.getMinimumSpan(targetSpan));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
                    if (boundGap >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
                        canAdjust = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
                        iter.setSpan(curSpan + adj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
                        offsetAdjust += adj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
                        roundError--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
     * An iterator to express the requirements to use when computing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
     * layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
    interface LayoutIterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
        void setOffset(int offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
        int getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
        void setSpan(int span);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
        int getSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
        int getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
        void setIndex(int i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
        float getMinimumSpan(float parentSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
        float getPreferredSpan(float parentSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
        float getMaximumSpan(float parentSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
        int getAdjustmentWeight(); //0 is the best weight WorstAdjustmentWeight is a worst one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
        //float getAlignment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
        float getBorderWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
        float getLeadingCollapseSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
        float getTrailingCollapseSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
        public static final int WorstAdjustmentWeight = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
    // Serialization support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
        // Determine what values in valueConvertor need to be written out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
        Enumeration keys = valueConvertor.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
        s.writeInt(valueConvertor.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
        if (keys != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
            while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
                Object key = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
                Object value = valueConvertor.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
                if (!(key instanceof Serializable) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
                    (key = StyleContext.getStaticAttributeKey(key)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
                    // Should we throw an exception here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
                    key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
                    value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
                else if (!(value instanceof Serializable) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
                    (value = StyleContext.getStaticAttributeKey(value)) == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
                    // Should we throw an exception here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
                    key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
                    value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
                s.writeObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
                s.writeObject(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
      throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
        // Reconstruct the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
        int numValues = s.readInt();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  3333
        valueConvertor = new Hashtable<Object, Object>(Math.max(1, numValues));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
        while (numValues-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
            Object key = s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
            Object value = s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
            Object staticKey = StyleContext.getStaticAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
            if (staticKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
                key = staticKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
            Object staticValue = StyleContext.getStaticAttribute(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
            if (staticValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
                value = staticValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
            if (key != null && value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
                valueConvertor.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
     * we need StyleSheet for resolving lenght units. (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
     * isW3CLengthUnits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
     * we can not pass stylesheet for handling relative sizes. (do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
     * think changing public API is necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
     * CSS is not likely to be accessed from more then one thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
     * Having local storage for StyleSheet for resolving relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
     * sizes is safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
     * idk 08/30/2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
    private StyleSheet getStyleSheet(StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
        if (ss != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
            styleSheet = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
        return styleSheet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
    // Instance variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
    /** Maps from CSS key to CssValue. */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
  3374
    private transient Hashtable<Object, Object> valueConvertor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
    /** Size used for relative units. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
    private int baseFontSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
    private transient StyleSheet styleSheet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
    static int baseFontSizeIndex = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
}