]> git.lyx.org Git - lyx.git/blob - src/frontends/FontMetrics.h
53f57a2880075b513d35aff12fd674f2319b7c6e
[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 /**
20  * A class holding helper functions for determining
21  * the screen dimensions of fonts.
22  *
23  * The geometry is the standard typographical geometry,
24  * as follows :
25  *
26  * --------------+------------------<maxAscent
27  *               |          |
28  *               <-------> (right bearing)
29  *               <-> (left bearing)
30  * char ascent>___          |
31  *               ^   oooo   |  oooo
32  *   origin>____ |  oo  oo  | oo  oo
33  *              \|  oo  oo  | oo  oo
34  * --------------+---ooooo--|--oooo-<baseline
35  *               |      oo  |
36  * char          |  oo  oo  |
37  * descent>______|   oooo   |
38  *               <-  width ->
39  * --------------+----------+-------<maxDescent
40  *
41  * Caution: All char_type and docstring arguments of any method of this class
42  * are no UCS4 chars or strings if the font is a symbol font. They simply
43  * denote the code points of the font instead. You have to keep this in mind
44  * when you implement the methods in a frontend. You must not pass these
45  * parameters to a unicode conversion function in particular.
46  */
47
48 namespace lyx {
49
50 class Dimension;
51
52 namespace frontend {
53
54 class FontMetrics
55 {
56 public:
57         virtual ~FontMetrics() {}
58
59         /// return the maximum ascent of the font
60         virtual int maxAscent() const = 0;
61         /// return the maximum descent of the font
62         virtual int maxDescent() const = 0;
63         /// return default dimension of the font.
64         /// \warning \c width is set to zero.
65         virtual Dimension const defaultDimension() const = 0;
66         /// return the em size
67         virtual int em() const = 0;
68
69         /// return the width of the char in the font
70         virtual int width(char_type c) const = 0;
71         /// return the ascent of the char in the font
72         virtual int ascent(char_type c) const = 0;
73         /// return the descent of the char in the font
74         virtual int descent(char_type c) const = 0;
75         /// return the left bearing of the char in the font
76         virtual int lbearing(char_type c) const = 0;
77         /// return the right bearing of the char in the font
78         virtual int rbearing(char_type c) const = 0;
79         /// return the width of the string in the font
80         virtual int width(docstring const & s) const = 0;
81         /// FIXME ??
82         virtual int signedWidth(docstring const & s) const = 0;
83         /**
84          * return the x offset of a position in the string. The
85          * direction of the string is forced, and the returned value
86          * is from the left edge of the word, not from the start of the string.
87          */
88         virtual int pos2x(docstring const & s, int pos, bool rtl) const = 0;
89         /**
90          * return the position in the string for a given x offset. The
91          * direction of the string is forced, and the returned value
92          * is from the left edge of the word, not from the start of the string.
93          * the offset x is updated to match the closest position in the string.
94          */
95         virtual int x2pos(docstring const & s, int & x, bool rtl) const = 0;
96         /// return char dimension for the font.
97         virtual Dimension const dimension(char_type c) const = 0;
98         /**
99          * fill in width,ascent,descent with the values for the
100          * given string in the font.
101          */
102         virtual void rectText(docstring const & str,
103                 int & width,
104                 int & ascent,
105                 int & descent) const = 0;
106         /**
107          * fill in width,ascent,descent with the values for the
108          * given string in the font for a button.
109          */
110         virtual void buttonText(docstring const & str,
111                 int & width,
112                 int & ascent,
113                 int & descent) const = 0;
114
115         /// return the maximum descent of the font
116         inline int maxHeight() const { return maxAscent() + maxDescent(); }
117
118         /// return the descent of the char in the font
119         inline int height(char_type c) const { return ascent(c) + descent(c); }
120
121         /// return the inner width of the char in the font
122         inline int center(char_type c) const {
123                 return (rbearing(c) - lbearing(c)) / 2;
124         }
125 };
126
127
128 } // namespace frontend
129
130 class Font;
131 class FontInfo;
132
133 /// Implementation is in Application.cpp
134 frontend::FontMetrics const & theFontMetrics(Font const & f);
135 frontend::FontMetrics const & theFontMetrics(FontInfo const & fi);
136
137 } // namespace lyx
138
139 #endif // FONT_METRICS_H