将文本**写入Graphics2D
对象时,我发现当我更改字体大小时,文本的x位置会发生变化。随着尺寸的增加,x 位置也会增加。无论我使用什么方法编写文本,都会发生这种情况:使用 GlyphVector
来编写文本:drawString
、fill
或 draw
,或drawGlyphVector
。
** 文本的开头字符和字体类型都会更改此行为:Sans Serif 字体,其中开头字符的最左侧笔画是垂直线(P、H、L、E 等...)是受影响最大的。如果字体是 Serif 和/或起始字符的最左边笔划不是垂直的(W、y、Y、T、X 等...),则这种行为会减少或消除。
下面是说明此行为的示例程序:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class FontSizeTest {
public FontSizeTest() {
super();
}
public static void main(String[] args) throws IOException {
int width = 700, height = 120;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
FontRenderContext frc = g2d.getFontRenderContext();
Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 10);
GlyphVector gv;
Rectangle pixelBounds;
g2d.setColor(Color.BLACK);
g2d.translate(10, 100);
for(float size : new float[]{40f, 80f, 120f}) {
font = font.deriveFont(size);
gv = font.createGlyphVector(frc, "Hello World");
pixelBounds = gv.getPixelBounds(frc, 0, 0);
System.out.printf("font: %s%npixel bounds: %s%n", font, pixelBounds);
g2d.fill(gv.getOutline());
g2d.draw(pixelBounds);
}
g2d.dispose();
File pngFile = new File("FontSizeTest.png");
ImageIO.write(image, "png", pngFile);
System.out.printf("png file written: %s%n", pngFile.getCanonicalPath());
}
}
该程序的输出是控制台的信息:
font: java.awt.Font[family=SansSerif,name=SansSerif,style=plain,size=40]
pixel bounds: java.awt.Rectangle[x=4,y=-31,width=217,height=31]
font: java.awt.Font[family=SansSerif,name=SansSerif,style=plain,size=80]
pixel bounds: java.awt.Rectangle[x=7,y=-62,width=432,height=63]
font: java.awt.Font[family=SansSerif,name=SansSerif,style=plain,size=120]
pixel bounds: java.awt.Rectangle[x=11,y=-93,width=651,height=94]
png file written: FontSizeTest.png
还有图像:
注意较大的字体大小如何与较小的字体大小绘制偏移。
如果这是预期的行为,我可以通过获取像素边界的偏移量并使用 translate
调整 x 坐标来进行调整;我只是不确定这是预期的行为。我觉得我做错了什么 - 缺少 Graphics2D
编程方法的某些部分。
最佳答案
此效果是字体的正常行为。即使 Microsoft Word 在不同大小的文本行中也存在这种偏移。
如果您想将所有文本行对齐在同一像素行,您可以使用 translate
与您提到的计算出的像素边界。
一种更优雅的方法是改变 GlyphVector 中的字形位置。这也将导致 getPixelBounds
结果发生变化。
public static void adjust(GlyphVector gv){
// calc the adjust Factor
int adjust = (int)Math.round(gv.getGlyphVisualBounds(0).getBounds2D().getMinX());
// Shift all the Glyphs in Vector
for (int i = 0; i < gv.getNumGlyphs(); i++) {
Point2D point = gv.getGlyphPosition(i);
gv.setGlyphPosition( i, new Point2D.Double(
point.getX() - adjust,
point.getY()));
}
}
创建字形 vector 后在代码中使用它,即可获得所需的结果。
gv = font.createGlyphVector(frc, "Hello World");
adjust(gv);
关于java - Graphics2D 文本 - 字体大小变化导致 X 轴移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34211978/