下面列出了java.awt.Font#createGlyphVector ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void writeImage(File fontFile, File outputFile, String value) throws Exception {
BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.BLACK);
Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
font = font.deriveFont(Font.PLAIN, 72f);
FontRenderContext frc = new FontRenderContext(null, false, false);
GlyphVector gv = font.createGlyphVector(frc, value);
g.drawGlyphVector(gv, 10, 80);
g.fill(gv.getOutline(10, 180));
ImageIO.write(image, "png", outputFile);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final int[] glyphs = tcctx.glyphs;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, glyphs);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final char[] chars = tcctx.chars;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, chars);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final int[] glyphs = tcctx.glyphs;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, glyphs);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final int[] glyphs = tcctx.glyphs;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, glyphs);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final int[] glyphs = tcctx.glyphs;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, glyphs);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final CharacterIterator ci = tcctx.ci;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, ci);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final String text = tcctx.text;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, text);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final CharacterIterator ci = tcctx.ci;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, ci);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final CharacterIterator ci = tcctx.ci;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, ci);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final String text = tcctx.text;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, text);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final char[] chars = tcctx.chars;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, chars);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final char[] chars = tcctx.chars;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, chars);
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
TCContext tcctx = (TCContext)ctx;
final Font font = tcctx.font;
final char[] chars = tcctx.chars;
final FontRenderContext frc = tcctx.frc;
GlyphVector gv;
do {
gv = font.createGlyphVector(frc, chars);
} while (--numReps >= 0);
}
private void textOut(String str,
Font font, PhysicalFont font2D,
FontRenderContext frc,
float deviceSize, int rotation, float awScale,
double scaleFactorX, double scaleFactorY,
float userx, float usery,
float devx, float devy, float targetW) {
String family = font2D.getFamilyName(null);
int style = font.getStyle() | font2D.getStyle();
WPrinterJob wPrinterJob = (WPrinterJob)getPrinterJob();
boolean setFont = wPrinterJob.setFont(family, deviceSize, style,
rotation, awScale);
if (!setFont) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
float[] glyphPos = null;
if (!okGDIMetrics(str, font, frc, scaleFactorX)) {
/* If there is a 1:1 char->glyph mapping then char positions
* are the same as glyph positions and we can tell GDI
* where to place the glyphs.
* On drawing we remove control chars so these need to be
* removed now so the string and positions are the same length.
* For other cases we need to pass glyph codes to GDI.
*/
str = wPrinterJob.removeControlChars(str);
char[] chars = str.toCharArray();
int len = chars.length;
GlyphVector gv = null;
if (!FontUtilities.isComplexText(chars, 0, len)) {
gv = font.createGlyphVector(frc, str);
}
if (gv == null) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
glyphPos = gv.getGlyphPositions(0, len, null);
Point2D gvAdvPt = gv.getGlyphPosition(gv.getNumGlyphs());
/* GDI advances must not include device space rotation.
* See earlier comment in printGlyphVector() for details.
*/
AffineTransform advanceTransform =
AffineTransform.getScaleInstance(scaleFactorX, scaleFactorY);
float[] glyphAdvPos = new float[glyphPos.length];
advanceTransform.transform(glyphPos, 0, //source
glyphAdvPos, 0, //destination
glyphPos.length/2); //num points
glyphPos = glyphAdvPos;
}
wPrinterJob.textOut(str, devx, devy, glyphPos);
}
private void textOut(String str,
Font font, PhysicalFont font2D,
FontRenderContext frc,
float deviceSize, int rotation, float awScale,
double scaleFactorX, double scaleFactorY,
float userx, float usery,
float devx, float devy, float targetW) {
String family = font2D.getFamilyName(null);
int style = font.getStyle() | font2D.getStyle();
WPrinterJob wPrinterJob = (WPrinterJob)getPrinterJob();
boolean setFont = wPrinterJob.setFont(family, deviceSize, style,
rotation, awScale);
if (!setFont) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
float[] glyphPos = null;
if (!okGDIMetrics(str, font, frc, scaleFactorX)) {
/* If there is a 1:1 char->glyph mapping then char positions
* are the same as glyph positions and we can tell GDI
* where to place the glyphs.
* On drawing we remove control chars so these need to be
* removed now so the string and positions are the same length.
* For other cases we need to pass glyph codes to GDI.
*/
str = wPrinterJob.removeControlChars(str);
char[] chars = str.toCharArray();
int len = chars.length;
GlyphVector gv = null;
if (!FontUtilities.isComplexText(chars, 0, len)) {
gv = font.createGlyphVector(frc, str);
}
if (gv == null) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
glyphPos = gv.getGlyphPositions(0, len, null);
Point2D gvAdvPt = gv.getGlyphPosition(gv.getNumGlyphs());
/* GDI advances must not include device space rotation.
* See earlier comment in printGlyphVector() for details.
*/
AffineTransform advanceTransform =
AffineTransform.getScaleInstance(scaleFactorX, scaleFactorY);
float[] glyphAdvPos = new float[glyphPos.length];
advanceTransform.transform(glyphPos, 0, //source
glyphAdvPos, 0, //destination
glyphPos.length/2); //num points
glyphPos = glyphAdvPos;
}
wPrinterJob.textOut(str, devx, devy, glyphPos);
}
public TextGlyphs( String text, Font font, FontRenderContext renderContext )
{
this.text = text;
this.glyphs = font.createGlyphVector( renderContext, text );
}
private void textOut(String str,
Font font, PhysicalFont font2D,
FontRenderContext frc,
float deviceSize, int rotation, float awScale,
AffineTransform deviceTransform,
double scaleFactorX,
float userx, float usery,
float devx, float devy, float targetW) {
String family = font2D.getFamilyName(null);
int style = font.getStyle() | font2D.getStyle();
WPrinterJob wPrinterJob = (WPrinterJob)getPrinterJob();
boolean setFont = wPrinterJob.setFont(family, deviceSize, style,
rotation, awScale);
if (!setFont) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
float[] glyphPos = null;
if (!okGDIMetrics(str, font, frc, scaleFactorX)) {
/* If there is a 1:1 char->glyph mapping then char positions
* are the same as glyph positions and we can tell GDI
* where to place the glyphs.
* On drawing we remove control chars so these need to be
* removed now so the string and positions are the same length.
* For other cases we need to pass glyph codes to GDI.
*/
str = wPrinterJob.removeControlChars(str);
char[] chars = str.toCharArray();
int len = chars.length;
GlyphVector gv = null;
if (!FontUtilities.isComplexText(chars, 0, len)) {
gv = font.createGlyphVector(frc, str);
}
if (gv == null) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
glyphPos = gv.getGlyphPositions(0, len, null);
Point2D gvAdvPt = gv.getGlyphPosition(gv.getNumGlyphs());
/* GDI advances must not include device space rotation.
* See earlier comment in printGlyphVector() for details.
*/
AffineTransform advanceTransform =
new AffineTransform(deviceTransform);
advanceTransform.rotate(rotation*Math.PI/1800.0);
float[] glyphAdvPos = new float[glyphPos.length];
advanceTransform.transform(glyphPos, 0, //source
glyphAdvPos, 0, //destination
glyphPos.length/2); //num points
glyphPos = glyphAdvPos;
}
wPrinterJob.textOut(str, devx, devy, glyphPos);
}
private void textOut(String str,
Font font, PhysicalFont font2D,
FontRenderContext frc,
float deviceSize, int rotation, float awScale,
double scaleFactorX, double scaleFactorY,
float userx, float usery,
float devx, float devy, float targetW) {
String family = font2D.getFamilyName(null);
int style = font.getStyle() | font2D.getStyle();
WPrinterJob wPrinterJob = (WPrinterJob)getPrinterJob();
boolean setFont = wPrinterJob.setFont(family, deviceSize, style,
rotation, awScale);
if (!setFont) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
float[] glyphPos = null;
if (!okGDIMetrics(str, font, frc, scaleFactorX)) {
/* If there is a 1:1 char->glyph mapping then char positions
* are the same as glyph positions and we can tell GDI
* where to place the glyphs.
* On drawing we remove control chars so these need to be
* removed now so the string and positions are the same length.
* For other cases we need to pass glyph codes to GDI.
*/
str = wPrinterJob.removeControlChars(str);
char[] chars = str.toCharArray();
int len = chars.length;
GlyphVector gv = null;
if (!FontUtilities.isComplexText(chars, 0, len)) {
gv = font.createGlyphVector(frc, str);
}
if (gv == null) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
glyphPos = gv.getGlyphPositions(0, len, null);
Point2D gvAdvPt = gv.getGlyphPosition(gv.getNumGlyphs());
/* GDI advances must not include device space rotation.
* See earlier comment in printGlyphVector() for details.
*/
AffineTransform advanceTransform =
AffineTransform.getScaleInstance(scaleFactorX, scaleFactorY);
float[] glyphAdvPos = new float[glyphPos.length];
advanceTransform.transform(glyphPos, 0, //source
glyphAdvPos, 0, //destination
glyphPos.length/2); //num points
glyphPos = glyphAdvPos;
}
wPrinterJob.textOut(str, devx, devy, glyphPos);
}
private void textOut(String str,
Font font, PhysicalFont font2D,
FontRenderContext frc,
float deviceSize, int rotation, float awScale,
AffineTransform deviceTransform,
double scaleFactorX,
float userx, float usery,
float devx, float devy, float targetW) {
String family = font2D.getFamilyName(null);
int style = font.getStyle() | font2D.getStyle();
WPrinterJob wPrinterJob = (WPrinterJob)getPrinterJob();
boolean setFont = wPrinterJob.setFont(family, deviceSize, style,
rotation, awScale);
if (!setFont) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
float[] glyphPos = null;
if (!okGDIMetrics(str, font, frc, scaleFactorX)) {
/* If there is a 1:1 char->glyph mapping then char positions
* are the same as glyph positions and we can tell GDI
* where to place the glyphs.
* On drawing we remove control chars so these need to be
* removed now so the string and positions are the same length.
* For other cases we need to pass glyph codes to GDI.
*/
str = wPrinterJob.removeControlChars(str);
char[] chars = str.toCharArray();
int len = chars.length;
GlyphVector gv = null;
if (!FontUtilities.isComplexText(chars, 0, len)) {
gv = font.createGlyphVector(frc, str);
}
if (gv == null) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
glyphPos = gv.getGlyphPositions(0, len, null);
Point2D gvAdvPt = gv.getGlyphPosition(gv.getNumGlyphs());
/* GDI advances must not include device space rotation.
* See earlier comment in printGlyphVector() for details.
*/
AffineTransform advanceTransform =
new AffineTransform(deviceTransform);
advanceTransform.rotate(rotation*Math.PI/1800.0);
float[] glyphAdvPos = new float[glyphPos.length];
advanceTransform.transform(glyphPos, 0, //source
glyphAdvPos, 0, //destination
glyphPos.length/2); //num points
glyphPos = glyphAdvPos;
}
wPrinterJob.textOut(str, devx, devy, glyphPos);
}