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

« back to all changes in this revision

Viewing changes to kcontrol/kfontinst/dbus/Utils.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-2009 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 <QtCore/QFile>
 
25
#include <QtCore/QByteArray>
 
26
#include <QtCore/QTextStream>
 
27
#include <KDE/KShell>
 
28
#include <fontconfig/fontconfig.h>
 
29
#include "Utils.h"
 
30
#include "FontInst.h"
 
31
#include "Misc.h"
 
32
#include "Fc.h"
 
33
#include "WritingSystems.h"
 
34
 
 
35
namespace KFI
 
36
{
 
37
 
 
38
namespace Utils
 
39
{
 
40
 
 
41
QString modifyName(const QString &fname)
 
42
{
 
43
    static const char constSymbols[]={ '-', ' ', ':', ';', '/', '~', 0 };
 
44
 
 
45
    QString rv(fname);
 
46
 
 
47
    for(int s=0; constSymbols[s]; ++s)
 
48
        rv=rv.replace(constSymbols[s], '_');
 
49
 
 
50
    int dotPos(rv.lastIndexOf('.'));
 
51
 
 
52
    return -1==dotPos
 
53
            ? rv
 
54
            : rv.left(dotPos+1)+rv.mid(dotPos+1).toLower();
 
55
}
 
56
 
 
57
bool isAAfm(const QString &fname)
 
58
{
 
59
    if(Misc::checkExt(QFile::encodeName(fname), "afm"))   // CPD? Is this a necessary check?
 
60
    {
 
61
        QFile file(fname);
 
62
 
 
63
        if(file.open(QIODevice::ReadOnly))
 
64
        {
 
65
            QTextStream stream(&file);
 
66
            QString     line;
 
67
 
 
68
            for(int lc=0; lc<30 && !stream.atEnd(); ++lc)
 
69
            {
 
70
                line=stream.readLine();
 
71
 
 
72
                if(line.contains("StartFontMetrics"))
 
73
                {
 
74
                    file.close();
 
75
                    return true;
 
76
                }
 
77
            }
 
78
 
 
79
            file.close();
 
80
        }
 
81
    }
 
82
 
 
83
    return false;
 
84
}
 
85
 
 
86
bool isAPfm(const QString &fname)
 
87
{
 
88
    bool ok=false;
 
89
 
 
90
    // I know extension checking is bad, but Ghostscript's pf2afm requires the pfm file to
 
91
    // have the .pfm extension...
 
92
    QByteArray name(QFile::encodeName(fname));
 
93
 
 
94
    if(Misc::checkExt(name, "pfm"))
 
95
    {
 
96
        //
 
97
        // OK, the extension matches, so perform a little contents checking...
 
98
        FILE *f=fopen(name.constData(), "r");
 
99
 
 
100
        if(f)
 
101
        {
 
102
            static const unsigned long constCopyrightLen =  60;
 
103
            static const unsigned long constTypeToExt    =  49;
 
104
            static const unsigned long constExtToFname   =  20;
 
105
            static const unsigned long constExtLen       =  30;
 
106
            static const unsigned long constFontnameMin  =  75;
 
107
            static const unsigned long constFontnameMax  = 512;
 
108
 
 
109
            unsigned short version=0,
 
110
                           type=0,
 
111
                           extlen=0;
 
112
            unsigned long  length=0,
 
113
                           fontname=0,
 
114
                           fLength=0;
 
115
 
 
116
            fseek(f, 0, SEEK_END);
 
117
            fLength=ftell(f);
 
118
            fseek(f, 0, SEEK_SET);
 
119
 
 
120
            if(2==fread(&version, 1, 2, f) && // Read version
 
121
               4==fread(&length, 1, 4, f) &&  // length...
 
122
               length==fLength &&
 
123
               0==fseek(f, constCopyrightLen, SEEK_CUR) &&   // Skip copyright notice...
 
124
               2==fread(&type, 1, 2, f) &&
 
125
               0==fseek(f, constTypeToExt, SEEK_CUR) &&
 
126
               2==fread(&extlen, 1, 2, f) &&
 
127
               extlen==constExtLen &&
 
128
               0==fseek(f, constExtToFname, SEEK_CUR) &&
 
129
               4==fread(&fontname, 1, 4, f) &&
 
130
               fontname>constFontnameMin && fontname<constFontnameMax)
 
131
                ok=true;
 
132
            fclose(f);
 
133
        }
 
134
    }
 
135
 
 
136
    return ok;
 
137
}
 
138
 
 
139
// This function is *only* used for the generation of AFMs from PFMs.
 
140
bool isAType1(const QString &fname)
 
141
{
 
142
    static const char         constStr[]="%!PS-AdobeFont-";
 
143
    static const unsigned int constStrLen=15;
 
144
    static const unsigned int constPfbOffset=6;
 
145
    static const unsigned int constPfbLen=constStrLen+constPfbOffset;
 
146
 
 
147
    QByteArray name(QFile::encodeName(fname));
 
148
    char       buffer[constPfbLen];
 
149
    bool       match=false;
 
150
 
 
151
    if(Misc::checkExt(name, "pfa"))
 
152
    {
 
153
        FILE *f=fopen(name.constData(), "r");
 
154
 
 
155
        if(f)
 
156
        {
 
157
            if(constStrLen==fread(buffer, 1, constStrLen, f))
 
158
                match=0==memcmp(buffer, constStr, constStrLen);
 
159
            fclose(f);
 
160
        }
 
161
    }
 
162
    else if(Misc::checkExt(name, "pfb"))
 
163
    {
 
164
        static const char constPfbMarker = static_cast<char>(0x80);
 
165
 
 
166
        FILE *f=fopen(name.constData(), "r");
 
167
 
 
168
        if(f)
 
169
        {
 
170
            if(constPfbLen==fread(buffer, 1, constPfbLen, f))
 
171
                match=buffer[0]==constPfbMarker && 0==memcmp(&buffer[constPfbOffset], constStr,
 
172
                                                             constStrLen);
 
173
            fclose(f);
 
174
        }
 
175
    }
 
176
 
 
177
    return match;
 
178
}
 
179
 
 
180
static QString getMatch(const QString &file, const char *extension)
 
181
{
 
182
    QString f(Misc::changeExt(file, extension));
 
183
 
 
184
    return Misc::fExists(f) ? f : QString();
 
185
}
 
186
 
 
187
void createAfm(const QString &file, EFileType type)
 
188
{
 
189
    bool pfm=FILE_PFM==type,
 
190
         type1=FILE_SCALABLE==type && isAType1(file);
 
191
 
 
192
    if(type1 || pfm)
 
193
    {
 
194
        // pf2afm wants files with lowercase extension, so just check for lowercase!
 
195
        // -- when a font is installed, the extension is converted to lowercase anyway...
 
196
        QString afm=getMatch(file, "afm");
 
197
 
 
198
        if(afm.isEmpty())  // No point creating if AFM already exists!
 
199
        {
 
200
            QString pfm,
 
201
                    t1;
 
202
 
 
203
            if(type1)      // Its a Type1, so look for existing PFM
 
204
            {
 
205
                pfm=getMatch(file, "pfm");
 
206
                t1=file;
 
207
            }
 
208
            else           // Its a PFM, so look for existing Type1
 
209
            {
 
210
                t1=getMatch(file, "pfa");
 
211
                if(t1.isEmpty())
 
212
                    t1=getMatch(file, "pfb");
 
213
                pfm=file;
 
214
            }
 
215
 
 
216
            if(!t1.isEmpty() && !pfm.isEmpty())         // Do we have both Type1 and PFM?
 
217
            {
 
218
                QString rootName(t1.left(t1.length()-4));
 
219
                Misc::doCmd("pf2afm", KShell::quoteArg(rootName)); // pf2afm wants name without extension...
 
220
                Misc::setFilePerms(QFile::encodeName(rootName+".afm"));
 
221
            }
 
222
        }
 
223
    }
 
224
}
 
225
 
 
226
EFileType check(const QString &file, Family &fam)
 
227
{
 
228
    if(isAAfm(file))
 
229
        return FILE_AFM;
 
230
    else if(isAPfm(file))
 
231
        return FILE_PFM;
 
232
    else
 
233
    {
 
234
        // Check that file is a font via FreeType...
 
235
        int       count=0;
 
236
        FcPattern *pat=FcFreeTypeQuery((const FcChar8 *)(QFile::encodeName(file).constData()), 0, NULL,
 
237
                                       &count);
 
238
 
 
239
        if(pat)
 
240
        {
 
241
            FcBool     scalable;
 
242
            QString    family,
 
243
                       foundry;
 
244
            quint32    style;
 
245
            int        index;
 
246
            qulonglong ws;
 
247
            EFileType  type=(FcResultMatch!=FcPatternGetBool(pat, FC_SCALABLE, 0, &scalable) || !scalable)
 
248
                                ? FILE_BITMAP : FILE_SCALABLE;
 
249
 
 
250
            FC::getDetails(pat, family, style, index, foundry);
 
251
            ws=WritingSystems::instance()->get(pat);
 
252
            FcPatternDestroy(pat);
 
253
            Style st(style, scalable, ws);
 
254
            st.add(File(file, foundry, index));
 
255
            fam=Family(family);
 
256
            fam.add(st);
 
257
            return type;
 
258
        }
 
259
    }
 
260
    return FILE_INVALID;
 
261
}
 
262
 
 
263
}
 
264
 
 
265
}