Skip to content

devicexx/font-spider

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

字蛛

NPM Version NPM Downloads Node.js Version Build Status

字蛛是一个中文 WebFont 自动化压缩工具,它能智能分析页面使用的 WebFont 并进行按需压缩。

官方网站:http://font-spider.org

特性

  1. 按需压缩:从原字体中剔除没有用到的字符,可以将数 MB 大小的中文字体压缩成几十 KB
  2. 本地处理:完全基于 HTML 与 CSS 分析进行本地处理,无需 js 与服务端辅助
  3. 自动转码:将字体转码成所有浏览器支持的格式,包括老旧的 IE6 与现代浏览器

New: 字蛛 v1.0.0 版本支持图标字体!更新日志

安装

安装好 nodejs,然后执行:

npm install font-spider -g

使用范例

一、书写 CSS

/*声明 WebFont*/
@font-face {
  font-family: 'pinghei';
  src: url('../font/pinghei.eot');
  src:
    url('../font/pinghei.eot?#font-spider') format('embedded-opentype'),
    url('../font/pinghei.woff') format('woff'),
    url('../font/pinghei.ttf') format('truetype'),
    url('../font/pinghei.svg') format('svg');
  font-weight: normal;
  font-style: normal;
}

/*使用指定字体*/
.home h1, .demo > .test {
    font-family: 'pinghei';
}

特别说明: @font-face 中的 src 定义的 .ttf 文件必须存在,其余的格式将由工具自动生成

二、压缩 WebFont

font-spider [options] <htmlFile1 htmlFile2 ...>

htmlFiles

一个或多个页面地址,支持 http 形式。

例如:

font-spider dest/news.html dest/index.html dest/about.html

如果有多个页面依赖相同的字体,请都传递进来

options

-h, --help                    输出帮助信息
-V, --version                 输出当前版本号
--info                        输出 WebFont 的 JSON 描述信息,不压缩与转码
--ignore <pattern>            忽略的文件配置(支持正则表达式)
--map <remotePath,localPath>  映射 CSS 内部 HTTP 路径到本地(支持正则表达式)
--no-backup                   关闭字体备份功能
--debug                       调试模式,打开它可以显示 CSS 解析错误

参数使用示例

使用通配符压缩多个 HTML 文件关联的 WebFont:

font-spider dest/*.html

--info 查看网站所应用的 WebFont:

font-spider --info http://fontawesome.io

--ignore 忽略文件:

font-spider --ignore "icon\\.css$" dest/*.html

--map 参数将线上的页面的 WebFont 映射到本地来进行压缩(本地路径必须使用绝对路径):

font-spider --map "http://font-spider.org/font,/Website/font" http://font-spider.org/index.html

构建插件

API

font-spider 包括爬虫与压缩器模块,接口文档:API.md

限制

  • 不支持 javascript 动态插入的元素与样式
  • .otf 字体需要转换成 .ttf 格式才能被压缩(免费 ttf 字体资源
  • 仅支持 utf-8 编码的 HTML 与 CSS 文件
  • CSS content 仅支持 content: 'prefix'content: attr(value) 这两种形式

字体兼容性参考

格式 IE Firefox Chrome Safari Opera iOS Safari Android Browser Chrome for Android
.eot 6 -- -- -- -- -- -- --
.woff 9 3.6 5 5.1 11.1 5.1 4.4 36
.ttf -- 3.5 4 3.1 10.1 4.3 2.2 36
.svg -- -- 4 3.2 9.6 3.2 3 36

来源:http://caniuse.com/#feat=fontface

贡献者

免费字体

相关链接

About

中文 WebFont 自动化压缩工具

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 86.6%
  • HTML 11.1%
  • CSS 2.3%