Path: blob/master/SLICK_HOME/src/org/newdawn/slick/font/Glyph.java
1460 views
1package org.newdawn.slick.font;23import java.awt.Rectangle;4import java.awt.Shape;5import java.awt.font.GlyphMetrics;6import java.awt.font.GlyphVector;78import org.newdawn.slick.Image;9import org.newdawn.slick.UnicodeFont;1011/**12* Represents the glyph in a font for a unicode codepoint.13*14* @author Nathan Sweet <[email protected]>15*/16public class Glyph {17/** The code point in which this glyph is found */18private int codePoint;19/** The width of this glyph in pixels */20private short width;21/** The height of this glyph in pixels */22private short height;23/** The offset on the y axis to draw the glyph at */24private short yOffset;25/** True if the glyph isn't defined */26private boolean isMissing;27/** The shape drawn for this glyph */28private Shape shape;29/** The image generated for this glyph */30private Image image;3132/**33* Create a new glyph34*35* @param codePoint The code point in which this glyph can be found36* @param bounds The bounds that this glrph can fill37* @param vector The vector this glyph is part of38* @param index The index of this glyph within the vector39* @param unicodeFont The font this glyph forms part of40*/41public Glyph(int codePoint, Rectangle bounds, GlyphVector vector, int index, UnicodeFont unicodeFont) {42this.codePoint = codePoint;4344GlyphMetrics metrics = vector.getGlyphMetrics(index);45int lsb = (int)metrics.getLSB();46if (lsb > 0) lsb = 0;47int rsb = (int)metrics.getRSB();48if (rsb > 0) rsb = 0;4950int glyphWidth = bounds.width - lsb - rsb;51int glyphHeight = bounds.height;52if (glyphWidth > 0 && glyphHeight > 0) {53int padTop = unicodeFont.getPaddingTop();54int padRight = unicodeFont.getPaddingRight();55int padBottom = unicodeFont.getPaddingBottom();56int padLeft = unicodeFont.getPaddingLeft();57int glyphSpacing = 1; // Needed to prevent filtering problems.58width = (short)(glyphWidth + padLeft + padRight + glyphSpacing);59height = (short)(glyphHeight + padTop + padBottom + glyphSpacing);60yOffset = (short)(unicodeFont.getAscent() + bounds.y - padTop);61}6263shape = vector.getGlyphOutline(index, -bounds.x + unicodeFont.getPaddingLeft(), -bounds.y + unicodeFont.getPaddingTop());6465isMissing = !unicodeFont.getFont().canDisplay((char)codePoint);66}6768/**69* The unicode codepoint the glyph represents.70*71* @return The codepoint the glyph represents72*/73public int getCodePoint () {74return codePoint;75}7677/**78* Returns true if the font does not have a glyph for this codepoint.79*80* @return True if this glyph is not defined in the given code point81*/82public boolean isMissing () {83return isMissing;84}8586/**87* The width of the glyph's image.88*89* @return The width in pixels of the glyphs image90*/91public int getWidth () {92return width;93}9495/**96* The height of the glyph's image.97*98* @return The height in pixels of the glyphs image99*/100public int getHeight () {101return height;102}103104/**105* The shape to use to draw this glyph. This is set to null after the glyph is stored106* in a GlyphPage.107*108* @return The shape drawn for this glyph109*/110public Shape getShape () {111return shape;112}113114/**115* Set the shape that should be drawn for this glyph116*117* @param shape The shape that should be drawn for this glyph118*/119public void setShape(Shape shape) {120this.shape = shape;121}122123/**124* The image to use for this glyph. This is null until after the glyph is stored in a125* GlyphPage.126*127* @return The image that has been generated for this glyph128*/129public Image getImage () {130return image;131}132133/**134* Set the image that has been generated for this glyph135*136* @param image The image that has been generated for this glyph137*/138public void setImage(Image image) {139this.image = image;140}141142/**143* The distance from drawing y location to top of this glyph, causing the glyph to sit144* on the baseline.145*146* @return The offset on the y axis this glyph should be drawn at147*/148public int getYOffset() {149return yOffset;150}151}152153154