Kaydet (Commit) 9088a4c2 authored tarafından Tobias Lippert's avatar Tobias Lippert Kaydeden (comit) Caolán McNamara

fdo#30732 Retain selected character attributes for table of contents

The text and selected attributes are copied from the source text node
and added to the target node at the corresponding positions.

Conflicts:
	sw/inc/ToxTextGenerator.hxx
	sw/source/core/tox/ToxTextGenerator.cxx

Change-Id: Ifa329bfcb2b9691120513236909bcadaf1127b5e
Reviewed-on: https://gerrit.libreoffice.org/9610Reviewed-by: 's avatarCaolán McNamara <caolanm@redhat.com>
Tested-by: 's avatarCaolán McNamara <caolanm@redhat.com>
üst 31d1d684
......@@ -20,14 +20,20 @@
#ifndef SW_TOXTEXTGENERATOR_HXX_
#define SW_TOXTEXTGENERATOR_HXX_
#include "rtl/ustring.hxx"
#include "sal/types.h"
#include "swdllapi.h"
#include <boost/shared_ptr.hpp>
#include <vector>
class SfxItemSet;
class SwAttrPool;
class SwFmtAutoFmt;
class SwDoc;
class SwForm;
class SwPageDesc;
class SwTxtAttr;
class SwTxtNode;
struct SwTOXSortTabBase;
namespace sw {
......@@ -54,6 +60,37 @@ public:
private:
const SwForm& mToxForm;
boost::shared_ptr<ToxLinkProcessor> mLinkProcessor;
/** A handled text token.
* It contains the information which should be added to the target text node.
*/
struct HandledTextToken {
OUString text;
std::vector<SwFmtAutoFmt*> autoFormats;
std::vector<sal_Int32> startPositions;
std::vector<sal_Int32> endPositions;
};
/** Append text (and selected attributes) to a target node.
*
* Will take the text of @p source, and return the text and the attributes which should be added to the
* target text node. @see CollectAttributesForTox() for the criteria of the attributes which are taken.
*/
static HandledTextToken
HandleTextToken(const SwTOXSortTabBase& source, SwAttrPool& attrPool);
/** Applies the result of a handled text token to a target node. */
static void
ApplyHandledTextToken(const HandledTextToken& htt, SwTxtNode& targetNode);
/** Collect the attributes of a hint that shall be copied over to the TOX.
*
* Some text attributes are used in the TOX entries. This method defines which attributes are used.
*
* @param hint The hint from which the attributes are taken
* @param pool The attribute pool for the new items
*/
static boost::shared_ptr<SfxItemSet>
CollectAttributesForTox(const SwTxtAttr& hint, SwAttrPool& pool);
};
}
......
......@@ -29,16 +29,23 @@
#include "pagedesc.hxx"
#include "tox.hxx"
#include "txmsrt.hxx"
#include "fmtautofmt.hxx"
#include "fmtfsize.hxx"
#include "fmtpdsc.hxx"
#include "DocumentSettingManager.hxx"
#include "SwStyleNameMapper.hxx"
#include "swatrset.hxx"
#include "ToxWhitespaceStripper.hxx"
#include "ToxLinkProcessor.hxx"
#include "txatbase.hxx"
#include "editeng/tstpitem.hxx"
#include "editeng/lrspitem.hxx"
#include "rtl/ustring.hxx"
#include "svl/itemiter.hxx"
#include <boost/foreach.hpp>
#include <boost/make_shared.hpp>
/// Generate String according to the Form and remove the
/// special characters 0-31 and 255.
......@@ -107,12 +114,10 @@ void ToxTextGenerator::GenerateText(SwDoc* pDoc, const std::vector<SwTOXSortTabB
rTxt += lcl_GetNumString( rBase, aToken.nChapterFormat == CF_NUMBER, static_cast<sal_uInt8>(aToken.nOutlineLevel - 1) ) ;
break;
case TOKEN_ENTRY_TEXT:
{
SwIndex aIdx( pTOXNd, std::min(pTOXNd->GetTxt().getLength(),rTxt.getLength()) );
ToxWhitespaceStripper stripper(rBase.GetTxt().sText);
pTOXNd->InsertText(stripper.GetStrippedString(), aIdx);
}
case TOKEN_ENTRY_TEXT: {
HandledTextToken htt = HandleTextToken(rBase, pDoc->GetAttrPool());
ApplyHandledTextToken(htt, *pTOXNd);
}
break;
case TOKEN_ENTRY:
......@@ -308,6 +313,73 @@ void ToxTextGenerator::GenerateText(SwDoc* pDoc, const std::vector<SwTOXSortTabB
mLinkProcessor->InsertLinkAttributes(*pTOXNd);
}
/*static*/ boost::shared_ptr<SfxItemSet>
ToxTextGenerator::CollectAttributesForTox(const SwTxtAttr& hint, SwAttrPool& pool)
{
boost::shared_ptr<SfxItemSet> retval = boost::make_shared<SfxItemSet>(pool);
if (hint.Which() != RES_TXTATR_AUTOFMT) {
return retval;
}
const SwFmtAutoFmt& afmt = hint.GetAutoFmt();
SfxItemIter aIter( *afmt.GetStyleHandle());
const SfxPoolItem* pItem = aIter.GetCurItem();
while (true) {
if (pItem->Which() == RES_CHRATR_ESCAPEMENT ||
pItem->Which() == RES_CHRATR_POSTURE ||
pItem->Which() == RES_CHRATR_CJK_POSTURE ||
pItem->Which() == RES_CHRATR_CTL_POSTURE) {
SfxPoolItem* clonedItem = pItem->Clone(NULL);
retval->Put(*clonedItem);
}
if (aIter.IsAtEnd()) {
break;
}
pItem = aIter.NextItem();
}
return retval;
}
ToxTextGenerator::HandledTextToken
ToxTextGenerator::HandleTextToken(const SwTOXSortTabBase& source, SwAttrPool& pool)
{
HandledTextToken result;
ToxWhitespaceStripper stripper(source.GetTxt().sText);
result.text = stripper.GetStrippedString();
const SwTxtNode* pSrc = source.aTOXSources.at(0).pNd->GetTxtNode();
if (!pSrc->HasHints()) {
return result;
}
const SwpHints& hints = pSrc->GetSwpHints();
for (sal_uInt16 i = 0; i < hints.Count(); ++i) {
const SwTxtAttr* hint = hints[i];
boost::shared_ptr<SfxItemSet> attributesToClone = CollectAttributesForTox(*hint, pool);
if (attributesToClone->Count() <= 0) {
continue;
}
SwFmtAutoFmt* clone = static_cast<SwFmtAutoFmt*>(hint->GetAutoFmt().Clone());
clone->SetStyleHandle(attributesToClone);
result.autoFormats.push_back(clone);
result.startPositions.push_back(stripper.GetPositionInStrippedString(*hint->GetStart()));
result.endPositions.push_back(stripper.GetPositionInStrippedString(*hint->GetAnyEnd()));
}
return result;
}
/*static*/ void
ToxTextGenerator::ApplyHandledTextToken(const HandledTextToken& htt, SwTxtNode& targetNode)
{
sal_Int32 offset = targetNode.GetTxt().getLength();
SwIndex aIdx(&targetNode, offset);
targetNode.InsertText(htt.text, aIdx);
for (size_t i=0; i < htt.autoFormats.size(); ++i) {
targetNode.InsertItem(*htt.autoFormats.at(i),
htt.startPositions.at(i) + offset,
htt.endPositions.at(i) + offset);
}
}
} // end namespace sw
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment