View Javadoc
1   
2   package org.newdawn.slick.font;
3   
4   import java.awt.Rectangle;
5   import java.awt.Shape;
6   import java.awt.font.GlyphMetrics;
7   import java.awt.font.GlyphVector;
8   
9   import org.newdawn.slick.Image;
10  import org.newdawn.slick.UnicodeFont;
11  
12  import javax.annotation.Nonnull;
13  import javax.annotation.Nullable;
14  
15  /**
16   * Represents the glyph in a font for a unicode codepoint.
17   * 
18   * @author Nathan Sweet <misc@n4te.com>
19   */
20  public class Glyph {
21      /** The code point in which this glyph is found */
22      private final int codePoint;
23      /** The width of this glyph in pixels */
24      private short width;
25      /** The height of this glyph in pixels */
26      private short height;
27      /** The offset on the y axis to draw the glyph at */
28      private short yOffset;
29      /** True if the glyph isn't defined */
30      private final boolean isMissing;
31      /** The shape drawn for this glyph */
32      private Shape shape;
33      /** The image generated for this glyph */
34      private Image image;
35  
36      /**
37       * Create a new glyph
38       *
39       * @param codePoint The code point in which this glyph can be found
40       * @param bounds The bounds that this glrph can fill
41       * @param vector The vector this glyph is part of
42       * @param index The index of this glyph within the vector
43       * @param unicodeFont The font this glyph forms part of
44       */
45      public Glyph(int codePoint, @Nonnull Rectangle bounds, @Nonnull GlyphVector vector, int index, @Nonnull UnicodeFont unicodeFont) {
46          this.codePoint = codePoint;
47  
48          GlyphMetrics metrics = vector.getGlyphMetrics(index);
49          int lsb = (int)metrics.getLSB();
50          if (lsb > 0) lsb = 0;
51          int rsb = (int)metrics.getRSB();
52          if (rsb > 0) rsb = 0;
53  
54          int glyphWidth = bounds.width - lsb - rsb;
55          int glyphHeight = bounds.height;
56          if (glyphWidth > 0 && glyphHeight > 0) {
57              int padTop = unicodeFont.getPaddingTop();
58              int padRight = unicodeFont.getPaddingRight();
59              int padBottom = unicodeFont.getPaddingBottom();
60              int padLeft = unicodeFont.getPaddingLeft();
61              int glyphSpacing = 1; // Needed to prevent filtering problems.
62              width = (short)(glyphWidth + padLeft + padRight + glyphSpacing);
63              height = (short)(glyphHeight + padTop + padBottom + glyphSpacing);
64              yOffset = (short)(unicodeFont.getAscent() + bounds.y - padTop);
65          }
66  
67          shape = vector.getGlyphOutline(index, -bounds.x + unicodeFont.getPaddingLeft(), -bounds.y + unicodeFont.getPaddingTop());
68  
69          isMissing = !unicodeFont.getFont().canDisplay((char)codePoint);
70      }
71  
72      /**
73       * The unicode codepoint the glyph represents.
74       *
75       * @return The codepoint the glyph represents
76       */
77      public int getCodePoint () {
78          return codePoint;
79      }
80  
81      /**
82       * Returns true if the font does not have a glyph for this codepoint.
83       *
84       * @return True if this glyph is not defined in the given code point
85       */
86      public boolean isMissing () {
87          return isMissing;
88      }
89  
90      /**
91       * The width of the glyph's image.
92       *
93       * @return The width in pixels of the glyphs image
94       */
95      public int getWidth () {
96          return width;
97      }
98  
99      /**
100      * The height of the glyph's image.
101      *
102      * @return The height in pixels of the glyphs image
103      */
104     public int getHeight () {
105         return height;
106     }
107 
108     /**
109      * The shape to use to draw this glyph. This is set to null after the glyph is stored
110      * in a GlyphPage.
111      *
112      * @return The shape drawn for this glyph
113      */
114     public Shape getShape () {
115         return shape;
116     }
117 
118     /**
119      * Set the shape that should be drawn for this glyph
120      *
121      * @param shape The shape that should be drawn for this glyph
122      */
123     public void setShape(@Nullable Shape shape) {
124         this.shape = shape;
125     }
126 
127     /**
128      * The image to use for this glyph. This is null until after the glyph is stored in a
129      * GlyphPage.
130      *
131      * @return The image that has been generated for this glyph
132      */
133     public Image getImage () {
134         return image;
135     }
136 
137     /**
138      * Set the image that has been generated for this glyph
139      *
140      * @param image The image that has been generated for this glyph
141      */
142     public void setImage(Image image) {
143         this.image = image;
144     }
145 
146     /**
147      * The distance from drawing y location to top of this glyph, causing the glyph to sit
148      * on the baseline.
149      *
150      * @return The offset on the y axis this glyph should be drawn at
151      */
152     public int getYOffset() {
153         return yOffset;
154     }
155 }