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
17
18
19
20 public class Glyph {
21
22 private final int codePoint;
23
24 private short width;
25
26 private short height;
27
28 private short yOffset;
29
30 private final boolean isMissing;
31
32 private Shape shape;
33
34 private Image image;
35
36
37
38
39
40
41
42
43
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;
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
74
75
76
77 public int getCodePoint () {
78 return codePoint;
79 }
80
81
82
83
84
85
86 public boolean isMissing () {
87 return isMissing;
88 }
89
90
91
92
93
94
95 public int getWidth () {
96 return width;
97 }
98
99
100
101
102
103
104 public int getHeight () {
105 return height;
106 }
107
108
109
110
111
112
113
114 public Shape getShape () {
115 return shape;
116 }
117
118
119
120
121
122
123 public void setShape(@Nullable Shape shape) {
124 this.shape = shape;
125 }
126
127
128
129
130
131
132
133 public Image getImage () {
134 return image;
135 }
136
137
138
139
140
141
142 public void setImage(Image image) {
143 this.image = image;
144 }
145
146
147
148
149
150
151
152 public int getYOffset() {
153 return yOffset;
154 }
155 }