Diffstat (limited to 'noncore/apps/opie-reader/FontControl.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-reader/FontControl.h | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/noncore/apps/opie-reader/FontControl.h b/noncore/apps/opie-reader/FontControl.h new file mode 100644 index 0000000..ed6c33f --- a/dev/null +++ b/noncore/apps/opie-reader/FontControl.h | |||
@@ -0,0 +1,121 @@ | |||
1 | #ifndef __FONTCONTROL_H | ||
2 | #define __FONTCONTROL_H | ||
3 | |||
4 | #include <qfontdatabase.h> | ||
5 | #include <qfontmetrics.h> | ||
6 | #include "StyleConsts.h" | ||
7 | |||
8 | class FontControl | ||
9 | { | ||
10 | int * m_fontsizes; | ||
11 | int m_size; | ||
12 | QString m_fontname; | ||
13 | int m_maxsize; | ||
14 | public: | ||
15 | FontControl(QString n = "helvetica", int size = 10) | ||
16 | : | ||
17 | m_fontsizes(NULL) | ||
18 | { | ||
19 | ChangeFont(n, size); | ||
20 | } | ||
21 | ~FontControl() | ||
22 | { | ||
23 | if (m_fontsizes != NULL) delete [] m_fontsizes; | ||
24 | } | ||
25 | QString name() { return m_fontname; } | ||
26 | int currentsize() { return m_fontsizes[m_size]; } | ||
27 | int getsize(CStyle size) | ||
28 | { | ||
29 | return m_fontsizes[m_size+size.getFontSize()]; | ||
30 | } | ||
31 | int ascent() | ||
32 | { | ||
33 | QFont f(name(), currentsize()); | ||
34 | QFontMetrics fm(f); | ||
35 | return fm.ascent(); | ||
36 | } | ||
37 | int ascent(CStyle ch) | ||
38 | { | ||
39 | QFont f(name(), getsize(ch)); | ||
40 | QFontMetrics fm(f); | ||
41 | return fm.ascent(); | ||
42 | } | ||
43 | int descent() | ||
44 | { | ||
45 | QFont f(name(), currentsize()); | ||
46 | QFontMetrics fm(f); | ||
47 | return fm.descent(); | ||
48 | } | ||
49 | int descent(CStyle ch) | ||
50 | { | ||
51 | QFont f(name(), getsize(ch)); | ||
52 | QFontMetrics fm(f); | ||
53 | return fm.descent(); | ||
54 | } | ||
55 | int lineSpacing() | ||
56 | { | ||
57 | QFont f(name(), currentsize()); | ||
58 | QFontMetrics fm(f); | ||
59 | return fm.lineSpacing(); | ||
60 | } | ||
61 | int lineSpacing(CStyle ch) | ||
62 | { | ||
63 | QFont f(name(), getsize(ch)); | ||
64 | QFontMetrics fm(f); | ||
65 | return fm.lineSpacing(); | ||
66 | } | ||
67 | bool decreasesize() | ||
68 | { | ||
69 | if (--m_size < 0) | ||
70 | { | ||
71 | m_size = 0; | ||
72 | return false; | ||
73 | } | ||
74 | else return true; | ||
75 | } | ||
76 | bool increasesize() | ||
77 | { | ||
78 | if (++m_size >= m_maxsize) | ||
79 | { | ||
80 | m_size = m_maxsize - 1; | ||
81 | return false; | ||
82 | } | ||
83 | else return true; | ||
84 | } | ||
85 | bool ChangeFont(QString& n) | ||
86 | { | ||
87 | return ChangeFont(n, currentsize()); | ||
88 | } | ||
89 | bool ChangeFont(QString& n, int tgt) | ||
90 | { | ||
91 | QValueList<int>::Iterator it; | ||
92 | QFontDatabase fdb; | ||
93 | QValueList<int> sizes = fdb.pointSizes(n); | ||
94 | if (sizes.count() == 0) | ||
95 | { | ||
96 | return false; | ||
97 | } | ||
98 | else | ||
99 | { | ||
100 | m_fontname = n; | ||
101 | m_maxsize = sizes.count(); | ||
102 | if (m_fontsizes != NULL) delete [] m_fontsizes; | ||
103 | m_fontsizes = new int[m_maxsize]; | ||
104 | uint i = 0; | ||
105 | uint best = 0; | ||
106 | for (it = sizes.begin(); it != sizes.end(); it++) | ||
107 | { | ||
108 | m_fontsizes[i] = (*it)/10; | ||
109 | if (abs(tgt-m_fontsizes[i]) < abs(tgt-m_fontsizes[best])) | ||
110 | { | ||
111 | best = i; | ||
112 | } | ||
113 | i++; | ||
114 | } | ||
115 | m_size = best; | ||
116 | } | ||
117 | return true; | ||
118 | } | ||
119 | }; | ||
120 | |||
121 | #endif | ||