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