1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
4 // Part of KDVI - A DVI previewer for the KDE desktop environment
6 // (C) 2003 Stefan Kebekus
7 // Distributed under the GPL
14 #include "kvs_debug.h"
18 #include <QTextStream>
20 //#define DEBUG_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.
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
;
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
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
;
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
;
73 QFile
file( map_fileName
);
74 if ( file
.open( QIODevice::ReadOnly
) ) {
75 QTextStream
stream( &file
);
77 while ( !stream
.atEnd() ) {
78 line
= stream
.readLine().simplified();
79 if (line
.isEmpty() || (line
.at(0) == '%'))
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);
92 int i
= line
.indexOf("SlantFont");
95 slant
= line
.left(i
).section(' ', -1, -1 ,QString::SectionSkipEmpty
).toDouble(&ok
);
100 fontMapEntry
&entry
= fontMapEntries
[TeXName
];
103 entry
.fontFileName
= fontFileName
;
104 entry
.fullFontName
= FullName
;
105 if (encodingName
.endsWith(".enc"))
106 entry
.fontEncoding
= encodingName
;
108 entry
.fontEncoding
.clear();
112 kError(kvs::dvi
) << QString("fontMap::fontMap(): The file '%1' could not be opened.").arg(map_fileName
) << endl
;
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
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
;
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
;
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
;
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
;
173 #endif // HAVE_FREETYPE