diff --git a/font-tools/README.md b/font-tools/README.md new file mode 100644 index 0000000..07d5b59 --- /dev/null +++ b/font-tools/README.md @@ -0,0 +1,63 @@ +# Font Tools + + +Collection of bash scripts for font processing and conversion. + +## Scripts + +### convert-ttf-in-woff2.sh + +Script to convert TTF fonts to WOFF2 format + +**Usage:** +```bash +./convert-ttf-in-woff2.sh file1.ttf file2.ttf ... +./convert-ttf-in-woff2.sh *.ttf +``` +Or select files in Nemo file manager and run as script + +**Parameters:** +- `files` - TTF files to convert + +**Examples:** +```bash +./convert-ttf-in-woff2.sh font1.ttf font2.ttf # Convert specific files +./convert-ttf-in-woff2.sh *.ttf # Convert all TTF files +./convert-ttf-in-woff2.sh fonts/*.ttf # Convert TTF files in fonts folder +``` + +**Features:** +- Converted fonts will be saved in the 'woff2' subdirectory +- When used as Nemo script, progress is shown graphically +- Dual-mode operation (GUI/Terminal) +- Progress tracking and batch processing +- Automatic output directory creation + +**Dependencies:** +- `woff2` package for font conversion +- `zenity` for GUI dialogs (usually pre-installed) + +## Installation + +1. Install dependencies: +```bash +sudo apt install woff2 # Ubuntu/Debian +sudo dnf install woff2-tools # Fedora +# Note: zenity is usually pre-installed with desktop environments +``` + +2. Make scripts executable: +```bash +chmod +x *.sh +``` + +3. For use as Nemo scripts, copy to: +```bash +~/.local/share/nemo/scripts/ +``` + +## Notes + +All scripts support dual-mode operation: +- **Terminal mode**: When run from command line +- **GUI mode**: When run as Nemo file manager scripts with zenity dialogs \ No newline at end of file diff --git a/font-tools/convert-ttf-in-woff2.sh b/font-tools/convert-ttf-in-woff2.sh new file mode 100644 index 0000000..015c2a8 --- /dev/null +++ b/font-tools/convert-ttf-in-woff2.sh @@ -0,0 +1,186 @@ +#!/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 "$@"