LibreOffice Module sw (master)  1
porglue.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <swrect.hxx>
21 #include <viewopt.hxx>
22 #include "porglue.hxx"
23 #include "inftxt.hxx"
24 #include "porlay.hxx"
25 #include "porfly.hxx"
26 #include <comphelper/string.hxx>
27 
28 SwGluePortion::SwGluePortion( const sal_uInt16 nInitFixWidth )
29  : nFixWidth( nInitFixWidth )
30 {
33 }
34 
36 {
37  // FIXME why nOfst > GetLen() ? is that supposed to be > Width() ?
38  if( !GetLen() || nOfst > sal_Int32(GetLen()) || !Width() )
40  else
41  return TextFrameIndex(nOfst / (Width() / sal_Int32(GetLen())));
42 }
43 
45 {
46  if (TextFrameIndex(1) >= GetLen() || rInf.GetLen() > GetLen() || !Width() || !GetLen())
47  return SwPosSize(*this);
48  else
49  return SwPosSize((Width() / sal_Int32(GetLen())) * sal_Int32(rInf.GetLen()), Height());
50 }
51 
52 bool SwGluePortion::GetExpText( const SwTextSizeInfo &rInf, OUString &rText ) const
53 {
54  if( GetLen() && rInf.OnWin() &&
55  rInf.GetOpt().IsBlank() && rInf.IsNoSymbol() )
56  {
57  OUStringBuffer aBuf;
59  rText = aBuf.makeStringAndClear();
60  return true;
61  }
62  return false;
63 }
64 
65 void SwGluePortion::Paint( const SwTextPaintInfo &rInf ) const
66 {
67  if( !GetLen() )
68  return;
69 
70  if( rInf.GetFont()->IsPaintBlank() )
71  {
72  OUStringBuffer aBuf;
73  comphelper::string::padToLength(aBuf, GetFixWidth() / sal_Int32(GetLen()), ' ');
74  OUString aText(aBuf.makeStringAndClear());
75  SwTextPaintInfo aInf( rInf, &aText );
76  aInf.DrawText(*this, TextFrameIndex(aText.getLength()), true);
77  }
78 
79  if( rInf.OnWin() && rInf.GetOpt().IsBlank() && rInf.IsNoSymbol() )
80  {
81 #if OSL_DEBUG_LEVEL > 0
82  const sal_Unicode cChar = rInf.GetChar( rInf.GetIdx() );
83  OSL_ENSURE( CH_BLANK == cChar || CH_BULLET == cChar,
84  "SwGluePortion::Paint: blank expected" );
85 #endif
86  if (TextFrameIndex(1) == GetLen())
87  {
88  OUString aBullet( CH_BULLET );
89  SwPosSize aBulletSize( rInf.GetTextSize( aBullet ) );
90  Point aPos( rInf.GetPos() );
91  aPos.AdjustX((Width()/2) - (aBulletSize.Width()/2) );
92  SwTextPaintInfo aInf( rInf, &aBullet );
93  aInf.SetPos( aPos );
94  SwTextPortion aBulletPor;
95  aBulletPor.Width( aBulletSize.Width() );
96  aBulletPor.Height( aBulletSize.Height() );
97  aBulletPor.SetAscent( GetAscent() );
98  aInf.DrawText(aBulletPor, TextFrameIndex(aBullet.getLength()), true);
99  }
100  else
101  {
102  SwTextSlot aSlot( &rInf, this, true, false );
103  rInf.DrawText( *this, rInf.GetLen(), true );
104  }
105  }
106 }
107 
108 void SwGluePortion::MoveGlue( SwGluePortion *pTarget, const long nPrtGlue )
109 {
110  auto nPrt = std::min( nPrtGlue, GetPrtGlue() );
111  if( 0 < nPrt )
112  {
113  pTarget->AddPrtWidth( nPrt ); //TODO: overflow
114  SubPrtWidth( nPrt ); //TODO: overflow
115  }
116 }
117 
119 {
120  // The GluePortion is extracted and flushed away ...
121  AddPrtWidth( pVictim->PrtWidth() );
122  SetLen( pVictim->GetLen() + GetLen() );
123  if( Height() < pVictim->Height() )
124  Height( pVictim->Height() );
125 
126  AdjFixWidth();
127  Cut( pVictim );
128  delete pVictim;
129 }
130 
135  :SwGluePortion( sal_uInt16(rRect.Width()) ), nFix( sal_uInt16(rRect.Left()) )
136 {
137  Height( sal_uInt16(rRect.Height()) );
139 }
140 
142  : SwGluePortion(0), nFix(0)
143 {
145 }
146 
148  :SwGluePortion( 0 )
149 {
151 }
152 
164 {
165  SwGluePortion *pRight = nullptr;
166  bool bNoMove = nullptr != pCurr->GetpKanaComp();
167  while( pRight != this )
168  {
169 
170  // 1) We search for the left Glue
171  SwLinePortion *pPos = this;
172  SwGluePortion *pLeft = nullptr;
173  while( pPos )
174  {
175  if( pPos->InFixMargGrp() )
176  pLeft = static_cast<SwGluePortion*>(pPos);
177  pPos = pPos->GetNextPortion();
178  if( pPos == pRight)
179  pPos = nullptr;
180  }
181 
182  // Two adjoining FlyPortions are merged
183  if( pRight && pLeft && pLeft->GetNextPortion() == pRight )
184  {
185  pRight->MoveAllGlue( pLeft );
186  pRight = nullptr;
187  }
188  auto nRightGlue = pRight && 0 < pRight->GetPrtGlue()
189  ? pRight->GetPrtGlue() : 0;
190  // 2) balance left and right Glue
191  // But not for tabs ...
192  if( pLeft && nRightGlue && !pRight->InTabGrp() )
193  {
194  // pPrev is the portion immediately before pRight
195  SwLinePortion *pPrev = pRight->FindPrevPortion( pLeft );
196 
197  if ( pRight->IsFlyPortion() && pRight->GetLen() )
198  {
199  SwFlyPortion *pFly = static_cast<SwFlyPortion *>(pRight);
200  if ( pFly->GetBlankWidth() < nRightGlue )
201  {
202  // Creating new TextPortion, that takes over the
203  // Blank previously swallowed by the Fly.
204  nRightGlue = nRightGlue - pFly->GetBlankWidth();
205  pFly->SubPrtWidth( pFly->GetBlankWidth() );
206  pFly->SetLen(TextFrameIndex(0));
207  SwTextPortion *pNewPor = new SwTextPortion;
208  pNewPor->SetLen(TextFrameIndex(1));
209  pNewPor->Height( pFly->Height() );
210  pNewPor->Width( pFly->GetBlankWidth() );
211  pFly->Insert( pNewPor );
212  }
213  else
214  pPrev = pLeft;
215  }
216  while( pPrev != pLeft )
217  {
218  if( bNoMove || pPrev->PrtWidth() >= nRightGlue ||
219  pPrev->InHyphGrp() || pPrev->IsKernPortion() )
220  {
221  // The portion before the pRight cannot be moved
222  // because no Glue is remaining.
223  // We set the break condition:
224  pPrev = pLeft;
225  }
226  else
227  {
228  nRightGlue = nRightGlue - pPrev->PrtWidth();
229  // pPrev is moved behind pRight. For this the
230  // Glue value between pRight and pLeft gets balanced.
231  pRight->MoveGlue( pLeft, pPrev->PrtWidth() );
232  // Now fix the linking of our portions.
233  SwLinePortion *pPrevPrev = pPrev->FindPrevPortion( pLeft );
234  pPrevPrev->SetNextPortion( pRight );
235  pPrev->SetNextPortion( pRight->GetNextPortion() );
236  pRight->SetNextPortion( pPrev );
237  if ( pPrev->GetNextPortion() && pPrev->InTextGrp()
238  && pPrev->GetNextPortion()->IsHolePortion() )
239  {
240  SwHolePortion *pHolePor =
241  static_cast<SwHolePortion*>(pPrev->GetNextPortion());
242  if ( !pHolePor->GetNextPortion() ||
243  !pHolePor->GetNextPortion()->InFixMargGrp() )
244  {
245  pPrev->AddPrtWidth( pHolePor->GetBlankWidth() );
246  pPrev->SetLen(pPrev->GetLen() + TextFrameIndex(1));
247  pPrev->SetNextPortion( pHolePor->GetNextPortion() );
248  delete pHolePor;
249  }
250  }
251  pPrev = pPrevPrev;
252  }
253  }
254  }
255  // If no left Glue remains, we set the break condition.
256  pRight = pLeft ? pLeft : this;
257  }
258 }
259 
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void MoveAllGlue(SwGluePortion *pTarget)
Definition: porglue.hxx:76
void AdjFixWidth()
Definition: porglue.hxx:70
std::deque< sal_uInt16 > * GetpKanaComp() const
Definition: porlay.hxx:184
sal_uInt16 Height() const
Definition: possiz.hxx:44
SwFont * GetFont()
Definition: inftxt.hxx:238
void SetPos(const Point &rNew)
Definition: inftxt.hxx:436
aBuf
void Height(long nNew)
Definition: swrect.hxx:191
void MoveGlue(SwGluePortion *pTarget, const long nPrtGlue)
Definition: porglue.cxx:108
void SetLen(TextFrameIndex const nLen)
Definition: porlin.hxx:75
const sal_Unicode CH_BULLET
Definition: swfont.hxx:46
void DrawText(const OUString &rText, const SwLinePortion &rPor, TextFrameIndex nIdx=TextFrameIndex(0), TextFrameIndex nLen=TextFrameIndex(COMPLETE_STRING), const bool bKern=false) const
Definition: inftxt.hxx:747
Of course Writer needs its own rectangles.
Definition: swrect.hxx:35
SwPosSize GetTextSize(OutputDevice *pOut, const SwScriptInfo *pSI, const OUString &rText, TextFrameIndex nIdx, TextFrameIndex nLen) const
Definition: inftxt.cxx:382
sal_uInt16 sal_Unicode
bool InHyphGrp() const
Definition: porlin.hxx:101
sal_Unicode GetChar(TextFrameIndex const nPos) const
Definition: inftxt.hxx:247
bool IsBlank(bool bHard=false) const
Definition: viewopt.hxx:220
bool InFixMargGrp() const
Definition: porlin.hxx:108
This portion represents a part of the paragraph string.
Definition: portxt.hxx:27
void Join(SwGluePortion *pVictim)
Definition: porglue.cxx:118
const SwViewOption & GetOpt() const
Definition: inftxt.hxx:245
virtual TextFrameIndex GetModelPositionForViewPoint(sal_uInt16 nOfst) const
the parameter is actually SwTwips apparently?
Definition: porlin.cxx:222
SwPosSize(const sal_uInt16 nW=0, const sal_uInt16 nH=0)
Definition: possiz.hxx:31
virtual bool GetExpText(const SwTextSizeInfo &rInf, OUString &rText) const override
Definition: porglue.cxx:52
Collection of SwLinePortion instances, representing one line of text.
Definition: porlay.hxx:78
void SetWhichPor(const PortionType nNew)
Definition: porlin.hxx:94
bool IsFlyPortion() const
Definition: porlin.hxx:125
const sal_Unicode CH_BLANK
Definition: swfont.hxx:42
bool IsKernPortion() const
Definition: porlin.hxx:132
SwGluePortion(const sal_uInt16 nInitFixWidth)
Definition: porglue.cxx:28
virtual void Paint(const SwTextPaintInfo &rInf) const override
Definition: porglue.cxx:65
TextFrameIndex GetIdx() const
Definition: inftxt.hxx:278
long GetPrtGlue() const
Definition: porglue.hxx:66
sal_uInt16 GetBlankWidth() const
Definition: porfly.hxx:37
virtual SwPosSize GetTextSize(const SwTextSizeInfo &rInfo) const override
Definition: porglue.cxx:44
SwLinePortion * Cut(SwLinePortion *pVictim)
Definition: porlin.cxx:200
TextFrameIndex GetLen() const
Definition: porlin.hxx:74
bool InTabGrp() const
Definition: porlin.hxx:100
bool IsNoSymbol() const
Definition: inftxt.hxx:285
bool IsPaintBlank() const
Definition: swfont.hxx:260
bool IsHolePortion() const
Definition: porlin.hxx:126
const Point & GetPos() const
Definition: inftxt.hxx:435
sal_uInt16 GetBlankWidth() const
Definition: portxt.hxx:69
Base class for anything that can be part of a line in the Writer layout.
Definition: porlin.hxx:50
For the text replacement and restoration of SwTextSizeInfo.
Definition: inftxt.hxx:680
bool OnWin() const
Definition: inftxt.hxx:199
TextFrameIndex GetLen() const
Definition: inftxt.hxx:280
SwLinePortion * FindPrevPortion(const SwLinePortion *pRoot)
Definition: porlin.cxx:209
bool InTextGrp() const
Definition: porlin.hxx:98
sal_uInt16 Width() const
Definition: possiz.hxx:46
sal_uInt16 nFixWidth
Definition: porglue.hxx:30
void SetNextPortion(SwLinePortion *pNew)
Definition: porlin.hxx:76
void SetAscent(const sal_uInt16 nNewAsc)
Definition: porlin.hxx:79
Left
void PrtWidth(sal_uInt16 nNewWidth)
Definition: porlin.hxx:80
void AdjustRight(const SwLineLayout *pCurr)
In the outer loop all portions are inspected - the GluePortions at the end are processed first...
Definition: porglue.cxx:163
sal_uInt16 PrtWidth() const
Definition: porlin.hxx:81
void SubPrtWidth(const sal_uInt16 nNew)
Definition: porlin.hxx:83
sal_uInt16 GetFixWidth() const
Definition: porglue.hxx:37
SwLinePortion * GetNextPortion() const
Definition: porlin.hxx:72
o3tl::strong_int< sal_Int32, struct Tag_TextFrameIndex > TextFrameIndex
Denotes a character index in a text frame at a layout level, after extent mapping from a text node at...
sal_uInt16 & GetAscent()
Definition: porlin.hxx:77
virtual SwLinePortion * Insert(SwLinePortion *pPortion)
Definition: porlin.cxx:168
virtual TextFrameIndex GetModelPositionForViewPoint(sal_uInt16 nOfst) const override
the parameter is actually SwTwips apparently?
Definition: porglue.cxx:35
void AddPrtWidth(const sal_uInt16 nNew)
Definition: porlin.hxx:82
OStringBuffer & padToLength(OStringBuffer &rBuffer, sal_Int32 nLength, char cFill= '\0')