Line data Source code
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 "oox/vml/vmlformatting.hxx"
21 : #include "oox/vml/vmltextboxcontext.hxx"
22 : #include "oox/vml/vmlshape.hxx"
23 : #include <com/sun/star/drawing/XShape.hpp>
24 :
25 : namespace oox {
26 : namespace vml {
27 :
28 : // ============================================================================
29 :
30 : using ::oox::core::ContextHandler2;
31 : using ::oox::core::ContextHandler2Helper;
32 : using ::oox::core::ContextHandlerRef;
33 :
34 : // ============================================================================
35 :
36 82 : TextPortionContext::TextPortionContext( ContextHandler2Helper& rParent,
37 : TextBox& rTextBox, TextParagraphModel& rParagraph, const TextFontModel& rParentFont,
38 : sal_Int32 nElement, const AttributeList& rAttribs ) :
39 : ContextHandler2( rParent ),
40 : mrTextBox( rTextBox ),
41 : maParagraph( rParagraph ),
42 : maFont( rParentFont ),
43 82 : mnInitialPortions( rTextBox.getPortionCount() )
44 : {
45 82 : switch( nElement )
46 : {
47 : case XML_font:
48 1 : maFont.moName = rAttribs.getXString( XML_face );
49 1 : maFont.moColor = rAttribs.getXString( XML_color );
50 1 : maFont.monSize = rAttribs.getInteger( XML_size );
51 1 : break;
52 : case XML_u:
53 : OSL_ENSURE( !maFont.monUnderline, "TextPortionContext::TextPortionContext - nested <u> elements" );
54 0 : maFont.monUnderline = (rAttribs.getToken( XML_class, XML_TOKEN_INVALID ) == XML_font4) ? XML_double : XML_single;
55 0 : break;
56 : case XML_sub:
57 : case XML_sup:
58 : OSL_ENSURE( !maFont.monEscapement, "TextPortionContext::TextPortionContext - nested <sub> or <sup> elements" );
59 0 : maFont.monEscapement = nElement;
60 0 : break;
61 : case XML_b:
62 : OSL_ENSURE( !maFont.mobBold, "TextPortionContext::TextPortionContext - nested <b> elements" );
63 0 : maFont.mobBold = true;
64 0 : break;
65 : case XML_i:
66 : OSL_ENSURE( !maFont.mobItalic, "TextPortionContext::TextPortionContext - nested <i> elements" );
67 0 : maFont.mobItalic = true;
68 0 : break;
69 : case XML_s:
70 : OSL_ENSURE( !maFont.mobStrikeout, "TextPortionContext::TextPortionContext - nested <s> elements" );
71 0 : maFont.mobStrikeout = true;
72 0 : break;
73 : case OOX_TOKEN(dml, blip):
74 : {
75 2 : OptValue<OUString> oRelId = rAttribs.getString(R_TOKEN(embed));
76 2 : if (oRelId.has())
77 2 : mrTextBox.mrTypeModel.moGraphicPath = getFragmentPathFromRelId(oRelId.get());
78 : }
79 2 : break;
80 : case VML_TOKEN(imagedata):
81 : {
82 1 : OptValue<OUString> oRelId = rAttribs.getString(R_TOKEN(id));
83 1 : if (oRelId.has())
84 1 : mrTextBox.mrTypeModel.moGraphicPath = getFragmentPathFromRelId(oRelId.get());
85 : }
86 1 : break;
87 : case XML_span:
88 : case OOX_TOKEN(doc, r):
89 22 : break;
90 : default:
91 : OSL_ENSURE( false, "TextPortionContext::TextPortionContext - unknown element" );
92 : }
93 82 : }
94 :
95 170 : ContextHandlerRef TextPortionContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
96 : {
97 : OSL_ENSURE( nElement != XML_font, "TextPortionContext::onCreateContext - nested <font> elements" );
98 170 : if (getNamespace(getCurrentElement()) == NMSP_doc)
99 111 : return this;
100 59 : return new TextPortionContext( *this, mrTextBox, maParagraph, maFont, nElement, rAttribs );
101 : }
102 :
103 186 : void TextPortionContext::onCharacters( const OUString& rChars )
104 : {
105 186 : if (getNamespace(getCurrentElement()) == NMSP_doc && getCurrentElement() != OOX_TOKEN(doc, t))
106 267 : return;
107 :
108 105 : switch( getCurrentElement() )
109 : {
110 : case XML_span:
111 : // replace all NBSP characters with SP
112 0 : mrTextBox.appendPortion( maParagraph, maFont, rChars.replace( 0xA0, ' ' ) );
113 0 : break;
114 : default:
115 105 : mrTextBox.appendPortion( maParagraph, maFont, rChars );
116 : }
117 : }
118 :
119 193 : void TextPortionContext::onStartElement(const AttributeList& rAttribs)
120 : {
121 193 : switch (getCurrentElement())
122 : {
123 : case OOX_TOKEN(doc, b):
124 11 : maFont.mobBold = true;
125 11 : break;
126 : case OOX_TOKEN(doc, sz):
127 15 : maFont.monSize = rAttribs.getInteger( OOX_TOKEN(doc, val) );
128 15 : break;
129 : case OOX_TOKEN(doc, br):
130 1 : mrTextBox.appendPortion( maParagraph, maFont, "\n" );
131 1 : break;
132 : case OOX_TOKEN(doc, color):
133 5 : maFont.moColor = rAttribs.getString( OOX_TOKEN(doc, val) );
134 5 : break;
135 : }
136 193 : }
137 :
138 193 : void TextPortionContext::onEndElement()
139 : {
140 193 : if (getNamespace(getCurrentElement()) == NMSP_doc && getCurrentElement() != OOX_TOKEN(doc, t))
141 305 : return;
142 :
143 : /* A child element without own child elements may contain a single space
144 : character, for example:
145 :
146 : <div>
147 : <font><i>abc</i></font>
148 : <font> </font>
149 : <font><b>def</b></font>
150 : </div>
151 :
152 : represents the italic text 'abc', an unformatted space character, and
153 : the bold text 'def'. Unfortunately, the XML parser skips the space
154 : character without issuing a 'characters' event. The class member
155 : 'mnInitialPortions' contains the number of text portions existing when
156 : this context has been constructed. If no text has been added in the
157 : meantime, the space character has to be added manually.
158 : */
159 81 : if( mrTextBox.getPortionCount() == mnInitialPortions )
160 35 : mrTextBox.appendPortion( maParagraph, maFont, OUString( sal_Unicode( ' ' ) ) );
161 : }
162 :
163 : // ============================================================================
164 :
165 36 : TextBoxContext::TextBoxContext( ContextHandler2Helper& rParent, TextBox& rTextBox, const AttributeList& rAttribs,
166 : const GraphicHelper& graphicHelper ) :
167 : ContextHandler2( rParent ),
168 36 : mrTextBox( rTextBox )
169 : {
170 36 : if( rAttribs.getString( XML_insetmode ).get() != "auto" )
171 : {
172 36 : OUString inset = rAttribs.getString( XML_inset ).get();
173 72 : OUString value;
174 36 : ConversionHelper::separatePair( value, inset, inset, ',' );
175 : rTextBox.borderDistanceLeft = ConversionHelper::decodeMeasureToHmm( graphicHelper,
176 36 : value.isEmpty() ? "0.1in" : value, 0, false, false );
177 36 : ConversionHelper::separatePair( value, inset, inset, ',' );
178 : rTextBox.borderDistanceTop = ConversionHelper::decodeMeasureToHmm( graphicHelper,
179 36 : value.isEmpty() ? "0.05in" : value, 0, false, false );
180 36 : ConversionHelper::separatePair( value, inset, inset, ',' );
181 : rTextBox.borderDistanceRight = ConversionHelper::decodeMeasureToHmm( graphicHelper,
182 36 : value.isEmpty() ? "0.1in" : value, 0, false, false );
183 36 : ConversionHelper::separatePair( value, inset, inset, ',' );
184 : rTextBox.borderDistanceBottom = ConversionHelper::decodeMeasureToHmm( graphicHelper,
185 36 : value.isEmpty() ? "0.05in" : value, 0, false, false );
186 72 : rTextBox.borderDistanceSet = true;
187 : }
188 :
189 36 : OUString sStyle = rAttribs.getString( XML_style, OUString() );
190 36 : sal_Int32 nIndex = 0;
191 113 : while( nIndex >= 0 )
192 : {
193 82 : OUString aName, aValue;
194 41 : if( ConversionHelper::separatePair( aName, aValue, sStyle.getToken( 0, ';', nIndex ), ':' ) )
195 : {
196 22 : if( aName == "layout-flow" ) rTextBox.maLayoutFlow = aValue;
197 21 : else if (aName == "mso-fit-shape-to-text")
198 6 : rTextBox.mrTypeModel.mbAutoHeight = true;
199 : else
200 : SAL_WARN("oox", "unhandled style property: " << aName);
201 : }
202 77 : }
203 36 : }
204 :
205 215 : ContextHandlerRef TextBoxContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
206 : {
207 215 : switch( getCurrentElement() )
208 : {
209 : case VML_TOKEN( textbox ):
210 15 : if( nElement == XML_div ) return this;
211 14 : else if (nElement == OOX_TOKEN(doc, txbxContent)) return this;
212 0 : break;
213 : case XML_div:
214 1 : if( nElement == XML_font ) return new TextPortionContext( *this, mrTextBox, maParagraph, TextFontModel(), nElement, rAttribs );
215 0 : break;
216 : case OOX_TOKEN(doc, txbxContent):
217 25 : if (nElement == OOX_TOKEN(doc, p)) return this;
218 0 : break;
219 : case OOX_TOKEN(doc, p):
220 52 : if (nElement == OOX_TOKEN(doc, r))
221 22 : return new TextPortionContext( *this, mrTextBox, maParagraph, TextFontModel(), nElement, rAttribs );
222 : else
223 30 : return this;
224 : break;
225 : case OOX_TOKEN(doc, pPr):
226 44 : return this;
227 : break;
228 : }
229 78 : return 0;
230 : }
231 :
232 150 : void TextBoxContext::onStartElement(const AttributeList& rAttribs)
233 : {
234 150 : switch (getCurrentElement())
235 : {
236 : case OOX_TOKEN(doc, jc):
237 7 : maParagraph.moParaAdjust = rAttribs.getString( OOX_TOKEN(doc, val) );
238 7 : break;
239 : }
240 150 : }
241 :
242 150 : void TextBoxContext::onEndElement()
243 : {
244 150 : if (getCurrentElement() == OOX_TOKEN(doc, p))
245 : {
246 25 : mrTextBox.appendPortion( maParagraph, TextFontModel(), "\n" );
247 25 : maParagraph = TextParagraphModel();
248 : }
249 150 : }
250 :
251 : // ============================================================================
252 :
253 : } // namespace vml
254 : } // namespace oox
255 :
256 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|