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
|
/* -*- 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 <editeng/formatbreakitem.hxx>
#include <sal/log.hxx>
#include <osl/diagnose.h>
#include <tools/stream.hxx>
#include <doc.hxx>
#include <IDocumentStatistics.hxx>
#include <IDocumentLayoutAccess.hxx>
#include <docstat.hxx>
#include <fmtpdsc.hxx>
#include <laycache.hxx>
#include "layhelp.hxx"
#include <pagefrm.hxx>
#include <rootfrm.hxx>
#include <txtfrm.hxx>
#include <swtable.hxx>
#include <tabfrm.hxx>
#include <rowfrm.hxx>
#include <sectfrm.hxx>
#include <fmtcntnt.hxx>
#include <pagedesc.hxx>
#include <frmtool.hxx>
#include <dflyobj.hxx>
#include <dcontact.hxx>
#include <viewopt.hxx>
#include <flyfrm.hxx>
#include <sortedobjs.hxx>
#include <ndindex.hxx>
#include <node.hxx>
#include <ndtxt.hxx>
#include <frameformats.hxx>
#include <limits>
using namespace ::com::sun::star;
SwLayoutCache::SwLayoutCache() : m_nLockCount( 0 ) {}
/*
* Reading and writing of the layout cache.
* The layout cache is not necessary, but it improves
* the performance and reduces the text flow during
* the formatting.
* The layout cache contains the index of the paragraphs/tables
* at the top of every page, so it's possible to create
* the right count of pages and to distribute the document content
* to this pages before the formatting starts.
*/
void SwLayoutCache::Read( SvStream &rStream )
{
if( !m_pImpl )
{
m_pImpl.reset( new SwLayCacheImpl );
if( !m_pImpl->Read( rStream ) )
{
m_pImpl.reset();
}
}
}
void SwLayCacheImpl::Insert( sal_uInt16 nType, SwNodeOffset nIndex, sal_Int32 nOffset )
{
m_aType.push_back( nType );
mIndices.push_back( nIndex );
m_aOffset.push_back( nOffset );
}
bool SwLayCacheImpl::Read( SvStream& rStream )
{
SwLayCacheIoImpl aIo( rStream, false );
if( aIo.GetMajorVersion() > SW_LAYCACHE_IO_VERSION_MAJOR )
return false;
// Due to an evil bug in the layout cache (#102759#), we cannot trust the
// sizes of fly frames which have been written using the "old" layout cache.
// This flag should indicate that we do not want to trust the width and
// height of fly frames
m_bUseFlyCache = aIo.GetMinorVersion() >= 1;
aIo.OpenRec( SW_LAYCACHE_IO_REC_PAGES );
aIo.OpenFlagRec();
aIo.CloseFlagRec();
while( aIo.BytesLeft() && !aIo.HasError() )
{
sal_uInt32 nIndex(0), nOffset(0);
switch( aIo.Peek() )
{
case SW_LAYCACHE_IO_REC_PARA:
{
aIo.OpenRec( SW_LAYCACHE_IO_REC_PARA );
sal_uInt8 cFlags = aIo.OpenFlagRec();
aIo.GetStream().ReadUInt32( nIndex );
if( (cFlags & 0x01) != 0 )
aIo.GetStream().ReadUInt32( nOffset );
else
nOffset = COMPLETE_STRING;
aIo.CloseFlagRec();
Insert( SW_LAYCACHE_IO_REC_PARA, SwNodeOffset(nIndex), static_cast<sal_Int32>(nOffset) );
aIo.CloseRec();
break;
}
case SW_LAYCACHE_IO_REC_TABLE:
aIo.OpenRec( SW_LAYCACHE_IO_REC_TABLE );
aIo.OpenFlagRec();
aIo.GetStream().ReadUInt32( nIndex )
.ReadUInt32( nOffset );
Insert( SW_LAYCACHE_IO_REC_TABLE, SwNodeOffset(nIndex), static_cast<sal_Int32>(nOffset) );
aIo.CloseFlagRec();
aIo.CloseRec();
break;
case SW_LAYCACHE_IO_REC_FLY:
{
aIo.OpenRec( SW_LAYCACHE_IO_REC_FLY );
aIo.OpenFlagRec();
aIo.CloseFlagRec();
sal_Int32 nX(0), nY(0), nW(0), nH(0);
sal_uInt16 nPgNum(0);
aIo.GetStream().ReadUInt16( nPgNum ).ReadUInt32( nIndex )
.ReadInt32( nX ).ReadInt32( nY ).ReadInt32( nW ).ReadInt32( nH );
m_FlyCache.emplace_back( nPgNum, nIndex, nX, nY, nW, nH );
aIo.CloseRec();
break;
}
default:
aIo.SkipRec();
break;
}
}
aIo.CloseRec();
return !aIo.HasError();
}
/** writes the index (more precise: the difference between
* the index and the first index of the document content)
* of the first paragraph/table at the top of every page.
* If at the top of a page is the rest of a paragraph/table
* from the bottom of the previous page, the character/row
* number is stored, too.
* The position, size and page number of the text frames
* are stored, too
*/
void SwLayoutCache::Write( SvStream &rStream, const SwDoc& rDoc )
{
if( !rDoc.getIDocumentLayoutAccess().GetCurrentLayout() ) // the layout itself ..
return;
SwLayCacheIoImpl aIo( rStream, true );
// We want to save the relative index, so we need the index
// of the first content
SwNodeOffset nStartOfContent = rDoc.GetNodes().GetEndOfContent().
StartOfSectionNode()->GetIndex();
// The first page...
SwPageFrame* pPage = const_cast<SwPageFrame*>(static_cast<const SwPageFrame*>(rDoc.getIDocumentLayoutAccess().GetCurrentLayout()->Lower()));
aIo.OpenRec( SW_LAYCACHE_IO_REC_PAGES );
aIo.OpenFlagRec( 0, 0 );
aIo.CloseFlagRec();
while( pPage )
{
if( pPage->GetPrev() )
{
SwLayoutFrame* pLay = pPage->FindBodyCont();
SwFrame* pTmp = pLay ? pLay->ContainsAny() : nullptr;
// We are only interested in paragraph or table frames,
// a section frames contains paragraphs/tables.
if( pTmp && pTmp->IsSctFrame() )
pTmp = static_cast<SwSectionFrame*>(pTmp)->ContainsAny();
if( pTmp ) // any content
{
if( pTmp->IsTextFrame() )
{
SwTextFrame const*const pFrame(static_cast<SwTextFrame const*>(pTmp));
assert(!pFrame->GetMergedPara());
SwNodeOffset nNdIdx = pFrame->GetTextNodeFirst()->GetIndex();
if( nNdIdx > nStartOfContent )
{
/* Open Paragraph Record */
aIo.OpenRec( SW_LAYCACHE_IO_REC_PARA );
bool bFollow = static_cast<SwTextFrame*>(pTmp)->IsFollow();
aIo.OpenFlagRec( bFollow ? 0x01 : 0x00,
bFollow ? 8 : 4 );
nNdIdx -= nStartOfContent;
aIo.GetStream().WriteUInt32( sal_Int32(nNdIdx) );
if( bFollow )
aIo.GetStream().WriteUInt32( sal_Int32(static_cast<SwTextFrame*>(pTmp)->GetOffset()) );
aIo.CloseFlagRec();
/* Close Paragraph Record */
aIo.CloseRec();
}
}
else if( pTmp->IsTabFrame() )
{
SwTabFrame* pTab = static_cast<SwTabFrame*>(pTmp);
sal_uLong nOfst = COMPLETE_STRING;
if( pTab->IsFollow() )
{
// If the table is a follow, we have to look for the
// master and to count all rows to get the row number
nOfst = 0;
if( pTab->IsFollow() )
pTab = pTab->FindMaster( true );
while( pTab != pTmp )
{
SwFrame* pSub = pTab->Lower();
while( pSub )
{
++nOfst;
pSub = pSub->GetNext();
}
pTab = pTab->GetFollow();
assert(pTab && "Table follow without master");
}
}
while (true)
{
SwNodeOffset nNdIdx =
pTab->GetTable()->GetTableNode()->GetIndex();
if( nNdIdx > nStartOfContent )
{
/* Open Table Record */
aIo.OpenRec( SW_LAYCACHE_IO_REC_TABLE );
aIo.OpenFlagRec( 0, 8 );
nNdIdx -= nStartOfContent;
aIo.GetStream().WriteUInt32( sal_Int32(nNdIdx) )
.WriteUInt32( nOfst );
aIo.CloseFlagRec();
/* Close Table Record */
aIo.CloseRec();
}
// If the table has a follow on the next page,
// we know already the row number and store this
// immediately.
if( pTab->GetFollow() )
{
if( nOfst == sal_uLong(COMPLETE_STRING) )
nOfst = 0;
do
{
SwFrame* pSub = pTab->Lower();
while( pSub )
{
++nOfst;
pSub = pSub->GetNext();
}
pTab = pTab->GetFollow();
SwPageFrame *pTabPage = pTab->FindPageFrame();
if( pTabPage != pPage )
{
OSL_ENSURE( pPage->GetPhyPageNum() <
pTabPage->GetPhyPageNum(),
"Looping Tableframes" );
pPage = pTabPage;
break;
}
} while ( pTab->GetFollow() );
}
else
break;
}
}
}
}
if( pPage->GetSortedObjs() )
{
SwSortedObjs &rObjs = *pPage->GetSortedObjs();
for (SwAnchoredObject* pAnchoredObj : rObjs)
{
if (SwFlyFrame *pFly = pAnchoredObj->DynCastFlyFrame())
{
if( pFly->getFrameArea().Left() != FAR_AWAY &&
!pFly->GetAnchorFrame()->FindFooterOrHeader() )
{
const SwContact *pC =
::GetUserCall(pAnchoredObj->GetDrawObj());
if( pC )
{
sal_uInt32 nOrdNum = pAnchoredObj->GetDrawObj()->GetOrdNum();
sal_uInt16 nPageNum = pPage->GetPhyPageNum();
/* Open Fly Record */
aIo.OpenRec( SW_LAYCACHE_IO_REC_FLY );
aIo.OpenFlagRec( 0, 0 );
aIo.CloseFlagRec();
const SwRect& rRct = pFly->getFrameArea();
sal_Int32 nX = rRct.Left() - pPage->getFrameArea().Left();
sal_Int32 nY = rRct.Top() - pPage->getFrameArea().Top();
aIo.GetStream().WriteUInt16( nPageNum ).WriteUInt32( nOrdNum )
.WriteInt32( nX ).WriteInt32( nY )
.WriteInt32( rRct.Width() )
.WriteInt32( rRct.Height() );
/* Close Fly Record */
aIo.CloseRec();
}
}
}
}
}
pPage = static_cast<SwPageFrame*>(pPage->GetNext());
}
aIo.CloseRec();
}
#ifdef DBG_UTIL
bool SwLayoutCache::CompareLayout( const SwDoc& rDoc ) const
{
if( !m_pImpl )
return true;
const SwRootFrame *pRootFrame = rDoc.getIDocumentLayoutAccess().GetCurrentLayout();
if( pRootFrame )
{
size_t nIndex = 0;
SwNodeOffset nStartOfContent = rDoc.GetNodes().GetEndOfContent().
StartOfSectionNode()->GetIndex();
const SwPageFrame* pPage = static_cast<const SwPageFrame*>(pRootFrame->Lower());
if( pPage )
pPage = static_cast<const SwPageFrame*>(pPage->GetNext());
while( pPage )
{
if( nIndex >= m_pImpl->size() )
return false;
const SwLayoutFrame* pLay = pPage->FindBodyCont();
const SwFrame* pTmp = pLay ? pLay->ContainsAny() : nullptr;
if( pTmp && pTmp->IsSctFrame() )
pTmp = static_cast<const SwSectionFrame*>(pTmp)->ContainsAny();
if( pTmp )
{
if( pTmp->IsTextFrame() )
{
SwTextFrame const*const pFrame(static_cast<SwTextFrame const*>(pTmp));
assert(!pFrame->GetMergedPara());
SwNodeOffset nNdIdx = pFrame->GetTextNodeFirst()->GetIndex();
if( nNdIdx > nStartOfContent )
{
bool bFollow = static_cast<const SwTextFrame*>(pTmp)->IsFollow();
nNdIdx -= nStartOfContent;
if( m_pImpl->GetBreakIndex( nIndex ) != nNdIdx ||
SW_LAYCACHE_IO_REC_PARA !=
m_pImpl->GetBreakType( nIndex ) ||
(bFollow
? sal_Int32(static_cast<const SwTextFrame*>(pTmp)->GetOffset())
: COMPLETE_STRING) != m_pImpl->GetBreakOfst(nIndex))
{
return false;
}
++nIndex;
}
}
else if( pTmp->IsTabFrame() )
{
const SwTabFrame* pTab = static_cast<const SwTabFrame*>(pTmp);
sal_Int32 nOfst = COMPLETE_STRING;
if( pTab->IsFollow() )
{
nOfst = 0;
if( pTab->IsFollow() )
pTab = pTab->FindMaster( true );
while( pTab != pTmp )
{
const SwFrame* pSub = pTab->Lower();
while( pSub )
{
++nOfst;
pSub = pSub->GetNext();
}
pTab = pTab->GetFollow();
}
}
do
{
SwNodeOffset nNdIdx =
pTab->GetTable()->GetTableNode()->GetIndex();
if( nNdIdx > nStartOfContent )
{
nNdIdx -= nStartOfContent;
if( m_pImpl->GetBreakIndex( nIndex ) != nNdIdx ||
SW_LAYCACHE_IO_REC_TABLE !=
m_pImpl->GetBreakType( nIndex ) ||
nOfst != m_pImpl->GetBreakOfst( nIndex ) )
{
return false;
}
++nIndex;
}
if( pTab->GetFollow() )
{
if( nOfst == COMPLETE_STRING )
nOfst = 0;
do
{
const SwFrame* pSub = pTab->Lower();
while( pSub )
{
++nOfst;
pSub = pSub->GetNext();
}
pTab = pTab->GetFollow();
const SwPageFrame *pTabPage = pTab->FindPageFrame();
if( pTabPage != pPage )
{
pPage = pTabPage;
break;
}
} while ( pTab->GetFollow() );
}
else
break;
} while( pTab );
}
}
pPage = static_cast<const SwPageFrame*>(pPage->GetNext());
}
}
return true;
}
#endif
void SwLayoutCache::ClearImpl()
{
if( !IsLocked() )
{
m_pImpl.reset();
}
}
SwLayoutCache::~SwLayoutCache()
{
OSL_ENSURE( !m_nLockCount, "Deleting a locked SwLayoutCache!?" );
}
/// helper class to create not nested section frames for nested sections.
SwActualSection::SwActualSection( SwActualSection *pUp,
SwSectionFrame *pSect,
SwSectionNode *pNd ) :
m_pUpper( pUp ),
m_pSectFrame( pSect ),
m_pSectNode( pNd )
{
if ( !m_pSectNode )
{
const SwNodeIndex *pIndex = pSect->GetFormat()->GetContent().GetContentIdx();
m_pSectNode = pIndex->GetNode().FindSectionNode();
}
}
namespace {
bool sanityCheckLayoutCache(SwLayCacheImpl const& rCache,
SwNodes const& rNodes, SwNodeOffset nNodeIndex)
{
auto const nStartOfContent(rNodes.GetEndOfContent().StartOfSectionNode()->GetIndex());
nNodeIndex -= nStartOfContent;
auto const nMaxIndex(rNodes.GetEndOfContent().GetIndex() - nStartOfContent);
for (size_t nIndex = 0; nIndex < rCache.size(); ++nIndex)
{
auto const nBreakIndex(rCache.GetBreakIndex(nIndex));
if (nBreakIndex < nNodeIndex || nMaxIndex <= nBreakIndex)
{
SAL_WARN("sw.layout",
"invalid node index in layout-cache: " << nBreakIndex);
return false;
}
auto const nBreakType(rCache.GetBreakType(nIndex));
switch (nBreakType)
{
case SW_LAYCACHE_IO_REC_PARA:
if (!rNodes[nBreakIndex + nStartOfContent]->IsTextNode())
{
SAL_WARN("sw.layout",
"invalid node of type 'P' in layout-cache");
return false;
}
break;
case SW_LAYCACHE_IO_REC_TABLE:
if (!rNodes[nBreakIndex + nStartOfContent]->IsTableNode())
{
SAL_WARN("sw.layout",
"invalid node of type 'T' in layout-cache");
return false;
}
break;
default:
assert(false); // Read shouldn't have inserted that
}
}
return true;
}
} // namespace
/** helper class, which utilizes the layout cache information
* to distribute the document content to the right pages.
* It's used by the InsertCnt_(..)-function.
* If there's no layout cache, the distribution to the pages is more
* a guess, but a guess with statistical background.
*/
SwLayHelper::SwLayHelper( SwDoc *pD, SwFrame* &rpF, SwFrame* &rpP, SwPageFrame* &rpPg,
SwLayoutFrame* &rpL, std::unique_ptr<SwActualSection> &rpA,
SwNodeOffset nNodeIndex, bool bCache )
: mrpFrame( rpF )
, mrpPrv( rpP )
, mrpPage( rpPg )
, mrpLay( rpL )
, mrpActualSection( rpA )
, mbBreakAfter(false)
, mpDoc(pD)
, mnMaxParaPerPage( 25 )
, mnParagraphCnt( bCache ? 0 : USHRT_MAX )
, mnFlyIdx( 0 )
, mbFirst( bCache )
{
mpImpl = mpDoc->GetLayoutCache() ? mpDoc->GetLayoutCache()->LockImpl() : nullptr;
if( mpImpl )
{
SwNodes const& rNodes(mpDoc->GetNodes());
if (sanityCheckLayoutCache(*mpImpl, rNodes, nNodeIndex))
{
mnIndex = 0;
mnStartOfContent = rNodes.GetEndOfContent().StartOfSectionNode()->GetIndex();
mnMaxParaPerPage = 1000;
}
else
{
mpDoc->GetLayoutCache()->UnlockImpl();
mpImpl = nullptr;
mnIndex = std::numeric_limits<size_t>::max();
mnStartOfContent = SwNodeOffset(USHRT_MAX);
}
}
else
{
mnIndex = std::numeric_limits<size_t>::max();
mnStartOfContent = NODE_OFFSET_MAX;
}
}
SwLayHelper::~SwLayHelper()
{
if( mpImpl )
{
OSL_ENSURE( mpDoc && mpDoc->GetLayoutCache(), "Missing layoutcache" );
mpDoc->GetLayoutCache()->UnlockImpl();
}
}
/** Does NOT really calculate the page count,
* it returns the page count value from the layout cache, if available,
* otherwise it estimates the page count.
*/
sal_uLong SwLayHelper::CalcPageCount()
{
sal_uLong nPgCount;
SwLayCacheImpl *pCache = mpDoc->GetLayoutCache() ?
mpDoc->GetLayoutCache()->LockImpl() : nullptr;
if( pCache )
{
nPgCount = pCache->size() + 1;
mpDoc->GetLayoutCache()->UnlockImpl();
}
else
{
nPgCount = mpDoc->getIDocumentStatistics().GetDocStat().nPage;
if ( nPgCount <= 10 ) // no page insertion for less than 10 pages
nPgCount = 0;
sal_Int32 nNdCount = mpDoc->getIDocumentStatistics().GetDocStat().nPara;
if ( nNdCount <= 1 )
{
//Estimates the number of paragraphs.
SwNodeOffset nTmp = mpDoc->GetNodes().GetEndOfContent().GetIndex() -
mpDoc->GetNodes().GetEndOfExtras().GetIndex();
//Tables have a little overhead...
nTmp -= SwNodeOffset(mpDoc->GetTableFrameFormats()->size() * 25);
//Fly frames, too ..
nTmp -= (mpDoc->GetNodes().GetEndOfAutotext().GetIndex() -
mpDoc->GetNodes().GetEndOfInserts().GetIndex()) / SwNodeOffset(3 * 5);
if ( nTmp > SwNodeOffset(0) )
nNdCount = sal_Int32(nTmp);
}
if ( nNdCount > 100 ) // no estimation below this value
{
if ( nPgCount > 0 )
{ // tdf#129529 avoid 0...
mnMaxParaPerPage = std::max<sal_uLong>(3, nNdCount / nPgCount);
}
else
{
mnMaxParaPerPage = std::max( sal_uLong(20),
sal_uLong(20 + nNdCount / 1000 * 3) );
const sal_uLong nMax = 53;
mnMaxParaPerPage = std::min( mnMaxParaPerPage, nMax );
nPgCount = nNdCount / mnMaxParaPerPage;
}
if ( nNdCount < 1000 )
nPgCount = 0;// no progress bar for small documents
SwViewShell *pSh = nullptr;
if( mrpLay && mrpLay->getRootFrame() )
pSh = mrpLay->getRootFrame()->GetCurrShell();
if( pSh && pSh->GetViewOptions()->getBrowseMode() )
mnMaxParaPerPage *= 6;
}
}
return nPgCount;
}
/**
* inserts a page and return true, if
* - the break after flag is set
* - the actual content wants a break before
* - the maximum count of paragraph/rows is reached
*
* The break after flag is set, if the actual content
* wants a break after.
*/
bool SwLayHelper::CheckInsertPage()
{
bool bEnd = nullptr == mrpPage->GetNext();
const SvxFormatBreakItem& rBrk = mrpFrame->GetBreakItem();
const SwFormatPageDesc& rDesc = mrpFrame->GetPageDescItem();
// #118195# Do not evaluate page description if frame
// is a follow frame!
const SwPageDesc* pDesc = mrpFrame->IsFlowFrame() &&
SwFlowFrame::CastFlowFrame( mrpFrame )->IsFollow() ?
nullptr :
rDesc.GetPageDesc();
bool bBrk = mnParagraphCnt > mnMaxParaPerPage || mbBreakAfter;
mbBreakAfter = rBrk.GetBreak() == SvxBreak::PageAfter ||
rBrk.GetBreak() == SvxBreak::PageBoth;
if ( !bBrk )
bBrk = rBrk.GetBreak() == SvxBreak::PageBefore ||
rBrk.GetBreak() == SvxBreak::PageBoth;
if ( bBrk || pDesc )
{
::std::optional<sal_uInt16> oPgNum;
if ( !pDesc )
{
pDesc = mrpPage->GetPageDesc()->GetFollow();
}
else
{
oPgNum = rDesc.GetNumOffset();
if ( oPgNum )
static_cast<SwRootFrame*>(mrpPage->GetUpper())->SetVirtPageNum(true);
}
bool bNextPageRight = !mrpPage->OnRightPage();
bool bInsertEmpty = false;
assert(mrpPage->GetUpper()->GetLower());
if (oPgNum && bNextPageRight != IsRightPageByNumber(
*static_cast<SwRootFrame*>(mrpPage->GetUpper()), *oPgNum))
{
bNextPageRight = !bNextPageRight;
bInsertEmpty = true;
}
// If the page style is changing, we'll have a first page.
bool bNextPageFirst = pDesc != mrpPage->GetPageDesc();
::InsertNewPage( const_cast<SwPageDesc&>(*pDesc), mrpPage->GetUpper(),
bNextPageRight, bNextPageFirst, bInsertEmpty, false, mrpPage->GetNext());
if ( bEnd )
{
OSL_ENSURE( mrpPage->GetNext(), "No new page?" );
do
{ mrpPage = static_cast<SwPageFrame*>(mrpPage->GetNext());
} while ( mrpPage->GetNext() );
}
else
{
OSL_ENSURE( mrpPage->GetNext(), "No new page?" );
mrpPage = static_cast<SwPageFrame*>(mrpPage->GetNext());
if ( mrpPage->IsEmptyPage() )
{
OSL_ENSURE( mrpPage->GetNext(), "No new page?" );
mrpPage = static_cast<SwPageFrame*>(mrpPage->GetNext());
}
}
mrpLay = mrpPage->FindBodyCont();
while( mrpLay->Lower() )
mrpLay = static_cast<SwLayoutFrame*>(mrpLay->Lower());
return true;
}
return false;
}
/** entry point for the InsertCnt_-function.
* The document content index is checked either it is
* in the layout cache either it's time to insert a page
* cause the maximal estimation of content per page is reached.
* A really big table or long paragraph may contains more than
* one page, in this case the needed count of pages will inserted.
*/
bool SwLayHelper::CheckInsert( SwNodeOffset nNodeIndex )
{
bool bRet = false;
bool bLongTab = false;
sal_uLong nMaxRowPerPage( 0 );
nNodeIndex -= mnStartOfContent;
sal_uInt16 nRows( 0 );
if( mrpFrame->IsTabFrame() )
{
//Inside a table counts every row as a paragraph
SwFrame *pLow = static_cast<SwTabFrame*>(mrpFrame)->Lower();
nRows = 0;
do
{
++nRows;
pLow = pLow->GetNext();
} while ( pLow );
mnParagraphCnt += nRows;
if( !mpImpl && mnParagraphCnt > mnMaxParaPerPage + 10 )
{
// OD 09.04.2003 #108698# - improve heuristics:
// Assume that a table, which has more than three times the quantity
// of maximal paragraphs per page rows, consists of rows, which have
// the height of a normal paragraph. Thus, allow as much rows per page
// as much paragraphs are allowed.
if ( nRows > ( 3*mnMaxParaPerPage ) )
{
nMaxRowPerPage = mnMaxParaPerPage;
}
else
{
SwFrame *pTmp = static_cast<SwTabFrame*>(mrpFrame)->Lower();
if( pTmp->GetNext() )
pTmp = pTmp->GetNext();
pTmp = static_cast<SwRowFrame*>(pTmp)->Lower();
sal_uInt16 nCnt = 0;
do
{
++nCnt;
pTmp = pTmp->GetNext();
} while( pTmp );
nMaxRowPerPage = std::max( sal_uLong(2), mnMaxParaPerPage / nCnt );
}
bLongTab = true;
}
}
else
++mnParagraphCnt;
if( mbFirst && mpImpl && mnIndex < mpImpl->size() &&
mpImpl->GetBreakIndex( mnIndex ) == nNodeIndex &&
( mpImpl->GetBreakOfst( mnIndex ) < COMPLETE_STRING ||
( ++mnIndex < mpImpl->size() &&
mpImpl->GetBreakIndex( mnIndex ) == nNodeIndex ) ) )
mbFirst = false;
// OD 09.04.2003 #108698# - always split a big tables.
if ( !mbFirst ||
( mrpFrame->IsTabFrame() && bLongTab )
)
{
sal_Int32 nRowCount = 0;
do
{
if( mpImpl || bLongTab )
{
sal_Int32 nOfst = COMPLETE_STRING;
sal_uInt16 nType = SW_LAYCACHE_IO_REC_PAGES;
if( bLongTab )
{
mbBreakAfter = true;
nOfst = static_cast<sal_Int32>(nRowCount + nMaxRowPerPage);
}
else
{
while( mnIndex < mpImpl->size() &&
mpImpl->GetBreakIndex(mnIndex) < nNodeIndex)
++mnIndex;
if( mnIndex < mpImpl->size() &&
mpImpl->GetBreakIndex(mnIndex) == nNodeIndex )
{
nType = mpImpl->GetBreakType( mnIndex );
nOfst = mpImpl->GetBreakOfst( mnIndex++ );
mbBreakAfter = true;
}
}
if( nOfst < COMPLETE_STRING )
{
bool bSplit = false;
sal_uInt16 nRepeat( 0 );
if( !bLongTab && mrpFrame->IsTextFrame() &&
SW_LAYCACHE_IO_REC_PARA == nType &&
nOfst < static_cast<SwTextFrame*>(mrpFrame)->GetText().getLength())
bSplit = true;
else if( mrpFrame->IsTabFrame() && nRowCount < nOfst &&
( bLongTab || SW_LAYCACHE_IO_REC_TABLE == nType ) )
{
nRepeat = static_cast<SwTabFrame*>(mrpFrame)->
GetTable()->GetRowsToRepeat();
bSplit = nOfst < nRows && nRowCount + nRepeat < nOfst;
bLongTab = bLongTab && bSplit;
}
if( bSplit )
{
mrpFrame->InsertBehind( mrpLay, mrpPrv );
{
SwFrameAreaDefinition::FrameAreaWriteAccess aFrm(*mrpFrame);
aFrm.Pos() = mrpLay->getFrameArea().Pos();
aFrm.Pos().AdjustY(1 );
}
mrpPrv = mrpFrame;
if( mrpFrame->IsTabFrame() )
{
SwTabFrame* pTab = static_cast<SwTabFrame*>(mrpFrame);
// #i33629#, #i29955#
::RegistFlys( pTab->FindPageFrame(), pTab );
SwFrame *pRow = pTab->Lower();
SwTabFrame *pFoll = new SwTabFrame( *pTab );
SwFrame *pPrv;
if( nRepeat > 0 )
{
sw::FlyCreationSuppressor aSuppressor;
// Insert new headlines:
sal_uInt16 nRowIdx = 0;
SwRowFrame* pHeadline = nullptr;
while( nRowIdx < nRepeat )
{
OSL_ENSURE( pTab->GetTable()->GetTabLines()[ nRowIdx ], "Table without rows?" );
pHeadline =
new SwRowFrame( *pTab->GetTable()->GetTabLines()[ nRowIdx ], pTab );
pHeadline->SetRepeatedHeadline( true );
pHeadline->InsertBefore( pFoll, nullptr );
pHeadline->RegistFlys();
++nRowIdx;
}
pPrv = pHeadline;
nRows = nRows + nRepeat;
}
else
pPrv = nullptr;
while( pRow && nRowCount < nOfst )
{
pRow = pRow->GetNext();
++nRowCount;
}
while ( pRow )
{
SwFrame* pNxt = pRow->GetNext();
pRow->RemoveFromLayout();
pRow->InsertBehind( pFoll, pPrv );
pPrv = pRow;
pRow = pNxt;
}
mrpFrame = pFoll;
}
else
{
SwTextFrame *const pNew = static_cast<SwTextFrame*>(
static_cast<SwTextFrame*>(mrpFrame)
->GetTextNodeFirst()->MakeFrame(mrpFrame));
pNew->ManipOfst( TextFrameIndex(nOfst) );
pNew->SetFollow( static_cast<SwTextFrame*>(mrpFrame)->GetFollow() );
static_cast<SwTextFrame*>(mrpFrame)->SetFollow( pNew );
mrpFrame = pNew;
}
}
}
}
SwPageFrame* pLastPage = mrpPage;
if( CheckInsertPage() )
{
CheckFlyCache_( pLastPage );
if( mrpPrv && mrpPrv->IsTextFrame() && !mrpPrv->isFrameAreaSizeValid() )
{
SwFrameAreaDefinition::FrameAreaWriteAccess aFrm(*mrpPrv);
aFrm.Height( mrpPrv->GetUpper()->getFramePrintArea().Height() );
}
bRet = true;
mrpPrv = nullptr;
mnParagraphCnt = 0;
if ( mrpActualSection )
{
//Did the SectionFrame even have a content? If not, we can
//directly put it somewhere else
SwSectionFrame *pSct;
bool bInit = false;
if ( !mrpActualSection->GetSectionFrame()->ContainsContent())
{
pSct = mrpActualSection->GetSectionFrame();
pSct->RemoveFromLayout();
}
else
{
pSct = new SwSectionFrame(
*mrpActualSection->GetSectionFrame(), false );
mrpActualSection->GetSectionFrame()->SimpleFormat();
bInit = true;
}
mrpActualSection->SetSectionFrame( pSct );
pSct->InsertBehind( mrpLay, nullptr );
if( bInit )
{
pSct->Init();
}
{
SwFrameAreaDefinition::FrameAreaWriteAccess aFrm(*pSct);
aFrm.Pos() = mrpLay->getFrameArea().Pos();
aFrm.Pos().AdjustY(1 ); //because of the notifications
}
mrpLay = pSct;
if ( mrpLay->Lower() && mrpLay->Lower()->IsLayoutFrame() )
mrpLay = mrpLay->GetNextLayoutLeaf();
}
}
} while( bLongTab || ( mpImpl && mnIndex < mpImpl->size() &&
mpImpl->GetBreakIndex( mnIndex ) == nNodeIndex ) );
}
mbFirst = false;
return bRet;
}
namespace {
struct SdrObjectCompare
{
bool operator()( const SdrObject* pF1, const SdrObject* pF2 ) const
{
return pF1->GetOrdNum() < pF2->GetOrdNum();
}
};
struct FlyCacheCompare
{
bool operator()( const SwFlyCache* pC1, const SwFlyCache* pC2 ) const
{
return pC1->nOrdNum < pC2->nOrdNum;
}
};
}
/**
* If a new page is inserted, the last page is analysed.
* If there are text frames with default position, the fly cache
* is checked, if these frames are stored in the cache.
*/
void SwLayHelper::CheckFlyCache_( SwPageFrame* pPage )
{
if( !mpImpl || !pPage )
return;
const size_t nFlyCount = mpImpl->GetFlyCount();
// Any text frames at the page, fly cache available?
if( !(pPage->GetSortedObjs() && mnFlyIdx < nFlyCount) )
return;
SwSortedObjs &rObjs = *pPage->GetSortedObjs();
sal_uInt16 nPgNum = pPage->GetPhyPageNum();
// NOTE: Here we do not use the absolute ordnums but
// relative ordnums for the objects on this page.
// skip fly frames from pages before the current page
while( mnFlyIdx < nFlyCount &&
mpImpl->GetFlyCache(mnFlyIdx).nPageNum < nPgNum )
++mnFlyIdx;
// sort cached objects on this page by ordnum
o3tl::sorted_vector< const SwFlyCache*, FlyCacheCompare > aFlyCacheSet;
size_t nIdx = mnFlyIdx;
SwFlyCache* pFlyC;
while( nIdx < nFlyCount &&
( pFlyC = &mpImpl->GetFlyCache( nIdx ) )->nPageNum == nPgNum )
{
aFlyCacheSet.insert( pFlyC );
++nIdx;
}
// sort objects on this page by ordnum
o3tl::sorted_vector< const SdrObject*, SdrObjectCompare > aFlySet;
for (SwAnchoredObject* pAnchoredObj : rObjs)
{
if (SwFlyFrame *pFly = pAnchoredObj->DynCastFlyFrame()) // a text frame?
{
if( pFly->GetAnchorFrame() &&
!pFly->GetAnchorFrame()->FindFooterOrHeader() )
{
const SwContact *pC = ::GetUserCall( pAnchoredObj->GetDrawObj() );
if( pC )
{
aFlySet.insert( pAnchoredObj->GetDrawObj() );
}
}
}
}
if ( aFlyCacheSet.size() != aFlySet.size() )
return;
auto aFlySetIt = aFlySet.begin();
for ( const SwFlyCache* pFlyCache : aFlyCacheSet )
{
SwFlyFrame* pFly = const_cast<SwVirtFlyDrawObj*>(static_cast<const SwVirtFlyDrawObj*>(*aFlySetIt))->GetFlyFrame();
if ( pFly->getFrameArea().Left() == FAR_AWAY )
{
// we get the stored information
SwFrameAreaDefinition::FrameAreaWriteAccess aFrm(*pFly);
aFrm.Pos().setX( pFlyCache->Left() + pPage->getFrameArea().Left() );
aFrm.Pos().setY( pFlyCache->Top() + pPage->getFrameArea().Top() );
if ( mpImpl->IsUseFlyCache() )
{
aFrm.Width( pFlyCache->Width() );
aFrm.Height( pFlyCache->Height() );
}
}
++aFlySetIt;
}
}
SwLayCacheIoImpl::SwLayCacheIoImpl( SvStream& rStrm, bool bWrtMd ) :
m_pStream( &rStrm ),
m_nFlagRecEnd ( 0 ),
m_nMajorVersion(SW_LAYCACHE_IO_VERSION_MAJOR),
m_nMinorVersion(SW_LAYCACHE_IO_VERSION_MINOR),
m_bWriteMode( bWrtMd ),
m_bError( false )
{
if( m_bWriteMode )
m_pStream->WriteUInt16( m_nMajorVersion )
.WriteUInt16( m_nMinorVersion );
else
m_pStream->ReadUInt16( m_nMajorVersion )
.ReadUInt16( m_nMinorVersion );
}
void SwLayCacheIoImpl::OpenRec( sal_uInt8 cType )
{
sal_uInt32 nPos = m_pStream->Tell();
if( m_bWriteMode )
{
m_aRecords.emplace_back(cType, nPos );
m_pStream->WriteUInt32( 0 );
}
else
{
sal_uInt32 nVal(0);
m_pStream->ReadUInt32( nVal );
sal_uInt8 cRecTyp = static_cast<sal_uInt8>(nVal);
if (!nVal || cRecTyp != cType || !m_pStream->good())
{
OSL_ENSURE( nVal, "OpenRec: Record-Header is 0" );
OSL_ENSURE( cRecTyp == cType, "OpenRec: Wrong Record Type" );
m_aRecords.emplace_back(0, m_pStream->Tell() );
m_bError = true;
}
else
{
sal_uInt32 nSize = nVal >> 8;
m_aRecords.emplace_back(cRecTyp, nPos+nSize );
}
}
}
// Close record
void SwLayCacheIoImpl::CloseRec()
{
bool bRes = true;
OSL_ENSURE( !m_aRecords.empty(), "CloseRec: no levels" );
if( !m_aRecords.empty() )
{
sal_uInt32 nPos = m_pStream->Tell();
if( m_bWriteMode )
{
sal_uInt32 nBgn = m_aRecords.back().size;
m_pStream->Seek( nBgn );
sal_uInt32 nSize = nPos - nBgn;
sal_uInt32 nVal = ( nSize << 8 ) | m_aRecords.back().type;
m_pStream->WriteUInt32( nVal );
m_pStream->Seek( nPos );
if( m_pStream->GetError() != ERRCODE_NONE )
bRes = false;
}
else
{
sal_uInt32 n = m_aRecords.back().size;
OSL_ENSURE( n >= nPos, "CloseRec: too much data read" );
if( n != nPos )
{
m_pStream->Seek( n );
if( n < nPos )
bRes = false;
}
if( m_pStream->GetErrorCode() != ERRCODE_NONE )
bRes = false;
}
m_aRecords.pop_back();
}
if( !bRes )
m_bError = true;
}
sal_uInt32 SwLayCacheIoImpl::BytesLeft()
{
sal_uInt32 n = 0;
if( !m_bError && !m_aRecords.empty() )
{
sal_uInt32 nEndPos = m_aRecords.back().size;
sal_uInt32 nPos = m_pStream->Tell();
if( nEndPos > nPos )
n = nEndPos - nPos;
}
return n;
}
sal_uInt8 SwLayCacheIoImpl::Peek()
{
sal_uInt8 c(0);
if( !m_bError )
{
sal_uInt32 nPos = m_pStream->Tell();
m_pStream->ReadUChar( c );
m_pStream->Seek( nPos );
if( m_pStream->GetErrorCode() != ERRCODE_NONE )
{
c = 0;
m_bError = true;
}
}
return c;
}
void SwLayCacheIoImpl::SkipRec()
{
sal_uInt8 c = Peek();
OpenRec( c );
m_pStream->Seek( m_aRecords.back().size );
CloseRec();
}
sal_uInt8 SwLayCacheIoImpl::OpenFlagRec()
{
OSL_ENSURE( !m_bWriteMode, "OpenFlagRec illegal in write mode" );
sal_uInt8 cFlags(0);
m_pStream->ReadUChar( cFlags );
m_nFlagRecEnd = m_pStream->Tell() + ( cFlags & 0x0F );
return (cFlags >> 4);
}
void SwLayCacheIoImpl::OpenFlagRec( sal_uInt8 nFlags, sal_uInt8 nLen )
{
OSL_ENSURE( m_bWriteMode, "OpenFlagRec illegal in read mode" );
OSL_ENSURE( (nFlags & 0xF0) == 0, "illegal flags set" );
OSL_ENSURE( nLen < 16, "wrong flag record length" );
sal_uInt8 cFlags = (nFlags << 4) + nLen;
m_pStream->WriteUChar( cFlags );
m_nFlagRecEnd = m_pStream->Tell() + nLen;
}
void SwLayCacheIoImpl::CloseFlagRec()
{
if( m_bWriteMode )
{
OSL_ENSURE( m_pStream->Tell() == m_nFlagRecEnd, "Wrong amount of data written" );
}
else
{
OSL_ENSURE( m_pStream->Tell() <= m_nFlagRecEnd, "Too many data read" );
if( m_pStream->Tell() != m_nFlagRecEnd )
m_pStream->Seek( m_nFlagRecEnd );
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|