下面列出了java.awt.font.GlyphVector#getGlyphOutline ( ) 实例代码,或者点击链接到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));
}
public static Set<PGData3> triangulateGLText(org.eclipse.swt.graphics.Font font, final String text, final double flatness, final double interpolateFlatness, final double deltaAngle) {
String[] ff = font.getFontData()[0].getName().split(Pattern.quote("-")); //$NON-NLS-1$
String fontName;
if (ff.length > 1) {
fontName = ff[1];
} else {
fontName = ff[0];
}
Font myFont = new Font(fontName, Font.BOLD | Font.HANGING_BASELINE, 8);
final GlyphVector vector = myFont.createGlyphVector(new FontRenderContext(null, false, false), text);
final Set<PGData3> finalTriangleSet = new HashSet<PGData3>();
if (vector.getNumGlyphs() == 0)
return finalTriangleSet;
for (int j = 0; j < vector.getNumGlyphs(); j++) {
Shape characterShape = vector.getGlyphOutline(j);
Set<PGData3> characterTriangleSet = triangulateGLShape(characterShape, flatness, interpolateFlatness, .002f, deltaAngle);
finalTriangleSet.addAll(characterTriangleSet);
}
return finalTriangleSet;
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
protected BufferedImage createImage(Color bgColor) {
BufferedImage bufferedImage = new BufferedImage(END_X, END_Y,
BufferedImage.TYPE_INT_RGB);
// create graphics and graphics2d
final Graphics graphics = bufferedImage.getGraphics();
final Graphics2D g2d = (Graphics2D) graphics;
// set the background color
g2d.setBackground(bgColor == null ? Color.gray : bgColor);
g2d.clearRect(START_X, START_Y, END_X, END_Y);
// create a pattern for the background
createPattern(g2d);
// set the fonts and font rendering hints
Font font = new Font("Helvetica", Font.ITALIC, 30);
g2d.setFont(font);
FontRenderContext frc = g2d.getFontRenderContext();
g2d.translate(10, 24);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(3));
// sets the foreground color
g2d.setPaint(Color.DARK_GRAY);
GlyphVector gv = font.createGlyphVector(frc, message);
int numGlyphs = gv.getNumGlyphs();
for (int ii = 0; ii < numGlyphs; ii++) {
AffineTransform at;
Point2D p = gv.getGlyphPosition(ii);
at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
at.rotate(Math.PI / 8);
Shape shape = gv.getGlyphOutline(ii);
Shape sss = at.createTransformedShape(shape);
g2d.fill(sss);
}
return blurImage(bufferedImage);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
Shape s;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
s = gv.getGlyphOutline(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);
}
@Override
public Shape createStrokedShape ( final Shape shape )
{
final FontRenderContext frc = new FontRenderContext ( null, true, true );
final GlyphVector glyphVector = font.createGlyphVector ( frc, text );
final GeneralPath result = new GeneralPath ();
final PathIterator it = new FlatteningPathIterator ( shape.getPathIterator ( null ), FLATNESS );
final float[] points = new float[ 6 ];
float moveX = 0;
float moveY = 0;
float lastX = 0;
float lastY = 0;
float thisX;
float thisY;
int type;
float next = 0;
int currentChar = 0;
final int length = glyphVector.getNumGlyphs ();
if ( length == 0 )
{
return result;
}
final float factor = stretchToFit ? measurePathLength ( shape ) / ( float ) glyphVector.getLogicalBounds ().getWidth () : 1.0f;
float nextAdvance = 0;
while ( currentChar < length && !it.isDone () )
{
type = it.currentSegment ( points );
switch ( type )
{
case PathIterator.SEG_MOVETO:
moveX = lastX = points[ 0 ];
moveY = lastY = points[ 1 ];
result.moveTo ( moveX, moveY );
nextAdvance = glyphVector.getGlyphMetrics ( currentChar ).getAdvance () * 0.5f;
next = nextAdvance;
break;
case PathIterator.SEG_CLOSE:
points[ 0 ] = moveX;
points[ 1 ] = moveY;
// Fall into....
case PathIterator.SEG_LINETO:
thisX = points[ 0 ];
thisY = points[ 1 ];
final float dx = thisX - lastX;
final float dy = thisY - lastY;
final float distance = ( float ) Math.sqrt ( dx * dx + dy * dy );
if ( distance >= next )
{
final float r = 1.0f / distance;
final float angle = ( float ) Math.atan2 ( dy, dx );
while ( currentChar < length && distance >= next )
{
final Shape glyph = glyphVector.getGlyphOutline ( currentChar );
final Point2D p = glyphVector.getGlyphPosition ( currentChar );
final float px = ( float ) p.getX ();
final float py = ( float ) p.getY ();
final float x = lastX + next * dx * r;
final float y = lastY + next * dy * r;
final float advance = nextAdvance;
nextAdvance =
currentChar < length - 1 ? glyphVector.getGlyphMetrics ( currentChar + 1 ).getAdvance () * 0.5f : 0;
t.setToTranslation ( x, y );
t.rotate ( angle );
t.translate ( -px - advance, -py );
result.append ( t.createTransformedShape ( glyph ), false );
next += ( advance + nextAdvance ) * factor;
currentChar++;
if ( repeat )
{
currentChar %= length;
}
}
}
next -= distance;
lastX = thisX;
lastY = thisY;
break;
}
it.next ();
}
return result;
}
@Override
public void paint(Graphics g1)
{
Graphics2D g = (Graphics2D) g1;
final int TEST = 144;
Font f = new Font(文泉驛正黑, Font.BOLD, TEST);
GlyphVector gv = f
.createGlyphVector(((Graphics2D) g1).getFontRenderContext(),
"一二三變⿴務⿰意⿱國a,龜龘"/* ⿻ab!" */);
// System.out.println(gv.getNumGlyphs());
// Shape shape = gv.getGlyphLogicalBounds(0);
System.out.println(gv.getGlyphLogicalBounds(0));
System.out.println(gv.getGlyphOutline(0).getBounds());
Rectangle2D.Double lDouble = new Rectangle2D.Double(0, 0, 1000, 1);
g.setStroke(new NullStroke());
g.translate(0, 144);
g.setColor(Color.RED);
g.draw(lDouble);
for (int i = 0; i < gv.getNumGlyphs(); ++i)
{
g.translate(1, 0);
g.setColor(Color.GRAY);
g.draw(gv.getGlyphLogicalBounds(i));
g.setColor(Color.LIGHT_GRAY);
g.draw(gv.getGlyphVisualBounds(i));
g.setColor(Color.BLACK);
g.draw(gv.getGlyphOutline(i));// TODO 忘記要幹麻了@@
System.out.println((new Area(gv.getGlyphLogicalBounds(i)))
.getBounds2D());
System.out.println((new Area(gv.getGlyphVisualBounds(i)))
.getBounds2D());
System.out.println((new Area(gv.getGlyphVisualBounds(i)))
.getBounds2D().getY()
+ (new Area(gv.getGlyphVisualBounds(i))).getBounds2D()
.getHeight() / 2);
// System.out.println(gv.getGlyphPosition(i));
ShapeInformation shapeInformation = new ShapeInformation(new Area(
gv.getGlyphOutline(i)));
System.out.println(i + "="
+ shapeInformation.getApproximativeRegion()
/ shapeInformation.getApproximativeCircumference() / TEST
+ "=" + shapeInformation.getApproximativeRegion() + "/"
+ shapeInformation.getApproximativeCircumference() + "/"
+ TEST);
}
return;
}