3 * \file GuiFontMetrics.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
12 #ifndef GUI_FONT_METRICS_H
13 #define GUI_FONT_METRICS_H
15 #include "frontends/FontMetrics.h"
17 #include "support/Cache.h"
18 #include "support/docstring.h"
21 #include <QFontMetrics>
23 #include <QTextLayout>
30 struct TextLayoutHelper;
34 bool operator==(BreakStringKey const & key) const {
35 return key.s == s && key.first_wid == first_wid && key.wid == wid
36 && key.rtl == rtl && key.force == force;
48 bool operator==(TextLayoutKey const & key) const {
49 return key.s == s && key.rtl == rtl && key.ws == ws;
58 class GuiFontMetrics : public FontMetrics
61 GuiFontMetrics(QFont const & font);
63 virtual ~GuiFontMetrics() {}
65 int maxAscent() const override;
66 int maxDescent() const override;
67 Dimension const defaultDimension() const override;
68 int em() const override;
69 int xHeight() const override;
70 int lineWidth() const override;
71 int underlinePos() const override;
72 int strikeoutPos() const override;
73 bool italic() const override;
74 double italicSlope() const override;
75 int width(char_type c) const override;
76 int ascent(char_type c) const override;
77 int descent(char_type c) const override;
78 int lbearing(char_type c) const override;
79 int rbearing(char_type c) const override;
80 int width(docstring const & s) const override;
81 int signedWidth(docstring const & s) const override;
82 int pos2x(docstring const & s, int pos, bool rtl, double ws) const override;
83 int x2pos(docstring const & s, int & x, bool rtl, double ws) const override;
84 Breaks breakString(docstring const & s, int first_wid, int wid, bool rtl, bool force) const override;
85 Dimension const dimension(char_type c) const override;
87 void rectText(docstring const & str,
90 int & descent) const override;
91 void buttonText(docstring const & str,
95 int & descent) const override;
98 int width(QString const & str) const;
100 /// Return a pointer to a cached QTextLayout object
101 std::shared_ptr<QTextLayout const>
102 getTextLayout(docstring const & s, bool const rtl, double const wordspacing) const;
106 Breaks breakString_helper(docstring const & s, int first_wid, int wid,
107 bool rtl, bool force) const;
109 /// A different version of getTextLayout for internal use
110 std::shared_ptr<QTextLayout const>
111 getTextLayout(TextLayoutHelper const & tlh, double const wordspacing) const;
116 /// Metrics on the font
117 QFontMetrics metrics_;
119 /// Slope of italic font
122 /// Cache of char widths
123 mutable QHash<char_type, int> width_cache_;
124 /// Cache of string widths
125 mutable Cache<docstring, int> strwidth_cache_;
126 /// Cache for breakString
127 mutable Cache<BreakStringKey, Breaks> breakstr_cache_;
128 /// Cache for QTextLayout
129 mutable Cache<TextLayoutKey, std::shared_ptr<QTextLayout>> qtextlayout_cache_;
131 struct AscendDescend {
135 /// Cache of char ascends and descends
136 mutable QHash<char_type, AscendDescend> metrics_cache_;
137 /// fill in \c metrics_cache_ at specified value.
138 AscendDescend const fillMetricsCache(char_type) const;
140 /// Cache of char left bearings
141 mutable QHash<char_type, int> lbearing_cache_;
142 /// Cache of char right bearings
143 mutable QHash<char_type, int> rbearing_cache_;
147 } // namespace frontend
150 #endif // GUI_FONT_METRICS_H