summaryrefslogtreecommitdiffstats
path: root/kdvi/dviFile.cpp
blob: db365482a9f191372cfc362ee450b4ba3af7fc25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Copyright (c) 1994 Paul Vojta.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * NOTE:
 *	xdvi is based on prior work as noted in the modification history, below.
 */

/*
 * DVI previewer for X.
 *
 * Eric Cooper, CMU, September 1985.
 *
 * Code derived from dvi-imagen.c.
 *
 * Modification history:
 * 1/1986	Modified for X.10	--Bob Scheifler, MIT LCS.
 * 7/1988	Modified for X.11	--Mark Eichin, MIT
 * 12/1988	Added 'R' option, toolkit, magnifying glass
 *					--Paul Vojta, UC Berkeley.
 * 2/1989	Added tpic support	--Jeffrey Lee, U of Toronto
 * 4/1989	Modified for System V	--Donald Richardson, Clarkson Univ.
 * 3/1990	Added VMS support	--Scott Allendorf, U of Iowa
 * 7/1990	Added reflection mode	--Michael Pak, Hebrew U of Jerusalem
 * 1/1992	Added greyscale code	--Till Brychcy, Techn. Univ. Muenchen
 *					  and Lee Hetherington, MIT
 * 4/1994	Added DPS support, bounding box
 *					--Ricardo Telichevesky
 *					  and Luis Miguel Silveira, MIT RLE.
 */

#include <config.h>

#include <kdebug.h>
#include <klocale.h>
#include <ktempfile.h>
#include <tqdir.h>
#include <tqfileinfo.h>
#include <stdlib.h>
#include <kprocio.h>

extern "C" {
#include "dvi.h"
}

#include "../kviewshell/pageSize.h"
#include "dviFile.h"
#include "fontpool.h"
#include "xdvi.h"


dvifile::dvifile(const dvifile *old, fontPool *fp) 
{
  errorMsg     = TQString();
  errorCounter = 0;
  page_offset  = 0;
  suggestedPageSize = 0;
  numberOfExternalPSFiles = 0;
  numberOfExternalNONPSFiles = 0;
  sourceSpecialMarker = old->sourceSpecialMarker;

  dviData = old->dviData.copy();

  filename = old->filename;
  size_of_file = old->size_of_file;
  end_pointer = dvi_Data()+size_of_file; 
  if (dvi_Data() == 0) {
    kdError(4300) << "Not enough memory to copy the DVI-file." << endl;
    return;
  }

  font_pool = fp;
  filename = old->filename;
  generatorString = old->generatorString;
  total_pages = old->total_pages;


  tn_table.clear();
  process_preamble();
  find_postamble();
  read_postamble();
  prepare_pages();
}


void dvifile::process_preamble()
{
  command_pointer = dvi_Data();
  
  TQ_UINT8 magic_number = readUINT8();
  if (magic_number != PRE) {
    errorMsg = i18n("The DVI file does not start with the preamble.");
    return;
  }
  magic_number =  readUINT8();
  if (magic_number != 2) {
    errorMsg = i18n("The DVI file contains the wrong version of DVI output for this program. "
		    "Hint: If you use the typesetting system Omega, you have to use a special "
		    "program, such as oxdvi.");
    return;
  }
  
  /** numerator, denominator and the magnification value that describe
      how many centimeters there are in one TeX unit, as explained in
      section A.3 of the DVI driver standard, Level 0, published by
      the TUG DVI driver standards committee. */
  TQ_UINT32 numerator     = readUINT32();
  TQ_UINT32 denominator   = readUINT32();
  _magnification = readUINT32();
  
  cmPerDVIunit =  (double(numerator) / double(denominator)) * (double(_magnification) / 1000.0) * (1.0 / 1e5);
  
  
  // Read the generatorString (such as "TeX output ..." from the
  // DVI-File). The variable "magic_number" holds the length of the
  // string.
  char	job_id[300];
  magic_number = readUINT8();
  strncpy(job_id, (char *)command_pointer, magic_number);
  job_id[magic_number] = '\0';
  generatorString = job_id;
}


/** find_postamble locates the beginning of the postamble and leaves
    the file ready to start reading at that location. */

void dvifile::find_postamble()
{
  // Move backwards through the TRAILER bytes
  command_pointer = dvi_Data() + size_of_file - 1;
  while((*command_pointer == TRAILER) && (command_pointer > dvi_Data()))
    command_pointer--;
  if (command_pointer == dvi_Data()) {
    errorMsg = i18n("The DVI file is badly corrupted. KDVI was not able to find the postamble.");
    return;
  }

  // And this is finally the pointer to the beginning of the postamble
  command_pointer -= 4;
  beginning_of_postamble = readUINT32();
  command_pointer  = dvi_Data() + beginning_of_postamble;
}


void dvifile::read_postamble()
{
  TQ_UINT8 magic_byte = readUINT8();
  if (magic_byte != POST) {
    errorMsg = i18n("The postamble does not begin with the POST command.");
    return;
  }
  last_page_offset = readUINT32();

  // Skip the numerator, denominator and magnification, the largest
  // box height and width and the maximal depth of the stack. These
  // are not used at the moment.
  command_pointer += 4 + 4 + 4 + 4 + 4 + 2;

  // The number of pages is more interesting for us.
  total_pages  = readUINT16();

  // As a next step, read the font definitions.
  TQ_UINT8 cmnd = readUINT8();
  while (cmnd >= FNTDEF1 && cmnd <= FNTDEF4) {
    TQ_UINT32 TeXnumber = readUINT(cmnd-FNTDEF1+1);
    TQ_UINT32 checksum  = readUINT32(); // Checksum of the font, as found by TeX in the TFM file

    // Read scale and design factor, and the name of the font. All
    // these are explained in section A.4 of the DVI driver standard,
    // Level 0, published by the TUG DVI driver standards committee
    TQ_UINT32 scale     = readUINT32(); 
    TQ_UINT32 design    = readUINT32(); 
    TQ_UINT16 len       = readUINT8() + readUINT8(); // Length of the font name, including the directory name
    char *fontname  = new char[len + 1];
    strncpy(fontname, (char *)command_pointer, len );
    fontname[len] = '\0';
    command_pointer += len;
    
#ifdef DEBUG_FONTS
    kdDebug(4300) << "Postamble: define font \"" << fontname << "\" scale=" << scale << " design=" << design << endl;
#endif
    
    // According to section A.4 of the DVI driver standard, this font
    // shall be enlarged by the following factor before it is used.
    double enlargement_factor = (double(scale) * double(_magnification))/(double(design) * 1000.0);
    
    if (font_pool != 0) {
      TeXFontDefinition *fontp = font_pool->appendx(fontname, checksum, scale, enlargement_factor);
      
      // Insert font in dictionary and make sure the dictionary is big
      // enough.
      if (tn_table.size()-2 <= tn_table.count())
	// Not quite optimal. The size of the dictionary should be a
	// prime for optimal performance. I don't care.
	tn_table.resize(tn_table.size()*2); 
      tn_table.insert(TeXnumber, fontp);
    }
    
    // Read the next command
    cmnd = readUINT8();
  }
  
  if (cmnd != POSTPOST) {
    errorMsg = i18n("The postamble contained a command other than FNTDEF.");
    return;
  }

  // Now we remove all those fonts from the memory which are no longer
  // in use.
  if (font_pool != 0)
    font_pool->release_fonts();
}


void dvifile::prepare_pages()
{
#ifdef DEBUG_DVIFILE
  kdDebug(4300) << "prepare_pages" << endl;
#endif

  if (page_offset.resize(total_pages+1) == false) {
    kdError(4300) << "No memory for page list!" << endl;
    return;
  }
  for(int i=0; i<=total_pages; i++)
    page_offset[i] = 0;
  

  page_offset[total_pages] = beginning_of_postamble;
  TQ_UINT16 i               = total_pages-1;
  page_offset[i]           = last_page_offset;

  // Follow back pointers through pages in the DVI file, storing the
  // offsets in the page_offset table.
   while (i > 0) {
    command_pointer  = dvi_Data() + page_offset[i--];
    if (readUINT8() != BOP) {
      errorMsg = i18n("The page %1 does not start with the BOP command.").tqarg(i+1);
      return;
    }
    command_pointer += 10 * 4;
    page_offset[i] = readUINT32();
    if ((dvi_Data()+page_offset[i] < dvi_Data())||(dvi_Data()+page_offset[i] > dvi_Data()+size_of_file))
      break;
  }
}


dvifile::dvifile(const TQString& fname, fontPool* pool)
{
#ifdef DEBUG_DVIFILE
  kdDebug(4300) << "init_dvi_file: " << fname << endl;
#endif

  errorMsg     = TQString();
  errorCounter = 0;
  page_offset  = 0;
  suggestedPageSize = 0;
  numberOfExternalPSFiles = 0;
  numberOfExternalNONPSFiles = 0;
  font_pool    = pool;
  sourceSpecialMarker = true;
  
  TQFile file(fname);
  filename = file.name();
  file.open( IO_ReadOnly );
  size_of_file = file.size();
  dviData.resize(size_of_file);
  // Sets the end pointer for the bigEndianByteReader so that the
  // whole memory buffer is readable
  end_pointer = dvi_Data()+size_of_file; 
  if (dvi_Data() == 0) {
    kdError() << i18n("Not enough memory to load the DVI-file.");
    return;
  }
  file.readBlock((char *)dvi_Data(), size_of_file);
  file.close();
  if (file.status() != IO_Ok) {
    kdError() << i18n("Could not load the DVI-file.");
    return;
  }

  tn_table.clear();
  
  process_preamble();
  find_postamble();
  read_postamble();
  prepare_pages();

  return;
}


dvifile::~dvifile()
{
#ifdef DEBUG_DVIFILE
  kdDebug(4300) << "destroy dvi-file" << endl;
#endif

  // Delete converted PDF files
  TQMap<TQString, TQString>::Iterator it;
  for ( it = convertedFiles.begin(); it != convertedFiles.end(); ++it ) 
    TQFile::remove(it.data());

  if (suggestedPageSize != 0)
    delete suggestedPageSize;
  if (font_pool != 0)
    font_pool->mark_fonts_as_unused();
}


void dvifile::renumber()
{
  dviData.detach();

  // Write the page number to the file, taking good care of byte
  // orderings.
  int wordSize;
  bool bigEndian;
  qSysInfo (&wordSize, &bigEndian);

  for(TQ_UINT32 i=1; i<=total_pages; i++) {
    TQ_UINT8 *ptr = dviData.data() + page_offset[i-1]+1;
    TQ_UINT8 *num = (TQ_UINT8 *)&i;
    for(TQ_UINT8 j=0; j<4; j++)
      if (bigEndian) {
	*(ptr++) = num[0];
	*(ptr++) = num[1];
	*(ptr++) = num[2];
	*(ptr++) = num[3];
      } else {
	*(ptr++) = num[3];
	*(ptr++) = num[2];
	*(ptr++) = num[1];
	*(ptr++) = num[0];
      }
  }
}


TQString dvifile::convertPDFtoPS(const TQString &PDFFilename)
{
  // Check if the PDFFile is known
  TQMap<TQString, TQString>::Iterator it =  convertedFiles.tqfind(PDFFilename);
  if (it != convertedFiles.end()) {
    // PDF-File is known. Good.
    return it.data();
  }

  // Get the name of a temporary file
  KTempFile tmpfile(TQString(), ".ps");
  TQString convertedFileName = tmpfile.name();
  tmpfile.close();
  tmpfile.unlink();

  // Use pdf2ps to do the conversion
  KProcIO proc;
  proc << "pdf2ps" << PDFFilename << convertedFileName;
  if (proc.start(KProcess::Block) == false) 
    convertedFileName = TQString(); // Indicates that conversion failed, won't try again.
  if (!TQFile::exists(convertedFileName))
    convertedFileName = TQString(); // Indicates that conversion failed, won't try again.

  // Save name of converted file to buffer, so PDF file won't be
  // converted again, and files can be deleted when *this is
  // deconstructed.
  convertedFiles[PDFFilename] = convertedFileName;

  return convertedFileName;
}


bool dvifile::saveAs(const TQString &filename)
{
  if (dvi_Data() == 0)
    return false;

  TQFile out(filename);
  if (out.open( IO_Raw|IO_WriteOnly ) == false)
    return false;
  if (out.writeBlock ( (char *)(dvi_Data()), size_of_file ) == -1)
    return false;
  out.close();
  return true;
}