michal-h21 / linebreaker

Some excersises with luatex's line breaking callback
15 stars 1 forks source link

[Bug] Wrong direction in bidirectional documents #7

Closed amarakon closed 1 year ago

amarakon commented 1 year ago

In issue #6, I mentioned how Arabic documents look wrong when using this package. This was fixed shortly after. However, a problem still persists. In bilingual documents where one language is left-to-right and the other language is right-to-left, both cannot work at the same time. If my main language is English and my secondary language is Arabic, the bottom line of the Arabic paragraph will be aligned to the left, when it should be aligned to the right. Similarly, if my main language is Arabic and my secondary language is English, the Arabic paragraph will be rendered correctly but the bottom line of the English paragraph will be aligned to the right instead of the left. Below, I have provided two minimum working examples that demonstrate this, along with the expected behaviour that happens when I don't load the linebreaker package.

\documentclass{article}

\usepackage{linebreaker, lipsum}
\usepackage[bidi = basic]{babel}

\babelprovide[main, import]{english}
\babelprovide[import]{arabic}
\babelfont[arabic]{rm}{Amiri}

\begin{document}

\lipsum[1]

\begin{otherlanguage}{arabic}
    لكن لا بد أن أوضح لك أن كل هذه الأفكار المغلوطة حول استنكار  النشوة وتمجيد
    الألم نشأت بالفعل، وسأعرض لك التفاصيل لتكتشف حقيقة وأساس تلك السعادة البشرية،
    فلا أحد يرفض أو يكره أو يتجنب الشعور بالسعادة، ولكن بفضل هؤلاء الأشخاص الذين لا
    يدركون بأن السعادة لا بد أن نستشعرها بصورة أكثر عقلانية ومنطقية فيعرضهم هذا
    لمواجهة الظروف الأليمة، وأكرر بأنه لا يوجد من يرغب في الحب ونيل المنال ويتلذذ
    بالآلام، الألم هو الألم ولكن نتيجة لظروف ما قد تكمن السعاده فيما نتحمله من كد
    وأسي.
\end{otherlanguage}

\end{document}
\documentclass{article}

\usepackage{linebreaker, lipsum}
\usepackage[bidi = basic]{babel}

\babelprovide[main, import]{arabic}
\babelprovide[import]{english}
\babelfont[arabic]{rm}{Amiri}

\begin{document}

\begin{otherlanguage}{english}
\lipsum[1]
\end{otherlanguage}

لكن لا بد أن أوضح لك أن كل هذه الأفكار المغلوطة حول استنكار  النشوة وتمجيد الألم
نشأت بالفعل، وسأعرض لك التفاصيل لتكتشف حقيقة وأساس تلك السعادة البشرية، فلا أحد
يرفض أو يكره أو يتجنب الشعور بالسعادة، ولكن بفضل هؤلاء الأشخاص الذين لا يدركون بأن
السعادة لا بد أن نستشعرها بصورة أكثر عقلانية ومنطقية فيعرضهم هذا لمواجهة الظروف
الأليمة، وأكرر بأنه لا يوجد من يرغب في الحب ونيل المنال ويتلذذ بالآلام، الألم هو الألم
ولكن نتيجة لظروف ما قد تكمن السعاده فيما نتحمله من كد وأسي.

\end{document}

Expected result

michal-h21 commented 1 year ago

This seems a bit strange. When Lineberak breaks this paragraph:

\begin{otherlanguage}{english}
\lipsum[1]
\end{otherlanguage}

it is marked in tex.pardir as TRT, which means that it should be right to left paragraph. I don't understand why only the last line uses this direction.

Anyway, when I add a paragraph end inside otherlanguage, the paragraph gets a correct pardir information:

\documentclass{article}

\usepackage{linebreaker}
\usepackage{lipsum}
\usepackage[bidi = basic]{babel}

\babelprovide[main, import]{arabic}
\babelprovide[import]{english}
\babelfont[arabic]{rm}{Amiri}

\begin{document}

\begin{otherlanguage}{english}
\lipsum[1]

\end{otherlanguage}

لكن لا بد أن أوضح لك أن كل هذه الأفكار المغلوطة حول استنكار  النشوة وتمجيد الألم
نشأت بالفعل، وسأعرض لك التفاصيل لتكتشف حقيقة وأساس تلك السعادة البشرية، فلا أحد
يرفض أو يكره أو يتجنب الشعور بالسعادة، ولكن بفضل هؤلاء الأشخاص الذين لا يدركون بأن
السعادة لا بد أن نستشعرها بصورة أكثر عقلانية ومنطقية فيعرضهم هذا لمواجهة الظروف
الأليمة، وأكرر بأنه لا يوجد من يرغب في الحب ونيل المنال ويتلذذ بالآلام، الألم هو الألم
ولكن نتيجة لظروف ما قد تكمن السعاده فيما نتحمله من كد وأسي.

\end{document}

obrazek

amarakon commented 1 year ago

Anyway, when I add a paragraph end inside otherlanguage, the paragraph gets a correct pardir information.

Unfortunately, this workaround doesn't work within a multicols environment (from the multicol package) if I try to change the Roman font for English. Every page except for the last will be messed up:

\documentclass{book}

\usepackage{linebreaker}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[bidi = basic]{babel}

\babelprovide[main, import]{english}
\babelprovide[import]{arabic}

\babelfont[english]{rm}{EB Garamond}
\babelfont[arabic]{rm}{Amiri}

\begin{document}

\begin{multicols}{2}
    \lipsum[1-6]

    \begin{otherlanguage}{arabic}
        لكن لا بد أن أوضح لك أن كل هذه الأفكار المغلوطة حول استنكار  النشوة وتمجيد الألم
        نشأت بالفعل، وسأعرض لك التفاصيل لتكتشف حقيقة وأساس تلك السعادة البشرية، فلا أحد
        يرفض أو يكره أو يتجنب الشعور بالسعادة، ولكن بفضل هؤلاء الأشخاص الذين لا يدركون بأن
        السعادة لا بد أن نستشعرها بصورة أكثر عقلانية ومنطقية فيعرضهم هذا لمواجهة الظروف
        الأليمة، وأكرر بأنه لا يوجد من يرغب في الحب ونيل المنال ويتلذذ بالآلام، الألم هو الألم
        ولكن نتيجة لظروف ما قد تكمن السعاده فيما نتحمله من كد وأسي.

    \end{otherlanguage}
\end{multicols}

\end{document}

test-1 test-2

If you uncomment the line \babelfont[english]{rm}{EB Garamond}, it will work as expected.

michal-h21 commented 1 year ago

Ah, that will need further investigation. Unfortunately, I received a bit too many bug reports this week, so it can take some time before I will have the capacity to do that :/

amarakon commented 1 year ago

Sure, no rush.

michal-h21 commented 1 year ago

I've tried your Multicols MWE, and it seems that it produces this wrong result even if I remove Linebreaker altogether. Maybe it is rather bug in Multicols, or in Babel?

amarakon commented 1 year ago

You’re right, the bug still happens without linebreaker. What’s even more interesting is the fact that this bug only happens with the EB Garamond font. I tested it with other fonts and it worked completely fine. So the issue is not with multicols or babel, it’s with EB Garamond for some reason. I’m going to open an issue on https://github.com/georgd/EB-Garamond.

hpvd commented 1 year ago

just gave it a few tries...

when using basic \twocolumn instead of loading multicol package plus \begin{multicols}{2} ... \end{multicols}{2}

enabling/disabling linebreaker package changes the alignment direction

\documentclass{book}

%\usepackage{linebreaker}
\usepackage{lipsum}
%\usepackage{multicol}
\usepackage[bidi = basic]{babel}

\babelprovide[main, import]{english}
\babelprovide[import]{arabic}

\babelfont[english]{rm}{EB Garamond}
\babelfont[arabic]{rm}{Amiri}

\begin{document}

%\begin{multicols}{2}
\twocolumn
    \lipsum[1-6]

    \begin{otherlanguage}{arabic}
        لكن لا بد أن أوضح لك أن كل هذه الأفكار المغلوطة حول استنكار  النشوة وتمجيد الألم
        نشأت بالفعل، وسأعرض لك التفاصيل لتكتشف حقيقة وأساس تلك السعادة البشرية، فلا أحد
        يرفض أو يكره أو يتجنب الشعور بالسعادة، ولكن بفضل هؤلاء الأشخاص الذين لا يدركون بأن
        السعادة لا بد أن نستشعرها بصورة أكثر عقلانية ومنطقية فيعرضهم هذا لمواجهة الظروف
        الأليمة، وأكرر بأنه لا يوجد من يرغب في الحب ونيل المنال ويتلذذ بالآلام، الألم هو الألم
        ولكن نتيجة لظروف ما قد تكمن السعاده فيما نتحمله من كد وأسي.

    \end{otherlanguage}
%\end{multicols}

\end{document}
hpvd commented 1 year ago

2023-03-15_12h38_32

hpvd commented 1 year ago

2023-03-15_12h41_11

hpvd commented 1 year ago

just to verify that not the automatic pagebreak makes the difference: the alignment change by enable//disable linebreaker package keeps the same when you reduce the length of lipsum to \lipsum[1-5] to make it fit to one page (I'm using A4)

hpvd commented 1 year ago

hmm if you add another lipsum after the arabic part, strange things happen even without linebreaker package


\documentclass{book}

%\usepackage{linebreaker}
\usepackage{lipsum}
%\usepackage{multicol}
\usepackage[bidi = basic]{babel}

\babelprovide[main, import]{english}
\babelprovide[import]{arabic}

\babelfont[english]{rm}{EB Garamond}
\babelfont[arabic]{rm}{Amiri}

\begin{document}

%\begin{multicols}{2}
\twocolumn
    \lipsum[1-7]

    \begin{otherlanguage}{arabic}
        لكن لا بد أن أوضح لك أن كل هذه الأفكار المغلوطة حول استنكار  النشوة وتمجيد الألم
        نشأت بالفعل، وسأعرض لك التفاصيل لتكتشف حقيقة وأساس تلك السعادة البشرية، فلا أحد
        يرفض أو يكره أو يتجنب الشعور بالسعادة، ولكن بفضل هؤلاء الأشخاص الذين لا يدركون بأن
        السعادة لا بد أن نستشعرها بصورة أكثر عقلانية ومنطقية فيعرضهم هذا لمواجهة الظروف
        الأليمة، وأكرر بأنه لا يوجد من يرغب في الحب ونيل المنال ويتلذذ بالآلام، الألم هو الألم
        ولكن نتيجة لظروف ما قد تكمن السعاده فيما نتحمله من كد وأسي.

    \end{otherlanguage}
    \lipsum[1-7]
%\end{multicols}

\end{document}

2023-03-15_13h23_47

michal-h21 commented 1 year ago

It seems that in the TRT context, Linebreaker causes the last line of a paragraph to be aligned to the left, instead of the right. Rest of the paragraph seems to be aligned correctly.

michal-h21 commented 1 year ago

Here is even more strange test. The alignment of the last line seems to be pretty random:

\documentclass{book}

\usepackage{linebreaker}
% \linebreakersetup{debug=true}
\usepackage{lipsum}
\usepackage[bidi = basic]{babel}

\babelprovide[main, import]{english}
\babelprovide[import]{arabic}

\babelfont[english]{rm}{EB Garamond}
\babelfont[arabic]{rm}{Amiri}
% \usepackage{lua-visual-debug}

\begin{document}

\newcommand\trysize[1]{%
\columnwidth=#1
\hsize=#1

    % \lipsum[1-5]

    \begin{otherlanguage}{arabic}
        لكن لا بد أن أوضح لك أن كل هذه الأفكار المغلوطة حول استنكار  النشوة وتمجيد الألم
        نشأت بالفعل، وسأعرض لك التفاصيل لتكتشف حقيقة وأساس تلك السعادة البشرية، فلا أحد
        يرفض أو يكره أو يتجنب الشعور بالسعادة، ولكن بفضل هؤلاء الأشخاص الذين لا يدركون بأن
        السعادة لا بد أن نستشعرها بصورة أكثر عقلانية ومنطقية فيعرضهم هذا لمواجهة الظروف
        الأليمة، وأكرر بأنه لا يوجد من يرغب في الحب ونيل المنال ويتلذذ بالآلام، الألم هو الألم
        ولكن نتيجة لظروف ما قد تكمن السعاده فيما نتحمله من كد وأسي.

    \end{otherlanguage}
}

\trysize{200pt}
\trysize{190pt}
\trysize{180pt}
\trysize{170pt}
\trysize{160pt}
\trysize{150pt}
\trysize{140pt}
%\end{multicols}

\end{document}

image

hpvd commented 1 year ago

to help on this topic, @amarakon started a discussion on tex.stackexchange.com https://tex.stackexchange.com/questions/680079/some-fonts-do-not-work-properly-with-multicols-and-babel

hpvd commented 1 year ago

there seems to be a babel issue too, but possibly several things come together:

There is clearly a bug in babel, but the misalignment of the last line is, I think, an issue in linebreaker. I must investigate, but I wonder if linebreaker makes any sense in unhyphenated languages, with different justification methods.

see Misplaced text with multicol, otherlanguage and RTL

michal-h21 commented 1 year ago

I've found the source of the issue with Linebreaker. It didn't pass the parameters table (which contained the info about pardir) to the line-breaking function when there was an overfull line in the paragraph. This is the reason why it was so random and why it happened with shorter line widths. The last update should fix that.

hpvd commented 1 year ago

awesome! many thanks! will post a short note in the babel issue linked above.

hpvd commented 1 year ago

could confirm enabling/disabling linebreaker and the RTL alignment keeps the same:

2023-03-20_15h13_56

amarakon commented 1 year ago

could confirm enabling/disabling linebreaker and the RTL alignment keeps the same:...

Me too. I tested it and the paragraph direction is now correct. There is still the other issue of the whole text being shifted way far to the left until the last page.

hpvd commented 1 year ago

great, since the still existing issue seems to be a babel bug see https://github.com/latex3/babel/issues/228

this one may be closed.

(with the option to reopen, if the upcoming babel fix will not solve it completely)

hpvd commented 1 year ago

of course it would be great to push a new release to ctan including this fix :-)

btw: on ctan one can see and obtain a newer release/tag/version than in this repository:

2023-03-21_09h48_20

michal-h21 commented 1 year ago

Thanks for noticing. It seems that I didn't push tags from my other computer. I've fixed another issue regarding the debug print, and updated CTAN. I hope I can close this issue now :)

hpvd commented 1 year ago

there was already a fix implemented in babel and the issue was closed, see https://github.com/latex3/babel/issues/228

could you @amarakon and @michal-h21 confirm, that this (latest linebreaker plus latest babel) make it work for you? On my side, things still look strange...

hpvd commented 1 year ago

hmm adding \listfiles to tex file shows that I'm not using the latest babel version I have just installed. Maybe because version keeps the same number...

amarakon commented 1 year ago

could you @amarakon and @michal-h21 confirm, that this (latest linebreaker plus latest babel) make it work for you? On my side, things still look strange...

I can confirm that I am seeing the expected result. Are you using the latest babel version from CTAN? I think you have to clone and install it manually from GitHub.

hpvd commented 1 year ago

@amarakon that's great to hear :-) I did a manual install directly using code from github, like I did for the updated linebreaker before. Unfortunately, babel keep using the version from previous install....

amarakon commented 1 year ago

Actually I realized I did not have linebreaker loaded in my preamble upon testing it again. Once I load the package, the problem of the last line being the wrong direction persists. However, the issue with babel is fixed. @hpvd are you having the same issue as me?

hpvd commented 1 year ago

@amarakon are you using latest linebreaker and checked with \listfiles comment at the beginning of the tex file the outputted package versions in the logfile? -> maybe you are doing the something like me but for linebreaker :-)

Maybe you can post your

If both packages are used in latest version, we have to reopen here. (I couldn't get rid of old babel and using this with new linebreaker the direction looks fine for me)

amarakon commented 1 year ago

Testing Code

\documentclass{book}

\usepackage{linebreaker}
\usepackage[bidi = basic]{babel}

\babelprovide[main, import]{english}
\babelprovide[import]{arabic}

\babelfont[english]{rm}{EB Garamond}
\babelfont[arabic]{rm}{Amiri}

\begin{document}

\begin{otherlanguage}{arabic}
    لكن لا بد أن أوضح لك أن كل هذه الأفكار المغلوطة حول استنكار  النشوة وتمجيد الألم
    نشأت بالفعل، وسأعرض لك التفاصيل لتكتشف حقيقة وأساس تلك السعادة البشرية، فلا أحد
    يرفض أو يكره أو يتجنب الشعور بالسعادة، ولكن بفضل هؤلاء الأشخاص الذين لا يدركون بأن
    السعادة لا بد أن نستشعرها بصورة أكثر عقلانية ومنطقية فيعرضهم هذا لمواجهة الظروف
    الأليمة، وأكرر بأنه لا يوجد من يرغب في الحب ونيل المنال ويتلذذ بالآلام، الألم هو الألم
    ولكن نتيجة لظروف ما قد تكمن السعاده فيما نتحمله من كد وأسي.
\end{otherlanguage}

\end{document}

Result

import-2023-03-21_18:39:18

File List

    book.cls    2021/10/04 v1.4n Standard LaTeX document class
    bk10.clo    2021/10/04 v1.4n Standard LaTeX file (size option)
multicol.sty    2021/10/28 v1.9b multicolumn formatting (FMi)
  lipsum.sty    2021-09-20 v2.7 150 paragraphs of Lorem Ipsum dummy text
l3keys2e.sty    2022-01-12 LaTeX2e option processing using LaTeX3 keys
   expl3.sty    2022-04-10 L3 programming layer (loader) 
l3backend-luatex.def    2022-04-14 L3 backend support: PDF output (LuaTeX)
  lipsum.ltd
linebreaker.sty    
luatexbase.sty    2015/10/04 v1.3 luatexbase interface to LuaTeX 
ctablestack.sty    2015/10/01 v1.0 Catcode table stable support
   babel.sty    2023/03/21 3.86.08068 The Babel package
     nil.ldf    2023/03/21 3.86.08068 Nil language
babel-english.tex
babel-arabic.tex
babel-ca-islamic.tex
fontspec.sty    2022/01/15 v2.8a Font selection for XeLaTeX and LuaLaTeX
  xparse.sty    2022-01-12 L3 Experimental document command parser
fontspec-luatex.sty    2022/01/15 v2.8a Font selection for XeLaTeX and LuaLaTeX

 fontenc.sty
fontspec.cfg
   Amiri.fontspec
  ts1cmr.fd    2019/12/16 v2.5j Standard LaTeX font definitions
michal-h21 commented 1 year ago

This is again the issue that we don't get the right paragraph direction in this case, you get the direction of the surrounding text, which is left to right. I don't think that there is a solution for this issue, other than explicit paragraph end before \end{otherlanguage}.

hpvd commented 1 year ago

@michal-h21 looks like we have to reopen this issue and check compatibility to latest Babel..

btw linebreaker package seems not to have the meta information included used by listfiles command, like exact version and date

hpvd commented 1 year ago

...I don't think that there is a solution for this issue, other than explicit paragraph end before \end{otherlanguage}.

If so, couldn't/shouldn't this be done automatically by babel?

Do you understand the latest changes by babel?

michal-h21 commented 1 year ago

The latest change in Babel solves just the multicolumn issue, I think. The problem here is that the otherlanguage environment can be used inside paragraphs, for mixing of languages, so I don't know how this issue could be solved without explicit paragraph end :/

hpvd commented 1 year ago

can confirm latest babel solved the multicolumn issue

hpvd commented 1 year ago

...but there seems to be another edgecase which produce problems when enabling linebreaker. Opened a new issue see https://github.com/michal-h21/linebreaker/issues/9