|
| 1 | + |
| 2 | +# Complex Condition Finding in Closure Glyph Keyed Segmenter |
| 3 | + |
| 4 | +Author: Garret Rieger__ |
| 5 | +Date: Dec 17, 2025 |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +Before reading this document is recommended to first review the [closure glyph |
| 10 | +segmentation](./closure_glyph_segmentation.md) document. |
| 11 | + |
| 12 | +In closure glyph segmentation the closure analysis step is capable of locating glyph activation conditions that are |
| 13 | +either fully disjunctive or fully conjunctive (eg. A or B or C). It is not capable of finding conditions that are a mix |
| 14 | +of conjunction and disjunction (eg. (A and B) or (B and C)). These are referred to as complex conditions. By default |
| 15 | +glyphs with complex conditions are assigned to a patch that is always loaded, since the true conditions are not known. |
| 16 | + |
| 17 | +This document describes an algorithm which can be used to find the complete set of segments which are a part of the |
| 18 | +complex condition for a glyph. If the set of segments which are present in a condition is known, then we can form a |
| 19 | +purely disjunctive condition using those segments which is guaranteed to be a superset of the true condition. That is it |
| 20 | +will always activate at least when the true condition would. This property allows the superset condition to be used for |
| 21 | +a patch in place of the true condition without violating the closure requirement. |
| 22 | + |
| 23 | +For example if we had a glyph with a activation condition of ((A and B) or (B and C)) then this process will find the set |
| 24 | +of segments {A, B, C} which would form the superset condition (A or B or C). In a segmentation we could then have a |
| 25 | +patch with condition (A or B or C) which loads the glyph and this would satisfy the closure requirement. In the future |
| 26 | +if we decide to develop an analysis to find the true condition then the segment set found by this process could be used |
| 27 | +to narrow down the search space to only those segments involved in the condition. |
| 28 | + |
| 29 | +## Foundations |
| 30 | + |
| 31 | +The algorithm is based on the following assertions: |
| 32 | + |
| 33 | +1. For any complex activation condition of a glyph, a disjunction over all segments appearing in that condition |
| 34 | + will always activate at least when the original condition does. |
| 35 | + |
| 36 | +2. Given some fully disjunctive condition, we can verify that condition is sufficient to meet the glyph closure |
| 37 | + requirement for a glyph by the following procedure: compute a glyph closure of the union of all segments except for |
| 38 | + those in the condition. If the glyph does not appear in this closure, then the condition satisfies the closure |
| 39 | + requirement for that glyph. This is called the “additional conditions” check. |
| 40 | + |
| 41 | +3. The glyph closure of all segments will include the glyphs that we are analyzing. |
| 42 | + |
| 43 | +4. We have a glyph which has some true activation condition. If we compute a glyph closure of some combination of |
| 44 | + segments, then adding or removing a segment, which is not part of the activation condition, to the glyph closure input |
| 45 | + will have no affect on whether or not the glyph appears in the closure output. |
| 46 | + |
| 47 | +## The Algorithm |
| 48 | + |
| 49 | +For each glyph with a complex condition we can use the above to find the complete set of segments which are part of the |
| 50 | +glyph's complex condition. A condition which is a disjunction across these segments will satisfy the closure requirement |
| 51 | +for that glyph. |
| 52 | + |
| 53 | +### Finding a Sub Condition |
| 54 | + |
| 55 | +The algorithm works by identifying a single sub condition at a time, this section describes the algorithm for |
| 56 | +finding a single sub condition. |
| 57 | + |
| 58 | +Inputs: |
| 59 | + |
| 60 | +* Segments to exclude from the analysis. |
| 61 | +* `glyph` to analyze. |
| 62 | + |
| 63 | +Algorithm: |
| 64 | + |
| 65 | +1. Start with a set of all segments except those to be excluded, called `to_test`. |
| 66 | +2. Initialize a second set of segments, `required`, to the empty set. |
| 67 | +3. Remove a segment `s` from `to_test` and compute the glyph closure of `to_test U required`. |
| 68 | +4. If `glyph` is not found in the closure then add `s` to `required`. |
| 69 | +5. If `to_test` is empty, then return the sub condition `required`. |
| 70 | +6. Otherwise, go back to step 3. |
| 71 | + |
| 72 | +### Finding the Complete Condition |
| 73 | + |
| 74 | +This section describes the algorithm which finds the complete condition, it utilizes `Finding a Sub Condition`. |
| 75 | + |
| 76 | +Inputs: |
| 77 | + |
| 78 | +* `glyph` to analyze. |
| 79 | + |
| 80 | +Algorithm: |
| 81 | + |
| 82 | +1. Initialize a set of segments `condition` to the empty set. |
| 83 | +2. Execute the `Finding a Sub Condition` algorithm with `condition` as the excluded set. |
| 84 | +3. Union the returned set into `condition`. |
| 85 | +4. Compute the glyph closure of all segments except those in `condition`. |
| 86 | +5. If `glyph` is found in the closure, then more conditions still exist. Go back to step 2. |
| 87 | +6. Return the complete condition, `condition`. |
| 88 | + |
| 89 | +### Initial Font |
| 90 | + |
| 91 | +Any time a closure operation is executed by the above two algorithms it's necessary to union the subset definition |
| 92 | +for the initial font into the closure input. That's because the closure of the initial font affects what's reachable |
| 93 | +by the segments. |
| 94 | + |
| 95 | +### Why this works |
| 96 | + |
| 97 | +* Any segments which are not part of the true condition will not impact the glyph's presence in the closure (assertion |
| 98 | + (4)). As a result they will never be moved into the `required` set and will not be returned by `Finding a Sub |
| 99 | + Condition`. Thus any segments returned by `Finding a Sub Condition` are part of the true condition. |
| 100 | + |
| 101 | +* Each iteration of `Finding a Sub Condition` is guaranteed to select at least one segment since we know that the |
| 102 | + initial closure always starts with the glyph in it, and the closure of no segments will not have the glyph in it. So |
| 103 | + at some point during the algorithm the glyph must be found to not be present. In the first iteration this is a result |
| 104 | + of assertion (3). For subsequent iterations this is guaranteed by the "additional conditions" check prior to starting |
| 105 | + the iteration. |
| 106 | + |
| 107 | +* Since all returned segments from `Finding a Sub Condition` are excluded from future calls, there will be a finite |
| 108 | + number of `Finding a Sub Condition` executions which return only segments part of the true condition. |
| 109 | + |
| 110 | +* Lastly, the algorithm terminates only once the additional conditions check finds no additional conditions, |
| 111 | + guaranteeing we have found the complete superset disjunctive condition. |
| 112 | + |
| 113 | +## Making it More Performant |
| 114 | + |
| 115 | +As described above this approach can be slow since it processes glyphs one at a time. Improvements to performance |
| 116 | +can be made by processing glyphs in a batch. This can be done with a recursive approach where after each segment test |
| 117 | +the set of input glyphs gets split into those that require the tested segment and those that don't. Each of the splits spawns |
| 118 | +a new recursion (if there is at least one glyph in the split). |
| 119 | + |
| 120 | +Also from the closure analysis run by the segmenter we may have discovered some partial conditions for glyphs. These |
| 121 | +can be incorporated as a starting point into the complex condition analysis. |
| 122 | + |
| 123 | +Furthermore we can reduce the amount of segments we need to test by checking which segments can interact in some way |
| 124 | +with the GSUB table. Segments that don't interact with GSUB can't by part of a conjunctive condition, so these can |
| 125 | +always be found via the standard closure analysis procedures. Then the search can be limited to just the set of segments |
| 126 | +which interact with GSUB and were not identified during regular closure analysis. |
| 127 | + |
| 128 | + |
| 129 | +## Integrating into the Segmentation Algorithm |
| 130 | + |
| 131 | +Initially the complex condition analysis has been added as a final step after merging. If after merging unmapped glyphs |
| 132 | +are present, then the complex condition analysis is run on those glyphs and the fallback patch is replaced with one or |
| 133 | +more patches based on the results of complex condition analysis. |
| 134 | + |
| 135 | +However, ideally complex condition analysis would be run before merging so that the patches it generates can participate |
| 136 | +in the merging process. This will require incremental updates to the complex condition analysis results, but that should |
| 137 | +be straightforward. Implementing this is planned for the near future. |
| 138 | + |
| 139 | + |
| 140 | + |
0 commit comments