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
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
//!
//! Defines the basic structures for table styling,
//! [PageLayout](struct.PageLayout.html)
//! and [Style](struct.Style.html)
//!

use string_cache::DefaultAtom;

use crate::text::TextTag;
use crate::CellRef;

pub use crate::attrmap::*;
use crate::sealed::Sealed;
use crate::style::color_string;
use color::Rgb;
use std::fmt::{Display, Formatter};

/// Origin of a style. Content.xml or Styles.xml.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StyleOrigin {
    Content,
    Styles,
}

impl Default for StyleOrigin {
    fn default() -> Self {
        StyleOrigin::Content
    }
}

/// Placement of a style. office:styles or office:automatic-styles
/// Defines the usage pattern for the style.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StyleUse {
    Default,
    Named,
    Automatic,
}

impl Default for StyleUse {
    fn default() -> Self {
        StyleUse::Automatic
    }
}

/// Applicability of this style.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StyleFor {
    Table,
    TableRow,
    TableColumn,
    TableCell,
    Graphic,
    Paragraph,
    None,
}

impl Default for StyleFor {
    fn default() -> Self {
        StyleFor::None
    }
}

/// Page layout.
/// Contains all header and footer information.
///
/// ```
/// use spreadsheet_ods::{write_ods, WorkBook};
/// use spreadsheet_ods::{cm};
/// use spreadsheet_ods::style::{HeaderFooter, PageLayout, Length};
/// use color::Rgb;
/// use spreadsheet_ods::style::{AttrFoBackgroundColor, AttrFoMinHeight, AttrFoMargin};
///
/// let mut wb = WorkBook::new();
///
/// let mut pl = PageLayout::new_default();
///
/// pl.set_background_color(Rgb::new(12, 129, 252));
///
/// pl.header_attr_mut().set_min_height(cm!(0.75));
/// pl.header_attr_mut().set_margin_left(cm!(0.15));
/// pl.header_attr_mut().set_margin_right(cm!(0.15));
/// pl.header_attr_mut().set_margin_bottom(Length::Cm(0.75));
///
/// pl.header_mut().center_mut().push_text("middle ground");
/// pl.header_mut().left_mut().push_text("left wing");
/// pl.header_mut().right_mut().push_text("right wing");
///
/// wb.add_pagelayout(pl);
///
/// write_ods(&wb, "test_out/hf0.ods").unwrap();
/// ```
///
#[derive(Clone, Debug, Default)]
pub struct PageLayout {
    name: String,
    master_page_name: String,

    attr: AttrMapType,

    header_attr: HeaderFooterAttr,
    header: HeaderFooter,
    header_left: HeaderFooter,

    footer_attr: HeaderFooterAttr,
    footer: HeaderFooter,
    footer_left: HeaderFooter,
}

impl Sealed for PageLayout {}

impl AttrMap for PageLayout {
    fn attr_map(&self) -> &AttrMapType {
        &self.attr
    }

    fn attr_map_mut(&mut self) -> &mut AttrMapType {
        &mut self.attr
    }
}

impl AttrFoBackgroundColor for PageLayout {}

impl AttrFoBorder for PageLayout {}

impl AttrFoMargin for PageLayout {}

impl AttrFoPadding for PageLayout {}

impl AttrStyleDynamicSpacing for PageLayout {}

impl AttrStyleShadow for PageLayout {}

impl AttrSvgHeight for PageLayout {}

impl PageLayout {
    /// Create with name "Mpm1" and masterpage-name "Default".
    pub fn new_default() -> Self {
        Self {
            name: "Mpm1".to_string(),
            master_page_name: "Default".to_string(),
            attr: None,
            header: Default::default(),
            header_left: Default::default(),
            header_attr: Default::default(),
            footer: Default::default(),
            footer_left: Default::default(),
            footer_attr: Default::default(),
        }
    }

    /// Create with name "Mpm2" and masterpage-name "Report".
    pub fn new_report() -> Self {
        Self {
            name: "Mpm2".to_string(),
            master_page_name: "Report".to_string(),
            attr: None,
            header: Default::default(),
            header_left: Default::default(),
            header_attr: Default::default(),
            footer: Default::default(),
            footer_left: Default::default(),
            footer_attr: Default::default(),
        }
    }

    /// Name.
    pub fn set_name(&mut self, name: String) {
        self.name = name;
    }

    /// Name.
    pub fn name(&self) -> &String {
        &self.name
    }

    /// In the xml pagelayout is split in two pieces. Each has a name.
    pub fn set_master_page_name(&mut self, name: String) {
        self.master_page_name = name;
    }

    /// In the xml pagelayout is split in two pieces. Each has a name.
    pub fn master_page_name(&self) -> &String {
        &self.master_page_name
    }

    /// Iterator over the attributes of this pagelayout.
    pub fn attr_iter(&self) -> AttrMapIter {
        AttrMapIter::from(self.attr_map())
    }

    /// Left side header.
    pub fn set_header(&mut self, header: HeaderFooter) {
        self.header = header;
    }

    /// Left side header.
    pub fn header(&self) -> &HeaderFooter {
        &self.header
    }

    /// Header.
    pub fn header_mut(&mut self) -> &mut HeaderFooter {
        &mut self.header
    }

    /// Left side header.
    pub fn set_header_left(&mut self, header: HeaderFooter) {
        self.header_left = header;
    }

    /// Left side header.
    pub fn header_left(&self) -> &HeaderFooter {
        &self.header_left
    }

    /// Left side header.
    pub fn header_left_mut(&mut self) -> &mut HeaderFooter {
        &mut self.header_left
    }

    /// Attributes for header.
    pub fn header_attr(&self) -> &HeaderFooterAttr {
        &self.header_attr
    }

    /// Attributes for header.
    pub fn header_attr_mut(&mut self) -> &mut HeaderFooterAttr {
        &mut self.header_attr
    }

    /// Footer.
    pub fn set_footer(&mut self, footer: HeaderFooter) {
        self.footer = footer;
    }

    /// Footer.
    pub fn footer(&self) -> &HeaderFooter {
        &self.footer
    }

    /// Footer.
    pub fn footer_mut(&mut self) -> &mut HeaderFooter {
        &mut self.footer
    }

    /// Left side footer.
    pub fn set_footer_left(&mut self, footer: HeaderFooter) {
        self.footer_left = footer;
    }

    /// Left side footer.
    pub fn footer_left(&self) -> &HeaderFooter {
        &self.footer_left
    }

    /// Left side footer.
    pub fn footer_left_mut(&mut self) -> &mut HeaderFooter {
        &mut self.footer_left
    }

    /// Attributes for footer.
    pub fn footer_attr(&self) -> &HeaderFooterAttr {
        &self.footer_attr
    }

    /// Attributes for footer.
    pub fn footer_attr_mut(&mut self) -> &mut HeaderFooterAttr {
        &mut self.footer_attr
    }
}

#[derive(Clone, Debug, Default)]
pub struct HeaderFooterAttr {
    attr: AttrMapType,
}

impl Sealed for HeaderFooterAttr {}

impl AttrMap for HeaderFooterAttr {
    fn attr_map(&self) -> &AttrMapType {
        &self.attr
    }

    fn attr_map_mut(&mut self) -> &mut AttrMapType {
        &mut self.attr
    }
}

impl AttrFoBackgroundColor for HeaderFooterAttr {}

impl AttrFoBorder for HeaderFooterAttr {}

impl AttrFoMargin for HeaderFooterAttr {}

impl AttrFoMinHeight for HeaderFooterAttr {}

impl AttrFoPadding for HeaderFooterAttr {}

impl AttrStyleDynamicSpacing for HeaderFooterAttr {}

impl AttrStyleShadow for HeaderFooterAttr {}

impl AttrSvgHeight for HeaderFooterAttr {}

impl<'a> IntoIterator for &'a HeaderFooterAttr {
    type Item = (&'a DefaultAtom, &'a String);
    type IntoIter = AttrMapIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        AttrMapIter::from(self.attr_map())
    }
}

/// Header/Footer data.
/// Can be seen as three regions left/center/right or as one region.
/// In the first case region* contains the data, in the second it's content.
/// Each is a TextTag of parsed XML-tags.
#[derive(Clone, Debug, Default)]
pub struct HeaderFooter {
    display: bool,

    region_left: Option<Box<TextTag>>,
    region_center: Option<Box<TextTag>>,
    region_right: Option<Box<TextTag>>,

    content: Option<Box<TextTag>>,
}

impl HeaderFooter {
    /// Create
    pub fn new() -> Self {
        Self {
            display: true,
            region_left: None,
            region_center: None,
            region_right: None,
            content: None,
        }
    }

    /// Is the header displayed. Used to deactivate left side headers.
    pub fn set_display(&mut self, display: bool) {
        self.display = display;
    }

    /// Display
    pub fn display(&self) -> bool {
        self.display
    }

    /// Left region.
    pub fn set_left(&mut self, txt: TextTag) {
        self.region_left = Some(Box::new(txt));
    }

    /// Left region.
    pub fn left(&self) -> Option<&TextTag> {
        match &self.region_left {
            None => None,
            Some(v) => Some(v.as_ref()),
        }
    }

    /// Left region.
    pub fn left_mut(&mut self) -> &mut TextTag {
        if self.region_left.is_none() {
            self.region_left = Some(Box::new(TextTag::new("text:p")));
        }
        if let Some(center) = &mut self.region_left {
            center
        } else {
            unreachable!()
        }
    }

    /// Center region.
    pub fn set_center(&mut self, txt: TextTag) {
        self.region_center = Some(Box::new(txt));
    }

    /// Center region.
    pub fn center(&self) -> Option<&TextTag> {
        match &self.region_center {
            None => None,
            Some(v) => Some(v.as_ref()),
        }
    }

    /// Center region.
    pub fn center_mut(&mut self) -> &mut TextTag {
        if self.region_center.is_none() {
            self.region_center = Some(Box::new(TextTag::new("text:p")));
        }
        if let Some(center) = &mut self.region_center {
            center
        } else {
            unreachable!()
        }
    }

    /// Right region.
    pub fn set_right(&mut self, txt: TextTag) {
        self.region_right = Some(Box::new(txt));
    }

    /// Right region.
    pub fn right(&self) -> Option<&TextTag> {
        match &self.region_right {
            None => None,
            Some(v) => Some(v.as_ref()),
        }
    }

    /// Right region.
    pub fn right_mut(&mut self) -> &mut TextTag {
        if self.region_right.is_none() {
            self.region_right = Some(Box::new(TextTag::new("text:p")));
        }
        if let Some(center) = &mut self.region_right {
            center
        } else {
            unreachable!()
        }
    }

    /// Header content, if there are no regions.
    pub fn set_content(&mut self, txt: TextTag) {
        self.content = Some(Box::new(txt));
    }

    /// Header content, if there are no regions.
    pub fn content(&self) -> Option<&TextTag> {
        match &self.content {
            None => None,
            Some(v) => Some(v.as_ref()),
        }
    }

    /// Header content, if there are no regions.
    pub fn content_mut(&mut self) -> &mut TextTag {
        if self.content.is_none() {
            self.content = Some(Box::new(TextTag::new("text:p")));
        }
        if let Some(center) = &mut self.content {
            center
        } else {
            unreachable!()
        }
    }
}

/// Font declarations.
#[derive(Clone, Debug, Default)]
pub struct FontFaceDecl {
    name: String,
    /// From where did we get this style.
    origin: StyleOrigin,
    /// All other attributes.
    attr: AttrMapType,
}

impl Sealed for FontFaceDecl {}

impl AttrFontDecl for FontFaceDecl {}

impl AttrMap for FontFaceDecl {
    fn attr_map(&self) -> &AttrMapType {
        &self.attr
    }

    fn attr_map_mut(&mut self) -> &mut AttrMapType {
        &mut self.attr
    }
}

impl FontFaceDecl {
    /// New, empty.
    pub fn new() -> Self {
        Self {
            name: "".to_string(),
            origin: Default::default(),
            attr: None,
        }
    }

    /// New, with a name.
    pub fn new_with_name<S: Into<String>>(name: S) -> Self {
        Self {
            name: name.into(),
            origin: StyleOrigin::Content,
            attr: None,
        }
    }

    /// Set the name.
    pub fn set_name<V: Into<String>>(&mut self, name: V) {
        self.name = name.into();
    }

    /// Returns the name.
    pub fn name(&self) -> &String {
        &self.name
    }

    /// Origin of the style
    pub fn set_origin(&mut self, origin: StyleOrigin) {
        self.origin = origin;
    }

    /// Origin of the style
    pub fn origin(&self) -> StyleOrigin {
        self.origin
    }

    /// Iterator over the attributes of this pagelayout.
    pub fn attr_iter(&self) -> AttrMapIter {
        AttrMapIter::from(self.attr_map())
    }
}

/// Styles define a large number of attributes. These are grouped together
/// as table, row, column, cell, paragraph and text attributes.
///
/// ```
/// use spreadsheet_ods::{Style, CellRef, WorkBook};
/// use spreadsheet_ods::style::{StyleOrigin, StyleUse, AttrText, StyleMap};
/// use color::Rgb;
///
/// let mut wb = WorkBook::new();
///
/// let mut st = Style::new_cell_style("ce12", "num2");
/// st.text_mut().set_color(Rgb::new(192, 128, 0));
/// st.text_mut().set_font_bold();
/// wb.add_style(st);
///
/// let mut st = Style::new_cell_style("ce11", "num2");
/// st.text_mut().set_color(Rgb::new(0, 192, 128));
/// st.text_mut().set_font_bold();
/// wb.add_style(st);
///
/// let mut st = Style::new_cell_style("ce13", "num4");
/// st.push_stylemap(StyleMap::new("cell-content()=\"BB\"", "ce12", CellRef::remote("sheet0", 4, 3)));
/// st.push_stylemap(StyleMap::new("cell-content()=\"CC\"", "ce11", CellRef::remote("sheet0", 4, 3)));
/// wb.add_style(st);
/// ```
/// Styles can be defined in content.xml or as global styles in styles.xml. This
/// is reflected as the StyleOrigin. The StyleUse differentiates between automatic
/// and user visible, named styles. And third StyleFor defines for which part of
/// the document the style can be used.
///
/// Cell styles usually reference a value format for text formatting purposes.
///
/// Styles can also link to a parent style and to a pagelayout.
///
#[derive(Debug, Clone, Default)]
pub struct Style {
    /// Style name.
    name: String,
    /// Nice String.
    display_name: Option<String>,
    /// From where did we get this style.
    origin: StyleOrigin,
    /// Which tag contains this style.
    styleuse: StyleUse,
    /// Applicability of this style.
    family: StyleFor,
    /// Styles can cascade.
    parent: Option<String>,
    /// References the actual formatting instructions in the value-styles.
    value_format: Option<String>,
    /// References a page format. Only valid for table styles.
    master_page_name: Option<String>,
    /// Table styling
    table_attr: TableAttr,
    /// Column styling
    table_col_attr: TableColAttr,
    /// Row styling
    table_row_attr: TableRowAttr,
    /// Cell styles
    table_cell_attr: TableCellAttr,
    /// Text paragraph styles
    paragraph_attr: ParagraphAttr,
    /// Text styles
    text_attr: TextAttr,
    /// Graphic styles
    graphic_attr: GraphicAttr,
    /// Style maps
    stylemaps: Option<Vec<StyleMap>>,
}

impl Style {
    /// New, empty.
    pub fn new() -> Self {
        Style {
            name: String::from(""),
            display_name: None,
            origin: Default::default(),
            styleuse: Default::default(),
            family: Default::default(),
            parent: None,
            value_format: None,
            master_page_name: None,
            table_attr: Default::default(),
            table_col_attr: Default::default(),
            table_row_attr: Default::default(),
            table_cell_attr: Default::default(),
            paragraph_attr: Default::default(),
            text_attr: Default::default(),
            graphic_attr: Default::default(),
            stylemaps: None,
        }
    }

    /// Creates a new cell style.
    /// value_style references a ValueFormat.
    pub fn new_cell_style<S: Into<String>, T: Into<String>>(name: S, value_style: T) -> Self {
        Style::new_with_name(StyleFor::TableCell, name, value_style)
    }

    /// Creates a new column style.
    /// value_style references a ValueFormat.
    pub fn new_col_style<S: Into<String>, T: Into<String>>(name: S, value_style: T) -> Self {
        Style::new_with_name(StyleFor::TableColumn, name, value_style)
    }

    /// Creates a new row style.
    /// value_style references a ValueFormat.
    pub fn new_row_style<S: Into<String>, T: Into<String>>(name: S, value_style: T) -> Self {
        Style::new_with_name(StyleFor::TableRow, name, value_style)
    }

    /// Creates a new table style.
    /// value_style references a ValueFormat.
    pub fn new_table_style<S: Into<String>, T: Into<String>>(name: S, value_style: T) -> Self {
        Style::new_with_name(StyleFor::Table, name, value_style)
    }

    /// New, with name.
    /// value_style references a ValueFormat.
    pub fn new_with_name<S: Into<String>, T: Into<String>>(
        family: StyleFor,
        name: S,
        value_style: T,
    ) -> Self {
        Style {
            name: name.into(),
            display_name: None,
            origin: Default::default(),
            styleuse: Default::default(),
            family,
            parent: Some(String::from("Default")),
            value_format: Some(value_style.into()),
            master_page_name: None,
            table_attr: Default::default(),
            table_col_attr: Default::default(),
            table_row_attr: Default::default(),
            table_cell_attr: Default::default(),
            paragraph_attr: Default::default(),
            text_attr: Default::default(),
            graphic_attr: Default::default(),
            stylemaps: None,
        }
    }

    /// Sets the name.
    pub fn set_name<S: Into<String>>(&mut self, name: S) {
        self.name = name.into();
    }

    /// Returns the name.
    pub fn name(&self) -> &String {
        &self.name
    }

    /// Sets the display name.
    pub fn set_display_name<S: Into<String>>(&mut self, name: S) {
        self.display_name = Some(name.into());
    }

    /// Returns the display name.
    pub fn display_name(&self) -> Option<&String> {
        self.display_name.as_ref()
    }

    /// Sets the origin.
    pub fn set_origin(&mut self, origin: StyleOrigin) {
        self.origin = origin;
    }

    /// Returns the origin.
    pub fn origin(&self) -> StyleOrigin {
        self.origin
    }

    /// Style usage.
    pub fn set_styleuse(&mut self, styleuse: StyleUse) {
        self.styleuse = styleuse;
    }

    /// Returns the usage.
    pub fn styleuse(&self) -> StyleUse {
        self.styleuse
    }

    /// Sets the style-family.
    pub fn set_family(&mut self, family: StyleFor) {
        self.family = family;
    }

    /// Returns the style-family.
    pub fn family(&self) -> StyleFor {
        self.family
    }

    /// Sets the parent style.
    pub fn set_parent<S: Into<String>>(&mut self, parent: S) {
        self.parent = Some(parent.into());
    }

    /// Returns the parent style.
    pub fn parent(&self) -> Option<&String> {
        self.parent.as_ref()
    }

    /// Sets the value format.
    pub fn set_value_format<S: Into<String>>(&mut self, value_format: S) {
        self.value_format = Some(value_format.into());
    }

    /// Returns the value format.
    pub fn value_format(&self) -> Option<&String> {
        self.value_format.as_ref()
    }

    /// Sets the value format.
    pub fn set_master_page_name<S: Into<String>>(&mut self, value_format: S) {
        self.master_page_name = Some(value_format.into());
    }

    /// Returns the value format.
    pub fn master_page_name(&self) -> Option<&String> {
        self.master_page_name.as_ref()
    }

    /// Table style attributes.
    ///
    /// Panic
    ///
    /// Only accessible when family() == StyleFor::Table.
    pub fn table(&self) -> &TableAttr {
        assert_eq!(self.family, StyleFor::Table, "Can only be used for Table-Style.");
        &self.table_attr
    }

    /// Table style attributes.
    ///
    /// Panic
    ///
    /// Only accessible when family() == StyleFor::Table.    
    pub fn table_mut(&mut self) -> &mut TableAttr {
        assert_eq!(self.family, StyleFor::Table, "Can only be used for Table-Style.");
        &mut self.table_attr
    }

    /// Table column style attributes.
    ///
    /// Panic
    ///
    /// Only accessible when family() == StyleFor::TableColumn.
    pub fn col(&self) -> &TableColAttr {
        assert_eq!(self.family, StyleFor::TableColumn, "Can only be used for Column-Style.");
        &self.table_col_attr
    }

    /// Table column style attributes.
    ///
    /// Panic
    ///
    /// Only accessible when family() == StyleFor::TableColumn.
    pub fn col_mut(&mut self) -> &mut TableColAttr {
        assert_eq!(self.family, StyleFor::TableColumn, "Can only be used for Column-Style.");
        &mut self.table_col_attr
    }

    /// Table-row style attributes.
    ///
    /// Panic
    ///
    /// Only accessible when family() == StyleFor::TableRow.
    pub fn row(&self) -> &TableRowAttr {
        assert_eq!(self.family, StyleFor::TableRow, "Can only be used for Row-Style.");
        &self.table_row_attr
    }

    /// Table-row style attributes.
    ///
    /// Panic
    ///
    /// Only accessible when family() == StyleFor::TableRow.
    pub fn row_mut(&mut self) -> &mut TableRowAttr {
        assert_eq!(self.family, StyleFor::TableRow, "Can only be used for Row-Style.");
        &mut self.table_row_attr
    }

    /// Table-cell style attributes.
    ///
    /// Panic
    ///
    /// Only accessible when family() == StyleFor::TableCell.    
    pub fn cell(&self) -> &TableCellAttr {
        assert_eq!(self.family, StyleFor::TableCell, "Can only be used for Cell-Style.");
        &self.table_cell_attr
    }

    /// Table-cell style attributes.
    ///
    /// Panic
    ///
    /// Only accessible when family() == StyleFor::TableCell.    
    pub fn cell_mut(&mut self) -> &mut TableCellAttr {
        assert_eq!(self.family, StyleFor::TableCell, "Can only be used for Cell-Style.");
        &mut self.table_cell_attr
    }

    /// Paragraph style attributes.
    pub fn paragraph(&self) -> &ParagraphAttr {
        &self.paragraph_attr
    }

    /// Paragraph style attributes.
    pub fn paragraph_mut(&mut self) -> &mut ParagraphAttr {
        &mut self.paragraph_attr
    }

    /// Graphic style attributes.
    pub fn graphic(&self) -> &GraphicAttr {
        &self.graphic_attr
    }

    /// Graphic style attributes.
    pub fn graphic_mut(&mut self) -> &mut GraphicAttr {
        &mut self.graphic_attr
    }

    /// Text style attributes.
    pub fn text(&self) -> &TextAttr {
        &self.text_attr
    }

    /// Text style attributes.
    pub fn text_mut(&mut self) -> &mut TextAttr {
        &mut self.text_attr
    }

    /// Adds a stylemap.
    pub fn push_stylemap(&mut self, stylemap: StyleMap) {
        self.stylemaps.get_or_insert_with(Vec::new).push(stylemap);
    }

    /// Returns the stylemaps
    pub fn stylemaps(&self) -> Option<&Vec<StyleMap>> {
        self.stylemaps.as_ref()
    }

    /// Returns the mutable stylemap.
    pub fn stylemaps_mut(&mut self) -> &mut Vec<StyleMap> {
        self.stylemaps.get_or_insert_with(Vec::new)
    }
}

/// One style mapping.
///
/// The rules for this are not very clear. It writes the necessary data fine,
/// but the interpretation by LibreOffice is not very intelligable.
///
/// * The base-cell must include a table-name.
/// * LibreOffice always adds calcext:conditional-formats which I can't handle.
///
/// TODO: clarify all of this.
///
#[derive(Clone, Debug, Default)]
pub struct StyleMap {
    condition: String,
    applied_style: String,
    base_cell: CellRef,
}

impl StyleMap {
    pub fn new<S: Into<String>, T: Into<String>>(
        condition: S,
        apply_style: T,
        cellref: CellRef,
    ) -> Self {
        Self {
            condition: condition.into(),
            applied_style: apply_style.into(),
            base_cell: cellref,
        }
    }

    pub fn condition(&self) -> &String {
        &self.condition
    }

    pub fn set_condition<S: Into<String>>(&mut self, cond: S) {
        self.condition = cond.into();
    }

    pub fn applied_style(&self) -> &String {
        &self.applied_style
    }

    pub fn set_applied_style<S: Into<String>>(&mut self, style: S) {
        self.applied_style = style.into();
    }

    pub fn base_cell(&self) -> &CellRef {
        &self.base_cell
    }

    pub fn set_base_cell(&mut self, cellref: CellRef) {
        self.base_cell = cellref;
    }
}

/// Style for the whole table.
#[derive(Clone, Debug, Default)]
pub struct TableAttr {
    attr: AttrMapType,
}

impl Sealed for TableAttr {}

impl AttrMap for TableAttr {
    fn attr_map(&self) -> &AttrMapType {
        &self.attr
    }

    fn attr_map_mut(&mut self) -> &mut AttrMapType {
        &mut self.attr
    }
}

impl<'a> IntoIterator for &'a TableAttr {
    type Item = (&'a DefaultAtom, &'a String);
    type IntoIter = AttrMapIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        AttrMapIter::from(self.attr_map())
    }
}

impl AttrFoBackgroundColor for TableAttr {}

impl AttrFoMargin for TableAttr {}

impl AttrFoBreak for TableAttr {}

impl AttrFoKeepWithNext for TableAttr {}

impl AttrStyleShadow for TableAttr {}

impl AttrStyleWritingMode for TableAttr {}

/// Styles for table rows.
#[derive(Clone, Debug, Default)]
pub struct TableRowAttr {
    attr: AttrMapType,
}

impl Sealed for TableRowAttr {}

impl AttrMap for TableRowAttr {
    fn attr_map(&self) -> &AttrMapType {
        &self.attr
    }

    fn attr_map_mut(&mut self) -> &mut AttrMapType {
        &mut self.attr
    }
}

impl<'a> IntoIterator for &'a TableRowAttr {
    type Item = (&'a DefaultAtom, &'a String);
    type IntoIter = AttrMapIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        AttrMapIter::from(self.attr_map())
    }
}

impl AttrFoBackgroundColor for TableRowAttr {}

impl AttrFoBreak for TableRowAttr {}

impl AttrFoKeepTogether for TableRowAttr {}

impl AttrTableRow for TableRowAttr {}

/// Styles for table columns.
#[derive(Clone, Debug, Default)]
pub struct TableColAttr {
    attr: AttrMapType,
}

impl Sealed for TableColAttr {}

impl AttrMap for TableColAttr {
    fn attr_map(&self) -> &AttrMapType {
        &self.attr
    }

    fn attr_map_mut(&mut self) -> &mut AttrMapType {
        &mut self.attr
    }
}

impl<'a> IntoIterator for &'a TableColAttr {
    type Item = (&'a DefaultAtom, &'a String);
    type IntoIter = AttrMapIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        AttrMapIter::from(self.attr_map())
    }
}

impl AttrFoBreak for TableColAttr {}

impl AttrTableCol for TableColAttr {}

/// Styles for table cells.
#[derive(Clone, Debug, Default)]
pub struct TableCellAttr {
    attr: AttrMapType,
}

impl Sealed for TableCellAttr {}

impl AttrMap for TableCellAttr {
    fn attr_map(&self) -> &AttrMapType {
        &self.attr
    }

    fn attr_map_mut(&mut self) -> &mut AttrMapType {
        &mut self.attr
    }
}

impl<'a> IntoIterator for &'a TableCellAttr {
    type Item = (&'a DefaultAtom, &'a String);
    type IntoIter = AttrMapIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        AttrMapIter::from(self.attr_map())
    }
}

impl AttrFoBackgroundColor for TableCellAttr {}

impl AttrFoBorder for TableCellAttr {}

impl AttrFoPadding for TableCellAttr {}

impl AttrStyleShadow for TableCellAttr {}

impl AttrStyleWritingMode for TableCellAttr {}

impl AttrTableCell for TableCellAttr {}

#[derive(Clone, Copy, Debug)]
pub enum TabStopType {
    Center,
    Left,
    Right,
    Char,
}

impl Display for TabStopType {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            TabStopType::Center => write!(f, "center"),
            TabStopType::Left => write!(f, "left"),
            TabStopType::Right => write!(f, "right"),
            TabStopType::Char => write!(f, "char"),
        }
    }
}

impl Default for TabStopType {
    fn default() -> Self {
        Self::Left
    }
}

/// Tabstops are part of a paragraph style.
#[derive(Clone, Debug, Default)]
pub struct TabStop {
    attr: AttrMapType,
}

impl TabStop {
    pub fn new() -> Self {
        Self {
            attr: Default::default(),
        }
    }

    /// Delimiter character for tabs of type Char.
    pub fn set_tabstop_char(&mut self, c: char) {
        self.set_attr("style:char", c.to_string());
    }

    /// Color
    pub fn set_leader_color(&mut self, color: Rgb<u8>) {
        self.set_attr("style:leader-color", color_string(color));
    }

    /// Linestyle for the leader line.
    pub fn set_leader_style(&mut self, style: LineStyle) {
        self.set_attr("style:leader-style", style.to_string());
    }

    /// Fill character for the leader line.
    pub fn set_leader_text(&mut self, text: char) {
        self.set_attr("style:leader-text", text.to_string());
    }

    /// Textstyle for the leader line.
    pub fn set_leader_text_style(&mut self, styleref: String) {
        self.set_attr("style:leader-text-style", styleref);
    }

    /// LineType for the leader line.
    pub fn set_leader_type(&mut self, t: LineType) {
        self.set_attr("style:leader-type", t.to_string());
    }

    /// Width of the leader line.
    pub fn set_leader_width(&mut self, w: LineWidth) {
        self.set_attr("style:leader-width", w.to_string());
    }

    /// Position of the tab stop.
    pub fn set_position(&mut self, pos: Length) {
        self.set_attr("style:position", pos.to_string());
    }

    /// Type of the tab stop.
    pub fn set_tabstop_type(&mut self, t: TabStopType) {
        self.set_attr("style:type", t.to_string());
    }
}

impl Sealed for TabStop {}

impl AttrMap for TabStop {
    fn attr_map(&self) -> &AttrMapType {
        &self.attr
    }

    fn attr_map_mut(&mut self) -> &mut AttrMapType {
        &mut self.attr
    }
}

impl<'a> IntoIterator for &'a TabStop {
    type Item = (&'a DefaultAtom, &'a String);
    type IntoIter = AttrMapIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        AttrMapIter::from(self.attr_map())
    }
}

/// Paragraph style.
#[derive(Clone, Debug, Default)]
pub struct ParagraphAttr {
    attr: AttrMapType,
    tabstops: Option<Vec<TabStop>>,
    // todo: drop-cap
    // todo: background-image
}

impl ParagraphAttr {
    pub fn add_tabstop(&mut self, ts: TabStop) {
        let tabstops = self.tabstops.get_or_insert_with(Vec::new);
        tabstops.push(ts);
    }

    pub fn tabstops(&self) -> Option<&Vec<TabStop>> {
        self.tabstops.as_ref()
    }
}

impl Sealed for ParagraphAttr {}

impl AttrMap for ParagraphAttr {
    fn attr_map(&self) -> &AttrMapType {
        &self.attr
    }

    fn attr_map_mut(&mut self) -> &mut AttrMapType {
        &mut self.attr
    }
}

impl<'a> IntoIterator for &'a ParagraphAttr {
    type Item = (&'a DefaultAtom, &'a String);
    type IntoIter = AttrMapIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        AttrMapIter::from(self.attr_map())
    }
}

impl AttrFoBackgroundColor for ParagraphAttr {}

impl AttrFoBorder for ParagraphAttr {}

impl AttrFoBreak for ParagraphAttr {}

impl AttrFoKeepTogether for ParagraphAttr {}

impl AttrFoKeepWithNext for ParagraphAttr {}

impl AttrFoMargin for ParagraphAttr {}

impl AttrFoPadding for ParagraphAttr {}

impl AttrStyleShadow for ParagraphAttr {}

impl AttrStyleWritingMode for ParagraphAttr {}

impl AttrParagraph for ParagraphAttr {}

/// Text styles.
#[derive(Clone, Debug, Default)]
pub struct TextAttr {
    attr: AttrMapType,
}

impl Sealed for TextAttr {}

impl AttrMap for TextAttr {
    fn attr_map(&self) -> &AttrMapType {
        &self.attr
    }

    fn attr_map_mut(&mut self) -> &mut AttrMapType {
        &mut self.attr
    }
}

impl<'a> IntoIterator for &'a TextAttr {
    type Item = (&'a DefaultAtom, &'a String);
    type IntoIter = AttrMapIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        AttrMapIter::from(self.attr_map())
    }
}

impl AttrFoBackgroundColor for TextAttr {}

impl AttrText for TextAttr {}

#[derive(Clone, Debug, Default)]
pub struct GraphicAttr {
    attr: AttrMapType,
}

impl Sealed for GraphicAttr {}

impl AttrMap for GraphicAttr {
    fn attr_map(&self) -> &AttrMapType {
        &self.attr
    }

    fn attr_map_mut(&mut self) -> &mut AttrMapType {
        &mut self.attr
    }
}

impl<'a> IntoIterator for &'a GraphicAttr {
    type Item = (&'a DefaultAtom, &'a String);
    type IntoIter = AttrMapIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        AttrMapIter::from(self.attr_map())
    }
}