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 : using ::rtl::OUString;
34 :
35 : // ============================================================================
36 :
37 72 : TextPortionContext::TextPortionContext( ContextHandler2Helper& rParent,
38 : TextBox& rTextBox, const TextFontModel& rParentFont,
39 : sal_Int32 nElement, const AttributeList& rAttribs ) :
40 : ContextHandler2( rParent ),
41 : mrTextBox( rTextBox ),
42 : maFont( rParentFont ),
43 72 : mnInitialPortions( rTextBox.getPortionCount() )
44 : {
45 72 : 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 12 : break;
90 : default:
91 : OSL_ENSURE( false, "TextPortionContext::TextPortionContext - unknown element" );
92 : }
93 72 : }
94 :
95 127 : ContextHandlerRef TextPortionContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
96 : {
97 : OSL_ENSURE( nElement != XML_font, "TextPortionContext::onCreateContext - nested <font> elements" );
98 127 : if (getNamespace(getCurrentElement()) == NMSP_doc)
99 68 : return this;
100 59 : return new TextPortionContext( *this, mrTextBox, maFont, nElement, rAttribs );
101 : }
102 :
103 114 : void TextPortionContext::onCharacters( const OUString& rChars )
104 : {
105 114 : if (getNamespace(getCurrentElement()) == NMSP_doc && getCurrentElement() != OOX_TOKEN(doc, t))
106 132 : return;
107 :
108 96 : switch( getCurrentElement() )
109 : {
110 : case XML_span:
111 : // replace all NBSP characters with SP
112 0 : mrTextBox.appendPortion( maFont, rChars.replace( 0xA0, ' ' ) );
113 0 : break;
114 : default:
115 96 : mrTextBox.appendPortion( maFont, rChars );
116 : }
117 : }
118 :
119 140 : void TextPortionContext::onStartElement(const AttributeList& rAttribs)
120 : {
121 140 : switch (getCurrentElement())
122 : {
123 : case OOX_TOKEN(doc, b):
124 8 : maFont.mobBold = true;
125 8 : break;
126 : case OOX_TOKEN(doc, sz):
127 9 : maFont.monSize = rAttribs.getInteger( OOX_TOKEN(doc, val) );
128 9 : break;
129 : }
130 140 : }
131 :
132 140 : void TextPortionContext::onEndElement()
133 : {
134 140 : if (getNamespace(getCurrentElement()) == NMSP_doc && getCurrentElement() != OOX_TOKEN(doc, t))
135 208 : return;
136 :
137 : /* A child element without own child elements may contain a single space
138 : character, for example:
139 :
140 : <div>
141 : <font><i>abc</i></font>
142 : <font> </font>
143 : <font><b>def</b></font>
144 : </div>
145 :
146 : represents the italic text 'abc', an unformatted space character, and
147 : the bold text 'def'. Unfortunately, the XML parser skips the space
148 : character without issuing a 'characters' event. The class member
149 : 'mnInitialPortions' contains the number of text portions existing when
150 : this context has been constructed. If no text has been added in the
151 : meantime, the space character has to be added manually.
152 : */
153 72 : if( mrTextBox.getPortionCount() == mnInitialPortions )
154 35 : mrTextBox.appendPortion( maFont, OUString( sal_Unicode( ' ' ) ) );
155 : }
156 :
157 : // ============================================================================
158 :
159 15 : TextBoxContext::TextBoxContext( ContextHandler2Helper& rParent, TextBox& rTextBox, const AttributeList& rAttribs,
160 : const GraphicHelper& graphicHelper ) :
161 : ContextHandler2( rParent ),
162 15 : mrTextBox( rTextBox )
163 : {
164 15 : if( rAttribs.getString( XML_insetmode ).get() != "auto" )
165 : {
166 15 : OUString inset = rAttribs.getString( XML_inset ).get();
167 15 : OUString value;
168 15 : ConversionHelper::separatePair( value, inset, inset, ',' );
169 : rTextBox.borderDistanceLeft = ConversionHelper::decodeMeasureToHmm( graphicHelper,
170 15 : value.isEmpty() ? "0.1in" : value, 0, false, false );
171 15 : ConversionHelper::separatePair( value, inset, inset, ',' );
172 : rTextBox.borderDistanceTop = ConversionHelper::decodeMeasureToHmm( graphicHelper,
173 15 : value.isEmpty() ? "0.05in" : value, 0, false, false );
174 15 : ConversionHelper::separatePair( value, inset, inset, ',' );
175 : rTextBox.borderDistanceRight = ConversionHelper::decodeMeasureToHmm( graphicHelper,
176 15 : value.isEmpty() ? "0.1in" : value, 0, false, false );
177 15 : ConversionHelper::separatePair( value, inset, inset, ',' );
178 : rTextBox.borderDistanceBottom = ConversionHelper::decodeMeasureToHmm( graphicHelper,
179 15 : value.isEmpty() ? "0.05in" : value, 0, false, false );
180 15 : rTextBox.borderDistanceSet = true;
181 : }
182 15 : }
183 :
184 47 : ContextHandlerRef TextBoxContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
185 : {
186 47 : switch( getCurrentElement() )
187 : {
188 : case VML_TOKEN( textbox ):
189 7 : if( nElement == XML_div ) return this;
190 6 : else if (nElement == OOX_TOKEN(doc, txbxContent)) return this;
191 0 : break;
192 : case XML_div:
193 1 : if( nElement == XML_font ) return new TextPortionContext( *this, mrTextBox, TextFontModel(), nElement, rAttribs );
194 0 : break;
195 : case OOX_TOKEN(doc, txbxContent):
196 15 : if (nElement == OOX_TOKEN(doc, p)) return this;
197 0 : break;
198 : case OOX_TOKEN(doc, p):
199 24 : if (nElement == OOX_TOKEN(doc, r)) return new TextPortionContext( *this, mrTextBox, TextFontModel(), nElement, rAttribs );
200 12 : break;
201 : }
202 12 : return 0;
203 : }
204 :
205 : // ============================================================================
206 :
207 : } // namespace vml
208 : } // namespace oox
209 :
210 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|