]> git.lyx.org Git - lyx.git/blob - src/frontends/FontMetrics.h
Amend 6c3447c8: FindAdv: sometimes a space is added on some math symbols
[lyx.git] / src / frontends / FontMetrics.h
1 // -*- C++ -*-
2 /**
3  * \file FontMetrics.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author unknown
8  * \author John Levon
9  * \author Abdelrazak Younes
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef FONT_METRICS_H
15 #define FONT_METRICS_H
16
17
18 #include "support/mute_warning.h"
19 #include "support/strfwd.h"
20
21 #include <vector>
22
23 /**
24  * A class holding helper functions for determining
25  * the screen dimensions of fonts.
26  *
27  * The geometry is the standard typographical geometry,
28  * as follows :
29  *
30  * --------------+------------------<maxAscent
31  *               |          |
32  *               <-------> (right bearing)
33  *               <-> (left bearing)
34  * char ascent>___          |
35  *               ^   oooo   |  oooo
36  *   origin>____ |  oo  oo  | oo  oo
37  *              \|  oo  oo  | oo  oo
38  * --------------+---ooooo--|--oooo-<baseline
39  *               |      oo  |
40  * char          |  oo  oo  |
41  * descent>______|   oooo   |
42  *               <-  width ->
43  * --------------+----------+-------<maxDescent
44  *
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.
50  */
51
52 namespace lyx {
53
54 class Dimension;
55
56 namespace frontend {
57
58 class FontMetrics
59 {
60 public:
61         virtual ~FontMetrics() {}
62
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
77         /// should be drawn.
78         virtual int underlinePos() const = 0;
79         /// return the distance from the base line to where the strike out line
80         /// should be drawn.
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;
86
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); }
95
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;
104         /// FIXME ??
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;
109         }
110
111         /**
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.
117          */
118         virtual int pos2x(docstring const & s, int pos, bool rtl, double ws) const = 0;
119         /**
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.
126          */
127         virtual int x2pos(docstring const & s, int & x, bool rtl, double ws) const = 0;
128
129         // The places where to break a string and the width of the resulting lines.
130         struct Break {
131                 Break(int l, int w, int nsw) : len(l), wid(w), nspc_wid(nsw) {}
132                 // Number of characters
133                 int len = 0;
134                 // text width
135                 int wid = 0;
136                 // text width when trailing spaces are removed; only makes a
137                 // difference for the last break.
138                 int nspc_wid = 0;
139         };
140         typedef std::vector<Break> Breaks;
141         /**
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.
150          */
151         virtual Breaks
152         breakString(docstring const & s, int first_wid, int wid, bool rtl, bool force) const = 0;
153
154         /// return char dimension for the font.
155         virtual Dimension const dimension(char_type c) const = 0;
156         /**
157          * fill in width,ascent,descent with the values for the
158          * given string in the font.
159          */
160         virtual void rectText(docstring const & str,
161                 int & width,
162                 int & ascent,
163                 int & descent) const = 0;
164         /**
165          * fill in width,ascent,descent with the values for the
166          * given string in the font for a button with given offset.
167          */
168         virtual void buttonText(docstring const & str,
169                 const int offset,
170                 int & width,
171                 int & ascent,
172                 int & descent) const = 0;
173 };
174
175
176 } // namespace frontend
177
178 class Font;
179 class FontInfo;
180
181 /// Implementation is in Application.cpp
182
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
187
188
189 } // namespace lyx
190
191 #endif // FONT_METRICS_H