Skip to content

Commit

Permalink
fix: fix LuaLaTeX rendering of left-to-right text in right-to-left books
Browse files Browse the repository at this point in the history
  • Loading branch information
max-heller committed Jan 12, 2025
1 parent b6334b2 commit 7214a48
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,43 @@ 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"
"#;
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
│ [WARNING] Missing character: There is no ف (U+0641) (U+0641) in font [lmroman12-bold]:+tlig;!
│ [WARNING] Missing character: There is no ه (U+0647) (U+0647) in font [lmroman12-bold]:+tlig;!
│ [WARNING] Missing character: There is no ر (U+0631) (U+0631) in font [lmroman12-bold]:+tlig;!
│ [WARNING] Missing character: There is no س (U+0633) (U+0633) in font [lmroman12-bold]:+tlig;!
│ [WARNING] Missing character: There is no ت (U+062A) (U+062A) in font [lmroman12-bold]:+tlig;!
│ [WARNING] Missing character: There is no م (U+0645) (U+0645) in font [lmroman12-bold]:+tlig;!
│ [WARNING] Missing character: There is no ط (U+0637) (U+0637) in font [lmroman12-bold]:+tlig;!
│ [WARNING] Missing character: There is no ا (U+0627) (U+0627) in font [lmroman12-bold]:+tlig;!
│ [WARNING] Missing character: There is no ل (U+0644) (U+0644) in font [lmroman12-bold]:+tlig;!
│ [WARNING] Missing character: There is no ب (U+0628) (U+0628) in font [lmroman12-bold]:+tlig;!
│ 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 7214a48

Please sign in to comment.