Skip to content

Commit

Permalink
feat: build hyprcursor with svgs
Browse files Browse the repository at this point in the history
as mentioned in #53 (comment).

Effectively rewrite the hyprcursor build system to be data driven, reflecting the other formats. As a result dependency on `hyprcursor-util` & `xcur2png` is gone.
  • Loading branch information
Covkie committed Nov 27, 2024
1 parent 42d57f7 commit 88e5428
Show file tree
Hide file tree
Showing 53 changed files with 72 additions and 291 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ cursor SVGs.
- [xcursorgen](https://wiki.archlinux.org/title/Xcursorgen) to generate the
cursors.
- [inkscape](https://wiki.inkscape.org/wiki/Inkscape) to convert SVGs to PNGs.
- **(Optional)** [hyprcursor](https://github.com/hyprwm/hyprcursor) to include
hyprcursor variants.
- **(Optional)** [just](https://github.com/casey/just) to easily run development
commands.

Expand All @@ -120,9 +118,7 @@ cursor SVGs.

```bash
just all # Build all flavor-accent variants.
just all_with_hyprcursor # Build all flavor-accent variants with hyprcursor support
just build mocha # To build all variants under one single flavor.
just build_with_hyprcursor mocha # To build all variants under one single flavor with hyprcursor support
just build mocha blue # To build only the blue variant under mocha.
just build mocha 'blue mauve peach' # To build only the blue, mauve, and peach variants under mocha.
```
Expand Down
72 changes: 13 additions & 59 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,6 @@ build_svgs() {
done
}

create_hyprcursors() {
local src_dir="$1"
local out_dir="$2"
local hypr_dir="$out_dir/hyprcursors"

[ -d "$out_dir" ] || mkdir -p "$out_dir"
[ -d "$hypr_dir" ] || mkdir -p "$hypr_dir"

hyprcursor-util -x "$src_dir" -o "$out_dir" >/dev/null
}

compile_hyprcursors() {
local hl_dir="$1"
local out_dir="$2"
local src_dir="$3"
local theme_name="$(basename "$1" | sed 's/extracted_//')"

[ -d "$hl_dir" ] || return 1
[ -d "$out_dir" ] || mkdir -p "$out_dir"

hyprcursor-util -c "$hl_dir" -o "$out_dir" >/dev/null
# For whatever reason, hyprcursor appends "Extracted Theme" to the end so we have to work around it
mv "$out_dir/theme_Extracted Theme/hyprcursors" "$out_dir/hyprcursors"
rm -rf "$out_dir/theme_Extracted Theme"

local theme_comment="$(grep Comment "$src_dir"/index.theme)"

cat >"$out_dir"/manifest.hl <<-EOM
name = ${theme_name%-cursors}
description = ${theme_comment#Comment=}
version = "v1.0.1" # x-release-please-version
cursors_directory = hyprcursors
EOM
}

SCRIPT_DIR="$(dirname "$0")"
SRC_DIR="$SCRIPT_DIR/src"
OUT_DIR="$SCRIPT_DIR/dist"
Expand All @@ -66,7 +31,6 @@ LICENSE="$SCRIPT_DIR/LICENSE"
ALIASES="$SRC_DIR/cursorList"

ACCENTS="blue dark flamingo green lavender light maroon mauve peach pink red rosewater sapphire sky teal yellow"
HYPRCURSOR=0

while getopts "f:a:h" option; do
case $option in
Expand All @@ -77,7 +41,7 @@ while getopts "f:a:h" option; do
ACCENTS="$OPTARG"
;;
h)
HYPRCURSOR=1
echo "uneeded, hyprcursor is generated by default now."
;;
\?)
echo "Invalid option: -$OPTARG"
Expand All @@ -87,41 +51,31 @@ while getopts "f:a:h" option; do
done
echo "[INFO] - Flavor: '$FLAVOR'"
echo "[INFO] - Accent(s): '$ACCENTS'"
if [ $HYPRCURSOR -eq 1 ]; then
echo "[INFO] - Building with hyprcursor: TRUE"
else
echo "[INFO] - Building with hyprcursor: FALSE"
fi

echo "[INFO] - Generating SVGs..."
build_svgs $SRC_DIR $FLAVOR
echo "[INFO] - Generating SVGs complete."

echo "[INFO] - Converting to xcursor & creating plasma svg cursors..."
echo "[INFO] - Building cursor formats; xcursor, hyprcursor, plasmasvg..."
for accent in $ACCENTS; do
theme_name="catppuccin-$FLAVOR-$accent-cursors"
theme_src_dir="$SVGS_DIR/$theme_name"
theme_out_dir="$OUT_DIR/$theme_name"
theme_png_dir="$PNG_DIR/$theme_name"
theme_hl_dir="$HL_DIR/$theme_name"

scripts/build-xcursor-plasmasvg "$theme_src_dir" "$theme_png_dir" "$theme_out_dir"
scripts/build-cursors "$theme_src_dir" "$theme_png_dir" "$theme_hl_dir" "$theme_out_dir"

cp -f "$AUTHORS" "$theme_out_dir"
cp -f "$LICENSE" "$theme_out_dir"
cp -f "$theme_src_dir/index.theme" "$theme_out_dir"
done
echo "[INFO] - Converting to xcursor & creating plasma svg cursors complete"

if [[ $HYPRCURSOR == 1 ]]; then
echo "[INFO] - Generating hyprcursor theme..."
for accent in $ACCENTS; do
theme_name="catppuccin-$FLAVOR-$accent-cursors"
theme_src_dir="$OUT_DIR/$theme_name"
theme_hl_dir="$HL_DIR/extracted_$theme_name"
theme_out_dir="$OUT_DIR/$theme_name"

create_hyprcursors "$theme_src_dir" "$HL_DIR" "$theme_name"
compile_hyprcursors "$theme_hl_dir" "$theme_out_dir" "$theme_src_dir"
done
echo "[INFO] - Generating hyprcursor theme complete"
fi
theme_comment="$(grep Comment "$theme_src_dir"/index.theme)"
cat >"$theme_out_dir"/manifest.hl <<-EOM
name = ${theme_name%-cursors}
description = ${theme_comment#Comment=}
version = "v1.0.1" # x-release-please-version
cursors_directory = hyprcursors
EOM
done
echo "[INFO] - Finished building cursor formats; xcursor, hyprcursor, plasmasvg"
6 changes: 1 addition & 5 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
inkscape,
just,
xcursorgen,
xcur2png,
hyprcursor,
zip,
catppuccin-whiskers,
python3,
Expand All @@ -30,9 +28,7 @@ stdenvNoCC.mkDerivation {
nativeBuildInputs = [
just
xcursorgen
xcur2png
inkscape
hyprcursor
zip
catppuccin-whiskers
python3
Expand All @@ -43,7 +39,7 @@ stdenvNoCC.mkDerivation {
runHook preBuild
patchShebangs .
just all_with_hyprcursor
just all
just zip
runHook postBuild
Expand Down
10 changes: 4 additions & 6 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ accents := "blue dark flamingo green lavender light maroon mauve peach pink red
clean:
rm -rf pngs/ hl/ dist/ releases/ svgs/

# Remove all hyprcursor related files
clean_hl:
rm -rf hl/
# Remove all hyprcursor related files // deprecated
clean_hl: clean

# Zip all directories inside of "./dist"
zip:
Expand All @@ -26,6 +25,5 @@ build_with_hyprcursor f a=accents: clean_hl
# Generate all flavors with their accents
all: clean (build "latte") (build "frappe") (build "macchiato") (build "mocha")

# Generate all flavors with their accents with hyprcursor support
all_with_hyprcursor: clean (build_with_hyprcursor "latte") (build_with_hyprcursor "frappe") (build_with_hyprcursor "macchiato") (build_with_hyprcursor "mocha")

# Generate all flavors with their accents with hyprcursor support // deprecated
all_with_hyprcursor: all
30 changes: 28 additions & 2 deletions scripts/build-xcursor-plasmasvg → scripts/build-cursors
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -euo pipefail

BIN_DIR="$( dirname "${BASH_SOURCE[0]}" )"
SRC_DIR="src"
HL_DIR="$3"
BUILD_DIR="$2"
RAWSVG_DIR="$1"
ALIASES="$SRC_DIR/cursorList"
Expand Down Expand Up @@ -39,11 +40,12 @@ done
inkscape --shell <<<${genPixmaps} &>/dev/null

# Generate cursor theme
OUTPUT=$3
OUTPUT=$4
rm -rf "$OUTPUT"
mkdir -p "$OUTPUT/cursors"
mkdir -p "$OUTPUT/cursors_scalable"
$BIN_DIR/generate-xcursor-plasmasvg ${RAWSVG_DIR} "$BUILD_DIR" "$OUTPUT/cursors" "$OUTPUT/cursors_scalable" ${NOMINAL_SIZE} ${FRAME_TIME} ${SCALES}
mkdir -p "$HL_DIR"
$BIN_DIR/generate-metadata ${RAWSVG_DIR} "$BUILD_DIR" "$OUTPUT/cursors" "$OUTPUT/cursors_scalable" "$HL_DIR" ${NOMINAL_SIZE} ${FRAME_TIME} ${SCALES}

# Generate shortcuts
while read ALIAS ; do
Expand All @@ -67,3 +69,27 @@ while read ALIAS ; do

ln -s "$TO" "$OUTPUT/cursors_scalable/$FROM"
done < $ALIASES

while read ALIAS ; do
FROM=${ALIAS% *}
TO=${ALIAS#* }

if [[ -e "$HL_DIR/$FROM" ]]; then
continue
fi

if [ -f "$HL_DIR/$TO/meta.hl" ]; then # need to ensure $TO is not a mapping itself
echo -ne "\ndefine_override = $FROM" >> $HL_DIR/$TO/meta.hl;
else
grep=$(grep -m 1 "^$TO " $ALIASES)
TO=${grep#* }
echo -ne "\ndefine_override = $FROM" >> $HL_DIR/$TO/meta.hl
fi
done < $ALIASES

#zip up hyprcursor
mkdir -p "$OUTPUT/hyprcursors"
for dir in $HL_DIR/*; do
dir_name=$(basename $dir)
zip -qr "$OUTPUT/hyprcursors/$dir_name.hlc" -j "$dir/"
done
31 changes: 26 additions & 5 deletions scripts/generate-xcursor-plasmasvg → scripts/generate-metadata
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ from PySide6.QtSvg import QSvgRenderer
# from potential off-by-one errors when the cursor is scaled.
HOTSPOT_DISPLACE = 1

if len(sys.argv) <= 7:
print("Usage: " + sys.argv[0] + " <svg cursor dir> <pixmap dir> <xcursor output dir> <svg cursor output dir> <base size> <animation frame time> <scales>")
if len(sys.argv) <= 8:
print("Usage: " + sys.argv[0] + " <svg cursor dir> <pixmap dir> <xcursor output dir> <svg cursor output dir> <hyprcursor output dir> <base size> <animation frame time> <scales>")
sys.exit(1)

svg_dir = Path(sys.argv[1])
pixmap_dir = Path(sys.argv[2])
xcursor_output_dir = Path(sys.argv[3])
svg_cursor_output_dir = Path(sys.argv[4])
nominal_size = int(sys.argv[5])
delay = int(sys.argv[6])
scales = [int(i) for i in sys.argv[7:]]
hyprcursor_output_dir = Path(sys.argv[5])
nominal_size = int(sys.argv[6])
delay = int(sys.argv[7])
scales = [int(i) for i in sys.argv[8:]]

svg_files = list(svg_dir.glob("*.svg"))
svg_files.sort()
Expand Down Expand Up @@ -94,3 +95,23 @@ for svg_file in svg_files:
"delay": delay
})
json.dump(l, metadata)
# Generate hypr cursor
output_dir = hyprcursor_output_dir / basename
output_dir.mkdir(parents=True, exist_ok=True)
hotspot_x = round(hotspot.x() / nominal_size, 4) # convert to hyprcursor's hotspot format
hotspot_y = round(hotspot.y() / nominal_size, 4)
with open(output_dir / "meta.hl", "w") as meta_file:
meta_data = f"""resize_algorithm = none
hotspot_x = {hotspot_x}
hotspot_y = {hotspot_y}"""
if len(frames) == 0:
filename = svg_file.name
shutil.copyfile(svg_file, output_dir / filename)
meta_data += f"\ndefine_size = 0, {filename}"
meta_file.write(meta_data)
else:
for i in frames:
filename = i.name
shutil.copyfile(i, output_dir / filename)
meta_data += f"\ndefine_size = 0, {filename}, {delay}"
meta_file.write(meta_data)
4 changes: 0 additions & 4 deletions src/config/alias.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/all-scroll.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/bottom_left_corner.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/bottom_right_corner.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/bottom_side.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/cell.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/center_ptr.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/col-resize.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/color-picker.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/context-menu.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/copy.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/crosshair.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/default.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/dnd-move.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/dnd-no-drop.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/down-arrow.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/draft.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/fleur.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/help.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/left-arrow.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/left_side.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/no-drop.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/not-allowed.hl

This file was deleted.

4 changes: 0 additions & 4 deletions src/config/openhand.hl

This file was deleted.

Loading

0 comments on commit 88e5428

Please sign in to comment.