File indexing completed on 2025-01-12 05:02:01

0001 /*
0002     SPDX-FileCopyrightText: 2003-2007 Craig Drummond <craig@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "FcQuery.h"
0007 #include "Fc.h"
0008 #include <QProcess>
0009 #include <stdio.h>
0010 
0011 namespace KFI
0012 {
0013 // key: 0(i)(s)
0014 static int getInt(const QString &str)
0015 {
0016     int rv = KFI_NULL_SETTING, start = str.lastIndexOf(':') + 1, end = str.lastIndexOf("(i)(s)");
0017 
0018     if (end > start) {
0019         rv = str.mid(start, end - start).trimmed().toInt();
0020     }
0021 
0022     return rv;
0023 }
0024 
0025 CFcQuery::~CFcQuery()
0026 {
0027 }
0028 
0029 void CFcQuery::run(const QString &query)
0030 {
0031     QStringList args;
0032 
0033     m_file = m_font = QString();
0034     m_buffer = QByteArray();
0035 
0036     if (m_proc) {
0037         m_proc->kill();
0038     } else {
0039         m_proc = new QProcess(this);
0040     }
0041 
0042     args << "-v" << query;
0043 
0044     connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(procExited()));
0045     connect(m_proc, &QProcess::readyReadStandardOutput, this, &CFcQuery::data);
0046 
0047     m_proc->start("fc-match", args);
0048 }
0049 
0050 void CFcQuery::procExited()
0051 {
0052     QString family;
0053     int weight(KFI_NULL_SETTING), slant(KFI_NULL_SETTING), width(KFI_NULL_SETTING);
0054     QStringList results(QString::fromUtf8(m_buffer, m_buffer.length()).split(QLatin1Char('\n')));
0055 
0056     if (!results.isEmpty()) {
0057         QStringList::ConstIterator it(results.begin()), end(results.end());
0058 
0059         for (; it != end; ++it) {
0060             QString line((*it).trimmed());
0061 
0062             if (0 == line.indexOf("file:")) // file: "Wibble"(s)
0063             {
0064                 int endPos = line.indexOf("\"(s)");
0065 
0066                 if (-1 != endPos) {
0067                     m_file = line.mid(7, endPos - 7);
0068                 }
0069             } else if (0 == line.indexOf("family:")) // family: "Wibble"(s)
0070             {
0071                 int endPos = line.indexOf("\"(s)");
0072 
0073                 if (-1 != endPos) {
0074                     family = line.mid(9, endPos - 9);
0075                 }
0076             } else if (0 == line.indexOf("slant:")) { // slant: 0(i)(s)
0077                 slant = getInt(line);
0078             } else if (0 == line.indexOf("weight:")) { // weight: 0(i)(s)
0079                 weight = getInt(line);
0080             } else if (0 == line.indexOf("width:")) { // width: 0(i)(s)
0081                 width = getInt(line);
0082             }
0083         }
0084     }
0085 
0086     if (!family.isEmpty()) {
0087         m_font = FC::createName(family, weight, width, slant);
0088     }
0089 
0090     Q_EMIT finished();
0091 }
0092 
0093 void CFcQuery::data()
0094 {
0095     m_buffer += m_proc->readAllStandardOutput();
0096 }
0097 
0098 }