-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmap.js
79 lines (72 loc) · 2.74 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// CharacterMap builds a map of font-specific glyph codes from unicode characaters.
// It is also able to suggest a substitution for a given glyph, based on the
// substitutionFrequencies table.
function RandomSelector(groups) {
this.groups = groups;
this.total = 0;
for (var k in this.groups) {
this.total += this.groups[k];
}
}
RandomSelector.prototype.choice = function() {
var index = Math.random() * this.total;
for (var k in this.groups) {
index -= this.groups[k];
if (index <= 0) {
return k;
}
}
};
RandomSelector.prototype.has = function(key) {
return key in this.groups;
};
function CharacterMap(page, substitutionGroups) {
var map = {};
var characterAnalyzer = {
showGlyph: function(font, matrix, glyph, unicode) {
var fontName = font.getName();
if (!(fontName in map)) {
map[fontName] = {};
}
map[fontName][unicode] = glyph;
},
fillText: function(text) { text.walk(this); },
clipText: function(text) { text.walk(this); },
strokeText: function(text) { text.walk(this); },
clipStrokeText: function(text) { text.walk(this); },
ignoreText: function(text) { text.walk(this); }
}
page.run(characterAnalyzer, Identity);
var fontSubstitutionGroups = {};
var fontSubstitutionGroupScores = {};
for (var fontName in map) {
fontSubstitutionGroups[fontName] = {};
fontSubstitutionGroupScores[fontName] = {};
for (var group in substitutionGroups) {
fontSubstitutionGroups[fontName][group] = {};
var total = 0, included = 0;
for (var chr in substitutionGroups[group]) {
total++;
if (chr.charCodeAt(0) in map[fontName]) {
included++;
fontSubstitutionGroups[fontName][group][chr] = substitutionGroups[group][chr];
}
}
fontSubstitutionGroups[fontName][group] = new RandomSelector(fontSubstitutionGroups[fontName][group]);
fontSubstitutionGroupScores[fontName][group] = included / total;
}
}
this.anonymize = function(fontName, unicode) {
var score = -1;
for (var group in fontSubstitutionGroups[fontName]) {
if (fontSubstitutionGroups[fontName][group].has(String.fromCharCode(unicode))) {
unicode = fontSubstitutionGroups[fontName][group].choice().charCodeAt(0);
score = fontSubstitutionGroupScores[fontName][group];
break;
}
}
var glyph = map[fontName][unicode];
return {unicode: unicode, glyph: glyph, score: score};
};
}
exports.CharacterMap = CharacterMap;