Kaydet (Commit) 7ef1ff66 authored tarafından Matteo Casalin's avatar Matteo Casalin

Group common code (multiplication/division/type conversion)

Change-Id: Ib220dcb0e714b9ab4389997d3466ef7240882e23
üst 58702f9a
...@@ -303,6 +303,17 @@ SwTable::~SwTable() ...@@ -303,6 +303,17 @@ SwTable::~SwTable()
delete pHTMLLayout; delete pHTMLLayout;
} }
namespace
{
template<class T>
inline T lcl_MulDiv64(sal_uInt64 nA, sal_uInt64 nM, sal_uInt64 nD)
{
return static_cast<T>((nA*nM)/nD);
}
}
static void FmtInArr( std::vector<SwFmt*>& rFmtArr, SwFmt* pBoxFmt ) static void FmtInArr( std::vector<SwFmt*>& rFmtArr, SwFmt* pBoxFmt )
{ {
std::vector<SwFmt*>::const_iterator it = std::find( rFmtArr.begin(), rFmtArr.end(), pBoxFmt ); std::vector<SwFmt*>::const_iterator it = std::find( rFmtArr.begin(), rFmtArr.end(), pBoxFmt );
...@@ -323,10 +334,8 @@ static void lcl_ModifyLines( SwTableLines &rLines, const long nOld, ...@@ -323,10 +334,8 @@ static void lcl_ModifyLines( SwTableLines &rLines, const long nOld,
for( size_t i = 0; i < rFmtArr.size(); ++i ) for( size_t i = 0; i < rFmtArr.size(); ++i )
{ {
SwFmt* pFmt = rFmtArr[i]; SwFmt* pFmt = rFmtArr[i];
sal_uInt64 nBox = pFmt->GetFrmSize().GetWidth(); const SwTwips nBox = lcl_MulDiv64<SwTwips>(pFmt->GetFrmSize().GetWidth(), nNew, nOld);
nBox *= nNew; SwFmtFrmSize aNewBox( ATT_VAR_SIZE, nBox, 0 );
nBox /= nOld;
SwFmtFrmSize aNewBox( ATT_VAR_SIZE, SwTwips(nBox), 0 );
pFmt->LockModify(); pFmt->LockModify();
pFmt->SetFmtAttr( aNewBox ); pFmt->SetFmtAttr( aNewBox );
pFmt->UnlockModify(); pFmt->UnlockModify();
...@@ -353,10 +362,7 @@ static void lcl_ModifyBoxes( SwTableBoxes &rBoxes, const long nOld, ...@@ -353,10 +362,7 @@ static void lcl_ModifyBoxes( SwTableBoxes &rBoxes, const long nOld,
nOriginalSum += nBox; nOriginalSum += nBox;
nBox *= nNew; nBox *= nNew;
nBox /= nOld; nBox /= nOld;
sal_uInt64 nWishedSum = nOriginalSum; const sal_uInt64 nWishedSum = lcl_MulDiv64<sal_uInt64>(nOriginalSum, nNew, nOld) - nSum;
nWishedSum *= nNew;
nWishedSum /= nOld;
nWishedSum -= nSum;
if( nWishedSum > 0 ) if( nWishedSum > 0 )
{ {
if( nBox == nWishedSum ) if( nBox == nWishedSum )
...@@ -447,23 +453,21 @@ static void lcl_SortedTabColInsert( SwTabCols &rToFill, const SwTableBox *pBox, ...@@ -447,23 +453,21 @@ static void lcl_SortedTabColInsert( SwTabCols &rToFill, const SwTableBox *pBox,
const SwTableBoxes &rBoxes = pLine->GetTabBoxes(); const SwTableBoxes &rBoxes = pLine->GetTabBoxes();
for ( size_t i = 0; i < rBoxes.size(); ++i ) for ( size_t i = 0; i < rBoxes.size(); ++i )
{ {
SwTwips nWidth = rBoxes[i]->GetFrmFmt()->GetFrmSize().GetWidth(); const SwTwips nWidth = rBoxes[i]->GetFrmFmt()->GetFrmSize().GetWidth();
nSum += nWidth; nSum += nWidth;
sal_uInt64 nTmp = nSum; const long nTmp = lcl_MulDiv64<long>(nSum, nAct, nWish);
nTmp *= nAct;
nTmp /= nWish;
if (rBoxes[i] != pCur) if (rBoxes[i] != pCur)
{ {
if ( pLine == pBox->GetUpper() || 0 == nLeftMin ) if ( pLine == pBox->GetUpper() || 0 == nLeftMin )
nLeftMin = static_cast<long>(nTmp - nPos); nLeftMin = nTmp - nPos;
nPos = static_cast<long>(nTmp); nPos = nTmp;
} }
else else
{ {
nSum -= nWidth; nSum -= nWidth;
if ( 0 == nRightMax ) if ( 0 == nRightMax )
nRightMax = static_cast<long>(nTmp - nPos); nRightMax = nTmp - nPos;
break; break;
} }
} }
...@@ -707,11 +711,9 @@ static void lcl_ProcessBoxSet( SwTableBox *pBox, Parm &rParm ) ...@@ -707,11 +711,9 @@ static void lcl_ProcessBoxSet( SwTableBox *pBox, Parm &rParm )
const SwTableBoxes &rBoxes = pLine->GetTabBoxes(); const SwTableBoxes &rBoxes = pLine->GetTabBoxes();
for ( size_t i = 0; (i < rBoxes.size()) && (rBoxes[i] != pCur); ++i) for ( size_t i = 0; (i < rBoxes.size()) && (rBoxes[i] != pCur); ++i)
{ {
sal_uInt64 nWidth = rBoxes[i]->GetFrmFmt()-> nLeft += lcl_MulDiv64<long>(
GetFrmSize().GetWidth(); rBoxes[i]->GetFrmFmt()->GetFrmSize().GetWidth(),
nWidth *= nOldAct; nOldAct, rParm.nOldWish);
nWidth /= rParm.nOldWish;
nLeft += static_cast<long>(nWidth);
} }
pCur = pLine->GetUpper(); pCur = pLine->GetUpper();
pLine = pCur ? pCur->GetUpper() : 0; pLine = pCur ? pCur->GetUpper() : 0;
...@@ -721,10 +723,10 @@ static void lcl_ProcessBoxSet( SwTableBox *pBox, Parm &rParm ) ...@@ -721,10 +723,10 @@ static void lcl_ProcessBoxSet( SwTableBox *pBox, Parm &rParm )
if ( nLeft != rParm.rOld.GetLeft() ) // There are still boxes before this. if ( nLeft != rParm.rOld.GetLeft() ) // There are still boxes before this.
{ {
// Right edge is left edge plus width. // Right edge is left edge plus width.
sal_uInt64 nWidth = pBox->GetFrmFmt()->GetFrmSize().GetWidth(); const long nWidth = lcl_MulDiv64<long>(
nWidth *= nOldAct; pBox->GetFrmFmt()->GetFrmSize().GetWidth(),
nWidth /= rParm.nOldWish; nOldAct, rParm.nOldWish);
long nRight = nLeft + (long)nWidth; const long nRight = nLeft + nWidth;
size_t nLeftPos = 0; size_t nLeftPos = 0;
size_t nRightPos = 0; size_t nRightPos = 0;
bool bFoundLeftPos = false; bool bFoundLeftPos = false;
...@@ -755,10 +757,10 @@ static void lcl_ProcessBoxSet( SwTableBox *pBox, Parm &rParm ) ...@@ -755,10 +757,10 @@ static void lcl_ProcessBoxSet( SwTableBox *pBox, Parm &rParm )
if ( rParm.rOld.Count() ) if ( rParm.rOld.Count() )
{ {
// Calculate the difference to the edge touching the first box. // Calculate the difference to the edge touching the first box.
sal_uInt64 nWidth = pBox->GetFrmFmt()->GetFrmSize().GetWidth(); const long nWidth = lcl_MulDiv64<long>(
nWidth *= nOldAct; pBox->GetFrmFmt()->GetFrmSize().GetWidth(),
nWidth /= rParm.nOldWish; nOldAct, rParm.nOldWish);
const long nTmp = (long)nWidth + rParm.rOld.GetLeft(); const long nTmp = nWidth + rParm.rOld.GetLeft();
for ( size_t i = 0; i < rParm.rOld.Count(); ++i ) for ( size_t i = 0; i < rParm.rOld.Count(); ++i )
{ {
if ( nTmp >= (rParm.rOld[i] - COLFUZZY) && if ( nTmp >= (rParm.rOld[i] - COLFUZZY) &&
...@@ -1115,12 +1117,9 @@ static void lcl_CalcNewWidths( std::list<sal_uInt16> &rSpanPos, ChangeList& rCha ...@@ -1115,12 +1117,9 @@ static void lcl_CalcNewWidths( std::list<sal_uInt16> &rSpanPos, ChangeList& rCha
aNewSpanPos.push_back( nRowSpanCount ); aNewSpanPos.push_back( nRowSpanCount );
bRowSpan = bCurrRowSpan; bRowSpan = bCurrRowSpan;
nOrgSum += nCurrWidth; nOrgSum += nCurrWidth;
sal_uInt64 nSum = nOrgSum; const sal_uInt16 nPos = lcl_MulDiv64<sal_uInt16>(
nSum *= nWidth; lcl_MulDiv64<sal_uInt64>(nOrgSum, nWidth, nWish),
nSum /= nWish; nWish, nWidth);
nSum *= nWish;
nSum /= nWidth;
const sal_uInt16 nPos = static_cast<sal_uInt16>(nSum);
while( pCurr != rChanges.end() && pCurr->first < nPos ) while( pCurr != rChanges.end() && pCurr->first < nPos )
{ {
++nCurr; ++nCurr;
...@@ -1165,11 +1164,10 @@ static void lcl_CalcNewWidths( std::list<sal_uInt16> &rSpanPos, ChangeList& rCha ...@@ -1165,11 +1164,10 @@ static void lcl_CalcNewWidths( std::list<sal_uInt16> &rSpanPos, ChangeList& rCha
pCurr->second = pLeftMove->second; pCurr->second = pLeftMove->second;
else else
{ {
sal_uInt64 nTmp = pCurr->first - pLast->first; pCurr->second = lcl_MulDiv64<sal_uInt16>(
nTmp *= pLeftMove->second - pLast->second; pCurr->first - pLast->first,
nTmp /= pLeftMove->first - pLast->first; pLeftMove->second - pLast->second,
nTmp += pLast->second; pLeftMove->first - pLast->first) + pLast->second;
pCurr->second = (sal_uInt16)nTmp;
} }
} }
pLast = pCurr; pLast = pCurr;
...@@ -1189,11 +1187,10 @@ static void lcl_CalcNewWidths( std::list<sal_uInt16> &rSpanPos, ChangeList& rCha ...@@ -1189,11 +1187,10 @@ static void lcl_CalcNewWidths( std::list<sal_uInt16> &rSpanPos, ChangeList& rCha
pCurr->second = pLast->second; pCurr->second = pLast->second;
else else
{ {
sal_uInt64 nTmp = pCurr->first - pLast->first; pCurr->second = lcl_MulDiv64<sal_uInt16>(
nTmp *= pNext->second - pLast->second; pCurr->first - pLast->first,
nTmp /= pNext->first - pLast->first; pNext->second - pLast->second,
nTmp += pLast->second; pNext->first - pLast->first) + pLast->second;
pCurr->second = (sal_uInt16)nTmp;
} }
++pCurr; ++pCurr;
} }
...@@ -1243,10 +1240,8 @@ void SwTable::NewSetTabCols( Parm &rParm, const SwTabCols &rNew, ...@@ -1243,10 +1240,8 @@ void SwTable::NewSetTabCols( Parm &rParm, const SwTabCols &rNew,
nOldPos = rOld[i] - rParm.rOld.GetLeft(); nOldPos = rOld[i] - rParm.rOld.GetLeft();
nNewPos = rNew[i] - rParm.rNew.GetLeft(); nNewPos = rNew[i] - rParm.rNew.GetLeft();
} }
nNewPos *= rParm.nNewWish; nNewPos = lcl_MulDiv64<sal_uInt64>(nNewPos, rParm.nNewWish, nNewWidth);
nNewPos /= nNewWidth; nOldPos = lcl_MulDiv64<sal_uInt64>(nOldPos, rParm.nOldWish, nOldWidth);
nOldPos *= rParm.nOldWish;
nOldPos /= nOldWidth;
if( nOldPos != nNewPos && nNewPos > 0 && nOldPos > 0 ) if( nOldPos != nNewPos && nNewPos > 0 && nOldPos > 0 )
{ {
ColChange aChg( (sal_uInt16)nOldPos, (sal_uInt16)nNewPos ); ColChange aChg( (sal_uInt16)nOldPos, (sal_uInt16)nNewPos );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment