2023-01-20 10:41:00 +00:00
|
|
|
# shellcheck shell=bash
|
2022-02-20 05:27:41 +00:00
|
|
|
fixupOutputHooks+=('convertDesktopFiles $prefix')
|
|
|
|
|
|
|
|
# Get a param out of a desktop file. First parameter is the file and the second
|
2023-04-29 16:46:19 +00:00
|
|
|
# is the key who's value we should fetch.
|
2022-02-20 05:27:41 +00:00
|
|
|
getDesktopParam() {
|
2023-04-29 16:46:19 +00:00
|
|
|
local file="$1"
|
|
|
|
local key="$2"
|
|
|
|
local line k v
|
|
|
|
|
|
|
|
while read -r line; do
|
|
|
|
if [[ "$line" = *=* ]]; then
|
|
|
|
k="${line%%=*}"
|
|
|
|
v="${line#*=}"
|
|
|
|
|
|
|
|
if [[ "$k" = "$key" ]]; then
|
|
|
|
echo "$v"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done < "$file"
|
2022-02-20 05:27:41 +00:00
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
return 1
|
2022-02-20 05:27:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 16:20:37 +00:00
|
|
|
# Convert a freedesktop.org icon theme for a given app to a .icns file. When possible, missing
|
|
|
|
# icons are synthesized from SVG or rescaled from existing ones (when within the size threshold).
|
|
|
|
convertIconTheme() {
|
|
|
|
local -r out=$1
|
|
|
|
local -r sharePath=$2
|
|
|
|
local -r iconName=$3
|
|
|
|
local -r theme=${4:-hicolor}
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
# Sizes based on archived Apple documentation:
|
|
|
|
# https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/app-icon#app-icon-sizes
|
|
|
|
local -ra iconSizes=(16 32 128 256 512)
|
2022-03-05 16:20:37 +00:00
|
|
|
local -ra scales=([1]="" [2]="@2")
|
|
|
|
|
|
|
|
# Based loosely on the algorithm at:
|
|
|
|
# https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html#icon_lookup
|
|
|
|
# Assumes threshold = 2 for ease of implementation.
|
|
|
|
function findIcon() {
|
|
|
|
local -r iconSize=$1
|
|
|
|
local -r scale=$2
|
|
|
|
|
|
|
|
local scaleSuffix=${scales[$scale]}
|
|
|
|
local exactSize=${iconSize}x${iconSize}${scaleSuffix}
|
|
|
|
|
|
|
|
local -a validSizes=(
|
|
|
|
${exactSize}
|
|
|
|
$((iconSize + 1))x$((iconSize + 1))${scaleSuffix}
|
|
|
|
$((iconSize + 2))x$((iconSize + 2))${scaleSuffix}
|
|
|
|
$((iconSize - 1))x$((iconSize - 1))${scaleSuffix}
|
|
|
|
$((iconSize - 2))x$((iconSize - 2))${scaleSuffix}
|
|
|
|
)
|
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
local fallbackIcon=
|
|
|
|
|
2022-03-05 16:20:37 +00:00
|
|
|
for iconIndex in "${!candidateIcons[@]}"; do
|
|
|
|
for maybeSize in "${validSizes[@]}"; do
|
|
|
|
icon=${candidateIcons[$iconIndex]}
|
|
|
|
if [[ $icon = */$maybeSize/* ]]; then
|
|
|
|
if [[ $maybeSize = $exactSize ]]; then
|
|
|
|
echo "fixed $icon"
|
2023-04-29 16:46:19 +00:00
|
|
|
return 0
|
2022-03-05 16:20:37 +00:00
|
|
|
else
|
|
|
|
echo "threshold $icon"
|
2023-04-29 16:46:19 +00:00
|
|
|
return 0
|
2022-03-05 16:20:37 +00:00
|
|
|
fi
|
2023-04-29 16:46:19 +00:00
|
|
|
elif [[ -a $icon && -z "$fallbackIcon" ]]; then
|
|
|
|
fallbackIcon="$icon"
|
2022-03-05 16:20:37 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
2023-04-29 16:46:19 +00:00
|
|
|
|
|
|
|
if [[ -n "$fallbackIcon" ]]; then
|
|
|
|
echo "fallback $fallbackIcon"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2022-03-05 16:20:37 +00:00
|
|
|
echo "scalable"
|
|
|
|
}
|
|
|
|
|
|
|
|
function resizeIcon() {
|
|
|
|
local -r in=$1
|
|
|
|
local -r out=$2
|
|
|
|
local -r iconSize=$3
|
|
|
|
local -r scale=$4
|
|
|
|
|
|
|
|
local density=$((72 * scale))x$((72 * scale))
|
|
|
|
local dim=$((iconSize * scale))
|
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
echo "desktopToDarwinBundle: resizing icon $in to $out, size $dim" >&2
|
2022-03-05 16:20:37 +00:00
|
|
|
magick convert -scale "${dim}x${dim}" -density "$density" -units PixelsPerInch "$in" "$out"
|
|
|
|
}
|
|
|
|
|
|
|
|
function synthesizeIcon() {
|
|
|
|
local -r in=$1
|
|
|
|
local -r out=$2
|
|
|
|
local -r iconSize=$3
|
|
|
|
local -r scale=$4
|
|
|
|
|
|
|
|
if [[ $in != '-' ]]; then
|
|
|
|
local density=$((72 * scale))x$((72 * scale))
|
|
|
|
local dim=$((iconSize * scale))
|
2023-04-29 16:46:19 +00:00
|
|
|
|
|
|
|
echo "desktopToDarwinBundle: rasterizing svg $in to $out, size $dim" >&2
|
2022-03-05 16:20:37 +00:00
|
|
|
rsvg-convert --keep-aspect-ratio --width "$dim" --height "$dim" "$in" --output "$out"
|
|
|
|
magick convert -density "$density" -units PixelsPerInch "$out" "$out"
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIcons() {
|
|
|
|
local -r sharePath=$1
|
|
|
|
local -r iconname=$2
|
|
|
|
local -r theme=$3
|
|
|
|
local -r resultdir=$(mktemp -d)
|
|
|
|
|
|
|
|
local -ar candidateIcons=(
|
|
|
|
"${sharePath}/icons/${theme}/"*"/${iconname}.png"
|
|
|
|
"${sharePath}/icons/${theme}/"*"/${iconname}.xpm"
|
|
|
|
)
|
|
|
|
|
|
|
|
local -a scalableIcon=("${sharePath}/icons/${theme}/scalable/${iconname}.svg"*)
|
|
|
|
if [[ ${#scalableIcon[@]} = 0 ]]; then
|
|
|
|
scalableIcon=('-')
|
|
|
|
fi
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
# Tri-state variable, NONE means no icons have been found, an empty
|
|
|
|
# icns file will be generated, not sure that's necessary because macOS
|
|
|
|
# will default to a generic icon if no icon can be found.
|
|
|
|
#
|
|
|
|
# OTHER means an appropriate icon was found.
|
|
|
|
#
|
|
|
|
# Any other value is a path to an icon file that isn't scalable or
|
|
|
|
# within the threshold. This is used as a fallback in case no better
|
|
|
|
# icon can be found and will be scaled as much as
|
|
|
|
# necessary to result in appropriate icon sizes.
|
|
|
|
local foundIcon=NONE
|
2022-03-05 16:20:37 +00:00
|
|
|
for iconSize in "${iconSizes[@]}"; do
|
|
|
|
for scale in "${!scales[@]}"; do
|
|
|
|
local iconResult=$(findIcon $iconSize $scale)
|
|
|
|
local type=${iconResult%% *}
|
|
|
|
local icon=${iconResult#* }
|
|
|
|
local scaleSuffix=${scales[$scale]}
|
|
|
|
local result=${resultdir}/${iconSize}x${iconSize}${scales[$scale]}${scaleSuffix:+x}.png
|
2023-04-29 16:46:19 +00:00
|
|
|
echo "desktopToDarwinBundle: using $type icon $icon for size $iconSize$scaleSuffix" >&2
|
2022-03-05 16:20:37 +00:00
|
|
|
case $type in
|
|
|
|
fixed)
|
|
|
|
local density=$((72 * scale))x$((72 * scale))
|
|
|
|
magick convert -density "$density" -units PixelsPerInch "$icon" "$result"
|
2022-03-30 09:31:56 +00:00
|
|
|
foundIcon=OTHER
|
2022-03-05 16:20:37 +00:00
|
|
|
;;
|
|
|
|
threshold)
|
|
|
|
# Synthesize an icon of the exact size if a scalable icon is available
|
|
|
|
# instead of scaling one and ending up with a fuzzy icon.
|
|
|
|
if ! synthesizeIcon "${scalableIcon[0]}" "$result" "$iconSize" "$scale"; then
|
|
|
|
resizeIcon "$icon" "$result" "$iconSize" "$scale"
|
|
|
|
fi
|
2022-03-30 09:31:56 +00:00
|
|
|
foundIcon=OTHER
|
2022-03-05 16:20:37 +00:00
|
|
|
;;
|
|
|
|
scalable)
|
|
|
|
synthesizeIcon "${scalableIcon[0]}" "$result" "$iconSize" "$scale" || true
|
2022-03-30 09:31:56 +00:00
|
|
|
foundIcon=OTHER
|
|
|
|
;;
|
|
|
|
fallback)
|
|
|
|
# Use the largest size available to scale to
|
|
|
|
# appropriate sizes.
|
|
|
|
if [[ $foundIcon != OTHER ]]; then
|
|
|
|
foundIcon=$icon
|
|
|
|
fi
|
2022-03-05 16:20:37 +00:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
done
|
2022-03-30 09:31:56 +00:00
|
|
|
if [[ $foundIcon != NONE && $foundIcon != OTHER ]]; then
|
|
|
|
# Ideally we'd only resize to whatever the closest sizes are,
|
|
|
|
# starting from whatever icon sizes are available.
|
|
|
|
for iconSize in 16 32 128 256 512; do
|
|
|
|
local result=${resultdir}/${iconSize}x${iconSize}.png
|
|
|
|
resizeIcon "$foundIcon" "$result" "$iconSize" 1
|
|
|
|
done
|
|
|
|
fi
|
2022-03-05 16:20:37 +00:00
|
|
|
echo "$resultdir"
|
|
|
|
}
|
|
|
|
|
|
|
|
iconsdir=$(getIcons "$sharePath" "apps/${iconName}" "$theme")
|
2022-04-27 09:35:20 +00:00
|
|
|
if [[ -n "$(ls -A1 "$iconsdir")" ]]; then
|
2022-03-30 09:31:56 +00:00
|
|
|
icnsutil compose --toc "$out/${iconName}.icns" "$iconsdir/"*
|
2022-03-05 16:20:37 +00:00
|
|
|
else
|
|
|
|
echo "Warning: no icons were found. Creating an empty icon for ${iconName}.icns."
|
|
|
|
touch "$out/${iconName}.icns"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-04-27 09:35:20 +00:00
|
|
|
processExecFieldCodes() {
|
|
|
|
local -r file=$1
|
|
|
|
local -r execRaw=$(getDesktopParam "${file}" "Exec")
|
|
|
|
local -r execNoK="${execRaw/\%k/${file}}"
|
|
|
|
local -r execNoKC="${execNoK/\%c/$(getDesktopParam "${file}" "Name")}"
|
|
|
|
local -r icon=$(getDesktopParam "${file}" "Icon")
|
|
|
|
local -r execNoKCI="${execNoKC/\%i/${icon:+--icon }${icon}}"
|
|
|
|
local -r execNoKCIfu="${execNoKCI/\%[fu]/\$1}"
|
|
|
|
local -r exec="${execNoKCIfu/\%[FU]/\$@}"
|
|
|
|
if [[ "$exec" != "$execRaw" ]]; then
|
|
|
|
echo 1>&2 "desktopToDarwinBundle: Application bundles do not understand desktop entry field codes. Changed '$execRaw' to '$exec'."
|
|
|
|
fi
|
|
|
|
echo "$exec"
|
|
|
|
}
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
# For a given .desktop file, generate a darwin '.app' bundle for it.
|
|
|
|
convertDesktopFile() {
|
2022-03-05 16:20:37 +00:00
|
|
|
local -r file=$1
|
|
|
|
local -r sharePath=$(dirname "$(dirname "$file")")
|
2023-04-29 16:46:19 +00:00
|
|
|
local -r name=$(getDesktopParam "${file}" "Name")
|
2022-04-27 09:35:20 +00:00
|
|
|
local -r macOSExec=$(getDesktopParam "${file}" "X-macOS-Exec")
|
|
|
|
if [[ "$macOSExec" ]]; then
|
|
|
|
local -r exec="$macOSExec"
|
|
|
|
else
|
|
|
|
local -r exec=$(processExecFieldCodes "${file}")
|
|
|
|
fi
|
2023-04-29 16:46:19 +00:00
|
|
|
local -r iconName=$(getDesktopParam "${file}" "Icon")
|
2022-03-05 16:20:37 +00:00
|
|
|
local -r squircle=$(getDesktopParam "${file}" "X-macOS-SquircleIcon")
|
2022-02-20 05:27:41 +00:00
|
|
|
|
2022-04-27 09:35:20 +00:00
|
|
|
mkdir -p "${!outputBin}/Applications/${name}.app/Contents/MacOS"
|
|
|
|
mkdir -p "${!outputBin}/Applications/${name}.app/Contents/Resources"
|
2022-02-20 05:27:41 +00:00
|
|
|
|
2022-04-27 09:35:20 +00:00
|
|
|
convertIconTheme "${!outputBin}/Applications/${name}.app/Contents/Resources" "$sharePath" "$iconName"
|
2022-02-20 05:27:41 +00:00
|
|
|
|
2022-04-27 09:35:20 +00:00
|
|
|
write-darwin-bundle "${!outputBin}" "$name" "$exec" "$iconName" "$squircle"
|
2022-02-20 05:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
convertDesktopFiles() {
|
|
|
|
local dir="$1/share/applications/"
|
|
|
|
|
|
|
|
if [ -d "${dir}" ]; then
|
|
|
|
for desktopFile in $(find "$dir" -iname "*.desktop"); do
|
|
|
|
convertDesktopFile "$desktopFile";
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|