diff options
-rw-r--r-- | editeng/Library_editeng.mk | 1 | ||||
-rw-r--r-- | editeng/qa/unit/core-test.cxx | 93 | ||||
-rw-r--r-- | editeng/source/editeng/editobj.cxx | 149 | ||||
-rw-r--r-- | editeng/source/editeng/editobj2.hxx | 8 | ||||
-rw-r--r-- | editeng/source/editeng/sectionattribute.cxx | 21 | ||||
-rw-r--r-- | include/editeng/editobj.hxx | 13 | ||||
-rw-r--r-- | include/editeng/sectionattribute.hxx | 37 |
7 files changed, 322 insertions, 0 deletions
diff --git a/editeng/Library_editeng.mk b/editeng/Library_editeng.mk index e9c5b4bea2d2..c4a9fbeb9c7d 100644 --- a/editeng/Library_editeng.mk +++ b/editeng/Library_editeng.mk @@ -60,6 +60,7 @@ $(eval $(call gb_Library_add_exception_objects,editeng,\ editeng/source/editeng/impedit3 \ editeng/source/editeng/impedit4 \ editeng/source/editeng/impedit5 \ + editeng/source/editeng/sectionattribute \ editeng/source/editeng/textconv \ editeng/source/items/borderline \ editeng/source/items/bulitem \ diff --git a/editeng/qa/unit/core-test.cxx b/editeng/qa/unit/core-test.cxx index ae3749c51450..86f5eb8dd2ea 100644 --- a/editeng/qa/unit/core-test.cxx +++ b/editeng/qa/unit/core-test.cxx @@ -21,9 +21,15 @@ #include "editeng/editdoc.hxx" #include "editeng/svxacorr.hxx" #include "editeng/unofield.hxx" +#include "editeng/wghtitem.hxx" +#include "editeng/postitem.hxx" +#include "editeng/sectionattribute.hxx" +#include "editeng/editobj.hxx" #include <com/sun/star/text/textfield/Type.hpp> +#include <boost/scoped_ptr.hpp> + using namespace com::sun::star; namespace { @@ -48,10 +54,13 @@ public: */ void testAutocorrect(); + void testSectionAttributes(); + CPPUNIT_TEST_SUITE(Test); CPPUNIT_TEST(testConstruction); CPPUNIT_TEST(testUnoTextFields); CPPUNIT_TEST(testAutocorrect); + CPPUNIT_TEST(testSectionAttributes); CPPUNIT_TEST_SUITE_END(); private: @@ -331,7 +340,91 @@ void Test::testAutocorrect() CPPUNIT_ASSERT_EQUAL(sExpected, aFoo.getResult()); } +} + +bool hasBold(const editeng::SectionAttribute& rSecAttr) +{ + std::vector<const SfxPoolItem*>::const_iterator it = rSecAttr.maAttributes.begin(), itEnd = rSecAttr.maAttributes.end(); + for (; it != itEnd; ++it) + { + const SfxPoolItem* p = *it; + if (p->Which() != EE_CHAR_WEIGHT) + continue; + + if (static_cast<const SvxWeightItem*>(p)->GetWeight() != WEIGHT_BOLD) + continue; + + return true; + } + return false; +} + +bool hasItalic(const editeng::SectionAttribute& rSecAttr) +{ + std::vector<const SfxPoolItem*>::const_iterator it = rSecAttr.maAttributes.begin(), itEnd = rSecAttr.maAttributes.end(); + for (; it != itEnd; ++it) + { + const SfxPoolItem* p = *it; + if (p->Which() != EE_CHAR_ITALIC) + continue; + + if (static_cast<const SvxPostureItem*>(p)->GetPosture() != ITALIC_NORMAL) + continue; + + return true; + } + return false; +} + +void Test::testSectionAttributes() +{ + EditEngine aEngine(mpItemPool); + + boost::scoped_ptr<SfxItemSet> pSet(new SfxItemSet(aEngine.GetEmptyItemSet())); + SvxWeightItem aBold(WEIGHT_BOLD, EE_CHAR_WEIGHT); + SvxPostureItem aItalic(ITALIC_NORMAL, EE_CHAR_ITALIC); + OUString aParaText = "aaabbbccc"; + aEngine.SetText(aParaText); + pSet->Put(aBold); + CPPUNIT_ASSERT_MESSAGE("There should be exactly one item.", pSet->Count() == 1); + aEngine.QuickSetAttribs(*pSet, ESelection(0,0,0,6)); // 'aaabbb' - end point is not inclusive. + pSet.reset(new SfxItemSet(aEngine.GetEmptyItemSet())); + pSet->Put(aItalic); + CPPUNIT_ASSERT_MESSAGE("There should be exactly one item.", pSet->Count() == 1); + + aEngine.QuickSetAttribs(*pSet, ESelection(0,3,0,9)); // 'bbbccc' + boost::scoped_ptr<EditTextObject> pEditText(aEngine.CreateTextObject()); + CPPUNIT_ASSERT_MESSAGE("Failed to create text object.", pEditText.get()); + std::vector<editeng::SectionAttribute> aAttrs; + pEditText->GetAllSectionAttributes(aAttrs); + + // Now, we should have a total of 3 sections. + CPPUNIT_ASSERT_MESSAGE("There should be 3 sections.", aAttrs.size() == 3); + + // First section should be 0-3 of paragraph 0, and it should only have boldness applied. + const editeng::SectionAttribute* pSecAttr = &aAttrs[0]; + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), pSecAttr->mnParagraph); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), pSecAttr->mnStart); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), pSecAttr->mnEnd); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pSecAttr->maAttributes.size()); + CPPUNIT_ASSERT_MESSAGE("This section must be bold.", hasBold(*pSecAttr)); + + // Second section should be 3-6, and it should be both bold and italic. + pSecAttr = &aAttrs[1]; + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), pSecAttr->mnParagraph); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), pSecAttr->mnStart); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(6), pSecAttr->mnEnd); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), pSecAttr->maAttributes.size()); + CPPUNIT_ASSERT_MESSAGE("This section must be bold and italic.", hasBold(*pSecAttr) && hasItalic(*pSecAttr)); + + // Third section should be 6-9, and it should be only italic. + pSecAttr = &aAttrs[2]; + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), pSecAttr->mnParagraph); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(6), pSecAttr->mnStart); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(9), pSecAttr->mnEnd); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pSecAttr->maAttributes.size()); + CPPUNIT_ASSERT_MESSAGE("This section must be italic.", hasItalic(*pSecAttr)); } CPPUNIT_TEST_SUITE_REGISTRATION(Test); diff --git a/editeng/source/editeng/editobj.cxx b/editeng/source/editeng/editobj.cxx index bcbef1ef199e..dbfd833f7402 100644 --- a/editeng/source/editeng/editobj.cxx +++ b/editeng/source/editeng/editobj.cxx @@ -28,6 +28,7 @@ #include "editeng/fieldupdater.hxx" #include "editeng/macros.hxx" +#include "editeng/sectionattribute.hxx" #include <editobj2.hxx> #include <editeng/editdata.hxx> #include <editattr.hxx> @@ -276,6 +277,11 @@ bool EditTextObject::RemoveCharAttribs( sal_uInt16 nWhich ) return mpImpl->RemoveCharAttribs(nWhich); } +void EditTextObject::GetAllSectionAttributes( std::vector<editeng::SectionAttribute>& rAttrs ) const +{ + mpImpl->GetAllSectionAttributes(rAttrs); +} + void EditTextObject::GetStyleSheet(sal_Int32 nPara, OUString& rName, SfxStyleFamily& eFamily) const { mpImpl->GetStyleSheet(nPara, rName, eFamily); @@ -824,6 +830,149 @@ bool EditTextObjectImpl::RemoveCharAttribs( sal_uInt16 _nWhich ) return bChanged; } +namespace { + +class FindByParagraph : std::unary_function<editeng::SectionAttribute, bool> +{ + size_t mnPara; +public: + FindByParagraph(size_t nPara) : mnPara(nPara) {} + bool operator() (const editeng::SectionAttribute& rAttr) const + { + return rAttr.mnParagraph == mnPara; + } +}; + +class FindBySectionStart : std::unary_function<editeng::SectionAttribute, bool> +{ + size_t mnPara; + size_t mnStart; +public: + FindBySectionStart(size_t nPara, size_t nStart) : mnPara(nPara), mnStart(nStart) {} + bool operator() (const editeng::SectionAttribute& rAttr) const + { + return rAttr.mnParagraph == mnPara && rAttr.mnStart == mnStart; + } +}; + +} + +void EditTextObjectImpl::GetAllSectionAttributes( std::vector<editeng::SectionAttribute>& rAttrs ) const +{ + typedef std::vector<size_t> SectionBordersType; + typedef std::map<size_t, SectionBordersType> ParagraphsType; + ParagraphsType aParaBorders; + + // First pass: determine section borders for each paragraph. + for (size_t nPara = 0; nPara < aContents.size(); ++nPara) + { + const ContentInfo& rC = aContents[nPara]; + for (size_t nAttr = 0; nAttr < rC.aAttribs.size(); ++nAttr) + { + const XEditAttribute& rAttr = rC.aAttribs[nAttr]; + const SfxPoolItem* pItem = rAttr.GetItem(); + if (!pItem || pItem->Which() == EE_FEATURE_FIELD) + continue; + + ParagraphsType::iterator it = aParaBorders.lower_bound(nPara); + SectionBordersType* pBorders = NULL; + if (it != aParaBorders.end() && !aParaBorders.key_comp()(nPara, it->first)) + { + // Container for this paragraph already exists. + pBorders = &it->second; + } + else + { + it = aParaBorders.insert(it, ParagraphsType::value_type(nPara, SectionBordersType())); + pBorders = &it->second; + } + + pBorders->push_back(rAttr.GetStart()); + pBorders->push_back(rAttr.GetEnd()); + } + } + + // Sort and remove duplicates for each paragraph. + ParagraphsType::iterator it = aParaBorders.begin(), itEnd = aParaBorders.end(); + for (; it != itEnd; ++it) + { + SectionBordersType& rBorders = it->second; + std::sort(rBorders.begin(), rBorders.end()); + SectionBordersType::iterator itUniqueEnd = std::unique(rBorders.begin(), rBorders.end()); + rBorders.erase(itUniqueEnd, rBorders.end()); + } + + std::vector<editeng::SectionAttribute> aAttrs; + + // Create storage for each section. Note that this creates storage even + // for unformatted sections. The entries are sorted first by paragraph, + // then by section positions. They don't overlap with each other. + it = aParaBorders.begin(); + for (; it != itEnd; ++it) + { + size_t nPara = it->first; + const SectionBordersType& rBorders = it->second; + if (rBorders.empty()) + continue; + + SectionBordersType::const_iterator itBorder = rBorders.begin(), itBorderEnd = rBorders.end(); + size_t nPrev = *itBorder; + size_t nCur; + for (++itBorder; itBorder != itBorderEnd; ++itBorder, nPrev = nCur) + { + nCur = *itBorder; + aAttrs.push_back(editeng::SectionAttribute(nPara, nPrev, nCur)); + } + } + + if (aAttrs.empty()) + return; + + // Go through all formatted paragraphs, and store format items. + it = aParaBorders.begin(); + std::vector<editeng::SectionAttribute>::iterator itAttr = aAttrs.begin(); + for (; it != itEnd; ++it) + { + size_t nPara = it->first; + const ContentInfo& rC = aContents[nPara]; + if (itAttr->mnParagraph != nPara) + // Find the first container for the current paragraph. + itAttr = std::find_if(itAttr, aAttrs.end(), FindByParagraph(nPara)); + + if (itAttr == aAttrs.end()) + // This should never happen. There is a logic error somewhere... + return; + + // Remember this position. + std::vector<editeng::SectionAttribute>::iterator itAttrHead = itAttr; + + for (size_t i = 0; i < rC.aAttribs.size(); ++i) + { + const XEditAttribute& rAttr = rC.aAttribs[i]; + const SfxPoolItem* pItem = rAttr.GetItem(); + if (!pItem || pItem->Which() == EE_FEATURE_FIELD) + continue; + + size_t nStart = rAttr.GetStart(), nEnd = rAttr.GetEnd(); + itAttr = itAttrHead; + + // Find the container whose start position matches. + itAttr = std::find_if(itAttr, aAttrs.end(), FindBySectionStart(nPara, nStart)); + if (itAttr == aAttrs.end()) + // This should never happen. There is a logic error somewhere... + return; + + for (; itAttr != aAttrs.end() && itAttr->mnEnd <= nEnd; ++itAttr) + { + editeng::SectionAttribute& rSecAttr = *itAttr; + rSecAttr.maAttributes.push_back(pItem); + } + } + } + + rAttrs.swap(aAttrs); +} + void EditTextObjectImpl::GetStyleSheet(sal_Int32 nPara, OUString& rName, SfxStyleFamily& rFamily) const { if (nPara < 0 || static_cast<size_t>(nPara) >= aContents.size()) diff --git a/editeng/source/editeng/editobj2.hxx b/editeng/source/editeng/editobj2.hxx index d602753a1f1a..b243647dccc1 100644 --- a/editeng/source/editeng/editobj2.hxx +++ b/editeng/source/editeng/editobj2.hxx @@ -28,6 +28,12 @@ #include <boost/ptr_container/ptr_vector.hpp> #include <boost/noncopyable.hpp> +namespace editeng { + +struct SectionAttribute; + +} + class XEditAttribute { private: @@ -219,6 +225,8 @@ public: bool RemoveCharAttribs( sal_uInt16 nWhich = 0 ); + void GetAllSectionAttributes( std::vector<editeng::SectionAttribute>& rAttrs ) const; + bool IsFieldObject() const; const SvxFieldItem* GetField() const; const SvxFieldData* GetFieldData(sal_Int32 nPara, size_t nPos, sal_Int32 nType) const; diff --git a/editeng/source/editeng/sectionattribute.cxx b/editeng/source/editeng/sectionattribute.cxx new file mode 100644 index 000000000000..d8e0e575e1fb --- /dev/null +++ b/editeng/source/editeng/sectionattribute.cxx @@ -0,0 +1,21 @@ +/* -*- 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/. + */ + +#include "editeng/sectionattribute.hxx" + +namespace editeng { + +SectionAttribute::SectionAttribute() : mnParagraph(0), mnStart(0), mnEnd(0) {} + +SectionAttribute::SectionAttribute(size_t nPara, size_t nStart, size_t nEnd) : + mnParagraph(nPara), mnStart(nStart), mnEnd(nEnd){} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/editeng/editobj.hxx b/include/editeng/editobj.hxx index 570615f5e254..c61f682672da 100644 --- a/include/editeng/editobj.hxx +++ b/include/editeng/editobj.hxx @@ -45,6 +45,7 @@ namespace editeng { class FieldUpdater; class FieldUpdaterImpl; +struct SectionAttribute; } @@ -99,6 +100,18 @@ public: bool RemoveCharAttribs( sal_uInt16 nWhich = 0 ); + /** + * Get all attributes that are applied to this content, separated by + * sections. If multiple attributes are applied to the same section, the + * object representing that section will store multiple attributes. + * Sections never overlap each other; if an attribute was applied to [0-6] + * and another applied to [3-10], you would get 3 sections that are [0-3], + * [3-6] and [6-10]. + * + * <p>Note that this method skips field attributes.</p> + */ + void GetAllSectionAttributes( std::vector<editeng::SectionAttribute>& rAttrs ) const; + bool IsFieldObject() const; const SvxFieldItem* GetField() const; const SvxFieldData* GetFieldData(sal_Int32 nPara, size_t nPos, sal_Int32 nType) const; diff --git a/include/editeng/sectionattribute.hxx b/include/editeng/sectionattribute.hxx new file mode 100644 index 000000000000..eafd4e2e320c --- /dev/null +++ b/include/editeng/sectionattribute.hxx @@ -0,0 +1,37 @@ +/* -*- 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/. + */ + +#ifndef EDITENG_SECTIONATTRIBUTE_HXX +#define EDITENG_SECTIONATTRIBUTE_HXX + +#include "editeng/editengdllapi.h" + +#include <vector> + +class SfxPoolItem; + +namespace editeng { + +struct EDITENG_DLLPUBLIC SectionAttribute +{ + size_t mnParagraph; + size_t mnStart; + size_t mnEnd; + + std::vector<const SfxPoolItem*> maAttributes; + + SectionAttribute(); + SectionAttribute(size_t nPara, size_t nStart, size_t nEnd); +}; + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |