<p>The way I would approach this is to get the outline curves of the font and then break each one down into some fixed number of steps (like, say, 100), and make a series of very small straight lines.</p>

<p>So if you've got some bezier curve, you loop <code>t</code> from 0 to 1 in 100 steps and plug it into the equation:</p>

<pre><code>double x = calculateBezier(0.0, c0.x, c1.x, c2.x, c3.x);
double y = calculateBezier(0.0, c0.y, c1.y, c2.y, c3.y);
double dist = 0.0;
for (double t = 0.01; t &lt;= 1.0; t+= 0.01)
{
    double nextX = calculateBezier(t, c0.x, c1.x, c2.x, c3.x);
    double nextY = calculateBezier(t, c0.y, c1.y, c2.y, c3.y);
    double nextDist = sqrt((nextX - x) * (nextX - x) + (nextY - y) * (nextY - y));
    dist += nextDist;
}
</code></pre>

<p>Where <code>calculateBezier()</code> is a function that calculates a point on a bezier curve given the parameter <code>t</code> and control points <code>c0</code> through <code>c3</code>:</p>

<pre><code>double calculateBezier(double t, double c0, double c1, double c2, double c3)
{
    double t1 = 1.0 - t;

    return (t1 * t1 * t1) * c0 + 3.0 * (t1 * t1) * c1 + 
            3.0 * t1 * (t * t) * c2 + (t * t * t) * c3;
}
</code></pre>

<p>And just add the distances of each part of the curve until you're out of curves.</p>

<p>Note that this assumes cubic bezier curves. For quadratic, the <code>calculateBezier</code> function would calculate a quadratic one (like described <a href="https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Quadratic_B.C3.A9zier_curves" rel="nofollow noreferrer">here</a>).</p>

CC BY-SA 3.0
2017-11-10T09:16:48.450Z
+1 - just not need break the font into the parts, because the font itself is stored in the `ttf` as a pairs of x-y coordinates. (the connections between the points could be straight or some are quadratic beziers). Also see my answer above - it is _too complicated_ for my use-case. **Thanks** anyway for the answer it kicked me to the right direction - e.g. start studying the ttf font structure. :)
clt60
CC BY-SA 3.0
2017-11-10T04:37:10.783Z
2017-11-10T04:37:10.783Z
user1118321
<p>I decided to solve the LED-rope problem using Inkscape as:</p>

<ul>
<li>enter the wanted text into the Inkscape</li>
<li>set the color of the text to yellow</li>
<li>add new layer above the text and lock the text layer</li>
<li>in the new layer, manually draw over the letters my own wanted bezier path(s)</li>
<li>and finally use the tool in <code>Extensions -&gt; Visualise Path -&gt; Measure Path...</code> - this adds the length of the given bezier path into the drawing as text.</li>
</ul>

<p>The reason why:</p>

<p>I studied a bit the ttf-font structure (the whole new world for me). <a href="http://stevehanov.ca/blog/index.php?id=143" rel="nofollow noreferrer">This blog entry</a> and this <a href="https://developer.apple.com/fonts/TrueType-Reference-Manual/" rel="nofollow noreferrer">Reference manual</a> helped a lot understanding the following (rough) basics.</p>

<ul>
<li>the given glyph (character) is composed from many <em>closed paths</em>, e.g. the letter <code>O</code> is composed from two paths (outer circle and the inner circle).</li>
<li>the paths are stored in the font file as pairs of x-y coordinates</li>
<li>so it isn't so hard to calculate the length of <em>each</em> path from the given coordinates.</li>
<li><em>but</em> - in such way I will get the lengths of the all <em>outlines</em>. Creating the outlines using the LED-rope it could be even interesting and also it is used at many examples, (like bellow), but this isn't what I want - I want a simple path(s) - as in the image in the question.</li>
<li>getting such <em>simple path</em> from the font's many outlines would need some tracing and approximation and other complicated math, which is definitely outside of my area of interest.</li>
<li>therefore (for me) would be much easier just manually create the simple path's and measure their length in the Inkscape.</li>
</ul>

<p>Also,</p>

<ul>
<li>if someone want the length of the font's outlines, it is easily doable</li>
<li>just need break apart the text into characters</li>
<li>transform the (font) objects into the strokes</li>
<li>and use the <code>Measure path...</code> tool</li>
</ul>

<p><a href="https://i.sstatic.net/gk26Z.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/gk26Z.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2017-11-10T09:11:31.297Z
2017-11-10T09:11:31.297Z
clt60
<p>I wan't create some LED-rope text, using some script font, like the following:</p>

<p><a href="https://i.sstatic.net/SRt9c.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/SRt9c.png" alt="enter image description here"></a></p>

<p>For this I need somewhat estimate the needed length of the LED-rope for the given letter (or combinations of letters).</p>

<p>Some more info:</p>

<ul>
<li>The font will be TTF or FT2.</li>
<li>I could manage some programs in perl using the FreeType library, eg. read some properties of the character in the font-file - if this helps.</li>
<li>Also could install Inkscape (Illustrator  open-source alternative) or even could use Illustrator at my friend - if any of this tools could help.</li>
</ul>

<p><strong>Looking for some suggestions how to measure the length of paths in the given font for the particular character (or whole text).</strong></p>

<p>I don't need exact precision, (see the purpose above) and looking for the solution <em>as simple as possible</em>, but I haven't any idea how even approach this problem. :(</p>

<p>Of course, I always could print the text and buy some inexpensive <a href="https://en.wikipedia.org/wiki/Opisometer" rel="nofollow noreferrer">curve runner</a>, but would be better to have some computer-based solution. :)</p>

<p>Ps: I'm not fully sure that this is the right place to ask such question, if not - I will delete it, but please suggest some better place.</p>

<transformations><computational-geometry><pathtracing><vector-graphics><font-rendering>
How to get the length of paths in an given font/character
clt60
# How to get the length of paths in an given font/character

(Asked by: username_0 on 2017-11-09)

I wan't create some LED-rope text, using some script font, like the following:

[![enter image description here](https://i.sstatic.net/SRt9c.png)](https://i.sstatic.net/SRt9c.png)

For this I need somewhat estimate the needed length of the LED-rope for the given letter (or combinations of letters).

Some more info:

* The font will be TTF or FT2.
* I could manage some programs in perl using the FreeType library, eg. read some properties of the character in the font-file - if this helps.
* Also could install Inkscape (Illustrator open-source alternative) or even could use Illustrator at my friend - if any of this tools could help.

**Looking for some suggestions how to measure the length of paths in the given font for the particular character (or whole text).**

I don't need exact precision, (see the purpose above) and looking for the solution *as simple as possible*, but I haven't any idea how even approach this problem. :(

Of course, I always could print the text and buy some inexpensive [curve runner](https://en.wikipedia.org/wiki/Opisometer), but would be better to have some computer-based solution. :)

Ps: I'm not fully sure that this is the right place to ask such question, if not - I will delete it, but please suggest some better place.

**Tags:** transformations, computational-geometry, pathtracing, vector-graphics, font-rendering


---

## 2 answers

### Answer by username_1 on 2017-11-10. Score: 2

The way I would approach this is to get the outline curves of the font and then break each one down into some fixed number of steps (like, say, 100), and make a series of very small straight lines.

So if you've got some bezier curve, you loop `t` from 0 to 1 in 100 steps and plug it into the equation:

```
double x = calculateBezier(0.0, c0.x, c1.x, c2.x, c3.x);
double y = calculateBezier(0.0, c0.y, c1.y, c2.y, c3.y);
double dist = 0.0;
for (double t = 0.01; t <= 1.0; t+= 0.01)
{
    double nextX = calculateBezier(t, c0.x, c1.x, c2.x, c3.x);
    double nextY = calculateBezier(t, c0.y, c1.y, c2.y, c3.y);
    double nextDist = sqrt((nextX - x) * (nextX - x) + (nextY - y) * (nextY - y));
    dist += nextDist;
}

```

Where `calculateBezier()` is a function that calculates a point on a bezier curve given the parameter `t` and control points `c0` through `c3`:

```
double calculateBezier(double t, double c0, double c1, double c2, double c3)
{
    double t1 = 1.0 - t;

    return (t1 * t1 * t1) * c0 + 3.0 * (t1 * t1) * c1 + 
            3.0 * t1 * (t * t) * c2 + (t * t * t) * c3;
}

```

And just add the distances of each part of the curve until you're out of curves.

Note that this assumes cubic bezier curves. For quadratic, the `calculateBezier` function would calculate a quadratic one (like described [here](https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Quadratic_B.C3.A9zier_curves)).

> **Comments:**
> **username_0**: +1 - just not need break the font into the parts, because the font itself is stored in the `ttf` as a pairs of x-y coordinates. (the connections between the points could be straight or some are quadratic beziers). Also see my answer above - it is _too complicated_ for my use-case. **Thanks** anyway for the answer it kicked me to the right direction - e.g. start studying the ttf font structure. :)  

---

### Answer by username_0 on 2017-11-10. Score: 1

I decided to solve the LED-rope problem using Inkscape as:

* enter the wanted text into the Inkscape
* set the color of the text to yellow
* add new layer above the text and lock the text layer
* in the new layer, manually draw over the letters my own wanted bezier path(s)
* and finally use the tool in `Extensions -> Visualise Path -> Measure Path...` - this adds the length of the given bezier path into the drawing as text.

The reason why:

I studied a bit the ttf-font structure (the whole new world for me). [This blog entry](http://stevehanov.ca/blog/index.php?id=143) and this [Reference manual](https://developer.apple.com/fonts/TrueType-Reference-Manual/) helped a lot understanding the following (rough) basics.

* the given glyph (character) is composed from many *closed paths*, e.g. the letter `O` is composed from two paths (outer circle and the inner circle).
* the paths are stored in the font file as pairs of x-y coordinates
* so it isn't so hard to calculate the length of *each* path from the given coordinates.
* *but* - in such way I will get the lengths of the all *outlines*. Creating the outlines using the LED-rope it could be even interesting and also it is used at many examples, (like bellow), but this isn't what I want - I want a simple path(s) - as in the image in the question.
* getting such *simple path* from the font's many outlines would need some tracing and approximation and other complicated math, which is definitely outside of my area of interest.
* therefore (for me) would be much easier just manually create the simple path's and measure their length in the Inkscape.

Also,

* if someone want the length of the font's outlines, it is easily doable
* just need break apart the text into characters
* transform the (font) objects into the strokes
* and use the `Measure path...` tool

[![enter image description here](https://i.sstatic.net/gk26Z.png)](https://i.sstatic.net/gk26Z.png)


---

