add more spacing
[personal-kdebase.git] / workspace / kcontrol / kfontinst / apps / Printer.cpp
blob71e494ec2405336c4070cc29412de0026129fb18
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 "FcEngine.h"
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>
39 #ifdef HAVE_LOCALE_H
40 #include <locale.h>
41 #endif
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
47 using namespace KFI;
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)
64 required+=sizes[s];
65 if(sizes[s+1])
66 required+=constMarginFont;
69 if(0==size)
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)
77 #ifdef HAVE_LOCALE_H
78 char *oldLocale=setlocale(LC_NUMERIC, "C"),
79 #endif
81 QList<Misc::TFont>::ConstIterator it(items.begin()),
82 end(items.end());
83 #ifdef KFI_PRINT_APP_FONTS
84 QHash<QString, int> appFont;
86 // Check for font files...
87 for(; it!=end; ++it)
89 if('/'==(*it).family[0] && KFI_NO_STYLE_INFO==(*it).styleInfo &&
90 Misc::fExists((*it).family))
91 appFont[(*it).family]=QFontDatabase::addApplicationFont((*it).family);
92 else
93 appFont[(*it).family]=-1;
95 #endif
96 QPrinter printer;
97 QPrintDialog *dialog = KdePrint::createPrintDialog(&printer, parent);
99 dialog->setWindowTitle(i18n("Print"));
101 if(dialog->exec())
103 QPainter painter;
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),
120 y=margin,
121 oneSize[2]={size, 0};
122 const int *sizes=oneSize;
123 bool firstFont(true);
125 if(0==size)
126 sizes=CFcEngine::constScalableSizes;
128 painter.setClipping(true);
129 painter.setClipRect(margin, margin, pageWidth, pageHeight);
131 for(it=items.begin(); it!=end; ++it)
133 unsigned int s=0;
134 #ifdef KFI_PRINT_APP_FONTS
135 QString family;
136 QFont font;
138 if(-1!=appFont[(*it).family])
140 family=QFontDatabase::applicationFontFamilies(appFont[(*it).family]).first();
141 font=QFont(family);
143 #endif
144 painter.setFont(sans);
145 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 0);
147 if(!firstFont && !sufficientSpace(y, painter.fontMetrics().height(), sizes, pageHeight, size))
149 printer.newPage();
150 y=margin;
152 painter.setFont(sans);
153 y+=painter.fontMetrics().height();
155 #ifdef KFI_PRINT_APP_FONTS
156 if(family.isEmpty())
157 #endif
158 painter.drawText(margin, y, FC::createName((*it).family, (*it).styleInfo));
159 #ifdef KFI_PRINT_APP_FONTS
160 else
161 painter.drawText(margin, y, family);
162 #endif
164 y+=constMarginLineBefore;
165 painter.drawLine(margin, y, margin+pageWidth, y);
166 y+=constMarginLineAfter;
168 if(0==size)
170 #ifdef KFI_PRINT_APP_FONTS
171 if(family.isEmpty())
172 #endif
173 painter.setFont(CFcEngine::getQFont((*it).family, (*it).styleInfo,
174 CFcEngine::constDefaultAlphaSize));
175 #ifdef KFI_PRINT_APP_FONTS
176 else
178 font.setPointSize(CFcEngine::constDefaultAlphaSize);
179 painter.setFont(font);
181 #endif
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;
193 for(; sizes[s]; ++s)
195 y+=sizes[s];
196 #ifdef KFI_PRINT_APP_FONTS
197 if(family.isEmpty())
198 #endif
199 painter.setFont(CFcEngine::getQFont((*it).family, (*it).styleInfo, sizes[s]));
200 #ifdef KFI_PRINT_APP_FONTS
201 else
203 font.setPointSize(sizes[s]);
204 painter.setFont(font);
206 #endif
207 if(sufficientSpace(y, pageHeight, sizes[s]))
209 painter.drawText(margin, y, str);
210 if(sizes[s+1])
211 y+=constMarginFont;
213 else
214 break;
216 y+=(s<1 || sizes[s-1]<25 ? 14 : 28);
217 firstFont=false;
220 painter.end();
223 // Did we change the users font settings? If so, reset to their previous values...
224 if(changedFontEmbeddingSetting)
225 printer.setFontEmbeddingEnabled(false);
227 #ifdef HAVE_LOCALE_H
228 if(oldLocale)
229 setlocale(LC_NUMERIC, oldLocale);
230 #endif
232 delete dialog;
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);
250 KApplication app;
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"));
259 if(listFile.size())
261 QFile f(listFile);
263 if(f.open(QIODevice::ReadOnly))
265 QTextStream str(&f);
267 while (!str.atEnd())
269 QString family(str.readLine()),
270 style(str.readLine());
272 if(!family.isEmpty() && !style.isEmpty())
273 fonts.append(Misc::TFont(family, style.toUInt()));
274 else
275 break;
277 f.close();
280 if(args->isSet("deletefile"))
281 ::unlink(listFile.toLocal8Bit().constData());
283 else
285 QStringList fl(args->getOptionList("pfont"));
286 QStringList::ConstIterator it(fl.begin()),
287 end(fl.end());
289 for(; it!=end; ++it)
291 QString f(*it);
293 int commaPos=f.lastIndexOf(',');
295 if(-1!=commaPos)
296 fonts.append(Misc::TFont(f.left(commaPos), f.mid(commaPos+1).toUInt()));
300 if(fonts.count())
302 KLocale::setMainCatalog(KFI_CATALOGUE);
303 printItems(fonts, size, createParent(args->getOption("embed").toInt(0, 16)));
305 return 0;
309 return -1;