]> git.lyx.org Git - lyx.git/blob - src/frontends/FontMetrics.h
9a190f439399746c90a13c22a778a793e9df8c7a
[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/docstring.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  */
42
43 namespace lyx {
44 namespace frontend {
45
46 class FontMetrics
47 {
48 public:
49         virtual ~FontMetrics() {}
50
51         /// return the maximum ascent of the font
52         virtual int maxAscent() const = 0;
53         /// return the maximum descent of the font
54         virtual int maxDescent() const = 0;
55         /// return the ascent of the char in the font
56         virtual int ascent(lyx::char_type c) const = 0;
57         /// return the descent of the char in the font
58         virtual int descent(lyx::char_type c) const = 0;
59         /// return the left bearing of the char in the font
60         virtual int lbearing(lyx::char_type c) const = 0;
61         /// return the right bearing of the char in the font
62         virtual int rbearing(lyx::char_type c) const = 0;
63         /// return the width of the string in the font
64         virtual int width(lyx::char_type const * s, size_t n) const = 0;
65         /// FIXME ??
66         virtual int signedWidth(lyx::docstring const & s) const = 0;
67         /**
68          * fill in width,ascent,descent with the values for the
69          * given string in the font.
70          */
71         virtual void rectText(lyx::docstring const & str,
72                 int & width,
73                 int & ascent,
74                 int & descent) const = 0;
75         /**
76          * fill in width,ascent,descent with the values for the
77          * given string in the font for a button.
78          */
79         virtual void buttonText(lyx::docstring const & str,
80                 int & width,
81                 int & ascent,
82                 int & descent) const = 0;
83
84         /// return the maximum descent of the font
85         inline int maxHeight() const {
86                 return maxAscent() + maxDescent();
87         }
88
89         /// return the descent of the char in the font
90         inline int height(lyx::char_type c) const
91         {
92                 return ascent(c) + descent(c);
93         }
94
95         /// return the inner width of the char in the font
96         inline int center(lyx::char_type c) const {
97                 return (rbearing(c) - lbearing(c)) / 2;
98         }
99
100         /// return the width of the char in the font
101         inline int width(lyx::char_type c) const
102         {
103                 lyx::char_type tmp[2] = { c, L'\0'};
104                 return width(tmp, 1);
105         }
106
107         /// return the width of the string in the font
108         inline int width(lyx::docstring const & s) const
109         {
110             return s.empty() ? 0 : width(s.data(), s.length());
111         }
112 };
113
114 } // namespace frontend
115 } // namespace lyx
116
117 #endif // FONT_METRICS_H