diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eae34f79..37fc85a5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Unreleased +- Fix wrapped right-to-left text shaped as one run, e.g. Arabic with `features`, drawing the space at the end of each line on its left, which pushed every line but the last one space past the right margin with `align: 'right'`. Fixes #1422 - Fix `heightOfString` drawing the measured text into the page when it is called after a `text` call with `continued: true`, because it measured with the line wrapper that call had left behind, which draws its lines. A measurement that passes `continued: true` itself still uses that wrapper and draws as before. Fixes #1284 - Render SVG quadratic curves with equivalent cubic control points, so reversing a path preserves its shape. Fixes #550 - Fix the visual test renderer rejecting its standard-font directory on Windows because its path ends in a backslash. diff --git a/lib/mixins/text.js b/lib/mixins/text.js index 8b216ab79..24c051608 100644 --- a/lib/mixins/text.js +++ b/lib/mixins/text.js @@ -710,7 +710,10 @@ export default { positions[positions.length - 1] = space; } } else { - [encoded, positions] = this._font.encode(text, options.features); + // Don't draw the whitespace at the end of a wrapped line. The alignment + // above leaves it out, and right-to-left text puts it on the left. + const drawn = options.width ? text.replace(/\s+$/, '') : text; + [encoded, positions] = this._font.encode(drawn, options.features); } const scale = this._fontSize / 1000; diff --git a/tests/fonts/NotoKufiArabic-Subset.ttf b/tests/fonts/NotoKufiArabic-Subset.ttf new file mode 100644 index 000000000..4d719204f Binary files /dev/null and b/tests/fonts/NotoKufiArabic-Subset.ttf differ diff --git a/tests/unit/text.spec.js b/tests/unit/text.spec.js index 561ceafd0..12bc9bbb8 100644 --- a/tests/unit/text.spec.js +++ b/tests/unit/text.spec.js @@ -337,6 +337,29 @@ Q expect(lines).toEqual(['a ', 'design-system']); }); + test('right-to-left wrapped lines do not draw their trailing space - issue #1422', () => { + document.font('tests/fonts/NotoKufiArabic-Subset.ttf', 12); + const features = ['rtla']; + const [[space]] = document._font.encode(' ', features); + const glyphs = []; + const addContent = document.addContent; + document.addContent = function (data) { + if (data.endsWith('TJ')) { + glyphs.push(data.match(/<([0-9a-f]+)>/)[1].match(/.{4}/g)); + } + return addContent.call(this, data); + }; + + document.text('هذا نص عربي', { width: 60, align: 'right', features }); + + // The trailing space of the first line comes first once the line is + // reversed, and would push the line one space past the right margin. + expect(glyphs).toHaveLength(2); + expect(glyphs[0]).toHaveLength(6); + expect(glyphs[0][0]).not.toBe(space); + expect(glyphs[0]).toContain(space); + }); + test('a continued segment can switch to lineBreak whitespace', () => { const lines = []; const line = document._line;