00001 #include "bitfont.h"
00002
00003 Bitfont::Bitfont(QString fontname, uchar firstChar, uchar lastChar)
00004 {
00005 if (!fontname.isEmpty())
00006 font.load(fontname);
00007 if (font.width() == font.height()) {
00008 fontWidth = fontHeight = font.width() / 16;
00009 fontFirstChar = 1;
00010 fontLastChar = 255;
00011 } else {
00012 fontWidth = font.width()/(lastChar-firstChar+1);
00013 fontHeight = font.height();
00014 fontFirstChar = firstChar;
00015 fontLastChar = lastChar;
00016 }
00017 }
00018
00019 QRect Bitfont::rect(QString str)
00020 {
00021 return QRect(0, 0, str.length()*fontWidth, fontHeight);
00022 }
00023
00024 QPixmap Bitfont::text(QString str, QColor fg, QColor bg)
00025 {
00026 QPixmap FG(str.length()*fontWidth, fontHeight);
00027 QBitmap MASK(str.length()*fontWidth, fontHeight, TRUE);
00028
00029 const uchar *s = (const uchar *) str.data();
00030 for (uint i = 0; i < str.length(); i++) {
00031 if (font.width() == font.height())
00032 bitBlt(&MASK, i*fontWidth, 0, &font,
00033 (*s%16)*fontWidth, (*s/16)*fontWidth, fontWidth, fontHeight);
00034 else
00035 if (*s >= fontFirstChar && *s <= fontLastChar)
00036 bitBlt(&MASK, i*fontWidth, 0, &font,
00037 (*s-fontFirstChar)*fontWidth, 0, fontWidth, fontHeight);
00038 s++;
00039 }
00040
00041 FG.fill(fg);
00042 FG.setMask(MASK);
00043
00044 if (bg.isValid()) {
00045 QPixmap BG(str.length()*fontWidth, fontHeight);
00046 BG.fill(bg);
00047 bitBlt(&BG, 0, 0, &FG);
00048 return BG;
00049 } else
00050 return FG;
00051 }
00052
00053 uchar Bitfont::firstChar()
00054 {
00055 return fontFirstChar;
00056 }
00057
00058 uchar Bitfont::lastChar()
00059 {
00060 return fontLastChar;
00061 }
00062
00063 int Bitfont::width()
00064 {
00065 return fontWidth;
00066 }
00067
00068 int Bitfont::height()
00069 {
00070 return fontHeight;
00071 }