Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions public/index.html
92 changes: 67 additions & 25 deletions public/js/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Code from
// http://nodebox.github.io/opentype.js/

var fileButton, fontFamily;
var pageSelected, font, fontScale, fontSize, fontBaseline, glyphScale, glyphSize, glyphBaseline;

var cellCount = 200,
Expand All @@ -23,19 +22,28 @@ function cellSelect(event) {
}
}

function onSearchUnicode(e) {
if (!font) return;
// get the index by unicode, e.target.value
var inputValue = e.target.value.trim().toLowerCase();
for (var i = 0; i < font.numGlyphs; i++) {
var unicode = font.glyphs.glyphs[i].unicode;
if (unicode !== undefined && inputValue == unicode.toString(16)) {
displayGlyph(i);
displayGlyphData(i);
break;
}
}
}

$(document).ready(function() {
fontFamily = document.getElementById('font-family');
fileButton = document.getElementById('file');
var fontUrl = document.getElementById("font-url");
fontUrl.addEventListener('input', onFetchFile);
var fileButton = document.getElementById('file');
fileButton.addEventListener('change', onReadFile, false);
var fontFileName = 'fonts/arialbd.ttf';
opentype.load(fontFileName, function(err, font) {
var amount, glyph, ctx, x, y, fontSize;
if (err) {
showErrorMessage(err.toString());
return;
}
onFontLoaded(font);
});
var searchButton = document.getElementById('search-unicode');
searchButton.addEventListener('input', onSearchUnicode);
fetchFile('fonts/arialbd.ttf');
prepareGlyphList();

$(".nicescroll-left").niceScroll({
Expand Down Expand Up @@ -150,7 +158,7 @@ function renderGlyphItem(canvas, glyphIndex) {
ctx.fillStyle = '#AAA';
ctx.font = '9px "Open Sans"';
ctx.fillText(glyphIndex, 2, cellHeight - 2);
var glyph = font.glyphs[glyphIndex],
var glyph = font.glyphs.glyphs[glyphIndex],
glyphWidth = glyph.advanceWidth * fontScale,
xmin = (cellWidth - glyphWidth) / 2,
xmax = (cellWidth + glyphWidth) / 2,
Expand Down Expand Up @@ -209,20 +217,33 @@ function initGlyphDisplay() {
hline('Descender', font.tables.os2.sTypoDescender);
}

function onFetchFile(e) {
fetchFile(e.target.value);
}

function fetchFile(filename) {
console.log("fetch file: " + filename);
if (filename.trim().length === 0) return;
fetch(filename)
.then(res => {
if (res.status !== 200) {
throw new Error("Network error " + res.status);
}
return res.arrayBuffer()
})
.then(data => loadFont(data))
.catch(err => {
console.log(err)
});
}

function onReadFile(e) {
var file = e.target.files[0];
console.log("read file: " + file.name);
var reader = new FileReader();

reader.onload = function(e) {
try {
font = opentype.parse(e.target.result);
// fontFamily.innerHTML = font.familyName || this.files[0].name.replace(/\.[^/.]+$/, "");
showErrorMessage('');
onFontLoaded(font);
} catch (err) {
showErrorMessage(err.toString());
throw (err);
}
loadFont(e.target.result);
};
reader.onerror = function(err) {
showErrorMessage(err.toString());
Expand All @@ -231,6 +252,27 @@ function onReadFile(e) {
reader.readAsArrayBuffer(file);
}

function loadFont(buffer) {
try {
if (window.Module.decompress === undefined) {
font = opentype.parse(buffer);
} else {
var decompressResult = window.Module.decompress(buffer);
if (!decompressResult) {
// decompress failed, use opentype to parse the file content directly.
font = opentype.parse(buffer);
} else {
// use opentype to parse decompress result.
font = opentype.parse(decompressResult);
}
}
onFontLoaded(font);
} catch (err) {
console.log(err)
showErrorMessage(err.toString());
}
}

function displayGlyphData(glyphIndex) {
var container = document.getElementById('glyph-data');
var holdtext = document.getElementById('copy-char');
Expand All @@ -239,7 +281,7 @@ function displayGlyphData(glyphIndex) {
container.innerHTML = '';
return;
}
var glyph = font.glyphs[glyphIndex],
var glyph = font.glyphs.glyphs[glyphIndex],
html;
html = '<div><dt>name</dt><dd>' + glyph.name + '</dd></div>';

Expand Down Expand Up @@ -289,7 +331,7 @@ function displayGlyph(glyphIndex) {

if (glyphIndex < 0) return;

var glyph = font.glyphs[glyphIndex],
var glyph = font.glyphs.glyphs[glyphIndex],
glyphWidth = glyph.advanceWidth * glyphScale,
xmin = (canvas.width / window.devicePixelRatio - glyphWidth) / 2,
xmax = (canvas.width / window.devicePixelRatio + glyphWidth) / 2,
Expand Down Expand Up @@ -331,7 +373,7 @@ function drawPoints(glyph, ctx, x, y, fontSize) {
x = x !== undefined ? x : 0;
y = y !== undefined ? y : 0;
fontSize = fontSize !== undefined ? fontSize : 24;
scale = 1 / glyph.font.unitsPerEm * fontSize;
scale = 1 / font.unitsPerEm * fontSize;

blueCircles = [];
redCircles = [];
Expand Down
13 changes: 12 additions & 1 deletion views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<link href='css/index.css' rel='stylesheet' type='text/css'/>
<script src="js/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="js/jquery.nicescroll.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/opentype.js/0.4.4/opentype.min.js"></script>
<script src="https://opentype.js.org/dist/opentype.js"></script>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested with opentype 1.3.4. should add comment for the version?

<script src="https://unpkg.com/wawoff2@2.0.1/build/decompress_binding.js" type="text/javascript"></script>
<script src="js/index.js" type="text/javascript"></script>
</head>
<body>
Expand All @@ -35,6 +36,16 @@
<div style="position:fixed;left:200px;width:475px;top:0;bottom:0;background:#1D242E;padding:20px;">
<div style="position:absolute;top:20px;left:0;right:0;bottom:0;z-index:2;">
<div class="nicescroll-center" style="overflow:auto;height:100%;overflow-x:hidden;padding:20px;padding-top:0;">
<div style="height:40px;width:100%;border-radius:4px;">
or input font url
<input id="font-url" type="text" placeholder="input url of font file"/>
</div>

<div style="height:40px;width:100%;border-radius:4px;">
search unicodes
<input id="search-unicode" type="text" placeholder="input unicode"/>
</div>

<div style="font-size: 25px;margin-top:5px;color:#5B6971;font-weight: 100;letter-spacing:1px;text-align:left;margin-bottom:15px;">
Font
</div>
Expand Down