bin/replace_string_in_zip.sh |   68 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

New commits:
commit 4a3412a34a15e5f5653329da8f073f013ca0c9dc
Author:     Xisco Fauli <xiscofa...@libreoffice.org>
AuthorDate: Mon Mar 4 17:55:52 2024 +0100
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Tue Mar 5 09:33:05 2024 +0100

    bin: Add initial script to replace missing fonts
    
    It already contains some replacements I found along the way
    For now it works with docx, xlsx and pptx files.
    I have to test it with odf formats.
    
    Change-Id: Ia68bc7561f0e41580ad1834a1a345b618f3e688c
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164393
    Tested-by: Jenkins
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/bin/replace_string_in_zip.sh b/bin/replace_string_in_zip.sh
new file mode 100755
index 000000000000..184f37835bc7
--- /dev/null
+++ b/bin/replace_string_in_zip.sh
@@ -0,0 +1,68 @@
+#!/bin/bash
+
+declare -A replacements
+replacements["Agency FB"]="Noto Sans"
+replacements["Noto Sans Light"]="Noto Sans"
+replacements["Segoe UI"]="Noto Sans"
+replacements["Univers 45 Light"]="Noto Sans"
+replacements["Trebuchet MS"]="Noto Sans"
+replacements["Georgia"]="Noto Serif"
+replacements["Perpetua"]="Noto Sans"
+replacements["Garamond"]="Noto Serif"
+replacements["Calibri Light"]="Noto Sans"
+replacements["Consolas"]="DejaVu Sans Mono"
+replacements["Verdana"]="Noto Sans"
+replacements["Rockwell"]="Noto Sans"
+replacements["Tms Rmn"]="DejaVu Sans"
+replacements["Tahoma"]="Noto Sans"
+replacements["DFKai-SB"]="Noto Sans"
+replacements["Gill Sans MT"]="Noto Sans"
+replacements["Helvetica"]="Liberation Sans"
+replacements["Liberation Serif"]="DejaVu Sans"
+replacements["BentonSans Medium"]="Noto Sans"
+replacements["BentonSans"]="Noto Sans"
+replacements["AdvPS88D1"]="Noto Sans"
+replacements["NexusSansOT"]="Noto Sans"
+
+extracted_folder=".temp_extracted"
+
+for file in $(find "$1" -type f); do
+    file_name=$(basename "$file")
+    current_extension="${file_name##*.}"
+
+    if [[ $current_extension == "docx" || $current_extension == "xlsx" || 
$current_extension == "pptx" ]]; then
+        base_name="${file_name%.*}"
+
+        # move the file to a new .zip file
+        cp "$file" "${base_name}.zip"
+
+        # extract the zip file to a temporary folder
+        unzip -qq ./"${base_name}.zip"  -d "$extracted_folder" > /dev/null
+
+        for key in "${!replacements[@]}"
+        do
+            file_changed=false
+            value=${replacements[$key]}
+            for subfile in $(find "$extracted_folder" -type f); do
+                if grep -q "$key" "$subfile"; then
+                    # use sed to replace the string in the file if it exists
+                    sed -i "s/$key/$value/g" "$subfile"
+                    file_changed=true
+                fi
+            done
+
+            if [ "$file_changed" = true ]; then
+                # Create a new zip file with the modified files
+                cd "$extracted_folder"; zip -r ../"${base_name}.zip" . > 
/dev/null; cd ..
+
+                mv "${base_name}.zip" "$file"
+
+                echo "Replacing '$key' with '$value' in $file"
+            fi
+        done
+
+        # Clean up the temporary extracted folder
+        rm -rf "$extracted_folder"
+    fi
+done
+

Reply via email to