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==(BreakAtKey const & key) const {
33 return key.s == s && key.x == x && key.rtl == rtl && key.force == force;
44 bool operator==(TextLayoutKey const & key) const {
45 return key.s == s && key.rtl == rtl && key.ws == ws;
54 class GuiFontMetrics : public FontMetrics
57 GuiFontMetrics(QFont const & font);
59 virtual ~GuiFontMetrics() {}
61 int maxAscent() const override;
62 int maxDescent() const override;
63 Dimension const defaultDimension() const override;
64 int em() const override;
65 int xHeight() const override;
66 int lineWidth() const override;
67 int underlinePos() const override;
68 int strikeoutPos() const override;
69 bool italic() const override;
70 double italicSlope() const override;
71 int width(char_type c) const override;
72 int ascent(char_type c) const override;
73 int descent(char_type c) const override;
74 int lbearing(char_type c) const override;
75 int rbearing(char_type c) const override;
76 int width(docstring const & s) const override;
77 int signedWidth(docstring const & s) const override;
78 int pos2x(docstring const & s, int pos, bool rtl, double ws) const override;
79 int x2pos(docstring const & s, int & x, bool rtl, double ws) const override;
80 bool breakAt(docstring & s, int & x, bool rtl, bool force) const override;
81 Dimension const dimension(char_type c) const override;
83 void rectText(docstring const & str,
86 int & descent) const override;
87 void buttonText(docstring const & str,
91 int & descent) const override;
93 int countExpanders(docstring const & str) const override;
95 int width(QString const & str) const;
97 /// Return a pointer to a cached QTextLayout object
98 std::shared_ptr<QTextLayout const>
99 getTextLayout(docstring const & s, bool const rtl,
100 double const wordspacing) const;
104 std::pair<int, int> breakAt_helper(docstring const & s, int const x,
105 bool const rtl, bool const force) const;
110 /// Metrics on the font
111 QFontMetrics metrics_;
113 /// Slope of italic font
116 /// Cache of char widths
117 mutable QHash<char_type, int> width_cache_;
118 /// Cache of string widths
119 mutable Cache<docstring, int> strwidth_cache_;
120 /// Cache for breakAt
121 mutable Cache<BreakAtKey, std::pair<int, int>> breakat_cache_;
122 /// Cache for QTextLayout
123 mutable Cache<TextLayoutKey, std::shared_ptr<QTextLayout>> qtextlayout_cache_;
125 struct AscendDescend {
129 /// Cache of char ascends and descends
130 mutable QHash<char_type, AscendDescend> metrics_cache_;
131 /// fill in \c metrics_cache_ at specified value.
132 AscendDescend const fillMetricsCache(char_type) const;
134 /// Cache of char left bearings
135 mutable QHash<char_type, int> lbearing_cache_;
136 /// Cache of char right bearings
137 mutable QHash<char_type, int> rbearing_cache_;
141 } // namespace frontend
144 #endif // GUI_FONT_METRICS_H