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 : #include "ConversionHelper.hxx"
20 : #include "GraphicHelpers.hxx"
21 :
22 : #include <ooxml/resourceids.hxx>
23 :
24 : #include <com/sun/star/text/HoriOrientation.hpp>
25 : #include <com/sun/star/text/VertOrientation.hpp>
26 : #include <com/sun/star/text/RelOrientation.hpp>
27 : #include <com/sun/star/text/WrapTextMode.hpp>
28 :
29 : #include "dmapperLoggers.hxx"
30 :
31 : #include <iostream>
32 : using namespace std;
33 :
34 : namespace writerfilter {
35 : namespace dmapper {
36 :
37 : using namespace com::sun::star;
38 :
39 : int PositionHandler::savedPositionOffsetV = 0;
40 : int PositionHandler::savedPositionOffsetH = 0;
41 : int PositionHandler::savedAlignV = text::VertOrientation::NONE;
42 : int PositionHandler::savedAlignH = text::HoriOrientation::NONE;
43 :
44 1226 : PositionHandler::PositionHandler( bool vertical ) :
45 1226 : LoggedProperties(dmapper_logger, "PositionHandler")
46 : {
47 1226 : m_nRelation = text::RelOrientation::FRAME;
48 1226 : if( vertical )
49 : {
50 613 : m_nPosition = savedPositionOffsetV;
51 613 : m_nOrient = savedAlignV;
52 613 : savedPositionOffsetV = 0;
53 613 : savedAlignV = text::VertOrientation::NONE;
54 : }
55 : else
56 : {
57 613 : m_nPosition = savedPositionOffsetH;
58 613 : m_nOrient = savedAlignH;
59 613 : savedPositionOffsetH = 0;
60 613 : savedAlignH = text::HoriOrientation::NONE;
61 : }
62 1226 : }
63 :
64 2452 : PositionHandler::~PositionHandler( )
65 : {
66 2452 : }
67 :
68 1222 : void PositionHandler::lcl_attribute( Id aName, Value& rVal )
69 : {
70 1222 : sal_Int32 nIntValue = rVal.getInt( );
71 1222 : switch ( aName )
72 : {
73 : case NS_ooxml::LN_CT_PosV_relativeFrom:
74 : {
75 : // TODO There are some other unhandled values
76 : static const Id pVertRelValues[] =
77 : {
78 : NS_ooxml::LN_Value_wordprocessingDrawing_ST_RelFromV_margin,
79 : NS_ooxml::LN_Value_wordprocessingDrawing_ST_RelFromV_page,
80 : NS_ooxml::LN_Value_wordprocessingDrawing_ST_RelFromV_paragraph,
81 : NS_ooxml::LN_Value_wordprocessingDrawing_ST_RelFromV_line
82 : };
83 :
84 : static const sal_Int16 pVertRelations[] =
85 : {
86 : text::RelOrientation::PAGE_PRINT_AREA,
87 : text::RelOrientation::PAGE_FRAME,
88 : text::RelOrientation::FRAME,
89 : text::RelOrientation::TEXT_LINE
90 : };
91 :
92 3055 : for ( int i = 0; i < 4; i++ )
93 : {
94 2444 : if ( pVertRelValues[i] == sal_uInt32( nIntValue ) )
95 611 : m_nRelation = pVertRelations[i];
96 : }
97 : }
98 611 : break;
99 : case NS_ooxml::LN_CT_PosH_relativeFrom:
100 : {
101 : // TODO There are some other unhandled values
102 : static const Id pHoriRelValues[] =
103 : {
104 : NS_ooxml::LN_Value_wordprocessingDrawing_ST_RelFromH_margin,
105 : NS_ooxml::LN_Value_wordprocessingDrawing_ST_RelFromH_page,
106 : NS_ooxml::LN_Value_wordprocessingDrawing_ST_RelFromH_column,
107 : NS_ooxml::LN_Value_wordprocessingDrawing_ST_RelFromH_character
108 : };
109 :
110 : static const sal_Int16 pHoriRelations[] =
111 : {
112 : text::RelOrientation::PAGE_PRINT_AREA,
113 : text::RelOrientation::PAGE_FRAME,
114 : text::RelOrientation::FRAME,
115 : text::RelOrientation::CHAR,
116 : };
117 :
118 3055 : for ( int i = 0; i < 4; i++ )
119 : {
120 2444 : if ( pHoriRelValues[i] == sal_uInt32( nIntValue ) )
121 611 : m_nRelation = pHoriRelations[i];
122 : }
123 : }
124 611 : break;
125 : default:
126 : #ifdef DEBUG_DOMAINMAPPER
127 : dmapper_logger->element("unhandled");
128 : #endif
129 0 : break;
130 : }
131 1222 : }
132 :
133 1216 : void PositionHandler::lcl_sprm( Sprm& )
134 : {
135 1216 : }
136 :
137 1226 : sal_Int16 PositionHandler::orientation() const
138 : {
139 1226 : if( m_nRelation == text::RelOrientation::TEXT_LINE )
140 : { // It appears that to 'line of text' alignment is backwards to other alignments,
141 : // 'top' meaning putting on top of the line instead of having top at the line.
142 6 : if( m_nOrient == text::VertOrientation::TOP )
143 3 : return text::VertOrientation::BOTTOM;
144 3 : else if( m_nOrient == text::VertOrientation::BOTTOM )
145 0 : return text::VertOrientation::TOP;
146 : }
147 1223 : return m_nOrient;
148 : }
149 :
150 1226 : sal_Int16 PositionHandler::relation() const
151 : {
152 1226 : return m_nRelation;
153 : }
154 :
155 1226 : sal_Int32 PositionHandler::position() const
156 : {
157 1226 : return m_nPosition;
158 : }
159 :
160 1165 : void PositionHandler::setPositionOffset(const OUString & sText, bool vertical)
161 : {
162 1165 : if( vertical )
163 591 : savedPositionOffsetV = ConversionHelper::convertEMUToMM100( sText.toInt32());
164 : else
165 574 : savedPositionOffsetH = ConversionHelper::convertEMUToMM100( sText.toInt32());
166 1165 : }
167 :
168 2176 : int PositionHandler::getPositionOffset(bool vertical)
169 : {
170 2176 : if (vertical)
171 1088 : return savedPositionOffsetV;
172 : else
173 1088 : return savedPositionOffsetH;
174 : }
175 :
176 38 : void PositionHandler::setAlignH(const OUString & sText)
177 : {
178 38 : if( sText == "left")
179 1 : savedAlignH = text::HoriOrientation::LEFT;
180 37 : else if( sText == "right" )
181 17 : savedAlignH = text::HoriOrientation::RIGHT;
182 20 : else if( sText == "center" )
183 20 : savedAlignH = text::HoriOrientation::CENTER;
184 0 : else if( sText == "inside" )
185 0 : savedAlignH = text::HoriOrientation::INSIDE;
186 0 : else if( sText == "outside" )
187 0 : savedAlignH = text::HoriOrientation::OUTSIDE;
188 38 : }
189 :
190 21 : void PositionHandler::setAlignV(const OUString & sText)
191 : {
192 21 : if( sText == "top" )
193 15 : savedAlignV = text::VertOrientation::TOP;
194 6 : else if( sText == "bottom" )
195 0 : savedAlignV = text::VertOrientation::BOTTOM;
196 6 : else if( sText == "center" )
197 6 : savedAlignV = text::VertOrientation::CENTER;
198 0 : else if( sText == "inside" )
199 0 : savedAlignV = text::VertOrientation::NONE;
200 0 : else if( sText == "outside" )
201 0 : savedAlignV = text::VertOrientation::NONE;
202 21 : }
203 :
204 10 : WrapHandler::WrapHandler( ) :
205 : LoggedProperties(dmapper_logger, "WrapHandler"),
206 : m_nType( 0 ),
207 10 : m_nSide( 0 )
208 : {
209 10 : }
210 :
211 20 : WrapHandler::~WrapHandler( )
212 : {
213 20 : }
214 :
215 20 : void WrapHandler::lcl_attribute( Id aName, Value& rVal )
216 : {
217 20 : switch ( aName )
218 : {
219 : case NS_ooxml::LN_CT_Wrap_type:
220 6 : m_nType = sal_Int32( rVal.getInt( ) );
221 6 : break;
222 : case NS_ooxml::LN_CT_Wrap_side:
223 0 : m_nSide = sal_Int32( rVal.getInt( ) );
224 0 : break;
225 : default:;
226 : }
227 20 : }
228 :
229 0 : void WrapHandler::lcl_sprm( Sprm& )
230 : {
231 0 : }
232 :
233 10 : sal_Int32 WrapHandler::getWrapMode( )
234 : {
235 : // The wrap values do not map directly to our wrap mode,
236 : // e.g. none in .docx actually means through in LO.
237 10 : sal_Int32 nMode = com::sun::star::text::WrapTextMode_THROUGHT;
238 :
239 10 : switch ( m_nType )
240 : {
241 : case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapType_square:
242 : // through and tight are somewhat complicated, approximate
243 : case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapType_tight:
244 : case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapType_through:
245 : {
246 6 : switch ( m_nSide )
247 : {
248 : case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapSide_left:
249 0 : nMode = com::sun::star::text::WrapTextMode_LEFT;
250 0 : break;
251 : case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapSide_right:
252 0 : nMode = com::sun::star::text::WrapTextMode_RIGHT;
253 0 : break;
254 : default:
255 6 : nMode = com::sun::star::text::WrapTextMode_PARALLEL;
256 : }
257 : }
258 6 : break;
259 : case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapType_topAndBottom:
260 0 : nMode = com::sun::star::text::WrapTextMode_NONE;
261 0 : break;
262 : case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapType_none:
263 : default:
264 4 : nMode = com::sun::star::text::WrapTextMode_THROUGHT;
265 : }
266 :
267 10 : return nMode;
268 : }
269 :
270 :
271 686 : void GraphicZOrderHelper::addItem( uno::Reference< beans::XPropertySet > props, sal_Int32 relativeHeight )
272 : {
273 686 : items[ relativeHeight ] = props;
274 686 : }
275 :
276 : // The relativeHeight value in .docx is an arbitrary number, where only the relative ordering matters.
277 : // But in Writer, the z-order is index in 0..(numitems-1) range, so whenever a new item needs to be
278 : // added in the proper z-order, it is necessary to find the proper index.
279 686 : sal_Int32 GraphicZOrderHelper::findZOrder( sal_Int32 relativeHeight, bool bOldStyle )
280 : {
281 686 : Items::const_iterator it = items.begin();
282 9191 : while( it != items.end())
283 : {
284 : // std::map is iterated sorted by key
285 :
286 : // Old-style ordering differs in what should happen when there is already an item with the same z-order:
287 : // we belong under it in case of new-style, but we belong below it in case of old-style.
288 8179 : bool bCond = bOldStyle ? (it->first > relativeHeight) : (it->first >= relativeHeight);
289 :
290 8179 : if( bCond )
291 360 : break; // this is the first one higher, we belong right before it
292 : else
293 7819 : ++it;
294 : }
295 686 : if( it == items.end()) // we're topmost
296 : {
297 326 : if( items.empty())
298 566 : return 0;
299 86 : sal_Int32 itemZOrder(0);
300 86 : --it;
301 344 : if( it->second->getPropertyValue(PropertyNameSupplier::GetPropertyNameSupplier()
302 258 : .GetName( PROP_Z_ORDER )) >>= itemZOrder )
303 86 : return itemZOrder + 1; // after the topmost
304 : }
305 : else
306 : {
307 360 : sal_Int32 itemZOrder(0);
308 1440 : if( it->second->getPropertyValue(PropertyNameSupplier::GetPropertyNameSupplier()
309 1080 : .GetName( PROP_Z_ORDER )) >>= itemZOrder )
310 360 : return itemZOrder; // before the item
311 : }
312 : SAL_WARN( "writerfilter", "findZOrder() didn't find item z-order" );
313 0 : return 0; // this should not(?) happen
314 : }
315 :
316 42 : } }
317 :
318 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|