首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FontMetrics上升不正确吗?

FontMetrics上升不正确吗?
EN

Stack Overflow用户
提问于 2011-06-01 13:48:12
回答 3查看 8.4K关注 0票数 20

当我查看FontMetric.getAscent()的javadoc时,我看到:

字体上升是从字体的基线到大多数字母数字字符顶部的距离。字体中的某些字符可能扩展到字体上升行的上方。

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

其中,每一行文本的4条水平线如下:

  • getDescent()降低基线位置
  • 基线位置
  • getAscent()提高基线位置
  • getHeight()提高基线位置

注意getAscent()行与字符顶部之间的空格。我看过大多数字体和尺寸,而且总是有这样的差距。(而字体下降看起来正好。)给出了什么?

代码语言:javascript
复制
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);
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-01 13:53:35

一个可能的原因是这个值包含了决裂学的帐户字母。

例如,添加umlauts显示他们的步调更接近上升(尽管他们还没有达到)。

寻找一个更普遍的提升定义,我发现维基百科的定义

。。上升跨越基线与字形顶部之间的距离,字形与基线相距最远。上升和下降可能包括也可能不包括由口音或指示符号增加的距离。

因此,即使在排版中,似乎也没有精确、绝对的定义。

票数 15
EN

Stack Overflow用户

发布于 2012-12-27 13:58:49

我遇到了同样的问题,似乎使用GlyphVector类可以获得字符的真正上界。

代码语言:javascript
复制
package graphics;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.font.GlyphVector;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class FontMetricsTest2 {

    public static void main(String[] args) throws IOException {
        //Draw the text to measure it with a drawing program
        BufferedImage img = new BufferedImage(
            500, 300, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();
        Font font = new Font(Font.SERIF, Font.PLAIN, 150);
        graphics.setFont(font);
        String text = "ABCxyz";
        graphics.drawString(text, 20, 180);
        ImageIO.write(img, "PNG", new File("G:\\someDir\\fontMetrics2.png"));

        //Failed attempts to determine ascent with FontMetrics
        FontMetrics fm = graphics.getFontMetrics();
        System.out.println("FM Ascent=" + fm.getAscent() + 
            ", FM descent=" + fm.getDescent());
        //returned ascent is too high
        System.out.println("FM string bounds: " + 
            fm.getStringBounds(text, graphics));
        //too high as well

        //The succesful way with glyph vector
        GlyphVector gv = font.layoutGlyphVector(
            graphics.getFontRenderContext(), text.toCharArray(),
            0, text.length(), Font.LAYOUT_LEFT_TO_RIGHT);
        Rectangle pixBounds = gv.getPixelBounds(
            graphics.getFontRenderContext(), 0, 0);
        System.out.println("GlyphVector - pixelBounds: " + pixBounds);
        Rectangle2D visBounds = gv.getVisualBounds();
        System.out.println("GlyphVector - visualBounds: " + visBounds);
    }

}

字符串中出现的字符上升所返回的矩形中的y值表示"text“变量。

像素界和视觉界的主要区别在于,pixelBounds是整数,visualBounds是浮点数。否则,他们看起来几乎是平等的。

票数 9
EN

Stack Overflow用户

发布于 2013-01-17 20:15:45

TrueType参考手册表示字体的升级换代存储在“hhea”表中。hhea的文档声明:“上升、下降和lineGap的值代表字体创建者的设计意图,而不是任何计算值。”OpenType规范是TrueType规范的扩展。它还将提升器存储在hhea表中,并引用TrueType的上升定义。底线,上升属性是一个指南,而不是一个绝对的。GlyphLayoutVector是获得文本边界的最精确的方法。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6202225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档