#ifndef RND_DRAW_WEB2_H
#define RND_DRAW_WEB2_H

/* from librnd 4.5.0 */

#include <genvector/gds_char.h>
#include <liblihata/dom.h>
#include <librnd/hid/hid.h>
#include <librnd/plugins/lib_exp_text/draw_svg.h>

typedef struct rnd_web2_s rnd_web2_t;

typedef enum rnd_web2_hilight_type_e {
	RND_WEB2_RECT,
	RND_WEB2_LINE,
	RND_WEB2_CIRCLE   /* center=x1;y1, radius=x2 */
} rnd_web2_hilight_type_t;

/* Call this from search_obj_hilight() to add new highlight objects for
   search results. obj_name is the real name of the object the user enters
   (e.g. "refdes", "netname"), without the name of the page or any other prefix. */
typedef void (rnd_web2_search_obj_append_t)(rnd_web2_t *web2, const char *obj_name, rnd_web2_hilight_type_t ht, rnd_coord_t x1, rnd_coord_t y1, rnd_coord_t x2, rnd_coord_t y2);

struct rnd_web2_s {
	/* public: config */
	rnd_design_t *hidlib;
	FILE *outf;
	rnd_svg_t svg;
	void *user_data;

	/* If not NULL, call this before the end of each page, to generate a
	   page-specific list of search results */
	void (*search_obj_hilight)(rnd_design_t *page, rnd_web2_t *web2, rnd_web2_search_obj_append_t *add_obj);

	/* public: result */
	long drawn_objs;

	/* private: cache and internal state */
	lht_doc_t *temp;
	int page_idx;
	const char *page_title;
	vtp0_t layer_names;        /* dyn. alloced string pairs of layer ID,name */
	vtp0_t search;             /* pairs of dyn. alloced string and page number */
	vtp0_t page_hidlib;        /* an rnd_hidlib_t * for each page */
	unsigned mapping:1;

	/* Spare: see doc/developer/spare.txt */
	void (*spare_f1)(void), (*spare_f2)(void);
	long spare_l1, spare_l2, spare_l3, spare_l4;
	void *spare_p1, *spare_p2, *spare_p3, *spare_p4;
	double spare_d1, spare_d2, spare_d3, spare_d4;
};

/* Set up context for a file; assumes other fields of pctx config will be
   filled in by the caller _after_ the call; this includes various
   graphical config settings in pctx->svg. Templ_fn is an external
   lihata template file name or NULL for using the internal.
   Returns 0 on success. */
int rnd_web2_init(rnd_web2_t *pctx, rnd_design_t *hidlib, FILE *f, const char *templ_fn);
int rnd_web2_uninit(rnd_web2_t *pctx);

/* Set up output file and print header before export, footer after export */
int rnd_web2_print_header(rnd_web2_t *pctx);
int rnd_web2_print_footer(rnd_web2_t *pctx);
int rnd_web2_print_page_pre(rnd_web2_t *pctx, const char *page_title);
int rnd_web2_print_page_post(rnd_web2_t *pctx);

/* CAM mode: start a new file for write, close previous file, report error
   when opening the new file fails */
int rnd_web2_new_file(rnd_web2_t *pctx, FILE *f, const char *fn);


/* Caller needs to run rnd_app.expose_main() twice: first after a
   rnd_web2_layer_group_map(pctx, 1) then after a 
   rnd_web2_layer_group_map(pctx, 0). The first one creates an internal map. */
void rnd_web2_layer_group_map(rnd_web2_t *pctx, int enable);

void rnd_web2_layer_group_begin(rnd_web2_t *pctx, long group, const char *name, int is_our_mask);

void rnd_web2_background(rnd_web2_t *pctx);

/* Append a search box on the current page to the list of searchables. Call
   this right before rnd_web2_print_page_post() of each page */
void rnd_web2_search_append(rnd_web2_t *pctx, const char *name, rnd_coord_t x1, rnd_coord_t y1, rnd_coord_t x2, rnd_coord_t y2);

/* standard HID API */
rnd_hid_gc_t rnd_web2_make_gc(rnd_hid_t *hid);
void rnd_web2_destroy_gc(rnd_hid_gc_t gc);
void rnd_web2_set_line_cap(rnd_hid_gc_t gc, rnd_cap_style_t style);
void rnd_web2_set_line_width(rnd_hid_gc_t gc, rnd_coord_t width);
void rnd_web2_set_draw_xor(rnd_hid_gc_t gc, int xor_);
void rnd_web2_set_crosshair(rnd_hid_t *hid, rnd_coord_t x, rnd_coord_t y, rnd_set_crosshair_t action);


/* standard HID API with extra pctx first arg */
void rnd_web2_set_drawing_mode(rnd_web2_t *pctx, rnd_hid_t *hid, rnd_composite_op_t op, rnd_bool direct, const rnd_box_t *screen);
void rnd_web2_set_color(rnd_web2_t *pctx, rnd_hid_gc_t gc, const rnd_color_t *color);
void rnd_web2_draw_rect(rnd_web2_t *pctx, rnd_hid_gc_t gc, rnd_coord_t x1, rnd_coord_t y1, rnd_coord_t x2, rnd_coord_t y2);
void rnd_web2_draw_line(rnd_web2_t *pctx, rnd_hid_gc_t gc, rnd_coord_t x1, rnd_coord_t y1, rnd_coord_t x2, rnd_coord_t y2);
void rnd_web2_draw_arc(rnd_web2_t *pctx, rnd_hid_gc_t gc, rnd_coord_t cx, rnd_coord_t cy, rnd_coord_t width, rnd_coord_t height, rnd_angle_t start_angle, rnd_angle_t delta_angle);
void rnd_web2_fill_circle(rnd_web2_t *pctx, rnd_hid_gc_t gc, rnd_coord_t cx, rnd_coord_t cy, rnd_coord_t radius);
void rnd_web2_fill_polygon_offs(rnd_web2_t *pctx, rnd_hid_gc_t gc, int n_coords, rnd_coord_t *x, rnd_coord_t *y, rnd_coord_t dx, rnd_coord_t dy);
void rnd_web2_fill_rect(rnd_web2_t *pctx, rnd_hid_gc_t gc, rnd_coord_t x1, rnd_coord_t y1, rnd_coord_t x2, rnd_coord_t y2);

#endif
