通常狀況我經過 iconfont 或者 icomoon 來實現圖標管理生成字體,導入到項目中使用。css
┌────────┐ ┌────────────┐ │iconfont│──┐ │ Project │ └────────┘ │ ┌────────────┐ ┌────────┐ │ ┌────────┐ │ ├─▶│created font│─▶│download│──▶│ │use font│ │ ┌────────┐ │ └────────────┘ └────────┘ │ └────────┘ │ │icomoon │──┘ └────────────┘ └────────┘
彩色圖標
。經過圖標平臺網站下載 svg 圖標,將圖標放到項目中管理,經過 svgtofont.js 工具來生成它,這將是新的字體圖標使用方式:html
┌────────────────────┐ │ Project │ │ │ ┌────────┐ │ ┌───────────┐ │ │iconfont│──┐ │ │ svg │──┐ │ └────────┘ │ ┌────────────┐ │ └───────────┘ │ │ ├─▶│download svg│──▶│ ┌───────────┐ │ │ ┌────────┐ │ └────────────┘ │┌──│create font│◀─┘ │ │icomoon │──┘ ││ └───────────┘ │ └────────┘ ││ ┌───────────┐ │ │└─▶│ use font │ │ │ └───────────┘ │ └────────────────────┘
新的方式維護方式好處:node
圖標
,再也不維護字體文件
SVG Symbol
在項目中使用讀取一組 SVG圖標並從SVG圖標輸出 TTF/EOT/WOFF/WOFF2/SVG 字體,字體生成器。git
特性github
SVG Symbol
文件。https://github.com/uiwjs/iconsweb
https://github.com/uiwjs/file...npm
npm i svgtofont
簡單的使用方式c#
const svgtofont = require("svgtofont"); svgtofont({ src: path.resolve(process.cwd(), "icon"), // svg 圖標目錄路徑 dist: path.resolve(process.cwd(), "fonts"), // 輸出到指定目錄中 fontName: "svgtofont", // 設置字體名稱 css: true, // 生成字體文件 }).then(() => { console.log('done!'); });
const svgtofont = require("svgtofont"); const path = require("path"); svgtofont({ src: path.resolve(process.cwd(), "icon"), // svg 圖標目錄路徑 dist: path.resolve(process.cwd(), "fonts"), // 輸出到指定目錄中 fontName: "svgtofont", // 設置字體名稱 css: true, // 生成字體文件 startNumber: 20000, // unicode起始編號 svgicons2svgfont: { fontHeight: 1000, normalize: true }, // website = null, 沒有演示html文件 website: { title: "svgtofont", // Must be a .svg format image. logo: path.resolve(process.cwd(), "svg", "git.svg"), version: pkg.version, meta: { description: "Converts SVG fonts to TTF/EOT/WOFF/WOFF2/SVG format.", keywords: "svgtofont,TTF,EOT,WOFF,WOFF2,SVG" }, description: ``, links: [ { title: "GitHub", url: "https://github.com/jaywcjlove/svgtofont" }, { title: "Feedback", url: "https://github.com/jaywcjlove/svgtofont/issues" }, { title: "Font Class", url: "index.html" }, { title: "Unicode", url: "unicode.html" } ], footerInfo: `Licensed under MIT. (Yes it's free and <a href="https://github.com/jaywcjlove/svgtofont">open-sourced</a>` } }).then(() => { console.log('done!'); });;
svgtofont 提供 API,能夠一個一個的本身生成,也能夠自動經過上面方法自動生成api
const { createSVG, createTTF, createEOT, createWOFF, createWOFF2 } = require("svgtofont/src/utils"); const options = { ... }; createSVG(options) // SVG => SVG Font .then(UnicodeObjChar => createTTF(options)) // SVG Font => TTF .then(() => createEOT(options)) // TTF => EOT .then(() => createWOFF(options)) // TTF => WOFF .then(() => createWOFF2(options)) // TTF => WOFF2 .then(() => createSvgSymbol(options)) // SVG Files => SVG Symbol
svgtofont(options)
Type:String
Default value:dist
svg 圖標路徑bash
Type:String
Default value:svg
輸出到指定目錄
Type:String
Default value:iconfont
您想要的字體名稱。
Type:Number
Default value:10000
unicode起始編號
Type:String
Default value: font name
建立字體類名稱前綴,默認值字體名稱。
Type:Boolean
Default value:false
建立CSS / LESS文件,默認爲「true」。
這是 svgicons2svgfont 的設置
Type:String
Default value:'iconfont'
您想要的字體名稱,與前面 fontName
同樣。
Type:String
Default value: the options.fontName value
你想要的字體ID。
Type:String
Default value:''
你想要的字體樣式。
Type:String
Default value:''
你想要的字體粗細。
Type:Boolean
Default value:false
建立最大輸入圖標寬度的等寬字體。
Type:Boolean
Default value:false
計算字形的邊界並使其水平居中。
Type:Boolean
Default value:false
經過將圖標縮放到最高圖標的高度來標準化圖標。
Type:Number
Default value:MAX(icons.height)
輸出的字體高度(默認爲最高輸入圖標的高度)。
Type:Number
Default value:10e12
設置SVG路徑舍入。
Type:Number
Default value:0
字體降低。 本身修復字體基線頗有用。
警告: 降低是一個正值!
Type:Number
Default value:fontHeight - descent
字體上升。 僅當您知道本身在作什麼時才使用此選項。 爲您計算一個合適的值。
Type:String
Default value:undefined
字體 metadata。 你能夠設置任何
字符數據,但它是適合說起版權的地方。
Type:Function
Default value:console.log
容許您提供本身的日誌記錄功能。 設置爲 function(){}
禁用日誌記錄
這是 svg2ttf 的設置
Type:
String
版權字符串
Type:
String
用於覆蓋建立時間的Unix時間戳(以秒爲單位)
Type:
Number
font version string, can be Version x.y
or x.y
.
定義預覽Web內容。 例:
{ ... // website = null, no demo html files website: { title: "svgtofont", logo: path.resolve(process.cwd(), "svg", "git.svg"), version: pkg.version, meta: { description: "Converts SVG fonts to TTF/EOT/WOFF/WOFF2/SVG format.", keywords: "svgtofont,TTF,EOT,WOFF,WOFF2,SVG", favicon: "./favicon.png" }, footerLinks: [ { title: "GitHub", url: "https://github.com/jaywcjlove/svgtofont" }, { title: "Feedback", url: "https://github.com/jaywcjlove/svgtofont/issues" }, { title: "Font Class", url: "index.html" }, { title: "Unicode", url: "unicode.html" } ] } }
Type:String
Default value: index.ejs
自定義模板可自定義參數。 您能夠根據默認模板定義本身的模板。
{ website: { template: path.join(process.cwd(), "my-template.ejs") } }
Type:String
Default value:font-class
, Enum{font-class
,unicode
,symbol
}
設置默認主頁。
假設字體名稱定義爲 svgtofont
,默認主頁爲unicode
,將生成:
font-class.html index.html symbol.html svgtofont.css svgtofont.eot svgtofont.less svgtofont.svg svgtofont.symbol.svg svgtofont.ttf svgtofont.woff svgtofont.woff2
預覽demo font-class.html
, symbol.html
和 index.html
。自動生成樣式svgtofont.css
和 svgtofont.less
。
<svg class="icon" aria-hidden="true"> <use xlink:href="svgtofont.symbol.svg#svgtofont-git"></use> </svg>
<style> .iconfont { font-family: "svgtofont-iconfont" !important; font-size: 16px; font-style: normal; -webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0.2px; -moz-osx-font-smoothing: grayscale; } </style> <span class="iconfont"></span>
支持.less
和.css
樣式引用。
<link rel="stylesheet" type="text/css" href="node_modules/fonts/svgtofont.css"> <i class="svgtofont-apple"></i>
Licensed under the MIT License.