Skip to content

Instantly share code, notes, and snippets.

@nanpuyue
Last active July 1, 2023 12:48
Show Gist options
  • Save nanpuyue/9563100 to your computer and use it in GitHub Desktop.
Save nanpuyue/9563100 to your computer and use it in GitHub Desktop.
读取字体文件内部名称并以此重命名字体文件文件。
#!/bin/bash
# 南浦月
# [email protected]
# 2013-5-11
# 用法:
# fonts-rename.sh <字体目录>
# fonts-rename.sh <字体文件>
# 选项:
# lang: zh-cn,en-us 等
# nametype: family,fullname
lang="zh-cn"
nametype="family"
function fontname()
{
fontfile="$1"
str=`fc-query "$fontfile" 2>/dev/null|grep "$nametype"`
name=`echo "$str"|grep "$nametype"":"`
name=`echo "$name"|sed -e "s#\t##g;s#:#,#g;s#(s)#,#g;s# \"##g;s#\"##g"|tr -s ','`
IFS=","
name=(${name})
namelang=`echo "$str"|grep "$nametype""lang:"`
namelang=`echo "$namelang"|sed -e "s#\t##g;s#:#,#g;s#(s)#,#g;s# \"##g;s#\"##g"|tr -s ','`
namelang=(${namelang})
n=0
for i in ${namelang[@]}
do
if [ "$i" = "$lang" ]; then
echo ${name[$n]}
fi
let n++
done
}
function renamefont()
{
filename=`basename "$1"`
extension=`tr 'A-Z' 'a-z' <<< ${1##*.}`
newname=`fontname "$1"|tail -n 1 -`
newfullname="$newname"."$extension"
if [[ -n "$newname" && "$filename" != "$newfullname" ]]; then
echo "$filename --> $newfullname"
mv -n "$1" "`dirname "$1"`/$newfullname"
fi
}
function main()
{
if [ -d "$1" ]; then
find "$1" -iname "*.ttf" -or -iname "*.ttc" -or -iname "*.otf" -or -iname "*.fon">.lists
while read line
do
renamefont "$line"
done < .lists
rm .lists
elif [ -s "$1" ]; then
renamefont "$1"
fi
}
main "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment