SVN_SILENT made messages (.desktop file)
[kdegraphics.git] / okular / generators / dvi / TeXFontDefinition.cpp
blobd780ad82286de518a8e48d7d750272bfe022b6b5
1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
2 #include <config.h>
4 #include "TeXFontDefinition.h"
5 #include "dviRenderer.h"
6 #include "kvs_debug.h"
7 #ifdef HAVE_FREETYPE
8 # include "TeXFont_PFB.h"
9 #endif
10 #include "TeXFont_PK.h"
11 #include "TeXFont_TFM.h"
12 #include "xdvi.h"
14 #include <klocale.h>
16 #include <QFile>
18 extern const int MFResolutions[];
20 #define PK_PRE 247
21 #define PK_ID 89
22 #define PK_MAGIC (PK_PRE << 8) + PK_ID
23 #define GF_PRE 247
24 #define GF_ID 131
25 #define GF_MAGIC (GF_PRE << 8) + GF_ID
26 #define VF_PRE 247
27 #define VF_ID_BYTE 202
28 #define VF_MAGIC (VF_PRE << 8) + VF_ID_BYTE
30 // #define DEBUG_FONT
33 TeXFontDefinition::TeXFontDefinition(const QString &nfontname, double _displayResolution_in_dpi, quint32 chk, qint32 _scaled_size_in_DVI_units,
34 class fontPool *pool, double _enlargement)
36 #ifdef DEBUG_FONT
37 kDebug(kvs::dvi) << "TeXFontDefinition::TeXFontDefinition(...); fontname=" << nfontname << ", enlargement=" << _enlargement;
38 #endif
40 enlargement = _enlargement;
41 font_pool = pool;
42 fontname = nfontname;
43 font = 0;
44 displayResolution_in_dpi = _displayResolution_in_dpi;
45 checksum = chk;
46 flags = TeXFontDefinition::FONT_IN_USE;
47 file = 0;
48 filename.clear();
49 scaled_size_in_DVI_units = _scaled_size_in_DVI_units;
51 macrotable = 0;
53 // By default, this font contains only empty characters. After the
54 // font has been loaded, this function pointer will be replaced by
55 // another one.
56 set_char_p = &dviRenderer::set_empty_char;
60 TeXFontDefinition::~TeXFontDefinition()
62 #ifdef DEBUG_FONT
63 kDebug(kvs::dvi) << "discarding font " << fontname << " at " << (int)(enlargement * MFResolutions[font_pool->getMetafontMode()] + 0.5) << " dpi";
64 #endif
66 if (font != 0) {
67 delete font;
68 font = 0;
70 if (macrotable != 0) {
71 delete [] macrotable;
72 macrotable = 0;
75 if (flags & FONT_LOADED) {
76 if (file != 0) {
77 fclose(file);
78 file = 0;
80 if (flags & FONT_VIRTUAL)
81 vf_table.clear();
86 void TeXFontDefinition::fontNameReceiver(const QString& fname)
88 #ifdef DEBUG_FONT
89 kDebug(kvs::dvi) << "void TeXFontDefinition::fontNameReceiver( " << fname << " )";
90 #endif
92 flags |= TeXFontDefinition::FONT_LOADED;
93 filename = fname;
94 #ifdef HAVE_FREETYPE
95 fullFontName.clear();
96 fullEncodingName.clear();
97 #endif
99 file = fopen(QFile::encodeName(filename), "r");
100 // Check if the file could be opened. If not, try to find the file
101 // in the DVI file's directory. If that works, modify the filename
102 // accordingly and go on.
103 if (file == 0) {
104 QString filename_test(font_pool->getExtraSearchPath() + '/' + filename);
105 file = fopen( QFile::encodeName(filename_test), "r");
106 if (file == 0) {
107 kError(kvs::dvi) << i18n("Cannot find font %1, file %2.", fontname, filename) << endl;
108 return;
109 } else
110 filename = filename_test;
113 set_char_p = &dviRenderer::set_char;
114 int magic = two(file);
116 if (fname.endsWith("pk"))
117 if (magic == PK_MAGIC) {
118 fclose(file);
119 file = 0;
120 font = new TeXFont_PK(this);
121 set_char_p = &dviRenderer::set_char;
122 if ((checksum != 0) && (checksum != font->checksum))
123 kWarning(kvs::dvi) << i18n("Checksum mismatch for font file %1", filename) ;
124 fontTypeName = "TeX PK";
125 return;
128 if (fname.endsWith(".vf"))
129 if (magic == VF_MAGIC) {
130 read_VF_index();
131 set_char_p = &dviRenderer::set_vf_char;
132 fontTypeName = i18n("TeX virtual");
133 return;
136 if (fname.endsWith(".tfm")) {
137 fclose(file);
138 file = 0;
139 font = new TeXFont_TFM(this);
140 set_char_p = &dviRenderer::set_char;
141 fontTypeName = i18n("TeX Font Metric");
142 return;
145 // None of these known types? Then it should be one of the font
146 // formats that are handled by the FreeType library
147 fclose(file);
148 file = 0;
149 #ifdef HAVE_FREETYPE
150 // Find the encoding for that font
151 const QString &enc = font_pool->fontsByTeXName.findEncoding(fontname);
153 if (enc.isEmpty() == false) {
154 #ifdef DEBUG_FONT
155 kDebug(kvs::dvi) << "Font " << fontname << " uses encoding " << enc;
156 #endif
157 font = new TeXFont_PFB(this, font_pool->encodingPool.findByName(enc), font_pool->fontsByTeXName.findSlant(fontname) );
158 } else {
159 #ifdef DEBUG_FONT
160 kDebug(kvs::dvi) << "Font " << fontname << " does not have an encoding.";
161 #endif
162 font = new TeXFont_PFB(this);
165 set_char_p = &dviRenderer::set_char;
166 fontTypeName = i18n("FreeType");
167 return;
168 #else
169 // If we don't have the FreeType library, we should never have
170 // reached this point. Complain, and leave this font blank
171 kError(kvs::dvi) << i18n("Cannot recognize format for font file %1", filename) << endl;
172 #endif
176 void TeXFontDefinition::reset()
178 if (font != 0) {
179 delete font;
180 font = 0;
183 if (macrotable != 0) {
184 delete [] macrotable;
185 macrotable = 0;
188 if (flags & FONT_LOADED) {
189 if (file != 0) {
190 fclose(file);
191 file = 0;
193 if (flags & FONT_VIRTUAL)
194 vf_table.clear();
197 filename.clear();
198 flags = TeXFontDefinition::FONT_IN_USE;
199 set_char_p = &dviRenderer::set_empty_char;
203 void TeXFontDefinition::setDisplayResolution(double _displayResolution_in_dpi)
205 displayResolution_in_dpi = _displayResolution_in_dpi;
206 if (font != 0)
207 font->setDisplayResolution();
211 /** mark_as_used marks the font, and all the fonts it referrs to, as
212 used, i.e. their FONT_IN_USE-flag is set. */
214 void TeXFontDefinition::mark_as_used()
216 #ifdef DEBUG_FONT
217 kDebug(kvs::dvi) << "TeXFontDefinition::mark_as_used()";
218 #endif
220 if (flags & TeXFontDefinition::FONT_IN_USE)
221 return;
223 flags |= TeXFontDefinition::FONT_IN_USE;
225 // For virtual fonts, also go through the list of referred fonts
226 if (flags & TeXFontDefinition::FONT_VIRTUAL) {
227 Q3IntDictIterator<TeXFontDefinition> it(vf_table);
228 while( it.current() ) {
229 it.current()->flags |= TeXFontDefinition::FONT_IN_USE;
230 ++it;
236 macro::macro()
238 pos = 0; /* address of first byte of macro */
239 end = 0; /* address of last+1 byte */
240 dvi_advance_in_units_of_design_size_by_2e20 = 0; /* DVI units to move reference point */
241 free_me = false;
245 macro::~macro()
247 if ((pos != 0L) && (free_me == true))
248 delete [] pos;