question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parsing on node.js, rendering in browser

See original GitHub issue

Expected Behavior

Feature request / help needed: we’d like to parse fonts on the server side (Node.JS) and only render the parsed font in the client (Browser).

Current Behavior

The server part is a REST api endpoint which returns a JSON object containing all font information - basically the result of the opentype.js font instance. This enables us to do validation (font support) and caching the results of each parsed font. Output looks like this:

{
    "supported": true,
    "glyphs": {
        "glyphs": {
            "0": {
                "index": 0,
                "name": ".notdef",
                "unicodes": [],
                "advanceWidth": 748,
                "leftSideBearing": 67
            },
            ...
        },
        ...
     },
     ...
}

Goal: using the returned object in the client so we can skip parsing an ArrayBuffer on the client - to improve performance and reducing file size on the client.

Possible Solution

Idea: to create an opentype.js instance in the client with an object as source. Other ideas to get this working would be greatly appreciated.

Your Environment

  • Version used: 0.10.0
  • Font used: all
  • Browser Name and version: Chrome
  • Operating System and version (desktop or mobile): All

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
adriaanmeuriscommented, Oct 16, 2018

Since we’re doing custom rendering on the client, we’ve used opentype to parse the font on the server side, returning a plain object that’s consumed on the client (were we don’t use opentype).

import { parse } from 'opentype.js';
import forEach from 'lodash/forEach';

class OpentypeParser {
  constructor(buffer) {
    // Parse the font using opentype
    this.font = parse(buffer);
    
    // Loop over glyps
    this.glyphs = [];
    forEach(this.font.glyphs.glyphs, (g) => {
      this.glyphs.push({
        index: g.index,
        advanceWidth: g.advanceWidth,
        ...
      });
    })
  }

  toJSON() {
    return {
      glyphs: this.glyphs,
    };
  }
}

This class allows us to parse a font using opentype as a plain object (pseudo code)

const buffer = fs.readFileSync('arial.ttf');
const obj = new OpentypeParser(buffer).toJSON();
console.log(obj); // Plain object to send as response to the client

This object can be used on the client as you wish, assuming you don’t want to import the entire library there.

0reactions
Jolg42commented, Oct 16, 2018

Great! Thanks 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Web Scraping and Parsing HTML in Node.js with jsdom - Twilio
With Node.js tools like jsdom, you can scrape and parse this data directly ... first take a look at the HTML that's rendered...
Read more >
The Ultimate Guide to Web Scraping with Node.js
So what's web scraping anyway? It involves automating away the laborious task of collecting information from websites.
Read more >
Parsing HTML in Node.js with Cheerio - LogRocket Blog
Manipulating and rendering markup with Cheerio is incredibly fast because it works with a concise and simple markup (similar to jQuery). And ...
Read more >
Web Scraping With NodeJS and Javascript - ScrapFly
In this tutorial, we'll learn web scraping with NodeJS and Javascript. We'll cover an in-depth look at HTTP connections, HTML parsing, ...
Read more >
HTML, CSS Parsing and Web Page Rendering Process in ...
After fetching HTML from the server, the browser starts parsing it and creating DOM, This process itself is divided into many further steps...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found