svgtofont.js 自動生成圖標字體和彩色圖標文件

通常狀況我經過 iconfont 或者 icomoon 來實現圖標管理生成字體,導入到項目中使用。css

┌────────┐                                  ┌────────────┐
│iconfont│──┐                               │  Project   │
└────────┘  │  ┌────────────┐  ┌────────┐   │ ┌────────┐ │
            ├─▶│created font│─▶│download│──▶│ │use font│ │
┌────────┐  │  └────────────┘  └────────┘   │ └────────┘ │
│icomoon │──┘                               └────────────┘
└────────┘

使用說明

  1. 圖標字體只能被渲染成單色,不能生成 彩色圖標
  2. 圖標將放到平臺中維護,下載字體文件到項目中使用,這樣團隊維護生成字體成本將很是高。

經過圖標平臺網站下載 svg 圖標,將圖標放到項目中管理,經過 svgtofont.js 工具來生成它,這將是新的字體圖標使用方式:html

┌────────────────────┐
                                │      Project       │
                                │                    │
┌────────┐                      │   ┌───────────┐    │
│iconfont│──┐                   │   │    svg    │──┐ │
└────────┘  │  ┌────────────┐   │   └───────────┘  │ │
            ├─▶│download svg│──▶│   ┌───────────┐  │ │
┌────────┐  │  └────────────┘   │┌──│create font│◀─┘ │
│icomoon │──┘                   ││  └───────────┘    │
└────────┘                      ││  ┌───────────┐    │
                                │└─▶│ use font  │    │
                                │   └───────────┘    │
                                └────────────────────┘

新的方式維護方式好處:node

  1. 不須要知道第三方平臺帳號維護,將圖標下載到項目中維護圖標,再也不維護字體文件
  2. 生成彩色圖標文件 SVG Symbol 在項目中使用

svgtofont

讀取一組 SVG圖標並從SVG圖標輸出 TTF/EOT/WOFF/WOFF2/SVG 字體,字體生成器。git

特性github

  1. 支持的字體格式:WOFF2,WOFF,EOT,TTF和SVG。
  2. 支持 SVG Symbol 文件。
  3. 自動生成模板(例如css,less等),能夠直接使用。
  4. 自動生成預覽網站,預覽字體文件。

實例

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!');
});;

API

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

options

svgtofont(options)

dist

Type: String
Default value: dist

svg 圖標路徑bash

src

Type: String
Default value: svg

輸出到指定目錄

fontName

Type: String
Default value: iconfont

您想要的字體名稱。

unicodeStart

Type: Number
Default value: 10000

unicode起始編號

clssaNamePrefix

Type: String
Default value: font name

建立字體類名稱前綴,默認值字體名稱。

css

Type: Boolean
Default value: false

建立CSS / LESS文件,默認爲「true」。

svgicons2svgfont

這是 svgicons2svgfont 的設置

svgicons2svgfont.fontName

Type: String
Default value: 'iconfont'

您想要的字體名稱,與前面 fontName 同樣。

svgicons2svgfont.fontId

Type: String
Default value: the options.fontName value

你想要的字體ID。

svgicons2svgfont.fontStyle

Type: String
Default value: ''

你想要的字體樣式。

svgicons2svgfont.fontWeight

Type: String
Default value: ''

你想要的字體粗細。

svgicons2svgfont.fixedWidth

Type: Boolean
Default value: false

建立最大輸入圖標寬度的等寬字體。

svgicons2svgfont.centerHorizontally

Type: Boolean
Default value: false

計算字形的邊界並使其水平居中。

svgicons2svgfont.normalize

Type: Boolean
Default value: false

經過將圖標縮放到最高圖標的高度來標準化圖標。

svgicons2svgfont.fontHeight

Type: Number
Default value: MAX(icons.height)

輸出的字體高度(默認爲最高輸入圖標的高度)。

svgicons2svgfont.round

Type: Number
Default value: 10e12

設置SVG路徑舍入。

svgicons2svgfont.descent

Type: Number
Default value: 0

字體降低。 本身修復字體基線頗有用。

警告: 降低是一個正值!

svgicons2svgfont.ascent

Type: Number
Default value: fontHeight - descent

字體上升。 僅當您知道本身在作什麼時才使用此選項。 爲您計算一個合適的值。

svgicons2svgfont.metadata

Type: String
Default value: undefined

字體 metadata。 你能夠設置任何
字符數據,但它是適合說起版權的地方。

svgicons2svgfont.log

Type: Function
Default value: console.log

容許您提供本身的日誌記錄功能。 設置爲 function(){} 禁用日誌記錄

svg2ttf

這是 svg2ttf 的設置

svg2ttf.copyright

Type: String

版權字符串

svg2ttf.ts

Type: String

用於覆蓋建立時間的Unix時間戳(以秒爲單位)

svg2ttf.version

Type: Number

font version string, can be Version x.y or x.y.

website

定義預覽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"
      }
    ]
  }
}

website.template

Type: String
Default value: index.ejs

自定義模板可自定義參數。 您能夠根據默認模板定義本身的模板。

{
  website: {
    template: path.join(process.cwd(), "my-template.ejs")
  }
}

website.index

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.htmlindex.html。自動生成樣式svgtofont.csssvgtofont.less

symbol svg

<svg class="icon" aria-hidden="true">
  <use xlink:href="svgtofont.symbol.svg#svgtofont-git"></use>
</svg>

Unicode

<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">&#59907;</span>

Class Name

支持.less.css樣式引用。

<link rel="stylesheet" type="text/css" href="node_modules/fonts/svgtofont.css">
<i class="svgtofont-apple"></i>

License

Licensed under the MIT License.

相關文章
相關標籤/搜索