]> git.lyx.org Git - lyx.git/blob - src/font.C
1590c72b551baede827d1d1305e2ba64df88966c
[lyx.git] / src / font.C
1 #include <config.h>
2
3 #include <cctype>
4
5 #include "font.h"
6 #include "FontLoader.h"
7 #include "lyxrc.h"
8
9 extern LyXRC lyxrc;
10
11 // namespace {
12 static inline
13 XFontStruct * getXFontstruct(LyXFont const & f)
14 {
15         return fontloader.load(f.family(), f.series(),
16                                f.realShape(), f.size());
17 }
18
19
20 static inline
21 XID getFontID(LyXFont const & f)
22 {
23         return getXFontstruct(f)->fid;
24 }
25 // } // end of anon namespace
26
27 int lyxfont::maxAscent(LyXFont const & f)
28 {
29         return getXFontstruct(f)->ascent;
30 }
31
32
33 int lyxfont::maxDescent(LyXFont const & f)
34 {
35         return getXFontstruct(f)->descent;
36 }
37
38
39 int lyxfont::ascent(char c, LyXFont const & f)
40 {
41         XFontStruct * finfo = getXFontstruct(f);
42         unsigned int uc = static_cast<unsigned char>(c);
43         if (finfo->per_char
44             && uc >= finfo->min_char_or_byte2
45             && uc <= finfo->max_char_or_byte2) 
46                 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
47         else
48                 return finfo->ascent;
49 }
50
51
52 int lyxfont::descent(char c, LyXFont const & f)
53 {
54         XFontStruct * finfo = getXFontstruct(f);
55         unsigned int uc = static_cast<unsigned char>(c);
56         if (finfo->per_char
57             && uc >= finfo->min_char_or_byte2
58             && uc <= finfo->max_char_or_byte2) 
59                 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
60         else
61                 return finfo->descent;
62 }
63
64
65 int lyxfont::lbearing(char c, LyXFont const & f)
66 {
67         XFontStruct * finfo = getXFontstruct(f);
68         unsigned int uc = static_cast<unsigned char>(c);
69         if (finfo->per_char
70             && uc >= finfo->min_char_or_byte2
71             && uc <= finfo->max_char_or_byte2) 
72                 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
73         else
74                 return 0;
75 }
76
77
78 int lyxfont::rbearing(char c, LyXFont const & f)
79 {
80         XFontStruct * finfo = getXFontstruct(f);
81         unsigned int uc = static_cast<unsigned char>(c);
82         if (finfo->per_char
83             && uc >= finfo->min_char_or_byte2
84             && uc <= finfo->max_char_or_byte2) 
85                 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
86         else
87                 return width(c, f);
88 }
89
90
91 int lyxfont::width(char c, LyXFont const & f)
92 {
93         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
94                 return lyxrc.use_gui ? ::XTextWidth(getXFontstruct(f), &c, 1)
95                         : 1;
96         } else {
97                 LyXFont smallfont(f);
98                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
99                 if (islower(static_cast<unsigned char>(c))) {
100                         c = toupper(c);
101                         return ::XTextWidth(getXFontstruct(smallfont), &c, 1);
102                 } else {
103                         return ::XTextWidth(getXFontstruct(f), &c, 1);
104                 }
105         }
106 }
107
108
109 int lyxfont::width(char const * s, int n, LyXFont const & f)
110 {
111         if (!lyxrc.use_gui)
112                 return n;
113         
114         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
115                 return ::XTextWidth(getXFontstruct(f), s, n);
116         } else {
117                 // emulate smallcaps since X doesn't support this
118                 unsigned int result = 0;
119                 char c;
120                 LyXFont smallfont(f);
121                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
122                 for (int i = 0; i < n; ++i) {
123                         c = s[i];
124                         // when islower is a macro, the cast is needed (JMarc)
125                         if (islower(static_cast<unsigned char>(c))) {
126                                 c = toupper(c);
127                                 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
128                         } else {
129                                 result += ::XTextWidth(getXFontstruct(f), &c, 1);
130                         }
131                 }
132                 return result;
133         }
134 }
135
136
137 int lyxfont::signedWidth(string const & s, LyXFont const & f)
138 {
139         if (s.empty()) return 0;
140         if (s.c_str()[0] == '-')
141                 return -width(s.c_str() + 1, s.length() - 1, f);
142         else
143                 return width(s.c_str(), s.length(), f);
144 }
145
146
147 int lyxfont::XTextWidth(LyXFont const & f, char * str, int count)
148 {
149         return ::XTextWidth(getXFontstruct(f), str, count);
150 }
151
152
153 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f) 
154 {
155         ::XSetFont(display, gc, getFontID(f));
156 }
157
158 //} // end of namespace font
159 //} // end of namespace lyx