SVN_SILENT made messages (.desktop file)
[kdegraphics.git] / okular / generators / dvi / fontMap.cpp
blob008d198d8580be47efd4e058d5d7a4351833a8da
1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
2 // fontMap.cpp
3 //
4 // Part of KDVI - A DVI previewer for the KDE desktop environment
5 //
6 // (C) 2003 Stefan Kebekus
7 // Distributed under the GPL
9 #include <config.h>
11 #ifdef HAVE_FREETYPE
13 #include "fontMap.h"
14 #include "kvs_debug.h"
16 #include <QFile>
17 #include <QProcess>
18 #include <QTextStream>
20 //#define DEBUG_FONTMAP
23 fontMap::fontMap()
25 // Read the map file of ps2pk which will provide us with a
26 // dictionary "TeX Font names" <-> "Name of font files, Font Names
27 // and Encodings" (example: the font "Times-Roman" is called
28 // "ptmr8y" in the DVI file, but the Type1 font file name is
29 // "utmr8a.pfb". We use the map file of "ps2pk" because that progam
30 // has, like kdvi (and unlike dvips), no built-in fonts.
32 // Finding ps2pk.map is not easy. In teTeX < 3.0, the kpsewhich
33 // program REQUIRES the option "--format=dvips config". In teTeX =
34 // 3.0, the option "--format=map" MUST be used. Since there is no
35 // way to give both options at the same time, there is seemingly no
36 // other way than to try both options one after another. We use the
37 // teTeX 3.0 format first.
38 QProcess kpsewhich;
39 kpsewhich.start("kpsewhich",
40 QStringList() << "--format=map" << "ps2pk.map",
41 QIODevice::ReadOnly|QIODevice::Text);
43 if (!kpsewhich.waitForStarted()) {
44 kError(kvs::dvi) << "fontMap::fontMap(): kpsewhich could not be started." << endl;
45 return;
48 // We wait here while the external program runs concurrently.
49 kpsewhich.waitForFinished(-1);
51 QString map_fileName = QString(kpsewhich.readAll()).trimmed();
52 if (map_fileName.isEmpty()) {
53 // Map file not found? Then we try the teTeX < 3.0 way of finding
54 // the file.
55 kpsewhich.start("kpsewhich",
56 QStringList() << "--format=dvips config" << "ps2pk.map",
57 QIODevice::ReadOnly|QIODevice::Text);
58 if (!kpsewhich.waitForStarted()) {
59 kError(kvs::dvi) << "fontMap::fontMap(): kpsewhich could not be started." << endl;
60 return;
63 kpsewhich.waitForFinished(-1);
65 map_fileName = QString(kpsewhich.readAll()).trimmed();
66 // If both versions fail, then there is nothing left to do.
67 if (map_fileName.isEmpty()) {
68 kError(kvs::dvi) << "fontMap::fontMap(): The file 'ps2pk.map' could not be found by kpsewhich." << endl;
69 return;
73 QFile file( map_fileName );
74 if ( file.open( QIODevice::ReadOnly ) ) {
75 QTextStream stream( &file );
76 QString line;
77 while ( !stream.atEnd() ) {
78 line = stream.readLine().simplified();
79 if (line.isEmpty() || (line.at(0) == '%'))
80 continue;
82 QString TeXName = line.section(' ', 0, 0);
83 QString FullName = line.section(' ', 1, 1);
84 QString fontFileName = line.section('<', -1).trimmed().section(' ', 0, 0);
85 QString encodingName = line.section('<', -2, -2).trimmed().section(' ', 0, 0);
86 // It seems that sometimes the encoding is prepended by the
87 // letter '[', which we ignore
88 if ((!encodingName.isEmpty()) && (encodingName[0] == '['))
89 encodingName = encodingName.mid(1);
91 double slant = 0.0;
92 int i = line.indexOf("SlantFont");
93 if (i >= 0) {
94 bool ok;
95 slant = line.left(i).section(' ', -1, -1 ,QString::SectionSkipEmpty).toDouble(&ok);
96 if (ok == false)
97 slant = 0.0;
100 fontMapEntry &entry = fontMapEntries[TeXName];
102 entry.slant = slant;
103 entry.fontFileName = fontFileName;
104 entry.fullFontName = FullName;
105 if (encodingName.endsWith(".enc"))
106 entry.fontEncoding = encodingName;
107 else
108 entry.fontEncoding.clear();
110 file.close();
111 } else
112 kError(kvs::dvi) << QString("fontMap::fontMap(): The file '%1' could not be opened.").arg(map_fileName) << endl;
114 #ifdef DEBUG_FONTMAP
115 kDebug(kvs::dvi) << "FontMap file parsed. Results:";
116 QMap<QString, fontMapEntry>::Iterator it;
117 for ( it = fontMapEntries.begin(); it != fontMapEntries.end(); ++it )
118 kDebug(kvs::dvi) << "TeXName: " << it.key()
119 << ", FontFileName=" << it.data().fontFileName
120 << ", FullName=" << it.data().fullFontName
121 << ", Encoding=" << it.data().fontEncoding
122 << "." << endl;;
123 #endif
127 const QString &fontMap::findFileName(const QString &TeXName)
129 QMap<QString, fontMapEntry>::Iterator it = fontMapEntries.find(TeXName);
131 if (it != fontMapEntries.end())
132 return it.value().fontFileName;
134 static const QString nullstring;
135 return nullstring;
139 const QString &fontMap::findFontName(const QString &TeXName)
141 QMap<QString, fontMapEntry>::Iterator it = fontMapEntries.find(TeXName);
143 if (it != fontMapEntries.end())
144 return it.value().fullFontName;
146 static const QString nullstring;
147 return nullstring;
151 const QString &fontMap::findEncoding(const QString &TeXName)
153 QMap<QString, fontMapEntry>::Iterator it = fontMapEntries.find(TeXName);
155 if (it != fontMapEntries.end())
156 return it.value().fontEncoding;
158 static const QString nullstring;
159 return nullstring;
163 double fontMap::findSlant(const QString &TeXName)
165 QMap<QString, fontMapEntry>::Iterator it = fontMapEntries.find(TeXName);
167 if (it != fontMapEntries.end())
168 return it.value().slant;
169 else
170 return 0.0;
173 #endif // HAVE_FREETYPE