2012-04-14 21 views

回答

2

您可以獲取字形輪廓使用Font.createGlyphVectorGlyphVector.getGlyphOutline

以下方法爲指定的String檢索GlyphVector並檢索其輪廓,並在該過程中應用AffineTransform

static Shape[] getGlyphShapes(Font font, String strGlyphs, AffineTransform transform) { 

    FontRenderContext frc = new FontRenderContext(null, true, true); 
    GlyphVector glyphs = font.createGlyphVector(frc, strGlyphs); 

    int count = glyphs.getNumGlyphs(); 
    Shape[] shapes = new Shape[count]; 
    for (int i = 0; i < count; i ++) { 

     // get transformed glyph shape 
     GeneralPath path = (GeneralPath) glyphs.getGlyphOutline(i); 
     shapes[i] = path.createTransformedShape(transform); 
    } 
    return shapes; 

} 

默認情況下,返回的字形的字體大小爲1,因此爲AffineTransform。一個例子變換是:

  AffineTransform transform = new AffineTransform(); 
      transform.translate(-20, 20); 
      transform.scale(40, 40); 

哪些應該大致在40字體大小居中每個字形(我想你會需要)爲了更準確的中心,你可以使用GlyphMetrics,通過GlyphVector.getGlyphMetric得到和計算你需要的確切的翻譯。

相關問題