Skip to content

Commit

Permalink
fix: fix LuaLaTeX rendering of left-to-right text in right-to-left bo…
Browse files Browse the repository at this point in the history
…oks (#144)

Fixes errors like:
```
Error producing PDF.
! Undefined control sequence.
l.279 ...اوقات زبان Rust را با C و \LR
```
  • Loading branch information
max-heller authored Jan 12, 2025
1 parent b6334b2 commit d506526
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion scripts/install-ci-deps
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export PATH="$PWD/pandoc-3.6.1/bin:$PATH"
sudo apt-get install -y librsvg2-bin

# TeX Live and fonts
sudo apt-get install -y texlive texlive-luatex texlive-lang-cjk texlive-fonts-extra fonts-font-awesome fonts-noto fonts-noto-cjk fonts-noto-color-emoji
sudo apt-get install -y texlive texlive-luatex texlive-lang-cjk texlive-lang-arabic texlive-fonts-extra fonts-font-awesome fonts-noto fonts-noto-cjk fonts-noto-color-emoji

# mdBook preprocessors needed by example books
cargo install mdbook-i18n-helpers --locked
33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,39 @@ include-in-header = ["file-in-root"]
");
}

#[test]
#[ignore]
fn right_to_left_fonts_lualatex() {
let cfg = r#"
[book]
language = "fa"
[output.pandoc.profile.pdf]
output-file = "book.pdf"
pdf-engine = "lualatex"
[output.pandoc.profile.pdf.variables]
mainfont = "Noto Naskh Arabic"
mainfontfallback = [
"NotoSerif:",
]
"#;
let output = MDBook::init()
.mdbook_config(cfg.parse().unwrap())
.chapter(Chapter::new("", "<span dir=ltr>C++</span>", "chapter.md"))
.build();
insta::assert_snapshot!(output, @r#"
├─ log output
│ INFO mdbook::book: Running the pandoc backend
│ INFO mdbook_pandoc::pandoc::renderer: Running pandoc
│ INFO mdbook_pandoc::pandoc::renderer: Wrote output to book/pdf/book.pdf
├─ pdf/book.pdf
│ <INVALID UTF8>
├─ pdf/src/chapter.md
│ [Para [Span ("", [], [("dir", "ltr")]) [Str "C++"]]]
"#);
}

static BOOKS: Lazy<PathBuf> = Lazy::new(|| Path::new(env!("CARGO_MANIFEST_DIR")).join("books"));

#[test]
Expand Down
15 changes: 15 additions & 0 deletions src/pandoc/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
};

use anyhow::Context as _;
use mdbook::config::TextDirection;
use normpath::PathExt;
use tempfile::NamedTempFile;

Expand Down Expand Up @@ -170,6 +171,20 @@ impl Renderer {
additional_variables.push(("include-before", include_before))
}

if ctx.mdbook_cfg.book.realized_text_direction() == TextDirection::RightToLeft {
// Without this, LuaTeX errors on left-to-right text because the \LR command isn't defined, e.g.:
// Error producing PDF.
// ! Undefined control sequence.
// l.279 ...اوقات زبان Rust را با C و \LR
// (see https://github.com/google/comprehensive-rust/pull/2531#issuecomment-2567445055)
// Using luabidi was suggested in
// https://github.com/jgm/pandoc/issues/8460#issuecomment-1344881107
additional_variables.push((
"header-includes",
r"\ifLuaTeX\usepackage{luabidi}\fi".into(),
));
}

let include_packages = packages
.needed()
.map(|package| format!(r"\usepackage{{{}}}", package.name()))
Expand Down

0 comments on commit d506526

Please sign in to comment.