1/*
2 * Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25/*
26 *
27 * (C) Copyright IBM Corp. 1998, 1999 - All Rights Reserved
28 */
29
30package sun.font;
31
32import java.awt.Font;
33import java.awt.Graphics2D;
34import java.awt.Rectangle;
35import java.awt.Shape;
36import java.awt.font.FontRenderContext;
37import java.awt.font.GlyphVector;
38import java.awt.geom.AffineTransform;
39import java.awt.geom.Rectangle2D;
40
41/**
42 * Implementation of TextLabel based on String.
43 */
44
45public class TextSourceLabel extends TextLabel {
46  TextSource source;
47
48  // caches
49  Rectangle2D lb;
50  Rectangle2D ab;
51  Rectangle2D vb;
52  Rectangle2D ib;
53  GlyphVector gv;
54
55  public TextSourceLabel(TextSource source) {
56    this(source, null, null, null);
57  }
58
59  public TextSourceLabel(TextSource source, Rectangle2D lb, Rectangle2D ab, GlyphVector gv) {
60    this.source = source;
61
62    this.lb = lb;
63    this.ab = ab;
64    this.gv = gv;
65  }
66
67  public TextSource getSource() {
68    return source;
69  }
70
71  public final Rectangle2D getLogicalBounds(float x, float y) {
72    if (lb == null) {
73      lb = createLogicalBounds();
74    }
75    return new Rectangle2D.Float((float)(lb.getX() + x),
76                                 (float)(lb.getY() + y),
77                                 (float)lb.getWidth(),
78                                 (float)lb.getHeight());
79  }
80
81  public final Rectangle2D getVisualBounds(float x, float y) {
82    if (vb == null) {
83      vb = createVisualBounds();
84
85    }
86    return new Rectangle2D.Float((float)(vb.getX() + x),
87                                 (float)(vb.getY() + y),
88                                 (float)vb.getWidth(),
89                                 (float)vb.getHeight());
90  }
91
92  public final Rectangle2D getAlignBounds(float x, float y) {
93    if (ab == null) {
94      ab = createAlignBounds();
95    }
96    return new Rectangle2D.Float((float)(ab.getX() + x),
97                                 (float)(ab.getY() + y),
98                                 (float)ab.getWidth(),
99                                 (float)ab.getHeight());
100  }
101
102  public Rectangle2D getItalicBounds(float x, float y) {
103    if (ib == null) {
104      ib = createItalicBounds();
105    }
106    return new Rectangle2D.Float((float)(ib.getX() + x),
107                                 (float)(ib.getY() + y),
108                                 (float)ib.getWidth(),
109                                 (float)ib.getHeight());
110
111  }
112
113  public Rectangle getPixelBounds(FontRenderContext frc, float x, float y) {
114      return getGV().getPixelBounds(frc, x, y); // no cache
115  }
116
117  public AffineTransform getBaselineTransform() {
118      Font font = source.getFont();
119      if (font.hasLayoutAttributes()) {
120          return AttributeValues.getBaselineTransform(font.getAttributes());
121      }
122      return null;
123  }
124
125  public Shape getOutline(float x, float y) {
126    return getGV().getOutline(x, y);
127  }
128
129  public void draw(Graphics2D g, float x, float y) {
130    g.drawGlyphVector(getGV(), x, y);
131  }
132
133  protected Rectangle2D createLogicalBounds() {
134    return getGV().getLogicalBounds();
135  }
136
137  protected Rectangle2D createVisualBounds() {
138    return getGV().getVisualBounds();
139  }
140
141  protected Rectangle2D createItalicBounds() {
142      // !!! fix
143    return getGV().getLogicalBounds();
144  }
145
146  protected Rectangle2D createAlignBounds() {
147    return createLogicalBounds();
148  }
149
150  private final GlyphVector getGV() {
151    if (gv == null) {
152      gv = createGV();
153    }
154
155    return gv;
156  }
157
158  protected GlyphVector createGV() {
159    Font font = source.getFont();
160    FontRenderContext frc = source.getFRC();
161    int flags = source.getLayoutFlags();
162    char[] context = source.getChars();
163    int start = source.getStart();
164    int length = source.getLength();
165
166    GlyphLayout gl = GlyphLayout.get(null); // !!! no custom layout engines
167    StandardGlyphVector gv = gl.layout(font, frc, context, start, length,
168                                       flags, null); // ??? use textsource
169    GlyphLayout.done(gl);
170
171    return gv;
172  }
173}
174