foliojs / fontkit

An advanced font engine for Node and the browser
1.45k stars 213 forks source link

Convert path.toSVG() output back to original text? #245

Closed rickybrown closed 3 years ago

rickybrown commented 3 years ago

Hopefully I'm not overlooking something that's already been addressed, but given this scenario, what could i do in the second function to get the original character codes/original string from svg info?:

const fontkit = require('fontkit');
const font = fontkit.openSync(__dirname + '/font.ttf');

// Works great
const StringToSVG = string => {
  let run = font.layout(string)

  return run.glyphs.map(g => g.path.toSVG())
}

let svg = StringToSVG(string)

const SVGtoString = svg => {
  // What can i do with these SVG Paths?
}

let originalString = SVGtoString(svg)

// To get this output?
console.log(originalString)
Pomax commented 3 years ago

You can't? Once you turn the text into outline vector graphics, you lose any relation to the original text. Unless you want to perform OCR of course. Instead, make your code remember which SVG belongs to which text and don't try to convert svg to text, but directly look up with text belongs to which svg path.