Skip to content
Closed
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
14 changes: 14 additions & 0 deletions fitz_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ int run_page_contents(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix

return 1;
}

static void go_fitz_silent_cb(void *user, const char *message) {
(void)user;
(void)message;
}

void go_fitz_silence(fz_context *ctx) {
fz_set_warning_callback(ctx, go_fitz_silent_cb, NULL);
fz_set_error_callback(ctx, go_fitz_silent_cb, NULL);
}
*/
import "C"

Expand Down Expand Up @@ -103,6 +113,8 @@ func New(filename string) (f *Document, err error) {
return
}

C.go_fitz_silence(f.ctx)

C.fz_register_document_handlers(f.ctx)

cfilename := C.CString(filename)
Expand Down Expand Up @@ -136,6 +148,8 @@ func NewFromMemory(b []byte) (f *Document, err error) {
return
}

C.go_fitz_silence(f.ctx)

C.fz_register_document_handlers(f.ctx)

f.stream = C.fz_open_memory(f.ctx, (*C.uchar)(&b[0]), C.size_t(len(b)))
Expand Down
19 changes: 19 additions & 0 deletions fitz_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func New(filename string) (f *Document, err error) {
return
}

applySilence(f.ctx)

fzRegisterDocumentHandlers(f.ctx)

f.doc = fzOpenDocument(f.ctx, filename)
Expand Down Expand Up @@ -75,6 +77,8 @@ func NewFromMemory(b []byte) (f *Document, err error) {
return
}

applySilence(f.ctx)

fzRegisterDocumentHandlers(f.ctx)

f.stream = fzOpenMemory(f.ctx, unsafe.SliceData(b), uint64(len(b)))
Expand Down Expand Up @@ -567,8 +571,19 @@ var (
fzPrintStextPageAsHTML func(ctx *fzContext, out *fzOutput, page *fzStextPage, id int)
fzPrintStextHeaderAsHTML func(ctx *fzContext, out *fzOutput)
fzPrintStextTrailerAsHTML func(ctx *fzContext, out *fzOutput)
fzSetWarningCallback func(ctx *fzContext, cb uintptr, user *byte)
fzSetErrorCallback func(ctx *fzContext, cb uintptr, user *byte)

silentCallback uintptr
)

// applySilence installs no-op warning/error callbacks on the given context,
// suppressing MuPDF stderr output.
func applySilence(ctx *fzContext) {
fzSetWarningCallback(ctx, silentCallback, nil)
fzSetErrorCallback(ctx, silentCallback, nil)
}

func init() {
libmupdf = loadLibrary()

Expand Down Expand Up @@ -624,6 +639,10 @@ func init() {
purego.RegisterLibFunc(&fzPrintStextPageAsHTML, libmupdf, "fz_print_stext_page_as_html")
purego.RegisterLibFunc(&fzPrintStextHeaderAsHTML, libmupdf, "fz_print_stext_header_as_html")
purego.RegisterLibFunc(&fzPrintStextTrailerAsHTML, libmupdf, "fz_print_stext_trailer_as_html")
purego.RegisterLibFunc(&fzSetWarningCallback, libmupdf, "fz_set_warning_callback")
purego.RegisterLibFunc(&fzSetErrorCallback, libmupdf, "fz_set_error_callback")

silentCallback = purego.NewCallback(func(user *byte, message *byte) {})

ver := version()
if ver != "" {
Expand Down
15 changes: 15 additions & 0 deletions fitz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,18 @@ func TestEmptyBytes(t *testing.T) {
type emptyReader struct{}

func (emptyReader) Read([]byte) (int, error) { return 0, io.EOF }

// TestSilencedCallbacks verifies that the unconditional silencing of
// MuPDF warning/error callbacks does not break normal Document creation
// and text extraction.
func TestSilencedCallbacks(t *testing.T) {
doc, err := fitz.New(filepath.Join("testdata", "test.pdf"))
if err != nil {
t.Fatal(err)
}
defer doc.Close()

if _, err := doc.Text(0); err != nil {
t.Fatal(err)
}
}
Loading