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 <paratr.hxx>
22 #include <viewopt.hxx>
23 #include <SwPortionHandler.hxx>
24 #include "porglue.hxx"
25 #include "inftxt.hxx"
26 #include "porlay.hxx"
27 #include "porfly.hxx"
28 #include <comphelper/string.hxx>
29 
30 SwGluePortion::SwGluePortion( const sal_uInt16 nInitFixWidth )
31  : nFixWidth( nInitFixWidth )
32 {
35 }
36 
37 TextFrameIndex SwGluePortion::GetCursorOfst(const sal_uInt16 nOfst) const
38 {
39  // FIXME why nOfst > GetLen() ? is that supposed to be > Width() ?
40  if( !GetLen() || nOfst > sal_Int32(GetLen()) || !Width() )
41  return SwLinePortion::GetCursorOfst( nOfst );
42  else
43  return TextFrameIndex(nOfst / (Width() / sal_Int32(GetLen())));
44 }
45 
47 {
48  if (TextFrameIndex(1) >= GetLen() || rInf.GetLen() > GetLen() || !Width() || !GetLen())
49  return SwPosSize(*this);
50  else
51  return SwPosSize((Width() / sal_Int32(GetLen())) * sal_Int32(rInf.GetLen()), Height());
52 }
53 
54 bool SwGluePortion::GetExpText( const SwTextSizeInfo &rInf, OUString &rText ) const
55 {
56  if( GetLen() && rInf.OnWin() &&
57  rInf.GetOpt().IsBlank() && rInf.IsNoSymbol() )
58  {
59  OUStringBuffer aBuf;
61  rText = aBuf.makeStringAndClear();
62  return true;
63  }
64  return false;
65 }
66 
67 void SwGluePortion::Paint( const SwTextPaintInfo &rInf ) const
68 {
69  if( !GetLen() )
70  return;
71 
72  if( rInf.GetFont()->IsPaintBlank() )
73  {
74  OUStringBuffer aBuf;
75  comphelper::string::padToLength(aBuf, GetFixWidth() / sal_Int32(GetLen()), ' ');
76  OUString aText(aBuf.makeStringAndClear());
77  SwTextPaintInfo aInf( rInf, &aText );
78  aInf.DrawText(*this, TextFrameIndex(aText.getLength()), true);
79  }
80 
81  if( rInf.OnWin() && rInf.GetOpt().IsBlank() && rInf.IsNoSymbol() )
82  {
83 #if OSL_DEBUG_LEVEL > 0
84  const sal_Unicode cChar = rInf.GetChar( rInf.GetIdx() );
85  OSL_ENSURE( CH_BLANK == cChar || CH_BULLET == cChar,
86  "SwGluePortion::Paint: blank expected" );
87 #endif
88  if (TextFrameIndex(1) == GetLen())
89  {
90  OUString aBullet( CH_BULLET );
91  SwPosSize aBulletSize( rInf.GetTextSize( aBullet ) );
92  Point aPos( rInf.GetPos() );
93  aPos.AdjustX((Width()/2) - (aBulletSize.Width()/2) );
94  SwTextPaintInfo aInf( rInf, &aBullet );
95  aInf.SetPos( aPos );
96  SwTextPortion aBulletPor;
97  aBulletPor.Width( aBulletSize.Width() );
98  aBulletPor.Height( aBulletSize.Height() );
99  aBulletPor.SetAscent( GetAscent() );
100  aInf.DrawText(aBulletPor, TextFrameIndex(aBullet.getLength()), true);
101  }
102  else
103  {
104  SwTextSlot aSlot( &rInf, this, true, false );
105  rInf.DrawText( *this, rInf.GetLen(), true );
106  }
107  }
108 }
109 
110 void SwGluePortion::MoveGlue( SwGluePortion *pTarget, const long nPrtGlue )
111 {
112  auto nPrt = std::min( nPrtGlue, GetPrtGlue() );
113  if( 0 < nPrt )
114  {
115  pTarget->AddPrtWidth( nPrt ); //TODO: overflow
116  SubPrtWidth( nPrt ); //TODO: overflow
117  }
118 }
119 
121 {
122  // The GluePortion is extracted and flushed away ...
123  AddPrtWidth( pVictim->PrtWidth() );
124  SetLen( pVictim->GetLen() + GetLen() );
125  if( Height() < pVictim->Height() )
126  Height( pVictim->Height() );
127 
128  AdjFixWidth();
129  Cut( pVictim );
130  delete pVictim;
131 }
132 
137  :SwGluePortion( sal_uInt16(rRect.Width()) ), nFix( sal_uInt16(rRect.Left()) )
138 {
139  Height( sal_uInt16(rRect.Height()) );
141 }
142 
144  : SwGluePortion(0), nFix(0)
145 {
147 }
148 
150  :SwGluePortion( 0 )
151 {
153 }
154 
166 {
167  SwGluePortion *pRight = nullptr;
168  bool bNoMove = nullptr != pCurr->GetpKanaComp();
169  while( pRight != this )
170  {
171 
172  // 1) We search for the left Glue
173  SwLinePortion *pPos = this;
174  SwGluePortion *pLeft = nullptr;
175  while( pPos )
176  {
177  if( pPos->InFixMargGrp() )
178  pLeft = static_cast<SwGluePortion*>(pPos);
179  pPos = pPos->GetNextPortion();
180  if( pPos == pRight)
181  pPos = nullptr;
182  }
183 
184  // Two adjoining FlyPortions are merged
185  if( pRight && pLeft && pLeft->GetNextPortion() == pRight )
186  {
187  pRight->MoveAllGlue( pLeft );
188  pRight = nullptr;
189  }
190  auto nRightGlue = pRight && 0 < pRight->GetPrtGlue()
191  ? pRight->GetPrtGlue() : 0;
192  // 2) balance left and right Glue
193  // But not for tabs ...
194  if( pLeft && nRightGlue && !pRight->InTabGrp() )
195  {
196  // pPrev is the portion immediately before pRight
197  SwLinePortion *pPrev = pRight->FindPrevPortion( pLeft );
198 
199  if ( pRight->IsFlyPortion() && pRight->GetLen() )
200  {
201  SwFlyPortion *pFly = static_cast<SwFlyPortion *>(pRight);
202  if ( pFly->GetBlankWidth() < nRightGlue )
203  {
204  // Creating new TextPortion, that takes over the
205  // Blank previously swallowed by the Fly.
206  nRightGlue = nRightGlue - pFly->GetBlankWidth();
207  pFly->SubPrtWidth( pFly->GetBlankWidth() );
208  pFly->SetLen(TextFrameIndex(0));
209  SwTextPortion *pNewPor = new SwTextPortion;
210  pNewPor->SetLen(TextFrameIndex(1));
211  pNewPor->Height( pFly->Height() );
212  pNewPor->Width( pFly->GetBlankWidth() );
213  pFly->Insert( pNewPor );
214  }
215  else
216  pPrev = pLeft;
217  }
218  while( pPrev != pLeft )
219  {
220  if( bNoMove || pPrev->PrtWidth() >= nRightGlue ||
221  pPrev->InHyphGrp() || pPrev->IsKernPortion() )
222  {
223  // The portion before the pRight cannot be moved
224  // because no Glue is remaining.
225  // We set the break condition:
226  pPrev = pLeft;
227  }
228  else
229  {
230  nRightGlue = nRightGlue - pPrev->PrtWidth();
231  // pPrev is moved behind pRight. For this the
232  // Glue value between pRight and pLeft gets balanced.
233  pRight->MoveGlue( pLeft, pPrev->PrtWidth() );
234  // Now fix the linking of our portions.
235  SwLinePortion *pPrevPrev = pPrev->FindPrevPortion( pLeft );
236  pPrevPrev->SetNextPortion( pRight );
237  pPrev->SetNextPortion( pRight->GetNextPortion() );
238  pRight->SetNextPortion( pPrev );
239  if ( pPrev->GetNextPortion() && pPrev->InTextGrp()
240  && pPrev->GetNextPortion()->IsHolePortion() )
241  {
242  SwHolePortion *pHolePor =
243  static_cast<SwHolePortion*>(pPrev->GetNextPortion());
244  if ( !pHolePor->GetNextPortion() ||
245  !pHolePor->GetNextPortion()->InFixMargGrp() )
246  {
247  pPrev->AddPrtWidth( pHolePor->GetBlankWidth() );
248  pPrev->SetLen(pPrev->GetLen() + TextFrameIndex(1));
249  pPrev->SetNextPortion( pHolePor->GetNextPortion() );
250  delete pHolePor;
251  }
252  }
253  pPrev = pPrevPrev;
254  }
255  }
256  }
257  // If no left Glue remains, we set the break condition.
258  pRight = pLeft ? pLeft : this;
259  }
260 }
261 
262 /* 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
long AdjustX(long nHorzMove)
void SetPos(const Point &rNew)
Definition: inftxt.hxx:436
aBuf
void Height(long nNew)
Definition: swrect.hxx:189
void MoveGlue(SwGluePortion *pTarget, const long nPrtGlue)
Definition: porglue.cxx:110
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:746
Of course Writer needs its own rectangles.
Definition: swrect.hxx:34
SwPosSize GetTextSize(OutputDevice *pOut, const SwScriptInfo *pSI, const OUString &rText, TextFrameIndex nIdx, TextFrameIndex nLen) const
Definition: inftxt.cxx:386
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:218
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:120
const SwViewOption & GetOpt() const
Definition: inftxt.hxx:245
virtual TextFrameIndex GetCursorOfst(sal_uInt16 nOfst) const
the parameter is actually SwTwips apparently?
Definition: porlin.cxx:226
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:54
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
OStringBuffer & padToLength(OStringBuffer &rBuffer, sal_Int32 nLength, sal_Char cFill= '\0')
SwGluePortion(const sal_uInt16 nInitFixWidth)
Definition: porglue.cxx:30
virtual void Paint(const SwTextPaintInfo &rInf) const override
Definition: porglue.cxx:67
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:46
SwLinePortion * Cut(SwLinePortion *pVictim)
Definition: porlin.cxx:204
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:677
bool OnWin() const
Definition: inftxt.hxx:199
TextFrameIndex GetLen() const
Definition: inftxt.hxx:280
SwLinePortion * FindPrevPortion(const SwLinePortion *pRoot)
Definition: porlin.cxx:213
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
virtual TextFrameIndex GetCursorOfst(sal_uInt16 nOfst) const override
the parameter is actually SwTwips apparently?
Definition: porglue.cxx:37
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:165
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:172
void AddPrtWidth(const sal_uInt16 nNew)
Definition: porlin.hxx:82