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
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <memory>
#include <xistyle.hxx>
#include <sfx2/objsh.hxx>
#include <svtools/ctrltool.hxx>
#include <editeng/editobj.hxx>
#include <scitems.hxx>
#include <editeng/fontitem.hxx>
#include <editeng/fhgtitem.hxx>
#include <editeng/wghtitem.hxx>
#include <editeng/udlnitem.hxx>
#include <editeng/postitem.hxx>
#include <editeng/crossedoutitem.hxx>
#include <editeng/contouritem.hxx>
#include <editeng/shdditem.hxx>
#include <editeng/escapementitem.hxx>
#include <svx/algitem.hxx>
#include <editeng/borderline.hxx>
#include <editeng/boxitem.hxx>
#include <editeng/lineitem.hxx>
#include <svx/rotmodit.hxx>
#include <editeng/colritem.hxx>
#include <editeng/brushitem.hxx>
#include <editeng/frmdiritem.hxx>
#include <editeng/eeitem.hxx>
#include <editeng/flstitem.hxx>
#include <editeng/justifyitem.hxx>
#include <editeng/editids.hrc>
#include <sal/macros.h>
#include <sal/log.hxx>
#include <vcl/fontcharmap.hxx>
#include <vcl/outdev.hxx>
#include <document.hxx>
#include <documentimport.hxx>
#include <docpool.hxx>
#include <attrib.hxx>
#include <patattr.hxx>
#include <stlpool.hxx>
#include <stlsheet.hxx>
#include <globstr.hrc>
#include <scresid.hxx>
#include <attarray.hxx>
#include <xladdress.hxx>
#include <xlcontent.hxx>
#include <xltracer.hxx>
#include <xltools.hxx>
#include <xistream.hxx>
#include <xicontent.hxx>

#include <root.hxx>
#include <colrowst.hxx>

#include <vector>

#include <cppuhelper/implbase.hxx>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/frame/XModel.hpp>

using ::std::vector;
using namespace ::com::sun::star;

typedef ::cppu::WeakImplHelper< container::XIndexAccess > XIndexAccess_BASE;
typedef ::std::vector< Color > ColorVec;

namespace {

class PaletteIndex : public XIndexAccess_BASE
{
public:
    explicit PaletteIndex( const ColorVec& rColorTable ) : maColor( rColorTable ) {}

    // Methods XIndexAccess
    virtual ::sal_Int32 SAL_CALL getCount() override
    {
         return  maColor.size();
    }

    virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
    {
        //--Index;  // apparently the palette is already 1 based
        return uno::makeAny( sal_Int32( maColor[ Index ] ) );
    }

    // Methods XElementAccess
    virtual uno::Type SAL_CALL getElementType() override
    {
        return ::cppu::UnoType<sal_Int32>::get();
    }
    virtual sal_Bool SAL_CALL hasElements() override
    {
        return (!maColor.empty());
    }

private:
    ColorVec        maColor;
};

}

void
XclImpPalette::ExportPalette()
{
    if( SfxObjectShell* pDocShell = mrRoot.GetDocShell() )
    {
        // copy values in color palette
        sal_Int16 nColors =  maColorTable.size();
        ColorVec aColors;
        aColors.resize( nColors );
        for( sal_uInt16 nIndex = 0; nIndex < nColors; ++nIndex )
            aColors[ nIndex ] = GetColor( nIndex );

        uno::Reference< beans::XPropertySet > xProps( pDocShell->GetModel(), uno::UNO_QUERY );
        if ( xProps.is() )
        {
            uno::Reference< container::XIndexAccess > xIndex( new PaletteIndex( aColors ) );
            xProps->setPropertyValue( "ColorPalette", uno::makeAny( xIndex ) );
        }
    }

}
// PALETTE record - color information =========================================

XclImpPalette::XclImpPalette( const XclImpRoot& rRoot ) :
    XclDefaultPalette( rRoot ), mrRoot( rRoot )
{
}

void XclImpPalette::Initialize()
{
    maColorTable.clear();
}

Color XclImpPalette::GetColor( sal_uInt16 nXclIndex ) const
{
    if( nXclIndex >= EXC_COLOR_USEROFFSET )
    {
        sal_uInt32 nIx = nXclIndex - EXC_COLOR_USEROFFSET;
        if( nIx < maColorTable.size() )
            return maColorTable[ nIx ];
    }
    return GetDefColor( nXclIndex );
}

void XclImpPalette::ReadPalette( XclImpStream& rStrm )
{
    sal_uInt16 nCount;
    nCount = rStrm.ReaduInt16();

    const size_t nMinRecordSize = 4;
    const size_t nMaxRecords = rStrm.GetRecLeft() / nMinRecordSize;
    if (nCount > nMaxRecords)
    {
        SAL_WARN("sc", "Parsing error: " << nMaxRecords <<
                 " max possible entries, but " << nCount << " claimed, truncating");
        nCount = nMaxRecords;
    }

    maColorTable.resize( nCount );
    Color aColor;
    for( sal_uInt16 nIndex = 0; nIndex < nCount; ++nIndex )
    {
        rStrm >> aColor;
        maColorTable[ nIndex ] = aColor;
    }
    ExportPalette();
}

// FONT record - font information =============================================
XclImpFont::XclImpFont( const XclImpRoot& rRoot ) :
    XclImpRoot( rRoot ),
    mbHasCharSet( false ),
    mbHasWstrn( true ),
    mbHasAsian( false ),
    mbHasCmplx( false )
{
    SetAllUsedFlags( false );
}

XclImpFont::XclImpFont( const XclImpRoot& rRoot, const XclFontData& rFontData ) :
    XclImpRoot( rRoot )
{
    SetFontData( rFontData, false );
}

void XclImpFont::SetAllUsedFlags( bool bUsed )
{
    mbFontNameUsed = mbHeightUsed = mbColorUsed = mbWeightUsed = mbEscapemUsed =
        mbUnderlUsed = mbItalicUsed = mbStrikeUsed = mbOutlineUsed = mbShadowUsed = bUsed;
}

void XclImpFont::SetFontData( const XclFontData& rFontData, bool bHasCharSet )
{
    maData = rFontData;
    mbHasCharSet = bHasCharSet;
    if( !maData.maStyle.isEmpty() )
    {
        if( SfxObjectShell* pDocShell = GetDocShell() )
        {
            if( const SvxFontListItem* pInfoItem = static_cast< const SvxFontListItem* >(
                pDocShell->GetItem( SID_ATTR_CHAR_FONTLIST ) ) )
            {
                if( const FontList* pFontList = pInfoItem->GetFontList() )
                {
                    FontMetric aFontMetric( pFontList->Get( maData.maName, maData.maStyle ) );
                    maData.SetScWeight( aFontMetric.GetWeight() );
                    maData.SetScPosture( aFontMetric.GetItalic() );
                }
            }
        }
        maData.maStyle.clear();
    }
    GuessScriptType();
    SetAllUsedFlags( true );
}

rtl_TextEncoding XclImpFont::GetFontEncoding() const
{
    // #i63105# use text encoding from FONT record
    // #i67768# BIFF2-BIFF4 FONT records do not contain character set
    rtl_TextEncoding eFontEnc = mbHasCharSet ? maData.GetFontEncoding() : GetTextEncoding();
    return (eFontEnc == RTL_TEXTENCODING_DONTKNOW) ? GetTextEncoding() : eFontEnc;
}

void XclImpFont::ReadFont( XclImpStream& rStrm )
{
    switch( GetBiff() )
    {
        case EXC_BIFF2:
            ReadFontData2( rStrm );
            ReadFontName2( rStrm );
        break;
        case EXC_BIFF3:
        case EXC_BIFF4:
            ReadFontData2( rStrm );
            ReadFontColor( rStrm );
            ReadFontName2( rStrm );
        break;
        case EXC_BIFF5:
            ReadFontData5( rStrm );
            ReadFontName2( rStrm );
        break;
        case EXC_BIFF8:
            ReadFontData5( rStrm );
            ReadFontName8( rStrm );
        break;
        default:
            DBG_ERROR_BIFF();
            return;
    }
    GuessScriptType();
    SetAllUsedFlags( true );
}

void XclImpFont::ReadEfont( XclImpStream& rStrm )
{
    ReadFontColor( rStrm );
}

void XclImpFont::ReadCFFontBlock( XclImpStream& rStrm )
{
    OSL_ENSURE_BIFF( GetBiff() == EXC_BIFF8 );
    if( GetBiff() != EXC_BIFF8 )
        return;

    rStrm.Ignore( 64 );
    sal_uInt32 nHeight = rStrm.ReaduInt32();
    sal_uInt32 nStyle = rStrm.ReaduInt32();
    sal_uInt16 nWeight = rStrm.ReaduInt16();
    rStrm.Ignore( 2 ); //nEscapem
    sal_uInt8 nUnderl = rStrm.ReaduInt8();
    rStrm.Ignore( 3 );
    sal_uInt32 nColor = rStrm.ReaduInt32();
    rStrm.Ignore( 4 );
    sal_uInt32 nFontFlags1 = rStrm.ReaduInt32();
    rStrm.Ignore( 4 ); //nFontFlags2
    sal_uInt32 nFontFlags3 = rStrm.ReaduInt32();
    rStrm.Ignore( 18 );

    if( (mbHeightUsed = (nHeight <= 0x7FFF)) )
        maData.mnHeight = static_cast< sal_uInt16 >( nHeight );
    if( (mbWeightUsed = !::get_flag( nFontFlags1, EXC_CF_FONT_STYLE ) && (nWeight < 0x7FFF)) )
        maData.mnWeight = nWeight;
    if( (mbItalicUsed = !::get_flag( nFontFlags1, EXC_CF_FONT_STYLE )) )
        maData.mbItalic = ::get_flag( nStyle, EXC_CF_FONT_STYLE );
    if( (mbUnderlUsed = !::get_flag( nFontFlags3, EXC_CF_FONT_UNDERL ) && (nUnderl <= 0x7F)) )
        maData.mnUnderline = nUnderl;
    if( (mbColorUsed = (nColor <= 0x7FFF)) )
        maData.maColor = GetPalette().GetColor( static_cast< sal_uInt16 >( nColor ) );
    if( (mbStrikeUsed = !::get_flag( nFontFlags1, EXC_CF_FONT_STRIKEOUT )) )
        maData.mbStrikeout = ::get_flag( nStyle, EXC_CF_FONT_STRIKEOUT );
}

void XclImpFont::FillToItemSet( SfxItemSet& rItemSet, XclFontItemType eType, bool bSkipPoolDefs ) const
{
    // true = edit engine Which-IDs (EE_CHAR_*); false = Calc Which-IDs (ATTR_*)
    bool bEE = eType != XclFontItemType::Cell;

// item = the item to put into the item set
// sc_which = the Calc Which-ID of the item
// ee_which = the edit engine Which-ID of the item
#define PUTITEM( item, sc_which, ee_which ) \
    ScfTools::PutItem( rItemSet, item, (bEE ? (static_cast<sal_uInt16>(ee_which)) : (sc_which)), bSkipPoolDefs )

// Font item
    if( mbFontNameUsed )
    {
        rtl_TextEncoding eFontEnc = maData.GetFontEncoding();
        rtl_TextEncoding eTempTextEnc = (bEE && (eFontEnc == GetTextEncoding())) ?
            ScfTools::GetSystemTextEncoding() : eFontEnc;

        //add corresponding pitch for FontFamily
        FontPitch ePitch = PITCH_DONTKNOW;
        FontFamily eFtFamily = maData.GetScFamily( GetTextEncoding() );
        switch( eFtFamily ) //refer http://msdn.microsoft.com/en-us/library/aa246306(v=VS.60).aspx
        {
            case FAMILY_ROMAN:              ePitch = PITCH_VARIABLE;        break;
            case FAMILY_SWISS:              ePitch = PITCH_VARIABLE;        break;
            case FAMILY_MODERN:             ePitch = PITCH_FIXED;           break;
            default:                        break;
         }
        SvxFontItem aFontItem( eFtFamily , maData.maName, EMPTY_OUSTRING, ePitch, eTempTextEnc, ATTR_FONT );

        // set only for valid script types
        if( mbHasWstrn )
            PUTITEM( aFontItem, ATTR_FONT,      EE_CHAR_FONTINFO );
        if( mbHasAsian )
            PUTITEM( aFontItem, ATTR_CJK_FONT,  EE_CHAR_FONTINFO_CJK );
        if( mbHasCmplx )
            PUTITEM( aFontItem, ATTR_CTL_FONT,  EE_CHAR_FONTINFO_CTL );
    }

// Font height (for all script types)
    if( mbHeightUsed )
    {
        sal_Int32 nHeight = maData.mnHeight;
        if( bEE && (eType != XclFontItemType::HeaderFooter) )     // do not convert header/footer height
            nHeight = (nHeight * 127 + 36) / EXC_POINTS_PER_INCH;   // 1 in == 72 pt

        SvxFontHeightItem aHeightItem( nHeight, 100, ATTR_FONT_HEIGHT );
        PUTITEM( aHeightItem,   ATTR_FONT_HEIGHT,       EE_CHAR_FONTHEIGHT );
        PUTITEM( aHeightItem,   ATTR_CJK_FONT_HEIGHT,   EE_CHAR_FONTHEIGHT_CJK );
        PUTITEM( aHeightItem,   ATTR_CTL_FONT_HEIGHT,   EE_CHAR_FONTHEIGHT_CTL );
    }

// Font color - pass AUTO_COL to item
    if( mbColorUsed )
        PUTITEM( SvxColorItem( maData.maColor, ATTR_FONT_COLOR  ), ATTR_FONT_COLOR, EE_CHAR_COLOR );

// Font weight (for all script types)
    if( mbWeightUsed )
    {
        SvxWeightItem aWeightItem( maData.GetScWeight(), ATTR_FONT_WEIGHT );
        PUTITEM( aWeightItem,   ATTR_FONT_WEIGHT,       EE_CHAR_WEIGHT );
        PUTITEM( aWeightItem,   ATTR_CJK_FONT_WEIGHT,   EE_CHAR_WEIGHT_CJK );
        PUTITEM( aWeightItem,   ATTR_CTL_FONT_WEIGHT,   EE_CHAR_WEIGHT_CTL );
    }

// Font underline
    if( mbUnderlUsed )
    {
        SvxUnderlineItem aUnderlItem( maData.GetScUnderline(), ATTR_FONT_UNDERLINE );
        PUTITEM( aUnderlItem,   ATTR_FONT_UNDERLINE,    EE_CHAR_UNDERLINE );
    }

// Font posture (for all script types)
    if( mbItalicUsed )
    {
        SvxPostureItem aPostItem( maData.GetScPosture(), ATTR_FONT_POSTURE );
        PUTITEM( aPostItem, ATTR_FONT_POSTURE,      EE_CHAR_ITALIC );
        PUTITEM( aPostItem, ATTR_CJK_FONT_POSTURE,  EE_CHAR_ITALIC_CJK );
        PUTITEM( aPostItem, ATTR_CTL_FONT_POSTURE,  EE_CHAR_ITALIC_CTL );
    }

// Boolean attributes crossed out, contoured, shadowed
    if( mbStrikeUsed )
        PUTITEM( SvxCrossedOutItem( maData.GetScStrikeout(), ATTR_FONT_CROSSEDOUT ), ATTR_FONT_CROSSEDOUT, EE_CHAR_STRIKEOUT );
    if( mbOutlineUsed )
        PUTITEM( SvxContourItem( maData.mbOutline, ATTR_FONT_CONTOUR ), ATTR_FONT_CONTOUR, EE_CHAR_OUTLINE );
    if( mbShadowUsed )
        PUTITEM( SvxShadowedItem( maData.mbShadow, ATTR_FONT_SHADOWED ), ATTR_FONT_SHADOWED, EE_CHAR_SHADOW );

// Super-/subscript: only on edit engine objects
    if( mbEscapemUsed && bEE )
        rItemSet.Put( SvxEscapementItem( maData.GetScEscapement(), EE_CHAR_ESCAPEMENT ) );

#undef PUTITEM
}

void XclImpFont::WriteFontProperties( ScfPropertySet& rPropSet,
        XclFontPropSetType eType, const Color* pFontColor ) const
{
    GetFontPropSetHelper().WriteFontProperties(
        rPropSet, eType, maData, mbHasWstrn, mbHasAsian, mbHasCmplx, pFontColor );
}

void XclImpFont::ReadFontData2( XclImpStream& rStrm )
{
    sal_uInt16 nFlags;
    maData.mnHeight = rStrm.ReaduInt16();
    nFlags = rStrm.ReaduInt16();

    maData.mnWeight     = ::get_flagvalue( nFlags, EXC_FONTATTR_BOLD, EXC_FONTWGHT_BOLD, EXC_FONTWGHT_NORMAL );
    maData.mnUnderline  = ::get_flagvalue( nFlags, EXC_FONTATTR_UNDERLINE, EXC_FONTUNDERL_SINGLE, EXC_FONTUNDERL_NONE );
    maData.mbItalic     = ::get_flag( nFlags, EXC_FONTATTR_ITALIC );
    maData.mbStrikeout  = ::get_flag( nFlags, EXC_FONTATTR_STRIKEOUT );
    maData.mbOutline    = ::get_flag( nFlags, EXC_FONTATTR_OUTLINE );
    maData.mbShadow     = ::get_flag( nFlags, EXC_FONTATTR_SHADOW );
    mbHasCharSet = false;
}

void XclImpFont::ReadFontData5( XclImpStream& rStrm )
{
    sal_uInt16 nFlags;

    maData.mnHeight = rStrm.ReaduInt16();
    nFlags = rStrm.ReaduInt16();
    ReadFontColor( rStrm );
    maData.mnWeight  = rStrm.ReaduInt16();
    maData.mnEscapem = rStrm.ReaduInt16();
    maData.mnUnderline = rStrm.ReaduInt8();
    maData.mnFamily = rStrm.ReaduInt8();
    maData.mnCharSet = rStrm.ReaduInt8();
    rStrm.Ignore( 1 );

    maData.mbItalic     = ::get_flag( nFlags, EXC_FONTATTR_ITALIC );
    maData.mbStrikeout  = ::get_flag( nFlags, EXC_FONTATTR_STRIKEOUT );
    maData.mbOutline    = ::get_flag( nFlags, EXC_FONTATTR_OUTLINE );
    maData.mbShadow     = ::get_flag( nFlags, EXC_FONTATTR_SHADOW );
    mbHasCharSet = maData.mnCharSet != 0;
}

void XclImpFont::ReadFontColor( XclImpStream& rStrm )
{
    maData.maColor = GetPalette().GetColor( rStrm.ReaduInt16() );
}

void XclImpFont::ReadFontName2( XclImpStream& rStrm )
{
    maData.maName = rStrm.ReadByteString( false );
}

void XclImpFont::ReadFontName8( XclImpStream& rStrm )
{
    maData.maName = rStrm.ReadUniString( rStrm.ReaduInt8() );
}

void XclImpFont::GuessScriptType()
{
    mbHasWstrn = true;
    mbHasAsian = mbHasCmplx = false;

    // find the script types for which the font contains characters
    if( OutputDevice* pPrinter = GetPrinter() )
    {
        vcl::Font aFont( maData.maName, Size( 0, 10 ) );
        FontCharMapRef xFontCharMap;

        pPrinter->SetFont( aFont );
        if( pPrinter->GetFontCharMap( xFontCharMap ) )
        {
            // CJK fonts
            mbHasAsian =
                xFontCharMap->HasChar( 0x3041 ) ||   // 3040-309F: Hiragana
                xFontCharMap->HasChar( 0x30A1 ) ||   // 30A0-30FF: Katakana
                xFontCharMap->HasChar( 0x3111 ) ||   // 3100-312F: Bopomofo
                xFontCharMap->HasChar( 0x3131 ) ||   // 3130-318F: Hangul Compatibility Jamo
                xFontCharMap->HasChar( 0x3301 ) ||   // 3300-33FF: CJK Compatibility
                xFontCharMap->HasChar( 0x3401 ) ||   // 3400-4DBF: CJK Unified Ideographs Extension A
                xFontCharMap->HasChar( 0x4E01 ) ||   // 4E00-9FFF: CJK Unified Ideographs
                xFontCharMap->HasChar( 0x7E01 ) ||   // 4E00-9FFF: CJK Unified Ideographs
                xFontCharMap->HasChar( 0xA001 ) ||   // A001-A48F: Yi Syllables
                xFontCharMap->HasChar( 0xAC01 ) ||   // AC00-D7AF: Hangul Syllables
                xFontCharMap->HasChar( 0xCC01 ) ||   // AC00-D7AF: Hangul Syllables
                xFontCharMap->HasChar( 0xF901 ) ||   // F900-FAFF: CJK Compatibility Ideographs
                xFontCharMap->HasChar( 0xFF71 );     // FF00-FFEF: Halfwidth/Fullwidth Forms
            // CTL fonts
            mbHasCmplx =
                xFontCharMap->HasChar( 0x05D1 ) ||   // 0590-05FF: Hebrew
                xFontCharMap->HasChar( 0x0631 ) ||   // 0600-06FF: Arabic
                xFontCharMap->HasChar( 0x0721 ) ||   // 0700-074F: Syriac
                xFontCharMap->HasChar( 0x0911 ) ||   // 0900-0DFF: Indic scripts
                xFontCharMap->HasChar( 0x0E01 ) ||   // 0E00-0E7F: Thai
                xFontCharMap->HasChar( 0xFB21 ) ||   // FB1D-FB4F: Hebrew Presentation Forms
                xFontCharMap->HasChar( 0xFB51 ) ||   // FB50-FDFF: Arabic Presentation Forms-A
                xFontCharMap->HasChar( 0xFE71 );     // FE70-FEFF: Arabic Presentation Forms-B
            // Western fonts
            mbHasWstrn = (!mbHasAsian && !mbHasCmplx) || xFontCharMap->HasChar( 'A' );
        }
    }
}

XclImpFontBuffer::XclImpFontBuffer( const XclImpRoot& rRoot ) :
    XclImpRoot( rRoot ),
    maFont4( rRoot ),
    maCtrlFont( rRoot )
{
    Initialize();

    // default font for form controls without own font information
    XclFontData aCtrlFontData;
    switch( GetBiff() )
    {
        case EXC_BIFF2:
        case EXC_BIFF3:
        case EXC_BIFF4:
        case EXC_BIFF5:
            aCtrlFontData.maName = "Helv";
            aCtrlFontData.mnHeight = 160;
            aCtrlFontData.mnWeight = EXC_FONTWGHT_BOLD;
        break;
        case EXC_BIFF8:
            aCtrlFontData.maName = "Tahoma";
            aCtrlFontData.mnHeight = 160;
            aCtrlFontData.mnWeight = EXC_FONTWGHT_NORMAL;
        break;
        default:
            DBG_ERROR_BIFF();
    }
    maCtrlFont.SetFontData( aCtrlFontData, false );
}

void XclImpFontBuffer::Initialize()
{
    maFontList.clear();

    // application font for column width calculation, later filled with first font from font list
    XclFontData aAppFontData;
    aAppFontData.maName = "Arial";
    aAppFontData.mnHeight = 200;
    aAppFontData.mnWeight = EXC_FONTWGHT_NORMAL;
    UpdateAppFont( aAppFontData, false );
}

const XclImpFont* XclImpFontBuffer::GetFont( sal_uInt16 nFontIndex ) const
{
    /*  Font with index 4 is not stored in an Excel file, but used e.g. by
        BIFF5 form pushbutton objects. It is the bold default font.
        This also means that entries above 4 are out by one in the list. */

    if (nFontIndex == 4)
        return &maFont4;

    if (nFontIndex < 4)
    {
        // Font ID is zero-based when it's less than 4.
        return nFontIndex >= maFontList.size() ? nullptr : &maFontList[nFontIndex];
    }

    // Font ID is greater than 4.  It is now 1-based.
    return nFontIndex > maFontList.size() ? nullptr : &maFontList[nFontIndex-1];
}

void XclImpFontBuffer::ReadFont( XclImpStream& rStrm )
{
    maFontList.emplace_back( GetRoot() );
    XclImpFont& rFont = maFontList.back();
    rFont.ReadFont( rStrm );

    if( maFontList.size() == 1 )
    {
        UpdateAppFont( rFont.GetFontData(), rFont.HasCharSet() );
    }
}

void XclImpFontBuffer::ReadEfont( XclImpStream& rStrm )
{
    if( !maFontList.empty() )
        maFontList.back().ReadEfont( rStrm );
}

void XclImpFontBuffer::FillToItemSet(
        SfxItemSet& rItemSet, XclFontItemType eType,
        sal_uInt16 nFontIdx, bool bSkipPoolDefs ) const
{
    if( const XclImpFont* pFont = GetFont( nFontIdx ) )
        pFont->FillToItemSet( rItemSet, eType, bSkipPoolDefs );
}

void XclImpFontBuffer::WriteFontProperties( ScfPropertySet& rPropSet,
        XclFontPropSetType eType, sal_uInt16 nFontIdx, const Color* pFontColor ) const
{
    if( const XclImpFont* pFont = GetFont( nFontIdx ) )
        pFont->WriteFontProperties( rPropSet, eType, pFontColor );
}

void XclImpFontBuffer::WriteDefaultCtrlFontProperties( ScfPropertySet& rPropSet ) const
{
    maCtrlFont.WriteFontProperties( rPropSet, EXC_FONTPROPSET_CONTROL );
}

void XclImpFontBuffer::UpdateAppFont( const XclFontData& rFontData, bool bHasCharSet )
{
    maAppFont = rFontData;
    // #i3006# Calculate the width of '0' from first font and current printer.
    SetCharWidth( maAppFont );

    // font 4 is bold font 0
    XclFontData aFont4Data( maAppFont );
    aFont4Data.mnWeight = EXC_FONTWGHT_BOLD;
    maFont4.SetFontData( aFont4Data, bHasCharSet );
}

// FORMAT record - number formats =============================================

XclImpNumFmtBuffer::XclImpNumFmtBuffer( const XclImpRoot& rRoot ) :
    XclNumFmtBuffer( rRoot ),
    XclImpRoot( rRoot ),
    mnNextXclIdx( 0 )
{
}

void XclImpNumFmtBuffer::Initialize()
{
    maIndexMap.clear();
    mnNextXclIdx = 0;
    InitializeImport();     // base class
}

void XclImpNumFmtBuffer::ReadFormat( XclImpStream& rStrm )
{
    OUString aFormat;
    switch( GetBiff() )
    {
        case EXC_BIFF2:
        case EXC_BIFF3:
            aFormat = rStrm.ReadByteString( false );
        break;

        case EXC_BIFF4:
            rStrm.Ignore( 2 );  // in BIFF4 the index field exists, but is undefined
            aFormat = rStrm.ReadByteString( false );
        break;

        case EXC_BIFF5:
            mnNextXclIdx = rStrm.ReaduInt16();
            aFormat = rStrm.ReadByteString( false );
        break;

        case EXC_BIFF8:
            mnNextXclIdx = rStrm.ReaduInt16();
            aFormat = rStrm.ReadUniString();
        break;

        default:
            DBG_ERROR_BIFF();
            return;
    }

    if( mnNextXclIdx < 0xFFFF )
    {
        InsertFormat( mnNextXclIdx, aFormat );
        ++mnNextXclIdx;
    }
}

sal_uInt16 XclImpNumFmtBuffer::ReadCFFormat( XclImpStream& rStrm, bool bIFmt )
{
    // internal number format ?
    if(bIFmt)
    {
        rStrm.Ignore(1);
        sal_uInt8 nIndex;
        nIndex = rStrm.ReaduInt8();
        return nIndex;
    }
    else
    {
        OUString aFormat = rStrm.ReadUniString();
        InsertFormat( mnNextXclIdx, aFormat );
        ++mnNextXclIdx;
        return mnNextXclIdx - 1;
    }
}

void XclImpNumFmtBuffer::CreateScFormats()
{
    OSL_ENSURE( maIndexMap.empty(), "XclImpNumFmtBuffer::CreateScFormats - already created" );

    SvNumberFormatter& rFormatter = GetFormatter();
    for( const auto& [rXclNumFmt, rNumFmt] : GetFormatMap() )
    {
        // insert/convert the Excel number format
        sal_Int32 nCheckPos;
        SvNumFormatType nType = SvNumFormatType::DEFINED;
        sal_uInt32 nKey;
        if( !rNumFmt.maFormat.isEmpty() )
        {
            OUString aFormat( rNumFmt.maFormat );
            rFormatter.PutandConvertEntry( aFormat, nCheckPos,
                                           nType, nKey, LANGUAGE_ENGLISH_US, rNumFmt.meLanguage, false);
        }
        else
            nKey = rFormatter.GetFormatIndex( rNumFmt.meOffset, rNumFmt.meLanguage );

        // insert the resulting format key into the Excel->Calc index map
        maIndexMap[ rXclNumFmt ] = nKey;
    }
}

sal_uInt32 XclImpNumFmtBuffer::GetScFormat( sal_uInt16 nXclNumFmt ) const
{
    XclImpIndexMap::const_iterator aIt = maIndexMap.find( nXclNumFmt );
    return (aIt != maIndexMap.end()) ? aIt->second : NUMBERFORMAT_ENTRY_NOT_FOUND;
}

void XclImpNumFmtBuffer::FillToItemSet( SfxItemSet& rItemSet, sal_uInt16 nXclNumFmt, bool bSkipPoolDefs ) const
{
    sal_uInt32 nScNumFmt = GetScFormat( nXclNumFmt );
    if( nScNumFmt == NUMBERFORMAT_ENTRY_NOT_FOUND )
        nScNumFmt = GetStdScNumFmt();
    FillScFmtToItemSet( rItemSet, nScNumFmt, bSkipPoolDefs );
}

void XclImpNumFmtBuffer::FillScFmtToItemSet( SfxItemSet& rItemSet, sal_uInt32 nScNumFmt, bool bSkipPoolDefs ) const
{
    OSL_ENSURE( nScNumFmt != NUMBERFORMAT_ENTRY_NOT_FOUND, "XclImpNumFmtBuffer::FillScFmtToItemSet - invalid number format" );
    ScfTools::PutItem( rItemSet, SfxUInt32Item( ATTR_VALUE_FORMAT, nScNumFmt ), bSkipPoolDefs );
    if( rItemSet.GetItemState( ATTR_VALUE_FORMAT, false ) == SfxItemState::SET )
        ScGlobal::AddLanguage( rItemSet, GetFormatter() );
}

// XF, STYLE record - Cell formatting =========================================

void XclImpCellProt::FillFromXF2( sal_uInt8 nNumFmt )
{
    mbLocked = ::get_flag( nNumFmt, EXC_XF2_LOCKED );
    mbHidden = ::get_flag( nNumFmt, EXC_XF2_HIDDEN );
}

void XclImpCellProt::FillFromXF3( sal_uInt16 nProt )
{
    mbLocked = ::get_flag( nProt, EXC_XF_LOCKED );
    mbHidden = ::get_flag( nProt, EXC_XF_HIDDEN );
}

void XclImpCellProt::FillToItemSet( SfxItemSet& rItemSet, bool bSkipPoolDefs ) const
{
    ScfTools::PutItem( rItemSet, ScProtectionAttr( mbLocked, mbHidden ), bSkipPoolDefs );
}

void XclImpCellAlign::FillFromXF2( sal_uInt8 nFlags )
{
    mnHorAlign = ::extract_value< sal_uInt8 >( nFlags, 0, 3 );
}

void XclImpCellAlign::FillFromXF3( sal_uInt16 nAlign )
{
    mnHorAlign = ::extract_value< sal_uInt8 >( nAlign, 0, 3 );
    mbLineBreak = ::get_flag( nAlign, EXC_XF_LINEBREAK );   // new in BIFF3
}

void XclImpCellAlign::FillFromXF4( sal_uInt16 nAlign )
{
    FillFromXF3( nAlign );
    mnVerAlign = ::extract_value< sal_uInt8 >( nAlign, 4, 2 );  // new in BIFF4
    mnOrient = ::extract_value< sal_uInt8 >( nAlign, 6, 2 );    // new in BIFF4
}

void XclImpCellAlign::FillFromXF5( sal_uInt16 nAlign )
{
    mnHorAlign = ::extract_value< sal_uInt8 >( nAlign, 0, 3 );
    mnVerAlign = ::extract_value< sal_uInt8 >( nAlign, 4, 3 );
    mbLineBreak = ::get_flag( nAlign, EXC_XF_LINEBREAK );
    mnOrient = ::extract_value< sal_uInt8 >( nAlign, 8, 2 );
}

void XclImpCellAlign::FillFromXF8( sal_uInt16 nAlign, sal_uInt16 nMiscAttrib )
{
    mnHorAlign = ::extract_value< sal_uInt8 >( nAlign, 0, 3 );
    mnVerAlign = ::extract_value< sal_uInt8 >( nAlign, 4, 3 );
    mbLineBreak = ::get_flag( nAlign, EXC_XF_LINEBREAK );
    mnRotation = ::extract_value< sal_uInt8 >( nAlign, 8, 8 );      // new in BIFF8
    mnIndent = ::extract_value< sal_uInt8 >( nMiscAttrib, 0, 4 );   // new in BIFF8
    mbShrink = ::get_flag( nMiscAttrib, EXC_XF8_SHRINK );           // new in BIFF8
    mnTextDir = ::extract_value< sal_uInt8 >( nMiscAttrib, 6, 2 );  // new in BIFF8
}

void XclImpCellAlign::FillFromCF( sal_uInt16 nAlign, sal_uInt16 nMiscAttrib )
{
    mnHorAlign = extract_value< sal_uInt8 >( nAlign, 0, 3 );
    mbLineBreak = get_flag< sal_uInt8 >( nAlign, EXC_XF_LINEBREAK );
    mnVerAlign = ::extract_value< sal_uInt8 >( nAlign, 4, 3 );
    mnRotation = ::extract_value< sal_uInt8 >( nAlign, 8, 8 );
    mnIndent = ::extract_value< sal_uInt8 >( nMiscAttrib, 0, 4 );
    mbShrink = ::get_flag( nMiscAttrib, EXC_XF8_SHRINK );
    mnTextDir = ::extract_value< sal_uInt8 >( nMiscAttrib, 6, 2 );
}

void XclImpCellAlign::FillToItemSet( SfxItemSet& rItemSet, const XclImpFont* pFont, bool bSkipPoolDefs ) const
{
    // horizontal alignment
    ScfTools::PutItem( rItemSet, SvxHorJustifyItem( GetScHorAlign(), ATTR_HOR_JUSTIFY ), bSkipPoolDefs );
    ScfTools::PutItem( rItemSet, SvxJustifyMethodItem( GetScHorJustifyMethod(), ATTR_HOR_JUSTIFY_METHOD ), bSkipPoolDefs );

    // text wrap (#i74508# always if vertical alignment is justified or distributed)
    bool bLineBreak = mbLineBreak || (mnVerAlign == EXC_XF_VER_JUSTIFY) || (mnVerAlign == EXC_XF_VER_DISTRIB);
    ScfTools::PutItem( rItemSet, ScLineBreakCell( bLineBreak ), bSkipPoolDefs );

    // vertical alignment
    ScfTools::PutItem( rItemSet, SvxVerJustifyItem( GetScVerAlign(), ATTR_VER_JUSTIFY ), bSkipPoolDefs );
    ScfTools::PutItem( rItemSet, SvxJustifyMethodItem( GetScVerJustifyMethod(), ATTR_VER_JUSTIFY_METHOD ), bSkipPoolDefs );

    // indent
    sal_uInt16 nScIndent = mnIndent * 200; // 1 Excel unit == 10 pt == 200 twips
    ScfTools::PutItem( rItemSet, ScIndentItem( nScIndent ), bSkipPoolDefs );

    // shrink to fit
    ScfTools::PutItem( rItemSet, ScShrinkToFitCell( mbShrink ), bSkipPoolDefs );

    // text orientation/rotation (BIFF2-BIFF7 sets mnOrient)
    sal_uInt8 nXclRot = (mnOrient == EXC_ORIENT_NONE) ? mnRotation : XclTools::GetXclRotFromOrient( mnOrient );
    bool bStacked = (nXclRot == EXC_ROT_STACKED);
    ScfTools::PutItem( rItemSet, ScVerticalStackCell( bStacked ), bSkipPoolDefs );
    // set an angle in the range from -90 to 90 degrees
    sal_Int32 nAngle = XclTools::GetScRotation( nXclRot, 0 );
    ScfTools::PutItem( rItemSet, ScRotateValueItem( nAngle ), bSkipPoolDefs );
    // set "Use asian vertical layout", if cell is stacked and font contains CKJ characters
    bool bAsianVert = bStacked && pFont && pFont->HasAsianChars();
    ScfTools::PutItem( rItemSet, SfxBoolItem( ATTR_VERTICAL_ASIAN, bAsianVert ), bSkipPoolDefs );

    // CTL text direction
    ScfTools::PutItem( rItemSet, SvxFrameDirectionItem( GetScFrameDir(), ATTR_WRITINGDIR ), bSkipPoolDefs );
}

XclImpCellBorder::XclImpCellBorder()
{
    SetUsedFlags( false, false );
}

void XclImpCellBorder::SetUsedFlags( bool bOuterUsed, bool bDiagUsed )
{
    mbLeftUsed = mbRightUsed = mbTopUsed = mbBottomUsed = bOuterUsed;
    mbDiagUsed = bDiagUsed;
}

void XclImpCellBorder::FillFromXF2( sal_uInt8 nFlags )
{
    mnLeftLine   = ::get_flagvalue( nFlags, EXC_XF2_LEFTLINE,   EXC_LINE_THIN, EXC_LINE_NONE );
    mnRightLine  = ::get_flagvalue( nFlags, EXC_XF2_RIGHTLINE,  EXC_LINE_THIN, EXC_LINE_NONE );
    mnTopLine    = ::get_flagvalue( nFlags, EXC_XF2_TOPLINE,    EXC_LINE_THIN, EXC_LINE_NONE );
    mnBottomLine = ::get_flagvalue( nFlags, EXC_XF2_BOTTOMLINE, EXC_LINE_THIN, EXC_LINE_NONE );
    mnLeftColor = mnRightColor = mnTopColor = mnBottomColor = EXC_COLOR_BIFF2_BLACK;
    SetUsedFlags( true, false );
}

void XclImpCellBorder::FillFromXF3( sal_uInt32 nBorder )
{
    mnTopLine     = ::extract_value< sal_uInt8  >( nBorder,  0, 3 );
    mnLeftLine    = ::extract_value< sal_uInt8  >( nBorder,  8, 3 );
    mnBottomLine  = ::extract_value< sal_uInt8  >( nBorder, 16, 3 );
    mnRightLine   = ::extract_value< sal_uInt8  >( nBorder, 24, 3 );
    mnTopColor    = ::extract_value< sal_uInt16 >( nBorder,  3, 5 );
    mnLeftColor   = ::extract_value< sal_uInt16 >( nBorder, 11, 5 );
    mnBottomColor = ::extract_value< sal_uInt16 >( nBorder, 19, 5 );
    mnRightColor  = ::extract_value< sal_uInt16 >( nBorder, 27, 5 );
    SetUsedFlags( true, false );
}

void XclImpCellBorder::FillFromXF5( sal_uInt32 nBorder, sal_uInt32 nArea )
{
    mnTopLine     = ::extract_value< sal_uInt8  >( nBorder,  0, 3 );
    mnLeftLine    = ::extract_value< sal_uInt8  >( nBorder,  3, 3 );
    mnBottomLine  = ::extract_value< sal_uInt8  >( nArea,   22, 3 );
    mnRightLine   = ::extract_value< sal_uInt8  >( nBorder,  6, 3 );
    mnTopColor    = ::extract_value< sal_uInt16 >( nBorder,  9, 7 );
    mnLeftColor   = ::extract_value< sal_uInt16 >( nBorder, 16, 7 );
    mnBottomColor = ::extract_value< sal_uInt16 >( nArea,   25, 7 );
    mnRightColor  = ::extract_value< sal_uInt16 >( nBorder, 23, 7 );
    SetUsedFlags( true, false );
}

void XclImpCellBorder::FillFromXF8( sal_uInt32 nBorder1, sal_uInt32 nBorder2 )
{
    mnLeftLine    = ::extract_value< sal_uInt8  >( nBorder1,  0, 4 );
    mnRightLine   = ::extract_value< sal_uInt8  >( nBorder1,  4, 4 );
    mnTopLine     = ::extract_value< sal_uInt8  >( nBorder1,  8, 4 );
    mnBottomLine  = ::extract_value< sal_uInt8  >( nBorder1, 12, 4 );
    mnLeftColor   = ::extract_value< sal_uInt16 >( nBorder1, 16, 7 );
    mnRightColor  = ::extract_value< sal_uInt16 >( nBorder1, 23, 7 );
    mnTopColor    = ::extract_value< sal_uInt16 >( nBorder2,  0, 7 );
    mnBottomColor = ::extract_value< sal_uInt16 >( nBorder2,  7, 7 );
    mbDiagTLtoBR  = ::get_flag( nBorder1, EXC_XF_DIAGONAL_TL_TO_BR );
    mbDiagBLtoTR  = ::get_flag( nBorder1, EXC_XF_DIAGONAL_BL_TO_TR );
    if( mbDiagTLtoBR || mbDiagBLtoTR )
    {
        mnDiagLine  = ::extract_value< sal_uInt8 >( nBorder2, 21, 4 );
        mnDiagColor = ::extract_value< sal_uInt16 >( nBorder2, 14, 7 );
    }
    SetUsedFlags( true, true );
}

void XclImpCellBorder::FillFromCF8( sal_uInt16 nLineStyle, sal_uInt32 nLineColor, sal_uInt32 nFlags )
{
    mnLeftLine    = ::extract_value< sal_uInt8  >( nLineStyle,  0, 4 );
    mnRightLine   = ::extract_value< sal_uInt8  >( nLineStyle,  4, 4 );
    mnTopLine     = ::extract_value< sal_uInt8  >( nLineStyle,  8, 4 );
    mnBottomLine  = ::extract_value< sal_uInt8  >( nLineStyle, 12, 4 );
    mnLeftColor   = ::extract_value< sal_uInt16 >( nLineColor,  0, 7 );
    mnRightColor  = ::extract_value< sal_uInt16 >( nLineColor,  7, 7 );
    mnTopColor    = ::extract_value< sal_uInt16 >( nLineColor, 16, 7 );
    mnBottomColor = ::extract_value< sal_uInt16 >( nLineColor, 23, 7 );
    mbLeftUsed    = !::get_flag( nFlags, EXC_CF_BORDER_LEFT );
    mbRightUsed   = !::get_flag( nFlags, EXC_CF_BORDER_RIGHT );
    mbTopUsed     = !::get_flag( nFlags, EXC_CF_BORDER_TOP );
    mbBottomUsed  = !::get_flag( nFlags, EXC_CF_BORDER_BOTTOM );
    mbDiagUsed    = false;
}

bool XclImpCellBorder::HasAnyOuterBorder() const
{
    return
        (mbLeftUsed   && (mnLeftLine != EXC_LINE_NONE)) ||
        (mbRightUsed  && (mnRightLine != EXC_LINE_NONE)) ||
        (mbTopUsed    && (mnTopLine != EXC_LINE_NONE)) ||
        (mbBottomUsed && (mnBottomLine != EXC_LINE_NONE));
}

namespace {

/** Converts the passed line style to a ::editeng::SvxBorderLine, or returns false, if style is "no line". */
bool lclConvertBorderLine( ::editeng::SvxBorderLine& rLine, const XclImpPalette& rPalette, sal_uInt8 nXclLine, sal_uInt16 nXclColor )
{
    static const sal_uInt16 ppnLineParam[][ 4 ] =
    {
        //  outer width,        type
        {   0,                  table::BorderLineStyle::SOLID        }, // 0 = none
        {   EXC_BORDER_THIN,    table::BorderLineStyle::SOLID        }, // 1 = thin
        {   EXC_BORDER_MEDIUM,  table::BorderLineStyle::SOLID        }, // 2 = medium
        {   EXC_BORDER_THIN,    table::BorderLineStyle::FINE_DASHED  }, // 3 = dashed
        {   EXC_BORDER_THIN,    table::BorderLineStyle::DOTTED       }, // 4 = dotted
        {   EXC_BORDER_THICK,   table::BorderLineStyle::SOLID        }, // 5 = thick
        {   EXC_BORDER_THICK,   table::BorderLineStyle::DOUBLE_THIN  }, // 6 = double
        {   EXC_BORDER_HAIR,    table::BorderLineStyle::SOLID        }, // 7 = hair
        {   EXC_BORDER_MEDIUM,  table::BorderLineStyle::DASHED       }, // 8 = med dash
        {   EXC_BORDER_THIN,    table::BorderLineStyle::DASH_DOT     }, // 9 = thin dashdot
        {   EXC_BORDER_MEDIUM,  table::BorderLineStyle::DASH_DOT     }, // A = med dashdot
        {   EXC_BORDER_THIN,    table::BorderLineStyle::DASH_DOT_DOT }, // B = thin dashdotdot
        {   EXC_BORDER_MEDIUM,  table::BorderLineStyle::DASH_DOT_DOT }, // C = med dashdotdot
        {   EXC_BORDER_MEDIUM,  table::BorderLineStyle::DASH_DOT     }  // D = med slant dashdot
    };

    if( nXclLine == EXC_LINE_NONE )
        return false;
    if( nXclLine >= SAL_N_ELEMENTS( ppnLineParam ) )
        nXclLine = EXC_LINE_THIN;

    rLine.SetColor( rPalette.GetColor( nXclColor ) );
    rLine.SetWidth( ppnLineParam[ nXclLine ][ 0 ] );
    rLine.SetBorderLineStyle( static_cast< SvxBorderLineStyle>(
                ppnLineParam[ nXclLine ][ 1 ]) );
    return true;
}

} // namespace

void XclImpCellBorder::FillToItemSet( SfxItemSet& rItemSet, const XclImpPalette& rPalette, bool bSkipPoolDefs ) const
{
    if( mbLeftUsed || mbRightUsed || mbTopUsed || mbBottomUsed )
    {
        SvxBoxItem aBoxItem( ATTR_BORDER );
        ::editeng::SvxBorderLine aLine;
        if( mbLeftUsed && lclConvertBorderLine( aLine, rPalette, mnLeftLine, mnLeftColor ) )
            aBoxItem.SetLine( &aLine, SvxBoxItemLine::LEFT );
        if( mbRightUsed && lclConvertBorderLine( aLine, rPalette, mnRightLine, mnRightColor ) )
            aBoxItem.SetLine( &aLine, SvxBoxItemLine::RIGHT );
        if( mbTopUsed && lclConvertBorderLine( aLine, rPalette, mnTopLine, mnTopColor ) )
            aBoxItem.SetLine( &aLine, SvxBoxItemLine::TOP );
        if( mbBottomUsed && lclConvertBorderLine( aLine, rPalette, mnBottomLine, mnBottomColor ) )
            aBoxItem.SetLine( &aLine, SvxBoxItemLine::BOTTOM );
        ScfTools::PutItem( rItemSet, aBoxItem, bSkipPoolDefs );
    }
    if( mbDiagUsed )
    {
        SvxLineItem aTLBRItem( ATTR_BORDER_TLBR );
        SvxLineItem aBLTRItem( ATTR_BORDER_BLTR );
        ::editeng::SvxBorderLine aLine;
        if( lclConvertBorderLine( aLine, rPalette, mnDiagLine, mnDiagColor ) )
        {
            if( mbDiagTLtoBR )
                aTLBRItem.SetLine( &aLine );
            if( mbDiagBLtoTR )
                aBLTRItem.SetLine( &aLine );
        }
        ScfTools::PutItem( rItemSet, aTLBRItem, bSkipPoolDefs );
        ScfTools::PutItem( rItemSet, aBLTRItem, bSkipPoolDefs );
    }
}

XclImpCellArea::XclImpCellArea()
{
    SetUsedFlags( false );
}

void XclImpCellArea::SetUsedFlags( bool bUsed )
{
    mbForeUsed = mbBackUsed = mbPattUsed = bUsed;
}

void XclImpCellArea::FillFromXF2( sal_uInt8 nFlags )
{
    mnPattern = ::get_flagvalue( nFlags, EXC_XF2_BACKGROUND, EXC_PATT_12_5_PERC, EXC_PATT_NONE );
    mnForeColor = EXC_COLOR_BIFF2_BLACK;
    mnBackColor = EXC_COLOR_BIFF2_WHITE;
    SetUsedFlags( true );
}

void XclImpCellArea::FillFromXF3( sal_uInt16 nArea )
{
    mnPattern   = ::extract_value< sal_uInt8  >( nArea,  0, 6 );
    mnForeColor = ::extract_value< sal_uInt16 >( nArea,  6, 5 );
    mnBackColor = ::extract_value< sal_uInt16 >( nArea, 11, 5 );
    SetUsedFlags( true );
}

void XclImpCellArea::FillFromXF5( sal_uInt32 nArea )
{
    mnPattern   = ::extract_value< sal_uInt8  >( nArea, 16, 6 );
    mnForeColor = ::extract_value< sal_uInt16 >( nArea,  0, 7 );
    mnBackColor = ::extract_value< sal_uInt16 >( nArea,  7, 7 );
    SetUsedFlags( true );
}

void XclImpCellArea::FillFromXF8( sal_uInt32 nBorder2, sal_uInt16 nArea )
{
    mnPattern   = ::extract_value< sal_uInt8  >( nBorder2, 26, 6 );
    mnForeColor = ::extract_value< sal_uInt16 >( nArea,     0, 7 );
    mnBackColor = ::extract_value< sal_uInt16 >( nArea,     7, 7 );
    SetUsedFlags( true );
}

void XclImpCellArea::FillFromCF8( sal_uInt16 nPattern, sal_uInt16 nColor, sal_uInt32 nFlags )
{
    mnForeColor = ::extract_value< sal_uInt16 >( nColor,    0, 7 );
    mnBackColor = ::extract_value< sal_uInt16 >( nColor,    7, 7 );
    mnPattern   = ::extract_value< sal_uInt8  >( nPattern, 10, 6 );
    mbForeUsed  = !::get_flag( nFlags, EXC_CF_AREA_FGCOLOR );
    mbBackUsed  = !::get_flag( nFlags, EXC_CF_AREA_BGCOLOR );
    mbPattUsed  = !::get_flag( nFlags, EXC_CF_AREA_PATTERN );

    if( mbBackUsed && (!mbPattUsed || (mnPattern == EXC_PATT_SOLID)) )
    {
        mnForeColor = mnBackColor;
        mnPattern = EXC_PATT_SOLID;
        mbForeUsed = mbPattUsed = true;
    }
    else if( !mbBackUsed && mbPattUsed && (mnPattern == EXC_PATT_SOLID) )
    {
        mbPattUsed = false;
    }
}

void XclImpCellArea::FillToItemSet( SfxItemSet& rItemSet, const XclImpPalette& rPalette, bool bSkipPoolDefs ) const
{
    if( mbPattUsed )    // colors may be both unused in cond. formats
    {
        SvxBrushItem aBrushItem( ATTR_BACKGROUND );

        // do not use IsTransparent() - old Calc filter writes transparency with different color indexes
        if( mnPattern == EXC_PATT_NONE )
        {
            aBrushItem.SetColor( COL_TRANSPARENT );
        }
        else
        {
            Color aFore( rPalette.GetColor( mbForeUsed ? mnForeColor : EXC_COLOR_WINDOWTEXT ) );
            Color aBack( rPalette.GetColor( mbBackUsed ? mnBackColor : EXC_COLOR_WINDOWBACK ) );
            aBrushItem.SetColor( XclTools::GetPatternColor( aFore, aBack, mnPattern ) );
        }

        ScfTools::PutItem( rItemSet, aBrushItem, bSkipPoolDefs );
    }
}

XclImpXF::XclImpXF( const XclImpRoot& rRoot ) :
    XclXFBase( true ),      // default is cell XF
    XclImpRoot( rRoot ),
    mpStyleSheet( nullptr ),
    mnXclNumFmt( 0 ),
    mnXclFont( 0 )
{
}

XclImpXF::~XclImpXF()
{
}

void XclImpXF::ReadXF2( XclImpStream& rStrm )
{
    sal_uInt8 nReadFont, nReadNumFmt, nFlags;
    nReadFont = rStrm.ReaduInt8();
    rStrm.Ignore( 1 );
    nReadNumFmt = rStrm.ReaduInt8();
    nFlags = rStrm.ReaduInt8();

    // XF type always cell, no parent, used flags always true
    SetAllUsedFlags( true );

    // attributes
    maProtection.FillFromXF2( nReadNumFmt );
    mnXclFont = nReadFont;
    mnXclNumFmt = nReadNumFmt & EXC_XF2_VALFMT_MASK;
    maAlignment.FillFromXF2( nFlags );
    maBorder.FillFromXF2( nFlags );
    maArea.FillFromXF2( nFlags );
}

void XclImpXF::ReadXF3( XclImpStream& rStrm )
{
    sal_uInt32 nBorder;
    sal_uInt16 nTypeProt, nAlign, nArea;
    sal_uInt8 nReadFont, nReadNumFmt;
    nReadFont = rStrm.ReaduInt8();
    nReadNumFmt = rStrm.ReaduInt8();
    nTypeProt = rStrm.ReaduInt16();
    nAlign = rStrm.ReaduInt16();
    nArea = rStrm.ReaduInt16();
    nBorder = rStrm.ReaduInt32();

    // XF type/parent, attribute used flags
    mbCellXF = !::get_flag( nTypeProt, EXC_XF_STYLE );          // new in BIFF3
    mnParent = ::extract_value< sal_uInt16 >( nAlign, 4, 12 );  // new in BIFF3
    SetUsedFlags( ::extract_value< sal_uInt8 >( nTypeProt, 10, 6 ) );

    // attributes
    maProtection.FillFromXF3( nTypeProt );
    mnXclFont = nReadFont;
    mnXclNumFmt = nReadNumFmt;
    maAlignment.FillFromXF3( nAlign );
    maBorder.FillFromXF3( nBorder );
    maArea.FillFromXF3( nArea );                        // new in BIFF3
}

void XclImpXF::ReadXF4( XclImpStream& rStrm )
{
    sal_uInt32 nBorder;
    sal_uInt16 nTypeProt, nAlign, nArea;
    sal_uInt8 nReadFont, nReadNumFmt;
    nReadFont = rStrm.ReaduInt8();
    nReadNumFmt = rStrm.ReaduInt8();
    nTypeProt = rStrm.ReaduInt16();
    nAlign = rStrm.ReaduInt16();
    nArea = rStrm.ReaduInt16();
    nBorder = rStrm.ReaduInt32();

    // XF type/parent, attribute used flags
    mbCellXF = !::get_flag( nTypeProt, EXC_XF_STYLE );
    mnParent = ::extract_value< sal_uInt16 >( nTypeProt, 4, 12 );
    SetUsedFlags( ::extract_value< sal_uInt8 >( nAlign, 10, 6 ) );

    // attributes
    maProtection.FillFromXF3( nTypeProt );
    mnXclFont = nReadFont;
    mnXclNumFmt = nReadNumFmt;
    maAlignment.FillFromXF4( nAlign );
    maBorder.FillFromXF3( nBorder );
    maArea.FillFromXF3( nArea );
}

void XclImpXF::ReadXF5( XclImpStream& rStrm )
{
    sal_uInt32 nArea, nBorder;
    sal_uInt16 nTypeProt, nAlign;
    mnXclFont = rStrm.ReaduInt16();
    mnXclNumFmt = rStrm.ReaduInt16();
    nTypeProt = rStrm.ReaduInt16();
    nAlign = rStrm.ReaduInt16();
    nArea = rStrm.ReaduInt32();
    nBorder = rStrm.ReaduInt32();

    // XF type/parent, attribute used flags
    mbCellXF = !::get_flag( nTypeProt, EXC_XF_STYLE );
    mnParent = ::extract_value< sal_uInt16 >( nTypeProt, 4, 12 );
    SetUsedFlags( ::extract_value< sal_uInt8 >( nAlign, 10, 6 ) );

    // attributes
    maProtection.FillFromXF3( nTypeProt );
    maAlignment.FillFromXF5( nAlign );
    maBorder.FillFromXF5( nBorder, nArea );
    maArea.FillFromXF5( nArea );
}

void XclImpXF::ReadXF8( XclImpStream& rStrm )
{
    sal_uInt32 nBorder1, nBorder2;
    sal_uInt16 nTypeProt, nAlign, nMiscAttrib, nArea;
    mnXclFont = rStrm.ReaduInt16();
    mnXclNumFmt = rStrm.ReaduInt16();
    nTypeProt = rStrm.ReaduInt16();
    nAlign = rStrm.ReaduInt16();
    nMiscAttrib = rStrm.ReaduInt16();
    nBorder1 = rStrm.ReaduInt32();
    nBorder2 = rStrm.ReaduInt32(  );
    nArea = rStrm.ReaduInt16();

    // XF type/parent, attribute used flags
    mbCellXF = !::get_flag( nTypeProt, EXC_XF_STYLE );
    mnParent = ::extract_value< sal_uInt16 >( nTypeProt, 4, 12 );
    SetUsedFlags( ::extract_value< sal_uInt8 >( nMiscAttrib, 10, 6 ) );

    // attributes
    maProtection.FillFromXF3( nTypeProt );
    maAlignment.FillFromXF8( nAlign, nMiscAttrib );
    maBorder.FillFromXF8( nBorder1, nBorder2 );
    maArea.FillFromXF8( nBorder2, nArea );
}

void XclImpXF::ReadXF( XclImpStream& rStrm )
{
    switch( GetBiff() )
    {
        case EXC_BIFF2: ReadXF2( rStrm );   break;
        case EXC_BIFF3: ReadXF3( rStrm );   break;
        case EXC_BIFF4: ReadXF4( rStrm );   break;
        case EXC_BIFF5: ReadXF5( rStrm );   break;
        case EXC_BIFF8: ReadXF8( rStrm );   break;
        default:        DBG_ERROR_BIFF();
    }
}

const ScPatternAttr& XclImpXF::CreatePattern( bool bSkipPoolDefs )
{
    if( mpPattern )
        return *mpPattern;

    // create new pattern attribute set
    mpPattern.reset( new ScPatternAttr( GetDoc().GetPool() ) );
    SfxItemSet& rItemSet = mpPattern->GetItemSet();
    XclImpXF* pParentXF = IsCellXF() ? GetXFBuffer().GetXF( mnParent ) : nullptr;

    // parent cell style
    if( IsCellXF() && !mpStyleSheet )
    {
        mpStyleSheet = GetXFBuffer().CreateStyleSheet( mnParent );

        /*  Enables mb***Used flags, if the formatting attributes differ from
            the passed XF record. In cell XFs Excel uses the cell attributes,
            if they differ from the parent style XF.
            ...or if the respective flag is not set in parent style XF. */
        if( pParentXF )
        {
            if( !mbProtUsed )
                mbProtUsed = !pParentXF->mbProtUsed || !(maProtection == pParentXF->maProtection);
            if( !mbFontUsed )
                mbFontUsed = !pParentXF->mbFontUsed || (mnXclFont != pParentXF->mnXclFont);
            if( !mbFmtUsed )
                mbFmtUsed = !pParentXF->mbFmtUsed || (mnXclNumFmt != pParentXF->mnXclNumFmt);
            if( !mbAlignUsed )
                mbAlignUsed = !pParentXF->mbAlignUsed || !(maAlignment == pParentXF->maAlignment);
            if( !mbBorderUsed )
                mbBorderUsed = !pParentXF->mbBorderUsed || !(maBorder == pParentXF->maBorder);
            if( !mbAreaUsed )
                mbAreaUsed = !pParentXF->mbAreaUsed || !(maArea == pParentXF->maArea);
        }
    }

    // cell protection
    if( mbProtUsed )
        maProtection.FillToItemSet( rItemSet, bSkipPoolDefs );

    // font
    if( mbFontUsed )
        GetFontBuffer().FillToItemSet( rItemSet, XclFontItemType::Cell, mnXclFont, bSkipPoolDefs );

    // value format
    if( mbFmtUsed )
    {
        GetNumFmtBuffer().FillToItemSet( rItemSet, mnXclNumFmt, bSkipPoolDefs );
        // Trace occurrences of Windows date formats
        GetTracer().TraceDates( mnXclNumFmt );
    }

    // alignment
    if( mbAlignUsed )
        maAlignment.FillToItemSet( rItemSet, GetFontBuffer().GetFont( mnXclFont ), bSkipPoolDefs );

    // border
    if( mbBorderUsed )
    {
        maBorder.FillToItemSet( rItemSet, GetPalette(), bSkipPoolDefs );
        GetTracer().TraceBorderLineStyle(maBorder.mnLeftLine > EXC_LINE_HAIR ||
            maBorder.mnRightLine > EXC_LINE_HAIR || maBorder.mnTopLine > EXC_LINE_HAIR ||
            maBorder.mnBottomLine > EXC_LINE_HAIR );
    }

    // area
    if( mbAreaUsed )
    {
        maArea.FillToItemSet( rItemSet, GetPalette(), bSkipPoolDefs );
        GetTracer().TraceFillPattern(maArea.mnPattern != EXC_PATT_NONE &&
            maArea.mnPattern != EXC_PATT_SOLID);
    }

    /*  #i38709# Decide which rotation reference mode to use. If any outer
        border line of the cell is set (either explicitly or via cell style),
        and the cell contents are rotated, set rotation reference to bottom of
        cell. This causes the borders to be painted rotated with the text. */
    if( mbAlignUsed || mbBorderUsed )
    {
        SvxRotateMode eRotateMode = SVX_ROTATE_MODE_STANDARD;
        const XclImpCellAlign* pAlign = mbAlignUsed ? &maAlignment : (pParentXF ? &pParentXF->maAlignment : nullptr);
        const XclImpCellBorder* pBorder = mbBorderUsed ? &maBorder : (pParentXF ? &pParentXF->maBorder : nullptr);
        if( pAlign && pBorder && (0 < pAlign->mnRotation) && (pAlign->mnRotation <= 180) && pBorder->HasAnyOuterBorder() )
            eRotateMode = SVX_ROTATE_MODE_BOTTOM;
        ScfTools::PutItem( rItemSet, SvxRotateModeItem( eRotateMode, ATTR_ROTATE_MODE ), bSkipPoolDefs );
    }

    // Excel's cell margins are different from Calc's default margins.
    SvxMarginItem aItem(40, 40, 40, 40, ATTR_MARGIN);
    ScfTools::PutItem(rItemSet, aItem, bSkipPoolDefs);

    return *mpPattern;
}

void XclImpXF::ApplyPatternToAttrVector(
    std::vector<ScAttrEntry>& rAttrs, SCROW nRow1, SCROW nRow2, sal_uInt32 nForceScNumFmt)
{
    // force creation of cell style and hard formatting, do it here to have mpStyleSheet
    CreatePattern();
    ScPatternAttr& rPat = *mpPattern;

    // insert into document
    ScDocument& rDoc = GetDoc();

    if (IsCellXF())
    {
        if (mpStyleSheet)
        {
            // Apply style sheet.  Don't clear the direct formats.
            rPat.SetStyleSheet(mpStyleSheet, false);
        }
        else
        {
            // When the cell format is not associated with any style, use the
            // 'Default' style.  Some buggy XLS docs generated by apps other
            // than Excel (such as 1C) may not have any built-in styles at
            // all.
            ScStyleSheetPool* pStylePool = rDoc.GetStyleSheetPool();
            if (pStylePool)
            {
                ScStyleSheet* pStyleSheet = static_cast<ScStyleSheet*>(
                    pStylePool->Find(
                        ScResId(STR_STYLENAME_STANDARD_CELL), SfxStyleFamily::Para));

                if (pStyleSheet)
                    rPat.SetStyleSheet(pStyleSheet, false);
            }

        }
    }

    if (nForceScNumFmt != NUMBERFORMAT_ENTRY_NOT_FOUND)
    {
        ScPatternAttr aNumPat(rDoc.GetPool());
        GetNumFmtBuffer().FillScFmtToItemSet(aNumPat.GetItemSet(), nForceScNumFmt);
        rPat.GetItemSet().Put(aNumPat.GetItemSet());
    }

    // Make sure we skip unnamed styles.
    if (rPat.GetStyleName())
    {
        // Check for a gap between the last entry and this one.
        bool bHasGap = false;
        if (rAttrs.empty() && nRow1 > 0)
            // First attribute range doesn't start at row 0.
            bHasGap = true;

        if (!rAttrs.empty() && rAttrs.back().nEndRow + 1 < nRow1)
            bHasGap = true;

        if (bHasGap)
        {
            // Fill this gap with the default pattern.
            ScAttrEntry aEntry;
            aEntry.nEndRow = nRow1 - 1;
            aEntry.pPattern = rDoc.GetDefPattern();
            rAttrs.push_back(aEntry);
        }

        ScAttrEntry aEntry;
        aEntry.nEndRow = nRow2;
        aEntry.pPattern = &rDoc.GetPool()->Put(rPat);
        rAttrs.push_back(aEntry);
    }
}

void XclImpXF::ApplyPattern(
        SCCOL nScCol1, SCROW nScRow1, SCCOL nScCol2, SCROW nScRow2,
        SCTAB nScTab )
{
    // force creation of cell style and hard formatting, do it here to have mpStyleSheet
    const ScPatternAttr& rPattern = CreatePattern();

    // insert into document
    ScDocument& rDoc = GetDoc();
    if( IsCellXF() && mpStyleSheet )
        rDoc.ApplyStyleAreaTab( nScCol1, nScRow1, nScCol2, nScRow2, nScTab, *mpStyleSheet );
    if( HasUsedFlags() )
        rDoc.ApplyPatternAreaTab( nScCol1, nScRow1, nScCol2, nScRow2, nScTab, rPattern );

}

/*static*/ void XclImpXF::ApplyPatternForBiff2CellFormat( const XclImpRoot& rRoot,
        const ScAddress& rScPos, sal_uInt8 nFlags1, sal_uInt8 nFlags2, sal_uInt8 nFlags3 )
{
    /*  Create an XF object and let it do the work. We will have access to its
        private members here. */
    XclImpXF aXF( rRoot );

    // no used flags available in BIFF2 (always true)
    aXF.SetAllUsedFlags( true );

    // set the attributes
    aXF.maProtection.FillFromXF2( nFlags1 );
    aXF.maAlignment.FillFromXF2( nFlags3 );
    aXF.maBorder.FillFromXF2( nFlags3 );
    aXF.maArea.FillFromXF2( nFlags3 );
    aXF.mnXclNumFmt = ::extract_value< sal_uInt16 >( nFlags2, 0, 6 );
    aXF.mnXclFont = ::extract_value< sal_uInt16 >( nFlags2, 6, 2 );

    // write the attributes to the cell
    aXF.ApplyPattern( rScPos.Col(), rScPos.Row(), rScPos.Col(), rScPos.Row(), rScPos.Tab() );
}

void XclImpXF::SetUsedFlags( sal_uInt8 nUsedFlags )
{
    /*  Notes about finding the mb***Used flags:
        - In cell XFs a *set* bit means a used attribute.
        - In style XFs a *cleared* bit means a used attribute.
        The mb***Used members always store true, if the attribute is used.
        The "mbCellXF == ::get_flag(...)" construct evaluates to true in
        both mentioned cases: cell XF and set bit; or style XF and cleared bit.
     */
    mbProtUsed   = (mbCellXF == ::get_flag( nUsedFlags, EXC_XF_DIFF_PROT ));
    mbFontUsed   = (mbCellXF == ::get_flag( nUsedFlags, EXC_XF_DIFF_FONT ));
    mbFmtUsed    = (mbCellXF == ::get_flag( nUsedFlags, EXC_XF_DIFF_VALFMT ));
    mbAlignUsed  = (mbCellXF == ::get_flag( nUsedFlags, EXC_XF_DIFF_ALIGN ));
    mbBorderUsed = (mbCellXF == ::get_flag( nUsedFlags, EXC_XF_DIFF_BORDER ));
    mbAreaUsed   = (mbCellXF == ::get_flag( nUsedFlags, EXC_XF_DIFF_AREA ));
}

XclImpStyle::XclImpStyle( const XclImpRoot& rRoot ) :
    XclImpRoot( rRoot ),
    mnXfId( EXC_XF_NOTFOUND ),
    mnBuiltinId( EXC_STYLE_USERDEF ),
    mnLevel( EXC_STYLE_NOLEVEL ),
    mbBuiltin( false ),
    mbCustom( false ),
    mbHidden( false ),
    mpStyleSheet( nullptr )
{
}

void XclImpStyle::ReadStyle( XclImpStream& rStrm )
{
    OSL_ENSURE_BIFF( GetBiff() >= EXC_BIFF3 );

    sal_uInt16 nXFIndex;
    nXFIndex = rStrm.ReaduInt16();
    mnXfId = nXFIndex & EXC_STYLE_XFMASK;
    mbBuiltin = ::get_flag( nXFIndex, EXC_STYLE_BUILTIN );

    if( mbBuiltin )
    {
        mnBuiltinId = rStrm.ReaduInt8();
        mnLevel = rStrm.ReaduInt8();
    }
    else
    {
        maName = (GetBiff() <= EXC_BIFF5) ? rStrm.ReadByteString( false ) : rStrm.ReadUniString();
        // #i103281# check if this is a new built-in style introduced in XL2007
        if( (GetBiff() == EXC_BIFF8) && (rStrm.GetNextRecId() == EXC_ID_STYLEEXT) && rStrm.StartNextRecord() )
        {
            sal_uInt8 nExtFlags;
            rStrm.Ignore( 12 );
            nExtFlags = rStrm.ReaduInt8();
            mbBuiltin = ::get_flag( nExtFlags, EXC_STYLEEXT_BUILTIN );
            mbCustom = ::get_flag( nExtFlags, EXC_STYLEEXT_CUSTOM );
            mbHidden = ::get_flag( nExtFlags, EXC_STYLEEXT_HIDDEN );
            if( mbBuiltin )
            {
                rStrm.Ignore( 1 );  // category
                mnBuiltinId = rStrm.ReaduInt8();
                mnLevel = rStrm.ReaduInt8();
            }
        }
    }
}

ScStyleSheet* XclImpStyle::CreateStyleSheet()
{
    // #i1624# #i1768# ignore unnamed user styles
    if( !mpStyleSheet && (!maFinalName.isEmpty()) )
    {
        bool bCreatePattern = false;
        XclImpXF* pXF = GetXFBuffer().GetXF( mnXfId );

        bool bDefStyle = mbBuiltin && (mnBuiltinId == EXC_STYLE_NORMAL);
        if( bDefStyle )
        {
            // set all flags to true to get all items in XclImpXF::CreatePattern()
            if( pXF ) pXF->SetAllUsedFlags( true );
            // use existing "Default" style sheet
            mpStyleSheet = static_cast< ScStyleSheet* >( GetStyleSheetPool().Find(
                ScResId( STR_STYLENAME_STANDARD_CELL ), SfxStyleFamily::Para ) );
            OSL_ENSURE( mpStyleSheet, "XclImpStyle::CreateStyleSheet - Default style not found" );
            bCreatePattern = true;
        }
        else
        {
            /*  #i103281# do not create another style sheet of the same name,
                if it exists already. This is needed to prevent that styles
                pasted from clipboard get duplicated over and over. */
            mpStyleSheet = static_cast< ScStyleSheet* >( GetStyleSheetPool().Find( maFinalName, SfxStyleFamily::Para ) );
            if( !mpStyleSheet )
            {
                mpStyleSheet = &static_cast< ScStyleSheet& >( GetStyleSheetPool().Make( maFinalName, SfxStyleFamily::Para, SfxStyleSearchBits::UserDefined ) );
                bCreatePattern = true;
            }
        }

        // bDefStyle==true omits default pool items in CreatePattern()
        if( bCreatePattern && mpStyleSheet && pXF )
            mpStyleSheet->GetItemSet().Put( pXF->CreatePattern( bDefStyle ).GetItemSet() );
    }
    return mpStyleSheet;
}

void XclImpStyle::CreateUserStyle( const OUString& rFinalName )
{
    maFinalName = rFinalName;
    if( !IsBuiltin() || mbCustom )
        CreateStyleSheet();
}

XclImpXFBuffer::XclImpXFBuffer( const XclImpRoot& rRoot ) :
    XclImpRoot( rRoot )
{
}

void XclImpXFBuffer::Initialize()
{
    maXFList.clear();
    maBuiltinStyles.clear();
    maUserStyles.clear();
    maStylesByXf.clear();
}

void XclImpXFBuffer::ReadXF( XclImpStream& rStrm )
{
    XclImpXF* pXF = new XclImpXF( GetRoot() );
    pXF->ReadXF( rStrm );
    maXFList.push_back( std::unique_ptr<XclImpXF>(pXF) );
}

void XclImpXFBuffer::ReadStyle( XclImpStream& rStrm )
{
    XclImpStyle* pStyle = new XclImpStyle( GetRoot() );
    pStyle->ReadStyle( rStrm );
    (pStyle->IsBuiltin() ? maBuiltinStyles : maUserStyles).push_back( std::unique_ptr<XclImpStyle>(pStyle) );
    OSL_ENSURE( maStylesByXf.count( pStyle->GetXfId() ) == 0, "XclImpXFBuffer::ReadStyle - multiple styles with equal XF identifier" );
    maStylesByXf[ pStyle->GetXfId() ] = pStyle;<--- Dereferencing 'pStyle' after it is deallocated / released
}

sal_uInt16 XclImpXFBuffer::GetFontIndex( sal_uInt16 nXFIndex ) const
{
    const XclImpXF* pXF = GetXF( nXFIndex );
    return pXF ? pXF->GetFontIndex() : EXC_FONT_NOTFOUND;
}

const XclImpFont* XclImpXFBuffer::GetFont( sal_uInt16 nXFIndex ) const
{
    return GetFontBuffer().GetFont( GetFontIndex( nXFIndex ) );
}

namespace {

/** Functor for case-insensitive string comparison, usable in maps etc. */
struct IgnoreCaseCompare
{
    bool operator()( const OUString& rName1, const OUString& rName2 ) const
        { return rName1.compareToIgnoreAsciiCase( rName2 ) < 0; }
};

} // namespace

void XclImpXFBuffer::CreateUserStyles()
{
    // calculate final names of all styles
    std::map< OUString, XclImpStyle*, IgnoreCaseCompare > aCellStyles;
    std::vector< XclImpStyle* > aConflictNameStyles;

    /*  First, reserve style names that are built-in in Calc. This causes that
        imported cell styles get different unused names and thus do not try to
        overwrite these built-in styles. For BIFF4 workbooks (which contain a
        separate list of cell styles per sheet), reserve all existing styles if
        current sheet is not the first sheet (this styles buffer will be
        initialized again for every new sheet). This will create unique names
        for styles in different sheets with the same name. Assuming that the
        BIFF4W import filter is never used to import from clipboard... */
    bool bReserveAll = (GetBiff() == EXC_BIFF4) && (GetCurrScTab() > 0);
    SfxStyleSheetIterator aStyleIter( GetDoc().GetStyleSheetPool(), SfxStyleFamily::Para );
    OUString aStandardName = ScResId( STR_STYLENAME_STANDARD_CELL );
    for( SfxStyleSheetBase* pStyleSheet = aStyleIter.First(); pStyleSheet; pStyleSheet = aStyleIter.Next() )
        if( (pStyleSheet->GetName() != aStandardName) && (bReserveAll || !pStyleSheet->IsUserDefined()) )
            if( aCellStyles.count( pStyleSheet->GetName() ) == 0 )
                aCellStyles[ pStyleSheet->GetName() ] = nullptr;

    /*  Calculate names of built-in styles. Store styles with reserved names
        in the aConflictNameStyles list. */
    for( const auto& rxStyle : maBuiltinStyles )
    {
        OUString aStyleName = XclTools::GetBuiltInStyleName( rxStyle->GetBuiltinId(), rxStyle->GetName(), rxStyle->GetLevel() );
        OSL_ENSURE( bReserveAll || (aCellStyles.count( aStyleName ) == 0),
            "XclImpXFBuffer::CreateUserStyles - multiple styles with equal built-in identifier" );
        if( aCellStyles.count( aStyleName ) > 0 )
            aConflictNameStyles.push_back( rxStyle.get() );
        else
            aCellStyles[ aStyleName ] = rxStyle.get();
    }

    /*  Calculate names of user defined styles. Store styles with reserved
        names in the aConflictNameStyles list. */
    for( const auto& rxStyle : maUserStyles )
    {
        // #i1624# #i1768# ignore unnamed user styles
        if( !rxStyle->GetName().isEmpty() )
        {
            if( aCellStyles.count( rxStyle->GetName() ) > 0 )
                aConflictNameStyles.push_back( rxStyle.get() );
            else
                aCellStyles[ rxStyle->GetName() ] = rxStyle.get();
        }
    }

    // find unused names for all styles with conflicting names
    for( XclImpStyle* pStyle : aConflictNameStyles )
    {
        OUString aUnusedName;
        sal_Int32 nIndex = 0;
        do
        {
            aUnusedName = pStyle->GetName() + " " + OUString::number( ++nIndex );
        }
        while( aCellStyles.count( aUnusedName ) > 0 );
        aCellStyles[ aUnusedName ] = pStyle;
    }

    // set final names and create user-defined and modified built-in cell styles
    for( auto& [rStyleName, rpStyle] : aCellStyles )
        if( rpStyle )
            rpStyle->CreateUserStyle( rStyleName );
}

ScStyleSheet* XclImpXFBuffer::CreateStyleSheet( sal_uInt16 nXFIndex )
{
    XclImpStyleMap::iterator aIt = maStylesByXf.find( nXFIndex );
    return (aIt == maStylesByXf.end()) ? nullptr : aIt->second->CreateStyleSheet();
}

// Buffer for XF indexes in cells =============================================

bool XclImpXFRange::Expand( SCROW nScRow, const XclImpXFIndex& rXFIndex )
{
    if( maXFIndex != rXFIndex )
        return false;

    if( mnScRow2 + 1 == nScRow )
    {
        ++mnScRow2;
        return true;
    }
    if( mnScRow1 > 0 && (mnScRow1 - 1 == nScRow) )
    {
        --mnScRow1;
        return true;
    }

    return false;
}

bool XclImpXFRange::Expand( const XclImpXFRange& rNextRange )
{
    OSL_ENSURE( mnScRow2 < rNextRange.mnScRow1, "XclImpXFRange::Expand - rows out of order" );
    if( (maXFIndex == rNextRange.maXFIndex) && (mnScRow2 + 1 == rNextRange.mnScRow1) )
    {
        mnScRow2 = rNextRange.mnScRow2;
        return true;
    }
    return false;
}

void XclImpXFRangeColumn::SetDefaultXF( const XclImpXFIndex& rXFIndex )
{
    // List should be empty when inserting the default column format.
    // Later explicit SetXF() calls will break up this range.
    OSL_ENSURE( maIndexList.empty(), "XclImpXFRangeColumn::SetDefaultXF - Setting Default Column XF is not empty" );

    // insert a complete row range with one insert.
    maIndexList.push_back( std::make_unique<XclImpXFRange>( 0, MAXROW, rXFIndex ) );
}

void XclImpXFRangeColumn::SetXF( SCROW nScRow, const XclImpXFIndex& rXFIndex )
{
    XclImpXFRange* pPrevRange;
    XclImpXFRange* pNextRange;
    sal_uLong nNextIndex;

    Find( pPrevRange, pNextRange, nNextIndex, nScRow );

    // previous range:
    // try to overwrite XF (if row is contained in) or try to expand range
    if( pPrevRange )
    {
        if( pPrevRange->Contains( nScRow ) )        // overwrite old XF
        {
            if( rXFIndex == pPrevRange->maXFIndex )
                return;

            SCROW nFirstScRow = pPrevRange->mnScRow1;
            SCROW nLastScRow = pPrevRange->mnScRow2;
            sal_uLong nIndex = nNextIndex - 1;
            XclImpXFRange* pThisRange = pPrevRange;
            pPrevRange = (nIndex > 0 && nIndex <= maIndexList.size()) ? maIndexList[ nIndex - 1 ].get() : nullptr;

            if( nFirstScRow == nLastScRow )         // replace solely XF
            {
                pThisRange->maXFIndex = rXFIndex;
                TryConcatPrev( nNextIndex );        // try to concat. next with this
                TryConcatPrev( nIndex );            // try to concat. this with previous
            }
            else if( nFirstScRow == nScRow )        // replace first XF
            {
                ++(pThisRange->mnScRow1);
                // try to concatenate with previous of this
                if( !pPrevRange || !pPrevRange->Expand( nScRow, rXFIndex ) )
                    Insert( new XclImpXFRange( nScRow, rXFIndex ), nIndex );
            }
            else if( nLastScRow == nScRow )         // replace last XF
            {
                --(pThisRange->mnScRow2);
                if( !pNextRange || !pNextRange->Expand( nScRow, rXFIndex ) )
                    Insert( new XclImpXFRange( nScRow, rXFIndex ), nNextIndex );
            }
            else                                    // insert in the middle of the range
            {
                pThisRange->mnScRow1 = nScRow + 1;
                // List::Insert() moves entries towards end of list, so insert twice at nIndex
                Insert( new XclImpXFRange( nScRow, rXFIndex ), nIndex );
                Insert( new XclImpXFRange( nFirstScRow, nScRow - 1, pThisRange->maXFIndex ), nIndex );
            }
            return;
        }
        else if( pPrevRange->Expand( nScRow, rXFIndex ) )    // try to expand
        {
            TryConcatPrev( nNextIndex );    // try to concatenate next with expanded
            return;
        }
    }

    // try to expand next range
    if( pNextRange && pNextRange->Expand( nScRow, rXFIndex ) )
        return;

    // create new range
    Insert( new XclImpXFRange( nScRow, rXFIndex ), nNextIndex );
}

void XclImpXFRangeColumn::Insert(XclImpXFRange* pXFRange, sal_uLong nIndex)
{
    maIndexList.insert( maIndexList.begin() + nIndex, std::unique_ptr<XclImpXFRange>(pXFRange) );
}

void XclImpXFRangeColumn::Find(
        XclImpXFRange*& rpPrevRange, XclImpXFRange*& rpNextRange,
        sal_uLong& rnNextIndex, SCROW nScRow )
{

    // test whether list is empty
    if( maIndexList.empty() )
    {
        rpPrevRange = rpNextRange = nullptr;
        rnNextIndex = 0;
        return;
    }

    rpPrevRange = maIndexList.front().get();
    rpNextRange = maIndexList.back().get();

    // test whether row is at end of list (contained in or behind last range)
    // rpPrevRange will contain a possible existing row
    if( rpNextRange->mnScRow1 <= nScRow )
    {
        rpPrevRange = rpNextRange;
        rpNextRange = nullptr;
        rnNextIndex = maIndexList.size();
        return;
    }

    // test whether row is at beginning of list (really before first range)
    if( nScRow < rpPrevRange->mnScRow1 )
    {
        rpNextRange = rpPrevRange;
        rpPrevRange = nullptr;
        rnNextIndex = 0;
        return;
    }

    // loop: find range entries before and after new row
    // break the loop if there is no more range between first and last -or-
    // if rpPrevRange contains nScRow (rpNextRange will never contain nScRow)
    sal_uLong nPrevIndex = 0;
    sal_uLong nMidIndex;
    rnNextIndex = maIndexList.size() - 1;
    XclImpXFRange* pMidRange;
    while( ((rnNextIndex - nPrevIndex) > 1) && (rpPrevRange->mnScRow2 < nScRow) )
    {
        nMidIndex = (nPrevIndex + rnNextIndex) / 2;
        pMidRange = maIndexList[nMidIndex].get();
        OSL_ENSURE( pMidRange, "XclImpXFRangeColumn::Find - missing XF index range" );
        if( nScRow < pMidRange->mnScRow1 )      // row is really before pMidRange
        {
            rpNextRange = pMidRange;
            rnNextIndex = nMidIndex;
        }
        else                                    // row is in or after pMidRange
        {
            rpPrevRange = pMidRange;
            nPrevIndex = nMidIndex;
        }
    }

    // find next rpNextRange if rpPrevRange contains nScRow
    if( nScRow <= rpPrevRange->mnScRow2 )
    {
        rnNextIndex = nPrevIndex + 1;
        rpNextRange = maIndexList[rnNextIndex].get();
    }
}

void XclImpXFRangeColumn::TryConcatPrev( sal_uLong nIndex )
{
    if( !nIndex || nIndex >= maIndexList.size() )
        return;

    XclImpXFRange& prevRange = *maIndexList[ nIndex - 1 ];
    XclImpXFRange& nextRange = *maIndexList[ nIndex ];

    if( prevRange.Expand( nextRange ) )
        maIndexList.erase( maIndexList.begin() + nIndex );
}

XclImpXFRangeBuffer::XclImpXFRangeBuffer( const XclImpRoot& rRoot ) :
    XclImpRoot( rRoot )
{
}

XclImpXFRangeBuffer::~XclImpXFRangeBuffer()
{
}

void XclImpXFRangeBuffer::Initialize()
{
    maColumns.clear();
    maHyperlinks.clear();
    maMergeList.RemoveAll();
}

void XclImpXFRangeBuffer::SetXF( const ScAddress& rScPos, sal_uInt16 nXFIndex, XclImpXFInsertMode eMode )
{
    SCCOL nScCol = rScPos.Col();
    SCROW nScRow = rScPos.Row();

    // set cell XF's
    size_t nIndex = static_cast< size_t >( nScCol );
    if( maColumns.size() <= nIndex )
        maColumns.resize( nIndex + 1 );
    if( !maColumns[ nIndex ] )
        maColumns[ nIndex ] = std::make_shared<XclImpXFRangeColumn>();
    // remember all Boolean cells, they will get 'Standard' number format
    maColumns[ nIndex ]->SetXF( nScRow, XclImpXFIndex( nXFIndex, eMode == xlXFModeBoolCell ) );

    // set "center across selection" and "fill" attribute for all following empty cells
    // ignore it on row default XFs
    if( eMode != xlXFModeRow )
    {
        const XclImpXF* pXF = GetXFBuffer().GetXF( nXFIndex );
        if( pXF && ((pXF->GetHorAlign() == EXC_XF_HOR_CENTER_AS) || (pXF->GetHorAlign() == EXC_XF_HOR_FILL)) )
        {
            // expand last merged range if this attribute is set repeatedly
            ScRange* pRange = maMergeList.empty() ? nullptr : &maMergeList.back();
            if (pRange && (pRange->aEnd.Row() == nScRow) && (pRange->aEnd.Col() + 1 == nScCol) && (eMode == xlXFModeBlank))
                pRange->aEnd.IncCol();
            else if( eMode != xlXFModeBlank )   // do not merge empty cells
                maMergeList.push_back( ScRange( nScCol, nScRow, 0 ) );
        }
    }
}

void XclImpXFRangeBuffer::SetXF( const ScAddress& rScPos, sal_uInt16 nXFIndex )
{
    SetXF( rScPos, nXFIndex, xlXFModeCell );
}

void XclImpXFRangeBuffer::SetBlankXF( const ScAddress& rScPos, sal_uInt16 nXFIndex )
{
    SetXF( rScPos, nXFIndex, xlXFModeBlank );
}

void XclImpXFRangeBuffer::SetBoolXF( const ScAddress& rScPos, sal_uInt16 nXFIndex )
{
    SetXF( rScPos, nXFIndex, xlXFModeBoolCell );
}

void XclImpXFRangeBuffer::SetRowDefXF( SCROW nScRow, sal_uInt16 nXFIndex )
{
    for( SCCOL nScCol = 0; nScCol <= MAXCOL; ++nScCol )
        SetXF( ScAddress( nScCol, nScRow, 0 ), nXFIndex, xlXFModeRow );
}

void XclImpXFRangeBuffer::SetColumnDefXF( SCCOL nScCol, sal_uInt16 nXFIndex )
{
    // our array should not have values when creating the default column format.
    size_t nIndex = static_cast< size_t >( nScCol );
    if( maColumns.size() <= nIndex )
        maColumns.resize( nIndex + 1 );
    OSL_ENSURE( !maColumns[ nIndex ], "XclImpXFRangeBuffer::SetColumnDefXF - default column of XFs already has values" );
    maColumns[ nIndex ] = std::make_shared<XclImpXFRangeColumn>();
    maColumns[ nIndex ]->SetDefaultXF( XclImpXFIndex( nXFIndex ) );
}

void XclImpXFRangeBuffer::SetBorderLine( const ScRange& rRange, SCTAB nScTab, SvxBoxItemLine nLine )
{
    SCCOL nFromScCol = (nLine == SvxBoxItemLine::RIGHT) ? rRange.aEnd.Col() : rRange.aStart.Col();
    SCROW nFromScRow = (nLine == SvxBoxItemLine::BOTTOM) ? rRange.aEnd.Row() : rRange.aStart.Row();
    ScDocument& rDoc = GetDoc();

    const SvxBoxItem* pFromItem =
        rDoc.GetAttr( nFromScCol, nFromScRow, nScTab, ATTR_BORDER );
    const SvxBoxItem* pToItem =
        rDoc.GetAttr( rRange.aStart.Col(), rRange.aStart.Row(), nScTab, ATTR_BORDER );

    SvxBoxItem aNewItem( *pToItem );
    aNewItem.SetLine( pFromItem->GetLine( nLine ), nLine );
    rDoc.ApplyAttr( rRange.aStart.Col(), rRange.aStart.Row(), nScTab, aNewItem );
}

void XclImpXFRangeBuffer::SetHyperlink( const XclRange& rXclRange, const OUString& rUrl )
{
    maHyperlinks.emplace_back( rXclRange, rUrl );
}

void XclImpXFRangeBuffer::SetMerge( SCCOL nScCol1, SCROW nScRow1, SCCOL nScCol2, SCROW nScRow2 )
{
    if( (nScCol1 < nScCol2) || (nScRow1 < nScRow2) )
        maMergeList.push_back( ScRange( nScCol1, nScRow1, 0, nScCol2, nScRow2, 0 ) );
}

void XclImpXFRangeBuffer::Finalize()
{
    ScDocumentImport& rDocImport = GetDocImport();
    ScDocument& rDoc = rDocImport.getDoc();
    SCTAB nScTab = GetCurrScTab();

    // apply patterns
    XclImpXFBuffer& rXFBuffer = GetXFBuffer();
    SCCOL nScCol = 0;
    for( const auto& rxColumn : maColumns )
    {
        // apply all cell styles of an existing column
        if( rxColumn )
        {
            XclImpXFRangeColumn& rColumn = *rxColumn;
            std::vector<ScAttrEntry> aAttrs;
            aAttrs.reserve(rColumn.end() - rColumn.begin());

            for (const auto& rxStyle : rColumn)
            {
                XclImpXFRange& rStyle = *rxStyle;
                const XclImpXFIndex& rXFIndex = rStyle.maXFIndex;
                XclImpXF* pXF = rXFBuffer.GetXF( rXFIndex.GetXFIndex() );
                if (!pXF)
                    continue;

                sal_uInt32 nForceScNumFmt = rXFIndex.IsBoolCell() ?
                    GetNumFmtBuffer().GetStdScNumFmt() : NUMBERFORMAT_ENTRY_NOT_FOUND;

                pXF->ApplyPatternToAttrVector(aAttrs, rStyle.mnScRow1, rStyle.mnScRow2, nForceScNumFmt);
            }

            if (aAttrs.empty() || aAttrs.back().nEndRow != rDoc.MaxRow())
            {
                ScAttrEntry aEntry;
                aEntry.nEndRow = rDoc.MaxRow();
                aEntry.pPattern = rDoc.GetDefPattern();
                aAttrs.push_back(aEntry);
            }

            aAttrs.shrink_to_fit();
            assert(aAttrs.size() > 0);
            ScDocumentImport::Attrs aAttrParam;
            aAttrParam.mvData.swap(aAttrs);
            aAttrParam.mbLatinNumFmtOnly = false; // when unsure, set it to false.
            rDocImport.setAttrEntries(nScTab, nScCol, std::move(aAttrParam));
        }
        ++nScCol;
    }

    // insert hyperlink cells
    for( const auto& [rXclRange, rUrl] : maHyperlinks )
        XclImpHyperlink::InsertUrl( GetRoot(), rXclRange, rUrl );

    // apply cell merging
    for ( size_t i = 0, nRange = maMergeList.size(); i < nRange; ++i )
    {
        const ScRange & rRange = maMergeList[ i ];
        const ScAddress& rStart = rRange.aStart;
        const ScAddress& rEnd = rRange.aEnd;
        bool bMultiCol = rStart.Col() != rEnd.Col();
        bool bMultiRow = rStart.Row() != rEnd.Row();
        // set correct right border
        if( bMultiCol )
            SetBorderLine( rRange, nScTab, SvxBoxItemLine::RIGHT );
        // set correct lower border
        if( bMultiRow )
            SetBorderLine( rRange, nScTab, SvxBoxItemLine::BOTTOM );
        // do merge
        if( bMultiCol || bMultiRow )
            rDoc.DoMerge( nScTab, rStart.Col(), rStart.Row(), rEnd.Col(), rEnd.Row() );
        // #i93609# merged range in a single row: test if manual row height is needed
        if( !bMultiRow )
        {
            bool bTextWrap = rDoc.GetAttr( rStart, ATTR_LINEBREAK )->GetValue();
            if( !bTextWrap && (rDoc.GetCellType( rStart ) == CELLTYPE_EDIT) )
                if (const EditTextObject* pEditObj = rDoc.GetEditText(rStart))
                    bTextWrap = pEditObj->GetParagraphCount() > 1;
            if( bTextWrap )
                GetOldRoot().pColRowBuff->SetManualRowHeight( rStart.Row() );
        }
    }
}

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