File indexing completed on 2024-05-12 04:33:57

0001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
0002 // fontEncoding.cpp
0003 //
0004 // Part of KDVI - A DVI previewer for the KDE desktop environment
0005 //
0006 // SPDX-FileCopyrightText: 2003 Stefan Kebekus
0007 // SPDX-License-Identifier: GPL-2.0-or-later
0008 
0009 #include <config.h>
0010 
0011 #ifdef HAVE_FREETYPE
0012 
0013 #include "debug_dvi.h"
0014 #include "fontEncoding.h"
0015 
0016 #include <QFile>
0017 #include <QLoggingCategory>
0018 #include <QProcess>
0019 #include <QStandardPaths>
0020 #include <QTextStream>
0021 
0022 //#define DEBUG_FONTENC
0023 
0024 fontEncoding::fontEncoding(const QString &encName)
0025 {
0026 #ifdef DEBUG_FONTENC
0027     qCDebug(OkularDviDebug) << "fontEncoding( " << encName << " )";
0028 #endif
0029 
0030     _isValid = false;
0031     // Use kpsewhich to find the encoding file.
0032     QProcess kpsewhich;
0033     kpsewhich.setProcessChannelMode(QProcess::MergedChannels);
0034 
0035     // Make sure kpsewhich is in PATH and not just in the CWD
0036     static const QString fullPath = QStandardPaths::findExecutable(QStringLiteral("kpsewhich"));
0037     if (fullPath.isEmpty()) {
0038         qCCritical(OkularDviDebug) << "fontEncoding::fontEncoding(...): kpsewhich is not in path.";
0039         return;
0040     }
0041 
0042     kpsewhich.start(fullPath, QStringList() << encName, QIODevice::ReadOnly | QIODevice::Text);
0043 
0044     if (!kpsewhich.waitForStarted()) {
0045         qCCritical(OkularDviDebug) << "fontEncoding::fontEncoding(...): kpsewhich could not be started.";
0046         return;
0047     }
0048 
0049     // We wait here while the external program runs concurrently.
0050     kpsewhich.waitForFinished(-1);
0051 
0052     const QString encFileName = QString::fromLocal8Bit(kpsewhich.readAll()).trimmed();
0053     if (encFileName.isEmpty()) {
0054         qCCritical(OkularDviDebug) << QStringLiteral("fontEncoding::fontEncoding(...): The file '%1' could not be found by kpsewhich.").arg(encName);
0055         return;
0056     }
0057 
0058 #ifdef DEBUG_FONTENC
0059     qCDebug(OkularDviDebug) << "FileName of the encoding: " << encFileName;
0060 #endif
0061 
0062     QFile file(encFileName);
0063     if (file.open(QIODevice::ReadOnly)) {
0064         // Read the file (excluding comments) into the QString variable
0065         // 'fileContent'
0066         QTextStream stream(&file);
0067         QString fileContent;
0068         while (!stream.atEnd()) {
0069             fileContent += stream.readLine().section(QLatin1Char('%'), 0, 0); // line of text excluding '\n' until first '%'-sign
0070         }
0071         file.close();
0072 
0073         fileContent = fileContent.trimmed();
0074 
0075         // Find the name of the encoding
0076         encodingFullName = fileContent.section(QLatin1Char('['), 0, 0).simplified().mid(1);
0077 #ifdef DEBUG_FONTENC
0078         qCDebug(OkularDviDebug) << "encodingFullName: " << encodingFullName;
0079 #endif
0080 
0081         fileContent = fileContent.section(QLatin1Char('['), 1, 1).section(QLatin1Char(']'), 0, 0).simplified();
0082         const QStringList glyphNameList = fileContent.split(QLatin1Char('/'), Qt::SkipEmptyParts);
0083 
0084         int i = 0;
0085         for (QStringList::ConstIterator it = glyphNameList.constBegin(); (it != glyphNameList.constEnd()) && (i < 256); ++it) {
0086             glyphNameVector[i] = (*it).simplified();
0087 #ifdef DEBUG_FONTENC
0088             qCDebug(OkularDviDebug) << i << ": " << glyphNameVector[i];
0089 #endif
0090             i++;
0091         }
0092         for (; i < 256; i++) {
0093             glyphNameVector[i] = QStringLiteral(".notdef");
0094         }
0095     } else {
0096         qCCritical(OkularDviDebug) << QStringLiteral("fontEncoding::fontEncoding(...): The file '%1' could not be opened.").arg(encFileName);
0097         return;
0098     }
0099 
0100     _isValid = true;
0101 }
0102 
0103 #endif // HAVE_FREETYPE