ext/xlsxwriter/libxlsxwriter/src/styles.c in xlsxwriter-0.0.5 vs ext/xlsxwriter/libxlsxwriter/src/styles.c in xlsxwriter-0.0.6

- old
+ new

@@ -12,10 +12,12 @@ #include "xlsxwriter/utility.h" /* * Forward declarations. */ +STATIC void _write_font(lxw_styles *self, lxw_format *format, + uint8_t is_rich_string); /***************************************************************************** * * Private functions. * @@ -64,10 +66,38 @@ } free(styles); } +/* + * Write the <t> element for rich strings. + */ +void +lxw_styles_write_string_fragment(lxw_styles *self, char *string) +{ + struct xml_attribute_list attributes; + struct xml_attribute *attribute; + + LXW_INIT_ATTRIBUTES(); + + /* Add attribute to preserve leading or trailing whitespace. */ + if (isspace((unsigned char) string[0]) + || isspace((unsigned char) string[strlen(string) - 1])) + LXW_PUSH_ATTRIBUTES_STR("xml:space", "preserve"); + + lxw_xml_data_element(self->file, "t", string, &attributes); + + LXW_FREE_ATTRIBUTES(); +} + +void +lxw_styles_write_rich_font(lxw_styles *self, lxw_format *format) +{ + + _write_font(self, format, LXW_TRUE); +} + /***************************************************************************** * * XML functions. * ****************************************************************************/ @@ -123,10 +153,11 @@ _write_num_fmts(lxw_styles *self) { struct xml_attribute_list attributes; struct xml_attribute *attribute; lxw_format *format; + uint16_t last_format_index = 0; if (!self->num_format_count) return; LXW_INIT_ATTRIBUTES(); @@ -139,11 +170,17 @@ /* Ignore built-in number formats, i.e., < 164. */ if (format->num_format_index < 164) continue; + /* Ignore duplicates which have an already used index. */ + if (format->num_format_index <= last_format_index) + continue; + _write_num_fmt(self, format->num_format_index, format->num_format); + + last_format_index = format->num_format_index; } lxw_xml_end_tag(self->file, "numFmts"); LXW_FREE_ATTRIBUTES(); @@ -205,11 +242,12 @@ /* * Write the <name> element. */ STATIC void -_write_font_name(lxw_styles *self, const char *font_name) +_write_font_name(lxw_styles *self, const char *font_name, + uint8_t is_rich_string) { struct xml_attribute_list attributes; struct xml_attribute *attribute; LXW_INIT_ATTRIBUTES(); @@ -217,11 +255,14 @@ if (*font_name) LXW_PUSH_ATTRIBUTES_STR("val", font_name); else LXW_PUSH_ATTRIBUTES_STR("val", LXW_DEFAULT_FONT_NAME); - lxw_xml_empty_tag(self->file, "name", &attributes); + if (is_rich_string) + lxw_xml_empty_tag(self->file, "rFont", &attributes); + else + lxw_xml_empty_tag(self->file, "name", &attributes); LXW_FREE_ATTRIBUTES(); } /* @@ -307,13 +348,16 @@ /* * Write the <font> element. */ STATIC void -_write_font(lxw_styles *self, lxw_format *format) +_write_font(lxw_styles *self, lxw_format *format, uint8_t is_rich_string) { - lxw_xml_start_tag(self->file, "font", NULL); + if (is_rich_string) + lxw_xml_start_tag(self->file, "rPr", NULL); + else + lxw_xml_start_tag(self->file, "font", NULL); if (format->bold) lxw_xml_empty_tag(self->file, "b", NULL); if (format->italic) @@ -345,22 +389,25 @@ else if (format->font_color != LXW_COLOR_UNSET) _write_font_color_rgb(self, format->font_color); else _write_font_color_theme(self, LXW_DEFAULT_FONT_THEME); - _write_font_name(self, format->font_name); + _write_font_name(self, format->font_name, is_rich_string); _write_font_family(self, format->font_family); /* Only write the scheme element for the default font type if it * is a hyperlink. */ if ((!*format->font_name || strcmp(LXW_DEFAULT_FONT_NAME, format->font_name) == 0) && !format->hyperlink) { _write_font_scheme(self, format->font_scheme); } - lxw_xml_end_tag(self->file, "font"); + if (is_rich_string) + lxw_xml_end_tag(self->file, "rPr"); + else + lxw_xml_end_tag(self->file, "font"); } /* * Write the <fonts> element. */ @@ -376,10 +423,10 @@ lxw_xml_start_tag(self->file, "fonts", &attributes); STAILQ_FOREACH(format, self->xf_formats, list_pointers) { if (format->has_font) - _write_font(self, format); + _write_font(self, format, LXW_FALSE); } lxw_xml_end_tag(self->file, "fonts"); LXW_FREE_ATTRIBUTES();