diff options
Diffstat (limited to 'libjava/classpath/java/awt/font')
5 files changed, 324 insertions, 112 deletions
diff --git a/libjava/classpath/java/awt/font/GlyphMetrics.java b/libjava/classpath/java/awt/font/GlyphMetrics.java index 18aaedc7185..0a78d3052f6 100644 --- a/libjava/classpath/java/awt/font/GlyphMetrics.java +++ b/libjava/classpath/java/awt/font/GlyphMetrics.java @@ -38,8 +38,6 @@ exception statement from your version. */ package java.awt.font; -import gnu.classpath.NotImplementedException; - import java.awt.geom.Rectangle2D; /** @@ -94,16 +92,18 @@ public final class GlyphMetrics return bounds; } - public float getLSB () - throws NotImplementedException + public float getLSB() { - throw new Error ("not implemented"); + if (horizontal) + return (float) bounds.getX(); + return (float) bounds.getY(); } - public float getRSB () - throws NotImplementedException + public float getRSB() { - throw new Error ("not implemented"); + if (horizontal) + return (float) (advanceX - (bounds.getX() + bounds.getWidth())); + return (float) (advanceY - (bounds.getY() + bounds.getHeight())); } public int getType () diff --git a/libjava/classpath/java/awt/font/GlyphVector.java b/libjava/classpath/java/awt/font/GlyphVector.java index 8d8a51d6877..f4cb01b9534 100644 --- a/libjava/classpath/java/awt/font/GlyphVector.java +++ b/libjava/classpath/java/awt/font/GlyphVector.java @@ -38,8 +38,6 @@ exception statement from your version. */ package java.awt.font; -import gnu.classpath.NotImplementedException; - import java.awt.Font; import java.awt.Rectangle; import java.awt.Shape; @@ -48,6 +46,7 @@ import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; /** + * @author Lillian Angel (langel at redhat dot com) * @author Michael Koch */ public abstract class GlyphVector implements Cloneable @@ -72,16 +71,22 @@ public abstract class GlyphVector implements Cloneable public abstract FontRenderContext getFontRenderContext (); public int getGlyphCharIndex (int glyphIndex) - throws NotImplementedException { - throw new Error ("not implemented"); + return glyphIndex; } - public int[] getGlyphCharIndices (int beginGlyphIndex, int numEntries, - int[] codeReturn) - throws NotImplementedException + public int[] getGlyphCharIndices(int beginGlyphIndex, int numEntries, + int[] codeReturn) { - throw new Error ("not implemented"); + if (codeReturn == null) + codeReturn = new int[numEntries]; + + int i = 0; + int j = beginGlyphIndex; + while (j < numEntries) + codeReturn[i++] = getGlyphCharIndex(j++); + + return codeReturn; } public abstract int getGlyphCode (int glyphIndex); @@ -98,17 +103,27 @@ public abstract class GlyphVector implements Cloneable public abstract Shape getGlyphOutline (int glyphIndex); - public Shape getGlyphOutline (int glyphIndex, float x, float y) - throws NotImplementedException + public Shape getGlyphOutline(int glyphIndex, float x, float y) { - throw new Error ("not implemented"); + Shape s = getGlyphOutline(glyphIndex); + + // This is the only way to translate the origin of a shape + AffineTransform at = AffineTransform.getTranslateInstance(x, y); + return at.createTransformedShape(s); } - public Rectangle getGlyphPixelBounds (int index, FontRenderContext renderFRC, - float x, float y) - throws NotImplementedException + public Rectangle getGlyphPixelBounds(int index, FontRenderContext renderFRC, + float x, float y) { - throw new Error ("not implemented"); + Rectangle bounds = new Rectangle(); + Rectangle2D rect = getGlyphVisualBounds(index).getBounds2D(); + + bounds.x = (int) (rect.getX() + x); + bounds.y = (int) (rect.getY() + y); + bounds.width = (int) rect.getMaxX() - bounds.x; + bounds.height = (int) rect.getMaxY() - bounds.y; + + return bounds; } public abstract Point2D getGlyphPosition (int glyphIndex); @@ -121,10 +136,9 @@ public abstract class GlyphVector implements Cloneable public abstract Shape getGlyphVisualBounds (int glyphIndex); - public int getLayoutFlags () - throws NotImplementedException + public int getLayoutFlags() { - throw new Error ("not implemented"); + return 0; } public abstract Rectangle2D getLogicalBounds (); @@ -137,9 +151,16 @@ public abstract class GlyphVector implements Cloneable public Rectangle getPixelBounds (FontRenderContext renderFRC, float x, float y) - throws NotImplementedException { - throw new Error ("not implemented"); + Rectangle bounds = new Rectangle(); + Rectangle2D rect = getVisualBounds(); + + bounds.x = (int) (rect.getX() + x); + bounds.y = (int) (rect.getY() + y); + bounds.width = (int) rect.getMaxX() - bounds.x; + bounds.height = (int) rect.getMaxY() - bounds.y; + + return bounds; } public abstract Rectangle2D getVisualBounds (); diff --git a/libjava/classpath/java/awt/font/GraphicAttribute.java b/libjava/classpath/java/awt/font/GraphicAttribute.java index 107f16dcd18..19f781bcc29 100644 --- a/libjava/classpath/java/awt/font/GraphicAttribute.java +++ b/libjava/classpath/java/awt/font/GraphicAttribute.java @@ -38,51 +38,100 @@ exception statement from your version. */ package java.awt.font; -import gnu.classpath.NotImplementedException; - import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; /** + * This class represents a graphic embedded in text. + * * @author Michael Koch + * @author Lillian Angel (langel at redhat dot com) */ public abstract class GraphicAttribute { - public static final int BOTTOM_ALIGNMENT = -2; + public static final int BOTTOM_ALIGNMENT = - 2; public static final int CENTER_BASELINE = 1; public static final int HANGING_BASELINE = 2; public static final int ROMAN_BASELINE = 0; - public static final int TOP_ALIGNMENT = -1; + public static final int TOP_ALIGNMENT = - 1; private int alignment; - - protected GraphicAttribute (int alignment) + + /** + * Constructor. + * + * @param alignment - the alignment to use for the graphic + */ + protected GraphicAttribute(int alignment) { + if (alignment < BOTTOM_ALIGNMENT || alignment > HANGING_BASELINE) + throw new IllegalArgumentException("Invalid alignment"); this.alignment = alignment; } - public abstract void draw (Graphics2D graphics, float x, float y); - - public abstract float getAdvance (); - - public final int getAlignment () + /** + * Draws the graphic. + * + * @param graphics - the graphics configuration to use + * @param x - the x location + * @param y - the y location + */ + public abstract void draw(Graphics2D graphics, float x, float y); + + /** + * Gets the distance from the origin of its graphic to the right side of the + * bounds of its graphic. + * + * @return the advance + */ + public abstract float getAdvance(); + + /** + * Gets the positive distance from the origin of its graphic to the top of + * bounds. + * + * @return the ascent + */ + public abstract float getAscent(); + + /** + * Gets the distance from the origin of its graphic to the bottom of the bounds. + * + * @return the descent + */ + public abstract float getDescent(); + + /** + * Gets the alignment. + * + * @return the alignment + */ + public final int getAlignment() { return alignment; } - public abstract float getAscent (); - - public Rectangle2D getBounds () - throws NotImplementedException + /** + * Returns a Rectangle2D that encloses the rendered area. + * Default bounds is the rectangle (0, -ascent, advance, ascent+descent). + * + * @return the bounds of the rendered area + */ + public Rectangle2D getBounds() { - throw new Error ("not implemented"); + float asc = getAscent(); + return new Rectangle2D.Float(0, - asc, getAdvance(), asc + getDescent()); } - public abstract float getDescent (); - - public GlyphJustificationInfo getJustificationInfo () - throws NotImplementedException + /** + * Returns the justification information for this object. + * + * @return the justification information + */ + public GlyphJustificationInfo getJustificationInfo() { - throw new Error ("not implemented"); + float adv = getAdvance(); + return new GlyphJustificationInfo(adv, false, 2, adv / 3, adv / 3, false, + 1, 0, 0); } } diff --git a/libjava/classpath/java/awt/font/ImageGraphicAttribute.java b/libjava/classpath/java/awt/font/ImageGraphicAttribute.java index c050255ee97..3e4fdcf733f 100644 --- a/libjava/classpath/java/awt/font/ImageGraphicAttribute.java +++ b/libjava/classpath/java/awt/font/ImageGraphicAttribute.java @@ -38,82 +38,150 @@ exception statement from your version. */ package java.awt.font; -import gnu.classpath.NotImplementedException; - import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.Rectangle2D; /** + * This is an implementation of GraphicAttribute which draws images in a + * TextLayout. + * + * @author Lillian Angel * @author Michael Koch */ -public final class ImageGraphicAttribute extends GraphicAttribute +public final class ImageGraphicAttribute + extends GraphicAttribute { private Image image; - - public ImageGraphicAttribute (Image image, int alignment) + private float originX; + private float originY; + + /** + * Constucts an instance from the specified Image. The origin is at (0, 0). + * + * @param image - image to construct from. + * @param alignment - the alignment + */ + public ImageGraphicAttribute(Image image, int alignment) { - super (alignment); - this.image = image; + this(image, alignment, 0, 0); } - public ImageGraphicAttribute (Image image, int alignment, float originX, - float originY) - throws NotImplementedException + /** + * Constucts an instance from the specified Image. The origin is at (originX, + * originY). + * + * @param image - image to construct from + * @param alignment - the alignment + * @param originX - x point of origin + * @param originY - y point of origin + */ + public ImageGraphicAttribute(Image image, int alignment, float originX, + float originY) { - super (alignment); + super(alignment); this.image = image; - - throw new Error ("not implemented"); + this.originX = originX; + this.originY = originY; } - public void draw (Graphics2D graphics, float x, float y) - throws NotImplementedException + /** + * Draws the image at the specified location, relative to the + * origin. + * + * @param g - the graphics to use to render the image + * @param x - the x location + * @param y - the y location + */ + public void draw(Graphics2D g, float x, float y) { - throw new Error ("not implemented"); + g.drawImage(image, (int) (x - originX), (int) (y - originY), null); } - public boolean equals (Object obj) + /** + * Compares this to the specified Object + * + * @param obj - the object to compare + * @return true if the obj and this are equivalent + */ + public boolean equals(Object obj) { if (! (obj instanceof ImageGraphicAttribute)) return false; - return equals ((ImageGraphicAttribute) obj); + return equals((ImageGraphicAttribute) obj); } - public boolean equals (ImageGraphicAttribute rhs) - throws NotImplementedException + /** + * Compares this to the ImageGraphicAttribute given, by + * comparing all fields and values. + * + * @param rhs - the ImageGraphicAttribute to compare + * @return true if the object given is equivalent to this + */ + public boolean equals(ImageGraphicAttribute rhs) { - throw new Error ("not implemented"); + return ((this == rhs) || ((this.getAscent() == rhs.getAscent()) + && (this.getAdvance() == rhs.getAdvance()) + && (this.getAlignment() == rhs.getAlignment()) + && (this.getBounds().equals(rhs.getBounds())) + && (this.getDescent() == rhs.getDescent()) + && (this.hashCode() == rhs.hashCode()) + && (this.image.equals(rhs.image)) + && (this.originX == rhs.originX) + && (this.originY == rhs.originY))); } - public float getAdvance () - throws NotImplementedException + /** + * Returns distance from the origin to the right edge of the image of this. + * + * @return the advance + */ + public float getAdvance() { - throw new Error ("not implemented"); + return Math.max(0, image.getWidth(null) - originX); } - public float getAscent () - throws NotImplementedException + /** + * Returns the the distance from the top of the image to the origin of this. + * + * @return the ascent. + */ + public float getAscent() { - throw new Error ("not implemented"); + return Math.max(0, originY); } - public Rectangle2D getBounds () - throws NotImplementedException + /** + * Gets the bounds of the object rendered, relative to the position. + * + * @return the bounds of the object rendered, relative to the position. + */ + public Rectangle2D getBounds() { - throw new Error ("not implemented"); + // This is equivalent to what Sun's JDK returns. + // I am not entirely sure why the origin is negative. + return new Rectangle2D.Float(- originX, - originY, image.getWidth(null), + image.getHeight(null)); } - public float getDescent () - throws NotImplementedException + /** + * Returns the distance from the origin to the bottom of the image. + * + * @return the descent + */ + public float getDescent() { - throw new Error ("not implemented"); + return Math.max(0, image.getHeight(null) - originY); } - public int hashCode () - throws NotImplementedException + /** + * Gets the hash code for this image. + * + * @return the hash code + */ + public int hashCode() { - throw new Error ("not implemented"); + return image.hashCode(); } } diff --git a/libjava/classpath/java/awt/font/ShapeGraphicAttribute.java b/libjava/classpath/java/awt/font/ShapeGraphicAttribute.java index d5320854c38..06814972b06 100644 --- a/libjava/classpath/java/awt/font/ShapeGraphicAttribute.java +++ b/libjava/classpath/java/awt/font/ShapeGraphicAttribute.java @@ -38,74 +38,148 @@ exception statement from your version. */ package java.awt.font; -import gnu.classpath.NotImplementedException; - import java.awt.Graphics2D; import java.awt.Shape; import java.awt.geom.Rectangle2D; +/** + * This is an implementation of GraphicAttribute that draws shapes in a TextLayout. + * + * @author Lillian Angel (langel at redhat dot com) + */ public final class ShapeGraphicAttribute extends GraphicAttribute { + /** True if the shape should be filled. */ public static final boolean FILL = false; + + /** True if the shape should be stroked with a 1-pixel wide stroke. */ public static final boolean STROKE = true; private Shape shape; private boolean stroke; + private Rectangle2D bounds; - public ShapeGraphicAttribute (Shape shape, int alignment, boolean stroke) + /** + * Constructor. + * + * @param shape - the Shape to render. The Shape is rendered with its origin. + * @param alignment - the alignment + * @param stroke - true if the Shape should be stroked; false if the Shape + * should be filled. + */ + public ShapeGraphicAttribute(Shape shape, int alignment, boolean stroke) { - super (alignment); + super(alignment); this.shape = shape; this.stroke = stroke; + this.bounds = shape.getBounds2D(); } - public void draw (Graphics2D graphics, float x, float y) - throws NotImplementedException + /** + * Draws the graphic at the given location. + * + * @param graphics - the graphics to use. + * @param x - the x location to draw at. + * @param y - the y location to draw at. + */ + public void draw(Graphics2D graphics, float x, float y) { - throw new Error ("not implemented"); + graphics.translate(x, y); + if (stroke == STROKE) + graphics.draw(shape); + else + graphics.fill(shape); + graphics.translate(- x, - y); } - public boolean equals (Object obj) + /** + * Compares this ShapeGraphicAttribute to obj. + * + * @param obj - the object to compare. + */ + public boolean equals(Object obj) { if (! (obj instanceof ShapeGraphicAttribute)) return false; - return equals ((ShapeGraphicAttribute) obj); + return equals((ShapeGraphicAttribute) obj); } - public boolean equals (ShapeGraphicAttribute rhs) + /** + * Compares this ShapeGraphicAttribute to rhs. + * + * @param rhs - the ShapeGraphicAttribute to compare. + */ + public boolean equals(ShapeGraphicAttribute rhs) { - return (shape.equals (rhs.shape) - && getAlignment () == rhs.getAlignment () - && stroke == rhs.stroke); + return (this == rhs || (this.shape.equals(rhs.shape) + && getAlignment() == rhs.getAlignment() + && stroke == rhs.stroke + && getAdvance() == rhs.getAdvance() + && getAscent() == rhs.getAscent() + && getBounds().equals(rhs.getBounds()) + && getDescent() == rhs.getDescent() + && hashCode() == rhs.hashCode())); } - public float getAdvance () - throws NotImplementedException + /** + * Gets the distance from the origin of its Shape to the right side of the + * bounds of its Shape. + * + * @return the advance + */ + public float getAdvance() { - throw new Error ("not implemented"); + return Math.max(0, (float) bounds.getMaxX()); } - public float getAscent () - throws NotImplementedException + /** + * Gets the positive distance from the origin of its Shape to the top of + * bounds. + * + * @return the ascent + */ + public float getAscent() { - throw new Error ("not implemented"); + return Math.max(0, -(float) bounds.getMinY()); } - public Rectangle2D getBounds () + /** + * Gets the distance from the origin of its Shape to the bottom of the bounds. + * + * @return the descent + */ + public float getDescent() { - return shape.getBounds2D (); + return Math.max(0, (float) bounds.getMaxY()); } - public float getDescent () - throws NotImplementedException + /** + * Returns a Rectangle2D that encloses all of the bits drawn by this shape. + * + * @return the bounds of the shape. + */ + public Rectangle2D getBounds() { - throw new Error ("not implemented"); + Rectangle2D.Float bounds = new Rectangle2D.Float(); + bounds.setRect(this.bounds); + + if (stroke == STROKE) + { + bounds.width++; + bounds.height++; + } + + return bounds; } - public int hashCode () + /** + * Gets the hash code. + * + * @return the hash code. + */ + public int hashCode() { - // FIXME: Check what SUN does here - return shape.hashCode (); + return shape.hashCode(); } } |