2017-08-08 14:23:30 -04:00
|
|
|
// pathfinder/demo/src/index.ts
|
|
|
|
|
2017-08-10 21:38:54 -04:00
|
|
|
const base64js = require('base64-js');
|
2017-08-08 14:23:30 -04:00
|
|
|
const opentype = require('opentype.js');
|
|
|
|
|
2017-08-10 21:38:54 -04:00
|
|
|
const TEXT: string = "A";
|
|
|
|
const FONT_SIZE: number = 16.0;
|
|
|
|
|
|
|
|
const PARTITION_FONT_ENDPOINT_URL: string = "/partition-font";
|
|
|
|
|
|
|
|
class PathfinderMeshes {
|
|
|
|
constructor(encodedResponse: string) {
|
|
|
|
const response = JSON.parse(encodedResponse);
|
|
|
|
if (!('Ok' in response))
|
|
|
|
throw new Error("Failed to partition the font!");
|
|
|
|
const meshes = response.Ok;
|
|
|
|
this.bQuads = base64js.toByteArray(meshes.bQuads);
|
|
|
|
this.bVertices = base64js.toByteArray(meshes.bVertices);
|
2017-08-08 14:23:30 -04:00
|
|
|
}
|
|
|
|
|
2017-08-10 21:38:54 -04:00
|
|
|
bQuads: ArrayBuffer;
|
|
|
|
bVertices: ArrayBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AppController {
|
|
|
|
constructor() {}
|
|
|
|
|
2017-08-08 14:23:30 -04:00
|
|
|
start() {
|
|
|
|
this.view = new PathfinderView(document.getElementById('pf-canvas') as HTMLCanvasElement);
|
|
|
|
|
|
|
|
this.loadFontButton = document.getElementById('pf-load-font-button') as HTMLInputElement;
|
|
|
|
this.loadFontButton.addEventListener('change', () => this.loadFont(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadFont() {
|
|
|
|
const file = this.loadFontButton.files[0];
|
2017-08-10 21:38:54 -04:00
|
|
|
const reader = new FileReader;
|
|
|
|
reader.addEventListener('loadend', () => {
|
|
|
|
this.fontData = reader.result;
|
|
|
|
this.fontLoaded();
|
|
|
|
}, false);
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
fontLoaded() {
|
|
|
|
this.font = opentype.parse(this.fontData);
|
|
|
|
if (!this.font.supported) {
|
|
|
|
window.alert("The font type is unsupported.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const glyphIDs = this.font.stringToGlyphs(TEXT).map(glyph => glyph.index);
|
|
|
|
|
|
|
|
const request = {
|
|
|
|
otf: base64js.fromByteArray(new Uint8Array(this.fontData)),
|
|
|
|
fontIndex: 0,
|
|
|
|
glyphIDs: glyphIDs,
|
|
|
|
pointSize: FONT_SIZE,
|
|
|
|
};
|
|
|
|
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.addEventListener('load', () => {
|
|
|
|
this.meshes = new PathfinderMeshes(xhr.responseText);
|
|
|
|
this.meshesReceived();
|
|
|
|
}, false);
|
|
|
|
xhr.open('POST', PARTITION_FONT_ENDPOINT_URL, true);
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
xhr.send(JSON.stringify(request));
|
2017-08-08 14:23:30 -04:00
|
|
|
}
|
|
|
|
|
2017-08-10 21:38:54 -04:00
|
|
|
meshesReceived() {
|
2017-08-08 14:23:30 -04:00
|
|
|
// TODO(pcwalton)
|
|
|
|
}
|
|
|
|
|
|
|
|
view: PathfinderView;
|
|
|
|
loadFontButton: HTMLInputElement;
|
2017-08-10 21:38:54 -04:00
|
|
|
fontData: ArrayBuffer;
|
|
|
|
font: any;
|
|
|
|
meshes: PathfinderMeshes;
|
2017-08-08 14:23:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class PathfinderView {
|
|
|
|
constructor(canvas: HTMLCanvasElement) {
|
|
|
|
this.canvas = canvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas: HTMLCanvasElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
const controller = new AppController;
|
|
|
|
window.addEventListener('load', () => controller.start(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|