LibreOffice Module oox (master)  1
textbodycontext.cxx
Go to the documentation of this file.
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 
26 #include <drawingml/textfield.hxx>
28 #include <oox/drawingml/shape.hxx>
29 #include <oox/token/namespaces.hxx>
31 #include <sax/fastattribs.hxx>
32 
33 #include <oox/mathml/import.hxx>
34 
35 #include <sal/log.hxx>
36 
37 using namespace ::oox::core;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::text;
40 using namespace ::com::sun::star::xml::sax;
41 
42 namespace oox::drawingml {
43 
44 namespace {
45 
46 // CT_TextParagraph
47 class TextParagraphContext : public ContextHandler2
48 {
49 public:
50  TextParagraphContext( ContextHandler2Helper const & rParent, TextParagraph& rPara );
51 
52  virtual ContextHandlerRef onCreateContext( sal_Int32 aElementToken, const AttributeList& rAttribs ) override;
53 
54 protected:
55  TextParagraph& mrParagraph;
56 };
57 
58 }
59 
60 TextParagraphContext::TextParagraphContext( ContextHandler2Helper const & rParent, TextParagraph& rPara )
61 : ContextHandler2( rParent )
62 , mrParagraph( rPara )
63 {
64  mbEnableTrimSpace = false;
65 }
66 
67 ContextHandlerRef TextParagraphContext::onCreateContext( sal_Int32 aElementToken, const AttributeList& rAttribs )
68 {
69  // EG_TextRun
70  switch( aElementToken )
71  {
72  case A_TOKEN( r ): // "CT_RegularTextRun" Regular Text Run.
73  case W_TOKEN( r ):
74  {
75  TextRunPtr pRun = std::make_shared<TextRun>();
76  mrParagraph.addRun( pRun );
77  return new RegularTextRunContext( *this, pRun );
78  }
79  case A_TOKEN( br ): // "CT_TextLineBreak" Soft return line break (vertical tab).
80  {
81  TextRunPtr pRun = std::make_shared<TextRun>();
82  pRun->setLineBreak();
83  mrParagraph.addRun( pRun );
84  return new RegularTextRunContext( *this, pRun );
85  }
86  case A_TOKEN( fld ): // "CT_TextField" Text Field.
87  {
88  auto pField = std::make_shared<TextField>();
89  mrParagraph.addRun( pField );
90  return new TextFieldContext( *this, rAttribs, *pField );
91  }
92  case A_TOKEN( pPr ):
93  case W_TOKEN( pPr ):
94  mrParagraph.setHasProperties();
95  return new TextParagraphPropertiesContext( *this, rAttribs, mrParagraph.getProperties() );
96  case A_TOKEN( endParaRPr ):
97  return new TextCharacterPropertiesContext( *this, rAttribs, mrParagraph.getEndProperties() );
98  case W_TOKEN( sdt ):
99  case W_TOKEN( sdtContent ):
100  return this;
101  case W_TOKEN( del ):
102  break;
103  case W_TOKEN( ins ):
104  return this;
105  case OOX_TOKEN(a14, m):
107  default:
108  SAL_WARN("oox", "TextParagraphContext::onCreateContext: unhandled element: " << getBaseToken(aElementToken));
109  }
110 
111  return nullptr;
112 }
113 
114 RegularTextRunContext::RegularTextRunContext( ContextHandler2Helper const & rParent, TextRunPtr const & pRunPtr )
115 : ContextHandler2( rParent )
116 , mpRunPtr( pRunPtr )
117 , mbIsInText( false )
118 {
119 }
120 
122 {
123  switch( getCurrentElement() )
124  {
125  case A_TOKEN( t ):
126  case W_TOKEN( t ):
127  {
128  mbIsInText = false;
129  break;
130  }
131  case A_TOKEN( r ):
132  break;
133  }
134 }
135 
136 void RegularTextRunContext::onCharacters( const OUString& aChars )
137 {
138  if( mbIsInText )
139  {
140  mpRunPtr->getText() += aChars;
141  }
142 }
143 
145 {
146  switch( aElementToken )
147  {
148  case A_TOKEN( rPr ): // "CT_TextCharPropertyBag" The text char properties of this text run.
149  case W_TOKEN( rPr ):
150  return new TextCharacterPropertiesContext( *this, rAttribs, mpRunPtr->getTextCharacterProperties() );
151  case A_TOKEN( t ): // "xsd:string" minOccurs="1" The actual text string.
152  case W_TOKEN( t ):
153  mbIsInText = true;
154  break;
155  case W_TOKEN( drawing ):
156  break;
157  default:
158  SAL_WARN("oox", "RegularTextRunContext::onCreateContext: unhandled element: " << getBaseToken(aElementToken));
159  break;
160  }
161 
162  return this;
163 }
164 
166 : ContextHandler2( rParent )
167 , mrTextBody( rTextBody )
168 {
169 }
170 
171 TextBodyContext::TextBodyContext(ContextHandler2Helper const& rParent, const ShapePtr& pShapePtr)
172  : TextBodyContext(rParent, *pShapePtr->getTextBody())
173 {
174  mpShapePtr = pShapePtr;
175 }
176 
177 ContextHandlerRef TextBodyContext::onCreateContext( sal_Int32 aElementToken, const AttributeList& rAttribs )
178 {
179  switch( aElementToken )
180  {
181  case A_TOKEN( bodyPr ): // CT_TextBodyPropertyBag
182  if (sax_fastparser::castToFastAttributeList(rAttribs.getFastAttributeList()).getFastAttributeTokens().size() > 0)
184  if ( mpShapePtr )
185  return new TextBodyPropertiesContext( *this, rAttribs, mpShapePtr );
186  else
187  return new TextBodyPropertiesContext( *this, rAttribs, mrTextBody.getTextProperties() );
188  case A_TOKEN( lstStyle ): // CT_TextListStyle
189  return new TextListStyleContext( *this, mrTextBody.getTextListStyle() );
190  case A_TOKEN( p ): // CT_TextParagraph
191  case W_TOKEN( p ):
192  return new TextParagraphContext( *this, mrTextBody.addParagraph() );
193  case W_TOKEN( sdt ):
194  case W_TOKEN( sdtContent ):
195  return this;
196  case W_TOKEN( sdtPr ):
197  case W_TOKEN( sdtEndPr ):
198  break;
199  case W_TOKEN( tbl ):
200  break;
201  default:
202  SAL_WARN("oox", "TextBodyContext::onCreateContext: unhandled element: " << getBaseToken(aElementToken));
203  }
204 
205  return nullptr;
206 }
207 
208 }
209 
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
TextParagraph & mrParagraph
virtual ::oox::core::ContextHandlerRef onCreateContext(::sal_Int32 Element, const ::oox::AttributeList &rAttribs) override
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
std::shared_ptr< TextRun > TextRunPtr
Definition: textrun.hxx:65
exports com.sun.star. drawing
virtual void onEndElement() override
Will be called when the current element is about to be left.
virtual ::oox::core::ContextHandlerRef onCreateContext(::sal_Int32 Element, const ::oox::AttributeList &rAttribs) override
TextParagraph & addParagraph()
Definition: textbody.cxx:46
sal_Int32 getCurrentElement() const
Returns the identifier of the currently processed element.
Provides access to attribute values of an element.
XPropertyListType t
virtual void onCharacters(const OUString &aChars) override
Will be called before a new child element starts, or if the current element is about to be left...
rtl::Reference< core::ContextHandler > CreateLazyMathBufferingContext(core::ContextHandler const &rParent, drawingml::TextParagraph &rPara)
Definition: import.cxx:106
const TextListStyle & getTextListStyle() const
Definition: textbody.hxx:50
::rtl::Reference< ContextHandler > ContextHandlerRef
const css::uno::Reference< css::xml::sax::XFastAttributeList > & getFastAttributeList() const
Returns the wrapped com.sun.star.xml.sax.XFastAttributeList object.
void * p
TextBodyContext(::oox::core::ContextHandler2Helper const &rParent, TextBody &rTextBody)
#define SAL_WARN(area, stream)
std::shared_ptr< Shape > ShapePtr
Helper class that provides a context stack.
const TextBodyProperties & getTextProperties() const
Definition: textbody.hxx:53
void setHasNoninheritedBodyProperties()
Flags textbody as having a non-empty bodyPr tag.
Definition: textbody.hxx:81