下面列出了java.awt.font.GlyphVector#getGlyphMetrics ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public GlyphElement(GlyphVector gv, int index) {
glyphCode = gv.getGlyphCode(index);
transform = gv.getGlyphTransform(index);
outline = new ShapeSerializationWrapper(gv.getGlyphOutline(index));
position = new Point2DSerializationWrapper(
gv.getGlyphPosition(index));
logicalBounds = new ShapeSerializationWrapper(
gv.getGlyphLogicalBounds(index));
GlyphJustificationInfo gji = gv.getGlyphJustificationInfo(index);
justificationInfo = gji == null ? null
: new GlyphJustificationInfoSerializationWrapper(gji);
glyphMetrics = new GlyphMetricsSerializationWrapper(
gv.getGlyphMetrics(index));
visualBounds = new ShapeSerializationWrapper(
gv.getGlyphVisualBounds(index));
}
private static GlyphVector getWrappedGlyphVector(String str, double width, Font font, FontRenderContext frc) {
Point2D gmPos = new Point2D.Float();
GlyphVector gv = font.createGlyphVector(frc, str);
float lineHeight = (float) gv.getLogicalBounds().getHeight();
float pos = 0f;
int lineCount = 0;
GlyphMetrics gm;
for (int i = 0; i < gv.getNumGlyphs(); i++) {
gm = gv.getGlyphMetrics(i);
float advance = gm.getAdvance();
if (pos < width && width <= pos + advance) {
lineCount++;
pos = 0f;
}
gmPos.setLocation(pos, lineHeight * lineCount);
gv.setGlyphPosition(i, gmPos);
pos += advance;
}
return gv;
}
private static GlyphVector getWrappedGlyphVector(String str, double width, Font font, FontRenderContext frc) {
Point2D gmPos = new Point2D.Float();
GlyphVector gv = font.createGlyphVector(frc, str);
float lineHeight = (float) gv.getLogicalBounds().getHeight();
float pos = 0f;
int lineCount = 0;
GlyphMetrics gm;
for (int i = 0; i < gv.getNumGlyphs(); i++) {
gm = gv.getGlyphMetrics(i);
float advance = gm.getAdvance();
if (pos < width && width <= pos + advance) {
lineCount++;
pos = 0f;
}
gmPos.setLocation(pos, lineHeight * lineCount);
gv.setGlyphPosition(i, gmPos);
pos += advance;
}
return gv;
}
private static GlyphVector makeJustifiedGlyphVector(GlyphVector gv, int width) {
Rectangle2D r = gv.getVisualBounds();
float jw = (float) width;
float vw = (float) r.getWidth();
if (jw > vw) {
int num = gv.getNumGlyphs();
float xx = (jw - vw) / (num - 1f);
float pos = num == 1 ? (jw - vw) * .5f : 0f;
Point2D gmPos = new Point2D.Float();
for (int i = 0; i < num; i++) {
GlyphMetrics gm = gv.getGlyphMetrics(i);
gmPos.setLocation(pos, 0f);
gv.setGlyphPosition(i, gmPos);
pos += gm.getAdvance() + xx;
}
}
return gv;
}
private static GlyphVector getWrappedGlyphVector(String str, double width, Font font, FontRenderContext frc) {
Point2D gmPos = new Point2D.Float();
GlyphVector gv = font.createGlyphVector(frc, str);
float lineHeight = (float) gv.getLogicalBounds().getHeight();
float pos = 0f;
int lineCount = 0;
GlyphMetrics gm;
for (int i = 0; i < gv.getNumGlyphs(); i++) {
// TEST: gv.setGlyphTransform(i, at);
gm = gv.getGlyphMetrics(i);
float advance = gm.getAdvance();
if (pos < width && width <= pos + advance) {
lineCount++;
pos = 0f;
}
gmPos.setLocation(pos, lineHeight * lineCount);
gv.setGlyphPosition(i, gmPos);
pos += advance;
}
return gv;
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
/**
* Create a new glyph
*
* @param codePoint The code point in which this glyph can be found
* @param bounds The bounds that this glrph can fill
* @param vector The vector this glyph is part of
* @param index The index of this glyph within the vector
* @param unicodeFont The font this glyph forms part of
*/
public Glyph(int codePoint, Rectangle bounds, GlyphVector vector, int index, UnicodeFont unicodeFont) {
this.codePoint = codePoint;
GlyphMetrics metrics = vector.getGlyphMetrics(index);
int lsb = (int)metrics.getLSB();
if (lsb > 0) lsb = 0;
int rsb = (int)metrics.getRSB();
if (rsb > 0) rsb = 0;
int glyphWidth = bounds.width - lsb - rsb;
int glyphHeight = bounds.height;
if (glyphWidth > 0 && glyphHeight > 0) {
int padTop = unicodeFont.getPaddingTop();
int padRight = unicodeFont.getPaddingRight();
int padBottom = unicodeFont.getPaddingBottom();
int padLeft = unicodeFont.getPaddingLeft();
int glyphSpacing = 1; // Needed to prevent filtering problems.
width = (short)(glyphWidth + padLeft + padRight + glyphSpacing);
height = (short)(glyphHeight + padTop + padBottom + glyphSpacing);
yOffset = (short)(unicodeFont.getAscent() + bounds.y - padTop);
}
shape = vector.getGlyphOutline(index, -bounds.x + unicodeFont.getPaddingLeft(), -bounds.y + unicodeFont.getPaddingTop());
isMissing = !unicodeFont.getFont().canDisplay((char)codePoint);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
private int[] getGlyphMetrics (Font font, int codePoint) {
// xOffset and xAdvance will be incorrect for unicode characters such as combining marks or non-spacing characters
// (eg Pnujabi's "\u0A1C\u0A47") that require the context of surrounding glyphs to determine spacing, but thisis the
// best we can do with the BMFont format.
char[] chars = Character.toChars(codePoint);
GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
GlyphMetrics metrics = vector.getGlyphMetrics(0);
int xOffset = vector.getGlyphPixelBounds(0, null, 0, 0).x - unicodeFont.getPaddingLeft();
int xAdvance = (int)(metrics.getAdvanceX() + unicodeFont.getPaddingAdvanceX() + unicodeFont.getPaddingLeft() + unicodeFont
.getPaddingRight());
return new int[] {xOffset, xAdvance};
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
private int[] getGlyphMetrics (Font font, int codePoint) {
// xOffset and xAdvance will be incorrect for unicode characters such as combining marks or non-spacing characters
// (eg Pnujabi's "\u0A1C\u0A47") that require the context of surrounding glyphs to determine spacing, but thisis the
// best we can do with the BMFont format.
char[] chars = Character.toChars(codePoint);
GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
GlyphMetrics metrics = vector.getGlyphMetrics(0);
int xOffset = vector.getGlyphPixelBounds(0, GlyphPage.renderContext, 0.5f, 0).x - unicodeFont.getPaddingLeft();
int xAdvance = (int)(metrics.getAdvanceX() + unicodeFont.getPaddingAdvanceX() + unicodeFont.getPaddingLeft() + unicodeFont
.getPaddingRight());
return new int[] {xOffset, xAdvance};
}
public static float getSystemFontAdvance(Font aFont, Character character, Character nextCharacter) {
String chars = "" + character + (nextCharacter == null ? "" : nextCharacter);
GlyphVector gv = aFont.layoutGlyphVector(new FontRenderContext(aFont.getTransform(), true, true), chars.toCharArray(), 0, chars.length(), Font.LAYOUT_LEFT_TO_RIGHT);
GlyphMetrics gm = gv.getGlyphMetrics(0);
return gm.getAdvanceX();
}