~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/kfontinst/apps/Printer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * KFontInst - KDE Font Installer
 
3
 *
 
4
 * Copyright 2003-2007 Craig Drummond <craig@kde.org>
 
5
 *
 
6
 * ----
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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.
 
22
 */
 
23
 
 
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>
 
33
 
 
34
#include <KDE/KCmdLineArgs>
 
35
#include <KDE/KAboutData>
 
36
#include <KDE/KApplication>
 
37
#include <kdeprintdialog.h>
 
38
 
 
39
#ifdef HAVE_LOCALE_H
 
40
#include <locale.h>
 
41
#endif
 
42
#include "CreateParent.h"
 
43
 
 
44
// Enable the following to allow printing of non-installed fonts. Does not seem to work :-(
 
45
//#define KFI_PRINT_APP_FONTS
 
46
 
 
47
using namespace KFI;
 
48
 
 
49
static const int constMarginLineBefore=1;
 
50
static const int constMarginLineAfter=2;
 
51
static const int constMarginFont=4;
 
52
 
 
53
inline bool sufficientSpace(int y, int pageHeight, int size)
 
54
{
 
55
    return (y+constMarginFont+size)<pageHeight;
 
56
}
 
57
 
 
58
static bool sufficientSpace(int y, int titleFontHeight, const int *sizes, int pageHeight, int size)
 
59
{
 
60
    int required=titleFontHeight+constMarginLineBefore+constMarginLineAfter;
 
61
 
 
62
    for(unsigned int s=0; sizes[s]; ++s)
 
63
    {
 
64
        required+=sizes[s];
 
65
        if(sizes[s+1])
 
66
            required+=constMarginFont;
 
67
    }
 
68
 
 
69
    if(0==size)
 
70
        required+=(3*(constMarginFont+CFcEngine::constDefaultAlphaSize))+
 
71
                  constMarginLineBefore+constMarginLineAfter;
 
72
    return (y+required)<pageHeight;
 
73
}
 
74
 
 
75
static void printItems(const QList<Misc::TFont> &items, int size, QWidget *parent)
 
76
{
 
77
#ifdef HAVE_LOCALE_H
 
78
    char *oldLocale=setlocale(LC_NUMERIC, "C"),
 
79
#endif
 
80
 
 
81
    QList<Misc::TFont>::ConstIterator it(items.begin()),
 
82
                                      end(items.end());
 
83
#ifdef KFI_PRINT_APP_FONTS
 
84
    QHash<QString, int>               appFont;
 
85
 
 
86
    // Check for font files...
 
87
    for(; it!=end; ++it)
 
88
    {
 
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;
 
94
    }
 
95
#endif
 
96
    QPrinter     printer;
 
97
    QPrintDialog *dialog = KdePrint::createPrintDialog(&printer, parent);
 
98
 
 
99
    dialog->setWindowTitle(i18n("Print"));
 
100
 
 
101
    if(dialog->exec())
 
102
    {
 
103
        QPainter   painter;
 
104
        QFont      sans("sans", 12, QFont::Bold);
 
105
        bool       changedFontEmbeddingSetting(false);
 
106
        QString    str(CFcEngine(false).getPreviewString());
 
107
 
 
108
        if(!printer.fontEmbeddingEnabled())
 
109
        {
 
110
            printer.setFontEmbeddingEnabled(true);
 
111
            changedFontEmbeddingSetting=true;
 
112
        }
 
113
 
 
114
        printer.setResolution(72);
 
115
        painter.begin(&printer);
 
116
 
 
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);
 
124
 
 
125
        if(0==size)
 
126
            sizes=CFcEngine::constScalableSizes;
 
127
 
 
128
        painter.setClipping(true);
 
129
        painter.setClipRect(margin, margin, pageWidth, pageHeight);
 
130
 
 
131
        for(it=items.begin(); it!=end; ++it)
 
132
        {
 
133
            unsigned int s=0;
 
134
#ifdef KFI_PRINT_APP_FONTS
 
135
            QString      family;
 
136
            QFont        font;
 
137
 
 
138
            if(-1!=appFont[(*it).family])
 
139
            {
 
140
                family=QFontDatabase::applicationFontFamilies(appFont[(*it).family]).first();
 
141
                font=QFont(family);
 
142
            }
 
143
#endif
 
144
            painter.setFont(sans);
 
145
            QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 0);
 
146
 
 
147
            if(!firstFont && !sufficientSpace(y, painter.fontMetrics().height(), sizes, pageHeight, size))
 
148
            {
 
149
                printer.newPage();
 
150
                y=margin;
 
151
            }
 
152
            painter.setFont(sans);
 
153
            y+=painter.fontMetrics().height();
 
154
 
 
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
 
163
 
 
164
            y+=constMarginLineBefore;
 
165
            painter.drawLine(margin, y, margin+pageWidth, y);
 
166
            y+=constMarginLineAfter;
 
167
 
 
168
            if(0==size)
 
169
            {
 
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
 
177
                {
 
178
                    font.setPointSize(CFcEngine::constDefaultAlphaSize);
 
179
                    painter.setFont(font);
 
180
                }
 
181
#endif
 
182
 
 
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;
 
192
            }
 
193
            for(; sizes[s]; ++s)
 
194
            {
 
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
 
202
                {
 
203
                    font.setPointSize(sizes[s]);
 
204
                    painter.setFont(font);
 
205
                }
 
206
#endif
 
207
                if(sufficientSpace(y, pageHeight, sizes[s]))
 
208
                {
 
209
                    painter.drawText(margin, y, str);
 
210
                    if(sizes[s+1])
 
211
                        y+=constMarginFont;
 
212
                }
 
213
                else
 
214
                    break;
 
215
            }
 
216
            y+=(s<1 || sizes[s-1]<25 ? 14 : 28);
 
217
            firstFont=false;
 
218
        }
 
219
 
 
220
        painter.end();
 
221
 
 
222
        //
 
223
        // Did we change the users font settings? If so, reset to their previous values...
 
224
        if(changedFontEmbeddingSetting)
 
225
            printer.setFontEmbeddingEnabled(false);
 
226
    }
 
227
#ifdef HAVE_LOCALE_H
 
228
    if(oldLocale)
 
229
        setlocale(LC_NUMERIC, oldLocale);
 
230
#endif
 
231
 
 
232
    delete dialog;
 
233
}
 
234
 
 
235
static KAboutData aboutData("kfontprint", 0, ki18n("Font Printer"), "1.0", ki18n("Simple font printer"),
 
236
                            KAboutData::License_GPL, ki18n("(C) Craig Drummond, 2007"));
 
237
 
 
238
int main(int argc, char **argv)
 
239
{
 
240
    KCmdLineArgs::init(argc, argv, &aboutData);
 
241
 
 
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);
 
249
 
 
250
    KApplication       app;
 
251
    KCmdLineArgs       *args(KCmdLineArgs::parsedArgs());
 
252
    QList<Misc::TFont> fonts;
 
253
    int                size(args->getOption("size").toInt());
 
254
 
 
255
    if(size>-1 && size<256)
 
256
    {
 
257
        QString listFile(args->getOption("listfile"));
 
258
 
 
259
        if(listFile.size())
 
260
        {
 
261
            QFile f(listFile);
 
262
 
 
263
            if(f.open(QIODevice::ReadOnly))
 
264
            {
 
265
                QTextStream str(&f);
 
266
 
 
267
                while (!str.atEnd())
 
268
                {
 
269
                    QString family(str.readLine()),
 
270
                            style(str.readLine());
 
271
 
 
272
                    if(!family.isEmpty() && !style.isEmpty())
 
273
                        fonts.append(Misc::TFont(family, style.toUInt()));
 
274
                    else
 
275
                        break;
 
276
                }
 
277
                f.close();
 
278
            }
 
279
 
 
280
            if(args->isSet("deletefile"))
 
281
                ::unlink(listFile.toLocal8Bit().constData());
 
282
        }
 
283
        else
 
284
        {
 
285
            QStringList                fl(args->getOptionList("pfont"));
 
286
            QStringList::ConstIterator it(fl.begin()),
 
287
                                          end(fl.end());
 
288
 
 
289
            for(; it!=end; ++it)
 
290
            {
 
291
                QString f(*it);
 
292
 
 
293
                int commaPos=f.lastIndexOf(',');
 
294
 
 
295
                if(-1!=commaPos)
 
296
                    fonts.append(Misc::TFont(f.left(commaPos), f.mid(commaPos+1).toUInt()));
 
297
            }
 
298
        }
 
299
 
 
300
        if(fonts.count())
 
301
        {
 
302
            KLocale::setMainCatalog(KFI_CATALOGUE);
 
303
            printItems(fonts, size, createParent(args->getOption("embed").toInt(0, 16)));
 
304
 
 
305
            return 0;
 
306
        }
 
307
    }
 
308
 
 
309
    return -1;
 
310
}