2
* KFontInst - KDE Font Installer
4
* Copyright 2003-2007 Craig Drummond <craig@kde.org>
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.
25
#include <QtCore/QCoreApplication>
26
#include <QtCore/QTextStream>
27
#include <QtGui/QPainter>
28
#include <QtGui/QFontDatabase>
29
#include <QtGui/QWidget>
30
#include <QtCore/QFile>
31
#include <QtGui/QPrinter>
32
#include <QtGui/QPrintDialog>
34
#include <KDE/KCmdLineArgs>
35
#include <KDE/KAboutData>
36
#include <KDE/KApplication>
37
#include <kdeprintdialog.h>
42
#include "CreateParent.h"
44
// Enable the following to allow printing of non-installed fonts. Does not seem to work :-(
45
//#define KFI_PRINT_APP_FONTS
49
static const int constMarginLineBefore=1;
50
static const int constMarginLineAfter=2;
51
static const int constMarginFont=4;
53
inline bool sufficientSpace(int y, int pageHeight, int size)
55
return (y+constMarginFont+size)<pageHeight;
58
static bool sufficientSpace(int y, int titleFontHeight, const int *sizes, int pageHeight, int size)
60
int required=titleFontHeight+constMarginLineBefore+constMarginLineAfter;
62
for(unsigned int s=0; sizes[s]; ++s)
66
required+=constMarginFont;
70
required+=(3*(constMarginFont+CFcEngine::constDefaultAlphaSize))+
71
constMarginLineBefore+constMarginLineAfter;
72
return (y+required)<pageHeight;
75
static void printItems(const QList<Misc::TFont> &items, int size, QWidget *parent)
78
char *oldLocale=setlocale(LC_NUMERIC, "C"),
81
QList<Misc::TFont>::ConstIterator it(items.begin()),
83
#ifdef KFI_PRINT_APP_FONTS
84
QHash<QString, int> appFont;
86
// Check for font files...
89
if('/'==(*it).family[0] && KFI_NO_STYLE_INFO==(*it).styleInfo &&
90
Misc::fExists((*it).family))
91
appFont[(*it).family]=QFontDatabase::addApplicationFont((*it).family);
93
appFont[(*it).family]=-1;
97
QPrintDialog *dialog = KdePrint::createPrintDialog(&printer, parent);
99
dialog->setWindowTitle(i18n("Print"));
104
QFont sans("sans", 12, QFont::Bold);
105
bool changedFontEmbeddingSetting(false);
106
QString str(CFcEngine(false).getPreviewString());
108
if(!printer.fontEmbeddingEnabled())
110
printer.setFontEmbeddingEnabled(true);
111
changedFontEmbeddingSetting=true;
114
printer.setResolution(72);
115
painter.begin(&printer);
117
int margin=(int)((2/2.54)*painter.device()->logicalDpiY()), // 2 cm margins
118
pageWidth=painter.device()->width()-(2*margin),
119
pageHeight=painter.device()->height()-(2*margin),
121
oneSize[2]={size, 0};
122
const int *sizes=oneSize;
123
bool firstFont(true);
126
sizes=CFcEngine::constScalableSizes;
128
painter.setClipping(true);
129
painter.setClipRect(margin, margin, pageWidth, pageHeight);
131
for(it=items.begin(); it!=end; ++it)
134
#ifdef KFI_PRINT_APP_FONTS
138
if(-1!=appFont[(*it).family])
140
family=QFontDatabase::applicationFontFamilies(appFont[(*it).family]).first();
144
painter.setFont(sans);
145
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 0);
147
if(!firstFont && !sufficientSpace(y, painter.fontMetrics().height(), sizes, pageHeight, size))
152
painter.setFont(sans);
153
y+=painter.fontMetrics().height();
155
#ifdef KFI_PRINT_APP_FONTS
158
painter.drawText(margin, y, FC::createName((*it).family, (*it).styleInfo));
159
#ifdef KFI_PRINT_APP_FONTS
161
painter.drawText(margin, y, family);
164
y+=constMarginLineBefore;
165
painter.drawLine(margin, y, margin+pageWidth, y);
166
y+=constMarginLineAfter;
170
#ifdef KFI_PRINT_APP_FONTS
173
painter.setFont(CFcEngine::getQFont((*it).family, (*it).styleInfo,
174
CFcEngine::constDefaultAlphaSize));
175
#ifdef KFI_PRINT_APP_FONTS
178
font.setPointSize(CFcEngine::constDefaultAlphaSize);
179
painter.setFont(font);
183
y+=CFcEngine::constDefaultAlphaSize;
184
painter.drawText(margin, y, CFcEngine::getLowercaseLetters());
185
y+=constMarginFont+CFcEngine::constDefaultAlphaSize;
186
painter.drawText(margin, y, CFcEngine::getUppercaseLetters());
187
y+=constMarginFont+CFcEngine::constDefaultAlphaSize;
188
painter.drawText(margin, y, CFcEngine::getPunctuation());
189
y+=constMarginFont+constMarginLineBefore;
190
painter.drawLine(margin, y, margin+pageWidth, y);
191
y+=constMarginLineAfter;
196
#ifdef KFI_PRINT_APP_FONTS
199
painter.setFont(CFcEngine::getQFont((*it).family, (*it).styleInfo, sizes[s]));
200
#ifdef KFI_PRINT_APP_FONTS
203
font.setPointSize(sizes[s]);
204
painter.setFont(font);
207
if(sufficientSpace(y, pageHeight, sizes[s]))
209
painter.drawText(margin, y, str);
216
y+=(s<1 || sizes[s-1]<25 ? 14 : 28);
223
// Did we change the users font settings? If so, reset to their previous values...
224
if(changedFontEmbeddingSetting)
225
printer.setFontEmbeddingEnabled(false);
229
setlocale(LC_NUMERIC, oldLocale);
235
static KAboutData aboutData("kfontprint", 0, ki18n("Font Printer"), "1.0", ki18n("Simple font printer"),
236
KAboutData::License_GPL, ki18n("(C) Craig Drummond, 2007"));
238
int main(int argc, char **argv)
240
KCmdLineArgs::init(argc, argv, &aboutData);
242
KCmdLineOptions options;
243
options.add("embed <winid>", ki18n("Makes the dialog transient for an X app specified by winid"));
244
options.add("size <index>", ki18n("Size index to print fonts"));
245
options.add("pfont <font>", ki18n("Font to print, specified as \"Family,Style\" where Style is a 24-bit decimal number composed as: <weight><width><slant>")); //krazy:exclude=i18ncheckarg
246
options.add("listfile <file>", ki18n("File containing list of fonts to print"));
247
options.add("deletefile", ki18n("Remove file containing list of fonts to print"));
248
KCmdLineArgs::addCmdLineOptions(options);
251
KCmdLineArgs *args(KCmdLineArgs::parsedArgs());
252
QList<Misc::TFont> fonts;
253
int size(args->getOption("size").toInt());
255
if(size>-1 && size<256)
257
QString listFile(args->getOption("listfile"));
263
if(f.open(QIODevice::ReadOnly))
269
QString family(str.readLine()),
270
style(str.readLine());
272
if(!family.isEmpty() && !style.isEmpty())
273
fonts.append(Misc::TFont(family, style.toUInt()));
280
if(args->isSet("deletefile"))
281
::unlink(listFile.toLocal8Bit().constData());
285
QStringList fl(args->getOptionList("pfont"));
286
QStringList::ConstIterator it(fl.begin()),
293
int commaPos=f.lastIndexOf(',');
296
fonts.append(Misc::TFont(f.left(commaPos), f.mid(commaPos+1).toUInt()));
302
KLocale::setMainCatalog(KFI_CATALOGUE);
303
printItems(fonts, size, createParent(args->getOption("embed").toInt(0, 16)));