Added more drawing structure.

This commit is contained in:
John McNamara 2015-12-24 07:55:20 +00:00
parent f1ef755a5e
commit e6014ec27f
4 changed files with 533 additions and 3 deletions

2
.indent.pro vendored
View File

@ -56,6 +56,8 @@
-T lxw_defined_name
-T lxw_doc_properties
-T lxw_drawing
-T lxw_drawing_coords
-T lxw_drawing_object
-T lxw_fill
-T lxw_font
-T lxw_format

View File

@ -21,6 +21,7 @@
#define LXW_SHEETNAME_MAX 32
#define LXW_SHEETNAME_LEN 65
#define LXW_UINT32_T_LEN 11 /* Length of 4294967296. */
enum lxw_boolean {
LXW_FALSE,

View File

@ -13,13 +13,66 @@
#include "common.h"
STAILQ_HEAD(lxw_drawing_objects, lxw_drawing_object);
enum lxw_drawing_types {
LXW_DRAWING_NONE = 0,
LXW_DRAWING_IMAGE,
LXW_DRAWING_CHART,
LXW_DRAWING_SHAPE
};
enum lxw_anchor_types {
LXW_ANCHOR_TYPE_NONE = 0,
LXW_ANCHOR_TYPE_IMAGE,
LXW_ANCHOR_TYPE_CHART
};
enum lxw_anchor_edit_types {
LXW_ANCHOR_EDIT_AS_NONE = 0,
LXW_ANCHOR_EDIT_AS_RELATIVE,
LXW_ANCHOR_EDIT_AS_ONE_CELL,
LXW_ANCHOR_EDIT_AS_ABSOLUTE
};
/* Coordinates used in a drawing object. */
typedef struct lxw_drawing_coords {
uint32_t col;
uint32_t row;
uint32_t col_offset;
uint32_t row_offset;
} lxw_drawing_coords;
/* Object to represent the properties of a drawing. */
typedef struct lxw_drawing_object {
uint8_t anchor_type;
uint8_t edit_as;
struct lxw_drawing_coords from;
struct lxw_drawing_coords to;
uint32_t col_absolute;
uint32_t row_absolute;
uint32_t width;
uint32_t height;
uint8_t shape;
char *description;
char *url;
char *tip;
STAILQ_ENTRY (lxw_drawing_object) list_pointers;
} lxw_drawing_object;
/*
* Struct to represent a drawing object.
* Struct to represent a collection of drawings.
*/
typedef struct lxw_drawing {
FILE *file;
uint8_t embedded;
struct lxw_drawing_objects *drawing_objects;
} lxw_drawing;
@ -32,12 +85,12 @@ extern "C" {
lxw_drawing *_new_drawing();
void _free_drawing(lxw_drawing *drawing);
void _drawing_assemble_xml_file(lxw_drawing *self);
void _free_drawing_object(struct lxw_drawing_object *drawing_object);
/* Declarations required for unit testing. */
#ifdef TESTING
STATIC void _drawing_xml_declaration(lxw_drawing *self);
#endif /* TESTING */
/* *INDENT-OFF* */

View File

@ -11,6 +11,10 @@
#include "xlsxwriter/drawing.h"
#include "xlsxwriter/utility.h"
#define DRAWING_SCHEMA "http://schemas.openxmlformats.org/drawingml/2006/"
#define OFFICEDOC_SCHEMA "http://schemas.openxmlformats.org/officeDocument/2006/"
#define PIC_NAME_LENGTH 14 /* "Picture 65536" */
/*
* Forward declarations.
*/
@ -22,7 +26,7 @@
****************************************************************************/
/*
* Create a new drawing object.
* Create a new drawing collection.
*/
lxw_drawing *
_new_drawing()
@ -30,6 +34,11 @@ _new_drawing()
lxw_drawing *drawing = calloc(1, sizeof(lxw_drawing));
GOTO_LABEL_ON_MEM_ERROR(drawing, mem_error);
drawing->drawing_objects = calloc(1, sizeof(struct lxw_drawing_objects));
GOTO_LABEL_ON_MEM_ERROR(drawing->drawing_objects, mem_error);
STAILQ_INIT(drawing->drawing_objects);
return drawing;
mem_error:
@ -41,11 +50,39 @@ mem_error:
* Free a drawing object.
*/
void
_free_drawing_object(lxw_drawing_object *drawing_object)
{
if (!drawing_object)
return;
free(drawing_object->description);
free(drawing_object->url);
free(drawing_object->tip);
free(drawing_object);
}
/*
* Free a drawing collection.
*/
void
_free_drawing(lxw_drawing *drawing)
{
lxw_drawing_object *drawing_object;
if (!drawing)
return;
if (drawing->drawing_objects) {
while (!STAILQ_EMPTY(drawing->drawing_objects)) {
drawing_object = STAILQ_FIRST(drawing->drawing_objects);
STAILQ_REMOVE_HEAD(drawing->drawing_objects, list_pointers);
_free_drawing_object(drawing_object);
}
free(drawing->drawing_objects);
}
free(drawing);
}
@ -64,6 +101,427 @@ _drawing_xml_declaration(lxw_drawing *self)
_xml_declaration(self->file);
}
/*
* Write the <xdr:wsDr> element.
*/
STATIC void
_write_drawing_workspace(lxw_drawing *self)
{
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
char xmlns_xdr[] = DRAWING_SCHEMA "spreadsheetDrawing";
char xmlns_a[] = DRAWING_SCHEMA "main";
_INIT_ATTRIBUTES();
_PUSH_ATTRIBUTES_STR("xmlns:xdr", xmlns_xdr);
_PUSH_ATTRIBUTES_STR("xmlns:a", xmlns_a);
_xml_start_tag(self->file, "xdr:wsDr", &attributes);
_FREE_ATTRIBUTES();
}
/*
* Write the <xdr:col> element.
*/
STATIC void
_drawing_write_col(lxw_drawing *self, char *data)
{
_xml_data_element(self->file, "xdr:col", data, NULL);
}
/*
* Write the <xdr:colOff> element.
*/
STATIC void
_drawing_write_col_off(lxw_drawing *self, char *data)
{
_xml_data_element(self->file, "xdr:colOff", data, NULL);
}
/*
* Write the <xdr:row> element.
*/
STATIC void
_drawing_write_row(lxw_drawing *self, char *data)
{
_xml_data_element(self->file, "xdr:row", data, NULL);
}
/*
* Write the <xdr:rowOff> element.
*/
STATIC void
_drawing_write_row_off(lxw_drawing *self, char *data)
{
_xml_data_element(self->file, "xdr:rowOff", data, NULL);
}
/*
* Write the <xdr:from> element.
*/
STATIC void
_drawing_write_from(lxw_drawing *self, lxw_drawing_coords *coords)
{
char data[LXW_UINT32_T_LEN];
_xml_start_tag(self->file, "xdr:from", NULL);
lxw_snprintf(data, LXW_UINT32_T_LEN, "%u", coords->col);
_drawing_write_col(self, data);
lxw_snprintf(data, LXW_UINT32_T_LEN, "%u", coords->col_offset);
_drawing_write_col_off(self, data);
lxw_snprintf(data, LXW_UINT32_T_LEN, "%u", coords->row);
_drawing_write_row(self, data);
lxw_snprintf(data, LXW_UINT32_T_LEN, "%u", coords->row_offset);
_drawing_write_row_off(self, data);
_xml_end_tag(self->file, "xdr:from");
}
/*
* Write the <xdr:to> element.
*/
STATIC void
_drawing_write_to(lxw_drawing *self, lxw_drawing_coords *coords)
{
char data[LXW_UINT32_T_LEN];
_xml_start_tag(self->file, "xdr:to", NULL);
lxw_snprintf(data, LXW_UINT32_T_LEN, "%u", coords->col);
_drawing_write_col(self, data);
lxw_snprintf(data, LXW_UINT32_T_LEN, "%u", coords->col_offset);
_drawing_write_col_off(self, data);
lxw_snprintf(data, LXW_UINT32_T_LEN, "%u", coords->row);
_drawing_write_row(self, data);
lxw_snprintf(data, LXW_UINT32_T_LEN, "%u", coords->row_offset);
_drawing_write_row_off(self, data);
_xml_end_tag(self->file, "xdr:to");
}
/*
* Write the <xdr:cNvPr> element.
*/
STATIC void
_drawing_write_c_nv_pr(lxw_drawing *self, uint16_t index,
lxw_drawing_object *drawing_object)
{
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
char name[PIC_NAME_LENGTH];
lxw_snprintf(name, PIC_NAME_LENGTH, "Picture %d", index);
_INIT_ATTRIBUTES();
_PUSH_ATTRIBUTES_INT("id", index + 1);
_PUSH_ATTRIBUTES_STR("name", name);
_PUSH_ATTRIBUTES_STR("descr", drawing_object->description);
_xml_empty_tag(self->file, "xdr:cNvPr", &attributes);
_FREE_ATTRIBUTES();
}
/*
* Write the <a:picLocks> element.
*/
STATIC void
_drawing_write_a_pic_locks(lxw_drawing *self)
{
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
_INIT_ATTRIBUTES();
_PUSH_ATTRIBUTES_STR("noChangeAspect", "1");
_xml_empty_tag(self->file, "a:picLocks", &attributes);
_FREE_ATTRIBUTES();
}
/*
* Write the <xdr:cNvPicPr> element.
*/
STATIC void
_drawing_write_c_nv_pic_pr(lxw_drawing *self)
{
_xml_start_tag(self->file, "xdr:cNvPicPr", NULL);
/* Write the a:picLocks element. */
_drawing_write_a_pic_locks(self);
_xml_end_tag(self->file, "xdr:cNvPicPr");
}
/*
* Write the <xdr:nvPicPr> element.
*/
STATIC void
_drawing_write_nv_pic_pr(lxw_drawing *self, uint16_t index,
lxw_drawing_object *drawing_object)
{
_xml_start_tag(self->file, "xdr:nvPicPr", NULL);
/* Write the xdr:cNvPr element. */
_drawing_write_c_nv_pr(self, index, drawing_object);
/* Write the xdr:cNvPicPr element. */
_drawing_write_c_nv_pic_pr(self);
_xml_end_tag(self->file, "xdr:nvPicPr");
}
/*
* Write the <a:blip> element.
*/
STATIC void
_drawing_write_a_blip(lxw_drawing *self, uint16_t index)
{
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
char xmlns_r[] = OFFICEDOC_SCHEMA "relationships";
char r_id[MAX_ATTRIBUTE_LENGTH];
lxw_snprintf(r_id, ATTR_32, "rId%d", index);
_INIT_ATTRIBUTES();
_PUSH_ATTRIBUTES_STR("xmlns:r", xmlns_r);
_PUSH_ATTRIBUTES_STR("r:embed", r_id);
_xml_empty_tag(self->file, "a:blip", &attributes);
_FREE_ATTRIBUTES();
}
/*
* Write the <a:fillRect> element.
*/
STATIC void
_drawing_write_a_fill_rect(lxw_drawing *self)
{
_xml_empty_tag(self->file, "a:fillRect", NULL);
}
/*
* Write the <a:stretch> element.
*/
STATIC void
_drawing_write_a_stretch(lxw_drawing *self)
{
_xml_start_tag(self->file, "a:stretch", NULL);
/* Write the a:fillRect element. */
_drawing_write_a_fill_rect(self);
_xml_end_tag(self->file, "a:stretch");
}
/*
* Write the <xdr:blipFill> element.
*/
STATIC void
_drawing_write_blip_fill(lxw_drawing *self, uint16_t index)
{
_xml_start_tag(self->file, "xdr:blipFill", NULL);
/* Write the a:blip element. */
_drawing_write_a_blip(self, index);
/* Write the a:stretch element. */
_drawing_write_a_stretch(self);
_xml_end_tag(self->file, "xdr:blipFill");
}
/*
* Write the <a:ext> element.
*/
STATIC void
_drawing_write_a_ext(lxw_drawing *self, lxw_drawing_object *drawing_object)
{
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
_INIT_ATTRIBUTES();
_PUSH_ATTRIBUTES_INT("cx", drawing_object->width);
_PUSH_ATTRIBUTES_INT("cy", drawing_object->height);
_xml_empty_tag(self->file, "a:ext", &attributes);
_FREE_ATTRIBUTES();
}
/*
* Write the <a:off> element.
*/
STATIC void
_drawing_write_a_off(lxw_drawing *self, lxw_drawing_object *drawing_object)
{
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
_INIT_ATTRIBUTES();
_PUSH_ATTRIBUTES_INT("x", drawing_object->col_absolute);
_PUSH_ATTRIBUTES_INT("y", drawing_object->row_absolute);
_xml_empty_tag(self->file, "a:off", &attributes);
_FREE_ATTRIBUTES();
}
/*
* Write the <a:xfrm> element.
*/
STATIC void
_drawing_write_a_xfrm(lxw_drawing *self, lxw_drawing_object *drawing_object)
{
_xml_start_tag(self->file, "a:xfrm", NULL);
/* Write the a:off element. */
_drawing_write_a_off(self, drawing_object);
/* Write the a:ext element. */
_drawing_write_a_ext(self, drawing_object);
_xml_end_tag(self->file, "a:xfrm");
}
/*
* Write the <a:avLst> element.
*/
STATIC void
_drawing_write_a_av_lst(lxw_drawing *self)
{
_xml_empty_tag(self->file, "a:avLst", NULL);
}
/*
* Write the <a:prstGeom> element.
*/
STATIC void
_drawing_write_a_prst_geom(lxw_drawing *self)
{
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
_INIT_ATTRIBUTES();
_PUSH_ATTRIBUTES_STR("prst", "rect");
_xml_start_tag(self->file, "a:prstGeom", &attributes);
/* Write the a:avLst element. */
_drawing_write_a_av_lst(self);
_xml_end_tag(self->file, "a:prstGeom");
_FREE_ATTRIBUTES();
}
/*
* Write the <xdr:spPr> element.
*/
STATIC void
_drawing_write_sp_pr(lxw_drawing *self, lxw_drawing_object *drawing_object)
{
_xml_start_tag(self->file, "xdr:spPr", NULL);
/* Write the a:xfrm element. */
_drawing_write_a_xfrm(self, drawing_object);
/* Write the a:prstGeom element. */
_drawing_write_a_prst_geom(self);
_xml_end_tag(self->file, "xdr:spPr");
}
/*
* Write the <xdr:pic> element.
*/
STATIC void
_drawing_write_pic(lxw_drawing *self, uint16_t index,
lxw_drawing_object *drawing_object)
{
_xml_start_tag(self->file, "xdr:pic", NULL);
/* Write the xdr:nvPicPr element. */
_drawing_write_nv_pic_pr(self, index, drawing_object);
/* Write the xdr:blipFill element. */
_drawing_write_blip_fill(self, index);
/* Write the xdr:spPr element. */
_drawing_write_sp_pr(self, drawing_object);
_xml_end_tag(self->file, "xdr:pic");
}
/*
* Write the <xdr:clientData> element.
*/
STATIC void
_drawing_write_client_data(lxw_drawing *self)
{
_xml_empty_tag(self->file, "xdr:clientData", NULL);
}
/*
* Write the <xdr:twoCellAnchor> element.
*/
STATIC void
_drawing_write_two_cell_anchor(lxw_drawing *self, uint16_t index,
lxw_drawing_object *drawing_object)
{
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
_INIT_ATTRIBUTES();
if (drawing_object->anchor_type == LXW_ANCHOR_TYPE_IMAGE) {
if (drawing_object->edit_as == LXW_ANCHOR_EDIT_AS_ABSOLUTE)
_PUSH_ATTRIBUTES_STR("editAs", "absolute");
else if (drawing_object->edit_as != LXW_ANCHOR_EDIT_AS_RELATIVE)
_PUSH_ATTRIBUTES_STR("editAs", "oneCell");
}
_xml_start_tag(self->file, "xdr:twoCellAnchor", &attributes);
_drawing_write_from(self, &drawing_object->from);
_drawing_write_to(self, &drawing_object->to);
if (drawing_object->anchor_type == LXW_ANCHOR_TYPE_CHART) {
/* Graphic frame. */
/* Write the xdr:graphicFrame element for charts. */
/*_drawing_write_graphic_frame(self, index, description); */
}
else if (drawing_object->anchor_type == LXW_ANCHOR_TYPE_IMAGE) {
/* Write the xdr:pic element. */
_drawing_write_pic(self, index, drawing_object);
}
else {
/* Write the xdr:sp element for shapes. */
/* _drawing_write_sp(self, index, col_absolute, row_absolute, width,
height, shape); */
}
/* Write the xdr:clientData element. */
_drawing_write_client_data(self);
_xml_end_tag(self->file, "xdr:twoCellAnchor");
_FREE_ATTRIBUTES();
}
/*****************************************************************************
*
* XML file assembly functions.
@ -76,9 +534,25 @@ _drawing_xml_declaration(lxw_drawing *self)
void
_drawing_assemble_xml_file(lxw_drawing *self)
{
uint16_t index;
lxw_drawing_object *drawing_object;
/* Write the XML declaration. */
_drawing_xml_declaration(self);
/* Write the xdr:wsDr element. */
_write_drawing_workspace(self);
if (self->embedded) {
index = 1;
STAILQ_FOREACH(drawing_object, self->drawing_objects, list_pointers) {
_drawing_write_two_cell_anchor(self, index, drawing_object);
index++;
}
}
_xml_end_tag(self->file, "xdr:wsDr");
}