Skip to content

Commit

Permalink
Add StructuredText.snap to snap selection points to text lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccxvii committed Jan 27, 2025
1 parent 3c6d33c commit 02782e6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/mupdf-wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ 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_snap_selection(text: Pointer<"fz_stext_page">, a: Pointer<"fz_point">, b: Pointer<"fz_point">, mode: number): Pointer<"fz_quad">,
_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_print_stext_page_as_html(page: Pointer<"fz_stext_page">, id: number): Pointer<"char">,
Expand Down
6 changes: 6 additions & 0 deletions src/mupdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,12 @@ 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
fz_quad * wasm_snap_selection(fz_stext_page *text, fz_point *a, fz_point *b, int mode)
{
QUAD(fz_snap_selection, text, a, b, mode);
}

EXPORT
char * wasm_copy_selection(fz_stext_page *text, fz_point *a, fz_point *b)
{
Expand Down
19 changes: 16 additions & 3 deletions src/mupdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,12 +1300,20 @@ interface StructuredTextWalker {
endTextBlock?(): void
}

type SelectMode = "chars" | "words" | "lines"

export class StructuredText extends Userdata<"fz_stext_page"> {
static override readonly _drop = libmupdf._wasm_drop_stext_page

static readonly SELECT_CHARS = 0
static readonly SELECT_WORDS = 1
static readonly SELECT_LINES = 2
static readonly SELECT_MODE: SelectMode[] = [
"chars",
"words",
"lines"
]

static readonly SELECT_CHARS = "chars"
static readonly SELECT_WORDS = "words"
static readonly SELECT_LINES = "lines"

walk(walker: StructuredTextWalker) {
let block = libmupdf._wasm_stext_page_get_first_block(this.pointer)
Expand Down Expand Up @@ -1373,6 +1381,11 @@ export class StructuredText extends Userdata<"fz_stext_page"> {
return fromStringFree(libmupdf._wasm_print_stext_page_as_text(this.pointer))
}

snap(p: Point, q: Point, mode: SelectMode): Quad {
let mm = ENUM<SelectMode>(mode, StructuredText.SELECT_MODE)
return fromQuad(libmupdf._wasm_snap_selection(this.pointer, POINT(p), POINT2(q), mm))
}

copy(p: Point, q: Point): string {
return fromStringFree(libmupdf._wasm_copy_selection(this.pointer, POINT(p), POINT2(q)))
}
Expand Down

0 comments on commit 02782e6

Please sign in to comment.