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>
32 bool operator==(BreakStringKey const & key) const {
33 return key.s == s && key.first_wid == first_wid && key.wid == wid
34 && key.rtl == rtl && key.force == force;
46 bool operator==(TextLayoutKey const & key) const {
47 return key.s == s && key.rtl == rtl && key.ws == ws;
56 class GuiFontMetrics : public FontMetrics
59 GuiFontMetrics(QFont const & font);
61 virtual ~GuiFontMetrics() {}
63 int maxAscent() const override;
64 int maxDescent() const override;
65 Dimension const defaultDimension() const override;
66 int em() const override;
67 int xHeight() const override;
68 int lineWidth() const override;
69 int underlinePos() const override;
70 int strikeoutPos() const override;
71 bool italic() const override;
72 double italicSlope() const override;
73 int width(char_type c) const override;
74 int ascent(char_type c) const override;
75 int descent(char_type c) const override;
76 int lbearing(char_type c) const override;
77 int rbearing(char_type c) const override;
78 int width(docstring const & s) const override;
79 int signedWidth(docstring const & s) const override;
80 int pos2x(docstring const & s, int pos, bool rtl, double ws) const override;
81 int x2pos(docstring const & s, int & x, bool rtl, double ws) const override;
82 Breaks breakString(docstring const & s, int first_wid, int wid, bool rtl, bool force) const override;
83 Dimension const dimension(char_type c) const override;
85 void rectText(docstring const & str,
88 int & descent) const override;
89 void buttonText(docstring const & str,
93 int & descent) const override;
96 int width(QString const & str) const;
98 /// Return a pointer to a cached QTextLayout object
99 std::shared_ptr<QTextLayout const>
100 getTextLayout(docstring const & s, bool const rtl,
101 double const wordspacing) const;
105 Breaks breakString_helper(docstring const & s, int first_wid, int wid,
106 bool rtl, bool force) const;
111 /// Metrics on the font
112 QFontMetrics metrics_;
114 /// Slope of italic font
117 /// Cache of char widths
118 mutable QHash<char_type, int> width_cache_;
119 /// Cache of string widths
120 mutable Cache<docstring, int> strwidth_cache_;
121 /// Cache for breakString
122 mutable Cache<BreakStringKey, Breaks> breakstr_cache_;
123 /// Cache for QTextLayout
124 mutable Cache<TextLayoutKey, std::shared_ptr<QTextLayout>> qtextlayout_cache_;
126 struct AscendDescend {
130 /// Cache of char ascends and descends
131 mutable QHash<char_type, AscendDescend> metrics_cache_;
132 /// fill in \c metrics_cache_ at specified value.
133 AscendDescend const fillMetricsCache(char_type) const;
135 /// Cache of char left bearings
136 mutable QHash<char_type, int> lbearing_cache_;
137 /// Cache of char right bearings
138 mutable QHash<char_type, int> rbearing_cache_;
142 } // namespace frontend
145 #endif // GUI_FONT_METRICS_H