QT6 源,七章对话框与多窗体(2) QFont 篇二:给出本类的源代码带注释

(18)本源代码定义于头文件 qfont . h

#ifndef QFONT_H
#define QFONT_H

#include <QtGui/qtguiglobal.h>
#include <QtGui/qwindowdefs.h>
#include <QtCore/qstring.h>
#include <QtCore/qsharedpointer.h>


QT_BEGIN_NAMESPACE


Q_GUI_EXPORT size_t qHash(const QFont &font, size_t seed = 0) noexcept;


/*****************************************************************************
  QFont stream functions
 *****************************************************************************/

#ifndef QT_NO_DATASTREAM
Q_GUI_EXPORT QDataStream & operator<< (QDataStream &, const QFont &);
Q_GUI_EXPORT QDataStream & operator>> (QDataStream &,       QFont &);
#endif

#ifndef QT_NO_DEBUG_STREAM
Q_GUI_EXPORT QDebug        operator<< (QDebug       , const QFont &);
#endif


class QFontPrivate;                                     /* don't touch */
class QVariant;

/*
The QFont class specifies a query for a font used for drawing text.

Note: All functions in this class are reentrant.

Detailed Description  :
QFont 可以被视为对系统中一个或多个字体的查询。

在创建 QFont 对象时,您会指定该字体应具有的各种属性。
Qt会使用具有指定属性的字体,或者如果不存在匹配的字体,则使用与之最接近的已安装字体。

实际使用的字体的属性可以从 QFontInfo 对象中检索。
如果窗口系统提供了精确匹配,则exactMatch方法将返回true。
使用 QFontMetricsF 可以获取测量值,例如使用 QFontMetrics::width()函数获取字符串的像素长度。

未特别设置的属性不会影响字体选择算法,默认值将被优先使用。

要加载特定的物理字体,通常由一个文件表示,请使用QRawFont。

请注意,在可以使用 QFont之前,必须存在一个 QGuiApplication实例。
您可以使用 QGuiApplication:setFont()来设置应用程序的默认字体。

如果所选字体并不包含所有雲要显示的字符,QFont将尝试在最近的等价字体中寻找这些字符。

当QPainter从某个字体中绘制一个字符时,QFont将报告其是否包含该字符;
如果不包含,则绘制一个未填充的方格。
Create QFonts like this:
    QFont serifFont("Times", 10, QFont::Bold);
    QFont  sansFont("Helvetica [Cronyx]", 12);

在构造函数中设置的属性也可以稍后设置,例如setFamily()、setPointSize()、
    setPointSizeF()setWeight()和setltalic()。
其余属性必须在构造后设置,例如setBold()、setUnderline ()、setOverline()、
    setStrike0ut()和setFixedPitch()。
QFontlnfo 对象应在设置字体属性后创建。即使您更改了字体的属性,QFontInfo 对象也不会更改。
相应的“get”函数,例如family()、pointSize()等,返回已设置的值,即使使用的值可能不同。
实际值可从 QFontInfo 对象中获得。

如果所请求的字体族不可用,您可以通过使用`setStyleHint()、
    `QFont::StyleHint()`和`QFont::StyleStrategy`来选择特定的影响字体匹配算法。
`defaultFamily()`函数会返回默认族(对应当前样式提示).

您可以使用`insertSubstitution()、和`insertSubstitutions()、来提供字体家族名称的替代方案。
通过removeSubstitutions()可以移除替代方案。
使用`substitute()、可获取一个家族的首个替代方案,或者在没有替代方案的情况下获取家族名称本身。
使用`substitutes()、可获取一个家族替代方案的列表(该列表可能为空)。
在替换字体后,必须通过销毁并重新创建所有`QFont`对象来触发字体的更新。

每个 OFont都有一个键key()(你可以将其用作缓存或字典中的键。
如果你想存储用户的字体偏好设置,可以使用QSettings,
通过toString()方法写入字体信息,再通过fromString()方法读取。
此外,还有 operator<<()和 operator>>()函数可用,但它们处理的是数据流。

通过调用`setPixelSize()、方法,可以将屏幕上显示的字符高度设置为指定的像素数;
不过,使用setPointSize()、方法也能达到类似效果,且具有设备独立性。

加载字体可能成本高昂,尤其是在X11上。
QFont 包含广泛的优化,以加快 QFont对象的复制过程,并缓存它所依赖的缓慢窗口系统函数的结果。

字体匹配算法的工作原理如下:
1.搜索指定的字体系列(由setFamilies()设置),
2.如果未找到,则如果指定的字体系列存在并且可以用来表示正在使用的书写系统,将被选中。
3.如果找不到,则会选用一种支持该书写系统的替代字体。
    字体匹配算法将尝试为 QFont 中设置的所有属性找到最佳匹配。具体实现方式因平台而异。
4.如果系统上不存在可以支持文本的字体,则会在其位置显示特殊的“缺失字符”框。

注:如果所选字体虽然在总体上支持该书写系统,但却缺少某个或某些特定字符的字形,
那么Qt将尝试为这些特定字符寻找一个备用字体。该功能可以通过使用QFont::NoFontMerging样式策略来禁用。

在Windows中,对“Courier”字体的请求会自动被转换为“Courier New”.
这是Courier的改进版本,支持平滑缩放.
通过设置“PreferBitmap”风格策略(参见setStyleStrategy()),可以选择较老的“Courier”位图字体。

Once a font is found, the remaining attributes are matched in order of priority:
    fixedPitch()
    pointSize() (see below)
    weight()
    style()

如果有一个字体在家族属性上匹配,即使其他属性都不匹配,
    那么这个字体将被优先选择,而不会选择那些在家族属性上不匹配但在其他属性上匹配的字体。
这是因为字体家族是主要的搜索标准。

点大小被定义为确保在请求点大小的土20%范围内相匹配。
当多个字体匹配且仅通过点大小区分时,将选择与请求点大小最接近的字体。

实际用于绘制文本的字体家族、字体大小、字重以及其他字体属性,将取决于所选字体家族在窗口系统中可用的属性。
可以使用 QFontInfo 对象来确定实际用于文本绘制的值。

要确定窗口系统中实际使用的字体属性,请使用QFontInfo对象,例如:
    QFontInfo info(f1);
    QString family = info.family();

要查找字体指标font metrics ,请使用 QFontMetrics 对象,例如:
    QFontMetrics fm(f1);
    int textWidthInPixels = fm.horizontalAdvance("How many pixels wide is this text?");
    int textHeightInPixels = fm.height();

有关字体的更多一般信息,请参阅comp.fonts常见问题解答。有关编码的信息可以在UTR17页面上找到。

*/

class Q_GUI_EXPORT QFont
{
    Q_GADGET

private:
    explicit QFont(QFontPrivate *);

    void detach();


    friend class QFontPrivate;
    friend class QFontDialogPrivate;
    friend class QFontMetrics;
    friend class QFontMetricsF;
    friend class QFontInfo;
    friend class QPainter;
    friend class QPainterPrivate;
    friend class QApplication;
    friend class QWidget;
    friend class QWidgetPrivate;
    friend class QTextLayout;
    friend class QTextEngine;
    friend class QStackTextEngine;
    friend class QTextLine;
    friend struct QScriptLine;
    friend class QOpenGLContext;
    friend class QWin32PaintEngine;
    friend class QAlphaPaintEngine;
    friend class QPainterPath;
    friend class QTextItemInt;
    friend class QPicturePaintEngine;
    friend class QPainterReplayer;
    friend class QPaintBufferEngine;
    friend class QCommandLinkButtonPrivate;
    friend class QFontEngine;

    friend Q_GUI_EXPORT QDataStream & operator<<(QDataStream &, const QFont &);
    friend Q_GUI_EXPORT QDataStream & operator>>(QDataStream &,       QFont &);
    friend Q_GUI_EXPORT QDebug        operator<<(QDebug       , const QFont &);

    QExplicitlySharedDataPointer<QFontPrivate> d           ; //本类的数据成员
    uint                                       resolve_mask;

public:

    //这个枚举类在本类定义,但本类的公共成员函数并没有使用该类。
    //遵循CSS命名约定的预定义拉伸值。数值越高,文本被拉伸得越多。
    enum Stretch {
        AnyStretch     =   0, //接受任何使用其他字体属性匹配的拉伸(在Qt5.8中添加)
        UltraCondensed =  50, //超浓缩
        ExtraCondensed =  62,
        Condensed      =  75,
        SemiCondensed  =  87,
        Unstretched    = 100,
        SemiExpanded   = 112,
        Expanded       = 125,
        ExtraExpanded  = 150,
        UltraExpanded  = 200
    };
    Q_ENUM(Stretch)

    //这个枚举类在本类定义,但本类的公共成员函数并没有使用该类。
    enum ResolveProperties {   //无官方注释,太好了
             NoPropertiesResolved   = 0x00000,
                   FamilyResolved   = 0x00001,
                     SizeResolved   = 0x00002,
                StyleHintResolved   = 0x00004,
            StyleStrategyResolved   = 0x00008,
                   WeightResolved   = 0x00010,
                    StyleResolved   = 0x00020,
                UnderlineResolved   = 0x00040,
                 OverlineResolved   = 0x00080,
                StrikeOutResolved   = 0x00100,
               FixedPitchResolved   = 0x00200,
                  StretchResolved   = 0x00400,
                  KerningResolved   = 0x00800,
           CapitalizationResolved   = 0x01000,
            LetterSpacingResolved   = 0x02000,
              WordSpacingResolved   = 0x04000,
        HintingPreferenceResolved   = 0x08000,
                StyleNameResolved   = 0x10000,
                 FamiliesResolved   = 0x20000,
            AllPropertiesResolved   = 0x3ffff
    };
    Q_ENUM(ResolveProperties)

    QFont(); //默认构造函数

    QFont(const QString     & family  , //有参构造函数
                int pointSize = -1, int weight = -1, bool italic = false);

    explicit
    QFont(const QStringList & families, //有参构造函数
                int pointSize = -1, int weight = -1, bool italic = false);

    QFont(const QFont & font);                          // copy构造函数
    QFont(const QFont & font, const QPaintDevice * pd); // copy构造函数

    ~QFont();  //析构函数

    QFont & operator= (const QFont &)      ; // copy赋值运算符函数
    bool    operator==(const QFont &) const; //相等比较运输符函数
    bool    operator!=(const QFont &) const;
    bool    operator< (const QFont &) const;
            operator QVariant() const;       //类型转换运算符函数,本类可默认转换为 QVariant类型

    bool    isCopyOf  (const QFont &) const;

    QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QFont) //以宏形式实现移动构造函数

    void swap(QFont & other)
    {
        qSwap(d, other.d);
        qSwap(resolve_mask, other.resolve_mask);
    }

    // dupicated重复的 from QFontInfo
    //如果存在一个完全符合此字体设置的窗口系统字体,则返回true。
    bool        exactMatch() const;

    //Returns the font's key, a textual representation of a font.
    //It is typically used as the key for a cache or dictionary字典 of fonts.
    //返回字体的键,即字体的文本表示。它通常用作缓存或字典中字体的键。
    QString     key() const;

    static void initialize     ();  //这三个静态成员函数,都没有官方注释。
    static void cleanup        ();
    static void cacheStatistics();

    //返回在指定familyName时使用的第一个姓氏 familyName。查找不区分大小写。
    //如果没有替代的姓氏,则返回姓氏。要获取替换列表,请使用substitutes()。
    static QString            substitute   (const QString     &     familyName);
    //返回一个列表,其中包含在指定familyName时使用的姓氏。查找不区分大小写。
    //如果没有替代的familyName,将返回一个空列表。
    static QStringList        substitutes  (const QString     &     familyName);
    static QStringList        substitutions();
    //Returns a sorted list of substituted family names.
    static void         insertSubstitution (const QString     &     familyName,
                                            const QString     & substituteName);
    //Inserts substituteName into the substitution table for the family familyName.
    //在替换字体后,通过销毁并重新创建所有 QFont 对象来触发字体的更新。
    static void         insertSubstitutions(const QString     &     familyName,
                                            const QStringList & substituteNames);
    //Inserts the list of families substituteNames into the
    //  substitution list for familyName.
    static void         removeSubstitutions(const QString     &     familyName);
    //Removes all the substitutions for familyName.

    /*
    //返回字体的描述。该描述是一个用逗号分隔的属性列表,非常适合在QSettings中使用,并包含以下内容:
    Font family、Point size、Pixel size、Style hint、Font weight、Font style、Underline、
    Strike out、Fixed pitch、Always 0、Capitalization、Letter spacing、Word spacing、
    Stretch、Style strategy、Font style (omitted when unavailable)
    */
    QString          toString() const;
    bool           fromString(const QString & descrip);
    //Sets this font to match the description descrip.
    //将此字体设置为匹配描述。描述是通过toString()方法返回的、以逗号分隔的字体属性列表。

    //Returns a new QFont that has attributes copied from
    //  other that have not been previously set on this font.
    //返回一个新的QFont,该字体具有从其他已设置的属性复制过来的属性。
    QFont              resolve    (const QFont & other) const;
    inline uint        resolveMask() const    { return resolve_mask; } //无注释
    inline void     setResolveMask(uint mask) { resolve_mask = mask; }


    QString            family() const;
    QString     defaultFamily() const; //返回与当前样式提示对应的姓氏。
    void            setFamily(const QString &);

    QStringList        families() const;
    void            setFamilies(const QStringList &);


    //这个枚举描述了用于显示文本的不同字体样式。
    enum Style {
        StyleNormal,
        StyleItalic, //专门为表示斜体文本而设计的斜体字形。
        StyleOblique //具有斜体外观的字符,通常基于未加修饰的字符,
        //但未经过专门微调以用于表示斜体文本。
    };
    Q_ENUM(Style)

    Style              style() const;   //style 里最有用的就是斜体。用下面的 italic()更好
    void            setStyle(Style style);

    QString            styleName() const;
    void            setStyleName(const QString &);

    inline bool        italic() const {  return (  style() != StyleNormal  )    ; }
    inline void     setItalic(bool b) {  setStyle(b ? StyleItalic : StyleNormal); }

    //样式提示用于字体匹配算法,当选定的字体系列不可用时,帮助找到合适的默认系列。
    enum StyleHint {
        Helvetica,  SansSerif  = Helvetica , //这俩值相等
        Times,      Serif      = Times     , //字体匹配器更喜欢衬线字体。
        Courier,    TypeWriter = Courier   ,
        OldEnglish, Decorative = OldEnglish, //字体匹配器更喜欢装饰性字体。
        System   , //the font matcher prefers system fonts.
        AnyStyle , //让字体匹配算法来选择字体系列。这是默认设置。
        Cursive  ,
        Monospace, //字体匹配器更喜欢映射到CSS通用字体系列'monospace'的字体。
        Fantasy    //字体匹配器更喜欢映射到CSS通用字体系列'fantasy'  的字体。
    };
    Q_ENUM(StyleHint)

    StyleHint          styleHint() const;
    void            setStyleHint(StyleHint, StyleStrategy = PreferDefault);

    StyleStrategy      styleStrategy() const;
    void            setStyleStrategy(StyleStrategy s);

    //样式策略告诉字体匹配算法应该使用哪种类型的字体来找到合适的默认系列。
    enum StyleStrategy {
        PreferDefault       = 0x0001, //默认样式策略。它不偏好任何类型的字体。
        PreferBitmap        = 0x0002, //偏好位图字体(而不是轮廓字体)
        PreferDevice        = 0x0004,
        PreferOutline       = 0x0008, //偏好轮廓字体(而不是位图字体)
        ForceOutline        = 0x0010,
        PreferMatch         = 0x0020,
        PreferQuality       = 0x0040,
        PreferAntialias     = 0x0080,
        NoAntialias         = 0x0100,
        NoSubpixelAntialias = 0x0800,
        PreferNoShaping     = 0x1000,
        NoFontMerging       = 0x8000  //如果为某种书写系统选定的字体不包含请求绘制的字符,
        //那么Qt将自动选择包含该字符的类似样式的字体。`NoFontMerging~标志会禁用此功能。
        //请注意,启用此标志并不会阻止Qt在选定的字体不支持文本的书写系统时自动选择合适的字体。
    };
    Q_ENUM(StyleStrategy)

    int                pointSize () const; // 返回 UI 界面里的数值 1点的大小 = 1/72 英寸
    void            setPointSize (int);
    qreal              pointSizeF() const;
    void            setPointSizeF(qreal);

    int                pixelSize() const; //默认值为 -1.
    void            setPixelSize(int);

    Weight             weight() const;
    void            setWeight(Weight weight);
    //Qt 使用一个从1到 1000 的加权比例,与 OpenType 兼容。权重为1将是细体,而 1000 将是非常黑的。
    enum Weight {
        Thin       = 100,
        ExtraLight = 200,
        Light      = 300,
        Normal     = 400,
        Medium     = 500,
        DemiBold   = 600,
        Bold       = 700,
        ExtraBold  = 800,
        Black      = 900
    };
    Q_ENUM(Weight)

    inline bool        bold() const { return    weight() > Medium      ; }
    inline void     setBold(bool)   { setWeight(enable ? Bold : Normal); }


    bool               underline() const; //下划线
    void            setUnderline(bool);

    bool               overline () const; //上划线,多用于数学公式。
    void            setOverline (bool);

    bool               strikeOut() const; //击出 strikeOut ,就是指删除线
    void            setStrikeOut(bool);

    bool               fixedPitch() const; //如果已设置固定螺距,则返回true;否则返回false。
    void            setFixedPitch(bool);

    bool               kerning   () const; //如果在使用此字体绘制文本时应使用字距,则返回true。
    void            setKerning   (bool)  ; //字距调整 kerning

    int                stretch   () const; //Returns the stretch factor for the font.
    void            setStretch   (int)   ; //伸缩因子


    enum SpacingType {
        PercentageSpacing, //值为100将保持间距不变;值为200将在字符后根据字符本身的宽度增加间距。
        AbsoluteSpacing  //正数值会将字母间距增加相应的像素;负数值会减少间距。
    };
    Q_ENUM(SpacingType)
    qreal              letterSpacing() const; //Returns the letter spacing for the font.
    SpacingType        letterSpacingType() const; //字符间距
    void            setLetterSpacing(SpacingType type, qreal spacing);

    qreal              wordSpacing  () const; //Returns the word   spacing for the font.
    void            setWordSpacing(qreal spacing);//词间距


    //此字体的文本渲染选项。
    enum Capitalization {
        MixedCase, //这是正常文本渲染选项,不进行大写更改。
        AllUppercase,
        AllLowercase, //This alters the text to be rendered in all lowercase type.
        SmallCaps, //This alters the text to be rendered in small-caps type.
        Capitalize    //这将使每个单词的第一个字符大写。
    };
    Q_ENUM(Capitalization)

    Capitalization     capitalization() const;
    void            setCapitalization(Capitalization);

    HintingPreference  hintingPreference() const;
    void            setHintingPreference(HintingPreference hintingPreference);
    //这个枚举描述了可以应用于字形的不同级别提示,以在像素密度可能允许的显示设备上提高可读性。
    enum HintingPreference {
        PreferDefaultHinting        = 0, //使用目标平台的默认提示级别。
        PreferNoHinting             = 1, //如果可能的话,应在不暗示字形轮廓的情况下呈现文本。
        //文本布局将在排版上准确且可缩放,使用与印刷时相同的度量标准。
        PreferVerticalHinting       = 2, //如果可能的话,应使文本在无水平提示的情况下呈现,
        //但使字形在垂直方向上对齐到像素网格。
        //在密度过低以至于无法准确呈现字形的显示器上,这种文本将显得更为清晰。
        //但由于字形的水平度量未加提示,文本的布局可以在更高密度的设备上(如打印机)进行缩放,
        //而不会影响行分隔等细节。
        PreferFullHinting           = 3 //如果可能的话,应使文本在水平和垂直方向上都具备衬线效果。
        //文本将经过调整以在目标设备上达到最佳的易读性,
        //但由于相关度量标准将取决于文本的目标字号因此字形、换行符位置以及其他排版细节并不会随之缩放,
        //这意味着在不同像素密度的设备上,文本布局可能看起来会有所不同。
    };
    Q_ENUM(HintingPreference)
    //请注意,此枚举仅描述了偏好设置,因为并非所有Qt支持的平台上都支持所有提示级别。
    //下表详细说明了特定提示偏好设置对所选目标平台集的影响。
    //注:请注意,在Windows上更改提示偏好设置可以通过DirectWrite字体引擎实现。
    //这在安装了平台更新后的Windows Vista上可用,在Windows7上也可用。
    //为了使用此扩展,请使用“-directwrite”选项配置Qt。
    //随后,目标应用程序的实现将取决于目标系统上DirectWrite的可用性。


}; //完结 class QFont

Q_DECLARE_SHARED(QFont)



QT_END_NAMESPACE

#endif // QFONT_H

(19)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值