Skip to content
Snippets Groups Projects
Commit 7c18cd0e authored by Timo Koch's avatar Timo Koch
Browse files

[cmake] Add new function to generate images with inkscape creating a target

The old inkscape_generate_png_from_svg is deprecated.
The new dune_create_inkscape_image_converter_target uses named arguments
and creates a user-defined target for generated images when built.
parent 10edaf3a
Branches
Tags
1 merge request!1065[cmake] Add new function to generate images with inkscape creating a target
Pipeline #41991 passed with warnings
......@@ -20,11 +20,45 @@
#
# dpi value for the generated image (default: 90)
#
# TODO Switch to named arguments!
#
#
# .. cmake_function:: dune_create_inkscape_image_converter_target
#
# Creates a target that when built, converts svg images to png images using inkscape
#
# .. cmake_param:: TARGET
# :single:
#
# Name of the target to be created.
#
# .. cmake_param:: OUTPUT_DIR
# :single:
# :required:
#
# The output directory for the generated png files.
# Defaults to the current build directory.
#
# .. cmake_param:: IMAGES
# :multi:
# :required:
#
# The files that should be converted.
#
# .. cmake_param:: DPI
# :single:
#
# dpi value for the generated image (default: 90)
#
# .. cmake_param:: ALL
# :option:
#
# If given, will add the created target to the all target
#
include_guard(GLOBAL)
function(inkscape_generate_png_from_svg)
message(DEPRECATION "inkscape_generate_png_from_svg is deprecated (will be removed after release 2.9). "
"Use dune_create_inkscape_image_converter_target.")
if(NOT INKSCAPE)
return()
endif()
......@@ -61,3 +95,57 @@ function(inkscape_generate_png_from_svg)
message(STATUS "Generate PNG from SVG in ${INKSCAPE_OUTPUT_DIR} - skipped")
endif()
endfunction()
function(dune_create_inkscape_image_converter_target)
set(OPTION ALL)
set(SINGLE TARGET OUTPUT_DIR DPI)
set(MULTI IMAGES)
cmake_parse_arguments(INKSCAPE_CONV "${OPTION}" "${SINGLE}" "${MULTI}" ${ARGN})
if(NOT INKSCAPE_CONV_TARGET)
message(FATAL_ERROR "Specifying a target name by setting TARGET is required.")
endif()
if(TARGET ${INKSCAPE_CONV_TARGET})
message(FATAL_ERROR "UseInkscape: provided TARGET already is an existing target. This function creates the target. Please provide a unique name.")
endif()
# we always create the target even if it stays empty
# (might happen because inkscape is not found or the image list is empty)
if(INKSCAPE_CONV_ALL)
add_custom_target(${INKSCAPE_CONV_TARGET} ALL)
else()
add_custom_target(${INKSCAPE_CONV_TARGET})
endif()
if(NOT INKSCAPE)
message(STATUS "Inkscape not found so no images will be converted")
return()
endif()
if(NOT INKSCAPE_CONV_OUTPUT_DIR)
set(INKSCAPE_CONV_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
endif()
if(NOT INKSCAPE_CONV_DPI)
set(INKSCAPE_CONV_DPI 90)
endif()
foreach(_output_png ${INKSCAPE_CONV_IMAGES})
string(REGEX REPLACE "\\.[a-zA-Z]+" ".svg" _input_svg ${_output_png})
if(INKSCAPE_NEW_VERSION)
add_custom_command(TARGET ${INKSCAPE_CONV_TARGET} PRE_BUILD
COMMAND ${INKSCAPE} --export-dpi=${INKSCAPE_CONV_DPI} --export-type=png --export-filename=${_output_png} ${CMAKE_CURRENT_SOURCE_DIR}/${_input_svg}
WORKING_DIRECTORY ${INKSCAPE_CONV_OUTPUT_DIR}
COMMENT "Generating ${_output_png} from ${_input_svg} with inkscape"
)
else()
add_custom_command(TARGET ${INKSCAPE_CONV_TARGET} PRE_BUILD
COMMAND ${INKSCAPE} -z --export-dpi=${INKSCAPE_CONV_DPI} -e ${_output_png} ${CMAKE_CURRENT_SOURCE_DIR}/${_input_svg}
WORKING_DIRECTORY ${INKSCAPE_CONV_OUTPUT_DIR}
COMMENT "Generating ${_output_png} from ${_input_svg} with inkscape"
)
endif()
endforeach()
endfunction()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment