OSDN Git Service

Version 0.5
[fontmanager/fontmanager.git] / installedfontinfo.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Takumi Asaki
4 ** All rights reserved.
5 ** Contact: Takumi Asaki (takumi.asaki@gmail.com)
6 **
7 ** This file is part of the fontmanager application.
8 **
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ****************************************************************************/
38
39 #include "installedfontinfo.h"
40
41 #include "fontsconfigproperties.h"
42
43 #include <QTextStream>
44
45 InstalledFontInfo::InstalledFontInfo(const QByteArray buf, const QString &localFontPath, QObject *parent) :
46     QObject(parent), mSystemFont(true), mLocale("en"), mFontProperty(0)
47 {
48     mValid = analyze(buf, localFontPath);
49 }
50
51 bool InstalledFontInfo::isValid()
52 {
53     return mValid;
54 }
55
56 QStringList InstalledFontInfo::family() const
57 {
58     return mFamily;
59 }
60
61 QStringList InstalledFontInfo::familylang() const
62 {
63     return mFamilylang;
64 }
65
66 QStringList InstalledFontInfo::style() const
67 {
68     return mStyle;
69 }
70
71 QStringList InstalledFontInfo::stylelang() const
72 {
73     return mStylelang;
74 }
75
76 QStringList InstalledFontInfo::fullname() const
77 {
78     return mFullname;
79 }
80
81 QStringList InstalledFontInfo::fullnamelang() const
82 {
83     return mFullnamelang;
84 }
85
86 QString InstalledFontInfo::foundry() const
87 {
88     return mFoundry;
89 }
90
91 QString InstalledFontInfo::file() const
92 {
93     return mFile;
94 }
95
96 QStringList InstalledFontInfo::lang() const
97 {
98     return mLang;
99 }
100
101 bool InstalledFontInfo::outline() const
102 {
103     return mOutline;
104 }
105
106 bool InstalledFontInfo::scalable() const
107 {
108     return mScalable;
109 }
110
111 int InstalledFontInfo::slant() const
112 {
113     return mSlant;
114 }
115
116 int InstalledFontInfo::weight() const
117 {
118     return mWeight;
119 }
120
121 int InstalledFontInfo::width() const
122 {
123     return mWidth;
124 }
125
126 QString InstalledFontInfo::localefamily() const
127 {
128     return localeString(mFamily, mFamilylang, mLocale);
129 }
130
131 QString InstalledFontInfo::localestyle() const
132 {
133     return localeString(mStyle, mStylelang, mLocale);
134 }
135
136 QString InstalledFontInfo::localefullname() const
137 {
138     return localeString(mFullname, mFullnamelang, mLocale);
139 }
140
141 QString InstalledFontInfo::enfamily() const
142 {
143     return localeString(mFamily, mFamilylang, QLatin1String("en"));
144 }
145
146 QString InstalledFontInfo::enstyle() const
147 {
148     return localeString(mStyle, mStylelang, QLatin1String("en"));
149 }
150
151 QString InstalledFontInfo::enfullname() const
152 {
153     return localeString(mFullname, mFullnamelang, QLatin1String("en"));
154 }
155
156 bool InstalledFontInfo::systemFont() const
157 {
158     return mSystemFont;
159 }
160
161 QString InstalledFontInfo::localeString(const QStringList &strlist, const QStringList &langlist, const QString &lang) const
162 {
163     int idx = langlist.indexOf(lang);
164     if (idx < 0)
165         idx = langlist.indexOf(QLatin1String("en"));
166     if (idx < 0)
167         idx = 0;
168     Q_ASSERT(idx < strlist.count());
169     return strlist.at(idx);
170 }
171
172 FontsConfigProperties *InstalledFontInfo::fontProperty() const
173 {
174     return mFontProperty;
175 }
176
177 void InstalledFontInfo::setFontProperty(FontsConfigProperties *prop)
178 {
179     mFontProperty = prop;
180     if (mFontProperty->parent() == 0)
181         mFontProperty->setParent(this);
182 }
183
184 bool InstalledFontInfo::compare(const InstalledFontInfo *info1, InstalledFontInfo *info2)
185 {
186     QString key1 = info1->localefamily() + ":" + info1->localefullname();
187     QString key2 = info2->localefamily() + ":" + info2->localefullname();
188     return (key1 < key2);
189 }
190
191 bool InstalledFontInfo::analyze(const QByteArray &buf, const QString &localFontPath)
192 {
193     static QRegExp headerRegexp("^Pattern has \\d+ elts \\(size \\d+\\)$");
194     static QRegExp splitter(":\\s+");
195     int idx = -1;
196
197     QTextStream ts(buf, QIODevice::ReadOnly);
198     QString linebuf = ts.readLine();
199     if (!headerRegexp.exactMatch(linebuf))
200         return false;
201
202     while (!ts.atEnd()) {
203         QString linebuf = ts.readLine().trimmed();
204         if ((idx = linebuf.indexOf(splitter)) > 0){
205             QString key = linebuf.left(idx);
206             QString val = linebuf.mid(idx + splitter.matchedLength());
207             if (key == "family") {
208                 mFamily = toStringList(val);
209             } else if (key == "familylang") {
210                 mFamilylang = toStringList(val);
211             } else if (key == "style") {
212                 mStyle = toStringList(val);
213             } else if (key == "stylelang") {
214                 mStylelang = toStringList(val);
215             } else if (key == "fullname") {
216                 mFullname = toStringList(val);
217             } else if (key == "fullnamelang") {
218                 mFullnamelang = toStringList(val);
219             } else if (key == "slant") {
220                 mSlant = toInt(val);
221             } else if (key == "weight") {
222                 mWeight = toInt(val);
223             } else if (key == "width") {
224                 mWidth = toInt(val);
225             } else if (key == "foundry") {
226                 mFoundry = toString(val);
227             } else if (key == "file") {
228                 mFile = toString(val);
229                 if (mFile.startsWith(localFontPath))
230                     mSystemFont = false;
231             } else if (key == "outline") {
232                 mOutline = toBool(val);
233             } else if (key == "scalable") {
234                 mScalable = toBool(val);
235             } else if (key == "lang") {
236                 int index = val.indexOf(QLatin1String("(s)"));
237                 Q_ASSERT(index >= 0);
238                 QString langs = val.left(index);
239                 mLang = langs.split(QLatin1Char('|'));
240             }
241         }
242     }
243
244     emit detailsChanged();
245
246     return true;
247 }
248
249 QString InstalledFontInfo::toString(const QString &value)
250 {
251     static QRegExp reg("\"([^\"]+)\"\\(s\\)");
252     QString str;
253     if (reg.indexIn(value) == 0)
254         str = reg.cap(1);
255     return str;
256 }
257
258 QStringList InstalledFontInfo::toStringList(const QString &value)
259 {
260     static QRegExp reg("\"([^\"]+)\"\\(s\\)");
261     QStringList list;
262     int index = 0;
263     while ((index = reg.indexIn(value, index)) >= 0) {
264         list << reg.cap(1);
265         index += reg.matchedLength();
266     }
267     return list;
268 }
269
270 int InstalledFontInfo::toInt(const QString &value)
271 {
272     static QRegExp reg("(\\d+)\\(i\\)\\(s\\)");
273     int val = 0;
274     if (reg.indexIn(value) == 0)
275         val = reg.cap(1).toInt();
276     return val;
277 }
278
279 bool InstalledFontInfo::toBool(const QString &value)
280 {
281     static QLatin1String fcTrue("FcTrue(s)");
282     if (value == fcTrue)
283         return true;
284     return false;
285 }
286
287 QString InstalledFontInfo::locale() const
288 {
289     return mLocale;
290 }
291
292 void InstalledFontInfo::setLocale(const QString &loc)
293 {
294     mLocale = loc;
295 }
296