Bump version to 4.1-6
[LibreOffice.git] / writerfilter / source / dmapper / GraphicHelpers.cxx
blob9a259b9e73e8592a56bccc06c35cddcfd5436723
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "ConversionHelper.hxx"
20 #include "GraphicHelpers.hxx"
22 #include <ooxml/resourceids.hxx>
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>
29 #include "dmapperLoggers.hxx"
31 #include <iostream>
32 using namespace std;
34 namespace writerfilter {
35 namespace dmapper {
37 using namespace com::sun::star;
39 int PositionHandler::savedPositionOffsetV = 0;
40 int PositionHandler::savedPositionOffsetH = 0;
41 int PositionHandler::savedAlignV = text::VertOrientation::NONE;
42 int PositionHandler::savedAlignH = text::HoriOrientation::NONE;
44 PositionHandler::PositionHandler( bool vertical ) :
45 LoggedProperties(dmapper_logger, "PositionHandler")
47 m_nRelation = text::RelOrientation::FRAME;
48 if( vertical )
50 m_nPosition = savedPositionOffsetV;
51 m_nOrient = savedAlignV;
52 savedPositionOffsetV = 0;
53 savedAlignV = text::VertOrientation::NONE;
55 else
57 m_nPosition = savedPositionOffsetH;
58 m_nOrient = savedAlignH;
59 savedPositionOffsetH = 0;
60 savedAlignH = text::HoriOrientation::NONE;
64 PositionHandler::~PositionHandler( )
68 void PositionHandler::lcl_attribute( Id aName, Value& rVal )
70 sal_Int32 nIntValue = rVal.getInt( );
71 switch ( aName )
73 case NS_ooxml::LN_CT_PosV_relativeFrom:
75 // TODO There are some other unhandled values
76 static Id pVertRelValues[] =
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
84 static sal_Int16 pVertRelations[] =
86 text::RelOrientation::PAGE_PRINT_AREA,
87 text::RelOrientation::PAGE_FRAME,
88 text::RelOrientation::FRAME,
89 text::RelOrientation::TEXT_LINE
92 for ( int i = 0; i < 4; i++ )
94 if ( pVertRelValues[i] == sal_uInt32( nIntValue ) )
95 m_nRelation = pVertRelations[i];
98 break;
99 case NS_ooxml::LN_CT_PosH_relativeFrom:
101 // TODO There are some other unhandled values
102 static Id pHoriRelValues[] =
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
110 static sal_Int16 pHoriRelations[] =
112 text::RelOrientation::PAGE_PRINT_AREA,
113 text::RelOrientation::PAGE_FRAME,
114 text::RelOrientation::FRAME,
115 text::RelOrientation::CHAR,
118 for ( int i = 0; i < 4; i++ )
120 if ( pHoriRelValues[i] == sal_uInt32( nIntValue ) )
121 m_nRelation = pHoriRelations[i];
124 break;
125 default:
126 #ifdef DEBUG_DOMAINMAPPER
127 dmapper_logger->element("unhandled");
128 #endif
129 break;
133 void PositionHandler::lcl_sprm( Sprm& )
137 sal_Int16 PositionHandler::orientation() const
139 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 if( m_nOrient == text::VertOrientation::TOP )
143 return text::VertOrientation::BOTTOM;
144 else if( m_nOrient == text::VertOrientation::BOTTOM )
145 return text::VertOrientation::TOP;
147 return m_nOrient;
150 sal_Int16 PositionHandler::relation() const
152 return m_nRelation;
155 sal_Int32 PositionHandler::position() const
157 return m_nPosition;
160 void PositionHandler::setPositionOffset(const OUString & sText, bool vertical)
162 if( vertical )
163 savedPositionOffsetV = ConversionHelper::convertEMUToMM100( sText.toInt32());
164 else
165 savedPositionOffsetH = ConversionHelper::convertEMUToMM100( sText.toInt32());
168 void PositionHandler::setAlignH(const OUString & sText)
170 if( sText == "left")
171 savedAlignH = text::HoriOrientation::LEFT;
172 else if( sText == "right" )
173 savedAlignH = text::HoriOrientation::RIGHT;
174 else if( sText == "center" )
175 savedAlignH = text::HoriOrientation::CENTER;
176 else if( sText == "inside" )
177 savedAlignH = text::HoriOrientation::INSIDE;
178 else if( sText == "outside" )
179 savedAlignH = text::HoriOrientation::OUTSIDE;
182 void PositionHandler::setAlignV(const OUString & sText)
184 if( sText == "top" )
185 savedAlignV = text::VertOrientation::TOP;
186 else if( sText == "bottom" )
187 savedAlignV = text::VertOrientation::BOTTOM;
188 else if( sText == "center" )
189 savedAlignV = text::VertOrientation::CENTER;
190 else if( sText == "inside" )
191 savedAlignV = text::VertOrientation::NONE;
192 else if( sText == "outside" )
193 savedAlignV = text::VertOrientation::NONE;
196 WrapHandler::WrapHandler( ) :
197 LoggedProperties(dmapper_logger, "WrapHandler"),
198 m_nType( 0 ),
199 m_nSide( 0 )
203 WrapHandler::~WrapHandler( )
207 void WrapHandler::lcl_attribute( Id aName, Value& rVal )
209 switch ( aName )
211 case NS_ooxml::LN_CT_Wrap_type:
212 m_nType = sal_Int32( rVal.getInt( ) );
213 break;
214 case NS_ooxml::LN_CT_Wrap_side:
215 m_nSide = sal_Int32( rVal.getInt( ) );
216 break;
217 default:;
221 void WrapHandler::lcl_sprm( Sprm& )
225 sal_Int32 WrapHandler::getWrapMode( )
227 // The wrap values do not map directly to our wrap mode,
228 // e.g. none in .docx actually means through in LO.
229 sal_Int32 nMode = com::sun::star::text::WrapTextMode_THROUGHT;
231 switch ( m_nType )
233 case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapType_square:
234 // through and tight are somewhat complicated, approximate
235 case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapType_tight:
236 case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapType_through:
238 switch ( m_nSide )
240 case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapSide_left:
241 nMode = com::sun::star::text::WrapTextMode_LEFT;
242 break;
243 case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapSide_right:
244 nMode = com::sun::star::text::WrapTextMode_RIGHT;
245 break;
246 default:
247 nMode = com::sun::star::text::WrapTextMode_PARALLEL;
250 break;
251 case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapType_topAndBottom:
252 nMode = com::sun::star::text::WrapTextMode_NONE;
253 break;
254 case NS_ooxml::LN_Value_vml_wordprocessingDrawing_ST_WrapType_none:
255 default:
256 nMode = com::sun::star::text::WrapTextMode_THROUGHT;
259 return nMode;
263 void GraphicZOrderHelper::addItem( uno::Reference< beans::XPropertySet > props, sal_Int32 relativeHeight )
265 items[ relativeHeight ] = props;
268 // The relativeHeight value in .docx is an arbitrary number, where only the relative ordering matters.
269 // But in Writer, the z-order is index in 0..(numitems-1) range, so whenever a new item needs to be
270 // added in the proper z-order, it is necessary to find the proper index.
271 sal_Int32 GraphicZOrderHelper::findZOrder( sal_Int32 relativeHeight )
273 Items::const_iterator it = items.begin();
274 while( it != items.end())
276 // std::map is iterated sorted by key
277 // if there is an item that has the same z-order, we belong under it
278 if( it->first >= relativeHeight )
279 break; // this is the first one higher, we belong right before it
280 else
281 ++it;
283 if( it == items.end()) // we're topmost
285 if( items.empty())
286 return 0;
287 sal_Int32 itemZOrder(0);
288 --it;
289 if( it->second->getPropertyValue(PropertyNameSupplier::GetPropertyNameSupplier()
290 .GetName( PROP_Z_ORDER )) >>= itemZOrder )
291 return itemZOrder + 1; // after the topmost
293 else
295 sal_Int32 itemZOrder(0);
296 if( it->second->getPropertyValue(PropertyNameSupplier::GetPropertyNameSupplier()
297 .GetName( PROP_Z_ORDER )) >>= itemZOrder )
298 return itemZOrder; // before the item
300 SAL_WARN( "writerfilter", "findZOrder() didn't find item z-order" );
301 return 0; // this should not(?) happen
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */