1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/vml/vmltextboxcontext.hxx"
25 
26 namespace oox {
27 namespace vml {
28 
29 // ============================================================================
30 
31 using ::oox::core::ContextHandler2;
32 using ::oox::core::ContextHandler2Helper;
33 using ::oox::core::ContextHandlerRef;
34 using ::rtl::OUString;
35 
36 // ============================================================================
37 
38 TextPortionContext::TextPortionContext( ContextHandler2Helper& rParent,
39         TextBox& rTextBox, const TextFontModel& rParentFont,
40         sal_Int32 nElement, const AttributeList& rAttribs ) :
41     ContextHandler2( rParent ),
42     mrTextBox( rTextBox ),
43     maFont( rParentFont ),
44     mnInitialPortions( rTextBox.getPortionCount() )
45 {
46     switch( nElement )
47     {
48         case XML_font:
49             maFont.moName = rAttribs.getXString( XML_face );
50             maFont.moColor = rAttribs.getXString( XML_color );
51             maFont.monSize = rAttribs.getInteger( XML_size );
52         break;
53         case XML_u:
54             OSL_ENSURE( !maFont.monUnderline, "TextPortionContext::TextPortionContext - nested <u> elements" );
55             maFont.monUnderline = (rAttribs.getToken( XML_class, XML_TOKEN_INVALID ) == XML_font4) ? XML_double : XML_single;
56         break;
57         case XML_sub:
58         case XML_sup:
59             OSL_ENSURE( !maFont.monEscapement, "TextPortionContext::TextPortionContext - nested <sub> or <sup> elements" );
60             maFont.monEscapement = nElement;
61         break;
62         case XML_b:
63             OSL_ENSURE( !maFont.mobBold, "TextPortionContext::TextPortionContext - nested <b> elements" );
64             maFont.mobBold = true;
65         break;
66         case XML_i:
67             OSL_ENSURE( !maFont.mobItalic, "TextPortionContext::TextPortionContext - nested <i> elements" );
68             maFont.mobItalic = true;
69         break;
70         case XML_s:
71             OSL_ENSURE( !maFont.mobStrikeout, "TextPortionContext::TextPortionContext - nested <s> elements" );
72             maFont.mobStrikeout = true;
73         break;
74         case XML_span:
75         break;
76         default:
77             OSL_ENSURE( false, "TextPortionContext::TextPortionContext - unknown element" );
78     }
79 }
80 
81 ContextHandlerRef TextPortionContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
82 {
83     OSL_ENSURE( nElement != XML_font, "TextPortionContext::onCreateContext - nested <font> elements" );
84     return new TextPortionContext( *this, mrTextBox, maFont, nElement, rAttribs );
85 }
86 
87 void TextPortionContext::onCharacters( const OUString& rChars )
88 {
89     switch( getCurrentElement() )
90     {
91         case XML_span:
92             // replace all NBSP characters with SP
93             mrTextBox.appendPortion( maFont, rChars.replace( 0xA0, ' ' ) );
94         break;
95         default:
96             mrTextBox.appendPortion( maFont, rChars );
97     }
98 }
99 
100 void TextPortionContext::onEndElement()
101 {
102     /*  A child element without own child elements may contain a single space
103         character, for example:
104 
105           <div>
106             <font><i>abc</i></font>
107             <font> </font>
108             <font><b>def</b></font>
109           </div>
110 
111         represents the italic text 'abc', an unformatted space character, and
112         the bold text 'def'. Unfortunately, the XML parser skips the space
113         character without issuing a 'characters' event. The class member
114         'mnInitialPortions' contains the number of text portions existing when
115         this context has been constructed. If no text has been added in the
116         meantime, the space character has to be added manually.
117      */
118     if( mrTextBox.getPortionCount() == mnInitialPortions )
119         mrTextBox.appendPortion( maFont, OUString( sal_Unicode( ' ' ) ) );
120 }
121 
122 // ============================================================================
123 
124 TextBoxContext::TextBoxContext( ContextHandler2Helper& rParent, TextBox& rTextBox, const AttributeList& /*rAttribs*/ ) :
125     ContextHandler2( rParent ),
126     mrTextBox( rTextBox )
127 {
128 }
129 
130 ContextHandlerRef TextBoxContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
131 {
132     switch( getCurrentElement() )
133     {
134         case VML_TOKEN( textbox ):
135             if( nElement == XML_div ) return this;
136         break;
137         case XML_div:
138             if( nElement == XML_font ) return new TextPortionContext( *this, mrTextBox, TextFontModel(), nElement, rAttribs );
139         break;
140     }
141     return 0;
142 }
143 
144 // ============================================================================
145 
146 } // namespace vml
147 } // namespace oox
148