[LyX/master] Account for babel's bidi option (#12866)

Juergen Spitzmueller spitz at lyx.org
Thu Aug 17 11:10:47 UTC 2023


commit 315c2f132ad79c1ac40a101681365a4150b0496f
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Thu Aug 17 14:25:33 2023 +0200

    Account for babel's bidi option (#12866)
    
    bidi=bidi-{r,l} load the bidi package
---
 src/BufferParams.cpp         |   27 +++++++++++++++++++++++++++
 src/BufferParams.h           |    5 +++++
 src/Font.cpp                 |    4 ++--
 src/OutputParams.cpp         |    6 ------
 src/OutputParams.h           |    2 --
 src/Paragraph.cpp            |    6 +++---
 src/insets/InsetCitation.cpp |    2 +-
 src/insets/InsetRef.cpp      |    2 +-
 src/insets/InsetTabular.cpp  |    4 ++--
 9 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp
index 875b8d0..3dad8c6 100644
--- a/src/BufferParams.cpp
+++ b/src/BufferParams.cpp
@@ -2948,6 +2948,33 @@ bool BufferParams::isLiterate() const
 }
 
 
+bool BufferParams::hasPackageOption(string const package, string const opt) const
+{
+	for (auto const & p : documentClass().packageOptions())
+		if (package == p.first && opt == p.second)
+			return true;
+	return false;
+}
+
+
+bool BufferParams::useBidiPackage(OutputParams const & rp) const
+{
+	return (rp.use_polyglossia
+		// as of babel 3.29, bidi=bidi-* is supported by babel
+		// So we check whether we use a respective version and
+		// whethert bidi-r or bidi-l have been requested either via class
+		// or package options
+		|| (rp.use_babel
+		    && LaTeXFeatures::isAvailableAtLeastFrom("babel", 2019, 4, 3)
+		    && (hasPackageOption("babel", "bidi-r")
+			|| hasPackageOption("babel", "bidi-l")
+			|| contains(options, "bidi-r")
+			|| contains(options, "bidi-l")))
+		)
+		&& rp.flavor == Flavor::XeTeX;
+}
+
+
 void BufferParams::readPreamble(Lexer & lex)
 {
 	if (lex.getString() != "\\begin_preamble")
diff --git a/src/BufferParams.h b/src/BufferParams.h
index 77b4856..2a2e0f3 100644
--- a/src/BufferParams.h
+++ b/src/BufferParams.h
@@ -49,6 +49,7 @@ class LayoutFile;
 class LayoutFileIndex;
 class Length;
 class Lexer;
+class OutputParams;
 class otexstream;
 class PDFOptions;
 class Spacing;
@@ -194,6 +195,10 @@ public:
 	bool isLatex() const;
 	/// returns \c true if the buffer contains a Wed document
 	bool isLiterate() const;
+	/// Is this package option requested?
+	bool hasPackageOption(std::string const package, std::string const opt) const;
+	/// Do we use the bidi package (which does some reordering and stuff)?
+	bool useBidiPackage(OutputParams const & rp) const;
 
 	/// return the format of the buffer on a string
 	std::string bufferFormat() const;
diff --git a/src/Font.cpp b/src/Font.cpp
index aa8c235..8c73e91 100644
--- a/src/Font.cpp
+++ b/src/Font.cpp
@@ -426,7 +426,7 @@ int Font::latexWriteStartChanges(otexstream & os, BufferParams const & bparams,
 	// the numbers are written Left-to-Right. ArabTeX package
 	// and bidi (polyglossia with XeTeX) reorder the number automatically
 	// but the packages used for Hebrew and Farsi (Arabi) do not.
-	if (!runparams.useBidiPackage()
+	if (!bparams.useBidiPackage(runparams)
 	    && !runparams.pass_thru
 	    && bits_.number() == FONT_ON
 	    && prev.fontInfo().number() != FONT_ON
@@ -604,7 +604,7 @@ int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
 	// the numbers are written Left-to-Right. ArabTeX package
 	// and bidi (polyglossia with XeTeX) reorder the number automatically
 	// but the packages used for Hebrew and Farsi (Arabi) do not.
-	if (!runparams.useBidiPackage()
+	if (!bparams.useBidiPackage(runparams)
 	    && !runparams.pass_thru
 	    && bits_.number() == FONT_ON
 	    && next.fontInfo().number() != FONT_ON
diff --git a/src/OutputParams.cpp b/src/OutputParams.cpp
index dd98c52..7c22cb1 100644
--- a/src/OutputParams.cpp
+++ b/src/OutputParams.cpp
@@ -51,10 +51,4 @@ bool OutputParams::isFullUnicode() const
 			|| flavor == Flavor::XeTeX;
 }
 
-
-bool OutputParams::useBidiPackage() const
-{
-	return use_polyglossia && flavor == Flavor::XeTeX;
-}
-
 } // namespace lyx
diff --git a/src/OutputParams.h b/src/OutputParams.h
index 40a7a47..9d9a7ae 100644
--- a/src/OutputParams.h
+++ b/src/OutputParams.h
@@ -83,8 +83,6 @@ public:
 	bool isLaTeX() const;
 	/// does this flavour support full unicode?
 	bool isFullUnicode() const;
-	/// Do we use the bidi package (which does some reordering and stuff)?
-	bool useBidiPackage() const;
 
 	/// Same, but for math output, which only matter is XHTML output.
 	MathFlavor math_flavor = NotApplicable;
diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp
index d4658fa..30a5e5e 100644
--- a/src/Paragraph.cpp
+++ b/src/Paragraph.cpp
@@ -2055,7 +2055,7 @@ char_type Paragraph::getUChar(BufferParams const & bparams,
 	// or if we use poylglossia/bidi (XeTeX)
 	// or with babel and Xe/LuaTeX.
 	if (!getFontSettings(bparams, pos).isRightToLeft()
-	    || rp.useBidiPackage()
+	    || bparams.useBidiPackage(rp)
 	    || (rp.use_babel && rp.isFullUnicode()))
 		return c;
 
@@ -2548,7 +2548,7 @@ int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
 	// RTL in classic (PDF)LaTeX (without the Bidi package)
 	// Luabibdi (used by LuaTeX) behaves like classic
 	bool const rtl_classic = owner_->getParLanguage(bparams)->rightToLeft()
-		&& !runparams.useBidiPackage();
+		&& !bparams.useBidiPackage(runparams);
 
 	switch (curAlign) {
 	case LYX_ALIGN_NONE:
@@ -2612,7 +2612,7 @@ bool Paragraph::Private::endTeXParParams(BufferParams const & bparams,
 	// RTL in classic (PDF)LaTeX (without the Bidi package)
 	// Luabibdi (used by LuaTeX) behaves like classic
 	bool const rtl_classic = owner_->getParLanguage(bparams)->rightToLeft()
-		&& !runparams.useBidiPackage();
+		&& !bparams.useBidiPackage(runparams);
 
 	switch (curAlign) {
 	case LYX_ALIGN_NONE:
diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp
index c684b4c..b1d01f4 100644
--- a/src/insets/InsetCitation.cpp
+++ b/src/insets/InsetCitation.cpp
@@ -543,7 +543,7 @@ bool InsetCitation::forceLTR(OutputParams const & rp) const
 	// We have to force LTR for numeric references
 	// [= bibliography, plain BibTeX, numeric natbib
 	// and biblatex]. Except for XeTeX/bidi. See #3005.
-	if (rp.useBidiPackage())
+	if (buffer().masterParams().useBidiPackage(rp))
 		return false;
 	return (buffer().masterParams().citeEngine() == "basic"
 		|| buffer().masterParams().citeEngineType() == ENGINE_TYPE_NUMERICAL);
diff --git a/src/insets/InsetRef.cpp b/src/insets/InsetRef.cpp
index 49cf630..0938b47 100644
--- a/src/insets/InsetRef.cpp
+++ b/src/insets/InsetRef.cpp
@@ -611,7 +611,7 @@ bool InsetRef::forceLTR(OutputParams const & rp) const
 	// * Parentheses are automatically swapped with XeTeX/bidi 
 	//   [not with LuaTeX/luabidi] (see #11626).
 	// FIXME: Re-Audit all other RTL cases.
-	if (rp.useBidiPackage())
+	if (buffer().masterParams().useBidiPackage(rp))
 		return false;
 	return (getCmdName() != "nameref" || !buffer().masterParams().useNonTeXFonts);
 }
diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp
index 69f9e73..e8fc1ba 100644
--- a/src/insets/InsetTabular.cpp
+++ b/src/insets/InsetTabular.cpp
@@ -3180,7 +3180,7 @@ void Tabular::TeXRow(otexstream & os, row_type row,
 	// Luabibdi (used by LuaTeX) behaves like classic
 	bool const bidi_rtl =
 		runparams.local_font->isRightToLeft()
-		&& runparams.useBidiPackage();
+		&& buffer().params().useBidiPackage(runparams);
 	bool const ct = !buffer().params().output_changes;
 	idx_type lastcell =
 		bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
@@ -3352,7 +3352,7 @@ void Tabular::latex(otexstream & os, OutputParams const & runparams) const
 	// order for RTL (#9686). Thus we use this list.
 	bool const bidi_rtl =
 		runparams.local_font->isRightToLeft()
-		&& runparams.useBidiPackage();
+		&& buffer().params().useBidiPackage(runparams);
 	list<col_type> columns;
 	list<col_type> logical_columns;
 	for (col_type cl = 0; cl < ncols(); ++cl) {


More information about the lyx-cvs mailing list