#!/bin/bash # Script to convert TTF fonts to WOFF2 format with progress bar # Usage: ./convert-ttf-in-woff2.sh file1.ttf file2.ttf ... # Usage: ./convert-ttf-in-woff2.sh *.ttf # Works as Nemo script with selected files # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Function to check if woff2_compress is installed check_woff2() { if ! command -v woff2_compress &> /dev/null; then echo -e "${RED}Error: woff2_compress is not installed.${NC}" echo -e "${YELLOW}Install woff2 with:${NC}" echo " Ubuntu/Debian: sudo apt-get install woff2" echo " CentOS/RHEL: sudo yum install woff2" echo " Arch: sudo pacman -S woff2" exit 1 fi } # Main function main() { local file_args=("$@") # Check woff2_compress check_woff2 # Create output directory local output_dir="woff2" if [ ! -d "$output_dir" ]; then mkdir -p "$output_dir" echo -e "${GREEN}Created output directory: $output_dir${NC}" fi # Get files to process local ttf_files=() # If we have file arguments, process them if [ ${#file_args[@]} -gt 0 ]; then # Process file arguments (expand patterns and check files) for pattern in "${file_args[@]}"; do # Use shell expansion for patterns like *.ttf for file in $pattern; do if [[ -f "$file" ]] && [[ "$file" =~ \.(ttf|TTF)$ ]]; then ttf_files+=("$file") fi done done else echo -e "${RED}Error: No files specified.${NC}" echo -e "${YELLOW}Usage from terminal: $0 file1.ttf file2.ttf ...${NC}" echo -e "${YELLOW}Usage from terminal: $0 *.ttf${NC}" echo -e "${YELLOW}Or select files in Nemo and run as script${NC}" exit 1 fi if [ ${#ttf_files[@]} -eq 0 ]; then echo -e "${YELLOW}No TTF files found to process.${NC}" exit 0 fi local total_files=${#ttf_files[@]} # Detect if running from Nemo (file manager) or terminal local use_gui=false # Use GUI if we're likely running from Nemo and zenity is available if [ -n "$NEMO_SCRIPT_SELECTED_FILE_PATHS" ] && command -v zenity &> /dev/null; then use_gui=true fi # Show initial info only for terminal if [ "$use_gui" = false ]; then echo -e "${GREEN}TTF to WOFF2 conversion started${NC}" echo -e "${BLUE}Files to process: ${total_files}${NC}" echo -e "${BLUE}Output directory: ${output_dir}${NC}" echo "" fi # Process each file with appropriate progress display local count=0 if [ "$use_gui" = true ]; then # Use zenity progress bar for GUI ( for file in "${ttf_files[@]}"; do if [ -f "$file" ]; then count=$((count + 1)) filename=$(basename "$file") output_file="${output_dir}/${filename%.ttf}.woff2" echo "# Converting $filename... ($count/$total_files)" # Convert the file (woff2_compress creates file in same directory) woff2_compress "$file" 2>/dev/null # Move the created file to output directory local created_file="${file%.ttf}.woff2" if [ -f "$created_file" ]; then mv "$created_file" "$output_file" fi echo "$((count * 100 / total_files))" fi done ) | zenity --progress --title="TTF to WOFF2 Conversion" --text="Initializing..." --percentage=0 --auto-close # Check if user cancelled if [ $? -ne 0 ]; then zenity --error --text="Conversion was cancelled." exit 1 fi else # Use terminal progress bar for file in "${ttf_files[@]}"; do if [ -f "$file" ]; then count=$((count + 1)) filename=$(basename "$file") output_file="${output_dir}/${filename%.ttf}.woff2" # Show terminal progress local percentage=$((count * 100 / total_files)) printf "\r${BLUE}[%3d%%] Processing $filename ($count/$total_files)${NC}" "$percentage" # Convert the file (woff2_compress creates file in same directory) woff2_compress "$file" 2>/dev/null # Move the created file to output directory local created_file="${file%.ttf}.woff2" if [ -f "$created_file" ]; then mv "$created_file" "$output_file" fi fi done echo "" # New line after progress fi # Show final report if [ "$use_gui" = true ]; then # Show report with zenity local report_message="✓ Conversion completed!\n\n" report_message+="Files converted: ${count}\n" report_message+="Output directory: ${output_dir}/" zenity --info --title="TTF to WOFF2 Conversion Complete" --text="$report_message" else # Show report in terminal echo "" echo -e "${GREEN}✓ Conversion completed!${NC}" echo "" echo -e "${BLUE}Results:${NC}" echo -e " Files converted: ${count}" echo -e " Output directory: ${output_dir}/" fi } # Show help if requested if [[ "$1" == "-h" || "$1" == "--help" ]]; then echo -e "${GREEN}Script to convert TTF fonts to WOFF2 format${NC}" echo "" echo -e "${YELLOW}Usage:${NC}" echo " $0 file1.ttf file2.ttf ..." echo " $0 *.ttf" echo " Or select files in Nemo file manager and run as script" echo "" echo -e "${YELLOW}Parameters:${NC}" echo " files TTF files to convert" echo "" echo -e "${YELLOW}Examples:${NC}" echo " $0 font1.ttf font2.ttf # Convert specific files" echo " $0 *.ttf # Convert all TTF files" echo " $0 fonts/*.ttf # Convert TTF files in fonts folder" echo "" echo -e "${BLUE}Note: Converted fonts will be saved in the 'woff2' subdirectory${NC}" echo -e "${BLUE} When used as Nemo script, progress is shown graphically${NC}" echo -e "${BLUE} Requires woff2 package for font conversion${NC}" exit 0 fi # Execute main main "$@"