30
30
from qgis .core import *
31
31
32
32
from utilities import (
33
- unittest ,
34
- expectedFailure ,
35
33
getTempfilePath ,
36
34
getExecutablePath ,
37
35
)
42
40
suiteTests
43
41
)
44
42
45
- # look for Poppler, then muPDF PDF-to-image utility
46
- for util in ['pdftoppm' , 'mudraw' ]:
43
+ # PDF-to-image utility
44
+ # look for Poppler w/ Cairo, then muPDF
45
+ # * Poppler w/ Cairo renders correctly
46
+ # * Poppler w/o Cairo does not always correctly render vectors in PDF to image
47
+ # * muPDF renders correctly, but sightly shifts colors
48
+ for util in [
49
+ 'pdftocairo' ,
50
+ # 'mudraw',
51
+ ]:
47
52
PDFUTIL = getExecutablePath (util )
48
53
if PDFUTIL :
49
54
break
50
55
51
-
52
- def skip_if_not_pdf_util (): # skip test class decorator
53
- if PDFUTIL :
54
- return lambda func : func
55
- return unittest .skip ('\n PDF-to-image utility not found on PATH\n '
56
- 'Install Poppler or muPDF utilities\n \n ' )
56
+ # noinspection PyUnboundLocalVariable
57
+ if not PDFUTIL :
58
+ raise Exception ('PDF-to-image utility not found on PATH: '
59
+ 'install Poppler (with Cairo)' )
57
60
58
61
59
62
# output kind enum
@@ -214,28 +217,40 @@ def _get_composer_pdf_image(self, width, height, dpi):
214
217
return False , ''
215
218
216
219
filepath = getTempfilePath ('png' )
217
- # pdftoppm -singlefile -r 72 -x 0 -y 0 -W 600 -H 400 -png in.pdf pngbase
218
- # mudraw -o out.png -r 72 -w 600 -h 400 -c rgb[a] in.pdf
219
- if PDFUTIL == 'pdftoppm' :
220
+ # Poppler (pdftocairo or pdftoppm):
221
+ # PDFUTIL -png -singlefile -r 72 -x 0 -y 0 -W 420 -H 280 in.pdf pngbase
222
+ # muPDF (mudraw):
223
+ # PDFUTIL -c rgb[a] -r 72 -w 420 -h 280 -o out.png in.pdf
224
+ if PDFUTIL .strip ().endswith ('pdftocairo' ):
220
225
filebase = os .path .join (
221
226
os .path .dirname (filepath ),
222
227
os .path .splitext (os .path .basename (filepath ))[0 ]
223
228
)
224
229
call = [
225
- 'pdftoppm ' , '-singlefile' , '-r' , str (dpi ),
226
- '-x' , str ( 0 ) , '-y' , str ( 0 ) , '-W' , str (width ), '-H' , str (height ),
227
- '-png' , pdfpath , filebase
230
+ PDFUTIL , '-png ' , '-singlefile' , '-r' , str (dpi ),
231
+ '-x' , '0' , '-y' , '0' , '-W' , str (width ), '-H' , str (height ),
232
+ pdfpath , filebase
228
233
]
229
- elif PDFUTIL == 'mudraw' :
234
+ elif PDFUTIL . strip (). endswith ( 'mudraw' ) :
230
235
call = [
231
- 'mudraw' , '-c' , 'rgba' ,
236
+ PDFUTIL , '-c' , 'rgba' ,
232
237
'-r' , str (dpi ), '-w' , str (width ), '-h' , str (height ),
238
+ # '-b', '8',
233
239
'-o' , filepath , pdfpath
234
240
]
235
241
else :
236
242
return False , ''
237
243
238
- res = subprocess .check_call (call )
244
+ qDebug ("_get_composer_pdf_image call: {0}" .format (' ' .join (call )))
245
+ res = False
246
+ try :
247
+ subprocess .check_call (call )
248
+ res = True
249
+ except subprocess .CalledProcessError as e :
250
+ qDebug ("_get_composer_pdf_image failed!\n "
251
+ "cmd: {0}\n "
252
+ "returncode: {1}\n "
253
+ "message: {2}" .format (e .cmd , e .returncode , e .message ))
239
254
240
255
if not res :
241
256
os .unlink (filepath )
@@ -269,6 +284,7 @@ def checkTest(self, **kwargs):
269
284
mismatch = self ._Mismatches [self ._TestGroup ]
270
285
colortol = 0
271
286
if 'PAL_NO_COLORTOL' not in os .environ :
287
+ colortol = self ._ColorTol if self ._ColorTol else 0
272
288
if self ._TestGroup in self ._ColorTols :
273
289
colortol = self ._ColorTols [self ._TestGroup ]
274
290
self .assertTrue (* self .renderCheck (mismatch = mismatch ,
@@ -320,7 +336,29 @@ def setUp(self):
320
336
super (TestComposerSvgVsComposerPoint , self ).setUp ()
321
337
self ._TestKind = OutputKind .Svg
322
338
self .configTest ('pal_composer' , 'sp_img' )
323
- self ._Mismatch = 350
339
+ self ._ColorTol = 4
340
+
341
+
342
+ class TestComposerPdfPoint (TestComposerPointBase , TestPointBase ):
343
+
344
+ def setUp (self ):
345
+ """Run before each test."""
346
+ super (TestComposerPdfPoint , self ).setUp ()
347
+ self ._TestKind = OutputKind .Pdf
348
+ self .configTest ('pal_composer' , 'sp_pdf' )
349
+
350
+
351
+ class TestComposerPdfVsComposerPoint (TestComposerPointBase , TestPointBase ):
352
+ """
353
+ Compare only to composer image, which is already compared to canvas point
354
+ """
355
+ def setUp (self ):
356
+ """Run before each test."""
357
+ super (TestComposerPdfVsComposerPoint , self ).setUp ()
358
+ self ._TestKind = OutputKind .Pdf
359
+ self .configTest ('pal_composer' , 'sp_img' )
360
+ self ._Mismatch = 50
361
+ self ._ColorTol = 18
324
362
325
363
326
364
if __name__ == '__main__' :
@@ -331,11 +369,17 @@ def setUp(self):
331
369
sp_ivs = ['TestComposerImageVsCanvasPoint.' + t for t in st ['sp_vs_suite' ]]
332
370
sp_s = ['TestComposerSvgPoint.' + t for t in st ['sp_suite' ]]
333
371
sp_svs = ['TestComposerSvgVsComposerPoint.' + t for t in st ['sp_vs_suite' ]]
372
+ sp_p = ['TestComposerPdfPoint.' + t for t in st ['sp_suite' ]]
373
+ sp_pvs = ['TestComposerPdfVsComposerPoint.' + t for t in st ['sp_vs_suite' ]]
334
374
suite = []
375
+
335
376
# extended separately for finer control of PAL_SUITE (comment-out undesired)
336
377
suite .extend (sp_i )
337
378
suite .extend (sp_ivs )
338
379
suite .extend (sp_s )
339
380
suite .extend (sp_svs )
381
+ suite .extend (sp_p )
382
+ suite .extend (sp_pvs )
383
+
340
384
res = runSuite (sys .modules [__name__ ], suite )
341
385
sys .exit (not res .wasSuccessful ())
0 commit comments