jdk/src/java.desktop/share/classes/sun/font/Underline.java
author prr
Wed, 09 Nov 2016 11:28:13 -0800
changeset 42208 7c1017f0ade5
parent 25859 3317bb8137f4
permissions -rw-r--r--
8155874: Fix java.desktop deprecation warnings about Class.newInstance Reviewed-by: serb, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1725
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1725
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1725
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1725
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1725
diff changeset
    23
 * questions.
2
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. 1998, 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
package sun.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.BasicStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Stroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.geom.GeneralPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.geom.Line2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.font.TextAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class provides drawing and bounds-measurement of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * underlines.  Additionally, it has a factory method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * obtaining underlines from values of underline attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
abstract class Underline {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Draws the underline into g2d.  The thickness should be obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * from a LineMetrics object.  Note that some underlines ignore the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * thickness parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * The underline is drawn from (x1, y) to (x2, y).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    abstract void drawUnderline(Graphics2D g2d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                float thickness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                float x1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                float x2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                float y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Returns the bottom of the bounding rectangle for this underline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    abstract float getLowerDrawLimit(float thickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Returns a Shape representing the underline.  The thickness should be obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * from a LineMetrics object.  Note that some underlines ignore the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * thickness parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    abstract Shape getUnderlineShape(float thickness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                     float x1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                     float x2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                     float y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     // Implementation of underline for standard and Input Method underlines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     // These classes are private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // IM Underlines ignore thickness param, and instead use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // DEFAULT_THICKNESS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final float DEFAULT_THICKNESS = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // StandardUnderline's constructor takes a boolean param indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // whether to override the default thickness.  These values clarify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // the semantics of the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final boolean USE_THICKNESS = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final boolean IGNORE_THICKNESS = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // Implementation of standard underline and all input method underlines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // except UNDERLINE_LOW_GRAY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static final class StandardUnderline extends Underline {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // the amount by which to move the underline
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        private float shift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // the actual line thickness is this value times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // the requested thickness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        private float thicknessMultiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // if non-null, underline is drawn with a BasicStroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // with this dash pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        private float[] dashPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // if false, all underlines are DEFAULT_THICKNESS thick
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // if true, use thickness param
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        private boolean useThickness;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // cached BasicStroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        private BasicStroke cachedStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        StandardUnderline(float shift,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                          float thicknessMultiplier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                          float[] dashPattern,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                          boolean useThickness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            this.shift = shift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            this.thicknessMultiplier = thicknessMultiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            this.dashPattern = dashPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            this.useThickness = useThickness;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            this.cachedStroke = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        private BasicStroke createStroke(float lineThickness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (dashPattern == null) {
1725
3be564c67d63 6751621: TextLayout.getBounds() doesn't account for strike through
prr
parents: 2
diff changeset
   129
                return new BasicStroke(lineThickness,
3be564c67d63 6751621: TextLayout.getBounds() doesn't account for strike through
prr
parents: 2
diff changeset
   130
                                       BasicStroke.CAP_BUTT,
3be564c67d63 6751621: TextLayout.getBounds() doesn't account for strike through
prr
parents: 2
diff changeset
   131
                                       BasicStroke.JOIN_MITER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                return new BasicStroke(lineThickness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                       BasicStroke.CAP_BUTT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                       BasicStroke.JOIN_MITER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                       10.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                       dashPattern,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                       0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
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 float getLineThickness(float thickness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (useThickness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                return thickness * thicknessMultiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                return DEFAULT_THICKNESS * thicknessMultiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        private Stroke getStroke(float thickness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            float lineThickness = getLineThickness(thickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            BasicStroke stroke = cachedStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (stroke == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    stroke.getLineWidth() != lineThickness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                stroke = createStroke(lineThickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                cachedStroke = stroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return stroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        void drawUnderline(Graphics2D g2d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                           float thickness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                           float x1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                           float x2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                           float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            Stroke saveStroke = g2d.getStroke();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            g2d.setStroke(getStroke(thickness));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            g2d.draw(new Line2D.Float(x1, y + shift, x2, y + shift));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            g2d.setStroke(saveStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        float getLowerDrawLimit(float thickness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return shift + getLineThickness(thickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Shape getUnderlineShape(float thickness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                float x1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                float x2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            Stroke ulStroke = getStroke(thickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            Line2D line = new Line2D.Float(x1, y + shift, x2, y + shift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return ulStroke.createStrokedShape(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // Implementation of UNDERLINE_LOW_GRAY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private static class IMGrayUnderline extends Underline {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        private BasicStroke stroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        IMGrayUnderline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            stroke = new BasicStroke(DEFAULT_THICKNESS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                     BasicStroke.CAP_BUTT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                     BasicStroke.JOIN_MITER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                     10.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                     new float[] {1, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                     0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        void drawUnderline(Graphics2D g2d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                           float thickness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                           float x1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                           float x2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                           float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            Stroke saveStroke = g2d.getStroke();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            g2d.setStroke(stroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            Line2D.Float drawLine = new Line2D.Float(x1, y, x2, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            g2d.draw(drawLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            drawLine.y1 += DEFAULT_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            drawLine.y2 += DEFAULT_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            drawLine.x1 += DEFAULT_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            g2d.draw(drawLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            g2d.setStroke(saveStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        float getLowerDrawLimit(float thickness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return DEFAULT_THICKNESS * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        Shape getUnderlineShape(float thickness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                float x1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                float x2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            GeneralPath gp = new GeneralPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            Line2D.Float line = new Line2D.Float(x1, y, x2, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            gp.append(stroke.createStrokedShape(line), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            line.y1 += DEFAULT_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            line.y2 += DEFAULT_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            line.x1 += DEFAULT_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            gp.append(stroke.createStrokedShape(line), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     // Keep a map of underlines, one for each type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     // of underline.  The Underline objects are Flyweights
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     // (shared across multiple clients), so they should be immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     // If this implementation changes then clone underline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     // instances in getUnderline before returning them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static final ConcurrentHashMap<Object, Underline>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        UNDERLINES = new ConcurrentHashMap<Object, Underline>(6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private static final Underline[] UNDERLINE_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        Underline[] uls = new Underline[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        uls[0] = new StandardUnderline(0, 1, null, USE_THICKNESS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        UNDERLINES.put(TextAttribute.UNDERLINE_ON, uls[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        uls[1] = new StandardUnderline(1, 1, null, IGNORE_THICKNESS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        UNDERLINES.put(TextAttribute.UNDERLINE_LOW_ONE_PIXEL, uls[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        uls[2] = new StandardUnderline(1, 2, null, IGNORE_THICKNESS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        UNDERLINES.put(TextAttribute.UNDERLINE_LOW_TWO_PIXEL, uls[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        uls[3] = new StandardUnderline(1, 1, new float[] { 1, 1 }, IGNORE_THICKNESS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        UNDERLINES.put(TextAttribute.UNDERLINE_LOW_DOTTED, uls[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        uls[4] = new IMGrayUnderline();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        UNDERLINES.put(TextAttribute.UNDERLINE_LOW_GRAY, uls[4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        uls[5] = new StandardUnderline(1, 1, new float[] { 4, 4 }, IGNORE_THICKNESS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        UNDERLINES.put(TextAttribute.UNDERLINE_LOW_DASHED, uls[5]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        UNDERLINE_LIST = uls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Return the Underline for the given value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * TextAttribute.INPUT_METHOD_UNDERLINE or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * TextAttribute.UNDERLINE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * If value is not an input method underline value or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * TextAttribute.UNDERLINE_ON, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    static Underline getUnderline(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   302
        return UNDERLINES.get(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    static Underline getUnderline(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return index < 0 ? null : UNDERLINE_LIST[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}