Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Paths Basics
Paths Basics

This is a short introduction to paths (also known as "vectors") as Gimp uses them.

Bézier Curves

In the sixties, Pierre Bézier was looking for a way to generate smooth curves in Computer-Aided Graphic and eventually settled on curves that have since been named after him. There are many types of Bézier curves but Gimp, as most graphics programs, uses the "cubic" ones. These are simple curves defined by:
  • Two points called the "anchors" (A1 and A2, below) that are the starting and ending point of the curve.
  • Two control points called the "handles" (H1 and H2, below), that indicate:
    • Using the line between the anchor and the handle: the general direction of the curve near the corresponding anchor. For the mathematically-oriented this line is the tangent of the curve on the anchor.
    • Using the distance between the anchor and the handle: how much the curve follows the direction of the handle.

If we bring H1 closer to A1 and drag H2 further from A2:


The closer the handle is to the anchor, the less influence it has on the shape of the whole curve. The limit is when the handle coincides with the anchor: in that case there is no tangent defined, and the general direction of the curve near the anchor is determined by the other handle. When there are no tangents defined at both ends, the curve becomes the straight line between the two anchors. In Gimp's path editor, anchors are usually created without handles, or, more accurately, with handles over the anchors, so a sequence of points creates a polygon, until you start dragging the handles out of the anchors.
We can even have loops and cusps:


Some nice properties of Bézier curves

The Bézier curves have a few properties worth remembering:
  • The whole curve is always inside the convex polygon defined by the two anchors and the two handles.
  • They are "compatible" with the simpler geometric transforms (a.k.a. "affine transforms") which are those that are found in Gimp's toolbox: shear, rotation, scale, perspective, flip. Which means that these two sequence of actions are equivalent:
    • "stroke" the curve to create a bitmap layer
    • use a transform tool on the layer
  • and
    • use a transform tool on the curve (in "Path" mode)
    • "stroke" the curve to create a bitmap layer
  • but of course the curve obtained with the second method will be sharper.


As elegant as they are, Bézier curves are not powerful enough to describe real-life curves such as the outline of characters. So more arbitrary curves are implemented by stringing together Bézier curves, the ending anchor of a curve also being the starting anchor of the next:


Each anchor becomes associated with two handles, one for the curve that ends on it (the "backward" handle) and one for the curve that starts from it (the "forward" handle). If these handles are aligned (A2) the curve remains smooth, but cusps can be obtained with handles on the same side (A3), and loops are obtained by interchanging the handles (or rotating them both by half a turn around the anchor)(A4).

In practice the stroke is defined and handled as a series of triplets of points, each triplet being made of the backward handle, the anchor, and the forward handle:


When doing so, the first and last anchor have one extra handle (backward for the first, forward for the last). These handles have a purpose: if you connect two strokes, the line between the two strokes is the Bézier curve controlled by these handles. Likewise, if you cut a stroke by Ctrl-Shift-clicking a segment to delete it, the new final anchors of the two resulting strokes keep the handles that controlled the removed segment.

Open and closed strokes

A similar principle is used to "close" strokes. When the stroke is marked "closed" (and this is just a supplemental closure indicator, Gimp doesn't add any point), an additional Bézier curve is generated from the last anchor to the first, under the control of these extra handles.


In Gimp, you mark the stroke as "closed" by connecting the first and last anchors.
Once the stroke is closed it is impossible to distinguish the actual first and last anchors, and in the picture above, the stroke could just as well start on A3 and end on A2. It is however possible to force Gimp to consider two anchors as the first and the last:
  • Given a stroke like this (the red part is the Bézier curve generated by the closure):
  • Open the stroke by deleting the segment between the two target anchors (2 and 3). These two anchors becomes the start and end of the open stroke. The handles are preserved:
  • Now, reconnect these anchors. The Bézier curve is regenerated, so the stroke shape is the same as initially, but the start and end anchors are the ones delimiting the temporarily deleted segment.

Stroke direction

Sometimes, the direction of the stroke is important. The stroke 1-to-4 above has the very same shape as the stroke obtained from the same triplets, ordered 4-to-1. But there are cases where the direction counts, for instance:
  • Using the "Text-along-path" function
  • Rendering the stroke by painting it with a gradient
Well, tough luck, there is nothing in out-of-the-box that will tell you the stroke direction, or let you change it. It just happens that Gimp doesn't usually change a stroke direction, so the first point of the stroke is usually the one you created first. But you have little control on the direction of strokes generated by Gimp (Layer>Text to path, or Select>To path).

But there are scripts available to help with this.


Strokes are powerful, but have a restriction: they are continuous lines. So, a single stroke is not powerful enough to describe complex curves such as the one needed to describe text. Enter the paths, that are nothing more than a bunch of strokes taken together. For instance, the word "hobbit" is made of 10 strokes:
  • One singe stroke for the 'h'.
  • Two strokes for the "o", the outer outline and the inner hole.
  • Two strokes for each "b", the outer outline and the inside for the loops.
  • Two strokes for the 'i', one for the body and one for the dot (a.k.a. "tittle").
  • One singe stroke for the 'h'.
In uppercase, 'HOBBIT' is 11 strokes, since the 'B's have one more loop but the 'I' loses the dot.

Path handling in Gimp
  • Gimp processes the path as a whole, there is no way to restrict its action to specific strokes.
  • The path is associated to the canvas, not to a specific layer.
  • A path can have points that lie outside of the canvas
  • Coordinates of anchors and handles are not restricted to integer pixels. Sub-pixel paths are possible
  • Selection from paths where strokes are not disjoint follows a 'toggle on line' rule. Starting unselected from a border, the selection state changes each time it crosses a line. This is neither and intersection nor a union, but a rule that creates the expected selection on text.

Forum Jump: