最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

WebFont智能压缩工具——字蛛1.0.0正式版发布_html/css

来源:动视网 责编:小采 时间:2020-11-27 16:27:30
文档

WebFont智能压缩工具——字蛛1.0.0正式版发布_html/css

WebFont智能压缩工具——字蛛1.0.0正式版发布_html/css_WEB-ITnose:字蛛是一个 WebFont 智能压缩工具,它能自动化分析页面中所使用的 WebFont 并进行按需压缩,通常好几 MB 的中文字体可以被压缩成几 KB 大小。 字蛛主页: http://font-spider.org 字蛛从 2014 年 7 月诞生以来,时隔近两年,终于发布了 v1.0.0
推荐度:
导读WebFont智能压缩工具——字蛛1.0.0正式版发布_html/css_WEB-ITnose:字蛛是一个 WebFont 智能压缩工具,它能自动化分析页面中所使用的 WebFont 并进行按需压缩,通常好几 MB 的中文字体可以被压缩成几 KB 大小。 字蛛主页: http://font-spider.org 字蛛从 2014 年 7 月诞生以来,时隔近两年,终于发布了 v1.0.0


字蛛是一个 WebFont 智能压缩工具,它能自动化分析页面中所使用的 WebFont 并进行按需压缩,通常好几 MB 的中文字体可以被压缩成几 KB 大小。

字蛛主页: http://font-spider.org

字蛛从 2014 年 7 月诞生以来,时隔近两年,终于发布了 v1.0.0 正式版本,改进如下:

  1. 支持绝大多数的中英文 Truetype 字体
  2. 支持开源图标字体库 (New: v1.0.0新特性)
  3. 支持 CSS 伪元素解析,支持 content: "string" 与 content: attr(value) 表达式
  4. 支持远程页面解析,并支持资源映射
  5. 支持四种样式规则:

    面朝大海,春暖花开

得到 webFonts :

{ "demo-font": { "family": "demo-font", "src": "url("file:///Users/aui/Documents/demo-font.ttf")", "chars": "" }}

查找字符

利用 document.querySelectorAll() 来获取使用 WebFont 的字符:

// 获取当前节点所使用的 webFontfunction matchFontFamily(cssRule) { var style = cssRule.style; var family = style['font-family']; return webFonts[family];}// 将 fontFace 与元素、字符关联起来eachCssRuleList(function(cssRule) { if (cssRule instanceof CSSStyleRule) { var selector = cssRule.selectorText; var webfont = matchFontFamily(cssRule); if (webfont) { // 根据选择器来查找元素 var elems = document.querySelectorAll(selector); Array.prototype.forEach.call(elems, function(element) { // 获取元素的文本 webfont.chars += element.textContent; // 将元素与字体关联起来 elements[webfont.family].push(element); }); } }});

此时 webFonts :

{ "demo-font": { "family": "demo-font", "src": "url("file:///Users/aui/Documents/demo-font.ttf")", "chars": "面朝大海,春暖花开" }}

伪元素

// 处理伪元素,找到继承的 webFonteachCssRuleList(function(cssRule) { if (cssRule instanceof CSSStyleRule) { var selector = cssRule.selectorText; var pseudoName = /::?(?:before|after)$/i; if (!pseudoName.test(selector)) { return; } // 查找伪元素所在的节点 selector = selector.replace(pseudoName, ''); var elems = document.querySelectorAll(selector); // 获取伪元素 content 值 var content = cssRule.style.content.replace(/^["']|["']$/g, ''); for (var i = 0; i < elems.length; i ++) { var elem = elems[i]; for (var family in webFonts) { // 从伪元素自身不断冒泡,直到找到继承的字体 while (elem) { if (elements[family].indexOf(elem) !== -1) { webFonts[family].chars += content; break; } elem = elem.parentNode; } } } }});

此时 WebFont:

{ "demo-font": { "family": "demo-font", "src": "url("file:///Users/aui/Documents/demo-font.ttf")", "chars": "面朝大海,春暖花开————海子" }}

完整代码在线演示: https://jsfiddle.net/9ont96c4/2

至此,以上例子已经成功演示了字蛛爬虫查找字体、查找文本的工作原理。实际上 HTML 与 CSS 远比上面示例页面复杂,需要处理:

  1. 伪类选择器
  2. font 缩写
  3. 行内样式
  4. 完整的字体匹配算法

由于篇幅有限,上述细节部分可以参见 字蛛爬虫模块源码 。

文档

WebFont智能压缩工具——字蛛1.0.0正式版发布_html/css

WebFont智能压缩工具——字蛛1.0.0正式版发布_html/css_WEB-ITnose:字蛛是一个 WebFont 智能压缩工具,它能自动化分析页面中所使用的 WebFont 并进行按需压缩,通常好几 MB 的中文字体可以被压缩成几 KB 大小。 字蛛主页: http://font-spider.org 字蛛从 2014 年 7 月诞生以来,时隔近两年,终于发布了 v1.0.0
推荐度:
标签: 智能 压缩 html
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top