summaryrefslogtreecommitdiffstats
path: root/src/liborcus/xlsx_styles_context.cpp
blob: 29454b177241f4a3e18d334c7fabab98333ffdad (plain)
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "xlsx_styles_context.hpp"
#include "impl_utils.hpp"
#include "ooxml_namespace_types.hpp"
#include "ooxml_token_constants.hpp"
#include "xlsx_helper.hpp"
#include "xml_context_global.hpp"

#include <orcus/tokens.hpp>
#include <orcus/measurement.hpp>
#include <orcus/spreadsheet/import_interface_styles.hpp>

#include <mdds/sorted_string_map.hpp>
#include <mdds/global.hpp>

#include <optional>

namespace ss = orcus::spreadsheet;

namespace orcus {

namespace {

namespace border_style {

using map_type = mdds::sorted_string_map<ss::border_style_t, mdds::string_view_map_entry>;

// Keys must be sorted.
constexpr map_type::entry entries[] = {
    { "dashDot",          ss::border_style_t::dash_dot            },
    { "dashDotDot",       ss::border_style_t::dash_dot_dot        },
    { "dashed",           ss::border_style_t::dashed              },
    { "dotted",           ss::border_style_t::dotted              },
    { "double",           ss::border_style_t::double_border       },
    { "hair",             ss::border_style_t::hair                },
    { "medium",           ss::border_style_t::medium              },
    { "mediumDashDot",    ss::border_style_t::medium_dash_dot     },
    { "mediumDashDotDot", ss::border_style_t::medium_dash_dot_dot },
    { "mediumDashed",     ss::border_style_t::medium_dashed       },
    { "none",             ss::border_style_t::none                },
    { "slantDashDot",     ss::border_style_t::slant_dash_dot      },
    { "thick",            ss::border_style_t::thick               },
    { "thin",             ss::border_style_t::thin                }
};

const map_type& get()
{
    static const map_type mt(entries, std::size(entries), ss::border_style_t::none);
    return mt;
}

} // border_style namespace

namespace fill_pattern {

using map_type = mdds::sorted_string_map<ss::fill_pattern_t, mdds::string_view_map_entry>;

// Keys must be sorted.
constexpr map_type::entry entries[] = {
    { "darkDown",        ss::fill_pattern_t::dark_down        },
    { "darkGray",        ss::fill_pattern_t::dark_gray        },
    { "darkGrid",        ss::fill_pattern_t::dark_grid        },
    { "darkHorizontal",  ss::fill_pattern_t::dark_horizontal  },
    { "darkTrellis",     ss::fill_pattern_t::dark_trellis     },
    { "darkUp",          ss::fill_pattern_t::dark_up          },
    { "darkVertical",    ss::fill_pattern_t::dark_vertical    },
    { "gray0625",        ss::fill_pattern_t::gray_0625        },
    { "gray125",         ss::fill_pattern_t::gray_125         },
    { "lightDown",       ss::fill_pattern_t::light_down       },
    { "lightGray",       ss::fill_pattern_t::light_gray       },
    { "lightGrid",       ss::fill_pattern_t::light_grid       },
    { "lightHorizontal", ss::fill_pattern_t::light_horizontal },
    { "lightTrellis",    ss::fill_pattern_t::light_trellis    },
    { "lightUp",         ss::fill_pattern_t::light_up         },
    { "lightVertical",   ss::fill_pattern_t::light_vertical   },
    { "mediumGray",      ss::fill_pattern_t::medium_gray      },
    { "none",            ss::fill_pattern_t::none             },
    { "solid",           ss::fill_pattern_t::solid            },
};

const map_type& get()
{
    static const map_type mt(entries, std::size(entries), ss::fill_pattern_t::none);
    return mt;
}

} // fill_pattern namespace

namespace underline {

using map_type = mdds::sorted_string_map<ss::underline_t, mdds::string_view_map_entry>;

// Keys must be sorted.
constexpr map_type::entry entries[] = {
    { "double", ss::underline_t::double_line },
    { "doubleAccounting", ss::underline_t::double_accounting },
    { "none", ss::underline_t::none },
    { "single", ss::underline_t::single_line },
    { "singleAccounting", ss::underline_t::single_accounting },
};

const map_type& get()
{
    static const map_type mt(entries, std::size(entries), ss::underline_t::none);
    return mt;
}

} // namespace underline

class border_attr_parser
{
    ss::border_direction_t m_dir;
    ss::iface::import_border_style& m_border_style;
public:
    border_attr_parser(ss::border_direction_t dir, ss::iface::import_border_style& style) :
        m_dir(dir), m_border_style(style) {}

    void operator() (const xml_token_attr_t& attr)
    {
        switch (attr.name)
        {
            case XML_style:
            {
                m_border_style.set_style(m_dir,
                    border_style::get().find(attr.value.data(), attr.value.size()));
                break;
            }
        }
    }
};

std::optional<std::size_t> extract_count(const xml_token_attrs_t& attrs)
{
    std::optional<std::size_t> count;

    for (const auto& attr : attrs)
    {
        if (attr.ns)
            continue;

        switch (attr.name)
        {
            case XML_count:
            {
                const char* p_end = nullptr;
                long v = to_long(attr.value, &p_end);
                if (attr.value.data() < p_end && v >= 0)
                    count = v;
                break;
            }
        }
    }

    return count;
}

} // anonymous namespace

xlsx_styles_context::xlsx_styles_context(session_context& session_cxt, const tokens& tokens, ss::iface::import_styles* styles) :
    xml_context_base(session_cxt, tokens),
    mp_styles(styles),
    m_diagonal_up(false), m_diagonal_down(false),
    m_cur_border_dir(ss::border_direction_t::unknown)
{
    static const xml_element_validator::rule rules[] = {
        // parent element -> child element
        { XMLNS_UNKNOWN_ID, XML_UNKNOWN_TOKEN, NS_ooxml_xlsx, XML_styleSheet }, // root element
        { NS_ooxml_xlsx, XML_border, NS_ooxml_xlsx, XML_bottom },
        { NS_ooxml_xlsx, XML_border, NS_ooxml_xlsx, XML_diagonal },
        { NS_ooxml_xlsx, XML_border, NS_ooxml_xlsx, XML_left },
        { NS_ooxml_xlsx, XML_border, NS_ooxml_xlsx, XML_right },
        { NS_ooxml_xlsx, XML_border, NS_ooxml_xlsx, XML_top },
        { NS_ooxml_xlsx, XML_borders, NS_ooxml_xlsx, XML_border },
        { NS_ooxml_xlsx, XML_bottom, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_cellStyleXfs, NS_ooxml_xlsx, XML_xf },
        { NS_ooxml_xlsx, XML_cellStyles, NS_ooxml_xlsx, XML_cellStyle },
        { NS_ooxml_xlsx, XML_cellXfs, NS_ooxml_xlsx, XML_xf },
        { NS_ooxml_xlsx, XML_diagonal, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_dx, NS_ooxml_xlsx, XML_fill },
        { NS_ooxml_xlsx, XML_dxf, NS_ooxml_xlsx, XML_alignment },
        { NS_ooxml_xlsx, XML_dxf, NS_ooxml_xlsx, XML_border },
        { NS_ooxml_xlsx, XML_dxf, NS_ooxml_xlsx, XML_font },
        { NS_ooxml_xlsx, XML_dxf, NS_ooxml_xlsx, XML_numFmt },
        { NS_ooxml_xlsx, XML_dxf, NS_ooxml_xlsx, XML_protection },
        { NS_ooxml_xlsx, XML_end, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_fill, NS_ooxml_xlsx, XML_patternFill },
        { NS_ooxml_xlsx, XML_fills, NS_ooxml_xlsx, XML_fill },
        { NS_ooxml_xlsx, XML_font, NS_ooxml_xlsx, XML_b },
        { NS_ooxml_xlsx, XML_font, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_font, NS_ooxml_xlsx, XML_family },
        { NS_ooxml_xlsx, XML_font, NS_ooxml_xlsx, XML_i },
        { NS_ooxml_xlsx, XML_font, NS_ooxml_xlsx, XML_name },
        { NS_ooxml_xlsx, XML_font, NS_ooxml_xlsx, XML_scheme },
        { NS_ooxml_xlsx, XML_font, NS_ooxml_xlsx, XML_sz },
        { NS_ooxml_xlsx, XML_font, NS_ooxml_xlsx, XML_u },
        { NS_ooxml_xlsx, XML_fonts, NS_ooxml_xlsx, XML_font },
        { NS_ooxml_xlsx, XML_horizontal, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_left, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_mruColors, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_numFmts, NS_ooxml_xlsx, XML_numFmt },
        { NS_ooxml_xlsx, XML_patternFill, NS_ooxml_xlsx, XML_bgColor },
        { NS_ooxml_xlsx, XML_patternFill, NS_ooxml_xlsx, XML_fgColor },
        { NS_ooxml_xlsx, XML_right, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_start, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_stop, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_styleSheet, NS_ooxml_xlsx, XML_borders },
        { NS_ooxml_xlsx, XML_styleSheet, NS_ooxml_xlsx, XML_cellStyleXfs },
        { NS_ooxml_xlsx, XML_styleSheet, NS_ooxml_xlsx, XML_cellStyles },
        { NS_ooxml_xlsx, XML_styleSheet, NS_ooxml_xlsx, XML_cellXfs },
        { NS_ooxml_xlsx, XML_styleSheet, NS_ooxml_xlsx, XML_dxfs },
        { NS_ooxml_xlsx, XML_styleSheet, NS_ooxml_xlsx, XML_fills },
        { NS_ooxml_xlsx, XML_styleSheet, NS_ooxml_xlsx, XML_fonts },
        { NS_ooxml_xlsx, XML_styleSheet, NS_ooxml_xlsx, XML_numFmts },
        { NS_ooxml_xlsx, XML_top, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_vertical, NS_ooxml_xlsx, XML_color },
        { NS_ooxml_xlsx, XML_xf, NS_ooxml_xlsx, XML_alignment },
        { NS_ooxml_xlsx, XML_xf, NS_ooxml_xlsx, XML_protection },
    };

    init_element_validator(rules, std::size(rules));
}

xlsx_styles_context::~xlsx_styles_context() = default;

void xlsx_styles_context::start_element(xmlns_id_t ns, xml_token_t name, const xml_token_attrs_t& attrs)
{
    xml_token_pair_t parent = push_stack(ns, name);

    if (ns == NS_ooxml_xlsx)
    {
        switch (name)
        {
            case XML_styleSheet:
            {
                // root element
                if (get_config().debug)
                    print_attrs(get_tokens(), attrs);
                break;
            }
            case XML_fonts:
            {
                std::string_view ps = for_each(
                    attrs.begin(), attrs.end(), single_attr_getter(m_pool, NS_ooxml_xlsx, XML_count)).get_value();
                size_t font_count = to_long(ps);
                mp_styles->set_font_count(font_count);
                m_font_ids.reserve(font_count);
                break;
            }
            case XML_font:
            {
                mp_font = mp_styles->start_font_style();
                ENSURE_INTERFACE(mp_font, import_font_style);
                break;
            }
            case XML_b:
                assert(mp_font);
                mp_font->set_bold(true);
                break;
            case XML_i:
                assert(mp_font);
                mp_font->set_italic(true);
                break;
            case XML_u:
            {
                assert(mp_font);
                ss::underline_t v = ss::underline_t::single_line; // default value

                for (const auto& attr : attrs)
                {
                    switch (name)
                    {
                        case XML_val:
                            v = underline::get().find(attr.value);
                            break;
                    }
                }

                mp_font->set_underline(v);
                break;
            }
            case XML_sz:
            {
                assert(mp_font);
                std::optional<double> font_size;

                for (const auto& attr : attrs)
                {
                    switch (attr.name)
                    {
                        case XML_val:
                        {
                            const char* p_end = nullptr;
                            double v = to_double(attr.value, &p_end);
                            if (attr.value.data() < p_end)
                                font_size = v;
                            break;
                        }
                    }
                }

                if (font_size)
                    mp_font->set_size(*font_size);

                break;
            }
            case XML_color:
            {
                if (parent.first == NS_ooxml_xlsx)
                {
                    switch (parent.second)
                    {
                        case XML_top:
                        case XML_bottom:
                        case XML_left:
                        case XML_right:
                        case XML_diagonal:
                            // This color is for a border.
                            start_border_color(attrs);
                        break;
                        case XML_font:
                            start_font_color(attrs);
                        default:
                            ;
                    }
                }
                break;
            }
            case XML_name:
            {
                std::string_view ps = for_each(
                    attrs.begin(), attrs.end(), single_attr_getter(m_pool, NS_ooxml_xlsx, XML_val)).get_value();
                mp_font->set_name(ps);
                break;
            }
            case XML_fills:
            {
                std::string_view ps = for_each(
                    attrs.begin(), attrs.end(), single_attr_getter(m_pool, NS_ooxml_xlsx, XML_count)).get_value();
                size_t fill_count = to_long(ps);
                mp_styles->set_fill_count(fill_count);
                m_fill_ids.reserve(fill_count);
                break;
            }
            case XML_fill:
            {
                mp_fill = mp_styles->start_fill_style();
                ENSURE_INTERFACE(mp_fill, import_fill_style);

                break;
            }
            case XML_patternFill:
            {
                std::string_view ps = for_each(
                    attrs.begin(), attrs.end(), single_attr_getter(m_pool, NS_ooxml_xlsx, XML_patternType)).get_value();
                assert(mp_fill);
                mp_fill->set_pattern_type(fill_pattern::get().find(ps.data(), ps.size()));
                break;
            }
            case XML_fgColor:
            {
                assert(mp_fill);

                for (const xml_token_attr_t& attr : attrs)
                {
                    switch (attr.name)
                    {
                        case XML_rgb:
                        {
                            ss::color_elem_t alpha;
                            ss::color_elem_t red;
                            ss::color_elem_t green;
                            ss::color_elem_t blue;
                            if (!to_rgb(attr.value, alpha, red, green, blue))
                                // invalid RGB color format.
                                continue;

                            mp_fill->set_fg_color(alpha, red, green, blue);
                            break;
                        }
                        case XML_indexed:
                            break;
                        default:
                            if (get_config().debug)
                                std::cerr << "warning: unknown attribute [ " << get_tokens().get_token_name(attr.name) << " ]" << std::endl;
                    }
                }

                break;
            }
            case XML_bgColor:
            {
                for (const xml_token_attr_t& attr : attrs)
                {
                    switch (attr.name)
                    {
                        case XML_rgb:
                        {
                            ss::color_elem_t alpha;
                            ss::color_elem_t red;
                            ss::color_elem_t green;
                            ss::color_elem_t blue;
                            if (!to_rgb(attr.value, alpha, red, green, blue))
                                // invalid RGB color format.
                                continue;

                            mp_fill->set_bg_color(alpha, red, green, blue);
                            break;
                        }
                        case XML_indexed:
                            break;
                        default:
                            if (get_config().debug)
                                std::cerr << "warning: unknown attribute [ " << get_tokens().get_token_name(attr.name) << " ]" << std::endl;
                    }
                }

                break;
            }
            case XML_borders:
            {
                std::string_view ps = for_each(
                    attrs.begin(), attrs.end(), single_attr_getter(m_pool, NS_ooxml_xlsx, XML_count)).get_value();
                size_t border_count = to_long(ps);
                mp_styles->set_border_count(border_count);
                m_border_ids.reserve(border_count);
                break;
            }
            case XML_border:
            {
                start_element_border(attrs);

                mp_border = mp_styles->start_border_style();
                ENSURE_INTERFACE(mp_border, import_border_style);

                break;
            }
            case XML_top:
            {
                assert(mp_border);
                m_cur_border_dir = ss::border_direction_t::top;
                border_attr_parser func(ss::border_direction_t::top, *mp_border);
                for_each(attrs.begin(), attrs.end(), func);
                break;
            }
            case XML_bottom:
            {
                assert(mp_border);
                m_cur_border_dir = ss::border_direction_t::bottom;
                border_attr_parser func(ss::border_direction_t::bottom, *mp_border);
                for_each(attrs.begin(), attrs.end(), func);
                break;
            }
            case XML_left:
            {
                assert(mp_border);
                m_cur_border_dir = ss::border_direction_t::left;
                border_attr_parser func(ss::border_direction_t::left, *mp_border);
                for_each(attrs.begin(), attrs.end(), func);
                break;
            }
            case XML_right:
            {
                assert(mp_border);
                m_cur_border_dir = ss::border_direction_t::right;
                border_attr_parser func(ss::border_direction_t::right, *mp_border);
                for_each(attrs.begin(), attrs.end(), func);
                break;
            }
            case XML_diagonal:
            {
                start_element_diagonal(attrs);
                break;
            }
            case XML_cellStyleXfs:
            {
                if (std::optional<std::size_t> count = extract_count(attrs); count)
                {
                    mp_styles->set_xf_count(ss::xf_category_t::cell_style, *count);
                    m_cell_style_xf_ids.reserve(*count);
                }

                mp_xf = mp_styles->start_xf(ss::xf_category_t::cell_style);
                ENSURE_INTERFACE(mp_xf, import_xf);
                m_xf_type = ss::xf_category_t::cell_style;
                break;
            }
            case XML_cellXfs:
            {
                if (std::optional<std::size_t> count = extract_count(attrs); count)
                    mp_styles->set_xf_count(ss::xf_category_t::cell, *count);

                mp_xf = mp_styles->start_xf(ss::xf_category_t::cell);
                ENSURE_INTERFACE(mp_xf, import_xf);
                m_xf_type = ss::xf_category_t::cell;
                break;
            }
            case XML_dxfs:
            {
                if (std::optional<std::size_t> count = extract_count(attrs); count)
                    mp_styles->set_xf_count(ss::xf_category_t::differential, *count);

                mp_xf = mp_styles->start_xf(ss::xf_category_t::differential);
                ENSURE_INTERFACE(mp_xf, import_xf);
                m_xf_type = ss::xf_category_t::differential;
                break;
            }
            case XML_cellStyles:
            {
                std::string_view ps = for_each(
                    attrs.begin(), attrs.end(), single_attr_getter(m_pool, NS_ooxml_xlsx, XML_count)).get_value();
                if (!ps.empty())
                {
                    size_t n = strtoul(ps.data(), nullptr, 10);
                    mp_styles->set_cell_style_count(n);
                }
                mp_cell_style = mp_styles->start_cell_style();
                ENSURE_INTERFACE(mp_cell_style, import_cell_style);
                break;
            }
            case XML_cellStyle:
            {
                // named cell style, some of which are built-in such as 'Normal'.
                assert(mp_cell_style);

                for (const xml_token_attr_t& attr : attrs)
                {
                    switch (attr.name)
                    {
                        case XML_name:
                            mp_cell_style->set_name(attr.value);
                            break;
                        case XML_xfId:
                        {
                            // reference ID to an xf entry in cellStyleXfs
                            const char* p_end = nullptr;
                            size_t n = to_long(attr.value, &p_end);
                            if (attr.value.data() < p_end)
                            {
                                if (n < m_cell_style_xf_ids.size())
                                    mp_cell_style->set_xf(m_cell_style_xf_ids[n]);
                                else
                                {
                                    std::ostringstream os;
                                    os << "out-of-bound cellStyle@xfId: id=" << n << "; count=" << m_cell_style_xf_ids.size();
                                    warn(os.str());
                                }
                            }
                            break;
                        }
                        case XML_builtinId:
                        {
                            size_t n = to_long(attr.value);
                            mp_cell_style->set_builtin(n);
                        }
                        break;
                    }
                }
                break;
            }
            case XML_xf:
            {
                start_xf(attrs);
                break;
            }
            case XML_protection:
            {
                mp_protection = mp_styles->start_cell_protection();
                ENSURE_INTERFACE(mp_protection, import_cell_protection);

                for (const auto& attr : attrs)
                {
                    switch (attr.name)
                    {
                        case XML_hidden:
                        {
                            bool b = to_long(attr.value) != 0;
                            mp_protection->set_hidden(b);
                            break;
                        }
                        case XML_locked:
                        {
                            bool b = to_long(attr.value) != 0;
                            mp_protection->set_locked(b);
                            break;
                        }
                    }
                }

                break;
            }
            case XML_alignment:
            {
                assert(mp_xf);

                // NB: default vertical alignment is 'bottom'.
                ss::hor_alignment_t hor_align = ss::hor_alignment_t::unknown;
                ss::ver_alignment_t ver_align = ss::ver_alignment_t::bottom;
                bool wrap_text = false;
                bool shrink_to_fit = false;

                for (const xml_token_attr_t& attr : attrs)
                {
                    switch (attr.name)
                    {
                        case XML_horizontal:
                        {
                            if (attr.value == "center")
                                hor_align = ss::hor_alignment_t::center;
                            else if (attr.value == "right")
                                hor_align = ss::hor_alignment_t::right;
                            else if (attr.value == "left")
                                hor_align = ss::hor_alignment_t::left;
                            else if (attr.value == "justify")
                                hor_align = ss::hor_alignment_t::justified;
                            else if (attr.value == "distributed")
                                hor_align = ss::hor_alignment_t::distributed;
                            break;
                        }
                        case XML_vertical:
                        {
                            if (attr.value == "top")
                                ver_align = ss::ver_alignment_t::top;
                            else if (attr.value == "center")
                                ver_align = ss::ver_alignment_t::middle;
                            else if (attr.value == "bottom")
                                ver_align = ss::ver_alignment_t::bottom;
                            else if (attr.value == "justify")
                                ver_align = ss::ver_alignment_t::justified;
                            else if (attr.value == "distributed")
                                ver_align = ss::ver_alignment_t::distributed;
                            break;
                        }
                        case XML_wrapText:
                            wrap_text = to_bool(attr.value);
                            break;
                        case XML_shrinkToFit:
                            shrink_to_fit = to_bool(attr.value);
                            break;
                    }
                }

                mp_xf->set_horizontal_alignment(hor_align);
                mp_xf->set_vertical_alignment(ver_align);
                mp_xf->set_wrap_text(wrap_text);
                mp_xf->set_shrink_to_fit(shrink_to_fit);
                break;
            }
            case XML_numFmts:
            {
                std::string_view val =
                    for_each(
                        attrs.begin(), attrs.end(),
                        single_attr_getter(m_pool, NS_ooxml_xlsx, XML_count)).get_value();
                if (!val.empty())
                {
                    size_t n = to_long(val);
                    mp_styles->set_number_format_count(n);
                }
                break;
            }
            case XML_numFmt:
                start_number_format(attrs);
                break;
            default:
                warn_unhandled();
        }
    }
    else
        warn_unhandled();
}

bool xlsx_styles_context::end_element(xmlns_id_t ns, xml_token_t name)
{
    switch (name)
    {
        case XML_font:
        {
            assert(mp_font);
            std::size_t id = mp_font->commit();
            m_font_ids.push_back(id);
            mp_font = nullptr;
            break;
        }
        case XML_fill:
        {
            assert(mp_fill);
            std::size_t id = mp_fill->commit();
            m_fill_ids.push_back(id);
            mp_fill = nullptr;
            break;
        }
        case XML_border:
        {
            assert(mp_border);
            std::size_t id = mp_border->commit();
            m_border_ids.push_back(id);
            mp_border = nullptr;
            break;
        }
        case XML_cellStyle:
            assert(mp_cell_style);
            mp_cell_style->commit();
            break;
        case XML_cellStyles:
            assert(mp_cell_style);
            mp_cell_style = nullptr;
            break;
        case XML_cellStyleXfs:
        case XML_cellXfs:
        case XML_dxfs:
            assert(mp_xf);
            mp_xf = nullptr;
            m_xf_type = ss::xf_category_t::unknown;
            break;
        case XML_xf:
        case XML_dxf:
        {
            assert(mp_xf);
            std::size_t id = mp_xf->commit();
            switch (m_xf_type)
            {
                    break;
                case ss::xf_category_t::cell_style:
                    // only cell style xf ID is referenced.
                    m_cell_style_xf_ids.push_back(id);
                    break;
                case ss::xf_category_t::cell:
                case ss::xf_category_t::differential:
                    // not used
                    break;
                case ss::xf_category_t::unknown:
                    warn("xf entry committed while the current xf category is unknown");
                    break;
            }
            break;
        }
        case XML_protection:
        {
            assert(mp_protection);
            size_t prot_id = mp_protection->commit();
            assert(mp_xf);
            mp_xf->set_protection(prot_id);
            break;
        }
        case XML_numFmt:
            end_number_format();
            break;
    }
    return pop_stack(ns, name);
}

void xlsx_styles_context::characters(std::string_view /*str*/, bool /*transient*/)
{
    // not used in the styles.xml part.
}

void xlsx_styles_context::start_number_format(const xml_token_attrs_t& attrs)
{
    if (!mp_styles)
        return;

    mp_numfmt = mp_styles->start_number_format();
    ENSURE_INTERFACE(mp_numfmt, import_number_format);

    m_cur_numfmt_id.reset();

    for (const xml_token_attr_t& attr : attrs)
    {
        if (attr.ns)
            continue;

        switch (attr.name)
        {
            case XML_numFmtId:
            {
                const char* p_end = nullptr;
                long id = to_long(attr.value, &p_end);
                if (attr.value.data() < p_end && id >= 0)
                {
                    mp_numfmt->set_identifier(id);
                    m_cur_numfmt_id = id;
                }
                break;
            }
            case XML_formatCode:
            {
                mp_numfmt->set_code(attr.value);
                break;
            }
        }
    }
}

void xlsx_styles_context::start_element_border(const xml_token_attrs_t& attrs)
{
    bool diagonal_up = false;
    bool diagonal_down = false;

    std::for_each(attrs.begin(), attrs.end(),
        [&diagonal_up,&diagonal_down](const xml_token_attr_t& attr)
        {
            if (attr.ns && attr.ns != NS_ooxml_xlsx)
                return;

            switch (attr.name)
            {
                case XML_diagonalDown:
                    // top-left to bottom-right.
                    diagonal_down = to_long(attr.value) != 0;
                    break;
                case XML_diagonalUp:
                    // bottom-left to top-right.
                    diagonal_up = to_long(attr.value) != 0;
                    break;
                default:
                    ;
            }
        }
    );

    m_diagonal_up = diagonal_up;
    m_diagonal_down = diagonal_down;
}

void xlsx_styles_context::start_element_diagonal(const xml_token_attrs_t& attrs)
{
    assert(mp_border);

    m_cur_border_dir = ss::border_direction_t::unknown;

    if (m_diagonal_up)
    {
        m_cur_border_dir = m_diagonal_down ?
            ss::border_direction_t::diagonal :
            ss::border_direction_t::diagonal_bl_tr;
    }
    else
    {
        m_cur_border_dir = m_diagonal_down ?
            ss::border_direction_t::diagonal_tl_br :
            ss::border_direction_t::unknown;
    }

    if (m_cur_border_dir == ss::border_direction_t::unknown)
        return;

    border_attr_parser func(m_cur_border_dir, *mp_border);
    for_each(attrs.begin(), attrs.end(), func);
}

void xlsx_styles_context::start_border_color(const xml_token_attrs_t& attrs)
{
    assert(mp_border);

    std::optional<std::string_view> rgb;

    for (const xml_token_attr_t& attr : attrs)
    {
        switch (attr.name)
        {
            case XML_rgb:
                rgb = attr.value;
                break;
            case XML_theme:
                // TODO : handle this.
                break;
        }
    }

    if (rgb)
    {
        ss::color_elem_t alpha;
        ss::color_elem_t red;
        ss::color_elem_t green;
        ss::color_elem_t blue;

        if (to_rgb(*rgb, alpha, red, green, blue))
            mp_border->set_color(m_cur_border_dir, alpha, red, green, blue);
    }
}

void xlsx_styles_context::start_font_color(const xml_token_attrs_t& attrs)
{
    assert(mp_font);

    std::optional<std::string_view> rgb;

    for (const xml_token_attr_t& attr : attrs)
    {
        switch (attr.name)
        {
            case XML_rgb:
                rgb = attr.value;
                break;
            case XML_theme:
                // TODO : handle this.
                break;
        }
    }

    if (rgb)
    {
        ss::color_elem_t alpha;
        ss::color_elem_t red;
        ss::color_elem_t green;
        ss::color_elem_t blue;
        if (to_rgb(*rgb, alpha, red, green, blue))
            mp_font->set_color(alpha, red, green, blue);
    }
}

void xlsx_styles_context::start_xf(const xml_token_attrs_t& attrs)
{
    assert(mp_xf);

    for (const xml_token_attr_t& attr : attrs)
    {
        switch (attr.name)
        {
            case XML_borderId:
            {
                const char* p_end = nullptr;
                size_t n = to_long(attr.value, &p_end);
                if (attr.value.data() < p_end)
                {
                    if (n < m_border_ids.size())
                        mp_xf->set_border(m_border_ids[n]);
                    else
                    {
                        std::ostringstream os;
                        os << "out-of-bound borderId: id=" << n << "; count=" << m_border_ids.size();
                        warn(os.str());
                    }
                }
                break;
            }
            case XML_fillId:
            {
                const char* p_end = nullptr;
                size_t n = to_long(attr.value, &p_end);
                if (attr.value.data() < p_end)
                {
                    if (n < m_fill_ids.size())
                        mp_xf->set_fill(m_fill_ids[n]);
                    else
                    {
                        std::ostringstream os;
                        os << "out-of-bound fillId: id=" << n << "; count=" << m_fill_ids.size();
                        warn(os.str());
                    }
                }
                break;
            }
            case XML_fontId:
            {
                const char* p_end = nullptr;
                size_t n = to_long(attr.value, &p_end);
                if (attr.value.data() < p_end)
                {
                    if (n < m_font_ids.size())
                        mp_xf->set_font(m_font_ids[n]);
                    else
                    {
                        std::ostringstream os;
                        os << "out-of-bound fontId: id=" << n << "; count=" << m_font_ids.size();
                        warn(os.str());
                    }
                }
                break;
            }
            case XML_numFmtId:
            {
                const char* p_end = nullptr;
                long n = to_long(attr.value, &p_end);
                if (attr.value.data() < p_end && n >= 0)
                {
                    auto it = m_numfmt_ids.find(n);
                    if (it == m_numfmt_ids.end())
                    {
                        std::ostringstream os;
                        os << "no entry for xf@numFmtId = " << n;
                        warn(os.str());
                    }
                    else
                        mp_xf->set_number_format(it->second);
                }
                break;
            }
            case XML_xfId:
            {
                // reference ID to an xf entry in cellStyleXfs
                const char* p_end = nullptr;
                size_t n = to_long(attr.value, &p_end);
                if (attr.value.data() < p_end)
                {
                    if (n < m_cell_style_xf_ids.size())
                        mp_xf->set_style_xf(m_cell_style_xf_ids[n]);
                    else
                    {
                        std::ostringstream os;
                        os << "out-of-bound xf@xfId: id=" << n << "; count=" << m_cell_style_xf_ids.size();
                        warn(os.str());
                    }
                }
                break;
            }
            case XML_applyBorder:
                break;
            case XML_applyFill:
                break;
            case XML_applyFont:
                break;
            case XML_applyNumberFormat:
                break;
            case XML_applyAlignment:
            {
                bool b = to_long(attr.value) != 0;
                mp_xf->set_apply_alignment(b);
                break;
            }
        }
    }
}

void xlsx_styles_context::end_number_format()
{
    if (!mp_styles)
        return;

    assert(mp_numfmt);
    std::size_t id = mp_numfmt->commit();
    mp_numfmt = nullptr;

    if (m_cur_numfmt_id)
    {
        auto res = m_numfmt_ids.insert_or_assign(*m_cur_numfmt_id, id);
        if (!res.second)
        {
            // assigned to an existing key
            std::ostringstream os;
            os << "number format id of " << *m_cur_numfmt_id << " referenced multiple times";
            warn(os.str());
        }
    }
}

} // namespace orcus

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */