A clustering example for levels 0 and 1: HarfBuzz Manual

## A clustering example for levels 0 and 1

The basic shaping operations affect clusters in a predictable manner when using level 0 or level 1:

- When two or more clusters merge, the resulting merged cluster takes as its cluster value the minimum of the incoming cluster values.

- When a cluster decomposes, all of the resulting child clusters inherit as their cluster value the cluster value of the parent cluster.

- When a character is reordered, the reordered character and all clusters that the character moves past as part of the reordering are merged into one cluster.

The functionality, guarantees, and benefits of level 0 and level 1 behavior can be seen with some examples. First, let us examine what happens with cluster values when shaping involves cluster merging with ligatures and decomposition.

Let's say we start with the following character sequence (top row) and initial cluster values (bottom row):

          A,B,C,D,E
          0,1,2,3,4

During shaping, HarfBuzz maps these characters to glyphs from the font. For simplicity, let us assume that each character maps to the corresponding, identical-looking glyph:

          A,B,C,D,E
          0,1,2,3,4

Now if, for example, `B` and `C` form a ligature, then the clusters to which they belong "merge". This merged cluster takes for its cluster value the minimum of all the cluster values of the clusters that went in to the ligature. In this case, we get:

          A,BC,D,E
          0,1 ,3,4

because 1 is the minimum of the set {1,2}, which were the cluster values of `B` and `C`.

Next, let us say that the `BC` ligature glyph decomposes into three components, and `D` also decomposes into two components. Whenever a cluster decomposes, its components each inherit the cluster value of their parent:

          A,BC0,BC1,BC2,D0,D1,E
          0,1  ,1  ,1  ,3 ,3 ,4

Next, if `BC2` and `D0` form a ligature, then their clusters (cluster values 1 and 3) merge into `min(1,3) = 1`:

          A,BC0,BC1,BC2D0,D1,E
          0,1  ,1  ,1    ,1 ,4

Note that the entirety of cluster 3 merges into cluster 1, not just the `D0` glyph. This reflects the fact that the cluster must be treated as an indivisible unit.

At this point, cluster 1 means: the character sequence `BCD` is represented by glyphs `BC0,BC1,BC2D0,D1` and cannot be broken down any further.