Skip to content

Commit

Permalink
Issue #101: Implement StructuredText.copy and .highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
ccxvii committed Jul 10, 2024
1 parent fe9ea16 commit cb04a26
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/mupdf-wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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">,
Expand Down
12 changes: 12 additions & 0 deletions src/mupdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/mupdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cb04a26

Please sign in to comment.