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 :
23 : namespace oox {
24 : namespace vml {
25 :
26 : // ============================================================================
27 :
28 : using ::oox::core::ContextHandler2;
29 : using ::oox::core::ContextHandler2Helper;
30 : using ::oox::core::ContextHandlerRef;
31 : using ::rtl::OUString;
32 :
33 : // ============================================================================
34 :
35 2 : TextPortionContext::TextPortionContext( ContextHandler2Helper& rParent,
36 : TextBox& rTextBox, const TextFontModel& rParentFont,
37 : sal_Int32 nElement, const AttributeList& rAttribs ) :
38 : ContextHandler2( rParent ),
39 : mrTextBox( rTextBox ),
40 : maFont( rParentFont ),
41 2 : mnInitialPortions( rTextBox.getPortionCount() )
42 : {
43 2 : switch( nElement )
44 : {
45 : case XML_font:
46 2 : maFont.moName = rAttribs.getXString( XML_face );
47 2 : maFont.moColor = rAttribs.getXString( XML_color );
48 2 : maFont.monSize = rAttribs.getInteger( XML_size );
49 2 : break;
50 : case XML_u:
51 : OSL_ENSURE( !maFont.monUnderline, "TextPortionContext::TextPortionContext - nested <u> elements" );
52 0 : maFont.monUnderline = (rAttribs.getToken( XML_class, XML_TOKEN_INVALID ) == XML_font4) ? XML_double : XML_single;
53 0 : break;
54 : case XML_sub:
55 : case XML_sup:
56 : OSL_ENSURE( !maFont.monEscapement, "TextPortionContext::TextPortionContext - nested <sub> or <sup> elements" );
57 0 : maFont.monEscapement = nElement;
58 0 : break;
59 : case XML_b:
60 : OSL_ENSURE( !maFont.mobBold, "TextPortionContext::TextPortionContext - nested <b> elements" );
61 0 : maFont.mobBold = true;
62 0 : break;
63 : case XML_i:
64 : OSL_ENSURE( !maFont.mobItalic, "TextPortionContext::TextPortionContext - nested <i> elements" );
65 0 : maFont.mobItalic = true;
66 0 : break;
67 : case XML_s:
68 : OSL_ENSURE( !maFont.mobStrikeout, "TextPortionContext::TextPortionContext - nested <s> elements" );
69 0 : maFont.mobStrikeout = true;
70 0 : break;
71 : case XML_span:
72 0 : break;
73 : default:
74 : OSL_ENSURE( false, "TextPortionContext::TextPortionContext - unknown element" );
75 : }
76 2 : }
77 :
78 0 : ContextHandlerRef TextPortionContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
79 : {
80 : OSL_ENSURE( nElement != XML_font, "TextPortionContext::onCreateContext - nested <font> elements" );
81 0 : return new TextPortionContext( *this, mrTextBox, maFont, nElement, rAttribs );
82 : }
83 :
84 2 : void TextPortionContext::onCharacters( const OUString& rChars )
85 : {
86 2 : switch( getCurrentElement() )
87 : {
88 : case XML_span:
89 : // replace all NBSP characters with SP
90 0 : mrTextBox.appendPortion( maFont, rChars.replace( 0xA0, ' ' ) );
91 0 : break;
92 : default:
93 2 : mrTextBox.appendPortion( maFont, rChars );
94 : }
95 2 : }
96 :
97 2 : void TextPortionContext::onEndElement()
98 : {
99 : /* A child element without own child elements may contain a single space
100 : character, for example:
101 :
102 : <div>
103 : <font><i>abc</i></font>
104 : <font> </font>
105 : <font><b>def</b></font>
106 : </div>
107 :
108 : represents the italic text 'abc', an unformatted space character, and
109 : the bold text 'def'. Unfortunately, the XML parser skips the space
110 : character without issuing a 'characters' event. The class member
111 : 'mnInitialPortions' contains the number of text portions existing when
112 : this context has been constructed. If no text has been added in the
113 : meantime, the space character has to be added manually.
114 : */
115 2 : if( mrTextBox.getPortionCount() == mnInitialPortions )
116 0 : mrTextBox.appendPortion( maFont, OUString( sal_Unicode( ' ' ) ) );
117 2 : }
118 :
119 : // ============================================================================
120 :
121 26 : TextBoxContext::TextBoxContext( ContextHandler2Helper& rParent, TextBox& rTextBox, const AttributeList& rAttribs,
122 : const GraphicHelper& graphicHelper ) :
123 : ContextHandler2( rParent ),
124 26 : mrTextBox( rTextBox )
125 : {
126 26 : if( rAttribs.getString( XML_insetmode ).get() != "auto" )
127 : {
128 26 : OUString inset = rAttribs.getString( XML_inset ).get();
129 26 : OUString value;
130 26 : ConversionHelper::separatePair( value, inset, inset, ',' );
131 : rTextBox.borderDistanceLeft = ConversionHelper::decodeMeasureToHmm( graphicHelper,
132 26 : value.isEmpty() ? "0.1in" : value, 0, false, false );
133 26 : ConversionHelper::separatePair( value, inset, inset, ',' );
134 : rTextBox.borderDistanceTop = ConversionHelper::decodeMeasureToHmm( graphicHelper,
135 26 : value.isEmpty() ? "0.05in" : value, 0, false, false );
136 26 : ConversionHelper::separatePair( value, inset, inset, ',' );
137 : rTextBox.borderDistanceRight = ConversionHelper::decodeMeasureToHmm( graphicHelper,
138 26 : value.isEmpty() ? "0.1in" : value, 0, false, false );
139 26 : ConversionHelper::separatePair( value, inset, inset, ',' );
140 : rTextBox.borderDistanceBottom = ConversionHelper::decodeMeasureToHmm( graphicHelper,
141 26 : value.isEmpty() ? "0.05in" : value, 0, false, false );
142 26 : rTextBox.borderDistanceSet = true;
143 : }
144 26 : }
145 :
146 4 : ContextHandlerRef TextBoxContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
147 : {
148 4 : switch( getCurrentElement() )
149 : {
150 : case VML_TOKEN( textbox ):
151 2 : if( nElement == XML_div ) return this;
152 0 : break;
153 : case XML_div:
154 2 : if( nElement == XML_font ) return new TextPortionContext( *this, mrTextBox, TextFontModel(), nElement, rAttribs );
155 0 : break;
156 : }
157 0 : return 0;
158 : }
159 :
160 : // ============================================================================
161 :
162 : } // namespace vml
163 : } // namespace oox
164 :
165 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|