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 #include "hyperlinkcontext.hxx"
33 
34 #include <oox/mathml/import.hxx>
35 
36 #include <sal/log.hxx>
37 
38 using namespace ::oox::core;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::text;
41 using namespace ::com::sun::star::xml::sax;
42 
43 namespace oox::drawingml {
44 
45 namespace {
46 
47 // CT_TextParagraph
48 class TextParagraphContext : public ContextHandler2
49 {
50 public:
51  TextParagraphContext( ContextHandler2Helper const & rParent, TextParagraph& rPara );
52 
53  virtual ContextHandlerRef onCreateContext( sal_Int32 aElementToken, const AttributeList& rAttribs ) override;
54 
55 protected:
56  TextParagraph& mrParagraph;
57 };
58 
59 }
60 
61 TextParagraphContext::TextParagraphContext( ContextHandler2Helper const & rParent, TextParagraph& rPara )
62 : ContextHandler2( rParent )
63 , mrParagraph( rPara )
64 {
65  mbEnableTrimSpace = false;
66 }
67 
68 ContextHandlerRef TextParagraphContext::onCreateContext( sal_Int32 aElementToken, const AttributeList& rAttribs )
69 {
70  // EG_TextRun
71  switch( aElementToken )
72  {
73  case A_TOKEN( r ): // "CT_RegularTextRun" Regular Text Run.
74  case W_TOKEN( r ):
75  {
76  TextRunPtr pRun = std::make_shared<TextRun>();
77  mrParagraph.addRun( pRun );
78  return new RegularTextRunContext( *this, pRun );
79  }
80  case A_TOKEN( br ): // "CT_TextLineBreak" Soft return line break (vertical tab).
81  {
82  TextRunPtr pRun = std::make_shared<TextRun>();
83  pRun->setLineBreak();
84  mrParagraph.addRun( pRun );
85  return new RegularTextRunContext( *this, pRun );
86  }
87  case A_TOKEN( fld ): // "CT_TextField" Text Field.
88  {
89  auto pField = std::make_shared<TextField>();
90  mrParagraph.addRun( pField );
91  return new TextFieldContext( *this, rAttribs, *pField );
92  }
93  case A_TOKEN( pPr ):
94  case W_TOKEN( pPr ):
95  mrParagraph.setHasProperties();
96  return new TextParagraphPropertiesContext( *this, rAttribs, mrParagraph.getProperties() );
97  case A_TOKEN( endParaRPr ):
98  return new TextCharacterPropertiesContext( *this, rAttribs, mrParagraph.getEndProperties() );
99  case W_TOKEN( sdt ):
100  case W_TOKEN( sdtContent ):
101  return this;
102  case W_TOKEN( del ):
103  break;
104  case W_TOKEN( ins ):
105  return this;
106  case OOX_TOKEN(a14, m):
108  case W_TOKEN( hyperlink ):
109  {
110  TextRunPtr pRun = std::make_shared<TextRun>();
111  mrParagraph.addRun(pRun);
112  // parse hyperlink attributes: use HyperLinkContext for that
113  rtl::Reference<HyperLinkContext> pContext(new HyperLinkContext(
114  *this, rAttribs, pRun->getTextCharacterProperties().maHyperlinkPropertyMap));
115  // but create text run context because HyperLinkContext can't process internal w:r, w:t, etc
116  return new RegularTextRunContext(*this, pRun);
117  }
118  default:
119  SAL_WARN("oox", "TextParagraphContext::onCreateContext: unhandled element: " << getBaseToken(aElementToken));
120  }
121 
122  return nullptr;
123 }
124 
125 RegularTextRunContext::RegularTextRunContext( ContextHandler2Helper const & rParent, TextRunPtr const & pRunPtr )
126 : ContextHandler2( rParent )
127 , mpRunPtr( pRunPtr )
128 , mbIsInText( false )
129 {
130 }
131 
133 {
134  switch( getCurrentElement() )
135  {
136  case A_TOKEN( t ):
137  case W_TOKEN( t ):
138  {
139  mbIsInText = false;
140  break;
141  }
142  case A_TOKEN( r ):
143  break;
144  }
145 }
146 
147 void RegularTextRunContext::onCharacters( const OUString& aChars )
148 {
149  if( mbIsInText )
150  {
151  mpRunPtr->getText() += aChars;
152  }
153 }
154 
156 {
157  switch( aElementToken )
158  {
159  case A_TOKEN( rPr ): // "CT_TextCharPropertyBag" The text char properties of this text run.
160  case W_TOKEN( rPr ):
161  return new TextCharacterPropertiesContext( *this, rAttribs, mpRunPtr->getTextCharacterProperties() );
162  case A_TOKEN( t ): // "xsd:string" minOccurs="1" The actual text string.
163  case W_TOKEN( t ):
164  mbIsInText = true;
165  break;
166  case W_TOKEN( drawing ):
167  break;
168  default:
169  SAL_WARN("oox", "RegularTextRunContext::onCreateContext: unhandled element: " << getBaseToken(aElementToken));
170  break;
171  }
172 
173  return this;
174 }
175 
177 : ContextHandler2( rParent )
178 , mrTextBody( rTextBody )
179 {
180 }
181 
182 TextBodyContext::TextBodyContext(ContextHandler2Helper const& rParent, const ShapePtr& pShapePtr)
183  : TextBodyContext(rParent, *pShapePtr->getTextBody())
184 {
185  mpShapePtr = pShapePtr;
186 }
187 
188 ContextHandlerRef TextBodyContext::onCreateContext( sal_Int32 aElementToken, const AttributeList& rAttribs )
189 {
190  switch( aElementToken )
191  {
192  case A_TOKEN( bodyPr ): // CT_TextBodyPropertyBag
193  if (sax_fastparser::castToFastAttributeList(rAttribs.getFastAttributeList()).getFastAttributeTokens().size() > 0)
195  if ( mpShapePtr )
196  return new TextBodyPropertiesContext( *this, rAttribs, mpShapePtr );
197  else
198  return new TextBodyPropertiesContext( *this, rAttribs, mrTextBody.getTextProperties() );
199  case A_TOKEN( lstStyle ): // CT_TextListStyle
200  return new TextListStyleContext( *this, mrTextBody.getTextListStyle() );
201  case A_TOKEN( p ): // CT_TextParagraph
202  case W_TOKEN( p ):
203  return new TextParagraphContext( *this, mrTextBody.addParagraph() );
204  case W_TOKEN( sdt ):
205  case W_TOKEN( sdtContent ):
206  return this;
207  case W_TOKEN( sdtPr ):
208  case W_TOKEN( sdtEndPr ):
209  break;
210  case W_TOKEN( tbl ):
211  break;
212  default:
213  SAL_WARN("oox", "TextBodyContext::onCreateContext: unhandled element: " << getBaseToken(aElementToken));
214  }
215 
216  return nullptr;
217 }
218 
219 }
220 
221 /* 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