Git Product home page Git Product logo

Comments (1)

pezy avatar pezy commented on June 12, 2024

Qt 里正好有一个类是做这件事的: QFontMetrics .
它可以计算所给字体的字符甚至字符串的 size .创建一个QFontMetrics 对象有三种方式:

  1. 构造函数, QFontMetrics(const QFont &font) ,但要注意的是, 这个字体不能是 painter font (可能会不准确). 如果该字体发生变化, 对象不会自动更新.
  2. QWidget::fontMetrics() == QFontMetrics(widget->font()). 同样的, 如果 widget 的字体变化, 不会随之更新.
  3. QPainter::fontMetrics() 返回 painter 当前的字体, 还是, 如果 painter 的字体变化, 不会随之更新.

拿到对象,就可以开搞了,看看有些什么工具:

  • ascent() : 基线到顶部的距离,实际运用时可能会非常小(非英文字体,个性字体)。
  • descent() : 基线到底部的距离,和上面一样,可能非常小。
  • height() : 字体高度, = ascent() + descent() + 1,1是baseline的高度。
  • leading() : 行距。(This is the natural inter-line spacing.)
  • lineSpacing() : 相邻 baseline 的距离。

再看些高级的:

  • underlinePos() : 基线到下划线(画出来的话)的距离。
  • overlinePos() : 基线到上划线(画出来的话)的距离。
  • strikeOutPos() : 基线到删除线(画出来的话)的距离。
  • lineWidth() : 下划线和删除线的宽度。

These functions are all fast.

  • minLeftBearing() : 最小左间距
  • minRightBearing() : 最小右间距
  • maxWidth() : 最大字宽

_These are by necessity slow, and we recommend avoiding them if possible._

说到这里,可能不太好理解这些位置的含义了,请见下图:
location

具体用法:

对于每一个 character, 可以用 width(), leftBearing(), rightBearing(), inFont()(用来确认该字是否在字体库内), 也可以把 character 当成 string.

对于每一个 string, 可以用 width(), boundingRect(), size(). 一个返回 QRect, 一个返回 QSize.

QFont font("times", 24);
QFontMetrics fm(font);
int pixelsWide = fm.width("What's the width of this text?");
int pixelsHigh = fm.height();

参考资料:


这个话题可以更加深入研究:

from qtlab.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.