From cb04a26148f386906e5acd37c3f3ddfea034b00e Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 10 Jul 2024 18:33:17 +0200 Subject: [PATCH] Issue #101: Implement StructuredText.copy and .highlight --- src/mupdf-wasm.d.ts | 2 ++ src/mupdf.c | 12 ++++++++++++ src/mupdf.ts | 19 +++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/mupdf-wasm.d.ts b/src/mupdf-wasm.d.ts index 3943b5a..a178d8c 100644 --- a/src/mupdf-wasm.d.ts +++ b/src/mupdf-wasm.d.ts @@ -243,6 +243,8 @@ interface Libmupdf { _wasm_close_document_writer(wri: Pointer<"fz_document_writer">): void, _wasm_print_stext_page_as_json(page: Pointer<"fz_stext_page">, scale: number): Pointer<"char">, _wasm_search_stext_page(text: Pointer<"fz_stext_page">, needle: Pointer<"char">, marks: Pointer<"int">, hits: Pointer<"fz_quad">, hit_max: number): number, + _wasm_copy_selection(text: Pointer<"fz_stext_page">, a: Pointer<"fz_point">, b: Pointer<"fz_point">): Pointer<"char">, + _wasm_highlight_selection(text: Pointer<"fz_stext_page">, a: Pointer<"fz_point">, b: Pointer<"fz_point">, hits: Pointer<"fz_quad">, n: number): number, _wasm_open_document_with_buffer(magic: Pointer<"char">, buffer: Pointer<"fz_buffer">): Pointer<"any_document">, _wasm_open_document_with_stream(magic: Pointer<"char">, stream: Pointer<"fz_stream">): Pointer<"any_document">, _wasm_format_link_uri(doc: Pointer<"any_document">, ch: number, pg: number, ty: number, x: number, y: number, w: number, h: number, z: number): Pointer<"char">, diff --git a/src/mupdf.c b/src/mupdf.c index 12f1b07..918f401 100644 --- a/src/mupdf.c +++ b/src/mupdf.c @@ -849,6 +849,18 @@ int wasm_search_stext_page(fz_stext_page *text, char *needle, int *marks, fz_qua INTEGER(fz_search_stext_page, text, needle, marks, hits, hit_max) } +EXPORT +char * wasm_copy_selection(fz_stext_page *text, fz_point *a, fz_point *b) +{ + POINTER(fz_copy_selection, text, *a, *b, 0); +} + +EXPORT +int wasm_highlight_selection(fz_stext_page *text, fz_point *a, fz_point *b, fz_quad *hits, int n) +{ + INTEGER(fz_highlight_selection, text, *a, *b, hits, n); +} + // --- Document --- EXPORT diff --git a/src/mupdf.ts b/src/mupdf.ts index 87c067e..fa1e6ef 100644 --- a/src/mupdf.ts +++ b/src/mupdf.ts @@ -1330,8 +1330,23 @@ export class StructuredText extends Userdata<"fz_stext_page"> { return fromStringFree(libmupdf._wasm_print_stext_page_as_json(this.pointer, scale)) } - // TODO: highlight(a, b) -> quad[] - // TODO: copy(a, b) -> string + copy(p: Point, q: Point): string { + return fromStringFree(libmupdf._wasm_copy_selection(this.pointer, POINT(p), POINT2(q))) + } + + highlight(p: Point, q: Point, max_hits = 100): Quad[] { + let hits = 0 as Pointer<"fz_quad"> + let result: Quad[] = [] + try { + hits = Malloc<"fz_quad">(32 * max_hits) + let n = libmupdf._wasm_highlight_selection(this.pointer, POINT(p), POINT2(q), hits, max_hits) + for (let i = 0; i < n; ++i) + result.push(fromQuad(hits + i * 32 as Pointer<"fz_quad">)) + } finally { + Free(hits) + } + return result + } search(needle: string, max_hits = 500) { return runSearch(libmupdf._wasm_search_stext_page, this.pointer, needle, max_hits)