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 | /* -*- 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 "rtfexport.hxx"
#include "rtfexportfilter.hxx"
#include "rtfsdrexport.hxx"
#include "rtfattributeoutput.hxx"
#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/i18n/ScriptType.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/XPropertySetInfo.hpp>
#include <docsh.hxx>
#include <viewsh.hxx>
#include <viewopt.hxx>
#include <fmtpdsc.hxx>
#include <ftninfo.hxx>
#include <fmthdft.hxx>
#include <editeng/colritem.hxx>
#include <editeng/udlnitem.hxx>
#include <editeng/fontitem.hxx>
#include <editeng/paperinf.hxx>
#include <editeng/brushitem.hxx>
#include <editeng/protitem.hxx>
#include <editeng/lrspitem.hxx>
#include <editeng/ulspitem.hxx>
#include <editeng/boxitem.hxx>
#include <editeng/shaditem.hxx>
#include <lineinfo.hxx>
#include <redline.hxx>
#include <swmodule.hxx>
#include <IDocumentLayoutAccess.hxx>
#include <comphelper/string.hxx>
#include <svtools/rtfkeywd.hxx>
#include <filter/msfilter/rtfutil.hxx>
#include <unotools/docinfohelper.hxx>
#include <osl/diagnose.h>
#include <rtl/tencinfo.h>
#include <sal/log.hxx>
#if OSL_DEBUG_LEVEL > 1
#include <iostream>
#endif
#include <svx/xflclit.hxx>
#include <fmtmeta.hxx>
#include <IDocumentSettingAccess.hxx>
#include <fmtfsize.hxx>
#include <ndtxt.hxx>
#include <numrule.hxx>
#include <frmatr.hxx>
#include <swtable.hxx>
#include <IMark.hxx>
using namespace ::com::sun::star;
// the default text encoding for the export, if it doesn't fit unicode will
// be used
#define DEF_ENCODING RTL_TEXTENCODING_ASCII_US
AttributeOutputBase& RtfExport::AttrOutput() const { return *m_pAttrOutput; }
MSWordSections& RtfExport::Sections() const { return *m_pSections; }
RtfSdrExport& RtfExport::SdrExporter() const { return *m_pSdrExport; }
bool RtfExport::CollapseScriptsforWordOk(sal_uInt16 nScript, sal_uInt16 nWhich)
{
// FIXME is this actually true for rtf? - this is copied from DOCX
if (nScript == i18n::ScriptType::ASIAN)
{
// for asian in ww8, there is only one fontsize
// and one fontstyle (posture/weight)
switch (nWhich)
{
case RES_CHRATR_FONTSIZE:
case RES_CHRATR_POSTURE:
case RES_CHRATR_WEIGHT:
return false;
default:
break;
}
}
else if (nScript != i18n::ScriptType::COMPLEX)
{
// for western in ww8, there is only one fontsize
// and one fontstyle (posture/weight)
switch (nWhich)
{
case RES_CHRATR_CJK_FONTSIZE:
case RES_CHRATR_CJK_POSTURE:
case RES_CHRATR_CJK_WEIGHT:
return false;
default:
break;
}
}
return true;
}
void RtfExport::AppendBookmarks(const SwTextNode& rNode, sal_Int32 nCurrentPos, sal_Int32 nLen)
{
std::vector<OUString> aStarts;
std::vector<OUString> aEnds;
IMarkVector aMarks;
if (GetBookmarks(rNode, nCurrentPos, nCurrentPos + nLen, aMarks))
{
for (const auto& pMark : aMarks)
{
const sal_Int32 nStart = pMark->GetMarkStart().nContent.GetIndex();
const sal_Int32 nEnd = pMark->GetMarkEnd().nContent.GetIndex();
if (nStart == nCurrentPos)
aStarts.push_back(pMark->GetName());
if (nEnd == nCurrentPos)
aEnds.push_back(pMark->GetName());
}
}
m_pAttrOutput->WriteBookmarks_Impl(aStarts, aEnds);
}
void RtfExport::AppendBookmark(const OUString& rName)
{
std::vector<OUString> aStarts;
std::vector<OUString> aEnds;
aStarts.push_back(rName);
aEnds.push_back(rName);
m_pAttrOutput->WriteBookmarks_Impl(aStarts, aEnds);
}
void RtfExport::AppendAnnotationMarks(const SwWW8AttrIter& rAttrs, sal_Int32 nCurrentPos,
sal_Int32 nLen)
{
std::vector<OUString> aStarts;
std::vector<OUString> aEnds;
IMarkVector aMarks;
if (GetAnnotationMarks(rAttrs, nCurrentPos, nCurrentPos + nLen, aMarks))
{
for (const auto& pMark : aMarks)
{
const sal_Int32 nStart = pMark->GetMarkStart().nContent.GetIndex();
const sal_Int32 nEnd = pMark->GetMarkEnd().nContent.GetIndex();
if (nStart == nCurrentPos)
aStarts.push_back(pMark->GetName());
if (nEnd == nCurrentPos)
aEnds.push_back(pMark->GetName());
}
}
m_pAttrOutput->WriteAnnotationMarks_Impl(aStarts, aEnds);
}
//For i120928,to export graphic of bullet for RTF filter
void RtfExport::ExportGrfBullet(const SwTextNode& /*rNd*/)
{
// Noop, would be too late, see WriteNumbering() instead.
}
void RtfExport::WriteChar(sal_Unicode /*c*/) { /* WriteChar() has nothing to do for rtf. */}
static bool IsExportNumRule(const SwNumRule& rRule)
{
sal_uInt8 nEnd = MAXLEVEL;
while (nEnd-- && !rRule.GetNumFormat(nEnd))
;
++nEnd;
sal_uInt8 nLvl;
for (nLvl = 0; nLvl < nEnd; ++nLvl)
{
const SwNumFormat* pNFormat = &rRule.Get(nLvl);
if (SVX_NUM_NUMBER_NONE != pNFormat->GetNumberingType() || !pNFormat->GetPrefix().isEmpty()
|| (!pNFormat->GetSuffix().isEmpty() && pNFormat->GetSuffix() != "."))
break;
}
return nLvl != nEnd;
}
void RtfExport::BuildNumbering()
{
const SwNumRuleTable& rListTable = m_pDoc->GetNumRuleTable();
SwNumRule* pOutlineRule = m_pDoc->GetOutlineNumRule();
if (IsExportNumRule(*pOutlineRule))
GetNumberingId(*pOutlineRule);
for (auto n = rListTable.size(); n;)
{
SwNumRule* pRule = rListTable[--n];
if (!SwDoc::IsUsed(*pRule))
continue;
if (IsExportNumRule(*pRule))
GetNumberingId(*pRule);
}
}
void RtfExport::WriteNumbering()
{
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " start");
if (!m_pUsedNumTable)
return; // no numbering is used
Strm()
.WriteChar('{')
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_IGNORE)
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_LISTTABLE);
CollectGrfsOfBullets();
if (!m_vecBulletPic.empty())
Strm()
.WriteChar('{')
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_IGNORE)
.WriteCharPtr(LO_STRING_SVTOOLS_RTF_LISTPICTURE);
BulletDefinitions();
if (!m_vecBulletPic.empty())
Strm().WriteChar('}');
AbstractNumberingDefinitions();
Strm().WriteChar('}');
Strm().WriteChar('{').WriteCharPtr(OOO_STRING_SVTOOLS_RTF_LISTOVERRIDETABLE);
NumberingDefinitions();
Strm().WriteChar('}');
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " end");
}
void RtfExport::WriteRevTab()
{
int nRevAuthors = m_pDoc->getIDocumentRedlineAccess().GetRedlineTable().size();
if (nRevAuthors < 1)
return;
// RTF always seems to use Unknown as the default first entry
GetRedline("Unknown");
for (SwRangeRedline* pRedl : m_pDoc->getIDocumentRedlineAccess().GetRedlineTable())
{
GetRedline(SW_MOD()->GetRedlineAuthor(pRedl->GetAuthor()));
}
// Now write the table
Strm()
.WriteChar('{')
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_IGNORE)
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_REVTBL)
.WriteChar(' ');
for (std::size_t i = 0; i < m_aRedlineTable.size(); ++i)
{
const OUString* pAuthor = GetRedline(i);
Strm().WriteChar('{');
if (pAuthor)
Strm().WriteCharPtr(
msfilter::rtfutil::OutString(*pAuthor, m_eDefaultEncoding).getStr());
Strm().WriteCharPtr(";}");
}
Strm().WriteChar('}').WriteCharPtr(SAL_NEWLINE_STRING);
}
void RtfExport::WriteHeadersFooters(sal_uInt8 nHeadFootFlags, const SwFrameFormat& rFormat,
const SwFrameFormat& rLeftFormat,
const SwFrameFormat& rFirstPageFormat, sal_uInt8 /*nBreakCode*/)
{
// headers
if (nHeadFootFlags & nsHdFtFlags::WW8_HEADER_EVEN)
WriteHeaderFooter(rLeftFormat, true, OOO_STRING_SVTOOLS_RTF_HEADERL);
if (nHeadFootFlags & nsHdFtFlags::WW8_HEADER_ODD)
WriteHeaderFooter(rFormat, true, OOO_STRING_SVTOOLS_RTF_HEADER);
if (nHeadFootFlags & nsHdFtFlags::WW8_HEADER_FIRST)
WriteHeaderFooter(rFirstPageFormat, true, OOO_STRING_SVTOOLS_RTF_HEADERF, true);
// footers
if (nHeadFootFlags & nsHdFtFlags::WW8_FOOTER_EVEN)
WriteHeaderFooter(rLeftFormat, false, OOO_STRING_SVTOOLS_RTF_FOOTERL);
if (nHeadFootFlags & nsHdFtFlags::WW8_FOOTER_ODD)
WriteHeaderFooter(rFormat, false, OOO_STRING_SVTOOLS_RTF_FOOTER);
if (nHeadFootFlags & nsHdFtFlags::WW8_FOOTER_FIRST)
WriteHeaderFooter(rFirstPageFormat, false, OOO_STRING_SVTOOLS_RTF_FOOTERF, true);
}
void RtfExport::OutputField(const SwField* pField, ww::eField eFieldType, const OUString& rFieldCmd,
FieldFlags nMode)
{
m_pAttrOutput->WriteField_Impl(pField, eFieldType, rFieldCmd, nMode);
}
void RtfExport::WriteFormData(const ::sw::mark::IFieldmark& /*rFieldmark*/)
{
SAL_INFO("sw.rtf", "TODO: " << OSL_THIS_FUNC);
}
void RtfExport::WriteHyperlinkData(const ::sw::mark::IFieldmark& /*rFieldmark*/)
{
SAL_INFO("sw.rtf", "TODO: " << OSL_THIS_FUNC);
}
void RtfExport::DoComboBox(const OUString& /*rName*/, const OUString& /*rHelp*/,
const OUString& /*rToolTip*/, const OUString& /*rSelected*/,
const uno::Sequence<OUString>& /*rListItems*/)
{
// this is handled in RtfAttributeOutput::OutputFlyFrame_Impl
}
void RtfExport::DoFormText(const SwInputField* pField)
{
OUString sResult = pField->ExpandField(true, nullptr);
const OUString& rHelp = pField->GetHelp();
OUString sName = pField->GetPar2();
const OUString& rStatus = pField->GetToolTip();
m_pAttrOutput->RunText().append("{" OOO_STRING_SVTOOLS_RTF_FIELD
"{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_FLDINST
"{ FORMTEXT }");
m_pAttrOutput->RunText().append(
"{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_FORMFIELD
" {" OOO_STRING_SVTOOLS_RTF_FFTYPE "0");
if (!rHelp.isEmpty())
m_pAttrOutput->RunText().append(OOO_STRING_SVTOOLS_RTF_FFOWNHELP);
if (!rStatus.isEmpty())
m_pAttrOutput->RunText().append(OOO_STRING_SVTOOLS_RTF_FFOWNSTAT);
m_pAttrOutput->RunText().append(OOO_STRING_SVTOOLS_RTF_FFTYPETXT "0");
if (!sName.isEmpty())
m_pAttrOutput->RunText()
.append("{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_FFNAME " ")
.append(msfilter::rtfutil::OutString(sName, m_eDefaultEncoding))
.append("}");
if (!rHelp.isEmpty())
m_pAttrOutput->RunText()
.append("{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_FFHELPTEXT " ")
.append(msfilter::rtfutil::OutString(rHelp, m_eDefaultEncoding))
.append("}");
m_pAttrOutput->RunText()
.append("{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_FFDEFTEXT " ")
.append(msfilter::rtfutil::OutString(sResult, m_eDefaultEncoding))
.append("}");
if (!rStatus.isEmpty())
m_pAttrOutput->RunText()
.append("{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_FFSTATTEXT " ")
.append(msfilter::rtfutil::OutString(rStatus, m_eDefaultEncoding))
.append("}");
m_pAttrOutput->RunText().append("}}}{" OOO_STRING_SVTOOLS_RTF_FLDRSLT " ");
m_pAttrOutput->RunText()
.append(msfilter::rtfutil::OutString(sResult, m_eDefaultEncoding))
.append("}}");
}
sal_uLong RtfExport::ReplaceCr(sal_uInt8 /*nChar*/)
{
// Completely unused for Rtf export... only here for code sharing
// purpose with binary export
return 0;
}
void RtfExport::WriteFonts()
{
Strm()
.WriteCharPtr(SAL_NEWLINE_STRING)
.WriteChar('{')
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_FONTTBL);
m_aFontHelper.WriteFontTable(*m_pAttrOutput);
Strm().WriteChar('}');
}
void RtfExport::WriteStyles()
{
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " start");
m_pStyles->OutputStylesTable();
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " end");
}
void RtfExport::WriteFootnoteSettings()
{
const SwPageFootnoteInfo& rFootnoteInfo = m_pDoc->GetPageDesc(0).GetFootnoteInfo();
// Request a separator only in case the width is larger than zero.
bool bSeparator = double(rFootnoteInfo.GetWidth()) > 0;
Strm()
.WriteChar('{')
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_IGNORE)
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_FTNSEP);
if (bSeparator)
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_CHFTNSEP);
Strm().WriteChar('}');
}
void RtfExport::WriteMainText()
{
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " start");
if (std::unique_ptr<SvxBrushItem> oBrush = getBackground(); oBrush)
{
Strm().WriteCharPtr(LO_STRING_SVTOOLS_RTF_VIEWBKSP).WriteChar('1');
Strm().WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_BACKGROUND);
Strm().WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_SHP);
Strm().WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_SHPINST);
std::vector<std::pair<OString, OString>> aProperties;
aProperties.push_back(std::make_pair<OString, OString>("shapeType", "1"));
aProperties.push_back(std::make_pair<OString, OString>(
"fillColor", OString::number(wwUtility::RGBToBGR(oBrush->GetColor()))));
for (const std::pair<OString, OString>& rPair : aProperties)
{
Strm().WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_SP "{");
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_SN " ");
Strm().WriteOString(rPair.first);
Strm().WriteCharPtr("}{" OOO_STRING_SVTOOLS_RTF_SV " ");
Strm().WriteOString(rPair.second);
Strm().WriteCharPtr("}}");
}
Strm().WriteChar('}'); // shpinst
Strm().WriteChar('}'); // shp
Strm().WriteChar('}'); // background
}
SwTableNode* pTableNode = m_pCurPam->GetNode().FindTableNode();
if (m_pWriter && m_pWriter->m_bWriteOnlyFirstTable && pTableNode != nullptr)
{
m_pCurPam->GetPoint()->nNode = *pTableNode;
m_pCurPam->GetMark()->nNode = *(pTableNode->EndOfSectionNode());
}
else
{
m_pCurPam->GetPoint()->nNode
= m_pDoc->GetNodes().GetEndOfContent().StartOfSectionNode()->GetIndex();
}
WriteText();
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " end");
}
void RtfExport::WriteInfo()
{
OString aGenerator
= OUStringToOString(utl::DocInfoHelper::GetGeneratorString(), RTL_TEXTENCODING_UTF8);
Strm()
.WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_IGNORE LO_STRING_SVTOOLS_RTF_GENERATOR " ")
.WriteOString(aGenerator)
.WriteChar('}');
Strm().WriteChar('{').WriteCharPtr(OOO_STRING_SVTOOLS_RTF_INFO);
SwDocShell* pDocShell(m_pDoc->GetDocShell());
uno::Reference<document::XDocumentProperties> xDocProps;
if (pDocShell)
{
uno::Reference<document::XDocumentPropertiesSupplier> xDPS(pDocShell->GetModel(),
uno::UNO_QUERY);
xDocProps.set(xDPS->getDocumentProperties());
}
if (xDocProps.is())
{
// Handle user-defined properties.
uno::Reference<beans::XPropertyContainer> xUserDefinedProperties
= xDocProps->getUserDefinedProperties();
if (xUserDefinedProperties.is())
{
uno::Reference<beans::XPropertySet> xPropertySet(xUserDefinedProperties,
uno::UNO_QUERY);
uno::Reference<beans::XPropertySetInfo> xPropertySetInfo
= xPropertySet->getPropertySetInfo();
// Do we have explicit markup in RTF for this property name?
if (xPropertySetInfo->hasPropertyByName("Company"))
{
OUString aValue;
xPropertySet->getPropertyValue("Company") >>= aValue;
OutUnicode(OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_COMPANY, aValue);
}
}
OutUnicode(OOO_STRING_SVTOOLS_RTF_TITLE, xDocProps->getTitle(), true);
OutUnicode(OOO_STRING_SVTOOLS_RTF_SUBJECT, xDocProps->getSubject());
OutUnicode(OOO_STRING_SVTOOLS_RTF_KEYWORDS,
::comphelper::string::convertCommaSeparated(xDocProps->getKeywords()));
OutUnicode(OOO_STRING_SVTOOLS_RTF_DOCCOMM, xDocProps->getDescription());
OutUnicode(OOO_STRING_SVTOOLS_RTF_AUTHOR, xDocProps->getAuthor());
OutDateTime(OOO_STRING_SVTOOLS_RTF_CREATIM, xDocProps->getCreationDate());
OutUnicode(OOO_STRING_SVTOOLS_RTF_AUTHOR, xDocProps->getModifiedBy());
OutDateTime(OOO_STRING_SVTOOLS_RTF_REVTIM, xDocProps->getModificationDate());
OutDateTime(OOO_STRING_SVTOOLS_RTF_PRINTIM, xDocProps->getPrintDate());
}
Strm().WriteChar('}');
}
void RtfExport::WriteUserPropType(int nType)
{
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PROPTYPE);
OutULong(nType);
}
void RtfExport::WriteUserPropValue(const OUString& rValue)
{
Strm().WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_STATICVAL " ");
Strm().WriteOString(msfilter::rtfutil::OutString(rValue, m_eDefaultEncoding));
Strm().WriteChar('}');
}
void RtfExport::WriteUserProps()
{
Strm().WriteChar('{').WriteCharPtr(
OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_USERPROPS);
SwDocShell* pDocShell(m_pDoc->GetDocShell());
uno::Reference<document::XDocumentProperties> xDocProps;
if (pDocShell)
{
uno::Reference<document::XDocumentPropertiesSupplier> xDPS(pDocShell->GetModel(),
uno::UNO_QUERY);
xDocProps.set(xDPS->getDocumentProperties());
}
else
{
// Clipboard document, read metadata from the meta field manager.
sw::MetaFieldManager& rManager = m_pDoc->GetMetaFieldManager();
xDocProps.set(rManager.getDocumentProperties());
}
if (xDocProps.is())
{
// Handle user-defined properties.
uno::Reference<beans::XPropertyContainer> xUserDefinedProperties
= xDocProps->getUserDefinedProperties();
if (xUserDefinedProperties.is())
{
uno::Reference<beans::XPropertySet> xPropertySet(xUserDefinedProperties,
uno::UNO_QUERY);
const uno::Sequence<beans::Property> aProperties
= xPropertySet->getPropertySetInfo()->getProperties();
for (const beans::Property& rProperty : aProperties)
{
if (rProperty.Name.startsWith("Company"))
// We have explicit markup in RTF for this property.
continue;
// Property name.
Strm().WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_PROPNAME " ");
Strm().WriteCharPtr(
msfilter::rtfutil::OutString(rProperty.Name, m_eDefaultEncoding).getStr());
Strm().WriteChar('}');
// Property value.
OUString aValue;
double fValue;
bool bValue;
util::DateTime aDate;
uno::Any aAny = xPropertySet->getPropertyValue(rProperty.Name);
if (aAny >>= bValue)<--- Uninitialized variable: bValue
{
WriteUserPropType(11);
WriteUserPropValue(OUString::number(static_cast<int>(bValue)));
}
else if (aAny >>= aValue)
{
WriteUserPropType(30);
WriteUserPropValue(aValue);
}
else if (aAny >>= fValue)<--- Uninitialized variable: fValue
{
aValue = OUString::number(fValue);
if (aValue.indexOf('.') == -1)
{
// Integer.
WriteUserPropType(3);
WriteUserPropValue(aValue);
}
else
{
// Real number.
WriteUserPropType(5);
WriteUserPropValue(aValue);
}
}
else if (aAny >>= aDate)
{
WriteUserPropType(64);
// Format is 'YYYY. MM. DD.'.
aValue += OUString::number(aDate.Year) + ". ";
if (aDate.Month < 10)
aValue += "0";
aValue += OUString::number(aDate.Month) + ". ";
if (aDate.Day < 10)
aValue += "0";
aValue += OUString::number(aDate.Day) + ".";
WriteUserPropValue(aValue);
}
}
}
}
Strm().WriteChar('}');
}
void RtfExport::WritePageDescTable()
{
// Write page descriptions (page styles)
std::size_t nSize = m_pDoc->GetPageDescCnt();
if (!nSize)
return;
Strm().WriteCharPtr(SAL_NEWLINE_STRING);
m_bOutPageDescs = true;
Strm()
.WriteChar('{')
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_IGNORE)
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PGDSCTBL);
for (std::size_t n = 0; n < nSize; ++n)
{
const SwPageDesc& rPageDesc = m_pDoc->GetPageDesc(n);
Strm()
.WriteCharPtr(SAL_NEWLINE_STRING)
.WriteChar('{')
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PGDSC);
OutULong(n).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PGDSCUSE);
OutULong(static_cast<sal_uLong>(rPageDesc.ReadUseOn()));
OutPageDescription(rPageDesc, false);
// search for the next page description
std::size_t i = nSize;
while (i)
if (rPageDesc.GetFollow() == &m_pDoc->GetPageDesc(--i))
break;
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PGDSCNXT);
OutULong(i).WriteChar(' ');
Strm()
.WriteCharPtr(
msfilter::rtfutil::OutString(rPageDesc.GetName(), m_eDefaultEncoding).getStr())
.WriteCharPtr(";}");
}
Strm().WriteChar('}').WriteCharPtr(SAL_NEWLINE_STRING);
m_bOutPageDescs = false;
// reset table infos, otherwise the depth of the cells will be incorrect,
// in case the page style (header or footer) had tables
m_pTableInfo = std::make_shared<ww8::WW8TableInfo>();
}
ErrCode RtfExport::ExportDocument_Impl()
{
// Make the header
Strm()
.WriteChar('{')
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_RTF)
.WriteChar('1')
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_ANSI);
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_DEFF);
OutULong(m_aFontHelper.GetId(m_pDoc->GetAttrPool().GetDefaultItem(RES_CHRATR_FONT)));
// If this not exist, MS don't understand our ansi characters (0x80-0xff).
Strm().WriteCharPtr("\\adeflang1025");
// Font table
WriteFonts();
m_pStyles = std::make_unique<MSWordStyles>(*this);
// Color and stylesheet table
WriteStyles();
// List table
BuildNumbering();
WriteNumbering();
WriteRevTab();
WriteInfo();
WriteUserProps();
// Default TabSize
Strm()
.WriteOString(m_pAttrOutput->GetTabStop().makeStringAndClear())
.WriteCharPtr(SAL_NEWLINE_STRING);
// Automatic hyphenation: it's a global setting in Word, it's a paragraph setting in Writer.
// Set it's value to "auto" and disable on paragraph level, if no hyphenation is used there.
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_HYPHAUTO);
OutULong(1);
// Zoom
SwViewShell* pViewShell(m_pDoc->getIDocumentLayoutAccess().GetCurrentViewShell());
if (pViewShell && pViewShell->GetViewOptions()->GetZoomType() == SvxZoomType::PERCENT)
{
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_VIEWSCALE);
OutULong(pViewShell->GetViewOptions()->GetZoom());
}
// Record changes?
if (RedlineFlags::On & m_nOrigRedlineFlags)
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_REVISIONS);
// Mirror margins?
if ((UseOnPage::Mirror & m_pDoc->GetPageDesc(0).ReadUseOn()) == UseOnPage::Mirror)
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_MARGMIRROR);
// Init sections
m_pSections = new MSWordSections(*this);
// Page description
WritePageDescTable();
// Enable form protection by default if needed, as there is no switch to
// enable it on a per-section basis. OTOH don't always enable it as it
// breaks moving of drawings - so write it only in case there is really a
// protected section in the document.
for (auto const& pSectionFormat : m_pDoc->GetSections())
{
if (!pSectionFormat->IsInUndo() && pSectionFormat->GetProtect().IsContentProtected())
{<--- Consider using std::any_of algorithm instead of a raw loop.
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_FORMPROT);
break;
}
}
// enable form field shading
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_FORMSHADE);
// size and empty margins of the page
if (m_pDoc->GetPageDescCnt())
{
// Seeking the first SwFormatPageDesc. If no set, the default is valid
const SwFormatPageDesc* pSttPgDsc = nullptr;
{
const SwNode& rSttNd
= *m_pDoc->GetNodes()[m_pDoc->GetNodes().GetEndOfExtras().GetIndex() + 2];
const SfxItemSet* pSet = nullptr;
if (rSttNd.IsContentNode())
pSet = &rSttNd.GetContentNode()->GetSwAttrSet();
else if (rSttNd.IsTableNode())
pSet = &rSttNd.GetTableNode()->GetTable().GetFrameFormat()->GetAttrSet();
else if (rSttNd.IsSectionNode())
pSet = &rSttNd.GetSectionNode()->GetSection().GetFormat()->GetAttrSet();
if (pSet)
{
std::size_t nPosInDoc;
pSttPgDsc = &pSet->Get(RES_PAGEDESC);
if (!pSttPgDsc->GetPageDesc())
pSttPgDsc = nullptr;
else if (m_pDoc->FindPageDesc(pSttPgDsc->GetPageDesc()->GetName(), &nPosInDoc))
{
Strm()
.WriteChar('{')
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_IGNORE)
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PGDSCNO);
OutULong(nPosInDoc).WriteChar('}');
}
}
}
const SwPageDesc& rPageDesc
= pSttPgDsc ? *pSttPgDsc->GetPageDesc() : m_pDoc->GetPageDesc(0);
const SwFrameFormat& rFormatPage = rPageDesc.GetMaster();
{
if (rPageDesc.GetLandscape())
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_LANDSCAPE);
const SwFormatFrameSize& rSz = rFormatPage.GetFrameSize();
// Clipboard document is always created without a printer, then
// the size will be always LONG_MAX! Solution then is to use A4
if (LONG_MAX == rSz.GetHeight() || LONG_MAX == rSz.GetWidth())
{
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PAPERH);
Size a4 = SvxPaperInfo::GetPaperSize(PAPER_A4);
OutULong(a4.Height()).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PAPERW);
OutULong(a4.Width());
}
else
{
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PAPERH);
OutULong(rSz.GetHeight()).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PAPERW);
OutULong(rSz.GetWidth());
}
}
{
const SvxLRSpaceItem& rLR = rFormatPage.GetLRSpace();
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_MARGL);
OutLong(rLR.GetLeft()).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_MARGR);
OutLong(rLR.GetRight());
}
{
const SvxULSpaceItem& rUL = rFormatPage.GetULSpace();
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_MARGT);
OutLong(rUL.GetUpper()).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_MARGB);
OutLong(rUL.GetLower());
}
Strm()
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_SECTD)
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_SBKNONE);
m_pAttrOutput->SectFootnoteEndnotePr();
// All sections are unlocked by default
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_SECTUNLOCKED);
OutLong(1);
OutPageDescription(rPageDesc, true); // Changed bCheckForFirstPage to true so headers
// following title page are correctly added - i13107
if (pSttPgDsc)
{
m_pCurrentPageDesc = &rPageDesc;
}
}
// line numbering
const SwLineNumberInfo& rLnNumInfo = m_pDoc->GetLineNumberInfo();
if (rLnNumInfo.IsPaintLineNumbers())
{
sal_uLong nLnNumRestartNo = 0;
if (const WW8_SepInfo* pSectionInfo = m_pSections->CurrentSectionInfo())
nLnNumRestartNo = pSectionInfo->nLnNumRestartNo;
AttrOutput().SectionLineNumbering(nLnNumRestartNo, rLnNumInfo);
}
{
// write the footnotes and endnotes-out Info
const SwFootnoteInfo& rFootnoteInfo = m_pDoc->GetFootnoteInfo();
const char* pOut = FTNPOS_CHAPTER == rFootnoteInfo.m_ePos ? OOO_STRING_SVTOOLS_RTF_ENDDOC
: OOO_STRING_SVTOOLS_RTF_FTNBJ;
Strm().WriteCharPtr(pOut).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_FTNSTART);
OutLong(rFootnoteInfo.m_nFootnoteOffset + 1);
switch (rFootnoteInfo.m_eNum)
{
case FTNNUM_PAGE:
pOut = OOO_STRING_SVTOOLS_RTF_FTNRSTPG;
break;
case FTNNUM_DOC:
pOut = OOO_STRING_SVTOOLS_RTF_FTNRSTCONT;
break;
default:
pOut = OOO_STRING_SVTOOLS_RTF_FTNRESTART;
break;
}
Strm().WriteCharPtr(pOut);
switch (rFootnoteInfo.m_aFormat.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_SYMBOL_CHICAGO:
pOut = OOO_STRING_SVTOOLS_RTF_FTNNCHI;
break;
default:
pOut = OOO_STRING_SVTOOLS_RTF_FTNNAR;
break;
}
Strm().WriteCharPtr(pOut);
const SwEndNoteInfo& rEndNoteInfo = m_pDoc->GetEndNoteInfo();
Strm()
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_AENDDOC)
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_AFTNRSTCONT)
.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_AFTNSTART);
OutLong(rEndNoteInfo.m_nFootnoteOffset + 1);
switch (rEndNoteInfo.m_aFormat.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_SYMBOL_CHICAGO:
pOut = OOO_STRING_SVTOOLS_RTF_AFTNNCHI;
break;
default:
pOut = OOO_STRING_SVTOOLS_RTF_AFTNNAR;
break;
}
Strm().WriteCharPtr(pOut);
}
if (!m_pDoc->getIDocumentSettingAccess().get(DocumentSettingId::PARA_SPACE_MAX))
// RTF default is true, so write compat flag if this should be false.
Strm().WriteCharPtr(LO_STRING_SVTOOLS_RTF_HTMAUTSP);
Strm().WriteCharPtr(SAL_NEWLINE_STRING);
WriteFootnoteSettings();
WriteMainText();
Strm().WriteChar('}');
return ERRCODE_NONE;
}
void RtfExport::PrepareNewPageDesc(const SfxItemSet* pSet, const SwNode& rNd,
const SwFormatPageDesc* pNewPgDescFormat,
const SwPageDesc* pNewPgDesc)
{
const SwSectionFormat* pFormat = GetSectionFormat(rNd);
const sal_uLong nLnNm = GetSectionLineNo(pSet, rNd);
OSL_ENSURE(pNewPgDescFormat || pNewPgDesc, "Neither page desc format nor page desc provided.");
if (pNewPgDescFormat)
m_pSections->AppendSection(*pNewPgDescFormat, rNd, pFormat, nLnNm);
else if (pNewPgDesc)
m_pSections->AppendSection(pNewPgDesc, rNd, pFormat, nLnNm);
// Don't insert a page break, when we're changing page style just because the next page has to be a different one.
if (!m_pAttrOutput->GetPrevPageDesc()
|| m_pAttrOutput->GetPrevPageDesc()->GetFollow() != pNewPgDesc)
AttrOutput().SectionBreak(msword::PageBreak, false, m_pSections->CurrentSectionInfo());
}
bool RtfExport::DisallowInheritingOutlineNumbering(const SwFormat& rFormat)
{
bool bRet(false);
if (SfxItemState::SET != rFormat.GetItemState(RES_PARATR_NUMRULE, false))
{
if (const SwFormat* pParent = rFormat.DerivedFrom())
{
if (static_cast<const SwTextFormatColl*>(pParent)
->IsAssignedToListLevelOfOutlineStyle())
{
// Level 9 disables the outline
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_LEVEL).WriteInt32(9);
bRet = true;
}
}
}
return bRet;
}
void RtfExport::OutputEndNode(const SwEndNode& rEndNode)
{
if (TXT_MAINTEXT == m_nTextTyp && rEndNode.StartOfSectionNode()->IsTableNode())
// End node of a table: see if a section break should be written after the table.
AttrOutput().SectionBreaks(rEndNode);
}
void RtfExport::OutputGrfNode(const SwGrfNode& /*rGrfNode*/)
{
/* noop, see RtfAttributeOutput::FlyFrameGraphic */
}
void RtfExport::OutputOLENode(const SwOLENode& /*rOLENode*/)
{
/* noop, see RtfAttributeOutput::FlyFrameOLE */
}
void RtfExport::OutputLinkedOLE(const OUString& /*rLinked*/) {}
void RtfExport::OutputTextNode(SwTextNode& rNode)
{
m_nCurrentNodeIndex = rNode.GetIndex();
if (!m_bOutOutlineOnly || rNode.IsOutline())
MSWordExportBase::OutputTextNode(rNode);
m_nCurrentNodeIndex = 0;
}
void RtfExport::AppendSection(const SwPageDesc* pPageDesc, const SwSectionFormat* pFormat,
sal_uLong nLnNum)
{
m_pSections->AppendSection(pPageDesc, pFormat, nLnNum);
AttrOutput().SectionBreak(msword::PageBreak, false, m_pSections->CurrentSectionInfo());
}
RtfExport::RtfExport(RtfExportFilter* pFilter, SwDoc* pDocument,
std::shared_ptr<SwUnoCursor>& pCurrentPam, SwPaM* pOriginalPam,
Writer* pWriter, bool bOutOutlineOnly)
: MSWordExportBase(pDocument, pCurrentPam, pOriginalPam)
, m_pFilter(pFilter)
, m_pWriter(pWriter)
, m_pSections(nullptr)
, m_bOutOutlineOnly(bOutOutlineOnly)
, m_eDefaultEncoding(
rtl_getTextEncodingFromWindowsCharset(sw::ms::rtl_TextEncodingToWinCharset(DEF_ENCODING)))
, m_eCurrentEncoding(m_eDefaultEncoding)
, m_bRTFFlySyntax(false)
, m_nCurrentNodeIndex(0)
{
m_bExportModeRTF = true;
// the attribute output for the document
m_pAttrOutput = std::make_unique<RtfAttributeOutput>(*this);
// that just causes problems for RTF
m_bSubstituteBullets = false;
// needed to have a complete font table
m_aFontHelper.bLoadAllFonts = true;
// the related SdrExport
m_pSdrExport = std::make_unique<RtfSdrExport>(*this);
if (!m_pWriter)
m_pWriter = &m_pFilter->GetWriter();
}
RtfExport::~RtfExport() = default;
SvStream& RtfExport::Strm()
{
if (m_pStream)
return *m_pStream;
return m_pWriter->Strm();
}
void RtfExport::setStream() { m_pStream = std::make_unique<SvMemoryStream>(); }
OString RtfExport::getStream()
{
OString aRet;
if (m_pStream)
aRet = OString(static_cast<const char*>(m_pStream->GetData()), m_pStream->Tell());
return aRet;
}
void RtfExport::resetStream() { m_pStream.reset(); }
SvStream& RtfExport::OutULong(sal_uLong nVal) { return Writer::OutULong(Strm(), nVal); }
SvStream& RtfExport::OutLong(long nVal) { return Writer::OutLong(Strm(), nVal); }
void RtfExport::OutUnicode(const char* pToken, const OUString& rContent, bool bUpr)
{
if (!rContent.isEmpty())
{
if (!bUpr)
{
Strm().WriteChar('{').WriteCharPtr(pToken).WriteChar(' ');
Strm().WriteCharPtr(
msfilter::rtfutil::OutString(rContent, m_eCurrentEncoding).getStr());
Strm().WriteChar('}');
}
else
Strm().WriteCharPtr(
msfilter::rtfutil::OutStringUpr(pToken, rContent, m_eCurrentEncoding).getStr());
}
}
void RtfExport::OutDateTime(const char* pStr, const util::DateTime& rDT)
{
Strm().WriteChar('{').WriteCharPtr(pStr).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_YR);
OutULong(rDT.Year).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_MO);
OutULong(rDT.Month).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_DY);
OutULong(rDT.Day).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_HR);
OutULong(rDT.Hours).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_MIN);
OutULong(rDT.Minutes).WriteChar('}');
}
sal_uInt16 RtfExport::GetColor(const Color& rColor) const
{
for (const auto& rEntry : m_aColTable)
if (rEntry.second == rColor)
{<--- Consider using std::find_if algorithm instead of a raw loop.
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " returning " << rEntry.first << " ("
<< rColor.GetRed() << "," << rColor.GetGreen() << ","
<< rColor.GetBlue() << ")");
return rEntry.first;
}
OSL_FAIL("No such Color in m_aColTable!");
return 0;
}
void RtfExport::InsColor(const Color& rCol)
{
sal_uInt16 n;
bool bAutoColorInTable = false;
for (const auto& rEntry : m_aColTable)
{
if (rEntry.second == rCol)
return; // Already in the table
if (rEntry.second == COL_AUTO)
bAutoColorInTable = true;
}
if (rCol == COL_AUTO)
// COL_AUTO gets value 0
n = 0;
else
{
// other colors get values >0
n = m_aColTable.size();
if (!bAutoColorInTable)
// reserve value "0" for COL_AUTO (if COL_AUTO wasn't inserted until now)
n++;
}
m_aColTable.insert(std::pair<sal_uInt16, Color>(n, rCol));
}
void RtfExport::InsColorLine(const SvxBoxItem& rBox)
{
const editeng::SvxBorderLine* pLine = nullptr;
if (rBox.GetTop())
{
pLine = rBox.GetTop();
InsColor(pLine->GetColor());
}
if (rBox.GetBottom() && pLine != rBox.GetBottom())
{
pLine = rBox.GetBottom();
InsColor(pLine->GetColor());
}
if (rBox.GetLeft() && pLine != rBox.GetLeft())
{
pLine = rBox.GetLeft();
InsColor(pLine->GetColor());
}
if (rBox.GetRight() && pLine != rBox.GetRight())
InsColor(rBox.GetRight()->GetColor());
}
void RtfExport::OutColorTable()
{
// Build the table from rPool since the colors provided to
// RtfAttributeOutput callbacks are too late.
const SfxItemPool& rPool = m_pDoc->GetAttrPool();
// MSO Word uses a default color table with 16 colors (which is used e.g. for highlighting)
InsColor(COL_BLACK);
InsColor(COL_LIGHTBLUE);
InsColor(COL_LIGHTCYAN);
InsColor(COL_LIGHTGREEN);
InsColor(COL_LIGHTMAGENTA);
InsColor(COL_LIGHTRED);
InsColor(COL_YELLOW);
InsColor(COL_WHITE);
InsColor(COL_BLUE);
InsColor(COL_CYAN);
InsColor(COL_GREEN);
InsColor(COL_MAGENTA);
InsColor(COL_RED);
InsColor(COL_BROWN);
InsColor(COL_GRAY);
InsColor(COL_LIGHTGRAY);
// char color
{
auto pCol = GetDfltAttr(RES_CHRATR_COLOR);
InsColor(pCol->GetValue());
pCol = rPool.GetPoolDefaultItem(RES_CHRATR_COLOR);
if (pCol)
InsColor(pCol->GetValue());
for (const SfxPoolItem* pItem : rPool.GetItemSurrogates(RES_CHRATR_COLOR))
{
pCol = dynamic_cast<const SvxColorItem*>(pItem);
if (pCol)
InsColor(pCol->GetValue());
}
auto pUnder = GetDfltAttr(RES_CHRATR_UNDERLINE);
InsColor(pUnder->GetColor());
for (const SfxPoolItem* pItem : rPool.GetItemSurrogates(RES_CHRATR_UNDERLINE))
{
pUnder = dynamic_cast<const SvxUnderlineItem*>(pItem);
if (pUnder)
InsColor(pUnder->GetColor());
}
auto pOver = GetDfltAttr(RES_CHRATR_OVERLINE);
InsColor(pOver->GetColor());
for (const SfxPoolItem* pItem : rPool.GetItemSurrogates(RES_CHRATR_OVERLINE))
{
pOver = dynamic_cast<const SvxOverlineItem*>(pItem);
if (pOver)
InsColor(pOver->GetColor());
}
}
// background color
static const sal_uInt16 aBrushIds[] = { RES_BACKGROUND, RES_CHRATR_BACKGROUND, 0 };
for (const sal_uInt16* pIds = aBrushIds; *pIds; ++pIds)
{
auto pBackground = static_cast<const SvxBrushItem*>(GetDfltAttr(*pIds));
InsColor(pBackground->GetColor());
pBackground = static_cast<const SvxBrushItem*>(rPool.GetPoolDefaultItem(*pIds));
if (pBackground)
{
InsColor(pBackground->GetColor());
}
for (const SfxPoolItem* pItem : rPool.GetItemSurrogates(*pIds))
{
pBackground = static_cast<const SvxBrushItem*>(pItem);
if (pBackground)
{
InsColor(pBackground->GetColor());
}
}
}
// shadow color
{
auto pShadow = GetDfltAttr(RES_SHADOW);
InsColor(pShadow->GetColor());
pShadow = rPool.GetPoolDefaultItem(RES_SHADOW);
if (nullptr != pShadow)
{
InsColor(pShadow->GetColor());
}
for (const SfxPoolItem* pItem : rPool.GetItemSurrogates(RES_SHADOW))
{
pShadow = dynamic_cast<const SvxShadowItem*>(pItem);
if (pShadow)
{
InsColor(pShadow->GetColor());
}
}
}
// frame border color
{
const SvxBoxItem* pBox = rPool.GetPoolDefaultItem(RES_BOX);
if (nullptr != pBox)
InsColorLine(*pBox);
for (const SfxPoolItem* pItem : rPool.GetItemSurrogates(RES_BOX))
{
pBox = dynamic_cast<const SvxBoxItem*>(pItem);
if (pBox)
InsColorLine(*pBox);
}
}
{
const SvxBoxItem* pCharBox = rPool.GetPoolDefaultItem(RES_CHRATR_BOX);
if (pCharBox)
InsColorLine(*pCharBox);
for (const SfxPoolItem* pItem : rPool.GetItemSurrogates(RES_CHRATR_BOX))
{
pCharBox = dynamic_cast<const SvxBoxItem*>(pItem);
if (pCharBox)
InsColorLine(*pCharBox);
}
}
// TextFrame or paragraph background solid fill.
for (const SfxPoolItem* pItem : rPool.GetItemSurrogates(XATTR_FILLCOLOR))
{
if (auto pColorItem = dynamic_cast<const XFillColorItem*>(pItem))
InsColor(pColorItem->GetColorValue());
}
for (std::size_t n = 0; n < m_aColTable.size(); ++n)
{
const Color& rCol = m_aColTable[n];
if (n || COL_AUTO != rCol)
{
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_RED);
OutULong(rCol.GetRed()).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_GREEN);
OutULong(rCol.GetGreen()).WriteCharPtr(OOO_STRING_SVTOOLS_RTF_BLUE);
OutULong(rCol.GetBlue());
}
Strm().WriteChar(';');
}
}
void RtfExport::InsStyle(sal_uInt16 nId, const OString& rStyle)
{
m_aStyTable.insert(std::pair<sal_uInt16, OString>(nId, rStyle));
}
OString* RtfExport::GetStyle(sal_uInt16 nId)
{
auto it = m_aStyTable.find(nId);
if (it != m_aStyTable.end())
return &it->second;
return nullptr;
}
sal_uInt16 RtfExport::GetRedline(const OUString& rAuthor)
{
auto it = m_aRedlineTable.find(rAuthor);
if (it != m_aRedlineTable.end())
return it->second;
const sal_uInt16 nId = m_aRedlineTable.size();
m_aRedlineTable.insert(std::pair<OUString, sal_uInt16>(rAuthor, nId));
return nId;
}
const OUString* RtfExport::GetRedline(sal_uInt16 nId)
{
for (const auto& rEntry : m_aRedlineTable)
if (rEntry.second == nId)<--- Consider using std::find_if algorithm instead of a raw loop.
return &rEntry.first;
return nullptr;
}
void RtfExport::OutPageDescription(const SwPageDesc& rPgDsc, bool bCheckForFirstPage)
{
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " start");
const SwPageDesc* pSave = m_pCurrentPageDesc;
m_pCurrentPageDesc = &rPgDsc;
if (bCheckForFirstPage && m_pCurrentPageDesc->GetFollow()
&& m_pCurrentPageDesc->GetFollow() != m_pCurrentPageDesc)
m_pCurrentPageDesc = m_pCurrentPageDesc->GetFollow();
if (m_pCurrentPageDesc->GetLandscape())
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_LNDSCPSXN);
const SwFormat* pFormat = &m_pCurrentPageDesc->GetMaster(); //GetLeft();
m_bOutPageDescs = true;
if (m_pCurrentPageDesc != &rPgDsc)
m_pFirstPageItemSet = &rPgDsc.GetMaster().GetAttrSet();
OutputFormat(*pFormat, true, false);
m_pFirstPageItemSet = nullptr;
m_bOutPageDescs = false;
// normal header / footer (without a style)
const SfxPoolItem* pItem;
if (m_pCurrentPageDesc->GetLeft().GetAttrSet().GetItemState(RES_HEADER, false, &pItem)
== SfxItemState::SET)
WriteHeaderFooter(*pItem, true);
if (m_pCurrentPageDesc->GetLeft().GetAttrSet().GetItemState(RES_FOOTER, false, &pItem)
== SfxItemState::SET)
WriteHeaderFooter(*pItem, false);
// title page
if (m_pCurrentPageDesc != &rPgDsc)
{
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_TITLEPG);
m_pCurrentPageDesc = &rPgDsc;
if (m_pCurrentPageDesc->GetMaster().GetAttrSet().GetItemState(RES_HEADER, false, &pItem)
== SfxItemState::SET)
WriteHeaderFooter(*pItem, true);
if (m_pCurrentPageDesc->GetMaster().GetAttrSet().GetItemState(RES_FOOTER, false, &pItem)
== SfxItemState::SET)
WriteHeaderFooter(*pItem, false);
}
// numbering type
AttrOutput().SectionPageNumbering(m_pCurrentPageDesc->GetNumType().GetNumberingType(),
std::nullopt);
m_pCurrentPageDesc = pSave;
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " end");
}
void RtfExport::WriteHeaderFooter(const SfxPoolItem& rItem, bool bHeader)
{
if (bHeader)
{
const auto& rHeader = static_cast<const SwFormatHeader&>(rItem);
if (!rHeader.IsActive())
return;
}
else
{
const auto& rFooter = static_cast<const SwFormatFooter&>(rItem);
if (!rFooter.IsActive())
return;
}
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " start");
const char* pStr = (bHeader ? OOO_STRING_SVTOOLS_RTF_HEADER : OOO_STRING_SVTOOLS_RTF_FOOTER);
/* is this a title page? */
if (m_pCurrentPageDesc->GetFollow() && m_pCurrentPageDesc->GetFollow() != m_pCurrentPageDesc)
{
Strm().WriteCharPtr(OOO_STRING_SVTOOLS_RTF_TITLEPG);
pStr = (bHeader ? OOO_STRING_SVTOOLS_RTF_HEADERF : OOO_STRING_SVTOOLS_RTF_FOOTERF);
}
Strm().WriteChar('{').WriteCharPtr(pStr);
WriteHeaderFooterText(m_pCurrentPageDesc->GetMaster(), bHeader);
Strm().WriteChar('}');
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " end");
}
void RtfExport::WriteHeaderFooter(const SwFrameFormat& rFormat, bool bHeader, const char* pStr,
bool bTitlepg)
{
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " start");
m_pAttrOutput->WriteHeaderFooter_Impl(rFormat, bHeader, pStr, bTitlepg);
SAL_INFO("sw.rtf", OSL_THIS_FUNC << " end");
}
namespace
{
/// Glue class to call RtfExport as an internal filter, needed by copy&paste support.
class SwRTFWriter : public Writer
{
private:
bool m_bOutOutlineOnly;
public:
SwRTFWriter(const OUString& rFilterName, const OUString& rBaseURL);
ErrCode WriteStream() override;
};
}
SwRTFWriter::SwRTFWriter(const OUString& rFilterName, const OUString& rBaseURL)
{
SetBaseURL(rBaseURL);
// export outline nodes, only (send outline to clipboard/presentation)
m_bOutOutlineOnly = rFilterName.startsWith("O");
}
ErrCode SwRTFWriter::WriteStream()
{
std::shared_ptr<SwUnoCursor> pCurPam(m_pDoc->CreateUnoCursor(*m_pCurrentPam->End(), false));
pCurPam->SetMark();
*pCurPam->GetPoint() = *m_pCurrentPam->Start();
RtfExport aExport(nullptr, m_pDoc, pCurPam, m_pCurrentPam.get(), this, m_bOutOutlineOnly);
aExport.ExportDocument(true);
return ERRCODE_NONE;
}
extern "C" SAL_DLLPUBLIC_EXPORT void ExportRTF(const OUString& rFltName, const OUString& rBaseURL,
WriterRef& xRet)
{
xRet = new SwRTFWriter(rFltName, rBaseURL);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|