4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * \author Abdelrazak Younes
11 * Full author contact details are available in file CREDITS.
14 #ifndef FONT_METRICS_H
15 #define FONT_METRICS_H
18 #include "support/mute_warning.h"
19 #include "support/strfwd.h"
24 * A class holding helper functions for determining
25 * the screen dimensions of fonts.
27 * The geometry is the standard typographical geometry,
30 * --------------+------------------<maxAscent
32 * <-------> (right bearing)
36 * origin>____ | oo oo | oo oo
38 * --------------+---ooooo--|--oooo-<baseline
41 * descent>______| oooo |
43 * --------------+----------+-------<maxDescent
45 * Caution: All char_type and docstring arguments of any method of this class
46 * are no UCS4 chars or strings if the font is a symbol font. They simply
47 * denote the code points of the font instead. You have to keep this in mind
48 * when you implement the methods in a frontend. You must not pass these
49 * parameters to a unicode conversion function in particular.
61 virtual ~FontMetrics() {}
63 /// return the maximum ascent of the font
64 virtual int maxAscent() const = 0;
65 /// return the maximum descent of the font
66 virtual int maxDescent() const = 0;
67 /// return default dimension of the font.
68 /// \warning \c width is set to zero.
69 virtual Dimension const defaultDimension() const = 0;
70 /// return the em size
71 virtual int em() const = 0;
72 /// return the x height
73 virtual int xHeight() const = 0;
74 /// return the width of a line for underlining
75 virtual int lineWidth() const = 0;
76 /// return the distance from the base line to where an underline
78 virtual int underlinePos() const = 0;
79 /// return the distance from the base line to where the strike out line
81 virtual int strikeoutPos() const = 0;
82 /// return true if font is not upright (italic or oblique)
83 virtual bool italic() const = 0;
84 /// return slope for italic font
85 virtual double italicSlope() const = 0;
87 /// return the ascent of the char in the font
88 virtual int ascent(char_type c) const = 0;
89 /// return the descent of the char in the font
90 virtual int descent(char_type c) const = 0;
91 /// return the maximum height of the font
92 inline int maxHeight() const { return maxAscent() + maxDescent(); }
93 /// return the height of the char in the font
94 inline int height(char_type c) const { return ascent(c) + descent(c); }
96 /// return the left bearing of the char in the font
97 virtual int lbearing(char_type c) const = 0;
98 /// return the right bearing of the char in the font
99 virtual int rbearing(char_type c) const = 0;
100 /// return the width of the char in the font
101 virtual int width(char_type c) const = 0;
102 /// return the width of the string in the font
103 virtual int width(docstring const & s) const = 0;
105 virtual int signedWidth(docstring const & s) const = 0;
106 /// return the inner width of the char in the font
107 inline int center(char_type c) const {
108 return (rbearing(c) - lbearing(c)) / 2;
112 * return the x offset of a position in the string. The
113 * direction of the string is forced, and the returned value
114 * is from the left edge of the word, not from the start of the string.
115 * \param rtl is true for right-to-left layout
116 * \param ws is the amount of extra inter-word space applied text justification.
118 virtual int pos2x(docstring const & s, int pos, bool rtl, double ws) const = 0;
120 * return the position in the string for a given x offset. The
121 * direction of the string is forced, and the returned value
122 * is from the left edge of the word, not from the start of the string.
123 * the offset x is updated to match the closest position in the string.
124 * \param rtl is true for right-to-left layout
125 * \param ws is the amount of extra inter-word space applied text justification.
127 virtual int x2pos(docstring const & s, int & x, bool rtl, double ws) const = 0;
129 // The places where to break a string and the width of the resulting lines.
131 Break(int l, int w, int nsw) : len(l), wid(w), nspc_wid(nsw) {}
132 // Number of characters
136 // text width when trailing spaces are removed; only makes a
137 // difference for the last break.
140 typedef std::vector<Break> Breaks;
142 * Break a string in multiple fragments according to width limits.
143 * \return a sequence of Break elements.
144 * \param s is the string to break.
145 * \param first_wid is the available width for first line.
146 * \param wid is the available width for the next lines.
147 * \param rtl is true for right-to-left layout.
148 * \param force is false for breaking at word separator, true for
149 * arbitrary position.
152 breakString(docstring const & s, int first_wid, int wid, bool rtl, bool force) const = 0;
154 /// return char dimension for the font.
155 virtual Dimension const dimension(char_type c) const = 0;
157 * fill in width,ascent,descent with the values for the
158 * given string in the font.
160 virtual void rectText(docstring const & str,
163 int & descent) const = 0;
165 * fill in width,ascent,descent with the values for the
166 * given string in the font for a button with given offset.
168 virtual void buttonText(docstring const & str,
172 int & descent) const = 0;
176 } // namespace frontend
181 /// Implementation is in Application.cpp
183 LYX_BEGIN_MUTE_GCC_WARNING(dangling-reference)
184 frontend::FontMetrics const & theFontMetrics(Font const & f);
185 frontend::FontMetrics const & theFontMetrics(FontInfo const & fi);
186 LYX_END_MUTE_GCC_WARNING
191 #endif // FONT_METRICS_H