When I look at javadoc for FontMetric.getAscent() , I see:
Font elevation is the distance from the baseline of the font to the top of most alphanumeric characters. Some characters in a font may extend above the font's ascent line.
But I wrote a small demo program, and I see the following: 
where 4 horizontal lines for each line of text:
- base position downgraded to
getDescent() - base position
- base position raised by
getAscent() - base position raised by
getHeight()
Notice the space between the getAscent () line and the top of the characters. I looked at most fonts and sizes, and there was always this gap. (While the descent of the font looks right.) What gives?
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,
java
Jason S Jun 01 2018-11-11T00: 00Z
source share