Diffstat (limited to 'noncore/games/kpacman/bitfont.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/kpacman/bitfont.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/noncore/games/kpacman/bitfont.cpp b/noncore/games/kpacman/bitfont.cpp new file mode 100644 index 0000000..40581c8 --- a/dev/null +++ b/noncore/games/kpacman/bitfont.cpp | |||
@@ -0,0 +1,71 @@ | |||
1 | #include "bitfont.h" | ||
2 | |||
3 | Bitfont::Bitfont(QString fontname, uchar firstChar, uchar lastChar) | ||
4 | { | ||
5 | if (!fontname.isEmpty()) | ||
6 | font.load(fontname); | ||
7 | if (font.width() == font.height()) { | ||
8 | fontWidth = fontHeight = font.width() / 16; | ||
9 | fontFirstChar = 1; | ||
10 | fontLastChar = 255; | ||
11 | } else { | ||
12 | fontWidth = font.width()/(lastChar-firstChar+1); | ||
13 | fontHeight = font.height(); | ||
14 | fontFirstChar = firstChar; | ||
15 | fontLastChar = lastChar; | ||
16 | } | ||
17 | } | ||
18 | |||
19 | QRect Bitfont::rect(QString str) | ||
20 | { | ||
21 | return QRect(0, 0, str.length()*fontWidth, fontHeight); | ||
22 | } | ||
23 | |||
24 | QPixmap Bitfont::text(QString str, QColor fg, QColor bg) | ||
25 | { | ||
26 | QPixmap FG(str.length()*fontWidth, fontHeight); | ||
27 | QBitmap MASK(str.length()*fontWidth, fontHeight, TRUE); | ||
28 | |||
29 | const uchar *s = (const uchar *) str.data(); | ||
30 | for (uint i = 0; i < str.length(); i++) { | ||
31 | if (font.width() == font.height()) | ||
32 | bitBlt(&MASK, i*fontWidth, 0, &font, | ||
33 | (*s%16)*fontWidth, (*s/16)*fontWidth, fontWidth, fontHeight); | ||
34 | else | ||
35 | if (*s >= fontFirstChar && *s <= fontLastChar) | ||
36 | bitBlt(&MASK, i*fontWidth, 0, &font, | ||
37 | (*s-fontFirstChar)*fontWidth, 0, fontWidth, fontHeight); | ||
38 | s++; | ||
39 | } | ||
40 | |||
41 | FG.fill(fg); | ||
42 | FG.setMask(MASK); | ||
43 | |||
44 | if (bg.isValid()) { | ||
45 | QPixmap BG(str.length()*fontWidth, fontHeight); | ||
46 | BG.fill(bg); | ||
47 | bitBlt(&BG, 0, 0, &FG); | ||
48 | return BG; | ||
49 | } else | ||
50 | return FG; | ||
51 | } | ||
52 | |||
53 | uchar Bitfont::firstChar() | ||
54 | { | ||
55 | return fontFirstChar; | ||
56 | } | ||
57 | |||
58 | uchar Bitfont::lastChar() | ||
59 | { | ||
60 | return fontLastChar; | ||
61 | } | ||
62 | |||
63 | int Bitfont::width() | ||
64 | { | ||
65 | return fontWidth; | ||
66 | } | ||
67 | |||
68 | int Bitfont::height() | ||
69 | { | ||
70 | return fontHeight; | ||
71 | } | ||