0N/A/*
2362N/A * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
0N/A * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
0N/A *
0N/A * The original version of this source code and documentation is
0N/A * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
0N/A * of IBM. These materials are provided under terms of a License
0N/A * Agreement between Taligent and Sun. This technology is protected
0N/A * by multiple US and International patents.
0N/A *
0N/A * This notice and attribution to Taligent may not be removed.
0N/A * Taligent is a registered trademark of Taligent, Inc.
0N/A *
0N/A */
0N/A
0N/Apackage java.awt.font;
0N/A
0N/Aimport java.awt.Graphics2D;
0N/Aimport java.awt.Font;
0N/Aimport java.awt.Shape;
0N/Aimport java.awt.geom.AffineTransform;
0N/Aimport java.awt.geom.Rectangle2D;
0N/A
0N/A/**
0N/A * This class is used with the CHAR_REPLACEMENT attribute.
0N/A * <p>
0N/A * The <code>GraphicAttribute</code> class represents a graphic embedded
0N/A * in text. Clients subclass this class to implement their own char
0N/A * replacement graphics. Clients wishing to embed shapes and images in
0N/A * text need not subclass this class. Instead, clients can use the
0N/A * {@link ShapeGraphicAttribute} and {@link ImageGraphicAttribute}
0N/A * classes.
0N/A * <p>
0N/A * Subclasses must ensure that their objects are immutable once they
0N/A * are constructed. Mutating a <code>GraphicAttribute</code> that
0N/A * is used in a {@link TextLayout} results in undefined behavior from the
0N/A * <code>TextLayout</code>.
0N/A */
0N/Apublic abstract class GraphicAttribute {
0N/A
0N/A private int fAlignment;
0N/A
0N/A /**
0N/A * Aligns top of graphic to top of line.
0N/A */
0N/A public static final int TOP_ALIGNMENT = -1;
0N/A
0N/A /**
0N/A * Aligns bottom of graphic to bottom of line.
0N/A */
0N/A public static final int BOTTOM_ALIGNMENT = -2;
0N/A
0N/A /**
0N/A * Aligns origin of graphic to roman baseline of line.
0N/A */
0N/A public static final int ROMAN_BASELINE = Font.ROMAN_BASELINE;
0N/A
0N/A /**
0N/A * Aligns origin of graphic to center baseline of line.
0N/A */
0N/A public static final int CENTER_BASELINE = Font.CENTER_BASELINE;
0N/A
0N/A /**
0N/A * Aligns origin of graphic to hanging baseline of line.
0N/A */
0N/A public static final int HANGING_BASELINE = Font.HANGING_BASELINE;
0N/A
0N/A /**
0N/A * Constructs a <code>GraphicAttribute</code>.
0N/A * Subclasses use this to define the alignment of the graphic.
0N/A * @param alignment an int representing one of the
0N/A * <code>GraphicAttribute</code> alignment fields
0N/A * @throws IllegalArgumentException if alignment is not one of the
0N/A * five defined values.
0N/A */
0N/A protected GraphicAttribute(int alignment) {
0N/A if (alignment < BOTTOM_ALIGNMENT || alignment > HANGING_BASELINE) {
0N/A throw new IllegalArgumentException("bad alignment");
0N/A }
0N/A fAlignment = alignment;
0N/A }
0N/A
0N/A /**
0N/A * Returns the ascent of this <code>GraphicAttribute</code>. A
0N/A * graphic can be rendered above its ascent.
0N/A * @return the ascent of this <code>GraphicAttribute</code>.
0N/A * @see #getBounds()
0N/A */
0N/A public abstract float getAscent();
0N/A
0N/A
0N/A /**
0N/A * Returns the descent of this <code>GraphicAttribute</code>. A
0N/A * graphic can be rendered below its descent.
0N/A * @return the descent of this <code>GraphicAttribute</code>.
0N/A * @see #getBounds()
0N/A */
0N/A public abstract float getDescent();
0N/A
0N/A /**
0N/A * Returns the advance of this <code>GraphicAttribute</code>. The
0N/A * <code>GraphicAttribute</code> object's advance is the distance
0N/A * from the point at which the graphic is rendered and the point where
0N/A * the next character or graphic is rendered. A graphic can be
0N/A * rendered beyond its advance
0N/A * @return the advance of this <code>GraphicAttribute</code>.
0N/A * @see #getBounds()
0N/A */
0N/A public abstract float getAdvance();
0N/A
0N/A /**
0N/A * Returns a {@link Rectangle2D} that encloses all of the
0N/A * bits drawn by this <code>GraphicAttribute</code> relative to the
0N/A * rendering position.
0N/A * A graphic may be rendered beyond its origin, ascent, descent,
0N/A * or advance; but if it is, this method's implementation must
0N/A * indicate where the graphic is rendered.
0N/A * Default bounds is the rectangle (0, -ascent, advance, ascent+descent).
0N/A * @return a <code>Rectangle2D</code> that encloses all of the bits
0N/A * rendered by this <code>GraphicAttribute</code>.
0N/A */
0N/A public Rectangle2D getBounds() {
0N/A float ascent = getAscent();
0N/A return new Rectangle2D.Float(0, -ascent,
0N/A getAdvance(), ascent+getDescent());
0N/A }
0N/A
0N/A /**
0N/A * Return a {@link java.awt.Shape} that represents the region that
0N/A * this <code>GraphicAttribute</code> renders. This is used when a
0N/A * {@link TextLayout} is requested to return the outline of the text.
0N/A * The (untransformed) shape must not extend outside the rectangular
0N/A * bounds returned by <code>getBounds</code>.
0N/A * The default implementation returns the rectangle returned by
0N/A * {@link #getBounds}, transformed by the provided {@link AffineTransform}
0N/A * if present.
0N/A * @param tx an optional {@link AffineTransform} to apply to the
0N/A * outline of this <code>GraphicAttribute</code>. This can be null.
0N/A * @return a <code>Shape</code> representing this graphic attribute,
0N/A * suitable for stroking or filling.
0N/A * @since 1.6
0N/A */
0N/A public Shape getOutline(AffineTransform tx) {
0N/A Shape b = getBounds();
0N/A if (tx != null) {
0N/A b = tx.createTransformedShape(b);
0N/A }
0N/A return b;
0N/A }
0N/A
0N/A /**
0N/A * Renders this <code>GraphicAttribute</code> at the specified
0N/A * location.
0N/A * @param graphics the {@link Graphics2D} into which to render the
0N/A * graphic
0N/A * @param x the user-space X coordinate where the graphic is rendered
0N/A * @param y the user-space Y coordinate where the graphic is rendered
0N/A */
0N/A public abstract void draw(Graphics2D graphics, float x, float y);
0N/A
0N/A /**
0N/A * Returns the alignment of this <code>GraphicAttribute</code>.
0N/A * Alignment can be to a particular baseline, or to the absolute top
0N/A * or bottom of a line.
0N/A * @return the alignment of this <code>GraphicAttribute</code>.
0N/A */
0N/A public final int getAlignment() {
0N/A
0N/A return fAlignment;
0N/A }
0N/A
0N/A /**
0N/A * Returns the justification information for this
0N/A * <code>GraphicAttribute</code>. Subclasses
0N/A * can override this method to provide different justification
0N/A * information.
0N/A * @return a {@link GlyphJustificationInfo} object that contains the
0N/A * justification information for this <code>GraphicAttribute</code>.
0N/A */
0N/A public GlyphJustificationInfo getJustificationInfo() {
0N/A
0N/A // should we cache this?
0N/A float advance = getAdvance();
0N/A
0N/A return new GlyphJustificationInfo(
0N/A advance, // weight
0N/A false, // growAbsorb
0N/A 2, // growPriority
0N/A advance/3, // growLeftLimit
0N/A advance/3, // growRightLimit
0N/A false, // shrinkAbsorb
0N/A 1, // shrinkPriority
0N/A 0, // shrinkLeftLimit
0N/A 0); // shrinkRightLimit
0N/A }
0N/A}