Skip to content

Commit

Permalink
Add gimp script-fu support
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Feb 6, 2025
1 parent 4d22dda commit 5be209f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ SET(LMMS_SOURCE_DIR ${CMAKE_SOURCE_DIR})
# Import of windows.h breaks min()/max()
ADD_DEFINITIONS(-DNOMINMAX)

file(REMOVE_RECURSE "${CMAKE_BINARY_DIR}/branding")
file(REMOVE_RECURSE "${CMAKE_BINARY_DIR}/lmms.iconset")

include(SvgRecolor)
set(svg_green "#27ab5f;#249a56;#34d07b;opacity=\".1\" fill=\"#fff\"")
set(svg_blue "#3992cb;#2b6fc5;#62a8d4;opacity=\".1\" fill=\"#fff\"")
Expand Down
14 changes: 14 additions & 0 deletions cmake/branding/gimp_convert.scm.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; Gimp scheme script: Uses script-fu to convert .svg to .png
; All @variables@ need to be manually replaced before calling
; Usage: gimp -b gimp_convert.scm
(let* (
(image (car (file-svg-load RUN-NONINTERACTIVE "@source_file@" "" @resolution@ @width@ @height@ 0)))
(drawable (car (gimp-image-get-active-layer image)))
(filename "@png_out@")
)
(if (not image)
(gimp-message "Error loading SVG: @source_file@")
(begin
(gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
(gimp-message "Image saved: @png_out@")
(gimp-image-delete image))))
68 changes: 61 additions & 7 deletions cmake/modules/SvgConvert.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ function(svg_convert size_list source_file output_pattern)
set(working_directory "${CMAKE_CURRENT_BINARY_DIR}")
endif()

if(CPACK_SOURCE_DIR)
# Support calling from CPACK
set(source_directory "${CPACK_SOURCE_DIR}")
else()
set(source_directory "${CMAKE_SOURCE_DIR}")
endif()

if(DEFINED ENV{WANT_DEBUG_BRANDING})
set(WANT_DEBUG_BRANDING ON)
endif()
Expand All @@ -26,9 +33,25 @@ function(svg_convert size_list source_file output_pattern)
message(FATAL_ERROR "SVG file does not exist: ${source_file}")
endif()
#find_best_tool()
rsvg_convert("${source_file}" ${size} "${working_directory}" "${output_pattern}" ${COMMAND_ECHO} png_rendered)
#rsvg_convert("${source_file}" ${size} "${working_directory}" "${output_pattern}" ${COMMAND_ECHO} png_rendered)
#inkscape_convert("${source_file}" ${size} "${working_directory}" "${output_pattern}" ${COMMAND_ECHO} png_rendered)
message(STATUS " svg_convert: ${source_file} --> ${png_rendered}")


foreach(tool rsvg;inkscape;gimp) # FIXME: Remove, this is just for unit testing

cmake_language(CALL ${tool}_convert "${source_file}"
${size}
"${working_directory}"
"${output_pattern}"
${COMMAND_ECHO}
png_rendered
)
message(STATUS " ${tool}_convert: ${source_file} --> ${png_rendered}")


endforeach() # FIXME: Remove, this is just for unit teting


endforeach()
endfunction()

Expand All @@ -50,12 +73,13 @@ function(inkscape_convert source_file size working_directory pattern command_ech
COMMAND_ERROR_IS_FATAL ANY
ERROR_VARIABLE inkscape_output)

# Inkscpae doesn't properly return error codes, parse output instead
if("${inkscape_output}" MATCHES "(Failed|failed|error)")
message(FATAL_ERROR "${inkscape_output}")
endif()
# Inkscape doesn't properly set error codes
if(NOT EXISTS "${png_out}")
# Blindly dump whatever was on stderr
message(FATAL_ERROR " inkscape_convert: ${png_out} was not created:\n${inkscape_output}")
endif()

set(${result} "${png_out}" PARENT_SCOPE)
set(${result} "${png_out}" PARENT_SCOPE)
endfunction()

# RSVG is much faster than inkscape
Expand All @@ -73,6 +97,36 @@ function(rsvg_convert source_file size working_directory pattern command_echo re
set(${result} "${png_out}" PARENT_SCOPE)
endfunction()

function(gimp_convert source_file size working_directory pattern command_echo result)
patternize_file("${source_file}" ${size} "${pattern}" scaled_size png_out)
# Use scheme language for gimp batch conversion

file(READ "${source_directory}/cmake/branding/gimp_convert.scm.in" gimp_lisp)
string(REPLACE "@source_file@" "${source_file}" gimp_lisp "${gimp_lisp}")
string(REPLACE "@width@" "${scaled_size}" gimp_lisp "${gimp_lisp}")
string(REPLACE "@height@" "${scaled_size}" gimp_lisp "${gimp_lisp}")
string(REPLACE "@resolution@" "72" gimp_lisp "${gimp_lisp}")
string(REPLACE "@png_out@" "${png_out}" gimp_lisp "${gimp_lisp}")

execute_process(COMMAND gimp
--no-interface
--console-messages
--batch "${gimp_lisp}"
--batch "(gimp-quit 0)" # quit must be a separate batch line or it will hang
WORKING_DIRECTORY "${working_directory}"
OUTPUT_QUIET
ERROR_QUIET
COMMAND_ECHO ${command_echo}
COMMAND_ERROR_IS_FATAL ANY)

# Gimp doesn't properly set error codes
if(NOT EXISTS "${png_out}")
message(FATAL_ERROR " gimp_convert: ${png_out} was not created:\n...\n${gimp_lisp}\n...\n")
endif()

set(${result} "${png_out}" PARENT_SCOPE)
endfunction()

# FIXME:
# 1. rsvg_convert is fastest
# 2. inkscape is slow but works well
Expand Down

0 comments on commit 5be209f

Please sign in to comment.