forked from Kitware/VTK
/
RasterizePostScript.cmake
64 lines (53 loc) · 1.95 KB
/
RasterizePostScript.cmake
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
#
# Simple CMake -P script to rasterize a postscript file to a png image.
#
# Usage:
#
# cmake -DPSFILE=<postscript file>
# [ -DPNGFILE=<png file> ]
# -DGS_EXECUTABLE=<ghostscript executable>
# -DREMOVEPS=<bool>
# -P RasterizePostScript.cmake
#
# PSFILE is the input postscript file, PNGFILE is the output png file path. If
# PNGFILE is not specified, it will have the same basename as PSFILE, but with
# a .png extension.
#
# The script simple calls ghostscript with the relevant parameters to produce
# a 500x500 png. The path to the ghostscript executable for your system must be
# specified in GS_EXECUTABLE.
#
# If REMOVEPS is true, the postscript file will be removed upon successful
# conversion.
if(NOT PSFILE)
message(FATAL_ERROR "PSFILE is not specified!")
endif()
if(NOT GS_EXECUTABLE)
message(FATAL_ERROR "GS_EXECUTABLE is not specified!")
endif()
if(NOT PNGFILE)
get_filename_component(BASENAME "${PSFILE}" NAME_WE)
get_filename_component(PSPATH "${PSFILE}" PATH)
set(PNGFILE "${PSPATH}/${BASENAME}.png")
endif()
# Remove any old output (in case conversion fails)
file(REMOVE "${PNGFILE}")
# Get the bounding box
execute_process(COMMAND "${GS_EXECUTABLE}" -sSAFER -sBATCH -sNOPAUSE -sDEVICE=bbox "${PSFILE}"
RESULT_VARIABLE EXITCODE ERROR_VARIABLE BBOXOUT)
if(NOT ${EXITCODE} EQUAL 0)
message(FATAL_ERROR "GhostScript exited with status ${EXITCODE}:\n${BBOXOUT}")
endif()
string(REGEX MATCH "%%BoundingBox:[ ]+[0-9-]+[ ]+[0-9-]+[ ]+[0-9]+[ ]+[0-9]+"
BBOX ${BBOXOUT})
string(REGEX REPLACE
"^%%BoundingBox:[ ]+[0-9-]+[ ]+[0-9-]+[ ]+([0-9]+)[ ]+([0-9]+)"
"\\1x\\2" BBOX ${BBOX})
execute_process(COMMAND "${GS_EXECUTABLE}" -sSAFER -sBATCH -sNOPAUSE -sDEVICE=png16m "-sOutputFile=${PNGFILE}" -g${BBOX} "${PSFILE}"
RESULT_VARIABLE EXITCODE OUTPUT_VARIABLE ERRORSTR)
if(NOT ${EXITCODE} EQUAL 0)
message(FATAL_ERROR "GhostScript exited with status ${EXITCODE}:\n${ERRORSTR}")
endif()
if(REMOVEPS)
file(REMOVE "${PSFILE}")
endif()