1 /****************************************************************************
3 ** Copyright (C) 2012 Takumi Asaki
4 ** All rights reserved.
5 ** Contact: Takumi Asaki (takumi.asaki@gmail.com)
7 ** This file is part of the fontmanager application.
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
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
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
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."
37 ****************************************************************************/
39 #include "installedfontinfo.h"
41 #include "fontsconfigproperties.h"
43 #include <QTextStream>
45 InstalledFontInfo::InstalledFontInfo(const QByteArray buf, const QString &localFontPath, QObject *parent) :
46 QObject(parent), mSystemFont(true), mLocale("en"), mFontProperty(0)
48 mValid = analyze(buf, localFontPath);
51 bool InstalledFontInfo::isValid()
56 QStringList InstalledFontInfo::family() const
61 QStringList InstalledFontInfo::familylang() const
66 QStringList InstalledFontInfo::style() const
71 QStringList InstalledFontInfo::stylelang() const
76 QStringList InstalledFontInfo::fullname() const
81 QStringList InstalledFontInfo::fullnamelang() const
86 QString InstalledFontInfo::foundry() const
91 QString InstalledFontInfo::file() const
96 QStringList InstalledFontInfo::lang() const
101 bool InstalledFontInfo::outline() const
106 bool InstalledFontInfo::scalable() const
111 int InstalledFontInfo::slant() const
116 int InstalledFontInfo::weight() const
121 int InstalledFontInfo::width() const
126 QString InstalledFontInfo::localefamily() const
128 return localeString(mFamily, mFamilylang, mLocale);
131 QString InstalledFontInfo::localestyle() const
133 return localeString(mStyle, mStylelang, mLocale);
136 QString InstalledFontInfo::localefullname() const
138 return localeString(mFullname, mFullnamelang, mLocale);
141 QString InstalledFontInfo::enfamily() const
143 return localeString(mFamily, mFamilylang, QLatin1String("en"));
146 QString InstalledFontInfo::enstyle() const
148 return localeString(mStyle, mStylelang, QLatin1String("en"));
151 QString InstalledFontInfo::enfullname() const
153 return localeString(mFullname, mFullnamelang, QLatin1String("en"));
156 bool InstalledFontInfo::systemFont() const
161 QString InstalledFontInfo::localeString(const QStringList &strlist, const QStringList &langlist, const QString &lang) const
163 int idx = langlist.indexOf(lang);
165 idx = langlist.indexOf(QLatin1String("en"));
168 Q_ASSERT(idx < strlist.count());
169 return strlist.at(idx);
172 FontsConfigProperties *InstalledFontInfo::fontProperty() const
174 return mFontProperty;
177 void InstalledFontInfo::setFontProperty(FontsConfigProperties *prop)
179 mFontProperty = prop;
180 if (mFontProperty->parent() == 0)
181 mFontProperty->setParent(this);
184 bool InstalledFontInfo::compare(const InstalledFontInfo *info1, InstalledFontInfo *info2)
186 QString key1 = info1->localefamily() + ":" + info1->localefullname();
187 QString key2 = info2->localefamily() + ":" + info2->localefullname();
188 return (key1 < key2);
191 bool InstalledFontInfo::analyze(const QByteArray &buf, const QString &localFontPath)
193 static QRegExp headerRegexp("^Pattern has \\d+ elts \\(size \\d+\\)$");
194 static QRegExp splitter(":\\s+");
197 QTextStream ts(buf, QIODevice::ReadOnly);
198 QString linebuf = ts.readLine();
199 if (!headerRegexp.exactMatch(linebuf))
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") {
221 } else if (key == "weight") {
222 mWeight = toInt(val);
223 } else if (key == "width") {
225 } else if (key == "foundry") {
226 mFoundry = toString(val);
227 } else if (key == "file") {
228 mFile = toString(val);
229 if (mFile.startsWith(localFontPath))
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('|'));
244 emit detailsChanged();
249 QString InstalledFontInfo::toString(const QString &value)
251 static QRegExp reg("\"([^\"]+)\"\\(s\\)");
253 if (reg.indexIn(value) == 0)
258 QStringList InstalledFontInfo::toStringList(const QString &value)
260 static QRegExp reg("\"([^\"]+)\"\\(s\\)");
263 while ((index = reg.indexIn(value, index)) >= 0) {
265 index += reg.matchedLength();
270 int InstalledFontInfo::toInt(const QString &value)
272 static QRegExp reg("(\\d+)\\(i\\)\\(s\\)");
274 if (reg.indexIn(value) == 0)
275 val = reg.cap(1).toInt();
279 bool InstalledFontInfo::toBool(const QString &value)
281 static QLatin1String fcTrue("FcTrue(s)");
287 QString InstalledFontInfo::locale() const
292 void InstalledFontInfo::setLocale(const QString &loc)