summaryrefslogtreecommitdiff
path: root/sw/source/filter/rtf/wrtrtf.cxx
blob: 8c80d688a2eee6eabd21aca766c8ae9510c915fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sw.hxx"
#include <stdlib.h>
#include <hintids.hxx>

#include <comphelper/string.hxx>
#include <tools/stream.hxx>
#include <tools/datetime.hxx>
#include <unotools/fontcvt.hxx>
#include <rtl/tencinfo.h>
#include <svtools/rtfkeywd.hxx>
#include <svtools/rtfout.hxx>
#include <editeng/paperinf.hxx>
#include <editeng/fontitem.hxx>
#include <editeng/shaditem.hxx>
#include <editeng/brshitem.hxx>
#include <editeng/colritem.hxx>
#include <editeng/boxitem.hxx>
#include <editeng/tstpitem.hxx>
#include <editeng/ulspitem.hxx>
#include <editeng/lrspitem.hxx>
#include <editeng/frmdiritem.hxx>
#include <editeng/udlnitem.hxx>
#include <fmtpdsc.hxx>
#include <fmtcntnt.hxx>
#include <fmtfsize.hxx>
#include <fmthdft.hxx>
#include <frmatr.hxx>
#include <fmtanchr.hxx>
#include <docary.hxx>
#include <pam.hxx>
#include <doc.hxx>
#include <paratr.hxx>
#include <fldbas.hxx>
#include <ndtxt.hxx>
#include <wrtrtf.hxx>
#include <flypos.hxx>
#include <IMark.hxx>
#include <pagedesc.hxx>     // fuer SwPageDesc...
#include <ftninfo.hxx>
#include <charfmt.hxx>
#include <SwStyleNameMapper.hxx>
#include <section.hxx>
#include <swtable.hxx>      // fuer SwPageDesc ...
#include <swmodule.hxx>
#include <swerror.h>
#include <mdiexp.hxx>       // ...Percent()
#include <statstr.hrc>      // ResId fuer Statusleiste
#include <docsh.hxx>

#include <com/sun/star/document/XDocumentProperties.hpp>
#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>


#if defined(UNX)
const sal_Char SwRTFWriter::sNewLine = '\012';
#else
const sal_Char __FAR_DATA SwRTFWriter::sNewLine[] = "\015\012";
#endif



SwRTFWriter::SwRTFWriter( const String& rFltName, const String & rBaseURL ) :
    eDefaultEncoding(
        rtl_getTextEncodingFromWindowsCharset(
            sw::ms::rtl_TextEncodingToWinCharset(DEF_ENCODING))),
    eCurrentEncoding(eDefaultEncoding)
{
    SetBaseURL( rBaseURL );
    // schreibe Win-RTF-HelpFileFmt
    bWriteHelpFmt = 'W' == rFltName.GetChar( 0 );
    // schreibe nur Gliederungs Absaetze
    bOutOutlineOnly = 'O' == rFltName.GetChar( 0 );
    // enable non-standard tags for cut and paste
    bNonStandard = '\0' == rFltName.GetChar( 0 );
}


SwRTFWriter::~SwRTFWriter()
{}


ULONG SwRTFWriter::WriteStream()
{
    bOutPageAttr = bOutSection = TRUE;

    bOutStyleTab = bOutTable = bOutPageDesc = bOutPageDescTbl =
    bAutoAttrSet = bOutListNumTxt = bOutLeftHeadFoot = bIgnoreNextPgBreak =
        bTxtAttr = bAssociated = FALSE;

    nCurScript = 1;                     // latin - ask the doc??

    nCurRedline = USHRT_MAX;
    if(pDoc->GetRedlineTbl().Count())
        nCurRedline = 0;


    pCurEndPosLst = 0;
    nBkmkTabPos = -1;
    pAktPageDesc = 0;
    pAttrSet = 0;
    pFlyFmt = 0;        // kein FlyFrmFormat gesetzt

    pColTbl = new RTFColorTbl;
    pNumRuleTbl = 0;

    BYTE nSz = (BYTE)Min( pDoc->GetSpzFrmFmts()->Count(), USHORT(255) );
    SwPosFlyFrms aFlyPos( nSz, nSz );

    //Abkuerzung wenn nur Gliederung ausgegeben werden soll, und keine
    //Gliederungsabsaetze ausserhalb des Body stehen.
    if ( bOutOutlineOnly &&
         pDoc->GetNodes().GetOutLineNds().Count() &&
         pDoc->GetNodes().GetOutLineNds()[0]->GetIndex() >
         pDoc->GetNodes().GetEndOfExtras().GetIndex() )
    {
        nAktFlyPos = 0;
        pFlyPos = 0;
        MakeHeader();

        const SwOutlineNodes &rOutLine = pDoc->GetNodes().GetOutLineNds();
        for ( USHORT i = 0; i < rOutLine.Count(); ++i )
        {
            const SwNode *pNd = pDoc->GetNodes()[*rOutLine[i]];
            ASSERT( pNd->IsCntntNode(), "Outlinenode and no CntNode!?" );

            SwCntntNode* pCNd = (SwCntntNode*)pNd;

            // erfrage den aktuellen PageDescriptor.
            const SwPageDesc* pTmp = pCNd->GetSwAttrSet().GetPageDesc().GetPageDesc();
            if( pTmp )
                pAktPageDesc = pTmp;
            pCurPam->GetPoint()->nContent.Assign( pCNd, 0 );
            Out( aRTFNodeFnTab, *pCNd, *this );
        }
    }
    else
    {
        long nMaxNode = pDoc->GetNodes().Count();

        if( bShowProgress )
            ::StartProgress( STR_STATSTR_W4WWRITE, 0, nMaxNode, pDoc->GetDocShell() );

        // Tabelle am Doc.-Anfang beachten
        {
            SwTableNode * pTNd = pCurPam->GetNode()->FindTableNode();
            if( pTNd && bWriteAll )
            {
                // mit dem TabellenNode anfangen !!
                pCurPam->GetPoint()->nNode = *pTNd;

                if( bWriteOnlyFirstTable )
                    pCurPam->GetMark()->nNode = *pTNd->EndOfSectionNode();
            }
        }

        // Tabelle fuer die freifliegenden Rahmen erzeugen, aber nur wenn
        // das gesamte Dokument geschrieben wird
        nAktFlyPos = 0;
        pDoc->GetAllFlyFmts( aFlyPos, bWriteAll ? 0 : pOrigPam );

        // sollten nur Rahmen vorhanden sein?
        // (Moeglich, wenn eine Rahmen-Selektion ins Clipboard
        // gestellt wurde)
        if( bWriteAll &&
            // keine Laenge
            *pCurPam->GetPoint() == *pCurPam->GetMark() &&
            // Rahmen vorhanden
            pDoc->GetSpzFrmFmts()->Count() && !aFlyPos.Count() &&
            // nur ein Node im Array
            pDoc->GetNodes().GetEndOfExtras().GetIndex() + 3 ==
            pDoc->GetNodes().GetEndOfContent().GetIndex() &&
            // und genau der ist selektiert
            pDoc->GetNodes().GetEndOfContent().GetIndex() - 1 ==
            pCurPam->GetPoint()->nNode.GetIndex() )
        {
            // dann den Inhalt vom Rahmen ausgeben.
            // dieser steht immer an Position 0 !!
            SwFrmFmt* pFmt = (*pDoc->GetSpzFrmFmts())[ 0 ];
            const SwNodeIndex* pIdx = pFmt->GetCntnt().GetCntntIdx();
            if( pIdx )
            {
                SwPosFlyFrm* pFPos = new SwPosFlyFrm( pCurPam->GetPoint()->nNode,
                                                        pFmt, aFlyPos.Count() );
                aFlyPos.Insert( pFPos );
            }
        }

        pFlyPos = &aFlyPos;

        // schreibe die StyleTabelle, allgemeine Angaben,Header/Footer/Footnotes
        MakeHeader();

        Out_SwDoc( pOrigPam );

        if( bShowProgress )
            ::EndProgress( pDoc->GetDocShell() );
    }

    Strm() << '}';

    // loesche die Tabelle mit den freifliegenden Rahmen
    for( USHORT i = aFlyPos.Count(); i > 0; )
        delete aFlyPos[ --i ];

    pFlyPos = 0;
    delete pColTbl;
    if( pNumRuleTbl )
    {
        // don't destroy the numrule pointers in the DTOR.
        pNumRuleTbl->Remove( 0, pNumRuleTbl->Count() );
        delete pNumRuleTbl;
    }
    delete pRedlAuthors;

    // schreibe Win-RTF-HelpFileFmt
    bOutOutlineOnly = bWriteHelpFmt = FALSE;
    pAttrSet = 0;

    return 0;
}


void SwRTFWriter::Out_SwDoc( SwPaM* pPam )
{
    BOOL bSaveWriteAll = bWriteAll;     // sichern
    // suche die naechste Bookmark-Position aus der Bookmark-Tabelle
    nBkmkTabPos = bWriteAll ? FindPos_Bkmk( *pCurPam->GetPoint() ) : -1;

    // gebe alle Bereiche des Pams in das RTF-File aus.
    do {
        bWriteAll = bSaveWriteAll;
        bFirstLine = TRUE;

        // suche den ersten am Pam-auszugebenen FlyFrame
        // fehlt noch:

        while( pCurPam->GetPoint()->nNode < pCurPam->GetMark()->nNode ||
              (pCurPam->GetPoint()->nNode == pCurPam->GetMark()->nNode &&
               pCurPam->GetPoint()->nContent.GetIndex() <= pCurPam->GetMark()->nContent.GetIndex()) )
        {
            SwNode& rNd = pCurPam->GetPoint()->nNode.GetNode();

            if( &rNd == &pDoc->GetNodes().GetEndOfContent() )
                break;

            if( rNd.IsCntntNode() )
            {
                SwCntntNode& rCNd = (SwCntntNode&)rNd;

                OutBreaks( rCNd.GetSwAttrSet() );
                OutFlyFrm();

                if( !bFirstLine )
                    pCurPam->GetPoint()->nContent.Assign( &rCNd, 0 );

                if( !bOutOutlineOnly ||
                    //( rCNd.IsTxtNode() && NO_NUMBERING !=     //#outline level,removed by zhaojianwei
                    //((SwTxtNode&)rCNd).GetTxtColl()->GetOutlineLevel() ))
                    ( rCNd.IsTxtNode() &&                       //->add by zhaojianwei
                        ((SwTxtNode&)rCNd).GetTxtColl()->IsAssignedToListLevelOfOutlineStyle()))//<-end,zhaojianwei
                    Out( aRTFNodeFnTab, rCNd, *this );

            }
            else if( !bOutOutlineOnly )
            {
                if (rNd.IsTableNode())
                {
                    bool bAllOk = false;
                    if (const SwTableNode *pNd = rNd.GetTableNode())
                    {
                        if (const SwFrmFmt *pFmt = pNd->GetTable().GetFrmFmt())
                        {
                            OutBreaks(pFmt->GetAttrSet());
                            bAllOk = true;
                        }
                        OutRTF_SwTblNode(*this, *pNd);
                    }
                    ASSERT(bAllOk, "Unexpected missing properties from tables");
                }
                else if( rNd.IsSectionNode() )
                {
                    OutBreaks( ((SwSectionNode&)rNd).GetSection().GetFmt()
                                ->GetAttrSet() );
                    OutRTF_SwSectionNode( *this, (SwSectionNode&)rNd );
                }
                else if( rNd.IsEndNode() )
                    CheckEndNodeForSection( rNd );
            }

            ULONG nPos = pCurPam->GetPoint()->nNode++;  // Bewegen

            if( bShowProgress )
                ::SetProgressState( nPos, pDoc->GetDocShell() );   // Wie weit ?

            /* sollen nur die Selectierten Bereiche gesichert werden, so
             * duerfen nur die vollstaendigen Nodes gespeichert werde,
             * d.H. der 1. und n. Node teilweise, der 2. bis n-1. Node
             * vollstaendig. (vollstaendig heisst mit allen Formaten! )
             */
            bWriteAll = bSaveWriteAll ||
                    pCurPam->GetPoint()->nNode != pCurPam->GetMark()->nNode;
            bFirstLine = FALSE;
        }
    } while( CopyNextPam( &pPam ) );        // bis alle PaM's bearbeitet

    bWriteAll = bSaveWriteAll;          // wieder auf alten Wert zurueck
}


// schreibe die StyleTabelle, algemeine Angaben,Header/Footer/Footnotes


void SwRTFWriter::MakeHeader()
{
    // baue den Vorspann wie Header, ColorTbl, FontTbl
    Strm() << '{' << OOO_STRING_SVTOOLS_RTF_RTF << '1'
        << OOO_STRING_SVTOOLS_RTF_ANSI;
    if( bWriteAll )
    {
        Strm() << OOO_STRING_SVTOOLS_RTF_DEFF;
        OutULong( GetId( (SvxFontItem&)pDoc->GetAttrPool().GetDefaultItem(
                        RES_CHRATR_FONT ) ));
    }
    // JP 13.02.2001 - if this not exist, MS don't understand our ansi
    //                  characters (0x80-0xff).
    Strm() << "\\adeflang1025";

    OutRTFFontTab();
    OutRTFColorTab();
    OutRTFStyleTab();
    OutRTFListTab();
    OutRTFRevTab();

    Strm() << SwRTFWriter::sNewLine;        // ein Trenner

    // wenn teilweise ausgegeben wird, die globalen Daten nicht speichern
    if( !bWriteAll )
        return;

    // Ausgeben der Doc-Info/-Statistik
    OutDocInfoStat();

    // einige globale Daten Schreiben
    {   // Default-TabSize
        const SvxTabStopItem& rTabs = (const SvxTabStopItem&)
                    pDoc->GetAttrPool().GetDefaultItem( RES_PARATR_TABSTOP );
        Strm() << OOO_STRING_SVTOOLS_RTF_DEFTAB;
        OutLong( rTabs[0].GetTabPos() );
        if ( !pDoc->get(IDocumentSettingAccess::USE_VIRTUAL_DEVICE) )
            Strm() << OOO_STRING_SVTOOLS_RTF_LYTPRTMET;
    }

    // PageDescriptor ausgeben (??nur wenn das gesamte Dokument??)
    OutPageDesc();

    // gebe die Groesse und die Raender der Seite aus
    if( pDoc->GetPageDescCnt() )
    {
        //JP 06.04.99: Bug 64361 - suche den ersten SwFmtPageDesc. Ist
        //              keiner gesetzt, so ist der Standard gueltig
        const SwFmtPageDesc* pSttPgDsc = 0;
        {
            const SwNode& rSttNd = *pDoc->GetNodes()[
                        pDoc->GetNodes().GetEndOfExtras().GetIndex() + 2 ];
            const SfxItemSet* pSet = 0;

            if( rSttNd.IsCntntNode() )
                pSet = &rSttNd.GetCntntNode()->GetSwAttrSet();
            else if( rSttNd.IsTableNode() )
                pSet = &rSttNd.GetTableNode()->GetTable().
                            GetFrmFmt()->GetAttrSet();
            else if( rSttNd.IsSectionNode() )
                pSet = &rSttNd.GetSectionNode()->GetSection().
                            GetFmt()->GetAttrSet();

            if( pSet )
            {
                USHORT nPosInDoc;
                pSttPgDsc = (SwFmtPageDesc*)&pSet->Get( RES_PAGEDESC );
                if( !pSttPgDsc->GetPageDesc() )
                    pSttPgDsc = 0;
                else if( pDoc->FindPageDescByName( pSttPgDsc->
                                    GetPageDesc()->GetName(), &nPosInDoc ))
                {
                                                // FALSE wegen schliessender Klammer !!
                    OutComment( *this, OOO_STRING_SVTOOLS_RTF_PGDSCNO, FALSE );
                    OutULong( nPosInDoc ) << '}';
                }
            }
        }
        const SwPageDesc& rPageDesc = pSttPgDsc ? *pSttPgDsc->GetPageDesc()
            : const_cast<const SwDoc *>(pDoc)->GetPageDesc( 0 );
        const SwFrmFmt &rFmtPage = rPageDesc.GetMaster();

        {
            if( rPageDesc.GetLandscape() )
                Strm() << OOO_STRING_SVTOOLS_RTF_LANDSCAPE;

            const SwFmtFrmSize& rSz = rFmtPage.GetFrmSize();
            // Clipboard-Dokument wird immer ohne Drucker angelegt, so ist
            // der Std.PageDesc immer aug LONG_MAX !! Mappe dann auf DIN A4
            if( LONG_MAX == rSz.GetHeight() || LONG_MAX == rSz.GetWidth() )
            {
                Strm() << OOO_STRING_SVTOOLS_RTF_PAPERH;
                Size a4 = SvxPaperInfo::GetPaperSize(PAPER_A4);
                OutULong( a4.Height() ) << OOO_STRING_SVTOOLS_RTF_PAPERW;
                OutULong( a4.Width() );
            }
            else
            {
                Strm() << OOO_STRING_SVTOOLS_RTF_PAPERH;
                OutULong( rSz.GetHeight() ) << OOO_STRING_SVTOOLS_RTF_PAPERW;
                OutULong( rSz.GetWidth() );
            }
        }

        {
            const SvxLRSpaceItem& rLR = rFmtPage.GetLRSpace();
            Strm() << OOO_STRING_SVTOOLS_RTF_MARGL;
            OutLong( rLR.GetLeft() ) << OOO_STRING_SVTOOLS_RTF_MARGR;
            OutLong( rLR.GetRight() );
        }

        {
            const SvxULSpaceItem& rUL = rFmtPage.GetULSpace();
            Strm() << OOO_STRING_SVTOOLS_RTF_MARGT;
            OutLong( rUL.GetUpper() ) << OOO_STRING_SVTOOLS_RTF_MARGB;
            OutLong( rUL.GetLower() );
        }

        Strm() << OOO_STRING_SVTOOLS_RTF_SECTD << OOO_STRING_SVTOOLS_RTF_SBKNONE;
        OutRTFPageDescription( rPageDesc, FALSE, TRUE );    // Changed bCheckForFirstPage to TRUE so headers
                                                            // following title page are correctly added - i13107
        if( pSttPgDsc )
        {
            bIgnoreNextPgBreak = TRUE;
            pAktPageDesc = &rPageDesc;
        }
    }


    {
        // schreibe die Fussnoten- und Endnoten-Info raus
        const SwFtnInfo& rFtnInfo = pDoc->GetFtnInfo();

        const char* pOut = FTNPOS_CHAPTER == rFtnInfo.ePos
                            ? OOO_STRING_SVTOOLS_RTF_ENDDOC
                            : OOO_STRING_SVTOOLS_RTF_FTNBJ;
        Strm() << pOut << OOO_STRING_SVTOOLS_RTF_FTNSTART;
        OutLong( rFtnInfo.nFtnOffset + 1 );

        switch( rFtnInfo.eNum )
        {
        case FTNNUM_PAGE:       pOut = OOO_STRING_SVTOOLS_RTF_FTNRSTPG; break;
        case FTNNUM_DOC:        pOut = OOO_STRING_SVTOOLS_RTF_FTNRSTCONT;   break;
//      case FTNNUM_CHAPTER:
        default:                pOut = OOO_STRING_SVTOOLS_RTF_FTNRESTART;   break;
        }
        Strm() << pOut;

        switch( rFtnInfo.aFmt.GetNumberingType() )
        {
        case SVX_NUM_CHARS_LOWER_LETTER:
        case SVX_NUM_CHARS_LOWER_LETTER_N:  pOut = OOO_STRING_SVTOOLS_RTF_FTNNALC;  break;
        case SVX_NUM_CHARS_UPPER_LETTER:
        case SVX_NUM_CHARS_UPPER_LETTER_N:  pOut = OOO_STRING_SVTOOLS_RTF_FTNNAUC;  break;
        case SVX_NUM_ROMAN_LOWER:           pOut = OOO_STRING_SVTOOLS_RTF_FTNNRLC;  break;
        case SVX_NUM_ROMAN_UPPER:           pOut = OOO_STRING_SVTOOLS_RTF_FTNNRUC;  break;
        case SVX_NUM_CHAR_SPECIAL:          pOut = OOO_STRING_SVTOOLS_RTF_FTNNCHI;  break;
//      case SVX_NUM_ARABIC:
        default:                    pOut = OOO_STRING_SVTOOLS_RTF_FTNNAR;       break;
        }
        Strm() << pOut;


        const SwEndNoteInfo& rEndNoteInfo = pDoc->GetEndNoteInfo();

        Strm() << OOO_STRING_SVTOOLS_RTF_AENDDOC << OOO_STRING_SVTOOLS_RTF_AFTNRSTCONT
               << OOO_STRING_SVTOOLS_RTF_AFTNSTART;
        OutLong( rEndNoteInfo.nFtnOffset + 1 );

        switch( rEndNoteInfo.aFmt.GetNumberingType() )
        {
        case SVX_NUM_CHARS_LOWER_LETTER:
        case SVX_NUM_CHARS_LOWER_LETTER_N:  pOut = OOO_STRING_SVTOOLS_RTF_AFTNNALC; break;
        case SVX_NUM_CHARS_UPPER_LETTER:
        case SVX_NUM_CHARS_UPPER_LETTER_N:  pOut = OOO_STRING_SVTOOLS_RTF_AFTNNAUC; break;
        case SVX_NUM_ROMAN_LOWER:           pOut = OOO_STRING_SVTOOLS_RTF_AFTNNRLC; break;
        case SVX_NUM_ROMAN_UPPER:           pOut = OOO_STRING_SVTOOLS_RTF_AFTNNRUC; break;
        case SVX_NUM_CHAR_SPECIAL:          pOut = OOO_STRING_SVTOOLS_RTF_AFTNNCHI; break;
//      case SVX_NUM_ARABIC:
        default:                    pOut = OOO_STRING_SVTOOLS_RTF_AFTNNAR;  break;
        }
        Strm() << pOut;
    }

    if( pDoc->_GetDBDesc().sDataSource.getLength() )
    {
        // stelle erstmal fest, ob ueberhaupt Datenbankfelder benutzt werden!
        const SwFldTypes* pTypes = pDoc->GetFldTypes();
        for( USHORT nCnt = pTypes->Count(); nCnt >= INIT_FLDTYPES; )
            if( RES_DBFLD == (*pTypes)[ --nCnt ]->Which() &&
                (*pTypes)[ nCnt ]->GetDepends() )
            {
                Strm() << '{' << OOO_STRING_SVTOOLS_RTF_FIELD;
                OutComment( *this, OOO_STRING_SVTOOLS_RTF_FLDINST ) << " DATA ";
                SwDBData aData = pDoc->GetDBData();
                String sOut(aData.sDataSource);
                sOut += DB_DELIM;
                sOut += (String)aData.sCommand;
                RTFOutFuncs::Out_String( Strm(), sOut,
                                        eDefaultEncoding, bWriteHelpFmt );
                Strm() << "}{" << OOO_STRING_SVTOOLS_RTF_FLDRSLT << " }}";
                break;
            }
    }

    pAttrSet = 0;

    Strm() << SwRTFWriter::sNewLine;        // ein Trenner
}

void SwRTFWriter::OutInfoDateTime( const sal_Char* i_pStr,
    const util::DateTime& i_rDT )
{
    Strm() << '{' << i_pStr << OOO_STRING_SVTOOLS_RTF_YR;
    OutLong( Strm(), i_rDT.Year ) << OOO_STRING_SVTOOLS_RTF_MO;
    OutLong( Strm(), i_rDT.Month ) << OOO_STRING_SVTOOLS_RTF_DY;
    OutLong( Strm(), i_rDT.Day ) << OOO_STRING_SVTOOLS_RTF_HR;
    OutLong( Strm(), i_rDT.Hours ) << OOO_STRING_SVTOOLS_RTF_MIN;
    OutLong( Strm(), i_rDT.Minutes ) << '}';
}

bool CharsetSufficient(const String &rString, rtl_TextEncoding eChrSet)
{
    const sal_uInt32 nFlags =
        RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
        RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR;
    rtl::OString sDummy;
    rtl::OUString sStr(rString);
    return sStr.convertToString(&sDummy, eChrSet, nFlags);
}

void SwRTFWriter::OutUnicodeSafeRecord(const sal_Char *pToken,
    const String &rContent)
{
    if (rContent.Len())
    {
        bool bNeedUnicodeWrapper = !CharsetSufficient(rContent, eDefaultEncoding);

        if (bNeedUnicodeWrapper)
            Strm() << '{' << OOO_STRING_SVTOOLS_RTF_UPR;

        Strm() << '{' << pToken << ' ';
        OutRTF_AsByteString(*this, rContent, eDefaultEncoding);
        Strm() << '}';

        if (bNeedUnicodeWrapper)
        {
            OutComment(*this, OOO_STRING_SVTOOLS_RTF_UD);
            Strm() << '{' << pToken << ' ';
            RTFOutFuncs::Out_String(Strm(), rContent, eDefaultEncoding,
                bWriteHelpFmt);
            Strm() << "}}}";
        }

    }
}

void SwRTFWriter::OutDocInfoStat()
{
    Strm() << '{' << OOO_STRING_SVTOOLS_RTF_INFO;

    SwDocShell *pDocShell(pDoc->GetDocShell());
    uno::Reference<document::XDocumentProperties> xDocProps;
    if (pDocShell) {
        uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
            pDocShell->GetModel(), uno::UNO_QUERY_THROW);
        xDocProps.set(xDPS->getDocumentProperties());
    }

    // may be null (in case of copying)
    if (xDocProps.is())
    {
        OutUnicodeSafeRecord(OOO_STRING_SVTOOLS_RTF_TITLE,    xDocProps->getTitle());
        OutUnicodeSafeRecord(OOO_STRING_SVTOOLS_RTF_SUBJECT,  xDocProps->getSubject());

        OutUnicodeSafeRecord(OOO_STRING_SVTOOLS_RTF_KEYWORDS,
        ::comphelper::string::convertCommaSeparated(xDocProps->getKeywords()));
        OutUnicodeSafeRecord(OOO_STRING_SVTOOLS_RTF_DOCCOMM,  xDocProps->getDescription());

        OutUnicodeSafeRecord(OOO_STRING_SVTOOLS_RTF_AUTHOR,   xDocProps->getAuthor() );
        OutInfoDateTime(OOO_STRING_SVTOOLS_RTF_CREATIM,       xDocProps->getCreationDate());

        OutUnicodeSafeRecord(OOO_STRING_SVTOOLS_RTF_AUTHOR,   xDocProps->getModifiedBy() );
        OutInfoDateTime(OOO_STRING_SVTOOLS_RTF_REVTIM,        xDocProps->getModificationDate());

        OutInfoDateTime(OOO_STRING_SVTOOLS_RTF_PRINTIM,       xDocProps->getPrintDate());

    }

    // fuer interne Zwecke - Versionsnummer rausschreiben
    Strm() << '{' << OOO_STRING_SVTOOLS_RTF_COMMENT << " StarWriter}{" << OOO_STRING_SVTOOLS_RTF_VERN;
    OutLong( Strm(), SUPD*10 ) << '}';

    Strm() << '}';
}

static void InsColor( RTFColorTbl& rTbl, const Color& rCol )
{
    for( size_t n = 0; n < rTbl.size(); ++n )
        if( rTbl[n] == rCol )
            return;         // already exists, return

    if ( COL_AUTO == rCol.GetColor() )
    {
        rTbl.push_front( rCol );
    }
    else
    {
        rTbl.push_back( rCol );
    }
}

static void InsColorLine( RTFColorTbl& rTbl, const SvxBoxItem& rBox )
{
    const SvxBorderLine* pLine = 0;

    if( rBox.GetTop() )
        InsColor( rTbl, (pLine = rBox.GetTop())->GetColor() );
    if( rBox.GetBottom() && pLine != rBox.GetBottom() )
        InsColor( rTbl, (pLine = rBox.GetBottom())->GetColor() );
    if( rBox.GetLeft() && pLine != rBox.GetLeft()  )
        InsColor( rTbl, (pLine = rBox.GetLeft())->GetColor() );
    if( rBox.GetRight() && pLine != rBox.GetRight()  )
        InsColor( rTbl, rBox.GetRight()->GetColor() );
}

void SwRTFWriter::OutRTFColorTab()
{
    ASSERT( pColTbl, "Where's table color?" );

    // Build the color table from all attributes. The Colors
    // included and registered in the pool are on.
    size_t n;
    sal_uInt32 nMaxItem;
    const SfxItemPool& rPool = pDoc->GetAttrPool();

    // the character - Color attribute
    {
        const SvxColorItem* pCol = (const SvxColorItem*)GetDfltAttr(
                                                RES_CHRATR_COLOR );
        InsColor( *pColTbl, pCol->GetValue() );
        if( 0 != ( pCol = (const SvxColorItem*)rPool.GetPoolDefaultItem(
                RES_CHRATR_COLOR ) ))
            InsColor( *pColTbl, pCol->GetValue() );
        nMaxItem = rPool.GetItemCount2(RES_CHRATR_COLOR);
        for( n = 0; n < nMaxItem; ++n )
        {
            if( 0 != (pCol = (const SvxColorItem*)rPool.GetItem2(
                RES_CHRATR_COLOR, n ) ) )
                InsColor( *pColTbl, pCol->GetValue() );
        }

        const SvxUnderlineItem* pUnder = (const SvxUnderlineItem*)GetDfltAttr( RES_CHRATR_UNDERLINE );
        InsColor( *pColTbl, pUnder->GetColor() );
        nMaxItem = rPool.GetItemCount2(RES_CHRATR_UNDERLINE);
        for( n = 0; n < nMaxItem;n++)
        {
            if( 0 != (pUnder = (const SvxUnderlineItem*)rPool.GetItem2( RES_CHRATR_UNDERLINE, n ) ) )
                InsColor( *pColTbl, pUnder->GetColor() );

        }

        const SvxOverlineItem* pOver = (const SvxOverlineItem*)GetDfltAttr( RES_CHRATR_OVERLINE );
        InsColor( *pColTbl, pOver->GetColor() );
        nMaxItem = rPool.GetItemCount2(RES_CHRATR_OVERLINE);
        for( n = 0; n < nMaxItem;n++)
        {
            if( 0 != (pOver = (const SvxOverlineItem*)rPool.GetItem2( RES_CHRATR_OVERLINE, n ) ) )
                InsColor( *pColTbl, pOver->GetColor() );

        }

    }

    // das Frame Hintergrund - Attribut
    static const USHORT aBrushIds[] = {
                                RES_BACKGROUND, RES_CHRATR_BACKGROUND, 0 };

    for( const USHORT* pIds = aBrushIds; *pIds; ++pIds )
    {
        const SvxBrushItem* pBkgrd = (const SvxBrushItem*)GetDfltAttr( *pIds );
        InsColor( *pColTbl, pBkgrd->GetColor() );
        if( 0 != ( pBkgrd = (const SvxBrushItem*)rPool.GetPoolDefaultItem(
                        *pIds ) ))
        {
            InsColor( *pColTbl, pBkgrd->GetColor() );
        }
        nMaxItem = rPool.GetItemCount2( *pIds );
        for( n = 0; n < nMaxItem; ++n )
            if( 0 != (pBkgrd = (const SvxBrushItem*)rPool.GetItem2(
                    *pIds , n ) ))
            {
                InsColor( *pColTbl, pBkgrd->GetColor() );
            }
    }

    // das Frame Schatten - Attribut
    {
        const SvxShadowItem* pShadow = (const SvxShadowItem*)GetDfltAttr(
                                                            RES_SHADOW );
        InsColor( *pColTbl, pShadow->GetColor() );
        if( 0 != ( pShadow = (const SvxShadowItem*)rPool.GetPoolDefaultItem(
                        RES_SHADOW ) ))
        {
            InsColor( *pColTbl, pShadow->GetColor() );
        }
        nMaxItem = rPool.GetItemCount2(RES_SHADOW);
        for( n = 0; n < nMaxItem; ++n )
            if( 0 != (pShadow = (const SvxShadowItem*)rPool.GetItem2(
                RES_SHADOW, n ) ) )
            {
                InsColor( *pColTbl, pShadow->GetColor() );
            }
    }

    // das Frame Umrandungs - Attribut
    {
        // Box muesste noch gemacht werden, aber default nie eine Line gesetzt!
        const SvxBoxItem* pBox;
        if( 0 != ( pBox = (const SvxBoxItem*)rPool.GetPoolDefaultItem(
                        RES_BOX ) ))
            InsColorLine( *pColTbl, *pBox );
        nMaxItem = rPool.GetItemCount2(RES_BOX);
        for( n = 0; n < nMaxItem; ++n )
            if( 0 != (pBox = (const SvxBoxItem*)rPool.GetItem2( RES_BOX, n ) ))
                InsColorLine( *pColTbl, *pBox );
    }

    // und raus damit
    Strm() << SwRTFWriter::sNewLine << '{' << OOO_STRING_SVTOOLS_RTF_COLORTBL;

    for( n = 0; n < pColTbl->size(); n++ )
    {
        const Color& rCol = (*pColTbl)[ n ];
        if( n || COL_AUTO != rCol.GetColor() )
        {
            Strm() << OOO_STRING_SVTOOLS_RTF_RED;
            OutULong( rCol.GetRed() ) << OOO_STRING_SVTOOLS_RTF_GREEN;
            OutULong( rCol.GetGreen() ) << OOO_STRING_SVTOOLS_RTF_BLUE;
            OutULong( rCol.GetBlue() );
        }
        Strm() << ';';
    }
    Strm() << '}';
}

bool FontCharsetSufficient(const String &rFntNm, const String &rAltNm,
    rtl_TextEncoding eChrSet)
{
    bool bRet = CharsetSufficient(rFntNm, eChrSet);
    if (bRet)
        bRet = CharsetSufficient(rAltNm, eChrSet);
    return bRet;
}

static void _OutFont( SwRTFWriter& rWrt, const SvxFontItem& rFont, USHORT nNo )
{
    rWrt.Strm() << '{' << OOO_STRING_SVTOOLS_RTF_F;

    const char* pStr = OOO_STRING_SVTOOLS_RTF_FNIL;
    switch (rFont.GetFamily())
    {
        case FAMILY_ROMAN:
            pStr = OOO_STRING_SVTOOLS_RTF_FROMAN;
            break;
        case FAMILY_SWISS:
            pStr = OOO_STRING_SVTOOLS_RTF_FSWISS;
            break;
        case FAMILY_MODERN:
            pStr = OOO_STRING_SVTOOLS_RTF_FMODERN;
            break;
        case FAMILY_SCRIPT:
            pStr = OOO_STRING_SVTOOLS_RTF_FSCRIPT;
            break;
        case FAMILY_DECORATIVE:
            pStr = OOO_STRING_SVTOOLS_RTF_FDECOR;
            break;
        default:
            break;
    }
    rWrt.OutULong(nNo) << pStr << OOO_STRING_SVTOOLS_RTF_FPRQ;

    USHORT nVal = 0;
    switch (rFont.GetPitch())
    {
        case PITCH_FIXED:
            nVal = 1;
            break;
        case PITCH_VARIABLE:
            nVal = 2;
            break;
        default:
            nVal = 0;
            break;
    }
    rWrt.OutULong(nVal);

    sw::util::FontMapExport aRes(rFont.GetFamilyName());

    /*
     #i10538#
     In rtf the fontname is in the fontcharset, so if that isn't possible
     then bump the charset up to unicode
    */
    sal_uInt8 nChSet = 0;
    rtl_TextEncoding eChrSet = rFont.GetCharSet();
    nChSet = sw::ms::rtl_TextEncodingToWinCharset(eChrSet);
    eChrSet = rtl_getTextEncodingFromWindowsCharset(nChSet);
    if (!FontCharsetSufficient(aRes.msPrimary, aRes.msSecondary, eChrSet))
    {
        eChrSet = RTL_TEXTENCODING_UNICODE;
        nChSet = sw::ms::rtl_TextEncodingToWinCharset(eChrSet);
        eChrSet = rtl_getTextEncodingFromWindowsCharset(nChSet);
    }

    rWrt.Strm() << OOO_STRING_SVTOOLS_RTF_FCHARSET;
    rWrt.OutULong( nChSet );
    rWrt.Strm() << ' ';
    if (aRes.HasDistinctSecondary())
    {
        RTFOutFuncs::Out_Fontname(rWrt.Strm(), aRes.msPrimary, eChrSet,
            rWrt.bWriteHelpFmt);
        OutComment(rWrt, OOO_STRING_SVTOOLS_RTF_FALT) << ' ';
        RTFOutFuncs::Out_Fontname(rWrt.Strm(), aRes.msSecondary, eChrSet,
            rWrt.bWriteHelpFmt) << '}';
    }
    else
    {
        RTFOutFuncs::Out_Fontname(rWrt.Strm(), aRes.msPrimary, eChrSet,
            rWrt.bWriteHelpFmt);
    }
    rWrt.Strm() << ";}";
}

void SwRTFWriter::OutRTFFontTab()
{
    USHORT n = 0;
    const SfxItemPool& rPool = pDoc->GetAttrPool();
    const SvxFontItem* pFont = (const SvxFontItem*)GetDfltAttr(RES_CHRATR_FONT);

    Strm() << SwRTFWriter::sNewLine << '{' << OOO_STRING_SVTOOLS_RTF_FONTTBL;
    _OutFont( *this, *pFont, n++ );

    pFont = (const SvxFontItem*)rPool.GetPoolDefaultItem(RES_CHRATR_FONT);
    if (pFont)
        _OutFont(*this, *pFont, n++);

    PutNumFmtFontsInAttrPool();
    PutCJKandCTLFontsInAttrPool();

    sal_uInt32 nMaxItem = rPool.GetItemCount2(RES_CHRATR_FONT);
    for (sal_uInt32 nGet = 0; nGet < nMaxItem; ++nGet)
    {
        pFont = (const SvxFontItem*)rPool.GetItem2(RES_CHRATR_FONT, nGet);
        if (pFont)
            _OutFont(*this, *pFont, n++);
    }

    Strm() << '}';
}

void RTF_WrtRedlineAuthor::Write(Writer &rWrt)
{
    SwRTFWriter & rRTFWrt = (SwRTFWriter&)rWrt;

    rRTFWrt.Strm() << '{' << OOO_STRING_SVTOOLS_RTF_IGNORE << OOO_STRING_SVTOOLS_RTF_REVTBL << ' ';
    typedef std::vector<String>::iterator myiter;

    for(std::vector<String>::iterator aIter = maAuthors.begin(); aIter != maAuthors.end(); ++aIter)
    {
        rRTFWrt.Strm() << '{';
        // rWrt.bWriteHelpFmt
        RTFOutFuncs::Out_String( rRTFWrt.Strm(), *aIter, rRTFWrt.eDefaultEncoding,  rRTFWrt.bWriteHelpFmt  ) << ";}";
    }
    rRTFWrt.Strm() << '}' << SwRTFWriter::sNewLine;
}

bool SwRTFWriter::OutRTFRevTab()
{
    // Writes the revision author table
    int nRevAuthors = pDoc->GetRedlineTbl().Count();

    pRedlAuthors = new RTF_WrtRedlineAuthor;
    // RTF always seems to use Unknown as the default first entry
    String sUnknown(RTL_CONSTASCII_STRINGPARAM("Unknown"));
    pRedlAuthors->AddName(sUnknown);

    if (nRevAuthors < 1)
        return false;

    // pull out all the redlines and make a vector of all the author names
    for( USHORT i = 0; i < pDoc->GetRedlineTbl().Count(); ++i )
    {
        const SwRedline* pRedl = pDoc->GetRedlineTbl()[ i ];
        const String sAuthor = SW_MOD()->GetRedlineAuthor( pRedl->GetAuthor() );
        pRedlAuthors->AddName(sAuthor);
    }

    pRedlAuthors->Write(*this);
    return true;
}

//Takashi Ono for CJK
const rtl::OUString SwRTFWriter::XlateFmtName( const rtl::OUString &rName, SwGetPoolIdFromName eFlags )
{
#define RES_NONE RES_POOLCOLL_DOC_END

    static const RES_POOL_COLLFMT_TYPE aArr[]={
        RES_POOLCOLL_STANDARD, RES_POOLCOLL_HEADLINE1, RES_POOLCOLL_HEADLINE2,
        RES_POOLCOLL_HEADLINE3, RES_POOLCOLL_HEADLINE4, RES_POOLCOLL_HEADLINE5,
        RES_POOLCOLL_HEADLINE6, RES_POOLCOLL_HEADLINE7, RES_POOLCOLL_HEADLINE8,
        RES_POOLCOLL_HEADLINE9,

        RES_POOLCOLL_TOX_IDX1, RES_POOLCOLL_TOX_IDX2, RES_POOLCOLL_TOX_IDX3,
        RES_NONE, RES_NONE, RES_NONE, RES_NONE, RES_NONE, RES_NONE,
        RES_POOLCOLL_TOX_CNTNT1,

        RES_POOLCOLL_TOX_CNTNT2, RES_POOLCOLL_TOX_CNTNT3, RES_POOLCOLL_TOX_CNTNT4,
        RES_POOLCOLL_TOX_CNTNT5, RES_POOLCOLL_TOX_CNTNT6, RES_POOLCOLL_TOX_CNTNT7,
        RES_POOLCOLL_TOX_CNTNT8, RES_POOLCOLL_TOX_CNTNT9,
        RES_POOLCOLL_TEXT_IDENT, RES_POOLCOLL_FOOTNOTE,

        RES_NONE, RES_POOLCOLL_HEADER, RES_POOLCOLL_FOOTER, RES_POOLCOLL_TOX_IDXH,
        RES_POOLCOLL_LABEL, RES_POOLCOLL_TOX_ILLUSH, RES_POOLCOLL_JAKETADRESS, RES_POOLCOLL_SENDADRESS,
        RES_NONE, RES_NONE,

        RES_NONE, RES_NONE, RES_NONE, RES_POOLCOLL_ENDNOTE, RES_POOLCOLL_TOX_AUTHORITIESH, RES_NONE, RES_NONE,
        RES_POOLCOLL_BUL_LEVEL1, RES_POOLCOLL_BUL_LEVEL1, RES_POOLCOLL_NUM_LEVEL1,

        RES_POOLCOLL_BUL_LEVEL2, RES_POOLCOLL_BUL_LEVEL3, RES_POOLCOLL_BUL_LEVEL4, RES_POOLCOLL_BUL_LEVEL5,
        RES_POOLCOLL_BUL_LEVEL2, RES_POOLCOLL_BUL_LEVEL3, RES_POOLCOLL_BUL_LEVEL4, RES_POOLCOLL_BUL_LEVEL5,
        RES_POOLCOLL_NUM_LEVEL2, RES_POOLCOLL_NUM_LEVEL3, RES_POOLCOLL_NUM_LEVEL4, RES_POOLCOLL_NUM_LEVEL5,

        RES_POOLCOLL_DOC_TITEL, RES_NONE, RES_POOLCOLL_SIGNATURE, RES_NONE,
        RES_POOLCOLL_TEXT, RES_POOLCOLL_TEXT_MOVE, RES_NONE, RES_NONE,

        RES_NONE, RES_NONE, RES_NONE, RES_NONE, RES_POOLCOLL_DOC_SUBTITEL };

    static const sal_Char *stiName[] = {
        "Normal", "heading 1", "heading 2",
        "heading 3", "heading 4", "heading 5",
        "heading 6", "heading 7", "heading 8",
        "heading 9",

        "index 1", "index 2", "index 3",
        "index 4", "index 5", "index 6",
        "index 7", "index 8", "index 9",

        "toc 1", "toc 2", "toc 3",
        "toc 4", "toc 5", "toc 6",
        "toc 7", "toc 8", "toc 9",
        "Normal Indent", "footnote text",

        "annotation text", "header", "footer",  "index heading",
        "caption", "table of figures", "envelope address", "envelope return",
        "footnote reference",   "annotation reference",

        "line number", "page number", "endnote reference",  "endnote text", "table of authorities", "macro", "toa heading",
        "List", "List Bullet", "List Number",

        "List 2", "List 3", "List 4", "List 5",
        "List Bullet 2", "List Bullet 3", "List Bullet 4", "List Bullet 5",
        "List Number 2", "List Number 3", "List Number 4", "List Number 5",

        "Title", "Closing", "Signature", "Default Paragraph Font",
        "Body Text", "Body Text Indent", "List Continue",

        "List Continue 2",  "List Continue 3", "List Continue 4", "List Continue 5", "Message Header", "Subtitle"};

    ASSERT( ( sizeof( aArr ) / sizeof( RES_POOL_COLLFMT_TYPE ) == 75 ),
            "Style-UEbersetzungstabelle hat falsche Groesse" );
    ASSERT( ( sizeof( stiName ) / sizeof( *stiName ) == 75 ),
            "Style-UEbersetzungstabelle hat falsche Groesse" );

    sal_uInt16 idcol = ::SwStyleNameMapper::GetPoolIdFromUIName( rName, eFlags );
    if (idcol==USHRT_MAX) //#i40770# user defined style names get lost
        return rName;

    for (size_t i = 0; i < sizeof( aArr ) / sizeof( *aArr ); i++)
    {
        if ( idcol == aArr[i] )
        {
            return rtl::OUString::createFromAscii(stiName[i]);
        }
    }
    return ::SwStyleNameMapper::GetProgName( idcol, String() );
}

void SwRTFWriter::OutRTFStyleTab()
{
    USHORT n;

    // das 0-Style ist das Default, wird nie ausgegeben !!
    USHORT nArrLen = pDoc->GetTxtFmtColls()->Count();
    if( nArrLen <= 1 && pDoc->GetCharFmts()->Count() <= 1 )
        return;

    bOutStyleTab = TRUE;
    Strm() << SwRTFWriter::sNewLine << '{' << OOO_STRING_SVTOOLS_RTF_STYLESHEET;

    // das Default-TextStyle wird nicht mit ausgegeben !!
    for( n = 1; n < nArrLen; ++n )
    {
        const SwTxtFmtColl* pColl = (*pDoc->GetTxtFmtColls())[ n ];
        pAttrSet = &pColl->GetAttrSet();

        Strm() << '{';
         // gebe Attribute aus
        OutRTF_SwFmt( *this, *pColl );

        if( pColl->DerivedFrom() )
            // suche die Id vom "Parent" Format
            for( USHORT nBasedOn=1; nBasedOn < nArrLen; nBasedOn++)
                if( (*pDoc->GetTxtFmtColls())[ nBasedOn ] ==
                        pColl->DerivedFrom() )
                {
                    // die Ableitung vom Format
                    Strm() << OOO_STRING_SVTOOLS_RTF_SBASEDON;
                    OutULong( nBasedOn );
                    break;
                }

        if( pColl == &pColl->GetNextTxtFmtColl() )
        {
            Strm() << OOO_STRING_SVTOOLS_RTF_SNEXT;
            OutULong( n );
        }
        else
            // suche die Id vom "Naechsten" Format
            for( USHORT nNext=1; nNext < nArrLen; nNext++)
                if( (*pDoc->GetTxtFmtColls())[ nNext ] ==
                        &pColl->GetNextTxtFmtColl() )
                {
                    // die Ableitung vom Format
                    Strm() << OOO_STRING_SVTOOLS_RTF_SNEXT;
                    OutULong( nNext );
                    break;
                }

        //if( NO_NUMBERING != pColl->GetOutlineLevel() )//#outline level,zhaojianwei
        if(pColl->IsAssignedToListLevelOfOutlineStyle())//<-end,zhaojianwei
        {
            Strm() << '{' << OOO_STRING_SVTOOLS_RTF_IGNORE << OOO_STRING_SVTOOLS_RTF_SOUTLVL;
            //OutULong( pColl->GetOutlineLevel() ) << '}';//#outline level,zhaojianwei
            OutULong( pColl->GetAssignedOutlineStyleLevel() ) << '}';//<-end,zhaojianwei
        }

        Strm() << ' ';
        RTFOutFuncs::Out_String( Strm(), XlateFmtName( pColl->GetName(), nsSwGetPoolIdFromName::GET_POOLID_TXTCOLL ), eDefaultEncoding,
                        bWriteHelpFmt ) << ";}" << SwRTFWriter::sNewLine;
    }

    USHORT nChrArrLen = pDoc->GetCharFmts()->Count();
    for( n = 1; n < nChrArrLen; ++n )
    {
        const SwCharFmt* pFmt = (*pDoc->GetCharFmts())[ n ];
        pAttrSet = &pFmt->GetAttrSet();

        Strm() << '{';
         // gebe Attribute aus
        OutRTF_SwFmt( *this, *pFmt );

        if( pFmt->DerivedFrom() )
            // suche die Id vom "Parent" Format
            for( USHORT nBasedOn=1; nBasedOn < nChrArrLen; nBasedOn++)
                if( (*pDoc->GetCharFmts())[ nBasedOn ] ==
                        pFmt->DerivedFrom() )
                {
                    // die Ableitung vom Format
                    Strm() << OOO_STRING_SVTOOLS_RTF_SBASEDON;
                    OutULong( nArrLen + nBasedOn );
                    break;
                }

        Strm() << ' ';
        RTFOutFuncs::Out_String( Strm(), XlateFmtName( pFmt->GetName(), nsSwGetPoolIdFromName::GET_POOLID_CHRFMT ), eDefaultEncoding,
                    bWriteHelpFmt ) << ";}" << SwRTFWriter::sNewLine;
    }

    Strm() << '}';
    bOutStyleTab = FALSE;
}

bool ExportAsInline(const SwFlyFrmFmt& rFlyFrmFmt)
{
    //if not an inline element (hack in our limitations here as to only
    //graphics like this!!!!
    return rFlyFrmFmt.GetAnchor().GetAnchorId() == FLY_AS_CHAR;
}

void SwRTFWriter::OutRTFFlyFrms(const SwFlyFrmFmt& rFlyFrmFmt)
{
    // ein FlyFrame wurde erkannt, gebe erstmal den aus

    // Hole vom Node und vom letzten Node die Position in der Section
    const SwFmtCntnt& rFlyCntnt = rFlyFrmFmt.GetCntnt();

    ULONG nStt = rFlyCntnt.GetCntntIdx()->GetIndex()+1;
    ULONG nEnd = pDoc->GetNodes()[ nStt - 1 ]->EndOfSectionIndex();

    if( nStt >= nEnd )      // kein Bereich, also kein gueltiger Node
        return;

    if (!ExportAsInline(rFlyFrmFmt))
        Strm() << SwRTFWriter::sNewLine << OOO_STRING_SVTOOLS_RTF_PARD << OOO_STRING_SVTOOLS_RTF_PLAIN;
    //If we are only exporting an inline graphic/object then we
    //only need the its pFlyFmt for the duration of exporting it
    //for floating objects its a little more complex at the moment
    const SwFlyFrmFmt *pOldFlyFmt = pFlyFmt;
    pFlyFmt = &rFlyFrmFmt;

    {
        RTFSaveData aSaveData( *this, nStt, nEnd );
        Out_SwDoc( pCurPam );
    }

    if (!ExportAsInline(rFlyFrmFmt))
        Strm() << OOO_STRING_SVTOOLS_RTF_PARD << SwRTFWriter::sNewLine;
//#i46098#:    else
        pFlyFmt = pOldFlyFmt;
}



void SwRTFWriter::OutRedline( xub_StrLen nCntntPos )
{
        const SwRedline *pCurRedline = 0;
        USHORT nCount = pDoc->GetRedlineTbl().Count();

        if (nCurRedline < nCount)
        {
            pCurRedline = pDoc->GetRedlineTbl()[nCurRedline];
            if(pCurRedline)
            {
                const SwPosition* pStartPos = pCurRedline->Start();
                const SwPosition* pEndPos = pStartPos == pCurRedline->GetPoint()
                                            ? pCurRedline->GetMark()
                                            : pCurRedline->GetPoint();

                USHORT nStart = pStartPos->nContent.GetIndex();
                USHORT nEnd = pEndPos->nContent.GetIndex();

                ULONG nCurPam  = pCurPam->GetPoint()->nNode.GetIndex();
                ULONG nStartIndex = pStartPos->nNode.GetIndex();
                ULONG nEndIndex = pEndPos->nNode.GetIndex();
                const String& rStr = pCurPam->GetNode()->GetTxtNode()->GetTxt();
                xub_StrLen nEnde = rStr.Len();

                bool bSpanRedline = (nCurPam >= nStartIndex) && (nCurPam <= nEndIndex) && (nStartIndex != nEndIndex);

                if ((bSpanRedline && nCntntPos == 0) ||
                    (nStartIndex == nCurPam && nStart == nCntntPos))
                {
                    // We are at the start of a redline just need to find out which type
                    Strm() << '{';
                    if(pCurRedline->GetType() == nsRedlineType_t::REDLINE_INSERT)
                    {
                        Strm() << OOO_STRING_SVTOOLS_RTF_REVISED;
                        Strm() << OOO_STRING_SVTOOLS_RTF_REVAUTH;
                        String sName = SW_MOD()->GetRedlineAuthor(pCurRedline->GetAuthor());
                        OutLong( pRedlAuthors->AddName(sName) );
                        Strm() << OOO_STRING_SVTOOLS_RTF_REVDTTM;
                        OutLong( sw::ms::DateTime2DTTM(pCurRedline->GetTimeStamp()) );
                        Strm() << ' ';
                    }
                    else if(pCurRedline->GetType() == nsRedlineType_t::REDLINE_DELETE)
                    {
                        Strm() << OOO_STRING_SVTOOLS_RTF_DELETED;
                        Strm() << OOO_STRING_SVTOOLS_RTF_REVAUTHDEL;
                        String sDelName = SW_MOD()->GetRedlineAuthor(pCurRedline->GetAuthor());
                        OutLong( pRedlAuthors->AddName(sDelName) );
                        Strm() << OOO_STRING_SVTOOLS_RTF_REVDTTMDEL;
                        OutLong( sw::ms::DateTime2DTTM(pCurRedline->GetTimeStamp()) );
                        Strm() << ' ';
                    }
                }

                // this is either then of the end of the node or the end of the redline
                // time to close off this one
                if( (bSpanRedline && nCntntPos == nEnde) ||
                    (nEndIndex == nCurPam && nEnd == nCntntPos) )
                {
                    Strm() << '}';
                }

                // We have come to the end of a redline move to the next one
                // and use resursion to see if another redline starts here
                if (nEndIndex == nCurPam && nEnd == nCntntPos)
                {
                    nCurRedline++;
                    OutRedline(nCntntPos);
                }
            }
        }
}

void SwRTFWriter::OutBookmarks( xub_StrLen nCntntPos)
{
    IDocumentMarkAccess* const pMarkAccess = pDoc->getIDocumentMarkAccess();
    if (-1 == nBkmkTabPos)
        return;

    const ::sw::mark::IMark* pBookmark = (pMarkAccess->getMarksBegin() + nBkmkTabPos)->get();
    if(!pBookmark)
        return;

    const SwPosition* pStartPos = &pBookmark->GetMarkStart();
    const SwPosition* pEndPos = &pBookmark->GetMarkEnd();

    ASSERT(pStartPos && pEndPos, "Impossible");
    if (!(pStartPos && pEndPos))
        return;

    if (pStartPos->nNode.GetIndex() == pCurPam->GetPoint()->nNode.GetIndex() &&
        pStartPos->nContent.GetIndex() == nCntntPos)
    {
        // zur Zeit umspannt das SwBookmark keinen Bereich also kann
        // es hier vollstaendig ausgegeben werden.

        // erst die SWG spezifischen Daten:
        const ::sw::mark::IBookmark* const pAsBookmark = dynamic_cast< const ::sw::mark::IBookmark* >(pBookmark);
        if(pAsBookmark && (pAsBookmark->GetShortName().getLength() || pAsBookmark->GetKeyCode().GetCode()))
        {
            OutComment( *this, OOO_STRING_SVTOOLS_RTF_BKMKKEY );
            OutULong( ( pAsBookmark->GetKeyCode().GetCode() |
                     pAsBookmark->GetKeyCode().GetModifier() ));
            if( !pAsBookmark->GetShortName().getLength() )
                Strm() << "  " ;
            else
            {
                Strm() << ' ';
                OutRTF_AsByteString( *this, pAsBookmark->GetShortName(), eDefaultEncoding );
            }
            Strm() << '}';
        }
        OutComment( *this, OOO_STRING_SVTOOLS_RTF_BKMKSTART ) << ' ';
        RTFOutFuncs::Out_String( Strm(), pBookmark->GetName(),
                                eDefaultEncoding, bWriteHelpFmt ) << '}';
    }

    if (pEndPos->nNode.GetIndex() == pCurPam->GetPoint()->nNode.GetIndex() &&
        pEndPos->nContent.GetIndex() == nCntntPos)
    {
        // zur Zeit umspannt das SwBookmark keinen Bereich also kann
        // es hier vollstaendig ausgegeben werden.

        // erst die SWG spezifischen Daten:
        const ::sw::mark::IBookmark* const pAsBookmark = dynamic_cast< const ::sw::mark::IBookmark* >(pBookmark);
        if(pAsBookmark && (pAsBookmark->GetShortName().getLength() || pAsBookmark->GetKeyCode().GetCode()))
        {
            OutComment( *this, OOO_STRING_SVTOOLS_RTF_BKMKKEY );
            OutULong( ( pAsBookmark->GetKeyCode().GetCode() |
                     pAsBookmark->GetKeyCode().GetModifier() ));
            if( !pAsBookmark->GetShortName().getLength() )
                Strm() << "  " ;
            else
            {
                Strm() << ' ';
                OutRTF_AsByteString( *this, pAsBookmark->GetShortName(), eDefaultEncoding );
            }
            Strm() << '}';
        }
        OutComment( *this, OOO_STRING_SVTOOLS_RTF_BKMKEND ) << ' ';

        {
            ::rtl::OUString aEmpty;
            ::rtl::OUString & rBookmarkName = aEmpty;

            if (pAsBookmark)
                rBookmarkName = pAsBookmark->GetName();

            RTFOutFuncs::Out_String( Strm(), rBookmarkName,
                                eDefaultEncoding, bWriteHelpFmt ) << '}';
        }

        if(++nBkmkTabPos >= pMarkAccess->getMarksCount())
            nBkmkTabPos = -1;
        else
            pBookmark = (pMarkAccess->getMarksBegin() + nBkmkTabPos)->get();
    }
}

void SwRTFWriter::OutFlyFrm()
{
    USHORT n;

    if( !pFlyPos )
        return;

    // gebe alle freifliegenden Rahmen die sich auf den akt. Absatz
    // und evt. auf das aktuelle Zeichen beziehen, aus.

    // suche nach dem Anfang der FlyFrames
    for( n = 0; n < pFlyPos->Count() &&
            (*pFlyPos)[n]->GetNdIndex().GetIndex() <
                pCurPam->GetPoint()->nNode.GetIndex(); ++n )
        ;
    if( n < pFlyPos->Count() )
        while( n < pFlyPos->Count() &&
                pCurPam->GetPoint()->nNode.GetIndex() ==
                    (*pFlyPos)[n]->GetNdIndex().GetIndex() )
        {
            // den Array-Iterator weiterschalten, damit nicht doppelt
            // ausgegeben wird !!
            OutRTFFlyFrms( (const SwFlyFrmFmt&)(*pFlyPos)[n++]->GetFmt() );
        }
}


USHORT SwRTFWriter::GetId( const Color& rColor ) const
{
    ASSERT( pColTbl, "Where's color table?" );
    for( size_t n = 0; n < pColTbl->size(); n++ )
        if( rColor == (*pColTbl)[ n ] )
            return n;

    ASSERT( FALSE, "Color not exists in the pColTbl table" );
    return 0;
}

USHORT SwRTFWriter::GetId( const SvxFontItem& rFont ) const
{
    const SfxItemPool& rPool = pDoc->GetAttrPool();
    const SvxFontItem* pFont = (const SvxFontItem*)GetDfltAttr( RES_CHRATR_FONT );
    if( rFont == *pFont )
        return 0;

    USHORT n = 1;
    if( 0 != ( pFont = (const SvxFontItem*)rPool.GetPoolDefaultItem(
                                                        RES_CHRATR_FONT )))
    {
        if( rFont == *pFont )
            return 1;
        ++n;
    }

    sal_uInt32 nMaxItem = rPool.GetItemCount2( RES_CHRATR_FONT );
    for( sal_uInt32 nGet = 0; nGet < nMaxItem; ++nGet )
        if( 0 != (pFont = (const SvxFontItem*)rPool.GetItem2(
            RES_CHRATR_FONT, nGet )) )
        {
            if( rFont == *pFont )
                return n;
            ++n;
        }

    ASSERT( FALSE, "Font nicht in der Tabelle" );
    return 0;
}

USHORT SwRTFWriter::GetId( const Font& rFont ) const
{
    return GetId( SvxFontItem( rFont.GetFamily(), rFont.GetName(),
                                rFont.GetStyleName(), rFont.GetPitch(),
                                rFont.GetCharSet(),
                                RES_CHRATR_FONT ) );
}

USHORT SwRTFWriter::GetId( const SwTxtFmtColl& rColl ) const
{
    // suche das angegebene Format
    const SvPtrarr & rArr = *pDoc->GetTxtFmtColls();
    for( USHORT n = 0; n < rArr.Count(); n++ )
        if( (SwTxtFmtColl*)rArr[ n ] == &rColl )
            return n;
    ASSERT( FALSE, "TextCollection nicht in der Tabelle" );
    return 0;
}

USHORT SwRTFWriter::GetId( const SwCharFmt& rFmt ) const
{
    // suche das angegebene Format
    const SvPtrarr & rArr = *pDoc->GetCharFmts();
    for( USHORT n = 0; n < rArr.Count(); n++ )
        if( (SwCharFmt*)rArr[ n ] == &rFmt )
            return n + pDoc->GetTxtFmtColls()->Count();
    ASSERT( FALSE, "CharDFFormat nicht in der Tabelle" );
    return 0;
}

void SwRTFWriter::OutPageDesc()
{
    // Ausgabe der Page-Descriptoren
    USHORT nSize = pDoc->GetPageDescCnt();
    if( !nSize )
        return;

    Strm() << SwRTFWriter::sNewLine;        // ein Trenner
    bOutPageDesc = bOutPageDescTbl = TRUE;
    OutComment( *this, OOO_STRING_SVTOOLS_RTF_PGDSCTBL );
    for( USHORT n = 0; n < nSize; ++n )
    {
        const SwPageDesc& rPageDesc =
            const_cast<const SwDoc*>(pDoc)->GetPageDesc( n );

        Strm() << SwRTFWriter::sNewLine << '{' << OOO_STRING_SVTOOLS_RTF_PGDSC;
        OutULong( n ) << OOO_STRING_SVTOOLS_RTF_PGDSCUSE;
        OutULong( rPageDesc.ReadUseOn() );

        OutRTFPageDescription( rPageDesc, FALSE, FALSE );

        // suche den Folge-PageDescriptor:
        USHORT i = nSize;
        while( i  )
            if( rPageDesc.GetFollow() ==
                &const_cast<const SwDoc *>(pDoc)->GetPageDesc( --i ) )
                break;
        Strm() << OOO_STRING_SVTOOLS_RTF_PGDSCNXT;
        OutULong( i ) << ' ';
        RTFOutFuncs::Out_String( Strm(), XlateFmtName( rPageDesc.GetName(), nsSwGetPoolIdFromName::GET_POOLID_PAGEDESC ), eDefaultEncoding,
                    bWriteHelpFmt ) << ";}";
    }
    Strm() << '}' << SwRTFWriter::sNewLine;
    bOutPageDesc = bOutPageDescTbl = FALSE;
}

void SwRTFWriter::OutRTFBorder(const SvxBorderLine* aLine, const USHORT nSpace )
{
    // M.M. This function writes out border lines in RTF similar to what
    // WW8_BRC SwWW8Writer::TranslateBorderLine does in the winword filter
    // Eventually it would be nice if all this functionality was in the one place
    int nDistance = aLine->GetDistance();
    int nOutWidth = aLine->GetOutWidth();
    int nInWidth = aLine->GetInWidth();
    int nWidth = aLine->GetOutWidth();

    if(nDistance == 0)  // Single Line
        Strm() << OOO_STRING_SVTOOLS_RTF_BRDRS;
    else                // Double Line
    {
        if(nOutWidth == nInWidth)
            Strm() << OOO_STRING_SVTOOLS_RTF_BRDRDB;
        else if (nOutWidth > nInWidth)
            Strm() << OOO_STRING_SVTOOLS_RTF_BRDRTNTHSG;
        else if (nOutWidth < nInWidth)
            Strm() << OOO_STRING_SVTOOLS_RTF_BRDRTHTNSG;
    }
    Strm() << OOO_STRING_SVTOOLS_RTF_BRDRW;
    OutULong(nWidth);

    Strm() << OOO_STRING_SVTOOLS_RTF_BRSP;
    OutULong(nSpace);
}

void SwRTFWriter::OutRTFBorders(SvxBoxItem aBox)
{
    const SvxBorderLine *pLine = aBox.GetTop();
    if(pLine)
    {
        Strm() << OOO_STRING_SVTOOLS_RTF_PGBRDRT;
        OutRTFBorder(pLine, aBox.GetDistance(BOX_LINE_TOP));
    }

    pLine = aBox.GetBottom();
    if(pLine)
    {
        Strm() << OOO_STRING_SVTOOLS_RTF_PGBRDRB;
        OutRTFBorder(pLine, aBox.GetDistance(BOX_LINE_BOTTOM));
    }

    pLine = aBox.GetRight();
    if(pLine)
    {
        Strm() << OOO_STRING_SVTOOLS_RTF_PGBRDRR;
        OutRTFBorder(pLine, aBox.GetDistance(BOX_LINE_LEFT));
    }

    pLine = aBox.GetLeft();
    if(pLine)
    {
        Strm() << OOO_STRING_SVTOOLS_RTF_PGBRDRL;
        OutRTFBorder(pLine, aBox.GetDistance(BOX_LINE_RIGHT));
    }
}

void SwRTFWriter::OutRTFPageDescription( const SwPageDesc& rPgDsc,
                                            BOOL bWriteReset,
                                            BOOL bCheckForFirstPage )
{
    // jetzt noch den Teil fuer alle anderen Applikationen:
    const SwPageDesc *pSave = pAktPageDesc;
    bool bOldOut = bOutPageDesc;
    bool bOldHDFT = bOutLeftHeadFoot;

    // falls es einen Follow gibt,
    pAktPageDesc = &rPgDsc;
    if( bCheckForFirstPage && pAktPageDesc->GetFollow() &&
        pAktPageDesc->GetFollow() != pAktPageDesc )
        pAktPageDesc = pAktPageDesc->GetFollow();

    bOutPageDesc = TRUE;
    bOutLeftHeadFoot = FALSE;

    if( bWriteReset )
    {
        if( bFirstLine && bWriteAll &&
            pCurPam->GetPoint()->nNode == pOrigPam->Start()->nNode )
            Strm() << OOO_STRING_SVTOOLS_RTF_SECTD << OOO_STRING_SVTOOLS_RTF_SBKNONE;
        else
            Strm() << OOO_STRING_SVTOOLS_RTF_SECT << OOO_STRING_SVTOOLS_RTF_SECTD;
    }

    if( pAktPageDesc->GetLandscape() )
        Strm() << OOO_STRING_SVTOOLS_RTF_LNDSCPSXN;

    const SwFmt *pFmt = &pAktPageDesc->GetMaster(); //GetLeft();
    OutRTF_SwFmt( *this, *pFmt );

    SvxBoxItem aBox = pFmt->GetAttrSet().GetBox();
    OutRTFBorders(pFmt->GetAttrSet().GetBox());

    // falls es gesharte Heaer/Footer gibt, so gebe diese auch noch aus
    if (
        (nsUseOnPage::PD_MIRROR & pAktPageDesc->GetUseOn()) &&
        (!pAktPageDesc->IsFooterShared() || !pAktPageDesc->IsHeaderShared())
       )
    {
        bOutLeftHeadFoot = TRUE;
        const SfxPoolItem* pHt;
        if( !pAktPageDesc->IsHeaderShared() &&
            SFX_ITEM_SET == pAktPageDesc->GetLeft().GetAttrSet().
                    GetItemState( RES_HEADER, FALSE, &pHt ))
            OutRTF_SwFmtHeader( *this, *pHt );

        if( !pAktPageDesc->IsFooterShared() &&
            SFX_ITEM_SET == pAktPageDesc->GetLeft().GetAttrSet().
                    GetItemState( RES_FOOTER, FALSE, &pHt ))
            OutRTF_SwFmtFooter( *this, *pHt );
        bOutLeftHeadFoot = FALSE;
    }

    if( pAktPageDesc != &rPgDsc )
    {
        pAktPageDesc = &rPgDsc;
        Strm() << OOO_STRING_SVTOOLS_RTF_TITLEPG;

        // die Header/Footer der 1. Seite ausgeben
        const SfxPoolItem* pHt;
        if( SFX_ITEM_SET == pAktPageDesc->GetMaster().GetAttrSet().
                    GetItemState( RES_HEADER, FALSE, &pHt ))
            OutRTF_SwFmtHeader( *this, *pHt );

        if( SFX_ITEM_SET == pAktPageDesc->GetMaster().GetAttrSet().
                                GetItemState( RES_FOOTER, FALSE, &pHt ))
            OutRTF_SwFmtFooter( *this, *pHt );
    }

    pAktPageDesc = pSave;
    bOutPageDesc = bOldOut;
    bOutLeftHeadFoot = bOldHDFT;
}

BOOL SwRTFWriter::OutBreaks( const SfxItemSet& rSet )
{
    // dann nie Seitenumbrueche ausgeben
    BOOL bPgDscWrite = FALSE;

    if( !bOutOutlineOnly && bOutPageAttr && !bIgnoreNextPgBreak)
    {
        const SfxPoolItem *pItem;
        if( SFX_ITEM_SET == rSet.GetItemState( RES_PAGEDESC, TRUE, &pItem )
            && ((SwFmtPageDesc*)pItem)->GetPageDesc() )
        {
            const SwFmtPageDesc& rPgDsc = *(SwFmtPageDesc*)pItem;
            for( USHORT nPos = pDoc->GetPageDescCnt(); nPos; )
                if( &const_cast<const SwDoc *>(pDoc)
                    ->GetPageDesc( --nPos ) == rPgDsc.GetPageDesc() )
                {
                    pAktPageDesc = ((SwFmtPageDesc*)pItem)->GetPageDesc();
                                                // FALSE wegen schliessender Klammer !!
                    OutComment( *this, OOO_STRING_SVTOOLS_RTF_PGDSCNO, FALSE );
                    OutULong( nPos ) << '}';

                    // nicht weiter, in Styles gibts keine SectionControls !!
                    if( !bOutStyleTab )
                        OutRTFPageDescription( *rPgDsc.GetPageDesc(),
                                                        TRUE, TRUE );
                    bPgDscWrite = TRUE;
                    break;
                }
        }
        else if( SFX_ITEM_SET == rSet.GetItemState( RES_BREAK, TRUE, &pItem ) )
        {
            const SvxFmtBreakItem &rBreak = *(SvxFmtBreakItem*)pItem;
            if( bWriteHelpFmt )
            {
                if( SVX_BREAK_PAGE_BEFORE == rBreak.GetBreak() ||
                    SVX_BREAK_PAGE_AFTER == rBreak.GetBreak() ||
                    SVX_BREAK_PAGE_BOTH == rBreak.GetBreak() )
                {
                    bOutFmtAttr = true;
                    Strm() << OOO_STRING_SVTOOLS_RTF_PAGE;
                }
            }
            else
            {
                switch( rBreak.GetBreak() )
                {
                    case SVX_BREAK_COLUMN_BEFORE:
                    case SVX_BREAK_COLUMN_AFTER:
                    case SVX_BREAK_COLUMN_BOTH:
                        break;
                    case SVX_BREAK_PAGE_BEFORE:
                        bOutFmtAttr = true;
                        Strm() << OOO_STRING_SVTOOLS_RTF_PAGE;
                        break;
                    case SVX_BREAK_PAGE_AFTER:
                        OutComment(*this, OOO_STRING_SVTOOLS_RTF_PGBRK, false) << "0}";
                        break;
                    case SVX_BREAK_PAGE_BOTH:
                        OutComment(*this, OOO_STRING_SVTOOLS_RTF_PGBRK, false) << "1}";
                        break;
                    default:
                        break;
                }
            }
        }
    }
    bIgnoreNextPgBreak = false;
    return bPgDscWrite;
}


void SwRTFWriter::CheckEndNodeForSection( const SwNode& rNd )
{
    const SwSectionNode* pSectNd = rNd.StartOfSectionNode()->GetSectionNode();
    if( pSectNd /*&& CONTENT_SECTION == pSectNd->GetSection().GetType()*/ )
    {
        const SwSectionFmt* pSectFmt = pSectNd->GetSection().GetFmt();

        // diese Section hatte den akt. Abschnitt bestimmt
        // wer bestimmt den nachsten??
        SwNodeIndex aIdx( rNd, 1 );
        pSectNd = aIdx.GetNode().GetSectionNode();
        if( !( ( pSectNd || (aIdx.GetNode().IsEndNode() &&
            0 != ( pSectNd = aIdx.GetNode().StartOfSectionNode()->GetSectionNode() )) )
            /*&& CONTENT_SECTION == pSectNd->GetSection().GetType()*/ ))
        {
            // wer bestimmt denn nun den neuen Abschnitt?
            // PageDesc oder eine uebergeordnete Section?
            SwSection* pParent = pSectFmt->GetParentSection();
//          while( pParent /*&& CONTENT_SECTION != pParent->GetType()*/ )
//              pParent = pParent->GetParent();

            if( pParent /*&& CONTENT_SECTION == pParent->GetType()*/ )
                OutRTF_SwSectionNode( *this, *pParent->
                        GetFmt()->GetSectionNode( TRUE ) );
            else
            {
                if (! bOutPageDesc)
                {
                    Strm() << OOO_STRING_SVTOOLS_RTF_SECT << OOO_STRING_SVTOOLS_RTF_SECTD << OOO_STRING_SVTOOLS_RTF_SBKNONE;
                    OutRTFPageDescription( ( pAktPageDesc
                                            ? *pAktPageDesc
                                            : const_cast<const SwDoc *>(pDoc)
                                            ->GetPageDesc(0) ),
                                          FALSE, TRUE );
                    Strm() << SwRTFWriter::sNewLine;
                }
            }
        }
        // else
            // weiter machen, der naechste definiert den neuen Abschnitt
    }
}

// Struktur speichert die aktuellen Daten des Writers zwischen, um
// einen anderen Dokument-Teil auszugeben, wie z.B. Header/Footer
RTFSaveData::RTFSaveData( SwRTFWriter& rWriter, ULONG nStt, ULONG nEnd )
    : rWrt( rWriter ),
    pOldPam( rWrt.pCurPam ), pOldEnd( rWrt.GetEndPaM() ),
    pOldFlyFmt( rWrt.pFlyFmt ), pOldPageDesc( rWrt.pAktPageDesc ),
    pOldAttrSet( rWrt.GetAttrSet() )
{
    bOldWriteAll = rWrt.bWriteAll;
    bOldOutTable = rWrt.bOutTable;
    bOldOutPageAttr = rWrt.bOutPageAttr;
    bOldAutoAttrSet = rWrt.bAutoAttrSet;
    bOldOutSection = rWrt.bOutSection;

    rWrt.pCurPam = rWrt.NewSwPaM( *rWrt.pDoc, nStt, nEnd );

    // Tabelle in Sonderbereichen erkennen
    if( nStt != rWrt.pCurPam->GetMark()->nNode.GetIndex() &&
        rWrt.pDoc->GetNodes()[ nStt ]->IsTableNode() )
        rWrt.pCurPam->GetMark()->nNode = nStt;

    rWrt.SetEndPaM( rWrt.pCurPam );
    rWrt.pCurPam->Exchange( );
    rWrt.bWriteAll = TRUE;
    rWrt.bOutTable = FALSE;
    rWrt.bOutPageAttr = FALSE;
    rWrt.SetAttrSet( 0 );
    rWrt.bAutoAttrSet = FALSE;
    rWrt.bOutSection = FALSE;
}


RTFSaveData::~RTFSaveData()
{
    delete rWrt.pCurPam;                    // Pam wieder loeschen

    rWrt.pCurPam = pOldPam;
    rWrt.SetEndPaM( pOldEnd );
    rWrt.bWriteAll = bOldWriteAll;
    rWrt.bOutTable = bOldOutTable;
    rWrt.pFlyFmt = pOldFlyFmt;
    rWrt.pAktPageDesc = pOldPageDesc;
    rWrt.SetAttrSet( pOldAttrSet );
    rWrt.bAutoAttrSet = bOldAutoAttrSet;
    rWrt.bOutPageAttr = bOldOutPageAttr;
    rWrt.bOutSection = bOldOutSection;
}

extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL ExportRTF( const String& rFltName, const String& rBaseURL, WriterRef& xRet )
{
    xRet = new SwRTFWriter( rFltName, rBaseURL );
}

short SwRTFWriter::GetCurrentPageDirection() const
{
    const SwFrmFmt  &rFmt = pAktPageDesc
        ? pAktPageDesc->GetMaster()
        : const_cast<const SwDoc *>(pDoc)
        ->GetPageDesc(0).GetMaster();
    const SvxFrameDirectionItem* pItem = &rFmt.GetFrmDir();

    if (!pItem)
    {
        pItem = (const SvxFrameDirectionItem*)
            &pDoc->GetAttrPool().GetDefaultItem(RES_FRAMEDIR);
    }
    return pItem->GetValue();
}

short SwRTFWriter::TrueFrameDirection(const SwFrmFmt &rFlyFmt) const
{
    const SwFrmFmt *pFlyFmt2 = &rFlyFmt;
    const SvxFrameDirectionItem* pItem = 0;
    while (pFlyFmt2)
    {
        pItem = &pFlyFmt2->GetFrmDir();
        if (FRMDIR_ENVIRONMENT == pItem->GetValue())
        {
            pItem = 0;
            const SwFmtAnchor* pAnchor = &pFlyFmt2->GetAnchor();
            if ((FLY_AT_PAGE != pAnchor->GetAnchorId()) &&
                pAnchor->GetCntntAnchor() )
            {
                pFlyFmt2 = pAnchor->GetCntntAnchor()->nNode.
                                    GetNode().GetFlyFmt();
            }
            else
                pFlyFmt2 = 0;
        }
        else
            pFlyFmt2 = 0;
    }

    short nRet;
    if (pItem)
        nRet = pItem->GetValue();
    else
        nRet = GetCurrentPageDirection();

    ASSERT(nRet != FRMDIR_ENVIRONMENT, "leaving with environment direction");
    return nRet;
}

/* vi:set tabstop=4 shiftwidth=4 expandtab: */