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