Skip to content

Kerning pairs might not get sorted correctly in fonts with >256 glyphs #14

@YellowAfterlife

Description

@YellowAfterlife

Describe the bug
Currently kerning pairs are sorted based on idx1 << 8 | idx2, which is usually fine, but can produce unexpected sorting weights in fonts with more than 256 glyphs (where higher bit(s) of idx2 can overflow into idx1's bit space).

Target
Any

To Reproduce
You can reproduce this in an archived version of my font tool using the library.

Load this font tileset:
Image

And this JSON: 6w4-debug.json

Unless the second kerning definition is removed (by adding a space in front of that line), the kerning table will be ordered wrong and thus discarded by the browser.

This happens because:

  • pair 1 has idx1: 4, idx2: 339, id: 1363
  • pair 2 has idx1: 5, idx2: 67, id: 1347

So pair 1 is placed after pair 2 even though its first index is lower.

Expected behavior
Compose the index as idx1 << 16 || idx2. In my copy, I changed the code to this, which seems to be okay too:

		pairs.sort(function (kp1, kp2) {
			var diff = kp1.idx1 - kp2.idx1;
			if (diff != 0) return diff > 0 ? 1 : -1;
			diff = kp1.idx2 - kp2.idx2;
			if (diff != 0) return diff > 0 ? 1 : -1;
			return 0;
		});

Platform (please complete the following information about the system where the bug appears):

  • OS: Windows 10
  • Version: 22H2
  • Happens in both Firefox and Chromium!

Additional context
s/o to Jon (isyourguy) for originally describing the problem to me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions