init commit
Some checks failed
Test / test (1.22.x, macos-latest) (push) Has been cancelled
Test / test (1.22.x, ubuntu-latest) (push) Has been cancelled

This commit is contained in:
landaiqing
2026-02-10 14:45:18 +08:00
parent a530a79566
commit 5ce88674da
142 changed files with 12394 additions and 4280 deletions

View File

@@ -1,3 +1,25 @@
// Copyright (C) 2004-2022 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.
#ifndef MUPDF_FITZ_OUTPUT_H
#define MUPDF_FITZ_OUTPUT_H
@@ -7,14 +29,13 @@
#include "mupdf/fitz/string-util.h"
#include "mupdf/fitz/stream.h"
/*
Generic output streams - generalise between outputting to a file,
a buffer, etc.
/**
Generic output streams - generalise between outputting to a
file, a buffer, etc.
*/
typedef struct fz_output_s fz_output;
/*
fz_output_write_fn: A function type for use when implementing
/**
A function type for use when implementing
fz_outputs. The supplied function of this type is called
whenever data is written to the output.
@@ -26,19 +47,19 @@ typedef struct fz_output_s fz_output;
*/
typedef void (fz_output_write_fn)(fz_context *ctx, void *state, const void *data, size_t n);
/*
fz_output_seek_fn: A function type for use when implementing
/**
A function type for use when implementing
fz_outputs. The supplied function of this type is called when
fz_seek_output is requested.
state: The output stream state to seek within.
offset, whence: as defined for fs_seek_output.
offset, whence: as defined for fz_seek().
*/
typedef void (fz_output_seek_fn)(fz_context *ctx, void *state, int64_t offset, int whence);
/*
fz_output_tell_fn: A function type for use when implementing
/**
A function type for use when implementing
fz_outputs. The supplied function of this type is called when
fz_tell_output is requested.
@@ -48,30 +69,45 @@ typedef void (fz_output_seek_fn)(fz_context *ctx, void *state, int64_t offset, i
*/
typedef int64_t (fz_output_tell_fn)(fz_context *ctx, void *state);
/*
fz_output_close_fn: A function type for use when implementing
/**
A function type for use when implementing
fz_outputs. The supplied function of this type is called
when the output stream is closed, to flush any pending writes.
*/
typedef void (fz_output_close_fn)(fz_context *ctx, void *state);
/*
fz_output_drop_fn: A function type for use when implementing
/**
A function type for use when implementing
fz_outputs. The supplied function of this type is called
when the output stream is dropped, to release the stream specific
state information.
when the output stream is reset, and resets the state
to that when it was first initialised.
*/
typedef void (fz_output_reset_fn)(fz_context *ctx, void *state);
/**
A function type for use when implementing
fz_outputs. The supplied function of this type is called
when the output stream is dropped, to release the stream
specific state information.
*/
typedef void (fz_output_drop_fn)(fz_context *ctx, void *state);
/*
fz_stream_from_output_fn: A function type for use when implementing
/**
A function type for use when implementing
fz_outputs. The supplied function of this type is called
when the fz_stream_from_output is called.
*/
typedef fz_stream *(fz_stream_from_output_fn)(fz_context *ctx, void *state);
/**
A function type for use when implementing
fz_outputs. The supplied function of this type is called
when fz_truncate_output is called to truncate the file
at that point.
*/
typedef void (fz_truncate_fn)(fz_context *ctx, void *state);
struct fz_output_s
struct fz_output
{
void *state;
fz_output_write_fn *write;
@@ -79,12 +115,19 @@ struct fz_output_s
fz_output_tell_fn *tell;
fz_output_close_fn *close;
fz_output_drop_fn *drop;
fz_output_reset_fn *reset;
fz_stream_from_output_fn *as_stream;
fz_truncate_fn *truncate;
int closed;
char *bp, *wp, *ep;
/* If buffered is non-zero, then we have that many
* bits (1-7) waiting to be written in bits. */
int buffered;
int bits;
};
/*
fz_new_output: Create a new output object with the given
/**
Create a new output object with the given
internal state and function pointers.
state: Internal state (opaque to everything but implementation).
@@ -96,8 +139,8 @@ struct fz_output_s
*/
fz_output *fz_new_output(fz_context *ctx, int bufsiz, void *state, fz_output_write_fn *write, fz_output_close_fn *close, fz_output_drop_fn *drop);
/*
fz_new_output_with_path: Open an output stream that writes to a
/**
Open an output stream that writes to a
given path.
filename: The filename to write to (specified in UTF-8).
@@ -107,207 +150,262 @@ fz_output *fz_new_output(fz_context *ctx, int bufsiz, void *state, fz_output_wri
*/
fz_output *fz_new_output_with_path(fz_context *, const char *filename, int append);
/*
fz_new_output_with_buffer: Open an output stream that appends
/**
Open an output stream that writes to a
given FILE *.
file: The file pointers to write to. NULL is interpreted as effectively
meaning /dev/null or similar.
*/
fz_output *fz_new_output_with_file_ptr(fz_context *ctx, FILE *file);
/**
Open an output stream that appends
to a buffer.
buf: The buffer to append to.
*/
fz_output *fz_new_output_with_buffer(fz_context *ctx, fz_buffer *buf);
/*
fz_stdout: The standard out output stream. By default
this stream writes to stdout. This may be overridden
using fz_set_stdout.
/**
Retrieve an fz_output that directs to stdout.
Optionally may be fz_dropped when finished with.
*/
fz_output *fz_stdout(fz_context *ctx);
/*
fz_stderr: The standard error output stream. By default
this stream writes to stderr. This may be overridden
using fz_set_stderr.
/**
Retrieve an fz_output that directs to stdout.
Optionally may be fz_dropped when finished with.
*/
fz_output *fz_stderr(fz_context *ctx);
/*
fz_set_stdout: Replace default standard output stream
with a given stream.
#ifdef _WIN32
/**
Retrieve an fz_output that directs to OutputDebugString.
out: The new stream to use.
Optionally may be fz_dropped when finished with.
*/
void fz_set_stdout(fz_context *ctx, fz_output *out);
fz_output *fz_stdods(fz_context *ctx);
#endif
/*
fz_set_stderr: Replace default standard error stream
with a given stream.
err: The new stream to use.
/**
Set the output stream to be used for fz_stddbg. Set to NULL to
reset to default (stderr).
*/
void fz_set_stderr(fz_context *ctx, fz_output *err);
void fz_set_stddbg(fz_context *ctx, fz_output *out);
/*
fz_write_printf: Format and write data to an output stream.
See fz_vsnprintf for formatting details.
/**
Retrieve an fz_output for the default debugging stream. On
Windows this will be OutputDebugString for non-console apps.
Otherwise, it is always fz_stderr.
Optionally may be fz_dropped when finished with.
*/
fz_output *fz_stddbg(fz_context *ctx);
/**
Format and write data to an output stream.
See fz_format_string for formatting details.
*/
void fz_write_printf(fz_context *ctx, fz_output *out, const char *fmt, ...);
/*
fz_write_vprintf: va_list version of fz_write_printf.
/**
va_list version of fz_write_printf.
*/
void fz_write_vprintf(fz_context *ctx, fz_output *out, const char *fmt, va_list ap);
/*
fz_seek_output: Seek to the specified file position.
/**
Seek to the specified file position.
See fseek for arguments.
Throw an error on unseekable outputs.
*/
void fz_seek_output(fz_context *ctx, fz_output *out, int64_t off, int whence);
/*
fz_tell_output: Return the current file position.
/**
Return the current file position.
Throw an error on untellable outputs.
*/
int64_t fz_tell_output(fz_context *ctx, fz_output *out);
/*
fz_flush_output: Flush unwritten data.
/**
Flush unwritten data.
*/
void fz_flush_output(fz_context *ctx, fz_output *out);
/*
fz_close_output: Flush pending output and close an output stream.
/**
Flush pending output and close an output stream.
*/
void fz_close_output(fz_context *, fz_output *);
/*
fz_drop_output: Free an output stream. Don't forget to close it first!
/**
Reset a closed output stream. Returns state to
(broadly) that which it was in when opened. Not
all outputs can be reset, so this may throw an
exception.
*/
void fz_reset_output(fz_context *, fz_output *);
/**
Free an output stream. Don't forget to close it first!
*/
void fz_drop_output(fz_context *, fz_output *);
/*
fz_stream_from_output: obtain the fz_output in the form of a fz_stream
/**
Query whether a given fz_output supports fz_stream_from_output.
*/
int fz_output_supports_stream(fz_context *ctx, fz_output *out);
This allows data to be read back from some forms of fz_output object.
When finished reading, the fz_stream should be released by calling
fz_drop_stream. Until the fz_stream is dropped, no further operations
should be performed on the fz_output object.
/**
Obtain the fz_output in the form of a fz_stream.
This allows data to be read back from some forms of fz_output
object. When finished reading, the fz_stream should be released
by calling fz_drop_stream. Until the fz_stream is dropped, no
further operations should be performed on the fz_output object.
*/
fz_stream *fz_stream_from_output(fz_context *, fz_output *);
/*
fz_write_data: Write data to output.
/**
Truncate the output at the current position.
This allows output streams which have seeked back from the end
of their storage to be truncated at the current point.
*/
void fz_truncate_output(fz_context *, fz_output *);
/**
Write data to output.
data: Pointer to data to write.
size: Size of data to write in bytes.
*/
void fz_write_data(fz_context *ctx, fz_output *out, const void *data, size_t size);
void fz_write_buffer(fz_context *ctx, fz_output *out, fz_buffer *data);
/*
fz_write_string: Write a string. Does not write zero terminator.
/**
Write a string. Does not write zero terminator.
*/
void fz_write_string(fz_context *ctx, fz_output *out, const char *s);
/*
fz_write_int32_be: Write a big-endian 32-bit binary integer.
/**
Write different sized data to an output stream.
*/
void fz_write_int32_be(fz_context *ctx, fz_output *out, int x);
/*
fz_write_int32_le: Write a little-endian 32-bit binary integer.
*/
void fz_write_int32_le(fz_context *ctx, fz_output *out, int x);
/*
fz_write_int16_be: Write a big-endian 16-bit binary integer.
*/
void fz_write_uint32_be(fz_context *ctx, fz_output *out, unsigned int x);
void fz_write_uint32_le(fz_context *ctx, fz_output *out, unsigned int x);
void fz_write_int16_be(fz_context *ctx, fz_output *out, int x);
/*
fz_write_int16_le: Write a little-endian 16-bit binary integer.
*/
void fz_write_int16_le(fz_context *ctx, fz_output *out, int x);
/*
fz_write_byte: Write a single byte.
*/
void fz_write_uint16_be(fz_context *ctx, fz_output *out, unsigned int x);
void fz_write_uint16_le(fz_context *ctx, fz_output *out, unsigned int x);
void fz_write_char(fz_context *ctx, fz_output *out, char x);
void fz_write_byte(fz_context *ctx, fz_output *out, unsigned char x);
void fz_write_float_be(fz_context *ctx, fz_output *out, float f);
void fz_write_float_le(fz_context *ctx, fz_output *out, float f);
/*
fz_write_rune: Write a UTF-8 encoded unicode character.
/**
Write a UTF-8 encoded unicode character.
*/
void fz_write_rune(fz_context *ctx, fz_output *out, int rune);
/*
fz_write_base64: Write base64 encoded data.
/**
Write a base64 encoded data block, optionally with periodic
newlines.
*/
void fz_write_base64(fz_context *ctx, fz_output *out, const unsigned char *data, size_t size, int newline);
/**
Write a base64 encoded fz_buffer, optionally with periodic
newlines.
*/
void fz_write_base64(fz_context *ctx, fz_output *out, const unsigned char *data, int size, int newline);
void fz_write_base64_buffer(fz_context *ctx, fz_output *out, fz_buffer *data, int newline);
/*
fz_format_string: Our customised 'printf'-like string formatter.
Takes %c, %d, %s, %u, %x, as usual.
Modifiers are not supported except for zero-padding ints (e.g. %02d, %03u, %04x, etc).
%g output in "as short as possible hopefully lossless non-exponent" form,
see fz_ftoa for specifics.
/**
Write num_bits of data to the end of the output stream, assumed to be packed
most significant bits first.
*/
void fz_write_bits(fz_context *ctx, fz_output *out, unsigned int data, int num_bits);
/**
Sync to byte boundary after writing bits.
*/
void fz_write_bits_sync(fz_context *ctx, fz_output *out);
/**
Copy the stream contents to the output.
*/
void fz_write_stream(fz_context *ctx, fz_output *out, fz_stream *in);
/**
Our customised 'printf'-like string formatter.
Takes %c, %d, %s, %u, %x, %X as usual.
Modifiers are not supported except for zero-padding ints (e.g.
%02d, %03u, %04x, etc).
%g output in "as short as possible hopefully lossless
non-exponent" form, see fz_ftoa for specifics.
%f and %e output as usual.
%C outputs a utf8 encoded int.
%M outputs a fz_matrix*. %R outputs a fz_rect*. %P outputs a fz_point*.
%M outputs a fz_matrix*.
%R outputs a fz_rect*.
%P outputs a fz_point*.
%n outputs a PDF name (with appropriate escaping).
%q and %( output escaped strings in C/PDF syntax.
%l{d,u,x} indicates that the values are int64_t.
%z{d,u,x} indicates that the value is a size_t.
%l{d,u,x,X} indicates that the values are int64_t.
%z{d,u,x,X} indicates that the value is a size_t.
user: An opaque pointer that is passed to the emit function.
emit: A function pointer called to emit output bytes as the string is being formatted.
*/
void
fz_format_string(fz_context *ctx, void *user, void (*emit)(fz_context *ctx, void *user, int c), const char *fmt, va_list args);
/*
fz_vsnprintf: A vsnprintf work-alike, using our custom formatter.
emit: A function pointer called to emit output bytes as the
string is being formatted.
*/
void fz_format_string(fz_context *ctx, void *user, void (*emit)(fz_context *ctx, void *user, int c), const char *fmt, va_list args);
/**
A vsnprintf work-alike, using our custom formatter.
*/
size_t fz_vsnprintf(char *buffer, size_t space, const char *fmt, va_list args);
/*
fz_snprintf: The non va_list equivalent of fz_vsnprintf.
/**
The non va_list equivalent of fz_vsnprintf.
*/
size_t fz_snprintf(char *buffer, size_t space, const char *fmt, ...);
/*
fz_asprintf: Print to allocated string.
/**
Allocated sprintf.
Returns a null terminated allocated block containing the
formatted version of the format string/args.
*/
char *fz_asprintf(fz_context *ctx, const char *fmt, ...);
/*
fz_tempfilename: Get a temporary filename based upon 'base'.
'hint' is the path of a file (normally the existing document file)
supplied to give the function an idea of what directory to use. This
may or may not be used depending on the implementation's whim.
The returned path must be freed.
*/
char *fz_tempfilename(fz_context *ctx, const char *base, const char *hint);
/*
fz_save_buffer: Save contents of a buffer to file.
/**
Save the contents of a buffer to a file.
*/
void fz_save_buffer(fz_context *ctx, fz_buffer *buf, const char *filename);
/*
/**
Compression and other filtering outputs.
These outputs write encoded data to another output. Create a filter
output with the destination, write to the filter, then drop it when
you're done. These can also be chained together, for example to write
ASCII Hex encoded, Deflate compressed, and RC4 encrypted data to a
buffer output.
These outputs write encoded data to another output. Create a
filter output with the destination, write to the filter, then
close and drop it when you're done. These can also be chained
together, for example to write ASCII Hex encoded, Deflate
compressed, and RC4 encrypted data to a buffer output.
Output streams don't use reference counting, so make sure to drop all
of the filters in the reverse order of creation so that data is flushed
properly.
Output streams don't use reference counting, so make sure to
close all of the filters in the reverse order of creation so
that data is flushed properly.
Accordingly, ownership of 'chain' is never passed into the
following functions, but remains with the caller, whose
responsibility it is to ensure they exist at least until
the returned fz_output is dropped.
*/
fz_output *fz_new_asciihex_output(fz_context *ctx, fz_output *chain);
fz_output *fz_new_ascii85_output(fz_context *ctx, fz_output *chain);
fz_output *fz_new_rle_output(fz_context *ctx, fz_output *chain);