diff --git a/CHANGELOG.md b/CHANGELOG.md index e98f622f..eeb06b70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Unreleased +- Rearrange glyphs in AAT morx tables only when the marked range holds every glyph the verb moves, like CoreText and HarfBuzz (#142, #143) - Fix AAT glyph insertion: insert the marked glyph count, set the mark after inserting, skip the inserted glyphs, and limit how many glyphs a font can insert, like HarfBuzz (#171, #172, #173, #174) - Stop AAT morx state machines that never advance after a bounded number of steps, like HarfBuzz (#175) - Shape Tai Tham sakot followed by a consonant after vowel signs like HarfBuzz, instead of inserting a dotted circle diff --git a/src/aat/AATMorxProcessor.js b/src/aat/AATMorxProcessor.js index 992101bf..dacdc51d 100644 --- a/src/aat/AATMorxProcessor.js +++ b/src/aat/AATMorxProcessor.js @@ -7,6 +7,9 @@ const MARK_FIRST = 0x8000; const MARK_LAST = 0x2000; const VERB = 0x000f; +// number of glyphs each rearrangement verb moves, e.g. 3 for AxCD => CDxA +const VERB_LENGTH = [0, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]; + // contextual substitution and glyph insertion flag const SET_MARK = 0x8000; @@ -148,15 +151,19 @@ export default class AATMorxProcessor { } if (entry.flags & MARK_LAST) { - this.lastGlyph = index; + this.lastGlyph = Math.min(index, this.glyphs.length - 1); } - reorderGlyphs( - this.glyphs, - entry.flags & VERB, - this.firstGlyph, - this.lastGlyph, - ); + // Like CoreText and HarfBuzz, leave the glyphs alone unless the marked + // range holds every glyph the verb moves (#142, #143). + let verb = entry.flags & VERB; + let firstGlyph = this.firstGlyph === null ? 0 : this.firstGlyph; + if ( + this.lastGlyph !== null && + this.lastGlyph - firstGlyph + 1 >= VERB_LENGTH[verb] + ) { + reorderGlyphs(this.glyphs, verb, firstGlyph, this.lastGlyph); + } } processContextualSubstitution(glyph, entry, index) { diff --git a/test/data/fonttest/TestMORXFour.ttf b/test/data/fonttest/TestMORXFour.ttf new file mode 100644 index 00000000..07512ab5 Binary files /dev/null and b/test/data/fonttest/TestMORXFour.ttf differ diff --git a/test/data/fonttest/TestMORXThree.ttf b/test/data/fonttest/TestMORXThree.ttf new file mode 100644 index 00000000..33cc72bd Binary files /dev/null and b/test/data/fonttest/TestMORXThree.ttf differ diff --git a/test/issues.js b/test/issues.js index f88dedef..739312be 100644 --- a/test/issues.js +++ b/test/issues.js @@ -355,4 +355,44 @@ describe('issues', function () { assert(glyphs.length <= 65536); }); }); + + describe('#142 - morx rearrangement without a marked range', function () { + it('should leave the glyphs unchanged', function () { + // MORX-3 from Unicode text-rendering-tests runs every rearrangement + // verb before any glyph has been marked. + let font = fontkit.openSync( + new URL('data/fonttest/TestMORXThree.ttf', import.meta.url), + ); + + for (let digit of ['3', '4', '8', '⓯']) { + assert.deepEqual( + font + .layout(`ABXCD${digit}`) + .glyphs.slice(0, 5) + .map((glyph) => glyph.name), + ['A', 'B', 'X', 'C', 'D'], + ); + } + }); + }); + + describe('#143 - morx rearrangement underflow', function () { + it('should not move glyphs outside the marked range', function () { + // MORX-5 from Unicode text-rendering-tests marks a range shorter than + // the glyphs each rearrangement verb moves. + let font = fontkit.openSync( + new URL('data/fonttest/TestMORXFour.ttf', import.meta.url), + ); + + for (let text of ['PQRAXYZ3', 'PQRAXYZ⓬', 'PQRABXYZ⓬', 'PQRABCXYZ⓯']) { + assert.deepEqual( + font + .layout(text) + .glyphs.slice(0, -1) + .map((glyph) => glyph.name), + [...text.slice(0, -1)], + ); + } + }); + }); });