Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions crates/tok-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,13 @@ fn handle_typeset_document(
}
}

// Compact output, serialized straight from the structs: this runs on every edit, and
// json!() would first copy every glyph into a serde_json::Value and widen f32 to f64.
#[derive(serde::Serialize)]
struct TypesetWithPreflight<'a> {
pages: &'a [tok_typeset::geometry::PageLayoutBox],
preflight: &'a tok_pdf::preflight::PreflightReport,
}
let with_preflight = extra_args.iter().any(|a| a == "--with-preflight");
let json_bytes = if with_preflight {
let mut opts = PdfExportOptions::default();
Expand All @@ -1053,12 +1060,12 @@ fn handle_typeset_document(
}
}
let report = PreflightEngine::evaluate(doc.root(), &pages, &engine.font_manager, &opts);
serde_json::to_vec_pretty(&serde_json::json!({
"pages": pages,
"preflight": report,
}))?
serde_json::to_vec(&TypesetWithPreflight {
pages: &pages,
preflight: &report,
})?
} else {
serde_json::to_vec_pretty(&pages)?
serde_json::to_vec(&pages)?
};
fs::write(output, json_bytes)?;
println!(" [SUCCESS] Written layout JSON to: {}", output);
Expand Down
6 changes: 5 additions & 1 deletion crates/tok-typeset/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use serde::{Deserialize, Serialize};

fn is_zero_u16(v: &u16) -> bool {
*v == 0
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct PhysicalPoint {
pub x: f32, // in DTP points (pt)
Expand Down Expand Up @@ -61,7 +65,7 @@ pub struct GlyphBox {
#[serde(default)]
pub font_index: u16,
/// Index into the owning line's [`LineBox::colors`]; `0` is the paragraph's base color.
#[serde(default)]
#[serde(default, skip_serializing_if = "is_zero_u16")]
pub color_index: u16,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub underline: bool,
Expand Down
9 changes: 4 additions & 5 deletions packages/tok-electron/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,11 +850,10 @@ handle('tok:typeset-document', async (_, payload: { document?: unknown; inputPat
throw new Error(`Typeset failed: output file was not created at ${tempOutFile}`);
}

const rawContent = await fs.promises.readFile(tempOutFile, 'utf-8');
const parsed = JSON.parse(rawContent);
const pages = Array.isArray(parsed) ? parsed : (Array.isArray(parsed?.pages) ? parsed.pages : []);
const preflight = Array.isArray(parsed) ? null : (parsed?.preflight ?? null);
return { success: true, pages, preflight };
// Sent as the raw string and parsed in the renderer: cloning a string over IPC is a copy,
// while parsing here and cloning the object tree cost as much as typesetting itself.
const layoutJson = await fs.promises.readFile(tempOutFile, 'utf-8');
return { success: true, layoutJson };
} finally {
if (tempDocFile) {
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/tok-electron/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface TokIpcBridge {
openDocument: (filePath: string) => Promise<unknown>;
renameDocumentFile: (payload: { oldPath: string; newPath: string }) => Promise<{ success: boolean; filePath?: string; error?: string }>;
getProjectsDir: () => Promise<string>;
typesetDocument: (payload: { document?: unknown; inputPath?: string; assetsFrom?: string; withPreflight?: boolean } | unknown) => Promise<{ success: boolean; pages: any[]; preflight?: unknown }>;
typesetDocument: (payload: { document?: unknown; inputPath?: string; assetsFrom?: string; withPreflight?: boolean } | unknown) => Promise<{ success: boolean; layoutJson?: string; pages?: any[]; preflight?: unknown }>;
runPreflight: (payload: { document?: unknown; inputPath?: string; assetsFrom?: string; standard?: string; bleed?: boolean }) => Promise<{ success: boolean; report?: unknown; error?: string }>;

// Font Inspection & Installation (Rust Core)
Expand Down
9 changes: 5 additions & 4 deletions packages/tok-ui/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
} from './engine/documentBridge';
import type { LineRunLookup, LineRunSource } from './engine/lineRuns';
import { importDocument, parseDocxXml, ImportResult } from './engine/textImporter';
import { layoutToPageDescriptors, PreflightReport } from './engine/typesetBridge';
import { layoutToPageDescriptors, PreflightReport, readTypesetResponse } from './engine/typesetBridge';

import { toHebrewGematria } from './gematria';
import { normalizeHebrew, normalizeQuotes, findHolyNames } from './plugins/hebrew-utils';
Expand Down Expand Up @@ -2365,9 +2365,10 @@ export class TypesetOkApp {
if (requestId !== this.latestTypesetRequestId || abortCtrl.signal.aborted) {
return;
}
if (res && res.success && Array.isArray(res.pages)) {
newPages = layoutToPageDescriptors(res.pages, this.buildLineRunLookup());
const pfReport = (res.preflight ?? null) as PreflightReport | null;
const layout = res && res.success ? readTypesetResponse(res) : null;
if (layout) {
newPages = layoutToPageDescriptors(layout.pages, this.buildLineRunLookup());
const pfReport = layout.preflight;
this.latestPreflightReport = pfReport;
this.inspector?.setPreflightReport(pfReport);
if (pfReport) {
Expand Down
16 changes: 16 additions & 0 deletions packages/tok-ui/src/engine/typesetBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ export interface PageLayoutBox {
diagnostics?: LayoutDiagnostic[];
}

/**
* Reads a typeset IPC response: `layoutJson` is the engine output as text (either a page
* array or `{ pages, preflight }`); older shells sent already-parsed `pages`/`preflight`.
*/
export function readTypesetResponse(res: { layoutJson?: string; pages?: unknown; preflight?: unknown }): {
pages: PageLayoutBox[];
preflight: PreflightReport | null;
} {
const parsed: any = typeof res.layoutJson === 'string' ? JSON.parse(res.layoutJson) : res;
if (Array.isArray(parsed)) return { pages: parsed, preflight: null };
return {
pages: Array.isArray(parsed?.pages) ? parsed.pages : [],
preflight: (parsed?.preflight ?? null) as PreflightReport | null,
};
}

function escapeHtml(str: string): string {
return str
.replace(/&/g, '&amp;')
Expand Down
Loading