harfbuzz_rs
v2.0.1
harfbuzz_rs是Harfbuzz的高級接口,使用Rust以安全的方式暴露其最重要的功能。
Harfbuzz是用於執行複雜文本佈局的庫。它不執行任何圖紙。這是一個低級操作。如果您想簡單地在屏幕上繪製一些文本,則應該選擇另一個高級庫。但是,如果要構建一個庫,以在某些畫布上繪製文本或需要對高級文本佈局進行大量控制,那麼這是可以使用的正確庫。
要形成簡單的文本字符串,您只需從字體文件創建Font ,請填充一些文本的Buffer並調用shape函數。
use harfbuzz_rs :: * ;
let path = "path/to/some/font_file.otf" ;
let index = 0 ; //< face index in the font file
let face = Face :: from_file ( path , index ) ? ;
let mut font = Font :: new ( face ) ;
let buffer = UnicodeBuffer :: new ( ) . add_str ( "Hello World!" ) ;
let output = shape ( & font , buffer , & [ ] ) ;
// The results of the shaping operation are stored in the `output` buffer.
let positions = output . get_glyph_positions ( ) ;
let infos = output . get_glyph_infos ( ) ;
// iterate over the shaped glyphs
for ( position , info ) in positions . iter ( ) . zip ( infos ) {
let gid = info . codepoint ;
let cluster = info . cluster ;
let x_advance = position . x_advance ;
let x_offset = position . x_offset ;
let y_offset = position . y_offset ;
// Here you would usually draw the glyphs.
println ! ( "gid{:?}={:?}@{:?},{:?}+{:?}" , gid , cluster , x_advance , x_offset , y_offset ) ;
}這應該打印出類似於以下內容的內容:
gid41=0@741,0+0
gid70=1@421,0+0
gid77=2@258,0+0
gid77=3@253,0+0
gid80=4@510,0+0
gid1=5@227,0+0
gid56=6@874,0+0
gid80=7@498,0+0
gid83=8@367,0+0
gid77=9@253,0+0
gid69=10@528,0+0
gid2=11@276,0+0
默認情況下, x_advance , x_offset , y_advance和y_offset的值都在所謂的字體單元中給出。通過調用face.upem()您可以獲得特定face的每個em字體單元數量。該upem升值可用於將進步和偏移擴展到給定的字體大小。例如,如果要在16點(PT)大小上顯示字體,則意味著1 EM = 16 pt 。在此示例中,要將一個值(例如x_advance )轉換為從字體單元轉換為點,我們計算((x_advance * font_size) as f64) / (upem as f64) ,其中font_size = 16是一個可變的,可以在點中指定字體大小。
請注意,Harfbuzz在內部也支持縮放字體自身(使用font.set_scale(...)等),但我認為,如上所述,更容易擴展結果。
該板條箱經過測試,可與Harfbuzz版本2.0及更高版本一起使用。較舊的版本也可能起作用。我建議您靜態地鏈接由harfbuzz-sys Crate提供的Harfbuzz庫,該庫始終是最新的。
如果要使用RustType作為字體函數,則可以啟用rusttype功能。