jdk/src/java.desktop/share/classes/sun/font/TextSourceLabel.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
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2005, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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, 1999 - 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.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.font.FontRenderContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.font.GlyphVector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Implementation of TextLabel based on String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class TextSourceLabel extends TextLabel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  TextSource source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  // caches
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  Rectangle2D lb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  Rectangle2D ab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  Rectangle2D vb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  Rectangle2D ib;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  GlyphVector gv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  public TextSourceLabel(TextSource source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    this(source, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  public TextSourceLabel(TextSource source, Rectangle2D lb, Rectangle2D ab, GlyphVector gv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    this.source = source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    this.lb = lb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    this.ab = ab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    this.gv = gv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
  public TextSource getSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    return source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  public final Rectangle2D getLogicalBounds(float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    if (lb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
      lb = createLogicalBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    return new Rectangle2D.Float((float)(lb.getX() + x),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                 (float)(lb.getY() + y),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                 (float)lb.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                 (float)lb.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
  public final Rectangle2D getVisualBounds(float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    if (vb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
      vb = createVisualBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    return new Rectangle2D.Float((float)(vb.getX() + x),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                 (float)(vb.getY() + y),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                 (float)vb.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                 (float)vb.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  public final Rectangle2D getAlignBounds(float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    if (ab == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
      ab = createAlignBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    return new Rectangle2D.Float((float)(ab.getX() + x),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                 (float)(ab.getY() + y),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                 (float)ab.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                 (float)ab.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
  public Rectangle2D getItalicBounds(float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    if (ib == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
      ib = createItalicBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    return new Rectangle2D.Float((float)(ib.getX() + x),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                 (float)(ib.getY() + y),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                 (float)ib.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                 (float)ib.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
  public Rectangle getPixelBounds(FontRenderContext frc, float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
      return getGV().getPixelBounds(frc, x, y); // no cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
  public AffineTransform getBaselineTransform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
      Font font = source.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      if (font.hasLayoutAttributes()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
          return AttributeValues.getBaselineTransform(font.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
      return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
  public Shape getOutline(float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    return getGV().getOutline(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
  public void draw(Graphics2D g, float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    g.drawGlyphVector(getGV(), x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
  protected Rectangle2D createLogicalBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    return getGV().getLogicalBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
  protected Rectangle2D createVisualBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    return getGV().getVisualBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
  protected Rectangle2D createItalicBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
      // !!! fix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    return getGV().getLogicalBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
  protected Rectangle2D createAlignBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    return createLogicalBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
  private final GlyphVector getGV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    if (gv == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
      gv = createGV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    return gv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
  protected GlyphVector createGV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    Font font = source.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    FontRenderContext frc = source.getFRC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    int flags = source.getLayoutFlags();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    char[] context = source.getChars();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    int start = source.getStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    int length = source.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    GlyphLayout gl = GlyphLayout.get(null); // !!! no custom layout engines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    StandardGlyphVector gv = gl.layout(font, frc, context, start, length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                       flags, null); // ??? use textsource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    GlyphLayout.done(gl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    return gv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}