您的位置:首页 > Web前端 > JavaScript

js分析 猫_眼_电_影 字体文件 @font-face

2018-05-12 21:35 459 查看

0. 参考

https://developer.mozilla.org/zh-CN/docs/Web/CSS/@font-face

这是一个叫做@font-face 的CSS @规则 ,它允许网页开发者为其网页指定在线字体。 通过这种作者自备字体的方式,@font-face 可以消除对用户电脑字体的依赖。

反爬虫解析-字体替换(天眼查/猫眼电影)  https://www.jianshu.com/p/79c4272c0969

woff 转 xml

猫眼破解数字反爬获取实时票房 https://zhuanlan.zhihu.com/p/33112359

专用工具 Font Creator 查看字体文件

1. 页面表现

In [3]: from fontTools.ttLib import TTFont

In [4]: font1 = TTFont('font1.woff')

In [5]: font2 = TTFont('font2.woff')

In [6]: font1_8 = font1['glyf']['uniEF04']
...: font2_8 = font2['glyf']['uniF72A']
...: font1_4 = font1['glyf']['uniF380']
...: font2_7 = font2['glyf']['uniF722']
...:

In [7]: font1_8?
Type:        Glyph
String form: <fontTools.ttLib.tables._g_l_y_f.Glyph object at 0x000000000573A978>
File:        e:\programdata\anaconda3\envs\py3\lib\site-packages\fonttools\ttlib\tables\_g_l_y_f.py
Docstring:   <no docstring>

In [8]: font1_8.coordinates
Out[8]: GlyphCoordinates([(177, 388),(69, 428),(69, 534),(69, 614),(181, 719),(369, 719),(483, 608),(483, 532),(483, 428),(377, 388),(443, 366),(512, 271),(512, 205),(512, 112),(382, -12),(170, -12),(105, 50),(41, 110),(41, 207),(41, 277),(111, 371),(159, 537),(159, 485),(225, 422),(277, 422),(325,
422),(360, 454),(393, 485),(393, 579),(326, 646),(224, 646),(159, 582),(131, 207),(131, 168),(165, 99),(202, 79),(236, 60),(277, 60),(309, 60),(360, 81),(381, 101),(422, 140),(422, 268),(338, 350),(212, 350),(131, 269)])

In [9]: font2_8.coordinates
Out[9]: GlyphCoordinates([(177, 388),(69, 428),(69, 534),(69, 614),(181, 719),(369, 719),(483, 608),(483, 532),(483, 428),(377, 388),(443, 366),(512, 271),(512, 205),(512, 112),(382, -12),(170, -12),(105, 50),(41, 110),(41, 207),(41, 277),(111, 371),(159, 537),(159, 485),(225, 422),(277, 422),(325,
422),(360, 454),(393, 485),(393, 579),(326, 646),(224, 646),(159, 582),(131, 207),(131, 168),(165, 99),(202, 79),(236, 60),(277, 60),(309, 60),(360, 81),(381, 101),(422, 140),(422, 268),(338, 350),(212, 350),(131, 269)])

In [11]: font1_8.coordinates == font2_8.coordinates
Out[11]: True

In [12]: font1_8 == font2_8
Out[12]: True

In [13]: font1_4.coordinates
Out[13]: GlyphCoordinates([(323, 0),(323, 171),(13, 171),(13, 252),(339, 716),(411, 716),(411, 252),(508, 252),(508, 171),(411, 171),(411, 0),(323, 252),(323, 575),(99, 252)])

In [14]: font2_7.coordinates
Out[14]: GlyphCoordinates([(47, 622),(47, 707),(511, 707),(511, 638),(476, 602),(409, 505),(341, 384),(290, 261),(271, 197),(246, 107),(238, 0),(147, 0),(148, 42),(165, 144),(181, 204),(212, 324),(271, 435),(301, 492),(365, 584),(398, 622)])

In [15]: font1_4.coordinates != font2_7.coordinates
Out[15]: True

In [16]: font1_4 != font2_7
Out[16]: True

In [17]: font1_4.coordinates != font1_8.coordinates
Out[17]: True

In [18]: font1_4 != font1_8
Out[18]: True
View Code

3. 根据基准字体文件为新字体文件建立映射关系的代码实现

运行时访问不了 .coordinates 和 .compileCoordinates() 属性???

'Glyph' object has no attribute 'coordinates'

from fontTools.ttLib import TTFont
font1 = TTFont('font1.woff')

print(font1['glyf'].keys())
keys = font1['glyf'].keys()
values = list(' .4209716385')
# 构建基准 {name: num}
dict1 = dict((k,v) for k,v in zip(keys, values))
print(dict1)

font2 = TTFont('font2.woff')
dict2 = {}
for key in font2['glyf'].keys():
for k, v in dict1.items():
# 通过比较 字形定义 填充新的name和num映射关系
if font1['glyf'][k] == font2['glyf'][key]:
dict2[key] = v.strip()
break
print(dict2)

 

 3.1 运行结果

 

 3.2 通过下图确认输出 dict2 的正确性

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: