Skip to content

Commit 8707b0a

Browse files
committed
Port dvi_four, dvi_two, and dvi_pop
1 parent 96a91dc commit 8707b0a

7 files changed

Lines changed: 81 additions & 57 deletions

File tree

crates/engine_xetex/src/c_api/dvi.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::c_api::engine::TEX_INFINITY;
1+
use crate::c_api::engine::{POP, TEX_INFINITY};
22
use crate::c_api::fatal_error;
33
use crate::c_api::globals::Globals;
44
use std::cell::RefCell;
@@ -171,3 +171,49 @@ pub fn rs_dvi_out(globals: &mut Globals<'_, '_>, c: u8) {
171171
pub extern "C" fn dvi_out(c: u8) {
172172
Globals::with(|globals| rs_dvi_out(globals, c))
173173
}
174+
175+
pub fn rs_dvi_four(globals: &mut Globals<'_, '_>, mut x: i32) {
176+
// TODO: Honestly, this could just use `x.to_*_bytes()`
177+
if x >= 0 {
178+
rs_dvi_out(globals, (x / 0x1000000) as u8);
179+
} else {
180+
x = x + 0x40000000;
181+
x = x + 0x40000000;
182+
rs_dvi_out(globals, ((x / 0x1000000) + 128) as u8);
183+
}
184+
185+
x = x % 0x1000000;
186+
rs_dvi_out(globals, (x / 0x10000) as u8);
187+
188+
x = x % 0x10000;
189+
rs_dvi_out(globals, (x / 0x100) as u8);
190+
rs_dvi_out(globals, (x % 0x100) as u8);
191+
}
192+
193+
#[no_mangle]
194+
pub extern "C" fn dvi_four(x: i32) {
195+
Globals::with(|globals| rs_dvi_four(globals, x))
196+
}
197+
198+
pub fn rs_dvi_two(globals: &mut Globals<'_, '_>, mut s: u16) {
199+
rs_dvi_out(globals, (s / 0x100) as u8);
200+
rs_dvi_out(globals, (s % 0x100) as u8);
201+
}
202+
203+
#[no_mangle]
204+
pub extern "C" fn dvi_two(s: u16) {
205+
Globals::with(|globals| rs_dvi_two(globals, s))
206+
}
207+
208+
pub fn rs_dvi_pop(globals: &mut Globals<'_, '_>, l: i32) {
209+
if l == globals.dvi.offset + globals.dvi.ptr && globals.dvi.ptr > 0 {
210+
globals.dvi.ptr -= 1;
211+
} else {
212+
rs_dvi_out(globals, POP);
213+
}
214+
}
215+
216+
#[no_mangle]
217+
pub extern "C" fn dvi_pop(l: i32) {
218+
Globals::with(|globals| rs_dvi_pop(globals, l))
219+
}

crates/engine_xetex/src/c_api/engine.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub const SCRIPT_SCRIPT_SIZE: usize = 512;
2626
/* "the largest positive value that TeX knows" */
2727
pub const TEX_INFINITY: i32 = 0x7FFFFFFF;
2828

29+
pub const POP: u8 = 142;
30+
2931
thread_local! {
3032
pub static ENGINE_CTX: RefCell<EngineCtx> = RefCell::new(EngineCtx::new())
3133
}
@@ -56,6 +58,7 @@ pub struct EngineCtx {
5658
pub(crate) cur_input: InputState,
5759
pub(crate) interaction: InteractionMode,
5860
pub(crate) history: History,
61+
pub(crate) total_pages: i32,
5962

6063
pub(crate) eqtb: Vec<MemoryWord>,
6164
pub(crate) prim: Box<[B32x2; PRIM_SIZE + 1]>,
@@ -160,6 +163,7 @@ impl EngineCtx {
160163
cur_input: InputState::default(),
161164
interaction: InteractionMode::Batch,
162165
history: History::Spotless,
166+
total_pages: 0,
163167

164168
eqtb: Vec::new(),
165169
prim: Box::new([B32x2 { s0: 0, s1: 0 }; PRIM_SIZE + 1]),
@@ -574,6 +578,16 @@ pub extern "C" fn set_history(val: u8) {
574578
ENGINE_CTX.with_borrow_mut(|engine| engine.history = History::try_from(val).unwrap())
575579
}
576580

581+
#[no_mangle]
582+
pub extern "C" fn total_pages() -> i32 {
583+
ENGINE_CTX.with_borrow(|engine| engine.total_pages)
584+
}
585+
586+
#[no_mangle]
587+
pub extern "C" fn set_total_pages(val: i32) {
588+
ENGINE_CTX.with_borrow_mut(|engine| engine.total_pages = val)
589+
}
590+
577591
#[no_mangle]
578592
pub extern "C" fn eqtb(idx: usize) -> MemoryWord {
579593
ENGINE_CTX.with_borrow(|engine| engine.eqtb[idx])

crates/engine_xetex/xetex/xetex-ini.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ int32_t *kern_base;
169169
int32_t *exten_base;
170170
int32_t *param_base;
171171
b16x4 null_character;
172-
int32_t total_pages;
173172
scaled_t max_v;
174173
scaled_t max_h;
175174
int32_t max_push;
@@ -3102,7 +3101,7 @@ initialize_more_variables(void)
31023101
null_character.s2 = 0;
31033102
null_character.s1 = 0;
31043103
null_character.s0 = 0;
3105-
total_pages = 0;
3104+
set_total_pages(0);
31063105
max_v = 0;
31073106
max_h = 0;
31083107
max_push = 0;

crates/engine_xetex/xetex/xetex-shipout.c

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ static void prune_movements(int32_t l);
2929
static void special_out(int32_t p);
3030
static void write_out(int32_t p);
3131
static void pic_out(int32_t p);
32-
static void dvi_four(int32_t x);
33-
static void dvi_two(UTF16_code s);
34-
static void dvi_pop(int32_t l);
3532
static void dvi_font_def(internal_font_number f);
3633

3734

@@ -163,7 +160,7 @@ ship_out(int32_t p)
163160

164161
/* First page? Emit preamble items. */
165162

166-
if (total_pages == 0) {
163+
if (total_pages() == 0) {
167164
dvi_out(PRE);
168165

169166
if (semantic_pagination_enabled)
@@ -233,7 +230,7 @@ ship_out(int32_t p)
233230
hlist_out();
234231

235232
dvi_out(EOP);
236-
total_pages++;
233+
set_total_pages(total_pages()+1);
237234
set_cur_s(-1);
238235

239236
done:
@@ -2265,13 +2262,13 @@ finalize_dvi_file(void)
22652262
dvi_out(POP);
22662263
} else {
22672264
dvi_out(EOP);
2268-
total_pages++;
2265+
set_total_pages(total_pages()+1);
22692266
}
22702267

22712268
set_cur_s(cur_s()-1);
22722269
}
22732270

2274-
if (total_pages == 0) {
2271+
if (total_pages() == 0) {
22752272
print_nl_cstr("No pages of output.");
22762273
return;
22772274
}
@@ -2291,8 +2288,8 @@ finalize_dvi_file(void)
22912288
dvi_four(max_h);
22922289
dvi_out(max_push / 256);
22932290
dvi_out(max_push % 256);
2294-
dvi_out((total_pages / 256) % 256);
2295-
dvi_out(total_pages % 256);
2291+
dvi_out((total_pages() / 256) % 256);
2292+
dvi_out(total_pages() % 256);
22962293

22972294
while (font_ptr > FONT_BASE) {
22982295
if (font_used[font_ptr])
@@ -2332,8 +2329,8 @@ finalize_dvi_file(void)
23322329
print_nl_cstr("Output written on ");
23332330
print(output_file_name);
23342331
print_cstr(" (");
2335-
print_int(total_pages);
2336-
if (total_pages != 1)
2332+
print_int(total_pages());
2333+
if (total_pages() != 1)
23372334
print_cstr(" pages");
23382335
else
23392336
print_cstr(" page");
@@ -2352,42 +2349,3 @@ finalize_dvi_file(void)
23522349
/* XeTeX adds history = OUTPUT_FAILURE = 4 here; I'm not implementing that. */
23532350
}
23542351
}
2355-
2356-
2357-
static void
2358-
dvi_four(int32_t x)
2359-
{
2360-
if (x >= 0) {
2361-
dvi_out(x / 0x1000000);
2362-
} else {
2363-
x = x + 0x40000000;
2364-
x = x + 0x40000000;
2365-
dvi_out((x / 0x1000000) + 128);
2366-
}
2367-
2368-
x = x % 0x1000000;
2369-
dvi_out(x / 0x10000);
2370-
2371-
x = x % 0x10000;
2372-
dvi_out(x / 0x100);
2373-
dvi_out(x % 0x100);
2374-
}
2375-
2376-
2377-
static void
2378-
dvi_two(UTF16_code s)
2379-
{
2380-
dvi_out(s / 0x100);
2381-
dvi_out(s % 0x100);
2382-
}
2383-
2384-
2385-
static void
2386-
dvi_pop(int32_t l)
2387-
{
2388-
if (l == dvi_offset() + dvi_ptr() && dvi_ptr() > 0) {
2389-
set_dvi_ptr(dvi_ptr()-1);
2390-
} else {
2391-
dvi_out(POP);
2392-
}
2393-
}

crates/engine_xetex/xetex/xetex-synctex.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ void synctex_sheet(int32_t mag)
400400
}
401401
return;
402402
}
403-
if (total_pages == 0) {
403+
if (total_pages() == 0) {
404404
/* Now it is time to properly set up the scale factor. */
405405
if (mag > 0) {
406406
synctex_ctxt.magnification = mag;
@@ -411,7 +411,7 @@ void synctex_sheet(int32_t mag)
411411
* or it was activated with the \synctex macro and the first page is already shipped out.
412412
* Second possibility: tries to open the .synctex, useful if synchronization was enabled
413413
* from the source file and not from the CLI. */
414-
synctex_record_sheet(total_pages + 1);
414+
synctex_record_sheet(total_pages() + 1);
415415
}
416416
return;
417417
}
@@ -424,7 +424,7 @@ void synctex_teehs(void)
424424
if (synctex_ctxt.flags.off || !synctex_ctxt.file) {
425425
return;
426426
}
427-
synctex_record_teehs(total_pages);/* not total_pages+1*/
427+
synctex_record_teehs(total_pages());/* not total_pages+1*/
428428
return;
429429
}
430430

crates/engine_xetex/xetex/xetex-xetexd.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ extern int32_t *kern_base;
529529
extern int32_t *exten_base;
530530
extern int32_t *param_base;
531531
extern b16x4 null_character;
532-
extern int32_t total_pages;
533532
extern scaled_t max_v;
534533
extern scaled_t max_h;
535534
extern int32_t max_push;

crates/engine_xetex/xetex/xetex_bindings.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
#define TEX_INFINITY 2147483647
4444

45+
#define POP 142
46+
4547
#define WHATSIT_NODE 8
4648

4749
#define NATIVE_WORD_NODE 40
@@ -205,6 +207,12 @@ void deinitialize_shipout_variables(void);
205207

206208
void dvi_out(uint8_t c);
207209

210+
void dvi_four(int32_t x);
211+
212+
void dvi_two(uint16_t s);
213+
214+
void dvi_pop(int32_t l);
215+
208216
uint32_t selector(void);
209217

210218
void set_selector(uint32_t val);

0 commit comments

Comments
 (0)