为何hb-shape无法读取通过变量传入的字体文件?
解决hb-shape无法处理脚本变量传入路径的问题
问题现象
在Shell中直接调用harfbuzz的hb-shape工具可以正常执行:
$ hb-shape "/System/Library/Fonts/Supplemental/Courier New.ttf" t [t=0+1229]
但将路径通过变量传入脚本执行时,却报错:
hb-shape: /System/Library/Fonts/Supplemental/Courier New.ttf : Failed reading file Try `hb-shape --help' for more information.
初始脚本
原脚本内容如下:
#!/opt/homebrew/Cellar/bash/5.2.26/bin/bash mapfile -t fonts < <(fc-list : file | head -4 | tr -d ':' | awk '{$1=$1};1') for font in "${fonts[@]}"; do hb-shape "$font" t done
尝试过的写法
为解决问题,尝试了以下几种调用方式,均无效:
hb-shape "$font" thb-shape "${font}" thb-shape '"'$font'"' t
系统环境
- 系统:Mac OS 14.1.2
- 终端:zsh 5.9 (x86_64-apple-darwin23.0)
- 脚本Shell:Bash 5.2.26
- fc-list(fontconfig组件):fontconfig version 2.15.0
- hb-shape(HarfBuzz):8.4.0
问题根源
通过hexdump排查发现,fonts数组中的每个路径变量末尾都包含换行符(ASCII码0a),导致hb-shape无法正确识别文件路径。
解决方案
使用printf '%s' "$font"去除路径末尾的换行符,修改后的脚本如下:
mapfile -t fonts < <(fc-list : file | tr -d ':' | awk '{$1=$1};1') for font in "${fonts[@]}"; do printf "'$font'"; hb-shape "$(printf '%s' "$font")" ⌘ done | grep -v '\.notdef\|gid0\|\.null'
内容的提问来源于stack exchange,提问作者RyanJM
相关产品推荐
相关产品推荐

