I have created svg elements using the p5.js-svg librarie. Now i want to convert the svgs into paths so i can create glyphs with it. and the push the glyphs into a new fontfile. In the README file i found the Path.fromSVG() method whitch i think would do exactly what i try to do. Im using the following script tag to load opentype.js:
Current Behavior
However when im running my code i get the error that “Path.fromSVG is not a function”. I have logged the Path object to the console and look at the provided prototype functions. and fromSVG is not in this list.
Possible Solution
In the opentype.js repository under /src i have found the path.js file in whitch i have found a fromSVG() function. So probably it dont work because i have linked to cdn instead of downloading opentype.js and so the path.js file isnt available in the browser.
Another possible reason could be that the fromSVG() function is still work in progress and not yet published in the opetnype.min.js 1.3.4,
Im Happy for every Help/Suggestions;)
Expected Behavior
I was expecting that the function gives me a Path whitch i can use to create a new Glyph. And then push the Glyphs in to a new Font File.
The current version of Opentype.js doesn't include this method.
You could write a similar helper function to convert svg pathdata to opentype.js native command object.
You need to draw/align all your svg glyphs in a consistent layout similar to an EM Square (actually rather a rectangle) used in font design applications to define metrics like left and right sidebearings, descender etc.
The above illustration shows different letters all drawn in a viewBox with a height of 100 units and variable widths specifying the desired sidebearings of the glyph.
All glyphs are vertically aligned to the baseline (green line) at y=80.
Now we can use this design pattern to convert from svg to opentype commands. Fonts use a Cartesian coordinate system:
Red dot: x/y = 0 in svg
Green dot: x/y = 0 in font (baseline position)
Flipping pathdata
So we need to flip svg y-coordinates and shift the glyph according to the glyphs bbox height.
I'm using
getPathData()polyfill to retrieve the path commands.The following example converts all commands to absolute commands also converting quadratic and arc commands to cubic via normalize parameter:
If you need to keep e.g quadratic commands you could also parse the path without normalizing
el.getPathData().In this case your path data must not contain relative or shorthand commands. However you could also convert these as described here: "Convert SVG Path to Relative Commands"
See also example for writing fonts in opentype.js repository and documentation
Obviously you need to adjust the scaling and descender values according to your layout.