Java 中哪个 API 返回正确的字体宽度?

发布于 2024-11-10 03:01:14 字数 293 浏览 0 评论 0原文

Java 的哪个 Font API 返回正确的宽度?我们当前使用

font.getStringBounds( "the string", frc ).getWidth();

这会在图像和显示上返回正确的值。但是用 Java SE 6 打印是错误的。用 Java 7 一切都是正确的。差异非常小。但是我们在一行的末尾丢失了一个“i”。我们在打印时收到完全相同的值。但文本在打印图形上需要更多空间。

我们使用抗锯齿和分数指标。

我知道有不同的API。但推荐的 API 是什么?

Which Font API of Java returns the right width? We use currently

font.getStringBounds( "the string", frc ).getWidth();

This return the right values on images and display. But it is wrong on printing with Java SE 6. With Java 7 all is correct. The difference is very small. But we lost one "i" at end of a line. We receive on printing exact the same values. But the text need more space on a printing graphics.

We use anti alias and fractional metrics.

I know there are different API. But what is the recommended API?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彡翼 2024-11-17 03:01:14

您有打印机的 Graphics 对象吗?如果您测试这段代码,在这种情况下会发生什么?这就是我用来测量宽度的东西:

/*
Graphics2D g
Font currentFont
String stringToMeasure
*/
FontMetrics currentFontMetrics = g.getFontMetrics(currentFont);
Rectangle2D stringBounds = currentFontMetrics.getStringBounds(stringToMeasure, g);
double width = stringBounds.getWidth();

Do you have a Graphics object to the printer? What does in that case happen if you test this code? It's what I'm using to measure width:

/*
Graphics2D g
Font currentFont
String stringToMeasure
*/
FontMetrics currentFontMetrics = g.getFontMetrics(currentFont);
Rectangle2D stringBounds = currentFontMetrics.getStringBounds(stringToMeasure, g);
double width = stringBounds.getWidth();
深海里的那抹蓝 2024-11-17 03:01:14

Java 默认舍入所有字体规格,因为它假设您正在尝试渲染到屏幕,您需要扩展 FontRenderContext 并打开 FractionalMetricsHint,如下所示。然后,使用它来传递字体渲染上下文。

import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;

public class MyFontRenderContext extends FontRenderContext {

public MyFontRenderContext()
{

}
@Override
public AffineTransform getTransform()
{
    return new AffineTransform();
}

/* (non-Javadoc)
 * @see java.awt.font.FontRenderContext#usesFractionalMetrics()
 */
@Override
public boolean usesFractionalMetrics()
{
    return true;
}

/* (non-Javadoc)
 * @see java.awt.font.FontRenderContext#getFractionalMetricsHint()
 */
@Override
public Object getFractionalMetricsHint()
{
    // TODO Auto-generated method stub
    return RenderingHints.VALUE_FRACTIONALMETRICS_ON;
}

/* (non-Javadoc)
 * @see java.awt.font.FontRenderContext#getAntiAliasingHint()
 */
@Override
public Object getAntiAliasingHint()
{
    return RenderingHints.VALUE_ANTIALIAS_ON;
}

/* (non-Javadoc)
 * @see java.awt.font.FontRenderContext#isAntiAliased()
 */
@Override
public boolean isAntiAliased()
{
    return true;
}

}

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class FontMetricsReader
{
public static void main(String[] args)
{
    Font f = new Font("Frutiger55Roman", java.awt.Font.PLAIN, 6);
    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();
    FontMetrics metrics = g.getFontMetrics(f);

    String str = "The quick brown fox jumps over the lazy dog";

    System.out.println("Rounded text width = " + metrics.stringWidth(str));

    System.out.println("Accurate text width = " + f.getStringBounds(str, new MyFontRenderContext()).getWidth());
}

}

Java rounds all font metrics by default, because it assumes you are attempting to render to a screen, what you need to extend FontRenderContext and turn on FractionalMetricsHint as shown below. Then, use that to pass in a font render context.

import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;

public class MyFontRenderContext extends FontRenderContext {

public MyFontRenderContext()
{

}
@Override
public AffineTransform getTransform()
{
    return new AffineTransform();
}

/* (non-Javadoc)
 * @see java.awt.font.FontRenderContext#usesFractionalMetrics()
 */
@Override
public boolean usesFractionalMetrics()
{
    return true;
}

/* (non-Javadoc)
 * @see java.awt.font.FontRenderContext#getFractionalMetricsHint()
 */
@Override
public Object getFractionalMetricsHint()
{
    // TODO Auto-generated method stub
    return RenderingHints.VALUE_FRACTIONALMETRICS_ON;
}

/* (non-Javadoc)
 * @see java.awt.font.FontRenderContext#getAntiAliasingHint()
 */
@Override
public Object getAntiAliasingHint()
{
    return RenderingHints.VALUE_ANTIALIAS_ON;
}

/* (non-Javadoc)
 * @see java.awt.font.FontRenderContext#isAntiAliased()
 */
@Override
public boolean isAntiAliased()
{
    return true;
}

}

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class FontMetricsReader
{
public static void main(String[] args)
{
    Font f = new Font("Frutiger55Roman", java.awt.Font.PLAIN, 6);
    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();
    FontMetrics metrics = g.getFontMetrics(f);

    String str = "The quick brown fox jumps over the lazy dog";

    System.out.println("Rounded text width = " + metrics.stringWidth(str));

    System.out.println("Accurate text width = " + f.getStringBounds(str, new MyFontRenderContext()).getWidth());
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文