From 940b4d1848e8c70ab7642901a68594e8016caffc Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 18:51:28 +0200 Subject: Adding upstream version 1:7.0.4. Signed-off-by: Daniel Baumann --- sw/source/core/txtnode/SwGrammarContact.cxx | 190 + sw/source/core/txtnode/atrfld.cxx | 732 ++++ sw/source/core/txtnode/atrflyin.cxx | 297 ++ sw/source/core/txtnode/atrftn.cxx | 576 +++ sw/source/core/txtnode/atrref.cxx | 109 + sw/source/core/txtnode/atrtox.cxx | 90 + sw/source/core/txtnode/chrfmt.cxx | 42 + sw/source/core/txtnode/fmtatr2.cxx | 843 ++++ sw/source/core/txtnode/fntcache.cxx | 2712 +++++++++++++ sw/source/core/txtnode/fntcap.cxx | 778 ++++ sw/source/core/txtnode/modeltoviewhelper.cxx | 337 ++ sw/source/core/txtnode/ndhints.cxx | 475 +++ sw/source/core/txtnode/ndtxt.cxx | 5326 ++++++++++++++++++++++++++ sw/source/core/txtnode/swfntcch.cxx | 75 + sw/source/core/txtnode/swfont.cxx | 1541 ++++++++ sw/source/core/txtnode/thints.cxx | 3512 +++++++++++++++++ sw/source/core/txtnode/txatbase.cxx | 163 + sw/source/core/txtnode/txatritr.cxx | 218 ++ sw/source/core/txtnode/txtatr2.cxx | 314 ++ sw/source/core/txtnode/txtedt.cxx | 2288 +++++++++++ 20 files changed, 20618 insertions(+) create mode 100644 sw/source/core/txtnode/SwGrammarContact.cxx create mode 100644 sw/source/core/txtnode/atrfld.cxx create mode 100644 sw/source/core/txtnode/atrflyin.cxx create mode 100644 sw/source/core/txtnode/atrftn.cxx create mode 100644 sw/source/core/txtnode/atrref.cxx create mode 100644 sw/source/core/txtnode/atrtox.cxx create mode 100644 sw/source/core/txtnode/chrfmt.cxx create mode 100644 sw/source/core/txtnode/fmtatr2.cxx create mode 100644 sw/source/core/txtnode/fntcache.cxx create mode 100644 sw/source/core/txtnode/fntcap.cxx create mode 100644 sw/source/core/txtnode/modeltoviewhelper.cxx create mode 100644 sw/source/core/txtnode/ndhints.cxx create mode 100644 sw/source/core/txtnode/ndtxt.cxx create mode 100644 sw/source/core/txtnode/swfntcch.cxx create mode 100644 sw/source/core/txtnode/swfont.cxx create mode 100644 sw/source/core/txtnode/thints.cxx create mode 100644 sw/source/core/txtnode/txatbase.cxx create mode 100644 sw/source/core/txtnode/txatritr.cxx create mode 100644 sw/source/core/txtnode/txtatr2.cxx create mode 100644 sw/source/core/txtnode/txtedt.cxx (limited to 'sw/source/core/txtnode') diff --git a/sw/source/core/txtnode/SwGrammarContact.cxx b/sw/source/core/txtnode/SwGrammarContact.cxx new file mode 100644 index 000000000..d957bfbcc --- /dev/null +++ b/sw/source/core/txtnode/SwGrammarContact.cxx @@ -0,0 +1,190 @@ +/* -*- 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 +#include +#include +#include +#include +#include +#include + +namespace { + +/* + * This class is responsible for the delayed display of grammar checks when a paragraph is edited + * It's a client of the paragraph the cursor points to. + * If the cursor position changes, updateCursorPosition has to be called + * If the grammar checker wants to set a grammar marker at a paragraph, he has to request + * the grammar list from this class. If the requested paragraph is not edited, it returns + * the normal grammar list. But if the paragraph is the active one, a proxy list will be returned and + * all changes are set in this proxy list. If the cursor leaves the paragraph the proxy list + * will replace the old list. If the grammar checker has completed the paragraph ('setChecked') + * then a timer is setup which replaces the old list as well. + */ +class SwGrammarContact : public IGrammarContact, public SwClient +{ + Timer aTimer; + std::unique_ptr mpProxyList; + bool mbFinished; + SwTextNode* getMyTextNode() { return static_cast(GetRegisteredIn()); } + DECL_LINK( TimerRepaint, Timer *, void ); + +public: + SwGrammarContact(); + virtual ~SwGrammarContact() override { aTimer.Stop(); } + + // (pure) virtual functions of IGrammarContact + virtual void updateCursorPosition( const SwPosition& rNewPos ) override; + virtual SwGrammarMarkUp* getGrammarCheck( SwTextNode& rTextNode, bool bCreate ) override; + virtual void finishGrammarCheck( SwTextNode& rTextNode ) override; +protected: + // virtual function of SwClient + virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew) override; +}; + +} + +SwGrammarContact::SwGrammarContact() : mbFinished( false ) +{ + aTimer.SetTimeout( 2000 ); // Repaint of grammar check after 'setChecked' + aTimer.SetInvokeHandler( LINK(this, SwGrammarContact, TimerRepaint) ); + aTimer.SetDebugName( "sw::SwGrammarContact TimerRepaint" ); +} + +IMPL_LINK( SwGrammarContact, TimerRepaint, Timer *, pTimer, void ) +{ + if( pTimer ) + { + pTimer->Stop(); + if( GetRegisteredIn() ) + { //Replace the old wrong list by the proxy list and repaint all frames + getMyTextNode()->SetGrammarCheck( mpProxyList.release() ); + SwTextFrame::repaintTextFrames( *getMyTextNode() ); + } + } +} + +/* I'm always a client of the current paragraph */ +void SwGrammarContact::updateCursorPosition( const SwPosition& rNewPos ) +{ + SwTextNode* pTextNode = rNewPos.nNode.GetNode().GetTextNode(); + if( pTextNode != GetRegisteredIn() ) // paragraph has been changed + { + aTimer.Stop(); + if( GetRegisteredIn() ) // My last paragraph has been left + { + if( mpProxyList ) + { // replace old list by the proxy list and repaint + getMyTextNode()->SetGrammarCheck( mpProxyList.release() ); + SwTextFrame::repaintTextFrames( *getMyTextNode() ); + } + EndListeningAll(); + } + if( pTextNode ) + pTextNode->Add( this ); // welcome new paragraph + } +} + +/* deliver a grammar check list for the given text node */ +SwGrammarMarkUp* SwGrammarContact::getGrammarCheck( SwTextNode& rTextNode, bool bCreate ) +{ + SwGrammarMarkUp *pRet = nullptr; + if( GetRegisteredIn() == &rTextNode ) // hey, that's my current paragraph! + { // so you will get a proxy list... + if( bCreate ) + { + if( mbFinished ) + { + mpProxyList.reset(); + } + if( !mpProxyList ) + { + if( rTextNode.GetGrammarCheck() ) + mpProxyList.reset( static_cast(rTextNode.GetGrammarCheck()->Clone()) ); + else + { + mpProxyList.reset( new SwGrammarMarkUp() ); + mpProxyList->SetInvalid( 0, COMPLETE_STRING ); + } + } + mbFinished = false; + } + pRet = mpProxyList.get(); + } + else + { + pRet = rTextNode.GetGrammarCheck(); // do you have already a list? + if( bCreate && !pRet ) // do you want to create a list? + { + pRet = new SwGrammarMarkUp(); + pRet->SetInvalid( 0, COMPLETE_STRING ); + rTextNode.SetGrammarCheck( pRet ); + rTextNode.SetGrammarCheckDirty( true ); + } + } + return pRet; +} + +void SwGrammarContact::Modify( const SfxPoolItem* pOld, const SfxPoolItem * ) +{ + if( !pOld || pOld->Which() != RES_OBJECTDYING ) + return; + + const SwPtrMsgPoolItem *pDead = static_cast(pOld); + if( pDead->pObject == GetRegisteredIn() ) + { // if my current paragraph dies, I throw the proxy list away + aTimer.Stop(); + EndListeningAll(); + mpProxyList.reset(); + } +} + +void SwGrammarContact::finishGrammarCheck( SwTextNode& rTextNode ) +{ + if( &rTextNode != GetRegisteredIn() ) // not my paragraph + SwTextFrame::repaintTextFrames( rTextNode ); // can be repainted directly + else + { + if( mpProxyList ) + { + mbFinished = true; + aTimer.Start(); // will replace old list and repaint with delay + } + else if( getMyTextNode()->GetGrammarCheck() ) + { // all grammar problems seems to be gone, no delay needed + getMyTextNode()->SetGrammarCheck( nullptr ); + SwTextFrame::repaintTextFrames( *getMyTextNode() ); + } + } +} + +IGrammarContact* createGrammarContact() +{ + return new SwGrammarContact(); +} + +void finishGrammarCheck( SwTextNode& rTextNode ) +{ + IGrammarContact* pGrammarContact = getGrammarContact( rTextNode ); + if( pGrammarContact ) + pGrammarContact->finishGrammarCheck( rTextNode ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/atrfld.cxx b/sw/source/core/txtnode/atrfld.cxx new file mode 100644 index 000000000..b8027bcd5 --- /dev/null +++ b/sw/source/core/txtnode/atrfld.cxx @@ -0,0 +1,732 @@ +/* -*- 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 + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +// constructor for default item in attribute-pool +SwFormatField::SwFormatField( sal_uInt16 nWhich ) + : SfxPoolItem( nWhich ) + , SfxBroadcaster() + , mpTextField( nullptr ) +{ +} + +SwFormatField::SwFormatField( const SwField &rField ) + : SfxPoolItem( RES_TXTATR_FIELD ) + , SfxBroadcaster() + , mpField( rField.CopyField() ) + , mpTextField( nullptr ) +{ + rField.GetTyp()->Add(this); + if ( mpField->GetTyp()->Which() == SwFieldIds::Input ) + { + // input field in-place editing + SetWhich( RES_TXTATR_INPUTFIELD ); + static_cast(mpField.get())->SetFormatField( *this ); + } + else if (mpField->GetTyp()->Which() == SwFieldIds::SetExp) + { + // see SwWrtShell::StartInputFieldDlg + SwSetExpField *const pSetField(static_cast(mpField.get())); + if (pSetField->GetInputFlag() + // only for string fields for now - inline editing of number fields + // tends to produce error messages... + && (static_cast(pSetField->GetTyp())->GetType() + & nsSwGetSetExpType::GSE_STRING)) + { + SetWhich( RES_TXTATR_INPUTFIELD ); + } + pSetField->SetFormatField(*this); + } + else if ( mpField->GetTyp()->Which() == SwFieldIds::Postit ) + { + // text annotation field + SetWhich( RES_TXTATR_ANNOTATION ); + } +} + +// #i24434# +// Since Items are used in ItemPool and in default constructed ItemSets with +// full pool range, all items need to be clonable. Thus, this one needed to be +// corrected +SwFormatField::SwFormatField( const SwFormatField& rAttr ) + : SfxPoolItem( rAttr ) + , SfxBroadcaster() + , mpTextField( nullptr ) +{ + if ( rAttr.mpField ) + { + rAttr.mpField->GetTyp()->Add(this); + mpField = rAttr.mpField->CopyField(); + if ( mpField->GetTyp()->Which() == SwFieldIds::Input ) + { + // input field in-place editing + SetWhich( RES_TXTATR_INPUTFIELD ); + SwInputField *pField = dynamic_cast(mpField.get()); + assert(pField); + if (pField) + pField->SetFormatField( *this ); + } + else if (mpField->GetTyp()->Which() == SwFieldIds::SetExp) + { + SwSetExpField *const pSetField(static_cast(mpField.get())); + if (pSetField->GetInputFlag() + && (static_cast(pSetField->GetTyp())->GetType() + & nsSwGetSetExpType::GSE_STRING)) + { + SetWhich( RES_TXTATR_INPUTFIELD ); + } + // see SwWrtShell::StartInputFieldDlg + pSetField->SetFormatField(*this); + } + else if ( mpField->GetTyp()->Which() == SwFieldIds::Postit ) + { + // text annotation field + SetWhich( RES_TXTATR_ANNOTATION ); + } + } +} + +SwFormatField::~SwFormatField() +{ + SwFieldType* pType = mpField ? mpField->GetTyp() : nullptr; + + if (pType && pType->Which() == SwFieldIds::Database) + pType = nullptr; // DB field types destroy themselves + + Broadcast( SwFormatFieldHint( this, SwFormatFieldHintWhich::REMOVED ) ); + mpField.reset(); + + // some fields need to delete their field type + if( pType && pType->HasOnlyOneListener() ) + { + bool bDel = false; + switch( pType->Which() ) + { + case SwFieldIds::User: + bDel = static_cast(pType)->IsDeleted(); + break; + + case SwFieldIds::SetExp: + bDel = static_cast(pType)->IsDeleted(); + break; + + case SwFieldIds::Dde: + bDel = static_cast(pType)->IsDeleted(); + break; + default: break; + } + + if( bDel ) + { + // unregister before deleting + pType->Remove( this ); + delete pType; + } + } +} + +void SwFormatField::RegisterToFieldType( SwFieldType& rType ) +{ + rType.Add(this); +} + +void SwFormatField::SetField(std::unique_ptr _pField) +{ + mpField = std::move(_pField); + if ( mpField->GetTyp()->Which() == SwFieldIds::Input ) + { + static_cast(mpField.get())->SetFormatField( *this ); + } + else if (mpField->GetTyp()->Which() == SwFieldIds::SetExp) + { + // see SwWrtShell::StartInputFieldDlg + static_cast(mpField.get())->SetFormatField(*this); + } + Broadcast( SwFormatFieldHint( this, SwFormatFieldHintWhich::CHANGED ) ); +} + +void SwFormatField::SetTextField( SwTextField& rTextField ) +{ + mpTextField = &rTextField; +} + +void SwFormatField::ClearTextField() +{ + mpTextField = nullptr; +} + +bool SwFormatField::operator==( const SfxPoolItem& rAttr ) const +{ + assert(SfxPoolItem::operator==(rAttr)); + return ( mpField + && static_cast(rAttr).mpField + && mpField->GetTyp() == static_cast(rAttr).mpField->GetTyp() + && mpField->GetFormat() == static_cast(rAttr).mpField->GetFormat() ) + || + ( !mpField && !static_cast(rAttr).mpField ); +} + +SwFormatField* SwFormatField::Clone( SfxItemPool* ) const +{ + return new SwFormatField( *this ); +} + +void SwFormatField::InvalidateField() +{ + SwPtrMsgPoolItem const item(RES_REMOVE_UNO_OBJECT, + &static_cast(*this)); // cast to base class (void*) + CallSwClientNotify(sw::LegacyModifyHint{ &item, &item }); +} + +void SwFormatField::SwClientNotify( const SwModify& rModify, const SfxHint& rHint ) +{ + SwClient::SwClientNotify(rModify, rHint); + if (const auto pFieldHint = dynamic_cast( &rHint )) + { + if( !mpTextField ) + return; + + // replace field content by text + SwPaM* pPaM = pFieldHint->m_pPaM; + SwDoc* pDoc = pPaM->GetDoc(); + const SwTextNode& rTextNode = mpTextField->GetTextNode(); + pPaM->GetPoint()->nNode = rTextNode; + pPaM->GetPoint()->nContent.Assign( const_cast(&rTextNode), mpTextField->GetStart() ); + + OUString const aEntry(mpField->ExpandField(pDoc->IsClipBoard(), pFieldHint->m_pLayout)); + pPaM->SetMark(); + pPaM->Move( fnMoveForward ); + pDoc->getIDocumentContentOperations().DeleteRange( *pPaM ); + pDoc->getIDocumentContentOperations().InsertString( *pPaM, aEntry ); + } else if (const auto pLegacyHint = dynamic_cast( &rHint )) + { + if( !mpTextField ) + return; + UpdateTextNode(pLegacyHint->m_pOld, pLegacyHint->m_pNew); + } else if (const auto pFindForFieldHint = dynamic_cast( &rHint )) + { + if(pFindForFieldHint->m_rpFormat == nullptr && pFindForFieldHint->m_pField == GetField()) + pFindForFieldHint->m_rpFormat = this; + } else if (const auto pFindForPostItIdHint = dynamic_cast( &rHint )) + { + auto pPostItField = dynamic_cast(mpField.get()); + if(pPostItField && pFindForPostItIdHint->m_rpFormat == nullptr && pFindForPostItIdHint->m_nPostItId == pPostItField->GetPostItId()) + pFindForPostItIdHint->m_rpFormat = this; + } else if (const auto pCollectPostItsHint = dynamic_cast( &rHint )) + { + if(GetTextField() && IsFieldInDoc() && (!pCollectPostItsHint->m_bHideRedlines || !sw::IsFieldDeletedInModel(pCollectPostItsHint->m_rIDRA, *GetTextField()))) + pCollectPostItsHint->m_rvFormatFields.push_back(this); + } else if (const auto pHasHiddenInfoHint = dynamic_cast( &rHint )) + { + if(!pHasHiddenInfoHint->m_rbHasHiddenInformationNotes && GetTextField() && IsFieldInDoc()) + pHasHiddenInfoHint->m_rbHasHiddenInformationNotes = true; + } else if (const auto pGatherNodeIndexHint = dynamic_cast( &rHint )) + { + if(auto pTextField = GetTextField()) + pGatherNodeIndexHint->m_rvNodeIndex.push_back(pTextField->GetTextNode().GetIndex()); + } else if (const auto pGatherRefFieldsHint = dynamic_cast( &rHint )) + { + if(!GetTextField() || pGatherRefFieldsHint->m_nType != GetField()->GetSubType()) + return; + SwTextNode* pNd = GetTextField()->GetpTextNode(); + if(pNd && pNd->GetNodes().IsDocNodes()) + pGatherRefFieldsHint->m_rvRFields.push_back(static_cast(GetField())); + } else if (const auto pGatherFieldsHint = dynamic_cast( &rHint )) + { + if(pGatherFieldsHint->m_bCollectOnlyInDocNodes) + { + if(!GetTextField()) + return; + SwTextNode* pNd = GetTextField()->GetpTextNode(); + if(!pNd || !pNd->GetNodes().IsDocNodes()) + return; + } + pGatherFieldsHint->m_rvFields.push_back(this); + } +} + +void SwFormatField::UpdateTextNode(const SfxPoolItem* pOld, const SfxPoolItem* pNew) +{ + if (pOld && (RES_REMOVE_UNO_OBJECT == pOld->Which())) + { // invalidate cached UNO object + m_wXTextField = nullptr; + // ??? why does this Modify method not already do this? + NotifyClients(pOld, pNew); + return; + } + + if( !mpTextField ) + return; + + // don't do anything, especially not expand! + if( pNew && pNew->Which() == RES_OBJECTDYING ) + return; + + SwTextNode* pTextNd = &mpTextField->GetTextNode(); + OSL_ENSURE( pTextNd, "Where is my Node?" ); + + bool bTriggerNode = false; + bool bExpand = false; + const SfxPoolItem* pNodeOld = nullptr; + const SfxPoolItem* pNodeNew = nullptr; + if(pNew) + { + switch(pNew->Which()) + { + case RES_REFMARKFLD_UPDATE: + // update GetRef fields + if( SwFieldIds::GetRef == mpField->GetTyp()->Which() ) + { + // #i81002# + static_cast(mpField.get())->UpdateField( mpTextField ); + } + break; + case RES_DOCPOS_UPDATE: + // handled in SwTextFrame::Modify() + bTriggerNode = true; + pNodeOld = pNew; + pNodeNew = this; + break; + case RES_ATTRSET_CHG: + case RES_FMT_CHG: + bTriggerNode = true; + pNodeOld = pOld; + pNodeNew = pNew; + break; + default: + break; + } + } + if(!bTriggerNode) + { + switch (mpField->GetTyp()->Which()) + { + case SwFieldIds::HiddenPara: + if( !pOld || pOld->Which() != RES_HIDDENPARA_PRINT ) { + bExpand =true; + break; + } + [[fallthrough]]; + case SwFieldIds::DbSetNumber: + case SwFieldIds::DbNumSet: + case SwFieldIds::DbNextSet: + case SwFieldIds::DatabaseName: + bTriggerNode = true; + pNodeNew = pNew; + break; + case SwFieldIds::User: + { + SwUserFieldType* pType = static_cast(mpField->GetTyp()); + if(!pType->IsValid()) + { + SwCalc aCalc( *pTextNd->GetDoc() ); + pType->GetValue( aCalc ); + } + bExpand = true; + } + break; + default: + bExpand = true; + break; + } + } + if(bTriggerNode) + { + pTextNd->ModifyNotification(pNodeOld, pNodeNew); + } + if(bExpand) + { + mpTextField->ExpandTextField( pOld == nullptr && pNew == nullptr ); + } +} + +bool SwFormatField::GetInfo( SfxPoolItem& rInfo ) const +{ + const SwTextNode* pTextNd; + return RES_AUTOFMT_DOCNODE != rInfo.Which() || + !mpTextField || nullptr == ( pTextNd = mpTextField->GetpTextNode() ) || + &pTextNd->GetNodes() != static_cast(rInfo).pNodes; +} + +bool SwFormatField::IsFieldInDoc() const +{ + return mpTextField != nullptr + && mpTextField->IsFieldInDoc(); +} + +bool SwFormatField::IsProtect() const +{ + return mpTextField != nullptr + && mpTextField->GetpTextNode() != nullptr + && mpTextField->GetpTextNode()->IsProtect(); +} + +void SwFormatField::dumpAsXml(xmlTextWriterPtr pWriter) const +{ + xmlTextWriterStartElement(pWriter, BAD_CAST("SwFormatField")); + xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this); + xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("mpTextField"), "%p", mpTextField); + + SfxPoolItem::dumpAsXml(pWriter); + mpField->dumpAsXml(pWriter); + + xmlTextWriterEndElement(pWriter); +} + +// class SwTextField //////////////////////////////////////////////////// + +SwTextField::SwTextField( + SwFormatField & rAttr, + sal_Int32 const nStartPos, + bool const bInClipboard) + : SwTextAttr( rAttr, nStartPos ) +// fdo#39694 the ExpandField here may not give the correct result in all cases, +// but is better than nothing + , m_aExpand( rAttr.GetField()->ExpandField(bInClipboard, nullptr) ) + , m_pTextNode( nullptr ) +{ + rAttr.SetTextField( *this ); + SetHasDummyChar(true); +} + +SwTextField::~SwTextField( ) +{ + SwFormatField & rFormatField( static_cast(GetAttr()) ); + if ( this == rFormatField.GetTextField() ) + { + rFormatField.ClearTextField(); + } +} + +bool SwTextField::IsFieldInDoc() const +{ + return GetpTextNode() != nullptr + && GetpTextNode()->GetNodes().IsDocNodes(); +} + +void SwTextField::ExpandTextField(const bool bForceNotify) const +{ + OSL_ENSURE( m_pTextNode, "SwTextField: where is my TextNode?" ); + + const SwField* pField = GetFormatField().GetField(); + const OUString aNewExpand( pField->ExpandField(m_pTextNode->GetDoc()->IsClipBoard(), + // can't do any better than this here... + m_pTextNode->GetDoc()->getIDocumentLayoutAccess().GetCurrentLayout()) ); + + const SwFieldIds nWhich = pField->GetTyp()->Which(); + const bool bSameExpandSimpleNotification + = SwFieldIds::Chapter != nWhich && SwFieldIds::PageNumber != nWhich + && SwFieldIds::RefPageGet != nWhich + // Page count fields to not use aExpand during formatting, + // therefore an invalidation of the text frame has to be triggered even if aNewExpand == aExpand: + && (SwFieldIds::DocStat != nWhich + || DS_PAGE != static_cast(pField)->GetSubType()) + && (SwFieldIds::GetExp != nWhich + || static_cast(pField)->IsInBodyText()); + + bool bHiddenParaChanged = false; + if (aNewExpand != m_aExpand || bSameExpandSimpleNotification) + bHiddenParaChanged = m_pTextNode->CalcHiddenParaField(); + + if (aNewExpand == m_aExpand) + { + if ( bSameExpandSimpleNotification ) + { + if( bHiddenParaChanged ) + { + m_pTextNode->ModifyNotification( nullptr, nullptr ); + } + if ( !bForceNotify ) + { + // done, if no further notification forced. + return; + } + } + } + else + m_aExpand = aNewExpand; + + const_cast(this)->NotifyContentChange( const_cast(GetFormatField()) ); +} + +void SwTextField::CopyTextField( SwTextField *pDest ) const +{ + OSL_ENSURE( m_pTextNode, "SwTextField: where is my TextNode?" ); + OSL_ENSURE( pDest->m_pTextNode, "SwTextField: where is pDest's TextNode?" ); + + IDocumentFieldsAccess* pIDFA = &m_pTextNode->getIDocumentFieldsAccess(); + IDocumentFieldsAccess* pDestIDFA = &pDest->m_pTextNode->getIDocumentFieldsAccess(); + + SwFormatField& rDestFormatField = const_cast(pDest->GetFormatField()); + const SwFieldIds nFieldWhich = rDestFormatField.GetField()->GetTyp()->Which(); + + if( pIDFA != pDestIDFA ) + { + // different documents, e.g. clipboard: + // register field type in target document + SwFieldType* pFieldType; + if( nFieldWhich != SwFieldIds::Database + && nFieldWhich != SwFieldIds::User + && nFieldWhich != SwFieldIds::SetExp + && nFieldWhich != SwFieldIds::Dde + && SwFieldIds::TableOfAuthorities != nFieldWhich ) + { + pFieldType = pDestIDFA->GetSysFieldType( nFieldWhich ); + } + else + { + pFieldType = pDestIDFA->InsertFieldType( *rDestFormatField.GetField()->GetTyp() ); + } + + // DDE fields need special treatment + if( SwFieldIds::Dde == nFieldWhich ) + { + if( rDestFormatField.GetTextField() ) + { + static_cast(rDestFormatField.GetField()->GetTyp())->DecRefCnt(); + } + static_cast(pFieldType)->IncRefCnt(); + } + + OSL_ENSURE( pFieldType, "unknown FieldType" ); + pFieldType->Add( &rDestFormatField ); // register at the field type + rDestFormatField.GetField()->ChgTyp( pFieldType ); + } + + // update expression fields + if( nFieldWhich == SwFieldIds::SetExp + || nFieldWhich == SwFieldIds::GetExp + || nFieldWhich == SwFieldIds::HiddenText ) + { + SwTextField* pField = const_cast(this); + pDestIDFA->UpdateExpFields( pField, true ); + } + // table fields: external display + else if( SwFieldIds::Table == nFieldWhich + && static_cast(rDestFormatField.GetField())->IsIntrnlName() ) + { + // convert internal (core) to external (UI) formula + const SwTableNode* pTableNd = m_pTextNode->FindTableNode(); + if( pTableNd ) // in a table? + static_cast(rDestFormatField.GetField())->PtrToBoxNm( &pTableNd->GetTable() ); + } +} + +void SwTextField::NotifyContentChange(SwFormatField& rFormatField) +{ + //if not in undo section notify the change + if (m_pTextNode && m_pTextNode->GetNodes().IsDocNodes()) + { + m_pTextNode->ModifyNotification(nullptr, &rFormatField); + } +} + +/*static*/ +void SwTextField::GetPamForTextField( + const SwTextField& rTextField, + std::shared_ptr< SwPaM >& rPamForTextField ) +{ + if (rTextField.GetpTextNode() == nullptr) + { + SAL_WARN("sw.core", " - missing "); + return; + } + + const SwTextNode& rTextNode = rTextField.GetTextNode(); + + rPamForTextField = std::make_shared( rTextNode, + (rTextField.End() != nullptr) ? *(rTextField.End()) : ( rTextField.GetStart() + 1 ), + rTextNode, + rTextField.GetStart() ); + +} + +/*static*/ +void SwTextField::DeleteTextField( const SwTextField& rTextField ) +{ + if (rTextField.GetpTextNode() != nullptr) + { + std::shared_ptr< SwPaM > pPamForTextField; + GetPamForTextField(rTextField, pPamForTextField); + if (pPamForTextField != nullptr) + { + rTextField.GetTextNode().GetDoc()->getIDocumentContentOperations().DeleteAndJoin(*pPamForTextField); + } + } +} + +// class SwTextInputField /////////////////////////////////////////////// + +// input field in-place editing +SwTextInputField::SwTextInputField( + SwFormatField & rAttr, + sal_Int32 const nStart, + sal_Int32 const nEnd, + bool const bInClipboard ) + + : SwTextAttr( rAttr, nStart ) + , SwTextAttrNesting( rAttr, nStart, nEnd ) + , SwTextField( rAttr, nStart, bInClipboard ) + , m_bLockNotifyContentChange( false ) +{ + SetHasDummyChar( false ); + SetHasContent( true ); +} + +SwTextInputField::~SwTextInputField() +{ +} + +bool SwTextInputField::LockNotifyContentChange() +{ + if (m_bLockNotifyContentChange) + { + return false; + } + m_bLockNotifyContentChange = true; + return true; +} + +void SwTextInputField::UnlockNotifyContentChange() +{ + m_bLockNotifyContentChange = false; +} + +void SwTextInputField::NotifyContentChange( SwFormatField& rFormatField ) +{ + if ( !m_bLockNotifyContentChange ) + { + LockNotifyContentChange(); + + SwTextField::NotifyContentChange( rFormatField ); + UpdateTextNodeContent( GetFieldContent() ); + + UnlockNotifyContentChange(); + } +} + +OUString SwTextInputField::GetFieldContent() const +{ + return GetFormatField().GetField()->ExpandField(false, nullptr/*ignored anyway*/); +} + +void SwTextInputField::UpdateFieldContent() +{ + if ( IsFieldInDoc() + && GetStart() != (*End()) ) + { + assert( (*End()) - GetStart() >= 2 && + " - Are CH_TXT_ATR_INPUTFIELDSTART and/or CH_TXT_ATR_INPUTFIELDEND missing?" ); + // skip CH_TXT_ATR_INPUTFIELDSTART character + const sal_Int32 nIdx = GetStart() + 1; + // skip CH_TXT_ATR_INPUTFIELDEND character + const sal_Int32 nLen = static_cast(std::max( 0, ( (*End()) - 1 - nIdx ) )); + const OUString aNewFieldContent = GetTextNode().GetExpandText(nullptr, nIdx, nLen); + + const SwField* pField = GetFormatField().GetField(); + const SwInputField* pInputField = dynamic_cast(pField); + if (pInputField) + const_cast(pInputField)->applyFieldContent( aNewFieldContent ); + + const SwSetExpField* pExpField = dynamic_cast(pField); + if (pExpField) + { + assert(pExpField->GetInputFlag()); + const_cast(pExpField)->SetPar2(aNewFieldContent); + } + assert(pInputField || pExpField); + + // trigger update of fields for scenarios in which the Input Field's content is part of e.g. a table formula + GetTextNode().GetDoc()->getIDocumentFieldsAccess().GetUpdateFields().SetFieldsDirty(true); + } +} + +void SwTextInputField::UpdateTextNodeContent( const OUString& rNewContent ) +{ + assert(IsFieldInDoc() && + " - misusage as Input Field is not in document content."); + + assert( (*End()) - GetStart() >= 2 && + " - Are CH_TXT_ATR_INPUTFIELDSTART and/or CH_TXT_ATR_INPUTFIELDEND missing?" ); + // skip CH_TXT_ATR_INPUTFIELDSTART character + const sal_Int32 nIdx = GetStart() + 1; + // skip CH_TXT_ATR_INPUTFIELDEND character + const sal_Int32 nDelLen = std::max( 0, ( (*End()) - 1 - nIdx ) ); + SwIndex aIdx( &GetTextNode(), nIdx ); + GetTextNode().ReplaceText( aIdx, nDelLen, rNewContent ); +} + +// class SwTextAnnotationField ////////////////////////////////////////// + +// text annotation field +SwTextAnnotationField::SwTextAnnotationField( + SwFormatField & rAttr, + sal_Int32 const nStart, + bool const bInClipboard ) + : SwTextAttr( rAttr, nStart ) + , SwTextField( rAttr, nStart, bInClipboard ) +{ +} + +SwTextAnnotationField::~SwTextAnnotationField() +{ +} + +::sw::mark::IMark* SwTextAnnotationField::GetAnnotationMark() const +{ + auto pPostItField = dynamic_cast(GetFormatField().GetField()); + assert(pPostItField); + + SwDoc* pDoc = static_cast(pPostItField->GetTyp())->GetDoc(); + assert(pDoc != nullptr); + + IDocumentMarkAccess* pMarksAccess = pDoc->getIDocumentMarkAccess(); + IDocumentMarkAccess::const_iterator_t pMark = pMarksAccess->findAnnotationMark( pPostItField->GetName() ); + return pMark != pMarksAccess->getAnnotationMarksEnd() + ? *pMark + : nullptr; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/atrflyin.cxx b/sw/source/core/txtnode/atrflyin.cxx new file mode 100644 index 000000000..4e61e568e --- /dev/null +++ b/sw/source/core/txtnode/atrflyin.cxx @@ -0,0 +1,297 @@ +/* -*- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +SwFormatFlyCnt::SwFormatFlyCnt( SwFrameFormat *pFrameFormat ) + : SfxPoolItem( RES_TXTATR_FLYCNT ), + m_pTextAttr( nullptr ), + m_pFormat( pFrameFormat ) +{ +} + +bool SwFormatFlyCnt::operator==( const SfxPoolItem& rAttr ) const +{ + assert(SfxPoolItem::operator==(rAttr)); + return( m_pTextAttr && static_cast(rAttr).m_pTextAttr && + m_pTextAttr->GetStart() == static_cast(rAttr).m_pTextAttr->GetStart() && + m_pFormat == static_cast(rAttr).GetFrameFormat() ); +} + +SwFormatFlyCnt* SwFormatFlyCnt::Clone( SfxItemPool* ) const +{ + return new SwFormatFlyCnt( m_pFormat ); +} + +SwTextFlyCnt::SwTextFlyCnt( SwFormatFlyCnt& rAttr, sal_Int32 nStartPos ) + : SwTextAttr( rAttr, nStartPos ) +{ + rAttr.m_pTextAttr = this; + SetHasDummyChar(true); +} + +/** An overview of how a new SwTextFlyCnt is created: + * MakeTextAttr() is called e.g. by SwTextNode::CopyText(). + * The following steps are required to clone: + * 1) copying the pFormat with content, attributes etc. + * 2) setting the anchor + * 3) notification + * Because not all required information is available at all times, + * the steps are distributed variously: + * ad 1) MakeTextAttr() calls DocumentLayoutManager::CopyLayoutFormat() + * which creates the new SwFlyFrameFormat and copies the content of the + * fly frame. + * ad 2) SetAnchor() is called by SwTextNode::InsertHint() and sets the anchor + * position in the SwFlyFrameFormat to the SwPosition of the dummy + * CH_TXTATR_BREAKWORD. This cannot be done in MakeTextAttr() because it + * doesn't know the target text node. + * ad 3) GetFlyFrame_() is called during text formatting by SwTextFormatter + * and searches for the SwFlyFrame for the dummy char of the current + * SwTextFrame. If none is found, a new SwFlyInContentFrame is created. + * Important: pTextFrame->AppendFly() immediately triggers a reformat + * of pTextFrame. However, the recursion is blocked by the lock mechanism + * in SwTextFrame::Format(). + * The advantage of all this is that it's not necessary to explicitly iterate + * over all SwTextFrames that depend on the SwTextNode to create the + * SwFlyInContentFrame - this is done automatically already. + */ + +void SwTextFlyCnt::CopyFlyFormat( SwDoc* pDoc ) +{ + SwFrameFormat* pFormat = GetFlyCnt().GetFrameFormat(); + assert(pFormat); + // The FlyFrameFormat must be copied - CopyLayoutFormat + // (DocumentLayoutManager.cxx) creates the FlyFrameFormat and copies the + // content. + + // disable undo while copying attribute + ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo()); + SwFormatAnchor aAnchor( pFormat->GetAnchor() ); + if ((RndStdIds::FLY_AT_PAGE != aAnchor.GetAnchorId()) && + (pDoc != pFormat->GetDoc())) // different documents? + { + // JP 03.06.96: ensure that the copied anchor points to valid content! + // setting it to the correct position is done later. + SwNodeIndex aIdx( pDoc->GetNodes().GetEndOfExtras(), +2 ); + SwContentNode* pCNd = aIdx.GetNode().GetContentNode(); + if( !pCNd ) + pCNd = pDoc->GetNodes().GoNext( &aIdx ); + + SwPosition pos = *aAnchor.GetContentAnchor(); + pos.nNode = aIdx; + if (RndStdIds::FLY_AS_CHAR == aAnchor.GetAnchorId()) + { + pos.nContent.Assign( pCNd, 0 ); + } + else + { + pos.nContent.Assign( nullptr, 0 ); + assert(false); + } + aAnchor.SetAnchor( &pos ); + } + + SwFrameFormat* pNew = pDoc->getIDocumentLayoutAccess().CopyLayoutFormat( *pFormat, aAnchor, false, false ); + const_cast(GetFlyCnt()).SetFlyFormat( pNew ); +} + +/** SetAnchor() is called by SwTextNode::InsertHint() and sets the anchor + * position in the SwFlyFrameFormat to the SwPosition of the dummy + * CH_TXTATR_BREAKWORD. This cannot be done in MakeTextAttr() because it + * doesn't know the target text node. + */ +void SwTextFlyCnt::SetAnchor( const SwTextNode *pNode ) +{ + // for Undo, the new anchor must be known already! + + SwDoc* pDoc = const_cast(pNode->GetDoc()); + + SwIndex aIdx( const_cast(pNode), GetStart() ); + SwPosition aPos( *pNode->StartOfSectionNode(), aIdx ); + SwFrameFormat* pFormat = GetFlyCnt().GetFrameFormat(); + SwFormatAnchor aAnchor( pFormat->GetAnchor() ); + SwNode *const pOldNode(aAnchor.GetContentAnchor() + ? &aAnchor.GetContentAnchor()->nNode.GetNode() + : nullptr); + + if (!pOldNode || !pOldNode->GetNodes().IsDocNodes() || + pOldNode != static_cast(pNode)) + { + aPos.nNode = *pNode; + } + else + { + aPos.nNode = *pOldNode; + } + + aAnchor.SetType( RndStdIds::FLY_AS_CHAR ); // default! + aAnchor.SetAnchor( &aPos ); + + // in case of anchor change, delete all FlyFrames + // JP 25.04.95: if the Frames can be moved within SplitNode, they don't + // need to be deleted + if( ( !pNode->GetpSwpHints() || !pNode->GetpSwpHints()->IsInSplitNode() ) + && RES_DRAWFRMFMT != pFormat->Which() ) + pFormat->DelFrames(); + + // copy into a different document? + if( pDoc != pFormat->GetDoc() ) + { + // disable undo while copying attribute + ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo()); + SwFrameFormat* pNew = pDoc->getIDocumentLayoutAccess().CopyLayoutFormat( *pFormat, aAnchor, false, false ); + + ::sw::UndoGuard const undoGuardFormat( + pFormat->GetDoc()->GetIDocumentUndoRedo()); + pFormat->GetDoc()->getIDocumentLayoutAccess().DelLayoutFormat( pFormat ); + const_cast(GetFlyCnt()).SetFlyFormat( pNew ); + } + else if( pNode->GetpSwpHints() && + pNode->GetpSwpHints()->IsInSplitNode() && + RES_DRAWFRMFMT != pFormat->Which() ) + { + pFormat->LockModify(); + pFormat->SetFormatAttr( aAnchor ); // only set the anchor + // tdf#91228 must notify the anchor nodes despite LockModify + assert(pOldNode); + pOldNode->RemoveAnchoredFly(pFormat); + aPos.nNode.GetNode().AddAnchoredFly(pFormat); + pFormat->UnlockModify(); + } + else + { + assert(!pFormat->IsModifyLocked()); // need to notify anchor node + if (RES_DRAWFRMFMT == pFormat->Which()) + { + if (SdrObject const*const pObj = pFormat->FindSdrObject()) + { // tdf#123259 disconnect with *old* anchor position + static_cast(::GetUserCall(pObj))->DisconnectFromLayout(false); + } + } + pFormat->SetFormatAttr( aAnchor ); // only set the anchor + + // If the draw format has a TextBox, then set its anchor as well. + if (SwFrameFormat* pTextBox + = SwTextBoxHelper::getOtherTextBoxFormat(pFormat, RES_DRAWFRMFMT)) + { + SwFormatAnchor aTextBoxAnchor(pTextBox->GetAnchor()); + aTextBoxAnchor.SetAnchor(aAnchor.GetContentAnchor()); + + // SwFlyAtContentFrame::Modify() assumes the anchor has a matching layout frame, which + // may not be the case when we're in the process of a node split, so block + // notifications. + bool bIsInSplitNode = pNode->GetpSwpHints() && pNode->GetpSwpHints()->IsInSplitNode(); + if (bIsInSplitNode) + { + pTextBox->LockModify(); + } + + pTextBox->SetFormatAttr(aTextBoxAnchor); + + if (bIsInSplitNode) + { + pOldNode->RemoveAnchoredFly(pTextBox); + aPos.nNode.GetNode().AddAnchoredFly(pTextBox); + pTextBox->UnlockModify(); + } + } + } + + // The node may have several SwTextFrames - for every SwTextFrame a + // SwFlyInContentFrame is created. +} + + +/** GetFlyFrame_() is called during text formatting by SwTextFormatter + * and searches for the SwFlyFrame for the dummy char of the current + * SwTextFrame. If none is found, a new SwFlyInContentFrame is created. + */ +SwFlyInContentFrame *SwTextFlyCnt::GetFlyFrame_( const SwFrame *pCurrFrame ) +{ + SwFrameFormat* pFrameFormat = GetFlyCnt().GetFrameFormat(); + if( RES_DRAWFRMFMT == pFrameFormat->Which() ) + { + OSL_ENSURE( false, "SwTextFlyCnt::GetFlyFrame_: DrawInCnt-under construction!" ); + return nullptr; + } + + SwIterator aIter( *GetFlyCnt().m_pFormat ); + assert(pCurrFrame->IsTextFrame()); + SwFrame* pFrame = aIter.First(); + if ( pFrame ) + { + SwTextFrame *pFirst = const_cast(static_cast(pCurrFrame)); + while ( pFirst->IsFollow() ) + pFirst = pFirst->FindMaster(); + do + { + SwTextFrame *pTmp = pFirst; + do + { if( static_cast(pFrame)->GetAnchorFrame() == static_cast(pTmp) ) + { + if ( pTmp != pCurrFrame ) + { + pTmp->RemoveFly( static_cast(pFrame) ); + const_cast(static_cast(pCurrFrame))->AppendFly( static_cast(pFrame) ); + } + return static_cast(pFrame); + } + pTmp = pTmp->GetFollow(); + } while ( pTmp ); + + pFrame = aIter.Next(); + + } while( pFrame ); + } + + // We did not find a matching FlyFrame, so create a new one. + // AppendFly() triggers a reformat of pCurrentFrame. However, the + // recursion is blocked by the lock mechanism in SwTextFrame::Format(). + SwFrame* pCurrentFrame = const_cast(pCurrFrame); + SwFlyInContentFrame *pFly = new SwFlyInContentFrame(static_cast(pFrameFormat), pCurrentFrame, pCurrentFrame); + pCurrentFrame->AppendFly(pFly); + pFly->RegistFlys(); + + // We must ensure that the content of the FlyInCnt is fully formatted + // right after construction. + // #i26945# - Use new object formatter to format Writer + // fly frame and its content. + SwObjectFormatter::FormatObj( *pFly, const_cast(pCurrFrame), + pCurrFrame->FindPageFrame() ); + + return pFly; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/atrftn.cxx b/sw/source/core/txtnode/atrftn.cxx new file mode 100644 index 000000000..fd8f65185 --- /dev/null +++ b/sw/source/core/txtnode/atrftn.cxx @@ -0,0 +1,576 @@ +/* -*- 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 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + /// Get a sorted list of the used footnote reference numbers. + /// @param[in] rDoc The active document. + /// @param[in] pExclude A footnote whose reference number should be excluded from the set. + /// @param[out] rUsedRef The set of used reference numbers. + /// @param[out] rInvalid A returned list of all items that had an invalid reference number. + void lcl_FillUsedFootnoteRefNumbers(SwDoc &rDoc, + SwTextFootnote const *pExclude, + std::set &rUsedRef, + std::vector &rInvalid) + { + SwFootnoteIdxs& ftnIdxs = rDoc.GetFootnoteIdxs(); + + rInvalid.clear(); + + for( size_t n = 0; n < ftnIdxs.size(); ++n ) + { + SwTextFootnote* pTextFootnote = ftnIdxs[ n ]; + if ( pTextFootnote != pExclude ) + { + if ( USHRT_MAX == pTextFootnote->GetSeqRefNo() ) + { + rInvalid.push_back(pTextFootnote); + } + else + { + rUsedRef.insert( pTextFootnote->GetSeqRefNo() ); + } + } + } + } + + /// Check whether a requested reference number is available. + /// @param[in] rUsedNums Set of used reference numbers. + /// @param[in] requested The requested reference number. + /// @returns true if the number is available, false if not. + bool lcl_IsRefNumAvailable(std::set const &rUsedNums, + sal_uInt16 requested) + { + if ( USHRT_MAX == requested ) + return false; // Invalid sequence number. + if ( rUsedNums.count(requested) ) + return false; // Number already used. + return true; + } + + /// Get the first few unused sequential reference numbers. + /// @param[out] rLowestUnusedNums The lowest unused sequential reference numbers. + /// @param[in] rUsedNums The set of used sequential reference numbers. + /// @param[in] numRequired The number of reference number required. + void lcl_FillUnusedSeqRefNums(std::vector &rLowestUnusedNums, + const std::set &rUsedNums, + size_t numRequired) + { + if (!numRequired) + return; + + rLowestUnusedNums.reserve(numRequired); + sal_uInt16 newNum = 0; + //Start by using numbers from gaps in rUsedNums + for( const auto& rNum : rUsedNums ) + { + while ( newNum < rNum ) + { + rLowestUnusedNums.push_back( newNum++ ); + if ( --numRequired == 0) + return; + } + newNum++; + } + //Filled in all gaps. Fill the rest of the list with new numbers. + do + { + rLowestUnusedNums.push_back( newNum++ ); + } + while ( --numRequired > 0 ); + } + +} + +SwFormatFootnote::SwFormatFootnote( bool bEndNote ) + : SfxPoolItem( RES_TXTATR_FTN ) + , SwModify() + , m_pTextAttr(nullptr) + , m_nNumber(0) + , m_nNumberRLHidden(0) + , m_bEndNote(bEndNote) +{ +} + +bool SwFormatFootnote::operator==( const SfxPoolItem& rAttr ) const +{ + assert(SfxPoolItem::operator==(rAttr)); + return m_nNumber == static_cast(rAttr).m_nNumber && + //FIXME? + m_aNumber == static_cast(rAttr).m_aNumber && + m_bEndNote == static_cast(rAttr).m_bEndNote; +} + +SwFormatFootnote* SwFormatFootnote::Clone( SfxItemPool* ) const +{ + SwFormatFootnote* pNew = new SwFormatFootnote; + pNew->m_aNumber = m_aNumber; + pNew->m_nNumber = m_nNumber; + pNew->m_nNumberRLHidden = m_nNumberRLHidden; + pNew->m_bEndNote = m_bEndNote; + return pNew; +} + +void SwFormatFootnote::Modify(SfxPoolItem const* pOld, SfxPoolItem const* pNew) +{ + NotifyClients(pOld, pNew); + if (pOld && (RES_REMOVE_UNO_OBJECT == pOld->Which())) + { // invalidate cached UNO object + SetXFootnote(css::uno::Reference(nullptr)); + } +} + +void SwFormatFootnote::InvalidateFootnote() +{ + SwPtrMsgPoolItem const item(RES_REMOVE_UNO_OBJECT, + &static_cast(*this)); // cast to base class (void*) + NotifyClients(&item, &item); +} + +void SwFormatFootnote::SetEndNote( bool b ) +{ + if ( b != m_bEndNote ) + { + if ( GetTextFootnote() ) + { + GetTextFootnote()->DelFrames(nullptr); + } + m_bEndNote = b; + } +} + +SwFormatFootnote::~SwFormatFootnote() +{ +} + +OUString SwFormatFootnote::GetFootnoteText(SwRootFrame const& rLayout) const +{ + OUStringBuffer buf; + if( m_pTextAttr->GetStartNode() ) + { + SwNodeIndex aIdx( *m_pTextAttr->GetStartNode(), 1 ); + SwContentNode* pCNd = aIdx.GetNode().GetTextNode(); + if( !pCNd ) + pCNd = aIdx.GetNodes().GoNext( &aIdx ); + + if( pCNd->IsTextNode() ) { + buf.append(static_cast(pCNd)->GetExpandText(&rLayout)); + + ++aIdx; + while ( !aIdx.GetNode().IsEndNode() ) { + if ( aIdx.GetNode().IsTextNode() ) + { + buf.append(" "); + buf.append(aIdx.GetNode().GetTextNode()->GetExpandText(&rLayout)); + } + ++aIdx; + } + } + } + return buf.makeStringAndClear(); +} + +/// return the view string of the foot/endnote +OUString SwFormatFootnote::GetViewNumStr(const SwDoc& rDoc, + SwRootFrame const*const pLayout, bool bInclStrings) const +{ + OUString sRet( GetNumStr() ); + if( sRet.isEmpty() ) + { + // in this case the number is needed, get it via SwDoc's FootnoteInfo + bool bMakeNum = true; + const SwSectionNode* pSectNd = m_pTextAttr + ? SwUpdFootnoteEndNtAtEnd::FindSectNdWithEndAttr( *m_pTextAttr ) + : nullptr; + sal_uInt16 const nNumber(pLayout && pLayout->IsHideRedlines() + ? GetNumberRLHidden() + : GetNumber()); + + if( pSectNd ) + { + const SwFormatFootnoteEndAtTextEnd& rFootnoteEnd = static_cast( + pSectNd->GetSection().GetFormat()->GetFormatAttr( + IsEndNote() ? + static_cast(RES_END_AT_TXTEND) : + static_cast(RES_FTN_AT_TXTEND) ) ); + + if( FTNEND_ATTXTEND_OWNNUMANDFMT == rFootnoteEnd.GetValue() ) + { + bMakeNum = false; + sRet = rFootnoteEnd.GetSwNumType().GetNumStr( nNumber ); + if( bInclStrings ) + { + sRet = rFootnoteEnd.GetPrefix() + sRet + rFootnoteEnd.GetSuffix(); + } + } + } + + if( bMakeNum ) + { + const SwEndNoteInfo* pInfo; + if( IsEndNote() ) + pInfo = &rDoc.GetEndNoteInfo(); + else + pInfo = &rDoc.GetFootnoteInfo(); + sRet = pInfo->m_aFormat.GetNumStr( nNumber ); + if( bInclStrings ) + { + sRet = pInfo->GetPrefix() + sRet + pInfo->GetSuffix(); + } + } + } + return sRet; +} + +uno::Reference SwFormatFootnote::getAnchor(SwDoc& rDoc) const +{ + SolarMutexGuard aGuard; + if (!m_pTextAttr) + return uno::Reference(); + SwPaM aPam(m_pTextAttr->GetTextNode(), m_pTextAttr->GetStart()); + aPam.SetMark(); + ++aPam.GetMark()->nContent; + const uno::Reference xRet = + SwXTextRange::CreateXTextRange(rDoc, *aPam.Start(), aPam.End()); + return xRet; +} + +SwTextFootnote::SwTextFootnote( SwFormatFootnote& rAttr, sal_Int32 nStartPos ) + : SwTextAttr( rAttr, nStartPos ) + , m_pTextNode( nullptr ) + , m_nSeqNo( USHRT_MAX ) +{ + rAttr.m_pTextAttr = this; + SetHasDummyChar(true); +} + +SwTextFootnote::~SwTextFootnote() +{ + SetStartNode( nullptr ); +} + +void SwTextFootnote::SetStartNode( const SwNodeIndex *pNewNode, bool bDelNode ) +{ + if( pNewNode ) + { + if ( !m_pStartNode ) + { + m_pStartNode.reset(new SwNodeIndex(*pNewNode)); + } + else + { + *m_pStartNode = *pNewNode; + } + } + else if ( m_pStartNode ) + { + // need to do 2 things: + // 1) unregister footnotes at their pages + // 2) delete the footnote section in the Inserts of the nodes-array + SwDoc* pDoc; + if ( m_pTextNode ) + { + pDoc = m_pTextNode->GetDoc(); + } + else + { + //JP 27.01.97: the sw3-Reader creates a StartNode but the + // attribute isn't anchored in the TextNode yet. + // If it is deleted (e.g. Insert File with footnote + // inside fly frame), the content must also be deleted. + pDoc = m_pStartNode->GetNodes().GetDoc(); + } + + // If called from ~SwDoc(), must not delete the footnote nodes, + // and not necessary to delete the footnote frames. + if( !pDoc->IsInDtor() ) + { + if( bDelNode ) + { + // 2) delete the section for the footnote nodes + // it's possible that the Inserts have already been deleted (how???) + pDoc->getIDocumentContentOperations().DeleteSection( &m_pStartNode->GetNode() ); + } + else + // If the nodes are not deleted, their frames must be removed + // from the page (deleted), there is nothing else that deletes + // them (particularly not Undo) + DelFrames( nullptr ); + } + m_pStartNode.reset(); + + // remove the footnote from the SwDoc's array + for( size_t n = 0; n < pDoc->GetFootnoteIdxs().size(); ++n ) + if( this == pDoc->GetFootnoteIdxs()[n] ) + { + pDoc->GetFootnoteIdxs().erase( pDoc->GetFootnoteIdxs().begin() + n ); + // if necessary, update following footnotes + if( !pDoc->IsInDtor() && n < pDoc->GetFootnoteIdxs().size() ) + { + SwNodeIndex aTmp( pDoc->GetFootnoteIdxs()[n]->GetTextNode() ); + pDoc->GetFootnoteIdxs().UpdateFootnote( aTmp ); + } + break; + } + } +} + +void SwTextFootnote::SetNumber(const sal_uInt16 nNewNum, + sal_uInt16 const nNumberRLHidden, const OUString &sNumStr) +{ + SwFormatFootnote& rFootnote = const_cast(GetFootnote()); + + rFootnote.m_aNumber = sNumStr; + if ( sNumStr.isEmpty() ) + { + rFootnote.m_nNumber = nNewNum; + rFootnote.m_nNumberRLHidden = nNumberRLHidden; + } + InvalidateNumberInLayout(); +} + +void SwTextFootnote::InvalidateNumberInLayout() +{ + assert(m_pTextNode); + SwFormatFootnote const& rFootnote(GetFootnote()); + SwNodes &rNodes = m_pTextNode->GetDoc()->GetNodes(); + m_pTextNode->ModifyNotification( nullptr, &rFootnote ); + if ( m_pStartNode ) + { + // must iterate over all TextNodes because of footnotes on other pages + sal_uLong nSttIdx = m_pStartNode->GetIndex() + 1; + sal_uLong nEndIdx = m_pStartNode->GetNode().EndOfSectionIndex(); + for( ; nSttIdx < nEndIdx; ++nSttIdx ) + { + SwNode* pNd; + if( ( pNd = rNodes[ nSttIdx ] )->IsTextNode() ) + static_cast(pNd)->ModifyNotification( nullptr, &rFootnote ); + } + } +} + +void SwTextFootnote::CopyFootnote( + SwTextFootnote & rDest, + SwTextNode & rDestNode ) const +{ + if (m_pStartNode && !rDest.GetStartNode()) + { + // dest missing node section? create it here! + // (happens in SwTextNode::CopyText if pDest == this) + rDest.MakeNewTextSection( rDestNode.GetNodes() ); + } + if (m_pStartNode && rDest.GetStartNode()) + { + // footnotes not necessarily in same document! + SwDoc *const pDstDoc = rDestNode.GetDoc(); + SwNodes &rDstNodes = pDstDoc->GetNodes(); + + // copy only the content of the section + SwNodeRange aRg( *m_pStartNode, 1, + *m_pStartNode->GetNode().EndOfSectionNode() ); + + // insert at the end of rDest, i.e., the nodes are appended. + // nDestLen contains number of ContentNodes in rDest _before_ copy. + SwNodeIndex aStart( *(rDest.GetStartNode()) ); + SwNodeIndex aEnd( *aStart.GetNode().EndOfSectionNode() ); + sal_uLong nDestLen = aEnd.GetIndex() - aStart.GetIndex() - 1; + + m_pTextNode->GetDoc()->GetDocumentContentOperationsManager().CopyWithFlyInFly(aRg, aEnd); + + // in case the destination section was not empty, delete the old nodes + // before: Src: SxxxE, Dst: SnE + // now: Src: SxxxE, Dst: SnxxxE + // after: Src: SxxxE, Dst: SxxxE + ++aStart; + rDstNodes.Delete( aStart, nDestLen ); + } + + // also copy user defined number string + if( !GetFootnote().m_aNumber.isEmpty() ) + { + const_cast(rDest.GetFootnote()).m_aNumber = GetFootnote().m_aNumber; + } +} + +/// create a new nodes-array section for the footnote +void SwTextFootnote::MakeNewTextSection( SwNodes& rNodes ) +{ + if ( m_pStartNode ) + return; + + // set the footnote style on the SwTextNode + SwTextFormatColl *pFormatColl; + const SwEndNoteInfo* pInfo; + sal_uInt16 nPoolId; + + if( GetFootnote().IsEndNote() ) + { + pInfo = &rNodes.GetDoc()->GetEndNoteInfo(); + nPoolId = RES_POOLCOLL_ENDNOTE; + } + else + { + pInfo = &rNodes.GetDoc()->GetFootnoteInfo(); + nPoolId = RES_POOLCOLL_FOOTNOTE; + } + + if( nullptr == (pFormatColl = pInfo->GetFootnoteTextColl() ) ) + pFormatColl = rNodes.GetDoc()->getIDocumentStylePoolAccess().GetTextCollFromPool( nPoolId ); + + SwStartNode* pSttNd = rNodes.MakeTextSection( SwNodeIndex( rNodes.GetEndOfInserts() ), + SwFootnoteStartNode, pFormatColl ); + m_pStartNode.reset(new SwNodeIndex(*pSttNd)); +} + +void SwTextFootnote::DelFrames(SwRootFrame const*const pRoot) +{ + // delete the FootnoteFrames from the pages + OSL_ENSURE( m_pTextNode, "SwTextFootnote: where is my TextNode?" ); + if ( !m_pTextNode ) + return; + + bool bFrameFnd = false; + { + SwIterator aIter(*m_pTextNode); + for( SwContentFrame* pFnd = aIter.First(); pFnd; pFnd = aIter.Next() ) + { + if( pRoot != pFnd->getRootFrame() && pRoot ) + continue; + SwPageFrame* pPage = pFnd->FindPageFrame(); + if( pPage ) + { + // note: we have found the correct frame only if the footnote + // was actually removed; in case this is called from + // SwTextFrame::DestroyImpl(), then that frame isn't connected + // to SwPageFrame any more, and RemoveFootnote on any follow + // must not prevent the fall-back to the !bFrameFnd code. + bFrameFnd = pPage->RemoveFootnote(pFnd, this); + } + } + } + //JP 13.05.97: if the layout is deleted before the footnotes are deleted, + // try to delete the footnote's frames by another way + if ( !bFrameFnd && m_pStartNode ) + { + SwNodeIndex aIdx( *m_pStartNode ); + SwContentNode* pCNd = m_pTextNode->GetNodes().GoNext( &aIdx ); + if( pCNd ) + { + SwIterator aIter(*pCNd); + for( SwContentFrame* pFnd = aIter.First(); pFnd; pFnd = aIter.Next() ) + { + if( pRoot != pFnd->getRootFrame() && pRoot ) + continue; + SwPageFrame* pPage = pFnd->FindPageFrame(); + + SwFrame *pFrame = pFnd->GetUpper(); + while ( pFrame && !pFrame->IsFootnoteFrame() ) + pFrame = pFrame->GetUpper(); + + SwFootnoteFrame *pFootnote = static_cast(pFrame); + while ( pFootnote && pFootnote->GetMaster() ) + pFootnote = pFootnote->GetMaster(); + OSL_ENSURE( pFootnote->GetAttr() == this, "Footnote mismatch error." ); + + while ( pFootnote ) + { + SwFootnoteFrame *pFoll = pFootnote->GetFollow(); + pFootnote->Cut(); + SwFrame::DestroyFrame(pFootnote); + pFootnote = pFoll; + } + + // #i20556# During hiding of a section, the connection + // to the layout is already lost. pPage may be 0: + if ( pPage ) + pPage->UpdateFootnoteNum(); + } + } + } +} + +/// Set the sequence number for the current footnote. +/// @returns The new sequence number or USHRT_MAX if invalid. +void SwTextFootnote::SetSeqRefNo() +{ + if( !m_pTextNode ) + return; + + SwDoc* pDoc = m_pTextNode->GetDoc(); + if( pDoc->IsInReading() ) + return; + + std::set aUsedNums; + std::vector badRefNums; + ::lcl_FillUsedFootnoteRefNumbers(*pDoc, this, aUsedNums, badRefNums); + if ( ::lcl_IsRefNumAvailable(aUsedNums, m_nSeqNo) ) + return; + std::vector unused; + ::lcl_FillUnusedSeqRefNums(unused, aUsedNums, 1); + m_nSeqNo = unused[0]; +} + +/// Set a unique sequential reference number for every footnote in the document. +/// @param[in] rDoc The document to be processed. +void SwTextFootnote::SetUniqueSeqRefNo( SwDoc& rDoc ) +{ + std::set aUsedNums; + std::vector badRefNums; + ::lcl_FillUsedFootnoteRefNumbers(rDoc, nullptr, aUsedNums, badRefNums); + std::vector aUnused; + ::lcl_FillUnusedSeqRefNums(aUnused, aUsedNums, badRefNums.size()); + + for (size_t i = 0; i < badRefNums.size(); ++i) + { + badRefNums[i]->m_nSeqNo = aUnused[i]; + } +} + +void SwTextFootnote::CheckCondColl() +{ +//FEATURE::CONDCOLL + if( GetStartNode() ) + static_cast(GetStartNode()->GetNode()).CheckSectionCondColl(); +//FEATURE::CONDCOLL +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/atrref.cxx b/sw/source/core/txtnode/atrref.cxx new file mode 100644 index 000000000..88292c8a2 --- /dev/null +++ b/sw/source/core/txtnode/atrref.cxx @@ -0,0 +1,109 @@ +/* -*- 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 + +#include +#include +#include + +SwFormatRefMark::~SwFormatRefMark( ) +{ +} + +SwFormatRefMark::SwFormatRefMark( const OUString& rName ) + : SfxPoolItem(RES_TXTATR_REFMARK) + , SwModify() + , m_pTextAttr(nullptr) + , m_aRefName(rName) +{ +} + +SwFormatRefMark::SwFormatRefMark( const SwFormatRefMark& rAttr ) + : SfxPoolItem(RES_TXTATR_REFMARK) + , SwModify() + , BroadcasterMixin() + , m_pTextAttr(nullptr) + , m_aRefName(rAttr.m_aRefName) +{ +} + +bool SwFormatRefMark::operator==( const SfxPoolItem& rAttr ) const +{ + assert(SfxPoolItem::operator==(rAttr)); + return m_aRefName == static_cast(rAttr).m_aRefName; +} + +SwFormatRefMark* SwFormatRefMark::Clone( SfxItemPool* ) const +{ + return new SwFormatRefMark( *this ); +} + +void SwFormatRefMark::Modify(SfxPoolItem const* pOld, SfxPoolItem const* pNew) +{ + NotifyClients(pOld, pNew); + if (pOld && (RES_REMOVE_UNO_OBJECT == pOld->Which())) + { // invalidate cached UNO object + SetXRefMark(css::uno::Reference(nullptr)); + } +} + +void SwFormatRefMark::InvalidateRefMark() +{ + SwPtrMsgPoolItem const item(RES_REMOVE_UNO_OBJECT, + &static_cast(*this)); // cast to base class (void*) + NotifyClients(&item, &item); +} + +// attribute for content references in the text + +SwTextRefMark::SwTextRefMark( SwFormatRefMark& rAttr, + sal_Int32 const nStartPos, sal_Int32 const*const pEnd) + : SwTextAttr(rAttr, nStartPos) + , SwTextAttrEnd( rAttr, nStartPos, nStartPos ) + , m_pTextNode( nullptr ) + , m_pEnd( nullptr ) +{ + rAttr.m_pTextAttr = this; + if ( pEnd ) + { + m_nEnd = *pEnd; + m_pEnd = & m_nEnd; + } + else + { + SetHasDummyChar(true); + } + SetDontMoveAttr( true ); + SetOverlapAllowedAttr( true ); +} + +const sal_Int32* SwTextRefMark::GetEnd() const +{ + return m_pEnd; +} + +void SwTextRefMark::SetEnd(sal_Int32 n) +{ + *m_pEnd = n; + if (m_pHints) + m_pHints->EndPosChanged(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/atrtox.cxx b/sw/source/core/txtnode/atrtox.cxx new file mode 100644 index 000000000..de3a782d2 --- /dev/null +++ b/sw/source/core/txtnode/atrtox.cxx @@ -0,0 +1,90 @@ +/* -*- 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 +#include +#include + +SwTextTOXMark::SwTextTOXMark( SwTOXMark& rAttr, + sal_Int32 const nStartPos, sal_Int32 const*const pEnd) + : SwTextAttr( rAttr, nStartPos ) + , SwTextAttrEnd( rAttr, nStartPos, nStartPos ) + , m_pTextNode( nullptr ) + , m_pEnd( nullptr ) +{ + rAttr.m_pTextAttr = this; + if ( rAttr.GetAlternativeText().isEmpty() ) + { + m_nEnd = *pEnd; + m_pEnd = & m_nEnd; + } + else + { + SetHasDummyChar(true); + } + SetDontMoveAttr( true ); + SetOverlapAllowedAttr( true ); +} + +SwTextTOXMark::~SwTextTOXMark() +{ +} + +const sal_Int32* SwTextTOXMark::GetEnd() const +{ + return m_pEnd; +} + +void SwTextTOXMark::SetEnd(sal_Int32 n) +{ + *m_pEnd = n; + if (m_pHints) + m_pHints->EndPosChanged(); +} + +void SwTextTOXMark::CopyTOXMark( SwDoc* pDoc ) +{ + SwTOXMark& rTOX = const_cast(GetTOXMark()); + TOXTypes eType = rTOX.GetTOXType()->GetType(); + const sal_uInt16 nCount = pDoc->GetTOXTypeCount( eType ); + const SwTOXType* pType = nullptr; + const OUString rNm = rTOX.GetTOXType()->GetTypeName(); + + for(sal_uInt16 i=0; i < nCount; ++i) + { + const SwTOXType* pSrcType = pDoc->GetTOXType(eType, i); + if(pSrcType->GetTypeName() == rNm ) + { + pType = pSrcType; + break; + } + } + + // if the requested tox type does not exist, create it + if(!pType) + { + pDoc->InsertTOXType( SwTOXType( *pDoc, eType, rNm ) ); + pType = pDoc->GetTOXType(eType, 0); + } + + // register at target tox type + const_cast(pType)->Add( &rTOX ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/chrfmt.cxx b/sw/source/core/txtnode/chrfmt.cxx new file mode 100644 index 000000000..b5fac1e16 --- /dev/null +++ b/sw/source/core/txtnode/chrfmt.cxx @@ -0,0 +1,42 @@ +/* -*- 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 + +#include +#include + + +void SwCharFormat::dumpAsXml(xmlTextWriterPtr pWriter) const +{ + xmlTextWriterStartElement(pWriter, BAD_CAST("SwCharFormat")); + xmlTextWriterWriteAttribute(pWriter, BAD_CAST("name"), BAD_CAST(GetName().toUtf8().getStr())); + GetAttrSet().dumpAsXml(pWriter); + xmlTextWriterEndElement(pWriter); +} + +void SwCharFormats::dumpAsXml(xmlTextWriterPtr pWriter) const +{ + xmlTextWriterStartElement(pWriter, BAD_CAST("SwCharFormats")); + for (size_t i = 0; i < size(); ++i) + GetFormat(i)->dumpAsXml(pWriter); + xmlTextWriterEndElement(pWriter); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/fmtatr2.cxx b/sw/source/core/txtnode/fmtatr2.cxx new file mode 100644 index 000000000..e825a99a8 --- /dev/null +++ b/sw/source/core/txtnode/fmtatr2.cxx @@ -0,0 +1,843 @@ +/* -*- 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 +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +using namespace ::com::sun::star; + + +SfxPoolItem* SwFormatINetFormat::CreateDefault() { return new SwFormatINetFormat; } + +SwFormatCharFormat::SwFormatCharFormat( SwCharFormat *pFormat ) + : SfxPoolItem( RES_TXTATR_CHARFMT ), + SwClient(pFormat), + m_pTextAttribute( nullptr ) +{ +} + +SwFormatCharFormat::SwFormatCharFormat( const SwFormatCharFormat& rAttr ) + : SfxPoolItem( RES_TXTATR_CHARFMT ), + SwClient( rAttr.GetCharFormat() ), + m_pTextAttribute( nullptr ) +{ +} + +SwFormatCharFormat::~SwFormatCharFormat() {} + +bool SwFormatCharFormat::operator==( const SfxPoolItem& rAttr ) const +{ + assert(SfxPoolItem::operator==(rAttr)); + return GetCharFormat() == static_cast(rAttr).GetCharFormat(); +} + +SwFormatCharFormat* SwFormatCharFormat::Clone( SfxItemPool* ) const +{ + return new SwFormatCharFormat( *this ); +} + +// forward to the TextAttribute +void SwFormatCharFormat::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew ) +{ + if( m_pTextAttribute ) + m_pTextAttribute->ModifyNotification( pOld, pNew ); +} + +// forward to the TextAttribute +bool SwFormatCharFormat::GetInfo( SfxPoolItem& rInfo ) const +{ + return m_pTextAttribute && m_pTextAttribute->GetInfo( rInfo ); +} +bool SwFormatCharFormat::QueryValue( uno::Any& rVal, sal_uInt8 ) const +{ + OUString sCharFormatName; + if(GetCharFormat()) + SwStyleNameMapper::FillProgName(GetCharFormat()->GetName(), sCharFormatName, SwGetPoolIdFromName::ChrFmt ); + rVal <<= sCharFormatName; + return true; +} +bool SwFormatCharFormat::PutValue( const uno::Any& , sal_uInt8 ) +{ + OSL_FAIL("format cannot be set with PutValue!"); + return false; +} + +SwFormatAutoFormat::SwFormatAutoFormat( sal_uInt16 nInitWhich ) + : SfxPoolItem( nInitWhich ) +{ +} + +bool SwFormatAutoFormat::operator==( const SfxPoolItem& rAttr ) const +{ + assert(SfxPoolItem::operator==(rAttr)); + return mpHandle == static_cast(rAttr).mpHandle; +} + +SwFormatAutoFormat* SwFormatAutoFormat::Clone( SfxItemPool* ) const +{ + return new SwFormatAutoFormat( *this ); +} + +bool SwFormatAutoFormat::QueryValue( uno::Any& rVal, sal_uInt8 ) const +{ + rVal <<= StylePool::nameOf( mpHandle ); + return true; +} + +bool SwFormatAutoFormat::PutValue( const uno::Any& , sal_uInt8 ) +{ + //the format is not renameable via API + return false; +} + +void SwFormatAutoFormat::dumpAsXml(xmlTextWriterPtr pWriter) const +{ + xmlTextWriterStartElement(pWriter, BAD_CAST("SwFormatAutoFormat")); + xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this); + xmlTextWriterWriteAttribute(pWriter, BAD_CAST("whichId"), BAD_CAST(OString::number(Which()).getStr())); + mpHandle->dumpAsXml(pWriter); + xmlTextWriterEndElement(pWriter); +} + +SwFormatINetFormat::SwFormatINetFormat() + : SfxPoolItem( RES_TXTATR_INETFMT ) + , msURL() + , msTargetFrame() + , msINetFormatName() + , msVisitedFormatName() + , msHyperlinkName() + , mpTextAttr( nullptr ) + , mnINetFormatId( 0 ) + , mnVisitedFormatId( 0 ) +{} + +SwFormatINetFormat::SwFormatINetFormat( const OUString& rURL, const OUString& rTarget ) + : SfxPoolItem( RES_TXTATR_INETFMT ) + , msURL( rURL ) + , msTargetFrame( rTarget ) + , msINetFormatName() + , msVisitedFormatName() + , msHyperlinkName() + , mpTextAttr( nullptr ) + , mnINetFormatId( RES_POOLCHR_INET_NORMAL ) + , mnVisitedFormatId( RES_POOLCHR_INET_VISIT ) +{ + SwStyleNameMapper::FillUIName( mnINetFormatId, msINetFormatName ); + SwStyleNameMapper::FillUIName( mnVisitedFormatId, msVisitedFormatName ); +} + +SwFormatINetFormat::SwFormatINetFormat( const SwFormatINetFormat& rAttr ) + : SfxPoolItem( RES_TXTATR_INETFMT ) + , sw::BroadcasterMixin() + , msURL( rAttr.GetValue() ) + , msTargetFrame( rAttr.msTargetFrame ) + , msINetFormatName( rAttr.msINetFormatName ) + , msVisitedFormatName( rAttr.msVisitedFormatName ) + , msHyperlinkName( rAttr.msHyperlinkName ) + , mpTextAttr( nullptr ) + , mnINetFormatId( rAttr.mnINetFormatId ) + , mnVisitedFormatId( rAttr.mnVisitedFormatId ) +{ + if ( rAttr.GetMacroTable() ) + mpMacroTable.reset( new SvxMacroTableDtor( *rAttr.GetMacroTable() ) ); +} + +SwFormatINetFormat::~SwFormatINetFormat() +{ +} + +bool SwFormatINetFormat::operator==( const SfxPoolItem& rAttr ) const +{ + assert(SfxPoolItem::operator==(rAttr)); + bool bRet = SfxPoolItem::operator==( rAttr ) + && msURL == static_cast(rAttr).msURL + && msHyperlinkName == static_cast(rAttr).msHyperlinkName + && msTargetFrame == static_cast(rAttr).msTargetFrame + && msINetFormatName == static_cast(rAttr).msINetFormatName + && msVisitedFormatName == static_cast(rAttr).msVisitedFormatName + && mnINetFormatId == static_cast(rAttr).mnINetFormatId + && mnVisitedFormatId == static_cast(rAttr).mnVisitedFormatId; + + if( !bRet ) + return false; + + const SvxMacroTableDtor* pOther = static_cast(rAttr).mpMacroTable.get(); + if( !mpMacroTable ) + return ( !pOther || pOther->empty() ); + if( !pOther ) + return mpMacroTable->empty(); + + const SvxMacroTableDtor& rOwn = *mpMacroTable; + const SvxMacroTableDtor& rOther = *pOther; + + return rOwn == rOther; +} + +SwFormatINetFormat* SwFormatINetFormat::Clone( SfxItemPool* ) const +{ + return new SwFormatINetFormat( *this ); +} + +void SwFormatINetFormat::SetMacroTable( const SvxMacroTableDtor* pNewTable ) +{ + if( pNewTable ) + { + if( mpMacroTable ) + *mpMacroTable = *pNewTable; + else + mpMacroTable.reset( new SvxMacroTableDtor( *pNewTable ) ); + } + else + { + mpMacroTable.reset(); + } +} + +void SwFormatINetFormat::SetMacro( SvMacroItemId nEvent, const SvxMacro& rMacro ) +{ + if( !mpMacroTable ) + mpMacroTable.reset( new SvxMacroTableDtor ); + + mpMacroTable->Insert( nEvent, rMacro ); +} + +const SvxMacro* SwFormatINetFormat::GetMacro( SvMacroItemId nEvent ) const +{ + const SvxMacro* pRet = nullptr; + if( mpMacroTable && mpMacroTable->IsKeyValid( nEvent ) ) + pRet = mpMacroTable->Get( nEvent ); + return pRet; +} + +bool SwFormatINetFormat::QueryValue( uno::Any& rVal, sal_uInt8 nMemberId ) const +{ + nMemberId &= ~CONVERT_TWIPS; + switch(nMemberId) + { + case MID_URL_URL: + rVal <<= msURL; + break; + case MID_URL_TARGET: + rVal <<= msTargetFrame; + break; + case MID_URL_HYPERLINKNAME: + rVal <<= msHyperlinkName; + break; + case MID_URL_VISITED_FMT: + { + OUString sVal = msVisitedFormatName; + if (sVal.isEmpty() && mnVisitedFormatId != 0) + SwStyleNameMapper::FillUIName(mnVisitedFormatId, sVal); + if (!sVal.isEmpty()) + SwStyleNameMapper::FillProgName(sVal, sVal, + SwGetPoolIdFromName::ChrFmt); + rVal <<= sVal; + } + break; + case MID_URL_UNVISITED_FMT: + { + OUString sVal = msINetFormatName; + if (sVal.isEmpty() && mnINetFormatId != 0) + SwStyleNameMapper::FillUIName(mnINetFormatId, sVal); + if (!sVal.isEmpty()) + SwStyleNameMapper::FillProgName(sVal, sVal, + SwGetPoolIdFromName::ChrFmt); + rVal <<= sVal; + } + break; + case MID_URL_HYPERLINKEVENTS: + { + // create (and return) event descriptor + SwHyperlinkEventDescriptor* pEvents = + new SwHyperlinkEventDescriptor(); + pEvents->copyMacrosFromINetFormat(*this); + uno::Reference xNameReplace(pEvents); + + // all others return a string; so we just set rVal here and exit + rVal <<= xNameReplace; + } + break; + default: + rVal <<= OUString(); + break; + } + return true; +} +bool SwFormatINetFormat::PutValue( const uno::Any& rVal, sal_uInt8 nMemberId ) +{ + bool bRet = true; + nMemberId &= ~CONVERT_TWIPS; + + // all properties except HyperlinkEvents are of type string, hence + // we treat HyperlinkEvents specially + if (MID_URL_HYPERLINKEVENTS == nMemberId) + { + uno::Reference xReplace; + rVal >>= xReplace; + if (xReplace.is()) + { + // Create hyperlink event descriptor. Then copy events + // from argument into descriptor. Then copy events from + // the descriptor into the format. + rtl::Reference pEvents = new SwHyperlinkEventDescriptor(); + pEvents->copyMacrosFromNameReplace(xReplace); + pEvents->copyMacrosIntoINetFormat(*this); + } + else + { + // wrong type! + bRet = false; + } + } + else + { + // all string properties: + if(rVal.getValueType() != ::cppu::UnoType::get()) + return false; + + switch(nMemberId) + { + case MID_URL_URL: + rVal >>= msURL; + break; + case MID_URL_TARGET: + rVal >>= msTargetFrame; + break; + case MID_URL_HYPERLINKNAME: + rVal >>= msHyperlinkName; + break; + case MID_URL_VISITED_FMT: + { + OUString sVal; + rVal >>= sVal; + OUString aString; + SwStyleNameMapper::FillUIName( sVal, aString, SwGetPoolIdFromName::ChrFmt ); + msVisitedFormatName = aString; + mnVisitedFormatId = SwStyleNameMapper::GetPoolIdFromUIName( msVisitedFormatName, + SwGetPoolIdFromName::ChrFmt ); + } + break; + case MID_URL_UNVISITED_FMT: + { + OUString sVal; + rVal >>= sVal; + OUString aString; + SwStyleNameMapper::FillUIName( sVal, aString, SwGetPoolIdFromName::ChrFmt ); + msINetFormatName = aString; + mnINetFormatId = SwStyleNameMapper::GetPoolIdFromUIName( msINetFormatName, SwGetPoolIdFromName::ChrFmt ); + } + break; + default: + bRet = false; + } + } + return bRet; +} + +SwFormatRuby::SwFormatRuby( const OUString& rRubyText ) + : SfxPoolItem( RES_TXTATR_CJK_RUBY ), + m_sRubyText( rRubyText ), + m_pTextAttr( nullptr ), + m_nCharFormatId( 0 ), + m_nPosition( 0 ), + m_eAdjustment( css::text::RubyAdjust_LEFT ) +{ +} + +SwFormatRuby::SwFormatRuby( const SwFormatRuby& rAttr ) + : SfxPoolItem( RES_TXTATR_CJK_RUBY ), + m_sRubyText( rAttr.m_sRubyText ), + m_sCharFormatName( rAttr.m_sCharFormatName ), + m_pTextAttr( nullptr ), + m_nCharFormatId( rAttr.m_nCharFormatId), + m_nPosition( rAttr.m_nPosition ), + m_eAdjustment( rAttr.m_eAdjustment ) +{ +} + +SwFormatRuby::~SwFormatRuby() +{ +} + +SwFormatRuby& SwFormatRuby::operator=( const SwFormatRuby& rAttr ) +{ + if(this == &rAttr) + return *this; + + m_sRubyText = rAttr.m_sRubyText; + m_sCharFormatName = rAttr.m_sCharFormatName; + m_nCharFormatId = rAttr.m_nCharFormatId; + m_nPosition = rAttr.m_nPosition; + m_eAdjustment = rAttr.m_eAdjustment; + m_pTextAttr = nullptr; + return *this; +} + +bool SwFormatRuby::operator==( const SfxPoolItem& rAttr ) const +{ + assert(SfxPoolItem::operator==(rAttr)); + return m_sRubyText == static_cast(rAttr).m_sRubyText && + m_sCharFormatName == static_cast(rAttr).m_sCharFormatName && + m_nCharFormatId == static_cast(rAttr).m_nCharFormatId && + m_nPosition == static_cast(rAttr).m_nPosition && + m_eAdjustment == static_cast(rAttr).m_eAdjustment; +} + +SwFormatRuby* SwFormatRuby::Clone( SfxItemPool* ) const +{ + return new SwFormatRuby( *this ); +} + +bool SwFormatRuby::QueryValue( uno::Any& rVal, + sal_uInt8 nMemberId ) const +{ + bool bRet = true; + nMemberId &= ~CONVERT_TWIPS; + switch( nMemberId ) + { + case MID_RUBY_TEXT: rVal <<= m_sRubyText; break; + case MID_RUBY_ADJUST: rVal <<= static_cast(m_eAdjustment); break; + case MID_RUBY_CHARSTYLE: + { + OUString aString; + SwStyleNameMapper::FillProgName(m_sCharFormatName, aString, SwGetPoolIdFromName::ChrFmt ); + rVal <<= aString; + } + break; + case MID_RUBY_ABOVE: + { + rVal <<= static_cast(!m_nPosition); + } + break; + case MID_RUBY_POSITION: + { + rVal <<= m_nPosition; + } + break; + default: + bRet = false; + } + return bRet; +} +bool SwFormatRuby::PutValue( const uno::Any& rVal, + sal_uInt8 nMemberId ) +{ + bool bRet = true; + nMemberId &= ~CONVERT_TWIPS; + switch( nMemberId ) + { + case MID_RUBY_TEXT: + bRet = rVal >>= m_sRubyText; + break; + case MID_RUBY_ADJUST: + { + sal_Int16 nSet = 0; + rVal >>= nSet; + if(nSet >= sal_Int16(text::RubyAdjust_LEFT) && nSet <= sal_Int16(text::RubyAdjust_INDENT_BLOCK)) + m_eAdjustment = static_cast(nSet); + else + bRet = false; + } + break; + case MID_RUBY_ABOVE: + { + const uno::Type& rType = cppu::UnoType::get(); + if(rVal.hasValue() && rVal.getValueType() == rType) + { + bool bAbove = *o3tl::doAccess(rVal); + m_nPosition = bAbove ? 0 : 1; + } + } + break; + case MID_RUBY_POSITION: + { + sal_Int16 nSet = 0; + rVal >>= nSet; + if(nSet >= sal_Int16(text::RubyPosition::ABOVE) && nSet <= sal_Int16(text::RubyPosition::INTER_CHARACTER)) + m_nPosition = nSet; + else + bRet = false; + } + break; + case MID_RUBY_CHARSTYLE: + { + OUString sTmp; + bRet = rVal >>= sTmp; + if(bRet) + m_sCharFormatName = SwStyleNameMapper::GetUIName(sTmp, SwGetPoolIdFromName::ChrFmt ); + } + break; + default: + bRet = false; + } + return bRet; +} + +SwFormatMeta * SwFormatMeta::CreatePoolDefault(const sal_uInt16 i_nWhich) +{ + return new SwFormatMeta(i_nWhich); +} + +SwFormatMeta::SwFormatMeta(const sal_uInt16 i_nWhich) + : SfxPoolItem( i_nWhich ) + , m_pMeta() + , m_pTextAttr( nullptr ) +{ + OSL_ENSURE((RES_TXTATR_META == i_nWhich) || (RES_TXTATR_METAFIELD == i_nWhich), + "ERROR: SwFormatMeta: invalid which id!"); +} + +SwFormatMeta::SwFormatMeta( std::shared_ptr< ::sw::Meta > const & i_pMeta, + const sal_uInt16 i_nWhich ) + : SfxPoolItem( i_nWhich ) + , m_pMeta( i_pMeta ) + , m_pTextAttr( nullptr ) +{ + OSL_ENSURE((RES_TXTATR_META == i_nWhich) || (RES_TXTATR_METAFIELD == i_nWhich), + "ERROR: SwFormatMeta: invalid which id!"); + OSL_ENSURE(m_pMeta, "SwFormatMeta: no Meta ?"); + // DO NOT call m_pMeta->SetFormatMeta(this) here; only from SetTextAttr! +} + +SwFormatMeta::~SwFormatMeta() +{ + if (m_pMeta && (m_pMeta->GetFormatMeta() == this)) + { + NotifyChangeTextNode(nullptr); + m_pMeta->SetFormatMeta(nullptr); + } +} + +bool SwFormatMeta::operator==( const SfxPoolItem & i_rOther ) const +{ + return SfxPoolItem::operator==( i_rOther ) + && m_pMeta == static_cast( i_rOther ).m_pMeta; +} + +SwFormatMeta* SwFormatMeta::Clone( SfxItemPool * /*pPool*/ ) const +{ + // if this is indeed a copy, then DoCopy must be called later! + return m_pMeta // #i105148# pool default may be cloned also! + ? new SwFormatMeta( m_pMeta, Which() ) : new SwFormatMeta( Which() ); +} + +void SwFormatMeta::SetTextAttr(SwTextMeta * const i_pTextAttr) +{ + OSL_ENSURE(!(m_pTextAttr && i_pTextAttr), + "SwFormatMeta::SetTextAttr: already has text attribute?"); + OSL_ENSURE( m_pTextAttr || i_pTextAttr , + "SwFormatMeta::SetTextAttr: no attribute to remove?"); + m_pTextAttr = i_pTextAttr; + OSL_ENSURE(m_pMeta, "inserted SwFormatMeta has no sw::Meta?"); + // the sw::Meta must be able to find the current text attribute! + if (m_pMeta) + { + if (i_pTextAttr) + { + m_pMeta->SetFormatMeta(this); + } + else if (m_pMeta->GetFormatMeta() == this) + { // text attribute gone => de-register from text node! + NotifyChangeTextNode(nullptr); + m_pMeta->SetFormatMeta(nullptr); + } + } +} + +void SwFormatMeta::NotifyChangeTextNode(SwTextNode *const pTextNode) +{ + // N.B.: do not reset m_pTextAttr here: see call in nodes.cxx, + // where the hint is not deleted! + OSL_ENSURE(m_pMeta, "SwFormatMeta::NotifyChangeTextNode: no Meta?"); + if (m_pMeta && (m_pMeta->GetFormatMeta() == this)) + { // do not call Modify, that would call SwXMeta::Modify! + m_pMeta->NotifyChangeTextNode(pTextNode); + } +} + +// this SwFormatMeta has been cloned and points at the same sw::Meta as the source +// this method copies the sw::Meta +void SwFormatMeta::DoCopy(::sw::MetaFieldManager & i_rTargetDocManager, + SwTextNode & i_rTargetTextNode) +{ + OSL_ENSURE(m_pMeta, "DoCopy called for SwFormatMeta with no sw::Meta?"); + if (m_pMeta) + { + const std::shared_ptr< ::sw::Meta> pOriginal( m_pMeta ); + if (RES_TXTATR_META == Which()) + { + m_pMeta = std::make_shared<::sw::Meta>(this); + } + else + { + ::sw::MetaField *const pMetaField( + static_cast< ::sw::MetaField* >(pOriginal.get())); + m_pMeta = i_rTargetDocManager.makeMetaField( this, + pMetaField->m_nNumberFormat, pMetaField->IsFixedLanguage() ); + } + // Meta must have a text node before calling RegisterAsCopyOf + m_pMeta->NotifyChangeTextNode(& i_rTargetTextNode); + // this cannot be done in Clone: a Clone is not necessarily a copy! + m_pMeta->RegisterAsCopyOf(*pOriginal); + } +} + +namespace sw { + +Meta::Meta(SwFormatMeta * const i_pFormat) + : ::sfx2::Metadatable() + , SwModify() + , m_pFormat(i_pFormat) + , m_pTextNode(nullptr) +{ +} + +Meta::~Meta() +{ +} + +SwTextMeta * Meta::GetTextAttr() const +{ + return m_pFormat ? m_pFormat->GetTextAttr() : nullptr; +} + + +void Meta::NotifyChangeTextNode(SwTextNode *const pTextNode) +{ + m_pTextNode = pTextNode; + if (m_pTextNode && (GetRegisteredIn() != m_pTextNode)) + { + m_pTextNode->Add(this); + } + else if (!m_pTextNode) + { + EndListeningAll(); + } + if (!pTextNode) // text node gone? invalidate UNO object! + { + GetNotifier().Broadcast(SfxHint(SfxHintId::Deinitializing)); + } +} + +// SwClient +void Meta::Modify( const SfxPoolItem *pOld, const SfxPoolItem *pNew ) +{ + NotifyClients(pOld, pNew); + GetNotifier().Broadcast(SfxHint(SfxHintId::DataChanged)); + if (pOld && (RES_REMOVE_UNO_OBJECT == pOld->Which())) + { // invalidate cached uno object + SetXMeta(uno::Reference(nullptr)); + GetNotifier().Broadcast(SfxHint(SfxHintId::Deinitializing)); + } +} + +// sfx2::Metadatable +::sfx2::IXmlIdRegistry& Meta::GetRegistry() +{ + SwTextNode * const pTextNode( GetTextNode() ); + // GetRegistry may only be called on a meta that is actually in the + // document, which means it has a pointer to its text node + OSL_ENSURE(pTextNode, "ERROR: GetRegistry: no text node?"); + if (!pTextNode) + throw uno::RuntimeException(); + return pTextNode->GetRegistry(); +} + +bool Meta::IsInClipboard() const +{ + const SwTextNode * const pTextNode( GetTextNode() ); +// no text node: in UNDO OSL_ENSURE(pTextNode, "IsInClipboard: no text node?"); + return pTextNode && pTextNode->IsInClipboard(); +} + +bool Meta::IsInUndo() const +{ + const SwTextNode * const pTextNode( GetTextNode() ); +// no text node: in UNDO OSL_ENSURE(pTextNode, "IsInUndo: no text node?"); + return pTextNode == nullptr || pTextNode->IsInUndo(); +} + +bool Meta::IsInContent() const +{ + const SwTextNode * const pTextNode( GetTextNode() ); + OSL_ENSURE(pTextNode, "IsInContent: no text node?"); + return pTextNode == nullptr || pTextNode->IsInContent(); +} + +css::uno::Reference< css::rdf::XMetadatable > Meta::MakeUnoObject() +{ + return SwXMeta::CreateXMeta(*this); +} + +MetaField::MetaField(SwFormatMeta * const i_pFormat, + const sal_uInt32 nNumberFormat, const bool bIsFixedLanguage) + : Meta(i_pFormat) + , m_nNumberFormat( nNumberFormat ) + , m_bIsFixedLanguage( bIsFixedLanguage ) +{ +} + +void MetaField::GetPrefixAndSuffix( + OUString *const o_pPrefix, OUString *const o_pSuffix) +{ + try + { + const uno::Reference xMetaField( MakeUnoObject() ); + OSL_ENSURE(dynamic_cast(xMetaField.get()), + "GetPrefixAndSuffix: no SwXMetaField?"); + if (xMetaField.is()) + { + SwTextNode * const pTextNode( GetTextNode() ); + SwDocShell const * const pShell(pTextNode->GetDoc()->GetDocShell()); + const uno::Reference xModel( + pShell ? pShell->GetModel() : nullptr, uno::UNO_SET_THROW); + getPrefixAndSuffix(xModel, xMetaField, o_pPrefix, o_pSuffix); + } + } + catch (const uno::Exception&) + { + OSL_FAIL("exception?"); + } +} + +sal_uInt32 MetaField::GetNumberFormat(OUString const & rContent) const +{ + //TODO: this probably lacks treatment for some special cases + sal_uInt32 nNumberFormat( m_nNumberFormat ); + SwTextNode * const pTextNode( GetTextNode() ); + if (pTextNode) + { + double number; + (void) pTextNode->GetDoc()->IsNumberFormat( rContent, nNumberFormat, number ); + } + return nNumberFormat; +} + +void MetaField::SetNumberFormat(sal_uInt32 nNumberFormat) +{ + // effectively, the member is only a default: + // GetNumberFormat checks if the text actually conforms + m_nNumberFormat = nNumberFormat; +} + +MetaFieldManager::MetaFieldManager() +{ +} + +std::shared_ptr +MetaFieldManager::makeMetaField(SwFormatMeta * const i_pFormat, + const sal_uInt32 nNumberFormat, const bool bIsFixedLanguage) +{ + const std::shared_ptr pMetaField( + new MetaField(i_pFormat, nNumberFormat, bIsFixedLanguage) ); + m_MetaFields.push_back(pMetaField); + return pMetaField; +} + +namespace { + +struct IsInUndo +{ + bool operator()(std::weak_ptr const & pMetaField) { + return pMetaField.lock()->IsInUndo(); + } +}; + +struct MakeUnoObject +{ + uno::Reference + operator()(std::weak_ptr const & pMetaField) { + return uno::Reference( + pMetaField.lock()->MakeUnoObject(), uno::UNO_QUERY); + } +}; + +} + +std::vector< uno::Reference > +MetaFieldManager::getMetaFields() +{ + // erase deleted fields + const MetaFieldList_t::iterator iter( + std::remove_if(m_MetaFields.begin(), m_MetaFields.end(), + [] (std::weak_ptr const& rField) { return rField.expired(); })); + m_MetaFields.erase(iter, m_MetaFields.end()); + // filter out fields in UNDO + MetaFieldList_t filtered(m_MetaFields.size()); + const MetaFieldList_t::iterator iter2( + std::remove_copy_if(m_MetaFields.begin(), m_MetaFields.end(), + filtered.begin(), IsInUndo())); + filtered.erase(iter2, filtered.end()); + // create uno objects + std::vector< uno::Reference > ret(filtered.size()); + std::transform(filtered.begin(), filtered.end(), ret.begin(), + MakeUnoObject()); + return ret; +} + +void MetaFieldManager::copyDocumentProperties(const SwDoc& rSource) +{ + const SwDocShell* pDocShell = rSource.GetDocShell(); + if (!pDocShell) + return; + + uno::Reference xDocumentPropertiesSupplier(pDocShell->GetModel(), uno::UNO_QUERY); + uno::Reference xCloneable(xDocumentPropertiesSupplier->getDocumentProperties(), uno::UNO_QUERY); + m_xDocumentProperties.set(xCloneable->createClone(), uno::UNO_QUERY); +} + +const uno::Reference& MetaFieldManager::getDocumentProperties() const +{ + return m_xDocumentProperties; +} + +} // namespace sw + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/fntcache.cxx b/sw/source/core/txtnode/fntcache.cxx new file mode 100644 index 000000000..decea02c4 --- /dev/null +++ b/sw/source/core/txtnode/fntcache.cxx @@ -0,0 +1,2712 @@ +/* -*- 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 +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace ::com::sun::star; + +// global variables declared in fntcache.hxx +// FontCache is created in txtinit.cxx TextInit_ and deleted in TextFinit +SwFntCache *pFntCache = nullptr; +// last Font set by ChgFntCache +SwFntObj *pLastFont = nullptr; + +static constexpr Color gWaveCol(COL_GRAY); + +long SwFntObj::nPixWidth; +MapMode* SwFntObj::pPixMap = nullptr; +static vcl::DeleteOnDeinit< VclPtr > s_pFntObjPixOut( new VclPtr ); + +namespace +{ + +long EvalGridWidthAdd( const SwTextGridItem *const pGrid, const SwDrawTextInfo &rInf ) +{ + SwDocShell* pDocShell = rInf.GetShell()->GetDoc()->GetDocShell(); + SfxStyleSheetBasePool* pBasePool = pDocShell->GetStyleSheetPool(); + + SfxStyleSheetBase* pStyle = pBasePool->Find(SwResId(STR_POOLCOLL_STANDARD), SfxStyleFamily::Para); + SfxItemSet& aTmpSet = pStyle->GetItemSet(); + const SvxFontHeightItem &aDefaultFontItem = aTmpSet.Get(RES_CHRATR_CJK_FONTSIZE); + + const SwDoc* pDoc = rInf.GetShell()->GetDoc(); + const sal_uInt16 nGridWidth = GetGridWidth(*pGrid, *pDoc); + const sal_uInt32 nFontHeight = aDefaultFontItem.GetHeight(); + const long nGridWidthAdd = nGridWidth > nFontHeight ? nGridWidth - nFontHeight : 0; + if( SwFontScript::Latin == rInf.GetFont()->GetActual() ) + return nGridWidthAdd / 2; + + return nGridWidthAdd; +} + +/** + * Pre-calculates glyph items for the rendered subset of rKey's text, assuming + * outdev state does not change between the outdev calls. + */ +SalLayoutGlyphs* lcl_CreateLayout(const SwTextGlyphsKey& rKey, SalLayoutGlyphs& rTextGlyphs) +{ + // Use pre-calculated result. + if (rTextGlyphs.IsValid()) + return &rTextGlyphs; + + if (rKey.m_nIndex >= rKey.m_aText.getLength()) + // Same as in OutputDevice::GetTextArray(). + return nullptr; + + // Calculate glyph items. + std::unique_ptr pLayout + = rKey.m_pOutputDevice->ImplLayout(rKey.m_aText, rKey.m_nIndex, rKey.m_nLength, Point(0, 0), 0, + nullptr, SalLayoutFlags::GlyphItemsOnly); + if (!pLayout) + return nullptr; + + const SalLayoutGlyphs* pGlyphs = pLayout->GetGlyphs(); + if (!pGlyphs) + return nullptr; + + // Remember the calculation result. + rTextGlyphs = *pGlyphs; + + return &rTextGlyphs; +} +} + +bool operator<(const SwTextGlyphsKey& l, const SwTextGlyphsKey& r) +{ + if (l.m_pOutputDevice.get() < r.m_pOutputDevice.get()) + return true; + if (l.m_pOutputDevice.get() > r.m_pOutputDevice.get()) + return false; + if (l.m_nIndex < r.m_nIndex) + return true; + if (l.m_nIndex > r.m_nIndex) + return false; + if (l.m_nLength < r.m_nLength) + return true; + if (l.m_nLength > r.m_nLength) + return false; + + // Comparing strings is expensive, so compare them: + // - only at the end of this function + // - only once + // - only the relevant substring (if the index/length is not out of bounds) + sal_Int32 nRet = 0; + if (l.m_nLength < 0 || l.m_nIndex < 0 || l.m_nIndex + l.m_nLength > l.m_aText.getLength()) + nRet = l.m_aText.compareTo(r.m_aText); + else + nRet = memcmp(l.m_aText.getStr() + l.m_nIndex, r.m_aText.getStr() + r.m_nIndex, + l.m_nLength * sizeof(sal_Unicode)); + if (nRet < 0) + return true; + if (nRet > 0) + return false; + + return false; +}; + +void SwFntCache::Flush( ) +{ + if ( pLastFont ) + { + pLastFont->Unlock(); + pLastFont = nullptr; + } + SwCache::Flush( ); +} + +SwFntObj::SwFntObj(const SwSubFont &rFont, std::uintptr_t nFontCacheId, SwViewShell const *pSh) + : SwCacheObj(reinterpret_cast(nFontCacheId)) + , m_aFont(rFont) + , m_pScrFont(nullptr) + , m_pPrtFont(&m_aFont) + , m_pPrinter(nullptr) + , m_nGuessedLeading(USHRT_MAX) + , m_nExtLeading(USHRT_MAX) + , m_nScrAscent(0) + , m_nPrtAscent(USHRT_MAX) + , m_nScrHeight(0) + , m_nPrtHeight(USHRT_MAX) + , m_nPropWidth(rFont.GetPropWidth()) +{ + m_nZoom = pSh ? pSh->GetViewOptions()->GetZoom() : USHRT_MAX; + m_bSymbol = RTL_TEXTENCODING_SYMBOL == m_aFont.GetCharSet(); + m_bPaintBlank = ( LINESTYLE_NONE != m_aFont.GetUnderline() + || LINESTYLE_NONE != m_aFont.GetOverline() + || STRIKEOUT_NONE != m_aFont.GetStrikeout() ) + && !m_aFont.IsWordLineMode(); + m_aFont.SetLanguage(rFont.GetLanguage()); +} + +SwFntObj::~SwFntObj() +{ + if ( m_pScrFont != m_pPrtFont ) + delete m_pScrFont; + if ( m_pPrtFont != &m_aFont ) + delete m_pPrtFont; +} + +void SwFntObj::CreatePrtFont( const OutputDevice& rPrt ) +{ + if ( m_nPropWidth == 100 || m_pPrinter == &rPrt ) + return; + + if( m_pScrFont != m_pPrtFont ) + delete m_pScrFont; + if( m_pPrtFont != &m_aFont ) + delete m_pPrtFont; + + const vcl::Font aOldFnt( rPrt.GetFont() ); + const_cast(rPrt).SetFont( m_aFont ); + const FontMetric aWinMet( rPrt.GetFontMetric() ); + const_cast(rPrt).SetFont( aOldFnt ); + auto nWidth = ( aWinMet.GetFontSize().Width() * m_nPropWidth ) / 100; + + if( !nWidth ) + ++nWidth; + m_pPrtFont = new vcl::Font( m_aFont ); + m_pPrtFont->SetFontSize( Size( nWidth, m_aFont.GetFontSize().Height() ) ); + m_pScrFont = nullptr; + +} + +/* + * returns whether we have to adjust the output font to resemble + * the formatting font + * + * _Not_ necessary if + * + * 1. RefDef == OutDev (text formatting, online layout...) + * 2. PDF export from online layout + * 3. Prospect/PagePreview printing + */ +static bool lcl_IsFontAdjustNecessary( const vcl::RenderContext& rOutDev, + const vcl::RenderContext& rRefDev ) +{ + return &rRefDev != &rOutDev && + OUTDEV_WINDOW != rRefDev.GetOutDevType() && + ( OUTDEV_PRINTER != rRefDev.GetOutDevType() || + OUTDEV_PRINTER != rOutDev.GetOutDevType() ); +} + +namespace { + +struct CalcLinePosData +{ + SwDrawTextInfo& rInf; + vcl::Font& rFont; + TextFrameIndex nCnt; + const bool bSwitchH2V; + const bool bSwitchH2VLRBT; + const bool bSwitchL2R; + long nHalfSpace; + long* pKernArray; + const bool bBidiPor; + + CalcLinePosData( SwDrawTextInfo& _rInf, vcl::Font& _rFont, + TextFrameIndex const _nCnt, const bool _bSwitchH2V, const bool _bSwitchH2VLRBT, const bool _bSwitchL2R, + long _nHalfSpace, long* _pKernArray, const bool _bBidiPor) : + rInf( _rInf ), + rFont( _rFont ), + nCnt( _nCnt ), + bSwitchH2V( _bSwitchH2V ), + bSwitchH2VLRBT( _bSwitchH2VLRBT ), + bSwitchL2R( _bSwitchL2R ), + nHalfSpace( _nHalfSpace ), + pKernArray( _pKernArray ), + bBidiPor( _bBidiPor ) + { + } +}; + +} + +// Computes the start and end position of an underline. This function is called +// from the DrawText-method (for underlining misspelled words or smarttag terms). +static void lcl_calcLinePos( const CalcLinePosData &rData, + Point &rStart, Point &rEnd, TextFrameIndex const nStart, TextFrameIndex const nWrLen) +{ + long nBlank = 0; + const TextFrameIndex nEnd = nStart + nWrLen; + const long nTmpSpaceAdd = rData.rInf.GetSpace() / SPACING_PRECISION_FACTOR; + + if ( nEnd < rData.nCnt + && CH_BLANK == rData.rInf.GetText()[sal_Int32(rData.rInf.GetIdx() + nEnd)] ) + { + if (nEnd + TextFrameIndex(1) == rData.nCnt) + nBlank -= nTmpSpaceAdd; + else + nBlank -= rData.nHalfSpace; + } + + // determine start, end and length of wave line + sal_Int32 nKernStart = nStart ? rData.pKernArray[sal_Int32(nStart) - 1] : 0; + sal_Int32 nKernEnd = rData.pKernArray[sal_Int32(nEnd) - 1]; + + const sal_uInt16 nDir = rData.bBidiPor ? 1800 + : UnMapDirection(rData.rFont.GetOrientation(), + rData.bSwitchH2V, rData.bSwitchH2VLRBT); + + switch ( nDir ) + { + case 0 : + rStart.AdjustX(nKernStart ); + rEnd.setX( nBlank + rData.rInf.GetPos().X() + nKernEnd ); + rEnd.setY( rData.rInf.GetPos().Y() ); + break; + case 900 : + rStart.AdjustY( -nKernStart ); + rEnd.setX( rData.rInf.GetPos().X() ); + rEnd.setY( nBlank + rData.rInf.GetPos().Y() - nKernEnd ); + break; + case 1800 : + rStart.AdjustX( -nKernStart ); + rEnd.setX( rData.rInf.GetPos().X() - nKernEnd - nBlank ); + rEnd.setY( rData.rInf.GetPos().Y() ); + break; + case 2700 : + rStart.AdjustY(nKernStart ); + rEnd.setX( rData.rInf.GetPos().X() ); + rEnd.setY( nBlank + rData.rInf.GetPos().Y() + nKernEnd ); + break; + } + + if ( rData.bSwitchL2R ) + { + rData.rInf.GetFrame()->SwitchLTRtoRTL( rStart ); + rData.rInf.GetFrame()->SwitchLTRtoRTL( rEnd ); + } + + if ( rData.bSwitchH2V ) + { + rData.rInf.GetFrame()->SwitchHorizontalToVertical( rStart ); + rData.rInf.GetFrame()->SwitchHorizontalToVertical( rEnd ); + } +} + +// Returns the Ascent of the Font on the given output device; +// it may be necessary to create the screen font first. +sal_uInt16 SwFntObj::GetFontAscent( const SwViewShell *pSh, const OutputDevice& rOut ) +{ + sal_uInt16 nRet = 0; + const OutputDevice& rRefDev = pSh ? pSh->GetRefDev() : rOut; + + if ( pSh && lcl_IsFontAdjustNecessary( rOut, rRefDev ) ) + { + CreateScrFont( *pSh, rOut ); + OSL_ENSURE( USHRT_MAX != m_nScrAscent, "nScrAscent is going berzerk" ); + nRet = m_nScrAscent; + } + else + { + if (m_nPrtAscent == USHRT_MAX) // printer ascent unknown? + { + CreatePrtFont( rOut ); + const vcl::Font aOldFnt( rRefDev.GetFont() ); + const_cast(rRefDev).SetFont( *m_pPrtFont ); + const FontMetric aOutMet( rRefDev.GetFontMetric() ); + m_nPrtAscent = static_cast(aOutMet.GetAscent()); + const_cast(rRefDev).SetFont( aOldFnt ); + } + + nRet = m_nPrtAscent; + } + +#if !defined(MACOSX) // #i89844# extleading is below the line for Mac + // TODO: move extleading below the line for all platforms too + nRet += GetFontLeading( pSh, rRefDev ); +#endif + + OSL_ENSURE( USHRT_MAX != nRet, "GetFontAscent returned USHRT_MAX" ); + return nRet; +} + +// Returns the height of the Font on the given output device; +// it may be necessary to create the screen font first. +sal_uInt16 SwFntObj::GetFontHeight( const SwViewShell* pSh, const OutputDevice& rOut ) +{ + sal_uInt16 nRet = 0; + const OutputDevice& rRefDev = pSh ? pSh->GetRefDev() : rOut; + + if ( pSh && lcl_IsFontAdjustNecessary( rOut, rRefDev ) ) + { + CreateScrFont( *pSh, rOut ); + OSL_ENSURE( USHRT_MAX != m_nScrHeight, "nScrHeight is going berzerk" ); + nRet = m_nScrHeight + GetFontLeading( pSh, rRefDev ); + } + else + { + if (m_nPrtHeight == USHRT_MAX) // printer height unknown? + { + CreatePrtFont( rOut ); + const vcl::Font aOldFnt( rRefDev.GetFont() ); + const_cast(rRefDev).SetFont( *m_pPrtFont ); + m_nPrtHeight = static_cast(rRefDev.GetTextHeight()); + +#if OSL_DEBUG_LEVEL > 0 + // Check if vcl did not change the meaning of GetTextHeight + const FontMetric aOutMet( rRefDev.GetFontMetric() ); + long nTmpPrtHeight = static_cast(aOutMet.GetAscent()) + aOutMet.GetDescent(); + // #i106098#: do not compare with == here due to rounding error + OSL_ENSURE( std::abs(nTmpPrtHeight - m_nPrtHeight) < 3, + "GetTextHeight != Ascent + Descent" ); +#endif + + const_cast(rRefDev).SetFont( aOldFnt ); + } + + nRet = m_nPrtHeight + GetFontLeading( pSh, rRefDev ); + } + + OSL_ENSURE( USHRT_MAX != nRet, "GetFontHeight returned USHRT_MAX" ); + return nRet; +} + +sal_uInt16 SwFntObj::GetFontLeading( const SwViewShell *pSh, const OutputDevice& rOut ) +{ + sal_uInt16 nRet = 0; + + if ( pSh ) + { + if ( USHRT_MAX == m_nGuessedLeading || USHRT_MAX == m_nExtLeading ) + { + SolarMutexGuard aGuard; + + const vcl::Font aOldFnt( rOut.GetFont() ); + const_cast(rOut).SetFont( *m_pPrtFont ); + const FontMetric aMet( rOut.GetFontMetric() ); + const_cast(rOut).SetFont( aOldFnt ); + m_bSymbol = RTL_TEXTENCODING_SYMBOL == aMet.GetCharSet(); + GuessLeading( *pSh, aMet ); + m_nExtLeading = static_cast(aMet.GetExternalLeading()); + /* HACK: FIXME There is something wrong with Writer's bullet rendering, causing lines + with bullets to be higher than they should be. I think this is because + Writer uses font's external leading incorrect, as the vertical distance + added to every line instead of only a distance between multiple lines, + which means a single bullet has external leading added even though it + shouldn't, but frankly this is just an educated guess rather than understanding + Writer's layout (heh). + Symbol font in some documents is 'StarSymbol; Arial Unicode MS', and Windows + machines often do not have StarSymbol, falling back to Arial Unicode MS, which + has unusually high external leading. So just reset external leading for fonts + which are used to bullets, as those should not be used on multiple lines anyway, + so in correct rendering external leading should be irrelevant anyway. + Interestingly enough, bSymbol is false for 'StarSymbol; Arial Unicode MS', so + also check explicitly. + */ + if( m_bSymbol || IsStarSymbol( m_pPrtFont->GetFamilyName())) + m_nExtLeading = 0; + } + + const IDocumentSettingAccess& rIDSA = pSh->getIDocumentSettingAccess(); + const bool bBrowse = ( pSh->GetWin() && + pSh->GetViewOptions()->getBrowseMode() && + !pSh->GetViewOptions()->IsPrtFormat() ); + + if ( !bBrowse && rIDSA.get(DocumentSettingId::ADD_EXT_LEADING) ) + nRet = m_nExtLeading; + else + nRet = m_nGuessedLeading; + } + + OSL_ENSURE( USHRT_MAX != nRet, "GetFontLeading returned USHRT_MAX" ); + return nRet; +} + +// pOut is the output device, not the reference device +void SwFntObj::CreateScrFont( const SwViewShell& rSh, const OutputDevice& rOut ) +{ + if ( m_pScrFont ) + return; + + // any changes to the output device are reset at the end of the function + OutputDevice* pOut = const_cast(&rOut); + + // Save old font + vcl::Font aOldOutFont( pOut->GetFont() ); + + m_nScrHeight = USHRT_MAX; + + // Condition for output font / refdev font adjustment + OutputDevice* pPrt = &rSh.GetRefDev(); + + if( !rSh.GetWin() || + !rSh.GetViewOptions()->getBrowseMode() || + rSh.GetViewOptions()->IsPrtFormat() ) + { + // After CreatePrtFont m_pPrtFont is the font which is actually used + // by the reference device + CreatePrtFont( *pPrt ); + m_pPrinter = pPrt; + + // save old reference device font + vcl::Font aOldPrtFnt( pPrt->GetFont() ); + + // set the font used at the reference device at the reference device + // and the output device + pPrt->SetFont( *m_pPrtFont ); + pOut->SetFont( *m_pPrtFont ); + + // This should be the default for pScrFont. + m_pScrFont = m_pPrtFont; + + FontMetric aMet = pPrt->GetFontMetric( ); + // Don't lose "faked" properties of the logical font that don't truly + // exist in the physical font metrics which vcl which fake up for us + aMet.SetWeight(m_pScrFont->GetWeight()); + aMet.SetItalic(m_pScrFont->GetItalic()); + + m_bSymbol = RTL_TEXTENCODING_SYMBOL == aMet.GetCharSet(); + + if ( USHRT_MAX == m_nGuessedLeading ) + GuessLeading( rSh, aMet ); + + if ( USHRT_MAX == m_nExtLeading ) + m_nExtLeading = static_cast(aMet.GetExternalLeading()); + + // reset the original reference device font + pPrt->SetFont( aOldPrtFnt ); + } + else + { + m_bSymbol = RTL_TEXTENCODING_SYMBOL == m_aFont.GetCharSet(); + if ( m_nGuessedLeading == USHRT_MAX ) + m_nGuessedLeading = 0; + + // no external leading in browse mode + if ( m_nExtLeading == USHRT_MAX ) + m_nExtLeading = 0; + + m_pScrFont = m_pPrtFont; + } + + // check zoom factor, e.g. because of PrtOle2 during export + { + // In case the zoom factor of the output device differs from the + // one in the ViewOptions, this Font must not be cached, + // hence set zoom factor to an invalid value + long nTmp; + if( pOut->GetMapMode().GetScaleX().IsValid() && + pOut->GetMapMode().GetScaleY().IsValid() && + pOut->GetMapMode().GetScaleX() == pOut->GetMapMode().GetScaleY() ) + { + nTmp = long(100 * pOut->GetMapMode().GetScaleX()); + } + else + nTmp = 0; + if( nTmp != m_nZoom ) + m_nZoom = USHRT_MAX - 1; + } + + m_nScrAscent = static_cast(pOut->GetFontMetric().GetAscent()); + if ( USHRT_MAX == m_nScrHeight ) + m_nScrHeight = static_cast(pOut->GetTextHeight()); + + // reset original output device font + pOut->SetFont( aOldOutFont ); +} + +void SwFntObj::GuessLeading( const SwViewShell& +#if defined(_WIN32) + rSh +#endif + , const FontMetric& rMet ) +{ + // If leading >= 5, this seems to be enough leading. + // Nothing has to be done. + if ( rMet.GetInternalLeading() >= 5 ) + { + m_nGuessedLeading = 0; + return; + } + +#if defined(_WIN32) + OutputDevice *pWin = rSh.GetWin() ? + rSh.GetWin() : + Application::GetDefaultDevice(); + if ( pWin ) + { + MapMode aTmpMap( MapUnit::MapTwip ); + MapMode aOldMap = pWin->GetMapMode( ); + pWin->SetMapMode( aTmpMap ); + const vcl::Font aOldFnt( pWin->GetFont() ); + pWin->SetFont( *m_pPrtFont ); + const FontMetric aWinMet( pWin->GetFontMetric() ); + const sal_uInt16 nWinHeight = sal_uInt16( aWinMet.GetFontSize().Height() ); + if( m_pPrtFont->GetFamilyName().indexOf( aWinMet.GetFamilyName() ) != -1 ) + { + // If the Leading on the Window is also 0, then it has to stay + // that way (see also StarMath). + long nTmpLeading = aWinMet.GetInternalLeading(); + if( nTmpLeading <= 0 ) + { + pWin->SetFont( rMet ); + nTmpLeading = pWin->GetFontMetric().GetInternalLeading(); + if( nTmpLeading < 0 ) + m_nGuessedLeading = 0; + else + m_nGuessedLeading = sal_uInt16(nTmpLeading); + } + else + { + m_nGuessedLeading = sal_uInt16(nTmpLeading); + // Manta-Hack #50153#: + // Wer beim Leading luegt, luegt moeglicherweise auch beim + // Ascent/Descent, deshalb wird hier ggf. der Font ein wenig + // tiefergelegt, ohne dabei seine Hoehe zu aendern. + // (above original comment preserved for cultural reasons) + // Those who lie about their Leading, may lie about their + // Ascent/Descent as well, hence the Font will be lowered a + // little without changing its height. + long nDiff = std::min( rMet.GetDescent() - aWinMet.GetDescent(), + aWinMet.GetAscent() - rMet.GetAscent() - nTmpLeading ); + if( nDiff > 0 ) + { + OSL_ENSURE( m_nPrtAscent < USHRT_MAX, "GuessLeading: PrtAscent-Fault" ); + if ( m_nPrtAscent < USHRT_MAX ) + m_nPrtAscent = m_nPrtAscent + static_cast(( 2 * nDiff ) / 5); + } + } + } + else + { + // If all else fails, take 15% of the height, as empirically + // determined by CL + m_nGuessedLeading = (nWinHeight * 15) / 100; + } + pWin->SetFont( aOldFnt ); + pWin->SetMapMode( aOldMap ); + } + else + m_nGuessedLeading = 0; +#else + m_nGuessedLeading = 0; +#endif +} + +// Set the font at the given output device; for screens it may be +// necessary to do some adjustment first. +void SwFntObj::SetDevFont( const SwViewShell *pSh, OutputDevice& rOut ) +{ + const OutputDevice& rRefDev = pSh ? pSh->GetRefDev() : rOut; + + if ( pSh && lcl_IsFontAdjustNecessary( rOut, rRefDev ) ) + { + CreateScrFont( *pSh, rOut ); + if( !GetScrFont()->IsSameInstance( rOut.GetFont() ) ) + rOut.SetFont( *m_pScrFont ); + if( m_pPrinter && ( !m_pPrtFont->IsSameInstance( m_pPrinter->GetFont() ) ) ) + m_pPrinter->SetFont( *m_pPrtFont ); + } + else + { + CreatePrtFont( rOut ); + if( !m_pPrtFont->IsSameInstance( rOut.GetFont() ) ) + rOut.SetFont( *m_pPrtFont ); + } + + // Here, we actually do not need the leading values, but by calling + // GetFontLeading() we assure that the values are calculated for later use. + GetFontLeading( pSh, rRefDev ); +} + +#define WRONG_SHOW_MIN 5 + +/* + * Output text: + * on screen => DrawTextArray + * on printer, !Kerning => DrawText + * on printer + Kerning => DrawStretchText + */ +static sal_uInt8 lcl_WhichPunctuation( sal_Unicode cChar ) +{ + if ( ( cChar < 0x3001 || cChar > 0x3002 ) && + ( cChar < 0x3008 || cChar > 0x3011 ) && + ( cChar < 0x3014 || cChar > 0x301F ) && + 0xFF62 != cChar && 0xFF63 != cChar ) + // no punctuation + return SwScriptInfo::NONE; + else if ( 0x3001 == cChar || 0x3002 == cChar || + 0x3009 == cChar || 0x300B == cChar || + 0x300D == cChar || 0x300F == cChar || + 0x3011 == cChar || 0x3015 == cChar || + 0x3017 == cChar || 0x3019 == cChar || + 0x301B == cChar || 0x301E == cChar || + 0x301F == cChar || 0xFF63 == cChar ) + // right punctuation + return SwScriptInfo::SPECIAL_RIGHT; + + return SwScriptInfo::SPECIAL_LEFT; +} + +static bool lcl_IsMonoSpaceFont( const vcl::RenderContext& rOut ) +{ + const long nWidth1 = rOut.GetTextWidth( OUString( u'\x3008' ) ); + const long nWidth2 = rOut.GetTextWidth( OUString( u'\x307C' ) ); + return nWidth1 == nWidth2; +} + +static bool lcl_IsFullstopCentered( const vcl::RenderContext& rOut ) +{ + const FontMetric aMetric( rOut.GetFontMetric() ); + return aMetric.IsFullstopCentered() ; +} + +/* This helper structure (SwForbidden) contains the already marked parts of the string + to avoid double lines (e.g grammar + spell check error) */ + +typedef std::vector> SwForbidden; + +static void lcl_DrawLineForWrongListData( + SwForbidden &rForbidden, + const SwDrawTextInfo &rInf, + sw::WrongListIterator *pWList, + const CalcLinePosData &rCalcLinePosData, + const Size &rPrtFontSize ) +{ + if (!pWList) return; + + TextFrameIndex nStart = rInf.GetIdx(); + TextFrameIndex nWrLen = rInf.GetLen(); + + // check if respective data is available in the current text range + if (!pWList->Check( nStart, nWrLen )) + { + return; + } + + long nHght = rInf.GetOut().LogicToPixel( rPrtFontSize ).Height(); + + // Draw wavy lines for spell and grammar errors only if font is large enough. + // Lines for smart tags will always be drawn. + if (pWList != rInf.GetSmartTags() && WRONG_SHOW_MIN >= nHght) + { + return; + } + + SwForbidden::iterator pIter = rForbidden.begin(); + if (rInf.GetOut().GetConnectMetaFile()) + rInf.GetOut().Push(); + + const Color aCol( rInf.GetOut().GetLineColor() ); + + // iterate over all ranges stored in the respective SwWrongList + do + { + nStart -= rInf.GetIdx(); + + const TextFrameIndex nEnd = nStart + nWrLen; + TextFrameIndex nNext = nStart; + while( nNext < nEnd ) + { + while( pIter != rForbidden.end() && pIter->second <= nNext ) + ++pIter; + + const TextFrameIndex nNextStart = nNext; + TextFrameIndex nNextEnd = nEnd; + + if( pIter == rForbidden.end() || nNextEnd <= pIter->first ) + { + // No overlapping mark up found + rForbidden.insert(pIter, std::make_pair(nNextStart, nNextEnd)); + pIter = rForbidden.begin(); + nNext = nEnd; + } + else + { + nNext = pIter->second; + if( nNextStart < pIter->first ) + { + nNextEnd = pIter->first; + pIter->first = nNextStart; + } + else + continue; + } + // determine line pos + Point aStart( rInf.GetPos() ); + Point aEnd; + lcl_calcLinePos( rCalcLinePosData, aStart, aEnd, nNextStart, nNextEnd - nNextStart ); + + SwWrongArea const*const wrongArea = pWList->GetWrongElement(nNextStart + rInf.GetIdx()); + if (wrongArea != nullptr) + { + if (WRONGAREA_WAVE == wrongArea->mLineType) + { + vcl::ScopedAntialiasing a(rInf.GetOut(), true); + rInf.GetOut().SetLineColor( wrongArea->mColor ); + rInf.GetOut().DrawWaveLine( aStart, aEnd, 1 ); + } + else if (WRONGAREA_BOLDWAVE == wrongArea->mLineType) + { + vcl::ScopedAntialiasing a(rInf.GetOut(), true); + rInf.GetOut().SetLineColor( wrongArea->mColor ); + rInf.GetOut().DrawWaveLine( aStart, aEnd, 2 ); + } + else if (WRONGAREA_BOLD == wrongArea->mLineType) + { + rInf.GetOut().SetLineColor( wrongArea->mColor ); + + aStart.AdjustY(30 ); + aEnd.AdjustY(30 ); + + LineInfo aLineInfo( LineStyle::Solid, 26 ); + + rInf.GetOut().DrawLine( aStart, aEnd, aLineInfo ); + } + else if (WRONGAREA_DASHED == wrongArea->mLineType) + { + rInf.GetOut().SetLineColor( wrongArea->mColor ); + + aStart.AdjustY(30 ); + aEnd.AdjustY(30 ); + + LineInfo aLineInfo( LineStyle::Dash ); + aLineInfo.SetDistance( 40 ); + aLineInfo.SetDashLen( 1 ); + aLineInfo.SetDashCount(1); + + rInf.GetOut().DrawLine( aStart, aEnd, aLineInfo ); + } + } + } + + nStart = nEnd + rInf.GetIdx(); + nWrLen = rInf.GetIdx() + rInf.GetLen() - nStart; + } + while (nWrLen && pWList->Check( nStart, nWrLen )); + + rInf.GetOut().SetLineColor( aCol ); + + if (rInf.GetOut().GetConnectMetaFile()) + rInf.GetOut().Pop(); +} + +void SwFntObj::DrawText( SwDrawTextInfo &rInf ) +{ + OSL_ENSURE( rInf.GetShell(), "SwFntObj::DrawText without shell" ); + + OutputDevice& rRefDev = rInf.GetShell()->GetRefDev(); + OutputDevice* pWin = rInf.GetShell()->GetWin(); + + // true if pOut is the printer and the printer has been used for formatting + const bool bPrt = OUTDEV_PRINTER == rInf.GetOut().GetOutDevType() && + OUTDEV_PRINTER == rRefDev.GetOutDevType(); + const bool bBrowse = ( pWin && + rInf.GetShell()->GetViewOptions()->getBrowseMode() && + !rInf.GetShell()->GetViewOptions()->IsPrtFormat() && + !rInf.GetBullet() && + ( rInf.GetSpace() || !rInf.GetKern() ) && + !rInf.GetWrong() && + !rInf.GetGrammarCheck() && + !rInf.GetSmartTags() && + !rInf.GetGreyWave() ); + + // bDirectPrint indicates that we can enter the branch which calls + // the DrawText functions instead of calling the DrawTextArray functions + const bool bDirectPrint = bPrt || bBrowse; + + // Condition for output font / refdev font adjustment + const bool bUseScrFont = + lcl_IsFontAdjustNecessary( rInf.GetOut(), rRefDev ); + + vcl::Font* pTmpFont = bUseScrFont ? m_pScrFont : m_pPrtFont; + + // bDirectPrint and bUseScrFont should have these values: + + // Outdev / RefDef | Printer | VirtPrinter | Window + + // Printer | 1 - 0 | 0 - 1 | - + + // VirtPrinter/PDF | 0 - 1 | 0 - 1 | - + + // Window/VirtWindow| 0 - 1 | 0 - 1 | 1 - 0 + + // Exception: During painting of a Writer OLE object, we do not have + // a window. Therefore bUseSrcFont is always 0 in this case. + +#if OSL_DEBUG_LEVEL > 0 + + const bool bNoAdjust = bPrt || + ( pWin && + rInf.GetShell()->GetViewOptions()->getBrowseMode() && + !rInf.GetShell()->GetViewOptions()->IsPrtFormat() ); + + if ( OUTDEV_PRINTER == rInf.GetOut().GetOutDevType() ) + { + // Printer output + if ( OUTDEV_PRINTER == rRefDev.GetOutDevType() ) + { + OSL_ENSURE( bNoAdjust && !bUseScrFont, "Outdev Check failed" ); + } + else if ( rRefDev.IsVirtual() ) + { + OSL_ENSURE( !bNoAdjust && bUseScrFont, "Outdev Check failed" ); + } + else + { + OSL_FAIL( "Outdev Check failed" ); + } + } + else if ( rInf.GetOut().IsVirtual() && ! pWin ) + { + // PDF export + if ( OUTDEV_PRINTER == rRefDev.GetOutDevType() ) + { + OSL_ENSURE( !bNoAdjust && bUseScrFont, "Outdev Check failed" ); + } + else if ( rRefDev.IsVirtual() ) + { + OSL_ENSURE( !bNoAdjust && bUseScrFont, "Outdev Check failed" ); + } + else + { + OSL_FAIL( "Outdev Check failed" ); + } + } + else if ( OUTDEV_WINDOW == rInf.GetOut().GetOutDevType() || + ( rInf.GetOut().IsVirtual() && pWin ) ) + { + // Window or virtual window + if ( OUTDEV_PRINTER == rRefDev.GetOutDevType() ) + { + OSL_ENSURE( !bNoAdjust && bUseScrFont, "Outdev Check failed" ); + } + else if ( rRefDev.IsVirtual() ) + { + OSL_ENSURE( !bNoAdjust && bUseScrFont, "Outdev Check failed" ); + } + else if ( OUTDEV_WINDOW == rRefDev.GetOutDevType() ) + { + OSL_ENSURE( bNoAdjust && !bUseScrFont, "Outdev Check failed" ); + } + else + { + OSL_FAIL( "Outdev Check failed" ); + } + } + else + { + OSL_FAIL( "Outdev Check failed" ); + } + +#endif + + // robust: better use the printer font instead of using no font at all + OSL_ENSURE( pTmpFont, "No screen or printer font?" ); + if ( ! pTmpFont ) + pTmpFont = m_pPrtFont; + + // HACK: LINESTYLE_WAVE must not be abused any more, hence the grey wave + // line of the ExtendedAttributeSets will appear in the font color first + + const bool bSwitchH2V = rInf.GetFrame() && rInf.GetFrame()->IsVertical(); + const bool bSwitchH2VLRBT = rInf.GetFrame() && rInf.GetFrame()->IsVertLRBT(); + const bool bSwitchL2R = rInf.GetFrame() && rInf.GetFrame()->IsRightToLeft() && + ! rInf.IsIgnoreFrameRTL(); + const ComplexTextLayoutFlags nMode = rInf.GetOut().GetLayoutMode(); + const bool bBidiPor = ( bSwitchL2R != + ( ComplexTextLayoutFlags::Default != ( ComplexTextLayoutFlags::BiDiRtl & nMode ) ) ); + + // be sure to have the correct layout mode at the printer + if ( m_pPrinter ) + { + m_pPrinter->SetLayoutMode( rInf.GetOut().GetLayoutMode() ); + m_pPrinter->SetDigitLanguage( rInf.GetOut().GetDigitLanguage() ); + } + + Point aTextOriginPos( rInf.GetPos() ); + if( !bPrt ) + { + if( rInf.GetpOut() != *s_pFntObjPixOut.get() || rInf.GetOut().GetMapMode() != *pPixMap ) + { + *pPixMap = rInf.GetOut().GetMapMode(); + (*s_pFntObjPixOut.get()) = rInf.GetpOut(); + Size aTmp( 1, 1 ); + nPixWidth = rInf.GetOut().PixelToLogic( aTmp ).Width(); + } + + aTextOriginPos.AdjustX(rInf.GetFrame()->IsRightToLeft() ? 0 : nPixWidth ); + } + + Color aOldColor( pTmpFont->GetColor() ); + bool bChgColor = rInf.ApplyAutoColor( pTmpFont ); + if( !pTmpFont->IsSameInstance( rInf.GetOut().GetFont() ) ) + rInf.GetOut().SetFont( *pTmpFont ); + if ( bChgColor ) + pTmpFont->SetColor( aOldColor ); + + if (TextFrameIndex(COMPLETE_STRING) == rInf.GetLen()) + rInf.SetLen( TextFrameIndex(rInf.GetText().getLength()) ); + + // ASIAN LINE AND CHARACTER GRID MODE START + + if ( rInf.GetFrame() && rInf.SnapToGrid() && rInf.GetFont() && + SwFontScript::CJK == rInf.GetFont()->GetActual() ) + { + SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame())); + + // ASIAN LINE AND CHARACTER GRID MODE: Do we want to snap asian characters to the grid? + if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && pGrid->IsSnapToChars()) + { + //for textgrid refactor + //const sal_uInt16 nGridWidth = pGrid->GetBaseHeight(); + const SwDoc* pDoc = rInf.GetShell()->GetDoc(); + const sal_uInt16 nGridWidth = GetGridWidth(*pGrid, *pDoc); + + // kerning array - gives the absolute position of end of each character + std::unique_ptr pKernArray(new long[sal_Int32(rInf.GetLen())]); + + if ( m_pPrinter ) + m_pPrinter->GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + else + rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + + // Change the average width per character to an appropriate grid width + // basically get the ratio of the avg width to the grid unit width, then + // multiple this ratio to give the new avg width - which in this case + // gives a new grid width unit size + + long nAvgWidthPerChar = pKernArray[sal_Int32(rInf.GetLen()) - 1] / sal_Int32(rInf.GetLen()); + + const sal_uLong nRatioAvgWidthCharToGridWidth = nAvgWidthPerChar ? + ( nAvgWidthPerChar - 1 ) / nGridWidth + 1: + 1; + + nAvgWidthPerChar = nRatioAvgWidthCharToGridWidth * nGridWidth; + + // the absolute end position of the first character is also its width + long nCharWidth = pKernArray[ 0 ]; + sal_uLong nHalfWidth = nAvgWidthPerChar / 2; + + long nNextFix=0; + + // we work out the start position (origin) of the first character, + // and we set the next "fix" offset to half the width of the char. + // The exceptions are for punctuation characters that are not centered + // so in these cases we just add half a regular "average" character width + // to the first characters actual width to allow the next character to + // be centered automatically + // If the character is "special right", then the offset is correct already + // so the fix offset is as normal - half the average character width + + sal_Unicode cChar = rInf.GetText()[ sal_Int32(rInf.GetIdx()) ]; + sal_uInt8 nType = lcl_WhichPunctuation( cChar ); + switch ( nType ) + { + // centre character + case SwScriptInfo::NONE : + aTextOriginPos.AdjustX(( nAvgWidthPerChar - nCharWidth ) / 2 ); + nNextFix = nCharWidth / 2; + break; + case SwScriptInfo::SPECIAL_RIGHT : + nNextFix = nHalfWidth; + break; + // punctuation + default: + aTextOriginPos.AdjustX(nAvgWidthPerChar - nCharWidth ); + nNextFix = nCharWidth - nHalfWidth; + } + + // calculate offsets + for (sal_Int32 j = 1; j < sal_Int32(rInf.GetLen()); ++j) + { + long nCurrentCharWidth = pKernArray[ j ] - pKernArray[ j - 1 ]; + nNextFix += nAvgWidthPerChar; + + // almost the same as getting the offset for the first character: + // punctuation characters are not centered, so just add half an + // average character width minus the characters actual char width + // to get the offset into the centre of the next character + + cChar = rInf.GetText()[ sal_Int32(rInf.GetIdx()) + j ]; + nType = lcl_WhichPunctuation( cChar ); + switch ( nType ) + { + case SwScriptInfo::NONE : + pKernArray[ j - 1 ] = nNextFix - ( nCurrentCharWidth / 2 ); + break; + case SwScriptInfo::SPECIAL_RIGHT : + pKernArray[ j - 1 ] = nNextFix - nHalfWidth; + break; + default: + pKernArray[ j - 1 ] = nNextFix + nHalfWidth - nCurrentCharWidth; + } + } + + // the layout engine requires the total width of the output + pKernArray[sal_Int32(rInf.GetLen()) - 1] = rInf.GetWidth() - + aTextOriginPos.X() + rInf.GetPos().X() ; + + if ( bSwitchH2V ) + rInf.GetFrame()->SwitchHorizontalToVertical( aTextOriginPos ); + + rInf.GetOut().DrawTextArray( aTextOriginPos, rInf.GetText(), + pKernArray.get(), sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + + return; + } + } + + // For text grid refactor + // ASIAN LINE AND CHARACTER GRID MODE START: not snap to characters + + if ( rInf.GetFrame() && rInf.SnapToGrid() ) + { + SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame())); + + // ASIAN LINE AND CHARACTER GRID MODE - do not snap to characters + if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() ) + { + const long nGridWidthAdd = EvalGridWidthAdd( pGrid, rInf ); + + std::unique_ptr pKernArray(new long[sal_Int32(rInf.GetLen())]); + + if ( m_pPrinter ) + m_pPrinter->GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + else + rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + if ( bSwitchH2V ) + rInf.GetFrame()->SwitchHorizontalToVertical( aTextOriginPos ); + if ( rInf.GetSpace() || rInf.GetKanaComp()) + { + long nSpaceAdd = rInf.GetSpace() / SPACING_PRECISION_FACTOR; + if ( rInf.GetFont() && rInf.GetLen() ) + { + bool bSpecialJust = false; + const SwScriptInfo* pSI = rInf.GetScriptInfo(); + const SwFontScript nActual = rInf.GetFont()->GetActual(); + ///Kana Compression + if( SwFontScript::CJK == nActual && rInf.GetKanaComp() && + pSI && pSI->CountCompChg() && + lcl_IsMonoSpaceFont( *(rInf.GetpOut()) ) ) + { + pSI->Compress( pKernArray.get(), rInf.GetIdx(), rInf.GetLen(), + rInf.GetKanaComp(), static_cast(m_aFont.GetFontSize().Height()), lcl_IsFullstopCentered( rInf.GetOut() ) , &aTextOriginPos ); + bSpecialJust = true; + } + ///Asian Justification + if ( ( SwFontScript::CJK == nActual || SwFontScript::Latin == nActual ) && nSpaceAdd ) + { + LanguageType aLang = rInf.GetFont()->GetLanguage( SwFontScript::CJK ); + if (!MsLangId::isKorean(aLang)) + { + long nSpaceSum = nSpaceAdd; + for (sal_Int32 nI = 0; nI < sal_Int32(rInf.GetLen()); ++nI) + { + pKernArray[ nI ] += nSpaceSum; + nSpaceSum += nSpaceAdd; + } + bSpecialJust = true; + nSpaceAdd = 0; + } + } + long nGridAddSum = nGridWidthAdd; + for (sal_Int32 i = 0; i < sal_Int32(rInf.GetLen()); i++, nGridAddSum += nGridWidthAdd ) + { + pKernArray[i] += nGridAddSum; + } + long nKernSum = rInf.GetKern(); + if ( bSpecialJust || rInf.GetKern() ) + { + for (sal_Int32 i = 0; i < sal_Int32(rInf.GetLen()); i++, nKernSum += rInf.GetKern()) + { + if (CH_BLANK == rInf.GetText()[sal_Int32(rInf.GetIdx())+i]) + nKernSum += nSpaceAdd; + pKernArray[i] += nKernSum; + } + ///With through/uderstr. Grouped style requires a blank at the end + ///of a text edition special measures: + if( m_bPaintBlank && rInf.GetLen() && (CH_BLANK == + rInf.GetText()[sal_Int32(rInf.GetIdx() + rInf.GetLen()) - 1])) + { + ///If it concerns a singular, underlined space acts, + ///we must spend two: + if (TextFrameIndex(1) == rInf.GetLen()) + { + pKernArray[0] = rInf.GetWidth() + nSpaceAdd; + rInf.GetOut().DrawTextArray( aTextOriginPos, rInf.GetText(), + pKernArray.get(), sal_Int32(rInf.GetIdx()), 1); + } + else + { + pKernArray[sal_Int32(rInf.GetLen()) - 2] += nSpaceAdd; + rInf.GetOut().DrawTextArray( aTextOriginPos, rInf.GetText(), + pKernArray.get(), sal_Int32(rInf.GetIdx()), + sal_Int32(rInf.GetLen())); + } + } + else + { + rInf.GetOut().DrawTextArray( aTextOriginPos, rInf.GetText(), + pKernArray.get(), sal_Int32(rInf.GetIdx()), + sal_Int32(rInf.GetLen())); + } + } + else + { + sal_Int32 i; + long nSpaceSum = 0; + for (i = 0; i < sal_Int32(rInf.GetLen()); i++) + { + if(CH_BLANK == rInf.GetText()[sal_Int32(rInf.GetIdx()) + i]) + nSpaceSum += nSpaceAdd + nKernSum; + + pKernArray[i] += nSpaceSum; + } + + rInf.GetOut().DrawTextArray(aTextOriginPos, + rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), + sal_Int32(rInf.GetLen())); + } + } + } + else + { + //long nKernAdd = rInf.GetKern(); + long nKernAdd = 0; + long nGridAddSum = nGridWidthAdd + nKernAdd; + for (sal_Int32 i = 0; i < sal_Int32(rInf.GetLen()); + i++, nGridAddSum += nGridWidthAdd + nKernAdd) + { + pKernArray[i] += nGridAddSum; + } + rInf.GetOut().DrawTextArray( aTextOriginPos, rInf.GetText(), + pKernArray.get(), sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + } + return; + } + } + + // DIRECT PAINTING WITHOUT SCREEN ADJUSTMENT + + if ( bDirectPrint ) + { + const Fraction aTmp( 1, 1 ); + bool bStretch = rInf.GetWidth() && (rInf.GetLen() > TextFrameIndex(1)) && bPrt + && ( aTmp != rInf.GetOut().GetMapMode().GetScaleX() ); + + if ( bSwitchL2R ) + rInf.GetFrame()->SwitchLTRtoRTL( aTextOriginPos ); + + if ( bSwitchH2V ) + rInf.GetFrame()->SwitchHorizontalToVertical( aTextOriginPos ); + + // In the good old days we used to have a simple DrawText if the + // output device is the printer. Now we need a DrawTextArray if + // 1. KanaCompression is enabled + // 2. Justified alignment + // Simple kerning is handled by DrawStretchText + if( rInf.GetSpace() || rInf.GetKanaComp() ) + { + std::unique_ptr pKernArray(new long[sal_Int32(rInf.GetLen())]); + rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + + if( bStretch ) + { + sal_Int32 nZwi = sal_Int32(rInf.GetLen()) - 1; + long nDiff = rInf.GetWidth() - pKernArray[ nZwi ] + - sal_Int32(rInf.GetLen()) * rInf.GetKern(); + long nRest = nDiff % nZwi; + long nAdd; + if( nRest < 0 ) + { + nAdd = -1; + nRest += nZwi; + } + else + { + nAdd = +1; + nRest = nZwi - nRest; + } + nDiff /= nZwi; + long nSum = nDiff; + for( sal_Int32 i = 0; i < nZwi; ) + { + pKernArray[ i ] += nSum; + if( ++i == nRest ) + nDiff += nAdd; + nSum += nDiff; + } + } + + // Modify Array for special justifications + + long nSpaceAdd = rInf.GetSpace() / SPACING_PRECISION_FACTOR; + bool bSpecialJust = false; + + if ( rInf.GetFont() && rInf.GetLen() ) + { + const SwScriptInfo* pSI = rInf.GetScriptInfo(); + const SwFontScript nActual = rInf.GetFont()->GetActual(); + + // Kana Compression + if ( SwFontScript::CJK == nActual && rInf.GetKanaComp() && + pSI && pSI->CountCompChg() && + lcl_IsMonoSpaceFont( rInf.GetOut() ) ) + { + pSI->Compress( pKernArray.get(), rInf.GetIdx(), rInf.GetLen(), + rInf.GetKanaComp(), + static_cast(m_aFont.GetFontSize().Height()), lcl_IsFullstopCentered( rInf.GetOut() ), &aTextOriginPos ); + bSpecialJust = true; + } + + // Asian Justification + if ( SwFontScript::CJK == nActual && nSpaceAdd ) + { + LanguageType aLang = rInf.GetFont()->GetLanguage( SwFontScript::CJK ); + + if (!MsLangId::isKorean(aLang)) + { + SwScriptInfo::CJKJustify( rInf.GetText(), pKernArray.get(), nullptr, + rInf.GetIdx(), rInf.GetLen(), aLang, nSpaceAdd, rInf.IsSpaceStop() ); + + bSpecialJust = true; + nSpaceAdd = 0; + } + } + + // Kashida Justification + if ( SwFontScript::CTL == nActual && nSpaceAdd ) + { + if ( SwScriptInfo::IsArabicText( rInf.GetText(), rInf.GetIdx(), rInf.GetLen() ) ) + { + if ( pSI && pSI->CountKashida() && + pSI->KashidaJustify( pKernArray.get(), nullptr, rInf.GetIdx(), + rInf.GetLen(), nSpaceAdd ) != -1 ) + { + bSpecialJust = true; + nSpaceAdd = 0; + } + } + } + + // Thai Justification + if ( SwFontScript::CTL == nActual && nSpaceAdd ) + { + LanguageType aLang = rInf.GetFont()->GetLanguage( SwFontScript::CTL ); + + if ( LANGUAGE_THAI == aLang ) + { + // Use rInf.GetSpace() because it has more precision than + // nSpaceAdd: + SwScriptInfo::ThaiJustify( rInf.GetText(), pKernArray.get(), nullptr, + rInf.GetIdx(), rInf.GetLen(), + rInf.GetNumberOfBlanks(), + rInf.GetSpace() ); + + // adding space to blanks is already done + bSpecialJust = true; + nSpaceAdd = 0; + } + } + } + + long nKernSum = rInf.GetKern(); + + if ( bStretch || m_bPaintBlank || rInf.GetKern() || bSpecialJust ) + { + for (sal_Int32 i = 0; i < sal_Int32(rInf.GetLen()); i++, + nKernSum += rInf.GetKern() ) + { + if (CH_BLANK == rInf.GetText()[sal_Int32(rInf.GetIdx()) + i]) + nKernSum += nSpaceAdd; + pKernArray[i] += nKernSum; + } + + // In case of underlined/strike-through justified text + // a blank at the end requires special handling: + if( m_bPaintBlank && rInf.GetLen() && ( CH_BLANK == + rInf.GetText()[sal_Int32(rInf.GetIdx() + rInf.GetLen())-1])) + { + // If it is a single underlined space, output 2 spaces: + if (TextFrameIndex(1) == rInf.GetLen()) + { + pKernArray[0] = rInf.GetWidth() + nSpaceAdd; + + rInf.GetOut().DrawTextArray( aTextOriginPos, rInf.GetText(), + pKernArray.get(), sal_Int32(rInf.GetIdx()), 1 ); + } + else + { + pKernArray[ sal_Int32(rInf.GetLen()) - 2 ] += nSpaceAdd; + rInf.GetOut().DrawTextArray( aTextOriginPos, rInf.GetText(), + pKernArray.get(), sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + } + } + else + rInf.GetOut().DrawTextArray( aTextOriginPos, rInf.GetText(), + pKernArray.get(), sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + } + else + { + Point aTmpPos( aTextOriginPos ); + sal_Int32 j = 0; + sal_Int32 i; + for( i = 0; i < sal_Int32(rInf.GetLen()); i++ ) + { + if (CH_BLANK == rInf.GetText()[sal_Int32(rInf.GetIdx()) + i]) + { + nKernSum += nSpaceAdd; + if( j < i ) + rInf.GetOut().DrawText( aTmpPos, rInf.GetText(), + sal_Int32(rInf.GetIdx()) + j, i - j); + j = i + 1; + SwTwips nAdd = pKernArray[ i ] + nKernSum; + if ( ( ComplexTextLayoutFlags::BiDiStrong | ComplexTextLayoutFlags::BiDiRtl ) == nMode ) + nAdd *= -1; + aTmpPos.setX( aTextOriginPos.X() + nAdd ); + } + } + if( j < i ) + rInf.GetOut().DrawText( aTmpPos, rInf.GetText(), + sal_Int32(rInf.GetIdx()) + j, i - j); + } + } + else if( bStretch ) + { + long nTmpWidth = rInf.GetWidth(); + if( rInf.GetKern() && rInf.GetLen() && nTmpWidth > rInf.GetKern() ) + nTmpWidth -= rInf.GetKern(); + rInf.GetOut().DrawStretchText( aTextOriginPos, nTmpWidth, + rInf.GetText(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + } + else if( rInf.GetKern() ) + { + const long nTmpWidth = GetTextSize( rInf ).Width(); + + const Color aSaveColor( pTmpFont->GetColor() ); + const bool bColorChanged = rInf.ApplyAutoColor( pTmpFont ); + + if( bColorChanged ) + { + if( !pTmpFont->IsSameInstance( rInf.GetOut().GetFont() ) ) + rInf.GetOut().SetFont( *pTmpFont ); + pTmpFont->SetColor( aSaveColor ); + } + + rInf.GetOut().DrawStretchText( aTextOriginPos, nTmpWidth, + rInf.GetText(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + } + else + rInf.GetOut().DrawText( aTextOriginPos, rInf.GetText(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + } + + // PAINTING WITH FORMATTING DEVICE/SCREEN ADJUSTMENT + + else + { + const OUString* pStr = &rInf.GetText(); + + OUString aStr; + OUString aBulletOverlay; + bool bBullet = rInf.GetBullet(); + if( m_bSymbol ) + bBullet = false; + std::unique_ptr pKernArray(new long[sal_Int32(rInf.GetLen())]); + CreateScrFont( *rInf.GetShell(), rInf.GetOut() ); + long nScrPos; + + // get screen array + std::unique_ptr pScrArray(new long[sal_Int32(rInf.GetLen())]); + SwTextGlyphsKey aGlyphsKey{ &rInf.GetOut(), rInf.GetText(), sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen()) }; + SalLayoutGlyphs* pGlyphs = lcl_CreateLayout(aGlyphsKey, m_aTextGlyphs[aGlyphsKey]); + rInf.GetOut().GetTextArray( rInf.GetText(), pScrArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen()), nullptr, pGlyphs); + + // OLE: no printer available + // OSL_ENSURE( pPrinter, "DrawText needs pPrinter" ) + if ( m_pPrinter ) + { + // pTmpFont has already been set as current font for rInf.GetOut() + if ( m_pPrinter.get() != rInf.GetpOut() || pTmpFont != m_pPrtFont ) + { + if( !m_pPrtFont->IsSameInstance( m_pPrinter->GetFont() ) ) + m_pPrinter->SetFont( *m_pPrtFont ); + } + aGlyphsKey = SwTextGlyphsKey{ m_pPrinter, rInf.GetText(), sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen()) }; + pGlyphs = lcl_CreateLayout(aGlyphsKey, m_aTextGlyphs[aGlyphsKey]); + m_pPrinter->GetTextArray(rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen()), nullptr, pGlyphs); + } + else + { + rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + } + + // Modify Printer and ScreenArrays for special justifications + + long nSpaceAdd = rInf.GetSpace() / SPACING_PRECISION_FACTOR; + bool bNoHalfSpace = false; + + if ( rInf.GetFont() && rInf.GetLen() ) + { + const SwFontScript nActual = rInf.GetFont()->GetActual(); + const SwScriptInfo* pSI = rInf.GetScriptInfo(); + + // Kana Compression + if ( SwFontScript::CJK == nActual && rInf.GetKanaComp() && + pSI && pSI->CountCompChg() && + lcl_IsMonoSpaceFont( rInf.GetOut() ) ) + { + Point aTmpPos( aTextOriginPos ); + pSI->Compress( pScrArray.get(), rInf.GetIdx(), rInf.GetLen(), + rInf.GetKanaComp(), + static_cast(m_aFont.GetFontSize().Height()), lcl_IsFullstopCentered( rInf.GetOut() ), &aTmpPos ); + pSI->Compress( pKernArray.get(), rInf.GetIdx(), rInf.GetLen(), + rInf.GetKanaComp(), + static_cast(m_aFont.GetFontSize().Height()), lcl_IsFullstopCentered( rInf.GetOut() ), &aTextOriginPos ); + } + + // Asian Justification + if ( SwFontScript::CJK == nActual && nSpaceAdd ) + { + LanguageType aLang = rInf.GetFont()->GetLanguage( SwFontScript::CJK ); + + if (!MsLangId::isKorean(aLang)) + { + SwScriptInfo::CJKJustify( rInf.GetText(), pKernArray.get(), pScrArray.get(), + rInf.GetIdx(), rInf.GetLen(), aLang, nSpaceAdd, rInf.IsSpaceStop() ); + + nSpaceAdd = 0; + } + } + + // Kashida Justification + if ( SwFontScript::CTL == nActual && nSpaceAdd ) + { + if ( SwScriptInfo::IsArabicText( rInf.GetText(), rInf.GetIdx(), rInf.GetLen() ) ) + { + if ( pSI && pSI->CountKashida() && + pSI->KashidaJustify( pKernArray.get(), pScrArray.get(), rInf.GetIdx(), + rInf.GetLen(), nSpaceAdd ) != -1 ) + nSpaceAdd = 0; + else + bNoHalfSpace = true; + } + } + + // Thai Justification + if ( SwFontScript::CTL == nActual && nSpaceAdd ) + { + LanguageType aLang = rInf.GetFont()->GetLanguage( SwFontScript::CTL ); + + if ( LANGUAGE_THAI == aLang ) + { + SwScriptInfo::ThaiJustify( rInf.GetText(), pKernArray.get(), + pScrArray.get(), rInf.GetIdx(), + rInf.GetLen(), + rInf.GetNumberOfBlanks(), + rInf.GetSpace() ); + + // adding space to blanks is already done + nSpaceAdd = 0; + } + } + } + + nScrPos = pScrArray[ 0 ]; + + if( bBullet ) + { + // !!! HACK !!! + // The Arabic layout engine requires some context of the string + // which should be painted. + sal_Int32 nCopyStart = sal_Int32(rInf.GetIdx()); + if ( nCopyStart ) + --nCopyStart; + + sal_Int32 nCopyLen = sal_Int32(rInf.GetLen()); + if ( nCopyStart + nCopyLen < rInf.GetText().getLength() ) + ++nCopyLen; + + aStr = rInf.GetText().copy( nCopyStart, nCopyLen ); + pStr = &aStr; + + aBulletOverlay = rInf.GetText().copy( nCopyStart, nCopyLen ); + + for( sal_Int32 i = 0; i < aBulletOverlay.getLength(); ++i ) + if( CH_BLANK == aBulletOverlay[ i ] ) + { + /* fdo#72488 Hack: try to see if the space is zero width + * and don't bother with inserting a bullet in this case. + */ + if ((i + nCopyStart + 1 >= sal_Int32(rInf.GetLen())) || + pKernArray[i + nCopyStart] != pKernArray[ i + nCopyStart + 1]) + { + aBulletOverlay = aBulletOverlay.replaceAt(i, 1, OUString(CH_BULLET)); + } + else + { + aBulletOverlay = aBulletOverlay.replaceAt(i, 1, OUString(CH_BLANK)); + } + } + else + { + aBulletOverlay = aBulletOverlay.replaceAt(i, 1, OUString(CH_BLANK)); + } + } + + TextFrameIndex nCnt(rInf.GetText().getLength()); + if ( nCnt < rInf.GetIdx() ) + assert(false); // layout bug, not handled below + else + nCnt = nCnt - rInf.GetIdx(); + nCnt = std::min(nCnt, rInf.GetLen()); + long nKernSum = rInf.GetKern(); + sal_Unicode cChPrev = rInf.GetText()[sal_Int32(rInf.GetIdx())]; + + // In case of a single underlined space in justified text, + // have to output 2 spaces: + if ((nCnt == TextFrameIndex(1)) && rInf.GetSpace() && (cChPrev == CH_BLANK)) + { + pKernArray[0] = rInf.GetWidth() + + rInf.GetKern() + + ( rInf.GetSpace() / SPACING_PRECISION_FACTOR ); + + if ( bSwitchL2R ) + rInf.GetFrame()->SwitchLTRtoRTL( aTextOriginPos ); + + if ( bSwitchH2V ) + rInf.GetFrame()->SwitchHorizontalToVertical( aTextOriginPos ); + + rInf.GetOut().DrawTextArray( aTextOriginPos, rInf.GetText(), + pKernArray.get(), sal_Int32(rInf.GetIdx()), 1 ); + if( bBullet ) + rInf.GetOut().DrawTextArray( aTextOriginPos, *pStr, pKernArray.get(), + rInf.GetIdx() ? 1 : 0, 1 ); + } + else + { + sal_Unicode nCh; + + // In case of Pair Kerning the printer influence on the positioning + // grows + const int nMul = m_pPrtFont->GetKerning() != FontKerning::NONE ? 1 : 3; + const int nDiv = nMul+1; + + // nSpaceSum contains the sum of the intermediate space distributed + // among Spaces by the Justification. + // The Spaces themselves will be positioned in the middle of the + // intermediate space, hence the nSpace/2. + // In case of word-by-word underlining they have to be positioned + // at the beginning of the intermediate space, so that the space + // is not underlined. + // A Space at the beginning or end of the text must be positioned + // before (resp. after) the whole intermediate space, otherwise + // the underline/strike-through would have gaps. + long nSpaceSum = 0; + // in word line mode and for Arabic, we disable the half space trick: + const long nHalfSpace = m_pPrtFont->IsWordLineMode() || bNoHalfSpace ? 0 : nSpaceAdd / 2; + const long nOtherHalf = nSpaceAdd - nHalfSpace; + if ( nSpaceAdd && ( cChPrev == CH_BLANK ) ) + nSpaceSum = nHalfSpace; + for (sal_Int32 i = 1; i < sal_Int32(nCnt); ++i, nKernSum += rInf.GetKern()) + { + nCh = rInf.GetText()[sal_Int32(rInf.GetIdx()) + i]; + + OSL_ENSURE( pScrArray, "Where is the screen array?" ); + long nScr; + nScr = pScrArray[ i ] - pScrArray[ i - 1 ]; + + // If there is an (ex-)Space before us, position optimally, + // i.e., our right margin to the 100% printer position; + // if we _are_ an ex-Space, position us left-aligned to the + // printer position. + if ( nCh == CH_BLANK ) + { + nScrPos = pKernArray[i-1] + nScr; + + if ( cChPrev == CH_BLANK ) + nSpaceSum += nOtherHalf; + if (i + 1 == sal_Int32(nCnt)) + nSpaceSum += nSpaceAdd; + else + nSpaceSum += nHalfSpace; + } + else + { + if ( cChPrev == CH_BLANK ) + { + nScrPos = pKernArray[i-1] + nScr; + // no Pixel is lost: + nSpaceSum += nOtherHalf; + } + else if ( cChPrev == '-' ) + nScrPos = pKernArray[i-1] + nScr; + else + { + nScrPos += nScr; + nScrPos = ( nMul * nScrPos + pKernArray[i] ) / nDiv; + } + } + cChPrev = nCh; + pKernArray[i-1] = nScrPos - nScr + nKernSum + nSpaceSum; + // In word line mode and for Arabic, we disabled the half space trick. If a portion + // ends with a blank, the full nSpaceAdd value has been added to the character in + // front of the blank. This leads to painting artifacts, therefore we remove the + // nSpaceAdd value again: + if ((bNoHalfSpace || m_pPrtFont->IsWordLineMode()) && i+1 == sal_Int32(nCnt) && nCh == CH_BLANK) + pKernArray[i-1] = pKernArray[i-1] - nSpaceAdd; + } + + // the layout engine requires the total width of the output + pKernArray[sal_Int32(rInf.GetLen()) - 1] += nKernSum + nSpaceSum; + + if( rInf.GetGreyWave() ) + { + if( rInf.GetLen() ) + { + long nHght = rInf.GetOut().LogicToPixel( + m_pPrtFont->GetFontSize() ).Height(); + if( WRONG_SHOW_MIN < nHght ) + { + if ( rInf.GetOut().GetConnectMetaFile() ) + rInf.GetOut().Push(); + + Color aCol( rInf.GetOut().GetLineColor() ); + bool bColSave = aCol != gWaveCol; + if ( bColSave ) + rInf.GetOut().SetLineColor( gWaveCol ); + + Point aEnd; + long nKernVal = pKernArray[sal_Int32(rInf.GetLen()) - 1]; + + const sal_uInt16 nDir = bBidiPor + ? 1800 + : UnMapDirection(GetFont().GetOrientation(), + bSwitchH2V, bSwitchH2VLRBT); + + switch ( nDir ) + { + case 0 : + aEnd.setX( rInf.GetPos().X() + nKernVal ); + aEnd.setY( rInf.GetPos().Y() ); + break; + case 900 : + aEnd.setX( rInf.GetPos().X() ); + aEnd.setY( rInf.GetPos().Y() - nKernVal ); + break; + case 1800 : + aEnd.setX( rInf.GetPos().X() - nKernVal ); + aEnd.setY( rInf.GetPos().Y() ); + break; + case 2700 : + aEnd.setX( rInf.GetPos().X() ); + aEnd.setY( rInf.GetPos().Y() + nKernVal ); + break; + } + + Point aCurrPos( rInf.GetPos() ); + + if ( bSwitchL2R ) + { + rInf.GetFrame()->SwitchLTRtoRTL( aCurrPos ); + rInf.GetFrame()->SwitchLTRtoRTL( aEnd ); + } + + if ( bSwitchH2V ) + { + rInf.GetFrame()->SwitchHorizontalToVertical( aCurrPos ); + rInf.GetFrame()->SwitchHorizontalToVertical( aEnd ); + } + { + vcl::ScopedAntialiasing a(rInf.GetOut(), true); + rInf.GetOut().DrawWaveLine( aCurrPos, aEnd ); + } + if ( bColSave ) + rInf.GetOut().SetLineColor( aCol ); + + if ( rInf.GetOut().GetConnectMetaFile() ) + rInf.GetOut().Pop(); + } + } + } + else if( !m_bSymbol && rInf.GetLen() ) + { + // anything to do? + if (rInf.GetWrong() || rInf.GetGrammarCheck() || rInf.GetSmartTags()) + { + CalcLinePosData aCalcLinePosData(rInf, GetFont(), nCnt, bSwitchH2V, + bSwitchH2VLRBT, bSwitchL2R, nHalfSpace, + pKernArray.get(), bBidiPor); + + SwForbidden aForbidden; + // draw line for smart tag data + lcl_DrawLineForWrongListData( aForbidden, rInf, rInf.GetSmartTags(), aCalcLinePosData, Size() ); + // draw wave line for spell check errors + // draw them BEFORE the grammar check lines to 'override' the latter in case of conflict. + // reason: some grammar errors can only be found if spelling errors are fixed, + // therefore we don't want the user to miss a spelling error. + lcl_DrawLineForWrongListData( aForbidden, rInf, rInf.GetWrong(), aCalcLinePosData, m_pPrtFont->GetFontSize() ); + // draw wave line for grammar check errors + lcl_DrawLineForWrongListData( aForbidden, rInf, rInf.GetGrammarCheck(), aCalcLinePosData, m_pPrtFont->GetFontSize() ); + } + } + + sal_Int32 nLen = sal_Int32(rInf.GetLen()); + + if( nLen > 0 ) + { + + if ( bSwitchL2R ) + rInf.GetFrame()->SwitchLTRtoRTL( aTextOriginPos ); + + if ( bSwitchH2V ) + rInf.GetFrame()->SwitchHorizontalToVertical( aTextOriginPos ); + + // If we paint bullets instead of spaces, we use a copy of + // the paragraph string. For the layout engine, the copy + // of the string has to be an environment of the range which + // is painted + sal_Int32 nTmpIdx = bBullet + ? (rInf.GetIdx() ? 1 : 0) + : sal_Int32(rInf.GetIdx()); + aGlyphsKey = SwTextGlyphsKey{ &rInf.GetOut(), *pStr, nTmpIdx, nLen }; + pGlyphs = lcl_CreateLayout(aGlyphsKey, m_aTextGlyphs[aGlyphsKey]); + rInf.GetOut().DrawTextArray( aTextOriginPos, *pStr, pKernArray.get(), + nTmpIdx , nLen, SalLayoutFlags::NONE, pGlyphs ); + if (bBullet) + { + rInf.GetOut().Push(); + Color aPreviousColor = pTmpFont->GetColor(); + + FontLineStyle aPreviousUnderline = pTmpFont->GetUnderline(); + FontLineStyle aPreviousOverline = pTmpFont->GetOverline(); + FontStrikeout aPreviousStrikeout = pTmpFont->GetStrikeout(); + + pTmpFont->SetColor( NON_PRINTING_CHARACTER_COLOR ); + pTmpFont->SetUnderline(LINESTYLE_NONE); + pTmpFont->SetOverline(LINESTYLE_NONE); + pTmpFont->SetStrikeout(STRIKEOUT_NONE); + rInf.GetOut().SetFont( *pTmpFont ); + long nShift = rInf.GetOut( ).GetFontMetric( ).GetBulletOffset( ); + if ( nShift ) + { + long nAdd = 0; + + if (aBulletOverlay.getLength() > nTmpIdx && + aBulletOverlay[ nTmpIdx ] == CH_BULLET ) + { + if (bSwitchH2V) + aTextOriginPos.AdjustY(nShift ) ; + else + aTextOriginPos.AdjustX(nShift ) ; + nAdd = nShift ; + } + for( sal_Int32 i = 1 ; i < nLen ; ++i ) + { + if ( aBulletOverlay[ i + nTmpIdx ] == CH_BULLET ) + pKernArray [ i - 1 ] += nShift ; + if ( nAdd ) + pKernArray [ i - 1 ] -= nAdd; + } + } + rInf.GetOut().DrawTextArray( aTextOriginPos, aBulletOverlay, pKernArray.get(), + nTmpIdx , nLen ); + pTmpFont->SetColor( aPreviousColor ); + + pTmpFont->SetUnderline(aPreviousUnderline); + pTmpFont->SetOverline(aPreviousOverline); + pTmpFont->SetStrikeout(aPreviousStrikeout); + rInf.GetOut().Pop(); + } + } + } + } +} + +Size SwFntObj::GetTextSize( SwDrawTextInfo& rInf ) +{ + Size aTextSize; + const TextFrameIndex nLn = (TextFrameIndex(COMPLETE_STRING) != rInf.GetLen()) + ? rInf.GetLen() + : TextFrameIndex(rInf.GetText().getLength()); + + // be sure to have the correct layout mode at the printer + if ( m_pPrinter ) + { + m_pPrinter->SetLayoutMode( rInf.GetOut().GetLayoutMode() ); + m_pPrinter->SetDigitLanguage( rInf.GetOut().GetDigitLanguage() ); + } + + if ( rInf.GetFrame() && nLn && rInf.SnapToGrid() && rInf.GetFont() && + SwFontScript::CJK == rInf.GetFont()->GetActual() ) + { + SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame())); + if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && pGrid->IsSnapToChars() ) + { + const SwDoc* pDoc = rInf.GetShell()->GetDoc(); + const sal_uInt16 nGridWidth = GetGridWidth(*pGrid, *pDoc); + + OutputDevice* pOutDev; + + if ( m_pPrinter ) + { + if( !m_pPrtFont->IsSameInstance( m_pPrinter->GetFont() ) ) + m_pPrinter->SetFont(*m_pPrtFont); + pOutDev = m_pPrinter; + } + else + pOutDev = rInf.GetpOut(); + + aTextSize.setWidth( pOutDev->GetTextWidth(rInf.GetText(), + sal_Int32(rInf.GetIdx()), sal_Int32(nLn)) ); + + OSL_ENSURE( !rInf.GetShell() || + ( USHRT_MAX != GetGuessedLeading() && USHRT_MAX != GetExternalLeading() ), + "Leading values should be already calculated" ); + aTextSize.setHeight( pOutDev->GetTextHeight() + + GetFontLeading( rInf.GetShell(), rInf.GetOut() ) ); + + long nAvgWidthPerChar = aTextSize.Width() / sal_Int32(nLn); + + const sal_uLong i = nAvgWidthPerChar ? + ( nAvgWidthPerChar - 1 ) / nGridWidth + 1: + 1; + + aTextSize.setWidth(i * nGridWidth * sal_Int32(nLn)); + rInf.SetKanaDiff( 0 ); + return aTextSize; + } + } + + //for textgrid refactor + if ( rInf.GetFrame() && nLn && rInf.SnapToGrid() && rInf.GetFont() ) + { + SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame())); + if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() ) + { + const long nGridWidthAdd = EvalGridWidthAdd( pGrid, rInf ); + OutputDevice* pOutDev; + if ( m_pPrinter ) + { + if( !m_pPrtFont->IsSameInstance( m_pPrinter->GetFont() ) ) + m_pPrinter->SetFont(*m_pPrtFont); + pOutDev = m_pPrinter; + } + else + pOutDev = rInf.GetpOut(); + aTextSize.setWidth(pOutDev->GetTextWidth(rInf.GetText(), + sal_Int32(rInf.GetIdx()), sal_Int32(nLn))); + aTextSize.setHeight( pOutDev->GetTextHeight() + + GetFontLeading( rInf.GetShell(), rInf.GetOut() ) ); + aTextSize.AdjustWidth(sal_Int32(nLn) * nGridWidthAdd); + //if ( rInf.GetKern() && nLn ) + // aTextSize.Width() += ( nLn ) * long( rInf.GetKern() ); + + rInf.SetKanaDiff( 0 ); + return aTextSize; + } + } + + const bool bCompress = rInf.GetKanaComp() && nLn && + rInf.GetFont() && + SwFontScript::CJK == rInf.GetFont()->GetActual() && + rInf.GetScriptInfo() && + rInf.GetScriptInfo()->CountCompChg() && + lcl_IsMonoSpaceFont( rInf.GetOut() ); + + OSL_ENSURE( !bCompress || ( rInf.GetScriptInfo() && rInf.GetScriptInfo()-> + CountCompChg()), "Compression without info" ); + + // This is the part used e.g., for cursor travelling + // See condition for DrawText or DrawTextArray (bDirectPrint) + if ( m_pPrinter && m_pPrinter.get() != rInf.GetpOut() ) + { + if( !m_pPrtFont->IsSameInstance( m_pPrinter->GetFont() ) ) + m_pPrinter->SetFont(*m_pPrtFont); + aTextSize.setWidth( m_pPrinter->GetTextWidth( rInf.GetText(), + sal_Int32(rInf.GetIdx()), sal_Int32(nLn))); + aTextSize.setHeight( m_pPrinter->GetTextHeight() ); + std::unique_ptr pKernArray(new long[sal_Int32(nLn)]); + CreateScrFont( *rInf.GetShell(), rInf.GetOut() ); + if( !GetScrFont()->IsSameInstance( rInf.GetOut().GetFont() ) ) + rInf.GetOut().SetFont( *m_pScrFont ); + long nScrPos; + + m_pPrinter->GetTextArray(rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(nLn)); + if( bCompress ) + rInf.SetKanaDiff( rInf.GetScriptInfo()->Compress( pKernArray.get(), + rInf.GetIdx(), nLn, rInf.GetKanaComp(), + static_cast(m_aFont.GetFontSize().Height()) ,lcl_IsFullstopCentered( rInf.GetOut() ) ) ); + else + rInf.SetKanaDiff( 0 ); + + if ( rInf.GetKanaDiff() ) + nScrPos = pKernArray[ sal_Int32(nLn) - 1 ]; + else + { + std::unique_ptr pScrArray(new long[sal_Int32(rInf.GetLen())]); + rInf.GetOut().GetTextArray( rInf.GetText(), pScrArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + nScrPos = pScrArray[ 0 ]; + TextFrameIndex nCnt(rInf.GetText().getLength()); + if ( nCnt < rInf.GetIdx() ) + nCnt = TextFrameIndex(0); // assert??? + else + nCnt = nCnt - rInf.GetIdx(); + nCnt = std::min(nCnt, nLn); + sal_Unicode nChPrev = rInf.GetText()[ sal_Int32(rInf.GetIdx()) ]; + + sal_Unicode nCh; + + // In case of Pair Kerning the printer influence on the positioning + // grows + const int nMul = m_pPrtFont->GetKerning() != FontKerning::NONE ? 1 : 3; + const int nDiv = nMul+1; + for (sal_Int32 i = 1; i < sal_Int32(nCnt); i++) + { + nCh = rInf.GetText()[ sal_Int32(rInf.GetIdx()) + i ]; + long nScr; + nScr = pScrArray[ i ] - pScrArray[ i - 1 ]; + if ( nCh == CH_BLANK ) + nScrPos = pKernArray[i-1]+nScr; + else + { + if ( nChPrev == CH_BLANK || nChPrev == '-' ) + nScrPos = pKernArray[i-1]+nScr; + else + { + nScrPos += nScr; + nScrPos = ( nMul * nScrPos + pKernArray[i] ) / nDiv; + } + } + nChPrev = nCh; + pKernArray[i-1] = nScrPos - nScr; + } + } + + pKernArray.reset(); + aTextSize.setWidth( nScrPos ); + } + else + { + if( !m_pPrtFont->IsSameInstance( rInf.GetOut().GetFont() ) ) + rInf.GetOut().SetFont( *m_pPrtFont ); + if( bCompress ) + { + std::unique_ptr pKernArray( new long[sal_Int32(nLn)] ); + rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(nLn)); + rInf.SetKanaDiff( rInf.GetScriptInfo()->Compress( pKernArray.get(), + rInf.GetIdx(), nLn, rInf.GetKanaComp(), + static_cast(m_aFont.GetFontSize().Height()) ,lcl_IsFullstopCentered( rInf.GetOut() ) ) ); + aTextSize.setWidth( pKernArray[sal_Int32(nLn) - 1] ); + } + else + { + SwTextGlyphsKey aGlyphsKey{ &rInf.GetOut(), rInf.GetText(), sal_Int32(rInf.GetIdx()), sal_Int32(nLn) }; + SalLayoutGlyphs* pGlyphs = lcl_CreateLayout(aGlyphsKey, m_aTextGlyphs[aGlyphsKey]); + aTextSize.setWidth( rInf.GetOut().GetTextWidth( rInf.GetText(), + sal_Int32(rInf.GetIdx()), sal_Int32(nLn), + rInf.GetVclCache(), pGlyphs) ); + rInf.SetKanaDiff( 0 ); + } + + aTextSize.setHeight( rInf.GetOut().GetTextHeight() ); + } + + if ( rInf.GetKern() && nLn ) + aTextSize.AdjustWidth((sal_Int32(nLn) - 1) * rInf.GetKern()); + + OSL_ENSURE( !rInf.GetShell() || + ( USHRT_MAX != GetGuessedLeading() && USHRT_MAX != GetExternalLeading() ), + "Leading values should be already calculated" ); + aTextSize.AdjustHeight(GetFontLeading( rInf.GetShell(), rInf.GetOut() ) ); + return aTextSize; +} + +TextFrameIndex SwFntObj::GetModelPositionForViewPoint(SwDrawTextInfo &rInf) +{ + long nSpaceAdd = rInf.GetSpace() / SPACING_PRECISION_FACTOR; + const long nSperren = -rInf.GetSperren() / SPACING_PRECISION_FACTOR; + long nKern = rInf.GetKern(); + + if( 0 != nSperren ) + nKern -= nSperren; + + std::unique_ptr pKernArray(new long[sal_Int32(rInf.GetLen())]); + + // be sure to have the correct layout mode at the printer + if ( m_pPrinter ) + { + m_pPrinter->SetLayoutMode( rInf.GetOut().GetLayoutMode() ); + m_pPrinter->SetDigitLanguage( rInf.GetOut().GetDigitLanguage() ); + SwTextGlyphsKey aGlyphsKey{ m_pPrinter, rInf.GetText(), sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen()) }; + SalLayoutGlyphs* pGlyphs = lcl_CreateLayout(aGlyphsKey, m_aTextGlyphs[aGlyphsKey]); + m_pPrinter->GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen()), nullptr, pGlyphs); + } + else + rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + + const SwScriptInfo* pSI = rInf.GetScriptInfo(); + if ( rInf.GetFont() && rInf.GetLen() ) + { + const SwFontScript nActual = rInf.GetFont()->GetActual(); + + // Kana Compression + if ( SwFontScript::CJK == nActual && rInf.GetKanaComp() && + pSI && pSI->CountCompChg() && + lcl_IsMonoSpaceFont( rInf.GetOut() ) ) + { + pSI->Compress( pKernArray.get(), rInf.GetIdx(), rInf.GetLen(), + rInf.GetKanaComp(), + static_cast(m_aFont.GetFontSize().Height()), + lcl_IsFullstopCentered( rInf.GetOut() ) ); + } + + // Asian Justification + if ( SwFontScript::CJK == rInf.GetFont()->GetActual() ) + { + LanguageType aLang = rInf.GetFont()->GetLanguage( SwFontScript::CJK ); + + if (!MsLangId::isKorean(aLang)) + { + SwScriptInfo::CJKJustify( rInf.GetText(), pKernArray.get(), nullptr, + rInf.GetIdx(), rInf.GetLen(), aLang, nSpaceAdd, rInf.IsSpaceStop() ); + + nSpaceAdd = 0; + } + + } + + // Kashida Justification + if ( SwFontScript::CTL == nActual && rInf.GetSpace() ) + { + if ( SwScriptInfo::IsArabicText( rInf.GetText(), rInf.GetIdx(), rInf.GetLen() ) ) + { + if ( pSI && pSI->CountKashida() && + pSI->KashidaJustify( pKernArray.get(), nullptr, rInf.GetIdx(), rInf.GetLen(), + nSpaceAdd ) != -1 ) + nSpaceAdd = 0; + } + } + + // Thai Justification + if ( SwFontScript::CTL == nActual && nSpaceAdd ) + { + LanguageType aLang = rInf.GetFont()->GetLanguage( SwFontScript::CTL ); + + if ( LANGUAGE_THAI == aLang ) + { + SwScriptInfo::ThaiJustify( rInf.GetText(), pKernArray.get(), nullptr, + rInf.GetIdx(), rInf.GetLen(), + rInf.GetNumberOfBlanks(), + rInf.GetSpace() ); + + // adding space to blanks is already done + nSpaceAdd = 0; + } + } + } + + long nLeft = 0; + long nRight = 0; + TextFrameIndex nCnt(0); + long nSpaceSum = 0; + long nKernSum = 0; + + if ( rInf.GetFrame() && rInf.GetLen() && rInf.SnapToGrid() && + rInf.GetFont() && SwFontScript::CJK == rInf.GetFont()->GetActual() ) + { + SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame())); + if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && pGrid->IsSnapToChars() ) + { + const SwDoc* pDoc = rInf.GetShell()->GetDoc(); + const sal_uInt16 nGridWidth = GetGridWidth(*pGrid, *pDoc); + + long nAvgWidthPerChar = pKernArray[sal_Int32(rInf.GetLen()) - 1] / sal_Int32(rInf.GetLen()); + + sal_uLong i = nAvgWidthPerChar ? + ( nAvgWidthPerChar - 1 ) / nGridWidth + 1: + 1; + + nAvgWidthPerChar = i * nGridWidth; + +// stupid CLANG + nCnt = TextFrameIndex(rInf.GetOffset() / nAvgWidthPerChar); + if (2 * (rInf.GetOffset() - sal_Int32(nCnt) * nAvgWidthPerChar) > nAvgWidthPerChar) + ++nCnt; + + return nCnt; + } + } + + //for textgrid refactor + if ( rInf.GetFrame() && rInf.GetLen() && rInf.SnapToGrid() ) + { + SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame())); + if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() ) + { + + const long nGridWidthAdd = EvalGridWidthAdd( pGrid, rInf ); + + for (TextFrameIndex j(0); j < rInf.GetLen(); j++) + { + long nScr = pKernArray[sal_Int32(j)] + (nSpaceAdd + nGridWidthAdd) * (sal_Int32(j) + 1); + if( nScr >= rInf.GetOffset()) + { + nCnt = j; + break; + } + } + return nCnt; + } + } + + sal_Int32 nDone = 0; + TextFrameIndex nIdx = rInf.GetIdx(); + TextFrameIndex nLastIdx = nIdx; + const TextFrameIndex nEnd = rInf.GetIdx() + rInf.GetLen(); + + // #i105901# + // skip character cells for all script types + LanguageType aLang = rInf.GetFont()->GetLanguage(); + + while ( ( nRight < long( rInf.GetOffset() ) ) && ( nIdx < nEnd ) ) + { + if (nSpaceAdd && CH_BLANK == rInf.GetText()[ sal_Int32(nIdx)]) + nSpaceSum += nSpaceAdd; + + // go to next character (cell). + nLastIdx = nIdx; + + nIdx = TextFrameIndex(g_pBreakIt->GetBreakIter()->nextCharacters( + rInf.GetText(), sal_Int32(nIdx), + g_pBreakIt->GetLocale( aLang ), + i18n::CharacterIteratorMode::SKIPCELL, 1, nDone)); + if ( nIdx <= nLastIdx ) + break; + + nLeft = nRight; + nRight = pKernArray[sal_Int32(nIdx - rInf.GetIdx()) - 1] + nKernSum + nSpaceSum; + + nKernSum += nKern; + } + + // step back if position is before the middle of the character + // or if we do not want to go to the next character + if ( nIdx > rInf.GetIdx() && + ( rInf.IsPosMatchesBounds() || + ( ( nRight > long( rInf.GetOffset() ) ) && + ( nRight - rInf.GetOffset() > rInf.GetOffset() - nLeft ) ) ) ) + nCnt = nLastIdx - rInf.GetIdx(); // first half + else + nCnt = nIdx - rInf.GetIdx(); // second half + + if ( pSI ) + rInf.SetCursorBidiLevel( pSI->DirType( nLastIdx ) ); + + return nCnt; +} + +SwFntAccess::SwFntAccess( const void* & rnFontCacheId, + sal_uInt16 &rIndex, const void *pOwn, SwViewShell const *pSh, + bool bCheck ) : + SwCacheAccess( *pFntCache, rnFontCacheId, rIndex ), + m_pShell( pSh ) +{ + // the used ctor of SwCacheAccess searches for rnFontCacheId+rIndex in the cache + if ( m_pObj ) + { + // fast case: known Font (rnFontCacheId), no need to check printer and zoom + if ( !bCheck ) + return; + + // Font is known, but has to be checked + } + else + { // Font not known, must be searched + bCheck = false; + } + + { + OutputDevice* pOut = nullptr; + sal_uInt16 nZoom = USHRT_MAX; + + // Get the reference device + if ( pSh ) + { + pOut = &pSh->GetRefDev(); + nZoom = pSh->GetViewOptions()->GetZoom(); + } + + SwFntObj *pFntObj; + if ( bCheck ) + { + pFntObj = Get(); + if ( ( pFntObj->GetZoom( ) == nZoom ) && + ( pFntObj->m_pPrinter == pOut ) && + pFntObj->GetPropWidth() == + static_cast(pOwn)->GetPropWidth() ) + { + return; // result of Check: Drucker+Zoom okay. + } + pFntObj->Unlock(); // forget this object, printer/zoom differs + m_pObj = nullptr; + } + + // Search by font comparison, quite expensive! + // Look for same font and same printer + pFntObj = pFntCache->First(); + while ( pFntObj && !( pFntObj->m_aFont == *static_cast(pOwn) && + pFntObj->GetZoom() == nZoom && + pFntObj->GetPropWidth() == + static_cast(pOwn)->GetPropWidth() && + ( !pFntObj->m_pPrinter || pFntObj->m_pPrinter == pOut ) ) ) + pFntObj = SwFntCache::Next( pFntObj ); + + if( pFntObj && pFntObj->m_pPrinter.get() != pOut ) + { + // found one without printer, let's see if there is one with + // the same printer as well + SwFntObj *pTmpObj = pFntObj; + while( pTmpObj && !( pTmpObj->m_aFont == *static_cast(pOwn) && + pTmpObj->GetZoom()==nZoom && pTmpObj->m_pPrinter==pOut && + pTmpObj->GetPropWidth() == + static_cast(pOwn)->GetPropWidth() ) ) + pTmpObj = SwFntCache::Next( pTmpObj ); + if( pTmpObj ) + pFntObj = pTmpObj; + } + + if ( !pFntObj ) // Font has not been found, create one + { + // Have to create new Object, hence Owner must be a SwFont, later + // the Owner will be the "MagicNumber" + SwCacheAccess::m_pOwner = pOwn; + pFntObj = Get(); // will create via NewObj() and lock + OSL_ENSURE(pFntObj, "No Font, no Fun."); + } + else // Font has been found, so we lock it. + { + pFntObj->Lock(); + if (pFntObj->m_pPrinter.get() != pOut) // if no printer is known by now + { + OSL_ENSURE( !pFntObj->m_pPrinter, "SwFntAccess: Printer Changed" ); + pFntObj->CreatePrtFont( *pOut ); + pFntObj->m_pPrinter = pOut; + pFntObj->m_pScrFont = nullptr; + pFntObj->m_nGuessedLeading = USHRT_MAX; + pFntObj->m_nExtLeading = USHRT_MAX; + pFntObj->m_nPrtAscent = USHRT_MAX; + pFntObj->m_nPrtHeight = USHRT_MAX; + } + m_pObj = pFntObj; + } + + // no matter if new or found, now the Owner of the Object is a + // MagicNumber, and will be given to the SwFont, as well as the Index + // for later direct access + rnFontCacheId = reinterpret_cast(reinterpret_cast(pFntObj->GetOwner())); + SwCacheAccess::m_pOwner = pFntObj->GetOwner(); + rIndex = pFntObj->GetCachePos(); + } +} + +SwCacheObj *SwFntAccess::NewObj( ) +{ + // "MagicNumber" used to identify Fonts + static std::uintptr_t fontCacheIdCounter = 0; + // a new Font, a new "MagicNumber". + return new SwFntObj( *static_cast(m_pOwner), ++fontCacheIdCounter, m_pShell ); +} + +TextFrameIndex SwFont::GetTextBreak(SwDrawTextInfo const & rInf, long nTextWidth) +{ + ChgFnt( rInf.GetShell(), rInf.GetOut() ); + + const bool bCompress = rInf.GetKanaComp() && rInf.GetLen() && + SwFontScript::CJK == GetActual() && + rInf.GetScriptInfo() && + rInf.GetScriptInfo()->CountCompChg() && + lcl_IsMonoSpaceFont( rInf.GetOut() ); + + OSL_ENSURE( !bCompress || ( rInf.GetScriptInfo() && rInf.GetScriptInfo()-> + CountCompChg()), "Compression without info" ); + + TextFrameIndex nTextBreak(0); + long nKern = 0; + + TextFrameIndex nLn = rInf.GetLen() == TextFrameIndex(COMPLETE_STRING) + ? TextFrameIndex(rInf.GetText().getLength()) : rInf.GetLen(); + + if ( rInf.GetFrame() && nLn && rInf.SnapToGrid() && + rInf.GetFont() && SwFontScript::CJK == rInf.GetFont()->GetActual() ) + { + SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame())); + if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && pGrid->IsSnapToChars() ) + { + const SwDoc* pDoc = rInf.GetShell()->GetDoc(); + const sal_uInt16 nGridWidth = GetGridWidth(*pGrid, *pDoc); + + std::unique_ptr pKernArray(new long[sal_Int32(rInf.GetLen())]); + rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + + long nAvgWidthPerChar = pKernArray[sal_Int32(rInf.GetLen()) - 1] / sal_Int32(rInf.GetLen()); + + const sal_uLong i = nAvgWidthPerChar ? + ( nAvgWidthPerChar - 1 ) / nGridWidth + 1: + 1; + + nAvgWidthPerChar = i * nGridWidth; + long nCurrPos = nAvgWidthPerChar; + + while( nTextBreak < rInf.GetLen() && nTextWidth >= nCurrPos ) + { + nCurrPos += nAvgWidthPerChar; + ++nTextBreak; + } + + return nTextBreak + rInf.GetIdx(); + } + } + + //for text grid enhancement + if ( rInf.GetFrame() && nLn && rInf.SnapToGrid() ) + { + SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame())); + if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() ) + { + const long nGridWidthAdd = EvalGridWidthAdd( pGrid, rInf ); + + std::unique_ptr pKernArray(new long[sal_Int32(rInf.GetLen())] ); + rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + long nCurrPos = pKernArray[sal_Int32(nTextBreak)] + nGridWidthAdd; + while (++nTextBreak < rInf.GetLen() && nTextWidth >= nCurrPos) + { + nCurrPos = pKernArray[sal_Int32(nTextBreak)] + nGridWidthAdd * (sal_Int32(nTextBreak) + 1); + } + return nTextBreak + rInf.GetIdx(); + } + } + + if( m_aSub[m_nActual].IsCapital() && nLn ) + { + nTextBreak = GetCapitalBreak( rInf.GetShell(), rInf.GetpOut(), + rInf.GetScriptInfo(), rInf.GetText(), nTextWidth, rInf.GetIdx(), + nLn ); + } + else + { + nKern = CheckKerning(); + + const OUString* pTmpText; + OUString aTmpText; + TextFrameIndex nTmpIdx; + TextFrameIndex nTmpLen; + bool bTextReplaced = false; + + if ( !m_aSub[m_nActual].IsCaseMap() ) + { + pTmpText = &rInf.GetText(); + nTmpIdx = rInf.GetIdx(); + nTmpLen = nLn; + } + else + { + const OUString aSnippet(rInf.GetText().copy(sal_Int32(rInf.GetIdx()), sal_Int32(nLn))); + aTmpText = m_aSub[m_nActual].CalcCaseMap( aSnippet ); + const bool bTitle = SvxCaseMap::Capitalize == m_aSub[m_nActual].GetCaseMap(); + + // Uaaaaahhhh!!! In title case mode, we would get wrong results + if ( bTitle && nLn ) + { + // check if rInf.GetIdx() is begin of word + if ( !g_pBreakIt->GetBreakIter()->isBeginWord( + rInf.GetText(), sal_Int32(rInf.GetIdx()), + g_pBreakIt->GetLocale( m_aSub[m_nActual].GetLanguage() ), + i18n::WordType::ANYWORD_IGNOREWHITESPACES ) ) + { + // In this case, the beginning of aTmpText is wrong. + OUString aSnippetTmp(aSnippet.copy(0, 1)); + aSnippetTmp = m_aSub[m_nActual].CalcCaseMap( aSnippetTmp ); + aTmpText = aTmpText.replaceAt( 0, aSnippetTmp.getLength(), OUString(aSnippet[0]) ); + } + } + + pTmpText = &aTmpText; + nTmpIdx = TextFrameIndex(0); + nTmpLen = TextFrameIndex(aTmpText.getLength()); + bTextReplaced = true; + } + + if( rInf.GetHyphPos() ) { + sal_Int32 nHyphPos = sal_Int32(*rInf.GetHyphPos()); + nTextBreak = TextFrameIndex(rInf.GetOut().GetTextBreak( + *pTmpText, nTextWidth, + u'-', nHyphPos, + sal_Int32(nTmpIdx), sal_Int32(nTmpLen), + nKern, rInf.GetVclCache())); + *rInf.GetHyphPos() = TextFrameIndex((nHyphPos == -1) ? COMPLETE_STRING : nHyphPos); + } + else + { + SwFntAccess aFntAccess(m_aSub[m_nActual].m_nFontCacheId, m_aSub[m_nActual].m_nFontIndex, + &m_aSub[m_nActual], rInf.GetShell()); + SwTextGlyphsKey aGlyphsKey{ &rInf.GetOut(), *pTmpText, sal_Int32(nTmpIdx), sal_Int32(nTmpLen) }; + SalLayoutGlyphs* pGlyphs + = lcl_CreateLayout(aGlyphsKey, aFntAccess.Get()->GetTextGlyphs()[aGlyphsKey]); + nTextBreak = TextFrameIndex(rInf.GetOut().GetTextBreak( + *pTmpText, nTextWidth, + sal_Int32(nTmpIdx), sal_Int32(nTmpLen), + nKern, rInf.GetVclCache(), pGlyphs)); + } + + if (bTextReplaced && sal_Int32(nTextBreak) != -1) + { + if ( nTmpLen != nLn ) + nTextBreak = sw_CalcCaseMap( *this, rInf.GetText(), + rInf.GetIdx(), nLn, nTextBreak ); + else + nTextBreak = nTextBreak + rInf.GetIdx(); + } + } + + TextFrameIndex nTextBreak2 = sal_Int32(nTextBreak) == -1 + ? TextFrameIndex(COMPLETE_STRING) + : nTextBreak; + + if ( ! bCompress ) + return nTextBreak2; + + nTextBreak2 = nTextBreak2 - rInf.GetIdx(); + + if( nTextBreak2 < nLn ) + { + if( !nTextBreak2 && nLn ) + nLn = TextFrameIndex(1); + else if (nLn > nTextBreak2 + nTextBreak2) + nLn = nTextBreak2 + nTextBreak2; + std::unique_ptr pKernArray( new long[sal_Int32(nLn)] ); + rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray.get(), + sal_Int32(rInf.GetIdx()), sal_Int32(nLn)); + if( rInf.GetScriptInfo()->Compress( pKernArray.get(), rInf.GetIdx(), nLn, + rInf.GetKanaComp(), static_cast(GetHeight( m_nActual )), + lcl_IsFullstopCentered( rInf.GetOut() ) ) ) + { + long nKernAdd = nKern; + TextFrameIndex const nTmpBreak = nTextBreak2; + if( nKern && nTextBreak2 ) + nKern *= sal_Int32(nTextBreak2) - 1; + while (nTextBreak2 < nLn && nTextWidth >= pKernArray[sal_Int32(nTextBreak2)] + nKern) + { + nKern += nKernAdd; + ++nTextBreak2; + } + if( rInf.GetHyphPos() ) + *rInf.GetHyphPos() += nTextBreak2 - nTmpBreak; // It's not perfect + } + } + nTextBreak2 = nTextBreak2 + rInf.GetIdx(); + + return nTextBreak2; +} + +bool SwDrawTextInfo::ApplyAutoColor( vcl::Font* pFont ) +{ + const vcl::Font& rFnt = pFont ? *pFont : GetOut().GetFont(); + Color nNewColor = COL_BLACK; + bool bChgFntColor = false; + bool bChgLineColor = false; + + if (GetShell() && !GetShell()->GetWin() && GetShell()->GetViewOptions()->IsBlackFont()) + { + if ( COL_BLACK != rFnt.GetColor() ) + bChgFntColor = true; + + if ( (COL_BLACK != GetOut().GetLineColor()) || + (COL_BLACK != GetOut().GetOverlineColor()) ) + bChgLineColor = true; + } + else + { + // FontColor has to be changed if: + // 1. FontColor = AUTO or 2. IsAlwaysAutoColor is set + // LineColor has to be changed if: + // 1. IsAlwaysAutoColor is set + + bChgLineColor = GetShell() && GetShell()->GetWin() && + GetShell()->GetAccessibilityOptions()->IsAlwaysAutoColor(); + + bChgFntColor = COL_AUTO == rFnt.GetColor() || bChgLineColor; + + if ( bChgFntColor ) + { + // check if current background has a user defined setting + const Color* pCol = GetFont() ? GetFont()->GetBackColor() : nullptr; + Color aColor; + if( ! pCol || COL_TRANSPARENT == *pCol ) + { + const SvxBrushItem* pItem; + SwRect aOrigBackRect; + drawinglayer::attribute::SdrAllFillAttributesHelperPtr aFillAttributes; + + /// OD 21.08.2002 + /// consider, that [GetBackgroundBrush(...)] can set + /// - see implementation in /core/layout/paintfrm.cxx + /// OD 21.08.2002 #99657# + /// There is a user defined setting for the background, if there + /// is a background brush and its color is *not* "no fill"/"auto fill". + if( GetFrame()->GetBackgroundBrush( aFillAttributes, pItem, pCol, aOrigBackRect, false, /*bConsiderTextBox=*/true ) ) + { + if (aFillAttributes && aFillAttributes->isUsed()) + { + // First see if fill attributes provide a color. + aColor = Color(aFillAttributes->getAverageColor(aGlobalRetoucheColor.getBColor())); + pCol = &aColor; + } + + // If not, then fall back to the old brush item. + if ( !pCol ) + { + pCol = &pItem->GetColor(); + } + + /// OD 30.08.2002 #99657# + /// determined color can be . Thus, check it. + if ( *pCol == COL_TRANSPARENT) + pCol = nullptr; + } + else + pCol = nullptr; + } + + // no user defined color at paragraph or font background + if ( ! pCol ) + pCol = &aGlobalRetoucheColor; + + if( GetShell() && GetShell()->GetWin() ) + { + // here we determine the preferred window text color for painting + const SwViewOption* pViewOption = GetShell()->GetViewOptions(); + if(pViewOption->IsPagePreview() && + !SW_MOD()->GetAccessibilityOptions().GetIsForPagePreviews()) + nNewColor = COL_BLACK; + else + // we take the font color from the appearance page + nNewColor = SwViewOption::GetFontColor(); + } + + // change painting color depending of dark/bright background + Color aTmpColor( nNewColor ); + if ( pCol->IsDark() && aTmpColor.IsDark() ) + nNewColor = COL_WHITE; + else if ( pCol->IsBright() && aTmpColor.IsBright() ) + nNewColor = COL_BLACK; + } + } + + if ( bChgFntColor || bChgLineColor ) + { + Color aNewColor( nNewColor ); + + if ( bChgFntColor ) + { + if ( pFont && aNewColor != pFont->GetColor() ) + { + // only set the new color at the font passed as argument + pFont->SetColor( aNewColor ); + } + else if ( aNewColor != GetOut().GetFont().GetColor() ) + { + // set new font with new color at output device + vcl::Font aFont( rFnt ); + aFont.SetColor( aNewColor ); + GetOut().SetFont( aFont ); + } + } + + // the underline and overline colors have to be set separately + if ( bChgLineColor ) + { + // get current font color or color set at output device + aNewColor = pFont ? pFont->GetColor() : GetOut().GetFont().GetColor(); + if ( aNewColor != GetOut().GetLineColor() ) + GetOut().SetLineColor( aNewColor ); + if ( aNewColor != GetOut().GetOverlineColor() ) + GetOut().SetOverlineColor( aNewColor ); + } + + return true; + } + + return false; +} + +void SwClearFntCacheTextGlyphs() +{ + for (SwFntObj* pFntObj = pFntCache->First(); pFntObj; pFntObj = SwFntCache::Next(pFntObj)) + pFntObj->GetTextGlyphs().clear(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/fntcap.cxx b/sw/source/core/txtnode/fntcap.cxx new file mode 100644 index 000000000..8ab9cf830 --- /dev/null +++ b/sw/source/core/txtnode/fntcap.cxx @@ -0,0 +1,778 @@ +/* -*- 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 + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +using namespace ::com::sun::star::i18n; + +namespace { + +// The information encapsulated in SwCapitalInfo is required +// by the ::Do functions. They contain the information about +// the original string, whereas rDo.GetInf() contains information +// about the display string. +class SwCapitalInfo +{ +public: + explicit SwCapitalInfo( const OUString& rOrigText ) : + rString( rOrigText ), nIdx( 0 ), nLen( 0 ) {}; + const OUString& rString; + TextFrameIndex nIdx; + TextFrameIndex nLen; +}; + +} + +// rFnt: required for CalcCaseMap +// rOrigString: The original string +// nOfst: Position of the substring in rOrigString +// nLen: Length if the substring in rOrigString +// nIdx: Refers to a position in the display string and should be mapped +// to a position in rOrigString +TextFrameIndex sw_CalcCaseMap(const SwFont& rFnt, + const OUString& rOrigString, + TextFrameIndex const nOfst, + TextFrameIndex const nLen, + TextFrameIndex const nIdx) +{ + int j = 0; + const TextFrameIndex nEnd = nOfst + nLen; + OSL_ENSURE( sal_Int32(nEnd) <= rOrigString.getLength(), "sw_CalcCaseMap: Wrong parameters" ); + + // special case for title case: + const bool bTitle = SvxCaseMap::Capitalize == rFnt.GetCaseMap(); + for (TextFrameIndex i = nOfst; i < nEnd; ++i) + { + OUString aTmp(rOrigString.copy(sal_Int32(i), 1)); + + if ( !bTitle || + g_pBreakIt->GetBreakIter()->isBeginWord( + rOrigString, sal_Int32(i), + g_pBreakIt->GetLocale( rFnt.GetLanguage() ), + WordType::ANYWORD_IGNOREWHITESPACES ) ) + aTmp = rFnt.GetActualFont().CalcCaseMap( aTmp ); + + j += aTmp.getLength(); + + if (TextFrameIndex(j) > nIdx) + return i; + } + + return nOfst + nLen; +} + +class SwDoCapitals +{ +protected: + SwDrawTextInfo &rInf; + SwCapitalInfo* pCapInf; // refers to additional information + // required by the ::Do function + explicit SwDoCapitals ( SwDrawTextInfo &rInfo ) : rInf( rInfo ), pCapInf( nullptr ) { } + ~SwDoCapitals() {} +public: + virtual void Init( SwFntObj *pUpperFont, SwFntObj *pLowerFont ) = 0; + virtual void Do() = 0; + OutputDevice& GetOut() { return rInf.GetOut(); } + SwDrawTextInfo& GetInf() { return rInf; } + SwCapitalInfo* GetCapInf() const { return pCapInf; } + void SetCapInf( SwCapitalInfo& rNew ) { pCapInf = &rNew; } +}; + +namespace { + +class SwDoGetCapitalSize : public SwDoCapitals +{ +protected: + Size aTextSize; +public: + explicit SwDoGetCapitalSize( SwDrawTextInfo &rInfo ) : SwDoCapitals ( rInfo ) { } + virtual ~SwDoGetCapitalSize() {} + virtual void Init( SwFntObj *pUpperFont, SwFntObj *pLowerFont ) override; + virtual void Do() override; + const Size &GetSize() const { return aTextSize; } +}; + +} + +void SwDoGetCapitalSize::Init( SwFntObj *, SwFntObj * ) +{ + aTextSize.setHeight( 0 ); + aTextSize.setWidth( 0 ); +} + +void SwDoGetCapitalSize::Do() +{ + aTextSize.AdjustWidth(rInf.GetSize().Width() ); + if( rInf.GetUpper() ) + aTextSize.setHeight( rInf.GetSize().Height() ); +} + +Size SwSubFont::GetCapitalSize( SwDrawTextInfo& rInf ) +{ + // Start: + const long nOldKern = rInf.GetKern(); + rInf.SetKern( CheckKerning() ); + rInf.SetPos( Point() ); + rInf.SetSpace( 0 ); + rInf.SetDrawSpace( false ); + SwDoGetCapitalSize aDo( rInf ); + DoOnCapitals( aDo ); + Size aTextSize( aDo.GetSize() ); + + // End: + if( !aTextSize.Height() ) + { + SV_STAT( nGetTextSize ); + aTextSize.setHeight( short ( rInf.GetpOut()->GetTextHeight() ) ); + } + rInf.SetKern( nOldKern ); + return aTextSize; +} + +namespace { + +class SwDoGetCapitalBreak : public SwDoCapitals +{ +protected: + long nTextWidth; + TextFrameIndex m_nBreak; + +public: + SwDoGetCapitalBreak( SwDrawTextInfo &rInfo, long const nWidth) + : SwDoCapitals ( rInfo ) + , nTextWidth( nWidth ) + , m_nBreak( -1 ) + { } + virtual ~SwDoGetCapitalBreak() {} + virtual void Init( SwFntObj *pUpperFont, SwFntObj *pLowerFont ) override; + virtual void Do() override; + TextFrameIndex getBreak() const { return m_nBreak; } +}; + +} + +void SwDoGetCapitalBreak::Init( SwFntObj *, SwFntObj * ) +{ +} + +void SwDoGetCapitalBreak::Do() +{ + if ( nTextWidth ) + { + if ( rInf.GetSize().Width() < nTextWidth ) + nTextWidth -= rInf.GetSize().Width(); + else + { + TextFrameIndex nEnd = rInf.GetEnd(); + m_nBreak = TextFrameIndex(GetOut().GetTextBreak( + rInf.GetText(), nTextWidth, sal_Int32(rInf.GetIdx()), + sal_Int32(rInf.GetLen()), rInf.GetKern())); + + if (m_nBreak > nEnd || m_nBreak < TextFrameIndex(0)) + m_nBreak = nEnd; + + // m_nBreak may be relative to the display string. It has to be + // calculated relative to the original string: + if ( GetCapInf() ) + { + if ( GetCapInf()->nLen != rInf.GetLen() ) + m_nBreak = sw_CalcCaseMap( *rInf.GetFont(), + GetCapInf()->rString, + GetCapInf()->nIdx, + GetCapInf()->nLen, m_nBreak ); + else + m_nBreak = m_nBreak + GetCapInf()->nIdx; + } + + nTextWidth = 0; + } + } +} + +TextFrameIndex SwFont::GetCapitalBreak( SwViewShell const * pSh, const OutputDevice* pOut, + const SwScriptInfo* pScript, const OUString& rText, long const nTextWidth, + TextFrameIndex const nIdx, TextFrameIndex const nLen) +{ + // Start: + Point aPos( 0, 0 ); + SwDrawTextInfo aInfo(pSh, *const_cast(pOut), pScript, rText, nIdx, nLen, + 0, false); + aInfo.SetPos( aPos ); + aInfo.SetSpace( 0 ); + aInfo.SetWrong( nullptr ); + aInfo.SetGrammarCheck( nullptr ); + aInfo.SetSmartTags( nullptr ); + aInfo.SetDrawSpace( false ); + aInfo.SetKern( CheckKerning() ); + aInfo.SetKanaComp( pScript ? 0 : 100 ); + aInfo.SetFont( this ); + + SwDoGetCapitalBreak aDo(aInfo, nTextWidth); + DoOnCapitals( aDo ); + return aDo.getBreak(); +} + +namespace { + +class SwDoDrawCapital : public SwDoCapitals +{ +protected: + SwFntObj *pUpperFnt; + SwFntObj *pLowerFnt; +public: + explicit SwDoDrawCapital( SwDrawTextInfo &rInfo ) : + SwDoCapitals( rInfo ), pUpperFnt(nullptr), pLowerFnt(nullptr) + { } + virtual ~SwDoDrawCapital() {} + virtual void Init( SwFntObj *pUpperFont, SwFntObj *pLowerFont ) override; + virtual void Do() override; + void DrawSpace( Point &rPos ); +}; + +} + +void SwDoDrawCapital::Init( SwFntObj *pUpperFont, SwFntObj *pLowerFont ) +{ + pUpperFnt = pUpperFont; + pLowerFnt = pLowerFont; +} + +void SwDoDrawCapital::Do() +{ + SV_STAT( nDrawText ); + const sal_uInt16 nOrgWidth = rInf.GetWidth(); + rInf.SetWidth( sal_uInt16(rInf.GetSize().Width()) ); + if ( rInf.GetUpper() ) + pUpperFnt->DrawText( rInf ); + else + { + bool bOldBullet = rInf.GetBullet(); + rInf.SetBullet( false ); + pLowerFnt->DrawText( rInf ); + rInf.SetBullet( bOldBullet ); + } + + OSL_ENSURE( pUpperFnt, "No upper font, dying soon!"); + rInf.Shift( pUpperFnt->GetFont().GetOrientation() ); + rInf.SetWidth( nOrgWidth ); +} + +void SwDoDrawCapital::DrawSpace( Point &rPos ) +{ + long nDiff = rInf.GetPos().X() - rPos.X(); + + Point aPos( rPos ); + const bool bSwitchL2R = rInf.GetFrame()->IsRightToLeft() && + ! rInf.IsIgnoreFrameRTL(); + + if ( bSwitchL2R ) + rInf.GetFrame()->SwitchLTRtoRTL( aPos ); + + const ComplexTextLayoutFlags nMode = rInf.GetpOut()->GetLayoutMode(); + const bool bBidiPor = ( bSwitchL2R != + ( ComplexTextLayoutFlags::Default != ( ComplexTextLayoutFlags::BiDiRtl & nMode ) ) ); + + if ( bBidiPor ) + nDiff = -nDiff; + + if ( rInf.GetFrame()->IsVertical() ) + rInf.GetFrame()->SwitchHorizontalToVertical( aPos ); + + if ( nDiff ) + { + rInf.ApplyAutoColor(); + GetOut().DrawStretchText( aPos, nDiff, + " ", 0, 2 ); + } + rPos.setX( rInf.GetPos().X() + rInf.GetWidth() ); +} + +void SwSubFont::DrawCapital( SwDrawTextInfo &rInf ) +{ + // Precondition: rInf.GetPos() has already been calculated + + rInf.SetDrawSpace( GetUnderline() != LINESTYLE_NONE || + GetOverline() != LINESTYLE_NONE || + GetStrikeout() != STRIKEOUT_NONE ); + SwDoDrawCapital aDo( rInf ); + DoOnCapitals( aDo ); +} + +namespace { + +class SwDoCapitalCursorOfst : public SwDoCapitals +{ +protected: + SwFntObj *pUpperFnt; + SwFntObj *pLowerFnt; + TextFrameIndex nCursor; + sal_uInt16 nOfst; +public: + SwDoCapitalCursorOfst( SwDrawTextInfo &rInfo, const sal_uInt16 nOfs ) : + SwDoCapitals( rInfo ), pUpperFnt(nullptr), pLowerFnt(nullptr), nCursor( 0 ), nOfst( nOfs ) + { } + virtual ~SwDoCapitalCursorOfst() {} + virtual void Init( SwFntObj *pUpperFont, SwFntObj *pLowerFont ) override; + virtual void Do() override; + + TextFrameIndex GetCursor() const { return nCursor; } +}; + +} + +void SwDoCapitalCursorOfst::Init( SwFntObj *pUpperFont, SwFntObj *pLowerFont ) +{ + pUpperFnt = pUpperFont; + pLowerFnt = pLowerFont; +} + +void SwDoCapitalCursorOfst::Do() +{ + if ( nOfst ) + { + if ( static_cast(nOfst) > rInf.GetSize().Width() ) + { + nOfst -= rInf.GetSize().Width(); + nCursor = nCursor + rInf.GetLen(); + } + else + { + SwDrawTextInfo aDrawInf( rInf.GetShell(), *rInf.GetpOut(), + rInf.GetScriptInfo(), + rInf.GetText(), + rInf.GetIdx(), + rInf.GetLen(), 0, false ); + aDrawInf.SetOffset( nOfst ); + aDrawInf.SetKern( rInf.GetKern() ); + aDrawInf.SetKanaComp( rInf.GetKanaComp() ); + aDrawInf.SetFrame( rInf.GetFrame() ); + aDrawInf.SetFont( rInf.GetFont() ); + + if ( rInf.GetUpper() ) + { + aDrawInf.SetSpace( 0 ); + nCursor = nCursor + pUpperFnt->GetModelPositionForViewPoint( aDrawInf ); + } + else + { + aDrawInf.SetSpace( rInf.GetSpace() ); + nCursor = nCursor + pLowerFnt->GetModelPositionForViewPoint( aDrawInf ); + } + nOfst = 0; + } + } +} + +TextFrameIndex SwSubFont::GetCapitalCursorOfst( SwDrawTextInfo& rInf ) +{ + const long nOldKern = rInf.GetKern(); + rInf.SetKern( CheckKerning() ); + SwDoCapitalCursorOfst aDo( rInf, rInf.GetOffset() ); + rInf.SetPos( Point() ); + rInf.SetDrawSpace( false ); + DoOnCapitals( aDo ); + rInf.SetKern( nOldKern ); + return aDo.GetCursor(); +} + +namespace { + +class SwDoDrawStretchCapital : public SwDoDrawCapital +{ + const TextFrameIndex nStrLen; + const sal_uInt16 nCapWidth; + const sal_uInt16 nOrgWidth; +public: + virtual void Do() override; + + SwDoDrawStretchCapital( SwDrawTextInfo &rInfo, const sal_uInt16 nCapitalWidth ) + : SwDoDrawCapital( rInfo ), + nStrLen( rInfo.GetLen() ), + nCapWidth( nCapitalWidth ), + nOrgWidth( rInfo.GetWidth() ) + { } +}; + +} + +void SwDoDrawStretchCapital::Do() +{ + SV_STAT( nDrawStretchText ); + long nPartWidth = rInf.GetSize().Width(); + + if( rInf.GetLen() ) + { + // small caps and kerning + long nDiff = long(nOrgWidth) - long(nCapWidth); + if( nDiff ) + { + nDiff *= sal_Int32(rInf.GetLen()); + nDiff /= sal_Int32(nStrLen); + nDiff += nPartWidth; + if( 0 < nDiff ) + nPartWidth = nDiff; + } + + rInf.ApplyAutoColor(); + + Point aPos( rInf.GetPos() ); + const bool bSwitchL2R = rInf.GetFrame()->IsRightToLeft() && + ! rInf.IsIgnoreFrameRTL(); + + if ( bSwitchL2R ) + rInf.GetFrame()->SwitchLTRtoRTL( aPos ); + + if ( rInf.GetFrame()->IsVertical() ) + rInf.GetFrame()->SwitchHorizontalToVertical( aPos ); + + // Optimise: + if (TextFrameIndex(1) >= rInf.GetLen()) + GetOut().DrawText(aPos, rInf.GetText(), sal_Int32(rInf.GetIdx()), + sal_Int32(rInf.GetLen())); + else + GetOut().DrawStretchText(aPos, nPartWidth, rInf.GetText(), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + } + const_cast(rInf.GetPos()).AdjustX(nPartWidth ); +} + +void SwSubFont::DrawStretchCapital( SwDrawTextInfo &rInf ) +{ + // Precondition: rInf.GetPos() has already been calculated + + if (rInf.GetLen() == TextFrameIndex(COMPLETE_STRING)) + rInf.SetLen(TextFrameIndex(rInf.GetText().getLength())); + + const Point aOldPos = rInf.GetPos(); + const sal_uInt16 nCapWidth = static_cast( GetCapitalSize( rInf ).Width() ); + rInf.SetPos(aOldPos); + + rInf.SetDrawSpace( GetUnderline() != LINESTYLE_NONE || + GetOverline() != LINESTYLE_NONE || + GetStrikeout() != STRIKEOUT_NONE ); + SwDoDrawStretchCapital aDo( rInf, nCapWidth ); + DoOnCapitals( aDo ); +} + +void SwSubFont::DoOnCapitals( SwDoCapitals &rDo ) +{ + OSL_ENSURE( pLastFont, "SwFont::DoOnCapitals: No LastFont?!" ); + + long nKana = 0; + const OUString aText( CalcCaseMap( rDo.GetInf().GetText() ) ); + TextFrameIndex nMaxPos = std::min( + TextFrameIndex(rDo.GetInf().GetText().getLength()) - rDo.GetInf().GetIdx(), + rDo.GetInf().GetLen() ); + rDo.GetInf().SetLen( nMaxPos ); + + const OUString oldText = rDo.GetInf().GetText(); + rDo.GetInf().SetText( aText ); + TextFrameIndex nPos = rDo.GetInf().GetIdx(); + TextFrameIndex nOldPos = nPos; + nMaxPos = nMaxPos + nPos; + + // Look if the length of the original text and the ToUpper-converted + // text is different. If yes, do special handling. + SwCapitalInfo aCapInf(oldText); + bool bCaseMapLengthDiffers(aText.getLength() != oldText.getLength()); + if ( bCaseMapLengthDiffers ) + rDo.SetCapInf( aCapInf ); + + SwFntObj *pOldLast = pLastFont; + std::unique_ptr pBigFontAccess; + SwFntObj *pBigFont; + std::unique_ptr pSpaceFontAccess; + SwFntObj *pSpaceFont = nullptr; + + const void* nFontCacheId2 = nullptr; + sal_uInt16 nIndex2 = 0; + SwSubFont aFont( *this ); + Point aStartPos( rDo.GetInf().GetPos() ); + + const bool bTextLines = aFont.GetUnderline() != LINESTYLE_NONE + || aFont.GetOverline() != LINESTYLE_NONE + || aFont.GetStrikeout() != STRIKEOUT_NONE; + const bool bWordWise = bTextLines && aFont.IsWordLineMode() && + rDo.GetInf().GetDrawSpace(); + const long nTmpKern = rDo.GetInf().GetKern(); + + if ( bTextLines ) + { + if ( bWordWise ) + { + aFont.SetWordLineMode( false ); + pSpaceFontAccess.reset(new SwFntAccess( nFontCacheId2, nIndex2, &aFont, + rDo.GetInf().GetShell() )); + pSpaceFont = pSpaceFontAccess->Get(); + } + else + pSpaceFont = pLastFont; + + // Construct a font for the capitals: + aFont.SetUnderline( LINESTYLE_NONE ); + aFont.SetOverline( LINESTYLE_NONE ); + aFont.SetStrikeout( STRIKEOUT_NONE ); + nFontCacheId2 = nullptr; + nIndex2 = 0; + pBigFontAccess.reset(new SwFntAccess( nFontCacheId2, nIndex2, &aFont, + rDo.GetInf().GetShell() )); + pBigFont = pBigFontAccess->Get(); + } + else + pBigFont = pLastFont; + + // Older LO versions had 66 as the small caps percentage size, later changed to 80, + // therefore a backwards compatibility option is kept (otherwise layout is changed). + // NOTE: There are more uses of SMALL_CAPS_PERCENTAGE in editeng, but it seems they + // do not matter for Writer (and if they did it'd be pretty ugly to propagate + // the option there). + int smallCapsPercentage = m_bSmallCapsPercentage66 ? 66 : SMALL_CAPS_PERCENTAGE; + aFont.SetProportion( (aFont.GetPropr() * smallCapsPercentage ) / 100 ); + nFontCacheId2 = nullptr; + nIndex2 = 0; + std::unique_ptr pSmallFontAccess( new SwFntAccess( nFontCacheId2, nIndex2, &aFont, + rDo.GetInf().GetShell() )); + SwFntObj *pSmallFont = pSmallFontAccess->Get(); + + rDo.Init( pBigFont, pSmallFont ); + OutputDevice* pOutSize = pSmallFont->GetPrt(); + if( !pOutSize ) + pOutSize = &rDo.GetOut(); + OutputDevice* pOldOut = &rDo.GetOut(); + + const LanguageType eLng = LANGUAGE_DONTKNOW == GetLanguage() + ? LANGUAGE_SYSTEM : GetLanguage(); + + if( nPos < nMaxPos ) + { + nPos = TextFrameIndex(g_pBreakIt->GetBreakIter()->endOfCharBlock( + oldText, sal_Int32(nPos), + g_pBreakIt->GetLocale( eLng ), CharType::LOWERCASE_LETTER)); + if (nPos < TextFrameIndex(0)) + nPos = nOldPos; + else if( nPos > nMaxPos ) + nPos = nMaxPos; + } + + while( nOldPos < nMaxPos ) + { + + // The lower ones... + if( nOldPos != nPos ) + { + SV_STAT( nGetTextSize ); + pLastFont = pSmallFont; + pLastFont->SetDevFont( rDo.GetInf().GetShell(), rDo.GetOut() ); + + // #107816#, #i14820# + if( bCaseMapLengthDiffers ) + { + // Build an own 'changed' string for the given part of the + // source string and use it. That new string may differ in length + // from the source string. + const OUString aNewText = CalcCaseMap( + oldText.copy(sal_Int32(nOldPos), sal_Int32(nPos-nOldPos))); + aCapInf.nIdx = nOldPos; + aCapInf.nLen = nPos - nOldPos; + rDo.GetInf().SetIdx(TextFrameIndex(0)); + rDo.GetInf().SetLen(TextFrameIndex(aNewText.getLength())); + rDo.GetInf().SetText( aNewText ); + } + else + { + rDo.GetInf().SetIdx( nOldPos ); + rDo.GetInf().SetLen( nPos - nOldPos ); + } + + rDo.GetInf().SetUpper( false ); + rDo.GetInf().SetOut( *pOutSize ); + Size aPartSize = pSmallFont->GetTextSize( rDo.GetInf() ); + nKana += rDo.GetInf().GetKanaDiff(); + rDo.GetInf().SetOut( *pOldOut ); + if( nTmpKern && nPos < nMaxPos ) + aPartSize.AdjustWidth(nTmpKern ); + rDo.GetInf().SetSize( aPartSize ); + rDo.Do(); + nOldPos = nPos; + } + nPos = TextFrameIndex(g_pBreakIt->GetBreakIter()->nextCharBlock( + oldText, sal_Int32(nPos), + g_pBreakIt->GetLocale( eLng ), CharType::LOWERCASE_LETTER)); + if (nPos < TextFrameIndex(0) || nPos > nMaxPos) + nPos = nMaxPos; + OSL_ENSURE( nPos, "nextCharBlock not implemented?" ); +#if OSL_DEBUG_LEVEL > 1 + if( !nPos ) + nPos = nMaxPos; +#endif + // The upper ones... + if( nOldPos != nPos ) + { + const long nSpaceAdd = rDo.GetInf().GetSpace() / SPACING_PRECISION_FACTOR; + + do + { + rDo.GetInf().SetUpper( true ); + pLastFont = pBigFont; + pLastFont->SetDevFont( rDo.GetInf().GetShell(), rDo.GetOut() ); + TextFrameIndex nTmp; + if( bWordWise ) + { + nTmp = nOldPos; + while (nTmp < nPos && CH_BLANK == oldText[sal_Int32(nTmp)]) + ++nTmp; + if( nOldPos < nTmp ) + { + pLastFont = pSpaceFont; + pLastFont->SetDevFont( rDo.GetInf().GetShell(), + rDo.GetOut() ); + static_cast(rDo).DrawSpace( aStartPos ); + pLastFont = pBigFont; + pLastFont->SetDevFont( rDo.GetInf().GetShell(), + rDo.GetOut() ); + + // #107816#, #i14820# + if( bCaseMapLengthDiffers ) + { + // Build an own 'changed' string for the given part of the + // source string and use it. That new string may differ in length + // from the source string. + const OUString aNewText = CalcCaseMap( + oldText.copy(sal_Int32(nOldPos), sal_Int32(nTmp-nOldPos))); + aCapInf.nIdx = nOldPos; + aCapInf.nLen = nTmp - nOldPos; + rDo.GetInf().SetIdx(TextFrameIndex(0)); + rDo.GetInf().SetLen(TextFrameIndex(aNewText.getLength())); + rDo.GetInf().SetText( aNewText ); + } + else + { + rDo.GetInf().SetIdx( nOldPos ); + rDo.GetInf().SetLen( nTmp - nOldPos ); + } + + rDo.GetInf().SetOut( *pOutSize ); + Size aPartSize = pBigFont->GetTextSize( rDo.GetInf() ); + nKana += rDo.GetInf().GetKanaDiff(); + rDo.GetInf().SetOut( *pOldOut ); + if( nSpaceAdd ) + aPartSize.AdjustWidth(nSpaceAdd * sal_Int32(nTmp - nOldPos)); + if( nTmpKern && nPos < nMaxPos ) + aPartSize.AdjustWidth(nTmpKern ); + rDo.GetInf().SetSize( aPartSize ); + rDo.Do(); + aStartPos = rDo.GetInf().GetPos(); + nOldPos = nTmp; + } + + while (nTmp < nPos && CH_BLANK != oldText[sal_Int32(nTmp)]) + ++nTmp; + } + else + nTmp = nPos; + if( nTmp > nOldPos ) + { + // #107816#, #i14820# + if( bCaseMapLengthDiffers ) + { + // Build an own 'changed' string for the given part of the + // source string and use it. That new string may differ in length + // from the source string. + const OUString aNewText = CalcCaseMap( + oldText.copy(sal_Int32(nOldPos), sal_Int32(nTmp-nOldPos))); + aCapInf.nIdx = nOldPos; + aCapInf.nLen = nTmp - nOldPos; + rDo.GetInf().SetIdx(TextFrameIndex(0)); + rDo.GetInf().SetLen(TextFrameIndex(aNewText.getLength())); + rDo.GetInf().SetText( aNewText ); + } + else + { + rDo.GetInf().SetIdx( nOldPos ); + rDo.GetInf().SetLen( nTmp - nOldPos ); + } + + rDo.GetInf().SetOut( *pOutSize ); + Size aPartSize = pBigFont->GetTextSize( rDo.GetInf() ); + nKana += rDo.GetInf().GetKanaDiff(); + rDo.GetInf().SetOut( *pOldOut ); + if( !bWordWise && rDo.GetInf().GetSpace() ) + { + for (TextFrameIndex nI = nOldPos; nI < nPos; ++nI) + { + if (CH_BLANK == oldText[sal_Int32(nI)]) + aPartSize.AdjustWidth(nSpaceAdd ); + } + } + if( nTmpKern && nPos < nMaxPos ) + aPartSize.AdjustWidth(nTmpKern ); + rDo.GetInf().SetSize( aPartSize ); + rDo.Do(); + nOldPos = nTmp; + } + } while( nOldPos != nPos ); + } + nPos = TextFrameIndex(g_pBreakIt->GetBreakIter()->endOfCharBlock( + oldText, sal_Int32(nPos), + g_pBreakIt->GetLocale( eLng ), CharType::LOWERCASE_LETTER)); + if (nPos < TextFrameIndex(0) || nPos > nMaxPos) + nPos = nMaxPos; + OSL_ENSURE( nPos, "endOfCharBlock not implemented?" ); +#if OSL_DEBUG_LEVEL > 1 + if( !nPos ) + nPos = nMaxPos; +#endif + } + + // clean up: + if( pBigFont != pOldLast ) + pBigFontAccess.reset(); + + if( bTextLines ) + { + if( rDo.GetInf().GetDrawSpace() ) + { + pLastFont = pSpaceFont; + pLastFont->SetDevFont( rDo.GetInf().GetShell(), rDo.GetOut() ); + static_cast( rDo ).DrawSpace( aStartPos ); + } + if ( bWordWise ) + pSpaceFontAccess.reset(); + } + pLastFont = pOldLast; + pLastFont->SetDevFont( rDo.GetInf().GetShell(), rDo.GetOut() ); + + pSmallFontAccess.reset(); + rDo.GetInf().SetText(oldText); + rDo.GetInf().SetKanaDiff( nKana ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/modeltoviewhelper.cxx b/sw/source/core/txtnode/modeltoviewhelper.cxx new file mode 100644 index 000000000..4675a632a --- /dev/null +++ b/sw/source/core/txtnode/modeltoviewhelper.cxx @@ -0,0 +1,337 @@ +/* -*- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +struct FieldResult +{ + sal_Int32 m_nFieldPos; + OUString m_sExpand; + enum { NONE, FIELD, FOOTNOTE } m_eType; + explicit FieldResult(sal_Int32 const nPos) + : m_nFieldPos(nPos), m_eType(NONE) + { } +}; + +class sortfieldresults +{ +public: + bool operator()(const FieldResult &rOne, const FieldResult &rTwo) const + { + return rOne.m_nFieldPos < rTwo.m_nFieldPos; + } +}; + +} + +typedef std::set FieldResultSet; + +namespace { + +struct block +{ + sal_Int32 m_nStart; + sal_Int32 m_nLen; + bool m_bVisible; + FieldResultSet m_aAttrs; + block(sal_Int32 nStart, sal_Int32 nLen, bool bVisible) + : m_nStart(nStart), m_nLen(nLen), m_bVisible(bVisible) + { + } +}; + +struct containsPos +{ + const sal_Int32 m_nPos; + explicit containsPos(const sal_Int32 nPos) + : m_nPos(nPos) + { + } + bool operator() (const block& rIn) const + { + return m_nPos >= rIn.m_nStart && m_nPos < rIn.m_nStart + rIn.m_nLen; + } +}; + +} + +ModelToViewHelper::ModelToViewHelper(const SwTextNode &rNode, + SwRootFrame const*const pLayout, ExpandMode eMode) +{ + const OUString& rNodeText = rNode.GetText(); + m_aRetText = rNodeText; + + if (eMode == ExpandMode::PassThrough) + return; + + Range aRange( 0, rNodeText.isEmpty() ? 0 : rNodeText.getLength() - 1); + MultiSelection aHiddenMulti(aRange); + + if (eMode & ExpandMode::HideInvisible) + SwScriptInfo::selectHiddenTextProperty(rNode, aHiddenMulti, nullptr); + + if (eMode & ExpandMode::HideDeletions) + SwScriptInfo::selectRedLineDeleted(rNode, aHiddenMulti); + + std::vector aBlocks; + + sal_Int32 nShownStart = 0; + for (sal_Int32 i = 0; i < aHiddenMulti.GetRangeCount(); ++i) + { + const Range& rRange = aHiddenMulti.GetRange(i); + const sal_Int32 nHiddenStart = rRange.Min(); + const sal_Int32 nHiddenEnd = rRange.Max() + 1; + const sal_Int32 nHiddenLen = nHiddenEnd - nHiddenStart; + + const sal_Int32 nShownEnd = nHiddenStart; + const sal_Int32 nShownLen = nShownEnd - nShownStart; + + if (nShownLen) + aBlocks.emplace_back(nShownStart, nShownLen, true); + + if (nHiddenLen) + aBlocks.emplace_back(nHiddenStart, nHiddenLen, false); + + nShownStart = nHiddenEnd; + } + + sal_Int32 nTrailingShownLen = rNodeText.getLength() - nShownStart; + if (nTrailingShownLen) + aBlocks.emplace_back(nShownStart, nTrailingShownLen, true); + + if (eMode & ExpandMode::ExpandFields || eMode & ExpandMode::ExpandFootnote) + { + //first the normal fields, get their position in the node and what the text they expand + //to is + const SwpHints* pSwpHints2 = rNode.GetpSwpHints(); + for ( size_t i = 0; pSwpHints2 && i < pSwpHints2->Count(); ++i ) + { + const SwTextAttr* pAttr = pSwpHints2->Get(i); + if (pAttr->HasDummyChar()) + { + const sal_Int32 nDummyCharPos = pAttr->GetStart(); + if (aHiddenMulti.IsSelected(nDummyCharPos)) + continue; + std::vector::iterator aFind = std::find_if(aBlocks.begin(), + aBlocks.end(), containsPos(nDummyCharPos)); + if (aFind != aBlocks.end()) + { + FieldResult aFieldResult(nDummyCharPos); + switch (pAttr->Which()) + { + case RES_TXTATR_FIELD: + case RES_TXTATR_ANNOTATION: + if (eMode & ExpandMode::ExpandFields) + { + // add a ZWSP before the expanded field in replace mode + aFieldResult.m_sExpand = ((eMode & ExpandMode::ReplaceMode) + ? OUString(CHAR_ZWSP) : OUString("")) + + static_txtattr_cast(pAttr)-> + GetFormatField().GetField()->ExpandField(true, pLayout); + aFieldResult.m_eType = FieldResult::FIELD; + } + break; + case RES_TXTATR_FTN: + if (eMode & ExpandMode::ExpandFootnote) + { + const SwFormatFootnote& rFootnote = static_cast(pAttr)->GetFootnote(); + const SwDoc *pDoc = rNode.GetDoc(); + aFieldResult.m_sExpand = (eMode & ExpandMode::ReplaceMode) + ? OUString(CHAR_ZWSP) + : rFootnote.GetViewNumStr(*pDoc, pLayout); + aFieldResult.m_eType = FieldResult::FOOTNOTE; + } + break; + default: + break; + } + aFind->m_aAttrs.insert(aFieldResult); + } + } + } + + if (eMode & ExpandMode::ExpandFields) + { + //now get the dropdown formfields, get their position in the node and what the text they expand + //to is + SwPaM aPaM(rNode, 0, rNode, rNode.Len()); + std::vector aDropDowns = + rNode.GetDoc()->getIDocumentMarkAccess()->getDropDownsFor(aPaM); + + for (sw::mark::IFieldmark *pMark : aDropDowns) + { + const sal_Int32 nDummyCharPos = pMark->GetMarkPos().nContent.GetIndex()-1; + if (aHiddenMulti.IsSelected(nDummyCharPos)) + continue; + std::vector::iterator aFind = std::find_if(aBlocks.begin(), aBlocks.end(), + containsPos(nDummyCharPos)); + if (aFind != aBlocks.end()) + { + FieldResult aFieldResult(nDummyCharPos); + aFieldResult.m_sExpand = (eMode & ExpandMode::ReplaceMode) + ? OUString(CHAR_ZWSP) + : sw::mark::ExpandFieldmark(pMark); + aFieldResult.m_eType = FieldResult::FIELD; + aFind->m_aAttrs.insert(aFieldResult); + } + } + } + } + + //store the end of each range in the model and where that end of range + //maps to in the view + sal_Int32 nOffset = 0; + for (const auto& rBlock : aBlocks) + { + const sal_Int32 nBlockLen = rBlock.m_nLen; + if (!nBlockLen) + continue; + const sal_Int32 nBlockStart = rBlock.m_nStart; + const sal_Int32 nBlockEnd = nBlockStart + nBlockLen; + + if (!rBlock.m_bVisible) + { + sal_Int32 const modelBlockPos(nBlockEnd); + sal_Int32 const viewBlockPos(nBlockStart + nOffset); + m_aMap.emplace_back(modelBlockPos, viewBlockPos, false); + + m_aRetText = m_aRetText.replaceAt(nOffset + nBlockStart, nBlockLen, OUString()); + nOffset -= nBlockLen; + } + else + { + for (const auto& rAttr : rBlock.m_aAttrs) + { + sal_Int32 const modelFieldPos(rAttr.m_nFieldPos); + sal_Int32 const viewFieldPos(rAttr.m_nFieldPos + nOffset); + m_aMap.emplace_back(modelFieldPos, viewFieldPos, true ); + + m_aRetText = m_aRetText.replaceAt(viewFieldPos, 1, rAttr.m_sExpand); + nOffset += rAttr.m_sExpand.getLength() - 1; + + switch (rAttr.m_eType) + { + case FieldResult::FIELD: + m_FieldPositions.push_back(viewFieldPos); + break; + case FieldResult::FOOTNOTE: + m_FootnotePositions.push_back(viewFieldPos); + break; + case FieldResult::NONE: /*ignore*/ + break; + } + } + + sal_Int32 const modelEndBlock(nBlockEnd); + sal_Int32 const viewFieldPos(nBlockEnd + nOffset); + m_aMap.emplace_back(modelEndBlock, viewFieldPos, true); + } + } +} + +/** Converts a model position into a view position +*/ +sal_Int32 ModelToViewHelper::ConvertToViewPosition( sal_Int32 nModelPos ) const +{ + // Search for entry after nPos: + auto aIter = std::find_if(m_aMap.begin(), m_aMap.end(), + [nModelPos](const ConversionMapEntry& rEntry) { return rEntry.m_nModelPos >= nModelPos; }); + if (aIter != m_aMap.end()) + { + //if it's an invisible portion, map all contained positions + //to the anchor viewpos + if (!aIter->m_bVisible) + return aIter->m_nViewPos; + + //if it's a visible portion, then the view position is the anchor + //viewpos - the offset of the input modelpos from the anchor + //modelpos + const sal_Int32 nOffsetFromEnd = aIter->m_nModelPos - nModelPos; + return aIter->m_nViewPos - nOffsetFromEnd; + } + + return nModelPos; +} + +/** Converts a view position into a model position +*/ +ModelToViewHelper::ModelPosition ModelToViewHelper::ConvertToModelPosition( sal_Int32 nViewPos ) const +{ + ModelPosition aRet; + aRet.mnPos = nViewPos; + + // Search for entry after nPos: + auto aIter = std::find_if(m_aMap.begin(), m_aMap.end(), + [nViewPos](const ConversionMapEntry& rEntry) { return rEntry.m_nViewPos > nViewPos; }); + + // If nViewPos is in front of first field, we are finished. + if (aIter != m_aMap.end() && aIter != m_aMap.begin()) + { + const sal_Int32 nPosModel = aIter->m_nModelPos; + const sal_Int32 nPosExpand = aIter->m_nViewPos; + + --aIter; + + // nPrevPosModel is the field position + const sal_Int32 nPrevPosModel = aIter->m_nModelPos; + const sal_Int32 nPrevPosExpand = aIter->m_nViewPos; + + const sal_Int32 nLengthModel = nPosModel - nPrevPosModel; + const sal_Int32 nLengthExpand = nPosExpand - nPrevPosExpand; + + const sal_Int32 nFieldLengthExpand = nLengthExpand - nLengthModel + 1; + const sal_Int32 nFieldEndExpand = nPrevPosExpand + nFieldLengthExpand; + + // Check if nPos is outside of field: + if ( nFieldEndExpand <= nViewPos ) + { + // nPos is outside of field: + const sal_Int32 nDistToField = nViewPos - nFieldEndExpand + 1; + aRet.mnPos = nPrevPosModel + nDistToField; + } + else + { + // nViewPos is inside a field: + aRet.mnPos = nPrevPosModel; + aRet.mnSubPos = nViewPos - nPrevPosExpand; + aRet.mbIsField = true; + } + } + + return aRet; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/ndhints.cxx b/sw/source/core/txtnode/ndhints.cxx new file mode 100644 index 000000000..e7f34081c --- /dev/null +++ b/sw/source/core/txtnode/ndhints.cxx @@ -0,0 +1,475 @@ +/* -*- 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 +#include +#include +#include +#include + +#ifdef DBG_UTIL +#include +#include +#include +#endif + +/// sort order: Start, End (reverse), Which (reverse), +/// (char style: sort number), at last the pointer +static bool CompareSwpHtStart( const SwTextAttr* lhs, const SwTextAttr* rhs ) +{ + const SwTextAttr &rHt1 = *lhs; + const SwTextAttr &rHt2 = *rhs; + if ( rHt1.GetStart() == rHt2.GetStart() ) + { + const sal_Int32 nHt1 = rHt1.GetAnyEnd(); + const sal_Int32 nHt2 = rHt2.GetAnyEnd(); + if ( nHt1 == nHt2 ) + { + const sal_uInt16 nWhich1 = rHt1.Which(); + const sal_uInt16 nWhich2 = rHt2.Which(); + if ( nWhich1 == nWhich2 ) + { + if ( RES_TXTATR_CHARFMT == nWhich1 ) + { + const sal_uInt16 nS1 = + static_txtattr_cast(rHt1).GetSortNumber(); + const sal_uInt16 nS2 = + static_txtattr_cast(rHt2).GetSortNumber(); + if ( nS1 != nS2 ) // robust + return nS1 < nS2; + } + + return reinterpret_cast(&rHt1) < reinterpret_cast(&rHt2); + } + // order is important! for requirements see hintids.hxx + return ( nWhich1 > nWhich2 ); + } + return ( nHt1 > nHt2 ); + } + return ( rHt1.GetStart() < rHt2.GetStart() ); +} + +/// sort order: Which, Start, End(reverse) at last the pointer +bool CompareSwpHtWhichStart::operator()( const SwTextAttr* lhs, const sal_uInt16 nWhich ) const +{ + return lhs->Which() < nWhich; +} +bool CompareSwpHtWhichStart::operator()( const SwTextAttr* lhs, const SwTextAttr* rhs ) const +{ + const SwTextAttr &rHt1 = *lhs; + const SwTextAttr &rHt2 = *rhs; + const sal_uInt16 nWhich1 = rHt1.Which(); + const sal_uInt16 nWhich2 = rHt2.Which(); + if ( nWhich1 < nWhich2 ) + return true; + if ( nWhich1 > nWhich2 ) + return false; + if (rHt1.GetStart() < rHt2.GetStart()) + return true; + if (rHt1.GetStart() > rHt2.GetStart()) + return false; + if ( RES_TXTATR_CHARFMT == nWhich1 ) + { + const sal_uInt16 nS1 = + static_txtattr_cast(rHt1).GetSortNumber(); + const sal_uInt16 nS2 = + static_txtattr_cast(rHt2).GetSortNumber(); + if ( nS1 != nS2 ) // robust + return nS1 < nS2; + } + const sal_Int32 nEnd1 = rHt1.GetAnyEnd(); + const sal_Int32 nEnd2 = rHt2.GetAnyEnd(); + if ( nEnd1 > nEnd2 ) + return true; + if ( nEnd1 < nEnd2 ) + return false; + return reinterpret_cast(&rHt1) < reinterpret_cast(&rHt2); +} + +/// sort order: End, Start(reverse), Which +/// (char style: sort number), at last the pointer(reverse) +bool CompareSwpHtEnd::operator()( sal_Int32 nEndPos, const SwTextAttr* rhs ) const +{ + return nEndPos < rhs->GetAnyEnd(); +} +bool CompareSwpHtEnd::operator()( const SwTextAttr* lhs, const SwTextAttr* rhs ) const +{ + const SwTextAttr &rHt1 = *lhs; + const SwTextAttr &rHt2 = *rhs; + const sal_Int32 nHt1 = rHt1.GetAnyEnd(); + const sal_Int32 nHt2 = rHt2.GetAnyEnd(); + if ( nHt1 == nHt2 ) + { + if ( rHt1.GetStart() == rHt2.GetStart() ) + { + const sal_uInt16 nWhich1 = rHt1.Which(); + const sal_uInt16 nWhich2 = rHt2.Which(); + if ( nWhich1 == nWhich2 ) + { + if ( RES_TXTATR_CHARFMT == nWhich1 ) + { + const sal_uInt16 nS1 = + static_txtattr_cast(rHt1).GetSortNumber(); + const sal_uInt16 nS2 = + static_txtattr_cast(rHt2).GetSortNumber(); + if ( nS1 != nS2 ) // robust + return nS1 > nS2; + } + + return reinterpret_cast(&rHt1) > reinterpret_cast(&rHt2); + } + // order is important! for requirements see hintids.hxx + return ( nWhich1 < nWhich2 ); + } + else + return ( rHt1.GetStart() > rHt2.GetStart() ); + } + return ( nHt1 < nHt2 ); +} + +void SwpHints::Insert(SwTextAttr* pHt) +{ + assert(std::find(m_HintsByStart.begin(), m_HintsByStart.end(), pHt) + == m_HintsByStart.end()); // "Insert: hint already in HtStart" + assert( pHt->m_pHints == nullptr ); + pHt->m_pHints = this; + + if (m_bStartMapNeedsSorting) + ResortStartMap(); + if (m_bEndMapNeedsSorting) + ResortEndMap(); + if (m_bWhichMapNeedsSorting) + ResortWhichMap(); + + auto it1 = std::lower_bound(m_HintsByStart.begin(), m_HintsByStart.end(), pHt, CompareSwpHtStart); + m_HintsByStart.insert(it1, pHt); + + auto it2 = std::lower_bound(m_HintsByEnd.begin(), m_HintsByEnd.end(), pHt, CompareSwpHtEnd()); + m_HintsByEnd.insert(it2, pHt); + + auto it3 = std::lower_bound(m_HintsByWhichAndStart.begin(), m_HintsByWhichAndStart.end(), pHt, CompareSwpHtWhichStart()); + m_HintsByWhichAndStart.insert(it3, pHt); +} + +bool SwpHints::Contains( const SwTextAttr *pHt ) const +{ + // DO NOT use find() or CHECK here! + // if called from SwTextNode::InsertItem, pHt has already been deleted, + // so it cannot be dereferenced + return std::find(m_HintsByStart.begin(), m_HintsByStart.end(), pHt) + != m_HintsByStart.end(); +} + +#ifdef DBG_UTIL + +#define CHECK_ERR(cond, text) \ + if(!(cond)) \ + { \ + SAL_WARN("sw.core", text); \ + Resort(); \ + return false; \ + } + +bool SwpHints::Check(bool bPortionsMerged) const +{ + // 1) both arrays have same size + CHECK_ERR( m_HintsByStart.size() == m_HintsByEnd.size(), + "HintsCheck: wrong sizes" ); + sal_Int32 nLastStart = 0; + sal_Int32 nLastEnd = 0; + + const SwTextAttr *pLastStart = nullptr; + const SwTextAttr *pLastEnd = nullptr; + o3tl::sorted_vector RsidOnlyAutoFormats; + if (bPortionsMerged) + { + for (size_t i = 0; i < Count(); ++i) + { + SwTextAttr const*const pHint(m_HintsByStart[i]); + if (RES_TXTATR_AUTOFMT == pHint->Which()) + { + std::shared_ptr const pSet( + pHint->GetAutoFormat().GetStyleHandle()); + if (pSet->Count() == 1 && pSet->GetItem(RES_CHRATR_RSID, false)) + { + RsidOnlyAutoFormats.insert(pHint); + } + } + } + } + + for( size_t i = 0; i < Count(); ++i ) + { + // --- check Starts --- + + // 2a) valid pointer? depends on overwriting freed mem with 0xFF + const SwTextAttr *pHt = m_HintsByStart[i]; + CHECK_ERR( 0xFF != *reinterpret_cast(pHt), "HintsCheck: start ptr was deleted" ); + + // 3a) start sort order? + sal_Int32 nIdx = pHt->GetStart(); + CHECK_ERR( nIdx >= nLastStart, "HintsCheck: starts are unsorted" ); + + // 4a) IsLessStart consistency + if( pLastStart ) + CHECK_ERR( CompareSwpHtStart( pLastStart, pHt ), "HintsCheck: IsLastStart" ); + + nLastStart = nIdx; + pLastStart = pHt; + + // --- check Ends --- + + // 2b) valid pointer? see DELETEFF + const SwTextAttr *pHtEnd = m_HintsByEnd[i]; + CHECK_ERR( 0xFF != *reinterpret_cast(pHtEnd), "HintsCheck: end ptr was deleted" ); + + // 3b) end sort order? + nIdx = pHtEnd->GetAnyEnd(); + CHECK_ERR( nIdx >= nLastEnd, "HintsCheck: ends are unsorted" ); + + // 4b) IsLessEnd consistency + if( pLastEnd ) + CHECK_ERR( CompareSwpHtEnd()( pLastEnd, pHtEnd ), "HintsCheck: IsLastEnd" ); + + nLastEnd = nIdx; + pLastEnd = pHtEnd; + + // --- cross checks --- + + // 5) same pointers in both arrays + if (std::lower_bound(m_HintsByStart.begin(), m_HintsByStart.end(), const_cast(pHt), CompareSwpHtStart) == m_HintsByStart.end()) + nIdx = COMPLETE_STRING; + + CHECK_ERR( COMPLETE_STRING != nIdx, "HintsCheck: no GetStartOf" ); + + // 6) same pointers in both arrays + if (std::lower_bound(m_HintsByEnd.begin(), m_HintsByEnd.end(), const_cast(pHt), CompareSwpHtEnd()) == m_HintsByEnd.end()) + nIdx = COMPLETE_STRING; + + CHECK_ERR( COMPLETE_STRING != nIdx, "HintsCheck: no GetEndOf" ); + + // 7a) character attributes in array? + sal_uInt16 nWhich = pHt->Which(); + CHECK_ERR( !isCHRATR(nWhich), + "HintsCheck: Character attribute in start array" ); + + // 7b) character attributes in array? + nWhich = pHtEnd->Which(); + CHECK_ERR( !isCHRATR(nWhich), + "HintsCheck: Character attribute in end array" ); + + // 8) style portion check + const SwTextAttr* pHtThis = m_HintsByStart[i]; + const SwTextAttr* pHtLast = i > 0 ? m_HintsByStart[i-1] : nullptr; + CHECK_ERR( (0 == i) + || ( (RES_TXTATR_CHARFMT != pHtLast->Which()) + && (RES_TXTATR_AUTOFMT != pHtLast->Which())) + || ( (RES_TXTATR_CHARFMT != pHtThis->Which()) + && (RES_TXTATR_AUTOFMT != pHtThis->Which())) + || (pHtThis->GetStart() >= *pHtLast->End()) // no overlap + || ( ( (pHtThis->GetStart() == pHtLast->GetStart()) + && (*pHtThis->End() == *pHtLast->End()) + ) // same range + && ( (pHtThis->Which() != RES_TXTATR_AUTOFMT) + || (pHtLast->Which() != RES_TXTATR_AUTOFMT) + ) // never two AUTOFMT on same range + && ( (pHtThis->Which() != RES_TXTATR_CHARFMT) + || (pHtLast->Which() != RES_TXTATR_CHARFMT) + || (static_txtattr_cast(pHtThis) + ->GetSortNumber() != + static_txtattr_cast(pHtLast) + ->GetSortNumber()) + ) // multiple CHARFMT on same range need distinct sorter + ) + || (pHtThis->GetStart() == *pHtThis->End()), // this empty + "HintsCheck: Portion inconsistency. " + "This can be temporarily ok during undo operations" ); + + // 8 1/2) format ignore start/end flag check + // (problems because MergePortions buggy or not called) + if (bPortionsMerged) + { + if (RES_TXTATR_AUTOFMT == pHt->Which() || + RES_TXTATR_CHARFMT == pHt->Which()) + { + // mostly ignore the annoying no-length hints + // BuildPortions inserts these in the middle of an existing one + bool const bNoLength(pHt->GetStart() == *pHt->End()); + bool bNeedContinuation(!bNoLength && pHt->IsFormatIgnoreEnd()); + bool bForbidContinuation(!bNoLength && !bNeedContinuation); + if (RES_TXTATR_AUTOFMT == pHt->Which()) + { + if (RsidOnlyAutoFormats.find(pHt) != RsidOnlyAutoFormats.end()) + { + assert(pHt->IsFormatIgnoreStart()); + bNeedContinuation = false; + // don't forbid continuation - may be other hint here! + } + } + if (bNeedContinuation || bForbidContinuation) + { + bool bFound(false); + for (size_t j = i + 1; j < Count(); ++j) + { + SwTextAttr *const pOther(m_HintsByStart[j]); + if (pOther->GetStart() > *pHt->End()) + { + break; // done + } + else if (pOther->GetStart() == pOther->GetAnyEnd()) + { + continue; // empty hint: ignore + } + else if (pOther->GetStart() == *pHt->End()) + { + if (RES_TXTATR_AUTOFMT == pOther->Which() || + RES_TXTATR_CHARFMT == pOther->Which()) + { // multiple charfmt on same range must all match + if (bNeedContinuation) + { + assert(pOther->IsFormatIgnoreStart()); + bFound = true; + } + else if (bForbidContinuation && + (RsidOnlyAutoFormats.find(pOther) == + RsidOnlyAutoFormats.end())) + { + assert(!pOther->IsFormatIgnoreStart()); + } + } + } + } + if (bNeedContinuation) + { + assert(bFound); // ? can this happen temp. during undo? + } + } + } + else + { + assert(!pHt->IsFormatIgnoreStart()); + assert(!pHt->IsFormatIgnoreEnd()); + } + } + + // 9) nesting portion check + if (pHtThis->IsNesting()) + { + for (size_t j = 0; j < i; ++j) + { + SwTextAttr const * const pOther( m_HintsByStart[j] ); + if (pOther->IsNesting()) + { + SwComparePosition cmp = ComparePosition( + pHtThis->GetStart(), *pHtThis->End(), + pOther->GetStart(), *pOther->End()); + CHECK_ERR( (SwComparePosition::OverlapBefore != cmp) && + (SwComparePosition::OverlapBehind != cmp), + "HintsCheck: overlapping nesting hints!!!" ); + } + } + } + + // 10) dummy char check (unfortunately cannot check SwTextNode::m_Text) + if (pHtThis->HasDummyChar()) + { + for ( size_t j = 0; j < i; ++j ) + { + SwTextAttr const * const pOther( m_HintsByStart[j] ); + if (pOther->HasDummyChar()) + { + CHECK_ERR( (pOther->GetStart() != pHtThis->GetStart()), + "HintsCheck: multiple hints claim same CH_TXTATR!"); + } + } + } + } + return true; +} + +#endif /* DBG_UTIL */ + +// Resort() is called before every Insert and Delete. +// Various SwTextNode methods modify hints in a way that violates the +// sort order of the m_HintsByStart, m_HintsByEnd arrays, so this method is needed +// to restore the order. + +void SwpHints::Resort() const +{ + auto & rStartMap = const_cast(this)->m_HintsByStart; + std::sort(rStartMap.begin(), rStartMap.end(), CompareSwpHtStart); + auto & rEndMap = const_cast(this)->m_HintsByEnd; + std::sort(rEndMap.begin(), rEndMap.end(), CompareSwpHtEnd()); + auto & rWhichStartMap = const_cast(this)->m_HintsByWhichAndStart; + std::sort(rWhichStartMap.begin(), rWhichStartMap.end(), CompareSwpHtWhichStart()); + m_bStartMapNeedsSorting = false; + m_bEndMapNeedsSorting = false; + m_bWhichMapNeedsSorting = false; +} + +void SwpHints::ResortStartMap() const +{ + auto & rStartMap = const_cast(this)->m_HintsByStart; + std::sort(rStartMap.begin(), rStartMap.end(), CompareSwpHtStart); + m_bStartMapNeedsSorting = false; +} + +void SwpHints::ResortEndMap() const +{ + auto & rEndMap = const_cast(this)->m_HintsByEnd; + std::sort(rEndMap.begin(), rEndMap.end(), CompareSwpHtEnd()); + m_bEndMapNeedsSorting = false; +} + +void SwpHints::ResortWhichMap() const +{ + m_bWhichMapNeedsSorting = false; + auto & rWhichStartMap = const_cast(this)->m_HintsByWhichAndStart; + std::sort(rWhichStartMap.begin(), rWhichStartMap.end(), CompareSwpHtWhichStart()); +} + +size_t SwpHints::GetFirstPosSortedByWhichAndStart( sal_uInt16 nWhich ) const +{ + if (m_bWhichMapNeedsSorting) + ResortWhichMap(); + auto it = std::lower_bound(m_HintsByWhichAndStart.begin(), m_HintsByWhichAndStart.end(), nWhich, CompareSwpHtWhichStart()); + if ( it == m_HintsByWhichAndStart.end() ) + return SAL_MAX_SIZE; + return it - m_HintsByWhichAndStart.begin(); +} + +int SwpHints::GetLastPosSortedByEnd( sal_Int32 nEndPos ) const +{ + if (m_bEndMapNeedsSorting) + ResortEndMap(); + auto it = std::upper_bound(m_HintsByEnd.begin(), m_HintsByEnd.end(), nEndPos, CompareSwpHtEnd()); + return it - m_HintsByEnd.begin() - 1; +} + +size_t SwpHints::GetIndexOf( const SwTextAttr *pHt ) const +{ + if (m_bStartMapNeedsSorting) + ResortStartMap(); + auto it = std::lower_bound(m_HintsByStart.begin(), m_HintsByStart.end(), const_cast(pHt), CompareSwpHtStart); + if ( it == m_HintsByStart.end() || *it != pHt ) + return SAL_MAX_SIZE; + return it - m_HintsByStart.begin(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/ndtxt.cxx b/sw/source/core/txtnode/ndtxt.cxx new file mode 100644 index 000000000..62b817138 --- /dev/null +++ b/sw/source/core/txtnode/ndtxt.cxx @@ -0,0 +1,5326 @@ +/* -*- 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 +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace ::com::sun::star; + +typedef std::vector SwpHts; + + +// unfortunately everyone can change Hints without ensuring order or the linking between them +#ifdef DBG_UTIL +#define CHECK_SWPHINTS(pNd) { if( pNd->GetpSwpHints() && \ + !pNd->GetDoc()->IsInReading() ) \ + pNd->GetpSwpHints()->Check(true); } +#define CHECK_SWPHINTS_IF_FRM(pNd) { if( pNd->GetpSwpHints() && \ + !pNd->GetDoc()->IsInReading() ) \ + pNd->GetpSwpHints()->Check(getLayoutFrame(nullptr, nullptr, nullptr) != nullptr); } +#else +#define CHECK_SWPHINTS(pNd) +#define CHECK_SWPHINTS_IF_FRM(pNd) +#endif + +SwTextNode *SwNodes::MakeTextNode( const SwNodeIndex & rWhere, + SwTextFormatColl *pColl, bool const bNewFrames) +{ + OSL_ENSURE( pColl, "Collection pointer is 0." ); + + SwTextNode *pNode = new SwTextNode( rWhere, pColl, nullptr ); + + SwNodeIndex aIdx( *pNode ); + + // call method only for the document nodes array + if ( IsDocNodes() ) + UpdateOutlineNode(*pNode); + + // if there is no layout or it is in a hidden section, MakeFrames is not needed + const SwSectionNode* pSectNd; + if (!bNewFrames || + !GetDoc()->getIDocumentLayoutAccess().GetCurrentViewShell() || + ( nullptr != (pSectNd = pNode->FindSectionNode()) && + pSectNd->GetSection().IsHiddenFlag() )) + return pNode; + + SwNodeIndex aTmp( rWhere ); + do { + // max. 2 loops: + // 1. take the successor + // 2. take the predecessor + + SwNode * pNd = & aTmp.GetNode(); + switch (pNd->GetNodeType()) + { + case SwNodeType::Table: + static_cast(pNd)->MakeFramesForAdjacentContentNode(aIdx); + return pNode; + + case SwNodeType::Section: + if( static_cast(pNd)->GetSection().IsHidden() || + static_cast(pNd)->IsContentHidden() ) + { + SwNodeIndex aTmpIdx( *pNode ); + pNd = FindPrvNxtFrameNode( aTmpIdx, pNode ); + if( !pNd ) + return pNode; + aTmp = *pNd; + break; + } + static_cast(pNd)->MakeFramesForAdjacentContentNode(aIdx); + return pNode; + + case SwNodeType::Text: + case SwNodeType::Grf: + case SwNodeType::Ole: + static_cast(pNd)->MakeFramesForAdjacentContentNode(*pNode); + return pNode; + + case SwNodeType::End: + if( pNd->StartOfSectionNode()->IsSectionNode() && + aTmp.GetIndex() < rWhere.GetIndex() ) + { + if( pNd->StartOfSectionNode()->GetSectionNode()->GetSection().IsHiddenFlag()) + { + if( !GoPrevSection( &aTmp, true, false ) || + aTmp.GetNode().FindTableNode() != + pNode->FindTableNode() ) + return pNode; + } + else + aTmp = *pNd->StartOfSectionNode(); + break; + } + else if( pNd->StartOfSectionNode()->IsTableNode() && + aTmp.GetIndex() < rWhere.GetIndex() ) + { + // after a table node + aTmp = *pNd->StartOfSectionNode(); + break; + } + [[fallthrough]]; + default: + if( rWhere == aTmp ) + aTmp -= 2; + else + return pNode; + break; + } + } while( true ); +} + +SwTextNode::SwTextNode( const SwNodeIndex &rWhere, SwTextFormatColl *pTextColl, const SfxItemSet* pAutoAttr ) +: SwContentNode( rWhere, SwNodeType::Text, pTextColl ), + m_Text(), + m_pParaIdleData_Impl(nullptr), + m_bContainsHiddenChars(false), + m_bHiddenCharsHidePara(false), + m_bRecalcHiddenCharFlags(false), + m_bLastOutlineState( false ), + m_bNotifiable( false ), + mbEmptyListStyleSetDueToSetOutlineLevelAttr( false ), + mbInSetOrResetAttr( false ), + m_pNumStringCache(), + m_wXParagraph(), + maFillAttributes() +{ + InitSwParaStatistics( true ); + + if( pAutoAttr ) + SetAttr( *pAutoAttr ); + + if (!IsInList() && GetNumRule() && !GetListId().isEmpty()) + { + // #i101516# + // apply paragraph style's assigned outline style list level as + // list level of the paragraph, if it has none set already. + if ( !HasAttrListLevel() && + pTextColl && pTextColl->IsAssignedToListLevelOfOutlineStyle() ) + { + SetAttrListLevel( pTextColl->GetAssignedOutlineStyleLevel() ); + } + AddToList(); + } + GetNodes().UpdateOutlineNode(*this); + + m_bNotifiable = true; + + m_bContainsHiddenChars = m_bHiddenCharsHidePara = false; + m_bRecalcHiddenCharFlags = true; +} + +SwTextNode::~SwTextNode() +{ + // delete only removes the pointer not the array elements! + if ( m_pSwpHints ) + { + // do not delete attributes twice when those delete their content + std::unique_ptr pTmpHints(std::move(m_pSwpHints)); + + for( size_t j = pTmpHints->Count(); j; ) + { + // first remove the attribute from the array otherwise + // if would delete itself + DestroyAttr( pTmpHints->Get( --j ) ); + } + } + + // must be removed from outline nodes by now +#if OSL_DEBUG_LEVEL > 0 && !defined NDEBUG + SwOutlineNodes::size_type foo; + assert(!GetNodes().GetOutLineNds().Seek_Entry(this, &foo)); +#endif + + RemoveFromList(); + + InitSwParaStatistics( false ); + DelFrames(nullptr); // must be called here while it's still a SwTextNode + DelFrames_TextNodePart(); +} + +void SwTextNode::FileLoadedInitHints() +{ + if (m_pSwpHints) + { + m_pSwpHints->MergePortions(*this); + } +} + +SwContentFrame *SwTextNode::MakeFrame( SwFrame* pSib ) +{ + SwContentFrame *pFrame = sw::MakeTextFrame(*this, pSib, sw::FrameMode::New); + return pFrame; +} + +sal_Int32 SwTextNode::Len() const +{ + return m_Text.getLength(); +} + +// After a split node, it's necessary to actualize the ref-pointer of the ftnfrms. +static void lcl_ChangeFootnoteRef( SwTextNode &rNode ) +{ + SwpHints *pSwpHints = rNode.GetpSwpHints(); + if( pSwpHints && rNode.GetDoc()->getIDocumentLayoutAccess().GetCurrentViewShell() ) + { + SwContentFrame* pFrame = nullptr; + // OD 07.11.2002 #104840# - local variable to remember first footnote + // of node in order to invalidate position of its first content. + // Thus, in its it will checked its position relative to its reference. + SwFootnoteFrame* pFirstFootnoteOfNode = nullptr; + for( size_t j = pSwpHints->Count(); j; ) + { + SwTextAttr* pHt = pSwpHints->Get(--j); + if (RES_TXTATR_FTN == pHt->Which()) + { + if( !pFrame ) + { + pFrame = SwIterator(rNode).First(); + if (!pFrame) + return; + } + SwTextFootnote *pAttr = static_cast(pHt); + OSL_ENSURE( pAttr->GetStartNode(), "FootnoteAtr without StartNode." ); + SwNodeIndex aIdx( *pAttr->GetStartNode(), 1 ); + SwContentNode *pNd = aIdx.GetNode().GetContentNode(); + if ( !pNd ) + pNd = pFrame->GetAttrSet()->GetDoc()-> + GetNodes().GoNextSection( &aIdx, true, false ); + if ( !pNd ) + continue; + + SwIterator aIter(*pNd); + SwContentFrame* pContent = aIter.First(); + if( pContent ) + { + OSL_ENSURE( pContent->getRootFrame() == pFrame->getRootFrame(), + "lcl_ChangeFootnoteRef: Layout double?" ); + SwFootnoteFrame *pFootnote = pContent->FindFootnoteFrame(); + if( pFootnote && pFootnote->GetAttr() == pAttr ) + { + while( pFootnote->GetMaster() ) + pFootnote = pFootnote->GetMaster(); + // #104840# - remember footnote frame + pFirstFootnoteOfNode = pFootnote; + while ( pFootnote ) + { + pFootnote->SetRef( pFrame ); + pFootnote = pFootnote->GetFollow(); + static_cast(pFrame)->SetFootnote( true ); + } + } +#if OSL_DEBUG_LEVEL > 0 + while( nullptr != (pContent = aIter.Next()) ) + { + SwFootnoteFrame *pDbgFootnote = pContent->FindFootnoteFrame(); + OSL_ENSURE( !pDbgFootnote || pDbgFootnote->GetRef() == pFrame, + "lcl_ChangeFootnoteRef: Who's that guy?" ); + } +#endif + } + } + } // end of for-loop on + // #104840# - invalidate + if ( pFirstFootnoteOfNode ) + { + SwContentFrame* pContent = pFirstFootnoteOfNode->ContainsContent(); + if ( pContent ) + { + pContent->InvalidatePos_(); + } + } + } +} + +namespace sw { + +// check if there are flys on the existing frames (now on "pNode") +// that need to be moved to the new frames of "this" +void MoveMergedFlysAndFootnotes(std::vector const& rFrames, + SwTextNode const& rFirstNode, SwTextNode & rSecondNode, + bool isSplitNode) +{ + if (!isSplitNode) + { + lcl_ChangeFootnoteRef(rSecondNode); + } + for (sal_uLong nIndex = rSecondNode.GetIndex() + 1; ; ++nIndex) + { + SwNode *const pTmp(rSecondNode.GetNodes()[nIndex]); + if (pTmp->IsCreateFrameWhenHidingRedlines() || pTmp->IsEndNode()) + { + break; + } + else if (pTmp->IsStartNode()) + { + nIndex = pTmp->EndOfSectionIndex(); + } + else if (pTmp->GetRedlineMergeFlag() == SwNode::Merge::NonFirst + && pTmp->IsTextNode()) + { + lcl_ChangeFootnoteRef(*pTmp->GetTextNode()); + } + } + for (SwTextFrame *const pFrame : rFrames) + { + if (SwSortedObjs *const pObjs = pFrame->GetDrawObjs()) + { + std::vector objs; + objs.reserve(pObjs->size()); + for (SwAnchoredObject *const pObj : *pObjs) + { + objs.push_back(pObj); + } + for (SwAnchoredObject *const pObj : objs) + { + SwFrameFormat & rFormat(pObj->GetFrameFormat()); + SwFormatAnchor const& rAnchor(rFormat.GetAnchor()); + if (rFirstNode.GetIndex() < rAnchor.GetContentAnchor()->nNode.GetIndex()) + { + // move it to the new frame of "this" + rFormat.NotifyClients(&rAnchor, &rAnchor); + // note pObjs will be deleted if it becomes empty + assert(!pFrame->GetDrawObjs() || !pObjs->Contains(*pObj)); + } + } + } + } +} + +} // namespace + +SwTextNode *SwTextNode::SplitContentNode(const SwPosition & rPos, + std::function const*const pContentIndexRestore) +{ + bool isHide(false); + SwNode::Merge const eOldMergeFlag(GetRedlineMergeFlag()); + bool parentIsOutline = IsOutline(); + + // create a node "in front" of me + const sal_Int32 nSplitPos = rPos.nContent.GetIndex(); + const sal_Int32 nTextLen = m_Text.getLength(); + SwTextNode* const pNode = + MakeNewTextNode( rPos.nNode, false, nSplitPos==nTextLen ); + + // the first paragraph gets the XmlId, + // _except_ if it is empty and the second is not empty + if (nSplitPos != 0) { + pNode->RegisterAsCopyOf(*this, true); + if (nSplitPos == nTextLen) + { + RemoveMetadataReference(); + // NB: SwUndoSplitNode will call pNode->JoinNext, + // which is sufficient even in this case! + } + } + + ResetAttr( RES_PARATR_LIST_ISRESTART ); + ResetAttr( RES_PARATR_LIST_RESTARTVALUE ); + ResetAttr( RES_PARATR_LIST_ISCOUNTED ); + if ( GetNumRule() == nullptr || (parentIsOutline && !IsOutline()) ) + { + ResetAttr( RES_PARATR_LIST_ID ); + ResetAttr( RES_PARATR_LIST_LEVEL ); + } + + if ( HasWriterListeners() && !m_Text.isEmpty() && (nTextLen / 2) < nSplitPos ) + { + // optimization for SplitNode: If a split is at the end of a node then + // move the frames from the current to the new one and create new ones + // for the current one. + + // If fly frames are moved, they don't need to destroy their layout + // frames. Set a flag that is checked in SwTextFlyCnt::SetAnchor. + if ( HasHints() ) + { + pNode->GetOrCreateSwpHints().SetInSplitNode(true); + } + + // Move the first part of the content to the new node and delete + // it in the old node. + SwIndex aIdx( this ); + CutText( pNode, aIdx, nSplitPos ); + + if( GetWrong() ) + { + pNode->SetWrong( GetWrong()->SplitList( nSplitPos ) ); + } + SetWrongDirty(WrongState::TODO); + + if( GetGrammarCheck() ) + { + pNode->SetGrammarCheck( GetGrammarCheck()->SplitGrammarList( nSplitPos ) ); + } + SetGrammarCheckDirty( true ); + + SetWordCountDirty( true ); + + if( GetSmartTags() ) + { + pNode->SetSmartTags( GetSmartTags()->SplitList( nSplitPos ) ); + } + SetSmartTagDirty( true ); + + if ( pNode->HasHints() ) + { + if ( pNode->m_pSwpHints->CanBeDeleted() ) + { + pNode->m_pSwpHints.reset(); + } + else + { + pNode->m_pSwpHints->SetInSplitNode(false); + } + + // All fly frames anchored as char that are moved to the new + // node must have their layout frames deleted. + // This comment is sort of silly because we actually delete the + // layout frames of those which were not moved? + // JP 01.10.96: delete all empty and not-to-be-expanded attributes + if ( HasHints() ) + { + for ( size_t j = m_pSwpHints->Count(); j; ) + { + SwTextAttr* const pHt = m_pSwpHints->Get( --j ); + if ( RES_TXTATR_FLYCNT == pHt ->Which() ) + { + pHt->GetFlyCnt().GetFrameFormat()->DelFrames(); + } + else if ( pHt->DontExpand() ) + { + const sal_Int32* const pEnd = pHt->GetEnd(); + if (pEnd && pHt->GetStart() == *pEnd ) + { + // delete it! + m_pSwpHints->DeleteAtPos( j ); + DestroyAttr( pHt ); + } + } + } + } + + } + + if (pContentIndexRestore) + { // call before making frames and before RegisterToNode + (*pContentIndexRestore)(pNode, sw::mark::RestoreMode::NonFlys); + } + if (eOldMergeFlag != SwNode::Merge::None) + { // clear before making frames and before RegisterToNode + SetRedlineMergeFlag(SwNode::Merge::None); + } // now RegisterToNode will set merge flags in both nodes properly! + + std::vector frames; + SwIterator aIter(*this); + for (SwTextFrame* pFrame = aIter.First(); pFrame; pFrame = aIter.Next()) + { + if (pFrame->getRootFrame()->IsHideRedlines()) + { + isHide = true; + } + frames.push_back(pFrame); + } + for (SwTextFrame * pFrame : frames) + { + pFrame->RegisterToNode( *pNode ); + if (!pFrame->IsFollow() && pFrame->GetOffset()) + { + pFrame->SetOffset( TextFrameIndex(0) ); + } + } + + if ( IsInCache() ) + { + SwFrame::GetCache().Delete( this ); + SetInCache( false ); + } + + if ( HasHints() ) + { + MoveTextAttr_To_AttrSet(); + } + // in case there are frames, the RegisterToNode has set the merge flag + pNode->MakeFramesForAdjacentContentNode(*this); + lcl_ChangeFootnoteRef( *this ); + if (pContentIndexRestore) + { // call after making frames; listeners will take care of adding to the right frame + (*pContentIndexRestore)(pNode, sw::mark::RestoreMode::Flys); + } + if (eOldMergeFlag != SwNode::Merge::None) + { + MoveMergedFlysAndFootnotes(frames, *pNode, *this, true); + } + } + else + { + SwWrongList *pList = GetWrong(); + SetWrong( nullptr, false ); + SetWrongDirty(WrongState::TODO); + + SwGrammarMarkUp *pList3 = GetGrammarCheck(); + SetGrammarCheck( nullptr, false ); + SetGrammarCheckDirty( true ); + + SetWordCountDirty( true ); + + SwWrongList *pList2 = GetSmartTags(); + SetSmartTags( nullptr, false ); + SetSmartTagDirty( true ); + + SwIndex aIdx( this ); + CutText( pNode, aIdx, nSplitPos ); + + // JP 01.10.96: delete all empty and not-to-be-expanded attributes + if ( HasHints() ) + { + for ( size_t j = m_pSwpHints->Count(); j; ) + { + SwTextAttr* const pHt = m_pSwpHints->Get( --j ); + const sal_Int32* const pEnd = pHt->GetEnd(); + if ( pHt->DontExpand() && pEnd && (pHt->GetStart() == *pEnd) ) + { + // delete it! + m_pSwpHints->DeleteAtPos( j ); + DestroyAttr( pHt ); + } + } + MoveTextAttr_To_AttrSet(); + } + + if( pList ) + { + pNode->SetWrong( pList->SplitList( nSplitPos ) ); + SetWrong( pList, false ); + } + + if( pList3 ) + { + pNode->SetGrammarCheck( pList3->SplitGrammarList( nSplitPos ) ); + SetGrammarCheck( pList3, false ); + } + + if( pList2 ) + { + pNode->SetSmartTags( pList2->SplitList( nSplitPos ) ); + SetSmartTags( pList2, false ); + } + + if (pContentIndexRestore) + { // call before making frames and before RegisterToNode + (*pContentIndexRestore)(pNode, sw::mark::RestoreMode::NonFlys); + } + + std::vector frames; + SwIterator aIter(*this); + for (SwTextFrame * pFrame = aIter.First(); pFrame; pFrame = aIter.Next()) + { + frames.push_back(pFrame); + if (pFrame->getRootFrame()->IsHideRedlines()) + { + isHide = true; + } + } + bool bNonMerged(false); + bool bRecreateThis(false); + for (SwTextFrame * pFrame : frames) + { + // sw_redlinehide: for this to work properly with hidden nodes, + // the frame needs to listen on them too. + // also: have to check the frame; this->GetRedlineMergeFlag() + // is None in case there's a delete redline inside the paragraph, + // but that could still result in a merged frame after split... + if (pFrame->GetMergedPara()) + { + // Can't special case this == First here - that could (if + // both nodes are still merged by redline) lead to + // duplicate frames on "this". + // Update the extents with new node; also inits merge flag, + // so the MakeFramesForAdjacentContentNode below respects it + pFrame->RegisterToNode(*pNode); + if (pFrame->GetText().isEmpty()) + { + // turns out it's empty - in this case, it was not + // invalidated because Cut didn't sent it any hints, + // so we have to invalidate it here! + pFrame->Prepare(PrepareHint::Clear, nullptr, false); + } + if (!pFrame->GetMergedPara() || + !pFrame->GetMergedPara()->listener.IsListeningTo(this)) + { + // it's no longer listening - need to recreate frame + // (note this is idempotent, can be done once per frame) + SetRedlineMergeFlag(SwNode::Merge::None); + bRecreateThis = true; + } + } + else + { + bNonMerged = true; + } + } + assert(!(bNonMerged && bRecreateThis)); // 2 layouts not handled yet - maybe best to simply use the other branch then? + if (!frames.empty() && bNonMerged) + { + // the existing frame on "this" should have been updated by Cut + MakeFramesForAdjacentContentNode(*pNode); + lcl_ChangeFootnoteRef(*pNode); + } + else if (bRecreateThis) + { + assert(pNode->HasWriterListeners()); // was just moved there + pNode->MakeFramesForAdjacentContentNode(*this); + lcl_ChangeFootnoteRef(*this); + } + + if (pContentIndexRestore) + { // call after making frames; listeners will take care of adding to the right frame + (*pContentIndexRestore)(pNode, sw::mark::RestoreMode::Flys); + } + + if (bRecreateThis) + { + MoveMergedFlysAndFootnotes(frames, *pNode, *this, true); + } + } + +#ifndef NDEBUG + if (isHide) // otherwise flags won't be set anyway + { + // First + // -> First,NonFirst + // -> First,Hidden + // -> None,First + // Hidden + // -> Hidden,Hidden (if still inside merge rl) + // -> NonFirst,First (if redline was split) + // NonFirst + // -> NonFirst,First (if split after end of "incoming" redline & + // before start of "outgoing" redline) + // -> NonFirst,None (if split after end of "incoming" redline) + // -> NonFirst,Hidden (if split after start of "outgoing" redline) + // -> Hidden, NonFirst (if split before end of "incoming" redline) + // None + // -> None,None + // -> First,NonFirst (if splitting inside a delete redline) + SwNode::Merge const eFirst(pNode->GetRedlineMergeFlag()); + SwNode::Merge const eSecond(GetRedlineMergeFlag()); + switch (eOldMergeFlag) + { + case Merge::First: + assert((eFirst == Merge::First && eSecond == Merge::NonFirst) + || (eFirst == Merge::First && eSecond == Merge::Hidden) + || (eFirst == Merge::None && eSecond == Merge::First)); + break; + case Merge::Hidden: + assert((eFirst == Merge::Hidden && eSecond == Merge::Hidden) + || (eFirst == Merge::NonFirst && eSecond == Merge::First) + // next ones can happen temp. in UndoDelete :( + || (eFirst == Merge::Hidden && eSecond == Merge::NonFirst) + || (eFirst == Merge::NonFirst && eSecond == Merge::None)); + break; + case Merge::NonFirst: + assert((eFirst == Merge::NonFirst && eSecond == Merge::First) + || (eFirst == Merge::NonFirst && eSecond == Merge::None) + || (eFirst == Merge::NonFirst && eSecond == Merge::Hidden) + || (eFirst == Merge::Hidden && eSecond == Merge::NonFirst)); + break; + case Merge::None: + assert((eFirst == Merge::None && eSecond == Merge::None) + || (eFirst == Merge::First && eSecond == Merge::NonFirst)); + break; + } + } +#else + (void) isHide; +#endif + + { + // Send Hint for PageDesc. This should be done in the Layout when + // pasting the frames, but that causes other problems that look + // expensive to solve. + const SfxPoolItem *pItem; + if( HasWriterListeners() && SfxItemState::SET == pNode->GetSwAttrSet(). + GetItemState( RES_PAGEDESC, true, &pItem ) ) + { + pNode->ModifyNotification( pItem, pItem ); + } + } + return pNode; +} + +void SwTextNode::MoveTextAttr_To_AttrSet() +{ + OSL_ENSURE( m_pSwpHints, "MoveTextAttr_To_AttrSet without SwpHints?" ); + for ( size_t i = 0; m_pSwpHints && i < m_pSwpHints->Count(); ++i ) + { + SwTextAttr *pHt = m_pSwpHints->Get(i); + + if( pHt->GetStart() ) + break; + + const sal_Int32* pHtEndIdx = pHt->GetEnd(); + + if( !pHtEndIdx ) + continue; + + if (*pHtEndIdx < m_Text.getLength() || pHt->IsCharFormatAttr()) + break; + + if( !pHt->IsDontMoveAttr() && + SetAttr( pHt->GetAttr() ) ) + { + m_pSwpHints->DeleteAtPos(i); + DestroyAttr( pHt ); + --i; + } + } + +} + +namespace sw { + +/// if first node is deleted & second survives, then the first node's frame +/// will be deleted too; prevent this by moving the frame to the second node +/// if necessary. +void MoveDeletedPrevFrames(const SwTextNode & rDeletedPrev, SwTextNode & rNode) +{ + std::vector frames; + SwIterator aIter(rDeletedPrev); + for (SwTextFrame* pFrame = aIter.First(); pFrame; pFrame = aIter.Next()) + { + if (pFrame->getRootFrame()->IsHideRedlines()) + { + frames.push_back(pFrame); + } + } + { + auto frames2(frames); + SwIterator aIt(rNode); + for (SwTextFrame* pFrame = aIt.First(); pFrame; pFrame = aIt.Next()) + { + if (pFrame->getRootFrame()->IsHideRedlines()) + { + auto const it(std::find(frames2.begin(), frames2.end(), pFrame)); + assert(it != frames2.end()); + frames2.erase(it); + } + } + assert(frames2.empty()); + } + for (SwTextFrame *const pFrame : frames) + { + pFrame->RegisterToNode(rNode, true); + } +} + +// typical Join: +// None,Node->None +// None,First->First +// First,NonFirst->First +// NonFirst,First->NonFirst +// NonFirst,None->NonFirst + +/// if first node is First, its frames may need to be moved, never deleted. +/// if first node is NonFirst, second node's own frames (First/None) must be deleted +void CheckResetRedlineMergeFlag(SwTextNode & rNode, Recreate const eRecreateMerged) +{ + if (eRecreateMerged != sw::Recreate::No) + { + SwTextNode * pMergeNode(&rNode); + if (eRecreateMerged == sw::Recreate::Predecessor + // tdf#135018 check that there is a predecessor node, i.e. rNode + // isn't the first node after the body start node + && rNode.GetNodes()[rNode.GetIndex() - 1]->StartOfSectionIndex() != 0) + { + for (sal_uLong i = rNode.GetIndex() - 1; ; --i) + { + SwNode *const pNode(rNode.GetNodes()[i]); + assert(!pNode->IsStartNode()); + if (pNode->IsEndNode()) + { + i = pNode->StartOfSectionIndex(); + } + else if (pNode->IsTextNode()) + { + pMergeNode = pNode->GetTextNode(); // use predecessor to merge + break; + } + } + } + std::vector frames; + SwIterator aIter(*pMergeNode); + for (SwTextFrame* pFrame = aIter.First(); pFrame; pFrame = aIter.Next()) + { + if (pFrame->getRootFrame()->IsHideRedlines()) + { + frames.push_back(pFrame); + } + } + auto eMode(sw::FrameMode::Existing); + for (SwTextFrame * pFrame : frames) + { + SwTextNode & rFirstNode(pFrame->GetMergedPara() + ? *pFrame->GetMergedPara()->pFirstNode + : *pMergeNode); + assert(rFirstNode.GetIndex() <= rNode.GetIndex()); + pFrame->SetMergedPara(sw::CheckParaRedlineMerge( + *pFrame, rFirstNode, eMode)); + assert(pFrame->GetMergedPara()); + assert(pFrame->GetMergedPara()->listener.IsListeningTo(&rNode)); + assert(rNode.GetIndex() <= pFrame->GetMergedPara()->pLastNode->GetIndex()); + eMode = sw::FrameMode::New; // Existing is not idempotent! + } + } + else if (rNode.GetRedlineMergeFlag() != SwNode::Merge::None) + { + SwIterator aIter(rNode); + for (SwTextFrame * pFrame = aIter.First(); pFrame; pFrame = aIter.Next()) + { + if (auto const pMergedPara = pFrame->GetMergedPara()) + { + if (pMergedPara->pFirstNode == pMergedPara->pLastNode) + { + assert(pMergedPara->pFirstNode == &rNode); + rNode.SetRedlineMergeFlag(SwNode::Merge::None); + } + break; // checking once is enough + } + else if (pFrame->getRootFrame()->IsHideRedlines()) + { + rNode.SetRedlineMergeFlag(SwNode::Merge::None); + break; // checking once is enough + } + } + } +} + +} // namespace + +SwContentNode *SwTextNode::JoinNext() +{ + SwNodes& rNds = GetNodes(); + SwNodeIndex aIdx( *this ); + if( SwContentNode::CanJoinNext( &aIdx ) ) + { + SwDoc* pDoc = rNds.GetDoc(); + const std::shared_ptr pContentStore(sw::mark::ContentIdxStore::Create()); + pContentStore->Save(pDoc, aIdx.GetIndex(), SAL_MAX_INT32); + SwTextNode *pTextNode = aIdx.GetNode().GetTextNode(); + sal_Int32 nOldLen = m_Text.getLength(); + + // METADATA: merge + JoinMetadatable(*pTextNode, !Len(), !pTextNode->Len()); + + SwWrongList *pList = GetWrong(); + if( pList ) + { + pList->JoinList( pTextNode->GetWrong(), nOldLen ); + SetWrongDirty(WrongState::TODO); + SetWrong( nullptr, false ); + } + else + { + pList = pTextNode->GetWrong(); + if( pList ) + { + pList->Move( 0, nOldLen ); + SetWrongDirty(WrongState::TODO); + pTextNode->SetWrong( nullptr, false ); + } + } + + SwGrammarMarkUp *pList3 = GetGrammarCheck(); + if( pList3 ) + { + pList3->JoinGrammarList( pTextNode->GetGrammarCheck(), nOldLen ); + SetGrammarCheckDirty( true ); + SetGrammarCheck( nullptr, false ); + } + else + { + pList3 = pTextNode->GetGrammarCheck(); + if( pList3 ) + { + pList3->MoveGrammar( 0, nOldLen ); + SetGrammarCheckDirty( true ); + pTextNode->SetGrammarCheck( nullptr, false ); + } + } + + SwWrongList *pList2 = GetSmartTags(); + if( pList2 ) + { + pList2->JoinList( pTextNode->GetSmartTags(), nOldLen ); + SetSmartTagDirty( true ); + SetSmartTags( nullptr, false ); + } + else + { + pList2 = pTextNode->GetSmartTags(); + if( pList2 ) + { + pList2->Move( 0, nOldLen ); + SetSmartTagDirty( true ); + pTextNode->SetSmartTags( nullptr, false ); + } + } + + { // scope for SwIndex + pTextNode->CutText( this, SwIndex(pTextNode), pTextNode->Len() ); + } + // move all Bookmarks/TOXMarks + if( !pContentStore->Empty()) + pContentStore->Restore( pDoc, GetIndex(), nOldLen ); + + if( pTextNode->HasAnyIndex() ) + { + // move all ShellCursor/StackCursor/UnoCursor out of delete range + pDoc->CorrAbs( aIdx, SwPosition( *this ), nOldLen, true ); + } + SwNode::Merge const eOldMergeFlag(pTextNode->GetRedlineMergeFlag()); + rNds.Delete(aIdx); + SetWrong( pList, false ); + SetGrammarCheck( pList3, false ); + SetSmartTags( pList2, false ); + InvalidateNumRule(); + CheckResetRedlineMergeFlag(*this, eOldMergeFlag == SwNode::Merge::First + ? sw::Recreate::ThisNode + : sw::Recreate::No); + } + else { + OSL_FAIL( "No TextNode." ); + } + + return this; +} + +void SwTextNode::JoinPrev() +{ + SwNodes& rNds = GetNodes(); + SwNodeIndex aIdx( *this ); + if( SwContentNode::CanJoinPrev( &aIdx ) ) + { + SwDoc* pDoc = rNds.GetDoc(); + const std::shared_ptr pContentStore(sw::mark::ContentIdxStore::Create()); + pContentStore->Save( pDoc, aIdx.GetIndex(), SAL_MAX_INT32); + SwTextNode *pTextNode = aIdx.GetNode().GetTextNode(); + const sal_Int32 nLen = pTextNode->Len(); + + SwWrongList *pList = pTextNode->GetWrong(); + if( pList ) + { + pList->JoinList( GetWrong(), Len() ); + SetWrongDirty(WrongState::TODO); + pTextNode->SetWrong( nullptr, false ); + SetWrong( nullptr ); + } + else + { + pList = GetWrong(); + if( pList ) + { + pList->Move( 0, nLen ); + SetWrongDirty(WrongState::TODO); + SetWrong( nullptr, false ); + } + } + + SwGrammarMarkUp *pList3 = pTextNode->GetGrammarCheck(); + if( pList3 ) + { + pList3->JoinGrammarList( GetGrammarCheck(), Len() ); + SetGrammarCheckDirty( true ); + pTextNode->SetGrammarCheck( nullptr, false ); + SetGrammarCheck( nullptr ); + } + else + { + pList3 = GetGrammarCheck(); + if( pList3 ) + { + pList3->MoveGrammar( 0, nLen ); + SetGrammarCheckDirty( true ); + SetGrammarCheck( nullptr, false ); + } + } + + SwWrongList *pList2 = pTextNode->GetSmartTags(); + if( pList2 ) + { + pList2->JoinList( GetSmartTags(), Len() ); + SetSmartTagDirty( true ); + pTextNode->SetSmartTags( nullptr, false ); + SetSmartTags( nullptr ); + } + else + { + pList2 = GetSmartTags(); + if( pList2 ) + { + pList2->Move( 0, nLen ); + SetSmartTagDirty( true ); + SetSmartTags( nullptr, false ); + } + } + + { // scope for SwIndex + pTextNode->CutText( this, SwIndex(this), SwIndex(pTextNode), nLen ); + } + // move all Bookmarks/TOXMarks + if( !pContentStore->Empty() ) + pContentStore->Restore( pDoc, GetIndex() ); + + if( pTextNode->HasAnyIndex() ) + { + // move all ShellCursor/StackCursor/UnoCursor out of delete range + pDoc->CorrAbs( aIdx, SwPosition( *this ), nLen, true ); + } + SwNode::Merge const eOldMergeFlag(pTextNode->GetRedlineMergeFlag()); + if (eOldMergeFlag == SwNode::Merge::First + && !IsCreateFrameWhenHidingRedlines()) + { + sw::MoveDeletedPrevFrames(*pTextNode, *this); + } + rNds.Delete(aIdx); + SetWrong( pList, false ); + SetGrammarCheck( pList3, false ); + SetSmartTags( pList2, false ); + InvalidateNumRule(); + sw::CheckResetRedlineMergeFlag(*this, + eOldMergeFlag == SwNode::Merge::NonFirst + ? sw::Recreate::Predecessor + : sw::Recreate::No); + } + else { + OSL_FAIL( "No TextNode." ); + } +} + +// create an AttrSet with ranges for Frame-/Para/Char-attributes +void SwTextNode::NewAttrSet( SwAttrPool& rPool ) +{ + OSL_ENSURE( !mpAttrSet, "AttrSet is set after all" ); + SwAttrSet aNewAttrSet( rPool, aTextNodeSetRange ); + + // put names of parent style and conditional style: + const SwFormatColl* pAnyFormatColl = &GetAnyFormatColl(); + const SwFormatColl* pFormatColl = GetFormatColl(); + OUString sVal; + SwStyleNameMapper::FillProgName( pAnyFormatColl->GetName(), sVal, SwGetPoolIdFromName::TxtColl ); + SfxStringItem aAnyFormatColl( RES_FRMATR_STYLE_NAME, sVal ); + if ( pFormatColl != pAnyFormatColl ) + SwStyleNameMapper::FillProgName( pFormatColl->GetName(), sVal, SwGetPoolIdFromName::TxtColl ); + SfxStringItem aFormatColl( RES_FRMATR_CONDITIONAL_STYLE_NAME, sVal ); + aNewAttrSet.Put( aAnyFormatColl ); + aNewAttrSet.Put( aFormatColl ); + + aNewAttrSet.SetParent( &pAnyFormatColl->GetAttrSet() ); + mpAttrSet = GetDoc()->GetIStyleAccess().getAutomaticStyle( aNewAttrSet, IStyleAccess::AUTO_STYLE_PARA, &sVal ); +} + +// override SwIndexReg::Update => text hints do not need SwIndex for start/end! +void SwTextNode::Update( + SwIndex const & rPos, + const sal_Int32 nChangeLen, + const bool bNegative, + const bool bDelete ) +{ + SetAutoCompleteWordDirty( true ); + + std::unique_ptr pCollector; + const sal_Int32 nChangePos = rPos.GetIndex(); + + if ( HasHints() ) + { + if ( bNegative ) + { + std::vector aTextInputFields; + + const sal_Int32 nChangeEnd = nChangePos + nChangeLen; + for ( size_t n = 0; n < m_pSwpHints->Count(); ++n ) + { + bool bTextAttrChanged = false; + bool bStartOfTextAttrChanged = false; + SwTextAttr * const pHint = m_pSwpHints->GetWithoutResorting(n); + if ( pHint->GetStart() > nChangePos ) + { + if ( pHint->GetStart() > nChangeEnd ) + { + pHint->SetStart( pHint->GetStart() - nChangeLen ); + } + else + { + pHint->SetStart( nChangePos ); + } + bStartOfTextAttrChanged = true; + } + + const sal_Int32 * const pEnd = pHint->GetEnd(); + if (pEnd && *pEnd > nChangePos ) + { + if( *pEnd > nChangeEnd ) + { + pHint->SetEnd(*pEnd - nChangeLen); + } + else + { + pHint->SetEnd(nChangePos); + } + bTextAttrChanged = !bStartOfTextAttrChanged; + } + + if ( bTextAttrChanged + && pHint->Which() == RES_TXTATR_INPUTFIELD ) + { + SwTextInputField* pTextInputField = dynamic_cast(pHint); + if ( pTextInputField ) + aTextInputFields.push_back(pTextInputField); + } + } + + //wait until all the attribute positions are correct + //before updating the field contents + for (SwTextInputField* pTextInputField : aTextInputFields) + { + pTextInputField->UpdateFieldContent(); + } + + m_pSwpHints->MergePortions( *this ); + } + else + { + bool bNoExp = false; + bool bResort = false; + bool bMergePortionsNeeded = false; + const int coArrSz = RES_TXTATR_WITHEND_END - RES_CHRATR_BEGIN; + std::vector aTextInputFields; + + bool aDontExp[ coArrSz ] = {}; + + for ( size_t n = 0; n < m_pSwpHints->Count(); ++n ) + { + bool bTextAttrChanged = false; + SwTextAttr * const pHint = m_pSwpHints->GetWithoutResorting(n); + const sal_Int32 * const pEnd = pHint->GetEnd(); + if ( pHint->GetStart() >= nChangePos ) + { + pHint->SetStart( pHint->GetStart() + nChangeLen ); + if ( pEnd ) + { + pHint->SetEnd(*pEnd + nChangeLen); + } + } + else if ( pEnd && (*pEnd >= nChangePos) ) + { + if ( (*pEnd > nChangePos) || IsIgnoreDontExpand() ) + { + pHint->SetEnd(*pEnd + nChangeLen); + bTextAttrChanged = true; + } + else // *pEnd == nChangePos + { + const sal_uInt16 nWhich = pHint->Which(); + + OSL_ENSURE(!isCHRATR(nWhich), "Update: char attr hint?"); + if (!(isCHRATR(nWhich) || isTXTATR_WITHEND(nWhich))) + continue; + + const sal_uInt16 nWhPos = nWhich - RES_CHRATR_BEGIN; + + if( aDontExp[ nWhPos ] ) + continue; + + if ( pHint->DontExpand() ) + { + pHint->SetDontExpand( false ); + bResort = true; + // could have a continuation with IgnoreStart()... + if (pHint->IsFormatIgnoreEnd()) + { + bMergePortionsNeeded = true; + } + if ( pHint->IsCharFormatAttr() ) + { + bNoExp = true; + aDontExp[ RES_TXTATR_CHARFMT - RES_CHRATR_BEGIN ] = true; + aDontExp[ RES_TXTATR_INETFMT - RES_CHRATR_BEGIN ] = true; + } + else + aDontExp[ nWhPos ] = true; + } + else if( bNoExp ) + { + if (!pCollector) + { + pCollector.reset( new SwpHts ); + } + auto it = std::find_if(pCollector->begin(), pCollector->end(), + [nWhich](const SwTextAttr *pTmp) { return nWhich == pTmp->Which(); }); + if (it != pCollector->end()) + { + SwTextAttr *pTmp = *it; + pCollector->erase( it ); + SwTextAttr::Destroy( pTmp, GetDoc()->GetAttrPool() ); + } + SwTextAttr * const pTmp = + MakeTextAttr( *GetDoc(), + pHint->GetAttr(), nChangePos, nChangePos + nChangeLen); + pCollector->push_back( pTmp ); + } + else + { + pHint->SetEnd(*pEnd + nChangeLen); + bTextAttrChanged = true; + } + } + } + + if ( bTextAttrChanged + && pHint->Which() == RES_TXTATR_INPUTFIELD ) + { + SwTextInputField* pTextInputField = dynamic_cast(pHint); + if ( pTextInputField ) + aTextInputFields.push_back(pTextInputField); + } + } + + //wait until all the attribute positions are correct + //before updating the field contents + for (SwTextInputField* pTextInputField : aTextInputFields) + { + pTextInputField->UpdateFieldContent(); + } + + if (bMergePortionsNeeded) + { + m_pSwpHints->MergePortions(*this); // does Resort too + } + else if (bResort) + { + m_pSwpHints->Resort(); + } + } + } + + bool bSortMarks = false; + SwIndexReg aTmpIdxReg; + if ( !bNegative && !bDelete ) + { + const SwRedlineTable& rTable = GetDoc()->getIDocumentRedlineAccess().GetRedlineTable(); + for (SwRangeRedline* pRedl : rTable) + { + if ( pRedl->HasMark() ) + { + SwPosition* const pEnd = pRedl->End(); + if ( this == &pEnd->nNode.GetNode() && + *pRedl->GetPoint() != *pRedl->GetMark() ) + { + SwIndex & rIdx = pEnd->nContent; + if (nChangePos == rIdx.GetIndex()) + { + rIdx.Assign( &aTmpIdxReg, rIdx.GetIndex() ); + } + } + } + else if ( this == &pRedl->GetPoint()->nNode.GetNode() ) + { + SwIndex & rIdx = pRedl->GetPoint()->nContent; + if (nChangePos == rIdx.GetIndex()) + { + rIdx.Assign( &aTmpIdxReg, rIdx.GetIndex() ); + } + // the unused position must not be on a SwTextNode + bool const isOneUsed(&pRedl->GetBound() == pRedl->GetPoint()); + assert(!pRedl->GetBound(!isOneUsed).nNode.GetNode().IsTextNode()); + assert(!pRedl->GetBound(!isOneUsed).nContent.GetIdxReg()); (void)isOneUsed; + } + } + + // Bookmarks must never grow to either side, when editing (directly) + // to the left or right (i#29942)! Exception: if the bookmark has + // 2 positions and start == end, then expand it (tdf#96479) + { + bool bAtLeastOneBookmarkMoved = false; + bool bAtLeastOneExpandedBookmarkAtInsertionPosition = false; + // A text node already knows its marks via its SwIndexes. + o3tl::sorted_vector aSeenMarks; + const SwIndex* next; + for (const SwIndex* pIndex = GetFirstIndex(); pIndex; pIndex = next ) + { + next = pIndex->GetNext(); + const sw::mark::IMark* pMark = pIndex->GetMark(); + if (!pMark) + continue; + // Only handle bookmarks once, if they start and end at this node as well. + if (!aSeenMarks.insert(pMark).second) + continue; + const SwPosition* pEnd = &pMark->GetMarkEnd(); + SwIndex & rEndIdx = const_cast(pEnd->nContent); + if( this == &pEnd->nNode.GetNode() && + rPos.GetIndex() == rEndIdx.GetIndex() ) + { + if (&rEndIdx == next) // nasty corner case: + { // don't switch to iterating aTmpIdxReg! + next = rEndIdx.GetNext(); + } + // tdf#96479: if start == end, ignore the other position + // so it is moved! + rEndIdx.Assign( &aTmpIdxReg, rEndIdx.GetIndex() ); + bAtLeastOneBookmarkMoved = true; + } + else if ( !bAtLeastOneExpandedBookmarkAtInsertionPosition ) + { + if ( pMark->IsExpanded() ) + { + const SwPosition* pStart = &pMark->GetMarkStart(); + if ( this == &pStart->nNode.GetNode() + && rPos.GetIndex() == pStart->nContent.GetIndex() ) + { + bAtLeastOneExpandedBookmarkAtInsertionPosition = true; + } + } + } + } + + bSortMarks = bAtLeastOneBookmarkMoved && bAtLeastOneExpandedBookmarkAtInsertionPosition; + } + + // at-char anchored flys shouldn't be moved, either. +#if OSL_DEBUG_LEVEL > 0 + std::vector checkFormats; + const SwFrameFormats& rFormats = *GetDoc()->GetSpzFrameFormats(); + for (auto& rpFormat : rFormats) + { + const SwFormatAnchor& rAnchor = rpFormat->GetAnchor(); + const SwPosition* pContentAnchor = rAnchor.GetContentAnchor(); + if (rAnchor.GetAnchorId() == RndStdIds::FLY_AT_CHAR && pContentAnchor) + { + // The fly is at-char anchored and has an anchor position. + SwIndex& rEndIdx = const_cast(pContentAnchor->nContent); + if (&pContentAnchor->nNode.GetNode() == this && rEndIdx.GetIndex() == rPos.GetIndex()) + { + // The anchor position is exactly our insert position. + #if 0 + rEndIdx.Assign(&aTmpIdxReg, rEndIdx.GetIndex()); + #endif + checkFormats.push_back( rpFormat ); + } + } + } +#endif + std::vector const*const pFlys(GetAnchoredFlys()); + for (size_t i = 0; pFlys && i != pFlys->size(); ++i) + { + SwFrameFormat const*const pFormat = (*pFlys)[i]; + const SwFormatAnchor& rAnchor = pFormat->GetAnchor(); + const SwPosition* pContentAnchor = rAnchor.GetContentAnchor(); + if (rAnchor.GetAnchorId() == RndStdIds::FLY_AT_CHAR && pContentAnchor) + { + // The fly is at-char anchored and has an anchor position. + SwIndex& rEndIdx = const_cast(pContentAnchor->nContent); + if (&pContentAnchor->nNode.GetNode() == this && rEndIdx.GetIndex() == rPos.GetIndex()) + { + // The anchor position is exactly our insert position. + rEndIdx.Assign(&aTmpIdxReg, rEndIdx.GetIndex()); +#if OSL_DEBUG_LEVEL > 0 + auto checkPos = std::find( checkFormats.begin(), checkFormats.end(), pFormat ); + assert( checkPos != checkFormats.end()); + checkFormats.erase( checkPos ); +#endif + } + } + } +#if OSL_DEBUG_LEVEL > 0 + assert( checkFormats.empty()); +#endif + + // The cursors of other shells shouldn't be moved, either. + if (SwDocShell* pDocShell = GetDoc()->GetDocShell()) + { + if (pDocShell->GetWrtShell()) + { + for (SwViewShell& rShell : pDocShell->GetWrtShell()->GetRingContainer()) + { + auto pWrtShell = dynamic_cast(&rShell); + if (!pWrtShell || pWrtShell == pDocShell->GetWrtShell()) + continue; + + SwShellCursor* pCursor = pWrtShell->GetCursor_(); + if (!pCursor) + continue; + + SwIndex& rIndex = pCursor->Start()->nContent; + if (&pCursor->Start()->nNode.GetNode() == this && rIndex.GetIndex() == rPos.GetIndex()) + { + // The cursor position of this other shell is exactly our insert position. + rIndex.Assign(&aTmpIdxReg, rIndex.GetIndex()); + } + } + } + } + } + + // base class + SwIndexReg::Update( rPos, nChangeLen, bNegative, bDelete ); + + if (pCollector) + { + const size_t nCount = pCollector->size(); + for ( size_t i = 0; i < nCount; ++i ) + { + m_pSwpHints->TryInsertHint( (*pCollector)[ i ], *this ); + } + } + + aTmpIdxReg.MoveTo( *this ); + if ( bSortMarks ) + { + getIDocumentMarkAccess()->assureSortedMarkContainers(); + } + + //Any drawing objects anchored into this text node may be sorted by their + //anchor position which may have changed here, so resort them + SwContentFrame* pContentFrame = getLayoutFrame(GetDoc()->getIDocumentLayoutAccess().GetCurrentLayout()); + SwSortedObjs* pSortedObjs = pContentFrame ? pContentFrame->GetDrawObjs() : nullptr; + if (pSortedObjs) + pSortedObjs->UpdateAll(); + + // Update the paragraph signatures. + if (SwEditShell* pEditShell = GetDoc()->GetEditShell()) + { + pEditShell->ValidateParagraphSignatures(this, true); + } + + // Inform LOK clients about change in position of redlines (if any) + // Don't emit notifications during save: redline flags are temporarily changed during save, but + // it's not useful to let clients know about such changes. + if (comphelper::LibreOfficeKit::isActive() && !GetDoc()->IsInWriting()) + { + const SwRedlineTable& rTable = GetDoc()->getIDocumentRedlineAccess().GetRedlineTable(); + for (SwRedlineTable::size_type nRedlnPos = 0; nRedlnPos < rTable.size(); ++nRedlnPos) + { + SwRangeRedline* pRedln = rTable[nRedlnPos]; + if (pRedln->HasMark()) + { + if (this == &pRedln->End()->nNode.GetNode() && *pRedln->GetPoint() != *pRedln->GetMark()) + { + // Redline is changed only when some change occurs before it + if (nChangePos <= pRedln->Start()->nContent.GetIndex()) + { + SwRedlineTable::LOKRedlineNotification(RedlineNotification::Modify, pRedln); + } + } + } + else if (this == &pRedln->GetPoint()->nNode.GetNode()) + SwRedlineTable::LOKRedlineNotification(RedlineNotification::Modify, pRedln); + } + } +} + +void SwTextNode::ChgTextCollUpdateNum( const SwTextFormatColl *pOldColl, + const SwTextFormatColl *pNewColl) +{ + SwDoc* pDoc = GetDoc(); + OSL_ENSURE( pDoc, "No Doc?" ); + // query the OutlineLevel and if it changed, notify the Nodes-Array! + const int nOldLevel = pOldColl && pOldColl->IsAssignedToListLevelOfOutlineStyle() ? + pOldColl->GetAssignedOutlineStyleLevel() : MAXLEVEL; + const int nNewLevel = pNewColl && pNewColl->IsAssignedToListLevelOfOutlineStyle() ? + pNewColl->GetAssignedOutlineStyleLevel() : MAXLEVEL; + + if ( MAXLEVEL != nNewLevel && -1 != nNewLevel ) + { + SetAttrListLevel(nNewLevel); + } + if (pDoc) + { + pDoc->GetNodes().UpdateOutlineNode(*this); + } + + SwNodes& rNds = GetNodes(); + // If Level 0 (Chapter), update the footnotes! + if( ( !nNewLevel || !nOldLevel) && pDoc && !pDoc->GetFootnoteIdxs().empty() && + FTNNUM_CHAPTER == pDoc->GetFootnoteInfo().m_eNum && + rNds.IsDocNodes() ) + { + SwNodeIndex aTmpIndex( rNds, GetIndex()); + + pDoc->GetFootnoteIdxs().UpdateFootnote( aTmpIndex); + } + + if( pNewColl && RES_CONDTXTFMTCOLL == pNewColl->Which() ) + { + // check the condition of the text node again + ChkCondColl(); + } +} + +// If positioned exactly at the end of a CharStyle or Hyperlink, +// set its DontExpand flag. +bool SwTextNode::DontExpandFormat( const SwIndex& rIdx, bool bFlag, + bool bFormatToTextAttributes ) +{ + const sal_Int32 nIdx = rIdx.GetIndex(); + if (bFormatToTextAttributes && nIdx == m_Text.getLength()) + { + FormatToTextAttr( this ); + } + + bool bRet = false; + if ( HasHints() ) + { + m_pSwpHints->SortIfNeedBe(); + int nPos = m_pSwpHints->GetLastPosSortedByEnd(nIdx); + for ( ; nPos >= 0; --nPos) + { + SwTextAttr *pTmp = m_pSwpHints->GetSortedByEnd( nPos ); + const sal_Int32 *pEnd = pTmp->GetEnd(); + if( !pEnd ) + continue; + assert( *pEnd <= nIdx ); + if( nIdx != *pEnd ) + break; + if( bFlag != pTmp->DontExpand() && !pTmp->IsLockExpandFlag() + && *pEnd > pTmp->GetStart()) + { + bRet = true; + m_pSwpHints->NoteInHistory( pTmp ); + pTmp->SetDontExpand( bFlag ); + } + } + } + return bRet; +} + +static bool lcl_GetTextAttrDefault(sal_Int32 nIndex, sal_Int32 nHintStart, sal_Int32 nHintEnd) +{ + return ((nHintStart <= nIndex) && (nIndex < nHintEnd)); +} +static bool lcl_GetTextAttrExpand(sal_Int32 nIndex, sal_Int32 nHintStart, sal_Int32 nHintEnd) +{ + return ((nHintStart < nIndex) && (nIndex <= nHintEnd)); +} +static bool lcl_GetTextAttrParent(sal_Int32 nIndex, sal_Int32 nHintStart, sal_Int32 nHintEnd) +{ + return ((nHintStart < nIndex) && (nIndex < nHintEnd)); +} + +static void +lcl_GetTextAttrs( + std::vector *const pVector, + SwTextAttr **const ppTextAttr, + SwpHints const *const pSwpHints, + sal_Int32 const nIndex, sal_uInt16 const nWhich, + enum SwTextNode::GetTextAttrMode const eMode) +{ + assert(nWhich >= RES_TXTATR_BEGIN && nWhich < RES_TXTATR_END); + if (!pSwpHints) + return; + size_t const nSize = pSwpHints->Count(); + sal_Int32 nPreviousIndex(0); // index of last hint with nWhich + bool (*pMatchFunc)(sal_Int32, sal_Int32, sal_Int32)=nullptr; + switch (eMode) + { + case SwTextNode::DEFAULT: pMatchFunc = &lcl_GetTextAttrDefault; break; + case SwTextNode::EXPAND: pMatchFunc = &lcl_GetTextAttrExpand; break; + case SwTextNode::PARENT: pMatchFunc = &lcl_GetTextAttrParent; break; + default: assert(false); + } + + for( size_t i = pSwpHints->GetFirstPosSortedByWhichAndStart(nWhich); i < nSize; ++i ) + { + SwTextAttr *const pHint = pSwpHints->GetSortedByWhichAndStart(i); + if (pHint->Which() != nWhich) + break; // hints are sorted by which&start, so we are done... + + sal_Int32 const nHintStart = pHint->GetStart(); + if (nIndex < nHintStart) + break; // hints are sorted by which&start, so we are done... + + sal_Int32 const*const pEndIdx = pHint->GetEnd(); + // cannot have hint with no end and no dummy char + assert(pEndIdx || pHint->HasDummyChar()); + // If EXPAND is set, simulate the text input behavior, i.e. + // move the start, and expand the end. + bool const bContained( pEndIdx + ? (*pMatchFunc)(nIndex, nHintStart, *pEndIdx) + : (nHintStart == nIndex) ); + if (bContained) + { + if (pVector) + { + if (nPreviousIndex < nHintStart) + { + pVector->clear(); // clear hints that are outside pHint + nPreviousIndex = nHintStart; + } + pVector->push_back(pHint); + } + else + { + *ppTextAttr = pHint; // and possibly overwrite outer hint + } + if (!pEndIdx) + { + break; + } + } + } +} + +std::vector +SwTextNode::GetTextAttrsAt(sal_Int32 const nIndex, sal_uInt16 const nWhich) const +{ + assert(nWhich >= RES_TXTATR_BEGIN && nWhich < RES_TXTATR_END); + std::vector ret; + lcl_GetTextAttrs(&ret, nullptr, m_pSwpHints.get(), nIndex, nWhich, DEFAULT); + return ret; +} + +SwTextAttr * +SwTextNode::GetTextAttrAt(sal_Int32 const nIndex, sal_uInt16 const nWhich, + enum GetTextAttrMode const eMode) const +{ + assert( (nWhich == RES_TXTATR_META) + || (nWhich == RES_TXTATR_METAFIELD) + || (nWhich == RES_TXTATR_AUTOFMT) + || (nWhich == RES_TXTATR_INETFMT) + || (nWhich == RES_TXTATR_CJK_RUBY) + || (nWhich == RES_TXTATR_UNKNOWN_CONTAINER) + || (nWhich == RES_TXTATR_INPUTFIELD ) ); + // "GetTextAttrAt() will give wrong result for this hint!") + + SwTextAttr * pRet(nullptr); + lcl_GetTextAttrs(nullptr, & pRet, m_pSwpHints.get(), nIndex, nWhich, eMode); + return pRet; +} + +const SwTextInputField* SwTextNode::GetOverlappingInputField( const SwTextAttr& rTextAttr ) const +{ + const SwTextInputField* pTextInputField = dynamic_cast(GetTextAttrAt( rTextAttr.GetStart(), RES_TXTATR_INPUTFIELD, PARENT )); + + if ( pTextInputField == nullptr && rTextAttr.End() != nullptr ) + { + pTextInputField = dynamic_cast(GetTextAttrAt( *(rTextAttr.End()), RES_TXTATR_INPUTFIELD, PARENT )); + } + + return pTextInputField; +} + +void SwTextNode::DelFrames_TextNodePart() +{ + SetWrong( nullptr ); + SetWrongDirty(WrongState::TODO); + + SetGrammarCheck( nullptr ); + SetGrammarCheckDirty( true ); + + SetSmartTags( nullptr ); + SetSmartTagDirty( true ); + + SetWordCountDirty( true ); + SetAutoCompleteWordDirty( true ); +} + +SwTextField* SwTextNode::GetFieldTextAttrAt( + const sal_Int32 nIndex, + const bool bIncludeInputFieldAtStart ) const +{ + SwTextField* pTextField = dynamic_cast(GetTextAttrForCharAt( nIndex, RES_TXTATR_FIELD )); + if ( pTextField == nullptr ) + { + pTextField = dynamic_cast(GetTextAttrForCharAt( nIndex, RES_TXTATR_ANNOTATION )); + } + if ( pTextField == nullptr ) + { + pTextField = + dynamic_cast( GetTextAttrAt( + nIndex, + RES_TXTATR_INPUTFIELD, + bIncludeInputFieldAtStart ? DEFAULT : EXPAND )); + } + + return pTextField; +} + +static SwCharFormat* lcl_FindCharFormat( const SwCharFormats* pCharFormats, const OUString& rName ) +{ + if( !rName.isEmpty() ) + { + const size_t nArrLen = pCharFormats->size(); + for( size_t i = 1; i < nArrLen; i++ ) + { + SwCharFormat* pFormat = (*pCharFormats)[ i ]; + if( pFormat->GetName()==rName ) + return pFormat; + } + } + return nullptr; +} + +static void lcl_CopyHint( + const sal_uInt16 nWhich, + const SwTextAttr * const pHt, + SwTextAttr *const pNewHt, + SwDoc *const pOtherDoc, + SwTextNode *const pDest ) +{ + assert(nWhich == pHt->Which()); // wrong hint-id + switch( nWhich ) + { + // copy nodesarray section with footnote content + case RES_TXTATR_FTN : + assert(pDest); // "lcl_CopyHint: no destination text node?" + static_cast(pHt)->CopyFootnote( *static_cast(pNewHt), *pDest); + break; + + // Fields that are copied into different SwDocs must be registered + // at their new FieldTypes. + + case RES_TXTATR_FIELD : + { + if( pOtherDoc != nullptr ) + { + static_txtattr_cast(pHt)->CopyTextField( + static_txtattr_cast(pNewHt)); + } + + // Table Formula must be copied relative. + const SwFormatField& rField = pHt->GetFormatField(); + if( SwFieldIds::Table == rField.GetField()->GetTyp()->Which() + && static_cast(rField.GetField())->IsIntrnlName()) + { + // convert internal formula to external + const SwTableNode* const pDstTableNd = + static_txtattr_cast(pHt)->GetTextNode().FindTableNode(); + if( pDstTableNd ) + { + SwTableField* const pTableField = + const_cast(static_cast( + pNewHt->GetFormatField().GetField())); + pTableField->PtrToBoxNm( &pDstTableNd->GetTable() ); + } + } + } + break; + + case RES_TXTATR_INPUTFIELD : + case RES_TXTATR_ANNOTATION : + if( pOtherDoc != nullptr ) + { + static_txtattr_cast(pHt)->CopyTextField( + static_txtattr_cast(pNewHt)); + } + break; + + case RES_TXTATR_TOXMARK : + if( pOtherDoc && pDest && pDest->GetpSwpHints() + && pDest->GetpSwpHints()->Contains( pNewHt ) ) + { + // ToXMarks that are copied to different SwDocs must register + // at their new ToX (SwModify). + static_txtattr_cast(pNewHt)->CopyTOXMark(pOtherDoc); + } + break; + + case RES_TXTATR_CHARFMT : + // For CharacterStyles, the format must be copied too. + if( pDest && pDest->GetpSwpHints() + && pDest->GetpSwpHints()->Contains( pNewHt ) ) + { + SwCharFormat* pFormat = pHt->GetCharFormat().GetCharFormat(); + + if (pOtherDoc) + { + pFormat = pOtherDoc->CopyCharFormat( *pFormat ); + } + const_cast( + pNewHt->GetCharFormat() ).SetCharFormat( pFormat ); + } + break; + case RES_TXTATR_INETFMT : + { + // For Hyperlinks, the format must be copied too. + if( pOtherDoc && pDest && pDest->GetpSwpHints() + && pDest->GetpSwpHints()->Contains( pNewHt ) ) + { + const SwDoc* const pDoc = static_txtattr_cast< + const SwTextINetFormat*>(pHt)->GetTextNode().GetDoc(); + if ( pDoc ) + { + const SwCharFormats* pCharFormats = pDoc->GetCharFormats(); + const SwFormatINetFormat& rFormat = pHt->GetINetFormat(); + SwCharFormat* pFormat; + pFormat = lcl_FindCharFormat( pCharFormats, rFormat.GetINetFormat() ); + if( pFormat ) + pOtherDoc->CopyCharFormat( *pFormat ); + pFormat = lcl_FindCharFormat( pCharFormats, rFormat.GetVisitedFormat() ); + if( pFormat ) + pOtherDoc->CopyCharFormat( *pFormat ); + } + } + //JP 24.04.98: The attribute must point to a text node, so that + // the styles can be created. + SwTextINetFormat *const pINetHt = static_txtattr_cast(pNewHt); + if ( !pINetHt->GetpTextNode() ) + { + pINetHt->ChgTextNode( pDest ); + } + + //JP 22.10.97: set up link to char style + pINetHt->GetCharFormat(); + break; + } + case RES_TXTATR_META: + case RES_TXTATR_METAFIELD: + OSL_ENSURE( pNewHt, "copying Meta should not fail!" ); + OSL_ENSURE( pDest + && (CH_TXTATR_INWORD == pDest->GetText()[pNewHt->GetStart()]), + "missing CH_TXTATR?"); + break; + } +} + +/// copy attributes at position nTextStartIdx to node pDest +// BP 7.6.93: Intentionally copy only attributes _with_ EndIdx! +// CopyAttr is usually called when attributes are set on a +// node with no text. +void SwTextNode::CopyAttr( SwTextNode *pDest, const sal_Int32 nTextStartIdx, + const sal_Int32 nOldPos ) +{ + if ( HasHints() ) + { + SwDoc* const pOtherDoc = (pDest->GetDoc() != GetDoc()) ? + pDest->GetDoc() : nullptr; + + for ( size_t i = 0; i < m_pSwpHints->Count(); ++i ) + { + SwTextAttr *const pHt = m_pSwpHints->Get(i); + sal_Int32 const nAttrStartIdx = pHt->GetStart(); + if ( nTextStartIdx < nAttrStartIdx ) + break; // beyond end of text, because nLen == 0 + + const sal_Int32 *const pEndIdx = pHt->GetEnd(); + if ( pEndIdx && !pHt->HasDummyChar() ) + { + sal_uInt16 const nWhich = pHt->Which(); + if (RES_TXTATR_INPUTFIELD != nWhich // fdo#74981 skip fields + && ( *pEndIdx > nTextStartIdx + || (*pEndIdx == nTextStartIdx + && nAttrStartIdx == nTextStartIdx))) + { + if ( RES_TXTATR_REFMARK != nWhich ) + { + // attribute in the area => copy + SwTextAttr *const pNewHt = + pDest->InsertItem( pHt->GetAttr(), nOldPos, nOldPos, SetAttrMode::IS_COPY); + if ( pNewHt ) + { + lcl_CopyHint( nWhich, pHt, pNewHt, + pOtherDoc, pDest ); + } + } + else if( !pOtherDoc + ? GetDoc()->IsCopyIsMove() + : nullptr == pOtherDoc->GetRefMark( pHt->GetRefMark().GetRefName() ) ) + { + pDest->InsertItem( + pHt->GetAttr(), nOldPos, nOldPos, SetAttrMode::IS_COPY); + } + } + } + } + } + + if( this != pDest ) + { + // notify layout frames, to prevent disappearance of footnote numbers + SwUpdateAttr aHint( + nOldPos, + nOldPos, + 0); + + pDest->ModifyNotification( nullptr, &aHint ); + } +} + +/// copy text and attributes to node pDest +void SwTextNode::CopyText( SwTextNode *const pDest, + const SwIndex &rStart, + const sal_Int32 nLen, + const bool bForceCopyOfAllAttrs ) +{ + SwIndex const aIdx( pDest, pDest->m_Text.getLength() ); + CopyText( pDest, aIdx, rStart, nLen, bForceCopyOfAllAttrs ); +} + +void SwTextNode::CopyText( SwTextNode *const pDest, + const SwIndex &rDestStart, + const SwIndex &rStart, + sal_Int32 nLen, + const bool bForceCopyOfAllAttrs ) +{ + CHECK_SWPHINTS_IF_FRM(this); + CHECK_SWPHINTS(pDest); + sal_Int32 nTextStartIdx = rStart.GetIndex(); + sal_Int32 nDestStart = rDestStart.GetIndex(); // remember old Pos + + if (pDest->GetDoc()->IsClipBoard() && GetNum()) + { + // #i111677# cache expansion of source (for clipboard) + pDest->m_pNumStringCache.reset( (nTextStartIdx != 0) + ? new OUString // fdo#49076: numbering only if copy from para start + : new OUString(GetNumString())); + } + + if( !nLen ) + { + // if no length is given, copy attributes at position rStart + CopyAttr( pDest, nTextStartIdx, nDestStart ); + + // copy hard attributes on whole paragraph + if( HasSwAttrSet() ) + { + // i#96213 all or just the Char attributes? + if ( !bForceCopyOfAllAttrs && + ( nDestStart || + pDest->HasSwAttrSet() || + nLen != pDest->GetText().getLength())) + { + SfxItemSet aCharSet( + pDest->GetDoc()->GetAttrPool(), + svl::Items< + RES_CHRATR_BEGIN, RES_CHRATR_END - 1, + RES_TXTATR_INETFMT, RES_TXTATR_CHARFMT, + RES_UNKNOWNATR_BEGIN, RES_UNKNOWNATR_END - 1>{} ); + aCharSet.Put( *GetpSwAttrSet() ); + if( aCharSet.Count() ) + { + pDest->SetAttr( aCharSet, nDestStart, nDestStart ); + } + } + else + { + GetpSwAttrSet()->CopyToModify( *pDest ); + } + } + return; + } + + // 1. copy text + const sal_Int32 oldLen = pDest->m_Text.getLength(); + // JP 15.02.96: missing attribute handling at the end! + // hence call InsertText and don't modify m_Text directly + pDest->InsertText( m_Text.copy(nTextStartIdx, nLen), rDestStart, + SwInsertFlags::EMPTYEXPAND ); + + // update with actual new size + nLen = pDest->m_Text.getLength() - oldLen; + if ( !nLen ) // string not longer? + return; + + SwDoc* const pOtherDoc = (pDest->GetDoc() != GetDoc()) ? pDest->GetDoc() : nullptr; + + // copy hard attributes on whole paragraph + if( HasSwAttrSet() ) + { + // i#96213 all or just the Char attributes? + if ( !bForceCopyOfAllAttrs && + ( nDestStart || + pDest->HasSwAttrSet() || + nLen != pDest->GetText().getLength())) + { + SfxItemSet aCharSet( + pDest->GetDoc()->GetAttrPool(), + svl::Items< + RES_CHRATR_BEGIN, RES_CHRATR_END - 1, + RES_TXTATR_INETFMT, RES_TXTATR_CHARFMT, + RES_UNKNOWNATR_BEGIN, RES_UNKNOWNATR_END - 1>{}); + aCharSet.Put( *GetpSwAttrSet() ); + if( aCharSet.Count() ) + { + pDest->SetAttr( aCharSet, nDestStart, nDestStart + nLen ); + } + } + else + { + GetpSwAttrSet()->CopyToModify( *pDest ); + } + } + + bool const bUndoNodes = !pOtherDoc + && GetDoc()->GetIDocumentUndoRedo().IsUndoNodes(GetNodes()); + + // Fetch end only now, because copying into self updates the start index + // and all attributes + nTextStartIdx = rStart.GetIndex(); + const sal_Int32 nEnd = nTextStartIdx + nLen; + + // 2. copy attributes + // Iterate over attribute array until the start of the attribute + // is behind the copied range + const size_t nSize = m_pSwpHints ? m_pSwpHints->Count() : 0; + + // If copying into self, inserting can delete attributes! + // Hence first copy into temp-array, and then move that into hints array. + SwpHts aArr; + + // Del-Array for all RefMarks without extent + SwpHts aRefMrkArr; + + std::vector> metaFieldRanges; + sal_Int32 nDeletedDummyChars(0); + for (size_t n = 0; n < nSize; ++n) + { + SwTextAttr * const pHt = m_pSwpHints->Get(n); + + const sal_Int32 nAttrStartIdx = pHt->GetStart(); + if ( nAttrStartIdx >= nEnd ) + break; + + const sal_Int32 * const pEndIdx = pHt->GetEnd(); + const sal_uInt16 nWhich = pHt->Which(); + + // JP 26.04.94: RefMarks are never copied. If the refmark doesn't have + // an extent, there is a dummy char in the text, which + // must be removed. So we first copy the attribute, + // but remember it, and when we're done delete it, + // which also deletes the dummy character! + // JP 14.08.95: May RefMarks be moved? + const bool bCopyRefMark = RES_TXTATR_REFMARK == nWhich + && ( bUndoNodes + || ( !pOtherDoc + ? GetDoc()->IsCopyIsMove() + : nullptr == pOtherDoc->GetRefMark( pHt->GetRefMark().GetRefName() ) ) ); + + if ( pEndIdx + && RES_TXTATR_REFMARK == nWhich + && !bCopyRefMark ) + { + continue; + } + + // Input Fields are only copied, if completely covered by copied text + if ( nWhich == RES_TXTATR_INPUTFIELD ) + { + assert(pEndIdx != nullptr && + " - RES_TXTATR_INPUTFIELD without EndIndex!" ); + if ( nAttrStartIdx < nTextStartIdx + || ( pEndIdx != nullptr + && *pEndIdx > nEnd ) ) + { + continue; + } + } + + if (nWhich == RES_TXTATR_METAFIELD) + { + // Skip metadata fields. Also remember the range to strip the text later. + metaFieldRanges.emplace_back(nAttrStartIdx, pEndIdx ? *pEndIdx : nEnd); + continue; + } + + sal_Int32 nAttrStt = 0; + sal_Int32 nAttrEnd = 0; + + if( nAttrStartIdx < nTextStartIdx ) + { + // start is before selection + // copy hints with end and CH_TXTATR only if dummy char is copied + if ( pEndIdx && (*pEndIdx > nTextStartIdx) && !pHt->HasDummyChar() ) + { + // attribute with extent and the end is in the selection + nAttrStt = nDestStart; + nAttrEnd = (*pEndIdx > nEnd) + ? rDestStart.GetIndex() + : nDestStart + (*pEndIdx) - nTextStartIdx; + } + else + { + continue; + } + } + else + { + // start is in the selection + nAttrStt = nDestStart + ( nAttrStartIdx - nTextStartIdx ); + if( pEndIdx ) + { + nAttrEnd = *pEndIdx > nEnd + ? rDestStart.GetIndex() + : nDestStart + ( *pEndIdx - nTextStartIdx ); + } + else + { + nAttrEnd = nAttrStt; + } + } + + SwTextAttr * pNewHt = nullptr; + + if( pDest == this ) + { + // copy the hint here, but insert it later + pNewHt = MakeTextAttr( *GetDoc(), pHt->GetAttr(), + nAttrStt, nAttrEnd, CopyOrNewType::Copy, pDest ); + + lcl_CopyHint(nWhich, pHt, pNewHt, nullptr, pDest); + aArr.push_back( pNewHt ); + } + else + { + pNewHt = pDest->InsertItem( + pHt->GetAttr(), + nAttrStt - nDeletedDummyChars, + nAttrEnd - nDeletedDummyChars, + SetAttrMode::NOTXTATRCHR | SetAttrMode::IS_COPY); + if (pNewHt) + { + lcl_CopyHint( nWhich, pHt, pNewHt, pOtherDoc, pDest ); + } + else if (pHt->HasDummyChar()) + { + // The attribute that has failed to be copied would insert + // dummy char, so positions of the following attributes have + // to be shifted by one to compensate for that missing char. + ++nDeletedDummyChars; + } + } + + if( RES_TXTATR_REFMARK == nWhich && !pEndIdx && !bCopyRefMark ) + { + aRefMrkArr.push_back( pNewHt ); + } + } + + // Strip the metadata fields, since we don't copy the RDF entries + // yet and so they are inconsistent upon copy/pasting. + if (!metaFieldRanges.empty()) + { + // Reverse to remove without messing the offsets. + std::reverse(metaFieldRanges.begin(), metaFieldRanges.end()); + for (const auto& pair : metaFieldRanges) + { + const SwIndex aIdx(pDest, pair.first); + pDest->EraseText(aIdx, pair.second - pair.first); + } + } + + // this can only happen when copying into self + for (SwTextAttr* i : aArr) + { + InsertHint( i, SetAttrMode::NOTXTATRCHR ); + } + + if( pDest->GetpSwpHints() ) + { + for (SwTextAttr* pNewHt : aRefMrkArr) + { + if( pNewHt->GetEnd() ) + { + pDest->GetpSwpHints()->Delete( pNewHt ); + pDest->DestroyAttr( pNewHt ); + } + else + { + const SwIndex aIdx( pDest, pNewHt->GetStart() ); + pDest->EraseText( aIdx, 1 ); + } + } + } + + CHECK_SWPHINTS_IF_FRM(this); + CHECK_SWPHINTS(pDest); +} + +OUString SwTextNode::InsertText( const OUString & rStr, const SwIndex & rIdx, + const SwInsertFlags nMode ) +{ + assert(rIdx <= m_Text.getLength()); // invalid index + + const sal_Int32 aPos = rIdx.GetIndex(); + sal_Int32 nLen = m_Text.getLength() - aPos; + sal_Int32 const nOverflow(rStr.getLength() - GetSpaceLeft()); + SAL_WARN_IF(nOverflow > 0, "sw.core", + "SwTextNode::InsertText: node text with insertion > capacity."); + OUString const sInserted( + (nOverflow > 0) ? rStr.copy(0, rStr.getLength() - nOverflow) : rStr); + if (sInserted.isEmpty()) + { + return sInserted; + } + m_Text = m_Text.replaceAt(aPos, 0, sInserted); + assert(GetSpaceLeft()>=0); + nLen = m_Text.getLength() - aPos - nLen; + assert(nLen != 0); + + bool bOldExpFlg = IsIgnoreDontExpand(); + if (nMode & SwInsertFlags::FORCEHINTEXPAND) + { + SetIgnoreDontExpand( true ); + } + + Update( rIdx, nLen ); // text content changed! + + if (nMode & SwInsertFlags::FORCEHINTEXPAND) + { + SetIgnoreDontExpand( bOldExpFlg ); + } + + if ( HasWriterListeners() ) + { // send this before messing with hints, which will send RES_UPDATE_ATTR + SwInsText aHint( aPos, nLen ); + NotifyClients( nullptr, &aHint ); + } + + if ( HasHints() ) + { + m_pSwpHints->SortIfNeedBe(); + bool const bHadHints(!m_pSwpHints->CanBeDeleted()); + bool bMergePortionsNeeded(false); + for ( size_t i = 0; i < m_pSwpHints->Count() && + rIdx >= m_pSwpHints->GetWithoutResorting(i)->GetStart(); ++i ) + { + SwTextAttr * const pHt = m_pSwpHints->GetWithoutResorting( i ); + const sal_Int32 * const pEndIdx = pHt->GetEnd(); + if( !pEndIdx ) + continue; + + if( rIdx == *pEndIdx ) + { + if ( (nMode & SwInsertFlags::NOHINTEXPAND) || + (!(nMode & SwInsertFlags::FORCEHINTEXPAND) + && pHt->DontExpand()) ) + { + m_pSwpHints->DeleteAtPos(i); + // on empty attributes also adjust Start + if( rIdx == pHt->GetStart() ) + pHt->SetStart( pHt->GetStart() - nLen ); + pHt->SetEnd(*pEndIdx - nLen); + // could be that pHt has IsFormatIgnoreEnd set, and it's + // not a RSID-only hint - now we have the inserted text + // between pHt and its continuation... which we don't know. + // punt the job to MergePortions below. + if (pHt->IsFormatIgnoreEnd()) + { + bMergePortionsNeeded = true; + } + InsertHint( pHt, SetAttrMode::NOHINTADJUST ); + } + // empty hints at insert position? + else if ( (nMode & SwInsertFlags::EMPTYEXPAND) + && (*pEndIdx == pHt->GetStart()) ) + { + m_pSwpHints->DeleteAtPos(i); + pHt->SetStart( pHt->GetStart() - nLen ); + const size_t nCurrentLen = m_pSwpHints->Count(); + InsertHint( pHt/* AUTOSTYLES:, SetAttrMode::NOHINTADJUST*/ ); + if ( nCurrentLen > m_pSwpHints->Count() && i ) + { + --i; + } + continue; + } + else + { + continue; + } + } + if ( !(nMode & SwInsertFlags::NOHINTEXPAND) && + rIdx == nLen && pHt->GetStart() == rIdx.GetIndex() && + !pHt->IsDontExpandStartAttr() ) + { + // no field, at paragraph start, HintExpand + m_pSwpHints->DeleteAtPos(i); + pHt->SetStart( pHt->GetStart() - nLen ); + // no effect on format ignore flags here (para start) + InsertHint( pHt, SetAttrMode::NOHINTADJUST ); + } + } + if (bMergePortionsNeeded) + { + m_pSwpHints->MergePortions(*this); + } + SAL_WARN_IF(bHadHints && m_pSwpHints->CanBeDeleted(), "sw.core", + "SwTextNode::InsertText: unexpected loss of hints"); + } + + // By inserting a character, the hidden flags + // at the TextNode can become invalid: + SetCalcHiddenCharFlags(); + + CHECK_SWPHINTS(this); + return sInserted; +} + +void SwTextNode::CutText( SwTextNode * const pDest, + const SwIndex & rStart, const sal_Int32 nLen ) +{ + assert(pDest); // Cut requires a destination + SwIndex aDestStt(pDest, pDest->GetText().getLength()); + CutImpl( pDest, aDestStt, rStart, nLen, false ); +} + +void SwTextNode::CutImpl( SwTextNode * const pDest, const SwIndex & rDestStart, + const SwIndex & rStart, sal_Int32 nLen, const bool bUpdate ) +{ + assert(pDest); // Cut requires a destination + + assert(GetDoc() == pDest->GetDoc()); // must be same document + + assert(pDest != this); // destination must be different node + + if( !nLen ) + { + // if no length is given, copy attributes at position rStart + CopyAttr( pDest, rStart.GetIndex(), rDestStart.GetIndex() ); + return; + } + + sal_Int32 nTextStartIdx = rStart.GetIndex(); + sal_Int32 nDestStart = rDestStart.GetIndex(); // remember old Pos + const sal_Int32 nInitSize = pDest->m_Text.getLength(); + + if (pDest->GetSpaceLeft() < nLen) + { // FIXME: could only happen when called from SwRangeRedline::Show. + // unfortunately can't really do anything here to handle that... + abort(); + } + pDest->m_Text = pDest->m_Text.replaceAt(nDestStart, 0, + m_Text.copy(nTextStartIdx, nLen)); + OUString const newText = m_Text.replaceAt(nTextStartIdx, nLen, ""); + nLen = pDest->m_Text.getLength() - nInitSize; // update w/ current size! + if (!nLen) // String didn't grow? + return; + + if (bUpdate) + { + // Update all SwIndex + pDest->Update( rDestStart, nLen, false, false/*??? why was it true*/); + } + + CHECK_SWPHINTS(pDest); + + const sal_Int32 nEnd = rStart.GetIndex() + nLen; + bool const bUndoNodes = + GetDoc()->GetIDocumentUndoRedo().IsUndoNodes(GetNodes()); + + // copy hard attributes on whole paragraph + if (HasSwAttrSet()) + { + bool hasSwAttrSet = pDest->HasSwAttrSet(); + if (hasSwAttrSet) + { + // if we have our own property set it doesn't mean + // that this set defines any style different to Standard one. + hasSwAttrSet = false; + + // so, let's check deeper if property set has defined any property + if (pDest->GetpSwAttrSet()) + { + // check all items in the property set + SfxItemIter aIter( *pDest->GetpSwAttrSet() ); + const SfxPoolItem* pItem = aIter.GetCurItem(); + do + { + // check current item + sal_uInt16 nWhich = IsInvalidItem( pItem ) + ? pDest->GetpSwAttrSet()->GetWhichByPos( aIter.GetCurPos() ) + : pItem->Which(); + if( RES_FRMATR_STYLE_NAME != nWhich && + RES_FRMATR_CONDITIONAL_STYLE_NAME != nWhich && + SfxItemState::SET == pDest->GetpSwAttrSet()->GetItemState( nWhich, false ) ) + { + // check if parent value (original value in style) has the same value as in [pItem] + const SfxPoolItem& rParentItem = pDest->GetpSwAttrSet()->GetParent()->Get( nWhich, true ); + + hasSwAttrSet = (rParentItem != *pItem); + + // property set is not empty => no need to make anymore checks + if (hasSwAttrSet) + break; + } + + // let's check next item + pItem = aIter.NextItem(); + } while (pItem); + } + } + + // all or just the Char attributes? + if( nInitSize || hasSwAttrSet || + nLen != pDest->GetText().getLength()) + { + SfxItemSet aCharSet( + pDest->GetDoc()->GetAttrPool(), + svl::Items< + RES_CHRATR_BEGIN, RES_CHRATR_END - 1, + RES_TXTATR_INETFMT, RES_TXTATR_CHARFMT, + RES_UNKNOWNATR_BEGIN, RES_UNKNOWNATR_END - 1>{}); + aCharSet.Put( *GetpSwAttrSet() ); + if( aCharSet.Count() ) + pDest->SetAttr( aCharSet, nDestStart, nDestStart + nLen ); + } + else + { + GetpSwAttrSet()->CopyToModify( *pDest ); + } + } + + // notify frames - before moving hints, because footnotes + // want to find their anchor text frame in the follow chain + SwInsText aInsHint( nDestStart, nLen ); + pDest->ModifyNotification( nullptr, &aInsHint ); + sw::MoveText const moveHint(pDest, nDestStart, nTextStartIdx, nLen); + CallSwClientNotify(moveHint); + SwDelText aDelHint( nTextStartIdx, nLen ); + ModifyNotification( nullptr, &aDelHint ); + + // 2. move attributes + // Iterate over attribute array until the start of the attribute + // is behind the moved range + bool bMergePortionsNeeded(false); + size_t nAttrCnt = 0; + while (m_pSwpHints && (nAttrCnt < m_pSwpHints->Count())) + { + SwTextAttr * const pHt = m_pSwpHints->Get(nAttrCnt); + const sal_Int32 nAttrStartIdx = pHt->GetStart(); + if ( nAttrStartIdx >= nEnd ) + break; + const sal_Int32 * const pEndIdx = pHt->GetEnd(); + const sal_uInt16 nWhich = pHt->Which(); + SwTextAttr *pNewHt = nullptr; + + // if the hint has a dummy character, then it must not be split! + if(nAttrStartIdx < nTextStartIdx) + { + // start is before the range + if (!pHt->HasDummyChar() && ( RES_TXTATR_REFMARK != nWhich + || bUndoNodes ) && pEndIdx && *pEndIdx > nTextStartIdx) + { + // attribute with extent and end of attribute is in the range + pNewHt = MakeTextAttr( *pDest->GetDoc(), pHt->GetAttr(), + nDestStart, + nDestStart + ( + *pEndIdx > nEnd + ? nLen + : *pEndIdx - nTextStartIdx ) ); + } + } + else + { + // start is inside the range + if (!pEndIdx || *pEndIdx < nEnd || + (!bUndoNodes && RES_TXTATR_REFMARK == nWhich) + || pHt->HasDummyChar() ) + { + // do not delete note and later add it -> sidebar flickering + if (GetDoc()->GetDocShell()) + { + GetDoc()->GetDocShell()->Broadcast( SfxHint(SfxHintId::SwSplitNodeOperation)); + } + // move attribute + m_pSwpHints->Delete( pHt ); + // reset start/end indexes + if (pHt->IsFormatIgnoreStart() || pHt->IsFormatIgnoreEnd()) + { + bMergePortionsNeeded = true; + } + pHt->SetStart(nDestStart + (nAttrStartIdx - nTextStartIdx)); + if (pEndIdx) + { + pHt->SetEnd( nDestStart + ( + *pEndIdx > nEnd + ? nLen + : *pEndIdx - nTextStartIdx ) ); + } + pDest->InsertHint( pHt, + SetAttrMode::NOTXTATRCHR + | SetAttrMode::DONTREPLACE ); + if (GetDoc()->GetDocShell()) + { + GetDoc()->GetDocShell()->Broadcast( SfxHint(SfxHintId::SwSplitNodeOperation)); + } + continue; // iterate while loop, no ++ ! + } + // the end is behind the range + else if (RES_TXTATR_REFMARK != nWhich || bUndoNodes) + { + pNewHt = MakeTextAttr( *GetDoc(), pHt->GetAttr(), + nDestStart + (nAttrStartIdx - nTextStartIdx), + nDestStart + (*pEndIdx > nEnd + ? nLen + : *pEndIdx - nTextStartIdx)); + } + } + if (pNewHt) + { + const bool bSuccess( pDest->InsertHint( pNewHt, + SetAttrMode::NOTXTATRCHR + | SetAttrMode::DONTREPLACE + | SetAttrMode::IS_COPY) ); + if (bSuccess) + { + lcl_CopyHint( nWhich, pHt, pNewHt, nullptr, pDest ); + } + } + ++nAttrCnt; + } + // If there are still empty attributes around, they have a higher priority + // than any attributes that become empty due to the move. + // So temporarily remove them and Update the array, then re-insert the + // removed ones so they overwrite the others. + if (m_pSwpHints && nAttrCnt < m_pSwpHints->Count()) + { + SwpHts aArr; + while (nAttrCnt < m_pSwpHints->Count()) + { + SwTextAttr * const pHt = m_pSwpHints->Get(nAttrCnt); + if (nEnd != pHt->GetStart()) + break; + const sal_Int32 * const pEndIdx = pHt->GetEnd(); + if (pEndIdx && *pEndIdx == nEnd) + { + aArr.push_back( pHt ); + m_pSwpHints->Delete( pHt ); + } + else + { + ++nAttrCnt; + } + } + Update( rStart, nLen, true, true ); + + for (SwTextAttr* pHt : aArr) + { + pHt->SetStart( rStart.GetIndex() ); + pHt->SetEnd( rStart.GetIndex() ); + InsertHint( pHt ); + } + } + else + { + Update( rStart, nLen, true, true ); + } + + // set after moving hints + m_Text = newText; + + if (bMergePortionsNeeded) + { + m_pSwpHints->MergePortions(*this); + } + + CHECK_SWPHINTS(this); + + TryDeleteSwpHints(); +} + +void SwTextNode::EraseText(const SwIndex &rIdx, const sal_Int32 nCount, + const SwInsertFlags nMode ) +{ + assert(rIdx <= m_Text.getLength()); // invalid index + + const sal_Int32 nStartIdx = rIdx.GetIndex(); + const sal_Int32 nCnt = (nCount==SAL_MAX_INT32) + ? m_Text.getLength() - nStartIdx : nCount; + const sal_Int32 nEndIdx = nStartIdx + nCnt; + if (nEndIdx <= m_Text.getLength()) + m_Text = m_Text.replaceAt(nStartIdx, nCnt, ""); + + // GCAttr(); don't remove all empty ones, just the ones that are in the + // range but not at the end of the range. + + for ( size_t i = 0; m_pSwpHints && i < m_pSwpHints->Count(); ++i ) + { + SwTextAttr *pHt = m_pSwpHints->Get(i); + + const sal_Int32 nHintStart = pHt->GetStart(); + + if ( nHintStart < nStartIdx ) + continue; + + if ( nHintStart > nEndIdx ) + break; // hints are sorted by end, so break here + + const sal_Int32* pHtEndIdx = pHt->GetEnd(); + const sal_uInt16 nWhich = pHt->Which(); + + if( !pHtEndIdx ) + { + // attribute with neither end nor CH_TXTATR? + assert(pHt->HasDummyChar()); + if (isTXTATR(nWhich) && (nHintStart < nEndIdx)) + { + m_pSwpHints->DeleteAtPos(i); + DestroyAttr( pHt ); + --i; + } + continue; + } + + assert(!( (nHintStart < nEndIdx) && (*pHtEndIdx > nEndIdx) + && pHt->HasDummyChar() ) + // next line: deleting exactly dummy char: DeleteAttributes + || ((nHintStart == nStartIdx) && (nHintStart + 1 == nEndIdx))); + // "ERROR: deleting left-overlapped attribute with CH_TXTATR"); + + // Delete the hint if: + // 1. The hint ends before the deletion end position or + // 2. The hint ends at the deletion end position and + // we are not in empty expand mode and + // the hint is a [toxmark|refmark|ruby|inputfield] text attribute + // 3. deleting exactly the dummy char of an hint with end and dummy + // char deletes the hint + if ( (*pHtEndIdx < nEndIdx) + || ( (*pHtEndIdx == nEndIdx) && + !(SwInsertFlags::EMPTYEXPAND & nMode) && + ( (RES_TXTATR_TOXMARK == nWhich) || + (RES_TXTATR_REFMARK == nWhich) || + (RES_TXTATR_CJK_RUBY == nWhich) || + (RES_TXTATR_INPUTFIELD == nWhich) ) ) + || ( (nHintStart < nEndIdx) && + pHt->HasDummyChar() ) + ) + { + m_pSwpHints->DeleteAtPos(i); + DestroyAttr( pHt ); + --i; + } + } + + OSL_ENSURE(rIdx.GetIndex() == nStartIdx, "huh? start index has changed?"); + + TryDeleteSwpHints(); + + Update( rIdx, nCnt, true ); + + if( 1 == nCnt ) + { + SwDelChr aHint( nStartIdx ); + NotifyClients( nullptr, &aHint ); + } + else + { + SwDelText aHint( nStartIdx, nCnt ); + NotifyClients( nullptr, &aHint ); + } + + OSL_ENSURE(rIdx.GetIndex() == nStartIdx, "huh? start index has changed?"); + + // By deleting a character, the hidden flags + // at the TextNode can become invalid: + SetCalcHiddenCharFlags(); + + CHECK_SWPHINTS(this); +} + +void SwTextNode::GCAttr() +{ + if ( !HasHints() ) + return; + + bool bChanged = false; + sal_Int32 nMin = m_Text.getLength(); + sal_Int32 nMax = 0; + const bool bAll = nMin != 0; // on empty paragraphs only remove INetFormats + + for ( size_t i = 0; m_pSwpHints && i < m_pSwpHints->Count(); ++i ) + { + SwTextAttr * const pHt = m_pSwpHints->Get(i); + + // if end and start are equal, delete it + const sal_Int32 * const pEndIdx = pHt->GetEnd(); + if (pEndIdx && !pHt->HasDummyChar() && (*pEndIdx == pHt->GetStart()) + && ( bAll || pHt->Which() == RES_TXTATR_INETFMT ) ) + { + bChanged = true; + nMin = std::min( nMin, pHt->GetStart() ); + nMax = std::max( nMax, *pHt->GetEnd() ); + DestroyAttr( m_pSwpHints->Cut(i) ); + --i; + } + else + { + pHt->SetDontExpand( false ); + } + } + TryDeleteSwpHints(); + + if(bChanged) + { + // textframes react to aHint, others to aNew + SwUpdateAttr aHint( + nMin, + nMax, + 0); + + NotifyClients( nullptr, &aHint ); + SwFormatChg aNew( GetTextColl() ); + NotifyClients( nullptr, &aNew ); + } +} + +SwNumRule* SwTextNode::GetNumRule(bool bInParent) const +{ + SwNumRule* pRet = nullptr; + + const SfxPoolItem* pItem = GetNoCondAttr( RES_PARATR_NUMRULE, bInParent ); + bool bNoNumRule = false; + if ( pItem ) + { + OUString sNumRuleName = + static_cast(pItem)->GetValue(); + if (!sNumRuleName.isEmpty()) + { + pRet = GetDoc()->FindNumRulePtr( sNumRuleName ); + } + else // numbering is turned off + bNoNumRule = true; + } + + if ( !bNoNumRule ) + { + if ( pRet && pRet == GetDoc()->GetOutlineNumRule() && + ( !HasSwAttrSet() || + SfxItemState::SET != + GetpSwAttrSet()->GetItemState( RES_PARATR_NUMRULE, false ) ) ) + { + SwTextFormatColl* pColl = GetTextColl(); + if ( pColl ) + { + const SwNumRuleItem& rDirectItem = pColl->GetNumRule( false ); + if ( rDirectItem.GetValue().isEmpty() ) + { + pRet = nullptr; + } + } + } + } + + return pRet; +} + +void SwTextNode::NumRuleChgd() +{ + if ( IsInList() ) + { + SwNumRule* pNumRule = GetNumRule(); + if ( pNumRule && pNumRule != GetNum()->GetNumRule() ) + { + mpNodeNum->ChangeNumRule( *pNumRule ); + if (mpNodeNumRLHidden) + { + mpNodeNumRLHidden->ChangeNumRule(*pNumRule); + } + } + } + + if( IsInCache() ) + { + SwFrame::GetCache().Delete( this ); + SetInCache( false ); + } + SetInSwFntCache( false ); + + // Sending "noop" modify in order to cause invalidations of registered + // instances to get the list style change respectively the change + // in the list tree reflected in the layout. + // Important note: + { + SvxLRSpaceItem& rLR = const_cast(GetSwAttrSet().GetLRSpace()); + NotifyClients( &rLR, &rLR ); + } + + SetWordCountDirty( true ); +} + +// -> #i27615# +bool SwTextNode::IsNumbered(SwRootFrame const*const pLayout) const +{ + SwNumRule* pRule = GetNum(pLayout) ? GetNum(pLayout)->GetNumRule() : nullptr; + return pRule && IsCountedInList(); +} + +bool SwTextNode::HasMarkedLabel() const +{ + bool bResult = false; + + if ( IsInList() ) + { + bResult = + GetDoc()->getIDocumentListsAccess().getListByName( GetListId() )->IsListLevelMarked( GetActualListLevel() ); + } + + return bResult; +} +// <- #i27615# + +SwTextNode* SwTextNode::MakeNewTextNode( const SwNodeIndex& rPos, bool bNext, + bool bChgFollow ) +{ + // ignore hard PageBreak/PageDesc/ColumnBreak from Auto-Set + std::unique_ptr pNewAttrSet; + // #i75353# + bool bClearHardSetNumRuleWhenFormatCollChanges( false ); + if( HasSwAttrSet() ) + { + pNewAttrSet.reset(new SwAttrSet( *GetpSwAttrSet() )); + const SfxItemSet* pTmpSet = GetpSwAttrSet(); + + if (bNext) // successor doesn't inherit breaks! + pTmpSet = pNewAttrSet.get(); + + // !bNext: remove PageBreaks/PageDesc/ColBreak from this + bool bRemoveFromCache = false; + std::vector aClearWhichIds; + if ( bNext ) + bRemoveFromCache = ( 0 != pNewAttrSet->ClearItem( RES_PAGEDESC ) ); + else + aClearWhichIds.push_back( RES_PAGEDESC ); + + if( SfxItemState::SET == pTmpSet->GetItemState( RES_BREAK, false ) ) + { + if ( bNext ) + pNewAttrSet->ClearItem( RES_BREAK ); + else + aClearWhichIds.push_back( RES_BREAK ); + bRemoveFromCache = true; + } + if( SfxItemState::SET == pTmpSet->GetItemState( RES_KEEP, false ) ) + { + if ( bNext ) + pNewAttrSet->ClearItem( RES_KEEP ); + else + aClearWhichIds.push_back( RES_KEEP ); + bRemoveFromCache = true; + } + if( SfxItemState::SET == pTmpSet->GetItemState( RES_PARATR_SPLIT, false ) ) + { + if ( bNext ) + pNewAttrSet->ClearItem( RES_PARATR_SPLIT ); + else + aClearWhichIds.push_back( RES_PARATR_SPLIT ); + bRemoveFromCache = true; + } + if(SfxItemState::SET == pTmpSet->GetItemState(RES_PARATR_NUMRULE, false)) + { + SwNumRule * pRule = GetNumRule(); + + if (pRule && IsOutline()) + { + if ( bNext ) + pNewAttrSet->ClearItem(RES_PARATR_NUMRULE); + else + { + // #i75353# + // No clear of hard set numbering rule at an outline paragraph at this point. + // Only if the paragraph style changes - see below. + bClearHardSetNumRuleWhenFormatCollChanges = true; + } + bRemoveFromCache = true; + } + } + + if ( !aClearWhichIds.empty() ) + bRemoveFromCache = 0 != ClearItemsFromAttrSet( aClearWhichIds ); + + if( !bNext && bRemoveFromCache && IsInCache() ) + { + SwFrame::GetCache().Delete( this ); + SetInCache( false ); + } + } + SwNodes& rNds = GetNodes(); + + SwTextFormatColl* pColl = GetTextColl(); + + SwTextNode *pNode = new SwTextNode( rPos, pColl, pNewAttrSet.get() ); + + pNewAttrSet.reset(); + + const SwNumRule* pRule = GetNumRule(); + if( pRule && pRule == pNode->GetNumRule() && rNds.IsDocNodes() ) + { + // #i55459# + // - correction: parameter has to be checked, as it was in the + // previous implementation. + if ( !bNext && !IsCountedInList() ) + SetCountedInList(true); + } + + // In case the numbering caused a style from the pool to be assigned to + // the new node, don't overwrite that here! + if( pColl != pNode->GetTextColl() || + ( bChgFollow && pColl != GetTextColl() )) + return pNode; // that ought to be enough? + + pNode->ChgTextCollUpdateNum( nullptr, pColl ); // for numbering/outline + if( bNext || !bChgFollow ) + return pNode; + + SwTextFormatColl *pNextColl = &pColl->GetNextTextFormatColl(); + // i#101870 perform action on different paragraph styles before applying + // the new paragraph style + if (pNextColl != pColl) + { + // #i75353# + if ( bClearHardSetNumRuleWhenFormatCollChanges ) + { + std::vector aClearWhichIds; + aClearWhichIds.push_back( RES_PARATR_NUMRULE ); + if ( ClearItemsFromAttrSet( aClearWhichIds ) != 0 && IsInCache() ) + { + SwFrame::GetCache().Delete( this ); + SetInCache( false ); + } + } + } + ChgFormatColl( pNextColl ); + + return pNode; +} + +SwContentNode* SwTextNode::AppendNode( const SwPosition & rPos ) +{ + // position behind which it will be inserted + SwNodeIndex aIdx( rPos.nNode, 1 ); + SwTextNode* pNew = MakeNewTextNode( aIdx ); + + // reset list attributes at appended text node + pNew->ResetAttr( RES_PARATR_LIST_ISRESTART ); + pNew->ResetAttr( RES_PARATR_LIST_RESTARTVALUE ); + pNew->ResetAttr( RES_PARATR_LIST_ISCOUNTED ); + if ( pNew->GetNumRule() == nullptr ) + { + pNew->ResetAttr( RES_PARATR_LIST_ID ); + pNew->ResetAttr( RES_PARATR_LIST_LEVEL ); + } + + if (!IsInList() && GetNumRule() && !GetListId().isEmpty()) + { + AddToList(); + } + + if( HasWriterListeners() ) + MakeFramesForAdjacentContentNode(*pNew); + return pNew; +} + +SwTextAttr * SwTextNode::GetTextAttrForCharAt( + const sal_Int32 nIndex, + const sal_uInt16 nWhich ) const +{ + assert(nWhich >= RES_TXTATR_BEGIN && nWhich <= RES_TXTATR_END); + if ( HasHints() ) + { + for ( size_t i = 0; i < m_pSwpHints->Count(); ++i ) + { + SwTextAttr * const pHint = m_pSwpHints->Get(i); + const sal_Int32 nStartPos = pHint->GetStart(); + if ( nIndex < nStartPos ) + { + return nullptr; + } + if ( (nIndex == nStartPos) && pHint->HasDummyChar() ) + { + return ( RES_TXTATR_END == nWhich || nWhich == pHint->Which() ) + ? pHint : nullptr; + } + } + } + return nullptr; +} + +namespace +{ + +sal_uInt16 lcl_BoundListLevel(const int nActualLevel) +{ + return static_cast( std::min( std::max(nActualLevel, 0), MAXLEVEL-1 ) ); +} + +} + +// -> #i29560# +bool SwTextNode::HasNumber() const +{ + bool bResult = false; + + const SwNumRule* pRule = GetNum() ? GetNum()->GetNumRule() : nullptr; + if ( pRule ) + { + const SwNumFormat& aFormat(pRule->Get(lcl_BoundListLevel(GetActualListLevel()))); + + // #i40041# + bResult = aFormat.IsEnumeration() && + SVX_NUM_NUMBER_NONE != aFormat.GetNumberingType(); + } + + return bResult; +} + +bool SwTextNode::HasBullet() const +{ + bool bResult = false; + + const SwNumRule* pRule = GetNum() ? GetNum()->GetNumRule() : nullptr; + if ( pRule ) + { + const SwNumFormat& aFormat(pRule->Get(lcl_BoundListLevel(GetActualListLevel()))); + + bResult = aFormat.IsItemize(); + } + + return bResult; +} +// <- #i29560# + +// #128041# - introduce parameter <_bInclPrefixAndSuffixStrings> +//i53420 added max outline parameter +OUString SwTextNode::GetNumString( const bool _bInclPrefixAndSuffixStrings, + const unsigned int _nRestrictToThisLevel, + SwRootFrame const*const pLayout) const +{ + if (GetDoc()->IsClipBoard() && m_pNumStringCache) + { + // #i111677# do not expand number strings in clipboard documents + return *m_pNumStringCache; + } + const SwNumRule* pRule = GetNum(pLayout) ? GetNum(pLayout)->GetNumRule() : nullptr; + if ( pRule && + IsCountedInList() ) + { + SvxNumberType const& rNumberType( + pRule->Get( lcl_BoundListLevel(GetActualListLevel()) ) ); + if (rNumberType.IsTextFormat() || + + (style::NumberingType::NUMBER_NONE == rNumberType.GetNumberingType())) + { + return pRule->MakeNumString( GetNum(pLayout)->GetNumberVector(), + _bInclPrefixAndSuffixStrings, + false, + _nRestrictToThisLevel, + nullptr, + GetLang(0)); + } + } + + return OUString(); +} + +long SwTextNode::GetLeftMarginWithNum( bool bTextLeft ) const +{ + long nRet = 0; + const SwNumRule* pRule = GetNum() ? GetNum()->GetNumRule() : nullptr; + if( pRule ) + { + const SwNumFormat& rFormat = pRule->Get(lcl_BoundListLevel(GetActualListLevel())); + + if ( rFormat.GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_WIDTH_AND_POSITION ) + { + nRet = rFormat.GetAbsLSpace(); + + if( !bTextLeft ) + { + if( 0 > rFormat.GetFirstLineOffset() && + nRet > -rFormat.GetFirstLineOffset() ) + nRet = nRet + rFormat.GetFirstLineOffset(); + else + nRet = 0; + } + + if( pRule->IsAbsSpaces() ) + nRet = nRet - GetSwAttrSet().GetLRSpace().GetLeft(); + } + else if ( rFormat.GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_ALIGNMENT ) + { + if ( AreListLevelIndentsApplicable() ) + { + nRet = rFormat.GetIndentAt(); + // #i90401# + // Only negative first line indents have consider for the left margin + if ( !bTextLeft && + rFormat.GetFirstLineIndent() < 0 ) + { + nRet = nRet + rFormat.GetFirstLineIndent(); + } + } + } + } + + return nRet; +} + +bool SwTextNode::GetFirstLineOfsWithNum( short& rFLOffset ) const +{ + // #i95907# + rFLOffset = 0; + + // #i51089# + const SwNumRule* pRule = GetNum() ? GetNum()->GetNumRule() : nullptr; + if ( pRule ) + { + if ( IsCountedInList() ) + { + const SwNumFormat& rFormat = pRule->Get(lcl_BoundListLevel(GetActualListLevel())); + if ( rFormat.GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_WIDTH_AND_POSITION ) + { + rFLOffset = rFormat.GetFirstLineOffset(); //TODO: overflow + + if (!getIDocumentSettingAccess()->get(DocumentSettingId::IGNORE_FIRST_LINE_INDENT_IN_NUMBERING)) + { + SvxLRSpaceItem aItem = GetSwAttrSet().GetLRSpace(); + rFLOffset = rFLOffset + aItem.GetTextFirstLineOffset(); + } + } + else if ( rFormat.GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_ALIGNMENT ) + { + if ( AreListLevelIndentsApplicable() ) + { + rFLOffset = rFormat.GetFirstLineIndent(); + } + else if (!getIDocumentSettingAccess()->get(DocumentSettingId::IGNORE_FIRST_LINE_INDENT_IN_NUMBERING)) + { + SvxLRSpaceItem aItem = GetSwAttrSet().GetLRSpace(); + rFLOffset = aItem.GetTextFirstLineOffset(); + } + } + } + + return true; + } + + rFLOffset = GetSwAttrSet().GetLRSpace().GetTextFirstLineOffset(); + return false; +} + +SwTwips SwTextNode::GetAdditionalIndentForStartingNewList() const +{ + SwTwips nAdditionalIndent = 0; + + const SwNumRule* pRule = GetNum() ? GetNum()->GetNumRule() : nullptr; + if ( pRule ) + { + const SwNumFormat& rFormat = pRule->Get(lcl_BoundListLevel(GetActualListLevel())); + if ( rFormat.GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_WIDTH_AND_POSITION ) + { + nAdditionalIndent = GetSwAttrSet().GetLRSpace().GetLeft(); + + if (getIDocumentSettingAccess()->get(DocumentSettingId::IGNORE_FIRST_LINE_INDENT_IN_NUMBERING)) + { + nAdditionalIndent = nAdditionalIndent - + GetSwAttrSet().GetLRSpace().GetTextFirstLineOffset(); + } + } + else if ( rFormat.GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_ALIGNMENT ) + { + if ( AreListLevelIndentsApplicable() ) + { + nAdditionalIndent = rFormat.GetIndentAt() + rFormat.GetFirstLineIndent(); + } + else + { + nAdditionalIndent = GetSwAttrSet().GetLRSpace().GetLeft(); + if (getIDocumentSettingAccess()->get(DocumentSettingId::IGNORE_FIRST_LINE_INDENT_IN_NUMBERING)) + { + nAdditionalIndent = nAdditionalIndent - + GetSwAttrSet().GetLRSpace().GetTextFirstLineOffset(); + } + } + } + } + else + { + nAdditionalIndent = GetSwAttrSet().GetLRSpace().GetLeft(); + } + + return nAdditionalIndent; +} + +// #i96772# +void SwTextNode::ClearLRSpaceItemDueToListLevelIndents( std::shared_ptr& o_rLRSpaceItem ) const +{ + if ( AreListLevelIndentsApplicable() ) + { + const SwNumRule* pRule = GetNumRule(); + if ( pRule && GetActualListLevel() >= 0 ) + { + const SwNumFormat& rFormat = pRule->Get(lcl_BoundListLevel(GetActualListLevel())); + if ( rFormat.GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_ALIGNMENT ) + { + o_rLRSpaceItem = std::make_shared(RES_LR_SPACE); + } + } + } +} + +// #i91133# +long SwTextNode::GetLeftMarginForTabCalculation() const +{ + long nLeftMarginForTabCalc = 0; + + bool bLeftMarginForTabCalcSetToListLevelIndent( false ); + const SwNumRule* pRule = GetNum() ? GetNum()->GetNumRule() : nullptr; + if( pRule ) + { + const SwNumFormat& rFormat = pRule->Get(lcl_BoundListLevel(GetActualListLevel())); + if ( rFormat.GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_ALIGNMENT ) + { + if ( AreListLevelIndentsApplicable() ) + { + nLeftMarginForTabCalc = rFormat.GetIndentAt(); + bLeftMarginForTabCalcSetToListLevelIndent = true; + } + } + } + if ( !bLeftMarginForTabCalcSetToListLevelIndent ) + { + nLeftMarginForTabCalc = GetSwAttrSet().GetLRSpace().GetTextLeft(); + } + + return nLeftMarginForTabCalc; +} + +static void Replace0xFF( + SwTextNode const& rNode, + OUStringBuffer & rText, + sal_Int32 & rTextStt, + sal_Int32 nEndPos ) +{ + if (rNode.GetpSwpHints()) + { + sal_Unicode cSrchChr = CH_TXTATR_BREAKWORD; + for( int nSrchIter = 0; 2 > nSrchIter; ++nSrchIter, cSrchChr = CH_TXTATR_INWORD ) + { + sal_Int32 nPos = rText.indexOf(cSrchChr); + while (-1 != nPos && nPos < nEndPos) + { + const SwTextAttr* const pAttr = + rNode.GetTextAttrForCharAt(rTextStt + nPos); + if( pAttr ) + { + switch( pAttr->Which() ) + { + case RES_TXTATR_FIELD: + case RES_TXTATR_ANNOTATION: + rText.remove(nPos, 1); + ++rTextStt; + break; + + case RES_TXTATR_FTN: + rText.remove(nPos, 1); + ++rTextStt; + break; + + default: + rText.remove(nPos, 1); + ++rTextStt; + } + } + else + { + ++nPos; + ++nEndPos; + } + nPos = rText.indexOf(cSrchChr, nPos); + } + } + } +} + +// Expand fields +// #i83479# - handling of new parameters +OUString SwTextNode::GetExpandText(SwRootFrame const*const pLayout, + const sal_Int32 nIdx, + const sal_Int32 nLen, + const bool bWithNum, + const bool bAddSpaceAfterListLabelStr, + const bool bWithSpacesForLevel, + const ExpandMode eAdditionalMode) const + +{ + ExpandMode eMode = ExpandMode::ExpandFields | eAdditionalMode; + if (pLayout && pLayout->IsHideRedlines()) + { + eMode |= ExpandMode::HideDeletions; + } + + ModelToViewHelper aConversionMap(*this, pLayout, eMode); + const OUString aExpandText = aConversionMap.getViewText(); + const sal_Int32 nExpandBegin = aConversionMap.ConvertToViewPosition( nIdx ); + sal_Int32 nEnd = nLen == -1 ? GetText().getLength() : nIdx + nLen; + const sal_Int32 nExpandEnd = aConversionMap.ConvertToViewPosition( nEnd ); + OUStringBuffer aText(aExpandText.copy(nExpandBegin, nExpandEnd-nExpandBegin)); + + // remove dummy characters of Input Fields + comphelper::string::remove(aText, CH_TXT_ATR_INPUTFIELDSTART); + comphelper::string::remove(aText, CH_TXT_ATR_INPUTFIELDEND); + + if( bWithNum ) + { + if (!GetNumString(true, MAXLEVEL, pLayout).isEmpty()) + { + if ( bAddSpaceAfterListLabelStr ) + { + const sal_Unicode aSpace = ' '; + aText.insert(0, aSpace); + } + aText.insert(0, GetNumString(true, MAXLEVEL, pLayout)); + } + } + + if (bWithSpacesForLevel) + { + const sal_Unicode aSpace = ' '; + for (int nLevel = GetActualListLevel(); nLevel > 0; --nLevel) + { + aText.insert(0, aSpace); + aText.insert(0, aSpace); + } + } + + return aText.makeStringAndClear(); +} + +bool SwTextNode::CopyExpandText(SwTextNode& rDestNd, const SwIndex* pDestIdx, + sal_Int32 nIdx, sal_Int32 nLen, + SwRootFrame const*const pLayout, bool bWithNum, + bool bWithFootnote, bool bReplaceTabsWithSpaces ) const +{ + if( &rDestNd == this ) + return false; + + SwIndex aDestIdx(&rDestNd, rDestNd.GetText().getLength()); + if( pDestIdx ) + aDestIdx = *pDestIdx; + const sal_Int32 nDestStt = aDestIdx.GetIndex(); + + // first, start with the text + OUStringBuffer buf(GetText()); + if( bReplaceTabsWithSpaces ) + buf.replace('\t', ' '); + + // mask hidden characters + const sal_Unicode cChar = CH_TXTATR_BREAKWORD; + SwScriptInfo::MaskHiddenRanges(*this, buf, 0, buf.getLength(), cChar); + + buf.remove(0, nIdx); + if (nLen != -1) + { + buf.truncate(nLen); + } + // remove dummy characters of Input Fields + { + comphelper::string::remove(buf, CH_TXT_ATR_INPUTFIELDSTART); + comphelper::string::remove(buf, CH_TXT_ATR_INPUTFIELDEND); + } + rDestNd.InsertText(buf.makeStringAndClear(), aDestIdx); + nLen = aDestIdx.GetIndex() - nDestStt; + + // set all char attributes with Symbol font + if ( HasHints() ) + { + sal_Int32 nInsPos = nDestStt - nIdx; + for ( size_t i = 0; i < m_pSwpHints->Count(); ++i ) + { + const SwTextAttr* pHt = m_pSwpHints->Get(i); + const sal_Int32 nAttrStartIdx = pHt->GetStart(); + const sal_uInt16 nWhich = pHt->Which(); + if (nIdx + nLen <= nAttrStartIdx) + break; // behind end of text + + const sal_Int32 *pEndIdx = pHt->End(); + if( pEndIdx && *pEndIdx > nIdx && + ( RES_CHRATR_FONT == nWhich || + RES_TXTATR_CHARFMT == nWhich || + RES_TXTATR_AUTOFMT == nWhich )) + { + const SvxFontItem* const pFont = + CharFormat::GetItem( *pHt, RES_CHRATR_FONT ); + if ( pFont && RTL_TEXTENCODING_SYMBOL == pFont->GetCharSet() ) + { + // attribute in area => copy + rDestNd.InsertItem( *const_cast(pFont), + nInsPos + nAttrStartIdx, nInsPos + *pEndIdx ); + } + } + else if ( pHt->HasDummyChar() && (nAttrStartIdx >= nIdx) ) + { + aDestIdx = nInsPos + nAttrStartIdx; + switch( nWhich ) + { + case RES_TXTATR_FIELD: + case RES_TXTATR_ANNOTATION: + { + OUString const aExpand( + static_txtattr_cast(pHt)->GetFormatField().GetField()->ExpandField(true, pLayout)); + if (!aExpand.isEmpty()) + { + ++aDestIdx; // insert behind + OUString const ins( + rDestNd.InsertText( aExpand, aDestIdx)); + SAL_INFO_IF(ins.getLength() != aExpand.getLength(), + "sw.core", "GetExpandText lossage"); + aDestIdx = nInsPos + nAttrStartIdx; + nInsPos += ins.getLength(); + } + rDestNd.EraseText( aDestIdx, 1 ); + --nInsPos; + } + break; + + case RES_TXTATR_FTN: + { + if ( bWithFootnote ) + { + const SwFormatFootnote& rFootnote = pHt->GetFootnote(); + OUString sExpand; + auto const number(pLayout && pLayout->IsHideRedlines() + ? rFootnote.GetNumberRLHidden() + : rFootnote.GetNumber()); + if( !rFootnote.GetNumStr().isEmpty() ) + sExpand = rFootnote.GetNumStr(); + else if( rFootnote.IsEndNote() ) + sExpand = GetDoc()->GetEndNoteInfo().m_aFormat. + GetNumStr(number); + else + sExpand = GetDoc()->GetFootnoteInfo().m_aFormat. + GetNumStr(number); + if( !sExpand.isEmpty() ) + { + ++aDestIdx; // insert behind + SvxEscapementItem aItem( SvxEscapement::Superscript, RES_CHRATR_ESCAPEMENT ); + rDestNd.InsertItem( + aItem, + aDestIdx.GetIndex(), + aDestIdx.GetIndex() ); + OUString const ins( rDestNd.InsertText(sExpand, aDestIdx, SwInsertFlags::EMPTYEXPAND)); + SAL_INFO_IF(ins.getLength() != sExpand.getLength(), + "sw.core", "GetExpandText lossage"); + aDestIdx = nInsPos + nAttrStartIdx; + nInsPos += ins.getLength(); + } + } + rDestNd.EraseText( aDestIdx, 1 ); + --nInsPos; + } + break; + + default: + rDestNd.EraseText( aDestIdx, 1 ); + --nInsPos; + } + } + } + } + + if( bWithNum ) + { + aDestIdx = nDestStt; + rDestNd.InsertText( GetNumString(true, MAXLEVEL, pLayout), aDestIdx ); + } + + aDestIdx = 0; + sal_Int32 nStartDelete(-1); + while (aDestIdx < rDestNd.GetText().getLength()) + { + sal_Unicode const cur(rDestNd.GetText()[aDestIdx.GetIndex()]); + if ( (cChar == cur) // filter substituted hidden text + || (CH_TXT_ATR_FIELDSTART == cur) // filter all fieldmarks + || (CH_TXT_ATR_FIELDSEP == cur) + || (CH_TXT_ATR_FIELDEND == cur) + || (CH_TXT_ATR_FORMELEMENT == cur)) + { + if (-1 == nStartDelete) + { + nStartDelete = aDestIdx.GetIndex(); // start deletion range + } + ++aDestIdx; + if (aDestIdx < rDestNd.GetText().getLength()) + { + continue; + } // else: end of paragraph => delete, see below + } + else + { + if (-1 == nStartDelete) + { + ++aDestIdx; + continue; + } // else: delete, see below + } + assert(-1 != nStartDelete); // without delete range, would have continued + rDestNd.EraseText( + SwIndex(&rDestNd, nStartDelete), + aDestIdx.GetIndex() - nStartDelete); + assert(aDestIdx.GetIndex() == nStartDelete); + nStartDelete = -1; // reset + } + + return true; +} + +OUString SwTextNode::GetRedlineText() const +{ + std::vector aRedlArr; + const SwDoc* pDoc = GetDoc(); + SwRedlineTable::size_type nRedlPos = pDoc->getIDocumentRedlineAccess().GetRedlinePos( *this, RedlineType::Delete ); + if( SwRedlineTable::npos != nRedlPos ) + { + // some redline-delete object exists for the node + const sal_uLong nNdIdx = GetIndex(); + for( ; nRedlPos < pDoc->getIDocumentRedlineAccess().GetRedlineTable().size() ; ++nRedlPos ) + { + const SwRangeRedline* pTmp = pDoc->getIDocumentRedlineAccess().GetRedlineTable()[ nRedlPos ]; + if( RedlineType::Delete == pTmp->GetType() ) + { + const SwPosition *pRStt = pTmp->Start(), *pREnd = pTmp->End(); + if( pRStt->nNode < nNdIdx ) + { + if( pREnd->nNode > nNdIdx ) + // paragraph is fully deleted + return OUString(); + else if( pREnd->nNode == nNdIdx ) + { + // deleted from 0 to nContent + aRedlArr.push_back( 0 ); + aRedlArr.push_back( pREnd->nContent.GetIndex() ); + } + } + else if( pRStt->nNode == nNdIdx ) + { + //aRedlArr.Insert( pRStt->nContent.GetIndex(), aRedlArr.Count() ); + aRedlArr.push_back( pRStt->nContent.GetIndex() ); + if( pREnd->nNode == nNdIdx ) + aRedlArr.push_back( pREnd->nContent.GetIndex() ); + else + { + aRedlArr.push_back(GetText().getLength()); + break; // that was all + } + } + else + break; // that was all + } + } + } + + OUStringBuffer aText(GetText()); + + sal_Int32 nTextStt = 0; + sal_Int32 nIdxEnd = aText.getLength(); + for( size_t n = 0; n < aRedlArr.size(); n += 2 ) + { + sal_Int32 nStt = aRedlArr[ n ]; + sal_Int32 nEnd = aRedlArr[ n+1 ]; + if( ( 0 <= nStt && nStt <= nIdxEnd ) || + ( 0 <= nEnd && nEnd <= nIdxEnd )) + { + if( nStt < 0 ) nStt = 0; + if( nIdxEnd < nEnd ) nEnd = nIdxEnd; + const sal_Int32 nDelCnt = nEnd - nStt; + aText.remove(nStt - nTextStt, nDelCnt); + Replace0xFF(*this, aText, nTextStt, nStt - nTextStt); + nTextStt += nDelCnt; + } + else if( nStt >= nIdxEnd ) + break; + } + Replace0xFF(*this, aText, nTextStt, aText.getLength()); + + return aText.makeStringAndClear(); +} + +void SwTextNode::ReplaceText( const SwIndex& rStart, const sal_Int32 nDelLen, + const OUString & rStr) +{ + assert( rStart.GetIndex() < m_Text.getLength() // index out of bounds + && rStart.GetIndex() + nDelLen <= m_Text.getLength()); + + sal_Int32 const nOverflow(rStr.getLength() - nDelLen - GetSpaceLeft()); + SAL_WARN_IF(nOverflow > 0, "sw.core", + "SwTextNode::ReplaceText: node text with insertion > node capacity."); + OUString const sInserted( + (nOverflow > 0) ? rStr.copy(0, rStr.getLength() - nOverflow) : rStr); + if (sInserted.isEmpty() && 0 == nDelLen) + { + return; // nothing to do + } + + const sal_Int32 nStartPos = rStart.GetIndex(); + sal_Int32 nEndPos = nStartPos + nDelLen; + sal_Int32 nLen = nDelLen; + for( sal_Int32 nPos = nStartPos; nPos < nEndPos; ++nPos ) + { + if ((CH_TXTATR_BREAKWORD == m_Text[nPos]) || + (CH_TXTATR_INWORD == m_Text[nPos])) + { + SwTextAttr *const pHint = GetTextAttrForCharAt( nPos ); + if (pHint) + { + assert(!( pHint->GetEnd() && pHint->HasDummyChar() + && (pHint->GetStart() < nEndPos) + && (*pHint->GetEnd() > nEndPos) )); + // "deleting left-overlapped attribute with CH_TXTATR" + DeleteAttribute( pHint ); + --nEndPos; + --nLen; + } + } + } + + bool bOldExpFlg = IsIgnoreDontExpand(); + SetIgnoreDontExpand( true ); + + if (nLen && sInserted.getLength()) + { + // Replace the 1st char, then delete the rest and insert. + // This way the attributes of the 1st char are expanded! + m_Text = m_Text.replaceAt(nStartPos, 1, sInserted.copy(0, 1)); + + ++const_cast(rStart); + m_Text = m_Text.replaceAt(rStart.GetIndex(), nLen - 1, ""); + Update( rStart, nLen - 1, true ); + + OUString aTmpText( sInserted.copy(1) ); + m_Text = m_Text.replaceAt(rStart.GetIndex(), 0, aTmpText); + Update( rStart, aTmpText.getLength() ); + } + else + { + m_Text = m_Text.replaceAt(nStartPos, nLen, ""); + Update( rStart, nLen, true ); + + m_Text = m_Text.replaceAt(nStartPos, 0, sInserted); + Update( rStart, sInserted.getLength() ); + } + + SetIgnoreDontExpand( bOldExpFlg ); + SwDelText aDelHint( nStartPos, nDelLen ); + NotifyClients( nullptr, &aDelHint ); + + if (sInserted.getLength()) + { + SwInsText aHint( nStartPos, sInserted.getLength() ); + NotifyClients( nullptr, &aHint ); + } +} + +namespace { + void lcl_ResetParAttrs( SwTextNode &rTextNode ) + { + std::set aAttrs; + aAttrs.insert( aAttrs.end(), RES_PARATR_LIST_ID ); + aAttrs.insert( aAttrs.end(), RES_PARATR_LIST_LEVEL ); + aAttrs.insert( aAttrs.end(), RES_PARATR_LIST_ISRESTART ); + aAttrs.insert( aAttrs.end(), RES_PARATR_LIST_RESTARTVALUE ); + aAttrs.insert( aAttrs.end(), RES_PARATR_LIST_ISCOUNTED ); + SwPaM aPam( rTextNode ); + // #i96644# + // suppress side effect "send data changed events" + rTextNode.GetDoc()->ResetAttrs( aPam, false, aAttrs, false ); + } + + // Helper method for special handling of modified attributes at text node. + // The following is handled: + // (1) on changing the paragraph style - RES_FMT_CHG: + // Check, if list style of the text node is changed. If yes, add respectively + // remove the text node to the corresponding list. + // (2) on changing the attributes - RES_ATTRSET_CHG: + // Same as (1). + // (3) on changing the list style - RES_PARATR_NUMRULE: + // Same as (1). + void HandleModifyAtTextNode( SwTextNode& rTextNode, + const SfxPoolItem* pOldValue, + const SfxPoolItem* pNewValue ) + { + const sal_uInt16 nWhich = pOldValue ? pOldValue->Which() : + pNewValue ? pNewValue->Which() : 0 ; + bool bNumRuleSet = false; + bool bParagraphStyleChanged = false; + OUString sNumRule; + OUString sOldNumRule; + switch ( nWhich ) + { + case RES_FMT_CHG: + { + bParagraphStyleChanged = true; + if( rTextNode.GetNodes().IsDocNodes() ) + { + const SwNumRule* pFormerNumRuleAtTextNode = + rTextNode.GetNum() ? rTextNode.GetNum()->GetNumRule() : nullptr; + if ( pFormerNumRuleAtTextNode ) + { + sOldNumRule = pFormerNumRuleAtTextNode->GetName(); + } + if ( rTextNode.IsEmptyListStyleDueToSetOutlineLevelAttr() ) + { + const SwNumRuleItem& rNumRuleItem = rTextNode.GetTextColl()->GetNumRule(); + if ( !rNumRuleItem.GetValue().isEmpty() ) + { + rTextNode.ResetEmptyListStyleDueToResetOutlineLevelAttr(); + } + } + const SwNumRule* pNumRuleAtTextNode = rTextNode.GetNumRule(); + if ( pNumRuleAtTextNode ) + { + bNumRuleSet = true; + sNumRule = pNumRuleAtTextNode->GetName(); + } + } + break; + } + case RES_ATTRSET_CHG: + { + const SfxPoolItem* pItem = nullptr; + const SwNumRule* pFormerNumRuleAtTextNode = + rTextNode.GetNum() ? rTextNode.GetNum()->GetNumRule() : nullptr; + if ( pFormerNumRuleAtTextNode ) + { + sOldNumRule = pFormerNumRuleAtTextNode->GetName(); + } + + const SwAttrSetChg* pSet = dynamic_cast(pNewValue); + if ( pSet && pSet->GetChgSet()->GetItemState( RES_PARATR_NUMRULE, false, &pItem ) == + SfxItemState::SET ) + { + // #i70748# + rTextNode.ResetEmptyListStyleDueToResetOutlineLevelAttr(); + bNumRuleSet = true; + } + // #i70748# + // The new list style set at the paragraph. + const SwNumRule* pNumRuleAtTextNode = rTextNode.GetNumRule(); + if ( pNumRuleAtTextNode ) + { + sNumRule = pNumRuleAtTextNode->GetName(); + } + break; + } + case RES_PARATR_NUMRULE: + { + if ( rTextNode.GetNodes().IsDocNodes() ) + { + const SwNumRule* pFormerNumRuleAtTextNode = + rTextNode.GetNum() ? rTextNode.GetNum()->GetNumRule() : nullptr; + if ( pFormerNumRuleAtTextNode ) + { + sOldNumRule = pFormerNumRuleAtTextNode->GetName(); + } + + if ( pNewValue ) + { + // #i70748# + rTextNode.ResetEmptyListStyleDueToResetOutlineLevelAttr(); + bNumRuleSet = true; + } + // #i70748# + // The new list style set at the paragraph. + const SwNumRule* pNumRuleAtTextNode = rTextNode.GetNumRule(); + if ( pNumRuleAtTextNode ) + { + sNumRule = pNumRuleAtTextNode->GetName(); + } + } + break; + } + } + if ( sNumRule != sOldNumRule ) + { + if ( bNumRuleSet ) + { + if (sNumRule.isEmpty()) + { + rTextNode.RemoveFromList(); + if ( bParagraphStyleChanged ) + { + lcl_ResetParAttrs(rTextNode); + } + } + else + { + rTextNode.RemoveFromList(); + // If new list style is the outline style, apply outline + // level as the list level. + if (sNumRule==SwNumRule::GetOutlineRuleName()) + { + // #i70748# + OSL_ENSURE( rTextNode.GetTextColl()->IsAssignedToListLevelOfOutlineStyle(), + " - text node with outline style, but its paragraph style is not assigned to outline style." ); + const int nNewListLevel = + rTextNode.GetTextColl()->GetAssignedOutlineStyleLevel(); + if ( 0 <= nNewListLevel && nNewListLevel < MAXLEVEL ) + { + rTextNode.SetAttrListLevel( nNewListLevel ); + } + } + rTextNode.AddToList(); + } + } + else // + { + rTextNode.RemoveFromList(); + if ( bParagraphStyleChanged ) + { + lcl_ResetParAttrs(rTextNode); + // #i70748# + if ( dynamic_cast(rTextNode.GetAttr( RES_PARATR_OUTLINELEVEL, false )).GetValue() > 0 ) + { + rTextNode.SetEmptyListStyleDueToSetOutlineLevelAttr(); + } + } + } + } + else if (!sNumRule.isEmpty() && !rTextNode.IsInList()) + { + rTextNode.AddToList(); + } + } + // End of method +} + +SwFormatColl* SwTextNode::ChgFormatColl( SwFormatColl *pNewColl ) +{ + OSL_ENSURE( pNewColl,"ChgFormatColl: Collectionpointer has value 0." ); + OSL_ENSURE( dynamic_cast(pNewColl) != nullptr, + "ChgFormatColl: is not a Text Collection pointer." ); + + SwTextFormatColl *pOldColl = GetTextColl(); + if( pNewColl != pOldColl ) + { + SetCalcHiddenCharFlags(); + SwContentNode::ChgFormatColl( pNewColl ); + OSL_ENSURE( !mbInSetOrResetAttr, + "DEBUG OSL_ENSURE(ON - called during " ); + if ( !mbInSetOrResetAttr ) + { + SwFormatChg aTmp1( pOldColl ); + SwFormatChg aTmp2( pNewColl ); + HandleModifyAtTextNode( *this, &aTmp1, &aTmp2 ); + } + + // reset fill information on parent style change + if(maFillAttributes) + { + maFillAttributes.reset(); + } + } + + // only for real nodes-array + if( GetNodes().IsDocNodes() ) + { + ChgTextCollUpdateNum( pOldColl, static_cast(pNewColl) ); + } + + GetNodes().UpdateOutlineNode(*this); + + return pOldColl; +} + +const SwNodeNum* SwTextNode::GetNum(SwRootFrame const*const pLayout) const +{ + // invariant: it's only in list in Hide mode if it's in list in normal mode + assert(mpNodeNum || !mpNodeNumRLHidden); + return pLayout && pLayout->IsHideRedlines() ? mpNodeNumRLHidden.get() : mpNodeNum.get(); +} + +void SwTextNode::DoNum(std::function const& rFunc) +{ + // temp. clear because GetActualListLevel() may be called and the assert + // there triggered during update, which is unhelpful + std::unique_ptr pBackup = std::move(mpNodeNumRLHidden); + assert(mpNodeNum); + rFunc(*mpNodeNum); + if (pBackup) + { + mpNodeNumRLHidden = std::move(pBackup); + rFunc(*mpNodeNumRLHidden); + } +} + +SwNumberTree::tNumberVector +SwTextNode::GetNumberVector(SwRootFrame const*const pLayout) const +{ + if (SwNodeNum const*const pNum = GetNum(pLayout)) + { + return pNum->GetNumberVector(); + } + else + { + SwNumberTree::tNumberVector aResult; + return aResult; + } +} + +bool SwTextNode::IsOutline() const +{ + bool bResult = false; + + if ( GetAttrOutlineLevel() > 0 ) + { + bResult = !IsInRedlines(); + } + else + { + const SwNumRule* pRule( GetNum() ? GetNum()->GetNumRule() : nullptr ); + if ( pRule && pRule->IsOutlineRule() ) + { + bResult = !IsInRedlines(); + } + } + + return bResult; +} + +bool SwTextNode::IsOutlineStateChanged() const +{ + return IsOutline() != m_bLastOutlineState; +} + +void SwTextNode::UpdateOutlineState() +{ + m_bLastOutlineState = IsOutline(); +} + +int SwTextNode::GetAttrOutlineLevel() const +{ + return static_cast(GetAttr(RES_PARATR_OUTLINELEVEL)).GetValue(); +} + +void SwTextNode::SetAttrOutlineLevel(int nLevel) +{ + assert(0 <= nLevel && nLevel <= MAXLEVEL); // Level Out Of Range + if ( 0 <= nLevel && nLevel <= MAXLEVEL ) + { + SetAttr( SfxUInt16Item( RES_PARATR_OUTLINELEVEL, + static_cast(nLevel) ) ); + } +} + +// #i70748# + +void SwTextNode::SetEmptyListStyleDueToSetOutlineLevelAttr() +{ + if ( !mbEmptyListStyleSetDueToSetOutlineLevelAttr ) + { + SetAttr( SwNumRuleItem() ); + mbEmptyListStyleSetDueToSetOutlineLevelAttr = true; + } +} + +void SwTextNode::ResetEmptyListStyleDueToResetOutlineLevelAttr() +{ + if ( mbEmptyListStyleSetDueToSetOutlineLevelAttr ) + { + ResetAttr( RES_PARATR_NUMRULE ); + mbEmptyListStyleSetDueToSetOutlineLevelAttr = false; + } +} + +void SwTextNode::SetAttrListLevel( int nLevel ) +{ + if ( nLevel < 0 || nLevel >= MAXLEVEL ) + { + assert(false); // invalid level + return; + } + + SfxInt16Item aNewListLevelItem( RES_PARATR_LIST_LEVEL, + static_cast(nLevel) ); + SetAttr( aNewListLevelItem ); +} + +bool SwTextNode::HasAttrListLevel() const +{ + return GetpSwAttrSet() && + GetpSwAttrSet()->GetItemState( RES_PARATR_LIST_LEVEL, false ) == SfxItemState::SET; +} + +int SwTextNode::GetAttrListLevel() const +{ + int nAttrListLevel = 0; + + const SfxInt16Item& aListLevelItem = + dynamic_cast(GetAttr( RES_PARATR_LIST_LEVEL )); + nAttrListLevel = static_cast(aListLevelItem.GetValue()); + + return nAttrListLevel; +} + +int SwTextNode::GetActualListLevel() const +{ + assert(!GetNum() || !mpNodeNumRLHidden || // must be in sync + GetNum()->GetLevelInListTree() == mpNodeNumRLHidden->GetLevelInListTree()); + return GetNum() ? GetNum()->GetLevelInListTree() : -1; +} + +void SwTextNode::SetListRestart( bool bRestart ) +{ + if ( !bRestart ) + { + // attribute not contained in paragraph style's attribute set. Thus, + // it can be reset to the attribute pool default by resetting the attribute. + ResetAttr( RES_PARATR_LIST_ISRESTART ); + } + else + { + SfxBoolItem aNewIsRestartItem( RES_PARATR_LIST_ISRESTART, + true ); + SetAttr( aNewIsRestartItem ); + } +} + +bool SwTextNode::IsListRestart() const +{ + const SfxBoolItem& aIsRestartItem = + dynamic_cast(GetAttr( RES_PARATR_LIST_ISRESTART )); + + return aIsRestartItem.GetValue(); +} + +/** Returns if the paragraph has a visible numbering or bullet. + This includes all kinds of numbering/bullet/outlines. + The concrete list label string has to be checked, too. + */ +bool SwTextNode::HasVisibleNumberingOrBullet() const +{ + const SwNumRule* pRule = GetNum() ? GetNum()->GetNumRule() : nullptr; + if ( pRule && IsCountedInList()) + { + // #i87154# + // Correction of #newlistlevelattrs#: + // The numbering type has to be checked for bullet lists. + const SwNumFormat& rFormat = pRule->Get( lcl_BoundListLevel(GetActualListLevel()) ); + return SVX_NUM_NUMBER_NONE != rFormat.GetNumberingType() || + !pRule->MakeNumString( *(GetNum()) ).isEmpty(); + } + + return false; +} + +void SwTextNode::SetAttrListRestartValue( SwNumberTree::tSwNumTreeNumber nNumber ) +{ + const bool bChanged( HasAttrListRestartValue() + ? GetAttrListRestartValue() != nNumber + : nNumber != USHRT_MAX ); + + if ( bChanged || !HasAttrListRestartValue() ) + { + if ( nNumber == USHRT_MAX ) + { + ResetAttr( RES_PARATR_LIST_RESTARTVALUE ); + } + else + { + SfxInt16Item aNewListRestartValueItem( RES_PARATR_LIST_RESTARTVALUE, + static_cast(nNumber) ); + SetAttr( aNewListRestartValueItem ); + } + } +} + +bool SwTextNode::HasAttrListRestartValue() const +{ + return GetpSwAttrSet() && + GetpSwAttrSet()->GetItemState( RES_PARATR_LIST_RESTARTVALUE, false ) == SfxItemState::SET; +} +SwNumberTree::tSwNumTreeNumber SwTextNode::GetAttrListRestartValue() const +{ + OSL_ENSURE( HasAttrListRestartValue(), + " - only ask for list restart value, if attribute is set at text node." ); + + const SfxInt16Item& aListRestartValueItem = + dynamic_cast(GetAttr( RES_PARATR_LIST_RESTARTVALUE )); + return static_cast(aListRestartValueItem.GetValue()); +} + +SwNumberTree::tSwNumTreeNumber SwTextNode::GetActualListStartValue() const +{ + SwNumberTree::tSwNumTreeNumber nListRestartValue = 1; + + if ( IsListRestart() && HasAttrListRestartValue() ) + { + nListRestartValue = GetAttrListRestartValue(); + } + else + { + SwNumRule* pRule = GetNumRule(); + if ( pRule ) + { + const SwNumFormat* pFormat = + pRule->GetNumFormat( static_cast(GetAttrListLevel()) ); + if ( pFormat ) + { + nListRestartValue = pFormat->GetStart(); + } + } + } + + return nListRestartValue; +} + +bool SwTextNode::IsNotifiable() const +{ + return m_bNotifiable && IsNotificationEnabled(); +} + +bool SwTextNode::IsNotificationEnabled() const +{ + bool bResult = false; + const SwDoc * pDoc = GetDoc(); + if( pDoc ) + { + bResult = !(pDoc->IsInReading() || pDoc->IsInDtor()); + } + return bResult; +} + +void SwTextNode::SetCountedInList( bool bCounted ) +{ + if ( bCounted ) + { + // attribute not contained in paragraph style's attribute set. Thus, + // it can be reset to the attribute pool default by resetting the attribute. + ResetAttr( RES_PARATR_LIST_ISCOUNTED ); + } + else + { + SfxBoolItem aIsCountedInListItem( RES_PARATR_LIST_ISCOUNTED, false ); + SetAttr( aIsCountedInListItem ); + } +} + +bool SwTextNode::IsCountedInList() const +{ + const SfxBoolItem& aIsCountedInListItem = + dynamic_cast(GetAttr( RES_PARATR_LIST_ISCOUNTED )); + + return aIsCountedInListItem.GetValue(); +} + +static SwList * FindList(SwTextNode *const pNode) +{ + const OUString sListId = pNode->GetListId(); + if (!sListId.isEmpty()) + { + auto & rIDLA(pNode->GetDoc()->getIDocumentListsAccess()); + SwList* pList = rIDLA.getListByName( sListId ); + if ( pList == nullptr ) + { + // Create corresponding list. + SwNumRule* pNumRule = pNode->GetNumRule(); + if ( pNumRule ) + { + pList = rIDLA.createList(sListId, pNode->GetNumRule()->GetName()); + } + } + OSL_ENSURE( pList != nullptr, + " - no list for given list id. Serious defect" ); + return pList; + } + return nullptr; +} + +void SwTextNode::AddToList() +{ + if ( IsInList() ) + { + OSL_FAIL( " - the text node is already added to a list. Serious defect" ); + return; + } + + SwList *const pList(FindList(this)); + if (pList && GetNodes().IsDocNodes()) // not for undo nodes + { + assert(!mpNodeNum); + mpNodeNum.reset(new SwNodeNum(this, false)); + pList->InsertListItem(*mpNodeNum, false, GetAttrListLevel()); + // iterate all frames & if there's one with hidden layout... + SwIterator iter(*this); + for (SwTextFrame* pFrame = iter.First(); pFrame; pFrame = iter.Next()) + { + if (pFrame->getRootFrame()->IsHideRedlines()) + { + if (pFrame->GetTextNodeForParaProps() == this) + { + AddToListRLHidden(); + } + break; // assume it's consistent, need to check only once + } + } + } +} + +void SwTextNode::AddToListRLHidden() +{ + if (mpNodeNumRLHidden) + { + assert(false); + OSL_FAIL( " - the text node is already added to a list. Serious defect" ); + return; + } + + SwList *const pList(FindList(this)); + if (pList) + { + assert(!mpNodeNumRLHidden); + mpNodeNumRLHidden.reset(new SwNodeNum(this, true)); + pList->InsertListItem(*mpNodeNumRLHidden, true, GetAttrListLevel()); + } +} + +void SwTextNode::RemoveFromList() +{ + // sw_redlinehide: ensure it's removed from the other half too! + RemoveFromListRLHidden(); + if ( IsInList() ) + { + SwList::RemoveListItem( *mpNodeNum ); + mpNodeNum.reset(); + + SetWordCountDirty( true ); + } +} + +void SwTextNode::RemoveFromListRLHidden() +{ + if (mpNodeNumRLHidden) // direct access because RemoveFromList doesn't have layout + { + assert(mpNodeNumRLHidden->GetParent() || !GetNodes().IsDocNodes()); + SwList::RemoveListItem(*mpNodeNumRLHidden); + mpNodeNumRLHidden.reset(); + + SetWordCountDirty( true ); + } +} + +bool SwTextNode::IsInList() const +{ + return GetNum() != nullptr && GetNum()->GetParent() != nullptr; +} + +bool SwTextNode::IsFirstOfNumRule(SwRootFrame const& rLayout) const +{ + bool bResult = false; + + SwNodeNum const*const pNum(GetNum(&rLayout)); + if (pNum && pNum->GetNumRule()) + bResult = pNum->IsFirst(); + + return bResult; +} + +void SwTextNode::SetListId(OUString const& rListId) +{ + const SfxStringItem& rListIdItem = + dynamic_cast(GetAttr( RES_PARATR_LIST_ID )); + if (rListIdItem.GetValue() != rListId) + { + if (rListId.isEmpty()) + { + ResetAttr( RES_PARATR_LIST_ID ); + } + else + { + SfxStringItem aNewListIdItem(RES_PARATR_LIST_ID, rListId); + SetAttr( aNewListIdItem ); + } + } +} + +OUString SwTextNode::GetListId() const +{ + const SfxStringItem& rListIdItem = + dynamic_cast(GetAttr( RES_PARATR_LIST_ID )); + const OUString& sListId {rListIdItem.GetValue()}; + + // As long as no explicit list id attribute is set, use the list id of + // the list, which has been created for the applied list style. + if (sListId.isEmpty()) + { + SwNumRule* pRule = GetNumRule(); + if ( pRule ) + { + return pRule->GetDefaultListId(); + } + } + + return sListId; +} + +/** Determines, if the list level indent attributes can be applied to the + paragraph. + + The list level indents can be applied to the paragraph under the one + of following conditions: + - the list style is directly applied to the paragraph and the paragraph + has no own indent attributes. + - the list style is applied to the paragraph through one of its paragraph + styles, the paragraph has no own indent attributes and on the paragraph + style hierarchy from the paragraph to the paragraph style with the + list style no indent attributes are found. + + @return boolean +*/ +bool SwTextNode::AreListLevelIndentsApplicable() const +{ + bool bAreListLevelIndentsApplicable( true ); + + if ( !GetNum() || !GetNum()->GetNumRule() ) + { + // no list style applied to paragraph + bAreListLevelIndentsApplicable = false; + } + else if ( HasSwAttrSet() && + GetpSwAttrSet()->GetItemState( RES_LR_SPACE, false ) == SfxItemState::SET ) + { + // paragraph has hard-set indent attributes + bAreListLevelIndentsApplicable = false; + } + else if ( HasSwAttrSet() && + GetpSwAttrSet()->GetItemState( RES_PARATR_NUMRULE, false ) == SfxItemState::SET ) + { + // list style is directly applied to paragraph and paragraph has no + // hard-set indent attributes + bAreListLevelIndentsApplicable = true; + } + else + { + // list style is applied through one of the paragraph styles and + // paragraph has no hard-set indent attributes + + // check, paragraph's + const SwTextFormatColl* pColl = GetTextColl(); + while ( pColl ) + { + if ( pColl->GetAttrSet().GetItemState( RES_LR_SPACE, false ) == SfxItemState::SET ) + { + // indent attributes found in the paragraph style hierarchy. + bAreListLevelIndentsApplicable = false; + break; + } + + if ( pColl->GetAttrSet().GetItemState( RES_PARATR_NUMRULE, false ) == SfxItemState::SET ) + { + // paragraph style with the list style found and until now no + // indent attributes are found in the paragraph style hierarchy. + bAreListLevelIndentsApplicable = true; + break; + } + + pColl = dynamic_cast(pColl->DerivedFrom()); + OSL_ENSURE( pColl, + " - something wrong in paragraph's style hierarchy. The applied list style is not found." ); + } + } + + return bAreListLevelIndentsApplicable; +} + +/** Retrieves the list tab stop position, if the paragraph's list level defines + one and this list tab stop has to merged into the tap stops of the paragraph + + @param nListTabStopPosition + output parameter - containing the list tab stop position + + @return boolean - indicating, if a list tab stop position is provided +*/ +bool SwTextNode::GetListTabStopPosition( long& nListTabStopPosition ) const +{ + bool bListTabStopPositionProvided(false); + + const SwNumRule* pNumRule = GetNum() ? GetNum()->GetNumRule() : nullptr; + if ( pNumRule && HasVisibleNumberingOrBullet() && GetActualListLevel() >= 0 ) + { + const SwNumFormat& rFormat = pNumRule->Get( static_cast(GetActualListLevel()) ); + if ( rFormat.GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_ALIGNMENT && + rFormat.GetLabelFollowedBy() == SvxNumberFormat::LISTTAB ) + { + bListTabStopPositionProvided = true; + nListTabStopPosition = rFormat.GetListtabPos(); + + if ( getIDocumentSettingAccess()->get(DocumentSettingId::TABS_RELATIVE_TO_INDENT) ) + { + // tab stop position are treated to be relative to the "before text" + // indent value of the paragraph. Thus, adjust . + if ( AreListLevelIndentsApplicable() ) + { + nListTabStopPosition -= rFormat.GetIndentAt(); + } + else if (!getIDocumentSettingAccess()->get(DocumentSettingId::IGNORE_FIRST_LINE_INDENT_IN_NUMBERING)) + { + SvxLRSpaceItem aItem = GetSwAttrSet().GetLRSpace(); + nListTabStopPosition -= aItem.GetTextLeft(); + } + } + } + } + + return bListTabStopPositionProvided; +} + +OUString SwTextNode::GetLabelFollowedBy() const +{ + const SwNumRule* pNumRule = GetNum() ? GetNum()->GetNumRule() : nullptr; + if ( pNumRule && HasVisibleNumberingOrBullet() && GetActualListLevel() >= 0 ) + { + const SwNumFormat& rFormat = pNumRule->Get( static_cast(GetActualListLevel()) ); + if ( rFormat.GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_ALIGNMENT ) + { + switch ( rFormat.GetLabelFollowedBy() ) + { + case SvxNumberFormat::LISTTAB: + { + return "\t"; + } + break; + case SvxNumberFormat::SPACE: + { + return " "; + } + break; + case SvxNumberFormat::NEWLINE: + { + return "\n"; + } + break; + case SvxNumberFormat::NOTHING: + { + // intentionally left blank. + } + break; + default: + { + OSL_FAIL( " - unknown SvxNumberFormat::GetLabelFollowedBy() return value" ); + } + } + } + } + + return OUString(); +} + +void SwTextNode::CalcHiddenCharFlags() const +{ + sal_Int32 nStartPos; + sal_Int32 nEndPos; + // Update of the flags is done inside GetBoundsOfHiddenRange() + SwScriptInfo::GetBoundsOfHiddenRange( *this, 0, nStartPos, nEndPos ); +} + +// #i12836# enhanced pdf export +bool SwTextNode::IsHidden() const +{ + if ( IsHiddenByParaField() || HasHiddenCharAttribute( true ) ) + return true; + + const SwSectionNode* pSectNd = FindSectionNode(); + return pSectNd && pSectNd->GetSection().IsHiddenFlag(); +} + +namespace { + // Helper class for special handling of setting attributes at text node: + // In constructor an instance of the helper class recognize whose attributes + // are set and perform corresponding actions before the intrinsic set of + // attributes has been taken place. + // In the destructor - after the attributes have been set at the text + // node - corresponding actions are performed. + // The following is handled: + // (1) When the list style attribute - RES_PARATR_NUMRULE - is set, + // (A) list style attribute is empty -> the text node is removed from + // its list. + // (B) list style attribute is not empty + // (a) text node has no list style -> add text node to its list after + // the attributes have been set. + // (b) text node has list style -> change of list style is notified + // after the attributes have been set. + // (2) When the list id attribute - RES_PARATR_LIST_ID - is set and changed, + // the text node is removed from its current list before the attributes + // are set and added to its new list after the attributes have been set. + // (3) Notify list tree, if list level - RES_PARATR_LIST_LEVEL - is set + // and changed after the attributes have been set + // (4) Notify list tree, if list restart - RES_PARATR_LIST_ISRESTART - is set + // and changed after the attributes have been set + // (5) Notify list tree, if list restart value - RES_PARATR_LIST_RESTARTVALUE - + // is set and changed after the attributes have been set + // (6) Notify list tree, if count in list - RES_PARATR_LIST_ISCOUNTED - is set + // and changed after the attributes have been set + // (7) Set or Reset empty list style due to changed outline level - RES_PARATR_OUTLINELEVEL. + class HandleSetAttrAtTextNode + { + public: + HandleSetAttrAtTextNode( SwTextNode& rTextNode, + const SfxPoolItem& pItem ); + HandleSetAttrAtTextNode( SwTextNode& rTextNode, + const SfxItemSet& rItemSet ); + ~HandleSetAttrAtTextNode() COVERITY_NOEXCEPT_FALSE; + + private: + SwTextNode& mrTextNode; + bool mbAddTextNodeToList; + bool mbUpdateListLevel; + bool mbUpdateListRestart; + bool mbUpdateListCount; + // #i70748# + bool mbOutlineLevelSet; + }; + + HandleSetAttrAtTextNode::HandleSetAttrAtTextNode( SwTextNode& rTextNode, + const SfxPoolItem& pItem ) + : mrTextNode( rTextNode ), + mbAddTextNodeToList( false ), + mbUpdateListLevel( false ), + mbUpdateListRestart( false ), + mbUpdateListCount( false ), + // #i70748# + mbOutlineLevelSet( false ) + { + switch ( pItem.Which() ) + { + // handle RES_PARATR_NUMRULE + case RES_PARATR_NUMRULE: + { + mrTextNode.RemoveFromList(); + + const SwNumRuleItem& rNumRuleItem = + dynamic_cast(pItem); + if ( !rNumRuleItem.GetValue().isEmpty() ) + { + mbAddTextNodeToList = true; + // #i105562# + + mrTextNode.ResetEmptyListStyleDueToResetOutlineLevelAttr(); + } + } + break; + + // handle RES_PARATR_LIST_ID + case RES_PARATR_LIST_ID: + { + const SfxStringItem& rListIdItem = + dynamic_cast(pItem); + OSL_ENSURE( rListIdItem.GetValue().getLength() > 0, + " - empty list id attribute not expected. Serious defect." ); + const OUString sListIdOfTextNode = rTextNode.GetListId(); + if ( rListIdItem.GetValue() != sListIdOfTextNode ) + { + mbAddTextNodeToList = true; + if ( mrTextNode.IsInList() ) + { + mrTextNode.RemoveFromList(); + } + } + } + break; + + // handle RES_PARATR_LIST_LEVEL + case RES_PARATR_LIST_LEVEL: + { + const SfxInt16Item& aListLevelItem = + dynamic_cast(pItem); + if ( aListLevelItem.GetValue() != mrTextNode.GetAttrListLevel() ) + { + mbUpdateListLevel = true; + } + } + break; + + // handle RES_PARATR_LIST_ISRESTART + case RES_PARATR_LIST_ISRESTART: + { + const SfxBoolItem& aListIsRestartItem = + dynamic_cast(pItem); + if ( aListIsRestartItem.GetValue() != + mrTextNode.IsListRestart() ) + { + mbUpdateListRestart = true; + } + } + break; + + // handle RES_PARATR_LIST_RESTARTVALUE + case RES_PARATR_LIST_RESTARTVALUE: + { + const SfxInt16Item& aListRestartValueItem = + dynamic_cast(pItem); + if ( !mrTextNode.HasAttrListRestartValue() || + aListRestartValueItem.GetValue() != mrTextNode.GetAttrListRestartValue() ) + { + mbUpdateListRestart = true; + } + } + break; + + // handle RES_PARATR_LIST_ISCOUNTED + case RES_PARATR_LIST_ISCOUNTED: + { + const SfxBoolItem& aIsCountedInListItem = + dynamic_cast(pItem); + if ( aIsCountedInListItem.GetValue() != + mrTextNode.IsCountedInList() ) + { + mbUpdateListCount = true; + } + } + break; + + // #i70748# + // handle RES_PARATR_OUTLINELEVEL + case RES_PARATR_OUTLINELEVEL: + { + const SfxUInt16Item& aOutlineLevelItem = + dynamic_cast(pItem); + if ( aOutlineLevelItem.GetValue() != mrTextNode.GetAttrOutlineLevel() ) + { + mbOutlineLevelSet = true; + } + } + break; + } + + } + + HandleSetAttrAtTextNode::HandleSetAttrAtTextNode( SwTextNode& rTextNode, + const SfxItemSet& rItemSet ) + : mrTextNode( rTextNode ), + mbAddTextNodeToList( false ), + mbUpdateListLevel( false ), + mbUpdateListRestart( false ), + mbUpdateListCount( false ), + // #i70748# + mbOutlineLevelSet( false ) + { + const SfxPoolItem* pItem = nullptr; + // handle RES_PARATR_NUMRULE + if ( rItemSet.GetItemState( RES_PARATR_NUMRULE, false, &pItem ) == SfxItemState::SET ) + { + mrTextNode.RemoveFromList(); + + const SwNumRuleItem* pNumRuleItem = + dynamic_cast(pItem); + assert(pNumRuleItem); + if ( !pNumRuleItem->GetValue().isEmpty() ) + { + mbAddTextNodeToList = true; + // #i70748# + mrTextNode.ResetEmptyListStyleDueToResetOutlineLevelAttr(); + } + } + + // handle RES_PARATR_LIST_ID + if ( rItemSet.GetItemState( RES_PARATR_LIST_ID, false, &pItem ) == SfxItemState::SET ) + { + const SfxStringItem* pListIdItem = + dynamic_cast(pItem); + const OUString sListIdOfTextNode = mrTextNode.GetListId(); + if ( pListIdItem && + pListIdItem->GetValue() != sListIdOfTextNode ) + { + mbAddTextNodeToList = true; + if ( mrTextNode.IsInList() ) + { + mrTextNode.RemoveFromList(); + } + } + } + + // handle RES_PARATR_LIST_LEVEL + if ( rItemSet.GetItemState( RES_PARATR_LIST_LEVEL, false, &pItem ) == SfxItemState::SET ) + { + const SfxInt16Item* pListLevelItem = + dynamic_cast(pItem); + if (pListLevelItem && pListLevelItem->GetValue() != mrTextNode.GetAttrListLevel()) + { + mbUpdateListLevel = true; + } + } + + // handle RES_PARATR_LIST_ISRESTART + if ( rItemSet.GetItemState( RES_PARATR_LIST_ISRESTART, false, &pItem ) == SfxItemState::SET ) + { + const SfxBoolItem* pListIsRestartItem = + dynamic_cast(pItem); + if (pListIsRestartItem && pListIsRestartItem->GetValue() != mrTextNode.IsListRestart()) + { + mbUpdateListRestart = true; + } + } + + // handle RES_PARATR_LIST_RESTARTVALUE + if ( rItemSet.GetItemState( RES_PARATR_LIST_RESTARTVALUE, false, &pItem ) == SfxItemState::SET ) + { + const SfxInt16Item* pListRestartValueItem = + dynamic_cast(pItem); + if ( !mrTextNode.HasAttrListRestartValue() || (pListRestartValueItem && + pListRestartValueItem->GetValue() != mrTextNode.GetAttrListRestartValue()) ) + { + mbUpdateListRestart = true; + } + } + + // handle RES_PARATR_LIST_ISCOUNTED + if ( rItemSet.GetItemState( RES_PARATR_LIST_ISCOUNTED, false, &pItem ) == SfxItemState::SET ) + { + const SfxBoolItem* pIsCountedInListItem = + dynamic_cast(pItem); + if (pIsCountedInListItem && pIsCountedInListItem->GetValue() != + mrTextNode.IsCountedInList()) + { + mbUpdateListCount = true; + } + } + + // #i70748# + // handle RES_PARATR_OUTLINELEVEL + if ( rItemSet.GetItemState( RES_PARATR_OUTLINELEVEL, false, &pItem ) == SfxItemState::SET ) + { + const SfxUInt16Item* pOutlineLevelItem = + dynamic_cast(pItem); + if (pOutlineLevelItem && pOutlineLevelItem->GetValue() != + mrTextNode.GetAttrOutlineLevel()) + { + mbOutlineLevelSet = true; + } + } + } + + HandleSetAttrAtTextNode::~HandleSetAttrAtTextNode() COVERITY_NOEXCEPT_FALSE + { + if ( mbAddTextNodeToList ) + { + SwNumRule* pNumRuleAtTextNode = mrTextNode.GetNumRule(); + if ( pNumRuleAtTextNode ) + { + mrTextNode.AddToList(); + } + } + else + { + if ( mbUpdateListLevel && mrTextNode.IsInList() ) + { + auto const nLevel(mrTextNode.GetAttrListLevel()); + mrTextNode.DoNum( + [nLevel](SwNodeNum & rNum) { rNum.SetLevelInListTree(nLevel); }); + } + + if ( mbUpdateListRestart && mrTextNode.IsInList() ) + { + mrTextNode.DoNum( + [](SwNodeNum & rNum) { + rNum.InvalidateMe(); + rNum.NotifyInvalidSiblings(); + }); + } + + if ( mbUpdateListCount && mrTextNode.IsInList() ) + { + mrTextNode.DoNum( + [](SwNodeNum & rNum) { rNum.InvalidateAndNotifyTree(); }); + } + } + + // #i70748# + if (mbOutlineLevelSet) + { + mrTextNode.GetNodes().UpdateOutlineNode(mrTextNode); + if (mrTextNode.GetAttrOutlineLevel() == 0) + { + mrTextNode.ResetEmptyListStyleDueToResetOutlineLevelAttr(); + } + else + { + const SfxPoolItem* pItem = nullptr; + if ( mrTextNode.GetSwAttrSet().GetItemState( RES_PARATR_NUMRULE, + true, &pItem ) + != SfxItemState::SET ) + { + mrTextNode.SetEmptyListStyleDueToSetOutlineLevelAttr(); + } + } + } + } + // End of class +} + +bool SwTextNode::SetAttr( const SfxPoolItem& pItem ) +{ + const bool bOldIsSetOrResetAttr( mbInSetOrResetAttr ); + mbInSetOrResetAttr = true; + + HandleSetAttrAtTextNode aHandleSetAttr( *this, pItem ); + + bool bRet = SwContentNode::SetAttr( pItem ); + + mbInSetOrResetAttr = bOldIsSetOrResetAttr; + + return bRet; +} + +bool SwTextNode::SetAttr( const SfxItemSet& rSet ) +{ + const bool bOldIsSetOrResetAttr( mbInSetOrResetAttr ); + mbInSetOrResetAttr = true; + + HandleSetAttrAtTextNode aHandleSetAttr( *this, rSet ); + + bool bRet = SwContentNode::SetAttr( rSet ); + + mbInSetOrResetAttr = bOldIsSetOrResetAttr; + + return bRet; +} + +namespace { + // Helper class for special handling of resetting attributes at text node: + // In constructor an instance of the helper class recognize whose attributes + // are reset and perform corresponding actions before the intrinsic reset of + // attributes has been taken place. + // In the destructor - after the attributes have been reset at the text + // node - corresponding actions are performed. + // The following is handled: + // (1) When the list style attribute - RES_PARATR_NUMRULE - is reset, + // the text is removed from its list before the attributes have been reset. + // (2) When the list id attribute - RES_PARATR_LIST_ID - is reset, + // the text is removed from its list before the attributes have been reset. + // (3) Notify list tree, if list level - RES_PARATR_LIST_LEVEL - is reset. + // (4) Notify list tree, if list restart - RES_PARATR_LIST_ISRESTART - is reset. + // (5) Notify list tree, if list restart value - RES_PARATR_LIST_RESTARTVALUE - is reset. + // (6) Notify list tree, if count in list - RES_PARATR_LIST_ISCOUNTED - is reset. + // (7) Reset empty list style, if outline level attribute - RES_PARATR_OUTLINELEVEL - is reset. + class HandleResetAttrAtTextNode + { + public: + HandleResetAttrAtTextNode( SwTextNode& rTextNode, + const sal_uInt16 nWhich1, + sal_uInt16 nWhich2 ); + HandleResetAttrAtTextNode( SwTextNode& rTextNode, + const std::vector& rWhichArr ); + explicit HandleResetAttrAtTextNode( SwTextNode& rTextNode ); + + ~HandleResetAttrAtTextNode() COVERITY_NOEXCEPT_FALSE; + + private: + SwTextNode& mrTextNode; + bool mbListStyleOrIdReset; + bool mbUpdateListLevel; + bool mbUpdateListRestart; + bool mbUpdateListCount; + + void init( const std::vector& rWhichArr ); + }; + + HandleResetAttrAtTextNode::HandleResetAttrAtTextNode( SwTextNode& rTextNode, + const sal_uInt16 nWhich1, + sal_uInt16 nWhich2 ) + : mrTextNode( rTextNode ), + mbListStyleOrIdReset( false ), + mbUpdateListLevel( false ), + mbUpdateListRestart( false ), + mbUpdateListCount( false ) + { + if ( nWhich2 < nWhich1 ) + nWhich2 = nWhich1; + std::vector rWhichArr; + for ( sal_uInt16 nWhich = nWhich1; nWhich <= nWhich2; ++nWhich ) + rWhichArr.push_back( nWhich ); + + init( rWhichArr ); + } + + HandleResetAttrAtTextNode::HandleResetAttrAtTextNode( SwTextNode& rTextNode, + const std::vector& rWhichArr ) + : mrTextNode( rTextNode ), + mbListStyleOrIdReset( false ), + mbUpdateListLevel( false ), + mbUpdateListRestart( false ), + mbUpdateListCount( false ) + { + init( rWhichArr ); + } + + HandleResetAttrAtTextNode::HandleResetAttrAtTextNode( SwTextNode& rTextNode ) + : mrTextNode( rTextNode ), + mbListStyleOrIdReset( true ), + mbUpdateListLevel( false ), + mbUpdateListRestart( false ), + mbUpdateListCount( false ) + { + if ( rTextNode.IsInList() ) + { + rTextNode.RemoveFromList(); + } + // #i70748# + mrTextNode.ResetEmptyListStyleDueToResetOutlineLevelAttr(); + } + + void HandleResetAttrAtTextNode::init( const std::vector& rWhichArr ) + { + bool bRemoveFromList( false ); + { + for (const auto& rWhich : rWhichArr) + { + if ( rWhich == RES_PARATR_NUMRULE ) + { + bRemoveFromList = bRemoveFromList || + mrTextNode.GetNumRule() != nullptr; + mbListStyleOrIdReset = true; + } + else if ( rWhich == RES_PARATR_LIST_ID ) + { + bRemoveFromList = bRemoveFromList || + ( mrTextNode.GetpSwAttrSet() && + mrTextNode.GetpSwAttrSet()->GetItemState( RES_PARATR_LIST_ID, false ) == SfxItemState::SET ); + mbListStyleOrIdReset = true; + } + else if ( rWhich == RES_PARATR_OUTLINELEVEL ) + mrTextNode.ResetEmptyListStyleDueToResetOutlineLevelAttr(); + else if ( rWhich == RES_BACKGROUND ) + mrTextNode.ResetAttr( XATTR_FILL_FIRST, XATTR_FILL_LAST ); + + if ( !bRemoveFromList ) + { + // RES_PARATR_LIST_LEVEL + mbUpdateListLevel = mbUpdateListLevel || + ( rWhich == RES_PARATR_LIST_LEVEL && + mrTextNode.HasAttrListLevel() ); + + // RES_PARATR_LIST_ISRESTART and RES_PARATR_LIST_RESTARTVALUE + mbUpdateListRestart = mbUpdateListRestart || + ( rWhich == RES_PARATR_LIST_ISRESTART && + mrTextNode.IsListRestart() ) || + ( rWhich == RES_PARATR_LIST_RESTARTVALUE && + mrTextNode.HasAttrListRestartValue() ); + + // RES_PARATR_LIST_ISCOUNTED + mbUpdateListCount = mbUpdateListCount || + ( rWhich == RES_PARATR_LIST_ISCOUNTED && + !mrTextNode.IsCountedInList() ); + } + } + } + + if ( bRemoveFromList && mrTextNode.IsInList() ) + { + mrTextNode.RemoveFromList(); + } + } + + HandleResetAttrAtTextNode::~HandleResetAttrAtTextNode() COVERITY_NOEXCEPT_FALSE + { + if ( mbListStyleOrIdReset && !mrTextNode.IsInList() ) + { + // check, if in spite of the reset of the list style or the list id + // the paragraph still has to be added to a list. + if (mrTextNode.GetNumRule() && !mrTextNode.GetListId().isEmpty()) + { + // #i96062# + // If paragraph has no list level attribute set and list style + // is the outline style, apply outline level as the list level. + if ( !mrTextNode.HasAttrListLevel() && + mrTextNode.GetNumRule()->GetName()==SwNumRule::GetOutlineRuleName() && + mrTextNode.GetTextColl()->IsAssignedToListLevelOfOutlineStyle() ) + { + int nNewListLevel = mrTextNode.GetTextColl()->GetAssignedOutlineStyleLevel(); + if ( 0 <= nNewListLevel && nNewListLevel < MAXLEVEL ) + { + mrTextNode.SetAttrListLevel( nNewListLevel ); + } + } + mrTextNode.AddToList(); + } + // #i70748# + // #i105562# + else + { + assert(!mrTextNode.GetpSwAttrSet() + || dynamic_cast( + &mrTextNode.GetAttr(RES_PARATR_OUTLINELEVEL, false))); + if (mrTextNode.GetpSwAttrSet() + && static_cast( + mrTextNode.GetAttr(RES_PARATR_OUTLINELEVEL, false)).GetValue() > 0) + { + mrTextNode.SetEmptyListStyleDueToSetOutlineLevelAttr(); + } + } + } + + if ( mrTextNode.IsInList() ) + { + if ( mbUpdateListLevel ) + { + auto const nLevel(mrTextNode.GetAttrListLevel()); + mrTextNode.DoNum( + [nLevel](SwNodeNum & rNum) { rNum.SetLevelInListTree(nLevel); }); + } + + if ( mbUpdateListRestart ) + { + mrTextNode.DoNum( + [](SwNodeNum & rNum) { + rNum.InvalidateMe(); + rNum.NotifyInvalidSiblings(); + }); + } + + if ( mbUpdateListCount ) + { + mrTextNode.DoNum( + [](SwNodeNum & rNum) { rNum.InvalidateAndNotifyTree(); }); + } + } + } + // End of class +} + +bool SwTextNode::ResetAttr( sal_uInt16 nWhich1, sal_uInt16 nWhich2 ) +{ + const bool bOldIsSetOrResetAttr( mbInSetOrResetAttr ); + mbInSetOrResetAttr = true; + + HandleResetAttrAtTextNode aHandleResetAttr( *this, nWhich1, nWhich2 ); + + bool bRet = SwContentNode::ResetAttr( nWhich1, nWhich2 ); + + mbInSetOrResetAttr = bOldIsSetOrResetAttr; + + return bRet; +} + +bool SwTextNode::ResetAttr( const std::vector& rWhichArr ) +{ + const bool bOldIsSetOrResetAttr( mbInSetOrResetAttr ); + mbInSetOrResetAttr = true; + + HandleResetAttrAtTextNode aHandleResetAttr( *this, rWhichArr ); + + bool bRet = SwContentNode::ResetAttr( rWhichArr ); + + mbInSetOrResetAttr = bOldIsSetOrResetAttr; + + return bRet; +} + +sal_uInt16 SwTextNode::ResetAllAttr() +{ + const bool bOldIsSetOrResetAttr( mbInSetOrResetAttr ); + mbInSetOrResetAttr = true; + + HandleResetAttrAtTextNode aHandleResetAttr( *this ); + + const sal_uInt16 nRet = SwContentNode::ResetAllAttr(); + + mbInSetOrResetAttr = bOldIsSetOrResetAttr; + + return nRet; +} + +void SwTextNode::dumpAsXml(xmlTextWriterPtr pWriter) const +{ + xmlTextWriterStartElement(pWriter, BAD_CAST("SwTextNode")); + xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this); + xmlTextWriterWriteAttribute(pWriter, BAD_CAST("index"), BAD_CAST(OString::number(GetIndex()).getStr())); + + OUString sText = GetText(); + for (int i = 0; i < 32; ++i) + sText = sText.replace(i, '*'); + xmlTextWriterStartElement(pWriter, BAD_CAST("m_Text")); + xmlTextWriterWriteString(pWriter, BAD_CAST(sText.toUtf8().getStr())); + xmlTextWriterEndElement(pWriter); + + if (GetFormatColl()) + { + xmlTextWriterStartElement(pWriter, BAD_CAST("SwTextFormatColl")); + xmlTextWriterWriteAttribute(pWriter, BAD_CAST("name"), BAD_CAST(GetFormatColl()->GetName().toUtf8().getStr())); + xmlTextWriterEndElement(pWriter); + } + + if (HasSwAttrSet()) + { + xmlTextWriterStartElement(pWriter, BAD_CAST("SwAttrSet")); + GetSwAttrSet().dumpAsXml(pWriter); + xmlTextWriterEndElement(pWriter); + } + + if (HasHints()) + { + xmlTextWriterStartElement(pWriter, BAD_CAST("SwpHints")); + const SwpHints& rHints = GetSwpHints(); + for (size_t i = 0; i < rHints.Count(); ++i) + rHints.Get(i)->dumpAsXml(pWriter); + xmlTextWriterEndElement(pWriter); + } + + if (GetNumRule()) + GetNumRule()->dumpAsXml(pWriter); + + xmlTextWriterEndElement(pWriter); +} + +sal_uInt32 SwTextNode::GetRsid( sal_Int32 nStt, sal_Int32 nEnd ) const +{ + SfxItemSet aSet( const_cast(static_cast(GetDoc()->GetAttrPool())), svl::Items{} ); + if (GetParaAttr(aSet, nStt, nEnd)) + { + const SvxRsidItem* pRsid = aSet.GetItem(RES_CHRATR_RSID); + if( pRsid ) + return pRsid->GetValue(); + } + + return 0; +} + +sal_uInt32 SwTextNode::GetParRsid() const +{ + return reinterpret_cast(GetAttr( RES_PARATR_RSID )).GetValue(); +} + +bool SwTextNode::CompareParRsid( const SwTextNode &rTextNode ) const +{ + sal_uInt32 nThisRsid = GetParRsid(); + sal_uInt32 nRsid = rTextNode.GetParRsid(); + + return nThisRsid == nRsid; +} + +bool SwTextNode::CompareRsid( const SwTextNode &rTextNode, sal_Int32 nStt1, sal_Int32 nStt2 ) const +{ + sal_uInt32 nThisRsid = GetRsid( nStt1, nStt1 ); + sal_uInt32 nRsid = rTextNode.GetRsid( nStt2, nStt2 ); + + return nThisRsid == nRsid; +} + +// sw::Metadatable +::sfx2::IXmlIdRegistry& SwTextNode::GetRegistry() +{ + return GetDoc()->GetXmlIdRegistry(); +} + +bool SwTextNode::IsInClipboard() const +{ + return GetDoc()->IsClipBoard(); +} + +bool SwTextNode::IsInUndo() const +{ + return GetDoc()->GetIDocumentUndoRedo().IsUndoNodes(GetNodes()); +} + +bool SwTextNode::IsInContent() const +{ + return !GetDoc()->IsInHeaderFooter( SwNodeIndex(*this) ); +} + +void SwTextNode::SwClientNotify( const SwModify& rModify, const SfxHint& rHint ) +{ + if (auto pLegacyHint = dynamic_cast(&rHint)) + { + bool bWasNotifiable = m_bNotifiable; + m_bNotifiable = false; + + const auto pOldValue = pLegacyHint->m_pOld; + const auto pNewValue = pLegacyHint->m_pNew; + // Override Modify so that deleting styles works properly (outline + // numbering!). + // Never call ChgTextCollUpdateNum for Nodes in Undo. + if( pOldValue + && pNewValue + && RES_FMT_CHG == pOldValue->Which() + && GetRegisteredIn() == static_cast(pNewValue)->pChangedFormat + && GetNodes().IsDocNodes() ) + { + ChgTextCollUpdateNum( + static_cast(static_cast(pOldValue)->pChangedFormat), + static_cast(static_cast(pNewValue)->pChangedFormat) ); + } + + // reset fill information + if (maFillAttributes && pNewValue) + { + const sal_uInt16 nWhich = pNewValue->Which(); + bool bReset(RES_FMT_CHG == nWhich); // ..on format change (e.g. style changed) + + if(!bReset && RES_ATTRSET_CHG == nWhich) // ..on ItemChange from DrawingLayer FillAttributes + { + SfxItemIter aIter(*static_cast(pNewValue)->GetChgSet()); + + for(const SfxPoolItem* pItem = aIter.GetCurItem(); pItem && !bReset; pItem = aIter.NextItem()) + { + bReset = !IsInvalidItem(pItem) && pItem->Which() >= XATTR_FILL_FIRST && pItem->Which() <= XATTR_FILL_LAST; + } + } + + if(bReset) + { + maFillAttributes.reset(); + } + } + + if ( !mbInSetOrResetAttr ) + { + HandleModifyAtTextNode( *this, pOldValue, pNewValue ); + } + + SwContentNode::SwClientNotify(rModify, rHint); + + SwDoc* pDoc = GetDoc(); + // #125329# - assure that text node is in document nodes array + if ( pDoc && !pDoc->IsInDtor() && &pDoc->GetNodes() == &GetNodes() ) + { + pDoc->GetNodes().UpdateOutlineNode(*this); + } + + m_bNotifiable = bWasNotifiable; + + if (pOldValue && (RES_REMOVE_UNO_OBJECT == pOldValue->Which())) + { // invalidate cached uno object + SetXParagraph(css::uno::Reference(nullptr)); + } + } + else if (dynamic_cast(&rHint)) + { + if (&rModify == GetRegisteredIn()) + ChkCondColl(); + } +} + +uno::Reference< rdf::XMetadatable > +SwTextNode::MakeUnoObject() +{ + const uno::Reference xMeta( + SwXParagraph::CreateXParagraph(*GetDoc(), this), uno::UNO_QUERY); + return xMeta; +} + +drawinglayer::attribute::SdrAllFillAttributesHelperPtr SwTextNode::getSdrAllFillAttributesHelper() const +{ + // create SdrAllFillAttributesHelper on demand + if(!maFillAttributes) + { + const_cast< SwTextNode* >(this)->maFillAttributes = std::make_shared(GetSwAttrSet()); + } + + return maFillAttributes; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/swfntcch.cxx b/sw/source/core/txtnode/swfntcch.cxx new file mode 100644 index 000000000..49783fd25 --- /dev/null +++ b/sw/source/core/txtnode/swfntcch.cxx @@ -0,0 +1,75 @@ +/* -*- 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 + +#include +#include +#include +#include +#include +#include + +// from atrstck.cxx +extern const sal_uInt8 StackPos[]; + +// FontCache is created in txtinit.cxx TextInit_ and deleted in TextFinit +SwFontCache *pSwFontCache = nullptr; + +SwFontObj::SwFontObj( const void *pOwn, SwViewShell *pSh ) : + SwCacheObj( pOwn ), + m_aSwFont( &static_cast(pOwn)->GetAttrSet(), pSh ? &pSh->getIDocumentSettingAccess() : nullptr ) +{ + m_aSwFont.AllocFontCacheId( pSh, m_aSwFont.GetActual() ); + const SwAttrSet& rAttrSet = static_cast(pOwn)->GetAttrSet(); + for (sal_uInt16 i = RES_CHRATR_BEGIN; i < RES_CHRATR_END; i++) + m_pDefaultArray[ StackPos[ i ] ] = &rAttrSet.Get( i ); +} + +SwFontObj::~SwFontObj() +{ +} + +SwFontAccess::SwFontAccess( const void *pOwn, SwViewShell *pSh ) : + SwCacheAccess( *pSwFontCache, pOwn, + static_cast(pOwn)->IsInSwFntCache() ), + m_pShell( pSh ) +{ +} + +SwFontObj *SwFontAccess::Get( ) +{ + return static_cast( SwCacheAccess::Get( ) ); +} + +SwCacheObj *SwFontAccess::NewObj( ) +{ + const_cast(static_cast(m_pOwner))->SetInSwFntCache( true ); + return new SwFontObj( m_pOwner, m_pShell ); +} + +SAL_DLLPUBLIC_EXPORT void FlushFontCache() +{ + if (pSwFontCache) + pSwFontCache->Flush(); + if (pFntCache) + pFntCache->Flush(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/swfont.cxx b/sw/source/core/txtnode/swfont.cxx new file mode 100644 index 000000000..60d22422c --- /dev/null +++ b/sw/source/core/txtnode/swfont.cxx @@ -0,0 +1,1541 @@ +/* -*- 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 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef DBG_UTIL +// global Variable +SvStatistics g_SvStat; +#endif + +using namespace ::com::sun::star; + +// set background brush, depending on character formatting +void SwFont::SetBackColor( Color* pNewColor ) +{ + m_pBackColor.reset( pNewColor ); + m_bFontChg = true; + m_aSub[SwFontScript::Latin].m_nFontCacheId = m_aSub[SwFontScript::CJK].m_nFontCacheId = m_aSub[SwFontScript::CTL].m_nFontCacheId = nullptr; +} + +void SwFont::SetTopBorder( const editeng::SvxBorderLine* pTopBorder ) +{ + if( pTopBorder ) + m_aTopBorder = *pTopBorder; + else + { + m_aTopBorder.reset(); + m_nTopBorderDist = 0; + } + m_bFontChg = true; + m_aSub[SwFontScript::Latin].m_nFontCacheId = m_aSub[SwFontScript::CJK].m_nFontCacheId = m_aSub[SwFontScript::CTL].m_nFontCacheId = nullptr; +} + +void SwFont::SetBottomBorder( const editeng::SvxBorderLine* pBottomBorder ) +{ + if( pBottomBorder ) + m_aBottomBorder = *pBottomBorder; + else + { + m_aBottomBorder.reset(); + m_nBottomBorderDist = 0; + } + m_bFontChg = true; + m_aSub[SwFontScript::Latin].m_nFontCacheId = m_aSub[SwFontScript::CJK].m_nFontCacheId = m_aSub[SwFontScript::CTL].m_nFontCacheId = nullptr; +} + +void SwFont::SetRightBorder( const editeng::SvxBorderLine* pRightBorder ) +{ + if( pRightBorder ) + m_aRightBorder = *pRightBorder; + else + { + m_aRightBorder.reset(); + m_nRightBorderDist = 0; + } + m_bFontChg = true; + m_aSub[SwFontScript::Latin].m_nFontCacheId = m_aSub[SwFontScript::CJK].m_nFontCacheId = m_aSub[SwFontScript::CTL].m_nFontCacheId = nullptr; +} + +void SwFont::SetLeftBorder( const editeng::SvxBorderLine* pLeftBorder ) +{ + if( pLeftBorder ) + m_aLeftBorder = *pLeftBorder; + else + { + m_aLeftBorder.reset(); + m_nLeftBorderDist = 0; + } + m_bFontChg = true; + m_aSub[SwFontScript::Latin].m_nFontCacheId = m_aSub[SwFontScript::CJK].m_nFontCacheId = m_aSub[SwFontScript::CTL].m_nFontCacheId = nullptr; +} + +const std::optional& +SwFont::GetAbsTopBorder(const bool bVertLayout, const bool bVertLayoutLRBT) const +{ + switch (GetOrientation(bVertLayout, bVertLayoutLRBT)) + { + case 0 : + return m_aTopBorder; + break; + case 900 : + return m_aRightBorder; + break; + case 1800 : + return m_aBottomBorder; + break; + case 2700 : + return m_aLeftBorder; + break; + default : + assert(false); + return m_aTopBorder; + break; + } +} + +const std::optional& +SwFont::GetAbsBottomBorder(const bool bVertLayout, const bool bVertLayoutLRBT) const +{ + switch (GetOrientation(bVertLayout, bVertLayoutLRBT)) + { + case 0 : + return m_aBottomBorder; + break; + case 900 : + return m_aLeftBorder; + break; + case 1800 : + return m_aTopBorder; + break; + case 2700 : + return m_aRightBorder; + break; + default : + assert(false); + return m_aBottomBorder; + break; + } +} + +const std::optional& +SwFont::GetAbsLeftBorder(const bool bVertLayout, const bool bVertLayoutLRBT) const +{ + switch (GetOrientation(bVertLayout, bVertLayoutLRBT)) + { + case 0 : + return m_aLeftBorder; + break; + case 900 : + return m_aTopBorder; + break; + case 1800 : + return m_aRightBorder; + break; + case 2700 : + return m_aBottomBorder; + break; + default : + assert(false); + return m_aLeftBorder; + break; + } +} + +const std::optional& +SwFont::GetAbsRightBorder(const bool bVertLayout, const bool bVertLayoutLRBT) const +{ + switch (GetOrientation(bVertLayout, bVertLayoutLRBT)) + { + case 0 : + return m_aRightBorder; + break; + case 900 : + return m_aBottomBorder; + break; + case 1800 : + return m_aLeftBorder; + break; + case 2700 : + return m_aTopBorder; + break; + default : + assert(false); + return m_aRightBorder; + break; + } +} + +SvxShadowLocation SwFont::GetAbsShadowLocation(const bool bVertLayout, + const bool bVertLayoutLRBT) const +{ + SvxShadowLocation aLocation = SvxShadowLocation::NONE; + switch (GetOrientation(bVertLayout, bVertLayoutLRBT)) + { + case 0: + aLocation = m_aShadowLocation; + break; + + case 900: + switch ( m_aShadowLocation ) + { + case SvxShadowLocation::TopLeft: + aLocation = SvxShadowLocation::BottomLeft; + break; + case SvxShadowLocation::TopRight: + aLocation = SvxShadowLocation::TopLeft; + break; + case SvxShadowLocation::BottomLeft: + aLocation = SvxShadowLocation::BottomRight; + break; + case SvxShadowLocation::BottomRight: + aLocation = SvxShadowLocation::TopRight; + break; + case SvxShadowLocation::NONE: + case SvxShadowLocation::End: + aLocation = m_aShadowLocation; + break; + } + break; + + case 1800: + switch ( m_aShadowLocation ) + { + case SvxShadowLocation::TopLeft: + aLocation = SvxShadowLocation::BottomRight; + break; + case SvxShadowLocation::TopRight: + aLocation = SvxShadowLocation::BottomLeft; + break; + case SvxShadowLocation::BottomLeft: + aLocation = SvxShadowLocation::TopRight; + break; + case SvxShadowLocation::BottomRight: + aLocation = SvxShadowLocation::TopLeft; + break; + case SvxShadowLocation::NONE: + case SvxShadowLocation::End: + aLocation = m_aShadowLocation; + break; + } + break; + + case 2700: + switch ( m_aShadowLocation ) + { + case SvxShadowLocation::TopLeft: + aLocation = SvxShadowLocation::TopRight; + break; + case SvxShadowLocation::TopRight: + aLocation = SvxShadowLocation::BottomRight; + break; + case SvxShadowLocation::BottomLeft: + aLocation = SvxShadowLocation::TopLeft; + break; + case SvxShadowLocation::BottomRight: + aLocation = SvxShadowLocation::BottomLeft; + break; + case SvxShadowLocation::NONE: + case SvxShadowLocation::End: + aLocation = m_aShadowLocation; + break; + } + break; + + default: + assert(false); + break; + } + return aLocation; +} + +sal_uInt16 SwFont::CalcShadowSpace(const SvxShadowItemSide nShadow, const bool bVertLayout, + const bool bVertLayoutLRBT, const bool bSkipLeft, + const bool bSkipRight) const +{ + sal_uInt16 nSpace = 0; + const sal_uInt16 nOrient = GetOrientation(bVertLayout, bVertLayoutLRBT); + const SvxShadowLocation aLoc = GetAbsShadowLocation(bVertLayout, bVertLayoutLRBT); + switch( nShadow ) + { + case SvxShadowItemSide::TOP: + if(( aLoc == SvxShadowLocation::TopLeft || + aLoc == SvxShadowLocation::TopRight ) && + ( nOrient == 0 || nOrient == 1800 || + ( nOrient == 900 && !bSkipRight ) || + ( nOrient == 2700 && !bSkipLeft ))) + { + nSpace = m_nShadowWidth; + } + break; + + case SvxShadowItemSide::BOTTOM: + if(( aLoc == SvxShadowLocation::BottomLeft || + aLoc == SvxShadowLocation::BottomRight ) && + ( nOrient == 0 || nOrient == 1800 || + ( nOrient == 900 && !bSkipLeft ) || + ( nOrient == 2700 && !bSkipRight ))) + { + nSpace = m_nShadowWidth; + } + break; + + case SvxShadowItemSide::LEFT: + if(( aLoc == SvxShadowLocation::TopLeft || + aLoc == SvxShadowLocation::BottomLeft ) && + ( nOrient == 900 || nOrient == 2700 || + ( nOrient == 0 && !bSkipLeft ) || + ( nOrient == 1800 && !bSkipRight ))) + { + nSpace = m_nShadowWidth; + } + break; + + case SvxShadowItemSide::RIGHT: + if(( aLoc == SvxShadowLocation::TopRight || + aLoc == SvxShadowLocation::BottomRight ) && + ( nOrient == 900 || nOrient == 2700 || + ( nOrient == 0 && !bSkipRight ) || + ( nOrient == 1800 && !bSkipLeft ))) + { + nSpace = m_nShadowWidth; + } + break; + default: + assert(false); + break; + } + + return nSpace; +} + +// maps directions for vertical layout +static sal_uInt16 MapDirection(sal_uInt16 nDir, const bool bVertFormat, const bool bVertFormatLRBT) +{ + if ( bVertFormat ) + { + switch ( nDir ) + { + case 0 : + if (bVertFormatLRBT) + nDir = 900; + else + nDir = 2700; + break; + case 900 : + nDir = 0; + break; + case 2700 : + nDir = 1800; + break; +#if OSL_DEBUG_LEVEL > 0 + default : + OSL_FAIL( "Unsupported direction" ); + break; +#endif + } + } + return nDir; +} + +// maps the absolute direction set at the font to its logical counterpart +// in the rotated environment +sal_uInt16 UnMapDirection(sal_uInt16 nDir, const bool bVertFormat, const bool bVertFormatLRBT) +{ + if (bVertFormatLRBT) + { + switch (nDir) + { + case 900: + nDir = 0; + break; + default: + SAL_WARN("sw.core", "unsupported direction for VertLRBT"); + break; + } + return nDir; + } + + if ( bVertFormat ) + { + switch ( nDir ) + { + case 0 : + nDir = 900; + break; + case 1800 : + nDir = 2700; + break; + case 2700 : + nDir = 0; + break; +#if OSL_DEBUG_LEVEL > 0 + default : + OSL_FAIL( "Unsupported direction" ); + break; +#endif + } + } + return nDir; +} + +sal_uInt16 SwFont::GetOrientation(const bool bVertFormat, const bool bVertFormatLRBT) const +{ + return UnMapDirection(m_aSub[m_nActual].GetOrientation(), bVertFormat, bVertFormatLRBT); +} + +void SwFont::SetVertical(sal_uInt16 nDir, const bool bVertFormat, const bool bVertLayoutLRBT) +{ + // map direction if frame has vertical layout + nDir = MapDirection(nDir, bVertFormat, bVertLayoutLRBT); + + if( nDir != m_aSub[SwFontScript::Latin].GetOrientation() ) + { + m_bFontChg = true; + bool bVertical = bVertFormat && !bVertLayoutLRBT; + m_aSub[SwFontScript::Latin].SetVertical(nDir, bVertical); + m_aSub[SwFontScript::CJK].SetVertical(nDir, bVertical); + m_aSub[SwFontScript::CTL].SetVertical(nDir, bVertical); + } +} + +/* + Escapement: + frEsc: Fraction, ratio of Escapements + Esc = resulting Escapement + A1 = original Ascent (nOrgAscent) + A2 = shrunk Ascent (nEscAscent) + Ax = resulting Ascent (GetAscent()) + H1 = original Height (nOrgHeight) + H2 = shrunk Height (nEscHeight) + Hx = resulting Height (GetHeight()) + Bx = resulting Baseline for Text (CalcPos()) + (Attention: Y - A1!) + + Escapement: + Esc = H1 * frEsc; + + Superscript: + Ax = A2 + Esc; + Hx = H2 + Esc; + Bx = A1 - Esc; + + Subscript: + Ax = A1; + Hx = A1 + Esc + (H2 - A2); + Bx = A1 + Esc; +*/ + +// nEsc is the percentage +sal_uInt16 SwSubFont::CalcEscAscent( const sal_uInt16 nOldAscent ) const +{ + if( DFLT_ESC_AUTO_SUPER != GetEscapement() && + DFLT_ESC_AUTO_SUB != GetEscapement() ) + { + const long nAscent = nOldAscent + + ( static_cast(m_nOrgHeight) * GetEscapement() ) / 100; + if ( nAscent>0 ) + return std::max( nAscent, m_nOrgAscent ); + } + return m_nOrgAscent; +} + +void SwFont::SetDiffFnt( const SfxItemSet *pAttrSet, + const IDocumentSettingAccess *pIDocumentSettingAccess ) +{ + m_pBackColor.reset(); + + if( pAttrSet ) + { + const SfxPoolItem* pItem; + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_FONT, + true, &pItem )) + { + const SvxFontItem *pFont = static_cast(pItem); + m_aSub[SwFontScript::Latin].SetFamily( pFont->GetFamily() ); + m_aSub[SwFontScript::Latin].Font::SetFamilyName( pFont->GetFamilyName() ); + m_aSub[SwFontScript::Latin].Font::SetStyleName( pFont->GetStyleName() ); + m_aSub[SwFontScript::Latin].Font::SetPitch( pFont->GetPitch() ); + m_aSub[SwFontScript::Latin].Font::SetCharSet( pFont->GetCharSet() ); + } + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_FONTSIZE, + true, &pItem )) + { + const SvxFontHeightItem *pHeight = static_cast(pItem); + m_aSub[SwFontScript::Latin].SvxFont::SetPropr( 100 ); + m_aSub[SwFontScript::Latin].m_aSize = m_aSub[SwFontScript::Latin].Font::GetFontSize(); + Size aTmpSize = m_aSub[SwFontScript::Latin].m_aSize; + aTmpSize.setHeight( pHeight->GetHeight() ); + m_aSub[SwFontScript::Latin].SetSize( aTmpSize ); + } + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_POSTURE, + true, &pItem )) + m_aSub[SwFontScript::Latin].Font::SetItalic( static_cast(pItem)->GetPosture() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_WEIGHT, + true, &pItem )) + m_aSub[SwFontScript::Latin].Font::SetWeight( static_cast(pItem)->GetWeight() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_LANGUAGE, + true, &pItem )) + m_aSub[SwFontScript::Latin].SetLanguage( static_cast(pItem)->GetLanguage() ); + + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CJK_FONT, + true, &pItem )) + { + const SvxFontItem *pFont = static_cast(pItem); + m_aSub[SwFontScript::CJK].SetFamily( pFont->GetFamily() ); + m_aSub[SwFontScript::CJK].Font::SetFamilyName( pFont->GetFamilyName() ); + m_aSub[SwFontScript::CJK].Font::SetStyleName( pFont->GetStyleName() ); + m_aSub[SwFontScript::CJK].Font::SetPitch( pFont->GetPitch() ); + m_aSub[SwFontScript::CJK].Font::SetCharSet( pFont->GetCharSet() ); + } + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CJK_FONTSIZE, + true, &pItem )) + { + const SvxFontHeightItem *pHeight = static_cast(pItem); + m_aSub[SwFontScript::CJK].SvxFont::SetPropr( 100 ); + m_aSub[SwFontScript::CJK].m_aSize = m_aSub[SwFontScript::CJK].Font::GetFontSize(); + Size aTmpSize = m_aSub[SwFontScript::CJK].m_aSize; + aTmpSize.setHeight( pHeight->GetHeight() ); + m_aSub[SwFontScript::CJK].SetSize( aTmpSize ); + } + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CJK_POSTURE, + true, &pItem )) + m_aSub[SwFontScript::CJK].Font::SetItalic( static_cast(pItem)->GetPosture() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CJK_WEIGHT, + true, &pItem )) + m_aSub[SwFontScript::CJK].Font::SetWeight( static_cast(pItem)->GetWeight() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CJK_LANGUAGE, + true, &pItem )) + { + LanguageType eNewLang = static_cast(pItem)->GetLanguage(); + m_aSub[SwFontScript::CJK].SetLanguage( eNewLang ); + m_aSub[SwFontScript::Latin].SetCJKContextLanguage( eNewLang ); + m_aSub[SwFontScript::CJK].SetCJKContextLanguage( eNewLang ); + m_aSub[SwFontScript::CTL].SetCJKContextLanguage( eNewLang ); + } + + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CTL_FONT, + true, &pItem )) + { + const SvxFontItem *pFont = static_cast(pItem); + m_aSub[SwFontScript::CTL].SetFamily( pFont->GetFamily() ); + m_aSub[SwFontScript::CTL].Font::SetFamilyName( pFont->GetFamilyName() ); + m_aSub[SwFontScript::CTL].Font::SetStyleName( pFont->GetStyleName() ); + m_aSub[SwFontScript::CTL].Font::SetPitch( pFont->GetPitch() ); + m_aSub[SwFontScript::CTL].Font::SetCharSet( pFont->GetCharSet() ); + } + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CTL_FONTSIZE, + true, &pItem )) + { + const SvxFontHeightItem *pHeight = static_cast(pItem); + m_aSub[SwFontScript::CTL].SvxFont::SetPropr( 100 ); + m_aSub[SwFontScript::CTL].m_aSize = m_aSub[SwFontScript::CTL].Font::GetFontSize(); + Size aTmpSize = m_aSub[SwFontScript::CTL].m_aSize; + aTmpSize.setHeight( pHeight->GetHeight() ); + m_aSub[SwFontScript::CTL].SetSize( aTmpSize ); + } + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CTL_POSTURE, + true, &pItem )) + m_aSub[SwFontScript::CTL].Font::SetItalic( static_cast(pItem)->GetPosture() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CTL_WEIGHT, + true, &pItem )) + m_aSub[SwFontScript::CTL].Font::SetWeight( static_cast(pItem)->GetWeight() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CTL_LANGUAGE, + true, &pItem )) + m_aSub[SwFontScript::CTL].SetLanguage( static_cast(pItem)->GetLanguage() ); + + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_UNDERLINE, + true, &pItem )) + { + SetUnderline( static_cast(pItem)->GetLineStyle() ); + SetUnderColor( static_cast(pItem)->GetColor() ); + } + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_OVERLINE, + true, &pItem )) + { + SetOverline( static_cast(pItem)->GetLineStyle() ); + SetOverColor( static_cast(pItem)->GetColor() ); + } + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CROSSEDOUT, + true, &pItem )) + SetStrikeout( static_cast(pItem)->GetStrikeout() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_COLOR, + true, &pItem )) + SetColor( static_cast(pItem)->GetValue() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_EMPHASIS_MARK, + true, &pItem )) + SetEmphasisMark( static_cast(pItem)->GetEmphasisMark() ); + + SetTransparent( true ); + SetAlign( ALIGN_BASELINE ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CONTOUR, + true, &pItem )) + SetOutline( static_cast(pItem)->GetValue() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_SHADOWED, + true, &pItem )) + SetShadow( static_cast(pItem)->GetValue() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_RELIEF, + true, &pItem )) + SetRelief( static_cast(pItem)->GetValue() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_SHADOWED, + true, &pItem )) + SetPropWidth(static_cast(pItem)->GetValue() ? 50 : 100 ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_AUTOKERN, + true, &pItem )) + { + if( static_cast(pItem)->GetValue() ) + { + SetAutoKern( ( !pIDocumentSettingAccess || + !pIDocumentSettingAccess->get(DocumentSettingId::KERN_ASIAN_PUNCTUATION) ) ? + FontKerning::FontSpecific : + FontKerning::Asian ); + } + else + SetAutoKern( FontKerning::NONE ); + } + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_WORDLINEMODE, + true, &pItem )) + SetWordLineMode( static_cast(pItem)->GetValue() ); + + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_ESCAPEMENT, + true, &pItem )) + { + const SvxEscapementItem *pEsc = static_cast(pItem); + SetEscapement( pEsc->GetEsc() ); + if( m_aSub[SwFontScript::Latin].IsEsc() ) + SetProportion( pEsc->GetProportionalHeight() ); + } + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_CASEMAP, + true, &pItem )) + SetCaseMap( static_cast(pItem)->GetCaseMap() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_KERNING, + true, &pItem )) + SetFixKerning( static_cast(pItem)->GetValue() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_ROTATE, + true, &pItem )) + SetVertical( static_cast(pItem)->GetValue() ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_BACKGROUND, + true, &pItem )) + m_pBackColor.reset( new Color( static_cast(pItem)->GetColor() ) ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_HIGHLIGHT, + true, &pItem )) + SetHighlightColor(static_cast(pItem)->GetColor()); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_BOX, + true, &pItem )) + { + const SvxBoxItem* pBoxItem = static_cast(pItem); + SetTopBorder(pBoxItem->GetTop()); + SetBottomBorder(pBoxItem->GetBottom()); + SetRightBorder(pBoxItem->GetRight()); + SetLeftBorder(pBoxItem->GetLeft()); + SetTopBorderDist(pBoxItem->GetDistance(SvxBoxItemLine::TOP)); + SetBottomBorderDist(pBoxItem->GetDistance(SvxBoxItemLine::BOTTOM)); + SetRightBorderDist(pBoxItem->GetDistance(SvxBoxItemLine::RIGHT)); + SetLeftBorderDist(pBoxItem->GetDistance(SvxBoxItemLine::LEFT)); + } + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_SHADOW, + true, &pItem )) + { + const SvxShadowItem* pShadowItem = static_cast(pItem); + SetShadowColor(pShadowItem->GetColor()); + SetShadowWidth(pShadowItem->GetWidth()); + SetShadowLocation(pShadowItem->GetLocation()); + } + const SfxPoolItem* pTwoLinesItem = nullptr; + if( SfxItemState::SET == + pAttrSet->GetItemState( RES_CHRATR_TWO_LINES, true, &pTwoLinesItem )) + if ( static_cast(pTwoLinesItem)->GetValue() ) + SetVertical( 0 ); + } + else + { + Invalidate(); + } + m_bPaintBlank = false; + OSL_ENSURE( m_aSub[SwFontScript::Latin].IsTransparent(), "SwFont: Transparent revolution" ); +} + +SwFont::SwFont( const SwFont &rFont ) + : m_aSub(rFont.m_aSub) +{ + m_nActual = rFont.m_nActual; + m_pBackColor.reset( rFont.m_pBackColor ? new Color( *rFont.m_pBackColor ) : nullptr ); + m_aHighlightColor = rFont.m_aHighlightColor; + m_aTopBorder = rFont.m_aTopBorder; + m_aBottomBorder = rFont.m_aBottomBorder; + m_aRightBorder = rFont.m_aRightBorder; + m_aLeftBorder = rFont.m_aLeftBorder; + m_nTopBorderDist = rFont.m_nTopBorderDist; + m_nBottomBorderDist = rFont.m_nBottomBorderDist; + m_nRightBorderDist = rFont.m_nRightBorderDist; + m_nLeftBorderDist = rFont.m_nLeftBorderDist; + m_aShadowColor = rFont.m_aShadowColor; + m_nShadowWidth = rFont.m_nShadowWidth; + m_aShadowLocation = rFont.m_aShadowLocation; + m_aUnderColor = rFont.GetUnderColor(); + m_aOverColor = rFont.GetOverColor(); + m_nToxCount = 0; + m_nRefCount = 0; + m_nMetaCount = 0; + m_nInputFieldCount = 0; + m_bFontChg = rFont.m_bFontChg; + m_bOrgChg = rFont.m_bOrgChg; + m_bPaintBlank = rFont.m_bPaintBlank; + m_bGreyWave = rFont.m_bGreyWave; +} + +SwFont::SwFont( const SwAttrSet* pAttrSet, + const IDocumentSettingAccess* pIDocumentSettingAccess ) + : m_aSub() +{ + m_nActual = SwFontScript::Latin; + m_nToxCount = 0; + m_nRefCount = 0; + m_nMetaCount = 0; + m_nInputFieldCount = 0; + m_bPaintBlank = false; + m_bGreyWave = false; + m_bOrgChg = true; + { + const SvxFontItem& rFont = pAttrSet->GetFont(); + m_aSub[SwFontScript::Latin].SetFamily( rFont.GetFamily() ); + m_aSub[SwFontScript::Latin].SetFamilyName( rFont.GetFamilyName() ); + m_aSub[SwFontScript::Latin].SetStyleName( rFont.GetStyleName() ); + m_aSub[SwFontScript::Latin].SetPitch( rFont.GetPitch() ); + m_aSub[SwFontScript::Latin].SetCharSet( rFont.GetCharSet() ); + m_aSub[SwFontScript::Latin].SvxFont::SetPropr( 100 ); // 100% of FontSize + Size aTmpSize = m_aSub[SwFontScript::Latin].m_aSize; + aTmpSize.setHeight( pAttrSet->GetSize().GetHeight() ); + m_aSub[SwFontScript::Latin].SetSize( aTmpSize ); + m_aSub[SwFontScript::Latin].SetItalic( pAttrSet->GetPosture().GetPosture() ); + m_aSub[SwFontScript::Latin].SetWeight( pAttrSet->GetWeight().GetWeight() ); + m_aSub[SwFontScript::Latin].SetLanguage( pAttrSet->GetLanguage().GetLanguage() ); + } + + { + const SvxFontItem& rFont = pAttrSet->GetCJKFont(); + m_aSub[SwFontScript::CJK].SetFamily( rFont.GetFamily() ); + m_aSub[SwFontScript::CJK].SetFamilyName( rFont.GetFamilyName() ); + m_aSub[SwFontScript::CJK].SetStyleName( rFont.GetStyleName() ); + m_aSub[SwFontScript::CJK].SetPitch( rFont.GetPitch() ); + m_aSub[SwFontScript::CJK].SetCharSet( rFont.GetCharSet() ); + m_aSub[SwFontScript::CJK].SvxFont::SetPropr( 100 ); // 100% of FontSize + Size aTmpSize = m_aSub[SwFontScript::CJK].m_aSize; + aTmpSize.setHeight( pAttrSet->GetCJKSize().GetHeight() ); + m_aSub[SwFontScript::CJK].SetSize( aTmpSize ); + m_aSub[SwFontScript::CJK].SetItalic( pAttrSet->GetCJKPosture().GetPosture() ); + m_aSub[SwFontScript::CJK].SetWeight( pAttrSet->GetCJKWeight().GetWeight() ); + LanguageType eNewLang = pAttrSet->GetCJKLanguage().GetLanguage(); + m_aSub[SwFontScript::CJK].SetLanguage( eNewLang ); + m_aSub[SwFontScript::Latin].SetCJKContextLanguage( eNewLang ); + m_aSub[SwFontScript::CJK].SetCJKContextLanguage( eNewLang ); + m_aSub[SwFontScript::CTL].SetCJKContextLanguage( eNewLang ); + } + + { + const SvxFontItem& rFont = pAttrSet->GetCTLFont(); + m_aSub[SwFontScript::CTL].SetFamily( rFont.GetFamily() ); + m_aSub[SwFontScript::CTL].SetFamilyName( rFont.GetFamilyName() ); + m_aSub[SwFontScript::CTL].SetStyleName( rFont.GetStyleName() ); + m_aSub[SwFontScript::CTL].SetPitch( rFont.GetPitch() ); + m_aSub[SwFontScript::CTL].SetCharSet( rFont.GetCharSet() ); + m_aSub[SwFontScript::CTL].SvxFont::SetPropr( 100 ); // 100% of FontSize + Size aTmpSize = m_aSub[SwFontScript::CTL].m_aSize; + aTmpSize.setHeight( pAttrSet->GetCTLSize().GetHeight() ); + m_aSub[SwFontScript::CTL].SetSize( aTmpSize ); + m_aSub[SwFontScript::CTL].SetItalic( pAttrSet->GetCTLPosture().GetPosture() ); + m_aSub[SwFontScript::CTL].SetWeight( pAttrSet->GetCTLWeight().GetWeight() ); + m_aSub[SwFontScript::CTL].SetLanguage( pAttrSet->GetCTLLanguage().GetLanguage() ); + } + if ( pAttrSet->GetCharHidden().GetValue() ) + SetUnderline( LINESTYLE_DOTTED ); + else + SetUnderline( pAttrSet->GetUnderline().GetLineStyle() ); + SetUnderColor( pAttrSet->GetUnderline().GetColor() ); + SetOverline( pAttrSet->GetOverline().GetLineStyle() ); + SetOverColor( pAttrSet->GetOverline().GetColor() ); + SetEmphasisMark( pAttrSet->GetEmphasisMark().GetEmphasisMark() ); + SetStrikeout( pAttrSet->GetCrossedOut().GetStrikeout() ); + SetColor( pAttrSet->GetColor().GetValue() ); + SetTransparent( true ); + SetAlign( ALIGN_BASELINE ); + SetOutline( pAttrSet->GetContour().GetValue() ); + SetShadow( pAttrSet->GetShadowed().GetValue() ); + SetPropWidth( pAttrSet->GetCharScaleW().GetValue() ); + SetRelief( pAttrSet->GetCharRelief().GetValue() ); + if( pAttrSet->GetAutoKern().GetValue() ) + { + SetAutoKern( ( !pIDocumentSettingAccess || + !pIDocumentSettingAccess->get(DocumentSettingId::KERN_ASIAN_PUNCTUATION) ) ? + FontKerning::FontSpecific : + FontKerning::Asian ); + } + else + SetAutoKern( FontKerning::NONE ); + SetWordLineMode( pAttrSet->GetWordLineMode().GetValue() ); + const SvxEscapementItem &rEsc = pAttrSet->GetEscapement(); + SetEscapement( rEsc.GetEsc() ); + if( m_aSub[SwFontScript::Latin].IsEsc() ) + SetProportion( rEsc.GetProportionalHeight() ); + SetCaseMap( pAttrSet->GetCaseMap().GetCaseMap() ); + SetFixKerning( pAttrSet->GetKerning().GetValue() ); + const SfxPoolItem* pItem; + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_BACKGROUND, + true, &pItem )) + m_pBackColor.reset( new Color( static_cast(pItem)->GetColor() ) ); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_HIGHLIGHT, + true, &pItem )) + SetHighlightColor(static_cast(pItem)->GetColor()); + else + SetHighlightColor(COL_TRANSPARENT); + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_BOX, + true, &pItem )) + { + const SvxBoxItem* pBoxItem = static_cast(pItem); + SetTopBorder(pBoxItem->GetTop()); + SetBottomBorder(pBoxItem->GetBottom()); + SetRightBorder(pBoxItem->GetRight()); + SetLeftBorder(pBoxItem->GetLeft()); + SetTopBorderDist(pBoxItem->GetDistance(SvxBoxItemLine::TOP)); + SetBottomBorderDist(pBoxItem->GetDistance(SvxBoxItemLine::BOTTOM)); + SetRightBorderDist(pBoxItem->GetDistance(SvxBoxItemLine::RIGHT)); + SetLeftBorderDist(pBoxItem->GetDistance(SvxBoxItemLine::LEFT)); + } + else + { + SetTopBorder(nullptr); + SetBottomBorder(nullptr); + SetRightBorder(nullptr); + SetLeftBorder(nullptr); + SetTopBorderDist(0); + SetBottomBorderDist(0); + SetRightBorderDist(0); + SetLeftBorderDist(0); + } + + if( SfxItemState::SET == pAttrSet->GetItemState( RES_CHRATR_SHADOW, + true, &pItem )) + { + const SvxShadowItem* pShadowItem = static_cast(pItem); + SetShadowColor(pShadowItem->GetColor()); + SetShadowWidth(pShadowItem->GetWidth()); + SetShadowLocation(pShadowItem->GetLocation()); + } + else + { + SetShadowColor(COL_TRANSPARENT); + SetShadowWidth(0); + SetShadowLocation(SvxShadowLocation::NONE); + } + + const SvxTwoLinesItem& rTwoLinesItem = pAttrSet->Get2Lines(); + if ( ! rTwoLinesItem.GetValue() ) + SetVertical( pAttrSet->GetCharRotate().GetValue() ); + else + SetVertical( 0 ); + if( pIDocumentSettingAccess && pIDocumentSettingAccess->get( DocumentSettingId::SMALL_CAPS_PERCENTAGE_66 )) + { + m_aSub[ SwFontScript::Latin ].m_bSmallCapsPercentage66 = true; + m_aSub[ SwFontScript::CJK ].m_bSmallCapsPercentage66 = true; + m_aSub[ SwFontScript::CTL ].m_bSmallCapsPercentage66 = true; + } +} + +SwFont::~SwFont() +{ +} + +SwFont& SwFont::operator=( const SwFont &rFont ) +{ + if (this != &rFont) + { + m_aSub[SwFontScript::Latin] = rFont.m_aSub[SwFontScript::Latin]; + m_aSub[SwFontScript::CJK] = rFont.m_aSub[SwFontScript::CJK]; + m_aSub[SwFontScript::CTL] = rFont.m_aSub[SwFontScript::CTL]; + m_nActual = rFont.m_nActual; + m_pBackColor.reset( rFont.m_pBackColor ? new Color( *rFont.m_pBackColor ) : nullptr ); + m_aHighlightColor = rFont.m_aHighlightColor; + m_aTopBorder = rFont.m_aTopBorder; + m_aBottomBorder = rFont.m_aBottomBorder; + m_aRightBorder = rFont.m_aRightBorder; + m_aLeftBorder = rFont.m_aLeftBorder; + m_nTopBorderDist = rFont.m_nTopBorderDist; + m_nBottomBorderDist = rFont.m_nBottomBorderDist; + m_nRightBorderDist = rFont.m_nRightBorderDist; + m_nLeftBorderDist = rFont.m_nLeftBorderDist; + m_aShadowColor = rFont.m_aShadowColor; + m_nShadowWidth = rFont.m_nShadowWidth; + m_aShadowLocation = rFont.m_aShadowLocation; + m_aUnderColor = rFont.GetUnderColor(); + m_aOverColor = rFont.GetOverColor(); + m_nToxCount = 0; + m_nRefCount = 0; + m_nMetaCount = 0; + m_nInputFieldCount = 0; + m_bFontChg = rFont.m_bFontChg; + m_bOrgChg = rFont.m_bOrgChg; + m_bPaintBlank = rFont.m_bPaintBlank; + m_bGreyWave = rFont.m_bGreyWave; + } + return *this; +} + +void SwFont::AllocFontCacheId( SwViewShell const *pSh, SwFontScript nWhich ) +{ + SwFntAccess aFntAccess( m_aSub[nWhich].m_nFontCacheId, m_aSub[nWhich].m_nFontIndex, + &m_aSub[nWhich], pSh, true ); +} + +bool SwSubFont::IsSymbol( SwViewShell const *pSh ) +{ + SwFntAccess aFntAccess( m_nFontCacheId, m_nFontIndex, this, pSh, false ); + return aFntAccess.Get()->IsSymbol(); +} + +bool SwSubFont::ChgFnt( SwViewShell const *pSh, OutputDevice& rOut ) +{ + if ( pLastFont ) + pLastFont->Unlock(); + SwFntAccess aFntAccess( m_nFontCacheId, m_nFontIndex, this, pSh, true ); + SV_STAT( nChangeFont ); + + pLastFont = aFntAccess.Get(); + + pLastFont->SetDevFont( pSh, rOut ); + + pLastFont->Lock(); + return LINESTYLE_NONE != GetUnderline() || + LINESTYLE_NONE != GetOverline() || + STRIKEOUT_NONE != GetStrikeout(); +} + +void SwFont::ChgPhysFnt( SwViewShell const *pSh, OutputDevice& rOut ) +{ + if( m_bOrgChg && m_aSub[m_nActual].IsEsc() ) + { + const sal_uInt8 nOldProp = m_aSub[m_nActual].GetPropr(); + SetProportion( 100 ); + ChgFnt( pSh, rOut ); + SwFntAccess aFntAccess( m_aSub[m_nActual].m_nFontCacheId, m_aSub[m_nActual].m_nFontIndex, + &m_aSub[m_nActual], pSh ); + m_aSub[m_nActual].m_nOrgHeight = aFntAccess.Get()->GetFontHeight( pSh, rOut ); + m_aSub[m_nActual].m_nOrgAscent = aFntAccess.Get()->GetFontAscent( pSh, rOut ); + SetProportion( nOldProp ); + m_bOrgChg = false; + } + + if( m_bFontChg ) + { + ChgFnt( pSh, rOut ); + m_bFontChg = m_bOrgChg; + } + if( rOut.GetTextLineColor() != m_aUnderColor ) + rOut.SetTextLineColor( m_aUnderColor ); + if( rOut.GetOverlineColor() != m_aOverColor ) + rOut.SetOverlineColor( m_aOverColor ); +} + +// Height = MaxAscent + MaxDescent +// MaxAscent = Max (T1_ascent, T2_ascent + (Esc * T1_height) ); +// MaxDescent = Max (T1_height-T1_ascent, +// T2_height-T2_ascent - (Esc * T1_height) +sal_uInt16 SwSubFont::CalcEscHeight( const sal_uInt16 nOldHeight, + const sal_uInt16 nOldAscent ) const +{ + if( DFLT_ESC_AUTO_SUPER != GetEscapement() && + DFLT_ESC_AUTO_SUB != GetEscapement() ) + { + long nDescent = nOldHeight - nOldAscent - + ( static_cast(m_nOrgHeight) * GetEscapement() ) / 100; + const sal_uInt16 nDesc = nDescent>0 + ? std::max( nDescent, m_nOrgHeight - m_nOrgAscent) + : m_nOrgHeight - m_nOrgAscent; + return ( nDesc + CalcEscAscent( nOldAscent ) ); + } + return m_nOrgHeight; +} + +short SwSubFont::CheckKerning_( ) +{ + short nKernx = - short( Font::GetFontSize().Height() / 6 ); + + if ( nKernx < GetFixKerning() ) + return GetFixKerning(); + return nKernx; +} + +sal_uInt16 SwSubFont::GetAscent( SwViewShell const *pSh, const OutputDevice& rOut ) +{ + SwFntAccess aFntAccess( m_nFontCacheId, m_nFontIndex, this, pSh ); + const sal_uInt16 nAscent = aFntAccess.Get()->GetFontAscent( pSh, rOut ); + return GetEscapement() ? CalcEscAscent( nAscent ) : nAscent; +} + +sal_uInt16 SwSubFont::GetHeight( SwViewShell const *pSh, const OutputDevice& rOut ) +{ + SV_STAT( nGetTextSize ); + SwFntAccess aFntAccess( m_nFontCacheId, m_nFontIndex, this, pSh ); + const sal_uInt16 nHeight = aFntAccess.Get()->GetFontHeight( pSh, rOut ); + if ( GetEscapement() ) + { + const sal_uInt16 nAscent = aFntAccess.Get()->GetFontAscent( pSh, rOut ); + return CalcEscHeight( nHeight, nAscent ); // + nLeading; + } + return nHeight; // + nLeading; +} + +Size SwSubFont::GetTextSize_( SwDrawTextInfo& rInf ) +{ + // Robust: the font is supposed to be set already, but better safe than + // sorry... + if ( !pLastFont || pLastFont->GetOwner() != reinterpret_cast(m_nFontCacheId) || + !IsSameInstance( rInf.GetpOut()->GetFont() ) ) + ChgFnt( rInf.GetShell(), rInf.GetOut() ); + + SwDigitModeModifier aDigitModeModifier( rInf.GetOut(), rInf.GetFont()->GetLanguage() ); + + Size aTextSize; + TextFrameIndex const nLn = rInf.GetLen() == TextFrameIndex(COMPLETE_STRING) + ? TextFrameIndex(rInf.GetText().getLength()) + : rInf.GetLen(); + rInf.SetLen( nLn ); + if( IsCapital() && nLn ) + aTextSize = GetCapitalSize( rInf ); + else + { + SV_STAT( nGetTextSize ); + long nOldKern = rInf.GetKern(); + const OUString oldText = rInf.GetText(); + rInf.SetKern( CheckKerning() ); + if ( !IsCaseMap() ) + aTextSize = pLastFont->GetTextSize( rInf ); + else + { + const OUString aTmp = CalcCaseMap( rInf.GetText() ); + const OUString oldStr = rInf.GetText(); + bool bCaseMapLengthDiffers(aTmp.getLength() != oldStr.getLength()); + + if(bCaseMapLengthDiffers && rInf.GetLen()) + { + // If the length of the original string and the CaseMapped one + // are different, it is necessary to handle the given text part as + // a single snippet since its size may differ, too. + TextFrameIndex const nOldIdx(rInf.GetIdx()); + TextFrameIndex const nOldLen(rInf.GetLen()); + const OUString aSnippet(oldStr.copy(sal_Int32(nOldIdx), sal_Int32(nOldLen))); + const OUString aNewText(CalcCaseMap(aSnippet)); + + rInf.SetText( aNewText ); + rInf.SetIdx( TextFrameIndex(0) ); + rInf.SetLen( TextFrameIndex(aNewText.getLength()) ); + + aTextSize = pLastFont->GetTextSize( rInf ); + + rInf.SetIdx( nOldIdx ); + rInf.SetLen( nOldLen ); + } + else + { + rInf.SetText( aTmp ); + aTextSize = pLastFont->GetTextSize( rInf ); + } + + rInf.SetText(oldStr); + } + rInf.SetKern( nOldKern ); + rInf.SetText(oldText); + // A word that's longer than one line, with escapement at the line + // break, must report its effective height. + if( GetEscapement() ) + { + const sal_uInt16 nAscent = pLastFont->GetFontAscent( rInf.GetShell(), + rInf.GetOut() ); + aTextSize.setHeight( + static_cast(CalcEscHeight( static_cast(aTextSize.Height()), nAscent)) ); + } + } + + if (TextFrameIndex(1) == rInf.GetLen() + && CH_TXT_ATR_FIELDSTART == rInf.GetText()[sal_Int32(rInf.GetIdx())]) + { + assert(!"this is presumably dead code"); + TextFrameIndex const nOldIdx(rInf.GetIdx()); + TextFrameIndex const nOldLen(rInf.GetLen()); + const OUString aNewText(CH_TXT_ATR_SUBST_FIELDSTART); + rInf.SetText( aNewText ); + rInf.SetIdx( TextFrameIndex(0) ); + rInf.SetLen( TextFrameIndex(aNewText.getLength()) ); + aTextSize = pLastFont->GetTextSize( rInf ); + rInf.SetIdx( nOldIdx ); + rInf.SetLen( nOldLen ); + } + else if (TextFrameIndex(1) == rInf.GetLen() + && CH_TXT_ATR_FIELDEND == rInf.GetText()[sal_Int32(rInf.GetIdx())]) + { + assert(!"this is presumably dead code"); + TextFrameIndex const nOldIdx(rInf.GetIdx()); + TextFrameIndex const nOldLen(rInf.GetLen()); + const OUString aNewText(CH_TXT_ATR_SUBST_FIELDEND); + rInf.SetText( aNewText ); + rInf.SetIdx( TextFrameIndex(0) ); + rInf.SetLen( TextFrameIndex(aNewText.getLength()) ); + aTextSize = pLastFont->GetTextSize( rInf ); + rInf.SetIdx( nOldIdx ); + rInf.SetLen( nOldLen ); + } + + return aTextSize; +} + +void SwSubFont::DrawText_( SwDrawTextInfo &rInf, const bool bGrey ) +{ + rInf.SetGreyWave( bGrey ); + TextFrameIndex const nLn(rInf.GetText().getLength()); + if( !rInf.GetLen() || !nLn ) + return; + if (TextFrameIndex(COMPLETE_STRING) == rInf.GetLen()) + rInf.SetLen( nLn ); + + FontLineStyle nOldUnder = LINESTYLE_NONE; + SwUnderlineFont* pUnderFnt = nullptr; + + if( rInf.GetUnderFnt() ) + { + nOldUnder = GetUnderline(); + SetUnderline( LINESTYLE_NONE ); + pUnderFnt = rInf.GetUnderFnt(); + } + + if( !pLastFont || pLastFont->GetOwner() != reinterpret_cast(m_nFontCacheId) ) + ChgFnt( rInf.GetShell(), rInf.GetOut() ); + + SwDigitModeModifier aDigitModeModifier( rInf.GetOut(), rInf.GetFont()->GetLanguage() ); + + const Point aOldPos(rInf.GetPos()); + Point aPos( rInf.GetPos() ); + + if( GetEscapement() ) + CalcEsc( rInf, aPos ); + + rInf.SetPos( aPos ); + rInf.SetKern( CheckKerning() + rInf.GetSperren() / SPACING_PRECISION_FACTOR ); + + if( IsCapital() ) + DrawCapital( rInf ); + else + { + SV_STAT( nDrawText ); + if ( !IsCaseMap() ) + pLastFont->DrawText( rInf ); + else + { + const OUString oldStr = rInf.GetText(); + const OUString aString( CalcCaseMap(oldStr) ); + bool bCaseMapLengthDiffers(aString.getLength() != oldStr.getLength()); + + if(bCaseMapLengthDiffers && rInf.GetLen()) + { + // If the length of the original string and the CaseMapped one + // are different, it is necessary to handle the given text part as + // a single snippet since its size may differ, too. + TextFrameIndex const nOldIdx(rInf.GetIdx()); + TextFrameIndex const nOldLen(rInf.GetLen()); + const OUString aSnippet(oldStr.copy(sal_Int32(nOldIdx), sal_Int32(nOldLen))); + const OUString aNewText = CalcCaseMap(aSnippet); + + rInf.SetText( aNewText ); + rInf.SetIdx( TextFrameIndex(0) ); + rInf.SetLen( TextFrameIndex(aNewText.getLength()) ); + + pLastFont->DrawText( rInf ); + + rInf.SetIdx( nOldIdx ); + rInf.SetLen( nOldLen ); + } + else + { + rInf.SetText( aString ); + pLastFont->DrawText( rInf ); + } + + rInf.SetText(oldStr); + } + } + + if( pUnderFnt && nOldUnder != LINESTYLE_NONE ) + { + Size aFontSize = GetTextSize_( rInf ); + const OUString oldStr = rInf.GetText(); + + TextFrameIndex const nOldIdx = rInf.GetIdx(); + TextFrameIndex const nOldLen = rInf.GetLen(); + long nSpace = 0; + if( rInf.GetSpace() ) + { + TextFrameIndex nTmpEnd = nOldIdx + nOldLen; + if (nTmpEnd > TextFrameIndex(oldStr.getLength())) + nTmpEnd = TextFrameIndex(oldStr.getLength()); + + const SwScriptInfo* pSI = rInf.GetScriptInfo(); + + const bool bAsianFont = + ( rInf.GetFont() && SwFontScript::CJK == rInf.GetFont()->GetActual() ); + for (TextFrameIndex nTmp = nOldIdx; nTmp < nTmpEnd; ++nTmp) + { + if (CH_BLANK == oldStr[sal_Int32(nTmp)] || bAsianFont || + (nTmp + TextFrameIndex(1) < TextFrameIndex(oldStr.getLength()) + && pSI + && i18n::ScriptType::ASIAN == pSI->ScriptType(nTmp + TextFrameIndex(1)))) + { + ++nSpace; + } + } + + // if next portion if a hole portion we do not consider any + // extra space added because the last character was ASIAN + if ( nSpace && rInf.IsSpaceStop() && bAsianFont ) + --nSpace; + + nSpace *= rInf.GetSpace() / SPACING_PRECISION_FACTOR; + } + + rInf.SetWidth( sal_uInt16(aFontSize.Width() + nSpace) ); + rInf.SetText( " " ); + rInf.SetIdx( TextFrameIndex(0) ); + rInf.SetLen( TextFrameIndex(2) ); + SetUnderline( nOldUnder ); + rInf.SetUnderFnt( nullptr ); + + // set position for underline font + rInf.SetPos( pUnderFnt->GetPos() ); + + pUnderFnt->GetFont().DrawStretchText_( rInf ); + + rInf.SetUnderFnt( pUnderFnt ); + rInf.SetText(oldStr); + rInf.SetIdx( nOldIdx ); + rInf.SetLen( nOldLen ); + } + + rInf.SetPos(aOldPos); +} + +void SwSubFont::DrawStretchText_( SwDrawTextInfo &rInf ) +{ + if( !rInf.GetLen() || !rInf.GetText().getLength() ) + return; + + FontLineStyle nOldUnder = LINESTYLE_NONE; + SwUnderlineFont* pUnderFnt = nullptr; + + if( rInf.GetUnderFnt() ) + { + nOldUnder = GetUnderline(); + SetUnderline( LINESTYLE_NONE ); + pUnderFnt = rInf.GetUnderFnt(); + } + + if ( !pLastFont || pLastFont->GetOwner() != reinterpret_cast(m_nFontCacheId) ) + ChgFnt( rInf.GetShell(), rInf.GetOut() ); + + SwDigitModeModifier aDigitModeModifier( rInf.GetOut(), rInf.GetFont()->GetLanguage() ); + + rInf.ApplyAutoColor(); + + const Point aOldPos(rInf.GetPos()); + Point aPos( rInf.GetPos() ); + + if( GetEscapement() ) + CalcEsc( rInf, aPos ); + + rInf.SetKern( CheckKerning() + rInf.GetSperren() / SPACING_PRECISION_FACTOR ); + rInf.SetPos( aPos ); + + if( IsCapital() ) + DrawStretchCapital( rInf ); + else + { + SV_STAT( nDrawStretchText ); + + if ( rInf.GetFrame() ) + { + if ( rInf.GetFrame()->IsRightToLeft() ) + rInf.GetFrame()->SwitchLTRtoRTL( aPos ); + + if ( rInf.GetFrame()->IsVertical() ) + rInf.GetFrame()->SwitchHorizontalToVertical( aPos ); + + rInf.SetPos( aPos ); + } + + if ( !IsCaseMap() ) + rInf.GetOut().DrawStretchText( aPos, rInf.GetWidth(), + rInf.GetText(), sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + else + rInf.GetOut().DrawStretchText( aPos, rInf.GetWidth(), + CalcCaseMap(rInf.GetText()), + sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen())); + } + + if( pUnderFnt && nOldUnder != LINESTYLE_NONE ) + { + const OUString oldStr = rInf.GetText(); + TextFrameIndex const nOldIdx = rInf.GetIdx(); + TextFrameIndex const nOldLen = rInf.GetLen(); + rInf.SetText( " " ); + rInf.SetIdx( TextFrameIndex(0) ); + rInf.SetLen( TextFrameIndex(2) ); + SetUnderline( nOldUnder ); + rInf.SetUnderFnt( nullptr ); + + // set position for underline font + rInf.SetPos( pUnderFnt->GetPos() ); + + pUnderFnt->GetFont().DrawStretchText_( rInf ); + + rInf.SetUnderFnt( pUnderFnt ); + rInf.SetText(oldStr); + rInf.SetIdx( nOldIdx ); + rInf.SetLen( nOldLen ); + } + + rInf.SetPos(aOldPos); +} + +TextFrameIndex SwSubFont::GetModelPositionForViewPoint_( SwDrawTextInfo& rInf ) +{ + if ( !pLastFont || pLastFont->GetOwner() != reinterpret_cast(m_nFontCacheId) ) + ChgFnt( rInf.GetShell(), rInf.GetOut() ); + + SwDigitModeModifier aDigitModeModifier( rInf.GetOut(), rInf.GetFont()->GetLanguage() ); + + TextFrameIndex const nLn = rInf.GetLen() == TextFrameIndex(COMPLETE_STRING) + ? TextFrameIndex(rInf.GetText().getLength()) + : rInf.GetLen(); + rInf.SetLen( nLn ); + TextFrameIndex nCursor(0); + if( IsCapital() && nLn ) + nCursor = GetCapitalCursorOfst( rInf ); + else + { + const OUString oldText = rInf.GetText(); + long nOldKern = rInf.GetKern(); + rInf.SetKern( CheckKerning() ); + SV_STAT( nGetTextSize ); + if ( !IsCaseMap() ) + nCursor = pLastFont->GetModelPositionForViewPoint( rInf ); + else + { + rInf.SetText( CalcCaseMap( rInf.GetText() ) ); + nCursor = pLastFont->GetModelPositionForViewPoint( rInf ); + } + rInf.SetKern( nOldKern ); + rInf.SetText(oldText); + } + return nCursor; +} + +void SwSubFont::CalcEsc( SwDrawTextInfo const & rInf, Point& rPos ) +{ + long nOfst; + + bool bVert = false; + bool bVertLRBT = false; + if (rInf.GetFrame()) + { + bVert = rInf.GetFrame()->IsVertical(); + bVertLRBT = rInf.GetFrame()->IsVertLRBT(); + } + const sal_uInt16 nDir = UnMapDirection(GetOrientation(), bVert, bVertLRBT); + + switch ( GetEscapement() ) + { + case DFLT_ESC_AUTO_SUB : + nOfst = m_nOrgHeight - m_nOrgAscent - + pLastFont->GetFontHeight( rInf.GetShell(), rInf.GetOut() ) + + pLastFont->GetFontAscent( rInf.GetShell(), rInf.GetOut() ); + + switch ( nDir ) + { + case 0 : + rPos.AdjustY(nOfst ); + break; + case 900 : + rPos.AdjustX(nOfst ); + break; + case 2700 : + rPos.AdjustX( -nOfst ); + break; + } + + break; + case DFLT_ESC_AUTO_SUPER : + nOfst = pLastFont->GetFontAscent( rInf.GetShell(), rInf.GetOut() ) - + m_nOrgAscent; + + switch ( nDir ) + { + case 0 : + rPos.AdjustY(nOfst ); + break; + case 900 : + rPos.AdjustX(nOfst ); + break; + case 2700 : + rPos.AdjustX( -nOfst ); + break; + } + + break; + default : + nOfst = (static_cast(m_nOrgHeight) * GetEscapement()) / 100; + + switch ( nDir ) + { + case 0 : + rPos.AdjustY( -nOfst ); + break; + case 900 : + rPos.AdjustX( -nOfst ); + break; + case 2700 : + rPos.AdjustX(nOfst ); + break; + } + } +} + +// used during painting of small capitals +void SwDrawTextInfo::Shift( sal_uInt16 nDir ) +{ +#ifdef DBG_UTIL + OSL_ENSURE( m_bPos, "DrawTextInfo: Undefined Position" ); + OSL_ENSURE( m_bSize, "DrawTextInfo: Undefined Width" ); +#endif + + const bool bBidiPor = ( GetFrame() && GetFrame()->IsRightToLeft() ) != + ( ComplexTextLayoutFlags::Default != ( ComplexTextLayoutFlags::BiDiRtl & GetpOut()->GetLayoutMode() ) ); + + bool bVert = false; + bool bVertLRBT = false; + if (GetFrame()) + { + bVert = GetFrame()->IsVertical(); + bVertLRBT = GetFrame()->IsVertLRBT(); + } + nDir = bBidiPor ? 1800 : UnMapDirection(nDir, bVert, bVertLRBT); + + switch ( nDir ) + { + case 0 : + m_aPos.AdjustX(GetSize().Width() ); + break; + case 900 : + OSL_ENSURE( m_aPos.Y() >= GetSize().Width(), "Going underground" ); + m_aPos.AdjustY( -(GetSize().Width()) ); + break; + case 1800 : + m_aPos.AdjustX( -(GetSize().Width()) ); + break; + case 2700 : + m_aPos.AdjustY(GetSize().Width() ); + break; + } +} + +/** + * @note Used for the "continuous underline" feature. + **/ +SwUnderlineFont::SwUnderlineFont(SwFont& rFnt, TextFrameIndex const nEnd, const Point& rPoint) + : m_aPos( rPoint ), m_nEnd( nEnd ), m_pFont( &rFnt ) +{ +}; + +SwUnderlineFont::~SwUnderlineFont() +{ +} + +/// Helper for filters to find true lineheight of a font +long AttrSetToLineHeight( const IDocumentSettingAccess& rIDocumentSettingAccess, + const SwAttrSet &rSet, + const vcl::RenderContext &rOut, sal_Int16 nScript) +{ + SwFont aFont(&rSet, &rIDocumentSettingAccess); + SwFontScript nActual; + switch (nScript) + { + default: + case i18n::ScriptType::LATIN: + nActual = SwFontScript::Latin; + break; + case i18n::ScriptType::ASIAN: + nActual = SwFontScript::CJK; + break; + case i18n::ScriptType::COMPLEX: + nActual = SwFontScript::CTL; + break; + } + aFont.SetActual(nActual); + + vcl::RenderContext &rMutableOut = const_cast(rOut); + const vcl::Font aOldFont(rMutableOut.GetFont()); + + rMutableOut.SetFont(aFont.GetActualFont()); + long nHeight = rMutableOut.GetTextHeight(); + + rMutableOut.SetFont(aOldFont); + return nHeight; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/thints.cxx b/sw/source/core/txtnode/thints.cxx new file mode 100644 index 000000000..f31e39713 --- /dev/null +++ b/sw/source/core/txtnode/thints.cxx @@ -0,0 +1,3512 @@ +/* -*- 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 +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifdef DBG_UTIL +#define CHECK Check(true); +#define CHECK_NOTMERGED Check(false); +#else +#define CHECK_NOTMERGED +#endif + +using namespace ::com::sun::star::i18n; + +SwpHints::SwpHints(const SwTextNode& rParent) + : m_rParent(rParent) + , m_pHistory(nullptr) + , m_bInSplitNode(false) + , m_bCalcHiddenParaField(false) + , m_bHiddenByParaField(false) + , m_bFootnote(false) + , m_bDDEFields(false) + , m_bStartMapNeedsSorting(false) + , m_bEndMapNeedsSorting(false) + , m_bWhichMapNeedsSorting(false) +{ +} + +static void TextAttrDelete( SwDoc & rDoc, SwTextAttr * const pAttr ) +{ + if (RES_TXTATR_META == pAttr->Which() || + RES_TXTATR_METAFIELD == pAttr->Which()) + { + static_txtattr_cast(pAttr)->ChgTextNode(nullptr); // prevents ASSERT + } + SwTextAttr::Destroy( pAttr, rDoc.GetAttrPool() ); +} + +static bool TextAttrContains(const sal_Int32 nPos, const SwTextAttrEnd * const pAttr) +{ + return (pAttr->GetStart() < nPos) && (nPos < *pAttr->End()); +} + +// a: |-----| +// b: +// |---| => valid: b before a +// |-----| => valid: start == end; b before a +// |---------| => invalid: overlap (1) +// |-----------| => valid: same end; b around a +// |-----------------| => valid: b around a +// |---| => valid; same start; b within a +// |-----| => valid; same start and end; b around or within a? +// |-----------| => valid: same start: b around a +// |-| => valid: b within a +// |---| => valid: same end; b within a +// |---------| => invalid: overlap (2) +// |-----| => valid: end == start; b after a +// |---| => valid: b after a +// ===> 2 invalid overlap cases +static +bool isOverlap(const sal_Int32 nStart1, const sal_Int32 nEnd1, + const sal_Int32 nStart2, const sal_Int32 nEnd2) +{ + return + ((nStart1 > nStart2) && (nStart1 < nEnd2) && (nEnd1 > nEnd2)) // (1) + || ((nStart1 < nStart2) && (nStart2 < nEnd1) && (nEnd1 < nEnd2)); // (2) +} + +/// #i106930#: now asymmetric: empty hint1 is _not_ nested, but empty hint2 is +static +bool isNestedAny(const sal_Int32 nStart1, const sal_Int32 nEnd1, + const sal_Int32 nStart2, const sal_Int32 nEnd2) +{ + return ((nStart1 == nStart2) || (nEnd1 == nEnd2)) + // same start/end: nested except if hint1 empty and hint2 not empty + ? (nStart1 != nEnd1) || (nStart2 == nEnd2) + : ((nStart1 < nStart2) ? (nEnd1 >= nEnd2) : (nEnd1 <= nEnd2)); +} + +static +bool isSelfNestable(const sal_uInt16 nWhich) +{ + if ((RES_TXTATR_INETFMT == nWhich) || + (RES_TXTATR_CJK_RUBY == nWhich) || + (RES_TXTATR_INPUTFIELD == nWhich)) + return false; + assert((RES_TXTATR_META == nWhich) || + (RES_TXTATR_METAFIELD == nWhich)); + return true; +} + +static +bool isSplittable(const sal_uInt16 nWhich) +{ + if ((RES_TXTATR_INETFMT == nWhich) || + (RES_TXTATR_CJK_RUBY == nWhich)) + return true; + assert((RES_TXTATR_META == nWhich) || + (RES_TXTATR_METAFIELD == nWhich) || + (RES_TXTATR_INPUTFIELD == nWhich)); + return false; +} + +namespace { + +enum Split_t { FAIL, SPLIT_NEW, SPLIT_OTHER }; + +} + +/** + Calculate splitting policy for overlapping hints, based on what kind of + hint is inserted, and what kind of existing hint overlaps. + */ +static Split_t +splitPolicy(const sal_uInt16 nWhichNew, const sal_uInt16 nWhichOther) +{ + if (!isSplittable(nWhichOther)) + { + if (!isSplittable(nWhichNew)) + return FAIL; + else + return SPLIT_NEW; + } + else + { + if ( RES_TXTATR_INPUTFIELD == nWhichNew ) + return FAIL; + else if ( (RES_TXTATR_INETFMT == nWhichNew) && + (RES_TXTATR_CJK_RUBY == nWhichOther) ) + return SPLIT_NEW; + else + return SPLIT_OTHER; + } +} + +void SwTextINetFormat::InitINetFormat(SwTextNode & rNode) +{ + ChgTextNode(&rNode); + SwCharFormat * const pFormat( + rNode.GetDoc()->getIDocumentStylePoolAccess().GetCharFormatFromPool(RES_POOLCHR_INET_NORMAL) ); + pFormat->Add( this ); +} + +void SwTextRuby::InitRuby(SwTextNode & rNode) +{ + ChgTextNode(&rNode); + SwCharFormat * const pFormat( + rNode.GetDoc()->getIDocumentStylePoolAccess().GetCharFormatFromPool(RES_POOLCHR_RUBYTEXT) ); + pFormat->Add( this ); +} + +/** + Create a new nesting text hint. + */ +static SwTextAttrNesting * +MakeTextAttrNesting(SwTextNode & rNode, SwTextAttrNesting & rNesting, + const sal_Int32 nStart, const sal_Int32 nEnd) +{ + SwTextAttr * const pNew( MakeTextAttr( + *rNode.GetDoc(), rNesting.GetAttr(), nStart, nEnd ) ); + switch (pNew->Which()) + { + case RES_TXTATR_INETFMT: + { + static_txtattr_cast(pNew)->InitINetFormat(rNode); + break; + } + case RES_TXTATR_CJK_RUBY: + { + static_txtattr_cast(pNew)->InitRuby(rNode); + break; + } + default: + assert(!"MakeTextAttrNesting: what the hell is that?"); + break; + } + return static_txtattr_cast(pNew); +} + +typedef std::vector NestList_t; + +static NestList_t::iterator +lcl_DoSplitImpl(NestList_t & rSplits, SwTextNode & rNode, + NestList_t::iterator const iter, sal_Int32 const nSplitPos, + bool const bSplitAtStart, bool const bOtherDummy) +{ + const sal_Int32 nStartPos( // skip other's dummy character! + (bSplitAtStart && bOtherDummy) ? nSplitPos + 1 : nSplitPos ); + SwTextAttrNesting * const pNew( MakeTextAttrNesting( + rNode, **iter, nStartPos, *(*iter)->GetEnd() ) ); + (*iter)->SetEnd(nSplitPos); + return rSplits.insert(iter + 1, pNew); +} + +static void +lcl_DoSplitNew(NestList_t & rSplits, SwTextNode & rNode, + const sal_Int32 nNewStart, + const sal_Int32 nOtherStart, const sal_Int32 nOtherEnd, bool bOtherDummy) +{ + const bool bSplitAtStart(nNewStart < nOtherStart); + const sal_Int32 nSplitPos( bSplitAtStart ? nOtherStart : nOtherEnd ); + // first find the portion that is split (not necessarily the last one!) + NestList_t::iterator const iter( + std::find_if( rSplits.begin(), rSplits.end(), + [nSplitPos](SwTextAttrEnd * const pAttr) { + return TextAttrContains(nSplitPos, pAttr); + } ) ); + if (iter != rSplits.end()) // already split here? + { + lcl_DoSplitImpl(rSplits, rNode, iter, nSplitPos, bSplitAtStart, bOtherDummy); + } +} + +/** + Insert nesting hint into the hints array. Also calls NoteInHistory. + @param rNewHint the hint to be inserted (must not overlap existing!) + */ +void SwpHints::InsertNesting(SwTextAttrNesting & rNewHint) +{ + Insert(& rNewHint); + NoteInHistory( & rNewHint, true ); +} + +/** + +The following hints correspond to well-formed XML elements in ODF: +RES_TXTATR_INETFMT, RES_TXTATR_CJK_RUBY, RES_TXTATR_META, RES_TXTATR_METAFIELD + +The writer core must ensure that these do not overlap; if they did, +the document would not be storable as ODF. + +Also, a Hyperlink must not be nested within another Hyperlink, +and a Ruby must not be nested within another Ruby. + +The ODF export in xmloff will only put a hyperlink into a ruby, never a ruby +into a hyperlink. + +Unfortunately the UNO API for Hyperlink and Ruby consists of the properties +Hyperlink* and Ruby* of the css.text.CharacterProperties service. In other +words, they are treated as formatting attributes, not as content entities. +Furthermore, for API users it is not possible to easily test whether a certain +range would be overlapping with other nested attributes, and most importantly, +which ones, so we can hardly refuse to insert these in cases of +overlap. + +It is possible to split Hyperlink and Ruby into multiple portions, such that +the result is properly nested. + +meta and meta-field must not be split, because they have xml:id. + +These constraints result in the following design: + +RES_TXTATR_INETFMT: + always succeeds + inserts n attributes split at RES_TXTATR_CJK_RUBY, RES_TXTATR_META, + RES_TXTATR_METAFIELD + may replace existing RES_TXTATR_INETFMT at overlap +RES_TXTATR_CJK_RUBY: + always succeeds + inserts n attributes split at RES_TXTATR_META, RES_TXTATR_METAFIELD + may replace existing RES_TXTATR_CJK_RUBY at overlap + may split existing overlapping RES_TXTATR_INETFMT +RES_TXTATR_META: + may fail if overlapping existing RES_TXTATR_META/RES_TXTATR_METAFIELD + may split existing overlapping RES_TXTATR_INETFMT or RES_TXTATR_CJK_RUBY + inserts 1 attribute +RES_TXTATR_METAFIELD: + may fail if overlapping existing RES_TXTATR_META/RES_TXTATR_METAFIELD + may split existing overlapping RES_TXTATR_INETFMT or RES_TXTATR_CJK_RUBY + inserts 1 attribute + +The nesting is expressed by the position of the hints. +RES_TXTATR_META and RES_TXTATR_METAFIELD have a CH_TXTATR, and there can +only be one such hint starting and ending at a given position. +Only RES_TXTATR_INETFMT and RES_TXTATR_CJK_RUBY lack a CH_TXTATR. +The interpretation given is that RES_TXTATR_CJK_RUBY is always around +a RES_TXTATR_INETFMT at the same start and end position (which corresponds +with the UNO API). +Both of these are always around a nesting hint with CH_TXTATR at the same +start and end position (if they should be inside, then the start should be +after the CH_TXTATR). +It would probably be a bad idea to add another nesting hint without +CH_TXTATR; on the other hand, it would be difficult adding a CH_TXTATR to +RES_TXTATR_INETFMT and RES_TXTATR_CJK_RUBY, due to the overwriting and +splitting of existing hints that is necessary for backward compatibility. + + @param rNode the text node + @param rHint the hint to be inserted + @returns true iff hint was successfully inserted +*/ +bool +SwpHints::TryInsertNesting( SwTextNode & rNode, SwTextAttrNesting & rNewHint ) +{ +// INVARIANT: the nestable hints in the array are properly nested + const sal_uInt16 nNewWhich( rNewHint.Which() ); + const sal_Int32 nNewStart( rNewHint.GetStart() ); + const sal_Int32 nNewEnd ( *rNewHint.GetEnd() ); + const bool bNewSelfNestable( isSelfNestable(nNewWhich) ); + + assert( (RES_TXTATR_INETFMT == nNewWhich) || + (RES_TXTATR_CJK_RUBY == nNewWhich) || + (RES_TXTATR_META == nNewWhich) || + (RES_TXTATR_METAFIELD == nNewWhich) || + (RES_TXTATR_INPUTFIELD == nNewWhich)); + + NestList_t OverlappingExisting; // existing hints to be split + NestList_t OverwrittenExisting; // existing hints to be replaced + NestList_t SplitNew; // new hints to be inserted + + SplitNew.push_back(& rNewHint); + + // pass 1: split the inserted hint into fragments if necessary + for ( size_t i = 0; i < Count(); ++i ) + { + SwTextAttr * const pOther = GetSortedByEnd(i); + + if (pOther->IsNesting()) + { + const sal_uInt16 nOtherWhich( pOther->Which() ); + const sal_Int32 nOtherStart( pOther->GetStart() ); + const sal_Int32 nOtherEnd ( *pOther->GetEnd() ); + if (isOverlap(nNewStart, nNewEnd, nOtherStart, nOtherEnd )) + { + switch (splitPolicy(nNewWhich, nOtherWhich)) + { + case FAIL: + SAL_INFO("sw.core", "cannot insert hint: overlap"); + for (const auto& aSplit : SplitNew) + TextAttrDelete(*rNode.GetDoc(), aSplit); + return false; + case SPLIT_NEW: + lcl_DoSplitNew(SplitNew, rNode, nNewStart, + nOtherStart, nOtherEnd, pOther->HasDummyChar()); + break; + case SPLIT_OTHER: + OverlappingExisting.push_back( + static_txtattr_cast(pOther)); + break; + default: + assert(!"bad code monkey"); + break; + } + } + else if (isNestedAny(nNewStart, nNewEnd, nOtherStart, nOtherEnd)) + { + if (!bNewSelfNestable && (nNewWhich == nOtherWhich)) + { + // ruby and hyperlink: if there is nesting, _overwrite_ + OverwrittenExisting.push_back( + static_txtattr_cast(pOther)); + } + else if ((nNewStart == nOtherStart) && pOther->HasDummyChar()) + { + if (rNewHint.HasDummyChar()) + { + assert(!"ERROR: inserting duplicate CH_TXTATR hint"); + return false; + } else if (nNewEnd < nOtherEnd) { + // other has dummy char, new is inside other, but + // new contains the other's dummy char? + // should be corrected because it may lead to problems + // in SwXMeta::createEnumeration + // SplitNew is sorted, so this is the first split + assert(SplitNew.front()->GetStart() == nNewStart); + SplitNew.front()->SetStart(nNewStart + 1); + } + } + } + } + } + + // pass 1b: tragically need to check for fieldmarks here too + for (auto iter = SplitNew.begin(); iter != SplitNew.end(); ++iter) + { + SwPaM const temp(rNode, (*iter)->GetStart(), rNode, *(*iter)->GetEnd()); + std::vector> Breaks; + sw::CalcBreaks(Breaks, temp, true); + if (!Breaks.empty()) + { + if (!isSplittable(nNewWhich)) + { + SAL_INFO("sw.core", "cannot insert hint: fieldmark overlap"); + assert(SplitNew.size() == 1); + TextAttrDelete(*rNode.GetDoc(), &rNewHint); + return false; + } + else + { + for (auto const& rPos : Breaks) + { + assert(rPos.first == rNode.GetIndex()); + iter = lcl_DoSplitImpl(SplitNew, rNode, iter, + rPos.second, true, true); + } + } + } + } + + assert((isSplittable(nNewWhich) || SplitNew.size() == 1) && + "splitting the unsplittable ???"); + + // pass 2: split existing hints that overlap/nest with new hint + // do not iterate over hints array, but over remembered set of overlapping + // hints, to keep things simple w.r.t. insertion/removal + // N.B: if there is a hint that splits the inserted hint, then + // that hint would also have already split any hint in OverlappingExisting + // so any hint in OverlappingExisting can be split at most by one hint + // in SplitNew, or even not at all (this is not true for existing hints + // that go _around_ new hint, which is the reason d'^etre for pass 4) + for (auto& rpOther : OverlappingExisting) + { + const sal_Int32 nOtherStart( rpOther->GetStart() ); + const sal_Int32 nOtherEnd ( *rpOther->GetEnd() ); + + for (const auto& rpNew : SplitNew) + { + const sal_Int32 nSplitNewStart( rpNew->GetStart() ); + const sal_Int32 nSplitNewEnd ( *rpNew->GetEnd() ); + // 4 cases: within, around, overlap l, overlap r, (OTHER: no action) + const bool bRemoveOverlap( + !bNewSelfNestable && (nNewWhich == rpOther->Which()) ); + + switch (ComparePosition(nSplitNewStart, nSplitNewEnd, + nOtherStart, nOtherEnd)) + { + case SwComparePosition::Inside: + { + assert(!bRemoveOverlap && + "this one should be in OverwrittenExisting?"); + } + break; + case SwComparePosition::Outside: + case SwComparePosition::Equal: + { + assert(!"existing hint inside new hint: why?"); + } + break; + case SwComparePosition::OverlapBefore: + { + Delete( rpOther ); // this also does NoteInHistory! + rpOther->SetStart(nSplitNewEnd); + InsertNesting( *rpOther ); + if (!bRemoveOverlap) + { + if ( MAX_HINTS <= Count() ) + { + SAL_INFO("sw.core", "hints array full :-("); + return false; + } + SwTextAttrNesting * const pOtherLeft( + MakeTextAttrNesting( rNode, *rpOther, + nOtherStart, nSplitNewEnd ) ); + InsertNesting( *pOtherLeft ); + } + } + break; + case SwComparePosition::OverlapBehind: + { + Delete( rpOther ); // this also does NoteInHistory! + rpOther->SetEnd(nSplitNewStart); + InsertNesting( *rpOther ); + if (!bRemoveOverlap) + { + if ( MAX_HINTS <= Count() ) + { + SAL_INFO("sw.core", "hints array full :-("); + return false; + } + SwTextAttrNesting * const pOtherRight( + MakeTextAttrNesting( rNode, *rpOther, + nSplitNewStart, nOtherEnd ) ); + InsertNesting( *pOtherRight ); + } + } + break; + default: + break; // overlap resolved by splitting new: nothing to do + } + } + } + + if ( MAX_HINTS <= Count() || MAX_HINTS - Count() <= SplitNew.size() ) + { + SAL_INFO("sw.core", "hints array full :-("); + return false; + } + + // pass 3: insert new hints + for (const auto& rpHint : SplitNew) + { + InsertNesting(*rpHint); + } + + // pass 4: handle overwritten hints + // RES_TXTATR_INETFMT and RES_TXTATR_CJK_RUBY should displace attributes + // of the same kind. + for (auto& rpOther : OverwrittenExisting) + { + const sal_Int32 nOtherStart( rpOther->GetStart() ); + const sal_Int32 nOtherEnd ( *rpOther->GetEnd() ); + + // overwritten portion is given by start/end of inserted hint + if ((nNewStart <= nOtherStart) && (nOtherEnd <= nNewEnd)) + { + Delete(rpOther); + rNode.DestroyAttr( rpOther ); + } + else + { + assert((nOtherStart < nNewStart) || (nNewEnd < nOtherEnd)); + // scenario: there is a RUBY, and contained within that a META; + // now a RUBY is inserted within the META => the existing RUBY is split: + // here it is not possible to simply insert the left/right fragment + // of the existing RUBY because they overlap with the META! + Delete( rpOther ); // this also does NoteInHistory! + if (nNewEnd < nOtherEnd) + { + SwTextAttrNesting * const pOtherRight( + MakeTextAttrNesting( + rNode, *rpOther, nNewEnd, nOtherEnd ) ); + bool const bSuccess( TryInsertNesting(rNode, *pOtherRight) ); + SAL_WARN_IF(!bSuccess, "sw.core", "recursive call 1 failed?"); + } + if (nOtherStart < nNewStart) + { + rpOther->SetEnd(nNewStart); + bool const bSuccess( TryInsertNesting(rNode, *rpOther) ); + SAL_WARN_IF(!bSuccess, "sw.core", "recursive call 2 failed?"); + } + else + { + rNode.DestroyAttr(rpOther); + } + } + } + + return true; +} + +// This function takes care for the following text attribute: +// RES_TXTATR_CHARFMT, RES_TXTATR_AUTOFMT +// These attributes have to be handled in a special way (Portion building). + +// The new attribute will be split by any existing RES_TXTATR_AUTOFMT or +// RES_TXTATR_CHARFMT. The new attribute itself will +// split any existing RES_TXTATR_AUTOFMT or RES_TXTATR_CHARFMT. + +void SwpHints::BuildPortions( SwTextNode& rNode, SwTextAttr& rNewHint, + const SetAttrMode nMode ) +{ + const sal_uInt16 nWhich = rNewHint.Which(); + + const sal_Int32 nThisStart = rNewHint.GetStart(); + const sal_Int32 nThisEnd = *rNewHint.GetEnd(); + const bool bNoLengthAttribute = nThisStart == nThisEnd; + + std::vector aInsDelHints; + + assert( RES_TXTATR_CHARFMT == rNewHint.Which() || + RES_TXTATR_AUTOFMT == rNewHint.Which() ); + + // 2. Find the hints which cover the start and end position + // of the new hint. These hints have to be split into two portions: + + if ( !bNoLengthAttribute ) // nothing to do for no length attributes + { + for ( size_t i = 0; i < Count(); ++i ) + { + // we're modifying stuff here which affects the sorting, and we + // don't want it changing underneath us + SwTextAttr* pOther = GetWithoutResorting(i); + + if ( RES_TXTATR_CHARFMT != pOther->Which() && + RES_TXTATR_AUTOFMT != pOther->Which() ) + continue; + + sal_Int32 nOtherStart = pOther->GetStart(); + const sal_Int32 nOtherEnd = *pOther->GetEnd(); + + // Check if start of new attribute overlaps with pOther: + // Split pOther if necessary: + if ( nOtherStart < nThisStart && nThisStart < nOtherEnd ) + { + SwTextAttr* pNewAttr = MakeTextAttr( *rNode.GetDoc(), + pOther->GetAttr(), nOtherStart, nThisStart ); + if ( RES_TXTATR_CHARFMT == pOther->Which() ) + { + static_txtattr_cast(pNewAttr)->SetSortNumber( + static_txtattr_cast(pOther)->GetSortNumber() ); + } + aInsDelHints.push_back( pNewAttr ); + + NoteInHistory( pOther ); + pOther->SetStart(nThisStart); + NoteInHistory( pOther, true ); + + nOtherStart = nThisStart; + } + + // Check if end of new attribute overlaps with pOther: + // Split pOther if necessary: + if ( nOtherStart < nThisEnd && nThisEnd < nOtherEnd ) + { + SwTextAttr* pNewAttr = MakeTextAttr( *rNode.GetDoc(), + pOther->GetAttr(), nOtherStart, nThisEnd ); + if ( RES_TXTATR_CHARFMT == pOther->Which() ) + { + static_txtattr_cast(pNewAttr)->SetSortNumber( + static_txtattr_cast(pOther)->GetSortNumber()); + } + aInsDelHints.push_back( pNewAttr ); + + NoteInHistory( pOther ); + pOther->SetStart(nThisEnd); + NoteInHistory( pOther, true ); + } + } + + // Insert the newly created attributes: + for ( const auto& rpHint : aInsDelHints ) + { + Insert( rpHint ); + NoteInHistory( rpHint, true ); + } + } + +#ifdef DBG_UTIL + if( !rNode.GetDoc()->IsInReading() ) + CHECK_NOTMERGED; // ignore flags not set properly yet, don't check them +#endif + + // 4. Split rNewHint into 1 ... n new hints: + + std::set aBounds; + aBounds.insert( nThisStart ); + aBounds.insert( nThisEnd ); + + if ( !bNoLengthAttribute ) // nothing to do for no length attributes + { + for ( size_t i = 0; i < Count(); ++i ) + { + const SwTextAttr* pOther = Get(i); + + if ( RES_TXTATR_CHARFMT != pOther->Which() && + RES_TXTATR_AUTOFMT != pOther->Which() ) + continue; + + const sal_Int32 nOtherStart = pOther->GetStart(); + const sal_Int32 nOtherEnd = *pOther->End(); + + aBounds.insert( nOtherStart ); + aBounds.insert( nOtherEnd ); + } + } + + std::set::iterator aStartIter = aBounds.lower_bound( nThisStart ); + std::set::iterator aEndIter = aBounds.upper_bound( nThisEnd ); + sal_Int32 nPorStart = *aStartIter; + ++aStartIter; + bool bDestroyHint = true; + + // Insert the 1...n new parts of the new attribute: + + while ( aStartIter != aEndIter || bNoLengthAttribute ) + { + OSL_ENSURE( bNoLengthAttribute || nPorStart < *aStartIter, "AUTOSTYLES: BuildPortion trouble" ); + + const sal_Int32 nPorEnd = bNoLengthAttribute ? nPorStart : *aStartIter; + aInsDelHints.clear(); + + // Get all hints that are in [nPorStart, nPorEnd[: + for ( size_t i = 0; i < Count(); ++i ) + { + // we get called from TryInsertHint, which changes ordering + SwTextAttr *pOther = GetWithoutResorting(i); + + if ( RES_TXTATR_CHARFMT != pOther->Which() && + RES_TXTATR_AUTOFMT != pOther->Which() ) + continue; + + const sal_Int32 nOtherStart = pOther->GetStart(); + + if ( nOtherStart > nPorStart ) + break; + + if ( pOther->GetEnd() && *pOther->GetEnd() == nPorEnd && nOtherStart == nPorStart ) + { + OSL_ENSURE( *pOther->GetEnd() == nPorEnd, "AUTOSTYLES: BuildPortion trouble" ); + aInsDelHints.push_back( pOther ); + } + } + + SwTextAttr* pNewAttr = nullptr; + if ( RES_TXTATR_CHARFMT == nWhich ) + { + // pNewHint can be inserted after calculating the sort value. + // This should ensure, that pNewHint comes behind the already present + // character style + sal_uInt16 nCharStyleCount = 0; + for ( const auto& rpHint : aInsDelHints ) + { + if ( RES_TXTATR_CHARFMT == rpHint->Which() ) + { + // #i74589# + const SwFormatCharFormat& rOtherCharFormat = rpHint->GetCharFormat(); + const SwFormatCharFormat& rThisCharFormat = rNewHint.GetCharFormat(); + const bool bSameCharFormat = rOtherCharFormat.GetCharFormat() == rThisCharFormat.GetCharFormat(); + + // #i90311# + // Do not remove existing character format hint during XML import + if ( !rNode.GetDoc()->IsInXMLImport() && + ( !( SetAttrMode::DONTREPLACE & nMode ) || + bNoLengthAttribute || + bSameCharFormat ) ) + { + // Remove old hint + Delete( rpHint ); + rNode.DestroyAttr( rpHint ); + } + else + ++nCharStyleCount; + } + else + { + // remove all attributes from auto styles, which are explicitly set in + // the new character format: + OSL_ENSURE( RES_TXTATR_AUTOFMT == rpHint->Which(), "AUTOSTYLES - Misc trouble" ); + SwTextAttr* pOther = rpHint; + std::shared_ptr pOldStyle = static_cast(pOther->GetAttr()).GetStyleHandle(); + + // For each attribute in the automatic style check if it + // is also set the new character style: + SfxItemSet aNewSet( *pOldStyle->GetPool(), + aCharAutoFormatSetRange); + SfxItemIter aItemIter( *pOldStyle ); + const SfxPoolItem* pItem = aItemIter.GetCurItem(); + do + { + if ( !CharFormat::IsItemIncluded( pItem->Which(), &rNewHint ) ) + { + aNewSet.Put( *pItem ); + } + + pItem = aItemIter.NextItem(); + } while (pItem); + + // Remove old hint + Delete( pOther ); + rNode.DestroyAttr( pOther ); + + // Create new AutoStyle + if ( aNewSet.Count() ) + { + pNewAttr = MakeTextAttr( *rNode.GetDoc(), + aNewSet, nPorStart, nPorEnd ); + Insert( pNewAttr ); + NoteInHistory( pNewAttr, true ); + } + } + } + + // If there is no current hint and start and end of rNewHint + // is ok, we do not need to create a new txtattr. + if ( nPorStart == nThisStart && + nPorEnd == nThisEnd && + !nCharStyleCount ) + { + pNewAttr = &rNewHint; + bDestroyHint = false; + } + else + { + pNewAttr = MakeTextAttr( *rNode.GetDoc(), rNewHint.GetAttr(), + nPorStart, nPorEnd ); + static_txtattr_cast(pNewAttr)->SetSortNumber(nCharStyleCount); + } + } + else + { + // Find the current autostyle. Mix attributes if necessary. + SwTextAttr* pCurrentAutoStyle = nullptr; + SwTextAttr* pCurrentCharFormat = nullptr; + for ( const auto& rpHint : aInsDelHints ) + { + if ( RES_TXTATR_AUTOFMT == rpHint->Which() ) + pCurrentAutoStyle = rpHint; + else if ( RES_TXTATR_CHARFMT == rpHint->Which() ) + pCurrentCharFormat = rpHint; + } + + std::shared_ptr pNewStyle = static_cast(rNewHint.GetAttr()).GetStyleHandle(); + if ( pCurrentAutoStyle ) + { + std::shared_ptr pCurrentStyle = static_cast(pCurrentAutoStyle->GetAttr()).GetStyleHandle(); + + // Merge attributes + SfxItemSet aNewSet( *pCurrentStyle ); + aNewSet.Put( *pNewStyle ); + + // #i75750# Remove attributes already set at whole paragraph + // #i81764# This should not be applied for no length attributes!!! <-- + if ( !bNoLengthAttribute && rNode.HasSwAttrSet() && aNewSet.Count() ) + { + SfxItemIter aIter2( aNewSet ); + const SfxPoolItem* pItem = aIter2.GetCurItem(); + const SfxItemSet& rWholeParaAttrSet = rNode.GetSwAttrSet(); + + do + { + const SfxPoolItem* pTmpItem = nullptr; + if ( SfxItemState::SET == rWholeParaAttrSet.GetItemState( pItem->Which(), false, &pTmpItem ) && + pTmpItem == pItem ) + { + // Do not clear item if the attribute is set in a character format: + if ( !pCurrentCharFormat || nullptr == CharFormat::GetItem( *pCurrentCharFormat, pItem->Which() ) ) + aNewSet.ClearItem( pItem->Which() ); + } + } + while ((pItem = aIter2.NextItem())); + } + + // Remove old hint + Delete( pCurrentAutoStyle ); + rNode.DestroyAttr( pCurrentAutoStyle ); + + // Create new AutoStyle + if ( aNewSet.Count() ) + pNewAttr = MakeTextAttr( *rNode.GetDoc(), aNewSet, + nPorStart, nPorEnd ); + } + else + { + // Remove any attributes which are already set at the whole paragraph: + bool bOptimizeAllowed = true; + + // #i75750# Remove attributes already set at whole paragraph + // #i81764# This should not be applied for no length attributes!!! <-- + if ( !bNoLengthAttribute && rNode.HasSwAttrSet() && pNewStyle->Count() ) + { + std::unique_ptr pNewSet; + + SfxItemIter aIter2( *pNewStyle ); + const SfxPoolItem* pItem = aIter2.GetCurItem(); + const SfxItemSet& rWholeParaAttrSet = rNode.GetSwAttrSet(); + + do + { + const SfxPoolItem* pTmpItem = nullptr; + if ( SfxItemState::SET == rWholeParaAttrSet.GetItemState( pItem->Which(), false, &pTmpItem ) && + pTmpItem == pItem ) + { + // Do not clear item if the attribute is set in a character format: + if ( !pCurrentCharFormat || nullptr == CharFormat::GetItem( *pCurrentCharFormat, pItem->Which() ) ) + { + if ( !pNewSet ) + pNewSet = pNewStyle->Clone(); + pNewSet->ClearItem( pItem->Which() ); + } + } + } + while ((pItem = aIter2.NextItem())); + + if ( pNewSet ) + { + bOptimizeAllowed = false; + if ( pNewSet->Count() ) + pNewStyle = rNode.getIDocumentStyleAccess().getAutomaticStyle( *pNewSet, IStyleAccess::AUTO_STYLE_CHAR ); + else + pNewStyle.reset(); + } + } + + // Create new AutoStyle + // If there is no current hint and start and end of rNewHint + // is ok, we do not need to create a new txtattr. + if ( bOptimizeAllowed && + nPorStart == nThisStart && + nPorEnd == nThisEnd ) + { + pNewAttr = &rNewHint; + bDestroyHint = false; + } + else if ( pNewStyle ) + { + pNewAttr = MakeTextAttr( *rNode.GetDoc(), *pNewStyle, + nPorStart, nPorEnd ); + } + } + } + + if ( pNewAttr ) + { + Insert( pNewAttr ); +// if ( bDestroyHint ) + NoteInHistory( pNewAttr, true ); + } + + if ( !bNoLengthAttribute ) + { + nPorStart = *aStartIter; + ++aStartIter; + } + else + break; + } + + if ( bDestroyHint ) + rNode.DestroyAttr( &rNewHint ); +} + +SwTextAttr* MakeRedlineTextAttr( SwDoc & rDoc, SfxPoolItem const & rAttr ) +{ + // this is intended _only_ for special-purpose redline attributes! + switch (rAttr.Which()) + { + case RES_CHRATR_COLOR: + case RES_CHRATR_WEIGHT: + case RES_CHRATR_CJK_WEIGHT: + case RES_CHRATR_CTL_WEIGHT: + case RES_CHRATR_POSTURE: + case RES_CHRATR_CJK_POSTURE: + case RES_CHRATR_CTL_POSTURE: + case RES_CHRATR_UNDERLINE: + case RES_CHRATR_CROSSEDOUT: + case RES_CHRATR_CASEMAP: + case RES_CHRATR_BACKGROUND: + break; + default: + assert(!"unsupported redline attribute"); + break; + } + + // Put new attribute into pool + // FIXME: this const_cast is evil! + SfxPoolItem& rNew = + const_cast( rDoc.GetAttrPool().Put( rAttr ) ); + return new SwTextAttrEnd( rNew, 0, 0 ); +} + +// create new text attribute +SwTextAttr* MakeTextAttr( + SwDoc & rDoc, + SfxPoolItem& rAttr, + sal_Int32 const nStt, + sal_Int32 const nEnd, + CopyOrNewType const bIsCopy, + SwTextNode *const pTextNode ) +{ + if ( isCHRATR(rAttr.Which()) ) + { + // Somebody wants to build a SwTextAttr for a character attribute. + // Sorry, this is not allowed any longer. + // You'll get a brand new autostyle attribute: + SfxItemSet aItemSet( rDoc.GetAttrPool(), + svl::Items{} ); + aItemSet.Put( rAttr ); + return MakeTextAttr( rDoc, aItemSet, nStt, nEnd ); + } + else if ( RES_TXTATR_AUTOFMT == rAttr.Which() && + static_cast(rAttr).GetStyleHandle()-> + GetPool() != &rDoc.GetAttrPool() ) + { + // If the attribute is an auto-style which refers to a pool that is + // different from rDoc's pool, we have to correct this: + const std::shared_ptr pAutoStyle = static_cast(rAttr).GetStyleHandle(); + std::unique_ptr pNewSet( + pAutoStyle->SfxItemSet::Clone( true, &rDoc.GetAttrPool() )); + SwTextAttr* pNew = MakeTextAttr( rDoc, *pNewSet, nStt, nEnd ); + return pNew; + } + + // Put new attribute into pool + // FIXME: this const_cast is evil! + SfxPoolItem& rNew = + const_cast( rDoc.GetAttrPool().Put( rAttr ) ); + + SwTextAttr* pNew = nullptr; + switch( rNew.Which() ) + { + case RES_TXTATR_CHARFMT: + { + SwFormatCharFormat &rFormatCharFormat = static_cast(rNew); + if( !rFormatCharFormat.GetCharFormat() ) + { + rFormatCharFormat.SetCharFormat( rDoc.GetDfltCharFormat() ); + } + + pNew = new SwTextCharFormat( rFormatCharFormat, nStt, nEnd ); + } + break; + case RES_TXTATR_INETFMT: + pNew = new SwTextINetFormat( static_cast(rNew), nStt, nEnd ); + break; + + case RES_TXTATR_FIELD: + pNew = new SwTextField( static_cast(rNew), nStt, + rDoc.IsClipBoard() ); + break; + + case RES_TXTATR_ANNOTATION: + { + pNew = new SwTextAnnotationField( static_cast(rNew), nStt, rDoc.IsClipBoard() ); + if (bIsCopy == CopyOrNewType::Copy) + { + // On copy of the annotation field do not keep the annotated text range by removing + // the relation to its annotation mark (relation established via annotation field's name). + // If the annotation mark is also copied, the relation and thus the annotated text range will be reestablished, + // when the annotation mark is created and inserted into the document. + const_cast(dynamic_cast(*(pNew->GetFormatField().GetField()))).SetName(OUString()); + } + } + break; + + case RES_TXTATR_INPUTFIELD: + pNew = new SwTextInputField( static_cast(rNew), nStt, nEnd, + rDoc.IsClipBoard() ); + break; + + case RES_TXTATR_FLYCNT: + { + // finally, copy the frame format (with content) + pNew = new SwTextFlyCnt( static_cast(rNew), nStt ); + if ( static_cast(rAttr).GetTextFlyCnt() ) + { + // if it has an existing attr then the format must be copied + static_cast(pNew)->CopyFlyFormat( &rDoc ); + } + } + break; + case RES_TXTATR_FTN: + pNew = new SwTextFootnote( static_cast(rNew), nStt ); + // copy note's SeqNo + if( static_cast(rAttr).GetTextFootnote() ) + static_cast(pNew)->SetSeqNo( static_cast(rAttr).GetTextFootnote()->GetSeqRefNo() ); + break; + case RES_TXTATR_REFMARK: + pNew = nStt == nEnd + ? new SwTextRefMark( static_cast(rNew), nStt ) + : new SwTextRefMark( static_cast(rNew), nStt, &nEnd ); + break; + case RES_TXTATR_TOXMARK: + { + SwTOXMark& rMark = static_cast(rNew); + + // tdf#98868 if the SwTOXType is from a different document that the + // target, re-register the TOXMark against a matching SwTOXType from + // the target document instead + const SwTOXType* pTOXType = rMark.GetTOXType(); + if (pTOXType && &pTOXType->GetDoc() != &rDoc) + { + SwTOXType* pToxType = SwHistorySetTOXMark::GetSwTOXType(rDoc, pTOXType->GetType(), + pTOXType->GetTypeName()); + rMark.RegisterToTOXType(*pToxType); + } + + pNew = new SwTextTOXMark(rMark, nStt, &nEnd); + break; + } + case RES_TXTATR_CJK_RUBY: + pNew = new SwTextRuby( static_cast(rNew), nStt, nEnd ); + break; + case RES_TXTATR_META: + case RES_TXTATR_METAFIELD: + pNew = SwTextMeta::CreateTextMeta( rDoc.GetMetaFieldManager(), pTextNode, + static_cast(rNew), nStt, nEnd, bIsCopy == CopyOrNewType::Copy ); + break; + default: + assert(RES_TXTATR_AUTOFMT == rNew.Which()); + pNew = new SwTextAttrEnd( rNew, nStt, nEnd ); + break; + } + + return pNew; +} + +SwTextAttr* MakeTextAttr( SwDoc & rDoc, const SfxItemSet& rSet, + sal_Int32 nStt, sal_Int32 nEnd ) +{ + IStyleAccess& rStyleAccess = rDoc.GetIStyleAccess(); + const std::shared_ptr pAutoStyle = rStyleAccess.getAutomaticStyle( rSet, IStyleAccess::AUTO_STYLE_CHAR ); + SwFormatAutoFormat aNewAutoFormat; + aNewAutoFormat.SetStyleHandle( pAutoStyle ); + SwTextAttr* pNew = MakeTextAttr( rDoc, aNewAutoFormat, nStt, nEnd ); + return pNew; +} + +// delete the text attribute and unregister its item at the pool +void SwTextNode::DestroyAttr( SwTextAttr* pAttr ) +{ + if( pAttr ) + { + // some things need to be done before deleting the formatting attribute + SwDoc* pDoc = GetDoc(); + switch( pAttr->Which() ) + { + case RES_TXTATR_FLYCNT: + { + SwFrameFormat* pFormat = pAttr->GetFlyCnt().GetFrameFormat(); + if( pFormat ) // set to 0 by Undo? + pDoc->getIDocumentLayoutAccess().DelLayoutFormat( pFormat ); + } + break; + + case RES_CHRATR_HIDDEN: + SetCalcHiddenCharFlags(); + break; + + case RES_TXTATR_FTN: + static_cast(pAttr)->SetStartNode( nullptr ); + static_cast(pAttr->GetAttr()).InvalidateFootnote(); + break; + + case RES_TXTATR_FIELD: + case RES_TXTATR_ANNOTATION: + case RES_TXTATR_INPUTFIELD: + if( !pDoc->IsInDtor() ) + { + SwTextField *const pTextField(static_txtattr_cast(pAttr)); + SwFieldType* pFieldType = pAttr->GetFormatField().GetField()->GetTyp(); + + //JP 06-08-95: DDE-fields are an exception + assert(SwFieldIds::Dde == pFieldType->Which() || + this == pTextField->GetpTextNode()); + + // certain fields must update the SwDoc's calculation flags + + // Certain fields (like HiddenParaField) must trigger recalculation of visible flag + if (GetDoc()->FieldCanHideParaWeight(pFieldType->Which())) + SetCalcHiddenParaField(); + + switch( pFieldType->Which() ) + { + case SwFieldIds::HiddenPara: + case SwFieldIds::DbSetNumber: + case SwFieldIds::GetExp: + case SwFieldIds::Database: + case SwFieldIds::SetExp: + case SwFieldIds::HiddenText: + case SwFieldIds::DbNumSet: + case SwFieldIds::DbNextSet: + if( !pDoc->getIDocumentFieldsAccess().IsNewFieldLst() && GetNodes().IsDocNodes() ) + pDoc->getIDocumentFieldsAccess().InsDelFieldInFieldLst(false, *pTextField); + break; + case SwFieldIds::Dde: + if (GetNodes().IsDocNodes() && pTextField->GetpTextNode()) + static_cast(pFieldType)->DecRefCnt(); + break; + case SwFieldIds::Postit: + { + const_cast(pAttr->GetFormatField()).Broadcast( + SwFormatFieldHint(&pTextField->GetFormatField(), SwFormatFieldHintWhich::REMOVED)); + break; + } + default: break; + } + } + static_cast(pAttr->GetAttr()).InvalidateField(); + break; + + case RES_TXTATR_TOXMARK: + static_cast(pAttr->GetAttr()).InvalidateTOXMark(); + break; + + case RES_TXTATR_REFMARK: + static_cast(pAttr->GetAttr()).InvalidateRefMark(); + break; + + case RES_TXTATR_META: + case RES_TXTATR_METAFIELD: + { + auto pTextMeta = static_txtattr_cast(pAttr); + SwFormatMeta & rFormatMeta( static_cast(pTextMeta->GetAttr()) ); + if (::sw::Meta* pMeta = rFormatMeta.GetMeta()) + { + if (SwDocShell* pDocSh = pDoc->GetDocShell()) + { + static const OUString metaNS("urn:bails"); + const css::uno::Reference xSubject = pMeta->MakeUnoObject(); + uno::Reference xModel = pDocSh->GetBaseModel(); + SwRDFHelper::clearStatements(xModel, metaNS, xSubject); + } + } + + static_txtattr_cast(pAttr)->ChgTextNode(nullptr); + } + break; + + default: + break; + } + + SwTextAttr::Destroy( pAttr, pDoc->GetAttrPool() ); + } +} + +SwTextAttr* SwTextNode::InsertItem( + SfxPoolItem& rAttr, + const sal_Int32 nStart, + const sal_Int32 nEnd, + const SetAttrMode nMode ) +{ + // character attributes will be inserted as automatic styles: + assert( !isCHRATR(rAttr.Which()) && "AUTOSTYLES - " + "SwTextNode::InsertItem should not be called with character attributes"); + + SwTextAttr *const pNew = + MakeTextAttr( + *GetDoc(), + rAttr, + nStart, + nEnd, + (nMode & SetAttrMode::IS_COPY) ? CopyOrNewType::Copy : CopyOrNewType::New, + this ); + + if ( pNew ) + { + const bool bSuccess( InsertHint( pNew, nMode ) ); + // N.B.: also check that the hint is actually in the hints array, + // because hints of certain types may be merged after successful + // insertion, and thus destroyed! + if (!bSuccess || !m_pSwpHints->Contains( pNew )) + { + return nullptr; + } + } + + return pNew; +} + +// take ownership of pAttr; if insertion fails, delete pAttr +bool SwTextNode::InsertHint( SwTextAttr * const pAttr, const SetAttrMode nMode ) +{ + bool bHiddenPara = false; + + assert(pAttr && pAttr->GetStart() <= Len()); + assert(!pAttr->GetEnd() || (*pAttr->GetEnd() <= Len())); + + // translate from SetAttrMode to InsertMode (for hints with CH_TXTATR) + const SwInsertFlags nInsertFlags = + (nMode & SetAttrMode::NOHINTEXPAND) + ? SwInsertFlags::NOHINTEXPAND + : (nMode & SetAttrMode::FORCEHINTEXPAND) + ? (SwInsertFlags::FORCEHINTEXPAND | SwInsertFlags::EMPTYEXPAND) + : SwInsertFlags::EMPTYEXPAND; + + // need this after TryInsertHint, when pAttr may be deleted + const sal_Int32 nStart( pAttr->GetStart() ); + const bool bDummyChar( pAttr->HasDummyChar() ); + if (bDummyChar) + { + SetAttrMode nInsMode = nMode; + switch( pAttr->Which() ) + { + case RES_TXTATR_FLYCNT: + { + SwTextFlyCnt *pFly = static_cast(pAttr); + SwFrameFormat* pFormat = pAttr->GetFlyCnt().GetFrameFormat(); + if( !(SetAttrMode::NOTXTATRCHR & nInsMode) ) + { + // Need to insert char first, because SetAnchor() reads + // GetStart(). + //JP 11.05.98: if the anchor is already set correctly, + // fix it after inserting the char, so that clients don't + // have to worry about it. + const SwFormatAnchor* pAnchor = nullptr; + (void)pFormat->GetItemState( RES_ANCHOR, false, + reinterpret_cast(&pAnchor) ); + + SwIndex aIdx( this, pAttr->GetStart() ); + const OUString c(GetCharOfTextAttr(*pAttr)); + OUString const ins( InsertText(c, aIdx, nInsertFlags) ); + if (ins.isEmpty()) + { + // do not record deletion of Format! + ::sw::UndoGuard const ug( + pFormat->GetDoc()->GetIDocumentUndoRedo()); + DestroyAttr(pAttr); + return false; // text node full :( + } + nInsMode |= SetAttrMode::NOTXTATRCHR; + + if (pAnchor && + (RndStdIds::FLY_AS_CHAR == pAnchor->GetAnchorId()) && + pAnchor->GetContentAnchor() && + pAnchor->GetContentAnchor()->nNode == *this && + pAnchor->GetContentAnchor()->nContent == aIdx ) + { + --const_cast( + pAnchor->GetContentAnchor()->nContent); + } + } + pFly->SetAnchor( this ); + + // format pointer could have changed in SetAnchor, + // when copying to other docs! + pFormat = pAttr->GetFlyCnt().GetFrameFormat(); + SwDoc *pDoc = pFormat->GetDoc(); + + // OD 26.06.2003 - allow drawing objects in header/footer. + // But don't allow control objects in header/footer + if( RES_DRAWFRMFMT == pFormat->Which() && + pDoc->IsInHeaderFooter( pFormat->GetAnchor().GetContentAnchor()->nNode ) ) + { + bool bCheckControlLayer = false; + pFormat->CallSwClientNotify(sw::CheckDrawFrameFormatLayerHint(&bCheckControlLayer)); + if( bCheckControlLayer ) + { + // This should not be allowed, prevent it here. + // The dtor of the SwTextAttr does not delete the + // char, so delete it explicitly here. + if( SetAttrMode::NOTXTATRCHR & nInsMode ) + { + // delete the char from the string + assert(CH_TXTATR_BREAKWORD == m_Text[pAttr->GetStart()] + || CH_TXTATR_INWORD == m_Text[pAttr->GetStart()]); + m_Text = m_Text.replaceAt(pAttr->GetStart(), 1, ""); + // Update SwIndexes + SwIndex aTmpIdx( this, pAttr->GetStart() ); + Update( aTmpIdx, 1, true ); + } + // do not record deletion of Format! + ::sw::UndoGuard const ug(pDoc->GetIDocumentUndoRedo()); + DestroyAttr( pAttr ); + return false; + } + } + break; + } + + case RES_TXTATR_FTN : + { + // Footnotes: create text node and put it into Inserts-section + SwDoc *pDoc = GetDoc(); + SwNodes &rNodes = pDoc->GetNodes(); + + // check that footnote is inserted into body or redline section + if( StartOfSectionIndex() < rNodes.GetEndOfAutotext().GetIndex() ) + { + // This should not be allowed, prevent it here. + // The dtor of the SwTextAttr does not delete the + // char, so delete it explicitly here. + if( SetAttrMode::NOTXTATRCHR & nInsMode ) + { + // delete the char from the string + assert(CH_TXTATR_BREAKWORD == m_Text[pAttr->GetStart()] + || CH_TXTATR_INWORD == m_Text[pAttr->GetStart()]); + m_Text = m_Text.replaceAt(pAttr->GetStart(), 1, ""); + // Update SwIndexes + SwIndex aTmpIdx( this, pAttr->GetStart() ); + Update( aTmpIdx, 1, true ); + } + DestroyAttr( pAttr ); + return false; + } + + // is a new footnote being inserted? + bool bNewFootnote = nullptr == static_cast(pAttr)->GetStartNode(); + if( bNewFootnote ) + { + static_cast(pAttr)->MakeNewTextSection( GetNodes() ); + SwRegHistory* pHist = GetpSwpHints() + ? GetpSwpHints()->GetHistory() : nullptr; + if( pHist ) + pHist->ChangeNodeIndex( GetIndex() ); + } + else if ( !GetpSwpHints() || !GetpSwpHints()->IsInSplitNode() ) + { + // existing footnote: delete all layout frames of its + // footnote section + sal_uLong nSttIdx = + static_cast(pAttr)->GetStartNode()->GetIndex(); + sal_uLong nEndIdx = rNodes[ nSttIdx++ ]->EndOfSectionIndex(); + for( ; nSttIdx < nEndIdx; ++nSttIdx ) + { + SwContentNode* pCNd = rNodes[ nSttIdx ]->GetContentNode(); + if( nullptr != pCNd ) + pCNd->DelFrames(nullptr); + else if (SwTableNode *const pTable = rNodes[nSttIdx]->GetTableNode()) + { + pTable->DelFrames(); + } + } + } + + if( !(SetAttrMode::NOTXTATRCHR & nInsMode) ) + { + // must insert first, to prevent identical indexes + // that could later prevent insertion into SwDoc's + // footnote array + SwIndex aNdIdx( this, pAttr->GetStart() ); + const OUString c(GetCharOfTextAttr(*pAttr)); + OUString const ins( InsertText(c, aNdIdx, nInsertFlags) ); + if (ins.isEmpty()) + { + DestroyAttr(pAttr); + return false; // text node full :( + } + nInsMode |= SetAttrMode::NOTXTATRCHR; + } + + // insert into SwDoc's footnote index array + SwTextFootnote* pTextFootnote = nullptr; + if( !bNewFootnote ) + { + // moving an existing footnote (e.g. SplitNode) + for( size_t n = 0; n < pDoc->GetFootnoteIdxs().size(); ++n ) + if( pAttr == pDoc->GetFootnoteIdxs()[n] ) + { + // assign new index by removing and re-inserting + pTextFootnote = pDoc->GetFootnoteIdxs()[n]; + pDoc->GetFootnoteIdxs().erase( pDoc->GetFootnoteIdxs().begin() + n ); + break; + } + // if the Undo set the StartNode, the Index isn't + // in the doc's array yet! + } + if( !pTextFootnote ) + pTextFootnote = static_cast(pAttr); + + // to update the numbers and for sorting, the Node must be set + static_cast(pAttr)->ChgTextNode( this ); + + // do not insert footnote in redline section into footnote array + if( StartOfSectionIndex() > rNodes.GetEndOfRedlines().GetIndex() ) + { + const bool bSuccess = pDoc->GetFootnoteIdxs().insert(pTextFootnote).second; + OSL_ENSURE( bSuccess, "FootnoteIdx not inserted." ); + } + SwNodeIndex aTmpIndex( *this ); + pDoc->GetFootnoteIdxs().UpdateFootnote( aTmpIndex); + static_cast(pAttr)->SetSeqRefNo(); + } + break; + + case RES_TXTATR_FIELD: + { + // trigger notification for relevant fields, like HiddenParaFields + if (GetDoc()->FieldCanHideParaWeight( + pAttr->GetFormatField().GetField()->GetTyp()->Which())) + { + bHiddenPara = true; + } + } + break; + + } + // CH_TXTATR_* are inserted for SwTextHints without EndIndex + // If the caller is SwTextNode::Copy, the char has already been copied, + // and SETATTR_NOTXTATRCHR prevents inserting it again here. + if( !(SetAttrMode::NOTXTATRCHR & nInsMode) ) + { + SwIndex aIdx( this, pAttr->GetStart() ); + OUString const ins( InsertText(OUString(GetCharOfTextAttr(*pAttr)), + aIdx, nInsertFlags) ); + if (ins.isEmpty()) + { + DestroyAttr(pAttr); + return false; // text node full :( + } + + // adjust end of hint to account for inserted CH_TXTATR + const sal_Int32 * const pEnd(pAttr->GetEnd()); + if (pEnd) + { + pAttr->SetEnd(*pEnd + 1); + } + } + } + + // handle attributes which provide content + sal_Int32 nEnd = nStart; + bool bInputFieldStartCharInserted = false; + bool bInputFieldEndCharInserted = false; + const bool bHasContent( pAttr->HasContent() ); + if ( bHasContent ) + { + switch( pAttr->Which() ) + { + case RES_TXTATR_INPUTFIELD: + { + SwTextInputField* pTextInputField = dynamic_cast(pAttr); + if ( pTextInputField ) + { + if( !(SetAttrMode::NOTXTATRCHR & nMode) ) + { + SwIndex aIdx( this, pAttr->GetStart() ); + const OUString aContent = OUStringChar(CH_TXT_ATR_INPUTFIELDSTART) + + pTextInputField->GetFieldContent() + OUStringChar(CH_TXT_ATR_INPUTFIELDEND); + InsertText( aContent, aIdx, nInsertFlags ); + + const sal_Int32* const pEnd(pAttr->GetEnd()); + assert(pEnd != nullptr); + pAttr->SetEnd(*pEnd + aContent.getLength()); + nEnd = *pAttr->GetEnd(); + } + else + { + // assure that CH_TXT_ATR_INPUTFIELDSTART and CH_TXT_ATR_INPUTFIELDEND are inserted. + if ( m_Text[ pAttr->GetStart() ] != CH_TXT_ATR_INPUTFIELDSTART ) + { + SwIndex aIdx( this, pAttr->GetStart() ); + InsertText( OUString(CH_TXT_ATR_INPUTFIELDSTART), aIdx, nInsertFlags ); + bInputFieldStartCharInserted = true; + const sal_Int32* const pEnd(pAttr->GetEnd()); + assert(pEnd != nullptr); + pAttr->SetEnd(*pEnd + 1); + nEnd = *pAttr->GetEnd(); + } + + const sal_Int32* const pEnd(pAttr->GetEnd()); + assert(pEnd != nullptr); + if (m_Text[ *pEnd - 1 ] != CH_TXT_ATR_INPUTFIELDEND) + { + SwIndex aIdx( this, *pEnd ); + InsertText( OUString(CH_TXT_ATR_INPUTFIELDEND), aIdx, nInsertFlags ); + bInputFieldEndCharInserted = true; + pAttr->SetEnd(*pEnd + 1); + nEnd = *pAttr->GetEnd(); + } + } + } + } + break; + default: + break; + } + } + + GetOrCreateSwpHints(); + + // handle overlap with an existing InputField + bool bInsertHint = true; + { + const SwTextInputField* pTextInputField = GetOverlappingInputField( *pAttr ); + if ( pTextInputField != nullptr ) + { + if ( pAttr->End() == nullptr ) + { + bInsertHint = false; + } + else + { + if ( pAttr->GetStart() > pTextInputField->GetStart() ) + { + pAttr->SetStart( pTextInputField->GetStart() ); + } + if ( *(pAttr->End()) < *(pTextInputField->End()) ) + { + pAttr->SetEnd(*(pTextInputField->End())); + } + } + } + } + + const bool bRet = bInsertHint + && m_pSwpHints->TryInsertHint( pAttr, *this, nMode ); + + if ( !bRet ) + { + if ( bDummyChar + && !(SetAttrMode::NOTXTATRCHR & nMode) ) + { + // undo insertion of dummy character + // N.B. cannot insert the dummy character after inserting the hint, + // because if the hint has no extent it will be moved in InsertText, + // resulting in infinite recursion + assert((CH_TXTATR_BREAKWORD == m_Text[nStart] || + CH_TXTATR_INWORD == m_Text[nStart] )); + SwIndex aIdx( this, nStart ); + EraseText( aIdx, 1 ); + } + + if ( bHasContent ) + { + if ( !(SetAttrMode::NOTXTATRCHR & nMode) + && (nEnd - nStart) > 0 ) + { + SwIndex aIdx( this, nStart ); + EraseText( aIdx, (nEnd - nStart) ); + } + else + { + if ( bInputFieldEndCharInserted + && (nEnd - nStart) > 0 ) + { + SwIndex aIdx( this, nEnd - 1 ); + EraseText( aIdx, 1 ); + } + + if ( bInputFieldStartCharInserted ) + { + SwIndex aIdx( this, nStart ); + EraseText( aIdx, 1 ); + } + } + } + } + + if ( bHiddenPara ) + { + SetCalcHiddenParaField(); + } + + return bRet; +} + +void SwTextNode::DeleteAttribute( SwTextAttr * const pAttr ) +{ + if ( !HasHints() ) + { + OSL_FAIL("DeleteAttribute called, but text node without hints?"); + return; + } + + if ( pAttr->HasDummyChar() ) + { + // copy index! + const SwIndex aIdx( this, pAttr->GetStart() ); + // erase the CH_TXTATR, which will also delete pAttr + EraseText( aIdx, 1 ); + } + else if ( pAttr->HasContent() ) + { + const SwIndex aIdx( this, pAttr->GetStart() ); + assert(pAttr->End() != nullptr); + EraseText( aIdx, *pAttr->End() - pAttr->GetStart() ); + } + else + { + // create MsgHint before start/end become invalid + SwUpdateAttr aHint( + pAttr->GetStart(), + *pAttr->GetEnd(), + pAttr->Which()); + + m_pSwpHints->Delete( pAttr ); + SwTextAttr::Destroy( pAttr, GetDoc()->GetAttrPool() ); + NotifyClients( nullptr, &aHint ); + + TryDeleteSwpHints(); + } +} + +//FIXME: this does NOT respect SORT NUMBER (for CHARFMT)! +void SwTextNode::DeleteAttributes( + const sal_uInt16 nWhich, + const sal_Int32 nStart, + const sal_Int32 nEnd ) +{ + if ( !HasHints() ) + return; + + for ( size_t nPos = 0; m_pSwpHints && nPos < m_pSwpHints->Count(); ++nPos ) + { + SwTextAttr * const pTextHt = m_pSwpHints->Get( nPos ); + const sal_Int32 nHintStart = pTextHt->GetStart(); + if (nStart < nHintStart) + { + break; // sorted by start + } + else if ( (nStart == nHintStart) && (nWhich == pTextHt->Which()) ) + { + if ( nWhich == RES_CHRATR_HIDDEN ) + { + assert(!"hey, that's a CHRATR! how did that get in?"); + SetCalcHiddenCharFlags(); + } + else if ( nWhich == RES_TXTATR_CHARFMT ) + { + // Check if character format contains hidden attribute: + const SwCharFormat* pFormat = pTextHt->GetCharFormat().GetCharFormat(); + const SfxPoolItem* pItem; + if ( SfxItemState::SET == pFormat->GetItemState( RES_CHRATR_HIDDEN, true, &pItem ) ) + SetCalcHiddenCharFlags(); + } + // #i75430# Recalc hidden flags if necessary + else if ( nWhich == RES_TXTATR_AUTOFMT ) + { + // Check if auto style contains hidden attribute: + const SfxPoolItem* pHiddenItem = CharFormat::GetItem( *pTextHt, RES_CHRATR_HIDDEN ); + if ( pHiddenItem ) + SetCalcHiddenCharFlags(); + // for auto styles DeleteAttributes is only called from Undo + // so it shouldn't need to care about ignore start/end flags + } + + sal_Int32 const * const pEndIdx = pTextHt->GetEnd(); + + if ( pTextHt->HasDummyChar() ) + { + // copy index! + const SwIndex aIdx( this, nStart ); + // erase the CH_TXTATR, which will also delete pTextHt + EraseText( aIdx, 1 ); + } + else if ( pTextHt->HasContent() ) + { + const SwIndex aIdx( this, nStart ); + OSL_ENSURE( pTextHt->End() != nullptr, " - missing End() at instance which has content" ); + EraseText( aIdx, *pTextHt->End() - nStart ); + } + else if( *pEndIdx == nEnd ) + { + // Create MsgHint before Start and End are gone. + // For HiddenParaFields it's not necessary to call + // SetCalcHiddenParaField because the dtor does that. + SwUpdateAttr aHint( + nStart, + *pEndIdx, + nWhich); + + m_pSwpHints->DeleteAtPos( nPos ); + SwTextAttr::Destroy( pTextHt, GetDoc()->GetAttrPool() ); + NotifyClients( nullptr, &aHint ); + } + } + } + TryDeleteSwpHints(); +} + +void SwTextNode::DelSoftHyph( const sal_Int32 nStt, const sal_Int32 nEnd ) +{ + sal_Int32 nFndPos = nStt; + sal_Int32 nEndPos = nEnd; + for (;;) + { + nFndPos = m_Text.indexOf(CHAR_SOFTHYPHEN, nFndPos); + if (nFndPos<0 || nFndPos>=nEndPos ) + { + break; + } + const SwIndex aIdx( this, nFndPos ); + EraseText( aIdx, 1 ); + --nEndPos; + } +} + +bool SwTextNode::IsIgnoredCharFormatForNumbering(const sal_uInt16 nWhich) +{ + return (nWhich == RES_CHRATR_UNDERLINE || nWhich == RES_CHRATR_BACKGROUND + || nWhich == RES_CHRATR_ESCAPEMENT); +} + +//In MS Word, following properties of the paragraph end position won't affect the formatting of bullets, so we ignore them: +//Font underline; +//Font Italic of Western, CJK and CTL; +//Font Bold of Wertern, CJK and CTL; +static bool lcl_IsIgnoredCharFormatForBullets(const sal_uInt16 nWhich) +{ + return (nWhich == RES_CHRATR_UNDERLINE || nWhich == RES_CHRATR_POSTURE || nWhich == RES_CHRATR_WEIGHT + || nWhich == RES_CHRATR_CJK_POSTURE || nWhich == RES_CHRATR_CJK_WEIGHT + || nWhich == RES_CHRATR_CTL_POSTURE || nWhich == RES_CHRATR_CTL_WEIGHT); +} + +//Condition for expanding char set to character style of specified number rule level: +//The item inside the set should not conflict to any exist and non-default item inside paragraph properties set (SwContentNode::SwPAttrSet); +//The node should have applied a number rule; +//The node should be counted in a list, if not, make it to be; +//The item should not conflict to any exist and non-default item inside the character of specified number rule level; +//The item should not be ignored depend on the exact number rule type; +void SwTextNode::TryCharSetExpandToNum(const SfxItemSet& aCharSet) +{ + SfxItemIter aIter( aCharSet ); + const SfxPoolItem* pItem = aIter.GetCurItem(); + if (!pItem) + return; + const sal_uInt16 nWhich = pItem->Which(); + + const SfxPoolItem& rInnerItem = GetAttr(nWhich,false); + + if (!IsDefaultItem(&rInnerItem) && !IsInvalidItem(&rInnerItem)) + return; + + if (!IsInList() && GetNumRule() && !GetListId().isEmpty()) + { + return; + } + + SwNumRule* pCurrNum = GetNumRule(false); + + int nLevel = GetActualListLevel(); + + if (nLevel != -1 && pCurrNum) + { + const SwNumFormat* pCurrNumFormat = pCurrNum->GetNumFormat(static_cast(nLevel)); + if (pCurrNumFormat) + { + if (pCurrNumFormat->IsItemize() && lcl_IsIgnoredCharFormatForBullets(nWhich)) + return; + if (pCurrNumFormat->IsEnumeration() && SwTextNode::IsIgnoredCharFormatForNumbering(nWhich)) + return; + SwCharFormat* pCurrCharFormat =pCurrNumFormat->GetCharFormat(); + + if (pCurrCharFormat && pCurrCharFormat->GetItemState(nWhich,false) != SfxItemState::SET) + { + pCurrCharFormat->SetFormatAttr(*pItem); + SwNumFormat aNewNumFormat(*pCurrNumFormat); + aNewNumFormat.SetCharFormat(pCurrCharFormat); + pCurrNum->Set(nLevel,aNewNumFormat); + } + } + } +} + +// Set these attributes on SwTextNode. If they apply to the entire paragraph +// text, set them in the SwTextNode's item set (SwContentNode::SetAttr). +bool SwTextNode::SetAttr( + const SfxItemSet& rSet, + const sal_Int32 nStt, + const sal_Int32 nEnd, + const SetAttrMode nMode, + SwTextAttr **ppNewTextAttr ) +{ + if( !rSet.Count() ) + return false; + + // split sets (for selection in nodes) + const SfxItemSet* pSet = &rSet; + SfxItemSet aTextSet( *rSet.GetPool(), svl::Items{} ); + + // entire paragraph + if ( !nStt && (nEnd == m_Text.getLength()) && + !(nMode & SetAttrMode::NOFORMATATTR ) ) + { + // if the node already has CharFormat hints, the new attributes must + // be set as hints too to override those. + bool bHasCharFormats = false; + if ( HasHints() ) + { + for ( size_t n = 0; n < m_pSwpHints->Count(); ++n ) + { + if ( m_pSwpHints->Get( n )->IsCharFormatAttr() ) + { + bHasCharFormats = true; + break; + } + } + } + + if( !bHasCharFormats ) + { + aTextSet.Put( rSet ); + // If there are any character attributes in rSet, + // we want to set them at the paragraph: + if( aTextSet.Count() != rSet.Count() ) + { + const bool bRet = SetAttr( rSet ); + if( !aTextSet.Count() ) + return bRet; + } + + // check for auto style: + const SfxPoolItem* pItem; + const bool bAutoStyle = SfxItemState::SET == aTextSet.GetItemState( RES_TXTATR_AUTOFMT, false, &pItem ); + if ( bAutoStyle ) + { + std::shared_ptr pAutoStyleSet = static_cast(pItem)->GetStyleHandle(); + const bool bRet = SetAttr( *pAutoStyleSet ); + if( 1 == aTextSet.Count() ) + return bRet; + } + + // Continue with the text attributes: + pSet = &aTextSet; + } + } + + GetOrCreateSwpHints(); + + SfxItemSet aCharSet( *rSet.GetPool(), aCharAutoFormatSetRange ); + + size_t nCount = 0; + SfxItemIter aIter( *pSet ); + const SfxPoolItem* pItem = aIter.GetCurItem(); + + do + { + if (!IsInvalidItem(pItem)) + { + const sal_uInt16 nWhich = pItem->Which(); + OSL_ENSURE( isCHRATR(nWhich) || isTXTATR(nWhich), + "SwTextNode::SetAttr(): unknown attribute" ); + if ( isCHRATR(nWhich) || isTXTATR(nWhich) ) + { + if ((RES_TXTATR_CHARFMT == nWhich) && + (GetDoc()->GetDfltCharFormat() == + static_cast(pItem)->GetCharFormat())) + { + SwIndex aIndex( this, nStt ); + RstTextAttr( aIndex, nEnd - nStt, RES_TXTATR_CHARFMT ); + DontExpandFormat( aIndex ); + } + else + { + if (isCHRATR(nWhich) || + (RES_TXTATR_UNKNOWN_CONTAINER == nWhich)) + { + aCharSet.Put( *pItem ); + } + else + { + + SwTextAttr *const pNew = MakeTextAttr( *GetDoc(), + const_cast(*pItem), nStt, nEnd ); + if ( pNew ) + { + // store the first one we create into the pp + if (ppNewTextAttr && !*ppNewTextAttr) + *ppNewTextAttr = pNew; + if ( nEnd != nStt && !pNew->GetEnd() ) + { + OSL_FAIL("Attribute without end, but area marked"); + DestroyAttr( pNew ); // do not insert + } + else if ( InsertHint( pNew, nMode ) ) + { + ++nCount; + } + } + } + } + } + } + pItem = aIter.NextItem(); + } while(pItem); + + if ( aCharSet.Count() ) + { + SwTextAttr* pTmpNew = MakeTextAttr( *GetDoc(), aCharSet, nStt, nEnd ); + if ( InsertHint( pTmpNew, nMode ) ) + { + ++nCount; + } + } + + TryDeleteSwpHints(); + + return nCount != 0; +} + +static void lcl_MergeAttr( SfxItemSet& rSet, const SfxPoolItem& rAttr ) +{ + if ( RES_TXTATR_AUTOFMT == rAttr.Which() ) + { + const SfxItemSet* pCFSet = CharFormat::GetItemSet( rAttr ); + if ( !pCFSet ) + return; + SfxWhichIter aIter( *pCFSet ); + sal_uInt16 nWhich = aIter.FirstWhich(); + while( nWhich ) + { + if( ( nWhich < RES_CHRATR_END || + RES_TXTATR_UNKNOWN_CONTAINER == nWhich ) && + ( SfxItemState::SET == pCFSet->GetItemState( nWhich ) ) ) + rSet.Put( pCFSet->Get( nWhich ) ); + nWhich = aIter.NextWhich(); + } + } + else + rSet.Put( rAttr ); +} + +static void lcl_MergeAttr_ExpandChrFormat( SfxItemSet& rSet, const SfxPoolItem& rAttr ) +{ + if( RES_TXTATR_CHARFMT == rAttr.Which() || + RES_TXTATR_INETFMT == rAttr.Which() || + RES_TXTATR_AUTOFMT == rAttr.Which() ) + { + const SfxItemSet* pCFSet = CharFormat::GetItemSet( rAttr ); + + if ( pCFSet ) + { + SfxWhichIter aIter( *pCFSet ); + sal_uInt16 nWhich = aIter.FirstWhich(); + while( nWhich ) + { + if( ( nWhich < RES_CHRATR_END || + ( RES_TXTATR_AUTOFMT == rAttr.Which() && RES_TXTATR_UNKNOWN_CONTAINER == nWhich ) ) && + ( SfxItemState::SET == pCFSet->GetItemState( nWhich ) ) ) + rSet.Put( pCFSet->Get( nWhich ) ); + nWhich = aIter.NextWhich(); + } + } + } + +/* If multiple attributes overlap, the last one wins! + Probably this can only happen between a RES_TXTATR_INETFMT and one of the + other hints, because BuildPortions ensures that CHARFMT/AUTOFMT don't + overlap. But there may be multiple CHARFMT/AUTOFMT with exactly the same + start/end, sorted by BuildPortions, in which case the same logic applies. + + 1234567890123456789 + |------------| Font1 + |------| Font2 + ^ ^ + |--| query range: -> Font2 +*/ + // merge into set + rSet.Put( rAttr ); +} + +namespace { + +struct SwPoolItemEndPair +{ +public: + const SfxPoolItem* mpItem; + sal_Int32 mnEndPos; + + SwPoolItemEndPair() : mpItem( nullptr ), mnEndPos( 0 ) {}; +}; + +} + +static void lcl_MergeListLevelIndentAsLRSpaceItem( const SwTextNode& rTextNode, + SfxItemSet& rSet ) +{ + if ( rTextNode.AreListLevelIndentsApplicable() ) + { + const SwNumRule* pRule = rTextNode.GetNumRule(); + if ( pRule && rTextNode.GetActualListLevel() >= 0 ) + { + const SwNumFormat& rFormat = pRule->Get(static_cast(rTextNode.GetActualListLevel())); + if ( rFormat.GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_ALIGNMENT ) + { + SvxLRSpaceItem aLR( RES_LR_SPACE ); + aLR.SetTextLeft( rFormat.GetIndentAt() ); + aLR.SetTextFirstLineOffset( static_cast(rFormat.GetFirstLineIndent()) ); + rSet.Put( aLR ); + } + } + } +} + +// request the attributes of the TextNode at the range +bool SwTextNode::GetParaAttr(SfxItemSet& rSet, sal_Int32 nStt, sal_Int32 nEnd, + const bool bOnlyTextAttr, const bool bGetFromChrFormat, + const bool bMergeIndentValuesOfNumRule, + SwRootFrame const*const pLayout) const +{ + assert(!rSet.Count()); // handled inconsistently, typically an error? + + if (pLayout && pLayout->IsHideRedlines()) + { + if (GetRedlineMergeFlag() == SwNode::Merge::Hidden) + { + return false; // ignore deleted node + } + } + + // get the node's automatic attributes + SfxItemSet aFormatSet( *rSet.GetPool(), rSet.GetRanges() ); + if (!bOnlyTextAttr) + { + SwTextNode const& rParaPropsNode( + sw::GetAttrMerged(aFormatSet, *this, pLayout)); + if (bMergeIndentValuesOfNumRule) + { + lcl_MergeListLevelIndentAsLRSpaceItem(rParaPropsNode, aFormatSet); + } + } + + if( HasHints() ) + { + // First, check which text attributes are valid in the range. + // cases: + // Ambiguous, if + // * the attribute is wholly contained in the range + // * the attribute end is in the range + // * the attribute start is in the range + // Unambiguous (merge into set), if + // * the attribute wholly contains the range + // Ignored, if + // * the attribute is wholly outside the range + + void (*fnMergeAttr)( SfxItemSet&, const SfxPoolItem& ) + = bGetFromChrFormat ? &lcl_MergeAttr_ExpandChrFormat + : &lcl_MergeAttr; + + const size_t nSize = m_pSwpHints->Count(); + + if (nStt == nEnd) // no range: + { + for (size_t n = 0; n < nSize; ++n) + { + const SwTextAttr* pHt = m_pSwpHints->Get(n); + const sal_Int32 nAttrStart = pHt->GetStart(); + if (nAttrStart > nEnd) // behind the range + break; + + const sal_Int32* pAttrEnd = pHt->End(); + if ( ! pAttrEnd ) // no attributes without end + continue; + + if( ( nAttrStart < nStt && + ( pHt->DontExpand() ? nStt < *pAttrEnd + : nStt <= *pAttrEnd )) || + ( nStt == nAttrStart && + ( nAttrStart == *pAttrEnd || !nStt ))) + (*fnMergeAttr)( rSet, pHt->GetAttr() ); + } + } + else // a query range is defined + { + // #i75299# + std::unique_ptr< std::vector< SwPoolItemEndPair > > pAttrArr; + + const size_t coArrSz = RES_TXTATR_WITHEND_END - RES_CHRATR_BEGIN; + + for (size_t n = 0; n < nSize; ++n) + { + const SwTextAttr* pHt = m_pSwpHints->Get(n); + const sal_Int32 nAttrStart = pHt->GetStart(); + if (nAttrStart > nEnd) // outside, behind + break; + + const sal_Int32* pAttrEnd = pHt->End(); + if ( ! pAttrEnd ) // no attributes without end + continue; + + bool bChkInvalid = false; + if (nAttrStart <= nStt) // before or exactly Start + { + if (*pAttrEnd <= nStt) // outside, before + continue; + + if (nEnd <= *pAttrEnd) // behind or exactly End + (*fnMergeAttr)( aFormatSet, pHt->GetAttr() ); + else +// else if( pHt->GetAttr() != aFormatSet.Get( pHt->Which() ) ) + // ambiguous + bChkInvalid = true; + } + else if (nAttrStart < nEnd // starts in the range +)// && pHt->GetAttr() != aFormatSet.Get( pHt->Which() ) ) + bChkInvalid = true; + + if( bChkInvalid ) + { + // ambiguous? + std::unique_ptr< SfxItemIter > pItemIter; + const SfxPoolItem* pItem = nullptr; + + if ( RES_TXTATR_AUTOFMT == pHt->Which() ) + { + const SfxItemSet* pAutoSet = CharFormat::GetItemSet( pHt->GetAttr() ); + if ( pAutoSet ) + { + pItemIter.reset( new SfxItemIter( *pAutoSet ) ); + pItem = pItemIter->GetCurItem(); + } + } + else + pItem = &pHt->GetAttr(); + + const sal_Int32 nHintEnd = *pAttrEnd; + + for (; pItem; pItem = pItemIter ? pItemIter->NextItem() : nullptr) + { + const sal_uInt16 nHintWhich = pItem->Which(); + OSL_ENSURE(!isUNKNOWNATR(nHintWhich), + "SwTextNode::GetAttr(): unknown attribute?"); + + if (!pAttrArr) + { + pAttrArr.reset( + new std::vector< SwPoolItemEndPair >(coArrSz)); + } + + std::vector< SwPoolItemEndPair >::iterator pPrev = pAttrArr->begin(); + if (isCHRATR(nHintWhich) || + isTXTATR_WITHEND(nHintWhich)) + { + pPrev += nHintWhich - RES_CHRATR_BEGIN; + } + else + { + pPrev = pAttrArr->end(); + } + + if( pPrev != pAttrArr->end() ) + { + if( !pPrev->mpItem ) + { + if ( bOnlyTextAttr || *pItem != aFormatSet.Get( nHintWhich ) ) + { + if( nAttrStart > nStt ) + { + rSet.InvalidateItem( nHintWhich ); + pPrev->mpItem = INVALID_POOL_ITEM; + } + else + { + pPrev->mpItem = pItem; + pPrev->mnEndPos = nHintEnd; + } + } + } + else if( !IsInvalidItem(pPrev->mpItem) ) + { + if( pPrev->mnEndPos == nAttrStart && + *pPrev->mpItem == *pItem ) + { + pPrev->mpItem = pItem; + pPrev->mnEndPos = nHintEnd; + } + else + { + rSet.InvalidateItem( nHintWhich ); + pPrev->mpItem = INVALID_POOL_ITEM; + } + } + } + } // end while + } + } + + if (pAttrArr) + { + for (size_t n = 0; n < coArrSz; ++n) + { + const SwPoolItemEndPair& rItemPair = (*pAttrArr)[ n ]; + if( rItemPair.mpItem && !IsInvalidItem(rItemPair.mpItem) ) + { + const sal_uInt16 nWh = + static_cast(n + RES_CHRATR_BEGIN); + + if (nEnd <= rItemPair.mnEndPos) // behind or exactly end + { + if( *rItemPair.mpItem != aFormatSet.Get( nWh ) ) + (*fnMergeAttr)( rSet, *rItemPair.mpItem ); + } + else + // ambiguous + rSet.InvalidateItem( nWh ); + } + } + } + } + if( aFormatSet.Count() ) + { + // remove all from the format-set that are also set in the text-set + aFormatSet.Differentiate( rSet ); + } + } + + if (aFormatSet.Count()) + { + // now "merge" everything + rSet.Put( aFormatSet ); + } + + return rSet.Count() != 0; +} + +namespace +{ + +typedef std::pair AttrSpan_t; +typedef std::multimap AttrSpanMap_t; + +struct IsAutoStyle +{ + bool + operator()(const AttrSpanMap_t::value_type& i_rAttrSpan) + const + { + return i_rAttrSpan.second && i_rAttrSpan.second->Which() == RES_TXTATR_AUTOFMT; + } +}; + +/** Removes from io_rAttrSet all items that are set by style on the + given span. + */ +struct RemovePresentAttrs +{ + explicit RemovePresentAttrs(SfxItemSet& io_rAttrSet) + : m_rAttrSet(io_rAttrSet) + { + } + + void + operator()(const AttrSpanMap_t::value_type& i_rAttrSpan) + const + { + if (!i_rAttrSpan.second) + { + return; + } + + const SwTextAttr* const pAutoStyle(i_rAttrSpan.second); + SfxItemIter aIter(m_rAttrSet); + for (const SfxPoolItem* pItem(aIter.GetCurItem()); pItem; pItem = aIter.NextItem()) + { + const sal_uInt16 nWhich(pItem->Which()); + if (CharFormat::IsItemIncluded(nWhich, pAutoStyle)) + { + m_rAttrSet.ClearItem(nWhich); + } + } + } + +private: + SfxItemSet& m_rAttrSet; +}; + +/** Collects all style-covered spans from i_rHints to o_rSpanMap. In + addition inserts dummy spans with pointer to format equal to 0 for + all gaps (i.e. spans not covered by any style). This simplifies + creation of autostyles for all needed spans, but it means all code + that tries to access the pointer has to check if it's non-null! + */ +void +lcl_CollectHintSpans(const SwpHints& i_rHints, const sal_Int32 nLength, + AttrSpanMap_t& o_rSpanMap) +{ + sal_Int32 nLastEnd(0); + + for (size_t i = 0; i < i_rHints.Count(); ++i) + { + const SwTextAttr* pHint = i_rHints.Get(i); + const sal_uInt16 nWhich(pHint->Which()); + if (nWhich == RES_TXTATR_CHARFMT || nWhich == RES_TXTATR_AUTOFMT) + { + const AttrSpan_t aSpan(pHint->GetStart(), *pHint->End()); + o_rSpanMap.emplace(aSpan, pHint); + + // < not != because there may be multiple CHARFMT at same range + if (nLastEnd < aSpan.first) + { + // insert dummy span covering the gap + o_rSpanMap.emplace( AttrSpan_t(nLastEnd, aSpan.first), nullptr ); + } + + nLastEnd = aSpan.second; + } + } + + // no hints at the end (special case: no hints at all in i_rHints) + if (nLastEnd != nLength && nLength != 0) + { + o_rSpanMap.emplace(AttrSpan_t(nLastEnd, nLength), nullptr); + } +} + +void +lcl_FillWhichIds(const SfxItemSet& i_rAttrSet, std::vector& o_rClearIds) +{ + o_rClearIds.reserve(i_rAttrSet.Count()); + SfxItemIter aIter(i_rAttrSet); + for (const SfxPoolItem* pItem = aIter.GetCurItem(); pItem; pItem = aIter.NextItem()) + { + o_rClearIds.push_back(pItem->Which()); + } +} + +struct SfxItemSetClearer +{ + SfxItemSet & m_rItemSet; + explicit SfxItemSetClearer(SfxItemSet & rItemSet) : m_rItemSet(rItemSet) { } + void operator()(sal_uInt16 const nWhich) { m_rItemSet.ClearItem(nWhich); } +}; + +} + +/** Does the hard work of SwTextNode::FormatToTextAttr: the real conversion + of items to automatic styles. + */ +void +SwTextNode::impl_FormatToTextAttr(const SfxItemSet& i_rAttrSet) +{ + typedef AttrSpanMap_t::iterator AttrSpanMap_iterator_t; + AttrSpanMap_t aAttrSpanMap; + + if (i_rAttrSet.Count() == 0) + { + return; + } + + // 1. Identify all spans in hints' array + + lcl_CollectHintSpans(*m_pSwpHints, m_Text.getLength(), aAttrSpanMap); + + // 2. Go through all spans and insert new attrs + + AttrSpanMap_iterator_t aCurRange(aAttrSpanMap.begin()); + const AttrSpanMap_iterator_t aEnd(aAttrSpanMap.end()); + while (aCurRange != aEnd) + { + typedef std::pair + AttrSpanMapRange_t; + AttrSpanMapRange_t aRange(aAttrSpanMap.equal_range(aCurRange->first)); + + // 2a. Collect attributes to insert + + SfxItemSet aCurSet(i_rAttrSet); + std::for_each(aRange.first, aRange.second, RemovePresentAttrs(aCurSet)); + + // 2b. Insert automatic style containing the collected attributes + + if (aCurSet.Count() != 0) + { + AttrSpanMap_iterator_t aAutoStyleIt( + std::find_if(aRange.first, aRange.second, IsAutoStyle())); + if (aAutoStyleIt != aRange.second) + { + // there already is an automatic style on that span: + // create new one and remove the original one + SwTextAttr* const pAutoStyle(const_cast(aAutoStyleIt->second)); + const std::shared_ptr pOldStyle( + static_cast( + pAutoStyle->GetAttr()).GetStyleHandle()); + aCurSet.Put(*pOldStyle); + + // remove the old hint + m_pSwpHints->Delete(pAutoStyle); + DestroyAttr(pAutoStyle); + } + m_pSwpHints->Insert( + MakeTextAttr(*GetDoc(), aCurSet, + aCurRange->first.first, aCurRange->first.second)); + } + + aCurRange = aRange.second; + } + + // hints were directly inserted, so need to fix the Ignore flags now + m_pSwpHints->MergePortions(*this); + + // 3. Clear items from the node + std::vector aClearedIds; + lcl_FillWhichIds(i_rAttrSet, aClearedIds); + ClearItemsFromAttrSet(aClearedIds); +} + +void SwTextNode::FormatToTextAttr( SwTextNode* pNd ) +{ + SfxItemSet aThisSet( GetDoc()->GetAttrPool(), aCharFormatSetRange ); + if( HasSwAttrSet() && GetpSwAttrSet()->Count() ) + aThisSet.Put( *GetpSwAttrSet() ); + + GetOrCreateSwpHints(); + + if( pNd == this ) + { + impl_FormatToTextAttr(aThisSet); + } + else + { + // There are five possible combinations of items from this and + // pNd (pNd is the 'main' node): + + // case pNd this action + + // 1 - - do nothing + // 2 - a convert item to attr of this + // 3 a - convert item to attr of pNd + // 4 a a clear item in this + // 5 a b convert item to attr of this + + SfxItemSet aNdSet( pNd->GetDoc()->GetAttrPool(), aCharFormatSetRange ); + if( pNd->HasSwAttrSet() && pNd->GetpSwAttrSet()->Count() ) + aNdSet.Put( *pNd->GetpSwAttrSet() ); + + pNd->GetOrCreateSwpHints(); + + std::vector aProcessedIds; + + if( aThisSet.Count() ) + { + SfxItemIter aIter( aThisSet ); + const SfxPoolItem* pItem = aIter.GetCurItem(), *pNdItem = nullptr; + SfxItemSet aConvertSet( GetDoc()->GetAttrPool(), aCharFormatSetRange ); + std::vector aClearWhichIds; + + do + { + if( SfxItemState::SET == aNdSet.GetItemState( pItem->Which(), false, &pNdItem ) ) + { + if (*pItem == *pNdItem) // 4 + { + aClearWhichIds.push_back( pItem->Which() ); + } + else // 5 + { + aConvertSet.Put(*pItem); + } + aProcessedIds.push_back(pItem->Which()); + } + else // 2 + { + aConvertSet.Put(*pItem); + } + + pItem = aIter.NextItem(); + } while (pItem); + + // 4/ clear items of this that are set with the same value on pNd + ClearItemsFromAttrSet( aClearWhichIds ); + + // 2, 5/ convert all other items to attrs + impl_FormatToTextAttr(aConvertSet); + } + + { + std::for_each(aProcessedIds.begin(), aProcessedIds.end(), + SfxItemSetClearer(aNdSet)); + + // 3/ convert items to attrs + pNd->impl_FormatToTextAttr(aNdSet); + + if( aNdSet.Count() ) + { + SwFormatChg aTmp1( pNd->GetFormatColl() ); + pNd->NotifyClients( &aTmp1, &aTmp1 ); + } + } + } + + SetCalcHiddenCharFlags(); + + pNd->TryDeleteSwpHints(); +} + +void SwpHints::CalcFlags() +{ + m_bDDEFields = m_bFootnote = false; + const size_t nSize = Count(); + for( size_t nPos = 0; nPos < nSize; ++nPos ) + { + const SwTextAttr* pAttr = Get( nPos ); + switch( pAttr->Which() ) + { + case RES_TXTATR_FTN: + m_bFootnote = true; + if ( m_bDDEFields ) + return; + break; + case RES_TXTATR_FIELD: + { + const SwField* pField = pAttr->GetFormatField().GetField(); + if( SwFieldIds::Dde == pField->GetTyp()->Which() ) + { + m_bDDEFields = true; + if ( m_bFootnote ) + return; + } + } + break; + } + } +} + +bool SwpHints::CalcHiddenParaField() const +{ + m_bCalcHiddenParaField = false; + const bool bOldHiddenByParaField = m_bHiddenByParaField; + bool bNewHiddenByParaField = false; + int nNewResultWeight = 0; + const size_t nSize = Count(); + const SwTextAttr* pTextHt; + + for (size_t nPos = 0; nPos < nSize; ++nPos) + { + pTextHt = Get(nPos); + const sal_uInt16 nWhich = pTextHt->Which(); + + if (RES_TXTATR_FIELD == nWhich) + { + // see also SwTextFrame::IsHiddenNow() + const SwFormatField& rField = pTextHt->GetFormatField(); + int nCurWeight = m_rParent.GetDoc()->FieldCanHideParaWeight(rField.GetField()->GetTyp()->Which()); + if (nCurWeight > nNewResultWeight) + { + nNewResultWeight = nCurWeight; + bNewHiddenByParaField = m_rParent.GetDoc()->FieldHidesPara(*rField.GetField()); + } + else if (nCurWeight == nNewResultWeight && bNewHiddenByParaField) + { + // Currently, for both supported hiding types (HiddenPara, Database), "Don't hide" + // takes precedence - i.e., if there's a "Don't hide" field of that weight, we only + // care about fields of higher weight. + bNewHiddenByParaField = m_rParent.GetDoc()->FieldHidesPara(*rField.GetField()); + } + } + } + SetHiddenByParaField(bNewHiddenByParaField); + return bOldHiddenByParaField != bNewHiddenByParaField; +} + +void SwpHints::NoteInHistory( SwTextAttr *pAttr, const bool bNew ) +{ + if ( m_pHistory ) { m_pHistory->AddHint( pAttr, bNew ); } +} + +bool SwpHints::MergePortions( SwTextNode& rNode ) +{ + if ( !Count() ) + return false; + + // sort before merging + Resort(); + + bool bRet = false; + typedef std::multimap< int, std::pair > PortionMap; + PortionMap aPortionMap; + std::unordered_map RsidOnlyAutoFormatFlagMap; + sal_Int32 nLastPorStart = COMPLETE_STRING; + int nKey = 0; + + // get portions by start position: + for ( size_t i = 0; i < Count(); ++i ) + { + SwTextAttr *pHt = Get( i ); + if ( RES_TXTATR_CHARFMT != pHt->Which() && + RES_TXTATR_AUTOFMT != pHt->Which() ) + //&& + //RES_TXTATR_INETFMT != pHt->Which() ) + continue; + + bool isRsidOnlyAutoFormat(false); + // check for RSID-only AUTOFMT + if (RES_TXTATR_AUTOFMT == pHt->Which()) + { + std::shared_ptr const pSet( + pHt->GetAutoFormat().GetStyleHandle()); + if ((pSet->Count() == 1) && pSet->GetItem(RES_CHRATR_RSID, false)) + { + // fdo#70201: eliminate no-extent RSID-only AUTOFMT + // could be produced by ReplaceText or (maybe?) RstAttr + if (pHt->GetStart() == *pHt->GetEnd()) + { + DeleteAtPos(i); // kill it without History! + SwTextAttr::Destroy(pHt, rNode.GetDoc()->GetAttrPool()); + --i; + continue; + } + // fdo#52028: this one has _only_ RSID => ignore it completely + if (!pHt->IsFormatIgnoreStart() || !pHt->IsFormatIgnoreEnd()) + { + NoteInHistory(pHt); + pHt->SetFormatIgnoreStart(true); + pHt->SetFormatIgnoreEnd (true); + NoteInHistory(pHt, true); + } + isRsidOnlyAutoFormat = true; + } + } + + if (pHt->GetStart() == *pHt->GetEnd()) + { + // no-length hints are a disease. ignore them here. + // the SwAttrIter::SeekFwd will not call Rst/Chg for them. + continue; + } + + const sal_Int32 nPorStart = pHt->GetStart(); + if (nPorStart != nLastPorStart) + ++nKey; + nLastPorStart = nPorStart; + aPortionMap.insert(std::make_pair(nKey, + std::make_pair(pHt, isRsidOnlyAutoFormat))); + RsidOnlyAutoFormatFlagMap[nKey] = isRsidOnlyAutoFormat; + } + + // check if portion i can be merged with portion i+1: + // note: need to include i=0 to set IgnoreStart and j=nKey+1 to reset + // IgnoreEnd at first / last portion + int i = 0; + int j = i + 1; + while ( i <= nKey ) + { + std::pair< PortionMap::iterator, PortionMap::iterator > aRange1 = aPortionMap.equal_range( i ); + std::pair< PortionMap::iterator, PortionMap::iterator > aRange2 = aPortionMap.equal_range( j ); + PortionMap::iterator aIter1 = aRange1.first; + PortionMap::iterator aIter2 = aRange2.first; + + enum { MATCH, DIFFER_ONLY_RSID, DIFFER } eMerge(MATCH); + size_t const nAttributesInPor1 = std::distance(aRange1.first, aRange1.second); + size_t const nAttributesInPor2 = std::distance(aRange2.first, aRange2.second); + bool const isRsidOnlyAutoFormat1(RsidOnlyAutoFormatFlagMap[i]); + bool const isRsidOnlyAutoFormat2(RsidOnlyAutoFormatFlagMap[j]); + + // if both have one they could be equal, but not if only one has it + bool const bSkipRsidOnlyAutoFormat(nAttributesInPor1 != nAttributesInPor2); + + // this loop needs to handle the case where one has a CHARFMT and the + // other CHARFMT + RSID-only AUTOFMT, so... + // want to skip over RSID-only AUTOFMT here, hence the -1 + if ((nAttributesInPor1 - (isRsidOnlyAutoFormat1 ? 1 : 0)) == + (nAttributesInPor2 - (isRsidOnlyAutoFormat2 ? 1 : 0)) + && (nAttributesInPor1 != 0 || nAttributesInPor2 != 0)) + { + // _if_ there is one element more either in aRange1 or aRange2 + // it _must_ be an RSID-only AUTOFMT, which can be ignored here... + // But if both have RSID-only AUTOFMT they could be equal, no skip! + while (aIter1 != aRange1.second || aIter2 != aRange2.second) + { + // first of all test if there's no gap (before skipping stuff!) + if (aIter1 != aRange1.second && aIter2 != aRange2.second && + *aIter1->second.first->End() < aIter2->second.first->GetStart()) + { + eMerge = DIFFER; + break; + } + // skip it - cannot be equal if bSkipRsidOnlyAutoFormat is set + if (bSkipRsidOnlyAutoFormat + && aIter1 != aRange1.second && aIter1->second.second) + { + assert(DIFFER != eMerge); + eMerge = DIFFER_ONLY_RSID; + ++aIter1; + continue; + } + if (bSkipRsidOnlyAutoFormat + && aIter2 != aRange2.second && aIter2->second.second) + { + assert(DIFFER != eMerge); + eMerge = DIFFER_ONLY_RSID; + ++aIter2; + continue; + } + assert(aIter1 != aRange1.second && aIter2 != aRange2.second); + SwTextAttr const*const p1 = aIter1->second.first; + SwTextAttr const*const p2 = aIter2->second.first; + if (p1->Which() != p2->Which()) + { + eMerge = DIFFER; + break; + } + if (!(*p1 == *p2)) + { + // fdo#52028: for auto styles, check if they differ only + // in the RSID, which should have no effect on text layout + if (RES_TXTATR_AUTOFMT == p1->Which()) + { + const SfxItemSet& rSet1 = *p1->GetAutoFormat().GetStyleHandle(); + const SfxItemSet& rSet2 = *p2->GetAutoFormat().GetStyleHandle(); + + // sadly SfxItemSet::operator== does not seem to work? + SfxItemIter iter1(rSet1); + SfxItemIter iter2(rSet2); + for (SfxPoolItem const* pItem1 = iter1.GetCurItem(), + * pItem2 = iter2.GetCurItem(); + pItem1 && pItem2; + pItem1 = iter1.NextItem(), + pItem2 = iter2.NextItem()) + { + if (pItem1->Which() == RES_CHRATR_RSID) + pItem1 = iter1.NextItem(); + if (pItem2->Which() == RES_CHRATR_RSID) + pItem2 = iter2.NextItem(); + if (!pItem1 && !pItem2) + break; + if (!pItem1 || !pItem2) + { + eMerge = DIFFER; + break; + } + if (pItem1 != pItem2) // all are poolable + { + assert(IsInvalidItem(pItem1) || IsInvalidItem(pItem2) || pItem1->Which() != pItem2->Which() || *pItem1 != *pItem2); + eMerge = DIFFER; + break; + } + if (iter1.IsAtEnd() && iter2.IsAtEnd()) + break; + if (iter1.IsAtEnd() || iter2.IsAtEnd()) + { + eMerge = DIFFER; + break; + } + } + if (DIFFER == eMerge) + break; // outer loop too + else + eMerge = DIFFER_ONLY_RSID; + } + else + { + eMerge = DIFFER; + break; + } + } + ++aIter1; + ++aIter2; + } + } + else + { + eMerge = DIFFER; + } + + if (MATCH == eMerge) + { + // important: delete second range so any IgnoreStart on the first + // range is still valid + // erase all elements with key i + 1 + sal_Int32 nNewPortionEnd = 0; + for ( aIter2 = aRange2.first; aIter2 != aRange2.second; ++aIter2 ) + { + SwTextAttr *const p2 = aIter2->second.first; + nNewPortionEnd = *p2->GetEnd(); + + const size_t nCountBeforeDelete = Count(); + Delete( p2 ); + + // robust: check if deletion actually took place before destroying attribute: + if ( Count() < nCountBeforeDelete ) + rNode.DestroyAttr( p2 ); + } + aPortionMap.erase( aRange2.first, aRange2.second ); + ++j; + + // change all attributes with key i + aRange1 = aPortionMap.equal_range( i ); + for ( aIter1 = aRange1.first; aIter1 != aRange1.second; ++aIter1 ) + { + SwTextAttr *const p1 = aIter1->second.first; + NoteInHistory( p1 ); + p1->SetEnd(nNewPortionEnd); + NoteInHistory( p1, true ); + bRet = true; + } + + if (bRet) + { + Resort(); + } + } + else + { + // when not merging the ignore flags need to be either set or reset + // (reset too in case one of the autofmts was recently changed) + bool const bSetIgnoreFlag(DIFFER_ONLY_RSID == eMerge); + for (aIter1 = aRange1.first; aIter1 != aRange1.second; ++aIter1) + { + if (!aIter1->second.second) // already set above, don't change + { + SwTextAttr *const pCurrent(aIter1->second.first); + if (pCurrent->IsFormatIgnoreEnd() != bSetIgnoreFlag) + { + NoteInHistory(pCurrent); + pCurrent->SetFormatIgnoreEnd(bSetIgnoreFlag); + NoteInHistory(pCurrent, true); + } + } + } + for (aIter2 = aRange2.first; aIter2 != aRange2.second; ++aIter2) + { + if (!aIter2->second.second) // already set above, don't change + { + SwTextAttr *const pCurrent(aIter2->second.first); + if (pCurrent->IsFormatIgnoreStart() != bSetIgnoreFlag) + { + NoteInHistory(pCurrent); + pCurrent->SetFormatIgnoreStart(bSetIgnoreFlag); + NoteInHistory(pCurrent, true); + } + } + } + i = j; // ++i not enough: i + 1 may have been deleted (MATCH)! + ++j; + } + } + + return bRet; +} + +// check if there is already a character format and adjust the sort numbers +static void lcl_CheckSortNumber( const SwpHints& rHints, SwTextCharFormat& rNewCharFormat ) +{ + const sal_Int32 nHtStart = rNewCharFormat.GetStart(); + const sal_Int32 nHtEnd = *rNewCharFormat.GetEnd(); + sal_uInt16 nSortNumber = 0; + + for ( size_t i = 0; i < rHints.Count(); ++i ) + { + const SwTextAttr* pOtherHt = rHints.Get(i); + + const sal_Int32 nOtherStart = pOtherHt->GetStart(); + + if ( nOtherStart > nHtStart ) + break; + + if ( RES_TXTATR_CHARFMT == pOtherHt->Which() ) + { + const sal_Int32 nOtherEnd = *pOtherHt->End(); + + if ( nOtherStart == nHtStart && nOtherEnd == nHtEnd ) + { + nSortNumber = static_txtattr_cast(pOtherHt)->GetSortNumber() + 1; + } + } + } + + if ( nSortNumber > 0 ) + rNewCharFormat.SetSortNumber( nSortNumber ); +} + +/* + * Try to insert the new hint. + * Depending on the type of the hint, this either always succeeds, or may fail. + * Depending on the type of the hint, other hints may be deleted or + * overwritten. + * The return value indicates successful insertion. + */ +bool SwpHints::TryInsertHint( + SwTextAttr* const pHint, + SwTextNode &rNode, + const SetAttrMode nMode ) +{ + if ( MAX_HINTS <= Count() ) // we're sorry, this flight is overbooked... + { + OSL_FAIL("hints array full :-("); + return false; + } + + const sal_Int32 *pHtEnd = pHint->GetEnd(); + const sal_uInt16 nWhich = pHint->Which(); + std::vector aWhichSublist; + + switch( nWhich ) + { + case RES_TXTATR_CHARFMT: + { + // Check if character format contains hidden attribute: + const SwCharFormat* pFormat = pHint->GetCharFormat().GetCharFormat(); + const SfxPoolItem* pItem; + if ( SfxItemState::SET == pFormat->GetItemState( RES_CHRATR_HIDDEN, true, &pItem ) ) + rNode.SetCalcHiddenCharFlags(); + + static_txtattr_cast(pHint)->ChgTextNode( &rNode ); + break; + } + // #i75430# Recalc hidden flags if necessary + case RES_TXTATR_AUTOFMT: + { + std::shared_ptr const pSet( pHint->GetAutoFormat().GetStyleHandle() ); + if (pHint->GetStart() == *pHint->GetEnd()) + { + if (pSet->Count() == 1 && pSet->GetItem(RES_CHRATR_RSID, false)) + { // empty range RSID-only hints could cause trouble, there's no + rNode.DestroyAttr(pHint); // need for them so don't insert + return false; + } + } + // Check if auto style contains hidden attribute: + const SfxPoolItem* pHiddenItem = CharFormat::GetItem( *pHint, RES_CHRATR_HIDDEN ); + if ( pHiddenItem ) + rNode.SetCalcHiddenCharFlags(); + + // fdo#71556: populate aWhichFormatAttr member of SwMsgPoolItem + const sal_uInt16 *pRanges = pSet->GetRanges(); + while( (*pRanges) != 0 ) + { + const sal_uInt16 nBeg = *pRanges; + ++pRanges; + const sal_uInt16 nEnd = *pRanges; + ++pRanges; + for( sal_uInt16 nSubElem = nBeg; nSubElem <= nEnd; ++nSubElem ) + if( pSet->HasItem( nSubElem ) ) + aWhichSublist.push_back( nSubElem ); + } + break; + } + case RES_TXTATR_INETFMT: + static_txtattr_cast(pHint)->InitINetFormat(rNode); + break; + + case RES_TXTATR_FIELD: + case RES_TXTATR_ANNOTATION: + case RES_TXTATR_INPUTFIELD: + { + SwTextField *const pTextField(static_txtattr_cast(pHint)); + bool bDelFirst = nullptr != pTextField->GetpTextNode(); + pTextField->ChgTextNode( &rNode ); + SwDoc* pDoc = rNode.GetDoc(); + const SwField* pField = pTextField->GetFormatField().GetField(); + + if( !pDoc->getIDocumentFieldsAccess().IsNewFieldLst() ) + { + // certain fields must update the SwDoc's calculation flags + switch( pField->GetTyp()->Which() ) + { + case SwFieldIds::Database: + case SwFieldIds::SetExp: + case SwFieldIds::HiddenPara: + case SwFieldIds::HiddenText: + case SwFieldIds::DbNumSet: + case SwFieldIds::DbNextSet: + { + if( bDelFirst ) + pDoc->getIDocumentFieldsAccess().InsDelFieldInFieldLst(false, *pTextField); + if( rNode.GetNodes().IsDocNodes() ) + pDoc->getIDocumentFieldsAccess().InsDelFieldInFieldLst(true, *pTextField); + } + break; + case SwFieldIds::Dde: + if( rNode.GetNodes().IsDocNodes() ) + static_cast(pField->GetTyp())->IncRefCnt(); + break; + default: break; + } + } + + // insert into real document's nodes-array? + if( rNode.GetNodes().IsDocNodes() ) + { + bool bInsFieldType = false; + switch( pField->GetTyp()->Which() ) + { + case SwFieldIds::SetExp: + bInsFieldType = static_cast(pField->GetTyp())->IsDeleted(); + if( nsSwGetSetExpType::GSE_SEQ & static_cast(pField->GetTyp())->GetType() ) + { + // register the field at its FieldType before setting + // the sequence reference number! + SwSetExpFieldType* pFieldType = static_cast( + pDoc->getIDocumentFieldsAccess().InsertFieldType( *pField->GetTyp() ) ); + if( pFieldType != pField->GetTyp() ) + { + SwFormatField* pFormatField = const_cast(&pTextField->GetFormatField()); + pFormatField->RegisterToFieldType( *pFieldType ); + pFormatField->GetField()->ChgTyp( pFieldType ); + } + pFieldType->SetSeqRefNo( *const_cast(static_cast(pField)) ); + } + break; + case SwFieldIds::User: + bInsFieldType = static_cast(pField->GetTyp())->IsDeleted(); + break; + + case SwFieldIds::Dde: + if( pDoc->getIDocumentFieldsAccess().IsNewFieldLst() ) + static_cast(pField->GetTyp())->IncRefCnt(); + bInsFieldType = static_cast(pField->GetTyp())->IsDeleted(); + break; + + case SwFieldIds::Postit: + if ( pDoc->GetDocShell() ) + { + pDoc->GetDocShell()->Broadcast( SwFormatFieldHint( + &pTextField->GetFormatField(), SwFormatFieldHintWhich::INSERTED)); + } + break; + default: break; + } + if( bInsFieldType ) + pDoc->getIDocumentFieldsAccess().InsDeletedFieldType( *pField->GetTyp() ); + } + } + break; + case RES_TXTATR_FTN : + static_cast(pHint)->ChgTextNode( &rNode ); + break; + case RES_TXTATR_REFMARK: + static_txtattr_cast(pHint)->ChgTextNode( &rNode ); + if( rNode.GetNodes().IsDocNodes() ) + { + // search for a reference with the same name + SwTextAttr* pTmpHt; + for( size_t n = 0, nEnd = Count(); n < nEnd; ++n ) + { + const sal_Int32 *pTmpHtEnd; + const sal_Int32 *pTmpHintEnd; + if (RES_TXTATR_REFMARK == (pTmpHt = Get(n))->Which() && + pHint->GetAttr() == pTmpHt->GetAttr() && + nullptr != ( pTmpHtEnd = pTmpHt->GetEnd() ) && + nullptr != ( pTmpHintEnd = pHint->GetEnd() ) ) + { + SwComparePosition eCmp = ::ComparePosition( + pTmpHt->GetStart(), *pTmpHtEnd, + pHint->GetStart(), *pTmpHintEnd ); + bool bDelOld = true, bChgStart = false, bChgEnd = false; + switch( eCmp ) + { + case SwComparePosition::Before: + case SwComparePosition::Behind: bDelOld = false; break; + + case SwComparePosition::Outside: bChgStart = bChgEnd = true; break; + + case SwComparePosition::CollideEnd: + case SwComparePosition::OverlapBefore: bChgStart = true; break; + case SwComparePosition::CollideStart: + case SwComparePosition::OverlapBehind: bChgEnd = true; break; + default: break; + } + + if( bChgStart ) + { + pHint->SetStart( pTmpHt->GetStart() ); + } + if( bChgEnd ) + pHint->SetEnd(*pTmpHtEnd); + + if( bDelOld ) + { + NoteInHistory( pTmpHt ); + rNode.DestroyAttr( Cut( n-- ) ); + --nEnd; + } + } + } + } + break; + case RES_TXTATR_TOXMARK: + static_txtattr_cast(pHint)->ChgTextNode( &rNode ); + break; + + case RES_TXTATR_CJK_RUBY: + static_txtattr_cast(pHint)->InitRuby(rNode); + break; + + case RES_TXTATR_META: + case RES_TXTATR_METAFIELD: + static_txtattr_cast(pHint)->ChgTextNode( &rNode ); + break; + + case RES_CHRATR_HIDDEN: + rNode.SetCalcHiddenCharFlags(); + break; + } + + if( SetAttrMode::DONTEXPAND & nMode ) + pHint->SetDontExpand( true ); + + // special handling for SwTextAttrs without end: + // 1) they cannot overlap + // 2) if two fields are adjacent, they must not be merged into one + // this is guaranteed by inserting a CH_TXTATR_* into the paragraph text! + sal_Int32 nHtStart = pHint->GetStart(); + if( !pHtEnd ) + { + Insert( pHint ); + NoteInHistory(pHint, true); + CalcFlags(); +#ifdef DBG_UTIL + if( !rNode.GetDoc()->IsInReading() ) + CHECK; +#endif + // ... and notify listeners + if(rNode.HasWriterListeners()) + { + SwUpdateAttr aHint( + nHtStart, + nHtStart, + nWhich); + + rNode.ModifyNotification(nullptr,&aHint); + } + + return true; + } + + // from here on, pHint is known to have an end index! + + if( *pHtEnd < nHtStart ) + { + assert(*pHtEnd >= nHtStart); + + // just swap the nonsense: + pHint->SetStart(*pHtEnd); + pHint->SetEnd(nHtStart); + nHtStart = pHint->GetStart(); + } + + // I need this value later on for notification but the pointer may become invalid + const sal_Int32 nHintEnd = *pHtEnd; + const bool bNoHintAdjustMode = bool(SetAttrMode::NOHINTADJUST & nMode); + + // handle nesting attributes: inserting may fail due to overlap! + if (pHint->IsNesting()) + { + const bool bRet( + TryInsertNesting(rNode, *static_txtattr_cast(pHint))); + if (!bRet) return false; + } + // Currently REFMARK and TOXMARK have OverlapAllowed set to true. + // These attributes may be inserted directly. + // Also attributes without length may be inserted directly. + // SETATTR_NOHINTADJUST is set e.g., during undo. + // Portion building in not necessary during XML import. + else if ( !bNoHintAdjustMode && + !pHint->IsOverlapAllowedAttr() && + !rNode.GetDoc()->IsInXMLImport() && + ( RES_TXTATR_AUTOFMT == nWhich || + RES_TXTATR_CHARFMT == nWhich ) ) + { + assert( nWhich != RES_TXTATR_AUTOFMT || + static_cast(pHint->GetAttr()).GetStyleHandle()->GetPool() == + &rNode.GetDoc()->GetAttrPool()); + + BuildPortions( rNode, *pHint, nMode ); + + if ( nHtStart < nHintEnd ) // skip merging for 0-length attributes + MergePortions( rNode ); + } + else + { + // There may be more than one character style at the current position. + // Take care of the sort number. + // Special case ruby portion: During import, the ruby attribute is set + // multiple times + // Special case hyperlink: During import, the ruby attribute is set + // multiple times + // FME 2007-11-08 #i82989# in NOHINTADJUST mode, we want to insert + // character attributes directly + if ( RES_TXTATR_CHARFMT == nWhich && !bNoHintAdjustMode ) + { + BuildPortions( rNode, *pHint, nMode ); + } + else + { + // #i82989# Check sort numbers in NoHintAdjustMode + if ( RES_TXTATR_CHARFMT == nWhich ) + lcl_CheckSortNumber(*this, *static_txtattr_cast(pHint)); + + Insert( pHint ); + NoteInHistory( pHint, true ); + } + } + + // ... and notify listeners + if ( rNode.HasWriterListeners() ) + { + SwUpdateAttr aHint(nHtStart, nHintEnd, nWhich, aWhichSublist); + + rNode.ModifyNotification( nullptr, &aHint ); + } + +#ifdef DBG_UTIL + if( !bNoHintAdjustMode && !rNode.GetDoc()->IsInReading() ) + CHECK; +#endif + + return true; +} + +void SwpHints::DeleteAtPos( const size_t nPos ) +{ + assert(!m_bStartMapNeedsSorting && "deleting at pos and the list needs sorting?"); + + SwTextAttr *pHint = Get(nPos); + assert( pHint->m_pHints == this ); + // ChainDelete( pHint ); + NoteInHistory( pHint ); + + // optimization: nPos is the position in the Starts array + SwTextAttr *pHt = m_HintsByStart[ nPos ]; + m_HintsByStart.erase( m_HintsByStart.begin() + nPos ); + + if (m_bStartMapNeedsSorting) + ResortStartMap(); + if (m_bEndMapNeedsSorting) + ResortEndMap(); + if (m_bWhichMapNeedsSorting) + ResortWhichMap(); + + auto findIt = std::lower_bound(m_HintsByEnd.begin(), m_HintsByEnd.end(), pHt, CompareSwpHtEnd()); + assert(*findIt == pHt); + m_HintsByEnd.erase(findIt); + + auto findIt2 = std::lower_bound(m_HintsByWhichAndStart.begin(), m_HintsByWhichAndStart.end(), pHt, CompareSwpHtWhichStart()); + assert(*findIt2 == pHt); + m_HintsByWhichAndStart.erase(findIt2); + + pHt->m_pHints = nullptr; + + if( pHint->Which() == RES_TXTATR_FIELD ) + { + SwTextField *const pTextField(static_txtattr_cast(pHint)); + const SwFieldType* pFieldTyp = pTextField->GetFormatField().GetField()->GetTyp(); + if( SwFieldIds::Dde == pFieldTyp->Which() ) + { + const SwTextNode* pNd = pTextField->GetpTextNode(); + if( pNd && pNd->GetNodes().IsDocNodes() ) + const_cast(static_cast(pFieldTyp))->DecRefCnt(); + pTextField->ChgTextNode(nullptr); + } + else if (m_bHiddenByParaField + && m_rParent.GetDoc()->FieldCanHideParaWeight(pFieldTyp->Which())) + { + m_bCalcHiddenParaField = true; + } + } + else if ( pHint->Which() == RES_TXTATR_ANNOTATION ) + { + SwTextField *const pTextField(static_txtattr_cast(pHint)); + const_cast(pTextField->GetFormatField()).Broadcast( + SwFormatFieldHint(&pTextField->GetFormatField(), SwFormatFieldHintWhich::REMOVED)); + } + + CalcFlags(); + CHECK_NOTMERGED; // called from BuildPortions +} + +/// delete the hint +/// precondition: pTextHt must be in this array +void SwpHints::Delete( SwTextAttr const * pTextHt ) +{ + const size_t nPos = GetIndexOf( pTextHt ); + assert(SAL_MAX_SIZE != nPos); + if( SAL_MAX_SIZE != nPos ) + DeleteAtPos( nPos ); +} + +void SwTextNode::ClearSwpHintsArr( bool bDelFields ) +{ + if ( HasHints() ) + { + size_t nPos = 0; + while ( nPos < m_pSwpHints->Count() ) + { + SwTextAttr* pDel = m_pSwpHints->Get( nPos ); + bool bDel = false; + + switch( pDel->Which() ) + { + case RES_TXTATR_FLYCNT: + case RES_TXTATR_FTN: + break; + + case RES_TXTATR_FIELD: + case RES_TXTATR_ANNOTATION: + case RES_TXTATR_INPUTFIELD: + if( bDelFields ) + bDel = true; + break; + default: + bDel = true; break; + } + + if( bDel ) + { + m_pSwpHints->DeleteAtPos( nPos ); + DestroyAttr( pDel ); + } + else + ++nPos; + } + } +} + +LanguageType SwTextNode::GetLang( const sal_Int32 nBegin, const sal_Int32 nLen, + sal_uInt16 nScript ) const +{ + LanguageType nRet = LANGUAGE_DONTKNOW; + + if ( ! nScript ) + { + nScript = g_pBreakIt->GetRealScriptOfText( m_Text, nBegin ); + } + + // #i91465# Consider nScript if pSwpHints == 0 + const sal_uInt16 nWhichId = GetWhichOfScript( RES_CHRATR_LANGUAGE, nScript ); + + if ( HasHints() ) + { + const sal_Int32 nEnd = nBegin + nLen; + const size_t nSize = m_pSwpHints->Count(); + for ( size_t i = 0; i < nSize; ++i ) + { + const SwTextAttr *pHt = m_pSwpHints->Get(i); + const sal_Int32 nAttrStart = pHt->GetStart(); + if( nEnd < nAttrStart ) + break; + + const sal_uInt16 nWhich = pHt->Which(); + + if( nWhichId == nWhich || + ( ( pHt->IsCharFormatAttr() || RES_TXTATR_AUTOFMT == nWhich ) && CharFormat::IsItemIncluded( nWhichId, pHt ) ) ) + { + const sal_Int32 *pEndIdx = pHt->End(); + // do the attribute and the range overlap? + if( !pEndIdx ) + continue; + if( nLen ) + { + if( nAttrStart >= nEnd || nBegin >= *pEndIdx ) + continue; + } + else if( nBegin != nAttrStart || ( nAttrStart != *pEndIdx && nBegin )) + { + if( nAttrStart >= nBegin ) + continue; + if( pHt->DontExpand() ? nBegin >= *pEndIdx : nBegin > *pEndIdx) + continue; + } + const SfxPoolItem* pItem = CharFormat::GetItem( *pHt, nWhichId ); + const LanguageType nLng = static_cast(pItem)->GetLanguage(); + + // does the attribute completely cover the range? + if( nAttrStart <= nBegin && nEnd <= *pEndIdx ) + nRet = nLng; + else if( LANGUAGE_DONTKNOW == nRet ) + nRet = nLng; // partial overlap, the first one wins + } + } + } + if( LANGUAGE_DONTKNOW == nRet ) + { + nRet = static_cast(GetSwAttrSet().Get( nWhichId )).GetLanguage(); + if( LANGUAGE_DONTKNOW == nRet ) + nRet = GetAppLanguage(); + } + return nRet; +} + +sal_Unicode GetCharOfTextAttr( const SwTextAttr& rAttr ) +{ + sal_Unicode cRet = CH_TXTATR_BREAKWORD; + switch ( rAttr.Which() ) + { + case RES_TXTATR_REFMARK: + case RES_TXTATR_TOXMARK: + case RES_TXTATR_ANNOTATION: + cRet = CH_TXTATR_INWORD; + break; + + case RES_TXTATR_FIELD: + case RES_TXTATR_FLYCNT: + case RES_TXTATR_FTN: + case RES_TXTATR_META: + case RES_TXTATR_METAFIELD: + { + cRet = CH_TXTATR_BREAKWORD; + } + break; + + default: + assert(!"GetCharOfTextAttr: unknown attr"); + break; + } + return cRet; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/txatbase.cxx b/sw/source/core/txtnode/txatbase.cxx new file mode 100644 index 000000000..4609bd841 --- /dev/null +++ b/sw/source/core/txtnode/txatbase.cxx @@ -0,0 +1,163 @@ +/* -*- 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 +#include +#include +#include +#include + +SwTextAttr::SwTextAttr( SfxPoolItem& rAttr, sal_Int32 nStart ) + : m_pAttr( &rAttr ) + , m_nStart( nStart ) + , m_bDontExpand( false ) + , m_bLockExpandFlag( false ) + , m_bDontMoveAttr( false ) + , m_bCharFormatAttr( false ) + , m_bOverlapAllowedAttr( false ) + , m_bPriorityAttr( false ) + , m_bDontExpandStart( false ) + , m_bNesting( false ) + , m_bHasDummyChar( false ) + , m_bFormatIgnoreStart(false) + , m_bFormatIgnoreEnd(false) + , m_bHasContent( false ) +{ +} + +SwTextAttr::~SwTextAttr() COVERITY_NOEXCEPT_FALSE +{ +} + +const sal_Int32* SwTextAttr::GetEnd() const +{ + return nullptr; +} + +void SwTextAttr::SetEnd(sal_Int32 ) +{ + assert(false); +} + +void SwTextAttr::Destroy( SwTextAttr * pToDestroy, SfxItemPool& rPool ) +{ + if (!pToDestroy) return; + SfxPoolItem * const pAttr = pToDestroy->m_pAttr; + delete pToDestroy; + rPool.Remove( *pAttr ); +} + +bool SwTextAttr::operator==( const SwTextAttr& rAttr ) const +{ + return GetAttr() == rAttr.GetAttr(); +} + +SwTextAttrEnd::SwTextAttrEnd( SfxPoolItem& rAttr, + sal_Int32 nStart, sal_Int32 nEnd ) : + SwTextAttr( rAttr, nStart ), m_nEnd( nEnd ) +{ +} + +const sal_Int32* SwTextAttrEnd::GetEnd() const +{ + return & m_nEnd; +} + +void SwTextAttrEnd::SetEnd(sal_Int32 n) +{ + m_nEnd = n; + if (m_pHints) + m_pHints->EndPosChanged(); +} + +void SwTextAttr::dumpAsXml(xmlTextWriterPtr pWriter) const +{ + xmlTextWriterStartElement(pWriter, BAD_CAST("SwTextAttr")); + + xmlTextWriterWriteAttribute(pWriter, BAD_CAST("start"), BAD_CAST(OString::number(m_nStart).getStr())); + if (End()) + xmlTextWriterWriteAttribute(pWriter, BAD_CAST("end"), BAD_CAST(OString::number(*End()).getStr())); + xmlTextWriterWriteAttribute(pWriter, BAD_CAST("whichId"), BAD_CAST(OString::number(Which()).getStr())); + const char* pWhich = nullptr; + std::optional oValue; + switch (Which()) + { + case RES_TXTATR_AUTOFMT: + pWhich = "autofmt"; + break; + case RES_TXTATR_ANNOTATION: + pWhich = "annotation"; + break; + case RES_TXTATR_FLYCNT: + pWhich = "fly content"; + break; + case RES_TXTATR_CHARFMT: + { + pWhich = "character format"; + if (SwCharFormat* pCharFormat = GetCharFormat().GetCharFormat()) + oValue = OString("name: " + OUStringToOString(pCharFormat->GetName(), RTL_TEXTENCODING_UTF8)); + break; + } + case RES_TXTATR_INETFMT: + { + pWhich = "inet format"; + const SwFormatINetFormat& rFormat = GetINetFormat(); + oValue = OString("url: " + rFormat.GetValue().toUtf8()); + break; + } + case RES_TXTATR_CJK_RUBY: + { + pWhich = "ruby"; + const SwFormatRuby& rFormat = GetRuby(); + oValue = OString("rubytext: " + rFormat.GetText().toUtf8()); + break; + } + case RES_TXTATR_META: + { + pWhich = "meta"; + break; + } + case RES_TXTATR_FIELD: + { + pWhich = "field"; + break; + } + default: + break; + } + if (pWhich) + xmlTextWriterWriteAttribute(pWriter, BAD_CAST("which"), BAD_CAST(pWhich)); + if (oValue) + xmlTextWriterWriteAttribute(pWriter, BAD_CAST("value"), BAD_CAST(oValue->getStr())); + switch (Which()) + { + case RES_TXTATR_AUTOFMT: + GetAutoFormat().dumpAsXml(pWriter); + break; + case RES_TXTATR_FIELD: + GetFormatField().dumpAsXml(pWriter); + break; + default: + break; + } + + xmlTextWriterEndElement(pWriter); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/txatritr.cxx b/sw/source/core/txtnode/txatritr.cxx new file mode 100644 index 000000000..b90f1060f --- /dev/null +++ b/sw/source/core/txtnode/txatritr.cxx @@ -0,0 +1,218 @@ +/* -*- 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 + +#include +#include +#include +#include +#include +#include +#include + +using namespace ::com::sun::star; + +SwScriptIterator::SwScriptIterator( + const OUString& rStr, sal_Int32 nStt, bool const bFrwrd) + : m_rText(rStr) + , m_nChgPos(rStr.getLength()) + , m_nCurScript(i18n::ScriptType::WEAK) + , m_bForward(bFrwrd) +{ + assert(g_pBreakIt && g_pBreakIt->GetBreakIter().is()); + if ( ! bFrwrd && nStt ) + --nStt; + + sal_Int32 nPos = nStt; + m_nCurScript = g_pBreakIt->GetBreakIter()->getScriptType(m_rText, nPos); + if( i18n::ScriptType::WEAK == m_nCurScript ) + { + if( nPos ) + { + nPos = g_pBreakIt->GetBreakIter()->beginOfScript( + m_rText, nPos, m_nCurScript); + if (nPos > 0 && nPos < m_rText.getLength()) + { + nStt = --nPos; + m_nCurScript = + g_pBreakIt->GetBreakIter()->getScriptType(m_rText,nPos); + } + } + } + + m_nChgPos = m_bForward + ? g_pBreakIt->GetBreakIter()->endOfScript( + m_rText, nStt, m_nCurScript) + : g_pBreakIt->GetBreakIter()->beginOfScript( + m_rText, nStt, m_nCurScript); +} + +void SwScriptIterator::Next() +{ + assert(g_pBreakIt && g_pBreakIt->GetBreakIter().is()); + if (m_bForward && m_nChgPos >= 0 && m_nChgPos < m_rText.getLength()) + { + m_nCurScript = + g_pBreakIt->GetBreakIter()->getScriptType(m_rText, m_nChgPos); + m_nChgPos = g_pBreakIt->GetBreakIter()->endOfScript( + m_rText, m_nChgPos, m_nCurScript); + } + else if (!m_bForward && m_nChgPos > 0) + { + --m_nChgPos; + m_nCurScript = + g_pBreakIt->GetBreakIter()->getScriptType(m_rText, m_nChgPos); + m_nChgPos = g_pBreakIt->GetBreakIter()->beginOfScript( + m_rText, m_nChgPos, m_nCurScript); + } +} + +SwLanguageIterator::SwLanguageIterator( const SwTextNode& rTNd, + sal_Int32 nStt ) + : m_aScriptIter( rTNd.GetText(), nStt ), + m_rTextNode( rTNd ), + m_pParaItem( nullptr ), + m_nAttrPos( 0 ), + m_nChgPos( nStt ) +{ + SearchNextChg(); +} + +bool SwLanguageIterator::Next() +{ + bool bRet = false; + if (m_nChgPos < m_aScriptIter.GetText().getLength()) + { + bRet = true; + if( !m_aStack.empty() ) + { + do { + const SwTextAttr* pHt = m_aStack.front(); + const sal_Int32 nEndPos = *pHt->End(); + if( m_nChgPos >= nEndPos ) + m_aStack.pop_front(); + else + break; + } while( !m_aStack.empty() ); + } + + if( !m_aStack.empty() ) + { + const size_t nSavePos = m_nAttrPos; + SearchNextChg(); + if( !m_aStack.empty() ) + { + const SwTextAttr* pHt = m_aStack.front(); + const sal_Int32 nEndPos = *pHt->End(); + if( m_nChgPos >= nEndPos ) + { + m_nChgPos = nEndPos; + m_nAttrPos = nSavePos; + + if( RES_TXTATR_CHARFMT == pHt->Which() ) + { + const sal_uInt16 nWId = GetWhichOfScript( RES_CHRATR_LANGUAGE, m_aScriptIter.GetCurrScript() ); + m_pCurrentItem = &pHt->GetCharFormat().GetCharFormat()->GetFormatAttr(nWId); + } + else + m_pCurrentItem = &pHt->GetAttr(); + + m_aStack.pop_front(); + } + } + } + else + SearchNextChg(); + } + return bRet; +} + +void SwLanguageIterator::AddToStack( const SwTextAttr& rAttr ) +{ + size_t nIns = 0; + const sal_Int32 nEndPos = *rAttr.End(); + for( ; nIns < m_aStack.size(); ++nIns ) + if( *m_aStack[ nIns ]->End() > nEndPos ) + break; + + m_aStack.insert( m_aStack.begin() + nIns, &rAttr ); +} + +void SwLanguageIterator::SearchNextChg() +{ + sal_uInt16 nWh = 0; + if( m_nChgPos == m_aScriptIter.GetScriptChgPos() ) + { + m_aScriptIter.Next(); + m_pParaItem = nullptr; + m_nAttrPos = 0; // must be restart at the beginning, because + // some attributes can start before or inside + // the current scripttype! + m_aStack.clear(); + } + if( !m_pParaItem ) + { + nWh = GetWhichOfScript( RES_CHRATR_LANGUAGE, m_aScriptIter.GetCurrScript() ); + m_pParaItem = &m_rTextNode.GetSwAttrSet().Get( nWh ); + } + + sal_Int32 nStt = m_nChgPos; + m_nChgPos = m_aScriptIter.GetScriptChgPos(); + m_pCurrentItem = m_pParaItem; + + const SwpHints* pHts = m_rTextNode.GetpSwpHints(); + if( pHts ) + { + if( !nWh ) + { + nWh = GetWhichOfScript( RES_CHRATR_LANGUAGE, m_aScriptIter.GetCurrScript() ); + } + + const SfxPoolItem* pItem = nullptr; + for( ; m_nAttrPos < pHts->Count(); ++m_nAttrPos ) + { + const SwTextAttr* pHt = pHts->Get( m_nAttrPos ); + const sal_Int32* pEnd = pHt->End(); + const sal_Int32 nHtStt = pHt->GetStart(); + if( nHtStt < nStt && ( !pEnd || *pEnd <= nStt )) + continue; + + if( nHtStt >= m_nChgPos ) + break; + + pItem = CharFormat::GetItem( *pHt, nWh ); + if ( pItem ) + { + if( nHtStt > nStt ) + { + if( m_nChgPos > nHtStt ) + m_nChgPos = nHtStt; + break; + } + AddToStack( *pHt ); + m_pCurrentItem = pItem; + if( *pEnd < m_nChgPos ) + m_nChgPos = *pEnd; + } + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/txtatr2.cxx b/sw/source/core/txtnode/txtatr2.cxx new file mode 100644 index 000000000..dd036d07e --- /dev/null +++ b/sw/source/core/txtnode/txtatr2.cxx @@ -0,0 +1,314 @@ +/* -*- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +SwTextCharFormat::SwTextCharFormat( SwFormatCharFormat& rAttr, + sal_Int32 nStt, sal_Int32 nEnd ) + : SwTextAttr( rAttr, nStt ) + , SwTextAttrEnd( rAttr, nStt, nEnd ) + , m_pTextNode( nullptr ) + , m_nSortNumber( 0 ) +{ + rAttr.m_pTextAttribute = this; + SetCharFormatAttr( true ); +} + +SwTextCharFormat::~SwTextCharFormat( ) +{ +} + +void SwTextCharFormat::ModifyNotification( const SfxPoolItem* pOld, const SfxPoolItem* pNew ) +{ + const sal_uInt16 nWhich = pOld ? pOld->Which() : pNew ? pNew->Which() : 0; + OSL_ENSURE( isCHRATR(nWhich) || (RES_OBJECTDYING == nWhich) + || (RES_ATTRSET_CHG == nWhich) || (RES_FMT_CHG == nWhich), + "SwTextCharFormat::Modify(): unknown Modify"); + + if ( m_pTextNode ) + { + SwUpdateAttr aUpdateAttr( + GetStart(), + *GetEnd(), + nWhich); + + m_pTextNode->ModifyNotification( &aUpdateAttr, &aUpdateAttr ); + } +} + +bool SwTextCharFormat::GetInfo( SfxPoolItem const & rInfo ) const +{ + return RES_AUTOFMT_DOCNODE != rInfo.Which() || !m_pTextNode || + &m_pTextNode->GetNodes() != static_cast(rInfo).pNodes; +} + +SwTextAttrNesting::SwTextAttrNesting( SfxPoolItem & i_rAttr, + const sal_Int32 i_nStart, const sal_Int32 i_nEnd ) + : SwTextAttr( i_rAttr, i_nStart ) + , SwTextAttrEnd( i_rAttr, i_nStart, i_nEnd ) +{ + SetDontExpand( true ); // never expand this attribute + // lock the expand flag: simple guarantee that nesting will not be + // invalidated by expand operations + SetLockExpandFlag( true ); + SetDontExpandStartAttr( true ); + SetNesting( true ); +} + +SwTextAttrNesting::~SwTextAttrNesting() +{ +} + +SwTextINetFormat::SwTextINetFormat( SwFormatINetFormat& rAttr, + sal_Int32 nStart, sal_Int32 nEnd ) + : SwTextAttr( rAttr, nStart ) + , SwTextAttrNesting( rAttr, nStart, nEnd ) + , SwClient( nullptr ) + , m_pTextNode( nullptr ) + , m_bVisited( false ) + , m_bVisitedValid( false ) +{ + rAttr.mpTextAttr = this; + SetCharFormatAttr( true ); +} + +SwTextINetFormat::~SwTextINetFormat( ) +{ +} + +SwCharFormat* SwTextINetFormat::GetCharFormat() +{ + const SwFormatINetFormat& rFormat = SwTextAttrEnd::GetINetFormat(); + SwCharFormat* pRet = nullptr; + + if (!rFormat.GetValue().isEmpty()) + { + SwDoc* pDoc = GetTextNode().GetDoc(); + if( !IsVisitedValid() ) + { + SetVisited( pDoc->IsVisitedURL( rFormat.GetValue() ) ); + SetVisitedValid( true ); + } + + const sal_uInt16 nId = IsVisited() ? rFormat.GetVisitedFormatId() : rFormat.GetINetFormatId(); + const OUString& rStr = IsVisited() ? rFormat.GetVisitedFormat() : rFormat.GetINetFormat(); + if (rStr.isEmpty()) + { + OSL_ENSURE( false, " - missing character format at hyperlink attribute"); + } + + // JP 10.02.2000, Bug 72806: don't modify the doc for getting the + // correct charstyle. + bool bResetMod = !pDoc->getIDocumentState().IsModified(); + Link aOle2Lnk; + if ( bResetMod ) + { + aOle2Lnk = pDoc->GetOle2Link(); + pDoc->SetOle2Link( Link() ); + } + + pRet = IsPoolUserFormat( nId ) + ? pDoc->FindCharFormatByName( rStr ) + : pDoc->getIDocumentStylePoolAccess().GetCharFormatFromPool( nId ); + + if ( bResetMod ) + { + pDoc->getIDocumentState().ResetModified(); + pDoc->SetOle2Link( aOle2Lnk ); + } + } + + if ( pRet ) + pRet->Add( this ); + else + EndListeningAll(); + + return pRet; +} + +void SwTextINetFormat::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew ) +{ + const sal_uInt16 nWhich = pOld ? pOld->Which() : pNew ? pNew->Which() : 0; + OSL_ENSURE( isCHRATR(nWhich) || (RES_OBJECTDYING == nWhich) + || (RES_ATTRSET_CHG == nWhich) || (RES_FMT_CHG == nWhich), + "SwTextINetFormat::Modify(): unknown Modify"); + + if ( m_pTextNode ) + { + SwUpdateAttr aUpdateAttr( + GetStart(), + *GetEnd(), + nWhich); + + m_pTextNode->ModifyNotification( &aUpdateAttr, &aUpdateAttr ); + } +} + +bool SwTextINetFormat::GetInfo( SfxPoolItem& rInfo ) const +{ + return RES_AUTOFMT_DOCNODE != rInfo.Which() || !m_pTextNode || + &m_pTextNode->GetNodes() != static_cast(rInfo).pNodes; +} + +bool SwTextINetFormat::IsProtect( ) const +{ + return m_pTextNode && m_pTextNode->IsProtect(); +} + +SwTextRuby::SwTextRuby( SwFormatRuby& rAttr, + sal_Int32 nStart, sal_Int32 nEnd ) + : SwTextAttr( rAttr, nStart ) + , SwTextAttrNesting( rAttr, nStart, nEnd ) + , SwClient( nullptr ) + , m_pTextNode( nullptr ) +{ + rAttr.m_pTextAttr = this; +} + +SwTextRuby::~SwTextRuby() +{ +} + +void SwTextRuby::Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew ) +{ + const sal_uInt16 nWhich = pOld ? pOld->Which() : pNew ? pNew->Which() : 0; + OSL_ENSURE( isCHRATR(nWhich) || (RES_OBJECTDYING == nWhich) + || (RES_ATTRSET_CHG == nWhich) || (RES_FMT_CHG == nWhich), + "SwTextRuby::Modify(): unknown Modify"); + + if ( m_pTextNode ) + { + SwUpdateAttr aUpdateAttr( + GetStart(), + *GetEnd(), + nWhich); + + m_pTextNode->ModifyNotification( &aUpdateAttr, &aUpdateAttr ); + } +} + +bool SwTextRuby::GetInfo( SfxPoolItem& rInfo ) const +{ + return RES_AUTOFMT_DOCNODE != rInfo.Which() || !m_pTextNode || + &m_pTextNode->GetNodes() != static_cast(rInfo).pNodes; +} + +SwCharFormat* SwTextRuby::GetCharFormat() +{ + const SwFormatRuby& rFormat = SwTextAttrEnd::GetRuby(); + SwCharFormat* pRet = nullptr; + + if( !rFormat.GetText().isEmpty() ) + { + const SwDoc* pDoc = GetTextNode().GetDoc(); + const OUString& rStr = rFormat.GetCharFormatName(); + const sal_uInt16 nId = rStr.isEmpty() + ? static_cast(RES_POOLCHR_RUBYTEXT) + : rFormat.GetCharFormatId(); + + // JP 10.02.2000, Bug 72806: don't modify the doc for getting the + // correct charstyle. + const bool bResetMod = !pDoc->getIDocumentState().IsModified(); + Link aOle2Lnk; + if( bResetMod ) + { + aOle2Lnk = pDoc->GetOle2Link(); + const_cast(pDoc)->SetOle2Link( Link() ); + } + + pRet = IsPoolUserFormat( nId ) + ? pDoc->FindCharFormatByName( rStr ) + : const_cast(pDoc)->getIDocumentStylePoolAccess().GetCharFormatFromPool( nId ); + + if( bResetMod ) + { + const_cast(pDoc)->getIDocumentState().ResetModified(); + const_cast(pDoc)->SetOle2Link( aOle2Lnk ); + } + } + + if( pRet ) + pRet->Add( this ); + else + EndListeningAll(); + + return pRet; +} + +SwTextMeta * +SwTextMeta::CreateTextMeta( + ::sw::MetaFieldManager & i_rTargetDocManager, + SwTextNode *const i_pTargetTextNode, + SwFormatMeta & i_rAttr, + sal_Int32 const i_nStart, + sal_Int32 const i_nEnd, + bool const i_bIsCopy) +{ + if (i_bIsCopy) + { // i_rAttr is already cloned, now call DoCopy to copy the sw::Meta + OSL_ENSURE(i_pTargetTextNode, "cannot copy Meta without target node"); + i_rAttr.DoCopy(i_rTargetDocManager, *i_pTargetTextNode); + } + SwTextMeta *const pTextMeta(new SwTextMeta(i_rAttr, i_nStart, i_nEnd)); + return pTextMeta; +} + +SwTextMeta::SwTextMeta( SwFormatMeta & i_rAttr, + const sal_Int32 i_nStart, const sal_Int32 i_nEnd ) + : SwTextAttr( i_rAttr, i_nStart ) + , SwTextAttrNesting( i_rAttr, i_nStart, i_nEnd ) +{ + i_rAttr.SetTextAttr( this ); + SetHasDummyChar(true); +} + +SwTextMeta::~SwTextMeta() +{ + SwFormatMeta & rFormatMeta( static_cast(GetAttr()) ); + if (rFormatMeta.GetTextAttr() == this) + { + rFormatMeta.SetTextAttr(nullptr); + } +} + +void SwTextMeta::ChgTextNode(SwTextNode * const pNode) +{ + SwFormatMeta & rFormatMeta( static_cast(GetAttr()) ); + if (rFormatMeta.GetTextAttr() == this) + { + rFormatMeta.NotifyChangeTextNode(pNode); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/txtedt.cxx b/sw/source/core/txtnode/txtedt.cxx new file mode 100644 index 000000000..cbc5f4f39 --- /dev/null +++ b/sw/source/core/txtnode/txtedt.cxx @@ -0,0 +1,2288 @@ +/* -*- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include + +using namespace ::com::sun::star; +using namespace ::com::sun::star::frame; +using namespace ::com::sun::star::i18n; +using namespace ::com::sun::star::beans; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::linguistic2; +using namespace ::com::sun::star::smarttags; + +namespace +{ + void DetectAndMarkMissingDictionaries( SwDoc* pDoc, + const uno::Reference< XSpellChecker1 >& xSpell, + const LanguageType eActLang ) + { + if( !pDoc ) + return; + + if( xSpell.is() && !xSpell->hasLanguage( eActLang.get() ) ) + pDoc->SetMissingDictionaries( true ); + else + pDoc->SetMissingDictionaries( false ); + } +} + +struct SwParaIdleData_Impl +{ + SwWrongList* pWrong; // for spell checking + SwGrammarMarkUp* pGrammarCheck; // for grammar checking / proof reading + SwWrongList* pSmartTags; + sal_uLong nNumberOfWords; + sal_uLong nNumberOfAsianWords; + sal_uLong nNumberOfChars; + sal_uLong nNumberOfCharsExcludingSpaces; + bool bWordCountDirty; + SwTextNode::WrongState eWrongDirty; ///< online spell checking needed/done? + bool bGrammarCheckDirty; + bool bSmartTagDirty; + bool bAutoComplDirty; ///< auto complete list dirty + + SwParaIdleData_Impl() : + pWrong ( nullptr ), + pGrammarCheck ( nullptr ), + pSmartTags ( nullptr ), + nNumberOfWords ( 0 ), + nNumberOfAsianWords ( 0 ), + nNumberOfChars ( 0 ), + nNumberOfCharsExcludingSpaces ( 0 ), + bWordCountDirty ( true ), + eWrongDirty ( SwTextNode::WrongState::TODO ), + bGrammarCheckDirty ( true ), + bSmartTagDirty ( true ), + bAutoComplDirty ( true ) {}; +}; + +/* + * This has basically the same function as SwScriptInfo::MaskHiddenRanges, + * only for deleted redlines + */ + +static sal_Int32 +lcl_MaskRedlines( const SwTextNode& rNode, OUStringBuffer& rText, + sal_Int32 nStt, sal_Int32 nEnd, + const sal_Unicode cChar ) +{ + sal_Int32 nNumOfMaskedRedlines = 0; + + const SwDoc& rDoc = *rNode.GetDoc(); + + for ( SwRedlineTable::size_type nAct = rDoc.getIDocumentRedlineAccess().GetRedlinePos( rNode, RedlineType::Any ); nAct < rDoc.getIDocumentRedlineAccess().GetRedlineTable().size(); ++nAct ) + { + const SwRangeRedline* pRed = rDoc.getIDocumentRedlineAccess().GetRedlineTable()[ nAct ]; + + if ( pRed->Start()->nNode > rNode.GetIndex() ) + break; + + if( RedlineType::Delete == pRed->GetType() ) + { + sal_Int32 nRedlineEnd; + sal_Int32 nRedlineStart; + + pRed->CalcStartEnd( rNode.GetIndex(), nRedlineStart, nRedlineEnd ); + + if ( nRedlineEnd < nStt || nRedlineStart > nEnd ) + continue; + + while ( nRedlineStart < nRedlineEnd && nRedlineStart < nEnd ) + { + if (nRedlineStart >= nStt) + { + rText[nRedlineStart] = cChar; + ++nNumOfMaskedRedlines; + } + ++nRedlineStart; + } + } + } + + return nNumOfMaskedRedlines; +} + +/** + * Used for spell checking. Deleted redlines and hidden characters are masked + */ +static bool +lcl_MaskRedlinesAndHiddenText( const SwTextNode& rNode, OUStringBuffer& rText, + sal_Int32 nStt, sal_Int32 nEnd, + const sal_Unicode cChar = CH_TXTATR_INWORD ) +{ + sal_Int32 nRedlinesMasked = 0; + sal_Int32 nHiddenCharsMasked = 0; + + const SwDoc& rDoc = *rNode.GetDoc(); + const bool bShowChg = IDocumentRedlineAccess::IsShowChanges( rDoc.getIDocumentRedlineAccess().GetRedlineFlags() ); + + // If called from word count or from spell checking, deleted redlines + // should be masked: + if ( bShowChg ) + { + nRedlinesMasked = lcl_MaskRedlines( rNode, rText, nStt, nEnd, cChar ); + } + + const bool bHideHidden = !SW_MOD()->GetViewOption(rDoc.GetDocumentSettingManager().get(DocumentSettingId::HTML_MODE))->IsShowHiddenChar(); + + // If called from word count, we want to mask the hidden ranges even + // if they are visible: + if ( bHideHidden ) + { + nHiddenCharsMasked = + SwScriptInfo::MaskHiddenRanges( rNode, rText, nStt, nEnd, cChar ); + } + + return (nRedlinesMasked > 0) || (nHiddenCharsMasked > 0); +} + +/** + * Used for spell checking. Calculates a rectangle for repaint. + */ +static SwRect lcl_CalculateRepaintRect( + SwTextFrame & rTextFrame, SwTextNode & rNode, + sal_Int32 const nChgStart, sal_Int32 const nChgEnd) +{ + TextFrameIndex const iChgStart(rTextFrame.MapModelToView(&rNode, nChgStart)); + TextFrameIndex const iChgEnd(rTextFrame.MapModelToView(&rNode, nChgEnd)); + + SwRect aRect = rTextFrame.GetPaintArea(); + SwRect aTmp = rTextFrame.GetPaintArea(); + + const SwTextFrame* pStartFrame = &rTextFrame; + while( pStartFrame->HasFollow() && + iChgStart >= pStartFrame->GetFollow()->GetOffset()) + pStartFrame = pStartFrame->GetFollow(); + const SwTextFrame* pEndFrame = pStartFrame; + while( pEndFrame->HasFollow() && + iChgEnd >= pEndFrame->GetFollow()->GetOffset()) + pEndFrame = pEndFrame->GetFollow(); + + bool bSameFrame = true; + + if( rTextFrame.HasFollow() ) + { + if( pEndFrame != pStartFrame ) + { + bSameFrame = false; + SwRect aStFrame( pStartFrame->GetPaintArea() ); + { + SwRectFnSet aRectFnSet(pStartFrame); + aRectFnSet.SetLeft( aTmp, aRectFnSet.GetLeft(aStFrame) ); + aRectFnSet.SetRight( aTmp, aRectFnSet.GetRight(aStFrame) ); + aRectFnSet.SetBottom( aTmp, aRectFnSet.GetBottom(aStFrame) ); + } + aStFrame = pEndFrame->GetPaintArea(); + { + SwRectFnSet aRectFnSet(pEndFrame); + aRectFnSet.SetTop( aRect, aRectFnSet.GetTop(aStFrame) ); + aRectFnSet.SetLeft( aRect, aRectFnSet.GetLeft(aStFrame) ); + aRectFnSet.SetRight( aRect, aRectFnSet.GetRight(aStFrame) ); + } + aRect.Union( aTmp ); + while( true ) + { + pStartFrame = pStartFrame->GetFollow(); + if( pStartFrame == pEndFrame ) + break; + aRect.Union( pStartFrame->GetPaintArea() ); + } + } + } + if( bSameFrame ) + { + SwRectFnSet aRectFnSet(pStartFrame); + if( aRectFnSet.GetTop(aTmp) == aRectFnSet.GetTop(aRect) ) + aRectFnSet.SetLeft( aRect, aRectFnSet.GetLeft(aTmp) ); + else + { + SwRect aStFrame( pStartFrame->GetPaintArea() ); + aRectFnSet.SetLeft( aRect, aRectFnSet.GetLeft(aStFrame) ); + aRectFnSet.SetRight( aRect, aRectFnSet.GetRight(aStFrame) ); + aRectFnSet.SetTop( aRect, aRectFnSet.GetTop(aTmp) ); + } + + if( aTmp.Height() > aRect.Height() ) + aRect.Height( aTmp.Height() ); + } + + return aRect; +} + +/** + * Used for automatic styles. Used during RstAttr. + */ +static bool lcl_HaveCommonAttributes( IStyleAccess& rStyleAccess, + const SfxItemSet* pSet1, + sal_uInt16 nWhichId, + const SfxItemSet& rSet2, + std::shared_ptr& pStyleHandle ) +{ + bool bRet = false; + + std::unique_ptr pNewSet; + + if ( !pSet1 ) + { + OSL_ENSURE( nWhichId, "lcl_HaveCommonAttributes not used correctly" ); + if ( SfxItemState::SET == rSet2.GetItemState( nWhichId, false ) ) + { + pNewSet = rSet2.Clone(); + pNewSet->ClearItem( nWhichId ); + } + } + else if ( pSet1->Count() ) + { + SfxItemIter aIter( *pSet1 ); + const SfxPoolItem* pItem = aIter.GetCurItem(); + do + { + if ( SfxItemState::SET == rSet2.GetItemState( pItem->Which(), false ) ) + { + if ( !pNewSet ) + pNewSet = rSet2.Clone(); + pNewSet->ClearItem( pItem->Which() ); + } + + pItem = aIter.NextItem(); + } while (pItem); + } + + if ( pNewSet ) + { + if ( pNewSet->Count() ) + pStyleHandle = rStyleAccess.getAutomaticStyle( *pNewSet, IStyleAccess::AUTO_STYLE_CHAR ); + bRet = true; + } + + return bRet; +} + +/** Delete all attributes + * + * 5 cases: + * 1) The attribute is completely in the deletion range: + * -> delete it + * 2) The end of the attribute is in the deletion range: + * -> delete it, then re-insert it with new end + * 3) The start of the attribute is in the deletion range: + * -> delete it, then re-insert it with new start + * 4) The attribute contains the deletion range: + * Split, i.e., + * -> Delete, re-insert from old start to start of deletion range + * -> insert new attribute from end of deletion range to old end + * 5) The attribute is outside the deletion range + * -> nothing to do + * + * @param rIdx starting position + * @param nLen length of the deletion + * @param nthat ??? + * @param pSet ??? + * @param bInclRefToxMark ??? + */ + +void SwTextNode::RstTextAttr( + const SwIndex &rIdx, + const sal_Int32 nLen, + const sal_uInt16 nWhich, + const SfxItemSet* pSet, + const bool bInclRefToxMark, + const bool bExactRange ) +{ + if ( !GetpSwpHints() ) + return; + + sal_Int32 nStt = rIdx.GetIndex(); + sal_Int32 nEnd = nStt + nLen; + { + // enlarge range for the reset of text attributes in case of an overlapping input field + const SwTextInputField* pTextInputField = dynamic_cast(GetTextAttrAt( nStt, RES_TXTATR_INPUTFIELD, PARENT )); + if ( pTextInputField == nullptr ) + { + pTextInputField = dynamic_cast(GetTextAttrAt(nEnd, RES_TXTATR_INPUTFIELD, PARENT )); + } + if ( pTextInputField != nullptr ) + { + if ( nStt > pTextInputField->GetStart() ) + { + nStt = pTextInputField->GetStart(); + } + if ( nEnd < *(pTextInputField->End()) ) + { + nEnd = *(pTextInputField->End()); + } + } + } + + bool bChanged = false; + + // nMin and nMax initialized to maximum / minimum (inverse) + sal_Int32 nMin = m_Text.getLength(); + sal_Int32 nMax = nStt; + const bool bNoLen = nMin == 0; + + // We have to remember the "new" attributes that have + // been introduced by splitting surrounding attributes (case 2,3,4). + std::vector newAttributes; + std::vector delAttributes; + + // iterate over attribute array until start of attribute is behind deletion range + m_pSwpHints->SortIfNeedBe(); // trigger sorting now, we don't want it during iteration + size_t i = 0; + sal_Int32 nAttrStart = sal_Int32(); + SwTextAttr *pHt = nullptr; + while ( (i < m_pSwpHints->Count()) + && ( ( ( nAttrStart = m_pSwpHints->GetWithoutResorting(i)->GetStart()) < nEnd ) + || nLen==0 ) && !bExactRange) + { + pHt = m_pSwpHints->GetWithoutResorting(i); + + // attributes without end stay in! + // but consider used by Undo + const sal_Int32* const pAttrEnd = pHt->GetEnd(); + const bool bKeepAttrWithoutEnd = + pAttrEnd == nullptr + && ( !bInclRefToxMark + || ( RES_TXTATR_REFMARK != pHt->Which() + && RES_TXTATR_TOXMARK != pHt->Which() + && RES_TXTATR_META != pHt->Which() + && RES_TXTATR_METAFIELD != pHt->Which() ) ); + if ( bKeepAttrWithoutEnd ) + { + + i++; + continue; + } + // attributes with content stay in + if ( pHt->HasContent() ) + { + ++i; + continue; + } + + // Default behavior is to process all attributes: + bool bSkipAttr = false; + std::shared_ptr pStyleHandle; + + // 1. case: We want to reset only the attributes listed in pSet: + if ( pSet ) + { + bSkipAttr = SfxItemState::SET != pSet->GetItemState( pHt->Which(), false ); + if ( bSkipAttr && RES_TXTATR_AUTOFMT == pHt->Which() ) + { + // if the current attribute is an autostyle, we have to check if the autostyle + // and pSet have any attributes in common. If so, pStyleHandle will contain + // a handle to AutoStyle / pSet: + bSkipAttr = !lcl_HaveCommonAttributes( getIDocumentStyleAccess(), pSet, 0, *static_cast(pHt->GetAttr()).GetStyleHandle(), pStyleHandle ); + } + } + else if ( nWhich ) + { + // 2. case: We want to reset only the attributes with WhichId nWhich: + bSkipAttr = nWhich != pHt->Which(); + if ( bSkipAttr && RES_TXTATR_AUTOFMT == pHt->Which() ) + { + bSkipAttr = !lcl_HaveCommonAttributes( getIDocumentStyleAccess(), nullptr, nWhich, *static_cast(pHt->GetAttr()).GetStyleHandle(), pStyleHandle ); + } + } + else if ( !bInclRefToxMark ) + { + // 3. case: Reset all attributes except from ref/toxmarks: + // skip hints with CH_TXTATR here + // (deleting those is ONLY allowed for UNDO!) + bSkipAttr = RES_TXTATR_REFMARK == pHt->Which() + || RES_TXTATR_TOXMARK == pHt->Which() + || RES_TXTATR_META == pHt->Which() + || RES_TXTATR_METAFIELD == pHt->Which(); + } + + if ( bSkipAttr ) + { + i++; + continue; + } + + if (nStt <= nAttrStart) // Case: 1,3,5 + { + const sal_Int32 nAttrEnd = pAttrEnd != nullptr + ? *pAttrEnd + : nAttrStart; + if (nEnd > nAttrStart + || (nEnd == nAttrEnd && nEnd == nAttrStart)) // Case: 1,3 + { + if ( nMin > nAttrStart ) + nMin = nAttrStart; + if ( nMax < nAttrEnd ) + nMax = nAttrEnd; + // If only a no-extent hint is deleted, no resorting is needed + bChanged = bChanged || nEnd > nAttrStart || bNoLen; + if (nAttrEnd <= nEnd) // Case: 1 + { + delAttributes.push_back(pHt); + + if ( pStyleHandle ) + { + SwTextAttr* pNew = MakeTextAttr( *GetDoc(), + *pStyleHandle, nAttrStart, nAttrEnd ); + newAttributes.push_back(pNew); + } + } + else // Case: 3 + { + bChanged = true; + m_pSwpHints->NoteInHistory( pHt ); + // UGLY: this may temporarily destroy the sorting! + pHt->SetStart(nEnd); + m_pSwpHints->NoteInHistory( pHt, true ); + + if ( pStyleHandle && nAttrStart < nEnd ) + { + SwTextAttr* pNew = MakeTextAttr( *GetDoc(), + *pStyleHandle, nAttrStart, nEnd ); + newAttributes.push_back(pNew); + } + } + } + } + else if (pAttrEnd != nullptr) // Case: 2,4,5 + { + if (*pAttrEnd > nStt) // Case: 2,4 + { + if (*pAttrEnd < nEnd) // Case: 2 + { + if ( nMin > nAttrStart ) + nMin = nAttrStart; + if ( nMax < *pAttrEnd ) + nMax = *pAttrEnd; + bChanged = true; + + const sal_Int32 nAttrEnd = *pAttrEnd; + + m_pSwpHints->NoteInHistory( pHt ); + // UGLY: this may temporarily destroy the sorting! + pHt->SetEnd(nStt); + m_pSwpHints->NoteInHistory( pHt, true ); + + if ( pStyleHandle ) + { + SwTextAttr* pNew = MakeTextAttr( *GetDoc(), + *pStyleHandle, nStt, nAttrEnd ); + newAttributes.push_back(pNew); + } + } + else if (nLen) // Case: 4 + { + // for Length 0 both hints would be merged again by + // InsertHint, so leave them alone! + if ( nMin > nAttrStart ) + nMin = nAttrStart; + if ( nMax < *pAttrEnd ) + nMax = *pAttrEnd; + bChanged = true; + const sal_Int32 nTmpEnd = *pAttrEnd; + m_pSwpHints->NoteInHistory( pHt ); + // UGLY: this may temporarily destroy the sorting! + pHt->SetEnd(nStt); + m_pSwpHints->NoteInHistory( pHt, true ); + + if ( pStyleHandle && nStt < nEnd ) + { + SwTextAttr* pNew = MakeTextAttr( *GetDoc(), + *pStyleHandle, nStt, nEnd ); + newAttributes.push_back(pNew); + } + + if( nEnd < nTmpEnd ) + { + SwTextAttr* pNew = MakeTextAttr( *GetDoc(), + pHt->GetAttr(), nEnd, nTmpEnd ); + if ( pNew ) + { + SwTextCharFormat* pCharFormat = dynamic_cast(pHt); + if ( pCharFormat ) + static_txtattr_cast(pNew)->SetSortNumber(pCharFormat->GetSortNumber()); + + newAttributes.push_back(pNew); + } + } + } + } + } + ++i; + } + + if (bExactRange) + { + // Only delete the hints which start at nStt and end at nEnd. + for (i = 0; i < m_pSwpHints->Count(); ++i) + { + SwTextAttr* pHint = m_pSwpHints->Get(i); + if ( (isTXTATR_WITHEND(pHint->Which()) && RES_TXTATR_AUTOFMT != pHint->Which()) + || pHint->GetStart() != nStt) + continue; + + const sal_Int32* pHintEnd = pHint->GetEnd(); + if (!pHintEnd || *pHintEnd != nEnd) + continue; + + delAttributes.push_back(pHint); + } + } + + if (bChanged && !delAttributes.empty()) + { // Delete() calls GetStartOf() - requires sorted hints! + m_pSwpHints->Resort(); + } + + // delay deleting the hints because it re-sorts the hints array + for (SwTextAttr *const pDel : delAttributes) + { + m_pSwpHints->Delete(pDel); + DestroyAttr(pDel); + } + + // delay inserting the hints because it re-sorts the hints array + for (SwTextAttr *const pNew : newAttributes) + { + InsertHint(pNew, SetAttrMode::NOHINTADJUST); + } + + TryDeleteSwpHints(); + + if (bChanged) + { + if ( HasHints() ) + { // possibly sometimes Resort would be sufficient, but... + m_pSwpHints->MergePortions(*this); + } + + // TextFrame's respond to aHint, others to aNew + SwUpdateAttr aHint( + nMin, + nMax, + 0); + + NotifyClients( nullptr, &aHint ); + SwFormatChg aNew( GetFormatColl() ); + NotifyClients( nullptr, &aNew ); + } +} + +static sal_Int32 clipIndexBounds(const OUString &rStr, sal_Int32 nPos) +{ + if (nPos < 0) + return 0; + if (nPos > rStr.getLength()) + return rStr.getLength(); + return nPos; +} + +// Return current word: +// Search from left to right, so find the word before nPos. +// Except if at the start of the paragraph, then return the first word. +// If the first word consists only of whitespace, return an empty string. +OUString SwTextFrame::GetCurWord(SwPosition const& rPos) const +{ + TextFrameIndex const nPos(MapModelToViewPos(rPos)); + SwTextNode *const pTextNode(rPos.nNode.GetNode().GetTextNode()); + assert(pTextNode); + OUString const& rText(GetText()); + assert(sal_Int32(nPos) <= rText.getLength()); // invalid index + + if (rText.isEmpty() || IsHiddenNow()) + return OUString(); + + assert(g_pBreakIt && g_pBreakIt->GetBreakIter().is()); + const uno::Reference< XBreakIterator > &rxBreak = g_pBreakIt->GetBreakIter(); + sal_Int16 nWordType = WordType::DICTIONARY_WORD; + lang::Locale aLocale( g_pBreakIt->GetLocale(pTextNode->GetLang(rPos.nContent.GetIndex())) ); + Boundary aBndry = + rxBreak->getWordBoundary(rText, sal_Int32(nPos), aLocale, nWordType, true); + + // if no word was found use previous word (if any) + if (aBndry.startPos == aBndry.endPos) + { + aBndry = rxBreak->previousWord(rText, sal_Int32(nPos), aLocale, nWordType); + } + + // check if word was found and if it uses a symbol font, if so + // enforce returning an empty string + if (aBndry.endPos != aBndry.startPos + && IsSymbolAt(TextFrameIndex(aBndry.startPos))) + { + aBndry.endPos = aBndry.startPos; + } + + // can have -1 as start/end of bounds not found + aBndry.startPos = clipIndexBounds(rText, aBndry.startPos); + aBndry.endPos = clipIndexBounds(rText, aBndry.endPos); + + return rText.copy(aBndry.startPos, + aBndry.endPos - aBndry.startPos); +} + +SwScanner::SwScanner( const SwTextNode& rNd, const OUString& rText, + const LanguageType* pLang, const ModelToViewHelper& rConvMap, + sal_uInt16 nType, sal_Int32 nStart, sal_Int32 nEnd, bool bClp ) + : SwScanner( + [&rNd](sal_Int32 const nBegin, sal_uInt16 const nScript, bool const bNoChar) + { return rNd.GetLang(nBegin, bNoChar ? 0 : 1, nScript); } + , rText, pLang, rConvMap, nType, nStart, nEnd, bClp) +{ +} + +SwScanner::SwScanner(std::function const& pGetLangOfChar, + const OUString& rText, const LanguageType* pLang, + const ModelToViewHelper& rConvMap, sal_uInt16 nType, sal_Int32 nStart, + sal_Int32 nEnd, bool bClp) + : m_pGetLangOfChar(pGetLangOfChar) + , m_aPreDashReplacementText(rText) + , m_pLanguage(pLang) + , m_ModelToView(rConvMap) + , m_nLength(0) + , m_nOverriddenDashCount(0) + , m_nWordType(nType) + , m_bClip(bClp) +{ + m_nStartPos = m_nBegin = nStart; + m_nEndPos = nEnd; + + //MSWord f.e has special emdash and endash behaviour in that they break + //words for the purposes of word counting, while a hyphen etc. doesn't. + + //The default configuration treats emdash/endash as a word break, but + //additional ones can be added in under tools->options + if (m_nWordType == i18n::WordType::WORD_COUNT) + { + OUString sDashes = officecfg::Office::Writer::WordCount::AdditionalSeparators::get(); + OUStringBuffer aBuf(m_aPreDashReplacementText); + for (sal_Int32 i = m_nStartPos; i < m_nEndPos; ++i) + { + if (i < 0) + continue; + sal_Unicode cChar = aBuf[i]; + if (sDashes.indexOf(cChar) != -1) + { + aBuf[i] = ' '; + ++m_nOverriddenDashCount; + } + } + m_aText = aBuf.makeStringAndClear(); + } + else + m_aText = m_aPreDashReplacementText; + + assert(m_aPreDashReplacementText.getLength() == m_aText.getLength()); + + if ( m_pLanguage ) + { + m_aCurrentLang = *m_pLanguage; + } + else + { + ModelToViewHelper::ModelPosition aModelBeginPos = + m_ModelToView.ConvertToModelPosition( m_nBegin ); + m_aCurrentLang = m_pGetLangOfChar(aModelBeginPos.mnPos, 0, true); + } +} + +namespace +{ + //fdo#45271 for Asian words count characters instead of words + sal_Int32 forceEachAsianCodePointToWord(const OUString &rText, sal_Int32 nBegin, sal_Int32 nLen) + { + if (nLen > 1) + { + const uno::Reference< XBreakIterator > &rxBreak = g_pBreakIt->GetBreakIter(); + + sal_uInt16 nCurrScript = rxBreak->getScriptType( rText, nBegin ); + + sal_Int32 indexUtf16 = nBegin; + rText.iterateCodePoints(&indexUtf16); + + //First character is Asian, consider it a word :-( + if (nCurrScript == i18n::ScriptType::ASIAN) + { + nLen = indexUtf16 - nBegin; + return nLen; + } + + //First character was not Asian, consider appearance of any Asian character + //to be the end of the word + while (indexUtf16 < nBegin + nLen) + { + nCurrScript = rxBreak->getScriptType( rText, indexUtf16 ); + if (nCurrScript == i18n::ScriptType::ASIAN) + { + nLen = indexUtf16 - nBegin; + return nLen; + } + rText.iterateCodePoints(&indexUtf16); + } + } + return nLen; + } +} + +bool SwScanner::NextWord() +{ + m_nBegin = m_nBegin + m_nLength; + Boundary aBound; + + CharClass& rCC = GetAppCharClass(); + LanguageTag aOldLanguageTag = rCC.getLanguageTag(); + + while ( true ) + { + // skip non-letter characters: + while (m_nBegin < m_aText.getLength()) + { + if (m_nBegin >= 0 && !u_isspace(m_aText[m_nBegin])) + { + if ( !m_pLanguage ) + { + const sal_uInt16 nNextScriptType = g_pBreakIt->GetBreakIter()->getScriptType( m_aText, m_nBegin ); + ModelToViewHelper::ModelPosition aModelBeginPos = + m_ModelToView.ConvertToModelPosition( m_nBegin ); + m_aCurrentLang = m_pGetLangOfChar(aModelBeginPos.mnPos, nNextScriptType, false); + } + + if ( m_nWordType != i18n::WordType::WORD_COUNT ) + { + rCC.setLanguageTag( LanguageTag( g_pBreakIt->GetLocale( m_aCurrentLang )) ); + if ( rCC.isLetterNumeric(OUString(m_aText[m_nBegin])) ) + break; + } + else + break; + } + ++m_nBegin; + } + + if ( m_nBegin >= m_aText.getLength() || m_nBegin >= m_nEndPos ) + return false; + + // get the word boundaries + aBound = g_pBreakIt->GetBreakIter()->getWordBoundary( m_aText, m_nBegin, + g_pBreakIt->GetLocale( m_aCurrentLang ), m_nWordType, true ); + OSL_ENSURE( aBound.endPos >= aBound.startPos, "broken aBound result" ); + + // we don't want to include preceding text + // to count words in text with mixed script punctuation correctly, + // but we want to include preceding symbols (eg. percent sign, section sign, + // degree sign defined by dict_word_hu to spell check their affixed forms). + if (m_nWordType == i18n::WordType::WORD_COUNT && aBound.startPos < m_nBegin) + aBound.startPos = m_nBegin; + + //no word boundaries could be found + if(aBound.endPos == aBound.startPos) + return false; + + //if a word before is found it has to be searched for the next + if(aBound.endPos == m_nBegin) + ++m_nBegin; + else + break; + } // end while( true ) + + rCC.setLanguageTag( aOldLanguageTag ); + + // #i89042, as discussed with HDU: don't evaluate script changes for word count. Use whole word. + if ( m_nWordType == i18n::WordType::WORD_COUNT ) + { + m_nBegin = std::max(aBound.startPos, m_nBegin); + m_nLength = 0; + if (aBound.endPos > m_nBegin) + m_nLength = aBound.endPos - m_nBegin; + } + else + { + // we have to differentiate between these cases: + if ( aBound.startPos <= m_nBegin ) + { + OSL_ENSURE( aBound.endPos >= m_nBegin, "Unexpected aBound result" ); + + // restrict boundaries to script boundaries and nEndPos + const sal_uInt16 nCurrScript = g_pBreakIt->GetBreakIter()->getScriptType( m_aText, m_nBegin ); + OUString aTmpWord = m_aText.copy( m_nBegin, aBound.endPos - m_nBegin ); + const sal_Int32 nScriptEnd = m_nBegin + + g_pBreakIt->GetBreakIter()->endOfScript( aTmpWord, 0, nCurrScript ); + const sal_Int32 nEnd = std::min( aBound.endPos, nScriptEnd ); + + // restrict word start to last script change position + sal_Int32 nScriptBegin = 0; + if ( aBound.startPos < m_nBegin ) + { + // search from nBegin backwards until the next script change + aTmpWord = m_aText.copy( aBound.startPos, + m_nBegin - aBound.startPos + 1 ); + nScriptBegin = aBound.startPos + + g_pBreakIt->GetBreakIter()->beginOfScript( aTmpWord, m_nBegin - aBound.startPos, + nCurrScript ); + } + + m_nBegin = std::max( aBound.startPos, nScriptBegin ); + m_nLength = nEnd - m_nBegin; + } + else + { + const sal_uInt16 nCurrScript = g_pBreakIt->GetBreakIter()->getScriptType( m_aText, aBound.startPos ); + OUString aTmpWord = m_aText.copy( aBound.startPos, + aBound.endPos - aBound.startPos ); + const sal_Int32 nScriptEnd = aBound.startPos + + g_pBreakIt->GetBreakIter()->endOfScript( aTmpWord, 0, nCurrScript ); + const sal_Int32 nEnd = std::min( aBound.endPos, nScriptEnd ); + m_nBegin = aBound.startPos; + m_nLength = nEnd - m_nBegin; + } + } + + // optionally clip the result of getWordBoundaries: + if ( m_bClip ) + { + aBound.startPos = std::max( aBound.startPos, m_nStartPos ); + aBound.endPos = std::min( aBound.endPos, m_nEndPos ); + if (aBound.endPos < aBound.startPos) + { + m_nBegin = m_nEndPos; + m_nLength = 0; // found word is outside of search interval + } + else + { + m_nBegin = aBound.startPos; + m_nLength = aBound.endPos - m_nBegin; + } + } + + if( ! m_nLength ) + return false; + + if ( m_nWordType == i18n::WordType::WORD_COUNT ) + m_nLength = forceEachAsianCodePointToWord(m_aText, m_nBegin, m_nLength); + + m_aWord = m_aPreDashReplacementText.copy( m_nBegin, m_nLength ); + + return true; +} + +// Note: this is a clone of SwTextFrame::AutoSpell_, so keep them in sync when fixing things! +bool SwTextNode::Spell(SwSpellArgs* pArgs) +{ + // modify string according to redline information and hidden text + const OUString aOldText( m_Text ); + OUStringBuffer buf(m_Text); + const bool bRestoreString = + lcl_MaskRedlinesAndHiddenText(*this, buf, 0, m_Text.getLength()); + if (bRestoreString) + { // ??? UGLY: is it really necessary to modify m_Text here? + m_Text = buf.makeStringAndClear(); + } + + sal_Int32 nBegin = ( pArgs->pStartNode != this ) + ? 0 + : pArgs->pStartIdx->GetIndex(); + + sal_Int32 nEnd = ( pArgs->pEndNode != this ) + ? m_Text.getLength() + : pArgs->pEndIdx->GetIndex(); + + pArgs->xSpellAlt = nullptr; + + // 4 cases: + + // 1. IsWrongDirty = 0 and GetWrong = 0 + // Everything is checked and correct + // 2. IsWrongDirty = 0 and GetWrong = 1 + // Everything is checked and errors are identified in the wrong list + // 3. IsWrongDirty = 1 and GetWrong = 0 + // Nothing has been checked + // 4. IsWrongDirty = 1 and GetWrong = 1 + // Text has been checked but there is an invalid range in the wrong list + + // Nothing has to be done for case 1. + if ( ( IsWrongDirty() || GetWrong() ) && m_Text.getLength() ) + { + if (nBegin > m_Text.getLength()) + { + nBegin = m_Text.getLength(); + } + if (nEnd > m_Text.getLength()) + { + nEnd = m_Text.getLength(); + } + + if(!IsWrongDirty()) + { + const sal_Int32 nTemp = GetWrong()->NextWrong( nBegin ); + if(nTemp > nEnd) + { + // reset original text + if ( bRestoreString ) + { + m_Text = aOldText; + } + return false; + } + if(nTemp > nBegin) + nBegin = nTemp; + + } + + // In case 2. we pass the wrong list to the scanned, because only + // the words in the wrong list have to be checked + SwScanner aScanner( *this, m_Text, nullptr, ModelToViewHelper(), + WordType::DICTIONARY_WORD, + nBegin, nEnd ); + while( !pArgs->xSpellAlt.is() && aScanner.NextWord() ) + { + const OUString& rWord = aScanner.GetWord(); + + // get next language for next word, consider language attributes + // within the word + LanguageType eActLang = aScanner.GetCurrentLanguage(); + DetectAndMarkMissingDictionaries( GetTextNode()->GetDoc(), pArgs->xSpeller, eActLang ); + + if( rWord.getLength() > 0 && LANGUAGE_NONE != eActLang ) + { + if (pArgs->xSpeller.is()) + { + SvxSpellWrapper::CheckSpellLang( pArgs->xSpeller, eActLang ); + pArgs->xSpellAlt = pArgs->xSpeller->spell( rWord, static_cast(eActLang), + Sequence< PropertyValue >() ); + } + if( pArgs->xSpellAlt.is() ) + { + if (IsSymbolAt(aScanner.GetBegin())) + { + pArgs->xSpellAlt = nullptr; + } + else + { + // make sure the selection build later from the data + // below does not include "in word" character to the + // left and right in order to preserve those. Therefore + // count those "in words" in order to modify the + // selection accordingly. + const sal_Unicode* pChar = rWord.getStr(); + sal_Int32 nLeft = 0; + while (*pChar++ == CH_TXTATR_INWORD) + ++nLeft; + pChar = rWord.getLength() ? rWord.getStr() + rWord.getLength() - 1 : nullptr; + sal_Int32 nRight = 0; + while (pChar && *pChar-- == CH_TXTATR_INWORD) + ++nRight; + + pArgs->pStartNode = this; + pArgs->pEndNode = this; + pArgs->pStartIdx->Assign(this, aScanner.GetEnd() - nRight ); + pArgs->pEndIdx->Assign(this, aScanner.GetBegin() + nLeft ); + } + } + } + } + } + + // reset original text + if ( bRestoreString ) + { + m_Text = aOldText; + } + + return pArgs->xSpellAlt.is(); +} + +void SwTextNode::SetLanguageAndFont( const SwPaM &rPaM, + LanguageType nLang, sal_uInt16 nLangWhichId, + const vcl::Font *pFont, sal_uInt16 nFontWhichId ) +{ + sal_uInt16 aRanges[] = { + nLangWhichId, nLangWhichId, + nFontWhichId, nFontWhichId, + 0, 0, 0 }; + if (!pFont) + aRanges[2] = aRanges[3] = 0; // clear entries with font WhichId + + SwEditShell *pEditShell = GetDoc()->GetEditShell(); + SfxItemSet aSet( pEditShell->GetAttrPool(), aRanges ); + aSet.Put( SvxLanguageItem( nLang, nLangWhichId ) ); + + OSL_ENSURE( pFont, "target font missing?" ); + if (pFont) + { + SvxFontItem aFontItem = static_cast( aSet.Get( nFontWhichId ) ); + aFontItem.SetFamilyName( pFont->GetFamilyName()); + aFontItem.SetFamily( pFont->GetFamilyType()); + aFontItem.SetStyleName( pFont->GetStyleName()); + aFontItem.SetPitch( pFont->GetPitch()); + aFontItem.SetCharSet( pFont->GetCharSet() ); + aSet.Put( aFontItem ); + } + + GetDoc()->getIDocumentContentOperations().InsertItemSet( rPaM, aSet ); + // SetAttr( aSet ); <- Does not set language attribute of empty paragraphs correctly, + // <- because since there is no selection the flag to garbage + // <- collect all attributes is set, and therefore attributes spanned + // <- over empty selection are removed. + +} + +bool SwTextNode::Convert( SwConversionArgs &rArgs ) +{ + // get range of text within node to be converted + // (either all the text or the text within the selection + // when the conversion was started) + const sal_Int32 nTextBegin = ( rArgs.pStartNode == this ) + ? std::min(rArgs.pStartIdx->GetIndex(), m_Text.getLength()) + : 0; + + const sal_Int32 nTextEnd = ( rArgs.pEndNode == this ) + ? std::min(rArgs.pEndIdx->GetIndex(), m_Text.getLength()) + : m_Text.getLength(); + + rArgs.aConvText.clear(); + + // modify string according to redline information and hidden text + const OUString aOldText( m_Text ); + OUStringBuffer buf(m_Text); + const bool bRestoreString = + lcl_MaskRedlinesAndHiddenText(*this, buf, 0, m_Text.getLength()); + if (bRestoreString) + { // ??? UGLY: is it really necessary to modify m_Text here? + m_Text = buf.makeStringAndClear(); + } + + bool bFound = false; + sal_Int32 nBegin = nTextBegin; + sal_Int32 nLen = 0; + LanguageType nLangFound = LANGUAGE_NONE; + if (m_Text.isEmpty()) + { + if (rArgs.bAllowImplicitChangesForNotConvertibleText) + { + // create SwPaM with mark & point spanning empty paragraph + //SwPaM aCurPaM( *this, *this, nBegin, nBegin + nLen ); <-- wrong c-tor, does sth different + SwPaM aCurPaM( *this, 0 ); + + SetLanguageAndFont( aCurPaM, + rArgs.nConvTargetLang, RES_CHRATR_CJK_LANGUAGE, + rArgs.pTargetFont, RES_CHRATR_CJK_FONT ); + } + } + else + { + SwLanguageIterator aIter( *this, nBegin ); + + // Implicit changes require setting new attributes, which in turn destroys + // the attribute sequence on that aIter iterates. We store the necessary + // coordinates and apply those changes after iterating through the text. + typedef std::pair ImplicitChangesRange; + std::vector aImplicitChanges; + + // find non zero length text portion of appropriate language + do { + nLangFound = aIter.GetLanguage(); + bool bLangOk = (nLangFound == rArgs.nConvSrcLang) || + (editeng::HangulHanjaConversion::IsChinese( nLangFound ) && + editeng::HangulHanjaConversion::IsChinese( rArgs.nConvSrcLang )); + + sal_Int32 nChPos = aIter.GetChgPos(); + // the position at the end of the paragraph is COMPLETE_STRING and + // thus must be cut to the end of the actual string. + assert(nChPos != -1); + if (nChPos == -1 || nChPos == COMPLETE_STRING) + { + nChPos = m_Text.getLength(); + } + + nLen = nChPos - nBegin; + bFound = bLangOk && nLen > 0; + if (!bFound) + { + // create SwPaM with mark & point spanning the attributed text + //SwPaM aCurPaM( *this, *this, nBegin, nBegin + nLen ); <-- wrong c-tor, does sth different + SwPaM aCurPaM( *this, nBegin ); + aCurPaM.SetMark(); + aCurPaM.GetPoint()->nContent = nBegin + nLen; + + // check script type of selected text + SwEditShell *pEditShell = GetDoc()->GetEditShell(); + pEditShell->Push(); // save current cursor on stack + pEditShell->SetSelection( aCurPaM ); + bool bIsAsianScript = (SvtScriptType::ASIAN == pEditShell->GetScriptType()); + pEditShell->Pop(SwCursorShell::PopMode::DeleteCurrent); // restore cursor from stack + + if (!bIsAsianScript && rArgs.bAllowImplicitChangesForNotConvertibleText) + { + // Store for later use + aImplicitChanges.emplace_back(nBegin, nBegin+nLen); + } + nBegin = nChPos; // start of next language portion + } + } while (!bFound && aIter.Next()); /* loop while nothing was found and still sth is left to be searched */ + + // Apply implicit changes, if any, now that aIter is no longer used + for (const auto& rImplicitChange : aImplicitChanges) + { + SwPaM aPaM( *this, rImplicitChange.first ); + aPaM.SetMark(); + aPaM.GetPoint()->nContent = rImplicitChange.second; + SetLanguageAndFont( aPaM, rArgs.nConvTargetLang, RES_CHRATR_CJK_LANGUAGE, rArgs.pTargetFont, RES_CHRATR_CJK_FONT ); + } + + } + + // keep resulting text within selection / range of text to be converted + if (nBegin < nTextBegin) + nBegin = nTextBegin; + if (nBegin + nLen > nTextEnd) + nLen = nTextEnd - nBegin; + bool bInSelection = nBegin < nTextEnd; + + if (bFound && bInSelection) // convertible text found within selection/range? + { + OSL_ENSURE( !m_Text.isEmpty(), "convertible text portion missing!" ); + rArgs.aConvText = m_Text.copy(nBegin, nLen); + rArgs.nConvTextLang = nLangFound; + + // position where to start looking in next iteration (after current ends) + rArgs.pStartNode = this; + rArgs.pStartIdx->Assign(this, nBegin + nLen ); + // end position (when we have travelled over the whole document) + rArgs.pEndNode = this; + rArgs.pEndIdx->Assign(this, nBegin ); + } + + // restore original text + if ( bRestoreString ) + { + m_Text = aOldText; + } + + return !rArgs.aConvText.isEmpty(); +} + +// Note: this is a clone of SwTextNode::Spell, so keep them in sync when fixing things! +SwRect SwTextFrame::AutoSpell_(SwTextNode & rNode, sal_Int32 nActPos) +{ + SwRect aRect; + assert(sw::FrameContainsNode(*this, rNode.GetIndex())); + SwTextNode *const pNode(&rNode); + if (!nActPos) + nActPos = COMPLETE_STRING; + + SwAutoCompleteWord& rACW = SwDoc::GetAutoCompleteWords(); + + // modify string according to redline information and hidden text + const OUString aOldText( pNode->GetText() ); + OUStringBuffer buf(pNode->m_Text); + const bool bRestoreString = + lcl_MaskRedlinesAndHiddenText(*pNode, buf, 0, pNode->GetText().getLength()); + if (bRestoreString) + { // ??? UGLY: is it really necessary to modify m_Text here? just for GetLang()? + pNode->m_Text = buf.makeStringAndClear(); + } + + // a change of data indicates that at least one word has been modified + + sal_Int32 nBegin = 0; + sal_Int32 nEnd = pNode->GetText().getLength(); + sal_Int32 nInsertPos = 0; + sal_Int32 nChgStart = COMPLETE_STRING; + sal_Int32 nChgEnd = 0; + sal_Int32 nInvStart = COMPLETE_STRING; + sal_Int32 nInvEnd = 0; + + const bool bAddAutoCmpl = pNode->IsAutoCompleteWordDirty() && + SwViewOption::IsAutoCompleteWords(); + + if( pNode->GetWrong() ) + { + nBegin = pNode->GetWrong()->GetBeginInv(); + if( COMPLETE_STRING != nBegin ) + { + nEnd = std::max(pNode->GetWrong()->GetEndInv(), pNode->GetText().getLength()); + } + + // get word around nBegin, we start at nBegin - 1 + if ( COMPLETE_STRING != nBegin ) + { + if ( nBegin ) + --nBegin; + + LanguageType eActLang = pNode->GetLang( nBegin ); + Boundary aBound = + g_pBreakIt->GetBreakIter()->getWordBoundary( pNode->GetText(), nBegin, + g_pBreakIt->GetLocale( eActLang ), + WordType::DICTIONARY_WORD, true ); + nBegin = aBound.startPos; + } + + // get the position in the wrong list + nInsertPos = pNode->GetWrong()->GetWrongPos( nBegin ); + + // sometimes we have to skip one entry + if( nInsertPos < pNode->GetWrong()->Count() && + nBegin == pNode->GetWrong()->Pos( nInsertPos ) + + pNode->GetWrong()->Len( nInsertPos ) ) + nInsertPos++; + } + + bool bFresh = nBegin < nEnd; + bool bPending(false); + + if( bFresh ) + { + uno::Reference< XSpellChecker1 > xSpell( ::GetSpellChecker() ); + SwDoc* pDoc = pNode->GetDoc(); + + SwScanner aScanner( *pNode, pNode->GetText(), nullptr, ModelToViewHelper(), + WordType::DICTIONARY_WORD, nBegin, nEnd); + + while( aScanner.NextWord() ) + { + const OUString& rWord = aScanner.GetWord(); + nBegin = aScanner.GetBegin(); + sal_Int32 nLen = aScanner.GetLen(); + + // get next language for next word, consider language attributes + // within the word + LanguageType eActLang = aScanner.GetCurrentLanguage(); + DetectAndMarkMissingDictionaries( pDoc, xSpell, eActLang ); + + bool bSpell = xSpell.is() && xSpell->hasLanguage( static_cast(eActLang) ); + if( bSpell && !rWord.isEmpty() ) + { + // check for: bAlter => xHyphWord.is() + OSL_ENSURE(!bSpell || xSpell.is(), "NULL pointer"); + + if( !xSpell->isValid( rWord, static_cast(eActLang), Sequence< PropertyValue >() ) ) + { + sal_Int32 nSmartTagStt = nBegin; + sal_Int32 nDummy = 1; + if ( !pNode->GetSmartTags() || !pNode->GetSmartTags()->InWrongWord( nSmartTagStt, nDummy ) ) + { + if( !pNode->GetWrong() ) + { + pNode->SetWrong( new SwWrongList( WRONGLIST_SPELL ) ); + pNode->GetWrong()->SetInvalid( 0, nEnd ); + } + SwWrongList::FreshState const eState(pNode->GetWrong()->Fresh( + nChgStart, nChgEnd, nBegin, nLen, nInsertPos, nActPos)); + switch (eState) + { + case SwWrongList::FreshState::FRESH: + pNode->GetWrong()->Insert(OUString(), nullptr, nBegin, nLen, nInsertPos++); + break; + case SwWrongList::FreshState::CURSOR: + bPending = true; + [[fallthrough]]; // to mark as invalid + case SwWrongList::FreshState::NOTHING: + nInvStart = nBegin; + nInvEnd = nBegin + nLen; + break; + } + } + } + else if( bAddAutoCmpl && rACW.GetMinWordLen() <= rWord.getLength() ) + { + rACW.InsertWord( rWord, *pDoc ); + } + } + } + } + + // reset original text + // i63141 before calling GetCharRect(..) with formatting! + if ( bRestoreString ) + { + pNode->m_Text = aOldText; + } + if( pNode->GetWrong() ) + { + if( bFresh ) + pNode->GetWrong()->Fresh( nChgStart, nChgEnd, + nEnd, 0, nInsertPos, nActPos ); + + // Calculate repaint area: + + if( nChgStart < nChgEnd ) + { + aRect = lcl_CalculateRepaintRect(*this, rNode, nChgStart, nChgEnd); + + // fdo#71558 notify misspelled word to accessibility + SwViewShell* pViewSh = getRootFrame() ? getRootFrame()->GetCurrShell() : nullptr; + if( pViewSh ) + pViewSh->InvalidateAccessibleParaAttrs( *this ); + } + + pNode->GetWrong()->SetInvalid( nInvStart, nInvEnd ); + pNode->SetWrongDirty( + (COMPLETE_STRING != pNode->GetWrong()->GetBeginInv()) + ? (bPending + ? SwTextNode::WrongState::PENDING + : SwTextNode::WrongState::TODO) + : SwTextNode::WrongState::DONE); + if( !pNode->GetWrong()->Count() && ! pNode->IsWrongDirty() ) + pNode->SetWrong( nullptr ); + } + else + pNode->SetWrongDirty(SwTextNode::WrongState::DONE); + + if( bAddAutoCmpl ) + pNode->SetAutoCompleteWordDirty( false ); + + return aRect; +} + +/** Function: SmartTagScan + + Function scans words in current text and checks them in the + smarttag libraries. If the check returns true to bounds of the + recognized words are stored into a list that is used later for drawing + the underline. + + @return SwRect Repaint area +*/ +SwRect SwTextFrame::SmartTagScan(SwTextNode & rNode) +{ + SwRect aRet; + + assert(sw::FrameContainsNode(*this, rNode.GetIndex())); + SwTextNode *const pNode = &rNode; + const OUString& rText = pNode->GetText(); + + // Iterate over language portions + SmartTagMgr& rSmartTagMgr = SwSmartTagMgr::Get(); + + SwWrongList* pSmartTagList = pNode->GetSmartTags(); + + sal_Int32 nBegin = 0; + sal_Int32 nEnd = rText.getLength(); + + if ( pSmartTagList ) + { + if ( pSmartTagList->GetBeginInv() != COMPLETE_STRING ) + { + nBegin = pSmartTagList->GetBeginInv(); + nEnd = std::min( pSmartTagList->GetEndInv(), rText.getLength() ); + + if ( nBegin < nEnd ) + { + const LanguageType aCurrLang = pNode->GetLang( nBegin ); + const css::lang::Locale aCurrLocale = g_pBreakIt->GetLocale( aCurrLang ); + nBegin = g_pBreakIt->GetBreakIter()->beginOfSentence( rText, nBegin, aCurrLocale ); + nEnd = g_pBreakIt->GetBreakIter()->endOfSentence(rText, nEnd, aCurrLocale); + if (nEnd > rText.getLength() || nEnd < 0) + nEnd = rText.getLength(); + } + } + } + + const sal_uInt16 nNumberOfEntries = pSmartTagList ? pSmartTagList->Count() : 0; + sal_uInt16 nNumberOfRemovedEntries = 0; + sal_uInt16 nNumberOfInsertedEntries = 0; + + // clear smart tag list between nBegin and nEnd: + if ( 0 != nNumberOfEntries ) + { + sal_Int32 nChgStart = COMPLETE_STRING; + sal_Int32 nChgEnd = 0; + const sal_uInt16 nCurrentIndex = pSmartTagList->GetWrongPos( nBegin ); + pSmartTagList->Fresh( nChgStart, nChgEnd, nBegin, nEnd - nBegin, nCurrentIndex, COMPLETE_STRING ); + nNumberOfRemovedEntries = nNumberOfEntries - pSmartTagList->Count(); + } + + if ( nBegin < nEnd ) + { + // Expand the string: + const ModelToViewHelper aConversionMap(*pNode, getRootFrame() /*TODO - replace or expand fields for smart tags?*/); + const OUString& aExpandText = aConversionMap.getViewText(); + + // Ownership ov ConversionMap is passed to SwXTextMarkup object! + uno::Reference const xTextMarkup = + new SwXTextMarkup(pNode, aConversionMap); + + css::uno::Reference< css::frame::XController > xController = pNode->GetDoc()->GetDocShell()->GetController(); + + SwPosition start(*pNode, nBegin); + SwPosition end (*pNode, nEnd); + Reference< css::text::XTextRange > xRange = SwXTextRange::CreateXTextRange(*pNode->GetDoc(), start, &end); + + rSmartTagMgr.RecognizeTextRange(xRange, xTextMarkup, xController); + + sal_Int32 nLangBegin = nBegin; + sal_Int32 nLangEnd; + + // smart tag recognition has to be done for each language portion: + SwLanguageIterator aIter( *pNode, nLangBegin ); + + do + { + const LanguageType nLang = aIter.GetLanguage(); + const css::lang::Locale aLocale = g_pBreakIt->GetLocale( nLang ); + nLangEnd = std::min( nEnd, aIter.GetChgPos() ); + + const sal_Int32 nExpandBegin = aConversionMap.ConvertToViewPosition( nLangBegin ); + const sal_Int32 nExpandEnd = aConversionMap.ConvertToViewPosition( nLangEnd ); + + rSmartTagMgr.RecognizeString(aExpandText, xTextMarkup, xController, aLocale, nExpandBegin, nExpandEnd - nExpandBegin ); + + nLangBegin = nLangEnd; + } + while ( aIter.Next() && nLangEnd < nEnd ); + + pSmartTagList = pNode->GetSmartTags(); + + const sal_uInt16 nNumberOfEntriesAfterRecognize = pSmartTagList ? pSmartTagList->Count() : 0; + nNumberOfInsertedEntries = nNumberOfEntriesAfterRecognize - ( nNumberOfEntries - nNumberOfRemovedEntries ); + } + + if( pSmartTagList ) + { + // Update WrongList stuff + pSmartTagList->SetInvalid( COMPLETE_STRING, 0 ); + pNode->SetSmartTagDirty( COMPLETE_STRING != pSmartTagList->GetBeginInv() ); + + if( !pSmartTagList->Count() && !pNode->IsSmartTagDirty() ) + pNode->SetSmartTags( nullptr ); + + // Calculate repaint area: + if ( nBegin < nEnd && ( 0 != nNumberOfRemovedEntries || + 0 != nNumberOfInsertedEntries ) ) + { + aRet = lcl_CalculateRepaintRect(*this, rNode, nBegin, nEnd); + } + } + else + pNode->SetSmartTagDirty( false ); + + return aRet; +} + +void SwTextFrame::CollectAutoCmplWrds(SwTextNode & rNode, sal_Int32 nActPos) +{ + assert(sw::FrameContainsNode(*this, rNode.GetIndex())); (void) this; + SwTextNode *const pNode(&rNode); + if (!nActPos) + nActPos = COMPLETE_STRING; + + SwDoc* pDoc = pNode->GetDoc(); + SwAutoCompleteWord& rACW = SwDoc::GetAutoCompleteWords(); + + sal_Int32 nBegin = 0; + sal_Int32 nEnd = pNode->GetText().getLength(); + sal_Int32 nLen; + bool bACWDirty = false; + + if( nBegin < nEnd ) + { + int nCnt = 200; + SwScanner aScanner( *pNode, pNode->GetText(), nullptr, ModelToViewHelper(), + WordType::DICTIONARY_WORD, nBegin, nEnd ); + while( aScanner.NextWord() ) + { + nBegin = aScanner.GetBegin(); + nLen = aScanner.GetLen(); + if( rACW.GetMinWordLen() <= nLen ) + { + const OUString& rWord = aScanner.GetWord(); + + if( nActPos < nBegin || ( nBegin + nLen ) < nActPos ) + { + if( rACW.GetMinWordLen() <= rWord.getLength() ) + rACW.InsertWord( rWord, *pDoc ); + } + else + bACWDirty = true; + } + if( !--nCnt ) + { + // don't wait for TIMER here, so we can finish big paragraphs + if (Application::AnyInput(VCL_INPUT_ANY & VclInputFlags(~VclInputFlags::TIMER))) + return; + nCnt = 100; + } + } + } + + if (!bACWDirty) + pNode->SetAutoCompleteWordDirty( false ); +} + +SwInterHyphInfoTextFrame::SwInterHyphInfoTextFrame( + SwTextFrame const& rFrame, SwTextNode const& rNode, + SwInterHyphInfo const& rHyphInfo) + : m_nStart(rFrame.MapModelToView(&rNode, rHyphInfo.m_nStart)) + , m_nEnd(rFrame.MapModelToView(&rNode, rHyphInfo.m_nEnd)) + , m_nWordStart(0) + , m_nWordLen(0) +{ +} + +void SwInterHyphInfoTextFrame::UpdateTextNodeHyphInfo(SwTextFrame const& rFrame, + SwTextNode const& rNode, SwInterHyphInfo & o_rHyphInfo) +{ + std::pair const wordStart(rFrame.MapViewToModel(m_nWordStart)); + std::pair const wordEnd(rFrame.MapViewToModel(m_nWordStart+m_nWordLen)); + if (wordStart.first != &rNode || wordEnd.first != &rNode) + { // not sure if this can happen since nStart/nEnd are in rNode + SAL_WARN("sw.core", "UpdateTextNodeHyphInfo: outside of node"); + return; + } + o_rHyphInfo.m_nWordStart = wordStart.second; + o_rHyphInfo.m_nWordLen = wordEnd.second - wordStart.second; + o_rHyphInfo.SetHyphWord(m_xHyphWord); +} + +/// Find the SwTextFrame and call its Hyphenate +bool SwTextNode::Hyphenate( SwInterHyphInfo &rHyphInf ) +{ + // shortcut: paragraph doesn't have a language set: + if ( LANGUAGE_NONE == GetSwAttrSet().GetLanguage().GetLanguage() + && LanguageType(USHRT_MAX) == GetLang(0, m_Text.getLength())) + { + return false; + } + + SwTextFrame *pFrame = ::sw::SwHyphIterCacheLastTextFrame(this, + [&rHyphInf, this]() { + std::pair tmp; + Point const*const pPoint = rHyphInf.GetCursorPos(); + if (pPoint) + { + tmp.first = *pPoint; + tmp.second = true; + } + return static_cast(this->getLayoutFrame( + this->GetDoc()->getIDocumentLayoutAccess().GetCurrentLayout(), + nullptr, pPoint ? &tmp : nullptr)); + }); + if (!pFrame) + { + // There was a comment here that claimed that the following assertion + // shouldn't exist as it's triggered by "Trennung ueber Sonderbereiche", + // (hyphenation across special sections?), whatever that means. + OSL_ENSURE( pFrame, "!SwTextNode::Hyphenate: can't find any frame" ); + return false; + } + SwInterHyphInfoTextFrame aHyphInfo(*pFrame, *this, rHyphInf); + + pFrame = &(pFrame->GetFrameAtOfst( aHyphInfo.m_nStart )); + + while( pFrame ) + { + if (pFrame->Hyphenate(aHyphInfo)) + { + // The layout is not robust wrt. "direct formatting" + // cf. layact.cxx, SwLayAction::TurboAction_(), if( !pCnt->IsValid() ... + pFrame->SetCompletePaint(); + aHyphInfo.UpdateTextNodeHyphInfo(*pFrame, *this, rHyphInf); + return true; + } + pFrame = pFrame->GetFollow(); + if( pFrame ) + { + aHyphInfo.m_nEnd = aHyphInfo.m_nEnd - (pFrame->GetOffset() - aHyphInfo.m_nStart); + aHyphInfo.m_nStart = pFrame->GetOffset(); + } + } + return false; +} + +namespace +{ + struct swTransliterationChgData + { + sal_Int32 nStart; + sal_Int32 nLen; + OUString sChanged; + Sequence< sal_Int32 > aOffsets; + }; +} + +// change text to Upper/Lower/Hiragana/Katakana/... +void SwTextNode::TransliterateText( + utl::TransliterationWrapper& rTrans, + sal_Int32 nStt, sal_Int32 nEnd, + SwUndoTransliterate* pUndo ) +{ + if (nStt < nEnd) + { + // since we don't use Hiragana/Katakana or half-width/full-width transliterations here + // it is fine to use ANYWORD_IGNOREWHITESPACES. (ANY_WORD btw is broken and will + // occasionally miss words in consecutive sentences). Also with ANYWORD_IGNOREWHITESPACES + // text like 'just-in-time' will be converted to 'Just-In-Time' which seems to be the + // proper thing to do. + const sal_Int16 nWordType = WordType::ANYWORD_IGNOREWHITESPACES; + + // In order to have less trouble with changing text size, e.g. because + // of ligatures or German small sz being resolved, we need to process + // the text replacements from end to start. + // This way the offsets for the yet to be changed words will be + // left unchanged by the already replaced text. + // For this we temporarily save the changes to be done in this vector + std::vector< swTransliterationChgData > aChanges; + swTransliterationChgData aChgData; + + if (rTrans.getType() == TransliterationFlags::TITLE_CASE) + { + // for 'capitalize every word' we need to iterate over each word + + Boundary aSttBndry; + Boundary aEndBndry; + aSttBndry = g_pBreakIt->GetBreakIter()->getWordBoundary( + GetText(), nStt, + g_pBreakIt->GetLocale( GetLang( nStt ) ), + nWordType, + true /*prefer forward direction*/); + aEndBndry = g_pBreakIt->GetBreakIter()->getWordBoundary( + GetText(), nEnd, + g_pBreakIt->GetLocale( GetLang( nEnd ) ), + nWordType, + false /*prefer backward direction*/); + + // prevent backtracking to the previous word if selection is at word boundary + if (aSttBndry.endPos <= nStt) + { + aSttBndry = g_pBreakIt->GetBreakIter()->nextWord( + GetText(), aSttBndry.endPos, + g_pBreakIt->GetLocale( GetLang( aSttBndry.endPos ) ), + nWordType); + } + // prevent advancing to the next word if selection is at word boundary + if (aEndBndry.startPos >= nEnd) + { + aEndBndry = g_pBreakIt->GetBreakIter()->previousWord( + GetText(), aEndBndry.startPos, + g_pBreakIt->GetLocale( GetLang( aEndBndry.startPos ) ), + nWordType); + } + + Boundary aCurWordBndry( aSttBndry ); + while (aCurWordBndry.startPos <= aEndBndry.startPos) + { + nStt = aCurWordBndry.startPos; + nEnd = aCurWordBndry.endPos; + const sal_Int32 nLen = nEnd - nStt; + OSL_ENSURE( nLen > 0, "invalid word length of 0" ); + + Sequence aOffsets; + OUString const sChgd( rTrans.transliterate( + GetText(), GetLang(nStt), nStt, nLen, &aOffsets) ); + + assert(nStt < m_Text.getLength()); + if (0 != rtl_ustr_shortenedCompare_WithLength( + m_Text.getStr() + nStt, m_Text.getLength() - nStt, + sChgd.getStr(), sChgd.getLength(), nLen)) + { + aChgData.nStart = nStt; + aChgData.nLen = nLen; + aChgData.sChanged = sChgd; + aChgData.aOffsets = aOffsets; + aChanges.push_back( aChgData ); + } + + aCurWordBndry = g_pBreakIt->GetBreakIter()->nextWord( + GetText(), nStt, + g_pBreakIt->GetLocale(GetLang(nStt, 1)), + nWordType); + } + } + else if (rTrans.getType() == TransliterationFlags::SENTENCE_CASE) + { + // for 'sentence case' we need to iterate sentence by sentence + + sal_Int32 nLastStart = g_pBreakIt->GetBreakIter()->beginOfSentence( + GetText(), nEnd, + g_pBreakIt->GetLocale( GetLang( nEnd ) ) ); + sal_Int32 nLastEnd = g_pBreakIt->GetBreakIter()->endOfSentence( + GetText(), nLastStart, + g_pBreakIt->GetLocale( GetLang( nLastStart ) ) ); + + // extend nStt, nEnd to the current sentence boundaries + sal_Int32 nCurrentStart = g_pBreakIt->GetBreakIter()->beginOfSentence( + GetText(), nStt, + g_pBreakIt->GetLocale( GetLang( nStt ) ) ); + sal_Int32 nCurrentEnd = g_pBreakIt->GetBreakIter()->endOfSentence( + GetText(), nCurrentStart, + g_pBreakIt->GetLocale( GetLang( nCurrentStart ) ) ); + + // prevent backtracking to the previous sentence if selection starts at end of a sentence + if (nCurrentEnd <= nStt) + { + // now nCurrentStart is probably located on a non-letter word. (unless we + // are in Asian text with no spaces...) + // Thus to get the real sentence start we should locate the next real word, + // that is one found by DICTIONARY_WORD + i18n::Boundary aBndry = g_pBreakIt->GetBreakIter()->nextWord( + GetText(), nCurrentEnd, + g_pBreakIt->GetLocale( GetLang( nCurrentEnd ) ), + i18n::WordType::DICTIONARY_WORD); + + // now get new current sentence boundaries + nCurrentStart = g_pBreakIt->GetBreakIter()->beginOfSentence( + GetText(), aBndry.startPos, + g_pBreakIt->GetLocale( GetLang( aBndry.startPos) ) ); + nCurrentEnd = g_pBreakIt->GetBreakIter()->endOfSentence( + GetText(), nCurrentStart, + g_pBreakIt->GetLocale( GetLang( nCurrentStart) ) ); + } + // prevent advancing to the next sentence if selection ends at start of a sentence + if (nLastStart >= nEnd) + { + // now nCurrentStart is probably located on a non-letter word. (unless we + // are in Asian text with no spaces...) + // Thus to get the real sentence start we should locate the previous real word, + // that is one found by DICTIONARY_WORD + i18n::Boundary aBndry = g_pBreakIt->GetBreakIter()->previousWord( + GetText(), nLastStart, + g_pBreakIt->GetLocale( GetLang( nLastStart) ), + i18n::WordType::DICTIONARY_WORD); + nLastEnd = g_pBreakIt->GetBreakIter()->endOfSentence( + GetText(), aBndry.startPos, + g_pBreakIt->GetLocale( GetLang( aBndry.startPos) ) ); + if (nCurrentEnd > nLastEnd) + nCurrentEnd = nLastEnd; + } + + while (nCurrentStart < nLastEnd) + { + sal_Int32 nLen = nCurrentEnd - nCurrentStart; + OSL_ENSURE( nLen > 0, "invalid word length of 0" ); + + Sequence aOffsets; + OUString const sChgd( rTrans.transliterate(GetText(), + GetLang(nCurrentStart), nCurrentStart, nLen, &aOffsets) ); + + assert(nStt < m_Text.getLength()); + if (0 != rtl_ustr_shortenedCompare_WithLength( + m_Text.getStr() + nStt, m_Text.getLength() - nStt, + sChgd.getStr(), sChgd.getLength(), nLen)) + { + aChgData.nStart = nCurrentStart; + aChgData.nLen = nLen; + aChgData.sChanged = sChgd; + aChgData.aOffsets = aOffsets; + aChanges.push_back( aChgData ); + } + + Boundary aFirstWordBndry = g_pBreakIt->GetBreakIter()->nextWord( + GetText(), nCurrentEnd, + g_pBreakIt->GetLocale( GetLang( nCurrentEnd ) ), + nWordType); + nCurrentStart = aFirstWordBndry.startPos; + nCurrentEnd = g_pBreakIt->GetBreakIter()->endOfSentence( + GetText(), nCurrentStart, + g_pBreakIt->GetLocale( GetLang( nCurrentStart ) ) ); + } + } + else + { + // here we may transliterate over complete language portions... + + std::unique_ptr pIter; + if( rTrans.needLanguageForTheMode() ) + pIter.reset(new SwLanguageIterator( *this, nStt )); + + sal_Int32 nEndPos = 0; + LanguageType nLang = LANGUAGE_NONE; + do { + if( pIter ) + { + nLang = pIter->GetLanguage(); + nEndPos = pIter->GetChgPos(); + if( nEndPos > nEnd ) + nEndPos = nEnd; + } + else + { + nLang = LANGUAGE_SYSTEM; + nEndPos = nEnd; + } + const sal_Int32 nLen = nEndPos - nStt; + + Sequence aOffsets; + OUString const sChgd( rTrans.transliterate( + m_Text, nLang, nStt, nLen, &aOffsets) ); + + assert(nStt < m_Text.getLength()); + if (0 != rtl_ustr_shortenedCompare_WithLength( + m_Text.getStr() + nStt, m_Text.getLength() - nStt, + sChgd.getStr(), sChgd.getLength(), nLen)) + { + aChgData.nStart = nStt; + aChgData.nLen = nLen; + aChgData.sChanged = sChgd; + aChgData.aOffsets = aOffsets; + aChanges.push_back( aChgData ); + } + + nStt = nEndPos; + } while( nEndPos < nEnd && pIter && pIter->Next() ); + } + + if (!aChanges.empty()) + { + // now apply the changes from end to start to leave the offsets of the + // yet unchanged text parts remain the same. + size_t nSum(0); + for (size_t i = 0; i < aChanges.size(); ++i) + { // check this here since AddChanges cannot be moved below + // call to ReplaceTextOnly + swTransliterationChgData & rData = + aChanges[ aChanges.size() - 1 - i ]; + nSum += rData.sChanged.getLength() - rData.nLen; + if (nSum > o3tl::make_unsigned(GetSpaceLeft())) + { + SAL_WARN("sw.core", "SwTextNode::ReplaceTextOnly: " + "node text with insertion > node capacity."); + return; + } + if (pUndo) + pUndo->AddChanges( *this, rData.nStart, rData.nLen, rData.aOffsets ); + ReplaceTextOnly( rData.nStart, rData.nLen, rData.sChanged, rData.aOffsets ); + } + } + } +} + +void SwTextNode::ReplaceTextOnly( sal_Int32 nPos, sal_Int32 nLen, + const OUString & rText, + const Sequence& rOffsets ) +{ + assert(rText.getLength() - nLen <= GetSpaceLeft()); + + m_Text = m_Text.replaceAt(nPos, nLen, rText); + + sal_Int32 nTLen = rText.getLength(); + const sal_Int32* pOffsets = rOffsets.getConstArray(); + // now look for no 1-1 mapping -> move the indices! + sal_Int32 nMyOff = nPos; + for( sal_Int32 nI = 0; nI < nTLen; ++nI ) + { + const sal_Int32 nOff = pOffsets[ nI ]; + if( nOff < nMyOff ) + { + // something is inserted + sal_Int32 nCnt = 1; + while( nI + nCnt < nTLen && nOff == pOffsets[ nI + nCnt ] ) + ++nCnt; + + Update( SwIndex( this, nMyOff ), nCnt ); + nMyOff = nOff; + //nMyOff -= nCnt; + nI += nCnt - 1; + } + else if( nOff > nMyOff ) + { + // something is deleted + Update( SwIndex( this, nMyOff+1 ), nOff - nMyOff, true ); + nMyOff = nOff; + } + ++nMyOff; + } + if( nMyOff < nLen ) + // something is deleted at the end + Update( SwIndex( this, nMyOff ), nLen - nMyOff, true ); + + // notify the layout! + SwDelText aDelHint( nPos, nTLen ); + NotifyClients( nullptr, &aDelHint ); + + SwInsText aHint( nPos, nTLen ); + NotifyClients( nullptr, &aHint ); +} + +// the return values allows us to see if we did the heavy- +// lifting required to actually break and count the words. +bool SwTextNode::CountWords( SwDocStat& rStat, + sal_Int32 nStt, sal_Int32 nEnd ) const +{ + if( nStt > nEnd ) + { // bad call + return false; + } + if (IsInRedlines()) + { //not counting txtnodes used to hold deleted redline content + return false; + } + bool bCountAll = ( (0 == nStt) && (GetText().getLength() == nEnd) ); + ++rStat.nAllPara; // #i93174#: count _all_ paragraphs + if ( IsHidden() ) + { // not counting hidden paras + return false; + } + // count words in numbering string if started at beginning of para: + bool bCountNumbering = nStt == 0; + bool bHasBullet = false, bHasNumbering = false; + OUString sNumString; + if (bCountNumbering) + { + sNumString = GetNumString(); + bHasNumbering = !sNumString.isEmpty(); + if (!bHasNumbering) + bHasBullet = HasBullet(); + bCountNumbering = bHasNumbering || bHasBullet; + } + + if( nStt == nEnd && !bCountNumbering) + { // unnumbered empty node or empty selection + return false; + } + + // count of non-empty paras + ++rStat.nPara; + + // Shortcut when counting whole paragraph and current count is clean + if ( bCountAll && !IsWordCountDirty() ) + { + // accumulate into DocStat record to return the values + if (m_pParaIdleData_Impl) + { + rStat.nWord += m_pParaIdleData_Impl->nNumberOfWords; + rStat.nAsianWord += m_pParaIdleData_Impl->nNumberOfAsianWords; + rStat.nChar += m_pParaIdleData_Impl->nNumberOfChars; + rStat.nCharExcludingSpaces += m_pParaIdleData_Impl->nNumberOfCharsExcludingSpaces; + } + return false; + } + + // ConversionMap to expand fields, remove invisible and redline deleted text for scanner + const ModelToViewHelper aConversionMap(*this, + getIDocumentLayoutAccess().GetCurrentLayout(), + ExpandMode::ExpandFields | ExpandMode::ExpandFootnote | ExpandMode::HideInvisible | ExpandMode::HideDeletions); + const OUString& aExpandText = aConversionMap.getViewText(); + + if (aExpandText.isEmpty() && !bCountNumbering) + { + return false; + } + + // map start and end points onto the ConversionMap + const sal_Int32 nExpandBegin = aConversionMap.ConvertToViewPosition( nStt ); + const sal_Int32 nExpandEnd = aConversionMap.ConvertToViewPosition( nEnd ); + + //do the count + // all counts exclude hidden paras and hidden+redlined within para + // definition of space/white chars in SwScanner (and BreakIter!) + // uses both u_isspace and BreakIter getWordBoundary in SwScanner + sal_uInt32 nTmpWords = 0; // count of all words + sal_uInt32 nTmpAsianWords = 0; //count of all Asian codepoints + sal_uInt32 nTmpChars = 0; // count of all chars + sal_uInt32 nTmpCharsExcludingSpaces = 0; // all non-white chars + + // count words in masked and expanded text: + if (!aExpandText.isEmpty()) + { + assert(g_pBreakIt && g_pBreakIt->GetBreakIter().is()); + + // zero is NULL for pLanguage -----------v last param = true for clipping + SwScanner aScanner( *this, aExpandText, nullptr, aConversionMap, i18n::WordType::WORD_COUNT, + nExpandBegin, nExpandEnd, true ); + + // used to filter out scanner returning almost empty strings (len=1; unichar=0x0001) + const OUString aBreakWord( CH_TXTATR_BREAKWORD ); + + while ( aScanner.NextWord() ) + { + if( !aExpandText.match(aBreakWord, aScanner.GetBegin() )) + { + ++nTmpWords; + const OUString &rWord = aScanner.GetWord(); + if (g_pBreakIt->GetBreakIter()->getScriptType(rWord, 0) == i18n::ScriptType::ASIAN) + ++nTmpAsianWords; + nTmpCharsExcludingSpaces += g_pBreakIt->getGraphemeCount(rWord); + } + } + + nTmpCharsExcludingSpaces += aScanner.getOverriddenDashCount(); + + nTmpChars = g_pBreakIt->getGraphemeCount(aExpandText, nExpandBegin, nExpandEnd); + } + + // no nTmpCharsExcludingSpaces adjust needed neither for blanked out MaskedChars + // nor for mid-word selection - set scanner bClip = true at creation + + // count outline number label - ? no expansion into map + // always counts all of number-ish label + if (bHasNumbering) // count words in numbering string + { + LanguageType aLanguage = GetLang( 0 ); + + SwScanner aScanner( *this, sNumString, &aLanguage, ModelToViewHelper(), + i18n::WordType::WORD_COUNT, 0, sNumString.getLength(), true ); + + while ( aScanner.NextWord() ) + { + ++nTmpWords; + const OUString &rWord = aScanner.GetWord(); + if (g_pBreakIt->GetBreakIter()->getScriptType(rWord, 0) == i18n::ScriptType::ASIAN) + ++nTmpAsianWords; + nTmpCharsExcludingSpaces += g_pBreakIt->getGraphemeCount(rWord); + } + + nTmpCharsExcludingSpaces += aScanner.getOverriddenDashCount(); + nTmpChars += g_pBreakIt->getGraphemeCount(sNumString); + } + else if ( bHasBullet ) + { + ++nTmpWords; + ++nTmpChars; + ++nTmpCharsExcludingSpaces; + } + + // If counting the whole para then update cached values and mark clean + if ( bCountAll ) + { + if ( m_pParaIdleData_Impl ) + { + m_pParaIdleData_Impl->nNumberOfWords = nTmpWords; + m_pParaIdleData_Impl->nNumberOfAsianWords = nTmpAsianWords; + m_pParaIdleData_Impl->nNumberOfChars = nTmpChars; + m_pParaIdleData_Impl->nNumberOfCharsExcludingSpaces = nTmpCharsExcludingSpaces; + } + SetWordCountDirty( false ); + } + // accumulate into DocStat record to return the values + rStat.nWord += nTmpWords; + rStat.nAsianWord += nTmpAsianWords; + rStat.nChar += nTmpChars; + rStat.nCharExcludingSpaces += nTmpCharsExcludingSpaces; + + return true; +} + +// Paragraph statistics start --> + +void SwTextNode::InitSwParaStatistics( bool bNew ) +{ + if ( bNew ) + { + m_pParaIdleData_Impl = new SwParaIdleData_Impl; + } + else if ( m_pParaIdleData_Impl ) + { + delete m_pParaIdleData_Impl->pWrong; + delete m_pParaIdleData_Impl->pGrammarCheck; + delete m_pParaIdleData_Impl->pSmartTags; + delete m_pParaIdleData_Impl; + m_pParaIdleData_Impl = nullptr; + } +} + +void SwTextNode::SetWrong( SwWrongList* pNew, bool bDelete ) +{ + if ( m_pParaIdleData_Impl ) + { + if ( bDelete ) + { + delete m_pParaIdleData_Impl->pWrong; + } + m_pParaIdleData_Impl->pWrong = pNew; + } +} + +SwWrongList* SwTextNode::GetWrong() +{ + return m_pParaIdleData_Impl ? m_pParaIdleData_Impl->pWrong : nullptr; +} + +// #i71360# +const SwWrongList* SwTextNode::GetWrong() const +{ + return m_pParaIdleData_Impl ? m_pParaIdleData_Impl->pWrong : nullptr; +} + +void SwTextNode::SetGrammarCheck( SwGrammarMarkUp* pNew, bool bDelete ) +{ + if ( m_pParaIdleData_Impl ) + { + if ( bDelete ) + { + delete m_pParaIdleData_Impl->pGrammarCheck; + } + m_pParaIdleData_Impl->pGrammarCheck = pNew; + } +} + +SwGrammarMarkUp* SwTextNode::GetGrammarCheck() +{ + return m_pParaIdleData_Impl ? m_pParaIdleData_Impl->pGrammarCheck : nullptr; +} + +SwWrongList const* SwTextNode::GetGrammarCheck() const +{ + return static_cast(const_cast(this)->GetGrammarCheck()); +} + +void SwTextNode::SetSmartTags( SwWrongList* pNew, bool bDelete ) +{ + OSL_ENSURE( !pNew || SwSmartTagMgr::Get().IsSmartTagsEnabled(), + "Weird - we have a smart tag list without any recognizers?" ); + + if ( m_pParaIdleData_Impl ) + { + if ( bDelete ) + { + delete m_pParaIdleData_Impl->pSmartTags; + } + m_pParaIdleData_Impl->pSmartTags = pNew; + } +} + +SwWrongList* SwTextNode::GetSmartTags() +{ + return m_pParaIdleData_Impl ? m_pParaIdleData_Impl->pSmartTags : nullptr; +} + +SwWrongList const* SwTextNode::GetSmartTags() const +{ + return const_cast(const_cast(this)->GetSmartTags()); +} + +void SwTextNode::SetWordCountDirty( bool bNew ) const +{ + if ( m_pParaIdleData_Impl ) + { + m_pParaIdleData_Impl->bWordCountDirty = bNew; + } +} + +bool SwTextNode::IsWordCountDirty() const +{ + return m_pParaIdleData_Impl && m_pParaIdleData_Impl->bWordCountDirty; +} + +void SwTextNode::SetWrongDirty(WrongState eNew) const +{ + if ( m_pParaIdleData_Impl ) + { + m_pParaIdleData_Impl->eWrongDirty = eNew; + } +} + +auto SwTextNode::GetWrongDirty() const -> WrongState +{ + return m_pParaIdleData_Impl ? m_pParaIdleData_Impl->eWrongDirty : WrongState::DONE; +} + +bool SwTextNode::IsWrongDirty() const +{ + return m_pParaIdleData_Impl && m_pParaIdleData_Impl->eWrongDirty != WrongState::DONE; +} + +void SwTextNode::SetGrammarCheckDirty( bool bNew ) const +{ + if ( m_pParaIdleData_Impl ) + { + m_pParaIdleData_Impl->bGrammarCheckDirty = bNew; + } +} + +bool SwTextNode::IsGrammarCheckDirty() const +{ + return m_pParaIdleData_Impl && m_pParaIdleData_Impl->bGrammarCheckDirty; +} + +void SwTextNode::SetSmartTagDirty( bool bNew ) const +{ + if ( m_pParaIdleData_Impl ) + { + m_pParaIdleData_Impl->bSmartTagDirty = bNew; + } +} + +bool SwTextNode::IsSmartTagDirty() const +{ + return m_pParaIdleData_Impl && m_pParaIdleData_Impl->bSmartTagDirty; +} + +void SwTextNode::SetAutoCompleteWordDirty( bool bNew ) const +{ + if ( m_pParaIdleData_Impl ) + { + m_pParaIdleData_Impl->bAutoComplDirty = bNew; + } +} + +bool SwTextNode::IsAutoCompleteWordDirty() const +{ + return m_pParaIdleData_Impl && m_pParaIdleData_Impl->bAutoComplDirty; +} + +// <-- Paragraph statistics end + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3