- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当我查看 FontMetric.getAscent()
的 javadoc 时,我看到:
The font ascent is the distance from the font's baseline to the top of most alphanumeric characters. Some characters in the Font might extend above the font ascent line.
但是我写了一个快速演示程序,我看到了这个:
每行文本的 4 条水平线是:
getDescent()
降低的基线位置getAscent()
提高的基线位置getHeight()
提高的基线位置注意 getAscent() 行和字符顶部之间的空格。我看过大部分字体和字号,总有这种差距。 (而字体下降看起来恰到好处。)是什么原因?
package com.example.fonts;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class FontMetricsExample extends JFrame
{
static final int marg = 10;
public FontMetricsExample()
{
super(FontMetricsExample.class.getSimpleName());
JPanel panel = new JPanel(new BorderLayout());
JPanel fontPanel = new JPanel(new BorderLayout());
final JTextPane textSource = new JTextPane();
textSource.setText("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
+"abcdefghijklmnopqrstuvwxyz\n"
+"0123456789!@#$%^&*()[]{}");
final SpinnerNumberModel fontSizeModel =
new SpinnerNumberModel(18, 4, 32, 1);
final String fonts[] =
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
final JComboBox fontFamilyBox = new JComboBox(fonts);
fontFamilyBox.setSelectedItem("Arial");
final JPanel text = new JPanel() {
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
String fontFamilyName =
fonts[fontFamilyBox.getSelectedIndex()];
int fontSize = fontSizeModel.getNumber().intValue();
Font f = new Font(fontFamilyName, 0, fontSize);
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
int lineHeight = fm.getHeight();
String[] s0 = textSource.getText().split("\n");
int x0 = marg;
int y0 = getHeight()-marg-(marg+lineHeight)*s0.length;
for (int i = 0; i < s0.length; ++i)
{
y0 += marg+lineHeight;
String s = s0[i];
g.drawString(s, x0, y0);
int w = fm.stringWidth(s);
for (int yofs : Arrays.asList(
0, // baseline
-fm.getHeight(),
-fm.getAscent(),
fm.getDescent()))
{
g.drawLine(x0,y0+yofs,x0+w,y0+yofs);
}
}
}
};
final JSpinner fontSizeSpinner = new JSpinner(fontSizeModel);
fontSizeSpinner.getModel().addChangeListener(
new ChangeListener() {
@Override public void stateChanged(ChangeEvent e) {
text.repaint();
}
});
text.setMinimumSize(new Dimension(200,100));
text.setPreferredSize(new Dimension(400,150));
ActionListener repainter = new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
text.repaint();
}
};
textSource.getDocument().addDocumentListener(new DocumentListener() {
@Override public void changedUpdate(DocumentEvent e) {
text.repaint();
}
@Override public void insertUpdate(DocumentEvent e) {}
@Override public void removeUpdate(DocumentEvent e) {}
});
fontFamilyBox.addActionListener(repainter);
fontPanel.add(fontFamilyBox, BorderLayout.CENTER);
fontPanel.add(fontSizeSpinner, BorderLayout.EAST);
fontPanel.add(textSource, BorderLayout.SOUTH);
panel.add(fontPanel, BorderLayout.NORTH);
panel.add(text, BorderLayout.CENTER);
setContentPane(panel);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new FontMetricsExample().setVisible(true);
}
}
最佳答案
一个可能的原因是这个值考虑了带有 diacritics 的字母.
例如,添加元音符号 ÄÖÜ 表明他们的 trema 更接近上升(尽管他们仍然没有完全达到)。
在寻找上升的更一般定义时,我找到了 the definition in Wikipedia :
[..] the ascent spans the distance between the baseline and the top of the glyph that reaches farthest from the baseline. The ascent and descent may or may not include distance added by accents or diacritical marks.
因此,即使在排版中,似乎也没有精确、绝对的定义。
关于Java:FontMetrics 上升不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6202225/
我需要找到在屏幕上绘制的文本的宽度。 This thread建议 FontMetrics.stringWidth() 有时不如 FontMetrics.getStringBounds() 准确。 有谁
我正在制作一个程序,我需要在其中设置 FontMetrics 对象中的字体。我之前已经创建了 fontmetrics,但现在我不能再访问 Graphics 对象了。然而,我想更改 FontMetric
由于我从事 LWJGL 项目,因此遇到了显示文本的问题。理论上,我想迭代(扩展)ascii 表并将所有字符的每个纹理保存在 ArrayList 中。 但我的问题是,在创建字符图像时它不会获得正确的尺寸
我需要在屏幕上绘制一些内容,但这需要字体规范才能正确绘制。我还需要使用 repaint() 或其他东西来立即绘制到屏幕上。 如果我有 paintComponent(Graphics) 方法,我可以通过
我想获得面板上字符串的准确高度(以像素为单位)。所以我写了一个程序来绘制字符串,然后在它周围绘制一个矩形。 使用 FontMetrics 我使用 getStringBounds 方法获取封闭矩形。 但
当我查看 FontMetric.getAscent() 的 javadoc 时,我看到: The font ascent is the distance from the font's baselin
如何在不使用 Graphics 的情况下获取 FontMetrics ?我想在构造函数中获取 FontMetrics,现在我这样做: BufferedImage bi = new BufferedIm
我正在为 Java 簿记程序创建的 GUI 的一部分需要显示不同的字符串。在显示此 String 之前,它必须在适当的地方添加换行符。为此,我创建了一个扩展 JTextArea 的类,并重写了 set
运行以下代码: import java.awt.Font; import java.awt.FontMetrics; public class MetricsTest { public sta
有没有比更友好的方法来获取 FontMetrics 的实例 FontMetrics fm = Graphics.getFontMetrics(Font); 我讨厌这种方式,因为下面的例子: 如果你想在
我当前正在使用指定字体将字符串绘制到 Canvas 上。但是,我想根据窗口大小缩放此字体。 给定一个目标字符串,如何找到特定字体的点大小,以便打印目标字符串的高度为 h 单位或 w 单位宽的?点大小和
通常,我会获得如下所示的图形实例: BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
我正在使用 Qt 5.2,我目前正在尝试从 QTableView 打印一个表格,但是我在根据内容计算行高时遇到了这个问题。我现在得到的是下面的循环,它循环遍历 QTableView 行并使用 boun
我有一个问题。如果我在那里使用东方语言,我的应用程序界面工作起来会慢得多。特别是在 JList、JCombobox、JTable 等组件中,我感受到了它。 如果文本中至少有一个字母是阿拉伯语或波斯语,
我使用 stringWidth("str") 计算字符串的宽度FontMetrics上的方法目的。这个方法只给了我上从起点到终点的宽度。基线 .不是字符串的总宽度。 关于如何计算整体宽度的任何想法?
如下图所示,在JPanel(500X500)上绘制了一个AttributedString。 该 AttributedString 的 FontMetrics.getStringBounds() 给出的
我有一个 Android 应用程序,可以根据 Android 设备的分辨率动态缩放文本。我已经在 Android 模拟器中的所有预定义分辨率上测试了这段代码,我的代码工作正常。 (这包括与 HTC D
已知 给定FontMetrics Font 中的 FontMetrics 有一个 protected 构造函数。 问题: 给定一个Font 对象,有没有一种方法可以构造一个FontMetrics 对象
这似乎是一个基本问题,但我在 SO 上找不到类似的问题。阅读时documentation ,我在理解概念时遇到了麻烦。我想了解 top 之间的区别是什么和 ascent还有 bottom和 desce
我是一名优秀的程序员,十分优秀!