forked from opentypejs/opentype.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadwrite.html
87 lines (79 loc) · 2.54 KB
/
readwrite.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>OpenType writing</title>
<script type="text/javascript" src="dist/opentype.js"></script>
<style>
body {
font: 13px Helvetica, arial, freesans, sans-serif;
line-height: 1.4;
color: #333;
margin: 0 0 50px 0;
padding: 0;
}
.container {
width: 940px;
margin-left: auto;
margin-right: auto;
}
#glyphs {
clear: both;
}
.wrapper {
float: left;
margin: 5px;
border: 1px solid #ccc;
}
.wrapper span {
text-align: center;
background: #ddd;
display: block;
}
</style>
</head>
<body>
<div class="container">
<h1 id="fontFamilyName"></h1>
<p>This font is generated dynamically in the browser.</p>
<button onclick="inFont.download()">Download Font</button>
<div id="glyphs"></div>
</div>
<script>
var inFont, outFont;
// Create a canvas and adds it to the document.
// Returns the 2d drawing context.
function createGlyphCanvas(glyph, size) {
var canvasId, html, glyphsDiv, wrap, canvas, ctx;
canvasId = 'c' + glyph.index;
html = '<div class="wrapper" style="width:' + size + 'px"><canvas id="' + canvasId + '" width="' + size + '" height="' + size + '"></canvas><span>' + glyph.index + '</span><span>' + glyph.name + '</span><span>U ' + glyph.unicode + '</span></div>';
glyphsDiv = document.getElementById('glyphs');
wrap = document.createElement('div');
wrap.innerHTML = html;
glyphsDiv.appendChild(wrap);
canvas = document.getElementById(canvasId);
ctx = canvas.getContext('2d');
return ctx;
}
opentype.load('fonts/FiraSansOT-Medium.otf', function (err, font) {
if (err) {
console.log(err);
}
inFont = font;
var buffer = inFont.toBuffer();
outFont = opentype.parse(buffer);
document.getElementById('fontFamilyName').innerHTML = outFont.familyName;
for (var i = 0; i < outFont.glyphs.length; i++) {
var glyph = outFont.glyphs[i];
var ctx = createGlyphCanvas(glyph, 150);
var x = 50;
var y = 120;
var fontSize = 72;
glyph.draw(ctx, x, y, fontSize);
glyph.drawPoints(ctx, x, y, fontSize);
glyph.drawMetrics(ctx, x, y, fontSize);
}
});
</script>
</body>
</html>