1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <com/sun/star/io/XOutputStream.hpp>
#include <tools/debug.hxx>
#include <sax/tools/converter.hxx>
#include <xmloff/xmltkmap.hxx>
#include <xmloff/xmluconv.hxx>
#include "xmloff/xmlnmspe.hxx"
#include <xmloff/xmltoken.hxx>
#include <xmloff/xmlimp.hxx>
#include <xmloff/nmspmap.hxx>
#include <xmloff/XMLBase64ImportContext.hxx>
#include "XMLBackgroundImageContext.hxx"
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::style;
using namespace ::com::sun::star::io;
using namespace ::xmloff::token;
enum SvXMLTokenMapAttrs
{
XML_TOK_BGIMG_HREF,
XML_TOK_BGIMG_TYPE,
XML_TOK_BGIMG_ACTUATE,
XML_TOK_BGIMG_SHOW,
XML_TOK_BGIMG_POSITION,
XML_TOK_BGIMG_REPEAT,
XML_TOK_BGIMG_FILTER,
XML_TOK_BGIMG_OPACITY,
XML_TOK_NGIMG_END=XML_TOK_UNKNOWN
};
static const SvXMLTokenMapEntry* lcl_getBGImgAttributesAttrTokenMap()
{
static SvXMLTokenMapEntry aBGImgAttributesAttrTokenMap[] =
{
{ XML_NAMESPACE_XLINK, XML_HREF, XML_TOK_BGIMG_HREF },
{ XML_NAMESPACE_XLINK, XML_TYPE, XML_TOK_BGIMG_TYPE },
{ XML_NAMESPACE_XLINK, XML_ACTUATE, XML_TOK_BGIMG_ACTUATE },
{ XML_NAMESPACE_XLINK, XML_SHOW, XML_TOK_BGIMG_SHOW },
{ XML_NAMESPACE_STYLE, XML_POSITION, XML_TOK_BGIMG_POSITION },
{ XML_NAMESPACE_STYLE, XML_REPEAT, XML_TOK_BGIMG_REPEAT },
{ XML_NAMESPACE_STYLE, XML_FILTER_NAME, XML_TOK_BGIMG_FILTER },
{ XML_NAMESPACE_DRAW, XML_OPACITY, XML_TOK_BGIMG_OPACITY },
XML_TOKEN_MAP_END
};
return aBGImgAttributesAttrTokenMap;
}
static SvXMLEnumMapEntry psXML_BrushHoriPos[] =
{
{ XML_LEFT, GraphicLocation_LEFT_MIDDLE },
{ XML_RIGHT, GraphicLocation_RIGHT_MIDDLE },
{ XML_TOKEN_INVALID, 0 }
};
static SvXMLEnumMapEntry psXML_BrushVertPos[] =
{
{ XML_TOP, GraphicLocation_MIDDLE_TOP },
{ XML_BOTTOM, GraphicLocation_MIDDLE_BOTTOM },
{ XML_TOKEN_INVALID, 0 }
};
static void lcl_xmlbic_MergeHoriPos( GraphicLocation& ePos,
GraphicLocation eHori )
{
DBG_ASSERT( GraphicLocation_LEFT_MIDDLE==eHori ||
GraphicLocation_MIDDLE_MIDDLE==eHori ||
GraphicLocation_RIGHT_MIDDLE==eHori,
"lcl_xmlbic_MergeHoriPos: vertical pos must be middle" );
switch( ePos )
{
case GraphicLocation_LEFT_TOP:
case GraphicLocation_MIDDLE_TOP:
case GraphicLocation_RIGHT_TOP:
ePos = GraphicLocation_LEFT_MIDDLE==eHori
? GraphicLocation_LEFT_TOP
: (GraphicLocation_MIDDLE_MIDDLE==eHori
? GraphicLocation_MIDDLE_TOP
: GraphicLocation_RIGHT_TOP);
break;
case GraphicLocation_LEFT_MIDDLE:
case GraphicLocation_MIDDLE_MIDDLE:
case GraphicLocation_RIGHT_MIDDLE:
ePos = eHori;
break;
case GraphicLocation_LEFT_BOTTOM:
case GraphicLocation_MIDDLE_BOTTOM:
case GraphicLocation_RIGHT_BOTTOM:
ePos = GraphicLocation_LEFT_MIDDLE==eHori
? GraphicLocation_LEFT_BOTTOM
: (GraphicLocation_MIDDLE_MIDDLE==eHori
? GraphicLocation_MIDDLE_BOTTOM
: GraphicLocation_RIGHT_BOTTOM);
break;
default:
break;
}
}
static void lcl_xmlbic_MergeVertPos( GraphicLocation& ePos,
GraphicLocation eVert )
{
DBG_ASSERT( GraphicLocation_MIDDLE_TOP==eVert ||
GraphicLocation_MIDDLE_MIDDLE==eVert ||
GraphicLocation_MIDDLE_BOTTOM==eVert,
"lcl_xmlbic_MergeVertPos: horizontal pos must be middle" );
switch( ePos )
{
case GraphicLocation_LEFT_TOP:
case GraphicLocation_LEFT_MIDDLE:
case GraphicLocation_LEFT_BOTTOM:
ePos = GraphicLocation_MIDDLE_TOP==eVert
? GraphicLocation_LEFT_TOP
: (GraphicLocation_MIDDLE_MIDDLE==eVert
? GraphicLocation_LEFT_MIDDLE
: GraphicLocation_LEFT_BOTTOM);
break;
case GraphicLocation_MIDDLE_TOP:
case GraphicLocation_MIDDLE_MIDDLE:
case GraphicLocation_MIDDLE_BOTTOM:
ePos = eVert;
break;
case GraphicLocation_RIGHT_TOP:
case GraphicLocation_RIGHT_MIDDLE:
case GraphicLocation_RIGHT_BOTTOM:
ePos = GraphicLocation_MIDDLE_TOP==eVert
? GraphicLocation_RIGHT_TOP
: (GraphicLocation_MIDDLE_MIDDLE==eVert
? GraphicLocation_RIGHT_MIDDLE
: GraphicLocation_RIGHT_BOTTOM);
break;
default:
break;
}
}
TYPEINIT1( XMLBackgroundImageContext, XMLElementPropertyContext );
void XMLBackgroundImageContext::ProcessAttrs(
const Reference< xml::sax::XAttributeList >& xAttrList )
{
SvXMLTokenMap aTokenMap( lcl_getBGImgAttributesAttrTokenMap() );
ePos = GraphicLocation_NONE;
sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
for( sal_Int16 i=0; i < nAttrCount; i++ )
{
const OUString& rAttrName = xAttrList->getNameByIndex( i );
OUString aLocalName;
sal_uInt16 nPrefix =
GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
&aLocalName );
const OUString& rValue = xAttrList->getValueByIndex( i );
switch( aTokenMap.Get( nPrefix, aLocalName ) )
{
case XML_TOK_BGIMG_HREF:
sURL = rValue;
if( GraphicLocation_NONE == ePos )
ePos = GraphicLocation_TILED;
break;
case XML_TOK_BGIMG_TYPE:
case XML_TOK_BGIMG_ACTUATE:
case XML_TOK_BGIMG_SHOW:
break;
case XML_TOK_BGIMG_POSITION:
{
GraphicLocation eNewPos = GraphicLocation_NONE, eTmp;
sal_uInt16 nTmp;
SvXMLTokenEnumerator aTokenEnum( rValue );
OUString aToken;
sal_Bool bHori = sal_False, bVert = sal_False;
sal_Bool bOK = sal_True;
while( bOK && aTokenEnum.getNextToken( aToken ) )
{
if( bHori && bVert )
{
bOK = sal_False;
}
else if( -1 != aToken.indexOf( sal_Unicode('%') ) )
{
sal_Int32 nPrc = 50;
if (::sax::Converter::convertPercent( nPrc, aToken ))
{
if( !bHori )
{
eNewPos = nPrc < 25
? GraphicLocation_LEFT_TOP
: (nPrc < 75 ? GraphicLocation_MIDDLE_MIDDLE
: GraphicLocation_RIGHT_BOTTOM);
bHori = sal_True;
}
else
{
eTmp = nPrc < 25
? GraphicLocation_LEFT_TOP
: (nPrc < 75 ? GraphicLocation_LEFT_MIDDLE
: GraphicLocation_LEFT_BOTTOM);
lcl_xmlbic_MergeVertPos( eNewPos, eTmp );
bVert = sal_True;
}
}
else
{
// wrong percentage
bOK = sal_False;
}
}
else if( IsXMLToken( aToken, XML_CENTER ) )
{
if( bHori )
lcl_xmlbic_MergeVertPos( eNewPos,
GraphicLocation_MIDDLE_MIDDLE );
else if ( bVert )
lcl_xmlbic_MergeHoriPos( eNewPos,
GraphicLocation_MIDDLE_MIDDLE );
else
eNewPos = GraphicLocation_MIDDLE_MIDDLE;
}
else if( SvXMLUnitConverter::convertEnum( nTmp, aToken,
psXML_BrushHoriPos ) )
{
if( bVert )
lcl_xmlbic_MergeHoriPos( eNewPos,
(GraphicLocation)nTmp );
else if( !bHori )
eNewPos = (GraphicLocation)nTmp;
else
bOK = sal_False;
bHori = sal_True;
}
else if( SvXMLUnitConverter::convertEnum( nTmp, aToken,
psXML_BrushVertPos ) )
{
if( bHori )
lcl_xmlbic_MergeVertPos( eNewPos,
(GraphicLocation)nTmp );
else if( !bVert )
eNewPos = (GraphicLocation)nTmp;
else
bOK = sal_False;
bVert = sal_True;
}
else
{
bOK = sal_False;
}
}
bOK &= GraphicLocation_NONE != eNewPos;
if( bOK )
ePos = eNewPos;
}
break;
case XML_TOK_BGIMG_REPEAT:
{
sal_uInt16 nPos = GraphicLocation_NONE;
static SvXMLEnumMapEntry psXML_BrushRepeat[] =
{
{ XML_BACKGROUND_REPEAT, GraphicLocation_TILED },
{ XML_BACKGROUND_NO_REPEAT, GraphicLocation_MIDDLE_MIDDLE },
{ XML_BACKGROUND_STRETCH, GraphicLocation_AREA },
{ XML_TOKEN_INVALID, 0 }
};
if( SvXMLUnitConverter::convertEnum( nPos, rValue,
psXML_BrushRepeat ) )
{
if( GraphicLocation_MIDDLE_MIDDLE != nPos ||
GraphicLocation_NONE == ePos ||
GraphicLocation_AREA == ePos ||
GraphicLocation_TILED == ePos )
ePos = (GraphicLocation)nPos;
}
}
break;
case XML_TOK_BGIMG_FILTER:
sFilter = rValue;
break;
case XML_TOK_BGIMG_OPACITY:
{
sal_Int32 nTmp;
// convert from percent and clip
if (::sax::Converter::convertPercent( nTmp, rValue ))
{
if( (nTmp >= 0) && (nTmp <= 100) )
nTransparency = static_cast<sal_Int8>( 100-nTmp );
}
}
break;
}
}
}
XMLBackgroundImageContext::XMLBackgroundImageContext(
SvXMLImport& rImport, sal_uInt16 nPrfx,
const OUString& rLName,
const Reference< xml::sax::XAttributeList > & xAttrList,
const XMLPropertyState& rProp,
sal_Int32 nPosIdx,
sal_Int32 nFilterIdx,
sal_Int32 nTransparencyIdx,
::std::vector< XMLPropertyState > &rProps ) :
XMLElementPropertyContext( rImport, nPrfx, rLName, rProp, rProps ),
aPosProp( nPosIdx ),
aFilterProp( nFilterIdx ),
aTransparencyProp( nTransparencyIdx ),
nTransparency( 0 )
{
ProcessAttrs( xAttrList );
}
XMLBackgroundImageContext::~XMLBackgroundImageContext()
{
}
SvXMLImportContext *XMLBackgroundImageContext::CreateChildContext(
sal_uInt16 nPrefix, const OUString& rLocalName,
const Reference< xml::sax::XAttributeList > & xAttrList )
{
SvXMLImportContext *pContext = NULL;
if( (XML_NAMESPACE_OFFICE == nPrefix) &&
xmloff::token::IsXMLToken( rLocalName,
xmloff::token::XML_BINARY_DATA ) )
{
if( sURL.isEmpty() && !xBase64Stream.is() )
{
xBase64Stream = GetImport().GetStreamForGraphicObjectURLFromBase64();
if( xBase64Stream.is() )
pContext = new XMLBase64ImportContext( GetImport(), nPrefix,
rLocalName, xAttrList,
xBase64Stream );
}
}
if( !pContext )
{
pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
}
return pContext;
}
void XMLBackgroundImageContext::EndElement()
{
if( !sURL.isEmpty() )
{
sURL = GetImport().ResolveGraphicObjectURL( sURL, sal_False );
}
else if( xBase64Stream.is() )
{
sURL = GetImport().ResolveGraphicObjectURLFromBase64( xBase64Stream );
xBase64Stream = 0;
}
if( sURL.isEmpty() )
ePos = GraphicLocation_NONE;
else if( GraphicLocation_NONE == ePos )
ePos = GraphicLocation_TILED;
aProp.maValue <<= sURL;
aPosProp.maValue <<= ePos;
aFilterProp.maValue <<= sFilter;
aTransparencyProp.maValue <<= nTransparency;
SetInsert( sal_True );
XMLElementPropertyContext::EndElement();
if( -1 != aPosProp.mnIndex )
rProperties.push_back( aPosProp );
if( -1 != aFilterProp.mnIndex )
rProperties.push_back( aFilterProp );
if( -1 != aTransparencyProp.mnIndex )
rProperties.push_back( aTransparencyProp );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|