1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::ffi::{CStr, CString};
use std::time::SystemTime;
use super::{ExecutionState, IoEventBackend, TectonicBridgeApi};
use crate::errors::{DefinitelySame, ErrorKind, Result};
use crate::io::IoStack;
use crate::status::StatusBackend;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TexResult {
Spotless,
Warnings,
Errors,
}
impl DefinitelySame for TexResult {
fn definitely_same(&self, other: &Self) -> bool {
self == other
}
}
#[derive(Debug)]
pub struct TexEngine {
halt_on_error: bool,
initex_mode: bool,
synctex_enabled: bool,
semantic_pagination_enabled: bool,
build_date: SystemTime,
}
impl Default for TexEngine {
fn default() -> Self {
TexEngine {
halt_on_error: true,
initex_mode: false,
synctex_enabled: false,
semantic_pagination_enabled: false,
build_date: SystemTime::UNIX_EPOCH,
}
}
}
impl TexEngine {
pub fn new() -> TexEngine {
TexEngine::default()
}
pub fn halt_on_error_mode(&mut self, halt_on_error: bool) -> &mut Self {
self.halt_on_error = halt_on_error;
self
}
pub fn initex_mode(&mut self, initex: bool) -> &mut Self {
self.initex_mode = initex;
self
}
pub fn synctex(&mut self, synctex_enabled: bool) -> &mut Self {
self.synctex_enabled = synctex_enabled;
self
}
pub fn semantic_pagination(&mut self, enabled: bool) -> &mut Self {
self.semantic_pagination_enabled = enabled;
self
}
pub fn build_date(&mut self, date: SystemTime) -> &mut Self {
self.build_date = date;
self
}
pub fn process(
&mut self,
io: &mut IoStack,
events: &mut dyn IoEventBackend,
status: &mut dyn StatusBackend,
format_file_name: &str,
input_file_name: &str,
) -> Result<TexResult> {
let _guard = super::ENGINE_LOCK.lock().unwrap();
let cformat = CString::new(format_file_name)?;
let cinput = CString::new(input_file_name)?;
let mut state = ExecutionState::new(io, events, status);
let bridge = TectonicBridgeApi::new(&mut state);
let v = if self.halt_on_error { 1 } else { 0 };
unsafe {
super::tt_xetex_set_int_variable(b"halt_on_error_p\0".as_ptr() as _, v);
}
let v = if self.initex_mode { 1 } else { 0 };
unsafe {
super::tt_xetex_set_int_variable(b"in_initex_mode\0".as_ptr() as _, v);
}
let v = if self.synctex_enabled { 1 } else { 0 };
unsafe {
super::tt_xetex_set_int_variable(b"synctex_enabled\0".as_ptr() as _, v);
}
let v = if self.semantic_pagination_enabled {
1
} else {
0
};
unsafe {
super::tt_xetex_set_int_variable(b"semantic_pagination_enabled\0".as_ptr() as _, v);
}
unsafe {
match super::tex_simple_main(
&bridge,
cformat.as_ptr(),
cinput.as_ptr(),
self.build_date
.duration_since(SystemTime::UNIX_EPOCH)
.expect("invalid build date")
.as_secs() as libc::time_t,
) {
0 => Ok(TexResult::Spotless),
1 => Ok(TexResult::Warnings),
2 => Ok(TexResult::Errors),
3 => {
let ptr = super::tt_get_error_message();
let msg = CStr::from_ptr(ptr).to_string_lossy().into_owned();
Err(ErrorKind::Msg(msg).into())
}
x => Err(ErrorKind::Msg(format!(
"internal error: unexpected 'history' value {}",
x
))
.into()),
}
}
}
}