]> git.lyx.org Git - lyx.git/blob - src/frontends/FontMetrics.h
Avoid dangling-reference warning in several places
[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 #include "support/strfwd.h"
18
19 #include <vector>
20
21 /**
22  * A class holding helper functions for determining
23  * the screen dimensions of fonts.
24  *
25  * The geometry is the standard typographical geometry,
26  * as follows :
27  *
28  * --------------+------------------<maxAscent
29  *               |          |
30  *               <-------> (right bearing)
31  *               <-> (left bearing)
32  * char ascent>___          |
33  *               ^   oooo   |  oooo
34  *   origin>____ |  oo  oo  | oo  oo
35  *              \|  oo  oo  | oo  oo
36  * --------------+---ooooo--|--oooo-<baseline
37  *               |      oo  |
38  * char          |  oo  oo  |
39  * descent>______|   oooo   |
40  *               <-  width ->
41  * --------------+----------+-------<maxDescent
42  *
43  * Caution: All char_type and docstring arguments of any method of this class
44  * are no UCS4 chars or strings if the font is a symbol font. They simply
45  * denote the code points of the font instead. You have to keep this in mind
46  * when you implement the methods in a frontend. You must not pass these
47  * parameters to a unicode conversion function in particular.
48  */
49
50 namespace lyx {
51
52 class Dimension;
53
54 namespace frontend {
55
56 class FontMetrics
57 {
58 public:
59         virtual ~FontMetrics() {}
60
61         /// return the maximum ascent of the font
62         virtual int maxAscent() const = 0;
63         /// return the maximum descent of the font
64         virtual int maxDescent() const = 0;
65         /// return default dimension of the font.
66         /// \warning \c width is set to zero.
67         virtual Dimension const defaultDimension() const = 0;
68         /// return the em size
69         virtual int em() const = 0;
70         /// return the x height
71         virtual int xHeight() const = 0;
72         /// return the width of a line for underlining
73         virtual int lineWidth() const = 0;
74         /// return the distance from the base line to where an underline
75         /// should be drawn.
76         virtual int underlinePos() const = 0;
77         /// return the distance from the base line to where the strike out line
78         /// should be drawn.
79         virtual int strikeoutPos() const = 0;
80         /// return true if font is not upright (italic or oblique)
81         virtual bool italic() const = 0;
82         /// return slope for italic font
83         virtual double italicSlope() const = 0;
84
85         /// return the ascent of the char in the font
86         virtual int ascent(char_type c) const = 0;
87         /// return the descent of the char in the font
88         virtual int descent(char_type c) const = 0;
89         /// return the maximum height of the font
90         inline int maxHeight() const { return maxAscent() + maxDescent(); }
91         /// return the height of the char in the font
92         inline int height(char_type c) const { return ascent(c) + descent(c); }
93
94         /// return the left bearing of the char in the font
95         virtual int lbearing(char_type c) const = 0;
96         /// return the right bearing of the char in the font
97         virtual int rbearing(char_type c) const = 0;
98         /// return the width of the char in the font
99         virtual int width(char_type c) const = 0;
100         /// return the width of the string in the font
101         virtual int width(docstring const & s) const = 0;
102         /// FIXME ??
103         virtual int signedWidth(docstring const & s) const = 0;
104         /// return the inner width of the char in the font
105         inline int center(char_type c) const {
106                 return (rbearing(c) - lbearing(c)) / 2;
107         }
108
109         /**
110          * return the x offset of a position in the string. The
111          * direction of the string is forced, and the returned value
112          * is from the left edge of the word, not from the start of the string.
113          * \param rtl is true for right-to-left layout
114          * \param ws is the amount of extra inter-word space applied text justification.
115          */
116         virtual int pos2x(docstring const & s, int pos, bool rtl, double ws) const = 0;
117         /**
118          * return the position in the string for a given x offset. The
119          * direction of the string is forced, and the returned value
120          * is from the left edge of the word, not from the start of the string.
121          * the offset x is updated to match the closest position in the string.
122          * \param rtl is true for right-to-left layout
123          * \param ws is the amount of extra inter-word space applied text justification.
124          */
125         virtual int x2pos(docstring const & s, int & x, bool rtl, double ws) const = 0;
126
127         // The places where to break a string and the width of the resulting lines.
128         struct Break {
129                 Break(int l, int w, int nsw) : len(l), wid(w), nspc_wid(nsw) {}
130                 // Number of characters
131                 int len = 0;
132                 // text width
133                 int wid = 0;
134                 // text width when trailing spaces are removed; only makes a
135                 // difference for the last break.
136                 int nspc_wid = 0;
137         };
138         typedef std::vector<Break> Breaks;
139         /**
140          * Break a string in multiple fragments according to width limits.
141          * \return a sequence of Break elements.
142          * \param s is the string to break.
143          * \param first_wid is the available width for first line.
144          * \param wid is the available width for the next lines.
145          * \param rtl is true for right-to-left layout.
146          * \param force is false for breaking at word separator, true for
147          *   arbitrary position.
148          */
149         virtual Breaks
150         breakString(docstring const & s, int first_wid, int wid, bool rtl, bool force) const = 0;
151
152         /// return char dimension for the font.
153         virtual Dimension const dimension(char_type c) const = 0;
154         /**
155          * fill in width,ascent,descent with the values for the
156          * given string in the font.
157          */
158         virtual void rectText(docstring const & str,
159                 int & width,
160                 int & ascent,
161                 int & descent) const = 0;
162         /**
163          * fill in width,ascent,descent with the values for the
164          * given string in the font for a button with given offset.
165          */
166         virtual void buttonText(docstring const & str,
167                 const int offset,
168                 int & width,
169                 int & ascent,
170                 int & descent) const = 0;
171 };
172
173
174 } // namespace frontend
175
176 class Font;
177 class FontInfo;
178
179 /// Implementation is in Application.cpp
180
181 LYX_BEGIN_MUTE_GCC_WARNING(dangling-reference)
182 frontend::FontMetrics const & theFontMetrics(Font const & f);
183 frontend::FontMetrics const & theFontMetrics(FontInfo const & fi);
184 LYX_END_MUTE_GCC_WARNING
185
186
187 } // namespace lyx
188
189 #endif // FONT_METRICS_H