libxlsxwriter/src/drawing.c in fast_excel-0.2.6 vs libxlsxwriter/src/drawing.c in fast_excel-0.3.0

- old
+ new

@@ -1,11 +1,11 @@ /***************************************************************************** * drawing - A library for creating Excel XLSX drawing files. * * Used in conjunction with the libxlsxwriter library. * - * Copyright 2014-2018, John McNamara, jmcnamara@cpan.org. See LICENSE.txt. + * Copyright 2014-2019, John McNamara, jmcnamara@cpan.org. See LICENSE.txt. * */ #include "xlsxwriter/xmlwriter.h" #include "xlsxwriter/common.h" @@ -25,11 +25,11 @@ /* * Create a new drawing collection. */ lxw_drawing * -lxw_drawing_new() +lxw_drawing_new(void) { 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)); @@ -223,11 +223,11 @@ /* * Write the <xdr:cNvPr> element. */ STATIC void -_drawing_write_c_nv_pr(lxw_drawing *self, char *object_name, uint16_t index, +_drawing_write_c_nv_pr(lxw_drawing *self, char *object_name, uint32_t index, lxw_drawing_object *drawing_object) { struct xml_attribute_list attributes; struct xml_attribute *attribute; @@ -237,11 +237,11 @@ LXW_INIT_ATTRIBUTES(); LXW_PUSH_ATTRIBUTES_INT("id", index + 1); LXW_PUSH_ATTRIBUTES_STR("name", name); - if (drawing_object) + if (drawing_object && drawing_object->description) LXW_PUSH_ATTRIBUTES_STR("descr", drawing_object->description); lxw_xml_empty_tag(self->file, "xdr:cNvPr", &attributes); LXW_FREE_ATTRIBUTES(); @@ -280,11 +280,11 @@ /* * Write the <xdr:nvPicPr> element. */ STATIC void -_drawing_write_nv_pic_pr(lxw_drawing *self, uint16_t index, +_drawing_write_nv_pic_pr(lxw_drawing *self, uint32_t index, lxw_drawing_object *drawing_object) { lxw_xml_start_tag(self->file, "xdr:nvPicPr", NULL); /* Write the xdr:cNvPr element. */ @@ -298,11 +298,11 @@ /* * Write the <a:blip> element. */ STATIC void -_drawing_write_a_blip(lxw_drawing *self, uint16_t index) +_drawing_write_a_blip(lxw_drawing *self, uint32_t index) { struct xml_attribute_list attributes; struct xml_attribute *attribute; char xmlns_r[] = LXW_SCHEMA_OFFICEDOC "/relationships"; char r_id[LXW_MAX_ATTRIBUTE_LENGTH]; @@ -343,11 +343,11 @@ /* * Write the <xdr:blipFill> element. */ STATIC void -_drawing_write_blip_fill(lxw_drawing *self, uint16_t index) +_drawing_write_blip_fill(lxw_drawing *self, uint32_t index) { lxw_xml_start_tag(self->file, "xdr:blipFill", NULL); /* Write the a:blip element. */ _drawing_write_a_blip(self, index); @@ -461,11 +461,11 @@ /* * Write the <xdr:pic> element. */ STATIC void -_drawing_write_pic(lxw_drawing *self, uint16_t index, +_drawing_write_pic(lxw_drawing *self, uint32_t index, lxw_drawing_object *drawing_object) { lxw_xml_start_tag(self->file, "xdr:pic", NULL); /* Write the xdr:nvPicPr element. */ @@ -488,23 +488,50 @@ { lxw_xml_empty_tag(self->file, "xdr:clientData", NULL); } /* + * Write the <a:graphicFrameLocks> element. + */ +STATIC void +_drawing_write_a_graphic_frame_locks(lxw_drawing *self) +{ + struct xml_attribute_list attributes; + struct xml_attribute *attribute; + + LXW_INIT_ATTRIBUTES(); + LXW_PUSH_ATTRIBUTES_INT("noGrp", 1); + + lxw_xml_empty_tag(self->file, "a:graphicFrameLocks", &attributes); + + LXW_FREE_ATTRIBUTES(); +} + +/* * Write the <xdr:cNvGraphicFramePr> element. */ STATIC void _drawing_write_c_nv_graphic_frame_pr(lxw_drawing *self) { - lxw_xml_empty_tag(self->file, "xdr:cNvGraphicFramePr", NULL); + if (self->embedded) { + lxw_xml_empty_tag(self->file, "xdr:cNvGraphicFramePr", NULL); + } + else { + lxw_xml_start_tag(self->file, "xdr:cNvGraphicFramePr", NULL); + + /* Write the a:graphicFrameLocks element. */ + _drawing_write_a_graphic_frame_locks(self); + + lxw_xml_end_tag(self->file, "xdr:cNvGraphicFramePr"); + } } /* * Write the <xdr:nvGraphicFramePr> element. */ STATIC void -_drawing_write_nv_graphic_frame_pr(lxw_drawing *self, uint16_t index) +_drawing_write_nv_graphic_frame_pr(lxw_drawing *self, uint32_t index) { lxw_xml_start_tag(self->file, "xdr:nvGraphicFramePr", NULL); /* Write the xdr:cNvPr element. */ _drawing_write_c_nv_pr(self, "Chart", index, NULL); @@ -570,11 +597,11 @@ /* * Write the <c:chart> element. */ STATIC void -_drawing_write_chart(lxw_drawing *self, uint16_t index) +_drawing_write_chart(lxw_drawing *self, uint32_t index) { struct xml_attribute_list attributes; struct xml_attribute *attribute; char xmlns_c[] = LXW_SCHEMA_DRAWING "/chart"; char xmlns_r[] = LXW_SCHEMA_OFFICEDOC "/relationships"; @@ -594,11 +621,11 @@ /* * Write the <a:graphicData> element. */ STATIC void -_drawing_write_a_graphic_data(lxw_drawing *self, uint16_t index) +_drawing_write_a_graphic_data(lxw_drawing *self, uint32_t index) { struct xml_attribute_list attributes; struct xml_attribute *attribute; char uri[] = LXW_SCHEMA_DRAWING "/chart"; @@ -617,11 +644,11 @@ /* * Write the <a:graphic> element. */ STATIC void -_drawing_write_a_graphic(lxw_drawing *self, uint16_t index) +_drawing_write_a_graphic(lxw_drawing *self, uint32_t index) { lxw_xml_start_tag(self->file, "a:graphic", NULL); /* Write the a:graphicData element. */ @@ -632,11 +659,11 @@ /* * Write the <xdr:graphicFrame> element. */ STATIC void -_drawing_write_graphic_frame(lxw_drawing *self, uint16_t index) +_drawing_write_graphic_frame(lxw_drawing *self, uint32_t index) { struct xml_attribute_list attributes; struct xml_attribute *attribute; LXW_INIT_ATTRIBUTES(); @@ -660,11 +687,11 @@ /* * Write the <xdr:twoCellAnchor> element. */ STATIC void -_drawing_write_two_cell_anchor(lxw_drawing *self, uint16_t index, +_drawing_write_two_cell_anchor(lxw_drawing *self, uint32_t index, lxw_drawing_object *drawing_object) { struct xml_attribute_list attributes; struct xml_attribute *attribute; @@ -703,10 +730,77 @@ lxw_xml_end_tag(self->file, "xdr:twoCellAnchor"); LXW_FREE_ATTRIBUTES(); } +/* + * Write the <xdr:ext> element. + */ +STATIC void +_drawing_write_ext(lxw_drawing *self, uint32_t cx, uint32_t cy) +{ + struct xml_attribute_list attributes; + struct xml_attribute *attribute; + + LXW_INIT_ATTRIBUTES(); + LXW_PUSH_ATTRIBUTES_INT("cx", cx); + LXW_PUSH_ATTRIBUTES_INT("cy", cy); + + lxw_xml_empty_tag(self->file, "xdr:ext", &attributes); + + LXW_FREE_ATTRIBUTES(); +} + +/* + * Write the <xdr:pos> element. + */ +STATIC void +_drawing_write_pos(lxw_drawing *self, int32_t x, int32_t y) +{ + struct xml_attribute_list attributes; + struct xml_attribute *attribute; + + LXW_INIT_ATTRIBUTES(); + LXW_PUSH_ATTRIBUTES_INT("x", x); + LXW_PUSH_ATTRIBUTES_INT("y", y); + + lxw_xml_empty_tag(self->file, "xdr:pos", &attributes); + + LXW_FREE_ATTRIBUTES(); +} + +/* + * Write the <xdr:absoluteAnchor> element. + */ +STATIC void +_drawing_write_absolute_anchor(lxw_drawing *self) +{ + lxw_xml_start_tag(self->file, "xdr:absoluteAnchor", NULL); + + if (self->orientation == LXW_LANDSCAPE) { + /* Write the xdr:pos element. */ + _drawing_write_pos(self, 0, 0); + + /* Write the xdr:ext element. */ + _drawing_write_ext(self, 9308969, 6078325); + } + else { + /* Write the xdr:pos element. */ + _drawing_write_pos(self, 0, -47625); + + /* Write the xdr:ext element. */ + _drawing_write_ext(self, 6162675, 6124575); + } + + _drawing_write_graphic_frame(self, 1); + + /* Write the xdr:clientData element. */ + _drawing_write_client_data(self); + + lxw_xml_end_tag(self->file, "xdr:absoluteAnchor"); +} + /***************************************************************************** * * XML file assembly functions. * ****************************************************************************/ @@ -715,11 +809,11 @@ * Assemble and write the XML file. */ void lxw_drawing_assemble_xml_file(lxw_drawing *self) { - uint16_t index; + uint32_t index; lxw_drawing_object *drawing_object; /* Write the XML declaration. */ _drawing_xml_declaration(self); @@ -731,10 +825,13 @@ STAILQ_FOREACH(drawing_object, self->drawing_objects, list_pointers) { _drawing_write_two_cell_anchor(self, index, drawing_object); index++; } - + } + else { + /* Write the xdr:absoluteAnchor element. Mainly for chartsheets. */ + _drawing_write_absolute_anchor(self); } lxw_xml_end_tag(self->file, "xdr:wsDr"); }