shrhdk / text-to-svg

Convert text to SVG path without native dependence.
Other
964 stars 131 forks source link

Support for data-url fonts? #40

Open kaldrenon opened 5 years ago

kaldrenon commented 5 years ago

I don't see anything in the documentation about loading a font via data-url, so my guess is that this isn't supported, but I would like to confirm that.

If that is true, is there a suggestion for how I could convert text to a path when I only have the data-url of a font?

If data-urls can be used, could the Readme include an example for how to use it?

GuiRitter commented 5 years ago

They can be used, at least for a browser environment. I use it like this (based on README snippet):

// First argument is URL on web browsers, but it is file path on Node.js.
TextToSVG.load('data:application/font-ttf;base64,SGVsbG8sIFdvcmxkIQ==', function(err, textToSVG) {
    const svg = textToSVG.getSVG('hello');
    console.log(svg);
});

According to what I read around, only TTF works. Also, if you're on Node, you can use this, but you need to do some hacking:

  1. Locate your project's node_modules folder;
  2. Locate the opentype.js folder;
  3. Open opentype.js/src/opentype.js;
  4. Locate this part of the code: function load(url, callback);
  5. Change const loadFn to always be loadFromUrl;
  6. Save;
  7. Have a terminal in the opentype.js folder and run npm i then npm run build;
  8. If you had used npm start, stop it and run it again.