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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
/**
* Copyright (C) 2000-2003 the KGhostView authors. See file AUTHORS.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kpswidget.h"
#include <stdlib.h>
#include <math.h>
#include <tqstringlist.h>
#include <tdeapplication.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kprocess.h>
#include "configuration.h"
#include <X11/Xlib.h>
#include <X11/Xatom.h>
int handler( Display* d, XErrorEvent* e )
{
char msg[80], req[80], number[80];
XGetErrorText( d, e->error_code, msg, sizeof( msg ) );
sprintf( number, "%d", e->request_code );
XGetErrorDatabaseText( d, "XRequest", number, "<unknown>",
req, sizeof( req ) );
return 0;
}
int orientation2angle( CDSC_ORIENTATION_ENUM orientation )
{
Q_ASSERT( orientation != CDSC_ORIENT_UNKNOWN );
int angle = 0;
switch( orientation )
{
case CDSC_ORIENT_UNKNOWN: break; // Catched by Q_ASSERT
case CDSC_PORTRAIT: angle = 0; break;
case CDSC_LANDSCAPE: angle = 90; break;
case CDSC_UPSIDEDOWN: angle = 180; break;
case CDSC_SEASCAPE: angle = 270; break;
}
return angle;
}
TQCString palette2String( Configuration::EnumPalette::type palette )
{
TQCString str;
switch( palette )
{
case Configuration::EnumPalette::Color: str = "Color"; break;
case Configuration::EnumPalette::Grayscale: str = "Grayscale"; break;
case Configuration::EnumPalette::Monochrome: str = "Monochrome"; break;
default: kdWarning( 4500 ) << "palette2String(): unkown palette" << endl;
str = "Color";
}
return str;
}
KPSWidget::KPSWidget( TQWidget* parent, const char* name ) :
TQWidget ( parent, name ),
_gsWindow ( None ),
_usePipe ( false ),
_doubleBuffer ( false ),
_ghostscriptDirty ( false ),
_orientation ( CDSC_PORTRAIT ),
_magnification ( 1 ),
_palette ( Configuration::EnumPalette::Color ),
_widgetDirty ( true ),
_process ( 0 ),
_buffer ( 0 ),
_stdinReady ( false ),
_interpreterBusy ( false ),
_interpreterReady ( false )
{
XSetErrorHandler( handler );
// Create the Atoms used to communicate with Ghostscript.
const char* const atomNames[] = { "GHOSTVIEW", "GHOSTVIEW_COLORS",
"NEXT", "PAGE", "DONE" };
XInternAtoms( x11Display(), const_cast<char**>( atomNames ),
5, false, _atoms );
// readSettings() TODO
}
KPSWidget::~KPSWidget()
{
if ( _buffer ) operator delete( _buffer );
stopInterpreter();
}
bool KPSWidget::isInterpreterReady() const
{
return isInterpreterRunning() && _interpreterReady;
}
bool KPSWidget::isInterpreterBusy() const
{
return _interpreterBusy;
}
bool KPSWidget::isInterpreterRunning() const
{
return ( _process && _process->isRunning() );
}
bool KPSWidget::nextPage()
{
if( !isInterpreterReady() )
return false;
if( _gsWindow == None ) {
kdDebug(4500) << "communication window unknown!" << endl;
return false;
}
_interpreterReady = false;
_interpreterBusy = true;
setCursor( waitCursor );
XEvent e;
e.xclient.type = ClientMessage;
e.xclient.display = x11Display();
e.xclient.window = _gsWindow;
e.xclient.message_type = _atoms[NEXT];
e.xclient.format = 32;
XSendEvent( x11Display(), _gsWindow, false, 0, &e );
XFlush( x11Display() );
return true;
}
void KPSWidget::clear()
{
//_backgroundPixmap.fill();
}
bool KPSWidget::sendPS( FILE* fp, unsigned int begin, unsigned int end )
{
kdDebug(4500) << "KPSWidget::sendPS" << endl;
if( !isInterpreterRunning() )
return false;
// Create a new record to add to the queue.
_inputQueue.push( Record( fp, begin, end - begin ) );
// Start processing the queue.
if( _stdinReady )
gs_input(_process);
return true;
}
void KPSWidget::setGhostscriptPath( const TQString& path )
{
kdDebug() << "KPSWidget::setGhostscriptPath( " << path << " )" << endl;
if( _ghostscriptPath != path )
{
_ghostscriptPath = path;
stopInterpreter();
_ghostscriptDirty = true;
}
}
void KPSWidget::setGhostscriptArguments( const TQStringList& arguments )
{
if( _ghostscriptArguments != arguments )
{
_ghostscriptArguments = arguments;
stopInterpreter();
_ghostscriptDirty = true;
}
}
void KPSWidget::setFileName( const TQString& fileName, bool usePipe )
{
if(( _fileName != fileName ) || (_usePipe != usePipe))
{
_usePipe = usePipe;
_fileName = fileName;
stopInterpreter();
_ghostscriptDirty = true;
}
}
void KPSWidget::setOrientation( CDSC_ORIENTATION_ENUM orientation )
{
if( _orientation != orientation )
{
_orientation = orientation;
stopInterpreter();
_widgetDirty = true;
}
}
void KPSWidget::setBoundingBox( const KDSCBBOX& boundingBox )
{
if( _boundingBox != boundingBox )
{
_boundingBox = boundingBox;
stopInterpreter();
_widgetDirty = true;
}
}
void KPSWidget::setMagnification( double magnification )
{
if( kAbs( magnification - _magnification ) > 0.0001 )
{
_magnification = magnification;
stopInterpreter();
_widgetDirty = true;
}
}
void KPSWidget::setPalette( Configuration::EnumPalette::type palette )
{
if( _palette != palette )
{
_palette = palette;
stopInterpreter();
_widgetDirty = true;
}
}
void KPSWidget::setDoubleBuffering( bool db )
{
if( _doubleBuffer != db )
{
_doubleBuffer = db;
stopInterpreter();
_widgetDirty = true;
}
}
namespace {
/* Rounding up is better than normal rounding because it is better to have a pixel too many than one too little.
* If we have one too many, no one will notice. If we have one too little, gs complains.
*
* I have a file which isn't displayed (gs error) without this fix.
*/
inline int round_up( double x )
{
return static_cast<int>( ceil( x ) );
}
}
void KPSWidget::setupWidget()
{
if( !_widgetDirty )
return;
Q_ASSERT( orientation() != CDSC_ORIENT_UNKNOWN );
const float dpiX = _magnification * TQT_TQPAINTDEVICE(this)->x11AppDpiX();
const float dpiY = _magnification * TQT_TQPAINTDEVICE(this)->x11AppDpiY();
int newWidth = 0, newHeight = 0;
if( orientation() == CDSC_PORTRAIT || orientation() == CDSC_UPSIDEDOWN )
{
newWidth = round_up( boundingBox().width() * dpiX / 72.0 );
newHeight = round_up( boundingBox().height() * dpiY / 72.0 );
}
else
{
newWidth = round_up( boundingBox().height() * dpiX / 72.0 );
newHeight = round_up( boundingBox().width() * dpiY / 72.0 );
}
if( newWidth != width() || newHeight != height() )
{
setEraseColor( white );
setFixedSize( newWidth, newHeight );
kapp->processEvents();
_backgroundPixmap.resize( size() );
_backgroundPixmap.fill( white );
// The line below is needed to work around certain "features" of styles such as liquid
// see bug:61711 for more info (LPC, 20 Aug '03)
setBackgroundOrigin( TQWidget::WidgetOrigin );
setErasePixmap( _backgroundPixmap );
}
char data[512];
sprintf( data, "%ld %d %d %d %d %d %g %g",
( _doubleBuffer ? 0 : _backgroundPixmap.handle() ),
orientation2angle( orientation() ),
boundingBox().llx(), boundingBox().lly(),
boundingBox().urx(), boundingBox().ury(),
dpiX, dpiY );
XChangeProperty( x11Display(), winId(),
_atoms[GHOSTVIEW],
XA_STRING, 8, PropModeReplace,
(unsigned char*) data, strlen( data ) );
sprintf( data, "%s %d %d",
palette2String( _palette ).data(),
(int)BlackPixel( x11Display(), DefaultScreen( x11Display() ) ),
(int)WhitePixel( x11Display(), DefaultScreen( x11Display() ) ) );
XChangeProperty( x11Display(), winId(),
_atoms[GHOSTVIEW_COLORS],
XA_STRING, 8, PropModeReplace,
(unsigned char*) data, strlen( data ) );
// Make sure the properties are updated immediately.
XSync( x11Display(), false );
repaint();
_widgetDirty = false;
}
bool KPSWidget::startInterpreter()
{
setupWidget();
_process = new TDEProcess;
if ( _doubleBuffer ) _process->setEnvironment( "GHOSTVIEW", TQString( "%1 %2" ).arg( winId() ).arg( _backgroundPixmap.handle() ) );
else _process->setEnvironment( "GHOSTVIEW", TQString::number( winId() ) );
*_process << _ghostscriptPath.local8Bit();
*_process << _ghostscriptArguments;
if( _usePipe )
*_process <<
// The following two lines are their to ensure that we are allowed to read _fileName
"-dDELAYSAFER" << "-sInputFile="+_fileName << "-c" <<
"<< /PermitFileReading [ InputFile ] /PermitFileWriting [] /PermitFileControl [] >> setuserparams .locksafe" <<
"-";
else
*_process << _fileName << "-c" << "quit";
connect( _process, TQT_SIGNAL( processExited( TDEProcess* ) ),
this, TQT_SLOT( slotProcessExited( TDEProcess* ) ) );
connect( _process, TQT_SIGNAL( receivedStdout( TDEProcess*, char*, int ) ),
this, TQT_SLOT( gs_output( TDEProcess*, char*, int ) ) );
connect( _process, TQT_SIGNAL( receivedStderr( TDEProcess*, char*, int ) ),
this, TQT_SLOT( gs_output( TDEProcess*, char*, int ) ) );
connect( _process, TQT_SIGNAL( wroteStdin( TDEProcess*) ),
this, TQT_SLOT( gs_input( TDEProcess* ) ) );
kapp->flushX();
// Finally fire up the interpreter.
kdDebug(4500) << "KPSWidget: starting interpreter" << endl;
if( _process->start( TDEProcess::NotifyOnExit,
_usePipe ? TDEProcess::All : TDEProcess::AllOutput ) )
{
_interpreterBusy = true;
setCursor( waitCursor );
_stdinReady = true;
_interpreterReady = false;
_ghostscriptDirty = false;
return true;
}
else
{
KMessageBox::error( this,
i18n( "Could not start Ghostscript. This is most likely "
"caused by an incorrectly specified interpreter." ) );
return false;
}
}
void KPSWidget::stopInterpreter()
{
kdDebug(4500) << "KPSWidget::stopInterpreter()" << endl;
// if( !_interpreterBusy ) return;
if( isInterpreterRunning() )
_process->kill( SIGHUP );
_process = 0;
while ( !_inputQueue.empty() ) _inputQueue.pop();
_interpreterBusy = false;
unsetCursor();
}
void KPSWidget::interpreterFailed()
{
stopInterpreter();
}
void KPSWidget::slotProcessExited( TDEProcess* process )
{
kdDebug(4500) << "KPSWidget: process exited" << endl;
if ( process == _process )
{
kdDebug( 4500 ) << "KPSWidget::slotProcessExited(): looks like it was not a clean exit." << endl;
if ( process->normalExit() ) {
emit ghostscriptError( TQString( i18n( "Exited with error code %1." ).arg( process->exitStatus() ) ) );
} else {
emit ghostscriptError( TQString( i18n( "Process killed or crashed." ) ) );
}
_process = 0;
stopInterpreter();
unsetCursor();
}
}
void KPSWidget::gs_output( TDEProcess*, char* buffer, int len )
{
emit output( buffer, len );
}
void KPSWidget::gs_input( TDEProcess* process )
{
kdDebug(4500) << "KPSWidget::gs_input" << endl;
if (process != _process)
{
kdDebug(4500) << "KPSWidget::gs_input(): process != _process" << endl;
return;
}
_stdinReady = true;
while( ! _inputQueue.empty() && _inputQueue.front().len == 0 ) _inputQueue.pop();
if( _inputQueue.empty() ) {
_interpreterReady = true;
return;
}
Record& current = _inputQueue.front();
if ( fseek( current.fp, current.begin, SEEK_SET ) ) {
kdDebug(4500) << "KPSWidget::gs_input(): seek failed!" << endl;
interpreterFailed();
return;
}
Q_ASSERT( current.len > 0 );
const unsigned buffer_size = 4096;
if ( !_buffer ) _buffer = static_cast<char*>( operator new( buffer_size ) );
const int bytesRead = fread( _buffer, sizeof (char),
TQMIN( buffer_size, current.len ),
current.fp );
if( bytesRead > 0 )
{
current.begin += bytesRead;
current.len -= bytesRead;
if( process && process->writeStdin( _buffer, bytesRead ) )
_stdinReady = false;
else
interpreterFailed();
}
else
interpreterFailed();
}
void KPSWidget::readSettings()
{
setGhostscriptPath( Configuration::interpreter() );
TQStringList arguments;
if( Configuration::antialiasing() )
arguments = TQStringList::split( " ", Configuration::antialiasingArguments() );
else
arguments = TQStringList::split( " ", Configuration::nonAntialiasingArguments() );
if( !Configuration::platformFonts() )
arguments << "-dNOPLATFONTS";
arguments << "-dNOPAUSE" << "-dQUIET" << "-dSAFER" << "-dPARANOIDSAFER";
setGhostscriptArguments( arguments );
setPalette( static_cast<Configuration::EnumPalette::type>( Configuration::palette() ) );
}
bool KPSWidget::x11Event( XEvent* e )
{
if( e->type == ClientMessage )
{
_gsWindow = e->xclient.data.l[0];
if( e->xclient.message_type == _atoms[PAGE] )
{
kdDebug(4500) << "KPSWidget: received PAGE" << endl;
_interpreterBusy = false;
unsetCursor();
emit newPageImage( _backgroundPixmap );
if ( _doubleBuffer ) setErasePixmap( _backgroundPixmap );
return true;
}
else if( e->xclient.message_type == _atoms[DONE] )
{
kdDebug(4500) << "KPSWidget: received DONE" << endl;
stopInterpreter();
return true;
}
}
return TQWidget::x11Event( e );
}
#include "kpswidget.moc"
|