samsa core
1.0.0
用於處理TrueType字體文件的庫。
特徵:
此存儲庫用於版本2開發。版本1在SAMSA GUI存儲庫中管理。
基本步驟是:
renderText()獲取SVG字符串這是node.js的示例代碼,從磁盤加載filename ,創建一個samsafont對象,創建一個Samsainstance對象,其中設置為某些位置的變體軸,使字符串hello, world!作為SVG,然後將SVG保存到文件render.svg 。
const nodeBuffer = fs . readFileSync ( filename ) ;
const arrayBuffer = nodeBuffer . buffer ;
const samsaBuffer = new SamsaBuffer ( arrayBuffer ) ;
const font = new SamsaFont ( samsaBuffer ) ;
const instance = font . instance ( { wght : 900 , wdth : 200 } ) ;
const svg = instance . renderText ( { text : "hello, world!" , fontSize : 72 } ) ;
fs . writeFileSync ( "render.svg" , svg ) ;在瀏覽器中,您可以獲得一個陣列,然後對其進行類似的處理。所得的SVG可以插入DOM。
const samsaBuffer = new SamsaBuffer ( arrayBuffer ) ;
const font = new SamsaFont ( samsaBuffer ) ;
const instance = font . instance ( { wght : 900 , wdth : 200 } ) ;
const svg = instance . renderText ( { text : "hello, world!" , fontSize : 72 } ) ;
document . getElementById ( "myDiv" ) . innerHTML = svg ;如果從遠程URL加載字體文件,則可能會使用fetch()然後使用response.arrayBuffer()獲取arrayBuffer。如果將字體文件拖動到瀏覽器上,則通過枚舉e.dataTransfer.items數組來獲得陣列,其中e是drop事件。對於數組中的每個item (可能有多個項目),如果其kind屬性等於file ,則set file = item.getAsFile()和Promise file.arrayBuffer()將產生陣列Buffer。
該圖說明了SAMSA如何從陣列延遲創建Samsafont對象,從Samsafont創建Samsainstance,然後將文本呈現為SVG。
流程圖TD
f1 [ttf] - > a [arraybuffer]
f2 [woff2] - > a [arraybuffer]
a [arraybuffer] - > b [samsabuffer]
b-> c [samsafont]
b [samsabuffer] - > |“ samsabuffer.decodewoff2()” | C [samsafont]
c-> |“ samsafont.instance(軸賽)” | D(samsainstance)
d-> |“ samsainstance.rendertext({text:myText,fontsize:mysize,color:mycolor:mycolor})” | E(SVG)