File: format.c

package info (click to toggle)
libxlsxwriter 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,864 kB
  • sloc: ansic: 65,601; python: 2,106; makefile: 693; perl: 229; sh: 224; xml: 168; cpp: 73; javascript: 5
file content (813 lines) | stat: -rw-r--r-- 17,010 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/*****************************************************************************
 * format - A library for creating Excel XLSX format files.
 *
 * Used in conjunction with the libxlsxwriter library.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 * Copyright 2014-2025, John McNamara, jmcnamara@cpan.org.
 *
 */

#include "xlsxwriter/xmlwriter.h"
#include "xlsxwriter/format.h"
#include "xlsxwriter/utility.h"

/*****************************************************************************
 *
 * Private functions.
 *
 ****************************************************************************/

/*
 * Create a new format object.
 */
lxw_format *
lxw_format_new(void)
{
    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;
    format->font_index = 0;
    format->has_font = LXW_FALSE;
    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_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 = 0;
    format->hyperlink = LXW_FALSE;

    format->hidden = LXW_FALSE;
    format->locked = LXW_TRUE;

    format->text_h_align = LXW_ALIGN_NONE;
    format->text_wrap = LXW_FALSE;
    format->text_v_align = LXW_ALIGN_NONE;
    format->text_justlast = LXW_FALSE;
    format->rotation = 0;

    format->fg_color = LXW_COLOR_UNSET;
    format->bg_color = LXW_COLOR_UNSET;
    format->pattern = LXW_PATTERN_NONE;
    format->has_fill = LXW_FALSE;
    format->has_dxf_fill = LXW_FALSE;
    format->fill_index = 0;
    format->fill_count = 0;

    format->border_index = 0;
    format->has_border = LXW_FALSE;
    format->has_dxf_border = LXW_FALSE;
    format->border_count = 0;

    format->bottom = LXW_BORDER_NONE;
    format->left = LXW_BORDER_NONE;
    format->right = LXW_BORDER_NONE;
    format->top = LXW_BORDER_NONE;
    format->diag_border = LXW_BORDER_NONE;
    format->diag_type = LXW_BORDER_NONE;
    format->bottom_color = LXW_COLOR_UNSET;
    format->left_color = LXW_COLOR_UNSET;
    format->right_color = LXW_COLOR_UNSET;
    format->top_color = LXW_COLOR_UNSET;
    format->diag_color = LXW_COLOR_UNSET;

    format->indent = 0;
    format->shrink = LXW_FALSE;
    format->merge_range = LXW_FALSE;
    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;
}

/*
 * Free a format object.
 */
void
lxw_format_free(lxw_format *format)
{
    if (!format)
        return;

    free(format);
    format = NULL;
}

/*
 * Check a user input border.
 */
STATIC uint8_t
_check_border(uint8_t border)
{
    if (border >= LXW_BORDER_THIN && border <= LXW_BORDER_SLANT_DASH_DOT)
        return border;
    else
        return LXW_BORDER_NONE;
}

/*****************************************************************************
 *
 * Public functions.
 *
 ****************************************************************************/

/*
 * Returns a format struct suitable for hashing as a lookup key. This is
 * mainly a memcpy with any pointer members set to NULL.
 */
STATIC lxw_format *
_get_format_key(lxw_format *self)
{
    lxw_format *key = calloc(1, sizeof(lxw_format));
    GOTO_LABEL_ON_MEM_ERROR(key, mem_error);

    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:
    return NULL;
}

/*
 * Returns a font struct suitable for hashing as a lookup key.
 */
lxw_font *
lxw_format_get_font_key(lxw_format *self)
{
    lxw_font *key = calloc(1, sizeof(lxw_font));
    GOTO_LABEL_ON_MEM_ERROR(key, mem_error);

    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->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;
    key->font_charset = self->font_charset;
    key->font_condense = self->font_condense;
    key->font_extend = self->font_extend;

    return key;

mem_error:
    return NULL;
}

/*
 * Returns a border struct suitable for hashing as a lookup key.
 */
lxw_border *
lxw_format_get_border_key(lxw_format *self)
{
    lxw_border *key = calloc(1, sizeof(lxw_border));
    GOTO_LABEL_ON_MEM_ERROR(key, mem_error);

    key->bottom = self->bottom;
    key->left = self->left;
    key->right = self->right;
    key->top = self->top;
    key->diag_border = self->diag_border;
    key->diag_type = self->diag_type;
    key->bottom_color = self->bottom_color;
    key->left_color = self->left_color;
    key->right_color = self->right_color;
    key->top_color = self->top_color;
    key->diag_color = self->diag_color;

    return key;

mem_error:
    return NULL;
}

/*
 * Returns a pattern fill struct suitable for hashing as a lookup key.
 */
lxw_fill *
lxw_format_get_fill_key(lxw_format *self)
{
    lxw_fill *key = calloc(1, sizeof(lxw_fill));
    GOTO_LABEL_ON_MEM_ERROR(key, mem_error);

    key->fg_color = self->fg_color;
    key->bg_color = self->bg_color;
    key->pattern = self->pattern;

    return key;

mem_error:
    return NULL;
}

/*
 * Returns the XF index number used by Excel to identify a format.
 */
int32_t
lxw_format_get_xf_index(lxw_format *self)
{
    lxw_format *format_key;
    lxw_format *existing_format;
    lxw_hash_element *hash_element;
    lxw_hash_table *formats_hash_table = self->xf_format_indices;
    int32_t index;

    /* Note: The formats_hash_table/xf_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->xf_index != LXW_PROPERTY_UNSET) {
        return self->xf_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->xf_index;
    }
    else {
        /* New format requiring an index. */
        index = formats_hash_table->unique_count;
        self->xf_index = index;
        lxw_insert_hash_element(formats_hash_table, format_key, self,
                                sizeof(lxw_format));
        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)
{
    LXW_FORMAT_FIELD_COPY(self->font_name, font_name);
}

/*
 * Set the font_size property.
 */
void
format_set_font_size(lxw_format *self, double size)
{

    if (size >= LXW_MIN_FONT_SIZE && size <= LXW_MAX_FONT_SIZE)
        self->font_size = size;
}

/*
 * Set the font_color property.
 */
void
format_set_font_color(lxw_format *self, lxw_color_t color)
{
    self->font_color = color;
}

/*
 * Set the bold property.
 */
void
format_set_bold(lxw_format *self)
{
    self->bold = LXW_TRUE;
}

/*
 * Set the italic property.
 */

void
format_set_italic(lxw_format *self)
{
    self->italic = LXW_TRUE;
}

/*
 * Set the underline property.
 */
void
format_set_underline(lxw_format *self, uint8_t style)
{
    if (style >= LXW_UNDERLINE_SINGLE
        && style <= LXW_UNDERLINE_DOUBLE_ACCOUNTING)
        self->underline = style;
}

/*
 * Set the font_strikeout property.
 */
void
format_set_font_strikeout(lxw_format *self)
{
    self->font_strikeout = LXW_TRUE;
}

/*
 * Set the font_script property.
 */
void
format_set_font_script(lxw_format *self, uint8_t style)
{
    if (style >= LXW_FONT_SUPERSCRIPT && style <= LXW_FONT_SUBSCRIPT)
        self->font_script = style;
}

/*
 * Set the font_outline property.
 */
void
format_set_font_outline(lxw_format *self)
{
    self->font_outline = LXW_TRUE;
}

/*
 * Set the font_shadow property.
 */
void
format_set_font_shadow(lxw_format *self)
{
    self->font_shadow = LXW_TRUE;
}

/*
 * Set the num_format property.
 */
void
format_set_num_format(lxw_format *self, const char *num_format)
{
    LXW_FORMAT_FIELD_COPY(self->num_format, num_format);
}

/*
 * Set the unlocked property.
 */
void
format_set_unlocked(lxw_format *self)
{
    self->locked = LXW_FALSE;
}

/*
 * Set the hidden property.
 */
void
format_set_hidden(lxw_format *self)
{
    self->hidden = LXW_TRUE;
}

/*
 * Set the align property.
 */
void
format_set_align(lxw_format *self, uint8_t value)
{
    if (value >= LXW_ALIGN_LEFT && value <= LXW_ALIGN_DISTRIBUTED) {
        self->text_h_align = value;
    }

    if (value >= LXW_ALIGN_VERTICAL_TOP
        && value <= LXW_ALIGN_VERTICAL_DISTRIBUTED) {
        self->text_v_align = value;
    }
}

/*
 * Set the text_wrap property.
 */
void
format_set_text_wrap(lxw_format *self)
{
    self->text_wrap = LXW_TRUE;
}

/*
 * Set the rotation property.
 */
void
format_set_rotation(lxw_format *self, int16_t angle)
{
    /* Convert user angle to Excel angle. */
    if (angle == 270) {
        self->rotation = 255;
    }
    else if (angle >= -90 && angle <= 90) {
        if (angle < 0)
            angle = -angle + 90;

        self->rotation = angle;
    }
    else {
        LXW_WARN("Rotation rotation outside range: -90 <= angle <= 90.");
        self->rotation = 0;
    }
}

/*
 * Set the indent property.
 */
void
format_set_indent(lxw_format *self, uint8_t value)
{
    self->indent = value;
}

/*
 * Set the shrink property.
 */
void
format_set_shrink(lxw_format *self)
{
    self->shrink = LXW_TRUE;
}

/*
 * Set the text_justlast property.
 */
void
format_set_text_justlast(lxw_format *self)
{
    self->text_justlast = LXW_TRUE;
}

/*
 * Set the pattern property.
 */
void
format_set_pattern(lxw_format *self, uint8_t value)
{
    self->pattern = value;
}

/*
 * Set the bg_color property.
 */
void
format_set_bg_color(lxw_format *self, lxw_color_t 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 = color;
}

/*
 * Set the border property.
 */
void
format_set_border(lxw_format *self, uint8_t style)
{
    style = _check_border(style);
    self->bottom = style;
    self->top = style;
    self->left = style;
    self->right = style;
}

/*
 * Set the border_color property.
 */
void
format_set_border_color(lxw_format *self, lxw_color_t color)
{
    self->bottom_color = color;
    self->top_color = color;
    self->left_color = color;
    self->right_color = color;
}

/*
 * Set the bottom property.
 */
void
format_set_bottom(lxw_format *self, uint8_t style)
{
    self->bottom = _check_border(style);
}

/*
 * Set the bottom_color property.
 */
void
format_set_bottom_color(lxw_format *self, lxw_color_t color)
{
    self->bottom_color = color;
}

/*
 * Set the left property.
 */
void
format_set_left(lxw_format *self, uint8_t style)
{
    self->left = _check_border(style);
}

/*
 * Set the left_color property.
 */
void
format_set_left_color(lxw_format *self, lxw_color_t color)
{
    self->left_color = color;
}

/*
 * Set the right property.
 */
void
format_set_right(lxw_format *self, uint8_t style)
{
    self->right = _check_border(style);
}

/*
 * Set the right_color property.
 */
void
format_set_right_color(lxw_format *self, lxw_color_t color)
{
    self->right_color = color;
}

/*
 * Set the top property.
 */
void
format_set_top(lxw_format *self, uint8_t style)
{
    self->top = _check_border(style);
}

/*
 * Set the top_color property.
 */
void
format_set_top_color(lxw_format *self, lxw_color_t color)
{
    self->top_color = color;
}

/*
 * Set the diag_type property.
 */
void
format_set_diag_type(lxw_format *self, uint8_t type)
{
    if (type >= LXW_DIAGONAL_BORDER_UP && type <= LXW_DIAGONAL_BORDER_UP_DOWN)
        self->diag_type = type;
}

/*
 * Set the diag_color property.
 */
void
format_set_diag_color(lxw_format *self, lxw_color_t color)
{
    self->diag_color = color;
}

/*
 * Set the diag_border property.
 */
void
format_set_diag_border(lxw_format *self, uint8_t style)
{
    self->diag_border = style;
}

/*
 * Set the num_format_index property.
 */
void
format_set_num_format_index(lxw_format *self, uint8_t value)
{
    self->num_format_index = value;
}

/*
 * Set the valign property.
 */
void
format_set_valign(lxw_format *self, uint8_t value)
{
    self->text_v_align = value;
}

/*
 * Set the reading_order property.
 */
void
format_set_reading_order(lxw_format *self, uint8_t value)
{
    self->reading_order = value;
}

/*
 * Set the font_family property.
 */
void
format_set_font_family(lxw_format *self, uint8_t value)
{
    self->font_family = value;
}

/*
 * Set the font_charset property.
 */
void
format_set_font_charset(lxw_format *self, uint8_t value)
{
    self->font_charset = value;
}

/*
 * Set the font_scheme property.
 */
void
format_set_font_scheme(lxw_format *self, const char *font_scheme)
{
    LXW_FORMAT_FIELD_COPY(self->font_scheme, font_scheme);
}

/*
 * Set the font_condense property.
 */
void
format_set_font_condense(lxw_format *self)
{
    self->font_condense = LXW_TRUE;
}

/*
 * Set the font_extend property.
 */
void
format_set_font_extend(lxw_format *self)
{
    self->font_extend = LXW_TRUE;
}

/*
 * Set the theme property.
 */
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;
}