opentypejs / opentype.js

Read and write OpenType fonts using JavaScript.
https://opentype.js.org/
MIT License
4.5k stars 479 forks source link

How to calculate perimeter of text? #396

Closed sunildarji811 closed 11 months ago

sunildarji811 commented 5 years ago

Is there any way to calculate perimeter of all the characters in given text using any pre defined function?

Like word 'neon' and I want to calculate how much length it will take to create a 'sign board' of that 'neon' text?

I want to calculate cost of board using given font type and font size by opentype.js

waruyama commented 3 years ago

I do not think there is a built-in functinality available, but it should not be too difficult to implement this.

Glyph contours consist of lines and quadratic and/or cubic bézier curves. You can march around a contour and sum up the length of these lines and curves. Note that calculating the length of a curve is not completely trivial if you want a precise solution. Here is some detailed background if you are interested, but all Javascript 2d packages have that functionality included if you need it.

Also, you will have to decide which of the contours is your perimeter (if the glyph has multiple contours).

So maybe just grab a 2d package like paper.js, feed the glyph paths, and let it calculate the path lengths (http://paperjs.org/reference/path/#length)

Connum commented 11 months ago

Duplicate of #272 Edit: OK, I misunderstood, but still this is out of scope for a library like ours.