1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <drawingml/textbody.hxx>
21 #include <com/sun/star/text/XText.hpp>
22 #include <com/sun/star/text/XTextCursor.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <drawingml/textparagraph.hxx>
25 #include <oox/helper/propertyset.hxx>
26 #include <oox/token/properties.hxx>
27 
28 using namespace ::com::sun::star::uno;
29 using namespace ::com::sun::star::text;
30 using namespace ::com::sun::star::beans;
31 
32 namespace oox { namespace drawingml {
33 
TextBody()34 TextBody::TextBody()
35 {
36 }
37 
TextBody(const TextBodyPtr & pBody)38 TextBody::TextBody( const TextBodyPtr& pBody )
39 {
40     if( pBody.get() ) {
41         maTextProperties = pBody->maTextProperties;
42         maTextListStyle = pBody->maTextListStyle;
43     }
44 }
45 
~TextBody()46 TextBody::~TextBody()
47 {
48 }
49 
addParagraph()50 TextParagraph& TextBody::addParagraph()
51 {
52     std::shared_ptr< TextParagraph > xPara( new TextParagraph );
53     maParagraphs.push_back( xPara );
54     return *xPara;
55 }
56 
appendParagraph(std::shared_ptr<TextParagraph> pTextParagraph)57 void TextBody::appendParagraph(std::shared_ptr<TextParagraph> pTextParagraph)
58 {
59     maParagraphs.push_back(pTextParagraph);
60 }
61 
insertAt(const::oox::core::XmlFilterBase & rFilterBase,const Reference<XText> & xText,const Reference<XTextCursor> & xAt,const TextCharacterProperties & rTextStyleProperties,const TextListStylePtr & pMasterTextListStylePtr) const62 void TextBody::insertAt(
63         const ::oox::core::XmlFilterBase& rFilterBase,
64         const Reference < XText > & xText,
65         const Reference < XTextCursor > & xAt,
66         const TextCharacterProperties& rTextStyleProperties,
67         const TextListStylePtr& pMasterTextListStylePtr ) const
68 {
69     TextListStyle aCombinedTextStyle;
70     aCombinedTextStyle.apply( *pMasterTextListStylePtr );
71     aCombinedTextStyle.apply( maTextListStyle );
72 
73     Reference<css::beans::XPropertySet> xPropertySet(xAt, UNO_QUERY);
74     float nCharHeight = xPropertySet->getPropertyValue("CharHeight").get<float>();
75     size_t nIndex = 0;
76     for (auto const& paragraph : maParagraphs)
77     {
78         paragraph->insertAt( rFilterBase, xText, xAt, rTextStyleProperties, aCombinedTextStyle, (nIndex == 0), nCharHeight );
79         ++nIndex;
80     }
81 }
82 
isEmpty() const83 bool TextBody::isEmpty() const
84 {
85     if (maParagraphs.empty())
86         return true;
87     if ( maParagraphs.size() > 1 )
88         return false;
89 
90     const TextRunVector aRuns = maParagraphs[0]->getRuns();
91     if ( aRuns.empty() )
92         return true;
93     if ( aRuns.size() > 1 )
94         return false;
95 
96     return aRuns[0]->getText().getLength() <= 0;
97 }
98 
toString() const99 OUString TextBody::toString() const
100 {
101     if (!isEmpty())
102         return maParagraphs.front()->getRuns().front()->getText();
103     else
104         return OUString();
105 }
106 
ApplyStyleEmpty(const::oox::core::XmlFilterBase & rFilterBase,const Reference<XText> & xText,const TextCharacterProperties & rTextStyleProperties,const TextListStylePtr & pMasterTextListStylePtr) const107 void TextBody::ApplyStyleEmpty(
108     const ::oox::core::XmlFilterBase& rFilterBase,
109     const Reference < XText > & xText,
110     const TextCharacterProperties& rTextStyleProperties,
111     const TextListStylePtr& pMasterTextListStylePtr) const
112 {
113     assert(isEmpty());
114 
115     if (maParagraphs.empty())
116         return;
117 
118     // Apply character properties
119     TextListStyle aCombinedTextStyle;
120     aCombinedTextStyle.apply( *pMasterTextListStylePtr );
121     aCombinedTextStyle.apply( maTextListStyle );
122 
123     PropertySet aPropSet(xText);
124     TextCharacterProperties aTextCharacterProps(maParagraphs[0]->getCharacterStyle(rTextStyleProperties, aCombinedTextStyle));
125     aTextCharacterProps.pushToPropSet(aPropSet, rFilterBase);
126 
127     // Apply paragraph properties
128     TextParagraphPropertiesPtr pTextParagraphStyle = maParagraphs[0]->getParagraphStyle(aCombinedTextStyle);
129     if (pTextParagraphStyle.get())
130     {
131         Reference< XPropertySet > xProps(xText, UNO_QUERY);
132         PropertyMap aioBulletList;
133         aioBulletList.setProperty< sal_Int32 >(PROP_LeftMargin, 0); // Init bullets left margin to 0 (no bullets).
134         float nCharHeight = xProps->getPropertyValue("CharHeight").get<float>();
135         TextParagraphProperties aParaProp;
136         aParaProp.apply(*pTextParagraphStyle);
137         aParaProp.pushToPropSet(&rFilterBase, xProps, aioBulletList, &pTextParagraphStyle->getBulletList(), true, nCharHeight, true);
138     }
139 }
140 
141 } }
142 
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
144