Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Sloppy prototype to load font in Node instead of browser #1

Closed
wants to merge 3 commits into from
Closed
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
61 changes: 48 additions & 13 deletions Font.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// basic inmports
import { manPage } from "./src/manpage.js";
// import { manPage } from "./src/manpage.js";
import { Event, EventManager } from "./src/eventing.js";
import { SFNT, WOFF, WOFF2 } from "./src/opentype/index.js";
import { loadTableClasses } from "./src/opentype/tables/createTable.js";

import fs from "fs";

// A simple hack to distinguish between browser and Node
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';

/**
* either return the appropriate CSS format
* for a specific font URL, or generate an
Expand Down Expand Up @@ -70,8 +75,17 @@ class Font extends EventManager {
*/
set src(url) {
this.__src = url;
this.defineFontFace(this.name, url, this.options);
this.loadFont(url);
if(isBrowser) {
this.defineFontFace(this.name, url, this.options);
this.loadFont(url);
}
else {
// Test, but probably not what we want. We can just
// directly call loadFont and not rely on events
// when in Node env?
const stream = fs.createReadStream(url)
console.log(stream);
}
}

/**
Expand Down Expand Up @@ -100,14 +114,31 @@ class Font extends EventManager {
*/
async loadFont(url) {
const type = getFontCSSFormat(url);
fetch(url)
.then(response => checkFetchResponseStatus(response) && response.arrayBuffer())
.then(buffer => this.fromDataBuffer(buffer, type))
.catch(err => {
const evt = new Event(`error`, err, `Failed to load font at ${url}`);
this.dispatch(evt);
if (this.onerror) this.onerror(evt);
});
if(isBrowser) {
fetch(url)
.then(response => checkFetchResponseStatus(response) && response.arrayBuffer())
.then(buffer => this.fromDataBuffer(buffer, type))
.catch(err => {
console.log(err);
const evt = new Event(`error`, err, `Failed to load font at ${url}`);
this.dispatch(evt);
if (this.onerror) this.onerror(evt);
});
} else {
// TODO: recreate the fetch stuff ↑ with Node's fs

// Load the font from filesytem -- so far, so good
let response = fs.readFileSync(url);

// Now, recreate the response/buffer stuff so Font.js can continue
// Once the font has been turned into a buffer/object/thingy that
// Font.js expects, I think it will work the same as in the browser
// from there on.

// response = response.arrayBuffer()
// this.fromDataBuffer(response, type);
// console.log(font.toString());
}
}

/**
Expand Down Expand Up @@ -210,6 +241,10 @@ class Font extends EventManager {
}
}

Font.manPage = manPage;
// Font.manPage = manPage;

window.Font = Font;
if(isBrowser) {
window.Font = Font;
} else {
global.Font = Font;
}
Binary file added Fraunces-VF.ttf
Binary file not shown.
Loading