/* 
  ==========================================================================
  Collection of general-purpose mupdf utilities
  ---
  NOTE: all these functions may raise exceptions and therefore should be called
        from a fz_try/fz_catch context  (even indirectly)
        
  Oct 2020 - A.Buratti fecit
  ==========================================================================
*/
#ifndef __XPDF_EXTENSION_INCLUDED
#define __XPDF_EXTENSION_INCLUDED

#include <mupdf/pdf.h>

/* 
** ==========================================================================
**  simple geometry utilities
** ==========================================================================
*/

fz_point  xfz_rect_center(fz_rect rect);

/* 
** ==========================================================================
**  xpdf image utilities
** ==========================================================================
*/

int xfz_image_type(fz_context* ctx, fz_image* image);
void xfz_image_savejpeg(fz_context *ctx, fz_image* image, const char *filename);
void xfz_image_savepng(fz_context *ctx, fz_image *image, int useAlpha, const char *filename);

int xpdf_number_of_images(fz_context* ctx, pdf_page* page);

/* 
** ==========================================================================
**  xpdf xobject utilities
** ==========================================================================
*/
pdf_obj* const xpdf_xobject_new(fz_context *ctx, pdf_document *doc, pdf_obj *xobj1, fz_matrix mtx, fz_rect bbox);
void xpdf_xobject_embed(fz_context *ctx, pdf_document *doc, pdf_page *toPage, 
			pdf_obj *xobj1, fz_matrix mtx, fz_rect bbox, int overlay  );

/* 
** ==========================================================================
**  xpdf annot utilities
** ==========================================================================
*/
void xpdf_annot_flatten(fz_context* ctx, pdf_annot* annot);

/* 
** ==========================================================================
**  xpdf field/fieldObj utilities
** ==========================================================================
*/

const char* xpdf_fieldObj_typename(fz_context* ctx, pdf_obj* field);
pdf_obj* xpdf_fieldObj_get(fz_context* ctx, pdf_document* doc, const char* fullname);
void xpdf_fieldObj_delete(fz_context* ctx, pdf_document* doc, pdf_obj* fieldObj);

pdf_annot* xpdf_addsigfield(fz_context* ctx, pdf_page* page, const char* fieldname, fz_rect rect);

/*
** ==========================================================================
**  xpdf  widget utilities 
** ==========================================================================
*/
           
pdf_annot* xpdf_widgetObj_to_widget(fz_context* ctx, pdf_page* page, pdf_obj* widgetObj);

/*
** ==========================================================================
**  xpdf  page  utilities 
** ==========================================================================
*/

fz_rect xpdf_page_cropbox(fz_context* ctx, pdf_page* page);
pdf_obj* const xpdf_page_new_deepcopy(fz_context *ctx, pdf_document *toPdf, pdf_page *fromPage, pdf_graft_map *gmap);

//-----------------------------------------------------------------------------
// Insert a buffer as a new separate /Contents object of a page.
//  Based on showPDFpage() from PyMuPDF .
//
// Copyright 2020-2022, Harald Lieder, mailto:harald.lieder@outlook.com
// License: GNU AFFERO GPL 3.0, https://www.gnu.org/licenses/agpl-3.0.html
//
// Part of "PyMuPDF", a Python binding for "MuPDF" (http://mupdf.com), a
// lightweight PDF, XPS, and E-book viewer, renderer and toolkit which is
// maintained and developed by Artifex Software, Inc. https://artifex.com.
//-----------------------------------------------------------------------------
int JM_insert_contents(fz_context *ctx, pdf_document *pdf,
                        pdf_obj *pageref, fz_buffer *newcont, int overlay);
            			
/*
** ==========================================================================
**  Color conversion between tk-colors and pdf-rgb-colors 
** ==========================================================================
*/
  //  "red" ->  {1.0, 0.0, 0.0}
  //  return 0 (OK) if tkcolor is valid,
  //         1 (ERR) if tkcolor is not a valid color 
int TkColorToPDFColor( const char* tkcolor, float rgb[3] );

  // n is the dimensione of the color array:
  //   1 gray
  //   3 rgb
  //   4 cmyk
  //  result "#RRGGBB" is placed in the tkcolor string (must be allocated by the caller)
void PDFColorToTkColor( int n, float* color, char tkcolor[8]);


#endif // __XPDF_EXTENSION_INCLUDED