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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 #include "XMLClipPropertyHandler.hxx"
27 #include <com/sun/star/uno/Any.hxx>
28 #include <rtl/ustrbuf.hxx>
29 #include <com/sun/star/text/GraphicCrop.hpp>
30 #include <xmloff/xmluconv.hxx>
31 #include <xmloff/xmltoken.hxx>
32 
33 using ::rtl::OUString;
34 using ::rtl::OUStringBuffer;
35 
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::text;
39 using namespace ::xmloff::token;
40 
41 ///////////////////////////////////////////////////////////////////////////////
42 //
43 // class XMLMeasurePropHdl
44 //
45 
XMLClipPropertyHandler(sal_Bool bODF11)46 XMLClipPropertyHandler::XMLClipPropertyHandler( sal_Bool bODF11 ) :
47 	m_bODF11( bODF11 )
48 {
49 }
50 
~XMLClipPropertyHandler()51 XMLClipPropertyHandler::~XMLClipPropertyHandler()
52 {
53 	// nothing to do
54 }
55 
equals(const Any & r1,const Any & r2) const56 bool XMLClipPropertyHandler::equals(
57 		const Any& r1,
58 		const Any& r2 ) const
59 {
60 	GraphicCrop aCrop1, aCrop2;
61 	r1 >>= aCrop1;
62 	r2 >>= aCrop2;
63 
64 	return aCrop1.Top == aCrop2.Top &&
65 		   aCrop1.Bottom == aCrop2.Bottom &&
66 		   aCrop1.Left == aCrop2.Left &&
67 		   aCrop1.Right == aCrop2.Right;
68 }
69 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter & rUnitConverter) const70 sal_Bool XMLClipPropertyHandler::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
71 {
72 	sal_Bool bRet = sal_False;
73 	sal_Int32 nLen = rStrImpValue.getLength();
74 	if( nLen > 6 &&
75 		0 == rStrImpValue.compareTo( GetXMLToken(XML_RECT), 4 ) &&
76 		rStrImpValue[4] == '(' &&
77 		rStrImpValue[nLen-1] == ')' )
78 	{
79 		GraphicCrop aCrop;
80 		OUString sTmp( rStrImpValue.copy( 5, nLen-6 ) );
81 
82 		sal_Bool bHasComma = sTmp.indexOf( ',' ) != -1;
83 		SvXMLTokenEnumerator aTokenEnum( sTmp, bHasComma ? ',' : ' ' );
84 
85 		sal_uInt16 nPos = 0;
86 		OUString aToken;
87 		while( aTokenEnum.getNextToken( aToken ) )
88 		{
89 			sal_Int32 nVal = 0;
90 			if( !IsXMLToken(aToken, XML_AUTO) &&
91 			 	!rUnitConverter.convertMeasure( nVal, aToken ) )
92 				break;
93 
94 			switch( nPos )
95 			{
96 			case 0: aCrop.Top = nVal;	break;
97 			case 1: aCrop.Right = nVal;	break;
98 			case 2: aCrop.Bottom = nVal;	break;
99 			case 3: aCrop.Left = nVal;	break;
100 			}
101 			nPos++;
102 		}
103 
104 		bRet = (4 == nPos );
105 		if( bRet )
106 			rValue <<= aCrop;
107 	}
108 
109 	return bRet;
110 }
111 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter & rUnitConverter) const112 sal_Bool XMLClipPropertyHandler::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
113 {
114 	sal_Bool bRet = sal_False;
115   	OUStringBuffer aOut(30);
116 	GraphicCrop aCrop;
117 
118 	if( rValue >>= aCrop )
119 	{
120 		aOut.append( GetXMLToken(XML_RECT) );
121 		aOut.append( (sal_Unicode)'(' );
122 		rUnitConverter.convertMeasure( aOut, aCrop.Top );
123 		if( !m_bODF11 )
124 			aOut.append( (sal_Unicode)',' );
125 		aOut.append( (sal_Unicode)' ' );
126 		rUnitConverter.convertMeasure( aOut, aCrop.Right );
127 		if( !m_bODF11 )
128 			aOut.append( (sal_Unicode)',' );
129 		aOut.append( (sal_Unicode)' ' );
130 		rUnitConverter.convertMeasure( aOut, aCrop.Bottom );
131 		if( !m_bODF11 )
132 			aOut.append( (sal_Unicode)',' );
133 		aOut.append( (sal_Unicode)' ' );
134 		rUnitConverter.convertMeasure( aOut, aCrop.Left );
135 		aOut.append( (sal_Unicode)')' );
136 		rStrExpValue = aOut.makeStringAndClear();
137 
138 		bRet = sal_True;
139 	}
140 
141 	return bRet;
142 }
143