jdk/src/share/classes/sun/font/Decoration.java
author darcy
Sat, 08 Feb 2014 18:03:38 -0800
changeset 23288 b7183846db97
parent 22584 eed64ee05369
child 24969 afa6934dd8e8
permissions -rw-r--r--
8033624: Fix raw and unchecked lint warnings in sun.font Reviewed-by: alanb, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1728
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1728
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1728
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1728
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1728
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright IBM Corp. 1999-2003, All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
package sun.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.BasicStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Paint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.RenderingHints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.Stroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.font.TextAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.geom.Area;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.geom.Line2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.geom.GeneralPath;
23288
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
    49
import java.text.AttributedCharacterIterator.Attribute;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import static sun.font.AttributeValues.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import static sun.font.EAttribute.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * This class handles underlining, strikethrough, and foreground and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * background styles on text.  Clients simply acquire instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * of this class and hand them off to ExtendedTextLabels or GraphicComponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class Decoration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * This interface is implemented by clients that use Decoration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Unfortunately, interface methods have to public;  ideally these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * would be package-private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public interface Label {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        CoreMetrics getCoreMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Rectangle2D getLogicalBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        void handleDraw(Graphics2D g2d, float x, float y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        Rectangle2D handleGetCharVisualBounds(int index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        Rectangle2D handleGetVisualBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Shape handleGetOutline(float x, float y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private Decoration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Return a Decoration which does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static Decoration getPlainDecoration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return PLAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static final int VALUES_MASK =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        AttributeValues.getMask(EFOREGROUND, EBACKGROUND, ESWAP_COLORS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                ESTRIKETHROUGH, EUNDERLINE, EINPUT_METHOD_HIGHLIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                EINPUT_METHOD_UNDERLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static Decoration getDecoration(AttributeValues values) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (values == null || !values.anyDefined(VALUES_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return PLAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        values = values.applyIMHighlight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return new DecorationImpl(values.getForeground(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                  values.getBackground(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                  values.getSwapColors(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                  values.getStrikethrough(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                  Underline.getUnderline(values.getUnderline()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                  Underline.getUnderline(values.getInputMethodUnderline()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Return a Decoration appropriate for the the given Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param attributes the Map used to determine the Decoration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
23288
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
   111
    public static Decoration getDecoration(Map<? extends Attribute, ?> attributes) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return PLAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return getDecoration(AttributeValues.fromMap(attributes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void drawTextAndDecorations(Label label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                Graphics2D g2d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                float x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        label.handleDraw(g2d, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public Rectangle2D getVisualBounds(Label label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return label.handleGetVisualBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public Rectangle2D getCharVisualBounds(Label label, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return label.handleGetCharVisualBounds(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    Shape getOutline(Label label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                     float x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                     float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return label.handleGetOutline(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static final Decoration PLAIN = new Decoration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static final class DecorationImpl extends Decoration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        private Paint fgPaint = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        private Paint bgPaint = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        private boolean swapColors = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        private boolean strikethrough = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        private Underline stdUnderline = null; // underline from TextAttribute.UNDERLINE_ON
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        private Underline imUnderline = null; // input method underline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        DecorationImpl(Paint foreground,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                       Paint background,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                       boolean swapColors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                       boolean strikethrough,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                       Underline stdUnderline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                       Underline imUnderline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   161
            fgPaint = foreground;
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   162
            bgPaint = background;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            this.swapColors = swapColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            this.strikethrough = strikethrough;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            this.stdUnderline = stdUnderline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            this.imUnderline = imUnderline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        private static boolean areEqual(Object lhs, Object rhs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (lhs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                return rhs == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                return lhs.equals(rhs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        public boolean equals(Object rhs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (rhs == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (rhs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            DecorationImpl other = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                other = (DecorationImpl) rhs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            catch(ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (!(swapColors == other.swapColors &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        strikethrough == other.strikethrough)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (!areEqual(stdUnderline, other.stdUnderline)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (!areEqual(fgPaint, other.fgPaint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (!areEqual(bgPaint, other.bgPaint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return areEqual(imUnderline, other.imUnderline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            int hc = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (strikethrough) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                hc |= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (swapColors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                hc |= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (stdUnderline != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                hc += stdUnderline.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        * Return the bottom of the Rectangle which encloses pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        * drawn by underlines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        private float getUnderlineMaxY(CoreMetrics cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            float maxY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (stdUnderline != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                float ulBottom = cm.underlineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                ulBottom += stdUnderline.getLowerDrawLimit(cm.underlineThickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                maxY = Math.max(maxY, ulBottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (imUnderline != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                float ulBottom = cm.underlineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                ulBottom += imUnderline.getLowerDrawLimit(cm.underlineThickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                maxY = Math.max(maxY, ulBottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return maxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        private void drawTextAndEmbellishments(Label label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                               Graphics2D g2d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                               float x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                               float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            label.handleDraw(g2d, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (!strikethrough && stdUnderline == null && imUnderline == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            float x1 = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            float x2 = x1 + (float)label.getLogicalBounds().getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            CoreMetrics cm = label.getCoreMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (strikethrough) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                Stroke savedStroke = g2d.getStroke();
1725
3be564c67d63 6751621: TextLayout.getBounds() doesn't account for strike through
prr
parents: 2
diff changeset
   271
                g2d.setStroke(new BasicStroke(cm.strikethroughThickness,
3be564c67d63 6751621: TextLayout.getBounds() doesn't account for strike through
prr
parents: 2
diff changeset
   272
                                              BasicStroke.CAP_BUTT,
3be564c67d63 6751621: TextLayout.getBounds() doesn't account for strike through
prr
parents: 2
diff changeset
   273
                                              BasicStroke.JOIN_MITER));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                float strikeY = y + cm.strikethroughOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                g2d.draw(new Line2D.Float(x1, strikeY, x2, strikeY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                g2d.setStroke(savedStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            float ulOffset = cm.underlineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            float ulThickness = cm.underlineThickness;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if (stdUnderline != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                stdUnderline.drawUnderline(g2d, ulThickness, x1, x2, y + ulOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (imUnderline != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                imUnderline.drawUnderline(g2d, ulThickness, x1, x2, y + ulOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        public void drawTextAndDecorations(Label label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                    Graphics2D g2d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                    float x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                    float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (fgPaint == null && bgPaint == null && swapColors == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                drawTextAndEmbellishments(label, g2d, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                Paint savedPaint = g2d.getPaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                Paint foreground, background;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                if (swapColors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    background = fgPaint==null? savedPaint : fgPaint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    if (bgPaint == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        if (background instanceof Color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                            Color bg = (Color)background;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                            // 30/59/11 is standard weights, tweaked a bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                            int brightness = 33 * bg.getRed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                + 53 * bg.getGreen()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                + 14 * bg.getBlue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                            foreground = brightness > 18500 ? Color.BLACK : Color.WHITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                            foreground = Color.WHITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        foreground = bgPaint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    foreground = fgPaint==null? savedPaint : fgPaint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    background = bgPaint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                if (background != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    Rectangle2D bgArea = label.getLogicalBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    bgArea = new Rectangle2D.Float(x + (float)bgArea.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                                y + (float)bgArea.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                                (float)bgArea.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                                (float)bgArea.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    g2d.setPaint(background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    g2d.fill(bgArea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                g2d.setPaint(foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                drawTextAndEmbellishments(label, g2d, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                g2d.setPaint(savedPaint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        public Rectangle2D getVisualBounds(Label label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            Rectangle2D visBounds = label.handleGetVisualBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
1725
3be564c67d63 6751621: TextLayout.getBounds() doesn't account for strike through
prr
parents: 2
diff changeset
   347
            if (swapColors || bgPaint != null || strikethrough
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        || stdUnderline != null || imUnderline != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                float minX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                Rectangle2D lb = label.getLogicalBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                float minY = 0, maxY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                if (swapColors || bgPaint != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    minY = (float)lb.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    maxY = minY + (float)lb.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                maxY = Math.max(maxY, getUnderlineMaxY(label.getCoreMetrics()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                Rectangle2D ab = new Rectangle2D.Float(minX, minY, (float)lb.getWidth(), maxY-minY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                visBounds.add(ab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            return visBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        Shape getOutline(Label label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                         float x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                         float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (!strikethrough && stdUnderline == null && imUnderline == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                return label.handleGetOutline(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            CoreMetrics cm = label.getCoreMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            // NOTE:  The performace of the following code may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            // be very poor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            float ulThickness = cm.underlineThickness;
1728
7c281f43cafa 6751616: outline for underline in TextLayout with underline is off rasterized underline
prr
parents: 1725
diff changeset
   383
            float ulOffset = cm.underlineOffset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            Rectangle2D lb = label.getLogicalBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            float x1 = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            float x2 = x1 + (float)lb.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            Area area = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (stdUnderline != null) {
1728
7c281f43cafa 6751616: outline for underline in TextLayout with underline is off rasterized underline
prr
parents: 1725
diff changeset
   392
                Shape ul = stdUnderline.getUnderlineShape(ulThickness,
7c281f43cafa 6751616: outline for underline in TextLayout with underline is off rasterized underline
prr
parents: 1725
diff changeset
   393
                                                          x1, x2, y+ulOffset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                area = new Area(ul);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            if (strikethrough) {
1725
3be564c67d63 6751621: TextLayout.getBounds() doesn't account for strike through
prr
parents: 2
diff changeset
   398
                Stroke stStroke = new BasicStroke(cm.strikethroughThickness,
3be564c67d63 6751621: TextLayout.getBounds() doesn't account for strike through
prr
parents: 2
diff changeset
   399
                                                  BasicStroke.CAP_BUTT,
3be564c67d63 6751621: TextLayout.getBounds() doesn't account for strike through
prr
parents: 2
diff changeset
   400
                                                  BasicStroke.JOIN_MITER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                float shiftY = y + cm.strikethroughOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                Line2D line = new Line2D.Float(x1, shiftY, x2, shiftY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                Area slArea = new Area(stStroke.createStrokedShape(line));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                if(area == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    area = slArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    area.add(slArea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            if (imUnderline != null) {
1728
7c281f43cafa 6751616: outline for underline in TextLayout with underline is off rasterized underline
prr
parents: 1725
diff changeset
   412
                Shape ul = imUnderline.getUnderlineShape(ulThickness,
7c281f43cafa 6751616: outline for underline in TextLayout with underline is off rasterized underline
prr
parents: 1725
diff changeset
   413
                                                         x1, x2, y+ulOffset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                Area ulArea = new Area(ul);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                if (area == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    area = ulArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    area.add(ulArea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            // area won't be null here, since at least one underline exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            area.add(new Area(label.handleGetOutline(x, y)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return new GeneralPath(area);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            buf.append(super.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            buf.append("[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (fgPaint != null) buf.append("fgPaint: " + fgPaint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if (bgPaint != null) buf.append(" bgPaint: " + bgPaint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (swapColors) buf.append(" swapColors: true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (strikethrough) buf.append(" strikethrough: true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            if (stdUnderline != null) buf.append(" stdUnderline: " + stdUnderline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (imUnderline != null) buf.append(" imUnderline: " + imUnderline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            buf.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
}