Skip to main content

FontCreator Tutorials

OpenType Fractions Using Reverse Chaining Substitution

written by Erwin Denissen, published November 21, 2024

Implementing Fractions in OpenType Fonts

Fractions are essential in typography for displaying mathematical expressions, measurements, and more. The frac feature in OpenType fonts replaces figures (digits) separated by a slash (U+002F) or a fraction slash (U+2044) with “common” (diagonal) fractions.

Common Method for Implementing Fractions

One of the more common ways to implement the frac feature is as follows:

@figures = [zero one two three four five six seven eight nine];
@figuresNumerator = [zero.numr one.numr two.numr three.numr four.numr five.numr six.numr seven.numr eight.numr nine.numr];
@figuresDenominator = [zero.dnom one.dnom two.dnom three.dnom four.dnom five.dnom six.dnom seven.dnom eight.dnom nine.dnom];
@slash = [slash fraction];

feature frac {
sub @figures by @figuresNumerator;
sub [@slash @figuresDenominator] @figuresNumerator' by @figuresDenominator;
sub slash by fraction;
} frac;

Issues with the Common Method:

While this method effectively formats fractions, it can inadvertently affect non-fractional digits and slashes within text blocks. For example:

1/2 123/456 4 5/8 m/s

    Potential Problems:

    • The slash in "m/s" would be substituted with the fraction slash.
    • Digits not intended to be part of a fraction (like the "4") would be converted to numerator forms.
    • This leads to incorrect rendering and affects readability.

    New Approach Using Reverse Chaining Single Substitution (rsub)

    To address these issues, we can use a more precise method that only affects actual fractions, leaving other parts of the text untouched. One effective approach is using GSUB LookupType 8 (Reverse Chaining Contextual Single Substitution), a technique we developed in June 2021.

    Here is the proposed solution:

    @figures = [zero one two three four five six seven eight nine];
    @figuresNumerator = [zero.numr one.numr two.numr three.numr four.numr five.numr six.numr seven.numr eight.numr nine.numr];
    @figuresNumeratorEx = [fraction @figuresNumerator];
    @figuresDenominator = [zero.dnom one.dnom two.dnom three.dnom four.dnom five.dnom six.dnom seven.dnom eight.dnom nine.dnom];
    @figuresDenominatorEx = [fraction @figuresDenominator];

    feature frac {
    sub @figures slash' @figures by fraction;
    rsub @figures' @figuresNumeratorEx by @figuresNumerator;
    sub @figuresDenominatorEx @figures' by @figuresDenominator;
    sub @figures space' @figuresNumerator by space.frac;
    } frac;

    Advantages of the New Method:

    • Contextual Precision:
      • Only affects digits and slashes that are part of actual fractions.
      • Leaves other digits and slashes in the text untouched.
    • Preservation of Non-Fraction Content:
      • Units like "m/s" remain correctly formatted.
      • Mathematical expressions or measurements not intended as fractions are displayed as intended.

    Summary of Benefits

    • Avoids Unintended Substitutions:
      • The common method alters all digits and slashes, which can be problematic in running text.
      • The new method selectively applies substitutions, enhancing typographic accuracy.
    • Enhances Readability:
      • Readers can correctly interpret measurements and units without confusion.
      • Fractions are formatted appropriately without affecting surrounding text.
    • Improves User Experience:
      • Typists and designers can input text without worrying about incorrect glyph substitutions.
      • The font behaves predictably, which is essential for professional typography.

    Final Thoughts

    Implementing the frac feature using rsub provides a significant advantage in handling real-world text scenarios. By focusing substitutions only where they are needed, we maintain the integrity of the text and enhance the overall typographic quality of the font.