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