Reversing glyph sequence

I'd like to be able to do things like:
sub A A B B by B B A A;
sub A B C D E F by F E D C B A;
I don't see how this could be achieved reading the spec, and FontTools errors out with an "Direct substitution of multiple glyphs by multiple glyphs is not supported" if I try the above, but I was wondering if there's a trick to still accomplish this.

Comments

  • It is not possible in a single lookup, but you can accomplish it with two lookups.

    First a ligature substitution:
    sub A A B B by A_A_B_B;
    Then a multiple substitution:
    sub A_A_B_B by B B A A;


  • Khaled HosnyKhaled Hosny Posts: 274
    OpenType does not support multiple to multiple glyph substitution. You can use hacks like the above, but be aware that it will mess with the glyph to character. In the example above you might not be able to select the individual A and B glyphs anymore.
  • K PeaseK Pease Posts: 39
    edited October 3
    Preservation of the plaintext is central to OpenType. If you have just a small list of words to reverse, the best thing would be to make a ligature substitution and stop.
    If you want a generalized process, you'll need a big matrix of glyphs with classes for the rows (what the letter really is) and columns (what it looks like). For instance A.Z would be an alternate A whose outline looks like a Z. Then you would need a block of code for each string length you want to be able to do, substituting each character one by one.
    Here's what the code would look like for four-character strings only, and if the only characters involved were A, B, C. For better comprehension I'm showing most of the classes as bracketed groups and leaving in the members that would be unchanged, but for a larger set you would definitely use defined classes.
    sub [A B C]' @any @any A by [A B.A C.A];
    sub [A B C]' @any @any B by [A.B B C.B];
    sub [A B C]' @any @any C by [A.C B.C C];
    sub @any [A B C]' A @any by [A B.A C.A];
    sub @any [A B C]' B @any by [A.B B C.B];
    sub @any [A B C]' C @any by [A.C B.C C];
    sub @any [A A.B A.C] [A B C]' @any by [A B.A C.A];
    sub @any [B.A B B.C] [A B C]' @any by [A.B B C.B];
    sub @any [C.A C.B C] [A B C]' @any by [A.C B.C C];
    sub [A A.B A.C] @any @any [A B C]' by [A B.A C.A];
    sub [B.A B B.C] @any @any [A B C]' by [A.B B C.B];
    sub [C.A C.B C] @any @any [A B C]' by [A.C B.C C];
    Every character more in the pool will multiply the number of lines of code, and there's no getting around it, lacking some kind of two-dimensional ordered meta-class.
    If you really want to break the plaintext, you could substitute it afterward, but the alternate glyphs are necessary to keep track during the second half of the process, so you may as well not.


  • @Erwin Denissen @Khaled Hosny Thanks, I gave that a shot. Swapping via an intermediate glyph seems to do the trick, but indeed the glyphs are then not processed by any subsequent rules. Which in my case is a dealbreaker, but good to know this workaround exists.

    @K Pease Thanks for the thorough answer. I was afraid this would result in an explosion of rules (and my brain). Appreciate the example code!
  • OpenType features should continue to be processed.

    script latn {
      feature StandardLigatures1;
    }

    feature StandardLigatures1 liga {
      lookup Ligature1;
      lookup MultipleSubstitution1;
      lookup Ligature2;
    }

    lookup Ligature1 {
      sub a b c -> a_b_c;
    }

    lookup MultipleSubstitution1 {
      sub a_b_c -> c b a;
    }

    lookup Ligature2 {
      sub b a d -> checkmark;
    }



  • @Erwin Denissen You are correct, I tested your approach in a regular font and features continue to be applied on the replaced glyphs. So something else in my particular setup is ignoring the advance widths of the replaced glyphs ("c b a" in your example). This might be caused the by the color font format I'm using — will investigate.

    Thanks for posting the example!
Sign In or Register to comment.