Skip to content

Syllable spacing in Lua#1720

Open
davidweichiang wants to merge 6 commits intogregorio-project:developfrom
davidweichiang:lua-syllable-spacing
Open

Syllable spacing in Lua#1720
davidweichiang wants to merge 6 commits intogregorio-project:developfrom
davidweichiang:lua-syllable-spacing

Conversation

@davidweichiang
Copy link
Copy Markdown
Contributor

@davidweichiang davidweichiang commented Mar 1, 2026

This isn't ready for merging yet, but is just here for comments.

This is aiming towards #1673, but pursuing a different strategy than what was proposed there.

The first commit (02ec952) moves syllable rewriting into Lua, which has the following benefits:

  • The old algorithm worked by moving the last part of a syllable to the first part of the next syllable. It could therefore break ligatures between the middle and last part of a syllable (Syllable rewriting bugs #1719). The new algorithm works by joining the text boxes together; it does not break ligatures.
  • If maximumspacewithoutdash > 0, the old algorithm could make the last part of a syllable "jump" across the space between syllables (Syllable rewriting bugs #1719 again); the new one only rewrites syllables if they are actually touching.

And there are still the following problems:

  • The old algorithm computed syllable spacing after rewriting; the new algorithm computes them in the opposite order. This means that some syllable spacing will be slightly too big or small, and is the main reason this is not ready for merging. It will be fixable by also moving syllable spacing into Lua.
  • When the width of a syllable changes due to rewriting, the new syllable is left-aligned with the old syllable, but it might make more sense to center it with the old syllable.

The first problem can be seen in the test
tex-output/syllable-rewriting/syllable-rewriting.tex

Further tests that have miniscule differences:
gabc-output/glyphs/torculus_resupinus_quilisma.gabc
gabc-output/glyphs/torculus_resupinus.gabc
gabc-output/slurs.gabc
gabc-output/FactusEst.gabc
tex-output/dominican-flats/dominican-flats.tex
tex-output/SalveReginaOP/SalveReginaOP.tex
tex-output/bugs/fix-595/fix-595.tex
tex-output/bugs/fix-1110/fix-1110.tex
tex-output/lyric-centering/lyric-centering.tex

Tests that have big changes:

Closes #1719.

…erently from the old one, in the following ways:

+ The old algorithm worked by moving the last part of a syllable to the first part of the next syllable. It could therefore break ligatures between the middle and last part of a syllable. The new algorithm works by joining the text boxes together; it does not break ligatures.
+ If maximumspacewithoutdash > 0, the old algorithm could make the last part of a syllable "jump" across the space between syllables; the new one only rewrites syllables if they are actually touching.
- The old algorithm computed syllable spacing after rewriting; the new algorithm computes them in the opposite order. This means that some syllable spacing will be slightly too big or small. This will be fixable when syllable spacing is also moved to Lua.
…calculated in TeX for now, because it's needed in a couple of places, but \GreSyllable only generates a zero-width \hskip, and the Lua code recalculates it and resizes the \hskip accordingly.

The Lua syllablefinalskip calculation uses the actual begindifference of the next syllable, not the predicted one (nextbegindifference). In the old code, sometimes this prediction was wrong (gregorio-project#1723). Therefore this commit breaks several tests, but I believe they are all actually bug fixes.
@davidweichiang
Copy link
Copy Markdown
Contributor Author

davidweichiang commented Mar 6, 2026

Commit 4fa508d starts to move the calculation of syllablefinalskip into Lua. The TeX code computes the minimum distance between text and notes (between \GreSyllables only, not \GreBarSyllables), and emits a zero-width skip instead of syllablefinalskip. The Lua code computes the syllablefinalskip and edits the skip to have the right width.

Because it uses the actual begindifference of the next syllable, not nextbegindifference, and there are some existing bugs in how nextbegindifference is calculated, there are quite a few new broken tests.

Because of #1723:
gabc-output/glyphs/accidentals.gabc (case 1)
gabc-output/glyphs/brackets.gabc (case 4)
gabc-output/bugs/fix-1700.gabc (case 2)
gabc-output/soft-alterations.gabc (case 3)
tex-output/nabc-alignment/nabc-alignment-two-voices.tex (case 5)
tex-output/nabc-alignment/nabc-alignment.tex (case 5)
gabc-output/lines/2-lines.gabc (case 6)
gabc-output/lines/3-lines.gabc (case 6)
gabc-output/lines/5-lines.gabc (case 6)

Because of #1724:
gabc-output/glyphs/clef_change.gabc
gabc-output/glyphs/only-special.gabc
gabc-output/bugs/fix-1285.gabc
gabc-output/edge_cases.gabc

Only miniscule changes:
gabc-output/glyphs/punctum-inclinatum-2.gabc

- Move syllable clearing for \GreSyllables into Lua, which works by directly measuring the distances between syllables. This fixes issue gregorio-project#1725 but also breaks tests/gabc-output/glyphs/clear.gabc because it now inserts no extra space (and never removes space).
- Simplify syllable spacing by directly measuring distances between syllables, similarly to above.
@davidweichiang
Copy link
Copy Markdown
Contributor Author

Commit ec4e22c moves syllable clearing into Lua.

The amount to shift a syllable in order to clear it is now measured exactly, whereas previously it depended on predicting the begindifference of the next syllable, as well as issue #1725. So cleared syllables are tighter than before.

Breaks two new tests:
gabc-output/glyphs/clear.gabc: because of the changes to clearing described above
tex-output/bar-spacing-old/bar-spacing-old.tex: because the actual begindifference of a bar is used, whereas before it was 0

…en a syllable and the next, a hyphen is inserted. This used to be done in \GreSyllable, but now is done in the Lua pre_linebreak filter.

- Added some missing cases to \gre@calculate@eolshift
- Remove TeX code computing syllablefinalskip and code that depends on it
  * It is still used inside the new bar spacing algorithm, but for a different purpose
  * There is still a macro \gre@calculate@syllablefinalskip, but it actually computes minNotesDistance and minTextDistance
- Intra-line and end-of-line hyphens are now generated by the same code
  * One very minor side effect is that end-of-line hyphens should now be correctly kerned.
@rpspringuel rpspringuel added this to the 7.0.0 milestone Mar 18, 2026
@davidweichiang
Copy link
Copy Markdown
Contributor Author

davidweichiang commented Mar 18, 2026

Commit a48c861 moves hyphenation into Lua. This was the last thing that depended on syllablefinalskip, so syllablefinalskip can be deleted from the TeX code, and perhaps it would make sense to merge (so that @lbssousa can work on #1715), after 6.2.0 of course.

Some very minor benefits:

  • A couple of cases in \gre@calculate@eolshift appeared to be incorrect or missing; these are now fixed.
  • End-of-line hyphens used to not be kerned, but now they are.
  • When lyrics are hidden, the old code inserted extra stretch after hyphenated syllables. The new code does not. This affects one test:
    • tex-output/hidelyricsnotes/hidelyricsnotes.tex

Several more tests are broken.

If a hyphenated word comes before a bar, the space between them is now incorrect until bar spacing is moved into Lua. This happens in the following tests:

  • gabc-output/glyphs/custos_linebreak.gabc
  • tex-output/bar-spacing/bar-spacing-new.tex
  • tex-output/bar-spacing/lyric-centering.tex: only the lines printed for debugging are affected

Miniscule differences:

  • gabc-output/two-nabc.gabc
  • gabc-output/bugs/fix-1424.gabc
  • gabc-output/bugs/fix-804.gabc
  • gabc-output/bugs/fix-347.gabc
  • tex-output/illumination/illumination.tex
  • tex-output/bugs/fix-1110/fix-1110.tex
  • tex-output/bugs/fix-1319/fix-1319.tex
  • tex-output/snippet/snippet.tex
  • tex-output/hidelyricsnotes/text-only.tex

More than miniscule but not worrisome in my opinion:

  • tex-output/visibility-controls/visibility-controls-main.tex

@davidweichiang davidweichiang marked this pull request as ready for review March 27, 2026 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Syllable rewriting bugs

2 participants