/* * (c) Copyright Ascensio System SIA 2010-2016 * * This program is a free software product. You can redistribute it and/or * modify it under the terms of the GNU Affero General Public License (AGPL) * version 3 as published by the Free Software Foundation. In accordance with * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect * that Ascensio System SIA expressly excludes the warranty of non-infringement * of any third-party rights. * * This program is distributed WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html * * You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, * EU, LV-1021. * * The interactive user interfaces in modified source and object code versions * of the Program must display Appropriate Legal Notices, as required under * Section 5 of the GNU AGPL version 3. * * Pursuant to Section 7(b) of the License you must retain the original Product * logo when distributing the program. Pursuant to Section 7(e) we decline to * grant you any rights under trademark law for use of our trademarks. * * All the Product's GUI elements, including illustrations and icon sets, as * well as technical writing content are licensed under the terms of the * Creative Commons Attribution-ShareAlike 4.0 International. See the License * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode * */ #include "List.h" namespace ASCDocFileFormat { List::List(short _numID): numID(_numID), level(0) { } List::List(const list& _textItems, short _numID) : textItems(_textItems), numID(_numID), level(0) { SetLevel(level); SetNumID(numID); } List::List(const List& _list) : textItems(_list.textItems), numID(_list.numID), level(_list.level) { } void List::SetLevel(unsigned char _level) { for (std::list::iterator iter = this->textItems.begin(); iter != this->textItems.end(); ++iter) { if (typeid(**iter) == typeid(Paragraph)) { Paragraph* paragraph = dynamic_cast(iter->operator->()); if (paragraph) { paragraph->RemoveProperty( (short)DocFileFormat::sprmPIlvl, (unsigned char*)(&(this->level)) ); paragraph->AddProperty( (short)DocFileFormat::sprmPIlvl, (unsigned char*)(&_level) ); } } else if (typeid(**iter) == typeid(List)) { List* subList = dynamic_cast(iter->operator->()); if (subList) { subList->SetLevel(_level + 1); } } } this->level = _level; } void List::SetNumID( short _numID ) { for (std::list::iterator iter = this->textItems.begin(); iter != textItems.end(); ++iter) { if (typeid(**iter) == typeid(Paragraph)) { Paragraph* paragraph = dynamic_cast(iter->operator->()); if (paragraph) { paragraph->RemoveProperty( (short)DocFileFormat::sprmPIlfo, (unsigned char*)(&this->numID) ); paragraph->AddProperty( (short)DocFileFormat::sprmPIlfo, (unsigned char*)(&_numID) ); } } else if (typeid(**iter) == typeid(List)) { List* subList = dynamic_cast(iter->operator->()); if (subList) { subList->SetNumID( _numID ); } } } numID = _numID; } void List::AddParagraph( const Paragraph& _paragraph ) { TextItemPtr newTextItem( static_cast( _paragraph.Clone() ) ); this->textItems.push_back( newTextItem ); } void List::AddTextItem( const ITextItem& _textItem ) { TextItemPtr newTextItem( static_cast( _textItem.Clone() ) ); this->textItems.push_back( newTextItem ); this->SetLevel( this->level ); this->SetNumID( this->numID ); } wstring List::GetAllText() const { wstring allListText; for ( list::const_iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { allListText += (*iter)->GetAllText(); } return allListText; } List::operator wstring() const { wstring listText; for ( list::const_iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { listText += (wstring)(**iter); } return listText; } vector List::GetAllParagraphsCopy() const { vector allParagraphs; for ( list::const_iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { if ( typeid(**iter) == typeid(Paragraph) ) { allParagraphs.push_back( TextItemPtr( static_cast( (*iter)->Clone() ) ) ); } else { vector textItemParagraphs = (*iter)->GetAllParagraphsCopy(); for ( vector::const_iterator textItemIter = textItemParagraphs.begin(); textItemIter != textItemParagraphs.end(); textItemIter++ ) { allParagraphs.push_back( TextItemPtr( static_cast( (*textItemIter)->Clone() ) ) ); } } } return allParagraphs; } vector List::GetAllParagraphs() { std::vector allParagraphs; for (std::list::iterator iter = textItems.begin(); iter != textItems.end(); ++iter) { if (typeid(**iter) == typeid(Paragraph)) { allParagraphs.push_back(iter->operator->()); } else { std::vector textItemParagraphs = (*iter)->GetAllParagraphs(); for (std::vector::iterator textItemIter = textItemParagraphs.begin(); textItemIter != textItemParagraphs.end(); ++textItemIter) { allParagraphs.push_back( *textItemIter ); } } } return allParagraphs; } vector List::GetAllRunsCopy( vector* allRunsOffsets ) const { vector allRuns; if ( allRunsOffsets != NULL ) { unsigned int runOffset = 0; for ( list::const_iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { vector allTextItemRunsOffsets; vector allTextItemRuns = (*iter)->GetAllRunsCopy( &allTextItemRunsOffsets ); for ( unsigned int i = 0; i < allTextItemRuns.size(); i++ ) { allRuns.push_back( IParagraphItemPtr( static_cast(allTextItemRuns[i]->Clone()) ) ); allRunsOffsets->push_back( runOffset + allTextItemRunsOffsets[i] ); } runOffset += ( sizeof(wchar_t) * (*iter)->GetAllText().size() ); } } return allRuns; } vector List::GetAllParagraphItemsCopy( vector* allParagraphItemsOffsets ) const { vector allParagraphItems; if ( allParagraphItemsOffsets != NULL ) { unsigned int paragraphItemOffset = 0; for ( list::const_iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { vector allTextItemParagraphItemsOffsets; vector allTextItemParagraphItems = (*iter)->GetAllParagraphItemsCopy( &allTextItemParagraphItemsOffsets ); for ( unsigned int i = 0; i < allTextItemParagraphItems.size(); i++ ) { allParagraphItems.push_back( IParagraphItemPtr( static_cast(allTextItemParagraphItems[i]->Clone()) ) ); allParagraphItemsOffsets->push_back( paragraphItemOffset + allTextItemParagraphItemsOffsets[i] ); } paragraphItemOffset += ( sizeof(wchar_t) * (*iter)->GetAllText().size() ); } } return allParagraphItems; } vector List::GetAllParagraphsProperties( vector* allParagraphsOffsets ) const { vector allParagraphsProperties; unsigned int paragraphOffset = 0; if (allParagraphsOffsets) { std::vector allParagraphs = GetAllParagraphsCopy(); for (std::vector::const_iterator iter = allParagraphs.begin(); iter != allParagraphs.end(); ++iter) { ITextItem* item = (ITextItem*)(iter->operator->()); Paragraph* paragraph = dynamic_cast(item); if (paragraph) { allParagraphsProperties.push_back( paragraph->GetProperties()); allParagraphsOffsets->push_back( paragraphOffset ); paragraphOffset += (sizeof(wchar_t) * paragraph->GetAllText().size()); } } } return allParagraphsProperties; } vector List::GetAllRunProperties( vector* allRunsOffsets ) const { vector allRunProperties; unsigned int runOffset = 0; if (allRunsOffsets) { std::vector allParagraphs = GetAllParagraphsCopy(); for (std::vector::const_iterator iter = allParagraphs.begin(); iter != allParagraphs.end(); ++iter) { std::vector paragraphRunProperties; std::vector paragraphRunsOffsets; // ITextItem* item = (ITextItem*)(iter->operator->()); Paragraph* paragraph = dynamic_cast(item); if (paragraph) { paragraphRunProperties = paragraph->GetAllRunProperties(¶graphRunsOffsets); for (unsigned int i = 0; i < paragraphRunProperties.size(); ++i) { allRunProperties.push_back( paragraphRunProperties[i] ); allRunsOffsets->push_back( runOffset + paragraphRunsOffsets[i] ); } runOffset += (sizeof(wchar_t) * paragraph->GetAllText().size()); } } } return allRunProperties; } List::~List() { } IVirtualConstructor* List::New() const { return new List(); } IVirtualConstructor* List::Clone() const { return new List( *this ); } }