libxlsxwriter/src/format.c in fast_excel-0.4.1 vs libxlsxwriter/src/format.c in fast_excel-0.5.0

- old
+ new

@@ -1,11 +1,11 @@ /***************************************************************************** * format - A library for creating Excel XLSX format files. * * Used in conjunction with the libxlsxwriter library. * - * Copyright 2014-2019, John McNamara, jmcnamara@cpan.org. See LICENSE.txt. + * Copyright 2014-2022, John McNamara, jmcnamara@cpan.org. See LICENSE.txt. * */ #include "xlsxwriter/xmlwriter.h" #include "xlsxwriter/format.h" @@ -25,13 +25,15 @@ { lxw_format *format = calloc(1, sizeof(lxw_format)); GOTO_LABEL_ON_MEM_ERROR(format, mem_error); format->xf_format_indices = NULL; + format->dxf_format_indices = NULL; format->xf_index = LXW_PROPERTY_UNSET; format->dxf_index = LXW_PROPERTY_UNSET; + format->xf_id = 0; format->font_name[0] = '\0'; format->font_scheme[0] = '\0'; format->num_format[0] = '\0'; format->num_format_index = 0; @@ -40,20 +42,20 @@ format->has_dxf_font = LXW_FALSE; format->font_size = 11.0; format->bold = LXW_FALSE; format->italic = LXW_FALSE; format->font_color = LXW_COLOR_UNSET; - format->underline = LXW_FALSE; + format->underline = LXW_UNDERLINE_NONE; format->font_strikeout = LXW_FALSE; format->font_outline = LXW_FALSE; format->font_shadow = LXW_FALSE; format->font_script = LXW_FALSE; format->font_family = LXW_DEFAULT_FONT_FAMILY; format->font_charset = LXW_FALSE; format->font_condense = LXW_FALSE; format->font_extend = LXW_FALSE; - format->theme = LXW_FALSE; + format->theme = 0; format->hyperlink = LXW_FALSE; format->hidden = LXW_FALSE; format->locked = LXW_TRUE; @@ -94,10 +96,12 @@ format->reading_order = 0; format->just_distrib = LXW_FALSE; format->color_indexed = LXW_FALSE; format->font_only = LXW_FALSE; + format->quote_prefix = LXW_FALSE; + return format; mem_error: lxw_format_free(format); return NULL; @@ -115,22 +119,10 @@ free(format); format = NULL; } /* - * Check a user input color. - */ -lxw_color_t -lxw_format_check_color(lxw_color_t color) -{ - if (color == LXW_COLOR_UNSET) - return color; - else - return color & LXW_COLOR_MASK; -} - -/* * Check a user input border. */ STATIC uint8_t _check_border(uint8_t border) { @@ -158,11 +150,13 @@ memcpy(key, self, sizeof(lxw_format)); /* Set pointer members to NULL since they aren't part of the comparison. */ key->xf_format_indices = NULL; + key->dxf_format_indices = NULL; key->num_xf_formats = NULL; + key->num_dxf_formats = NULL; key->list_pointers.stqe_next = NULL; return key; mem_error: @@ -180,12 +174,13 @@ LXW_FORMAT_FIELD_COPY(key->font_name, self->font_name); key->font_size = self->font_size; key->bold = self->bold; key->italic = self->italic; - key->font_color = self->font_color; key->underline = self->underline; + key->theme = self->theme; + key->font_color = self->font_color; key->font_strikeout = self->font_strikeout; key->font_outline = self->font_outline; key->font_shadow = self->font_shadow; key->font_script = self->font_script; key->font_family = self->font_family; @@ -295,10 +290,61 @@ return index; } } /* + * Returns the DXF index number used by Excel to identify a format. + */ +int32_t +lxw_format_get_dxf_index(lxw_format *self) +{ + lxw_format *format_key; + lxw_format *existing_format; + lxw_hash_element *hash_element; + lxw_hash_table *formats_hash_table = self->dxf_format_indices; + int32_t index; + + /* Note: The formats_hash_table/dxf_format_indices contains the unique and + * more importantly the *used* formats in the workbook. + */ + + /* Format already has an index number so return it. */ + if (self->dxf_index != LXW_PROPERTY_UNSET) { + return self->dxf_index; + } + + /* Otherwise, the format doesn't have an index number so we assign one. + * First generate a unique key to identify the format in the hash table. + */ + format_key = _get_format_key(self); + + /* Return the default format index if the key generation failed. */ + if (!format_key) + return 0; + + /* Look up the format in the hash table. */ + hash_element = + lxw_hash_key_exists(formats_hash_table, format_key, + sizeof(lxw_format)); + + if (hash_element) { + /* Format matches existing format with an index. */ + free(format_key); + existing_format = hash_element->value; + return existing_format->dxf_index; + } + else { + /* New format requiring an index. */ + index = formats_hash_table->unique_count; + self->dxf_index = index; + lxw_insert_hash_element(formats_hash_table, format_key, self, + sizeof(lxw_format)); + return index; + } +} + +/* * Set the font_name property. */ void format_set_font_name(lxw_format *self, const char *font_name) { @@ -321,11 +367,11 @@ * Set the font_color property. */ void format_set_font_color(lxw_format *self, lxw_color_t color) { - self->font_color = lxw_format_check_color(color); + self->font_color = color; } /* * Set the bold property. */ @@ -507,20 +553,20 @@ * Set the bg_color property. */ void format_set_bg_color(lxw_format *self, lxw_color_t color) { - self->bg_color = lxw_format_check_color(color); + self->bg_color = color; } /* * Set the fg_color property. */ void format_set_fg_color(lxw_format *self, lxw_color_t color) { - self->fg_color = lxw_format_check_color(color); + self->fg_color = color; } /* * Set the border property. */ @@ -538,11 +584,10 @@ * Set the border_color property. */ void format_set_border_color(lxw_format *self, lxw_color_t color) { - color = lxw_format_check_color(color); self->bottom_color = color; self->top_color = color; self->left_color = color; self->right_color = color; } @@ -560,11 +605,11 @@ * Set the bottom_color property. */ void format_set_bottom_color(lxw_format *self, lxw_color_t color) { - self->bottom_color = lxw_format_check_color(color); + self->bottom_color = color; } /* * Set the left property. */ @@ -578,11 +623,11 @@ * Set the left_color property. */ void format_set_left_color(lxw_format *self, lxw_color_t color) { - self->left_color = lxw_format_check_color(color); + self->left_color = color; } /* * Set the right property. */ @@ -596,11 +641,11 @@ * Set the right_color property. */ void format_set_right_color(lxw_format *self, lxw_color_t color) { - self->right_color = lxw_format_check_color(color); + self->right_color = color; } /* * Set the top property. */ @@ -614,11 +659,11 @@ * Set the top_color property. */ void format_set_top_color(lxw_format *self, lxw_color_t color) { - self->top_color = lxw_format_check_color(color); + self->top_color = color; } /* * Set the diag_type property. */ @@ -633,11 +678,11 @@ * Set the diag_color property. */ void format_set_diag_color(lxw_format *self, lxw_color_t color) { - self->diag_color = lxw_format_check_color(color); + self->diag_color = color; } /* * Set the diag_border property. */ @@ -724,6 +769,45 @@ */ void format_set_theme(lxw_format *self, uint8_t value) { self->theme = value; +} + +/* + * Set the color_indexed property. + */ +void +format_set_color_indexed(lxw_format *self, uint8_t value) +{ + self->color_indexed = value; +} + +/* + * Set the font_only property. + */ +void +format_set_font_only(lxw_format *self) +{ + self->font_only = LXW_TRUE; +} + +/* + * Set the theme property. + */ +void +format_set_hyperlink(lxw_format *self) +{ + self->hyperlink = LXW_TRUE; + self->xf_id = 1; + self->underline = LXW_UNDERLINE_SINGLE; + self->theme = 10; +} + +/* + * Set the quote_prefix property. + */ +void +format_set_quote_prefix(lxw_format *self) +{ + self->quote_prefix = LXW_TRUE; }