-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_contour.sh
More file actions
69 lines (56 loc) · 2.26 KB
/
remove_contour.sh
File metadata and controls
69 lines (56 loc) · 2.26 KB
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
65
66
67
68
69
#!/bin/bash
# This script was generated with the help of DeepSeek and is licensed under the MIT License.
# See LICENSE-SCRIPT file for full terms.
cd ~/Documents/RampartOne-Regular.ufo/glyphs
# Install required tools if needed
sudo apt-get install -y xmlstarlet bc
for glif in *.glif; do
echo "Processing $glif..."
# Backup original file
cp "$glif" "${glif}.bak"
# Find global extreme points
extremes=$(xmlstarlet sel -t -m "//point" \
-v "@x" -o " " -v "@y" -n "$glif" | awk '
BEGIN {
min_x = max_x = min_y = max_y = ""
}
{
if (min_x == "" || $1 < min_x) min_x = $1
if (max_x == "" || $1 > max_x) max_x = $1
if (min_y == "" || $2 < min_y) min_y = $2
if (max_y == "" || $2 > max_y) max_y = $2
}
END {
print min_x, max_x, min_y, max_y
}')
read min_x max_x min_y max_y <<< "$extremes"
# Find contour containing ALL extreme points
contour_count=$(xmlstarlet sel -t -v "count(//contour)" "$glif")
contour_to_remove=""
for (( idx=1; idx<=contour_count; idx++ )); do
points=$(xmlstarlet sel -t -m "//contour[$idx]/point" \
-v "@x" -o " " -v "@y" -n "$glif")
has_min_x=0; has_max_x=0; has_min_y=0; has_max_y=0
while read x y; do
# Floating point comparison
if [ $(echo "$x == $min_x" | bc -l) -eq 1 ]; then has_min_x=1; fi
if [ $(echo "$x == $max_x" | bc -l) -eq 1 ]; then has_max_x=1; fi
if [ $(echo "$y == $min_y" | bc -l) -eq 1 ]; then has_min_y=1; fi
if [ $(echo "$y == $max_y" | bc -l) -eq 1 ]; then has_max_y=1; fi
done <<< "$points"
if [[ $has_min_x -eq 1 && $has_max_x -eq 1 && $has_min_y -eq 1 && $has_max_y -eq 1 ]]; then
contour_to_remove=$idx
break
fi
done
# Remove the contour if found
if [ -n "$contour_to_remove" ]; then
echo "Removing contour $contour_to_remove containing extreme points"
xmlstarlet ed -d "//contour[$contour_to_remove]" "$glif" > "${glif}.tmp"
mv "${glif}.tmp" "$glif"
else
echo "No contour with all extreme points found"
# Restore original if no changes
rm "${glif}.bak"
fi
done