-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacsymbol.html
217 lines (199 loc) · 7.33 KB
/
macsymbol.html
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>mac symbol</title>
<style>
.mono {font-family: monospace, "Courier New"; font-size: 12px;}
.spaces {white-space: pre-wrap;}
.good {color: green;}
.bad {color: red}
</style>
</head>
<body>
<br>testing why mac symbol font is not detected when devicePixelRatio == 1<br><br>
<table width="670">
<col width="50%"><col width="50%">
<tr>
<td><button onClick="getFonts(1)"> run 1 </button></td>
<td><button onClick="getFonts(2)"> run 2 </button></td>
</tr>
<tr><td colspan="2"><hr></td></tr>
<tr>
<td><div id="base1" class="mono spaces"></div></td>
<td><div id="base2" class="mono spaces"></div></td>
</tr>
<tr><td colspan="2"><hr></td></tr>
<tr>
<td><div id="detect1" class="mono spaces"></div></td>
<td><div id="detect2" class="mono spaces"></div></td>
</tr>
<tr><td colspan="2"><hr></td></tr>
<tr>
<td><div id="run1" class="mono spaces"></div></td>
<td><div id="run2" class="mono spaces"></div></td>
</tr>
</table>
<script>
'use strict';
let s0 = "<span class='",
sb = s0+"bad'>",
sg = s0+"good'>",
sc = "</span>"
function mini(str, call, log = true) {
// https://stackoverflow.com/a/22429679
const json = `${JSON.stringify(str)}`
let i, len, hash = 0x811c9dc5
for (i = 0, len = json.length; i < len; i++) {
hash = Math.imul(31, hash) + json.charCodeAt(i) | 0
}
return ('0000000' + (hash >>> 0).toString(16)).slice(-8)
}
function getFonts(runtype) {
try {
let baseFonts = ['monospace','sans-serif','serif','-apple-system',]
let fntStrA = "mmmLLLmmmWWWwwwmmmllliii"
let fntList = ['Arial','I Am a Fake Font','Symbol']
const doc = document
const id = `font-fingerprint`
const div = doc.createElement('div')
div.setAttribute('id', id)
doc.body.appendChild(div)
doc.getElementById(id).innerHTML = `
<style>
#${id}-detector {
--font: '';
position: absolute !important;
left: -9999px!important;
font-size: 256px !important;
font-style: normal !important;
font-weight: normal !important;
letter-spacing: normal !important;
line-break: auto !important;
line-height: normal !important;
text-transform: none !important;
text-align: left !important;
text-decoration: none !important;
text-shadow: none !important;
white-space: normal !important;
word-break: normal !important;
word-spacing: normal !important;
/* in order to test scrollWidth, clientWidth, etc. */
padding: 0 !important;
margin: 0 !important;
/* in order to test inlineSize and blockSize */
writing-mode: horizontal-tb !important;
/* for transform and perspective */
transform-origin: unset !important;
perspective-origin: unset !important;
}
#${id}-detector::after {
font-family: var(--font);
content: '` + fntStrA + `';
}
</style>
<span id="${id}-detector"></span>`
const span = doc.getElementById(`${id}-detector`)
const style = getComputedStyle(span)
const pixelsToNumber = pixels => +pixels.replace('px','')
const originPixelsToNumber = pixels => 2*pixels.replace('px', '')
const detectedViaPixelNumber = new Set()
const detectedViaOffset = new Set()
const detectedViaTransformNumber = new Set()
const getDimensions = (span, style) => {
const transform = style.transformOrigin.split(' ')
const dimensions = {
offsetWidth: span.offsetWidth,
offsetHeight: span.offsetHeight,
npixelHeight: pixelsToNumber(style.height),
npixelWidth: pixelsToNumber(style.width),
ntransformHeight: originPixelsToNumber(transform[1]),
ntransformWidth: originPixelsToNumber(transform[0]),
}
return dimensions
}
const base = baseFonts.reduce((acc, font) => {
span.style.setProperty('--font', font)
const dimensions = getDimensions(span, style)
acc[font.split(",")[0]] = dimensions
return acc
}, {})
let display = []
for (const k of Object.keys(base)) {
display.push("<u>"+ k + "</u>")
display.push(" offset: " + base[k]["offsetWidth"] + " x " + base[k]["offsetHeight"])
display.push(" npixel: " + base[k]["npixelWidth"] + " x " + base[k]["npixelHeight"])
display.push(" ntransform: " + base[k]["ntransformWidth"] + " x " + base[k]["ntransformHeight"])
}
let hash = mini(base)
let element = document.getElementById("base"+ runtype)
element.innerHTML = "<b><u>BASEFONTS:</u></b> " + hash +"<br><br>" + display.join("<br>")
let aTestsValid = [
["offset", detectedViaOffset],
["npixel", detectedViaPixelNumber],
["ntransform", detectedViaTransformNumber],
]
let isDetected = false
let oResults = {}
fntList.forEach(font => {
oResults[font] = {}
baseFonts.forEach(basefont => {
oResults[font][basefont] = []
isDetected = false // reset
const family = "'"+ font +"', "+ basefont
span.style.setProperty('--font', family)
const style = getComputedStyle(span)
const dimensions = getDimensions(span, style)
basefont = basefont.split(",")[0] // switch to short generic name
aTestsValid.forEach(function(pair) {
let wName = pair[0] +"Width", hName = pair[0] +"Height"
if (dimensions[wName] != base[basefont][wName] || dimensions[hName] != base[basefont][hName]) {
pair[1].add(font +":"+ basefont +":"+ dimensions[wName] +" x "+ dimensions[hName])
isDetected = true
}
oResults[font][basefont].push(pair[0].padStart(12))
oResults[font][basefont].push("base: ".padStart(20) + base[basefont][wName] +" x "+ base[basefont][hName])
oResults[font][basefont].push("got: ".padStart(20) + dimensions[wName] +" x "+ dimensions[hName])
oResults[font][basefont].push("result: ".padStart(20) + (isDetected ? sg+ "detected" : sb+ "not detected") + sc)
})
return
})
})
// detected
const fontsOffset = [...detectedViaOffset]
const fontsPixelNumber = [...detectedViaPixelNumber]
const fontsTransformNumber = [...detectedViaTransformNumber]
let fontsAll = fontsOffset.concat(fontsPixelNumber)
fontsAll = fontsAll.concat(fontsTransformNumber)
hash = mini(fontsAll)
//console.debug(fontsOffset)
//console.debug(fontsPixelNumber)
//console.debug(fontsTransformNumber)
//console.debug(fontsAll)
display = ["<u><b>DETECTED:</b></u> " + hash +"<br>"]
display.push("offset: <br>" + (fontsOffset.length ? " - "+ fontsOffset.join("<br> - ") : " - nothing found" ) +"<br>")
display.push("npixel: <br>" + (fontsPixelNumber.length ? " - "+ fontsPixelNumber.join("<br> - ") : " - nothing found" ) +"<br>")
display.push("ntransform: <br>" + (fontsTransformNumber.length ? " - "+ fontsTransformNumber.join("<br> - ") : " - nothing found" ) +"<br>")
element = document.getElementById("detect"+ runtype)
element.innerHTML = display.join("<br>")
// detailed results
//console.debug(oResults)
display = []
for (const k of Object.keys(oResults).sort()) {
display.push("<br><b><u>"+ k +"</u></b>")
for (const j of Object.keys(oResults[k])) {
display.push("* <u>"+ j +"</u>")
let tmpArray = oResults[k][j]
display.push(tmpArray.join("<br>") +"<br>")
}
}
hash = mini(display)
element = document.getElementById("run"+ runtype)
element.innerHTML = "<b><u>DETAIL:</u></b> " + hash +"<br>"+ display.join("<br>")
} catch(e) {
console.debug(e.name, e.message)
}
}
</script>
</body>
</html>