Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kcontrol / kfontinst / thumbnail / FontThumbnail.cpp
blob84934695d1c5b222026042c1fbef081bbfa5dfa8
1 /*
2 * KFontInst - KDE Font Installer
4 * Copyright 2003-2007 Craig Drummond <craig@kde.org>
6 * ----
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 #include "FontThumbnail.h"
25 #include "KfiConstants.h"
26 #include "FcEngine.h"
27 #include <QtGui/QImage>
28 #include <QtGui/QPixmap>
29 #include <QtCore/QFile>
30 #include <KDE/KGlobalSettings>
31 #include <KDE/KLocale>
32 #include <KDE/KUrl>
33 #include <KDE/KZip>
34 #include <KDE/KTempDir>
35 #include <KDE/KMimeType>
36 #include <KDE/KStandardDirs>
37 #include <KDE/KDebug>
39 #define KFI_DBUG kDebug(7115)
41 extern "C"
43 KDE_EXPORT ThumbCreator *new_creator()
45 return new KFI::CFontThumbnail;
49 namespace KFI
52 CFontThumbnail::CFontThumbnail()
54 KGlobal::locale()->insertCatalog(KFI_CATALOGUE);
57 bool CFontThumbnail::create(const QString &path, int width, int height, QImage &img)
59 QPixmap pix;
60 QString realPath(path);
61 KTempDir *tempDir = 0;
63 CFcEngine::setBgndCol(Qt::white);
64 CFcEngine::setTextCol(Qt::black);
66 KFI_DBUG << "Create font thumbnail for:" << path << endl;
68 // Is this a fonts/package file? If so, extract 1 scalable font...
69 if(Misc::isPackage(path))
71 KZip zip(path);
73 if(zip.open(QIODevice::ReadOnly))
75 const KArchiveDirectory *zipDir=zip.directory();
77 if(zipDir)
79 QStringList fonts(zipDir->entries());
81 if(fonts.count())
83 QStringList::ConstIterator it(fonts.begin()),
84 end(fonts.end());
86 for(; it!=end; ++it)
88 const KArchiveEntry *entry=zipDir->entry(*it);
90 if(entry && entry->isFile())
92 delete tempDir;
93 tempDir=new KTempDir(KStandardDirs::locateLocal("tmp", KFI_TMP_DIR_PREFIX));
94 tempDir->setAutoRemove(true);
96 ((KArchiveFile *)entry)->copyTo(tempDir->name());
98 QString mime(KMimeType::findByPath(tempDir->name()+entry->name())->name());
100 if(mime=="application/x-font-ttf" || mime=="application/x-font-otf" ||
101 mime=="application/x-font-type1")
103 realPath=tempDir->name()+entry->name();
104 break;
106 else
107 ::unlink(QFile::encodeName(tempDir->name()+entry->name()).data());
115 if(CFcEngine::instance()->draw(KUrl(realPath), width, height, pix, 0, true))
117 img=pix.toImage().convertToFormat(QImage::Format_ARGB32);
119 int pixelsPerLine=img.bytesPerLine()/4;
121 for(int l=0; l<img.height(); ++l)
123 QRgb *scanLine=(QRgb *)img.scanLine(l);
125 for(int pixel=0; pixel<pixelsPerLine; ++pixel)
126 scanLine[pixel]=qRgba(qRed(scanLine[pixel]), qGreen(scanLine[pixel]),
127 qBlue(scanLine[pixel]),
128 0xFF-qRed(scanLine[pixel]));
131 delete tempDir;
132 return true;
135 delete tempDir;
136 return false;
139 ThumbCreator::Flags CFontThumbnail::flags() const
141 return None;