Fraction OT Code

Ken Krugh's picture

Hi Guys,

I've looked over the great posts that are here for this and I think I've settled on the code below, which I grabbed from the Adobe Caslon Pro font. It's nice and compact and should suit out needs just right.

It all seems to be working fine but I'm not completely clear as to how the single quote works and I don't want to end up in a "A little knowedge is a dangerous thing" situation. An Adobe page states that the single quote "Marks a glyph or glyph class for contextual substitution or positioning" but I'm still lost.

Thanks for any help you can offer my feeble mind.

All Best,
Ken

@frac1regfigs=zero one two three four five six seven eight nine
@frac2numr=zero.numr one.numr two.numr three.numr four.numr five.numr six.numr seven.numr eight.numr nine.numr
@frac3dnom=zero.dnom one.dnom two.dnom three.dnom four.dnom five.dnom six.dnom seven.dnom eight.dnom nine.dnom
@frac4fracsdnom=perthousand slash percent onehalf onethird onequarter twothirds threequarters threeeighths seveneighths one.dnom two.dnom three.dnom four.dnom five.dnom six.dnom seven.dnom eight.dnom nine.dnom

feature frac {
lookup frac01 {
#all to numerators
sub @frac1regfigs by @frac2numr;
} frac01;
lookup frac02 {
#change numerators to denominators contextually
sub zero.numr' @SLASH' zero.numr' zero.numr' by perthousand;
sub zero.numr' @SLASH' zero.numr' by percent;
sub one.numr' slash' two.numr' by onehalf;
sub one.numr' slash' three.numr' by onethird;
sub one.numr' slash' four.numr' by onequarter;
sub two.numr' slash' three.numr' by twothirds;
sub three.numr' slash' four.numr' by threequarters;
sub three.numr' slash' eight.numr' by threeeighths;
sub seven.numr' slash' eight.numr' by seveneighths;
sub @frac4fracsdnom @frac2numr' by @frac3dnom;
} frac02;
lookup frac03 {
#change the slash
sub slash by fraction;
} frac03;
} frac;

behnam's picture

I don't think you should put single quote at all. Single quote identifies the glyph in the contextual chain, that needs to be substituted in that context. The other glyphs without single quote in that context remain intact. If you don't put anything, the substitution will apply to all glyphs in the context.
In this case, since all two or three or four or whatever number of glyphs in the contextual chain will be removed and replaced by a single glyph, you don't need to use the single quote. I'm actually surprised that it did work for you. But it should be more work for CPU I guess.
I'm interested to know more from experts on this too.

agisaak's picture

Well, he definitely needs the single quote in the line

sub @frac4fracsdnom @frac2numr' by @frac3dnom;

If he removes the other single quotes the code will likely not compile since then you will have a single lookup containing two different types of rules. He would need to split frac02 into two separate lookups.

André

Ken Krugh's picture

Ah-ha! I thought that was how it worked. The glyphs (or classes) that have the single quote will be substituted. Right?

And yes, Adnre is right, that line needs to leave the @frac4fracsdnom alone. The other lines all have it to remain within the same lookup. I assumed for efficency?

Any other info will be more than welcomed.

Thanks a bunch guys!

Ken

agisaak's picture

Yes, when you have something sub @A @B' @C by @D , the apostrophe is needed to distinguish the substitution target from the substitution context.

In the example you give, I can see no reason why it would have to be a single lookup. You could break it up into

lookup frac1A {
  sub zero.numr slash zero.numr zero.numr by perthousand;
  sub zero.numr slash zero.numr by percent;
  sub one.numr slash two.numr by onehalf;
  # etc.
} frac1A;

lookup frac1B {
  sub @frac4fracsdnom @frac2numr' by @frac3dnom;
} frac1B;

In this case, the apostrophes can be omitted in the first lookup. They are only needed when the final line is included since you can't include both ligature and contextual substitutions in the same lookup. The apostrphes force the ligature rules to be interpreted as contextual substitutions which happen to have the same effect as ligature substitutions.

André

Ken Krugh's picture

André, while I have your "ear."

Regarding the line sub @frac4fracsdnom @frac2numr' by @frac3dnom;: The first line of the feature changes everything to numerators, it then does the fractions that are built as glyphs. However, there is nothing in the line I mentioned above to indicate which side of the slash the numerators (class @frac2numr) are being "searched." I was assuming that because there is a dnom feature (as describded here: http://www.microsoft.com/typography/otspec/features_fj.htm) the frac feature is matching the glyphs in the @frac3dnom to the denom feature and knows to substitute only to the right of the slash. Am I correct?

Font work like this isn't my main function here, so commenting this code with what is happening will be immensly helpful when I come back into it later. Not sure where I'd be without this forum!

Thanks very much again for your time,
Ken

Nick Shinn's picture

I’ve been using this technique for several years (although without quite so many lookups):

http://talleming.com/2008/04/16/fraction-fever/

Nick Shinn's picture

Sorry, I posted the first one I Googled, without looking at it.

agisaak's picture

Ken,

The line sub @frac4fracsdnom @frac2numr' by @frac3dnom; specifies that your fraction numerators will be replaced by denominators *only* when they follow elements in the class @frac4fracsdnom. Since this class includes the slash along with 'slash-containing' glyphs, that's what determines that numerators will only be changed to a denominator following the slash. The dnom feature, whether implemented or not, doesn't play a role in this.

André

Syndicate content Syndicate content