gpt4 book ai didi

Java:FontMetrics 上升不正确?

转载 作者:太空狗 更新时间:2023-10-29 22:46:10 25 4
gpt4 key购买 nike

当我查看 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.

但是我写了一个快速演示程序,我看到了这个: enter image description here

每行文本的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com