Recognize and Open

To implement a new document handler, a new fz_document_handler structure is required. There are 3 components to such a structure, all function pointers:


\begin{lstlisting}
typedef struct fz_document_handler_s
{
fz_document_recognize...
...t_open_with_stream_fn *open_with_stream;
} fz_document_handler;
\end{lstlisting}

The first is a function to recognize a document from a magic string, typically a mimetype or a filename:


\begin{lstlisting}
/*
fz_document_recognize_fn: Recognize a document type from
...
...(fz_document_recognize_fn)(fz_context *ctx, const char *magic);
\end{lstlisting}

The second is a function to open a document from a filename:


\begin{lstlisting}
/*
fz_document_open_fn: Function type to open a document fro...
... *(fz_document_open_fn)(fz_context *ctx, const char *filename);
\end{lstlisting}

This function can permissibly be NULL, as it can be synthesized automatically from the third entry, a function to open a document from a stream:


\begin{lstlisting}
/*
fz_document_open_with_stream_fn: Function type to open a
...
...ument_open_with_stream_fn)(fz_context *ctx, fz_stream *stream);
\end{lstlisting}

To create a fz_document use the fz_new_document macro. For a document of type foo, typically a foo_document structure would be defined as below:


\begin{lstlisting}
typedef struct
{
fz_document super;
<foo specific fields>
} foo_document;
\end{lstlisting}

This would then be created using a call to fz_new_document, such as:


\begin{lstlisting}
foo_document *foo = fz_new_document(ctx, foo_document);
\end{lstlisting}

This returns an empty document structure with super populated with default values, and the foo specific fields initialized to 0. The document handler then needs to fill in the document level functions.